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