1 /* === S Y N F I G ========================================================= */
2 /*! \file cellrenderer_value.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
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 */
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"
50 #include "cellrenderer_gradient.h"
51 #include "cellrenderer_value.h"
53 #include "widget_gradient.h"
54 #include "dialog_gradient.h"
55 #include "dialog_color.h"
56 #include <gtkmm/textview.h>
62 using namespace synfig;
65 using namespace studio;
67 /* === M A C R O S ========================================================= */
71 /* === G L O B A L S ======================================================= */
73 class studio::ValueBase_Entry : public Gtk::EventBox, public Gtk::CellEditable
76 Widget_ValueBase *valuewidget;
77 bool edit_done_called;
81 Glib::ObjectBase (typeid(ValueBase_Entry)),
86 edit_done_called=false;
88 Gtk::HBox *const hbox = new Gtk::HBox(false, 0);
89 add(*Gtk::manage(hbox));
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
98 valuewidget=manage(new class Widget_ValueBase());
99 valuewidget->inside_cellrenderer();
103 //set_flags(Gtk::CAN_FOCUS);
104 //set_events(Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
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
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));
126 //signal_show().connect(sigc::mem_fun(*this, &ValueBase_Entry::grab_focus));
132 void on_editing_done()
135 if(parent)parent->grab_focus();
136 if(!edit_done_called)
138 edit_done_called=true;
139 Gtk::CellEditable::on_editing_done();
143 synfig::error("on_editing_done(): Called twice!");
146 void set_parent(Gtk::Widget*x) { parent=x; }
147 void on_remove_widget()
150 edit_done_called=true;
151 if(parent)parent->grab_focus();
152 Gtk::CellEditable::on_remove_widget();
154 void start_editing_vfunc(GdkEvent */*event*/)
156 valuewidget->signal_activate().connect(sigc::mem_fun(*this, &studio::ValueBase_Entry::editing_done));
158 //valuewidget->grab_focus();
159 //get_window()->set_focus(*valuewidget);
161 bool on_event(GdkEvent *event)
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)
170 return Gtk::EventBox::on_event(event);
174 Gtk::EventBox::on_grab_focus();
176 valuewidget->grab_focus();
178 void set_path(const Glib::ustring &p)
182 void set_value(const synfig::ValueBase &data)
185 valuewidget->set_value(data);
186 //valuewidget->grab_focus();
188 void set_canvas(const etl::handle<synfig::Canvas> &data)
192 valuewidget->set_canvas(data);
194 void set_param_desc(const synfig::ParamDesc &data)
197 valuewidget->set_param_desc(data);
200 const synfig::ValueBase &get_value()
203 return valuewidget->get_value();
205 warning("%s:%d this code shouldn't be reached", __FILE__, __LINE__);
206 return *(new synfig::ValueBase());
209 const Glib::ustring &get_path()
216 /* === P R O C E D U R E S ================================================= */
218 bool get_paragraph(synfig::String& text)
221 _("Paragraph"), // Title
223 true // use_separator
225 Gtk::Label label(_("Enter Paragraph Text Here:"));
227 dialog.get_vbox()->pack_start(label);
230 Glib::RefPtr<Gtk::TextBuffer> text_buffer(Gtk::TextBuffer::create());
231 text_buffer->set_text(text);
233 Gtk::TextView text_view(text_buffer);
235 dialog.get_vbox()->pack_start(text_view);
239 entry.set_text(text);
241 entry.set_activates_default(true);
242 dialog.get_vbox()->pack_start(entry);
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);
249 //text_entry.signal_activate().connect(sigc::bind(sigc::mem_fun(dialog,&Gtk::Dialog::response),Gtk::RESPONSE_OK));
253 if(dialog.run()!=Gtk::RESPONSE_OK)
256 text=text_buffer->get_text();
261 /* === M E T H O D S ======================================================= */
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())
270 CellRendererText::signal_edited().connect(sigc::mem_fun(*this,&CellRenderer_ValueBase::string_edited_));
271 value_entry=new ValueBase_Entry();
274 Pango::AttrList attr_list;
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);
281 property_attributes()=attr_list;
283 property_foreground()=Glib::ustring("#7f7f7f");
284 property_inconsistent()=false;
287 CellRenderer_ValueBase::~CellRenderer_ValueBase()
289 if (getenv("SYNFIG_DEBUG_DESTRUCTORS"))
290 synfig::info("CellRenderer_ValueBase::~CellRenderer_ValueBase(): Deleted");
294 CellRenderer_ValueBase::string_edited_(const Glib::ustring&path,const Glib::ustring&str)
296 ValueBase old_value=property_value_.get_value();
299 if(old_value.get_type()==ValueBase::TYPE_TIME)
301 value=ValueBase(Time((String)str,get_canvas()->rend_desc().get_frame_rate()));
304 value=ValueBase((String)str);
307 signal_edited_(path,value);
311 CellRenderer_ValueBase::render_vfunc(
312 const Glib::RefPtr<Gdk::Drawable>& window,
314 const Gdk::Rectangle& background_area,
315 const Gdk::Rectangle& ca,
316 const Gdk::Rectangle& expose_area,
317 Gtk::CellRendererState flags)
321 // const unsigned int cell_xpad = property_xpad();
322 // const unsigned int cell_ypad = property_ypad();
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);
329 // width -= cell_xpad * 2;
330 // height -= cell_ypad * 2;
332 // if(width <= 0 || height <= 0)
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;
341 ValueBase data=property_value_.get_value();
343 switch(data.get_type())
345 case ValueBase::TYPE_REAL:
346 if(((synfig::ParamDesc)property_param_desc_).get_is_distance())
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();
353 property_text()=(Glib::ustring)strprintf("%.6f",data.get(Real()));
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());
358 case ValueBase::TYPE_ANGLE:
359 property_text()=(Glib::ustring)strprintf("%.2f DEG",(Real)Angle::deg(data.get(Angle())).get());
361 case ValueBase::TYPE_INTEGER:
362 if(((synfig::ParamDesc)property_param_desc_).get_hint()!="enum")
364 property_text()=(Glib::ustring)strprintf("%i",data.get(int()));
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;
372 for(iter=enum_list.begin();iter!=enum_list.end();iter++)
373 if(iter->value==data.get(int()))
375 // don't show the key_board s_hortcut under_scores
376 String local_name = iter->local_name;
377 String::size_type pos = local_name.find_first_of('_');
378 if (pos != String::npos)
379 property_text() = local_name.substr(0,pos) + local_name.substr(pos+1);
381 property_text() = local_name;
387 case ValueBase::TYPE_VECTOR:
389 Vector vector=data.get(Vector());
390 Distance x(vector[0],Distance::SYSTEM_UNITS),y(vector[1],Distance::SYSTEM_UNITS);
391 x.convert(App::distance_system,get_canvas()->rend_desc());
392 y.convert(App::distance_system,get_canvas()->rend_desc());
393 property_text()=static_cast<Glib::ustring>(strprintf("%s,%s",x.get_string(6).c_str(),y.get_string(6).c_str()));
397 case ValueBase::TYPE_STRING:
399 if(data.get_type()==ValueBase::TYPE_STRING)
401 if(!data.get(synfig::String()).empty())
402 property_text()=static_cast<Glib::ustring>(data.get(synfig::String()));
404 property_text()=Glib::ustring("<empty>");
407 case ValueBase::TYPE_CANVAS:
408 if(data.get(etl::handle<synfig::Canvas>()))
410 if(data.get(etl::handle<synfig::Canvas>())->is_inline())
411 property_text()=_("<Inline Canvas>");
413 property_text()=(Glib::ustring)data.get(etl::handle<synfig::Canvas>())->get_id();
416 property_text()="<No Image Selected>";
418 case ValueBase::TYPE_COLOR:
420 render_color_to_window(window,ca,data.get(Color()));
424 case ValueBase::TYPE_BOOL:
426 widget.get_style()->paint_check(
427 Glib::RefPtr<Gdk::Window>::cast_static(window), state,
428 data.get(bool())?Gtk::SHADOW_IN:Gtk::SHADOW_OUT,
429 ca, widget, "cellcheck",
430 ca.get_x()/* + x_offset + cell_xpad*/,
431 ca.get_y()/* + y_offset + cell_ypad*/,
436 case ValueBase::TYPE_NIL:
437 //property_text()=(Glib::ustring)" ";
440 case ValueBase::TYPE_SEGMENT:
441 property_text()=(Glib::ustring)_("Segment");
443 case ValueBase::TYPE_GRADIENT:
444 render_gradient_to_window(window,ca,data.get(Gradient()));
447 case ValueBase::TYPE_LIST:
448 property_text()=(Glib::ustring)_("List");
450 case ValueBase::TYPE_BLINEPOINT:
451 property_text()=(Glib::ustring)_("BLine Point");
454 property_text()=static_cast<Glib::ustring>(_("UNKNOWN"));
457 CellRendererText::render_vfunc(window,widget,background_area,ca,expose_area,flags);
463 CellRenderer_ValueBase::activate_vfunc( GdkEvent* event,
465 const Glib::ustring& path,
466 const Gdk::Rectangle& background_area,
467 const Gdk::Rectangle& cell_area,
468 Gtk::CellRendererState flags)
470 ValueBase data=(ValueBase)property_value_.get_value();
474 case ValueBase::TYPE_BOOL:
475 if(property_editable())
476 signal_edited_(path,ValueBase(!data.get(bool())));
478 case ValueBase::TYPE_STRING:
479 return CellRendererText::activate_vfunc(event,widget,path,background_area,cell_area,flags);
486 CellRenderer_ValueBase::gradient_edited(synfig::Gradient gradient, Glib::ustring path)
488 ValueBase old_value(property_value_.get_value());
489 ValueBase value(gradient);
491 signal_edited_(path,value);
495 CellRenderer_ValueBase::color_edited(synfig::Color color, Glib::ustring path)
497 ValueBase old_value(property_value_.get_value());
498 ValueBase value(color);
500 signal_edited_(path,value);
504 CellRenderer_ValueBase::start_editing_vfunc(
505 GdkEvent* event __attribute__ ((unused)),
507 const Glib::ustring& path,
508 const Gdk::Rectangle& background_area __attribute__ ((unused)),
509 const Gdk::Rectangle& cell_area __attribute__ ((unused)),
510 Gtk::CellRendererState flags __attribute__ ((unused)))
512 // If we aren't editable, then there is nothing to do
513 if(!property_editable())
516 ValueBase data=property_value_.get_value();
518 switch(data.get_type())
520 case ValueBase::TYPE_BOOL:
521 signal_edited_(path,ValueBase(!data.get(bool())));
523 //case ValueBase::TYPE_TIME:
524 // property_text()=(Glib::ustring)data.get(Time()).get_string(get_canvas()->rend_desc().get_frame_rate(),App::get_time_format()|Time::FORMAT_FULL);
525 // return CellRendererText::start_editing_vfunc(event,widget,path,background_area,cell_area,flags);
527 case ValueBase::TYPE_GRADIENT:
528 App::dialog_gradient->reset();
529 App::dialog_gradient->set_gradient(data.get(Gradient()));
530 App::dialog_gradient->signal_edited().connect(
532 sigc::mem_fun(*this,&studio::CellRenderer_ValueBase::gradient_edited),
536 App::dialog_gradient->present();
540 case ValueBase::TYPE_COLOR:
541 App::dialog_color->reset();
542 App::dialog_color->set_color(data.get(Color()));
543 App::dialog_color->signal_edited().connect(
545 sigc::mem_fun(*this,&studio::CellRenderer_ValueBase::color_edited),
549 App::dialog_color->present();
552 case ValueBase::TYPE_STRING:
553 if(get_param_desc().get_hint()=="paragraph")
555 synfig::String string;
556 string=data.get(string);
557 if(get_paragraph(string))
558 signal_edited_(path,ValueBase(string));
561 // if(get_param_desc().get_hint()!="filename")
562 // return CellRendererText::start_editing_vfunc(event,widget,path,background_area,cell_area,flags);
565 assert(get_canvas());
566 //delete value_entry;
567 value_entry=manage(new ValueBase_Entry());
568 value_entry->set_path(path);
569 value_entry->set_canvas(get_canvas());
570 value_entry->set_param_desc(get_param_desc());
571 value_entry->set_value(data);
572 value_entry->set_parent(&widget);
573 value_entry->signal_editing_done().connect(sigc::mem_fun(*this, &CellRenderer_ValueBase::on_value_editing_done));
581 CellRenderer_ValueBase::on_value_editing_done()
585 ValueBase old_value(property_value_.get_value());
586 ValueBase value(value_entry->get_value());
589 signal_edited_(value_entry->get_path(),value);
591 //delete value_entry;