]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxComboBox.cxx
box VectorFilterString with EraseDuplicateLines option
[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     /*
191         // Patch to put spaces at the beginin
192         int strLength=-1;
193         // Looking for the longest string
194         for (i=0 ;i<size; i++)
195         {       
196                 if ( strLength < (int) lstIn[i].length() ) { strLength=lstIn[i].length();  }
197         }       // for
198         // Adding spaces at the bigining to the others strings in the list to have the same size
199         int ii,len2;
200         for (i=0 ;i<size; i++)
201         {       
202                 len2 = strLength - lstIn[i].length();
203                 for (ii=0;ii<len2;ii++)
204                 {       
205                         lstIn[i]="  "+lstIn[i];  // spaces characters
206                 } // for ii
207         } // for i
208      */
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