]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxComboBox.cxx
06ec5912e0d115e929e31be9711424aab6d10a51
[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         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                 VerifyDeselect(iSelection);
125                 if (mTypeForm==0) 
126                 { 
127                         wxChoice *wxchoise=(wxChoice *)itemcontainer;
128                         wxchoise->SetToolTip( itemcontainer->GetString(iSelection)    );
129                 } // if mTypeForm
130
131
132 //              mBox->bbSignalOutputModification("Out");
133 //              mBox->bbSignalOutputModification("OutString");
134         } // if iSelection
135   }
136
137 //--------------------------------------------------------------------------
138 void ComboBoxWidget::OnComboBox(wxEvent& event)
139 {
140         OnComboBoxSelection( itemcontainer->GetSelection() );
141 }
142
143 //--------------------------------------------------------------------------
144 void ComboBoxWidget::FillItems( int iSelection, std::vector< std::string > lstIn )
145 {
146         int i;
147         itemcontainer->Clear();
148         for (i=0;i<lstIn.size(); i++)
149         {
150                 itemcontainer->Append(  bbtk::std2wx( lstIn[i] )  ); 
151         } // for i
152         if (iSelection>=0) itemcontainer->SetSelection(iSelection);
153
154
155 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,ComboBox)
156 BBTK_BLACK_BOX_IMPLEMENTATION(ComboBox,bbtk::WxBlackBox);
157
158 //===== 
159 // 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)
160 //===== 
161 void ComboBox::Process()
162 {
163         int iSelection = bbGetInputSelection();
164         if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection=bbGetInputIn().size()-1; }
165 //      if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection=0; }
166         ComboBoxWidget *w = (ComboBoxWidget*)bbGetOutputWidget();
167         w->FillItems( iSelection, bbGetInputIn() );
168     bbSetInputSelection( iSelection );
169     bbSetOutputOut( iSelection );
170     int size = bbGetInputIn().size();
171     if  (  (iSelection>=0) && ( iSelection<size) ) 
172     { 
173        bbSetOutputOutString( bbGetInputIn()[ iSelection ] );
174     } // if iSelection
175         w->VerifyDeselect(iSelection);
176 }
177 //===== 
178 // 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)
179 //===== 
180 void ComboBox::CreateWidget(wxWindow* parent)
181 {
182     ComboBoxWidget *w = new ComboBoxWidget(
183                                 this,
184                                                         parent,
185                                 bbGetInputSelection() ,
186                                 bbGetInputTitle(),
187                                 bbGetInputIn(),
188                                 bbGetInputForm(),
189                                                         bbGetInputWinWidth(), bbGetInputWinHeight()  );
190    bbSetOutputOut( bbGetInputSelection() );
191    if (bbGetInputIn().size()> bbGetInputSelection() )
192    {
193            bbSetOutputOutString( bbGetInputIn()[ bbGetInputSelection() ] );
194    } // if InputIn size
195    bbSetOutputWidget( w );
196 }
197 //===== 
198 // 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)
199 //===== 
200 void ComboBox::bbUserSetDefaultValues()
201 {
202         bbSetInputSelection(0);
203         bbSetInputTitle("");
204         bbSetInputForm(0);
205         bbSetInputWinWidth(10);
206         bbSetInputWinHeight(45);
207         bbSetInputDeselect(false);
208 }
209 //===== 
210 // 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)
211 //===== 
212 void ComboBox::bbUserInitializeProcessing()
213 {
214 }
215 //===== 
216 // 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)
217 //===== 
218 void ComboBox::bbUserFinalizeProcessing()
219 {
220
221 }
222 }
223 // EO namespace bbwx
224
225