Change the "Flipbook Dialog" string to the more user intuitive "Preview Window"
[synfig.git] / synfig-studio / trunk / src / gtkmm / widget_value.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file widget_value.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include <gtkmm/label.h>
33 #include "widget_value.h"
34 #include <ETL/stringf>
35 #include <gtkmm/celleditable.h>
36 #include <gtkmm/editable.h>
37 #include <gtkmm/entry.h>
38 #include <gtkmm/eventbox.h>
39 #include <gtk/gtkentry.h> /* see XXX below */
40 #include "app.h"
41
42
43 #include "widget_vector.h"
44 #include "widget_filename.h"
45 #include "widget_enum.h"
46 #include "widget_coloredit.h"
47 #include "widget_canvaschooser.h"
48 #include "widget_time.h"
49 #include "app.h"
50 #include "widget_distance.h"
51
52 #include "general.h"
53
54 #endif
55
56 using namespace synfig;
57 using namespace etl;
58 using namespace std;
59 using namespace studio;
60
61 /* === M A C R O S ========================================================= */
62
63 #define DIGITS          15
64
65 /* === G L O B A L S ======================================================= */
66
67 /* === P R O C E D U R E S ================================================= */
68
69 /* === M E T H O D S ======================================================= */
70
71 Widget_ValueBase::Widget_ValueBase():
72         Glib::ObjectBase        (typeid(Widget_ValueBase)),
73         Gtk::HBox(),
74         real_adjustment(0,-2000000000,2000000000,0.05,0.05,0),
75         integer_adjustment(0,-2000000000,2000000000,1,1,0),
76         angle_adjustment(0,-2000000000,2000000000,1,1,0)
77 {
78         set_no_show_all();
79
80         label=manage(new class Gtk::Label("Unknown Datatype"));
81         pack_start(*label);
82         label->show();
83
84         vector_widget=manage(new class Widget_Vector());
85         pack_start(*vector_widget);
86
87         color_widget=manage(new class Widget_ColorEdit());
88         pack_start(*color_widget);
89
90         enum_widget=manage(new class Widget_Enum());
91         pack_start(*enum_widget);
92
93         real_widget=manage(new class Gtk::SpinButton(real_adjustment,0.05,DIGITS));
94         pack_start(*real_widget);
95
96         integer_widget=manage(new class Gtk::SpinButton(integer_adjustment,1,0));
97         pack_start(*integer_widget);
98
99         angle_widget=manage(new class Gtk::SpinButton(angle_adjustment,15,2));
100         pack_start(*angle_widget);
101
102         bool_widget=manage(new class Gtk::CheckButton());
103         pack_start(*bool_widget);
104
105         //color_widget=manage(new class Gtk::ColorSelection());
106         //pack_start(*color_widget);
107
108         string_widget=manage(new class Gtk::Entry());
109         pack_start(*string_widget);
110
111         canvas_widget=manage(new class Widget_CanvasChooser());
112         pack_start(*canvas_widget);
113
114         filename_widget=manage(new class Widget_Filename());
115         pack_start(*filename_widget);
116
117         time_widget=manage(new class Widget_Time());
118         pack_start(*time_widget);
119
120         distance_widget=manage(new class Widget_Distance());
121         pack_start(*distance_widget);
122
123
124         vector_widget->signal_activate().connect(sigc::mem_fun(*this,&Widget_ValueBase::activate));
125         color_widget->signal_activate().connect(sigc::mem_fun(*this,&Widget_ValueBase::activate));
126         enum_widget->signal_activate().connect(sigc::mem_fun(*this,&Widget_ValueBase::activate));
127         real_widget->signal_activate().connect(sigc::mem_fun(*this,&Widget_ValueBase::activate));
128         integer_widget->signal_activate().connect(sigc::mem_fun(*this,&Widget_ValueBase::activate));
129         angle_widget->signal_activate().connect(sigc::mem_fun(*this,&Widget_ValueBase::activate));
130         string_widget->signal_activate().connect(sigc::mem_fun(*this,&Widget_ValueBase::activate));
131         canvas_widget->signal_activate().connect(sigc::mem_fun(*this,&Widget_ValueBase::activate));
132         filename_widget->signal_activate().connect(sigc::mem_fun(*this,&Widget_ValueBase::activate));
133         time_widget->signal_activate().connect(sigc::mem_fun(*this,&Widget_ValueBase::activate));
134         distance_widget->signal_activate().connect(sigc::mem_fun(*this,&Widget_ValueBase::activate));
135
136         /*signal_focus_in_event().connect(
137                 sigc::bind_return(
138                 sigc::hide(
139                         sigc::mem_fun(*this,&Widget_ValueBase::grab_focus)
140                 ),false
141                 )
142         );*/
143 }
144
145 Widget_ValueBase::~Widget_ValueBase()
146 {
147 }
148
149 void
150 Widget_ValueBase::activate()
151 {
152         signal_activate()();
153 }
154
155 void
156 Widget_ValueBase::inside_cellrenderer()
157 {
158         string_widget->set_has_frame(false);
159         string_widget->gobj()->is_cell_renderer = true; // XXX
160
161         real_widget->set_has_frame(false);
162         //static_cast<Gtk::Entry*>(real_widget)->gobj()->is_cell_renderer = true; // XXX
163
164         distance_widget->set_has_frame(false);
165         //static_cast<Gtk::Entry*>(distance_widget)->gobj()->is_cell_renderer = true; // XXX
166
167         integer_widget->set_has_frame(false);
168         //static_cast<Gtk::Entry*>(integer_widget)->gobj()->is_cell_renderer = true; // XXX
169         vector_widget->set_has_frame(false);
170     //vector_widget->set_digits(10);
171
172         color_widget->set_has_frame(false);
173     //color_widget->set_digits(10);
174         filename_widget->set_has_frame(false);
175         time_widget->set_has_frame(false);
176 }
177
178 void
179 Widget_ValueBase::set_sensitive(bool x)
180 {
181         Gtk::HBox::set_sensitive(x);
182         label->set_sensitive(x);
183         vector_widget->set_sensitive(x);
184         real_widget->set_sensitive(x);
185         integer_widget->set_sensitive(x);
186         bool_widget->set_sensitive(x);
187     color_widget->set_sensitive(x);
188         string_widget->set_sensitive(x);
189         canvas_widget->set_sensitive(x);
190         enum_widget->set_sensitive(x);
191         angle_widget->set_sensitive(x);
192         filename_widget->set_sensitive(x);
193         time_widget->set_sensitive(x);
194         distance_widget->set_sensitive(x);
195 }
196
197 void
198 Widget_ValueBase::set_value(const synfig::ValueBase &data)
199 {
200         label->hide();
201         vector_widget->hide();
202         real_widget->hide();
203         integer_widget->hide();
204         bool_widget->hide();
205     color_widget->hide();
206         string_widget->hide();
207         canvas_widget->hide();
208         enum_widget->hide();
209         angle_widget->hide();
210         filename_widget->hide();
211         time_widget->hide();
212         distance_widget->hide();
213
214         value=data;
215         try{
216         switch(value.get_type())
217         {
218         case ValueBase::TYPE_VECTOR:
219                 vector_widget->set_canvas(canvas);
220                 vector_widget->set_value(value.get(Vector()));
221                 vector_widget->show();
222                 break;
223         case ValueBase::TYPE_REAL:
224                 if(param_desc.get_is_distance() && canvas)
225                 {
226                         Distance dist(value.get(Real()),Distance::SYSTEM_UNITS);
227                         dist.convert(App::distance_system,canvas->rend_desc());
228                         distance_widget->set_value(dist);
229                         distance_widget->show();
230                 }
231                 else
232                 {
233                         real_widget->set_value(value.get(Real()));
234                         real_widget->show();
235                 }
236                 break;
237         case ValueBase::TYPE_TIME:
238                 if(canvas)time_widget->set_fps(canvas->rend_desc().get_frame_rate());
239                 time_widget->set_value(value.get(Time()));
240                 time_widget->show();
241                 break;
242         case ValueBase::TYPE_ANGLE:
243                 angle_widget->set_value(Angle::deg(value.get(Angle())).get());
244                 angle_widget->show();
245                 break;
246         case ValueBase::TYPE_INTEGER:
247                 if(param_desc.get_hint()!="enum")
248                 {
249                         integer_widget->set_value(value.get(int()));
250                         integer_widget->show();
251                 }
252                 else
253                 {
254                         enum_widget->set_param_desc(param_desc);
255                         enum_widget->set_value(value.get(int()));
256                         enum_widget->show();
257                 }
258                 break;
259         case ValueBase::TYPE_CANVAS:
260                 assert(canvas);
261                 canvas_widget->set_parent_canvas(canvas);
262                 canvas_widget->set_value(value.get(etl::loose_handle<synfig::Canvas>()));
263                 canvas_widget->show();
264                 break;
265         case ValueBase::TYPE_BOOL:
266                 bool_widget->set_active(value.get(bool()));
267                 bool_widget->show();
268                 break;
269         case ValueBase::TYPE_STRING:
270                 if(param_desc.get_hint()!="filename")
271                 {
272                         string_widget->set_text(value.get(string()));
273                         string_widget->show();
274                 }
275                 else
276                 {
277                         filename_widget->set_value(value.get(string()));
278                         filename_widget->show();
279                 }
280                 break;
281         case ValueBase::TYPE_COLOR:
282         {
283                 color_widget->set_value(value.get(synfig::Color()));
284                 color_widget->show();
285 /*
286                         Gdk::Color gdkcolor;
287                         synfig::Color color=value.get(synfig::Color());
288                         gdkcolor.set_rgb_p(color.get_r(),color.get_g(),color.get_b());
289                         color_widget->set_current_color(gdkcolor);
290                         color_widget->set_has_opacity_control(true);
291                         color_widget->set_current_alpha((unsigned short)(color.get_a()*65535.0));
292                         color_widget->show();
293 */
294                 }
295                 break;
296         default:
297                 label->show();
298                 break;
299         }
300         }catch(...) { synfig::error(__FILE__":%d: Caught something that was thrown",__LINE__); }
301 }
302
303 const synfig::ValueBase &
304 Widget_ValueBase::get_value()
305 {
306         switch(value.get_type())
307         {
308         case ValueBase::TYPE_VECTOR:
309                 value=vector_widget->get_value();
310                 break;
311         case ValueBase::TYPE_REAL:
312                 if(param_desc.get_is_distance() && canvas)
313                         value=distance_widget->get_value().units(canvas->rend_desc());
314                 else
315                         value=real_widget->get_value();
316                 break;
317         case ValueBase::TYPE_TIME:
318                 value=time_widget->get_value();
319                 break;
320         case ValueBase::TYPE_ANGLE:
321                 value=Angle::deg(angle_widget->get_value());
322                 break;
323         case ValueBase::TYPE_CANVAS:
324                 value=canvas_widget->get_value();
325                 break;
326         case ValueBase::TYPE_INTEGER:
327                 if(param_desc.get_hint()!="enum")
328                 {
329                         value=integer_widget->get_value_as_int();
330                 }
331                 else
332                 {
333                         value=enum_widget->get_value();
334                 }
335
336                 break;
337         case ValueBase::TYPE_BOOL:
338                 value=bool_widget->get_active();
339                 break;
340         case ValueBase::TYPE_STRING:
341                 if(param_desc.get_hint()!="filename")
342                 {
343                         value=string(string_widget->get_text());
344                 }
345                 else
346                 {
347                         value=string(filename_widget->get_value());
348                 }
349                 break;
350         case ValueBase::TYPE_COLOR:
351         {
352                         value=color_widget->get_value();
353 /*
354                         Gdk::Color gdkcolor;
355                         synfig::Color color;
356                         gdkcolor=color_widget->get_current_color();
357                         color.set_r(gdkcolor.get_red_p());
358             color.set_g(gdkcolor.get_green_p());
359             color.set_b(gdkcolor.get_blue_p());
360                         color.set_a(color_widget->get_current_alpha()/65535.0);
361
362                         value=color;
363 */
364                 }
365                 break;
366         default:
367                 break;
368         }
369
370         return value;
371 }
372
373
374 void
375 Widget_ValueBase::on_grab_focus()
376 {
377         switch(value.get_type())
378         {
379         case ValueBase::TYPE_VECTOR:
380                 vector_widget->grab_focus();
381                 break;
382         case ValueBase::TYPE_REAL:
383                 if(param_desc.get_is_distance()&& canvas)
384                         distance_widget->grab_focus();
385                 else
386                         real_widget->grab_focus();
387                 break;
388         case ValueBase::TYPE_TIME:
389                 time_widget->grab_focus();
390                 break;
391         case ValueBase::TYPE_ANGLE:
392                 angle_widget->grab_focus();
393                 break;
394         case ValueBase::TYPE_CANVAS:
395                 canvas_widget->grab_focus();
396                 break;
397         case ValueBase::TYPE_INTEGER:
398                 if(param_desc.get_hint()!="enum")
399                 {
400                         integer_widget->grab_focus();
401                 }
402                 else
403                 {
404                         enum_widget->grab_focus();
405                 }
406
407                 break;
408         case ValueBase::TYPE_BOOL:
409                 bool_widget->grab_focus();
410                 break;
411         case ValueBase::TYPE_STRING:
412                 if(param_desc.get_hint()!="filename")
413                 {
414                         string_widget->grab_focus();
415                 }
416                 else
417                 {
418                         filename_widget->grab_focus();
419                 }
420                 break;
421         case ValueBase::TYPE_COLOR:
422         {
423                         color_widget->grab_focus();
424                 }
425                 break;
426         default:
427                 break;
428         }
429 }
430
431 /*
432 Glib::SignalProxy0<void>
433 Widget_ValueBase::signal_activate()
434 {
435         switch(value.get_type())
436         {
437         case ValueBase::TYPE_VECTOR:
438                 return vector_widget->signal_activate();
439                 break;
440         case ValueBase::TYPE_REAL:
441                 if(param_desc.get_is_distance()&& canvas)
442                         return distance_widget->signal_activate();
443                 else
444                         return real_widget->signal_activate();
445
446                 break;
447         case ValueBase::TYPE_TIME:
448                 return time_widget->signal_activate();
449                 break;
450         case ValueBase::TYPE_ANGLE:
451                 return angle_widget->signal_activate();
452                 break;
453         case ValueBase::TYPE_CANVAS:
454                 return canvas_widget->signal_activate();
455                 break;
456         case ValueBase::TYPE_INTEGER:
457                 if(param_desc.get_hint()!="enum")
458                         return integer_widget->signal_activate();
459                 else
460                         return enum_widget->signal_activate();
461
462                 break;
463         case ValueBase::TYPE_BOOL:
464                 return string_widget->signal_activate();
465                 break;
466         case ValueBase::TYPE_STRING:
467                 if(param_desc.get_hint()!="filename")
468                 {
469                         return string_widget->signal_activate();
470                 }
471                 else
472                 {
473                         return filename_widget->signal_activate();
474                 }
475                 break;
476         case ValueBase::TYPE_COLOR:
477         {
478                         return color_widget->signal_activate();
479                 }
480                 break;
481         default:
482                 return string_widget->signal_activate();
483                 break;
484         }
485 }
486 */