]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxComboBox.cxx
37f5338385e81efa8f8185470558cdb7b6d6c83b
[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 //      if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection=bbGetInputIn().size()-1; }
244         if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection=0; }
245         ComboBoxWidget *w = (ComboBoxWidget*)bbGetOutputWidget();
246         w->FillItems( iSelection, bbGetInputIn() );
247     bbSetInputSelection( iSelection );
248     bbSetOutputOut( iSelection );
249     int size = bbGetInputIn().size();
250     if  (  (iSelection>=0) && ( iSelection<size) ) 
251     { 
252        bbSetOutputOutString( bbGetInputIn()[ iSelection ] );
253     } // if iSelection
254         w->VerifyDeselect(iSelection);
255 }
256 //===== 
257 // 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)
258 //===== 
259 void ComboBox::CreateWidget(wxWindow* parent)
260 {
261     ComboBoxWidget *w = new ComboBoxWidget(
262                                 this,
263                                                         parent,
264                                 bbGetInputSelection() ,
265                                 bbGetInputTitle(),
266                                 bbGetInputIn(),
267                                 bbGetInputForm(),
268                                                         bbGetInputWinWidth(), bbGetInputWinHeight()  );
269    bbSetOutputOut( bbGetInputSelection() );
270    if (bbGetInputIn().size()> bbGetInputSelection() )
271    {
272            bbSetOutputOutString( bbGetInputIn()[ bbGetInputSelection() ] );
273    } // if InputIn size
274    bbSetOutputWidget( w );
275 }
276 //===== 
277 // 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)
278 //===== 
279 void ComboBox::bbUserSetDefaultValues()
280 {
281         bbSetInputSelection(0);
282         bbSetInputTitle("");
283         bbSetInputForm(0);
284         bbSetInputWinWidth(10);
285         bbSetInputWinHeight(45);
286         bbSetInputDeselect(false);
287         bbSetInputWithSpinButton(false);
288 }
289 //===== 
290 // 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)
291 //===== 
292 void ComboBox::bbUserInitializeProcessing()
293 {
294 }
295 //===== 
296 // 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)
297 //===== 
298 void ComboBox::bbUserFinalizeProcessing()
299 {
300
301 }
302 }
303 // EO namespace bbwx
304
305