]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxColourSelectorButton.cxx
*** empty log message ***
[bbtk.git] / packages / wx / src / bbwxColourSelectorButton.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbwxColourSelectorButton.cxx,v $
4   Language:  C++
5   Date:      $Date: 2008/12/12 08:55:23 $
6   Version:   $Revision: 1.9 $
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   void ColourSelectorButton::bbUserConstructor() 
140   { 
141   // Default input value
142     bbSetInputIn("1 1 1");
143   // Initial output value = the same than input
144     bbSetOutputOut("1 1 1");
145     // IMPORTANT : Initialize output widget to null pointer to test it after 
146     bbSetOutputWidget(0);
147   }
148
149
150   void ColourSelectorButton::Process() 
151   { 
152   // The widget may not be created : have to test it before using it
153     wxColourPickerCtrlWidget* w = ( wxColourPickerCtrlWidget* )bbGetOutputWidget();
154     if (w) 
155       {
156         w->UpdateBox();
157       }
158     else 
159       {
160       // If widget does not exist yet you have to update the output value according 
161       // to the input (which may have changed if the user set it)
162         bbSetOutputOut(bbGetInputIn());
163       }
164   }
165
166
167   /**
168    * \brief  Create wxWidget .
169    *
170    *
171    */
172   void ColourSelectorButton::CreateWidget(wxWindow* parent)
173   {
174     
175     float r,g,b;
176     sscanf( bbGetInputIn().c_str(), "%f %f %f", &r, &g ,&b);
177     unsigned char cr,cg,cb;
178     cr = (unsigned char)(255.*r);
179     cg = (unsigned char)(255.*g);
180     cb = (unsigned char)(255.*b);
181
182     wxColourPickerCtrlWidget* w = new wxColourPickerCtrlWidget(this, //bbGetWxParent() , 
183                                                                 parent,
184                                                                 cr , cg , cb );    
185     
186     bbSetOutputWidget( w );
187   }
188
189
190 }  //namespace bbwx
191
192 #endif  // _USE_WXWIDGETS_
193