e2948b716f4e91002e12b9f202efdea9b91301e1
[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 **      Copyright (c) 2007 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include <gtkmm/label.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
41 #include "app.h"
42 #include "widget_value.h"
43 #include "widget_vector.h"
44 #include "widget_filename.h"
45 #include "widget_enum.h"
46 #include "widget_color.h"
47 #include "widget_canvaschooser.h"
48 #include "widget_time.h"
49
50 #include "cellrenderer_gradient.h"
51 #include "cellrenderer_value.h"
52
53 #include "widget_gradient.h"
54 #include "dialog_gradient.h"
55 #include "dialog_color.h"
56 #include <gtkmm/textview.h>
57
58 #include "general.h"
59
60 #endif
61
62 using namespace synfig;
63 using namespace etl;
64 using namespace std;
65 using namespace studio;
66
67 /* === M A C R O S ========================================================= */
68
69 #define DIGITS          15
70
71 /* === G L O B A L S ======================================================= */
72
73 class studio::ValueBase_Entry : public Gtk::EventBox, public Gtk::CellEditable
74 {
75         Glib::ustring path;
76         Widget_ValueBase *valuewidget;
77         bool edit_done_called;
78         Gtk::Widget *parent;
79 public:
80         ValueBase_Entry():
81                 Glib::ObjectBase  (typeid(ValueBase_Entry)),
82                 Gtk::EventBox     (),
83                 Gtk::CellEditable ()
84         {
85                 parent=0;
86                 edit_done_called=false;
87 /*
88                   Gtk::HBox *const hbox = new Gtk::HBox(false, 0);
89                   add(*Gtk::manage(hbox));
90
91                   Gtk::Entry *entry_ = new Gtk::Entry();
92                         entry_->set_text("bleh");
93                   hbox->pack_start(*Gtk::manage(entry_), Gtk::PACK_EXPAND_WIDGET);
94                   entry_->set_has_frame(false);
95                   entry_->gobj()->is_cell_renderer = true; // XXX
96
97 */
98                 valuewidget=manage(new class Widget_ValueBase());
99                 valuewidget->inside_cellrenderer();
100                 add(*valuewidget);
101                 valuewidget->show();
102
103                 //set_flags(Gtk::CAN_FOCUS);
104                 //set_events(Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
105
106                 /*
107                 set_events(//(Gdk::ALL_EVENTS_MASK)
108                 ~(      Gdk::EXPOSURE_MASK
109                         | Gdk::ENTER_NOTIFY_MASK
110                         | Gdk::LEAVE_NOTIFY_MASK
111                         | Gdk::FOCUS_CHANGE_MASK
112                         | Gdk::STRUCTURE_MASK
113                         | Gdk::PROPERTY_CHANGE_MASK
114                         | Gdk::VISIBILITY_NOTIFY_MASK
115                         | Gdk::PROXIMITY_IN_MASK
116                         | Gdk::PROXIMITY_OUT_MASK
117                         | Gdk::SUBSTRUCTURE_MASK
118                 )
119                 );
120                 */
121                 //signal_editing_done().connect(sigc::mem_fun(*this, &studio::ValueBase_Entry::hide));
122                 //signal_remove_widget().connect(sigc::mem_fun(*this, &studio::ValueBase_Entry::hide));
123
124                 show_all_children();
125
126                 //signal_show().connect(sigc::mem_fun(*this, &ValueBase_Entry::grab_focus));
127         }
128         ~ValueBase_Entry()
129         {
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
205                 warning("%s:%d this code shouldn't be reached", __FILE__, __LINE__);
206                 return *(new synfig::ValueBase());
207         }
208
209         const Glib::ustring &get_path()
210         {
211                 return path;
212         }
213
214 };
215
216 /* === P R O C E D U R E S ================================================= */
217
218 bool get_paragraph(synfig::String& text)
219 {
220         Gtk::Dialog dialog(
221                 _("Paragraph"),         // Title
222                 true,           // Modal
223                 true            // use_separator
224         );
225         Gtk::Label label(_("Enter Paragraph Text Here:"));
226         label.show();
227         dialog.get_vbox()->pack_start(label);
228
229
230         Glib::RefPtr<Gtk::TextBuffer> text_buffer(Gtk::TextBuffer::create());
231         text_buffer->set_text(text);
232
233         Gtk::TextView text_view(text_buffer);
234         text_view.show();
235         dialog.get_vbox()->pack_start(text_view);
236
237 /*
238         Gtk::Entry entry;
239         entry.set_text(text);
240         entry.show();
241         entry.set_activates_default(true);
242         dialog.get_vbox()->pack_start(entry);
243 */
244
245         dialog.add_button(Gtk::StockID("gtk-ok"),Gtk::RESPONSE_OK);
246         dialog.add_button(Gtk::StockID("gtk-cancel"),Gtk::RESPONSE_CANCEL);
247         dialog.set_default_response(Gtk::RESPONSE_OK);
248
249         //text_entry.signal_activate().connect(sigc::bind(sigc::mem_fun(dialog,&Gtk::Dialog::response),Gtk::RESPONSE_OK));
250
251         dialog.show();
252
253         if(dialog.run()!=Gtk::RESPONSE_OK)
254                 return false;
255
256         text=text_buffer->get_text();
257
258         return true;
259 }
260
261 /* === M E T H O D S ======================================================= */
262
263 CellRenderer_ValueBase::CellRenderer_ValueBase():
264         Glib::ObjectBase        (typeid(CellRenderer_ValueBase)),
265         Gtk::CellRendererText   (),
266         property_value_ (*this,"value",synfig::ValueBase()),
267         property_canvas_(*this,"canvas",etl::handle<synfig::Canvas>()),
268         property_param_desc_(*this,"param_desc",synfig::ParamDesc())
269 {
270         CellRendererText::signal_edited().connect(sigc::mem_fun(*this,&CellRenderer_ValueBase::string_edited_));
271         value_entry=new ValueBase_Entry();
272         value_entry->hide();
273
274         Pango::AttrList attr_list;
275         {
276                 Pango::AttrInt pango_size(Pango::Attribute::create_attr_size(Pango::SCALE*8));
277                 pango_size.set_start_index(0);
278                 pango_size.set_end_index(64);
279                 attr_list.change(pango_size);
280         }
281         property_attributes()=attr_list;
282
283         property_foreground()=Glib::ustring("#7f7f7f");
284         property_inconsistent()=false;
285 }
286
287 CellRenderer_ValueBase::~CellRenderer_ValueBase()
288 {
289         if (getenv("SYNFIG_DEBUG_DESTRUCTORS"))
290                 synfig::info("CellRenderer_ValueBase::~CellRenderer_ValueBase(): Deleted");
291 }
292
293 void
294 CellRenderer_ValueBase::string_edited_(const Glib::ustring&path,const Glib::ustring&str)
295 {
296         ValueBase old_value=property_value_.get_value();
297         ValueBase value;
298
299         if(old_value.get_type()==ValueBase::TYPE_TIME)
300         {
301                 value=ValueBase(Time((String)str,get_canvas()->rend_desc().get_frame_rate()));
302         }
303         else
304                 value=ValueBase((String)str);
305
306         if(old_value!=value)
307                 signal_edited_(path,value);
308 }
309
310 void
311 CellRenderer_ValueBase::render_vfunc(
312                 const Glib::RefPtr<Gdk::Drawable>& window,
313                 Gtk::Widget& widget,
314                 const Gdk::Rectangle& background_area,
315                 const Gdk::Rectangle& ca,
316                 const Gdk::Rectangle& expose_area,
317                 Gtk::CellRendererState flags)
318 {
319         if(!window)
320                 return;
321 //      const unsigned int cell_xpad = property_xpad();
322 //      const unsigned int cell_ypad = property_ypad();
323
324         //int x_offset = 0, y_offset = 0;
325 //      int     width = ca.get_width();
326         int     height = ca.get_height();
327 //      get_size(widget, ca, x_offset, y_offset, width, height);
328
329 //      width  -= cell_xpad * 2;
330 //      height -= cell_ypad * 2;
331
332 //      if(width <= 0 || height <= 0)
333 //              return;
334
335         Gtk::StateType state = Gtk::STATE_INSENSITIVE;
336         if(property_editable())
337                 state = Gtk::STATE_NORMAL;
338         if((flags & Gtk::CELL_RENDERER_SELECTED) != 0)
339                 state = (widget.has_focus()) ? Gtk::STATE_SELECTED : Gtk::STATE_ACTIVE;
340
341         ValueBase data=property_value_.get_value();
342
343         switch(data.get_type())
344         {
345         case ValueBase::TYPE_REAL:
346                 if(((synfig::ParamDesc)property_param_desc_).get_is_distance())
347                 {
348                         Distance x(data.get(Real()),Distance::SYSTEM_UNITS);
349                         x.convert(App::distance_system,get_canvas()->rend_desc());
350                         property_text()=(Glib::ustring)x.get_string(6).c_str();
351                 }
352                 else
353                         property_text()=(Glib::ustring)strprintf("%.6f",data.get(Real()));
354                 break;
355         case ValueBase::TYPE_TIME:
356                 property_text()=(Glib::ustring)data.get(Time()).get_string(get_canvas()->rend_desc().get_frame_rate(),App::get_time_format());
357                 break;
358         case ValueBase::TYPE_ANGLE:
359                 property_text()=(Glib::ustring)strprintf("%.2f DEG",(Real)Angle::deg(data.get(Angle())).get());
360                 break;
361         case ValueBase::TYPE_INTEGER:
362                 if(((synfig::ParamDesc)property_param_desc_).get_hint()!="enum")
363                 {
364                         property_text()=(Glib::ustring)strprintf("%i",data.get(int()));
365                 }
366                 else
367                 {
368                         property_text()=(Glib::ustring)strprintf("(%i)",data.get(int()));
369                         std::list<synfig::ParamDesc::EnumData> enum_list=((synfig::ParamDesc)property_param_desc_).get_enum_list();
370                         std::list<synfig::ParamDesc::EnumData>::iterator iter;
371
372                         for(iter=enum_list.begin();iter!=enum_list.end();iter++)
373                                 if(iter->value==data.get(int()))
374                                 {
375                                         property_text()=(Glib::ustring)iter->local_name;
376                                         break;
377                                 }
378                 }
379
380                 break;
381         case ValueBase::TYPE_VECTOR:
382                 {
383                         Vector vector=data.get(Vector());
384                         Distance x(vector[0],Distance::SYSTEM_UNITS),y(vector[1],Distance::SYSTEM_UNITS);
385                         x.convert(App::distance_system,get_canvas()->rend_desc());
386                         y.convert(App::distance_system,get_canvas()->rend_desc());
387                         property_text()=static_cast<Glib::ustring>(strprintf("%s,%s",x.get_string(6).c_str(),y.get_string(6).c_str()));
388                 }
389                 break;
390
391         case ValueBase::TYPE_STRING:
392
393                 if(data.get_type()==ValueBase::TYPE_STRING)
394                 {
395                         if(!data.get(synfig::String()).empty())
396                                 property_text()=static_cast<Glib::ustring>(data.get(synfig::String()));
397                         else
398                                 property_text()=Glib::ustring("<empty>");
399                 }
400                 break;
401         case ValueBase::TYPE_CANVAS:
402                 if(data.get(etl::handle<synfig::Canvas>()))
403                 {
404                         if(data.get(etl::handle<synfig::Canvas>())->is_inline())
405                                 property_text()=_("<Inline Canvas>");
406                         else
407                                 property_text()=(Glib::ustring)data.get(etl::handle<synfig::Canvas>())->get_id();
408                 }
409                 else
410                         property_text()="<No Image Selected>";
411                 break;
412         case ValueBase::TYPE_COLOR:
413                 {
414                         render_color_to_window(window,ca,data.get(Color()));
415                         return;
416                 }
417                 break;
418         case ValueBase::TYPE_BOOL:
419                 {
420                         widget.get_style()->paint_check(
421                                 Glib::RefPtr<Gdk::Window>::cast_static(window), state,
422                                 data.get(bool())?Gtk::SHADOW_IN:Gtk::SHADOW_OUT,
423                                 ca, widget, "cellcheck",
424                                 ca.get_x()/* + x_offset + cell_xpad*/,
425                                 ca.get_y()/* + y_offset + cell_ypad*/,
426                                 height-1,height-1);
427                         return;
428                 }
429                 break;
430         case ValueBase::TYPE_NIL:
431                 //property_text()=(Glib::ustring)" ";
432                 return;
433                 break;
434         case ValueBase::TYPE_SEGMENT:
435                 property_text()=(Glib::ustring)_("Segment");
436                 break;
437         case ValueBase::TYPE_GRADIENT:
438                 render_gradient_to_window(window,ca,data.get(Gradient()));
439                 return;
440                 break;
441         case ValueBase::TYPE_LIST:
442                 property_text()=(Glib::ustring)_("List");
443                 break;
444         case ValueBase::TYPE_BLINEPOINT:
445                 property_text()=(Glib::ustring)_("BLine Point");
446                 break;
447         default:
448                 property_text()=static_cast<Glib::ustring>(_("UNKNOWN"));
449                 break;
450         }
451         CellRendererText::render_vfunc(window,widget,background_area,ca,expose_area,flags);
452 }
453
454
455 /*
456 bool
457 CellRenderer_ValueBase::activate_vfunc( GdkEvent* event,
458         Gtk::Widget& widget,
459         const Glib::ustring& path,
460         const Gdk::Rectangle& background_area,
461         const Gdk::Rectangle& cell_area,
462         Gtk::CellRendererState flags)
463 {
464         ValueBase data=(ValueBase)property_value_.get_value();
465
466         switch(data.type)
467         {
468         case ValueBase::TYPE_BOOL:
469                 if(property_editable())
470                         signal_edited_(path,ValueBase(!data.get(bool())));
471         return true;
472         case ValueBase::TYPE_STRING:
473                 return CellRendererText::activate_vfunc(event,widget,path,background_area,cell_area,flags);
474         }
475         return false;
476 }
477 */
478
479 void
480 CellRenderer_ValueBase::gradient_edited(synfig::Gradient gradient, Glib::ustring path)
481 {
482         ValueBase old_value(property_value_.get_value());
483         ValueBase value(gradient);
484         if(old_value!=value)
485                 signal_edited_(path,value);
486 }
487
488 void
489 CellRenderer_ValueBase::color_edited(synfig::Color color, Glib::ustring path)
490 {
491         ValueBase old_value(property_value_.get_value());
492         ValueBase value(color);
493         if(old_value!=value)
494                 signal_edited_(path,value);
495 }
496
497 Gtk::CellEditable*
498 CellRenderer_ValueBase::start_editing_vfunc(
499         GdkEvent* event __attribute__ ((unused)),
500         Gtk::Widget& widget,
501         const Glib::ustring& path,
502         const Gdk::Rectangle& background_area __attribute__ ((unused)),
503         const Gdk::Rectangle& cell_area __attribute__ ((unused)),
504         Gtk::CellRendererState flags __attribute__ ((unused)))
505 {
506         // If we aren't editable, then there is nothing to do
507         if(!property_editable())
508                 return 0;
509
510         ValueBase data=property_value_.get_value();
511
512         switch(data.get_type())
513         {
514         case ValueBase::TYPE_BOOL:
515                 signal_edited_(path,ValueBase(!data.get(bool())));
516         return NULL;
517         //case ValueBase::TYPE_TIME:
518         //      property_text()=(Glib::ustring)data.get(Time()).get_string(get_canvas()->rend_desc().get_frame_rate(),App::get_time_format()|Time::FORMAT_FULL);
519         //      return CellRendererText::start_editing_vfunc(event,widget,path,background_area,cell_area,flags);
520
521         case ValueBase::TYPE_GRADIENT:
522                 App::dialog_gradient->reset();
523                 App::dialog_gradient->set_gradient(data.get(Gradient()));
524                 App::dialog_gradient->signal_edited().connect(
525                         sigc::bind(
526                                 sigc::mem_fun(*this,&studio::CellRenderer_ValueBase::gradient_edited),
527                                 path
528                         )
529                 );
530                 App::dialog_gradient->present();
531
532                 return NULL;
533
534         case ValueBase::TYPE_COLOR:
535                 App::dialog_color->reset();
536                 App::dialog_color->set_color(data.get(Color()));
537                 App::dialog_color->signal_edited().connect(
538                         sigc::bind(
539                                 sigc::mem_fun(*this,&studio::CellRenderer_ValueBase::color_edited),
540                                 path
541                         )
542                 );
543                 App::dialog_color->present();
544
545                 return NULL;
546         case ValueBase::TYPE_STRING:
547                 if(get_param_desc().get_hint()=="paragraph")
548                 {
549                         synfig::String string;
550                         string=data.get(string);
551                         if(get_paragraph(string))
552                                 signal_edited_(path,ValueBase(string));
553                         return NULL;
554                 }
555                 // if(get_param_desc().get_hint()!="filename")
556                         // return CellRendererText::start_editing_vfunc(event,widget,path,background_area,cell_area,flags);
557         default:
558                 {
559                         assert(get_canvas());
560                         //delete value_entry;
561                         value_entry=manage(new ValueBase_Entry());
562                         value_entry->set_path(path);
563                         value_entry->set_canvas(get_canvas());
564                         value_entry->set_param_desc(get_param_desc());
565                         value_entry->set_value(data);
566                         value_entry->set_parent(&widget);
567                         value_entry->signal_editing_done().connect(sigc::mem_fun(*this, &CellRenderer_ValueBase::on_value_editing_done));
568                         return value_entry;
569                 }
570         }
571         return NULL;
572 }
573
574 void
575 CellRenderer_ValueBase::on_value_editing_done()
576 {
577         if(value_entry)
578         {
579                 ValueBase old_value(property_value_.get_value());
580                 ValueBase value(value_entry->get_value());
581
582                 if(old_value!=value)
583                         signal_edited_(value_entry->get_path(),value);
584
585                 //delete value_entry;
586                 //value_entry=0;
587         }
588 }