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