Documentation for target_scanline.h
[synfig.git] / synfig-studio / src / gui / docks / 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 "docks/dockbook.h"
34 #include "docks/dockable.h"
35 #include "app.h"
36 #include "docks/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         signal_changed_();
131 }
132
133 void
134 DockBook::refresh_tab(Dockable* dockable)
135 {
136         Gtk::Widget* label(dockable->create_tab_label());
137
138         label->signal_button_press_event().connect(
139                 sigc::bind(
140                         sigc::mem_fun(
141                                 *this,
142                                 &DockBook::tab_button_pressed
143                         ),
144                         dockable
145                 )
146         );
147
148         set_tab_label(*dockable, *label);
149         label->show();
150 }
151
152
153 void
154 DockBook::remove(Dockable& dockable)
155 {
156         dockable.hide();
157         remove_page(dockable);
158         dockable.parent_=0;
159
160         if(!deleting_)
161         {
162                 signal_changed_();
163
164                 if(get_n_pages()==0)
165                         signal_empty()();
166         }
167 }
168
169 void
170 DockBook::present()
171 {
172         show();
173 }
174
175 synfig::String
176 DockBook::get_local_contents()const
177 {
178         synfig::String ret;
179
180         for(int i(0);i!=const_cast<DockBook*>(this)->get_n_pages();i++)
181         {
182                 Dockable& dockable(static_cast<Dockable&>(*const_cast<DockBook*>(this)->get_nth_page(i)));
183
184                 if(i)
185                         ret+=", ";
186                 ret+=dockable.get_local_name();
187         }
188
189         return ret;
190 }
191
192 synfig::String
193 DockBook::get_contents()const
194 {
195         synfig::String ret;
196
197         for(int i(0);i!=const_cast<DockBook*>(this)->get_n_pages();i++)
198         {
199                 Dockable& dockable(static_cast<Dockable&>(*const_cast<DockBook*>(this)->get_nth_page(i)));
200
201                 if(i)
202                         ret+=' ';
203                 ret+=dockable.get_name();
204         }
205
206         return ret;
207 }
208
209 void
210 DockBook::set_contents(const synfig::String& x)
211 {
212         synfig::String str(x);
213         while(!str.empty())
214         {
215                 synfig::String::size_type separator=str.find_first_of(' ');
216                 synfig::String dock;
217                 if(separator==synfig::String::npos)
218                 {
219                         dock=str;
220                         str.clear();
221                 }
222                 else
223                 {
224                         dock=String(str.begin(),str.begin()+separator);
225                         str=String(str.begin()+separator+1,str.end());
226                 }
227
228                 try
229                 {
230                         add(App::dock_manager->find_dockable(dock));
231                 }catch(...) { }
232         }
233 }
234
235 bool
236 DockBook::tab_button_pressed(GdkEventButton* event, Dockable* dockable)
237 {
238         if(event->button!=3)
239                 return false;
240
241         Gtk::Menu *tabmenu=manage(new class Gtk::Menu());
242         tabmenu->signal_hide().connect(sigc::bind(sigc::ptr_fun(&delete_widget), tabmenu));
243
244         tabmenu->items().push_back(
245                 Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-close"),
246                         sigc::mem_fun(*dockable,&Dockable::detach)
247                 )
248         );
249
250         tabmenu->popup(event->button,gtk_get_current_event_time());
251
252         return true;
253 }