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)
4 #include "bbwxComboBox.h"
5 #include "bbwxPackage.h"
10 #include <wx/choice.h>
11 #include <wx/control.h>
12 #include <wx/listbox.h>
14 #include <wx/spinbutt.h>
18 //--------------------------------------------------------------------------
19 // The widget created by the box
20 class ComboBoxWidget : public wxPanel
23 ComboBoxWidget( ComboBox* box, wxWindow *parent,
26 std::vector< std::string > lstIn,
28 int sizeX, int sizeY );
30 void OnComboBoxSelection(int iSelection);
31 void OnComboBox(wxEvent& event);
32 void FillItems( int iSelection, std::vector< std::string > lstIn);
33 void VerifyDeselect(int iSelection);
35 void OnSpinCtrlClick(wxCommandEvent& event);
41 wxItemContainer *itemcontainer;
42 wxSpinButton *mwxspinbutton;
47 //------------------------------------------------------------------------
48 //------------------------------------------------------------------------
49 //------------------------------------------------------------------------
51 //-------------------------------------------------------------------------
52 ComboBoxWidget::ComboBoxWidget( ComboBox* box,
56 std::vector< std::string > lstIn,
61 wxPanel( parent, -1,wxDefaultPosition ) ,
67 wxPanel *panel = this;
69 //---------------------------------------------------------------------
70 // 2) Insertion of the components in the window
72 // We use a FlexGridSizer
73 // wxFlexGridSizer *sizerV = new wxFlexGridSizer(1);
74 // wxFlexGridSizer *sizerH1 = new wxFlexGridSizer(0);
75 // wxFlexGridSizer *sizerH2 = new wxFlexGridSizer(2);
76 wxFlexGridSizer *sizer = new wxFlexGridSizer(2);
80 sizer->Add( new wxStaticText(panel,-1, bbtk::std2wx(title) ) );
81 sizer->Add( new wxStaticText(panel,-1, _T("") ) );
83 //---------------------------------------------------------------------
87 wxListBox *wxlistbox = new wxListBox ( panel , -1 , wxDefaultPosition,wxSize(sizeX,sizeY),0, NULL, wxLB_SINGLE );
88 itemcontainer=wxlistbox;
89 sizer->Add( wxlistbox,1,wxEXPAND );
90 Connect( wxlistbox->GetId(),
91 wxEVT_COMMAND_LISTBOX_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox );
92 } else if (mTypeForm==0) {
93 wxChoice *wxchoice = new wxChoice ( panel , -1, wxDefaultPosition,wxSize(sizeX,sizeY));
94 itemcontainer=wxchoice;
95 sizer->Add( wxchoice,1,wxGROW );
96 Connect( wxchoice->GetId(),
97 wxEVT_COMMAND_CHOICE_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox );
100 if (mBox->bbGetInputWithSpinButton()==true)
102 mwxspinbutton = new wxSpinButton( panel, -1 );
103 Connect( mwxspinbutton->GetId(),
105 (wxObjectEventFunction)
106 (void (wxPanel::*)(wxScrollEvent&))
107 &ComboBoxWidget::OnSpinCtrlClick);
108 sizer->Add( mwxspinbutton,1,wxEXPAND );
110 sizer->AddGrowableCol(0);
112 panel->SetSizer(sizer);
113 FillItems( iSelection,lstIn );
116 //-------------------------------------------------------------------------
117 ComboBoxWidget::~ComboBoxWidget()
121 //--------------------------------------------------------------------------
122 void ComboBoxWidget::VerifyDeselect(int iSelection)
124 if ((iSelection>=0) && (mBox->bbGetInputDeselect()==true) )
128 ((wxListBox*)itemcontainer)->Deselect( iSelection );
133 //--------------------------------------------------------------------------
134 void ComboBoxWidget::OnComboBoxSelection(int iSelection)
138 mBox->bbSetInputSelection( iSelection );
139 mBox->bbSetOutputOut( iSelection );
141 // Patch to clean the spaces at the begining
142 std::string tmpStr = bbtk::wx2std( itemcontainer->GetString(iSelection) );
143 if (tmpStr.length()>0) { while(tmpStr[0]==' ') tmpStr.erase(0,1); }
144 mBox->bbSetOutputOutString( tmpStr );
145 // mBox->bbSetOutputOutString( bbtk::wx2std( itemcontainer->GetString(iSelection) ) );
147 mBox->bbSignalOutputModification();
148 VerifyDeselect(iSelection);
151 wxChoice *wxchoise=(wxChoice *)itemcontainer;
152 wxchoise->SetToolTip( itemcontainer->GetString(iSelection) );
156 // mBox->bbSignalOutputModification("Out");
157 // mBox->bbSignalOutputModification("OutString");
161 //--------------------------------------------------------------------------
162 void ComboBoxWidget::OnComboBox(wxEvent& event)
164 int iSelection = itemcontainer->GetSelection();
165 OnComboBoxSelection( iSelection );
166 if (mBox->bbGetInputWithSpinButton()==true)
168 mwxspinbutton->SetValue( iSelection );
173 //--------------------------------------------------------------------------
174 void ComboBoxWidget::OnSpinCtrlClick(wxCommandEvent& event)
176 int iSelection = mwxspinbutton->GetValue();
177 itemcontainer->SetSelection(iSelection);
178 OnComboBoxSelection( iSelection );
182 //--------------------------------------------------------------------------
183 void ComboBoxWidget::FillItems( int iSelection, std::vector< std::string > lstIn )
185 int i,size = lstIn.size();
189 // Patch to put spaces at the beginin
192 // Patch to put spaces at the beginin
194 // Looking for the longest string
195 for (i=0 ;i<size; i++)
197 if ( strLength < (int) lstIn[i].length() ) { strLength=lstIn[i].length(); }
199 // Adding spaces at the bigining to the others strings in the list to have the same size
201 for (i=0 ;i<size; i++)
203 len2 = strLength - lstIn[i].length();
204 for (ii=0;ii<len2;ii++)
206 lstIn[i]=" "+lstIn[i]; // spaces characters
210 #endif // defined(_WIN32)
212 itemcontainer->Clear();
213 for (i=0;i<size; i++)
215 itemcontainer->Append( bbtk::std2wx( lstIn[i] ) );
219 itemcontainer->SetSelection(iSelection);
220 if (mBox->bbGetInputWithSpinButton()==true)
222 mwxspinbutton->SetRange( 0,size-1 );
223 mwxspinbutton->SetValue( iSelection );
230 ((wxChoice*)itemcontainer)->SetAutoLayout( true );
231 ((wxChoice*)itemcontainer)->Layout( );
235 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,ComboBox)
236 BBTK_BLACK_BOX_IMPLEMENTATION(ComboBox,bbtk::WxBlackBox);
239 // 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)
241 void ComboBox::Process()
243 int iSelection = bbGetInputSelection();
244 // if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection=bbGetInputIn().size()-1; }
245 if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection=0; }
246 ComboBoxWidget *w = (ComboBoxWidget*)bbGetOutputWidget();
248 w->FillItems( iSelection, bbGetInputIn() );
249 bbSetInputSelection( iSelection );
250 bbSetOutputOut( iSelection );
251 int size = bbGetInputIn().size();
252 if ( (iSelection>=0) && ( iSelection<size) )
254 bbSetOutputOutString( bbGetInputIn()[ iSelection ] );
256 w->VerifyDeselect(iSelection);
260 // 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)
262 void ComboBox::CreateWidget(wxWindow* parent)
264 ComboBoxWidget *w = new ComboBoxWidget(
267 bbGetInputSelection() ,
271 bbGetInputWinWidth(), bbGetInputWinHeight() );
272 bbSetOutputOut( bbGetInputSelection() );
273 if (bbGetInputIn().size()> bbGetInputSelection() )
275 bbSetOutputOutString( bbGetInputIn()[ bbGetInputSelection() ] );
277 bbSetOutputWidget( w );
281 // 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)
283 void ComboBox::bbUserSetDefaultValues()
285 bbSetInputSelection(0);
288 bbSetInputWinWidth(10);
289 bbSetInputWinHeight(45);
290 bbSetInputDeselect(false);
291 bbSetInputWithSpinButton(false);
294 // 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)
296 void ComboBox::bbUserInitializeProcessing()
300 // 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)
302 void ComboBox::bbUserFinalizeProcessing()
306 }// EO namespace bbwx