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