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