initial version
[synfig.git] / synfig-studio / trunk / src / gtkmm / dockbook.cpp
1 /* === S I N F G =========================================================== */
2 /*!     \file dockbook.cpp
3 **      \brief Template File
4 **
5 **      $Id: dockbook.cpp,v 1.1.1.1 2005/01/07 03:34:36 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "dockbook.h"
32 #include "dockable.h"
33 #include "app.h"
34 #include "dockmanager.h"
35
36 #include <gtkmm/image.h>
37 #include <gtkmm/eventbox.h>
38 #include <gtkmm/menu.h>
39
40 #endif
41
42 /* === U S I N G =========================================================== */
43
44 using namespace std;
45 using namespace etl;
46 using namespace sinfg;
47 using namespace studio;
48
49 /* === M A C R O S ========================================================= */
50
51 /* === G L O B A L S ======================================================= */
52
53 /* === P R O C E D U R E S ================================================= */
54
55 /* === M E T H O D S ======================================================= */
56
57 DockBook::DockBook()
58 {
59         std::list<Gtk::TargetEntry> listTargets;
60         listTargets.push_back( Gtk::TargetEntry("DOCK") );
61
62         drag_dest_set(listTargets);
63         //set_sensitive(true);
64         set_flags(get_flags()|Gtk::RECEIVES_DEFAULT|Gtk::HAS_GRAB);
65         //add_events(Gdk::ALL_EVENTS_MASK);
66         //set_extension_events(Gdk::EXTENSION_EVENTS_ALL);
67         set_show_tabs(true);
68         deleting_=false;
69 }
70
71 DockBook::~DockBook()
72 {
73         deleting_=true;
74         clear();
75 }
76
77 void
78 DockBook::clear()
79 {
80         while(get_n_pages())
81         {
82                 remove(static_cast<Dockable&>(*get_nth_page(get_n_pages()-1)));
83         }
84 }
85
86 void
87 DockBook::on_drag_data_received(const Glib::RefPtr<Gdk::DragContext>& context, int, int, const Gtk::SelectionData& selection_data, guint, guint time)
88 {
89         if ((selection_data.get_length() >= 0) && (selection_data.get_format() == 8))
90         {
91                 Dockable& dockable(**reinterpret_cast<Dockable**>(const_cast<guint8*>(selection_data.get_data())));
92                 if(dockable.parent_!=this)
93                         add(dockable);
94                 dockable.present();
95                 context->drag_finish(true, false, time);
96                 return;
97         }
98         
99         context->drag_finish(false, false, time);
100 }
101
102 void
103 DockBook::add(Dockable& dockable, int position)
104 {
105         dockable.detach();
106         
107         if(position==-1)
108                 append_page(dockable, " ");
109         else
110                 insert_page(dockable, " ", position);
111
112         refresh_tab(&dockable);
113
114         dockable.signal_stock_id_changed().connect(
115                 sigc::bind(
116                         sigc::mem_fun(
117                                 *this,
118                                 &DockBook::refresh_tab
119                         ),
120                         &dockable
121                 )
122         );
123         
124         dockable.parent_=this;
125
126         dockable.show();
127
128         //set_current_page(get_n_pages()-1);
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 sinfg::String
176 DockBook::get_local_contents()const
177 {
178         sinfg::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 sinfg::String
193 DockBook::get_contents()const
194 {
195         sinfg::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 sinfg::String& x)
211 {
212         sinfg::String str(x);
213         while(!str.empty())
214         {
215                 unsigned int separator=str.find_first_of(' ');
216                 sinfg::String dock;
217                 if(separator==sinfg::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         
243         tabmenu->items().push_back(
244                 Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-close"),
245                         sigc::mem_fun(*dockable,&Dockable::detach)
246                 )
247         );
248
249         tabmenu->popup(event->button,gtk_get_current_event_time());
250         
251         return true;
252 }