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
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
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 */
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"
49 #include "cellrenderer_gradient.h"
50 #include "cellrenderer_value.h"
52 #include "widget_gradient.h"
53 #include "dialog_gradient.h"
54 #include "dialog_color.h"
55 #include <gtkmm/textview.h>
61 using namespace synfig;
64 using namespace studio;
66 /* === M A C R O S ========================================================= */
70 /* === G L O B A L S ======================================================= */
72 class studio::ValueBase_Entry : public Gtk::EventBox, public Gtk::CellEditable
75 Widget_ValueBase *valuewidget;
76 bool edit_done_called;
80 Glib::ObjectBase (typeid(ValueBase_Entry)),
85 edit_done_called=false;
87 Gtk::HBox *const hbox = new Gtk::HBox(false, 0);
88 add(*Gtk::manage(hbox));
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
97 valuewidget=manage(new class Widget_ValueBase());
98 valuewidget->inside_cellrenderer();
102 //set_flags(Gtk::CAN_FOCUS);
103 //set_events(Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
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
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));
125 //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();
204 return synfig::ValueBase();
206 const Glib::ustring &get_path()
213 /* === P R O C E D U R E S ================================================= */
215 bool get_paragraph(synfig::String& text)
218 _("Paragraph"), // Title
220 true // use_separator
222 Gtk::Label label(_("Enter Paragraph Text Here:"));
224 dialog.get_vbox()->pack_start(label);
227 Glib::RefPtr<Gtk::TextBuffer> text_buffer(Gtk::TextBuffer::create());
228 text_buffer->set_text(text);
230 Gtk::TextView text_view(text_buffer);
232 dialog.get_vbox()->pack_start(text_view);
236 entry.set_text(text);
238 entry.set_activates_default(true);
239 dialog.get_vbox()->pack_start(entry);
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);
246 //text_entry.signal_activate().connect(sigc::bind(sigc::mem_fun(dialog,&Gtk::Dialog::response),Gtk::RESPONSE_OK));
250 if(dialog.run()!=Gtk::RESPONSE_OK)
253 text=text_buffer->get_text();
258 /* === M E T H O D S ======================================================= */
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())
267 CellRendererText::signal_edited().connect(sigc::mem_fun(*this,&CellRenderer_ValueBase::string_edited_));
268 value_entry=new ValueBase_Entry();
271 Pango::AttrList attr_list;
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);
278 property_attributes()=attr_list;
280 property_foreground()=Glib::ustring("#7f7f7f");
281 property_inconsistent()=false;
284 CellRenderer_ValueBase::~CellRenderer_ValueBase()
286 // synfig::info("CellRenderer_ValueBase::~CellRenderer_ValueBase(): deleted");
290 CellRenderer_ValueBase::string_edited_(const Glib::ustring&path,const Glib::ustring&str)
292 ValueBase old_value=property_value_.get_value();
295 if(old_value.get_type()==ValueBase::TYPE_TIME)
297 value=ValueBase(Time((String)str,get_canvas()->rend_desc().get_frame_rate()));
300 value=ValueBase((String)str);
303 signal_edited_(path,value);
307 CellRenderer_ValueBase::render_vfunc(
308 const Glib::RefPtr<Gdk::Drawable>& window,
310 const Gdk::Rectangle& background_area,
311 const Gdk::Rectangle& ca,
312 const Gdk::Rectangle& expose_area,
313 Gtk::CellRendererState flags)
317 // const unsigned int cell_xpad = property_xpad();
318 // const unsigned int cell_ypad = property_ypad();
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);
325 // width -= cell_xpad * 2;
326 // height -= cell_ypad * 2;
328 // if(width <= 0 || height <= 0)
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;
337 ValueBase data=property_value_.get_value();
339 switch(data.get_type())
341 case ValueBase::TYPE_REAL:
342 if(((synfig::ParamDesc)property_param_desc_).get_is_distance())
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();
349 property_text()=(Glib::ustring)strprintf("%.6f",data.get(Real()));
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());
354 case ValueBase::TYPE_ANGLE:
355 property_text()=(Glib::ustring)strprintf("%.2f DEG",(Real)Angle::deg(data.get(Angle())).get());
357 case ValueBase::TYPE_INTEGER:
358 if(((synfig::ParamDesc)property_param_desc_).get_hint()!="enum")
360 property_text()=(Glib::ustring)strprintf("%i",data.get(int()));
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;
368 for(iter=enum_list.begin();iter!=enum_list.end();iter++)
369 if(iter->value==data.get(int()))
371 property_text()=(Glib::ustring)iter->local_name;
377 case ValueBase::TYPE_VECTOR:
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()));
387 case ValueBase::TYPE_STRING:
389 if(data.get_type()==ValueBase::TYPE_STRING)
391 if(!data.get(synfig::String()).empty())
392 property_text()=static_cast<Glib::ustring>(data.get(synfig::String()));
394 property_text()=Glib::ustring("<empty>");
397 case ValueBase::TYPE_CANVAS:
398 if(data.get(etl::handle<synfig::Canvas>()))
400 if(data.get(etl::handle<synfig::Canvas>())->is_inline())
401 property_text()="<Inline Canvas>";
403 property_text()=(Glib::ustring)data.get(etl::handle<synfig::Canvas>())->get_id();
406 property_text()="<No Image Selected>";
408 case ValueBase::TYPE_COLOR:
410 render_color_to_window(window,ca,data.get(Color()));
414 case ValueBase::TYPE_BOOL:
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*/,
426 case ValueBase::TYPE_NIL:
427 //property_text()=(Glib::ustring)" ";
430 case ValueBase::TYPE_SEGMENT:
431 property_text()=(Glib::ustring)_("Segment");
433 case ValueBase::TYPE_GRADIENT:
434 render_gradient_to_window(window,ca,data.get(Gradient()));
437 case ValueBase::TYPE_LIST:
438 property_text()=(Glib::ustring)_("List");
440 case ValueBase::TYPE_BLINEPOINT:
441 property_text()=(Glib::ustring)_("BLine Point");
444 property_text()=static_cast<Glib::ustring>(_("UNKNOWN"));
447 CellRendererText::render_vfunc(window,widget,background_area,ca,expose_area,flags);
453 CellRenderer_ValueBase::activate_vfunc( GdkEvent* event,
455 const Glib::ustring& path,
456 const Gdk::Rectangle& background_area,
457 const Gdk::Rectangle& cell_area,
458 Gtk::CellRendererState flags)
460 ValueBase data=(ValueBase)property_value_.get_value();
464 case ValueBase::TYPE_BOOL:
465 if(property_editable())
466 signal_edited_(path,ValueBase(!data.get(bool())));
468 case ValueBase::TYPE_STRING:
469 return CellRendererText::activate_vfunc(event,widget,path,background_area,cell_area,flags);
476 CellRenderer_ValueBase::gradient_edited(synfig::Gradient gradient, Glib::ustring path)
478 ValueBase old_value(property_value_.get_value());
479 ValueBase value(gradient);
481 signal_edited_(path,value);
485 CellRenderer_ValueBase::color_edited(synfig::Color color, Glib::ustring path)
487 ValueBase old_value(property_value_.get_value());
488 ValueBase value(color);
490 signal_edited_(path,value);
494 CellRenderer_ValueBase::start_editing_vfunc(
497 const Glib::ustring& path,
498 const Gdk::Rectangle& background_area,
499 const Gdk::Rectangle& cell_area,
500 Gtk::CellRendererState flags)
502 // If we aren't editable, then there is nothing to do
503 if(!property_editable())
506 ValueBase data=property_value_.get_value();
508 switch(data.get_type())
510 case ValueBase::TYPE_BOOL:
511 signal_edited_(path,ValueBase(!data.get(bool())));
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);
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(
522 sigc::mem_fun(*this,&studio::CellRenderer_ValueBase::gradient_edited),
526 App::dialog_gradient->present();
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(
535 sigc::mem_fun(*this,&studio::CellRenderer_ValueBase::color_edited),
539 App::dialog_color->present();
542 case ValueBase::TYPE_STRING:
543 if(get_param_desc().get_hint()=="paragraph")
545 synfig::String string;
546 string=data.get(string);
547 if(get_paragraph(string))
548 signal_edited_(path,ValueBase(string));
551 // if(get_param_desc().get_hint()!="filename")
552 // return CellRendererText::start_editing_vfunc(event,widget,path,background_area,cell_area,flags);
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));
571 CellRenderer_ValueBase::on_value_editing_done()
575 ValueBase old_value(property_value_.get_value());
576 ValueBase value(value_entry->get_value());
579 signal_edited_(value_entry->get_path(),value);
581 //delete value_entry;