]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxComboBox.cxx
#3465 Spin in combobox Box and FilterString (find strings)
[bbtk.git] / packages / wx / src / bbwxComboBox.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 "bbwxComboBox.h"
5 #include "bbwxPackage.h"
6
7
8 #include <vector>
9 #include <string>
10
11 #include <wx/choice.h>
12 #include <wx/control.h>
13 #include <wx/listbox.h>
14
15 #include <wx/spinbutt.h>
16
17 namespace bbwx
18 {
19   //--------------------------------------------------------------------------
20   // The widget created by the box 
21   class ComboBoxWidget : public wxPanel
22   {
23   public:
24         ComboBoxWidget( ComboBox* box, wxWindow *parent, 
25                     int iSelection, 
26                     std::string title, 
27                     std::vector< std::string > lstIn, 
28                                         int typeForm, 
29                                         int sizeX, int sizeY );
30         ~ComboBoxWidget();
31         void OnComboBoxSelection(int iSelection);
32         void OnComboBox(wxEvent& event);
33         void FillItems( int iSelection, std::vector< std::string > lstIn);
34     void VerifyDeselect(int iSelection);
35
36     void OnSpinCtrlClick(wxCommandEvent& event);
37
38
39   private:
40     int                         mTypeForm;
41     ComboBox            *mBox;
42         wxItemContainer *itemcontainer;
43         wxSpinButton    *mwxspinbutton;
44   };
45   
46
47
48   //------------------------------------------------------------------------
49   //------------------------------------------------------------------------
50   //------------------------------------------------------------------------
51
52   //-------------------------------------------------------------------------
53   ComboBoxWidget::ComboBoxWidget( ComboBox* box,
54             wxWindow *parent,
55             int iSelection,
56             std::string title,
57             std::vector< std::string > lstIn,
58                         int typeForm,
59                         int sizeX, 
60                         int sizeY
61                 )
62     :
63     wxPanel( parent, -1,wxDefaultPosition ) ,
64     mBox(box),
65     mTypeForm(typeForm)
66   {
67         mwxspinbutton                   = NULL;
68         itemcontainer                   = NULL;
69         wxPanel                 *panel  = this;
70         int                     i;
71     //---------------------------------------------------------------------
72     // 2) Insertion of the components in the window
73     
74     // We use a FlexGridSizer
75     wxFlexGridSizer *sizer      = new wxFlexGridSizer(2);
76     if (title!="")
77     {
78            sizer->Add( new wxStaticText(panel,-1,  bbtk::std2wx(title) ) ); 
79     }
80     //---------------------------------------------------------------------
81         if (mTypeForm==1)
82         {
83                 wxListBox *wxlistbox = new wxListBox ( panel , -1 , wxDefaultPosition,wxSize(sizeX,sizeY),0, NULL, wxLB_SINGLE );       
84                 itemcontainer=wxlistbox;
85                 sizer->Add( wxlistbox,1,wxEXPAND ); 
86                 Connect( wxlistbox->GetId(), 
87                  wxEVT_COMMAND_LISTBOX_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox );                  
88         } else if (mTypeForm==0) {
89                 wxChoice *wxchoice = new wxChoice ( panel , -1, wxDefaultPosition,wxSize(sizeX,sizeY));
90                 itemcontainer=wxchoice;
91                 sizer->Add( wxchoice,1,wxGROW ); 
92                 Connect( wxchoice->GetId(), 
93                  wxEVT_COMMAND_CHOICE_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox ); 
94         }
95         
96         if (mBox->bbGetInputWithSpinButton()==true)
97         {
98                 mwxspinbutton = new wxSpinButton( panel, -1 );    
99                 Connect( mwxspinbutton->GetId(), 
100                          wxEVT_SPIN, 
101                          (wxObjectEventFunction) 
102                          (void (wxPanel::*)(wxScrollEvent&))
103                          &ComboBoxWidget::OnSpinCtrlClick);
104   
105                 sizer->Add( mwxspinbutton,1,wxEXPAND ); 
106         }
107
108     sizer->AddGrowableCol(0);
109     panel->SetSizer(sizer);
110         FillItems( iSelection,lstIn );
111         
112 //      for (i=0;i<lstIn.size(); i++)
113 //      {
114 //              itemcontainer->Append(  bbtk::std2wx( lstIn[i] )  ); 
115 //      } // for i
116 //      itemcontainer->SetSelection(iSelection);
117   }
118
119   //-------------------------------------------------------------------------  
120   ComboBoxWidget::~ComboBoxWidget()
121   {
122   }
123
124   //--------------------------------------------------------------------------
125   void ComboBoxWidget::VerifyDeselect(int iSelection)
126   {
127         if ((iSelection>=0) && (mBox->bbGetInputDeselect()==true) )
128         {
129                 if (mTypeForm==1) 
130                 { 
131                         ((wxListBox*)itemcontainer)->Deselect( iSelection ); 
132                 } // if mTypeForm
133         } // if iSelection
134   }
135
136   //--------------------------------------------------------------------------
137   void ComboBoxWidget::OnComboBoxSelection(int iSelection)
138   {
139         if (iSelection>=0) 
140         {
141                 mBox->bbSetInputSelection( iSelection );
142                 mBox->bbSetOutputOut( iSelection );
143
144 // Patch to clean the spaces at the begining 
145                 std::string tmpStr = bbtk::wx2std( itemcontainer->GetString(iSelection) );
146                 if (tmpStr.length()>0) { while(tmpStr[0]==' ') tmpStr.erase(0,1); }
147                 mBox->bbSetOutputOutString(   tmpStr   );
148 //              mBox->bbSetOutputOutString(   bbtk::wx2std( itemcontainer->GetString(iSelection) )   );
149
150                 mBox->bbSignalOutputModification();
151                 VerifyDeselect(iSelection);
152                 if (mTypeForm==0) 
153                 { 
154                         wxChoice *wxchoise=(wxChoice *)itemcontainer;
155                         wxchoise->SetToolTip( itemcontainer->GetString(iSelection)    );
156                 } // if mTypeForm
157
158
159 //              mBox->bbSignalOutputModification("Out");
160 //              mBox->bbSignalOutputModification("OutString");
161         } // if iSelection
162   }
163
164 //--------------------------------------------------------------------------
165 void ComboBoxWidget::OnComboBox(wxEvent& event)
166 {
167         int iSelection = itemcontainer->GetSelection();
168         OnComboBoxSelection( iSelection );
169         if (mBox->bbGetInputWithSpinButton()==true)
170         {
171                 mwxspinbutton->SetValue( iSelection );
172         }
173
174 }
175
176 //--------------------------------------------------------------------------
177 void ComboBoxWidget::OnSpinCtrlClick(wxCommandEvent& event)
178 {
179         int iSelection = mwxspinbutton->GetValue();
180         printf("EED ComboBoxWidget::OnSpinCtrlClick %d\n", iSelection );
181         itemcontainer->SetSelection(iSelection);
182         OnComboBoxSelection( iSelection );
183
184 /*
185         if (mBox->bbGetInputType()==0)
186         {
187           mBox->bbSetOutputOut( mwxSpinCtrl->GetValue() );
188         } else {
189           mBox->bbSetOutputOut( mwxSpinButton->GetValue() );
190         }// if Type
191
192           // and signal that the output has changed
193           //if(_reactiveOnKeyStroke==1){
194           mBox->bbSignalOutputModification(std::string("Out"));
195           //}
196 */        
197 }
198
199
200 //--------------------------------------------------------------------------
201 void ComboBoxWidget::FillItems( int iSelection, std::vector< std::string > lstIn )
202 {
203         int i,size=lstIn.size();
204
205
206 #if defined(_WIN32)
207         // Patch to put spaces at the beginin
208 #else
209         // Patch to put spaces at the beginin
210         int strLength=-1;
211         // Looking for the longest string
212         for (i=0 ;i<size; i++)
213         {       
214                 if ( strLength < (int) lstIn[i].length() ) { strLength=lstIn[i].length();  }
215         }       // for
216         // Adding spaces at the bigining to the others strings in the list to have the same size
217         int ii,len2;
218         for (i=0 ;i<size; i++)
219         {       
220                 len2 = strLength - lstIn[i].length();
221                 for (ii=0;ii<len2;ii++)
222                 {       
223                         lstIn[i]="  "+lstIn[i];  // spaces characters
224                 } // for ii
225         } // for i
226 #endif // defined(_WIN32)
227         
228         itemcontainer->Clear();
229         for (i=0;i<size; i++)
230         {
231                 itemcontainer->Append(  bbtk::std2wx( lstIn[i] )  ); 
232         } // for i
233         if (iSelection>=0)
234         { 
235                 itemcontainer->SetSelection(iSelection); 
236                 if (mBox->bbGetInputWithSpinButton()==true)
237                 {
238                     mwxspinbutton->SetRange( 0,size-1 );
239                         mwxspinbutton->SetValue( iSelection );
240                 }
241
242         } // iSelection
243
244         if (mTypeForm==0) 
245         { 
246                 ((wxChoice*)itemcontainer)->SetAutoLayout( true ); 
247                 ((wxChoice*)itemcontainer)->Layout( ); 
248         } // if mTypeForm       
249
250
251 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,ComboBox)
252 BBTK_BLACK_BOX_IMPLEMENTATION(ComboBox,bbtk::WxBlackBox);
253
254 //===== 
255 // 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)
256 //===== 
257 void ComboBox::Process()
258 {
259         int iSelection = bbGetInputSelection();
260         if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection=bbGetInputIn().size()-1; }
261 //      if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection=0; }
262         ComboBoxWidget *w = (ComboBoxWidget*)bbGetOutputWidget();
263         w->FillItems( iSelection, bbGetInputIn() );
264     bbSetInputSelection( iSelection );
265     bbSetOutputOut( iSelection );
266     int size = bbGetInputIn().size();
267     if  (  (iSelection>=0) && ( iSelection<size) ) 
268     { 
269        bbSetOutputOutString( bbGetInputIn()[ iSelection ] );
270     } // if iSelection
271         w->VerifyDeselect(iSelection);
272 }
273 //===== 
274 // 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)
275 //===== 
276 void ComboBox::CreateWidget(wxWindow* parent)
277 {
278     ComboBoxWidget *w = new ComboBoxWidget(
279                                 this,
280                                                         parent,
281                                 bbGetInputSelection() ,
282                                 bbGetInputTitle(),
283                                 bbGetInputIn(),
284                                 bbGetInputForm(),
285                                                         bbGetInputWinWidth(), bbGetInputWinHeight()  );
286    bbSetOutputOut( bbGetInputSelection() );
287    if (bbGetInputIn().size()> bbGetInputSelection() )
288    {
289            bbSetOutputOutString( bbGetInputIn()[ bbGetInputSelection() ] );
290    } // if InputIn size
291    bbSetOutputWidget( w );
292 }
293 //===== 
294 // 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)
295 //===== 
296 void ComboBox::bbUserSetDefaultValues()
297 {
298         bbSetInputSelection(0);
299         bbSetInputTitle("");
300         bbSetInputForm(0);
301         bbSetInputWinWidth(10);
302         bbSetInputWinHeight(45);
303         bbSetInputDeselect(false);
304         bbSetInputWithSpinButton(false);
305 }
306 //===== 
307 // 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)
308 //===== 
309 void ComboBox::bbUserInitializeProcessing()
310 {
311 }
312 //===== 
313 // 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)
314 //===== 
315 void ComboBox::bbUserFinalizeProcessing()
316 {
317
318 }
319 }
320 // EO namespace bbwx
321
322