Removed a couple of debugpoints.
[synfig.git] / synfig-studio / trunk / 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         // Remove all of the dock books
148         for(;!dock_book_list.empty();dock_book_list.pop_front())
149         {
150                 dock_book_list.front()->clear();
151
152                 // UGLY HACK
153                 // The following line really should be uncommented,
154                 // but it causes crashes. Without it, a small
155                 // memory hole is created--but at least it doesn't crash
156                 // delete dock_book_list.front();
157
158                 // Oddly enough, the following line should
159                 // theoreticly do the same thing after this
160                 // class is destroyed, but it doesn't seem to
161                 // caues a crash.
162                 manage(dock_book_list.front());
163         }
164
165         // Remove us from the dock manager
166         if(App::dock_manager)try{
167                 std::list<DockDialog*>::iterator iter;
168                 for(iter=App::dock_manager->dock_dialog_list_.begin();iter!=App::dock_manager->dock_dialog_list_.end();++iter)
169                         if(*iter==this)
170                         {
171                                 App::dock_manager->dock_dialog_list_.erase(iter);
172                                 break;
173                         }
174         }
175         catch(...)
176         {
177                 synfig::warning("DockDialog::~DockDialog(): Exception thrown when trying to remove from dock manager...?");
178         }
179
180         delete widget_comp_select;
181 }
182
183 void
184 DockDialog::drop_on_prepend(const Glib::RefPtr<Gdk::DragContext>& context, int, int, const Gtk::SelectionData& selection_data, guint, guint time)
185 {
186         if ((selection_data.get_length() >= 0) && (selection_data.get_format() == 8))
187         {
188                 Dockable& dockable(**reinterpret_cast<Dockable**>(const_cast<guint8*>(selection_data.get_data())));
189                 prepend_dock_book()->add(dockable);
190                 context->drag_finish(true, false, time);
191                 return;
192         }
193
194         context->drag_finish(false, false, time);
195 }
196
197 void
198 DockDialog::drop_on_append(const Glib::RefPtr<Gdk::DragContext>& context, int, int, const Gtk::SelectionData& selection_data, guint, guint time)
199 {
200         if ((selection_data.get_length() >= 0) && (selection_data.get_format() == 8))
201         {
202                 Dockable& dockable(**reinterpret_cast<Dockable**>(const_cast<guint8*>(selection_data.get_data())));
203                 append_dock_book()->add(dockable);
204                 context->drag_finish(true, false, time);
205                 return;
206         }
207
208         context->drag_finish(false, false, time);
209 }
210
211
212 void
213 DockDialog::on_hide()
214 {
215         Gtk::Window::on_hide();
216         close();
217 }
218
219 DockBook*
220 DockDialog::prepend_dock_book()
221 {
222         if(is_deleting)return 0;
223
224         dock_book_list.push_front(new DockBook);
225         last_dock_book=dock_book_list.front();
226
227
228         last_dock_book->signal_empty().connect(
229                 sigc::bind(
230                         sigc::mem_fun(*this,&DockDialog::erase_dock_book),
231                         last_dock_book
232                 )
233         );
234
235         dock_book_sizes_.insert(dock_book_sizes_.begin(),225);
236         refresh();
237         return last_dock_book;
238 }
239
240 DockBook*
241 DockDialog::append_dock_book()
242 {
243         if(is_deleting)return 0;
244
245         dock_book_list.push_back(new DockBook);
246         last_dock_book=dock_book_list.back();
247         last_dock_book->signal_empty().connect(
248                 sigc::bind(
249                         sigc::mem_fun(*this,&DockDialog::erase_dock_book),
250                         last_dock_book
251                 )
252         );
253         last_dock_book->signal_changed().connect(
254                 sigc::mem_fun(*this,&DockDialog::refresh_title)
255         );
256         last_dock_book->signal_changed().connect(
257                 sigc::mem_fun(*this,&DockDialog::refresh_title)
258         );
259         dock_book_sizes_.push_back(225);
260
261         //last_dock_book->show();
262         refresh();
263         return last_dock_book;
264 }
265
266 void
267 DockDialog::erase_dock_book(DockBook* dock_book)
268 {
269         if(is_deleting)return;
270
271         std::list<DockBook*>::iterator iter;
272         for(iter=dock_book_list.begin();iter!=dock_book_list.end();++iter)
273                 if(*iter==dock_book)
274                 {
275                         dock_book_list.erase(iter);
276
277                         if(dock_book_list.empty())
278                         {
279                                 last_dock_book=0;
280                                 close();
281                                 return;
282                         }
283                         else
284                         {
285                                 if(last_dock_book==dock_book)
286                                         last_dock_book=dock_book_list.front();
287                         }
288
289                         refresh();
290
291                         return;
292                 }
293 }
294
295 void
296 DockDialog::refresh()
297 {
298         // synfig::info("dock_book_list.size()=%d",dock_book_list.size());
299         //remove();
300
301         if(dock_book_list.empty())
302                 return;
303
304         if(box)delete box;
305         box=(manage(is_horizontal?(Gtk::Box*)new Gtk::HBox:(Gtk::Box*)new Gtk::VBox));
306         add(*box);
307
308         box->pack_start(*widget_comp_select,false,true);
309
310         Gtk::Button* append_button(manage(new Gtk::Button));
311         Gtk::Button* prepend_button(manage(new Gtk::Button));
312
313         std::list<Gtk::TargetEntry> listTargets;
314         listTargets.push_back( Gtk::TargetEntry("DOCK") );
315
316         append_button->drag_dest_set(listTargets);
317         prepend_button->drag_dest_set(listTargets);
318
319         append_button->signal_drag_data_received().connect(
320                 sigc::mem_fun(*this,&DockDialog::drop_on_append)
321         );
322
323         prepend_button->signal_drag_data_received().connect(
324                 sigc::mem_fun(*this,&DockDialog::drop_on_prepend)
325         );
326
327         box->pack_start(*prepend_button,false,true);
328         box->pack_end(*append_button,false,true);
329
330         //prepend_button->show();
331         //append_button->show();
332         pannels_.clear();
333
334         if(dock_book_list.size()==1)
335         {
336                 box->pack_start(get_dock_book(),true,true);
337         }
338         else
339         {
340                 Gtk::Paned* parent(manage(is_horizontal?(Gtk::Paned*)new Gtk::HPaned:(Gtk::Paned*)new Gtk::VPaned));
341
342                 pannels_.push_back(parent);
343
344                 if(pannels_.size()<=dock_book_sizes_.size())
345                         pannels_.back()->set_position(dock_book_sizes_[pannels_.size()-1]);
346                 pannels_.back()->property_position().signal_changed().connect(
347                         sigc::mem_fun(*this,&DockDialog::rebuild_sizes)
348                 );
349                 //parent->show();
350                 parent->add1(*dock_book_list.front());
351                 //dock_book_list.front()->show();
352
353                 box->pack_start(*parent,true,true);
354
355                 std::list<DockBook*>::iterator iter,next;
356                 for(next=dock_book_list.begin(),next++,iter=next++;next!=dock_book_list.end();iter=next++)
357                 {
358                         Gtk::Paned* current(manage(is_horizontal?(Gtk::Paned*)new Gtk::HPaned:(Gtk::Paned*)new Gtk::VPaned));
359                         pannels_.push_back(current);
360
361                         if(pannels_.size()<=dock_book_sizes_.size())
362                                 pannels_.back()->set_position(dock_book_sizes_[pannels_.size()-1]);
363                         pannels_.back()->property_position().signal_changed().connect(
364                                 sigc::mem_fun(*this,&DockDialog::rebuild_sizes)
365                         );
366
367
368                         parent->add2(*current);
369
370                         current->add1(**iter);
371                         //(*iter)->show();
372                         //current->show();
373
374                         parent=current;
375                 }
376                 parent->add2(**iter);
377                 //(*iter)->show();
378         }
379
380         box->show_all();
381         if(!composition_selector_)
382                 widget_comp_select->hide();
383         rebuild_sizes();
384 }
385
386 void
387 DockDialog::rebuild_sizes()
388 {
389         unsigned int i=0;
390         dock_book_sizes_.clear();
391         for(i=0;i<pannels_.size();i++)
392         {
393                 dock_book_sizes_.push_back(pannels_[i]->get_position());
394         }
395 }
396
397 void
398 DockDialog::set_dock_book_sizes(const std::vector<int>& new_sizes)
399 {
400         unsigned int i=0;
401         for(i=0;i<pannels_.size() && i<new_sizes.size();i++)
402         {
403                 pannels_[i]->set_position(new_sizes[i]);
404         }
405         dock_book_sizes_=new_sizes;
406         //rebuild_sizes();
407 }
408
409 void
410 DockDialog::refresh_accel_group()
411 {
412 /*
413         if(last_accel_group_)
414         {
415                 last_accel_group_->unlock();
416                 remove_accel_group(last_accel_group_);
417                 last_accel_group_=Glib::RefPtr<Gtk::AccelGroup>();
418         }
419
420         etl::loose_handle<CanvasView> canvas_view(App::get_selected_canvas_view());
421         if(canvas_view)
422         {
423                 last_accel_group_=canvas_view->get_accel_group();
424                 last_accel_group_->lock();
425                 add_accel_group(last_accel_group_);
426         }
427 */
428         etl::loose_handle<CanvasView> canvas_view(App::get_selected_canvas_view());
429         if(canvas_view)
430         {
431                 canvas_view->mainmenu.accelerate(*this);
432         }
433 }
434
435 bool
436 DockDialog::close()
437 {
438         synfig::info("DockDialog::close(): DELETED!");
439         empty_sig.disconnect();
440         //get_dock_book().clear();
441         delete this;
442         return true;
443 }
444
445 DockBook&
446 DockDialog::get_dock_book()
447 {
448         if(!last_dock_book)
449                 return *append_dock_book();
450         return *last_dock_book;
451 }
452
453 const DockBook&
454 DockDialog::get_dock_book()const
455 {
456         return *last_dock_book;
457 }
458
459
460 synfig::String
461 DockDialog::get_contents()const
462 {
463         synfig::String ret;
464
465         std::list<DockBook*>::const_iterator iter;
466         for(iter=dock_book_list.begin();iter!=dock_book_list.end();++iter)
467         {
468                 if(!ret.empty())
469                         ret+=is_horizontal?" | ":" - ";
470                 ret+=(*iter)->get_contents();
471         }
472
473
474         return ret;
475 }
476
477 void
478 DockDialog::set_contents(const synfig::String& z)
479 {
480         int x,y;
481         get_size(x,y);
482
483         synfig::String str(z);
484         while(!str.empty())
485         {
486                 synfig::String::size_type separator=str.find_first_of('-');
487                 {
488                         synfig::String::size_type sep2=str.find_first_of('|');
489                         if(separator!=synfig::String::npos || sep2!=synfig::String::npos)
490                         {
491                                 if((separator==synfig::String::npos || sep2<separator) && sep2!=synfig::String::npos)
492                                 {
493                                         separator=sep2;
494                                         is_horizontal=true;
495                                 }
496                                 else
497                                         is_horizontal=false;
498                         }
499                 }
500
501                 synfig::String book_contents;
502                 if(separator==synfig::String::npos)
503                 {
504                         book_contents=str;
505                         str.clear();
506                 }
507                 else
508                 {
509                         book_contents=String(str.begin(),str.begin()+separator);
510                         str=String(str.begin()+separator+1,str.end());
511                 }
512
513                 try
514                 {
515                         append_dock_book()->set_contents(book_contents);
516                 }catch(...) { }
517         }
518
519         resize(x,y);
520 }
521
522 void
523 DockDialog::set_composition_selector(bool x)
524 {
525         if(x==get_composition_selector())
526                 return;
527         composition_selector_=x;
528         if(x)
529                 widget_comp_select->show();
530         else
531                 widget_comp_select->hide();
532 }
533
534 void
535 DockDialog::refresh_title()
536 {
537         if(is_deleting)return;
538         if(dock_book_list.size())
539         {
540                 synfig::String title;
541
542                 std::list<DockBook*>::const_iterator iter;
543                 for(iter=dock_book_list.begin();iter!=dock_book_list.end();++iter)
544                 {
545                         if(!title.empty())
546                                 title+=", ";
547                         title+=(*iter)->get_local_contents();
548                 }
549                 set_title(title);
550         }
551         else
552                 set_title(_("Empty Dock Dialog"));
553 }