From 1b84c2e38d8374e519af4d793dbc54c427d5490a Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Date: Thu, 2 May 2013 15:10:52 +0200 Subject: [PATCH] Feature #2006 Black Box for BitmapButtons A bitmap button is a control that contains a bitmap. If no bitmap is provided, then a label is used instead. --- packages/wx/src/bbwxBitmapButton.cxx | 230 +++++++++++++++++++++++++++ packages/wx/src/bbwxBitmapButton.h | 76 +++++++++ 2 files changed, 306 insertions(+) create mode 100644 packages/wx/src/bbwxBitmapButton.cxx create mode 100644 packages/wx/src/bbwxBitmapButton.h diff --git a/packages/wx/src/bbwxBitmapButton.cxx b/packages/wx/src/bbwxBitmapButton.cxx new file mode 100644 index 0000000..652ac4f --- /dev/null +++ b/packages/wx/src/bbwxBitmapButton.cxx @@ -0,0 +1,230 @@ +#include "bbwxBitmapButton.h" +#include "bbwxPackage.h" +#include "bbtkInterpreter.h" +#include "bbtkExecuter.h" +namespace bbwx +{ + BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,BitmapButton) + BBTK_BLACK_BOX_IMPLEMENTATION(BitmapButton,bbtk::WxBlackBox); + + void BitmapButton::Process() + { + BitmapButtonWidget* w = (BitmapButtonWidget*)bbGetOutputWidget(); + if (w) + { + UpdateColour(); + UpdateLabel(); + UpdateIcon(); + } + } + + void BitmapButton::CreateWidget(wxWindow* parent) + { + bbSetOutputWidget( + new BitmapButtonWidget( + this, + parent, + bbGetInputBitmap(), + bbtk::std2wx(bbGetInputLabel()) + ) + ); + UpdateColour(); + } + + void BitmapButton::bbUserSetDefaultValues() + { + bbSetInputIn(""); + bbSetInputBitmap(NULL); + bbSetInputLabel(""); + colorVector defaultColor; + defaultColor.push_back(0.75); + defaultColor.push_back(0.75); + defaultColor.push_back(0.75); + bbSetInputColour(defaultColor); + bbSetOutputWidget(0); + } + + void BitmapButton::bbUserInitializeProcessing() + { + + } + + void BitmapButton::bbUserFinalizeProcessing() + { + + } + + void BitmapButton::UpdateColour() + { + BitmapButtonWidget* w = (BitmapButtonWidget*)bbGetOutputWidget(); + if ((bbGetInputColour()[0] == -1) && + (bbGetInputColour()[1] == -1) && + (bbGetInputColour()[2] == -1) + ) + { + w->SetColour( w->GetParent()->GetBackgroundColour() ); + } + else + { + int r=(int) (255*bbGetInputColour()[0]); + int g=(int) (255*bbGetInputColour()[1]); + int b=(int) (255*bbGetInputColour()[2]); + w->SetColour( wxColour(r,g,b) ); + } + + } + + void BitmapButton::UpdateLabel() + { + BitmapButtonWidget* w = (BitmapButtonWidget*)bbGetOutputWidget(); + w->SetLabel( bbtk::std2wx( bbGetInputLabel() ) ); + } + + void BitmapButton::UpdateIcon() + { + if(bbGetInputBitmap() != NULL) + { + BitmapButtonWidget* w = (BitmapButtonWidget*)bbGetOutputWidget(); + w->SetIcon(bbGetInputBitmap()); + } + } + +//-------------------------------------------------------------------------- + + BitmapButtonWidget::BitmapButtonWidget( + BitmapButton* bx, + wxWindow* parent, + wxBitmap* bitmap, + wxString title + ) : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL), + box(bx) + { + + wxPanel* panel = this; + sizer = new wxBoxSizer(wxHORIZONTAL); + + if(bitmap != NULL) + { + button = new wxBitmapButton(panel, -1, *bitmap, wxDefaultPosition, wxSize(bitmap->GetWidth(),bitmap->GetHeight())); + std::cout << "Size: " << bitmap->GetWidth() << " " << bitmap->GetHeight() << std::endl; + } + else + { + button = new wxButton( panel, -1, title); + } + Connect( button->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , + (wxObjectEventFunction) + (void (wxPanel::*)(wxEvent&)) + &BitmapButtonWidget::OnClick ); + + sizer -> Add(button,1,wxEXPAND | wxALL, 5); + + panel -> SetSizer(sizer); + sizer->Fit(panel); + } + + BitmapButtonWidget::~BitmapButtonWidget() + { + } + + + void BitmapButtonWidget::OnClick( wxEvent& ) + { + // Look for the interpreter or the executer if no interpreter + bbtk::Interpreter::Pointer I; + bbtk::VirtualExec::Pointer E; + if (box->bbGetParent() != 0) + { + bbtk::Factory::Pointer f = boost::dynamic_pointer_cast(box->bbGetParent()->bbGetDescriptor())->GetFactory(); + if ((f != 0)&& + (f->GetExecuter())) + { + E = f->GetExecuter(); + I = E->GetInterpreter(); + } + } + if (I==0) + { + // bbtkError("CommandButton::DoProcess() : could not find interpreter"); + if (E==0) + { + // If no executer : create a totally independant interpreter + I = bbtk::Interpreter::New(); + } + else + { + // If executer : create an interpreter using E + I = bbtk::Interpreter::New(E); + } + } + + std::string commandstr(box->bbGetInputIn()); + + // bbtk::Interpreter::mGlobalInterpreter->InterpretLine( commandstr ); + unsigned int i; + bool ok=true; + int pos1=0,pos2; + pos2 = commandstr.find(";",pos1); + std::string ccommand; + while (ok==true) + { + if (pos2==-1) + { + ok=false; + ccommand=commandstr.substr(pos1,commandstr.length()-pos1 ); + } + else + { + ccommand=commandstr.substr(pos1,pos2-pos1); + } + for ( i=0 ; i < ccommand.length() ; i++) + { + if (ccommand[i]==39) // ' + { + ccommand[i]=34; // " + } + } + I->InterpretLine( ccommand ); + pos1=pos2+1; + pos2 = commandstr.find(";",pos2+1); + + } + + box->UpdateLabel(); + box->UpdateColour(); + box->bbSignalOutputModification(); + } + + void BitmapButtonWidget::SetLabel(wxString title) + { + button->SetLabel(title); + } + + void BitmapButtonWidget::SetColour(wxColour color) + { + button->SetBackgroundColour(color); + } + + void BitmapButtonWidget::SetIcon( wxBitmap* bitmap) + { + if(bitmap != NULL) + { + wxPanel* panel = this; + sizer->Remove(0); + button->Destroy(); + + button = new wxBitmapButton(panel, -1, *bitmap, wxDefaultPosition, wxSize(bitmap->GetWidth(),bitmap->GetHeight())); + + Connect( button->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , + (wxObjectEventFunction) + (void (wxPanel::*)(wxEvent&)) + &BitmapButtonWidget::OnClick ); + + sizer->Add(button, 0, wxALL, 5); + sizer->RecalcSizes(); + } + } +} +// EO namespace bbwx + + diff --git a/packages/wx/src/bbwxBitmapButton.h b/packages/wx/src/bbwxBitmapButton.h new file mode 100644 index 0000000..a1bdb26 --- /dev/null +++ b/packages/wx/src/bbwxBitmapButton.h @@ -0,0 +1,76 @@ +//===== +// 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) +//===== +#ifdef _USE_WXWIDGETS_ +#ifndef __bbwxBitmapButton_h_INCLUDED__ +#define __bbwxBitmapButton_h_INCLUDED__ +#include "bbwx_EXPORT.h" +#include "bbtkWxBlackBox.h" + +namespace bbwx +{ + class bbwx_EXPORT BitmapButton : public bbtk::WxBlackBox + { + friend class BitmapButtonWidget; + + BBTK_BLACK_BOX_INTERFACE(BitmapButton,bbtk::WxBlackBox); + + BBTK_DECLARE_INPUT(In, std::string ); + BBTK_DECLARE_INPUT(Bitmap, wxBitmap* ); + BBTK_DECLARE_INPUT(Label, std::string ); + BBTK_DECLARE_INPUT(Colour, std::vector ); + + + BBTK_PROCESS(Process); + void Process(); + BBTK_CREATE_WIDGET(CreateWidget); + void CreateWidget(wxWindow*); + + void UpdateColour(); + void UpdateLabel(); + void UpdateIcon(); + + }; + + class BitmapButtonWidget : public wxPanel + { + public: + BitmapButtonWidget( + BitmapButton* box, + wxWindow *parent, + wxBitmap* bitmap = NULL, + wxString title = wxT("") + ); + ~BitmapButtonWidget(); + void OnClick( wxEvent& ); + void SetLabel(wxString title); + void SetColour(wxColour color); + void SetIcon( wxBitmap* bitmap); + + private: + BitmapButton* box; + wxBoxSizer* sizer; + wxButton * button; + }; + + typedef std::vector colorVector; + + BBTK_BEGIN_DESCRIBE_BLACK_BOX(BitmapButton,bbtk::WxBlackBox); + BBTK_NAME("BitmapButton"); + BBTK_AUTHOR("daniel.gonzalez@creatis.insa-lyon.fr"); + BBTK_DESCRIPTION("A bitmap button is a control that contains a bitmap. If no bitmap is provided, then a label is used instead."); + //BBTK_CATEGORY("widget"); + BBTK_INPUT(BitmapButton,In,"Commands to be executed separated by commas (;). Each single quote (') is replaced by a double quote (\").",std::string,""); + BBTK_INPUT(BitmapButton,Bitmap,"Bitmap image of the button",wxBitmap*,""); + BBTK_INPUT(BitmapButton,Label,"Label of the button",std::string,""); + BBTK_INPUT(BitmapButton,Colour,"Button background color. By default (-1, -1, -1)",colorVector,"color"); + BBTK_END_DESCRIBE_BLACK_BOX(BitmapButton); +//===== +// 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) +//===== +} +// EO namespace bbwx + +#endif // __bbwxBitmapButton_h_INCLUDED__ +#endif // _USE_WXWIDGETS_ + -- 2.45.1