Move states into subfolder
[synfig.git] / synfig-studio / src / gui / dockable.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file dockable.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 "app.h"
33 #include <sigc++/hide.h>
34
35 #include "dockable.h"
36 #include "dockmanager.h"
37 #include "dockbook.h"
38 #include "dockdialog.h"
39 #include <synfig/general.h>
40 #include <gtkmm/table.h>
41 #include <gtk/gtk.h>
42
43 #include "general.h"
44
45 #endif
46
47 /* === U S I N G =========================================================== */
48
49 using namespace std;
50 using namespace etl;
51 using namespace synfig;
52 using namespace studio;
53
54 /* === M A C R O S ========================================================= */
55
56 #ifdef WIN32
57 #       ifdef IMAGE_DIR
58 #               undef IMAGE_DIR
59 #               define IMAGE_DIR "share\\pixmaps"
60 #       endif
61 #endif
62
63 #ifndef IMAGE_DIR
64 #       define IMAGE_DIR "/usr/local/share/pixmaps"
65 #endif
66
67 #ifndef IMAGE_EXT
68 #       define IMAGE_EXT        "png"
69 #endif
70
71 /* === G L O B A L S ======================================================= */
72
73 /* === P R O C E D U R E S ================================================= */
74
75 /* === M E T H O D S ======================================================= */
76
77 Dockable::Dockable(const synfig::String& name,const synfig::String& local_name,Gtk::StockID stock_id_):
78 //      Gtk::Window(Gtk::WINDOW_TOPLEVEL),
79         name_(name),
80         local_name_(local_name),
81 //      dialog_settings(this,name),
82         title_label_(local_name,Gtk::ALIGN_LEFT),
83         stock_id_(stock_id_)
84 {
85         parent_=0;
86         scrolled_=0;
87
88         use_scrolled_=true;
89
90         attach_dnd_to(title_label_);
91
92         toolbar_=0;
93         //button_box_.show();
94
95         Gtk::Table* table(this);
96
97         {
98                 title_label_.set_padding(0,0);
99                 //title_label_.show();
100                 Gtk::EventBox* event_box(manage(new Gtk::EventBox()));
101                 event_box->set_border_width(0);
102                 event_box->add(title_label_);
103                 //table->attach(*event_box, 0, 1, 0,1, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
104
105                 header_box_.pack_start(*event_box);
106
107                 attach_dnd_to(*event_box);
108                 event_box->show();
109         //      event_box->set_events(Gdk::ALL_EVENTS_MASK); //!< \todo change this to only allow what is necessary for DnD
110
111
112                 Gtk::Button* bttn_close(manage(new Gtk::Button(_("X"))));
113                 //table->attach(*bttn_close, 1, 2, 0,1, Gtk::SHRINK|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
114                 header_box_.pack_end(*bttn_close,false,false);
115                 bttn_close->show();
116                 bttn_close->set_relief(Gtk::RELIEF_NONE);
117                 bttn_close->signal_clicked().connect(sigc::mem_fun(*this,&Dockable::detach));
118                 bttn_close->set_border_width(0);
119                 dynamic_cast<Gtk::Misc*>(bttn_close->get_child())->set_padding(0,0);
120         }
121
122         prev_widget_=manage(new Gtk::Label(" "));
123
124         //table->attach(header_box_, 0, 1, 0,1, Gtk::SHRINK|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
125         table->attach(*prev_widget_, 0, 1, 1,2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
126         //table->attach(*toolbar_, 0, 1, 2,3, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
127         set_toolbar(*manage(new Gtk::Toolbar));
128         table->show();
129
130         prev_widget_->show();
131
132         set_size_request(175,120);
133
134 }
135
136 Dockable::~Dockable()
137 {
138         if(scrolled_)
139         {
140                 delete scrolled_;
141                 scrolled_=0;
142         }
143 }
144
145 void
146 Dockable::attach_dnd_to(Gtk::Widget& widget)
147 {
148         std::list<Gtk::TargetEntry> listTargets;
149         listTargets.push_back( Gtk::TargetEntry("DOCK") );
150
151         widget.drag_source_set(listTargets);
152         widget.drag_source_set_icon(get_stock_id());
153         widget.drag_dest_set(listTargets);
154
155
156         widget.signal_drag_data_get().connect(sigc::mem_fun(*this,&Dockable::on_drag_data_get));
157         widget.signal_drag_end().connect(sigc::mem_fun(*this,&Dockable::on_drag_end));
158         widget.signal_drag_begin().connect(sigc::mem_fun(*this,&Dockable::on_drag_begin));
159         widget.signal_drag_data_received().connect(sigc::mem_fun(*this,&Dockable::on_drag_data_received));
160 }
161
162 void
163 Dockable::on_drag_data_received(const Glib::RefPtr<Gdk::DragContext>& context, int, int, const Gtk::SelectionData& selection_data, guint, guint time)
164 {
165         if ((selection_data.get_length() >= 0) && (selection_data.get_format() == 8))
166         {
167                 Dockable& dockable(**reinterpret_cast<Dockable**>(const_cast<guint8*>(selection_data.get_data())));
168
169                 if(dockable.parent_ != parent_)
170                         parent_->add(dockable,parent_->page_num(*this));
171                 else
172                         parent_->reorder_child(dockable,parent_->page_num(*this));
173                 dockable.present();
174                 context->drag_finish(true, false, time);
175                 return;
176         }
177
178         context->drag_finish(false, false, time);
179 }
180
181 void
182 Dockable::on_drag_end(const Glib::RefPtr<Gdk::DragContext>&/*context*/)
183 {
184         if(!dnd_success_)
185         {
186                 detach();
187                 present();
188         }
189 }
190
191 void
192 Dockable::on_drag_begin(const Glib::RefPtr<Gdk::DragContext>&/*context*/)
193 {
194         dnd_success_=false;
195 }
196
197 void
198 Dockable::on_drag_data_get(const Glib::RefPtr<Gdk::DragContext>&, Gtk::SelectionData& selection_data, guint /*info*/, guint /*time*/)
199 {
200         Dockable* tmp(this);
201         dnd_success_=true;
202
203         selection_data.set(8, reinterpret_cast<const guchar*>(&tmp), 4);
204 }
205
206 void
207 Dockable::set_local_name(const synfig::String& local_name)
208 {
209         //set_title(local_name);
210         title_label_.set_text(local_name);
211 }
212
213 void
214 Dockable::clear()
215 {
216         //if(!toolbar_->children().empty())
217         //      toolbar_->children().clear();
218         set_toolbar(*manage(new Gtk::Toolbar));
219
220 }
221
222 void
223 Dockable::set_toolbar(Gtk::Toolbar& toolbar)
224 {
225         if(toolbar_)remove(*toolbar_);
226         toolbar_=0;
227         toolbar_=&toolbar;
228         if(toolbar_)
229         {
230                 attach(*toolbar_, 0, 1, 2,3, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
231                 gtk_toolbar_set_icon_size(toolbar_->gobj(),GtkIconSize(1)/*GTK_ICON_SIZE_MENU*/);
232                 toolbar_->show();
233         }
234 }
235
236 bool
237 Dockable::clear_previous()
238 {
239         prev_widget_=0;
240         prev_widget_delete_connection.disconnect();
241         return false;
242 }
243
244 void
245 Dockable::add(Gtk::Widget& x)
246 {
247         if(prev_widget_)
248         {
249                 remove(*prev_widget_);
250                 clear_previous();
251         }
252
253         if(scrolled_)
254         {
255                 delete scrolled_;
256                 scrolled_=0;
257         }
258
259         if(use_scrolled_)
260         {
261                 scrolled_=new Gtk::ScrolledWindow;
262
263                 scrolled_->add(x);
264
265                 attach(*scrolled_, 0, 1, 1,2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
266
267                 x.show();
268
269                 scrolled_->show();
270
271                 scrolled_->set_shadow_type(Gtk::SHADOW_NONE);
272                 scrolled_->set_policy(Gtk::POLICY_AUTOMATIC,Gtk::POLICY_AUTOMATIC);
273                 prev_widget_=scrolled_;
274         }
275         else
276         {
277                 attach(x, 0, 1, 1,2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
278                 x.show();
279                 prev_widget_=&x;
280         }
281         prev_widget_delete_connection=prev_widget_->signal_delete_event().connect(
282                 sigc::hide(
283                         sigc::mem_fun(
284                                 *this,
285                                 &Dockable::clear_previous
286                         )
287                 )
288         );
289 }
290
291 Gtk::ToolButton*
292 Dockable::add_button(const Gtk::StockID& stock_id, const synfig::String& tooltip)
293 {
294         if(!toolbar_)
295                 set_toolbar(*manage(new Gtk::Toolbar));
296
297         //Gtk::IconSize iconsize(4);
298         //Gtk::IconSize iconsize(Gtk::IconSize::from_name("synfig-small_icon"));
299
300         Gtk::ToolButton* ret(manage(new Gtk::ToolButton(stock_id)));
301         //Gtk::Image* icon(manage(new Gtk::Image(stock_id,iconsize)));
302         //ret->add(*icon);
303         //ret->set_relief(Gtk::RELIEF_HALF);
304         //ret->set_relief(Gtk::RELIEF_NONE);
305         ret->set_label(tooltip);
306         if (toolbar_->get_tooltips_object())
307                 toolbar_->get_tooltips_object()->set_tip(*ret,tooltip);
308
309         ret->show();
310         //icon->show();
311         toolbar_->set_tooltips(true);
312
313         toolbar_->append(*ret);
314         //button_box_.pack_start(*ret,false,false);
315         //get_action_area()->pack_start(*ret,false,false);
316         //add_action_widget(*ret,1);
317         return ret;
318 }
319
320
321 void
322 Dockable::detach()
323 {
324         if(parent_)
325                 parent_->remove(*this);
326 }
327
328 void
329 Dockable::present()
330 {
331         if(parent_)
332         {
333                 parent_->set_current_page(parent_->page_num(*this));
334                 parent_->present();
335         }
336         else
337         {
338                 DockDialog* dock_dialog(new DockDialog());
339                 dock_dialog->get_dock_book().add(*this);
340 /*              //hack: always display composition selector on top of canvas browser
341                 if(get_name()=="canvases")
342                         dock_dialog->set_composition_selector(true);
343 */
344                 dock_dialog->present();
345         }
346 }
347
348 Gtk::Widget*
349 Dockable::create_tab_label()
350 {
351         Gtk::EventBox* event_box(manage(new Gtk::EventBox()));
352
353         attach_dnd_to(*event_box);
354
355         {
356                 Gtk::StockID stock_id(get_stock_id());
357                 Gtk::StockItem item;
358
359                 // Check to make sure the icon is valid
360                 if(Gtk::Stock::lookup(stock_id,item))
361                 {
362                         Gtk::Image* icon(manage(new Gtk::Image(stock_id,Gtk::IconSize(4))));
363                         event_box->add(*icon);
364                         tooltips_.set_tip(*event_box,get_local_name());
365                         icon->show();
366                 }
367                 else
368                 {
369                         // Bad icon, try to make a label
370
371                         Glib::ustring text(get_local_name());
372
373                         Gtk::Label* label(manage(new Gtk::Label(text)));
374                         event_box->add(*label);
375                         label->show();
376                 }
377         }
378
379         return event_box;
380 }