Fix a crash when running single-threaded and dragging the time slider.
[synfig.git] / synfig-studio / trunk / src / gtkmm / layergrouptree.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layergrouptree.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 <synfig/layer.h>
33 #include "layergrouptree.h"
34 #include <gtkmm/treemodelsort.h>
35 #include <ETL/misc>
36
37 #endif
38
39 /* === U S I N G =========================================================== */
40
41 using namespace std;
42 using namespace etl;
43 using namespace synfig;
44 using namespace studio;
45
46 /* === M A C R O S ========================================================= */
47
48 /* === G L O B A L S ======================================================= */
49
50 /* === P R O C E D U R E S ================================================= */
51
52 /* === M E T H O D S ======================================================= */
53
54 LayerGroupTree::LayerGroupTree()
55 {
56         const LayerGroupTreeStore::Model model;
57
58
59         {       // --- O N / O F F ----------------------------------------------------
60                 int index;
61                 index=append_column_editable(_(" "),model.active);
62                 //Gtk::TreeView::Column* column = get_column(index-1);
63         }
64         {       // --- I C O N --------------------------------------------------------
65                 int index;
66                 index=append_column(_(" "),model.icon);
67                 Gtk::TreeView::Column* column = get_column(index-1);
68                 set_expander_column(*column);
69         }
70         {       // --- N A M E --------------------------------------------------------
71                 int index;
72                 index=append_column_editable(_("Name"),model.label);
73                 label_column = get_column(index-1);
74
75                 //column->set_sort_column_id(layer_model.index);
76
77                 //set_expander_column(*column);
78                 //column->set_reorderable();
79                 //column->set_resizable();
80                 //column->set_clickable(false);
81
82                 //Gtk::CellRendererPixbuf* icon_cellrenderer = Gtk::manage( new Gtk::CellRendererPixbuf() );
83                 //column->pack_start(*icon_cellrenderer,false);
84                 //column->add_attribute(icon_cellrenderer->property_pixbuf(), layer_model.icon);
85         }
86
87         set_enable_search(true);
88         set_search_column(model.label);
89         set_search_equal_func(sigc::ptr_fun(&studio::LayerGroupTreeStore::search_func));
90
91         // This makes things easier to read.
92         set_rules_hint();
93
94         // Make us more sensitive to several events
95         add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::BUTTON1_MOTION_MASK | Gdk::BUTTON2_MOTION_MASK|Gdk::POINTER_MOTION_MASK);
96
97         set_reorderable(true);
98
99         get_selection()->set_mode(Gtk::SELECTION_MULTIPLE);
100
101         //set_flags(get_flags()|Gtk::RECEIVES_DEFAULT|Gtk::HAS_GRAB);
102
103         //std::list<Gtk::TargetEntry> listTargets;
104         //listTargets.push_back( Gtk::TargetEntry("LAYER") );
105         //listTargets.push_back( Gtk::TargetEntry("GROUP") );
106         //drag_dest_set(listTargets);
107 }
108
109 LayerGroupTree::~LayerGroupTree()
110 {
111         synfig::info("LayerGroupTree::~LayerGroupTree(): deleted");
112 }
113
114 void
115 LayerGroupTree::set_model(Glib::RefPtr<LayerGroupTreeStore> layer_group_tree_store)
116 {
117         layer_group_tree_store_=layer_group_tree_store;
118         LayerGroupTreeStore::Model model;
119
120 #if 0
121         {
122                 Glib::RefPtr<Gtk::TreeModelSort> sorted_store(Gtk::TreeModelSort::create(layer_group_tree_store_));
123                 sorted_store->set_default_sort_func(sigc::ptr_fun(&studio::LayerGroupTreeStore::time_sorter));
124                 sorted_store->set_sort_func(model.time.index(),sigc::ptr_fun(&studio::LayerGroupTreeStore::time_sorter));
125                 sorted_store->set_sort_column_id(model.time.index(), Gtk::SORT_ASCENDING);
126                 Gtk::TreeView::set_model(sorted_store);
127         }
128 #else
129                 Gtk::TreeView::set_model(layer_group_tree_store);
130 #endif
131 }
132
133 void
134 LayerGroupTree::set_editable(bool x)
135 {
136         editable_=x;
137 /*
138         if(editable_)
139         {
140                 cell_renderer_time->property_editable()=true;
141                 cell_renderer_time_delta->property_editable()=true;
142                 cell_renderer_description->property_editable()=true;
143         }
144         else
145         {
146                 cell_renderer_time->property_editable()=false;
147                 cell_renderer_time_delta->property_editable()=false;
148                 cell_renderer_description->property_editable()=false;
149         }
150 */
151 }
152 /*
153 void
154 LayerGroupTree::on_edited_time(const Glib::ustring&path_string,synfig::Time time)
155 {
156         Gtk::TreePath path(path_string);
157
158         const Gtk::TreeRow row(*(get_model()->get_iter(path)));
159
160         synfig::Keyframe keyframe(row[model.keyframe]);
161         if(time!=keyframe.get_time())
162         {
163                 row[model.time]=time;
164                 //keyframe.set_time(time);
165                 //signal_edited_time()(keyframe,time);
166                 //signal_edited()(keyframe);
167         }
168 }
169
170 void
171 LayerGroupTree::on_edited_time_delta(const Glib::ustring&path_string,synfig::Time time)
172 {
173         Gtk::TreePath path(path_string);
174
175         const Gtk::TreeRow row(*(get_model()->get_iter(path)));
176
177         if(row)row[model.time_delta]=time;
178 }
179
180 void
181 LayerGroupTree::on_edited_description(const Glib::ustring&path_string,const Glib::ustring &desc)
182 {
183         Gtk::TreePath path(path_string);
184
185         const Gtk::TreeRow row = *(get_model()->get_iter(path));
186
187         const synfig::String description(desc);
188         synfig::Keyframe keyframe(row[model.keyframe]);
189         if(description!=keyframe.get_description())
190         {
191                 row[model.description]=desc;
192                 keyframe.set_description(description);
193                 signal_edited_description()(keyframe,description);
194                 signal_edited()(keyframe);
195         }
196 }
197 */
198
199 bool
200 LayerGroupTree::on_event(GdkEvent *event)
201 {
202     switch(event->type)
203     {
204         case GDK_BUTTON_PRESS:
205                 {
206                         Gtk::TreeModel::Path path;
207                         Gtk::TreeViewColumn *column;
208                         int cell_x, cell_y;
209                         int wx(round_to_int(event->button.x)),wy(round_to_int(event->button.y));
210                         //tree_to_widget_coords (,, wx, wy);
211                         if(!get_path_at_pos(
212                                 wx,wy,  // x, y
213                                 path, // TreeModel::Path&
214                                 column, //TreeViewColumn*&
215                                 cell_x,cell_y //int&cell_x,int&cell_y
216                                 )
217                         ) break;
218                         const Gtk::TreeRow row = *(get_model()->get_iter(path));
219
220                         if(row[model.is_layer] && event->button.button==3)
221                         {
222                                 signal_popup_layer_menu()((Layer::Handle)row[model.layer]);
223                                 return true;
224                         }
225
226                         /*signal_user_click()(event->button.button,row,(ColumnID)column->get_sort_column_id());
227                         if((ColumnID)column->get_sort_column_id()==COLUMNID_JUMP)
228                         {
229                                 layer_group_tree_store_->canvas_interface()->set_time(row[model.time]);
230                         }*/
231                 }
232                 break;
233         case GDK_2BUTTON_PRESS:
234                 {
235                         Gtk::TreeModel::Path path;
236                         Gtk::TreeViewColumn *column;
237                         int cell_x, cell_y;
238                         if(!get_path_at_pos(
239                                 int(event->button.x),int(event->button.y),      // x, y
240                                 path, // TreeModel::Path&
241                                 column, //TreeViewColumn*&
242                                 cell_x,cell_y //int&cell_x,int&cell_y
243                                 )
244                         ) break;
245                         const Gtk::TreeRow row = *(get_model()->get_iter(path));
246
247                         LayerList layer_list(row[model.all_layers]);
248                         if(!layer_list.empty())
249                         {
250                                 if(!(event->button.state&GDK_CONTROL_MASK))
251                                 {
252                                         layer_group_tree_store_->canvas_interface()->get_selection_manager()->clear_selected_layers();
253                                 }
254                                 layer_group_tree_store_->canvas_interface()->get_selection_manager()->set_selected_layers(layer_list);
255                                 return true;
256                         }
257                 }
258                 break;
259         case GDK_BUTTON_RELEASE:
260                 break;
261         default:
262                 break;
263         }
264         return Gtk::TreeView::on_event(event);
265         //return false;
266 }
267
268 static inline void __group_grabber(const Gtk::TreeModel::iterator& iter, std::list<synfig::String>* ret)
269 {
270         const LayerGroupTreeStore::Model model;
271         if((bool)(*iter)[model.is_group])
272                 ret->push_back((Glib::ustring)(*iter)[model.group_name]);
273 }
274
275 std::list<synfig::String>
276 LayerGroupTree::get_selected_groups()const
277 {
278         Glib::RefPtr<Gtk::TreeSelection> selection=const_cast<LayerGroupTree&>(*this).get_selection();
279
280         if(!selection)
281                 return std::list<synfig::String>();
282
283         std::list<synfig::String> ret;
284
285         selection->selected_foreach_iter(
286                 sigc::bind(
287                         sigc::ptr_fun(
288                                 &__group_grabber
289                         ),
290                         &ret
291                 )
292         );
293
294         return ret;
295 }
296
297 static inline void __layer_grabber(const Gtk::TreeModel::iterator& iter, LayerGroupTree::LayerList* ret)
298 {
299         const LayerGroupTreeStore::Model model;
300         if((bool)(*iter)[model.is_layer])
301                 ret->push_back((Layer::Handle)(*iter)[model.layer]);
302 }
303
304 LayerGroupTree::LayerList
305 LayerGroupTree::get_selected_layers()const
306 {
307         Glib::RefPtr<Gtk::TreeSelection> selection=const_cast<LayerGroupTree&>(*this).get_selection();
308
309         if(!selection)
310                 return LayerList();
311
312         LayerList ret;
313
314         selection->selected_foreach_iter(
315                 sigc::bind(
316                         sigc::ptr_fun(
317                                 &__layer_grabber
318                         ),
319                         &ret
320                 )
321         );
322
323         return ret;
324 }
325
326 void
327 LayerGroupTree::set_cursor(const Gtk::TreeModel::Path& path, bool start_editing)
328 {
329         Gtk::TreeView::set_cursor(path, *label_column, start_editing);
330 }