Documentation for target_scanline.h
[synfig.git] / synfig-studio / src / gui / trees / 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 "trees/childrentree.h"
34 #include "cellrenderer/cellrenderer_value.h"
35 #include "cellrenderer/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 /*  // Buttons to raise/lower/duplicate/delete children valuenodes
184         //   Commented out because these functions are not implemented
185     //   and children sort themselves alphabetically
186
187         Gtk::Image *icon;
188         //Gtk::IconSize iconsize(Gtk::IconSize::from_name("synfig-small_icon"));
189         Gtk::IconSize iconsize(Gtk::ICON_SIZE_SMALL_TOOLBAR);
190
191         SMALL_BUTTON(button_raise,"gtk-go-up",_("Raise"));
192         SMALL_BUTTON(button_lower,"gtk-go-down",_("Lower"));
193         SMALL_BUTTON(button_duplicate,"synfig-duplicate",_("Duplicate"));
194         SMALL_BUTTON(button_delete,"gtk-delete",_("Delete"));
195
196         hbox->pack_start(*button_raise,Gtk::PACK_SHRINK);
197         hbox->pack_start(*button_lower,Gtk::PACK_SHRINK);
198         hbox->pack_start(*button_duplicate,Gtk::PACK_SHRINK);
199         hbox->pack_start(*button_delete,Gtk::PACK_SHRINK);
200
201         button_raise->signal_clicked().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_raise_pressed));
202         button_lower->signal_clicked().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_lower_pressed));
203         button_duplicate->signal_clicked().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_duplicate_pressed));
204         button_delete->signal_clicked().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_delete_pressed));
205
206         button_raise->set_sensitive(false);
207         button_lower->set_sensitive(false);
208         button_duplicate->set_sensitive(false);
209         button_delete->set_sensitive(false);
210 */
211
212         get_selection()->signal_changed().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_selection_changed));
213
214         tree_view.set_reorderable(true);
215
216         hbox->show();
217         tree_view.show();
218
219         tooltips_.enable();
220
221         //get_selection()->set_mode(Gtk::SELECTION_MULTIPLE);
222 }
223
224 ChildrenTree::~ChildrenTree()
225 {
226 }
227
228 void
229 ChildrenTree::set_show_timetrack(bool x)
230 {
231         column_time_track->set_visible(x);
232 }
233
234 void
235 ChildrenTree::set_model(Glib::RefPtr<ChildrenTreeStore> children_tree_store)
236 {
237         children_tree_store_=children_tree_store;
238         tree_view.set_model(children_tree_store_);
239         cellrenderer_time_track->set_canvas_interface(children_tree_store_->canvas_interface()); // am I smart people?  (cellrenderer/cellrenderer_timetrack.h:176)
240         children_tree_store_->canvas_interface()->signal_dirty_preview().connect(sigc::mem_fun(*this,&studio::ChildrenTree::on_dirty_preview));
241 }
242
243 void
244 ChildrenTree::set_time_adjustment(Gtk::Adjustment &adjustment)
245 {
246         cellrenderer_time_track->set_adjustment(adjustment);
247 }
248
249 void
250 ChildrenTree::on_dirty_preview()
251 {
252 }
253
254 void
255 ChildrenTree::on_selection_changed()
256 {
257         if(0)
258                 {
259                 button_raise->set_sensitive(false);
260                 button_lower->set_sensitive(false);
261                 button_duplicate->set_sensitive(false);
262                 button_delete->set_sensitive(false);
263                 return;
264         }
265 }
266
267 void
268 ChildrenTree::on_edited_value(const Glib::ustring&path_string,synfig::ValueBase value)
269 {
270         Gtk::TreePath path(path_string);
271
272         const Gtk::TreeRow row = *(tree_view.get_model()->get_iter(path));
273
274         row[model.value]=value;
275 //      signal_edited_value()(row[model.value_desc],value);
276 }
277
278 void
279 ChildrenTree::on_waypoint_clicked_childrentree(const etl::handle<synfig::Node>& node __attribute__ ((unused)),
280                                                                                            const synfig::Time& time __attribute__ ((unused)),
281                                                                                            const synfig::Time& time_offset __attribute__ ((unused)),
282                                                                                            int button __attribute__ ((unused)))
283 {
284         std::set<synfig::Waypoint, std::less<UniqueID> > waypoint_set;
285         synfig::waypoint_collect(waypoint_set,time,node);
286
287         synfigapp::ValueDesc value_desc;
288
289         if (waypoint_set.size() == 1)
290         {
291                 ValueNode::Handle value_node(waypoint_set.begin()->get_parent_value_node());
292                 assert(value_node);
293
294                 Gtk::TreeRow row;
295                 if (children_tree_store_->find_first_value_node(value_node, row) && row)
296                         value_desc = static_cast<synfigapp::ValueDesc>(row[model.value_desc]);
297         }
298
299         if (!waypoint_set.empty())
300                 signal_waypoint_clicked_childrentree()(value_desc,waypoint_set,button);
301 }
302
303 bool
304 ChildrenTree::on_tree_event(GdkEvent *event)
305 {
306     switch(event->type)
307     {
308         case GDK_BUTTON_PRESS:
309                 {
310                         Gtk::TreeModel::Path path;
311                         Gtk::TreeViewColumn *column;
312                         int cell_x, cell_y;
313                         if(!tree_view.get_path_at_pos(
314                                 int(event->button.x),int(event->button.y),      // x, y
315                                 path, // TreeModel::Path&
316                                 column, //TreeViewColumn*&
317                                 cell_x,cell_y //int&cell_x,int&cell_y
318                                 )
319                         ) break;
320                         const Gtk::TreeRow row = *(tree_view.get_model()->get_iter(path));
321
322                         if(column->get_first_cell_renderer()==cellrenderer_time_track)
323                         {
324                                 Gdk::Rectangle rect;
325                                 tree_view.get_cell_area(path,*column,rect);
326                                 cellrenderer_time_track->property_value_desc()=row[model.value_desc];
327                                 cellrenderer_time_track->property_canvas()=row[model.canvas];
328                                 cellrenderer_time_track->activate(event,*this,path.to_string(),rect,rect,Gtk::CellRendererState());
329                                 queue_draw_area(rect.get_x(),rect.get_y(),rect.get_width(),rect.get_height());
330                                 return true;
331                         }
332                         else if(column->get_first_cell_renderer()==cellrenderer_value)
333                                 return signal_user_click()(event->button.button,row,COLUMNID_VALUE);
334                         else
335                                 return signal_user_click()(event->button.button,row,COLUMNID_ID);
336
337                 }
338                 break;
339
340         case GDK_MOTION_NOTIFY:
341                 {
342                         Gtk::TreeModel::Path path;
343                         Gtk::TreeViewColumn *column;
344                         int cell_x, cell_y;
345                         if(!tree_view.get_path_at_pos(
346                                 (int)event->button.x,(int)event->button.y,      // x, y
347                                 path, // TreeModel::Path&
348                                 column, //TreeViewColumn*&
349                                 cell_x,cell_y //int&cell_x,int&cell_y
350                                 )
351                         ) break;
352
353                         if(!tree_view.get_model()->get_iter(path))
354                                 break;
355
356                         Gtk::TreeRow row = *(tree_view.get_model()->get_iter(path));
357
358                         if(cellrenderer_time_track==column->get_first_cell_renderer())
359                         {
360                                 // Movement on TimeLine
361                                 Gdk::Rectangle rect;
362                                 tree_view.get_cell_area(path,*column,rect);
363                                 cellrenderer_time_track->property_value_desc()=row[model.value_desc];
364                                 cellrenderer_time_track->property_canvas()=row[model.canvas];
365                                 cellrenderer_time_track->activate(event,*this,path.to_string(),rect,rect,Gtk::CellRendererState());
366                                 queue_draw();
367                                 //queue_draw_area(rect.get_x(),rect.get_y(),rect.get_width(),rect.get_height());
368                                 return true;
369                         }
370                         else
371                         if(last_tooltip_path.get_depth()<=0 || path!=last_tooltip_path)
372                         {
373                                 tooltips_.unset_tip(*this);
374                                 Glib::ustring tooltips_string(row[model.tooltip]);
375                                 last_tooltip_path=path;
376                                 if(!tooltips_string.empty())
377                                 {
378                                         tooltips_.set_tip(*this,tooltips_string);
379                                         tooltips_.force_window();
380                                 }
381                         }
382                 }
383                 break;
384         case GDK_BUTTON_RELEASE:
385                 {
386                         Gtk::TreeModel::Path path;
387                         Gtk::TreeViewColumn *column;
388                         int cell_x, cell_y;
389                         if(!tree_view.get_path_at_pos(
390                                    (int)event->button.x,(int)event->button.y,   // x, y
391                                    path, // TreeModel::Path&
392                                    column, //TreeViewColumn*&
393                                    cell_x,cell_y //int&cell_x,int&cell_y
394                                    )
395                                 ) break;
396
397                         if(!tree_view.get_model()->get_iter(path))
398                                 break;
399
400                         Gtk::TreeRow row = *(tree_view.get_model()->get_iter(path));
401
402                         if(column && cellrenderer_time_track == column->get_first_cell_renderer())
403                         {
404                                 Gdk::Rectangle rect;
405                                 tree_view.get_cell_area(path,*column,rect);
406                                 cellrenderer_time_track->property_value_desc()=row[model.value_desc];
407                                 cellrenderer_time_track->property_canvas()=row[model.canvas];
408                                 cellrenderer_time_track->activate(event,*this,path.to_string(),rect,rect,Gtk::CellRendererState());
409                                 queue_draw();
410                                 queue_draw_area(rect.get_x(),rect.get_y(),rect.get_width(),rect.get_height());
411                                 return true;
412                         }
413                 }
414                 break;
415         default:
416                 break;
417         }
418         return false;
419 }
420
421 void
422 ChildrenTree::on_raise_pressed()
423 {
424 }
425
426 void
427 ChildrenTree::on_lower_pressed()
428 {
429 }
430
431 void
432 ChildrenTree::on_duplicate_pressed()
433 {
434 }
435
436 void
437 ChildrenTree::on_delete_pressed()
438 {
439 }