1 /* wconfig_palette.cc
2 * This file belongs to Worker, a file manager for UN*X/X11.
3 * Copyright (C) 2006,2011 Ralf Hoffmann.
4 * You can contact me at: ralf@boomerangsworld.de
5 * or http://www.boomerangsworld.de/worker
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "wconfig_palette.hh"
23 #include "wconfig.h"
24 #include "wc_color.hh"
25 #include "worker.h"
26 #include <string>
27 #include <sstream>
28 #include "aguix/acontainer.h"
29 #include "aguix/acontainerbb.h"
30 #include "aguix/kartei.h"
31 #include "aguix/slider.h"
32 #include "aguix/button.h"
33 #include "aguix/stringgadget.h"
34 #include "aguix/solidbutton.h"
35 #include "worker_locale.h"
36 #include "simplelist.hh"
37
PalettePanel(AWindow & basewin,WConfig & baseconfig)38 PalettePanel::PalettePanel( AWindow &basewin, WConfig &baseconfig ) : WConfigPanel( basewin, baseconfig )
39 {
40 tempconfig = _baseconfig.duplicate();
41 labelindex = -1;
42 _values_changed = false;
43 }
44
~PalettePanel()45 PalettePanel::~PalettePanel()
46 {
47 delete tempconfig;
48 }
49
create()50 int PalettePanel::create()
51 {
52 int sx;
53 Panel::create();
54
55 AContainer *ac1 = setContainer( new AContainer( this, 1, 2 ), true );
56 ac1->setMinSpace( 5 );
57 ac1->setMaxSpace( 5 );
58
59 addMultiLineText( catalog.getLocale( 671 ),
60 *ac1,
61 0, 0,
62 NULL, NULL );
63
64 AContainer *ac1_1 = ac1->add( new AContainer( this, 2, 1 ), 0, 1 );
65 ac1_1->setMinSpace( 5 );
66 ac1_1->setMaxSpace( 5 );
67 ac1_1->setBorderWidth( 0 );
68
69 sx = (int)sqrt( (double)( tempconfig->getColors()->size() ) );
70 if ( ( sx * sx ) < tempconfig->getColors()->size() ) sx++;
71 AContainerBB *ac1_1_1 = static_cast<AContainerBB*>( ac1_1->add( new AContainerBB( this, sx, sx ), 0, 0 ) );
72 ac1_1_1->setMinSpace( 0 );
73 ac1_1_1->setMaxSpace( 0 );
74
75 int th = 256 / sx;
76 for ( int i = 0; i < tempconfig->getColors()->size(); i++ ) {
77 colbs[i] = (Button*)ac1_1_1->add( new Button( _aguix,
78 0,
79 0,
80 th,
81 th,
82 "",
83 0,
84 i,
85 i + 1 ), i % sx, i / sx, AContainer::CO_FIX );
86 colbs[i]->connect( this );
87 }
88
89 AContainer *ac1_2 = ac1_1->add( new AContainer( this, 1, 3 ), 1, 0 );
90 ac1_2->setMinSpace( 5 );
91 ac1_2->setMaxSpace( 5 );
92 ac1_2->setBorderWidth( 0 );
93
94 AContainer *ac1_2_1 = ac1_2->add( new AContainer( this, 2, 1 ), 0, 0 );
95 ac1_2_1->setMinSpace( 5 );
96 ac1_2_1->setMaxSpace( 5 );
97 ac1_2_1->setBorderWidth( 0 );
98
99 ac1_2_1->add( new Text( _aguix,
100 0,
101 0,
102 catalog.getLocale(27)
103 ), 0, 0, AContainer::CO_FIX );
104 palentryt = (Text*)ac1_2_1->add( new Text( _aguix,
105 0,
106 0,
107 " "
108 ), 1, 0, AContainer::CO_INCWNR );
109
110 AContainer *ac1_2_2 = ac1_2->add( new AContainer( this, 3, 3 ), 0, 1 );
111 ac1_2_2->setMinSpace( 5 );
112 ac1_2_2->setMaxSpace( 5 );
113 ac1_2_2->setBorderWidth( 0 );
114
115 ac1_2_2->add( new Text( _aguix,
116 0,
117 0,
118 catalog.getLocale(28)
119 ), 0, 0, AContainer::CO_FIX );
120 redsg = (StringGadget*)ac1_2_2->add( new StringGadget( _aguix,
121 0,
122 0,
123 _aguix->getTextWidth( "000000" ),
124 "",
125 1 ), 1, 0, AContainer::CO_FIX );
126 redsg->connect( this );
127 redsl = (Slider*)ac1_2_2->add( new Slider( _aguix,
128 0,
129 0,
130 100,
131 20,
132 false,
133 0 ), 2, 0, AContainer::CO_INCW );
134 redsl->connect( this );
135 redsl->setMaxDisplay( 1 );
136 redsl->setMaxLen( 256 );
137 redsl->setDisplayFocus( true );
138 ac1_2_2->add( new Text( _aguix,
139 0,
140 0,
141 catalog.getLocale( 29 )
142 ), 0, 1, AContainer::CO_FIX );
143 greensg = (StringGadget*)ac1_2_2->add( new StringGadget( _aguix,
144 0,
145 0,
146 _aguix->getTextWidth( "000000" ),
147 "",
148 1 ), 1, 1, AContainer::CO_FIX );
149 greensg->connect( this );
150 greensl = (Slider*)ac1_2_2->add( new Slider( _aguix,
151 0,
152 0,
153 100,
154 20,
155 false,
156 0 ), 2, 1, AContainer::CO_INCW );
157 greensl->connect( this );
158 greensl->setMaxDisplay( 1 );
159 greensl->setMaxLen( 256 );
160 greensl->setDisplayFocus( true );
161 ac1_2_2->add( new Text( _aguix,
162 0,
163 0,
164 catalog.getLocale( 30 )
165 ), 0, 2, AContainer::CO_FIX );
166 bluesg = (StringGadget*)ac1_2_2->add( new StringGadget( _aguix,
167 0,
168 0,
169 _aguix->getTextWidth( "000000" ),
170 "",
171 1 ), 1, 2, AContainer::CO_FIX );
172 bluesg->connect( this );
173 bluesl = (Slider*)ac1_2_2->add( new Slider( _aguix,
174 0,
175 0,
176 100,
177 20,
178 false,
179 0 ), 2, 2, AContainer::CO_INCW );
180 bluesl->connect( this );
181 bluesl->setMaxDisplay( 1 );
182 bluesl->setMaxLen( 256 );
183 bluesl->setDisplayFocus( true );
184 editcolsb = (SolidButton*)ac1_2->add( new SolidButton( _aguix,
185 0,
186 0,
187 32,
188 32,
189 "",
190 0,
191 0,
192 false ), 0, 2, AContainer::CO_INCW );
193
194 showColor( 0 );
195 contMaximize( true );
196 return 0;
197 }
198
saveValues()199 int PalettePanel::saveValues()
200 {
201 _baseconfig.setColors( tempconfig->getColors() );
202 return 0;
203 }
204
run(Widget * elem,const AGMessage & msg)205 void PalettePanel::run( Widget *elem, const AGMessage &msg )
206 {
207 List *colors = tempconfig->getColors();
208 char *tstr;
209 WC_Color *tc1;
210
211 if ( _need_recreate == true ) return;
212
213 if ( msg.type == AG_BUTTONCLICKED ) {
214 if ( msg.button.button->getData() > 0 ) {
215 // ein Eintrag wurde ausgew�hlt
216
217 showColor( msg.button.button->getData() - 1 );
218 }
219 } else if(msg.type==AG_STRINGGADGET_DEACTIVATE) {
220 if ( labelindex >= 0 ) {
221 int val = 0;
222 sscanf( msg.stringgadget.sg->getText(), "%d", &val );
223 if ( val < 0 ) val = 0;
224 if ( val > 255 ) val = 255;
225 tstr = (char*)_allocsafe( A_BYTESFORNUMBER( int ) );
226 sprintf( tstr, "%d", val );
227
228 tc1=(WC_Color*)colors->getElementAt(labelindex);
229
230 if ( msg.stringgadget.sg == redsg ) {
231 redsg->setText(tstr);
232 redsl->setOffset( val );
233 tc1->setRed( val );
234 } else if ( msg.stringgadget.sg == greensg ) {
235 greensg->setText(tstr);
236 greensl->setOffset( val );
237 tc1->setGreen( val );
238 } else if ( msg.stringgadget.sg == bluesg ) {
239 bluesg->setText(tstr);
240 bluesl->setOffset( val );
241 tc1->setBlue( val );
242 }
243 _freesafe(tstr);
244
245 _aguix->changeColor( labelindex, tc1->getRed(), tc1->getGreen(), tc1->getBlue() );
246 if ( labelindex > 3 ) {
247 editcolsb->redraw();
248 colbs[labelindex]->redraw();
249 } else redraw();
250
251 _values_changed = true;
252 } else {
253 if(msg.stringgadget.sg==redsg) {
254 redsg->setText("");
255 } else if(msg.stringgadget.sg==greensg) {
256 greensg->setText("");
257 } else if(msg.stringgadget.sg==bluesg) {
258 bluesg->setText("");
259 }
260 }
261 } else if ( msg.type == AG_SLIDER_CHANGED && labelindex >= 0 ) {
262 tstr = (char*)_allocsafe( A_BYTESFORNUMBER( int ) );
263 sprintf( tstr, "%d", msg.slider.offset );
264
265 tc1 = (WC_Color*)colors->getElementAt( labelindex );
266
267 if ( msg.slider.slider == redsl ) {
268 redsg->setText( tstr );
269 tc1->setRed( msg.slider.offset );
270 } else if ( msg.slider.slider == greensl ) {
271 greensg->setText( tstr );
272 tc1->setGreen( msg.slider.offset );
273 } else if ( msg.slider.slider == bluesl ) {
274 bluesg->setText( tstr );
275 tc1->setBlue( msg.slider.offset );
276 }
277
278 _freesafe( tstr );
279
280 _aguix->changeColor( labelindex, tc1->getRed(), tc1->getGreen(), tc1->getBlue() );
281 if ( labelindex > 3 ) {
282 editcolsb->redraw();
283 colbs[labelindex]->redraw();
284 } else redraw();
285
286 _values_changed = true;
287 }
288 }
289
hide()290 void PalettePanel::hide()
291 {
292 if ( _values_changed == true ) {
293 if ( _conf_cb != NULL )
294 _conf_cb->setColors( tempconfig->getColors() );
295 _values_changed = false;
296 }
297 WConfigPanel::hide();
298 }
299
setColors(List * colors)300 WConfigPanel::panel_action_t PalettePanel::setColors( List *colors )
301 {
302 if ( colors != tempconfig->getColors() ) {
303 tempconfig->setColors( colors );
304
305 _need_recreate = true;
306 return PANEL_RECREATE;
307 }
308 return PANEL_NOACTION;
309 }
310
showColor(int col)311 void PalettePanel::showColor( int col )
312 {
313 List *colors = tempconfig->getColors();
314 WC_Color *tc1;
315
316 if ( col < 0 || col >= colors->size() ) return;
317
318 labelindex = col;
319
320 editcolsb->setBG( labelindex );
321 tc1 = (WC_Color*)colors->getElementAt( labelindex );
322
323 {
324 std::stringstream redval;
325 redval << tc1->getRed();
326 redsg->setText( redval.str().c_str() );
327 redsl->setOffset( tc1->getRed() );
328 }
329
330 {
331 std::stringstream greenval;
332 greenval << tc1->getGreen();
333 greensg->setText( greenval.str().c_str() );
334 greensl->setOffset( tc1->getGreen() );
335 }
336
337 {
338 std::stringstream blueval;
339 blueval << tc1->getBlue();
340 bluesg->setText( blueval.str().c_str() );
341 bluesl->setOffset( tc1->getBlue() );
342 }
343
344 {
345 std::stringstream entryval;
346 entryval << labelindex;
347 palentryt->setText( entryval.str().c_str() );
348 }
349 }
350