wxrealnuminput.h

00001 /*************************************************************************************
00002  * MechSys - A C++ library to simulate (Continuum) Mechanical Systems                *
00003  * Copyright (C) 2005 Dorival de Moraes Pedroso <dorival.pedroso at gmail.com>       *
00004  * Copyright (C) 2005 Raul Dario Durand Farfan  <raul.durand at gmail.com>           *
00005  *                                                                                   *
00006  * This file is part of MechSys.                                                     *
00007  *                                                                                   *
00008  * MechSys is free software; you can redistribute it and/or modify it under the      *
00009  * terms of the GNU General Public License as published by the Free Software         *
00010  * Foundation; either version 2 of the License, or (at your option) any later        *
00011  * version.                                                                          *
00012  *                                                                                   *
00013  * MechSys is distributed in the hope that it will be useful, but WITHOUT ANY        *
00014  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A   *
00015  * PARTICULAR PURPOSE. See the GNU General Public License for more details.          *
00016  *                                                                                   *
00017  * You should have received a copy of the GNU General Public License along with      *
00018  * MechSys; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, *
00019  * Fifth Floor, Boston, MA 02110-1301, USA                                           *
00020  *************************************************************************************/
00021 
00022 #ifndef MECHSYS_WXREALNUMINPUT_H
00023 #define MECHSYS_WXREALNUMINPUT_H
00024 
00025 #ifdef HAVE_CONFIG_H
00026   #include "config.h"
00027 #else
00028   #ifndef REAL
00029     #define REAL double
00030   #endif
00031 #endif
00032 
00033 #include <wx/wx.h>
00034 
00035 // ---------------------------- WxRealNumInput_Event ------------------------------
00036 
00037 class WxRealNumInput_Event : public wxNotifyEvent
00038 {
00039 public:
00040     // Constructor
00041     WxRealNumInput_Event(wxEventType CmdType=wxEVT_NULL, int Id=0)
00042         : wxNotifyEvent(CmdType, Id)
00043     {}
00044     // Copy constructor
00045     WxRealNumInput_Event(WxRealNumInput_Event const & Event)
00046         : wxNotifyEvent(Event)
00047     {}
00048     // Clone function
00049     virtual wxEvent * Clone() const { return new WxRealNumInput_Event(*this); }
00050     // Declare dynamic class
00051     DECLARE_DYNAMIC_CLASS(WxRealNumInput_Event);
00052     
00053     // Methods
00054     void SetValue (REAL Val)       { _value = Val;  }
00055     REAL GetValue ()         const { return _value; }
00056 
00057 private:
00058     // Data
00059     REAL _value;
00060 }; // class WxRealNumInput_Event
00061 
00062 // --- 801 --- DECLARE and IMPLEMENT WxRealNumInput_Event {{{
00063 
00064 // The typedef
00065 typedef void (wxEvtHandler::*WxRealNumInput_Event_Fun)(WxRealNumInput_Event&);
00066 
00067 // Events
00068 BEGIN_DECLARE_EVENT_TYPES()
00069     DECLARE_EVENT_TYPE(WxEVT_REALNUM_CHANGED, 801)
00070 END_DECLARE_EVENT_TYPES()
00071 
00072 // Macros for handling events
00073 #define EVT_REALNUM_CHANGED(id, fn)                                               \
00074     DECLARE_EVENT_TABLE_ENTRY                                                     \
00075     (                                                                             \
00076         WxEVT_REALNUM_CHANGED                                                   , \
00077         id                                                                      , \
00078         -1                                                                      , \
00079         (wxObjectEventFunction)(wxEventFunction)(WxRealNumInput_Event_Fun) & fn , \
00080         (wxObject*) NULL                                                          \
00081     ),
00082 
00083 // Implementation
00084 DEFINE_EVENT_TYPE       (WxEVT_REALNUM_CHANGED)
00085 IMPLEMENT_DYNAMIC_CLASS (WxRealNumInput_Event, wxNotifyEvent)
00086 
00087 // }}}
00088 
00089 // ------------------------------- WxRealNumInput ---------------------------------
00090 
00091 class WxRealNumInput : public wxTextCtrl
00092 {
00093 public:
00094     // Constructor
00095     WxRealNumInput(wxWindow * Parent, wxWindowID Id, const wxString & Str, const wxPoint & Pos, const wxSize & Size, int Style=0)
00096         : wxTextCtrl(Parent, Id, Str, Pos, Size, Style|wxTE_PROCESS_ENTER)
00097     {}
00098 
00099     // Events
00100     void OnChar      (wxKeyEvent     & Event);
00101     void OnSetFocus  (wxFocusEvent   & Event);
00102     void OnKillFocus (wxFocusEvent   & Event);
00103     void OnTextEnter (wxCommandEvent & Event); 
00104 
00105 private:
00106     // Event table
00107     DECLARE_EVENT_TABLE()
00108     // Data
00109     wxString _buffer;
00110     // Methods
00111     bool _validate_value             (REAL & TmpValue);
00112     void _send_realnum_changed_event (REAL Val);
00113 }; // class WxRealInput
00114 
00115 
00117 
00118 
00119 // ------------------------------- WxRealNumInput ---------------------------------
00120 
00121 inline void WxRealNumInput::OnChar(wxKeyEvent & Event) // {{{
00122 {
00123     if (!isalpha(Event.GetKeyCode())) Event.Skip(); // Input is ok, so skip to main loop
00124 } // }}}
00125 
00126 inline void WxRealNumInput::OnSetFocus(wxFocusEvent & Event) // {{{
00127 {
00128     _buffer = GetValue(); // save a buffered value
00129 } // }}}
00130 
00131 inline void WxRealNumInput::OnKillFocus(wxFocusEvent & Event) // {{{
00132 {
00133     REAL temp;
00134     if (_validate_value(temp)) _send_realnum_changed_event(temp);
00135 } // }}}
00136 
00137 inline void WxRealNumInput::OnTextEnter(wxCommandEvent & Event) // {{{
00138 {
00139     REAL temp;
00140     if (_validate_value(temp)) _send_realnum_changed_event(temp);
00141 } // }}}
00142 
00143 inline bool WxRealNumInput::_validate_value(REAL & OutValue) // {{{
00144 {
00145     bool ok_and_changed = false;
00146     if (!GetValue().ToDouble(&OutValue)) SetValue(_buffer); // not valid, so restore buffered value
00147     else
00148     {
00149         if (GetValue()!=_buffer) // really changed ?
00150         {
00151             REAL buffer_temp;
00152             if (_buffer.ToDouble(&buffer_temp))
00153             {
00154                 if (buffer_temp==OutValue) SetValue(_buffer); // did not change, just the format is different, ex.: 0 != 000
00155                 else ok_and_changed=true;
00156             }
00157         }
00158     }
00159     return ok_and_changed;
00160 } // }}}
00161 
00162 inline void WxRealNumInput::_send_realnum_changed_event(REAL Val) // {{{
00163 {
00164     WxRealNumInput_Event event(WxEVT_REALNUM_CHANGED, GetId());
00165     event.SetEventObject (this);
00166     event.SetValue       (Val);
00167     GetEventHandler()->ProcessEvent(event);
00168 } // }}}
00169 
00170 BEGIN_EVENT_TABLE(WxRealNumInput, wxTextCtrl) // {{{
00171     EVT_CHAR       (           WxRealNumInput::OnChar      )
00172     EVT_SET_FOCUS  (           WxRealNumInput::OnSetFocus  )
00173     EVT_KILL_FOCUS (           WxRealNumInput::OnKillFocus )
00174     EVT_TEXT_ENTER ( wxID_ANY, WxRealNumInput::OnTextEnter )
00175 END_EVENT_TABLE() // }}}
00176 
00177 #endif // MECHSYS_WXREALNUMINPUT_H
00178 
00179 // vim:fdm=marker

Generated on Wed Jan 24 15:56:26 2007 for MechSys by  doxygen 1.4.7