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