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"
11 #include <wx/choice.h>
12 #include <wx/control.h>
13 #include <wx/listbox.h>
15 #include <wx/spinbutt.h>
19 //--------------------------------------------------------------------------
20 // The widget created by the box
21 class ComboBoxWidget : public wxPanel
24 ComboBoxWidget( ComboBox* box, wxWindow *parent,
27 std::vector< std::string > lstIn,
29 int sizeX, int sizeY );
31 void OnComboBoxSelection(int iSelection);
32 void OnComboBox(wxEvent& event);
33 void FillItems( int iSelection, std::vector< std::string > lstIn);
34 void VerifyDeselect(int iSelection);
36 void OnSpinCtrlClick(wxCommandEvent& event);
42 wxItemContainer *itemcontainer;
43 wxSpinButton *mwxspinbutton;
48 //------------------------------------------------------------------------
49 //------------------------------------------------------------------------
50 //------------------------------------------------------------------------
52 //-------------------------------------------------------------------------
53 ComboBoxWidget::ComboBoxWidget( ComboBox* box,
57 std::vector< std::string > lstIn,
63 wxPanel( parent, -1,wxDefaultPosition ) ,
69 wxPanel *panel = this;
71 //---------------------------------------------------------------------
72 // 2) Insertion of the components in the window
74 // We use a FlexGridSizer
75 // wxFlexGridSizer *sizerV = new wxFlexGridSizer(1);
76 // wxFlexGridSizer *sizerH1 = new wxFlexGridSizer(0);
77 // wxFlexGridSizer *sizerH2 = new wxFlexGridSizer(2);
78 wxFlexGridSizer *sizer = new wxFlexGridSizer(2);
82 sizer->Add( new wxStaticText(panel,-1, bbtk::std2wx(title) ) );
83 sizer->Add( new wxStaticText(panel,-1, _T("") ) );
85 //---------------------------------------------------------------------
88 wxListBox *wxlistbox = new wxListBox ( panel , -1 , wxDefaultPosition,wxSize(sizeX,sizeY),0, NULL, wxLB_SINGLE );
89 itemcontainer=wxlistbox;
90 sizer->Add( wxlistbox,1,wxEXPAND );
91 Connect( wxlistbox->GetId(),
92 wxEVT_COMMAND_LISTBOX_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox );
93 } else if (mTypeForm==0) {
94 wxChoice *wxchoice = new wxChoice ( panel , -1, wxDefaultPosition,wxSize(sizeX,sizeY));
95 itemcontainer=wxchoice;
96 sizer->Add( wxchoice,1,wxGROW );
97 Connect( wxchoice->GetId(),
98 wxEVT_COMMAND_CHOICE_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox );
101 if (mBox->bbGetInputWithSpinButton()==true)
103 mwxspinbutton = new wxSpinButton( panel, -1 );
104 Connect( mwxspinbutton->GetId(),
106 (wxObjectEventFunction)
107 (void (wxPanel::*)(wxScrollEvent&))
108 &ComboBoxWidget::OnSpinCtrlClick);
109 sizer->Add( mwxspinbutton,1,wxEXPAND );
111 sizer->AddGrowableCol(0);
113 panel->SetSizer(sizer);
114 FillItems( iSelection,lstIn );
117 //-------------------------------------------------------------------------
118 ComboBoxWidget::~ComboBoxWidget()
122 //--------------------------------------------------------------------------
123 void ComboBoxWidget::VerifyDeselect(int iSelection)
125 if ((iSelection>=0) && (mBox->bbGetInputDeselect()==true) )
129 ((wxListBox*)itemcontainer)->Deselect( iSelection );
134 //--------------------------------------------------------------------------
135 void ComboBoxWidget::OnComboBoxSelection(int iSelection)
139 mBox->bbSetInputSelection( iSelection );
140 mBox->bbSetOutputOut( iSelection );
142 // Patch to clean the spaces at the begining
143 std::string tmpStr = bbtk::wx2std( itemcontainer->GetString(iSelection) );
144 if (tmpStr.length()>0) { while(tmpStr[0]==' ') tmpStr.erase(0,1); }
145 mBox->bbSetOutputOutString( tmpStr );
146 // mBox->bbSetOutputOutString( bbtk::wx2std( itemcontainer->GetString(iSelection) ) );
148 mBox->bbSignalOutputModification();
149 VerifyDeselect(iSelection);
152 wxChoice *wxchoise=(wxChoice *)itemcontainer;
153 wxchoise->SetToolTip( itemcontainer->GetString(iSelection) );
157 // mBox->bbSignalOutputModification("Out");
158 // mBox->bbSignalOutputModification("OutString");
162 //--------------------------------------------------------------------------
163 void ComboBoxWidget::OnComboBox(wxEvent& event)
165 int iSelection = itemcontainer->GetSelection();
166 OnComboBoxSelection( iSelection );
167 if (mBox->bbGetInputWithSpinButton()==true)
169 mwxspinbutton->SetValue( iSelection );
174 //--------------------------------------------------------------------------
175 void ComboBoxWidget::OnSpinCtrlClick(wxCommandEvent& event)
177 int iSelection = mwxspinbutton->GetValue();
178 itemcontainer->SetSelection(iSelection);
179 OnComboBoxSelection( iSelection );
183 //--------------------------------------------------------------------------
184 void ComboBoxWidget::FillItems( int iSelection, std::vector< std::string > lstIn )
186 int i,size = lstIn.size();
190 // 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
209 #endif // defined(_WIN32)
211 itemcontainer->Clear();
212 for (i=0;i<size; i++)
214 itemcontainer->Append( bbtk::std2wx( lstIn[i] ) );
218 itemcontainer->SetSelection(iSelection);
219 if (mBox->bbGetInputWithSpinButton()==true)
221 mwxspinbutton->SetRange( 0,size-1 );
222 mwxspinbutton->SetValue( iSelection );
229 ((wxChoice*)itemcontainer)->SetAutoLayout( true );
230 ((wxChoice*)itemcontainer)->Layout( );
234 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,ComboBox)
235 BBTK_BLACK_BOX_IMPLEMENTATION(ComboBox,bbtk::WxBlackBox);
238 // 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)
240 void ComboBox::Process()
242 int iSelection = bbGetInputSelection();
243 // if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection=bbGetInputIn().size()-1; }
244 if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection=0; }
245 ComboBoxWidget *w = (ComboBoxWidget*)bbGetOutputWidget();
246 w->FillItems( iSelection, bbGetInputIn() );
247 bbSetInputSelection( iSelection );
248 bbSetOutputOut( iSelection );
249 int size = bbGetInputIn().size();
250 if ( (iSelection>=0) && ( iSelection<size) )
252 bbSetOutputOutString( bbGetInputIn()[ iSelection ] );
254 w->VerifyDeselect(iSelection);
257 // 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)
259 void ComboBox::CreateWidget(wxWindow* parent)
261 ComboBoxWidget *w = new ComboBoxWidget(
264 bbGetInputSelection() ,
268 bbGetInputWinWidth(), bbGetInputWinHeight() );
269 bbSetOutputOut( bbGetInputSelection() );
270 if (bbGetInputIn().size()> bbGetInputSelection() )
272 bbSetOutputOutString( bbGetInputIn()[ bbGetInputSelection() ] );
274 bbSetOutputWidget( w );
277 // 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)
279 void ComboBox::bbUserSetDefaultValues()
281 bbSetInputSelection(0);
284 bbSetInputWinWidth(10);
285 bbSetInputWinHeight(45);
286 bbSetInputDeselect(false);
287 bbSetInputWithSpinButton(false);
290 // 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)
292 void ComboBox::bbUserInitializeProcessing()
296 // 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)
298 void ComboBox::bbUserFinalizeProcessing()