Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_06 / src / gtkmm / dockdialog.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file dockdialog.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 "app.h"
33 #include <sigc++/adaptors/hide.h>
34
35 #include "dockdialog.h"
36 #include "dockbook.h"
37 #include "dockmanager.h"
38 #include "toolbox.h"
39 #include "widget_compselect.h"
40 #include <synfig/general.h>
41 #include <synfig/uniqueid.h>
42 #include <gtkmm/table.h>
43 #include <sigc++/hide.h>
44 #include <sigc++/slot.h>
45 #include <sigc++/retype_return.h>
46 #include <sigc++/retype.h>
47 #include "canvasview.h"
48 #include <gtkmm/paned.h>
49 #include <gtkmm/box.h>
50 #include <synfigapp/main.h>
51
52 #endif
53
54 /* === U S I N G =========================================================== */
55
56 using namespace std;
57 using namespace etl;
58 using namespace synfig;
59 using namespace studio;
60
61 /* === M A C R O S ========================================================= */
62
63 #define GRAB_HINT_DATA(y,default)       { \
64                 String x; \
65                 if(synfigapp::Main::settings().get_value(String("pref.")+y+"_hints",x)) \
66                 { \
67                         set_type_hint((Gdk::WindowTypeHint)atoi(x.c_str()));    \
68                 } else {\
69                         set_type_hint(default); \
70                 } \
71         }
72
73 /* === G L O B A L S ======================================================= */
74
75 /* === P R O C E D U R E S ================================================= */
76
77 /* === M E T H O D S ======================================================= */
78
79 DockDialog::DockDialog():
80         Gtk::Window(Gtk::WINDOW_TOPLEVEL)
81 {
82         composition_selector_=false;
83         is_deleting=false;
84         is_horizontal=false;
85         last_dock_book=0;
86         box=0;
87
88         widget_comp_select=new Widget_CompSelect();
89
90         // Give ourselves an ID that is most likely unique
91         set_id(synfig::UniqueID().get_uid()^reinterpret_cast<long>(this));
92
93         set_role(strprintf("dock_dialog_%d",get_id()));
94         GRAB_HINT_DATA(
95                 "dock_dialog",
96 #ifdef __APPLE__
97                 Gdk::WINDOW_TYPE_HINT_NORMAL
98 #else
99                 Gdk::WINDOW_TYPE_HINT_UTILITY
100 #endif
101         );
102         set_keep_above(false);
103
104         //! \todo can we set dialog windows transient for all normal windows, not just the toolbox?
105         //! paragraph 3 of http://standards.freedesktop.org/wm-spec/1.3/ar01s07.html suggests we can
106         // this seems to have bad effects on KDE, so leave it disabled by default
107         if(getenv("SYNFIG_TRANSIENT_DIALOGS"))
108                 set_transient_for(*App::toolbox);
109
110         // Set up the window
111         //set_type_hint(Gdk::WINDOW_TYPE_HINT_UTILITY);
112         set_title("Dock Dialog");
113
114         // Register with the dock manager
115         App::dock_manager->dock_dialog_list_.push_back(this);
116
117
118         // connect our signals
119         signal_delete_event().connect(
120                 sigc::hide(
121                         sigc::mem_fun(*this,&DockDialog::close)
122                 )
123         );
124
125 /*
126         App::signal_canvas_view_focus().connect(
127                 sigc::hide(
128                         sigc::mem_fun(
129                                 *this,
130                                 &DockDialog::refresh_accel_group
131                         )
132                 )
133         );
134 */
135
136         add_accel_group(App::ui_manager()->get_accel_group());
137         App::signal_present_all().connect(sigc::mem_fun0(*this,&DockDialog::present));
138
139 }
140
141 DockDialog::~DockDialog()
142 {
143         empty_sig.disconnect();
144
145         is_deleting=true;
146
147         DEBUGPOINT();
148
149         // Remove all of the dock books
150         for(;!dock_book_list.empty();dock_book_list.pop_front())
151         {
152                 dock_book_list.front()->clear();
153
154                 // UGLY HACK
155                 // The following line really should be uncommented,
156                 // but it causes crashes. Without it, a small
157                 // memory hole is created--but at least it doesn't crash
158                 // delete dock_book_list.front();
159
160                 // Oddly enough, the following line should
161                 // theoreticly do the same thing after this
162                 // class is destroyed, but it doesn't seem to
163                 // caues a crash.
164                 manage(dock_book_list.front());
165         }
166
167         // Remove us from the dock manager
168         if(App::dock_manager)try{
169                 std::list<DockDialog*>::iterator iter;
170                 for(iter=App::dock_manager->dock_dialog_list_.begin();iter!=App::dock_manager->dock_dialog_list_.end();++iter)
171                         if(*iter==this)
172                         {
173                                 App::dock_manager->dock_dialog_list_.erase(iter);
174                                 break;
175                         }
176         }
177         catch(...)
178         {
179                 synfig::warning("DockDialog::~DockDialog(): Exception thrown when trying to remove from dock manager...?");
180         }
181
182         delete widget_comp_select;
183
184         DEBUGPOINT();
185 }
186
187 void
188 DockDialog::drop_on_prepend(const Glib::RefPtr<Gdk::DragContext>& context, int, int, const Gtk::SelectionData& selection_data, guint, guint time)
189 {
190         if ((selection_data.get_length() >= 0) && (selection_data.get_format() == 8))
191         {
192                 Dockable& dockable(**reinterpret_cast<Dockable**>(const_cast<guint8*>(selection_data.get_data())));
193                 prepend_dock_book()->add(dockable);
194                 context->drag_finish(true, false, time);
195                 return;
196         }
197
198         context->drag_finish(false, false, time);
199 }
200
201 void
202 DockDialog::drop_on_append(const Glib::RefPtr<Gdk::DragContext>& context, int, int, const Gtk::SelectionData& selection_data, guint, guint time)
203 {
204         if ((selection_data.get_length() >= 0) && (selection_data.get_format() == 8))
205         {
206                 Dockable& dockable(**reinterpret_cast<Dockable**>(const_cast<guint8*>(selection_data.get_data())));
207                 append_dock_book()->add(dockable);
208                 context->drag_finish(true, false, time);
209                 return;
210         }
211
212         context->drag_finish(false, false, time);
213 }
214
215
216 void
217 DockDialog::on_hide()
218 {
219         Gtk::Window::on_hide();
220         close();
221 }
222
223 DockBook*
224 DockDialog::prepend_dock_book()
225 {
226         if(is_deleting)return 0;
227
228         dock_book_list.push_front(new DockBook);
229         last_dock_book=dock_book_list.front();
230
231
232         last_dock_book->signal_empty().connect(
233                 sigc::bind(
234                         sigc::mem_fun(*this,&DockDialog::erase_dock_book),
235                         last_dock_book
236                 )
237         );
238
239         dock_book_sizes_.insert(dock_book_sizes_.begin(),225);
240         refresh();
241         return last_dock_book;
242 }
243
244 DockBook*
245 DockDialog::append_dock_book()
246 {
247         if(is_deleting)return 0;
248
249         dock_book_list.push_back(new DockBook);
250         last_dock_book=dock_book_list.back();
251         last_dock_book->signal_empty().connect(
252                 sigc::bind(
253                         sigc::mem_fun(*this,&DockDialog::erase_dock_book),
254                         last_dock_book
255                 )
256         );
257         last_dock_book->signal_changed().connect(
258                 sigc::mem_fun(*this,&DockDialog::refresh_title)
259         );
260         last_dock_book->signal_changed().connect(
261                 sigc::mem_fun(*this,&DockDialog::refresh_title)
262         );
263         dock_book_sizes_.push_back(225);
264
265         //last_dock_book->show();
266         refresh();
267         return last_dock_book;
268 }
269
270 void
271 DockDialog::erase_dock_book(DockBook* dock_book)
272 {
273         if(is_deleting)return;
274
275         std::list<DockBook*>::iterator iter;
276         for(iter=dock_book_list.begin();iter!=dock_book_list.end();++iter)
277                 if(*iter==dock_book)
278                 {
279                         dock_book_list.erase(iter);
280
281                         if(dock_book_list.empty())
282                         {
283                                 last_dock_book=0;
284                                 close();
285                                 return;
286                         }
287                         else
288                         {
289                                 if(last_dock_book==dock_book)
290                                         last_dock_book=dock_book_list.front();
291                         }
292
293                         refresh();
294
295                         return;
296                 }
297 }
298
299 void
300 DockDialog::refresh()
301 {
302         synfig::info("dock_book_list.size()=%d",dock_book_list.size());
303         //remove();
304
305         if(dock_book_list.empty())
306                 return;
307
308         if(box)delete box;
309         box=(manage(is_horizontal?(Gtk::Box*)new Gtk::HBox:(Gtk::Box*)new Gtk::VBox));
310         add(*box);
311
312         box->pack_start(*widget_comp_select,false,true);
313
314         Gtk::Button* append_button(manage(new Gtk::Button));
315         Gtk::Button* prepend_button(manage(new Gtk::Button));
316
317         std::list<Gtk::TargetEntry> listTargets;
318         listTargets.push_back( Gtk::TargetEntry("DOCK") );
319
320         append_button->drag_dest_set(listTargets);
321         prepend_button->drag_dest_set(listTargets);
322
323         append_button->signal_drag_data_received().connect(
324                 sigc::mem_fun(*this,&DockDialog::drop_on_append)
325         );
326
327         prepend_button->signal_drag_data_received().connect(
328                 sigc::mem_fun(*this,&DockDialog::drop_on_prepend)
329         );
330
331         box->pack_start(*prepend_button,false,true);
332         box->pack_end(*append_button,false,true);
333
334         //prepend_button->show();
335         //append_button->show();
336         pannels_.clear();
337
338         if(dock_book_list.size()==1)
339         {
340                 box->pack_start(get_dock_book(),true,true);
341         }
342         else
343         {
344                 Gtk::Paned* parent(manage(is_horizontal?(Gtk::Paned*)new Gtk::HPaned:(Gtk::Paned*)new Gtk::VPaned));
345
346                 pannels_.push_back(parent);
347
348                 if(pannels_.size()<=dock_book_sizes_.size())
349                         pannels_.back()->set_position(dock_book_sizes_[pannels_.size()-1]);
350                 pannels_.back()->property_position().signal_changed().connect(
351                         sigc::mem_fun(*this,&DockDialog::rebuild_sizes)
352                 );
353                 //parent->show();
354                 parent->add1(*dock_book_list.front());
355                 //dock_book_list.front()->show();
356
357                 box->pack_start(*parent,true,true);
358
359                 std::list<DockBook*>::iterator iter,next;
360                 for(next=dock_book_list.begin(),next++,iter=next++;next!=dock_book_list.end();iter=next++)
361                 {
362                         Gtk::Paned* current(manage(is_horizontal?(Gtk::Paned*)new Gtk::HPaned:(Gtk::Paned*)new Gtk::VPaned));
363                         pannels_.push_back(current);
364
365                         if(pannels_.size()<=dock_book_sizes_.size())
366                                 pannels_.back()->set_position(dock_book_sizes_[pannels_.size()-1]);
367                         pannels_.back()->property_position().signal_changed().connect(
368                                 sigc::mem_fun(*this,&DockDialog::rebuild_sizes)
369                         );
370
371
372                         parent->add2(*current);
373
374                         current->add1(**iter);
375                         //(*iter)->show();
376                         //current->show();
377
378                         parent=current;
379                 }
380                 parent->add2(**iter);
381                 //(*iter)->show();
382         }
383
384         box->show_all();
385         if(!composition_selector_)
386                 widget_comp_select->hide();
387         rebuild_sizes();
388 }
389
390 void
391 DockDialog::rebuild_sizes()
392 {
393         unsigned int i=0;
394         dock_book_sizes_.clear();
395         for(i=0;i<pannels_.size();i++)
396         {
397                 dock_book_sizes_.push_back(pannels_[i]->get_position());
398         }
399 }
400
401 void
402 DockDialog::set_dock_book_sizes(const std::vector<int>& new_sizes)
403 {
404         unsigned int i=0;
405         for(i=0;i<pannels_.size() && i<new_sizes.size();i++)
406         {
407                 pannels_[i]->set_position(new_sizes[i]);
408         }
409         dock_book_sizes_=new_sizes;
410         //rebuild_sizes();
411 }
412
413 void
414 DockDialog::refresh_accel_group()
415 {
416 /*
417         if(last_accel_group_)
418         {
419                 last_accel_group_->unlock();
420                 remove_accel_group(last_accel_group_);
421                 last_accel_group_=Glib::RefPtr<Gtk::AccelGroup>();
422         }
423
424         etl::loose_handle<CanvasView> canvas_view(App::get_selected_canvas_view());
425         if(canvas_view)
426         {
427                 last_accel_group_=canvas_view->get_accel_group();
428                 last_accel_group_->lock();
429                 add_accel_group(last_accel_group_);
430         }
431 */
432         etl::loose_handle<CanvasView> canvas_view(App::get_selected_canvas_view());
433         if(canvas_view)
434         {
435                 canvas_view->mainmenu.accelerate(*this);
436         }
437 }
438
439 bool
440 DockDialog::close()
441 {
442         synfig::info("DockDialog::close(): DELETED!");
443         empty_sig.disconnect();
444         //get_dock_book().clear();
445         delete this;
446         return true;
447 }
448
449 DockBook&
450 DockDialog::get_dock_book()
451 {
452         if(!last_dock_book)
453                 return *append_dock_book();
454         return *last_dock_book;
455 }
456
457 const DockBook&
458 DockDialog::get_dock_book()const
459 {
460         return *last_dock_book;
461 }
462
463
464 synfig::String
465 DockDialog::get_contents()const
466 {
467         synfig::String ret;
468
469         std::list<DockBook*>::const_iterator iter;
470         for(iter=dock_book_list.begin();iter!=dock_book_list.end();++iter)
471         {
472                 if(!ret.empty())
473                         ret+=is_horizontal?" | ":" - ";
474                 ret+=(*iter)->get_contents();
475         }
476
477
478         return ret;
479 }
480
481 void
482 DockDialog::set_contents(const synfig::String& z)
483 {
484         int x,y;
485         get_size(x,y);
486
487         synfig::String str(z);
488         while(!str.empty())
489         {
490                 synfig::String::size_type separator=str.find_first_of('-');
491                 {
492                         synfig::String::size_type sep2=str.find_first_of('|');
493                         if(separator!=synfig::String::npos || sep2!=synfig::String::npos)
494                         {
495                                 if((separator==synfig::String::npos || sep2<separator) && sep2!=synfig::String::npos)
496                                 {
497                                         separator=sep2;
498                                         is_horizontal=true;
499                                 }
500                                 else
501                                         is_horizontal=false;
502                         }
503                 }
504
505                 synfig::String book_contents;
506                 if(separator==synfig::String::npos)
507                 {
508                         book_contents=str;
509                         str.clear();
510                 }
511                 else
512                 {
513                         book_contents=String(str.begin(),str.begin()+separator);
514                         str=String(str.begin()+separator+1,str.end());
515                 }
516
517                 try
518                 {
519                         append_dock_book()->set_contents(book_contents);
520                 }catch(...) { }
521         }
522
523         resize(x,y);
524 }
525
526 void
527 DockDialog::set_composition_selector(bool x)
528 {
529         if(x==get_composition_selector())
530                 return;
531         composition_selector_=x;
532         if(x)
533                 widget_comp_select->show();
534         else
535                 widget_comp_select->hide();
536 }
537
538 void
539 DockDialog::refresh_title()
540 {
541         if(is_deleting)return;
542         if(dock_book_list.size())
543         {
544                 synfig::String title;
545
546                 std::list<DockBook*>::const_iterator iter;
547                 for(iter=dock_book_list.begin();iter!=dock_book_list.end();++iter)
548                 {
549                         if(!title.empty())
550                                 title+=", ";
551                         title+=(*iter)->get_local_contents();
552                 }
553                 set_title(title);
554         }
555         else
556                 set_title(_("Empty Dock Dialog"));
557 }