5d1a4601679e0024db886c49d51fed72a16dd7a7
[synfig.git] / synfig-studio / trunk / src / gtkmm / dockbook.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file dockbook.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 "dockbook.h"
33 #include "dockable.h"
34 #include "app.h"
35 #include "dockmanager.h"
36
37 #include <gtkmm/image.h>
38 #include <gtkmm/eventbox.h>
39 #include <gtkmm/menu.h>
40
41 #include "general.h"
42
43 #endif
44
45 /* === U S I N G =========================================================== */
46
47 using namespace std;
48 using namespace etl;
49 using namespace synfig;
50 using namespace studio;
51
52 /* === M A C R O S ========================================================= */
53
54 /* === G L O B A L S ======================================================= */
55
56 /* === P R O C E D U R E S ================================================= */
57
58 /* === M E T H O D S ======================================================= */
59
60 DockBook::DockBook()
61 {
62         std::list<Gtk::TargetEntry> listTargets;
63         listTargets.push_back( Gtk::TargetEntry("DOCK") );
64
65         drag_dest_set(listTargets);
66         //set_sensitive(true);
67         set_flags(get_flags()|Gtk::RECEIVES_DEFAULT|Gtk::HAS_GRAB);
68         //add_events(Gdk::ALL_EVENTS_MASK);
69         //set_extension_events(Gdk::EXTENSION_EVENTS_ALL);
70         set_show_tabs(true);
71         deleting_=false;
72 }
73
74 DockBook::~DockBook()
75 {
76         deleting_=true;
77         clear();
78 }
79
80 void
81 DockBook::clear()
82 {
83         while(get_n_pages())
84                 remove(static_cast<Dockable&>(*get_nth_page(get_n_pages()-1)));
85 }
86
87 void
88 DockBook::on_drag_data_received(const Glib::RefPtr<Gdk::DragContext>& context, int, int, const Gtk::SelectionData& selection_data, guint, guint time)
89 {
90         if ((selection_data.get_length() >= 0) && (selection_data.get_format() == 8))
91         {
92                 Dockable& dockable(**reinterpret_cast<Dockable**>(const_cast<guint8*>(selection_data.get_data())));
93                 if(dockable.parent_!=this)
94                         add(dockable);
95                 dockable.present();
96                 context->drag_finish(true, false, time);
97                 return;
98         }
99
100         context->drag_finish(false, false, time);
101 }
102
103 void
104 DockBook::add(Dockable& dockable, int position)
105 {
106         dockable.detach();
107
108         if(position==-1)
109                 append_page(dockable, " ");
110         else
111                 insert_page(dockable, " ", position);
112
113         refresh_tab(&dockable);
114
115         dockable.signal_stock_id_changed().connect(
116                 sigc::bind(
117                         sigc::mem_fun(
118                                 *this,
119                                 &DockBook::refresh_tab
120                         ),
121                         &dockable
122                 )
123         );
124
125         dockable.parent_=this;
126
127         dockable.show();
128
129         //set_current_page(get_n_pages()-1);
130
131         signal_changed_();
132 }
133
134 void
135 DockBook::refresh_tab(Dockable* dockable)
136 {
137         Gtk::Widget* label(dockable->create_tab_label());
138
139         label->signal_button_press_event().connect(
140                 sigc::bind(
141                         sigc::mem_fun(
142                                 *this,
143                                 &DockBook::tab_button_pressed
144                         ),
145                         dockable
146                 )
147         );
148
149         set_tab_label(*dockable, *label);
150         label->show();
151 }
152
153
154 void
155 DockBook::remove(Dockable& dockable)
156 {
157         dockable.hide();
158         remove_page(dockable);
159         dockable.parent_=0;
160
161         if(!deleting_)
162         {
163                 signal_changed_();
164
165                 if(get_n_pages()==0)
166                         signal_empty()();
167         }
168 }
169
170 void
171 DockBook::present()
172 {
173         show();
174 }
175
176 synfig::String
177 DockBook::get_local_contents()const
178 {
179         synfig::String ret;
180
181         for(int i(0);i!=const_cast<DockBook*>(this)->get_n_pages();i++)
182         {
183                 Dockable& dockable(static_cast<Dockable&>(*const_cast<DockBook*>(this)->get_nth_page(i)));
184
185                 if(i)
186                         ret+=", ";
187                 ret+=dockable.get_local_name();
188         }
189
190         return ret;
191 }
192
193 synfig::String
194 DockBook::get_contents()const
195 {
196         synfig::String ret;
197
198         for(int i(0);i!=const_cast<DockBook*>(this)->get_n_pages();i++)
199         {
200                 Dockable& dockable(static_cast<Dockable&>(*const_cast<DockBook*>(this)->get_nth_page(i)));
201
202                 if(i)
203                         ret+=' ';
204                 ret+=dockable.get_name();
205         }
206
207         return ret;
208 }
209
210 void
211 DockBook::set_contents(const synfig::String& x)
212 {
213         synfig::String str(x);
214         while(!str.empty())
215         {
216                 synfig::String::size_type separator=str.find_first_of(' ');
217                 synfig::String dock;
218                 if(separator==synfig::String::npos)
219                 {
220                         dock=str;
221                         str.clear();
222                 }
223                 else
224                 {
225                         dock=String(str.begin(),str.begin()+separator);
226                         str=String(str.begin()+separator+1,str.end());
227                 }
228
229                 try
230                 {
231                         add(App::dock_manager->find_dockable(dock));
232                 }catch(...) { }
233         }
234 }
235
236 bool
237 DockBook::tab_button_pressed(GdkEventButton* event, Dockable* dockable)
238 {
239         if(event->button!=3)
240                 return false;
241
242         Gtk::Menu *tabmenu=manage(new class Gtk::Menu());
243         tabmenu->signal_hide().connect(sigc::bind(sigc::ptr_fun(&delete_widget), tabmenu));
244
245         tabmenu->items().push_back(
246                 Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-close"),
247                         sigc::mem_fun(*dockable,&Dockable::detach)
248                 )
249         );
250
251         tabmenu->popup(event->button,gtk_get_current_event_time());
252
253         return true;
254 }