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