]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxComboBox.cxx
#3235 BBTK Bug New Normal - size wxcombo box
[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         wxPanel         *panel          = this;
62         itemcontainer                   = NULL;
63         int i;
64
65     //---------------------------------------------------------------------
66     // 2) Insertion of the components in the window
67     
68     // We use a FlexGridSizer
69     wxFlexGridSizer *sizer      = new wxFlexGridSizer(1);
70     if (title!="")
71     {
72            sizer->Add( new wxStaticText(panel,-1,  bbtk::std2wx(title) ) ); 
73     }
74     //---------------------------------------------------------------------
75         if (mTypeForm==1)
76         {
77                 wxListBox *wxlistbox = new wxListBox ( panel , -1 , wxDefaultPosition,wxSize(sizeX,sizeY),0, NULL, wxLB_SINGLE );       
78                 itemcontainer=wxlistbox;
79                 sizer->Add( wxlistbox,1,wxEXPAND ); 
80                 Connect( wxlistbox->GetId(), 
81                  wxEVT_COMMAND_LISTBOX_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox ); 
82         } else if (mTypeForm==0) {
83                 wxChoice *wxchoice = new wxChoice ( panel , -1, wxDefaultPosition,wxSize(sizeX,sizeY));
84                 itemcontainer=wxchoice;
85                 sizer->Add( wxchoice,1,wxGROW ); 
86
87                 Connect( wxchoice->GetId(), 
88                  wxEVT_COMMAND_CHOICE_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox ); 
89         }
90     sizer->AddGrowableCol(0);
91     panel->SetSizer(sizer);
92         
93         for (i=0;i<lstIn.size(); i++)
94         {
95                 itemcontainer->Append(  bbtk::std2wx( lstIn[i] )  ); 
96         } // for i
97 //      itemcontainer->SetSelection(iSelection);
98   }
99
100   //-------------------------------------------------------------------------  
101   ComboBoxWidget::~ComboBoxWidget()
102   {
103   }
104
105   //--------------------------------------------------------------------------
106   void ComboBoxWidget::VerifyDeselect(int iSelection)
107   {
108         if ((iSelection>=0) && (mBox->bbGetInputDeselect()==true) )
109         {
110                 if (mTypeForm==1) 
111                 { 
112                         ((wxListBox*)itemcontainer)->Deselect( iSelection ); 
113                 } // if mTypeForm
114         } // if iSelection
115   }
116
117   //--------------------------------------------------------------------------
118   void ComboBoxWidget::OnComboBoxSelection(int iSelection)
119   {
120         if (iSelection>=0) 
121         {
122                 mBox->bbSetInputSelection( iSelection );
123                 mBox->bbSetOutputOut( iSelection );
124                 mBox->bbSetOutputOutString(    bbtk::wx2std( itemcontainer->GetString(iSelection) )     );
125                 mBox->bbSignalOutputModification();
126
127                 VerifyDeselect(iSelection);
128
129                 if (mTypeForm==0) 
130                 { 
131                         wxChoice *wxchoise=(wxChoice *)itemcontainer;
132                         wxchoise->SetToolTip( itemcontainer->GetString(iSelection)    );
133                 } // if mTypeForm
134
135
136 //              mBox->bbSignalOutputModification("Out");
137 //              mBox->bbSignalOutputModification("OutString");
138         } // if iSelection
139   }
140
141   //--------------------------------------------------------------------------
142   void ComboBoxWidget::OnComboBox(wxEvent& event)
143   {
144         OnComboBoxSelection( itemcontainer->GetSelection() );
145   }
146 //--------------------------------------------------------------------------
147   void ComboBoxWidget::FillItems( int iSelection, std::vector< std::string > lstIn )
148         {
149                 int i;
150                 itemcontainer->Clear();
151         for (i=0;i<lstIn.size(); i++)
152         {
153                         itemcontainer->Append(  bbtk::std2wx( lstIn[i] )  ); 
154                 } // for i
155                 if (iSelection>=0) itemcontainer->SetSelection(iSelection);
156         } 
157
158 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,ComboBox)
159 BBTK_BLACK_BOX_IMPLEMENTATION(ComboBox,bbtk::WxBlackBox);
160 //===== 
161 // 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)
162 //===== 
163 void ComboBox::Process()
164 {
165         int iSelection = bbGetInputSelection();
166         if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection=bbGetInputIn().size()-1; }
167         ComboBoxWidget *w = (ComboBoxWidget*)bbGetOutputWidget();
168         w->FillItems( iSelection, bbGetInputIn() );
169     bbSetInputSelection( iSelection );
170     bbSetOutputOut( iSelection );
171     int size = bbGetInputIn().size();
172     if  (  (iSelection>0) && ( (size-1)<=iSelection) ) 
173     { 
174        bbSetOutputOutString( bbGetInputIn()[ iSelection ] );
175     } // if iSelection
176
177         w->VerifyDeselect(iSelection);
178 }
179 //===== 
180 // 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)
181 //===== 
182 void ComboBox::CreateWidget(wxWindow* parent)
183 {
184     ComboBoxWidget *w = new ComboBoxWidget(
185                                 this,
186                                                         parent,
187                                 bbGetInputSelection() ,
188                                 bbGetInputTitle(),
189                                 bbGetInputIn(),
190                                 bbGetInputForm(),
191                                                         bbGetInputWinWidth(), bbGetInputWinHeight()  );
192    bbSetOutputOut( bbGetInputSelection() );
193    bbSetOutputOutString( bbGetInputIn()[ bbGetInputSelection() ] );
194    bbSetOutputWidget( w );
195 }
196 //===== 
197 // 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)
198 //===== 
199 void ComboBox::bbUserSetDefaultValues()
200 {
201         bbSetInputSelection(0);
202         bbSetInputTitle("");
203         bbSetInputForm(0);
204         bbSetInputWinWidth(10);
205         bbSetInputWinHeight(45);
206         bbSetInputDeselect(false);
207 }
208 //===== 
209 // 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)
210 //===== 
211 void ComboBox::bbUserInitializeProcessing()
212 {
213 }
214 //===== 
215 // 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)
216 //===== 
217 void ComboBox::bbUserFinalizeProcessing()
218 {
219
220 }
221 }
222 // EO namespace bbwx
223
224