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 //---------------------------------------------------------------------
86 wxListBox *wxlistbox = new wxListBox ( panel , -1 , wxDefaultPosition,wxSize(sizeX,sizeY),0, NULL, wxLB_SINGLE );
87 itemcontainer=wxlistbox;
88 sizer->Add( wxlistbox,1,wxEXPAND );
89 Connect( wxlistbox->GetId(),
90 wxEVT_COMMAND_LISTBOX_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox );
91 } else if (mTypeForm==0) {
92 wxChoice *wxchoice = new wxChoice ( panel , -1, wxDefaultPosition,wxSize(sizeX,sizeY));
93 itemcontainer=wxchoice;
94 sizer->Add( wxchoice,1,wxGROW );
95 Connect( wxchoice->GetId(),
96 wxEVT_COMMAND_CHOICE_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox );
99 if (mBox->bbGetInputWithSpinButton()==true)
101 mwxspinbutton = new wxSpinButton( panel, -1 );
102 Connect( mwxspinbutton->GetId(),
104 (wxObjectEventFunction)
105 (void (wxPanel::*)(wxScrollEvent&))
106 &ComboBoxWidget::OnSpinCtrlClick);
107 sizer->Add( mwxspinbutton,1,wxEXPAND );
109 sizer->AddGrowableCol(0);
111 panel->SetSizer(sizer);
112 FillItems( iSelection,lstIn );
115 //-------------------------------------------------------------------------
116 ComboBoxWidget::~ComboBoxWidget()
120 //--------------------------------------------------------------------------
121 void ComboBoxWidget::VerifyDeselect(int iSelection)
123 if ((iSelection>=0) && (mBox->bbGetInputDeselect()==true) )
127 ((wxListBox*)itemcontainer)->Deselect( iSelection );
132 //--------------------------------------------------------------------------
133 void ComboBoxWidget::OnComboBoxSelection(int iSelection)
137 mBox->bbSetInputSelection( iSelection );
138 mBox->bbSetOutputOut( iSelection );
140 // Patch to clean the spaces at the begining
141 std::string tmpStr = bbtk::wx2std( itemcontainer->GetString(iSelection) );
142 if (tmpStr.length()>0) { while(tmpStr[0]==' ') tmpStr.erase(0,1); }
143 mBox->bbSetOutputOutString( tmpStr );
144 // mBox->bbSetOutputOutString( bbtk::wx2std( itemcontainer->GetString(iSelection) ) );
146 mBox->bbSignalOutputModification();
147 VerifyDeselect(iSelection);
150 wxChoice *wxchoise=(wxChoice *)itemcontainer;
151 wxchoise->SetToolTip( itemcontainer->GetString(iSelection) );
155 // mBox->bbSignalOutputModification("Out");
156 // mBox->bbSignalOutputModification("OutString");
160 //--------------------------------------------------------------------------
161 void ComboBoxWidget::OnComboBox(wxEvent& event)
163 int iSelection = itemcontainer->GetSelection();
164 OnComboBoxSelection( iSelection );
165 if (mBox->bbGetInputWithSpinButton()==true)
167 mwxspinbutton->SetValue( iSelection );
172 //--------------------------------------------------------------------------
173 void ComboBoxWidget::OnSpinCtrlClick(wxCommandEvent& event)
175 int iSelection = mwxspinbutton->GetValue();
176 itemcontainer->SetSelection(iSelection);
177 OnComboBoxSelection( iSelection );
181 //--------------------------------------------------------------------------
182 void ComboBoxWidget::FillItems( int iSelection, std::vector< std::string > lstIn )
184 int i,size = lstIn.size();
188 // Patch to put spaces at the beginin
191 // Patch to put spaces at the beginin
193 // Looking for the longest string
194 for (i=0 ;i<size; i++)
196 if ( strLength < (int) lstIn[i].length() ) { strLength=lstIn[i].length(); }
198 // Adding spaces at the bigining to the others strings in the list to have the same size
200 for (i=0 ;i<size; i++)
202 len2 = strLength - lstIn[i].length();
203 for (ii=0;ii<len2;ii++)
205 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()