]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxComboBox.cxx
f0c3cc5d23de828a180d3a8ed0fd9fafb7ac11d2
[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
35     void OnSpinCtrlClick(wxCommandEvent& event);
36
37
38   private:
39     int                         mTypeForm;
40     ComboBox            *mBox;
41         wxItemContainer *itemcontainer;
42         wxSpinButton    *mwxspinbutton;
43   };
44   
45
46
47   //------------------------------------------------------------------------
48   //------------------------------------------------------------------------
49   //------------------------------------------------------------------------
50
51   //-------------------------------------------------------------------------
52   ComboBoxWidget::ComboBoxWidget( ComboBox* box,
53             wxWindow *parent,
54             int iSelection,
55             std::string title,
56             std::vector< std::string > lstIn,
57                         int typeForm,
58                         int sizeX, 
59                         int sizeY
60                 ) :
61     wxPanel( parent, -1,wxDefaultPosition ) ,
62     mBox(box),
63     mTypeForm(typeForm)
64   {
65         mwxspinbutton                   = NULL;
66         itemcontainer                   = NULL;
67         wxPanel                 *panel  = this;
68         int                     i;
69     //---------------------------------------------------------------------
70     // 2) Insertion of the components in the window
71     
72     // We use a FlexGridSizer
73 //    wxFlexGridSizer *sizerV           = new wxFlexGridSizer(1);
74 //    wxFlexGridSizer *sizerH1  = new wxFlexGridSizer(0);
75 //    wxFlexGridSizer *sizerH2  = new wxFlexGridSizer(2);
76     wxFlexGridSizer *sizer      = new wxFlexGridSizer(2);
77         
78     if (title!="")
79     {
80            sizer->Add( new wxStaticText(panel,-1,  bbtk::std2wx(title) ) ); 
81            sizer->Add( new wxStaticText(panel,-1,  _T("") ) ); 
82     }
83     //---------------------------------------------------------------------
84         
85         if (mTypeForm==1)
86         {
87                 wxListBox *wxlistbox = new wxListBox ( panel , -1 , wxDefaultPosition,wxSize(sizeX,sizeY),0, NULL, wxLB_SINGLE );       
88                 itemcontainer=wxlistbox;
89                 sizer->Add( wxlistbox,1,wxEXPAND ); 
90                 Connect( wxlistbox->GetId(), 
91                  wxEVT_COMMAND_LISTBOX_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox );                  
92         } else if (mTypeForm==0) {
93                 wxChoice *wxchoice = new wxChoice ( panel , -1, wxDefaultPosition,wxSize(sizeX,sizeY));
94                 itemcontainer=wxchoice;
95                 sizer->Add( wxchoice,1,wxGROW ); 
96                 Connect( wxchoice->GetId(), 
97                  wxEVT_COMMAND_CHOICE_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox ); 
98         }
99         
100         if (mBox->bbGetInputWithSpinButton()==true)
101         {
102                 mwxspinbutton = new wxSpinButton( panel, -1 );    
103                 Connect( mwxspinbutton->GetId(), 
104                          wxEVT_SPIN, 
105                          (wxObjectEventFunction) 
106                          (void (wxPanel::*)(wxScrollEvent&))
107                          &ComboBoxWidget::OnSpinCtrlClick);
108                 sizer->Add( mwxspinbutton,1,wxEXPAND ); 
109         }
110     sizer->AddGrowableCol(0);
111
112     panel->SetSizer(sizer);
113         FillItems( iSelection,lstIn );  
114   }
115
116   //-------------------------------------------------------------------------  
117   ComboBoxWidget::~ComboBoxWidget()
118   {
119   }
120
121   //--------------------------------------------------------------------------
122   void ComboBoxWidget::VerifyDeselect(int iSelection)
123   {
124         if ((iSelection>=0) && (mBox->bbGetInputDeselect()==true) )
125         {
126                 if (mTypeForm==1) 
127                 { 
128                         ((wxListBox*)itemcontainer)->Deselect( iSelection ); 
129                 } // if mTypeForm
130         } // if iSelection
131   }
132
133   //--------------------------------------------------------------------------
134   void ComboBoxWidget::OnComboBoxSelection(int iSelection)
135   {
136         if (iSelection>=0) 
137         {
138                 mBox->bbSetInputSelection( iSelection );
139                 mBox->bbSetOutputOut( iSelection );
140
141 // Patch to clean the spaces at the begining 
142                 std::string tmpStr = bbtk::wx2std( itemcontainer->GetString(iSelection) );
143                 if (tmpStr.length()>0) { while(tmpStr[0]==' ') tmpStr.erase(0,1); }
144                 mBox->bbSetOutputOutString(   tmpStr   );
145 //              mBox->bbSetOutputOutString(   bbtk::wx2std( itemcontainer->GetString(iSelection) )   );
146
147                 mBox->bbSignalOutputModification();
148                 VerifyDeselect(iSelection);
149                 if (mTypeForm==0) 
150                 { 
151                         wxChoice *wxchoise=(wxChoice *)itemcontainer;
152                         wxchoise->SetToolTip( itemcontainer->GetString(iSelection)    );
153                 } // if mTypeForm
154
155
156 //              mBox->bbSignalOutputModification("Out");
157 //              mBox->bbSignalOutputModification("OutString");
158         } // if iSelection
159   }
160
161 //--------------------------------------------------------------------------
162 void ComboBoxWidget::OnComboBox(wxEvent& event)
163 {
164         int iSelection = itemcontainer->GetSelection();
165         OnComboBoxSelection( iSelection );
166         if (mBox->bbGetInputWithSpinButton()==true)
167         {
168                 mwxspinbutton->SetValue( iSelection );
169         }
170
171 }
172
173 //--------------------------------------------------------------------------
174 void ComboBoxWidget::OnSpinCtrlClick(wxCommandEvent& event)
175 {
176         int iSelection = mwxspinbutton->GetValue();
177         itemcontainer->SetSelection(iSelection);
178         OnComboBoxSelection( iSelection );
179 }
180
181
182 //--------------------------------------------------------------------------
183 void ComboBoxWidget::FillItems( int iSelection, std::vector< std::string > lstIn )
184 {
185         int i,size = lstIn.size();
186
187
188 #if defined(_WIN32)
189         // Patch to put spaces at the beginin
190 #else
191     /*
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      */
210 #endif // defined(_WIN32)
211         
212         itemcontainer->Clear();
213         for (i=0;i<size; i++)
214         {
215                 itemcontainer->Append(  bbtk::std2wx( lstIn[i] )  ); 
216         } // for i
217         if (iSelection>=0)
218         { 
219                 itemcontainer->SetSelection(iSelection); 
220                 if (mBox->bbGetInputWithSpinButton()==true)
221                 {
222                     mwxspinbutton->SetRange( 0,size-1 );
223                         mwxspinbutton->SetValue( iSelection );
224                 }
225
226         } // iSelection
227
228         if (mTypeForm==0) 
229         { 
230                 ((wxChoice*)itemcontainer)->SetAutoLayout( true ); 
231                 ((wxChoice*)itemcontainer)->Layout( ); 
232         } // if mTypeForm       
233
234
235 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,ComboBox)
236 BBTK_BLACK_BOX_IMPLEMENTATION(ComboBox,bbtk::WxBlackBox);
237
238 //===== 
239 // 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)
240 //===== 
241 void ComboBox::Process()
242 {
243         int iSelection = bbGetInputSelection();
244 //      if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection=bbGetInputIn().size()-1; }
245         if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection=0; }
246         ComboBoxWidget *w = (ComboBoxWidget*)bbGetOutputWidget();
247         if (w!=NULL) {
248                 w->FillItems( iSelection, bbGetInputIn() );
249                 bbSetInputSelection( iSelection );
250                 bbSetOutputOut( iSelection );
251                 int size = bbGetInputIn().size();
252                 if      (  (iSelection>=0) && ( iSelection<size) ) 
253                 {
254                    bbSetOutputOutString( bbGetInputIn()[ iSelection ] );
255                 } // if iSelection
256                 w->VerifyDeselect(iSelection);
257         } // if w
258 }
259 //===== 
260 // 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)
261 //===== 
262 void ComboBox::CreateWidget(wxWindow* parent)
263 {
264     ComboBoxWidget *w = new ComboBoxWidget(
265                                 this,
266                                                         parent,
267                                 bbGetInputSelection() ,
268                                 bbGetInputTitle(),
269                                 bbGetInputIn(),
270                                 bbGetInputForm(),
271                                                         bbGetInputWinWidth(), bbGetInputWinHeight()  );
272    bbSetOutputOut( bbGetInputSelection() );
273    if (bbGetInputIn().size()> bbGetInputSelection() )
274    {
275            bbSetOutputOutString( bbGetInputIn()[ bbGetInputSelection() ] );
276    } // if InputIn size
277    bbSetOutputWidget( w );
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::bbUserSetDefaultValues()
284 {
285         bbSetInputSelection(0);
286         bbSetInputTitle("");
287         bbSetInputForm(0);
288         bbSetInputWinWidth(10);
289         bbSetInputWinHeight(45);
290         bbSetInputDeselect(false);
291         bbSetInputWithSpinButton(false);
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::bbUserInitializeProcessing()
297 {
298 }
299 //===== 
300 // 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)
301 //===== 
302 void ComboBox::bbUserFinalizeProcessing()
303 {
304 }
305
306 }// EO namespace bbwx
307
308