3f4a8fb9e24b1a73db542f4c36bf6f3a5c982512
[synfig.git] / synfig-studio / trunk / src / gtkmm / cellrenderer_value.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file cellrenderer_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 <ETL/stringf>
34 #include <gtkmm/celleditable.h>
35 #include <gtkmm/editable.h>
36 #include <gtkmm/entry.h>
37 #include <gtkmm/eventbox.h>
38 #include <gtk/gtkentry.h> /* see XXX below */
39
40 #include "app.h"
41 #include "widget_value.h"
42 #include "widget_vector.h"
43 #include "widget_filename.h"
44 #include "widget_enum.h"
45 #include "widget_color.h"
46 #include "widget_canvaschooser.h"
47 #include "widget_time.h"
48
49 #include "cellrenderer_gradient.h"
50 #include "cellrenderer_value.h"
51
52 #include "widget_gradient.h"
53 #include "dialog_gradient.h"
54 #include "dialog_color.h"
55 #include <gtkmm/textview.h>
56
57 #include "general.h"
58
59 #endif
60
61 using namespace synfig;
62 using namespace etl;
63 using namespace std;
64 using namespace studio;
65
66 /* === M A C R O S ========================================================= */
67
68 #define DIGITS          15
69
70 /* === G L O B A L S ======================================================= */
71
72 class studio::ValueBase_Entry : public Gtk::EventBox, public Gtk::CellEditable
73 {
74         Glib::ustring path;
75         Widget_ValueBase *valuewidget;
76         bool edit_done_called;
77         Gtk::Widget *parent;
78 public:
79         ValueBase_Entry():
80                 Glib::ObjectBase  (typeid(ValueBase_Entry)),
81                 Gtk::EventBox     (),
82                 Gtk::CellEditable ()
83         {
84                 parent=0;
85                 edit_done_called=false;
86 /*
87                   Gtk::HBox *const hbox = new Gtk::HBox(false, 0);
88                   add(*Gtk::manage(hbox));
89
90                   Gtk::Entry *entry_ = new Gtk::Entry();
91                         entry_->set_text("bleh");
92                   hbox->pack_start(*Gtk::manage(entry_), Gtk::PACK_EXPAND_WIDGET);
93                   entry_->set_has_frame(false);
94                   entry_->gobj()->is_cell_renderer = true; // XXX
95
96 */
97                 valuewidget=manage(new class Widget_ValueBase());
98                 valuewidget->inside_cellrenderer();
99                 add(*valuewidget);
100                 valuewidget->show();
101
102                 //set_flags(Gtk::CAN_FOCUS);
103                 //set_events(Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
104
105                 /*
106                 set_events(//(Gdk::ALL_EVENTS_MASK)
107                 ~(      Gdk::EXPOSURE_MASK
108                         | Gdk::ENTER_NOTIFY_MASK
109                         | Gdk::LEAVE_NOTIFY_MASK
110                         | Gdk::FOCUS_CHANGE_MASK
111                         | Gdk::STRUCTURE_MASK
112                         | Gdk::PROPERTY_CHANGE_MASK
113                         | Gdk::VISIBILITY_NOTIFY_MASK
114                         | Gdk::PROXIMITY_IN_MASK
115                         | Gdk::PROXIMITY_OUT_MASK
116                         | Gdk::SUBSTRUCTURE_MASK
117                 )
118                 );
119                 */
120                 //signal_editing_done().connect(sigc::mem_fun(*this, &studio::ValueBase_Entry::hide));
121                 //signal_remove_widget().connect(sigc::mem_fun(*this, &studio::ValueBase_Entry::hide));
122
123                 show_all_children();
124
125                 //signal_show().connect(sigc::mem_fun(*this, &ValueBase_Entry::grab_focus));
126         }
127         ~ValueBase_Entry()
128         {
129                 DEBUGPOINT();
130         }
131
132         void on_editing_done()
133         {
134                 hide();
135                 if(parent)parent->grab_focus();
136                 if(!edit_done_called)
137                 {
138                         edit_done_called=true;
139                         Gtk::CellEditable::on_editing_done();
140                 }
141                 else
142                 {
143                         synfig::error("on_editing_done(): Called twice!");
144                 }
145         }
146         void set_parent(Gtk::Widget*x) { parent=x; }
147         void on_remove_widget()
148         {
149                 hide();
150                 edit_done_called=true;
151                 if(parent)parent->grab_focus();
152                 Gtk::CellEditable::on_remove_widget();
153         }
154         void start_editing_vfunc(GdkEvent */*event*/)
155         {
156                 valuewidget->signal_activate().connect(sigc::mem_fun(*this, &studio::ValueBase_Entry::editing_done));
157                 show();
158                 //valuewidget->grab_focus();
159                 //get_window()->set_focus(*valuewidget);
160         }
161         bool on_event(GdkEvent *event)
162         {
163                 if(event->any.type==GDK_BUTTON_PRESS ||
164                         event->any.type==GDK_2BUTTON_PRESS ||
165                         event->any.type==GDK_KEY_PRESS ||
166                         event->any.type==GDK_KEY_RELEASE ||
167                         event->any.type==GDK_SCROLL ||
168                         event->any.type==GDK_3BUTTON_PRESS)
169                         return true;
170                 return Gtk::EventBox::on_event(event);
171         }
172         void on_grab_focus()
173         {
174                 Gtk::EventBox::on_grab_focus();
175                 if(valuewidget)
176                         valuewidget->grab_focus();
177         }
178         void set_path(const Glib::ustring &p)
179         {
180                 path=p;
181         }
182         void set_value(const synfig::ValueBase &data)
183         {
184                 if(valuewidget)
185                         valuewidget->set_value(data);
186                 //valuewidget->grab_focus();
187         }
188         void set_canvas(const etl::handle<synfig::Canvas> &data)
189         {
190                 assert(data);
191                 if(valuewidget)
192                         valuewidget->set_canvas(data);
193         }
194         void set_param_desc(const synfig::ParamDesc &data)
195         {
196                 if(valuewidget)
197                         valuewidget->set_param_desc(data);
198         }
199
200         const synfig::ValueBase &get_value()
201         {
202                 if(valuewidget)
203                         return valuewidget->get_value();
204                 return synfig::ValueBase();
205         }
206         const Glib::ustring &get_path()
207         {
208                 return path;
209         }
210
211 };
212
213 /* === P R O C E D U R E S ================================================= */
214
215 bool get_paragraph(synfig::String& text)
216 {
217         Gtk::Dialog dialog(
218                 _("Paragraph"),         // Title
219                 true,           // Modal
220                 true            // use_separator
221         );
222         Gtk::Label label(_("Enter Paragraph Text Here:"));
223         label.show();
224         dialog.get_vbox()->pack_start(label);
225
226
227         Glib::RefPtr<Gtk::TextBuffer> text_buffer(Gtk::TextBuffer::create());
228         text_buffer->set_text(text);
229
230         Gtk::TextView text_view(text_buffer);
231         text_view.show();
232         dialog.get_vbox()->pack_start(text_view);
233
234 /*
235         Gtk::Entry entry;
236         entry.set_text(text);
237         entry.show();
238         entry.set_activates_default(true);
239         dialog.get_vbox()->pack_start(entry);
240 */
241
242         dialog.add_button(Gtk::StockID("gtk-ok"),Gtk::RESPONSE_OK);
243         dialog.add_button(Gtk::StockID("gtk-cancel"),Gtk::RESPONSE_CANCEL);
244         dialog.set_default_response(Gtk::RESPONSE_OK);
245
246         //text_entry.signal_activate().connect(sigc::bind(sigc::mem_fun(dialog,&Gtk::Dialog::response),Gtk::RESPONSE_OK));
247
248         dialog.show();
249
250         if(dialog.run()!=Gtk::RESPONSE_OK)
251                 return false;
252
253         text=text_buffer->get_text();
254
255         return true;
256 }
257
258 /* === M E T H O D S ======================================================= */
259
260 CellRenderer_ValueBase::CellRenderer_ValueBase():
261         Glib::ObjectBase        (typeid(CellRenderer_ValueBase)),
262         Gtk::CellRendererText   (),
263         property_value_ (*this,"value",synfig::ValueBase()),
264         property_canvas_(*this,"canvas",etl::handle<synfig::Canvas>()),
265         property_param_desc_(*this,"param_desc",synfig::ParamDesc())
266 {
267         CellRendererText::signal_edited().connect(sigc::mem_fun(*this,&CellRenderer_ValueBase::string_edited_));
268         value_entry=new ValueBase_Entry();
269         value_entry->hide();
270
271         Pango::AttrList attr_list;
272         {
273                 Pango::AttrInt pango_size(Pango::Attribute::create_attr_size(Pango::SCALE*8));
274                 pango_size.set_start_index(0);
275                 pango_size.set_end_index(64);
276                 attr_list.change(pango_size);
277         }
278         property_attributes()=attr_list;
279
280         property_foreground()=Glib::ustring("#7f7f7f");
281         property_inconsistent()=false;
282 }
283
284 CellRenderer_ValueBase::~CellRenderer_ValueBase()
285 {
286 //      synfig::info("CellRenderer_ValueBase::~CellRenderer_ValueBase(): deleted");
287 }
288
289 void
290 CellRenderer_ValueBase::string_edited_(const Glib::ustring&path,const Glib::ustring&str)
291 {
292         ValueBase old_value=property_value_.get_value();
293         ValueBase value;
294
295         if(old_value.get_type()==ValueBase::TYPE_TIME)
296         {
297                 value=ValueBase(Time((String)str,get_canvas()->rend_desc().get_frame_rate()));
298         }
299         else
300                 value=ValueBase((String)str);
301
302         if(old_value!=value)
303                 signal_edited_(path,value);
304 }
305
306 void
307 CellRenderer_ValueBase::render_vfunc(
308                 const Glib::RefPtr<Gdk::Drawable>& window,
309                 Gtk::Widget& widget,
310                 const Gdk::Rectangle& background_area,
311                 const Gdk::Rectangle& ca,
312                 const Gdk::Rectangle& expose_area,
313                 Gtk::CellRendererState flags)
314 {
315         if(!window)
316                 return;
317 //      const unsigned int cell_xpad = property_xpad();
318 //      const unsigned int cell_ypad = property_ypad();
319
320         //int x_offset = 0, y_offset = 0;
321 //      int     width = ca.get_width();
322         int     height = ca.get_height();
323 //      get_size(widget, ca, x_offset, y_offset, width, height);
324
325 //      width  -= cell_xpad * 2;
326 //      height -= cell_ypad * 2;
327
328 //      if(width <= 0 || height <= 0)
329 //              return;
330
331         Gtk::StateType state = Gtk::STATE_INSENSITIVE;
332         if(property_editable())
333                 state = Gtk::STATE_NORMAL;
334         if((flags & Gtk::CELL_RENDERER_SELECTED) != 0)
335                 state = (widget.has_focus()) ? Gtk::STATE_SELECTED : Gtk::STATE_ACTIVE;
336
337         ValueBase data=property_value_.get_value();
338
339         switch(data.get_type())
340         {
341         case ValueBase::TYPE_REAL:
342                 if(((synfig::ParamDesc)property_param_desc_).get_is_distance())
343                 {
344                         Distance x(data.get(Real()),Distance::SYSTEM_UNITS);
345                         x.convert(App::distance_system,get_canvas()->rend_desc());
346                         property_text()=(Glib::ustring)x.get_string(6).c_str();
347                 }
348                 else
349                         property_text()=(Glib::ustring)strprintf("%.6f",data.get(Real()));
350                 break;
351         case ValueBase::TYPE_TIME:
352                 property_text()=(Glib::ustring)data.get(Time()).get_string(get_canvas()->rend_desc().get_frame_rate(),App::get_time_format());
353                 break;
354         case ValueBase::TYPE_ANGLE:
355                 property_text()=(Glib::ustring)strprintf("%.2f DEG",(Real)Angle::deg(data.get(Angle())).get());
356                 break;
357         case ValueBase::TYPE_INTEGER:
358                 if(((synfig::ParamDesc)property_param_desc_).get_hint()!="enum")
359                 {
360                         property_text()=(Glib::ustring)strprintf("%i",data.get(int()));
361                 }
362                 else
363                 {
364                         property_text()=(Glib::ustring)strprintf("(%i)",data.get(int()));
365                         std::list<synfig::ParamDesc::EnumData> enum_list=((synfig::ParamDesc)property_param_desc_).get_enum_list();
366                         std::list<synfig::ParamDesc::EnumData>::iterator iter;
367
368                         for(iter=enum_list.begin();iter!=enum_list.end();iter++)
369                                 if(iter->value==data.get(int()))
370                                 {
371                                         property_text()=(Glib::ustring)iter->local_name;
372                                         break;
373                                 }
374                 }
375
376                 break;
377         case ValueBase::TYPE_VECTOR:
378                 {
379                         Vector vector=data.get(Vector());
380                         Distance x(vector[0],Distance::SYSTEM_UNITS),y(vector[1],Distance::SYSTEM_UNITS);
381                         x.convert(App::distance_system,get_canvas()->rend_desc());
382                         y.convert(App::distance_system,get_canvas()->rend_desc());
383                         property_text()=static_cast<Glib::ustring>(strprintf("%s,%s",x.get_string(6).c_str(),y.get_string(6).c_str()));
384                 }
385                 break;
386
387         case ValueBase::TYPE_STRING:
388
389                 if(data.get_type()==ValueBase::TYPE_STRING)
390                 {
391                         if(!data.get(synfig::String()).empty())
392                                 property_text()=static_cast<Glib::ustring>(data.get(synfig::String()));
393                         else
394                                 property_text()=Glib::ustring("<empty>");
395                 }
396                 break;
397         case ValueBase::TYPE_CANVAS:
398                 if(data.get(etl::handle<synfig::Canvas>()))
399                 {
400                         if(data.get(etl::handle<synfig::Canvas>())->is_inline())
401                                 property_text()=_("<Inline Canvas>");
402                         else
403                                 property_text()=(Glib::ustring)data.get(etl::handle<synfig::Canvas>())->get_id();
404                 }
405                 else
406                         property_text()="<No Image Selected>";
407                 break;
408         case ValueBase::TYPE_COLOR:
409                 {
410                         render_color_to_window(window,ca,data.get(Color()));
411                         return;
412                 }
413                 break;
414         case ValueBase::TYPE_BOOL:
415                 {
416                         widget.get_style()->paint_check(
417                                 Glib::RefPtr<Gdk::Window>::cast_static(window), state,
418                                 data.get(bool())?Gtk::SHADOW_IN:Gtk::SHADOW_OUT,
419                                 ca, widget, "cellcheck",
420                                 ca.get_x()/* + x_offset + cell_xpad*/,
421                                 ca.get_y()/* + y_offset + cell_ypad*/,
422                                 height-1,height-1);
423                         return;
424                 }
425                 break;
426         case ValueBase::TYPE_NIL:
427                 //property_text()=(Glib::ustring)" ";
428                 return;
429                 break;
430         case ValueBase::TYPE_SEGMENT:
431                 property_text()=(Glib::ustring)_("Segment");
432                 break;
433         case ValueBase::TYPE_GRADIENT:
434                 render_gradient_to_window(window,ca,data.get(Gradient()));
435                 return;
436                 break;
437         case ValueBase::TYPE_LIST:
438                 property_text()=(Glib::ustring)_("List");
439                 break;
440         case ValueBase::TYPE_BLINEPOINT:
441                 property_text()=(Glib::ustring)_("BLine Point");
442                 break;
443         default:
444                 property_text()=static_cast<Glib::ustring>(_("UNKNOWN"));
445                 break;
446         }
447         CellRendererText::render_vfunc(window,widget,background_area,ca,expose_area,flags);
448 }
449
450
451 /*
452 bool
453 CellRenderer_ValueBase::activate_vfunc( GdkEvent* event,
454         Gtk::Widget& widget,
455         const Glib::ustring& path,
456         const Gdk::Rectangle& background_area,
457         const Gdk::Rectangle& cell_area,
458         Gtk::CellRendererState flags)
459 {
460         ValueBase data=(ValueBase)property_value_.get_value();
461
462         switch(data.type)
463         {
464         case ValueBase::TYPE_BOOL:
465                 if(property_editable())
466                         signal_edited_(path,ValueBase(!data.get(bool())));
467         return true;
468         case ValueBase::TYPE_STRING:
469                 return CellRendererText::activate_vfunc(event,widget,path,background_area,cell_area,flags);
470         }
471         return false;
472 }
473 */
474
475 void
476 CellRenderer_ValueBase::gradient_edited(synfig::Gradient gradient, Glib::ustring path)
477 {
478         ValueBase old_value(property_value_.get_value());
479         ValueBase value(gradient);
480         if(old_value!=value)
481                 signal_edited_(path,value);
482 }
483
484 void
485 CellRenderer_ValueBase::color_edited(synfig::Color color, Glib::ustring path)
486 {
487         ValueBase old_value(property_value_.get_value());
488         ValueBase value(color);
489         if(old_value!=value)
490                 signal_edited_(path,value);
491 }
492
493 Gtk::CellEditable*
494 CellRenderer_ValueBase::start_editing_vfunc(
495         GdkEvent* event,
496         Gtk::Widget& widget,
497         const Glib::ustring& path,
498         const Gdk::Rectangle& background_area,
499         const Gdk::Rectangle& cell_area,
500         Gtk::CellRendererState flags)
501 {
502         // If we aren't editable, then there is nothing to do
503         if(!property_editable())
504                 return 0;
505
506         ValueBase data=property_value_.get_value();
507
508         switch(data.get_type())
509         {
510         case ValueBase::TYPE_BOOL:
511                 signal_edited_(path,ValueBase(!data.get(bool())));
512         return NULL;
513         //case ValueBase::TYPE_TIME:
514         //      property_text()=(Glib::ustring)data.get(Time()).get_string(get_canvas()->rend_desc().get_frame_rate(),App::get_time_format()|Time::FORMAT_FULL);
515         //      return CellRendererText::start_editing_vfunc(event,widget,path,background_area,cell_area,flags);
516
517         case ValueBase::TYPE_GRADIENT:
518                 App::dialog_gradient->reset();
519                 App::dialog_gradient->set_gradient(data.get(Gradient()));
520                 App::dialog_gradient->signal_edited().connect(
521                         sigc::bind(
522                                 sigc::mem_fun(*this,&studio::CellRenderer_ValueBase::gradient_edited),
523                                 path
524                         )
525                 );
526                 App::dialog_gradient->present();
527
528                 return NULL;
529
530         case ValueBase::TYPE_COLOR:
531                 App::dialog_color->reset();
532                 App::dialog_color->set_color(data.get(Color()));
533                 App::dialog_color->signal_edited().connect(
534                         sigc::bind(
535                                 sigc::mem_fun(*this,&studio::CellRenderer_ValueBase::color_edited),
536                                 path
537                         )
538                 );
539                 App::dialog_color->present();
540
541                 return NULL;
542         case ValueBase::TYPE_STRING:
543                 if(get_param_desc().get_hint()=="paragraph")
544                 {
545                         synfig::String string;
546                         string=data.get(string);
547                         if(get_paragraph(string))
548                                 signal_edited_(path,ValueBase(string));
549                         return NULL;
550                 }
551                 // if(get_param_desc().get_hint()!="filename")
552                         // return CellRendererText::start_editing_vfunc(event,widget,path,background_area,cell_area,flags);
553         default:
554                 {
555                         assert(get_canvas());
556                         //delete value_entry;
557                         value_entry=manage(new ValueBase_Entry());
558                         value_entry->set_path(path);
559                         value_entry->set_canvas(get_canvas());
560                         value_entry->set_param_desc(get_param_desc());
561                         value_entry->set_value(data);
562                         value_entry->set_parent(&widget);
563                         value_entry->signal_editing_done().connect(sigc::mem_fun(*this, &CellRenderer_ValueBase::on_value_editing_done));
564                         return value_entry;
565                 }
566         }
567         return NULL;
568 }
569
570 void
571 CellRenderer_ValueBase::on_value_editing_done()
572 {
573         if(value_entry)
574         {
575                 ValueBase old_value(property_value_.get_value());
576                 ValueBase value(value_entry->get_value());
577
578                 if(old_value!=value)
579                         signal_edited_(value_entry->get_path(),value);
580
581                 //delete value_entry;
582                 //value_entry=0;
583         }
584 }