]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxComboBox.cxx
0a1a99e909831c5397b8f1ee73266c6532182cbe
[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 *sizerV           = new wxFlexGridSizer(1);
76 //    wxFlexGridSizer *sizerH1  = new wxFlexGridSizer(0);
77 //    wxFlexGridSizer *sizerH2  = new wxFlexGridSizer(2);
78     wxFlexGridSizer *sizer      = new wxFlexGridSizer(2);
79         
80     if (title!="")
81     {
82            sizer->Add( new wxStaticText(panel,-1,  bbtk::std2wx(title) ) ); 
83            sizer->Add( new wxStaticText(panel,-1,  _T("") ) ); 
84     }
85     //---------------------------------------------------------------------
86         if (mTypeForm==1)
87         {
88                 wxListBox *wxlistbox = new wxListBox ( panel , -1 , wxDefaultPosition,wxSize(sizeX,sizeY),0, NULL, wxLB_SINGLE );       
89                 itemcontainer=wxlistbox;
90                 sizer->Add( wxlistbox,1,wxEXPAND ); 
91                 Connect( wxlistbox->GetId(), 
92                  wxEVT_COMMAND_LISTBOX_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox );                  
93         } else if (mTypeForm==0) {
94                 wxChoice *wxchoice = new wxChoice ( panel , -1, wxDefaultPosition,wxSize(sizeX,sizeY));
95                 itemcontainer=wxchoice;
96                 sizer->Add( wxchoice,1,wxGROW ); 
97                 Connect( wxchoice->GetId(), 
98                  wxEVT_COMMAND_CHOICE_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox ); 
99         }
100         
101         if (mBox->bbGetInputWithSpinButton()==true)
102         {
103                 mwxspinbutton = new wxSpinButton( panel, -1 );    
104                 Connect( mwxspinbutton->GetId(), 
105                          wxEVT_SPIN, 
106                          (wxObjectEventFunction) 
107                          (void (wxPanel::*)(wxScrollEvent&))
108                          &ComboBoxWidget::OnSpinCtrlClick);
109                 sizer->Add( mwxspinbutton,1,wxEXPAND ); 
110         }
111     sizer->AddGrowableCol(0);
112
113     panel->SetSizer(sizer);
114         FillItems( iSelection,lstIn );  
115   }
116
117   //-------------------------------------------------------------------------  
118   ComboBoxWidget::~ComboBoxWidget()
119   {
120   }
121
122   //--------------------------------------------------------------------------
123   void ComboBoxWidget::VerifyDeselect(int iSelection)
124   {
125         if ((iSelection>=0) && (mBox->bbGetInputDeselect()==true) )
126         {
127                 if (mTypeForm==1) 
128                 { 
129                         ((wxListBox*)itemcontainer)->Deselect( iSelection ); 
130                 } // if mTypeForm
131         } // if iSelection
132   }
133
134   //--------------------------------------------------------------------------
135   void ComboBoxWidget::OnComboBoxSelection(int iSelection)
136   {
137         if (iSelection>=0) 
138         {
139                 mBox->bbSetInputSelection( iSelection );
140                 mBox->bbSetOutputOut( iSelection );
141
142 // Patch to clean the spaces at the begining 
143                 std::string tmpStr = bbtk::wx2std( itemcontainer->GetString(iSelection) );
144                 if (tmpStr.length()>0) { while(tmpStr[0]==' ') tmpStr.erase(0,1); }
145                 mBox->bbSetOutputOutString(   tmpStr   );
146 //              mBox->bbSetOutputOutString(   bbtk::wx2std( itemcontainer->GetString(iSelection) )   );
147
148                 mBox->bbSignalOutputModification();
149                 VerifyDeselect(iSelection);
150                 if (mTypeForm==0) 
151                 { 
152                         wxChoice *wxchoise=(wxChoice *)itemcontainer;
153                         wxchoise->SetToolTip( itemcontainer->GetString(iSelection)    );
154                 } // if mTypeForm
155
156
157 //              mBox->bbSignalOutputModification("Out");
158 //              mBox->bbSignalOutputModification("OutString");
159         } // if iSelection
160   }
161
162 //--------------------------------------------------------------------------
163 void ComboBoxWidget::OnComboBox(wxEvent& event)
164 {
165         int iSelection = itemcontainer->GetSelection();
166         OnComboBoxSelection( iSelection );
167         if (mBox->bbGetInputWithSpinButton()==true)
168         {
169                 mwxspinbutton->SetValue( iSelection );
170         }
171
172 }
173
174 //--------------------------------------------------------------------------
175 void ComboBoxWidget::OnSpinCtrlClick(wxCommandEvent& event)
176 {
177         int iSelection = mwxspinbutton->GetValue();
178         itemcontainer->SetSelection(iSelection);
179         OnComboBoxSelection( iSelection );
180 }
181
182
183 //--------------------------------------------------------------------------
184 void ComboBoxWidget::FillItems( int iSelection, std::vector< std::string > lstIn )
185 {
186         int i,size = lstIn.size();
187
188
189 #if defined(_WIN32)
190         // Patch to put spaces at the beginin
191 #else
192         // Patch to put spaces at the beginin
193         int strLength=-1;
194         // Looking for the longest string
195         for (i=0 ;i<size; i++)
196         {       
197                 if ( strLength < (int) lstIn[i].length() ) { strLength=lstIn[i].length();  }
198         }       // for
199         // Adding spaces at the bigining to the others strings in the list to have the same size
200         int ii,len2;
201         for (i=0 ;i<size; i++)
202         {       
203                 len2 = strLength - lstIn[i].length();
204                 for (ii=0;ii<len2;ii++)
205                 {       
206                         lstIn[i]="  "+lstIn[i];  // spaces characters
207                 } // for ii
208         } // for i
209 #endif // defined(_WIN32)
210         
211         itemcontainer->Clear();
212         for (i=0;i<size; i++)
213         {
214                 itemcontainer->Append(  bbtk::std2wx( lstIn[i] )  ); 
215         } // for i
216         if (iSelection>=0)
217         { 
218                 itemcontainer->SetSelection(iSelection); 
219                 if (mBox->bbGetInputWithSpinButton()==true)
220                 {
221                     mwxspinbutton->SetRange( 0,size-1 );
222                         mwxspinbutton->SetValue( iSelection );
223                 }
224
225         } // iSelection
226
227         if (mTypeForm==0) 
228         { 
229                 ((wxChoice*)itemcontainer)->SetAutoLayout( true ); 
230                 ((wxChoice*)itemcontainer)->Layout( ); 
231         } // if mTypeForm       
232
233
234 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,ComboBox)
235 BBTK_BLACK_BOX_IMPLEMENTATION(ComboBox,bbtk::WxBlackBox);
236
237 //===== 
238 // 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)
239 //===== 
240 void ComboBox::Process()
241 {
242         int iSelection = bbGetInputSelection();
243  printf("ComboBox::Process A iSelection=%d\n", iSelection);   
244 //      if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection=bbGetInputIn().size()-1; }
245         if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection=0; }
246         ComboBoxWidget *w = (ComboBoxWidget*)bbGetOutputWidget();
247         w->FillItems( iSelection, bbGetInputIn() );
248     bbSetInputSelection( iSelection );
249     bbSetOutputOut( iSelection );
250     int size = bbGetInputIn().size();
251     if  (  (iSelection>=0) && ( iSelection<size) ) 
252     { 
253        bbSetOutputOutString( bbGetInputIn()[ iSelection ] );
254     } // if iSelection
255  printf("ComboBox::Process B iSelection=%d\n", iSelection);   
256         w->VerifyDeselect(iSelection);
257 }
258 //===== 
259 // 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)
260 //===== 
261 void ComboBox::CreateWidget(wxWindow* parent)
262 {
263     ComboBoxWidget *w = new ComboBoxWidget(
264                                 this,
265                                                         parent,
266                                 bbGetInputSelection() ,
267                                 bbGetInputTitle(),
268                                 bbGetInputIn(),
269                                 bbGetInputForm(),
270                                                         bbGetInputWinWidth(), bbGetInputWinHeight()  );
271    bbSetOutputOut( bbGetInputSelection() );
272    if (bbGetInputIn().size()> bbGetInputSelection() )
273    {
274            bbSetOutputOutString( bbGetInputIn()[ bbGetInputSelection() ] );
275    } // if InputIn size
276    bbSetOutputWidget( w );
277 }
278 //===== 
279 // 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)
280 //===== 
281 void ComboBox::bbUserSetDefaultValues()
282 {
283         bbSetInputSelection(0);
284         bbSetInputTitle("");
285         bbSetInputForm(0);
286         bbSetInputWinWidth(10);
287         bbSetInputWinHeight(45);
288         bbSetInputDeselect(false);
289         bbSetInputWithSpinButton(false);
290 }
291 //===== 
292 // 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)
293 //===== 
294 void ComboBox::bbUserInitializeProcessing()
295 {
296 }
297 //===== 
298 // 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)
299 //===== 
300 void ComboBox::bbUserFinalizeProcessing()
301 {
302
303 }
304 }
305 // EO namespace bbwx
306
307