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