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