]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxColourSelectorButton.cxx
e0c22782e32490e69986645ee7a53ec9220bee25
[bbtk.git] / packages / wx / src / bbwxColourSelectorButton.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbwxColourSelectorButton.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:52:14 $
33   Version:   $Revision: 1.11 $
34 =========================================================================*/
35
36 /**
37  * \file
38  * \brief Short description in one line
39  *
40  * Long
41  * description
42  *
43  */
44
45 #ifdef _USE_WXWIDGETS_
46
47 #include "bbwxColourSelectorButton.h"
48 #include "bbwxPackage.h"
49
50 #include <wx/clrpicker.h>
51
52 namespace bbwx
53 {
54 //-------------------------------------------------------------------------
55 class wxColourPickerCtrlWidget :public wxPanel
56 {
57
58 public:
59    wxColourPickerCtrlWidget( ColourSelectorButton* box,
60                              wxWindow *parent,
61                              unsigned char  cr,
62                              unsigned char  cg,
63                              unsigned char  cb  );
64
65    ~wxColourPickerCtrlWidget();
66    void OnColorChange(wxColourPickerEvent& e);
67    void UpdateBox();
68 private:
69    wxColourPickerCtrl *picker;
70    ColourSelectorButton *mBox;
71 };
72 //-------------------------------------------------------------------------
73
74 //-------------------------------------------------------------------------
75 wxColourPickerCtrlWidget::wxColourPickerCtrlWidget(     ColourSelectorButton* box,
76                                                                                                         wxWindow *parent, 
77                                                                                                         unsigned char  cr,
78                                                                                                         unsigned char  cg,
79                                                                                                         unsigned char  cb
80                                                                                                         )
81   :   wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
82           mBox(box)
83 {
84     bbtkDebugMessage("Process",9,"=> wxColourPickerCtrlWidget::wxColourPickerCtrlWidget"<<std::endl);
85
86     wxPanel * panel = this;
87     picker = new wxColourPickerCtrl(panel,-1,
88                                         wxColour(cr,cg,cb),
89                                         wxDefaultPosition,
90                                         wxDefaultSize,
91                                         wxCLRP_USE_TEXTCTRL
92                                 );
93     picker->SetPickerCtrlGrowable(false);
94     picker->SetTextCtrlGrowable(false);
95
96     Connect ( picker->GetId(),
97               wxEVT_COMMAND_COLOURPICKER_CHANGED,
98               wxColourPickerEventHandler( wxColourPickerCtrlWidget::OnColorChange ) );
99
100     bbtkDebugMessage("Process",9,"<= wxColourPickerCtrlWidget::wxColourPickerCtrlWidget"<<std::endl);
101     wxFlexGridSizer *sizer = new wxFlexGridSizer(10);
102         sizer -> AddGrowableRow(0);
103         sizer -> Add( picker, 1, wxEXPAND, 0 );
104         panel -> SetSizer(sizer);
105   }
106
107 wxColourPickerCtrlWidget::~wxColourPickerCtrlWidget()
108 {
109 }
110
111
112 //---------------------------------------------------------------------
113 void wxColourPickerCtrlWidget::OnColorChange(wxColourPickerEvent& e)
114 {
115         UpdateBox();
116 }
117
118
119 //---------------------------------------------------------------------
120 void wxColourPickerCtrlWidget::UpdateBox()
121 {
122     char col[100];
123     wxColour c = picker->GetColour();
124     sprintf(col,"%f %f %f",c.Red()/255.,c.Green()/255.,c.Blue()/255.);
125
126     //    std::cout << col << std::endl;
127
128     ColourSelectorButton* b = (ColourSelectorButton*)mBox;
129     b->bbSetOutputOut( col );
130     b->bbSignalOutputModification("Out");
131 }
132
133   //--------------------------------------------------------------------------
134   //-------------------------------------------------------------------------
135   // WxBlackBox implementation
136   //--------------------------------------------------------------------------
137   //--------------------------------------------------------------------------
138
139   //--------------------------------------------------------------------------
140
141   BBTK_BLACK_BOX_IMPLEMENTATION(ColourSelectorButton,bbtk::WxBlackBox);
142   BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,ColourSelectorButton);
143   
144         //-----------------------------------------------------------------     
145         void ColourSelectorButton::bbUserSetDefaultValues()
146         {
147                 // Default input value
148                 bbSetInputIn("1 1 1");
149                 // Initial output value = the same than input
150                 bbSetOutputOut("1 1 1");
151                 // IMPORTANT : Initialize output widget to null pointer to test it after 
152                 bbSetOutputWidget(0);
153         }
154         
155         //-----------------------------------------------------------------     
156         void ColourSelectorButton::bbUserInitializeProcessing()
157         {
158         }
159         
160         //-----------------------------------------------------------------     
161         void ColourSelectorButton::bbUserFinalizeProcessing()
162         {
163         }       
164
165   void ColourSelectorButton::Process() 
166   { 
167   // The widget may not be created : have to test it before using it
168     wxColourPickerCtrlWidget* w = ( wxColourPickerCtrlWidget* )bbGetOutputWidget();
169     if (w) 
170       {
171         w->UpdateBox();
172       }
173     else 
174       {
175       // If widget does not exist yet you have to update the output value according 
176       // to the input (which may have changed if the user set it)
177         bbSetOutputOut(bbGetInputIn());
178       }
179   }
180
181
182   /**
183    * \brief  Create wxWidget .
184    *
185    *
186    */
187   void ColourSelectorButton::CreateWidget(wxWindow* parent)
188   {
189     
190     float r,g,b;
191     sscanf( bbGetInputIn().c_str(), "%f %f %f", &r, &g ,&b);
192     unsigned char cr,cg,cb;
193     cr = (unsigned char)(255.*r);
194     cg = (unsigned char)(255.*g);
195     cb = (unsigned char)(255.*b);
196
197     wxColourPickerCtrlWidget* w = new wxColourPickerCtrlWidget(this, //bbGetWxParent() , 
198                                                                 parent,
199                                                                 cr , cg , cb );    
200     
201     bbSetOutputWidget( w );
202   }
203
204
205 }  //namespace bbwx
206
207 #endif  // _USE_WXWIDGETS_
208