]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxSpinCtrl.cxx
Clean Code
[bbtk.git] / packages / wx / src / bbwxSpinCtrl.cxx
1 //===== 
2 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
3 //===== 
4 #include "bbwxSpinCtrl.h"
5 #include "bbwxPackage.h"
6
7 #include <wx/spinbutt.h>
8 #include <wx/spinctrl.h>
9
10 namespace bbwx
11 {
12
13
14
15   //--------------------------------------------------------------------------
16   // The widget created by the box 
17   class SpinCtrlWidget : public wxPanel 
18   {
19   public:
20     /// Ctor with the two first params the parent window and the creator box
21     /// which must be passed to the WxBlackBoxWidget constructor.
22     /// The other params initialize the widget 
23     SpinCtrlWidget(SpinCtrl* box, wxWindow *parent,
24                  wxString title, int reactiveOnKeyStroke );
25     /// Dtor
26     ~SpinCtrlWidget();
27     /// Events callbacks
28     /// Called when the box is clicked
29     void OnSpinCtrlClick(wxCommandEvent& event);
30
31     // Accessors
32     bool GetValue() { return mwxSpinCtrl->GetValue(); }
33     void SetValue(int  min, int max, int selected);
34     // Update the texts which display the min/max/current values of the slider
35         
36   private:
37     SpinCtrl*       mBox;
38     wxSpinCtrl          *mwxSpinCtrl;
39     wxSpinButton        *mwxSpinButton;
40     int                 _reactiveOnKeyStroke;
41     int                 selected;
42   };
43   //------------------------------------------------------------------------
44   //------------------------------------------------------------------------
45   //------------------------------------------------------------------------
46
47   
48     
49   //-------------------------------------------------------------------------
50   SpinCtrlWidget::SpinCtrlWidget(SpinCtrl* box, wxWindow *parent,
51                              wxString title,
52                               int reactiveOnKeyStroke)
53     :  
54     wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
55     mBox(box),
56     _reactiveOnKeyStroke(reactiveOnKeyStroke)
57   {
58     wxPanel * panel = this;
59     //---------------------------------------------------------------------
60     // 1) Creation of the components of the widget
61     // Any top level sub-widget must have the panel returned by panel
62     // for parent
63
64
65     wxFlexGridSizer *sizer;
66     sizer       = new wxFlexGridSizer(1);
67
68     
69     mwxSpinCtrl         = NULL;
70     mwxSpinButton       = NULL;
71     
72         if (mBox->bbGetInputTitle()!="")
73         {
74                 sizer->Add( new wxStaticText(panel , -1,  bbtk::std2wx(mBox->bbGetInputTitle())  ) );           
75         }
76     
77         if (mBox->bbGetInputType()==0)
78         {
79             mwxSpinCtrl = new wxSpinCtrl( panel, -1 );      
80                 // Connecting events to callbacks
81                 Connect( mwxSpinCtrl->GetId(), 
82                          wxEVT_COMMAND_SPINCTRL_UPDATED, 
83                          (wxObjectEventFunction) 
84                          (void (wxPanel::*)(wxScrollEvent&))
85                          &SpinCtrlWidget::OnSpinCtrlClick);
86                 sizer->Add( mwxSpinCtrl );
87         } else {
88             mwxSpinButton = new wxSpinButton( panel, -1 );      
89                 // Connecting events to callbacks
90                 Connect( mwxSpinButton->GetId(), 
91                          wxEVT_SPIN, 
92                          (wxObjectEventFunction) 
93                          (void (wxPanel::*)(wxScrollEvent&))
94                          &SpinCtrlWidget::OnSpinCtrlClick);
95                 sizer->Add( mwxSpinButton );
96         }// if Type
97               
98     //---------------------------------------------------------------------
99
100     //---------------------------------------------------------------------
101     // 2) Insertion of the components in the window
102     
103     // We use a FlexGridSizer
104     panel->SetSizer(sizer);
105   }
106   //-------------------------------------------------------------------------
107   
108
109   //-------------------------------------------------------------------------
110   SpinCtrlWidget::~SpinCtrlWidget()
111   {
112   }
113   //-------------------------------------------------------------------------
114
115
116   //-------------------------------------------------------------------------
117
118
119
120   //-------------------------------------------------------------------------
121   void SpinCtrlWidget::OnSpinCtrlClick(wxCommandEvent& event)
122   {
123           // When user clicks the box 
124           // we update the output of the box
125
126         if (mBox->bbGetInputType()==0)
127         {
128           mBox->bbSetOutputOut( mwxSpinCtrl->GetValue() );
129         } else {
130           mBox->bbSetOutputOut( mwxSpinButton->GetValue() );
131         }// if Type
132
133           // and signal that the output has changed
134           //if(_reactiveOnKeyStroke==1){
135           mBox->bbSignalOutputModification(std::string("Out"));
136           //}
137   }
138   //-------------------------------------------------------------------------
139
140   //-------------------------------------------------------------------------
141   
142
143   //-------------------------------------------------------------------------
144   void SpinCtrlWidget::SetValue(int min, int max, int selected)
145   {
146         if (mBox->bbGetInputType()==0)
147         {
148             mwxSpinCtrl->SetRange( min,max );
149             mwxSpinCtrl->SetValue( selected );
150         } else {
151             mwxSpinButton->SetRange( min,max );
152             mwxSpinButton->SetValue( selected );
153         }// if Type
154   }
155   //-------------------------------------------------------------------------
156
157
158 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,SpinCtrl)
159 BBTK_BLACK_BOX_IMPLEMENTATION(SpinCtrl,bbtk::WxBlackBox);
160 //===== 
161 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
162 //===== 
163 void SpinCtrl::Process()
164 {
165                 // if with label
166                 ((SpinCtrlWidget*)bbGetOutputWidget())->SetValue( bbGetInputMin() , bbGetInputMax(), bbGetInputSelected() );    
167                 bbSetOutputOut( bbGetInputSelected() );
168 }
169 //===== 
170 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
171 //===== 
172 void SpinCtrl::CreateWidget(wxWindow* parent)
173 {
174
175                 SpinCtrlWidget *spinctrlwidget=new  SpinCtrlWidget( this , parent, bbGetInputTitle() ,1);
176                 bbSetOutputWidget( spinctrlwidget );
177                 spinctrlwidget->SetValue( bbGetInputMin() , bbGetInputMax(), bbGetInputSelected() );    
178
179 }
180 //===== 
181 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
182 //===== 
183 void SpinCtrl::bbUserSetDefaultValues()
184 {
185         bbSetInputMin(0);
186         bbSetInputMax(100);
187         bbSetInputSelected(0);
188         bbSetInputType(0);
189         bbSetInputTitle("");    
190
191 }
192 //===== 
193 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
194 //===== 
195 void SpinCtrl::bbUserInitializeProcessing()
196 {
197 }
198 //===== 
199 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
200 //===== 
201 void SpinCtrl::bbUserFinalizeProcessing()
202 {
203 }
204
205 } // EO namespace bbwx
206
207