1 /* === S Y N F I G ========================================================= */
2 /*! \file cellrenderer_value.cpp
3 ** \brief Template File
5 ** $Id: cellrenderer_value.cpp,v 1.1.1.1 2005/01/07 03:34:36 darco Exp $
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>
59 using namespace synfig;
62 using namespace studio;
64 /* === M A C R O S ========================================================= */
68 #define use_colorspace_gamma() App::use_colorspace_gamma
69 #define colorspace_gamma() (2.2f)
70 #define gamma_in(x) pow((float)x,1.0f/colorspace_gamma())
71 #define gamma_out(x) pow((float)x,colorspace_gamma())
73 /* === G L O B A L S ======================================================= */
75 class studio::ValueBase_Entry : public Gtk::EventBox, public Gtk::CellEditable
78 Widget_ValueBase *valuewidget;
79 bool edit_done_called;
83 Glib::ObjectBase (typeid(ValueBase_Entry)),
88 edit_done_called=false;
90 Gtk::HBox *const hbox = new Gtk::HBox(false, 0);
91 add(*Gtk::manage(hbox));
93 Gtk::Entry *entry_ = new Gtk::Entry();
94 entry_->set_text("bleh");
95 hbox->pack_start(*Gtk::manage(entry_), Gtk::PACK_EXPAND_WIDGET);
96 entry_->set_has_frame(false);
97 entry_->gobj()->is_cell_renderer = true; // XXX
100 valuewidget=manage(new class Widget_ValueBase());
101 valuewidget->inside_cellrenderer();
105 //set_flags(Gtk::CAN_FOCUS);
106 //set_events(Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
109 set_events(//(Gdk::ALL_EVENTS_MASK)
110 ~( Gdk::EXPOSURE_MASK
111 | Gdk::ENTER_NOTIFY_MASK
112 | Gdk::LEAVE_NOTIFY_MASK
113 | Gdk::FOCUS_CHANGE_MASK
114 | Gdk::STRUCTURE_MASK
115 | Gdk::PROPERTY_CHANGE_MASK
116 | Gdk::VISIBILITY_NOTIFY_MASK
117 | Gdk::PROXIMITY_IN_MASK
118 | Gdk::PROXIMITY_OUT_MASK
119 | Gdk::SUBSTRUCTURE_MASK
123 //signal_editing_done().connect(sigc::mem_fun(*this, &studio::ValueBase_Entry::hide));
124 //signal_remove_widget().connect(sigc::mem_fun(*this, &studio::ValueBase_Entry::hide));
128 //signal_show().connect(sigc::mem_fun(*this, &ValueBase_Entry::grab_focus));
135 void on_editing_done()
138 if(parent)parent->grab_focus();
139 if(!edit_done_called)
141 edit_done_called=true;
142 Gtk::CellEditable::on_editing_done();
146 synfig::error("on_editing_done(): Called twice!");
149 void set_parent(Gtk::Widget*x) { parent=x; }
150 void on_remove_widget()
153 edit_done_called=true;
154 if(parent)parent->grab_focus();
155 Gtk::CellEditable::on_remove_widget();
157 void start_editing_vfunc(GdkEvent *event)
159 valuewidget->signal_activate().connect(sigc::mem_fun(*this, &studio::ValueBase_Entry::editing_done));
161 //valuewidget->grab_focus();
162 //get_window()->set_focus(*valuewidget);
164 bool on_event(GdkEvent *event)
166 if(event->any.type==GDK_BUTTON_PRESS ||
167 event->any.type==GDK_2BUTTON_PRESS ||
168 event->any.type==GDK_KEY_PRESS ||
169 event->any.type==GDK_KEY_RELEASE ||
170 event->any.type==GDK_SCROLL ||
171 event->any.type==GDK_3BUTTON_PRESS)
173 return Gtk::EventBox::on_event(event);
177 Gtk::EventBox::on_grab_focus();
179 valuewidget->grab_focus();
181 void set_path(const Glib::ustring &p)
185 void set_value(const synfig::ValueBase &data)
188 valuewidget->set_value(data);
189 //valuewidget->grab_focus();
191 void set_canvas(const etl::handle<synfig::Canvas> &data)
195 valuewidget->set_canvas(data);
197 void set_param_desc(const synfig::ParamDesc &data)
200 valuewidget->set_param_desc(data);
203 const synfig::ValueBase &get_value()
206 return valuewidget->get_value();
207 return 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_inconsistant()=false;
287 CellRenderer_ValueBase::~CellRenderer_ValueBase()
289 // synfig::info("CellRenderer_ValueBase::~CellRenderer_ValueBase(): deleted");
293 CellRenderer_ValueBase::string_edited_(const Glib::ustring&path,const Glib::ustring&str)
295 ValueBase old_value=property_value_.get_value();
298 if(old_value.get_type()==ValueBase::TYPE_TIME)
300 value=ValueBase(Time((String)str,get_canvas()->rend_desc().get_frame_rate()));
303 value=ValueBase((String)str);
306 signal_edited_(path,value);
310 CellRenderer_ValueBase::render_vfunc(
311 const Glib::RefPtr<Gdk::Drawable>& window,
313 const Gdk::Rectangle& background_area,
314 const Gdk::Rectangle& ca,
315 const Gdk::Rectangle& expose_area,
316 Gtk::CellRendererState flags)
320 // const unsigned int cell_xpad = property_xpad();
321 // const unsigned int cell_ypad = property_ypad();
323 //int x_offset = 0, y_offset = 0;
324 // int width = ca.get_width();
325 int height = ca.get_height();
326 // get_size(widget, ca, x_offset, y_offset, width, height);
328 // width -= cell_xpad * 2;
329 // height -= cell_ypad * 2;
331 // if(width <= 0 || height <= 0)
334 Gtk::StateType state = Gtk::STATE_INSENSITIVE;
335 if(property_editable())
336 state = Gtk::STATE_NORMAL;
337 if((flags & Gtk::CELL_RENDERER_SELECTED) != 0)
338 state = (widget.has_focus()) ? Gtk::STATE_SELECTED : Gtk::STATE_ACTIVE;
340 ValueBase data=property_value_.get_value();
342 switch(data.get_type())
344 case ValueBase::TYPE_REAL:
345 if(((synfig::ParamDesc)property_param_desc_).get_is_distance())
347 Distance x(data.get(Real()),Distance::SYSTEM_UNITS);
348 x.convert(App::distance_system,get_canvas()->rend_desc());
349 property_text()=(Glib::ustring)x.get_string(6).c_str();
352 property_text()=(Glib::ustring)strprintf("%.6f",data.get(Real()));
354 case ValueBase::TYPE_TIME:
355 property_text()=(Glib::ustring)data.get(Time()).get_string(get_canvas()->rend_desc().get_frame_rate(),App::get_time_format());
357 case ValueBase::TYPE_ANGLE:
358 property_text()=(Glib::ustring)strprintf("%.2f DEG",(Real)Angle::deg(data.get(Angle())).get());
360 case ValueBase::TYPE_INTEGER:
361 if(((synfig::ParamDesc)property_param_desc_).get_hint()!="enum")
363 property_text()=(Glib::ustring)strprintf("%i",data.get(int()));
367 property_text()=(Glib::ustring)strprintf("(%i)",data.get(int()));
368 std::list<synfig::ParamDesc::EnumData> enum_list=((synfig::ParamDesc)property_param_desc_).get_enum_list();
369 std::list<synfig::ParamDesc::EnumData>::iterator iter;
371 for(iter=enum_list.begin();iter!=enum_list.end();iter++)
372 if(iter->value==data.get(int()))
374 property_text()=(Glib::ustring)iter->local_name;
380 case ValueBase::TYPE_VECTOR:
382 Vector vector=data.get(Vector());
383 Distance x(vector[0],Distance::SYSTEM_UNITS),y(vector[1],Distance::SYSTEM_UNITS);
384 x.convert(App::distance_system,get_canvas()->rend_desc());
385 y.convert(App::distance_system,get_canvas()->rend_desc());
386 property_text()=static_cast<Glib::ustring>(strprintf("%s,%s",x.get_string(6).c_str(),y.get_string(6).c_str()));
390 case ValueBase::TYPE_STRING:
392 if(data.get_type()==ValueBase::TYPE_STRING)
394 if(!data.get(synfig::String()).empty())
395 property_text()=static_cast<Glib::ustring>(data.get(synfig::String()));
397 property_text()=Glib::ustring("<empty>");
400 case ValueBase::TYPE_CANVAS:
401 if(data.get(etl::handle<synfig::Canvas>()))
403 if(data.get(etl::handle<synfig::Canvas>())->is_inline())
404 property_text()="<Inline Canvas>";
406 property_text()=(Glib::ustring)data.get(etl::handle<synfig::Canvas>())->get_id();
409 property_text()="<No Image Selected>";
411 case ValueBase::TYPE_COLOR:
413 render_color_to_window(window,ca,data.get(Color()));
417 case ValueBase::TYPE_BOOL:
419 widget.get_style()->paint_check(
420 Glib::RefPtr<Gdk::Window>::cast_static(window), state,
421 data.get(bool())?Gtk::SHADOW_IN:Gtk::SHADOW_OUT,
422 ca, widget, "cellcheck",
423 ca.get_x()/* + x_offset + cell_xpad*/,
424 ca.get_y()/* + y_offset + cell_ypad*/,
429 case ValueBase::TYPE_NIL:
430 //property_text()=(Glib::ustring)" ";
433 case ValueBase::TYPE_SEGMENT:
434 property_text()=(Glib::ustring)_("Segment");
436 case ValueBase::TYPE_GRADIENT:
437 render_gradient_to_window(window,ca,data.get(Gradient()));
440 case ValueBase::TYPE_LIST:
441 property_text()=(Glib::ustring)_("List");
443 case ValueBase::TYPE_BLINEPOINT:
444 property_text()=(Glib::ustring)_("BLine Point");
447 property_text()=static_cast<Glib::ustring>(_("UNKNOWN"));
450 CellRendererText::render_vfunc(window,widget,background_area,ca,expose_area,flags);
456 CellRenderer_ValueBase::activate_vfunc( GdkEvent* event,
458 const Glib::ustring& path,
459 const Gdk::Rectangle& background_area,
460 const Gdk::Rectangle& cell_area,
461 Gtk::CellRendererState flags)
463 ValueBase data=(ValueBase)property_value_.get_value();
467 case ValueBase::TYPE_BOOL:
468 if(property_editable())
469 signal_edited_(path,ValueBase(!data.get(bool())));
471 case ValueBase::TYPE_STRING:
472 return CellRendererText::activate_vfunc(event,widget,path,background_area,cell_area,flags);
479 CellRenderer_ValueBase::gradient_edited(synfig::Gradient gradient, Glib::ustring path)
481 ValueBase old_value(property_value_.get_value());
482 ValueBase value(gradient);
484 signal_edited_(path,value);
488 CellRenderer_ValueBase::color_edited(synfig::Color color, Glib::ustring path)
490 ValueBase old_value(property_value_.get_value());
491 ValueBase value(color);
493 signal_edited_(path,value);
497 CellRenderer_ValueBase::start_editing_vfunc(
500 const Glib::ustring& path,
501 const Gdk::Rectangle& background_area,
502 const Gdk::Rectangle& cell_area,
503 Gtk::CellRendererState flags)
505 // If we aren't editable, then there is nothing to do
506 if(!property_editable())
509 ValueBase data=property_value_.get_value();
511 switch(data.get_type())
513 case ValueBase::TYPE_BOOL:
514 signal_edited_(path,ValueBase(!data.get(bool())));
516 //case ValueBase::TYPE_TIME:
517 // property_text()=(Glib::ustring)data.get(Time()).get_string(get_canvas()->rend_desc().get_frame_rate(),App::get_time_format()|Time::FORMAT_FULL);
518 // return CellRendererText::start_editing_vfunc(event,widget,path,background_area,cell_area,flags);
520 case ValueBase::TYPE_GRADIENT:
521 App::dialog_gradient->reset();
522 App::dialog_gradient->set_gradient(data.get(Gradient()));
523 App::dialog_gradient->signal_edited().connect(
525 sigc::mem_fun(*this,&studio::CellRenderer_ValueBase::gradient_edited),
529 App::dialog_gradient->present();
533 case ValueBase::TYPE_COLOR:
534 App::dialog_color->reset();
535 App::dialog_color->set_color(data.get(Color()));
536 App::dialog_color->signal_edited().connect(
538 sigc::mem_fun(*this,&studio::CellRenderer_ValueBase::color_edited),
542 App::dialog_color->present();
545 case ValueBase::TYPE_STRING:
546 if(get_param_desc().get_hint()=="paragraph")
548 synfig::String string;
549 string=data.get(string);
550 if(get_paragraph(string))
552 signal_edited_(path,ValueBase(string));
556 if(get_param_desc().get_hint()!="filename")
557 return CellRendererText::start_editing_vfunc(event,widget,path,background_area,cell_area,flags);
560 assert(get_canvas());
561 //delete value_entry;
562 value_entry=manage(new ValueBase_Entry());
563 value_entry->set_path(path);
564 value_entry->set_canvas(get_canvas());
565 value_entry->set_param_desc(get_param_desc());
566 value_entry->set_value(data);
567 value_entry->set_parent(&widget);
568 value_entry->signal_editing_done().connect(sigc::mem_fun(*this, &CellRenderer_ValueBase::on_value_editing_done));
576 CellRenderer_ValueBase::on_value_editing_done()
580 ValueBase old_value(property_value_.get_value());
581 ValueBase value(value_entry->get_value());
584 signal_edited_(value_entry->get_path(),value);
586 //delete value_entry;