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