]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxComboBox.cxx
#3054 BBTK Bug New Normal - package wx - combo box. crash with empty input
[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         ~ComboBoxWidget();
28         void OnComboBox(int iSelection);
29         void OnComboBox(wxEvent& event);
30         void FillItems( int iSelection, std::vector< std::string > lstIn);
31
32   private:
33     int                         mTypeForm;
34     ComboBox            *mBox;
35         wxListBox               *wxlistbox;
36         wxChoice                *wxchoice;
37   };
38   
39
40
41   //------------------------------------------------------------------------
42   //------------------------------------------------------------------------
43   //------------------------------------------------------------------------
44
45   //-------------------------------------------------------------------------
46   ComboBoxWidget::ComboBoxWidget( ComboBox* box,
47                wxWindow *parent,
48                int iSelection,
49                std::string title,
50                std::vector< std::string > lstIn,
51                                         int typeForm)
52     :
53     wxPanel( parent, -1) ,
54     mBox(box),
55     mTypeForm(typeForm)
56   {
57         wxPanel         *panel          = this;
58         wxlistbox                               = NULL;
59         wxchoice                                = NULL;
60         int i;
61
62     //---------------------------------------------------------------------
63     // 2) Insertion of the components in the window
64     
65     // We use a FlexGridSizer
66     wxFlexGridSizer *sizer      = new wxFlexGridSizer(1);
67     if (title!="")
68     {
69            sizer->Add( new wxStaticText(panel,-1,  bbtk::std2wx(title) ) ); 
70     }
71     sizer->AddGrowableCol(0);
72     panel->SetSizer(sizer);
73                 
74     //---------------------------------------------------------------------
75     // 1) Creation de wxChoise widget
76
77                 if (mTypeForm==1)
78                 {
79
80                         wxlistbox = new wxListBox ( panel , -1 );                       
81                         Connect( wxlistbox->GetId(), wxEVT_COMMAND_CHOICE_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox ); 
82                 for (i=0;i<lstIn.size(); i++)
83                 {
84                                 wxlistbox->Append(  bbtk::std2wx( lstIn[i] )  ); 
85                         } // for i
86                 wxlistbox->SetSelection(iSelection);
87                    sizer->Add( wxlistbox,1,wxGROW ); 
88
89                 } else {
90
91                         wxchoice = new wxChoice ( panel , -1 );
92                         Connect( wxchoice->GetId(), wxEVT_COMMAND_CHOICE_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox ); 
93                 for (i=0;i<lstIn.size(); i++)
94                 {
95                                 wxchoice->Append(  bbtk::std2wx( lstIn[i] )  ); 
96                         } // for i
97                 wxchoice->SetSelection(iSelection);
98                    sizer->Add( wxchoice,1,wxGROW ); 
99                 }
100 //    panel->SetAutoLayout(true);
101 //    panel->Layout();
102   }
103   //-------------------------------------------------------------------------
104   
105   ComboBoxWidget::~ComboBoxWidget()
106   {
107   }
108
109
110   //--------------------------------------------------------------------------
111   void ComboBoxWidget::OnComboBox(int iSelection)
112   {
113     mBox->bbSetInputSelection( iSelection );
114     mBox->bbSetOutputOut( iSelection );
115     mBox->bbSetOutputOutString(    bbtk::wx2std( wxchoice->GetString(iSelection) )     );
116     mBox->bbSignalOutputModification("Out");
117     mBox->bbSignalOutputModification("OutString");
118   }
119
120   //--------------------------------------------------------------------------
121   void ComboBoxWidget::OnComboBox(wxEvent& event)
122   {
123          int iSelection;
124          if (mTypeForm==1)
125          {
126                  iSelection = wxlistbox->GetSelection();
127          } else {
128                  iSelection = wxchoice->GetSelection();
129          }
130          OnComboBox(iSelection);
131   }
132 //--------------------------------------------------------------------------
133   void ComboBoxWidget::FillItems(
134                int iSelection,
135                std::vector< std::string > lstIn
136         )
137         {
138                 int i;
139                 if (mTypeForm==1)
140                 {
141                         wxlistbox->Clear();
142
143                 for (i=0;i<lstIn.size(); i++)
144                 {
145                                 wxlistbox->Append(  bbtk::std2wx( lstIn[i] )  ); 
146                         } // for i
147
148                 wxlistbox->SetSelection(iSelection);
149                 } else {
150                         wxchoice->Clear();
151                 for (i=0;i<lstIn.size(); i++)
152                 {
153                                 wxchoice->Append(  bbtk::std2wx( lstIn[i] )  ); 
154                         } // for i
155                 wxchoice->SetSelection(iSelection);
156                 } // if
157         } 
158
159 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,ComboBox)
160 BBTK_BLACK_BOX_IMPLEMENTATION(ComboBox,bbtk::WxBlackBox);
161 //===== 
162 // 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)
163 //===== 
164 void ComboBox::Process()
165 {
166
167 printf("EED ComboBox::Process start\n");
168
169         int iSelection = bbGetInputSelection();
170 printf("EED ComboBox::Process 0\n");
171         if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection=bbGetInputIn().size()-1; }
172 printf("EED ComboBox::Process 1\n");
173         ComboBoxWidget *w = (ComboBoxWidget*)bbGetOutputWidget();
174 printf("EED ComboBox::Process 2\n");
175         w->FillItems( iSelection, bbGetInputIn() );
176 printf("EED ComboBox::Process 3\n");
177     bbSetInputSelection( iSelection );
178 printf("EED ComboBox::Process 4\n");
179     bbSetOutputOut( iSelection );
180 printf("EED ComboBox::Process 5\n");
181
182     int size = bbGetInputIn().size();
183     if  (  (iSelection>0) && ( (size-1)<=iSelection) ) 
184     { 
185        bbSetOutputOutString( bbGetInputIn()[ iSelection ] );
186     }
187 //    bbSignalOutputModification("Out");
188 //    bbSignalOutputModification("OutString");
189
190 printf("EED ComboBox::Process end\n");
191
192 }
193
194 //===== 
195 // 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)
196 //===== 
197 void ComboBox::CreateWidget(wxWindow* parent)
198 {
199
200 //   bbSetOutputWidget( new wxStaticText ( parent , -1 , _T("") ) );
201 //   bbSetOutputWidget( new wxComboBox ( parent , -1 , _T("ups") ) );  
202 //   bbSetOutputWidget( new wxChoice ( parent , -1 ) );  
203
204     ComboBoxWidget *w = new ComboBoxWidget(
205                                 this,
206                                                         parent,
207                                 bbGetInputSelection() ,
208                                 bbGetInputTitle(),
209                                 bbGetInputIn(),
210                                 bbGetInputForm() );
211
212    bbSetOutputOut( bbGetInputSelection() );
213    bbSetOutputOutString( bbGetInputIn()[ bbGetInputSelection() ] );
214    bbSetOutputWidget( w );
215
216
217 }
218 //===== 
219 // 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)
220 //===== 
221 void ComboBox::bbUserSetDefaultValues()
222 {
223         bbSetInputSelection(0);
224         bbSetInputTitle("");
225         bbSetInputForm(0);
226 }
227 //===== 
228 // 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)
229 //===== 
230 void ComboBox::bbUserInitializeProcessing()
231 {
232
233 }
234 //===== 
235 // 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)
236 //===== 
237 void ComboBox::bbUserFinalizeProcessing()
238 {
239
240 }
241 }
242 // EO namespace bbwx
243
244