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