Implements PXEGeek's http://wiki.synfig.com/Wish_list entry: "Optionally display...
[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 **
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.
14 **
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.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "childrentree.h"
33 #include "cellrenderer_value.h"
34 #include "cellrenderer_timetrack.h"
35 #include <synfigapp/action.h>
36 #include <synfigapp/instance.h>
37 #include <gtkmm/scrolledwindow.h>
38
39 #endif
40
41 /* === U S I N G =========================================================== */
42
43 using namespace std;
44 using namespace etl;
45 using namespace synfig;
46 using namespace studio;
47
48 /* === M A C R O S ========================================================= */
49
50 #ifndef SMALL_BUTTON
51 #define SMALL_BUTTON(button,stockid,tooltip)    \
52         button = manage(new class Gtk::Button());       \
53         icon=manage(new Gtk::Image(Gtk::StockID(stockid),iconsize));    \
54         button->add(*icon);     \
55         tooltips_.set_tip(*button,tooltip);     \
56         icon->set_padding(0,0);\
57         icon->show();   \
58         button->set_relief(Gtk::RELIEF_NONE); \
59         button->show()
60 #endif
61
62 #ifndef NORMAL_BUTTON
63 #define NORMAL_BUTTON(button,stockid,tooltip)   \
64         button = manage(new class Gtk::Button());       \
65         icon=manage(new Gtk::Image(Gtk::StockID(stockid),Gtk::ICON_SIZE_BUTTON));       \
66         button->add(*icon);     \
67         tooltips_.set_tip(*button,tooltip);     \
68         icon->set_padding(0,0);\
69         icon->show();   \
70         /*button->set_relief(Gtk::RELIEF_NONE);*/ \
71         button->show()
72 #endif
73
74 #define NEW_SMALL_BUTTON(x,y,z) Gtk::Button *SMALL_BUTTON(x,y,z)
75
76 #define NOT_IMPLEMENTED_SLOT sigc::mem_fun(*reinterpret_cast<studio::CanvasViewUIInterface*>(get_ui_interface().get()),&studio::CanvasViewUIInterface::not_implemented)
77
78 /* === G L O B A L S ======================================================= */
79
80 /* === P R O C E D U R E S ================================================= */
81
82 /* === M E T H O D S ======================================================= */
83
84 ChildrenTree::ChildrenTree()
85 {
86         const ChildrenTreeStore::Model model;
87
88         {       // --- N A M E --------------------------------------------------------
89                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("ID")) );
90
91                 // Set up the icon cell-renderer
92                 Gtk::CellRendererPixbuf* icon_cellrenderer = Gtk::manage( new Gtk::CellRendererPixbuf() );
93                 column->pack_start(*icon_cellrenderer,false);
94                 column->add_attribute(icon_cellrenderer->property_pixbuf(), model.icon);
95
96                 // Pack the label into the column
97                 column->pack_start(model.label,true);
98
99                 // Finish setting up the column
100                 column->set_reorderable();
101                 column->set_resizable();
102                 column->set_clickable();
103                 column->set_min_width(150);
104                 column->set_sort_column_id(model.label);
105                 tree_view.append_column(*column);
106
107         }
108         {       // --- T Y P E --------------------------------------------------------
109                 int cols_count = tree_view.append_column(_("Type"),model.type);
110                 Gtk::TreeViewColumn* column = tree_view.get_column(cols_count-1);
111                 if(column)
112                 {
113                         column->set_reorderable();
114                         column->set_resizable();
115                         column->set_clickable();
116                         column->set_sort_column_id(model.type);
117                 }
118         }
119         {       // --- V A L U E  -----------------------------------------------------
120                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("ValueBase")) );
121
122                 // Set up the value cell-renderer
123                 cellrenderer_value=ChildrenTreeStore::add_cell_renderer_value(column);
124                 cellrenderer_value->signal_edited().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_edited_value));
125                 cellrenderer_value->property_value()=synfig::ValueBase();
126
127                 // Finish setting up the column
128                 tree_view.append_column(*column);
129                 column->set_sizing(Gtk::TREE_VIEW_COLUMN_AUTOSIZE);
130                 column->set_min_width(150);
131                 column->set_reorderable();
132                 column->set_resizable();
133                 column->set_clickable(false);
134         }
135         {       // --- T I M E   T R A C K --------------------------------------------
136                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Time Track")) );
137                 column_time_track=column;
138
139                 // Set up the value-node cell-renderer
140                 cellrenderer_time_track=ChildrenTreeStore::add_cell_renderer_value_node(column);
141                 cellrenderer_time_track->property_mode()=Gtk::CELL_RENDERER_MODE_ACTIVATABLE;
142                 cellrenderer_time_track->signal_waypoint_clicked().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_waypoint_clicked) );
143                 column->add_attribute(cellrenderer_time_track->property_value_desc(), model.value_desc);
144                 column->add_attribute(cellrenderer_time_track->property_canvas(), model.canvas);
145
146                 //column->pack_start(*cellrenderer_time_track);
147
148                 // Finish setting up the column
149                 column->set_reorderable();
150                 column->set_resizable();
151                 tree_view.append_column(*column);
152         }
153
154         // This makes things easier to read.
155         tree_view.set_rules_hint();
156
157         // Make us more sensitive to several events
158         tree_view.add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::BUTTON1_MOTION_MASK | Gdk::BUTTON2_MOTION_MASK|Gdk::POINTER_MOTION_MASK);
159
160         tree_view.signal_event().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_tree_event));
161
162         // Create a scrolled window for that tree
163         Gtk::ScrolledWindow *scroll_children_tree = manage(new class Gtk::ScrolledWindow());
164         scroll_children_tree->set_flags(Gtk::CAN_FOCUS);
165         scroll_children_tree->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
166         scroll_children_tree->add(tree_view);
167         scroll_children_tree->set_shadow_type(Gtk::SHADOW_ETCHED_IN);
168         scroll_children_tree->show();
169
170         attach(*scroll_children_tree, 0, 3, 0, 1, Gtk::EXPAND|Gtk::FILL,Gtk::EXPAND|Gtk::FILL, 0, 0);
171
172         hbox=manage(new Gtk::HBox());
173
174         attach(*hbox, 0, 1, 1, 2, Gtk::FILL|Gtk::SHRINK, Gtk::SHRINK, 0, 0);
175
176
177
178         tree_view.set_enable_search(true);
179         tree_view.set_search_column(model.label);
180
181
182 /*
183         Gtk::Image *icon;
184         //Gtk::IconSize iconsize(Gtk::IconSize::from_name("synfig-small_icon"));
185         Gtk::IconSize iconsize(Gtk::ICON_SIZE_SMALL_TOOLBAR);
186
187         SMALL_BUTTON(button_raise,"gtk-go-up","Raise");
188         SMALL_BUTTON(button_lower,"gtk-go-down","Lower");
189         SMALL_BUTTON(button_duplicate,"synfig-duplicate","Duplicate");
190         SMALL_BUTTON(button_delete,"gtk-delete","Delete");
191
192         hbox->pack_start(*button_raise,Gtk::PACK_SHRINK);
193         hbox->pack_start(*button_lower,Gtk::PACK_SHRINK);
194         hbox->pack_start(*button_duplicate,Gtk::PACK_SHRINK);
195         hbox->pack_start(*button_delete,Gtk::PACK_SHRINK);
196
197         button_raise->signal_clicked().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_raise_pressed));
198         button_lower->signal_clicked().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_lower_pressed));
199         button_duplicate->signal_clicked().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_duplicate_pressed));
200         button_delete->signal_clicked().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_delete_pressed));
201
202         button_raise->set_sensitive(false);
203         button_lower->set_sensitive(false);
204         button_duplicate->set_sensitive(false);
205         button_delete->set_sensitive(false);
206 */
207
208
209
210         get_selection()->signal_changed().connect(sigc::mem_fun(*this, &studio::ChildrenTree::on_selection_changed));
211
212
213         tree_view.set_reorderable(true);
214
215         hbox->show();
216         tree_view.show();
217
218         tooltips_.enable();
219
220         //get_selection()->set_mode(Gtk::SELECTION_MULTIPLE);
221 }
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         children_tree_store_->canvas_interface()->signal_dirty_preview().connect(sigc::mem_fun(*this,&studio::ChildrenTree::on_dirty_preview));
240 }
241
242 void
243 ChildrenTree::set_time_adjustment(Gtk::Adjustment &adjustment)
244 {
245         cellrenderer_time_track->set_adjustment(adjustment);
246 }
247
248 void
249 ChildrenTree::on_dirty_preview()
250 {
251 }
252
253 void
254 ChildrenTree::on_selection_changed()
255 {
256         if(0)
257                 {
258                 button_raise->set_sensitive(false);
259                 button_lower->set_sensitive(false);
260                 button_duplicate->set_sensitive(false);
261                 button_delete->set_sensitive(false);
262                 return;
263         }
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(const Glib::ustring &path_string, synfig::Waypoint waypoint,int button)
280 {
281         Gtk::TreePath path(path_string);
282
283         const Gtk::TreeRow row = *(tree_view.get_model()->get_iter(path));
284
285         signal_waypoint_clicked()(static_cast<synfigapp::ValueDesc>(row[model.value_desc]),waypoint,button);
286 }
287
288 bool
289 ChildrenTree::on_tree_event(GdkEvent *event)
290 {
291     switch(event->type)
292     {
293         case GDK_BUTTON_PRESS:
294                 {
295                         Gtk::TreeModel::Path path;
296                         Gtk::TreeViewColumn *column;
297                         int cell_x, cell_y;
298                         if(!tree_view.get_path_at_pos(
299                                 int(event->button.x),int(event->button.y),      // x, y
300                                 path, // TreeModel::Path&
301                                 column, //TreeViewColumn*&
302                                 cell_x,cell_y //int&cell_x,int&cell_y
303                                 )
304                         ) break;
305                         const Gtk::TreeRow row = *(tree_view.get_model()->get_iter(path));
306
307                         if(column->get_first_cell_renderer()==cellrenderer_time_track)
308                         {
309                                 return signal_user_click()(event->button.button,row,COLUMNID_TIME_TRACK);
310                         }
311                         else if(column->get_first_cell_renderer()==cellrenderer_value)
312                                 return signal_user_click()(event->button.button,row,COLUMNID_VALUE);
313                         else
314                                 return signal_user_click()(event->button.button,row,COLUMNID_ID);
315
316                 }
317                 break;
318
319         case GDK_MOTION_NOTIFY:
320                 {
321                         Gtk::TreeModel::Path path;
322                         Gtk::TreeViewColumn *column;
323                         int cell_x, cell_y;
324                         if(!tree_view.get_path_at_pos(
325                                 (int)event->button.x,(int)event->button.y,      // x, y
326                                 path, // TreeModel::Path&
327                                 column, //TreeViewColumn*&
328                                 cell_x,cell_y //int&cell_x,int&cell_y
329                                 )
330                         ) break;
331
332                         if(!tree_view.get_model()->get_iter(path))
333                                 break;
334
335                         Gtk::TreeRow row = *(tree_view.get_model()->get_iter(path));
336
337                         if(cellrenderer_time_track==column->get_first_cell_renderer())
338                         {
339                                 // Movement on TimeLine
340                                 return true;
341                         }
342                         else
343                         if(last_tooltip_path.get_depth()<=0 || path!=last_tooltip_path)
344                         {
345                                 tooltips_.unset_tip(*this);
346                                 Glib::ustring tooltips_string(row[model.tooltip]);
347                                 last_tooltip_path=path;
348                                 if(!tooltips_string.empty())
349                                 {
350                                         tooltips_.set_tip(*this,tooltips_string);
351                                         tooltips_.force_window();
352                                 }
353                         }
354                 }
355                 break;
356         case GDK_BUTTON_RELEASE:
357                 break;
358         default:
359                 break;
360         }
361         return false;
362 }
363
364 void
365 ChildrenTree::on_raise_pressed()
366 {
367 }
368
369 void
370 ChildrenTree::on_lower_pressed()
371 {
372 }
373
374 void
375 ChildrenTree::on_duplicate_pressed()
376 {
377 }
378
379 void
380 ChildrenTree::on_delete_pressed()
381 {
382 }