]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxComboBox.cxx
#3416 BBTK Feature New Normal - Patch combobox to show the end of the string item
[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     void VerifyDeselect(int iSelection);
33
34   private:
35     int                         mTypeForm;
36     ComboBox            *mBox;
37         wxItemContainer* itemcontainer;
38   };
39   
40
41
42   //------------------------------------------------------------------------
43   //------------------------------------------------------------------------
44   //------------------------------------------------------------------------
45
46   //-------------------------------------------------------------------------
47   ComboBoxWidget::ComboBoxWidget( ComboBox* box,
48             wxWindow *parent,
49             int iSelection,
50             std::string title,
51             std::vector< std::string > lstIn,
52                         int typeForm,
53                         int sizeX, 
54                         int sizeY
55                 )
56     :
57     wxPanel( parent, -1,wxDefaultPosition ) ,
58     mBox(box),
59     mTypeForm(typeForm)
60   {
61         itemcontainer                   = NULL;
62         wxPanel                 *panel  = this;
63         int                     i;
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     //---------------------------------------------------------------------
74         if (mTypeForm==1)
75         {
76                 wxListBox *wxlistbox = new wxListBox ( panel , -1 , wxDefaultPosition,wxSize(sizeX,sizeY),0, NULL, wxLB_SINGLE );       
77                 itemcontainer=wxlistbox;
78                 sizer->Add( wxlistbox,1,wxEXPAND ); 
79                 Connect( wxlistbox->GetId(), 
80                  wxEVT_COMMAND_LISTBOX_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox ); 
81         } else if (mTypeForm==0) {
82                 wxChoice *wxchoice = new wxChoice ( panel , -1, wxDefaultPosition,wxSize(sizeX,sizeY));
83                 itemcontainer=wxchoice;
84                 sizer->Add( wxchoice,1,wxGROW ); 
85                 Connect( wxchoice->GetId(), 
86                  wxEVT_COMMAND_CHOICE_SELECTED, (wxObjectEventFunction) (void (wxPanel::*)(wxEvent&))&ComboBoxWidget::OnComboBox ); 
87         }
88     sizer->AddGrowableCol(0);
89     panel->SetSizer(sizer);
90         
91
92
93         FillItems( iSelection,lstIn );
94         
95 //      for (i=0;i<lstIn.size(); i++)
96 //      {
97 //              itemcontainer->Append(  bbtk::std2wx( lstIn[i] )  ); 
98 //      } // for i
99 //      itemcontainer->SetSelection(iSelection);
100   }
101
102   //-------------------------------------------------------------------------  
103   ComboBoxWidget::~ComboBoxWidget()
104   {
105   }
106
107   //--------------------------------------------------------------------------
108   void ComboBoxWidget::VerifyDeselect(int iSelection)
109   {
110         if ((iSelection>=0) && (mBox->bbGetInputDeselect()==true) )
111         {
112                 if (mTypeForm==1) 
113                 { 
114                         ((wxListBox*)itemcontainer)->Deselect( iSelection ); 
115                 } // if mTypeForm
116         } // if iSelection
117   }
118
119   //--------------------------------------------------------------------------
120   void ComboBoxWidget::OnComboBoxSelection(int iSelection)
121   {
122         if (iSelection>=0) 
123         {
124                 mBox->bbSetInputSelection( iSelection );
125                 mBox->bbSetOutputOut( iSelection );
126
127 // Patch to clean the spaces at the begining 
128                 std::string tmpStr = bbtk::wx2std( itemcontainer->GetString(iSelection) );
129                 if (tmpStr.length()>0) { while(tmpStr[0]==' ') tmpStr.erase(0,1); }
130                 mBox->bbSetOutputOutString(   tmpStr   );
131 //              mBox->bbSetOutputOutString(   bbtk::wx2std( itemcontainer->GetString(iSelection) )   );
132
133                 mBox->bbSignalOutputModification();
134                 VerifyDeselect(iSelection);
135                 if (mTypeForm==0) 
136                 { 
137                         wxChoice *wxchoise=(wxChoice *)itemcontainer;
138                         wxchoise->SetToolTip( itemcontainer->GetString(iSelection)    );
139                 } // if mTypeForm
140
141
142 //              mBox->bbSignalOutputModification("Out");
143 //              mBox->bbSignalOutputModification("OutString");
144         } // if iSelection
145   }
146
147 //--------------------------------------------------------------------------
148 void ComboBoxWidget::OnComboBox(wxEvent& event)
149 {
150         OnComboBoxSelection( itemcontainer->GetSelection() );
151 }
152
153 //--------------------------------------------------------------------------
154 void ComboBoxWidget::FillItems( int iSelection, std::vector< std::string > lstIn )
155 {
156         int i,size=lstIn.size();
157
158 // Patch to put spaces at the beginin
159         int strLength=-1;
160         // Looking for the longest string
161         for (i=0 ;i<size; i++)
162         {       
163                 if ( strLength < (int) lstIn[i].length() ) { strLength=lstIn[i].length();  }
164         }       // for
165         // Adding spaces at the bigining to the others strings in the list to have the same size
166         int ii,len2;
167         for (i=0 ;i<size; i++)
168         {       
169                 len2 = strLength - lstIn[i].length();
170                 for (ii=0;ii<len2;ii++)
171                 {       
172                         lstIn[i]="   "+lstIn[i];  // 3 space characters
173                 } // for ii
174         } // for i
175         
176         itemcontainer->Clear();
177         for (i=0;i<size; i++)
178         {
179                 itemcontainer->Append(  bbtk::std2wx( lstIn[i] )  ); 
180         } // for i
181         if (iSelection>=0) itemcontainer->SetSelection(iSelection);
182
183         if (mTypeForm==0) 
184         { 
185                 ((wxChoice*)itemcontainer)->SetAutoLayout( true ); 
186                 ((wxChoice*)itemcontainer)->Layout( ); 
187         } // if mTypeForm       
188
189
190 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,ComboBox)
191 BBTK_BLACK_BOX_IMPLEMENTATION(ComboBox,bbtk::WxBlackBox);
192
193 //===== 
194 // 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)
195 //===== 
196 void ComboBox::Process()
197 {
198         int iSelection = bbGetInputSelection();
199         if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection=bbGetInputIn().size()-1; }
200 //      if (bbGetInputSelection() >= bbGetInputIn().size()) { iSelection=0; }
201         ComboBoxWidget *w = (ComboBoxWidget*)bbGetOutputWidget();
202         w->FillItems( iSelection, bbGetInputIn() );
203     bbSetInputSelection( iSelection );
204     bbSetOutputOut( iSelection );
205     int size = bbGetInputIn().size();
206     if  (  (iSelection>=0) && ( iSelection<size) ) 
207     { 
208        bbSetOutputOutString( bbGetInputIn()[ iSelection ] );
209     } // if iSelection
210         w->VerifyDeselect(iSelection);
211 }
212 //===== 
213 // 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)
214 //===== 
215 void ComboBox::CreateWidget(wxWindow* parent)
216 {
217     ComboBoxWidget *w = new ComboBoxWidget(
218                                 this,
219                                                         parent,
220                                 bbGetInputSelection() ,
221                                 bbGetInputTitle(),
222                                 bbGetInputIn(),
223                                 bbGetInputForm(),
224                                                         bbGetInputWinWidth(), bbGetInputWinHeight()  );
225    bbSetOutputOut( bbGetInputSelection() );
226    if (bbGetInputIn().size()> bbGetInputSelection() )
227    {
228            bbSetOutputOutString( bbGetInputIn()[ bbGetInputSelection() ] );
229    } // if InputIn size
230    bbSetOutputWidget( w );
231 }
232 //===== 
233 // 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)
234 //===== 
235 void ComboBox::bbUserSetDefaultValues()
236 {
237         bbSetInputSelection(0);
238         bbSetInputTitle("");
239         bbSetInputForm(0);
240         bbSetInputWinWidth(10);
241         bbSetInputWinHeight(45);
242         bbSetInputDeselect(false);
243 }
244 //===== 
245 // 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)
246 //===== 
247 void ComboBox::bbUserInitializeProcessing()
248 {
249 }
250 //===== 
251 // 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)
252 //===== 
253 void ComboBox::bbUserFinalizeProcessing()
254 {
255
256 }
257 }
258 // EO namespace bbwx
259
260