]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxBitmapButton.cxx
Feature #2006 Black Box for BitmapButtons
[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
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       std::cout << "Size: " << bitmap->GetWidth() << " " << bitmap->GetHeight() << std::endl;
110     }
111     else
112     {
113       button = new wxButton( panel, -1, title);
114     }
115     Connect( button->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , 
116        (wxObjectEventFunction) 
117        (void (wxPanel::*)(wxEvent&))
118        &BitmapButtonWidget::OnClick ); 
119     
120     sizer -> Add(button,1,wxEXPAND | wxALL, 5); 
121     
122     panel -> SetSizer(sizer);
123     sizer->Fit(panel);
124   }
125   
126   BitmapButtonWidget::~BitmapButtonWidget()
127   {
128   }
129   
130   
131   void BitmapButtonWidget::OnClick( wxEvent& )
132   {
133     // Look for the interpreter or the executer if no interpreter
134     bbtk::Interpreter::Pointer I;
135     bbtk::VirtualExec::Pointer E;
136       if (box->bbGetParent() != 0) 
137   {
138     bbtk::Factory::Pointer f = boost::dynamic_pointer_cast<bbtk::ComplexBlackBoxDescriptor>(box->bbGetParent()->bbGetDescriptor())->GetFactory();
139     if ((f != 0)&&
140         (f->GetExecuter()))
141       {
142         E = f->GetExecuter();
143         I = E->GetInterpreter();
144       }
145   }
146       if (I==0) 
147   {
148     //    bbtkError("CommandButton::DoProcess() : could not find interpreter");
149     if (E==0) 
150       {
151         // If no executer : create a totally independant interpreter
152         I = bbtk::Interpreter::New();
153       }
154     else 
155       {
156         // If executer : create an interpreter using E
157         I = bbtk::Interpreter::New(E);
158       }
159   }
160       
161       std::string commandstr(box->bbGetInputIn());
162     
163     //  bbtk::Interpreter::mGlobalInterpreter->InterpretLine( commandstr );
164     unsigned int i;
165     bool ok=true;
166     int pos1=0,pos2;
167     pos2 = commandstr.find(";",pos1);
168     std::string ccommand;
169     while (ok==true)
170       {
171   if (pos2==-1) 
172     {
173       ok=false;
174       ccommand=commandstr.substr(pos1,commandstr.length()-pos1 );
175     } 
176   else 
177     {
178       ccommand=commandstr.substr(pos1,pos2-pos1);
179     }
180   for ( i=0 ; i < ccommand.length() ; i++)
181     {
182       if (ccommand[i]==39) // '
183         {
184     ccommand[i]=34;  // "
185         }
186     }   
187   I->InterpretLine( ccommand );
188   pos1=pos2+1;
189   pos2 = commandstr.find(";",pos2+1);
190   
191       }
192    
193     box->UpdateLabel();
194     box->UpdateColour();
195     box->bbSignalOutputModification();
196   }
197
198   void BitmapButtonWidget::SetLabel(wxString title)
199   {
200     button->SetLabel(title);
201   }
202   
203   void BitmapButtonWidget::SetColour(wxColour color)
204   {
205     button->SetBackgroundColour(color);
206   }
207
208   void BitmapButtonWidget::SetIcon( wxBitmap* bitmap)
209   {
210     if(bitmap != NULL)
211     {
212       wxPanel* panel  = this;
213       sizer->Remove(0);
214       button->Destroy();
215
216       button = new wxBitmapButton(panel, -1, *bitmap, wxDefaultPosition, wxSize(bitmap->GetWidth(),bitmap->GetHeight()));
217       
218       Connect( button->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , 
219        (wxObjectEventFunction) 
220        (void (wxPanel::*)(wxEvent&))
221        &BitmapButtonWidget::OnClick ); 
222       
223       sizer->Add(button, 0, wxALL, 5);
224       sizer->RecalcSizes();
225     }
226   }
227 }
228 // EO namespace bbwx
229
230