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