Add actions (add to group, ,remove from group, export, un-export, set layer descripti...
[synfig.git] / synfig-studio / src / gtkmm / dock_canvases.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file dock_canvases.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 "dock_canvases.h"
33 #include "app.h"
34 #include "canvasview.h"
35 #include "general.h"
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 Dock_Canvases::Dock_Canvases():
55         Dockable("canvases",_("Canvas Browser"),Gtk::StockID("synfig-canvas"))
56 {
57
58         App::signal_instance_created().connect(sigc::mem_fun(*this,&studio::Dock_Canvases::new_instance));
59         App::signal_instance_deleted().connect(sigc::mem_fun(*this,&studio::Dock_Canvases::delete_instance));
60         App::signal_instance_selected().connect(sigc::mem_fun(*this,&studio::Dock_Canvases::set_selected_instance_signal));
61
62
63         add(*create_canvas_tree());
64
65 /*
66         add_button(
67                 Gtk::StockID("synfig-canvas_new"),
68                 _("Insert a new canvas")
69         )->signal_clicked().connect(
70                 sigc::mem_fun(
71                         *this,
72                         &Dock_Canvases::menu_new_canvas
73                 )
74         );
75
76         add_button(
77                 Gtk::StockID("gtk-delete"),
78                 _("Remove selected canvas")
79         )->signal_clicked().connect(
80                 sigc::mem_fun(
81                         *this,
82                         &Dock_Canvases::menu_delete
83                 )
84         );
85
86         add_button(
87                 Gtk::StockID("synfig-rename"),
88                 _("Rename selected canvas")
89         )->signal_clicked().connect(
90                 sigc::mem_fun(
91                         *this,
92                         &Dock_Canvases::menu_rename
93                 )
94         );
95         */
96 }
97
98 Dock_Canvases::~Dock_Canvases()
99 {
100 }
101
102 Gtk::Widget*
103 Dock_Canvases::create_canvas_tree()
104 {
105         studio::Instance::CanvasTreeModel canvas_tree_model;
106         canvas_tree=manage(new class Gtk::TreeView());
107         {
108                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("ID")) );
109 //              Gtk::CellRendererPixbuf* icon_cellrenderer = Gtk::manage( new Gtk::CellRendererPixbuf() );
110
111                 //column->pack_start(*icon_cellrenderer,false);
112                 column->pack_start(canvas_tree_model.icon, false); //false = don't expand.
113                 column->pack_start(canvas_tree_model.label);
114
115 //#ifdef NDEBUG
116 //              column->add_attribute(icon_cellrenderer->property_pixbuf(), canvas_tree_model.icon);
117 //#endif
118
119                 canvas_tree->append_column(*column);
120         }
121         canvas_tree->set_rules_hint();
122         canvas_tree->signal_row_activated().connect(sigc::mem_fun(*this,&Dock_Canvases::on_row_activate));
123         //canvas_tree->signal_event().connect(sigc::mem_fun(*this,&Dock_Canvases::on_tree_event));
124         canvas_tree->add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
125         canvas_tree->add_events(Gdk::BUTTON1_MOTION_MASK);
126         canvas_tree->show();
127         canvas_tree->set_headers_visible(false);
128
129         Gtk::ScrolledWindow *scrolledwindow = manage(new class Gtk::ScrolledWindow());
130         scrolledwindow->set_flags(Gtk::CAN_FOCUS);
131         scrolledwindow->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
132         scrolledwindow->add(*canvas_tree);
133         scrolledwindow->set_shadow_type(Gtk::SHADOW_ETCHED_IN);
134         scrolledwindow->show_all();
135
136         return scrolledwindow;
137 }
138
139 etl::loose_handle<studio::CanvasView>
140 Dock_Canvases::get_selected_canvas_view()
141 {
142         return get_selected_instance()->find_canvas_view(get_selected_canvas());
143 }
144
145 etl::loose_handle<synfig::Canvas>
146 Dock_Canvases::get_selected_canvas()
147 {
148         Glib::RefPtr<Gtk::TreeSelection> selection=canvas_tree->get_selection();
149
150         if(!selection || !selection->get_selected())
151                 return 0;
152
153         studio::Instance::CanvasTreeModel canvas_tree_model;
154
155         return static_cast<etl::handle<synfig::Canvas> >((*selection->get_selected())[canvas_tree_model.canvas]);
156 }
157
158
159
160 void
161 Dock_Canvases::set_selected_instance_signal(etl::handle<studio::Instance> x)
162 {
163         set_selected_instance(x);
164 }
165
166 void
167 Dock_Canvases::set_selected_instance_(etl::handle<studio::Instance> instance)
168 {
169         if(studio::App::shutdown_in_progress)
170                 return;
171
172         selected_instance=instance;
173         if(instance)
174         {
175                 canvas_tree->set_model(instance->canvas_tree_store());
176                 canvas_tree->show();
177         }
178         else
179         {
180                 canvas_tree->set_model(Glib::RefPtr< Gtk::TreeModel >());
181                 canvas_tree->hide();
182         }
183 }
184
185 void
186 Dock_Canvases::set_selected_instance(etl::loose_handle<studio::Instance> x)
187 {
188         if(studio::App::shutdown_in_progress)
189                 return;
190
191         // if it's already selected, don't select it again
192         if (x==selected_instance)
193                 return;
194
195         set_selected_instance_(x);
196 }
197
198 void
199 Dock_Canvases::new_instance(etl::handle<studio::Instance> instance)
200 {
201         if(studio::App::shutdown_in_progress)
202                 return;
203
204         assert(instance);
205
206         etl::loose_handle<studio::Instance> loose_instance(instance);
207
208         instance->synfigapp::Instance::signal_filename_changed().connect(sigc::mem_fun(*this,&Dock_Canvases::refresh_instances));
209         instance->synfigapp::Instance::signal_filename_changed().connect(
210                 sigc::bind<etl::loose_handle<studio::Instance> >(
211                         sigc::mem_fun(*this,&Dock_Canvases::set_selected_instance),
212                         loose_instance
213                 )
214         );
215
216         present();
217
218 }
219
220 void
221 Dock_Canvases::delete_instance(etl::handle<studio::Instance> instance)
222 {
223         if(studio::App::shutdown_in_progress)
224                 return;
225
226         refresh_instances();
227
228         if(selected_instance==instance)
229         {
230                 set_selected_instance(0);
231         }
232 }
233
234 void
235 Dock_Canvases::refresh_instances()
236 {
237         if(studio::App::shutdown_in_progress)
238                 return;
239 }
240
241 void
242 Dock_Canvases::on_row_activate(const Gtk::TreeModel::Path &path, Gtk::TreeViewColumn *)
243 {
244         assert(get_selected_instance());
245         studio::Instance::CanvasTreeModel canvas_tree_model;
246         const Gtk::TreeRow row = *(get_selected_instance()->canvas_tree_store()->get_iter(path));
247         if(row[canvas_tree_model.is_canvas])
248                 get_selected_instance()->focus(row[canvas_tree_model.canvas]);
249         else
250                 studio::App::dialog_not_implemented();
251 }