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