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);
34 void OnSpinCtrlClick(wxCommandEvent& event);
38 wxItemContainer *itemcontainer;
39 wxSpinButton *mwxspinbutton;
44 //------------------------------------------------------------------------
45 //------------------------------------------------------------------------
46 //------------------------------------------------------------------------
48 //-------------------------------------------------------------------------
49 ComboBoxWidget::ComboBoxWidget( ComboBox* box,
53 std::vector< std::string > lstIn,
58 wxPanel( parent, -1,wxDefaultPosition ) ,
64 wxPanel *panel = this;
66 //---------------------------------------------------------------------
67 // 2) Insertion of the components in the window
69 // We use a FlexGridSizer
70 // wxFlexGridSizer *sizerV = new wxFlexGridSizer(1);
71 // wxFlexGridSizer *sizerH1 = new wxFlexGridSizer(0);
72 // wxFlexGridSizer *sizerH2 = new wxFlexGridSizer(2);
73 wxFlexGridSizer *sizer = new wxFlexGridSizer(2);
76 sizer->Add( new wxStaticText(panel,-1, bbtk::std2wx(title) ) );
77 sizer->Add( new wxStaticText(panel,-1, _T("") ) );
80 //---------------------------------------------------------------------
83 wxListBox *wxlistbox = new wxListBox ( panel , -1 , wxDefaultPosition,wxSize(sizeX,sizeY),0, NULL, wxLB_SINGLE );
84 itemcontainer=wxlistbox;
85 sizer->Add( wxlistbox,1,wxEXPAND );
86 Connect( wxlistbox->GetId(),
87 wxEVT_COMMAND_LISTBOX_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox );
88 } else if (mTypeForm==0) {
89 wxChoice *wxchoice = new wxChoice ( panel , -1, wxDefaultPosition,wxSize(sizeX,sizeY));
90 itemcontainer=wxchoice;
91 sizer->Add( wxchoice,1,wxGROW );
92 Connect( wxchoice->GetId(),
93 wxEVT_COMMAND_CHOICE_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox );
95 if (mBox->bbGetInputWithSpinButton()==true)
97 mwxspinbutton = new wxSpinButton( panel, -1 );
98 Connect( mwxspinbutton->GetId(),
100 (wxObjectEventFunction)
101 (void (wxPanel::*)(wxScrollEvent&))
102 &ComboBoxWidget::OnSpinCtrlClick);
103 sizer->Add( mwxspinbutton,1,wxEXPAND );
105 sizer->AddGrowableCol(0);
106 panel->SetSizer(sizer);
107 FillItems( iSelection,lstIn );
110 //-------------------------------------------------------------------------
111 ComboBoxWidget::~ComboBoxWidget()
115 //--------------------------------------------------------------------------
116 void ComboBoxWidget::VerifyDeselect(int iSelection)
118 if ((iSelection>=0) && (mBox->bbGetInputDeselect()==true) )
122 ((wxListBox*)itemcontainer)->Deselect( iSelection );
127 //--------------------------------------------------------------------------
128 void ComboBoxWidget::OnComboBoxSelection(int iSelection)
132 mBox->bbSetInputSelection( iSelection );
133 mBox->bbSetOutputOut( iSelection );
134 // Patch to clean the spaces at the begining
135 std::string tmpStr = bbtk::wx2std( itemcontainer->GetString(iSelection) );
136 if (tmpStr.length()>0) { while(tmpStr[0]==' ') tmpStr.erase(0,1); }
137 mBox->bbSetOutputOutString( tmpStr );
138 // mBox->bbSetOutputOutString( bbtk::wx2std( itemcontainer->GetString(iSelection) ) );
139 mBox->bbSignalOutputModification();
140 VerifyDeselect(iSelection);
143 wxChoice *wxchoise=(wxChoice *)itemcontainer;
144 wxchoise->SetToolTip( itemcontainer->GetString(iSelection) );
146 // mBox->bbSignalOutputModification("Out");
147 // mBox->bbSignalOutputModification("OutString");
151 //--------------------------------------------------------------------------
152 void ComboBoxWidget::OnComboBox(wxEvent& event)
154 int iSelection = itemcontainer->GetSelection();
155 OnComboBoxSelection( iSelection );
156 if (mBox->bbGetInputWithSpinButton()==true)
158 mwxspinbutton->SetValue( iSelection );
162 //--------------------------------------------------------------------------
163 void ComboBoxWidget::OnSpinCtrlClick(wxCommandEvent& event)
165 int iSelection = mwxspinbutton->GetValue();
166 itemcontainer->SetSelection(iSelection);
167 OnComboBoxSelection( iSelection );
171 //--------------------------------------------------------------------------
172 void ComboBoxWidget::FillItems( int iSelection, std::vector< std::string > lstIn )
174 int i,size = lstIn.size();
176 // Patch to put spaces at the beginin
179 // Patch to put spaces at the beginin
181 // Looking for the longest string
182 for (i=0 ;i<size; i++)
184 if ( strLength < (int) lstIn[i].length() ) { strLength=lstIn[i].length(); }
186 // Adding spaces at the bigining to the others strings in the list to have the same size
188 for (i=0 ;i<size; i++)
190 len2 = strLength - lstIn[i].length();
191 for (ii=0;ii<len2;ii++)
193 lstIn[i]=" "+lstIn[i]; // spaces characters
197 #endif // defined(_WIN32)
198 itemcontainer->Clear();
199 for (i=0;i<size; i++)
201 itemcontainer->Append( bbtk::std2wx( lstIn[i] ) );
205 itemcontainer->SetSelection(iSelection);
206 if (mBox->bbGetInputWithSpinButton()==true)
208 mwxspinbutton->SetRange( 0,size-1 );
209 mwxspinbutton->SetValue( iSelection );
215 ((wxChoice*)itemcontainer)->SetAutoLayout( true );
216 ((wxChoice*)itemcontainer)->Layout( );
220 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,ComboBox)
221 BBTK_BLACK_BOX_IMPLEMENTATION(ComboBox,bbtk::WxBlackBox);
224 // 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)
226 void ComboBox::Process()
228 int iSelection = bbGetInputSelection();
229 // if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection = bbGetInputIn().size()-1; }
230 if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection = 0; }
231 if (bbGetInputIn().size()==0) { iSelection = -1; }
232 ComboBoxWidget *w = (ComboBoxWidget*)bbGetOutputWidget();
235 w->FillItems( iSelection, bbGetInputIn() );
236 bbSetInputSelection( iSelection );
237 bbSetOutputOut( iSelection );
238 int size = bbGetInputIn().size();
239 if ( (iSelection>=0) && ( iSelection<size) )
241 bbSetOutputOutString( bbGetInputIn()[ iSelection ] );
243 w->VerifyDeselect(iSelection);
247 // 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)
249 void ComboBox::CreateWidget(wxWindow* parent)
251 ComboBoxWidget *w = new ComboBoxWidget(
254 bbGetInputSelection() ,
258 bbGetInputWinWidth(), bbGetInputWinHeight() );
259 bbSetOutputOut( bbGetInputSelection() );
260 if (bbGetInputIn().size()> bbGetInputSelection() )
262 bbSetOutputOutString( bbGetInputIn()[ bbGetInputSelection() ] );
264 bbSetOutputWidget( w );
267 // 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)
269 void ComboBox::bbUserSetDefaultValues()
271 bbSetInputSelection(0);
274 bbSetInputWinWidth(10);
275 bbSetInputWinHeight(45);
276 bbSetInputDeselect(false);
277 bbSetInputWithSpinButton(false);
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::bbUserInitializeProcessing()
288 // 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)
290 void ComboBox::bbUserFinalizeProcessing()
294 }// EO namespace bbwx