Build rectanglular blines in the same handedness as all the other shapes.
[synfig.git] / synfig-studio / trunk / src / gtkmm / childrentree.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file childrentree.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) 2008 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 "childrentree.h"
34 #include "cellrenderer_value.h"
35 #include "cellrenderer_timetrack.h"
36 #include <synfigapp/action.h>
37 #include <synfigapp/instance.h>
38 #include <gtkmm/scrolledwindow.h>
39 #include <synfig/timepointcollect.h>
40
41 #include "general.h"
42
43 #endif
44
45 /* === U S I N G =========================================================== */
46
47 using namespace std;
48 using namespace etl;
49 using namespace synfig;
50 using namespace studio;
51
52 /* === M A C R O S ========================================================= */
53
54 #ifndef SMALL_BUTTON
55 #define SMALL_BUTTON(button,stockid,tooltip)    \
56         button = manage(new class Gtk::Button());       \
57         icon=manage(new Gtk::Image(Gtk::StockID(stockid),iconsize));    \
58         button->add(*icon);     \
59         tooltips_.set_tip(*button,tooltip);     \
60         icon->set_padding(0,0);\
61         icon->show();   \
62         button->set_relief(Gtk::RELIEF_NONE); \
63         button->show()
64 #endif
65
66 #ifndef NORMAL_BUTTON
67 #define NORMAL_BUTTON(button,stockid,tooltip)   \
68         button = manage(new class Gtk::Button());       \
69         icon=manage(new Gtk::Image(Gtk::StockID(stockid),Gtk::ICON_SIZE_BUTTON));       \
70         button->add(*icon);     \
71         tooltips_.set_tip(*button,tooltip);     \
72         icon->set_padding(0,0);\
73         icon->show();   \
74         /*button->set_relief(Gtk::RELIEF_NONE);*/ \
75         button->show()
76 #endif
77
78 #define NEW_SMALL_BUTTON(x,y,z) Gtk::Button *SMALL_BUTTON(x,y,z)
79
80 #define NOT_IMPLEMENTED_SLOT sigc::mem_fun(*reinterpret_cast<studio::CanvasViewUIInterface*>(get_ui_interface().get()),&studio::CanvasViewUIInterface::not_implemented)
81
82 /* === G L O B A L S ======================================================= */
83
84 /* === P R O C E D U R E S ================================================= */
85
86 /* === M E T H O D S ======================================================= */
87
88 ChildrenTree::ChildrenTree()
89 {
90         const ChildrenTreeStore::Model model;
91
92         {       // --- N A M E --------------------------------------------------------
93                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("ID")) );
94
95                 // Set up the icon cell-renderer
96                 Gtk::CellRendererPixbuf* icon_cellrenderer = Gtk::manage( new Gtk::CellRendererPixbuf() );
97                 column->pack_start(*icon_cellrenderer,false);
98                 column->add_attribute(icon_cellrenderer->property_pixbuf(), model.icon);
99
100                 // Pack the label into the column
101                 column->pack_start(model.label,true);
102
103                 // Finish setting up the column
104                 column->set_reorderable();
105                 column->set_resizable();
106                 column->set_clickable();
107                 column->set_min_width(150);
108                 column->set_sort_column(model.label);
109                 tree_view.append_column(*column);
110
111         }
112         {       // --- T Y P E --------------------------------------------------------
113                 int cols_count = tree_view.append_column(_("Type"),model.type);
114                 Gtk::TreeViewColumn* column = tree_view.get_column(cols_count-1);
115                 if(column)
116                 {
117                         column->set_reorderable();
118                         column->set_resizable();
119                         column->set_clickable();
120                         column->set_sort_column(model.type);
121                 }
122         }
123         {       // --- V A L U E  -----------------------------------------------------
124                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("ValueBase")) );
125
126                 // Set up the value cell-renderer
127                 cellrenderer_value=ChildrenTreeStore::add_cell_renderer_value(column);
128                 cellrenderer_value->signal_edited().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_edited_value));
129                 cellrenderer_value->property_value()=synfig::ValueBase();
130
131                 // Finish setting up the column
132                 tree_view.append_column(*column);
133                 column->set_sizing(Gtk::TREE_VIEW_COLUMN_AUTOSIZE);
134                 column->set_min_width(150);
135                 column->set_reorderable();
136                 column->set_resizable();
137                 column->set_clickable(false);
138         }
139         {       // --- T I M E   T R A C K --------------------------------------------
140                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Time Track")) );
141                 column_time_track=column;
142
143                 // Set up the value-node cell-renderer
144                 cellrenderer_time_track=ChildrenTreeStore::add_cell_renderer_value_node(column);
145                 cellrenderer_time_track->property_mode()=Gtk::CELL_RENDERER_MODE_ACTIVATABLE;
146                 cellrenderer_time_track->signal_waypoint_clicked_cellrenderer().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_waypoint_clicked_childrentree) );
147                 column->add_attribute(cellrenderer_time_track->property_value_desc(), model.value_desc);
148                 column->add_attribute(cellrenderer_time_track->property_canvas(), model.canvas);
149
150                 //column->pack_start(*cellrenderer_time_track);
151
152                 // Finish setting up the column
153                 column->set_reorderable();
154                 column->set_resizable();
155                 tree_view.append_column(*column);
156         }
157
158         // This makes things easier to read.
159         tree_view.set_rules_hint();
160
161         // Make us more sensitive to several events
162         tree_view.add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::BUTTON1_MOTION_MASK | Gdk::BUTTON2_MOTION_MASK|Gdk::POINTER_MOTION_MASK);
163
164         tree_view.signal_event().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_tree_event));
165
166         // Create a scrolled window for that tree
167         Gtk::ScrolledWindow *scroll_children_tree = manage(new class Gtk::ScrolledWindow());
168         scroll_children_tree->set_flags(Gtk::CAN_FOCUS);
169         scroll_children_tree->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
170         scroll_children_tree->add(tree_view);
171         scroll_children_tree->set_shadow_type(Gtk::SHADOW_ETCHED_IN);
172         scroll_children_tree->show();
173
174         attach(*scroll_children_tree, 0, 3, 0, 1, Gtk::EXPAND|Gtk::FILL,Gtk::EXPAND|Gtk::FILL, 0, 0);
175
176         hbox=manage(new Gtk::HBox());
177
178         attach(*hbox, 0, 1, 1, 2, Gtk::FILL|Gtk::SHRINK, Gtk::SHRINK, 0, 0);
179
180         tree_view.set_enable_search(true);
181         tree_view.set_search_column(model.label);
182
183 /*
184         Gtk::Image *icon;
185         //Gtk::IconSize iconsize(Gtk::IconSize::from_name("synfig-small_icon"));
186         Gtk::IconSize iconsize(Gtk::ICON_SIZE_SMALL_TOOLBAR);
187
188         SMALL_BUTTON(button_raise,"gtk-go-up",_("Raise"));
189         SMALL_BUTTON(button_lower,"gtk-go-down",_("Lower"));
190         SMALL_BUTTON(button_duplicate,"synfig-duplicate",_("Duplicate"));
191         SMALL_BUTTON(button_delete,"gtk-delete",_("Delete"));
192
193         hbox->pack_start(*button_raise,Gtk::PACK_SHRINK);
194         hbox->pack_start(*button_lower,Gtk::PACK_SHRINK);
195         hbox->pack_start(*button_duplicate,Gtk::PACK_SHRINK);
196         hbox->pack_start(*button_delete,Gtk::PACK_SHRINK);
197
198         button_raise->signal_clicked().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_raise_pressed));
199         button_lower->signal_clicked().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_lower_pressed));
200         button_duplicate->signal_clicked().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_duplicate_pressed));
201         button_delete->signal_clicked().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_delete_pressed));
202
203         button_raise->set_sensitive(false);
204         button_lower->set_sensitive(false);
205         button_duplicate->set_sensitive(false);
206         button_delete->set_sensitive(false);
207 */
208
209         get_selection()->signal_changed().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_selection_changed));
210
211         tree_view.set_reorderable(true);
212
213         hbox->show();
214         tree_view.show();
215
216         tooltips_.enable();
217
218         //get_selection()->set_mode(Gtk::SELECTION_MULTIPLE);
219 }
220
221 ChildrenTree::~ChildrenTree()
222 {
223 }
224
225 void
226 ChildrenTree::set_show_timetrack(bool x)
227 {
228         column_time_track->set_visible(x);
229 }
230
231 void
232 ChildrenTree::set_model(Glib::RefPtr<ChildrenTreeStore> children_tree_store)
233 {
234         children_tree_store_=children_tree_store;
235         tree_view.set_model(children_tree_store_);
236         cellrenderer_time_track->set_canvas_interface(children_tree_store_->canvas_interface()); // am I smart people?  (cellrenderer_timetrack.h:176)
237         children_tree_store_->canvas_interface()->signal_dirty_preview().connect(sigc::mem_fun(*this,&studio::ChildrenTree::on_dirty_preview));
238 }
239
240 void
241 ChildrenTree::set_time_adjustment(Gtk::Adjustment &adjustment)
242 {
243         cellrenderer_time_track->set_adjustment(adjustment);
244 }
245
246 void
247 ChildrenTree::on_dirty_preview()
248 {
249 }
250
251 void
252 ChildrenTree::on_selection_changed()
253 {
254         if(0)
255                 {
256                 button_raise->set_sensitive(false);
257                 button_lower->set_sensitive(false);
258                 button_duplicate->set_sensitive(false);
259                 button_delete->set_sensitive(false);
260                 return;
261         }
262 }
263
264 void
265 ChildrenTree::on_edited_value(const Glib::ustring&path_string,synfig::ValueBase value)
266 {
267         Gtk::TreePath path(path_string);
268
269         const Gtk::TreeRow row = *(tree_view.get_model()->get_iter(path));
270
271         row[model.value]=value;
272 //      signal_edited_value()(row[model.value_desc],value);
273 }
274
275 void
276 ChildrenTree::on_waypoint_clicked_childrentree(const etl::handle<synfig::Node>& node __attribute__ ((unused)),
277                                                                                            const synfig::Time& time __attribute__ ((unused)),
278                                                                                            const synfig::Time& time_offset __attribute__ ((unused)),
279                                                                                            int button __attribute__ ((unused)))
280 {
281         std::set<synfig::Waypoint, std::less<UniqueID> > waypoint_set;
282         synfig::waypoint_collect(waypoint_set,time,node);
283
284         synfigapp::ValueDesc value_desc;
285
286         if (waypoint_set.size() == 1)
287         {
288                 ValueNode::Handle value_node(waypoint_set.begin()->get_parent_value_node());
289                 assert(value_node);
290
291                 Gtk::TreeRow row;
292                 if (children_tree_store_->find_first_value_node(value_node, row) && row)
293                         value_desc = static_cast<synfigapp::ValueDesc>(row[model.value_desc]);
294         }
295
296         if (!waypoint_set.empty())
297                 signal_waypoint_clicked_childrentree()(value_desc,waypoint_set,button);
298 }
299
300 bool
301 ChildrenTree::on_tree_event(GdkEvent *event)
302 {
303     switch(event->type)
304     {
305         case GDK_BUTTON_PRESS:
306                 {
307                         Gtk::TreeModel::Path path;
308                         Gtk::TreeViewColumn *column;
309                         int cell_x, cell_y;
310                         if(!tree_view.get_path_at_pos(
311                                 int(event->button.x),int(event->button.y),      // x, y
312                                 path, // TreeModel::Path&
313                                 column, //TreeViewColumn*&
314                                 cell_x,cell_y //int&cell_x,int&cell_y
315                                 )
316                         ) break;
317                         const Gtk::TreeRow row = *(tree_view.get_model()->get_iter(path));
318
319                         if(column->get_first_cell_renderer()==cellrenderer_time_track)
320                         {
321                                 Gdk::Rectangle rect;
322                                 tree_view.get_cell_area(path,*column,rect);
323                                 cellrenderer_time_track->property_value_desc()=row[model.value_desc];
324                                 cellrenderer_time_track->property_canvas()=row[model.canvas];
325                                 cellrenderer_time_track->activate(event,*this,path.to_string(),rect,rect,Gtk::CellRendererState());
326                                 queue_draw_area(rect.get_x(),rect.get_y(),rect.get_width(),rect.get_height());
327                                 return true;
328                         }
329                         else if(column->get_first_cell_renderer()==cellrenderer_value)
330                                 return signal_user_click()(event->button.button,row,COLUMNID_VALUE);
331                         else
332                                 return signal_user_click()(event->button.button,row,COLUMNID_ID);
333
334                 }
335                 break;
336
337         case GDK_MOTION_NOTIFY:
338                 {
339                         Gtk::TreeModel::Path path;
340                         Gtk::TreeViewColumn *column;
341                         int cell_x, cell_y;
342                         if(!tree_view.get_path_at_pos(
343                                 (int)event->button.x,(int)event->button.y,      // x, y
344                                 path, // TreeModel::Path&
345                                 column, //TreeViewColumn*&
346                                 cell_x,cell_y //int&cell_x,int&cell_y
347                                 )
348                         ) break;
349
350                         if(!tree_view.get_model()->get_iter(path))
351                                 break;
352
353                         Gtk::TreeRow row = *(tree_view.get_model()->get_iter(path));
354
355                         if(cellrenderer_time_track==column->get_first_cell_renderer())
356                         {
357                                 // Movement on TimeLine
358                                 Gdk::Rectangle rect;
359                                 tree_view.get_cell_area(path,*column,rect);
360                                 cellrenderer_time_track->property_value_desc()=row[model.value_desc];
361                                 cellrenderer_time_track->property_canvas()=row[model.canvas];
362                                 cellrenderer_time_track->activate(event,*this,path.to_string(),rect,rect,Gtk::CellRendererState());
363                                 queue_draw();
364                                 //queue_draw_area(rect.get_x(),rect.get_y(),rect.get_width(),rect.get_height());
365                                 return true;
366                         }
367                         else
368                         if(last_tooltip_path.get_depth()<=0 || path!=last_tooltip_path)
369                         {
370                                 tooltips_.unset_tip(*this);
371                                 Glib::ustring tooltips_string(row[model.tooltip]);
372                                 last_tooltip_path=path;
373                                 if(!tooltips_string.empty())
374                                 {
375                                         tooltips_.set_tip(*this,tooltips_string);
376                                         tooltips_.force_window();
377                                 }
378                         }
379                 }
380                 break;
381         case GDK_BUTTON_RELEASE:
382                 {
383                         Gtk::TreeModel::Path path;
384                         Gtk::TreeViewColumn *column;
385                         int cell_x, cell_y;
386                         if(!tree_view.get_path_at_pos(
387                                    (int)event->button.x,(int)event->button.y,   // x, y
388                                    path, // TreeModel::Path&
389                                    column, //TreeViewColumn*&
390                                    cell_x,cell_y //int&cell_x,int&cell_y
391                                    )
392                                 ) break;
393
394                         if(!tree_view.get_model()->get_iter(path))
395                                 break;
396
397                         Gtk::TreeRow row = *(tree_view.get_model()->get_iter(path));
398
399                         if(column && cellrenderer_time_track == column->get_first_cell_renderer())
400                         {
401                                 Gdk::Rectangle rect;
402                                 tree_view.get_cell_area(path,*column,rect);
403                                 cellrenderer_time_track->property_value_desc()=row[model.value_desc];
404                                 cellrenderer_time_track->property_canvas()=row[model.canvas];
405                                 cellrenderer_time_track->activate(event,*this,path.to_string(),rect,rect,Gtk::CellRendererState());
406                                 queue_draw();
407                                 queue_draw_area(rect.get_x(),rect.get_y(),rect.get_width(),rect.get_height());
408                                 return true;
409                         }
410                 }
411                 break;
412         default:
413                 break;
414         }
415         return false;
416 }
417
418 void
419 ChildrenTree::on_raise_pressed()
420 {
421 }
422
423 void
424 ChildrenTree::on_lower_pressed()
425 {
426 }
427
428 void
429 ChildrenTree::on_duplicate_pressed()
430 {
431 }
432
433 void
434 ChildrenTree::on_delete_pressed()
435 {
436 }