Fix a crash when running single-threaded and dragging the time slider.
[synfig.git] / synfig-studio / trunk / src / gtkmm / dock_layers.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file dock_layers.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) 2007 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 "dock_layers.h"
34 #include "app.h"
35
36 #include <gtkmm/scrolledwindow.h>
37 #include <cassert>
38 #include "instance.h"
39 #include <sigc++/signal.h>
40 #include <sigc++/hide.h>
41 #include <sigc++/retype_return.h>
42 #include <sigc++/slot.h>
43 #include "layertreestore.h"
44 #include "layertree.h"
45 #include "canvasview.h"
46 #include "layeractionmanager.h"
47 //#include <ETL/ref_count>
48
49 #endif
50
51 /* === U S I N G =========================================================== */
52
53 using namespace std;
54 using namespace etl;
55 using namespace synfig;
56 using namespace studio;
57
58 /* === M A C R O S ========================================================= */
59
60 /* === G L O B A L S ======================================================= */
61
62 /* === P R O C E D U R E S ================================================= */
63
64 /*static void do_nothing(reference_counter x)
65 {
66         synfig::info(__FILE__":%d:ref_count.count()=%d",__LINE__,x.count());
67 }*/
68
69 /* === M E T H O D S ======================================================= */
70
71 Dock_Layers::Dock_Layers():
72         Dock_CanvasSpecific("layers",_("Layers"),Gtk::StockID("synfig-layer")),
73 //      layer_action_manager(0)
74         layer_action_manager(new LayerActionManager)
75 {
76         if(layer_action_manager)layer_action_manager->set_ui_manager(App::ui_manager());
77
78         action_group_new_layers=Gtk::ActionGroup::create();
79         action_group_layer_ops=Gtk::ActionGroup::create();
80
81         std::map<synfig::String,synfig::String> category_map;
82
83         // Build layer creation actions
84         synfig::Layer::Book::iterator iter;
85         for(iter=synfig::Layer::book().begin();iter!=synfig::Layer::book().end();++iter)
86         {
87                 synfig::Layer::Book::value_type lyr(*iter);
88
89                 if(lyr.second.category==_("Do Not Use"))
90                         continue;
91
92                 action_group_new_layers->add(Gtk::Action::create(
93                         strprintf("layer-new-%s",lyr.first.c_str()),
94                         layer_icon(lyr.first.c_str()),
95                         lyr.second.local_name,lyr.second.local_name
96                 ),
97                         sigc::hide_return(
98                                 sigc::bind(
99                                         sigc::mem_fun(*this,&studio::Dock_Layers::add_layer),
100                                         lyr.first
101                                 )
102                         )
103                 );
104
105                 category_map[lyr.second.category]+=strprintf("<menuitem action='layer-new-%s' />",lyr.first.c_str());
106
107                 //(*category_map)[lyr.second.category]->items().push_back(Gtk::Menu_Helpers::MenuElem(lyr.second.local_name,
108                 //));
109         }
110
111         {
112                 Glib::RefPtr<Gtk::ActionGroup> action_group_categories(Gtk::ActionGroup::create("layer-category"));
113                 synfig::String layer_ui_info;
114
115                 layer_ui_info+="<ui><popup action='menu-main'><menu action='menu-layer'><menu action='menu-layer-new'>";
116
117                 std::map<synfig::String,synfig::String>::iterator iter;
118                 for(iter=category_map.begin();iter!=category_map.end();++iter)
119                 {
120                         layer_ui_info+=strprintf("<menu action='%s'>%s</menu>",iter->first.c_str(),iter->second.c_str());
121                         action_group_categories->add(Gtk::Action::create(iter->first.c_str(),iter->first.c_str()));
122                 }
123
124                 layer_ui_info+="</menu></menu></popup></ui>";
125
126                 App::ui_manager()->insert_action_group(action_group_categories);
127                 App::ui_manager()->insert_action_group(action_group_new_layers);
128                 App::ui_manager()->add_ui_from_string(layer_ui_info);
129         }
130
131
132         action_group_layer_ops->add( Gtk::Action::create("toolbar-layer", "Layer Ops") );
133         App::ui_manager()->insert_action_group(action_group_layer_ops);
134
135
136     Glib::ustring ui_info =
137         "<ui>"
138         "       <toolbar action='toolbar-layer'>"
139         "       <toolitem action='action-layer_raise' />"
140         "       <toolitem action='action-layer_lower' />"
141         "       <toolitem action='action-layer_duplicate' />"
142         "       <toolitem action='action-layer_remove' />"
143         "       <toolitem action='cut' />"
144         "       <toolitem action='copy' />"
145         "       <toolitem action='paste' />"
146         "       </toolbar>"
147         "</ui>"
148         ;
149
150         App::ui_manager()->add_ui_from_string(ui_info);
151
152         action_group_new_layers->set_sensitive(false);
153
154         set_toolbar(*dynamic_cast<Gtk::Toolbar*>(App::ui_manager()->get_widget("/toolbar-layer")));
155
156
157
158
159
160         /*
161         reference_counter ref_count;
162         synfig::info(__FILE__":%d:ref_count.count()=%d",__LINE__,ref_count.count());
163
164         {
165                 sigc::signal<void> tmp_signal;
166
167                 tmp_signal.connect(
168                         sigc::bind(
169                                 sigc::ptr_fun(do_nothing),
170                                 ref_count
171                         )
172                 );
173
174         synfig::info(__FILE__":%d:ref_count.count()=%d",__LINE__,ref_count.count());
175                 tmp_signal();
176         synfig::info(__FILE__":%d:ref_count.count()=%d",__LINE__,ref_count.count());
177
178                 tmp_signal.clear();
179         synfig::info(__FILE__":%d:ref_count.count()=%d",__LINE__,ref_count.count());
180
181                 tmp_signal();
182         synfig::info(__FILE__":%d:ref_count.count()=%d",__LINE__,ref_count.count());
183                 tmp_signal.connect(
184                         sigc::bind(
185                                 sigc::ptr_fun(do_nothing),
186                                 ref_count
187                         )
188                 );
189         synfig::info(__FILE__":%d:ref_count.count()=%d",__LINE__,ref_count.count());
190         }
191         synfig::info(__FILE__":%d:ref_count.count()=%d",__LINE__,ref_count.count());
192         assert(ref_count.count()==1);
193         */
194 }
195
196
197 Dock_Layers::~Dock_Layers()
198 {
199         delete layer_action_manager;
200 }
201
202
203 void
204 Dock_Layers::init_canvas_view_vfunc(etl::loose_handle<CanvasView> canvas_view)
205 {
206         Glib::RefPtr<LayerTreeStore> layer_tree_store;
207         layer_tree_store=LayerTreeStore::create(canvas_view->canvas_interface());
208
209         canvas_view->set_tree_model(get_name(),layer_tree_store);
210         LayerTree* layer_tree(new LayerTree());
211         layer_tree->set_model(layer_tree_store);
212         layer_tree->set_time_adjustment(canvas_view->time_adjustment());
213
214         layer_tree->signal_edited_value().connect(
215                 sigc::hide_return(
216                         sigc::mem_fun(*canvas_view->canvas_interface(), &synfigapp::CanvasInterface::change_value)
217                 )
218         );
219
220         canvas_view->set_ext_widget(get_name()+"_cmp",layer_tree);
221         canvas_view->set_ext_widget(get_name(),&layer_tree->get_layer_tree_view());
222         canvas_view->set_ext_widget("params",&layer_tree->get_param_tree_view());
223         canvas_view->set_tree_model("params",layer_tree->get_param_tree_view().get_model());
224
225         /*
226         canvas_view->layermenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-delete"),Gtk::AccelKey("Delete"),
227                 sigc::mem_fun(*layer_tree, &LayerTree::on_delete_pressed))
228         );
229         */
230
231         // Hide the time bar
232         if(canvas_view->get_canvas()->rend_desc().get_time_start()==canvas_view->get_canvas()->rend_desc().get_time_end())
233                 canvas_view->hide_timebar();
234         layer_tree_store->rebuild();
235         present();
236 }
237
238 void
239 Dock_Layers::changed_canvas_view_vfunc(etl::loose_handle<CanvasView> canvas_view)
240 {
241         if(canvas_view)
242         {
243                 Gtk::Widget* tree_view(canvas_view->get_ext_widget(get_name()));
244
245                 add(*tree_view);
246                 tree_view->show();
247                 action_group_new_layers->set_sensitive(true);
248                 if(layer_action_manager)
249                 {
250                         layer_action_manager->set_layer_tree(dynamic_cast<LayerTree*>(canvas_view->get_ext_widget(get_name()+"_cmp")));
251                         layer_action_manager->set_canvas_interface(canvas_view->canvas_interface());
252                         layer_action_manager->refresh();
253                 }
254         }
255         else
256         {
257                 action_group_new_layers->set_sensitive(false);
258                 if(layer_action_manager)
259                 {
260                         layer_action_manager->clear();
261                         layer_action_manager->set_canvas_interface(0);
262                         layer_action_manager->set_layer_tree(0);
263                 }
264
265                 clear_previous();
266         }
267 }
268
269 void
270 Dock_Layers::add_layer(synfig::String id)
271 {
272         etl::loose_handle<CanvasView> canvas_view(get_canvas_view());
273         if(canvas_view)
274         {
275                 canvas_view->add_layer(id);
276         }
277 }