]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxBitmapButton.cxx
#3054 BBTK Bug New Normal - package wx - combo box. crash with empty input
[bbtk.git] / packages / wx / src / bbwxBitmapButton.cxx
1 #include "bbwxBitmapButton.h"
2 #include "bbwxPackage.h"
3 #include "bbtkInterpreter.h"
4 #include "bbtkExecuter.h"
5 namespace bbwx
6 {
7   BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,BitmapButton)
8   BBTK_BLACK_BOX_IMPLEMENTATION(BitmapButton,bbtk::WxBlackBox);
9
10   void BitmapButton::Process()
11   {
12     BitmapButtonWidget* w = (BitmapButtonWidget*)bbGetOutputWidget();
13     if (w) 
14     {
15       UpdateColour();
16       UpdateLabel();
17       UpdateIcon();
18     }
19   }
20
21   void BitmapButton::CreateWidget(wxWindow* parent)
22   {
23     bbSetOutputWidget(
24       new BitmapButtonWidget(
25         this,
26         parent,
27         bbGetInputBitmap(),
28         bbtk::std2wx(bbGetInputLabel())
29       )
30     );
31     UpdateColour();
32   }
33
34   void BitmapButton::bbUserSetDefaultValues()
35   {
36     bbSetInputIn("");
37     bbSetInputBitmap(NULL);
38     bbSetInputLabel("");
39     colorVector defaultColor;
40     defaultColor.push_back(0.75);
41     defaultColor.push_back(0.75);
42     defaultColor.push_back(0.75);
43     bbSetInputColour(defaultColor);
44     bbSetOutputWidget(0);
45   }
46
47   void BitmapButton::bbUserInitializeProcessing()
48   {
49
50   }
51
52   void BitmapButton::bbUserFinalizeProcessing()
53   {
54
55   }
56
57   void BitmapButton::UpdateColour()
58   {
59     BitmapButtonWidget* w = (BitmapButtonWidget*)bbGetOutputWidget();
60     if ((bbGetInputColour()[0] == -1) && 
61         (bbGetInputColour()[1] == -1) &&
62         (bbGetInputColour()[2] == -1)
63        )
64     {
65       w->SetColour( w->GetParent()->GetBackgroundColour() );
66     } 
67     else 
68     {
69       int r=(int) (255*bbGetInputColour()[0]);
70       int g=(int) (255*bbGetInputColour()[1]);
71       int b=(int) (255*bbGetInputColour()[2]);
72       w->SetColour( wxColour(r,g,b) );
73     }
74
75   } 
76   
77   void BitmapButton::UpdateLabel()
78   {
79     BitmapButtonWidget* w = (BitmapButtonWidget*)bbGetOutputWidget();
80     w->SetLabel( bbtk::std2wx( bbGetInputLabel() ) );
81   }   
82
83   void BitmapButton::UpdateIcon()
84   {
85     if(bbGetInputBitmap() != NULL)
86     {
87       BitmapButtonWidget* w = (BitmapButtonWidget*)bbGetOutputWidget();
88       w->SetIcon(bbGetInputBitmap());
89     }
90   }
91
92 //--------------------------------------------------------------------------
93
94   BitmapButtonWidget::BitmapButtonWidget(
95     BitmapButton* bx,
96     wxWindow* parent,
97     wxBitmap* bitmap,
98     wxString title
99     ) : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
100       box(bx)
101   { 
102     this->title = title;
103     wxPanel* panel  = this;
104     sizer = new wxBoxSizer(wxHORIZONTAL);
105     
106     if(bitmap != NULL)
107     {
108       button = new wxBitmapButton(panel, -1, *bitmap, wxDefaultPosition, wxSize(bitmap->GetWidth(),bitmap->GetHeight()));
109
110       sizer -> Add(button, 0, wxALL | wxALIGN_CENTER, 3);
111       sizer -> Add(new wxStaticText(panel,-1,title), 0, wxALL | wxALIGN_CENTER, 3);  
112     }
113     else
114     {
115       button = new wxButton( panel, -1, title);
116       sizer -> Add(button,1,wxEXPAND | wxALL | wxALIGN_CENTER, 5); 
117     }
118     Connect( button->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , 
119        (wxObjectEventFunction) 
120        (void (wxPanel::*)(wxEvent&))
121        &BitmapButtonWidget::OnClick ); 
122     
123     
124     
125     panel -> SetSizer(sizer);
126     sizer->Fit(panel);
127   }
128   
129   BitmapButtonWidget::~BitmapButtonWidget()
130   {
131   }
132   
133   
134   void BitmapButtonWidget::OnClick( wxEvent& )
135   {
136     // Look for the interpreter or the executer if no interpreter
137     bbtk::Interpreter::Pointer I;
138     bbtk::VirtualExec::Pointer E;
139       if (box->bbGetParent() != 0) 
140   {
141     bbtk::Factory::Pointer f = boost::dynamic_pointer_cast<bbtk::ComplexBlackBoxDescriptor>(box->bbGetParent()->bbGetDescriptor())->GetFactory();
142     if ((f != 0)&&
143         (f->GetExecuter()))
144       {
145         E = f->GetExecuter();
146         I = E->GetInterpreter();
147       }
148   }
149       if (I==0) 
150   {
151     //    bbtkError("CommandButton::DoProcess() : could not find interpreter");
152     if (E==0) 
153       {
154         // If no executer : create a totally independant interpreter
155         I = bbtk::Interpreter::New();
156       }
157     else 
158       {
159         // If executer : create an interpreter using E
160         I = bbtk::Interpreter::New(E);
161       }
162   }
163       
164       std::string commandstr(box->bbGetInputIn());
165     
166     //  bbtk::Interpreter::mGlobalInterpreter->InterpretLine( commandstr );
167     unsigned int i;
168     bool ok=true;
169     int pos1=0,pos2;
170     pos2 = commandstr.find(";",pos1);
171     std::string ccommand;
172     while (ok==true)
173       {
174   if (pos2==-1) 
175     {
176       ok=false;
177       ccommand=commandstr.substr(pos1,commandstr.length()-pos1 );
178     } 
179   else 
180     {
181       ccommand=commandstr.substr(pos1,pos2-pos1);
182     }
183   for ( i=0 ; i < ccommand.length() ; i++)
184     {
185       if (ccommand[i]==39) // '
186         {
187     ccommand[i]=34;  // "
188         }
189     }   
190   I->InterpretLine( ccommand );
191   pos1=pos2+1;
192   pos2 = commandstr.find(";",pos2+1);
193   
194       }
195    
196     box->UpdateLabel();
197     box->UpdateColour();
198     box->bbSignalOutputModification();
199   }
200
201   void BitmapButtonWidget::SetLabel(wxString title)
202   {
203     button->SetLabel(title);
204   }
205   
206   void BitmapButtonWidget::SetColour(wxColour color)
207   {
208     button->SetBackgroundColour(color);
209   }
210
211   void BitmapButtonWidget::SetIcon( wxBitmap* bitmap)
212   {
213     if(bitmap != NULL)
214     {
215       wxPanel* panel  = this;
216       sizer->Clear();
217       button->Destroy();
218
219       button = new wxBitmapButton(panel, -1, *bitmap, wxDefaultPosition, wxSize(bitmap->GetWidth(),bitmap->GetHeight()));
220       
221       Connect( button->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , 
222        (wxObjectEventFunction) 
223        (void (wxPanel::*)(wxEvent&))
224        &BitmapButtonWidget::OnClick ); 
225       
226       sizer->Add(button, 0, wxALL | wxALIGN_CENTER, 3);
227       sizer -> Add(new wxStaticText(panel,-1,title), 0, wxALL | wxALIGN_CENTER, 3);  
228       sizer->RecalcSizes();
229     }
230   }
231 }
232 // EO namespace bbwx
233
234