81816778fe8ca40ef2c0fc5adc06f438e701f3aa
[synfig.git] / synfig-studio / src / gtkmm / toolbox.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file toolbox.cpp
3 **      \brief writeme
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 Chris Moore
10 **  Copyright (c) 2008 Paul Wise
11 **      Copyright (c) 2009 Nikita Kitaev
12 **
13 **      This package is free software; you can redistribute it and/or
14 **      modify it under the terms of the GNU General Public License as
15 **      published by the Free Software Foundation; either version 2 of
16 **      the License, or (at your option) any later version.
17 **
18 **      This package is distributed in the hope that it will be useful,
19 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 **      General Public License for more details.
22 **      \endlegal
23 **
24 ** === N O T E S ===========================================================
25 **
26 ** ========================================================================= */
27
28 /* === H E A D E R S ======================================================= */
29
30 #ifdef USING_PCH
31 #       include "pch.h"
32 #else
33 #ifdef HAVE_CONFIG_H
34 #       include <config.h>
35 #endif
36
37 #include <gtk/gtk.h>
38 #include <gtkmm/uimanager.h>
39
40 #include <gtkmm/ruler.h>
41 #include <gtkmm/arrow.h>
42 #include <gtkmm/image.h>
43 #include <gdkmm/pixbufloader.h>
44 #include <gtkmm/viewport.h>
45 #include <gtkmm/adjustment.h>
46 #include <gtkmm/scrolledwindow.h>
47 #include <gtkmm/table.h>
48 #include <gtkmm/statusbar.h>
49 #include <gtkmm/menubar.h>
50 #include <gtkmm/menu.h>
51 #include <gtkmm/button.h>
52 #include <gtkmm/toolbar.h>
53 #include <gtkmm/box.h>
54 #include <gtkmm/image.h>
55 #include <gtkmm/stock.h>
56 #include <gtkmm/handlebox.h>
57
58 #include <gtkmm/inputdialog.h>
59
60 #include <sigc++/signal.h>
61 #include <sigc++/hide.h>
62 #include <sigc++/slot.h>
63 #include <sigc++/retype_return.h>
64 #include <sigc++/retype.h>
65
66 #include <sstream>
67
68 #include "toolbox.h"
69 #include "instance.h"
70 #include "app.h"
71 #include "canvasview.h"
72 #include "dialog_gradient.h"
73 #include "dialog_color.h"
74 #include "dialog_tooloptions.h"
75 #include "dialog_preview.h"
76 #include "dockable.h"
77 #include "dockmanager.h"
78 #include "dockdialog.h"
79
80 #include "widget_defaults.h"
81
82 #include <synfigapp/main.h>
83
84 #include "general.h"
85
86 #endif
87
88 using namespace std;
89 using namespace etl;
90 using namespace synfig;
91 using namespace studio;
92 using namespace sigc;
93
94 /* === M A C R O S ========================================================= */
95
96 #define GRAB_HINT_DATA(y,default)       { \
97                 String x; \
98                 if(synfigapp::Main::settings().get_value(String("pref.")+y+"_hints",x)) \
99                 { \
100                         set_type_hint((Gdk::WindowTypeHint)atoi(x.c_str()));    \
101                 } else {\
102                         set_type_hint(default); \
103                 } \
104         }
105
106 /* === G L O B A L S ======================================================= */
107
108 /* === P R O C E D U R E S ================================================= */
109
110 /* === M E T H O D S ======================================================= */
111
112 #define TOGGLE_TOOLBOX_BUTTON(button,stockid,tooltip)   \
113         button = manage(new class Gtk::ToggleButton()); \
114         icon=manage(new Gtk::Image(Gtk::StockID(stockid),Gtk::IconSize(4)));    \
115         button->add(*icon);     \
116         tooltips.set_tip(*button,tooltip);      \
117         icon->show();   \
118         button->show()
119
120 #define TOOLBOX_BUTTON(button,stockid,tooltip)  \
121         button = manage(new class Gtk::Button());       \
122         icon=manage(new Gtk::Image(Gtk::StockID(stockid),Gtk::IconSize(4)));    \
123         button->add(*icon);     \
124         tooltips.set_tip(*button,tooltip);      \
125         icon->show();   \
126         button->show()
127
128 #define ADD_TOOLBOX_BUTTON(button,stockid,tooltip)      Gtk::Button *TOOLBOX_BUTTON(button,stockid,tooltip)
129
130 void
131 save_selected_instance()
132 {
133         if(!studio::App::get_selected_instance())
134         {
135                 App::dialog_error_blocking(_("Cannot save"),_("Nothing to save"));
136                 return;
137         }
138
139         studio::App::get_selected_instance()->save();
140 }
141
142 void
143 save_as_selected_instance()
144 {
145         if(!studio::App::get_selected_instance())
146         {
147                 App::dialog_error_blocking(_("Cannot save as"),_("Nothing to save"));
148                 return;
149         }
150
151         studio::App::get_selected_instance()->dialog_save_as();
152 }
153
154 void
155 save_all()
156 {
157         std::list<etl::handle<Instance> >::iterator iter;
158         for(iter=App::instance_list.begin();iter!=App::instance_list.end();iter++)
159                 (*iter)->save();
160 }
161
162 void
163 close_selected_instance()
164 {
165         etl::handle<studio::Instance> instance=studio::App::get_selected_instance();
166
167         if(!instance)
168         {
169                 App::dialog_error_blocking(_("Cannot close"),_("Nothing to close"));
170                 return;
171         }
172
173         instance->safe_close();
174
175         //assert(instance.unique());
176 }
177
178
179 static void
180 show_dialog_input()
181 {
182         App::dialog_input->present();
183 }
184
185 void _create_stock_dialog1()
186 {
187         DockDialog* dock_dialog(new DockDialog);
188         dock_dialog->set_contents("canvases history");
189         dock_dialog->set_composition_selector(true);
190         dock_dialog->present();
191 }
192 void _create_stock_dialog2()
193 {
194         DockDialog* dock_dialog(new DockDialog);
195         dock_dialog->set_contents("layers children keyframes | params");
196         dock_dialog->present();
197 }
198
199 Toolbox::Toolbox():
200         Gtk::Window(Gtk::WINDOW_TOPLEVEL),
201         dialog_settings(this,"toolbox")
202 {
203         GRAB_HINT_DATA(
204                 "toolbox",
205 //#ifdef __APPLE__
206                 Gdk::WINDOW_TYPE_HINT_NORMAL
207 //#else
208 //              Gdk::WINDOW_TYPE_HINT_UTILITY
209 //#endif
210         );
211         set_keep_above(false);
212         set_role("toolbox");
213
214
215
216         recent_files_menu= manage(new class Gtk::Menu());
217
218         Gtk::Menu       *filemenu       =manage(new class Gtk::Menu());
219
220         dock_dialogs=manage(new class Gtk::Menu());
221
222         dock_dialogs->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Vertical Dock: Canvases, History"),sigc::ptr_fun(_create_stock_dialog1)));
223         dock_dialogs->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Horizontal Dock: Layers, Children, Params"),sigc::ptr_fun(_create_stock_dialog2)));
224         dock_dialogs->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
225         dock_dialogs->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Reset Windows to Original Layout"),sigc::ptr_fun(App::reset_initial_window_configuration)));
226         dock_dialogs->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
227
228
229         filemenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::Stock::NEW,
230                 sigc::ptr_fun(&studio::App::new_instance)));
231         filemenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::Stock::OPEN,
232                 sigc::bind(sigc::ptr_fun(&studio::App::dialog_open), "")));
233
234         filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Open Recent"),*recent_files_menu));
235
236         filemenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("synfig-saveall"),
237                 sigc::ptr_fun(save_all)));
238         filemenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::Stock::CLOSE,
239                 sigc::ptr_fun(close_selected_instance)));
240         filemenu->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
241         filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Panels"),*dock_dialogs));
242
243         //filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Canvas Browser..."),
244         //      sigc::mem_fun(studio::App::show_comp_view)));
245         //filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Gradient Editor..."),
246         //      sigc::mem_fun(show_dialog_gradient)));
247         //filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Tool Options"),
248         //      sigc::mem_fun(show_dialog_tool_options)));
249         //filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Colors..."),
250         //      sigc::mem_fun(show_dialog_color)));
251         //filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Color Palette..."),
252         //      sigc::mem_fun(show_dialog_palette)));
253         filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Input Devices..."),
254                 sigc::ptr_fun(&show_dialog_input)));
255         filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Setup..."),
256                 sigc::ptr_fun(&studio::App::show_setup)));
257         filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Reset to default Setup values"),
258                 sigc::ptr_fun(&studio::App::reset_initial_preferences)));
259
260         filemenu->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
261         filemenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID(Gtk::Stock::QUIT),
262                 sigc::ptr_fun(studio::App::quit)));
263
264 #define WIKI(title,page)                                                                                        \
265         helpmenu->items().push_back(Gtk::Menu_Helpers::MenuElem(title,  \
266                 sigc::bind(sigc::ptr_fun(&studio::App::open_url),String("http://synfig.org/wiki")+page)))
267
268 #define SITE(title,page)                                                                                        \
269         helpmenu->items().push_back(Gtk::Menu_Helpers::MenuElem(title,  \
270                 sigc::bind(sigc::ptr_fun(&studio::App::open_url),String("http://synfig.org")+page)))
271
272         Gtk::Menu       *helpmenu = manage(new class Gtk::Menu());
273         helpmenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::Stock::HELP, sigc::ptr_fun(studio::App::dialog_help)));
274         helpmenu->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
275
276         /* TRANSLATORS: Help menu entry */ WIKI(_("Tutorials"),                                 /* TRANSLATORS: a wiki page */ _("/Category:Tutorials")                         );
277         /* TRANSLATORS: Help menu entry */ WIKI(_("Reference"),                                 /* TRANSLATORS: a wiki page */ _("/Category:Reference")                         );
278         /* TRANSLATORS: Help menu entry */ WIKI(_("Frequently Asked Questions"),/* TRANSLATORS: a wiki page */ _("/FAQ")                                        );
279         helpmenu->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
280         /* TRANSLATORS: Help menu entry */ SITE(_("Get Support"),                               /* TRANSLATORS: a website page */ _("/en/support")                      );
281         helpmenu->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
282         helpmenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("synfig-about"),
283                 sigc::ptr_fun(studio::App::dialog_about)));
284
285         Gtk::MenuBar *menubar1 = manage(new class Gtk::MenuBar());
286         menubar1->items().push_back(Gtk::Menu_Helpers::MenuElem(_("_File"),*filemenu));
287         menubar1->items().push_back(Gtk::Menu_Helpers::MenuElem(_("_Help"),*helpmenu));
288
289
290         menubar1->show();
291
292         Gtk::Image *icon;
293
294         ADD_TOOLBOX_BUTTON(button_new,"gtk-new",_("New..."));
295         ADD_TOOLBOX_BUTTON(button_open,"gtk-open",_("Open..."));
296         ADD_TOOLBOX_BUTTON(button_save,"gtk-save",_("Save"));
297         ADD_TOOLBOX_BUTTON(button_saveas,"gtk-save-as",_("Save As..."));
298         ADD_TOOLBOX_BUTTON(button_save_all,"synfig-saveall",_("Save All"));
299         TOOLBOX_BUTTON(button_undo,"gtk-undo",_("Undo"));
300         TOOLBOX_BUTTON(button_redo,"gtk-redo",_("Redo"));
301         ADD_TOOLBOX_BUTTON(button_setup,"gtk-properties",_("Setup"));
302         ADD_TOOLBOX_BUTTON(button_about,"synfig-about",_("About Synfig Studio"));
303         ADD_TOOLBOX_BUTTON(button_help,"gtk-help",_("Help"));
304
305         button_setup->signal_clicked().connect(sigc::ptr_fun(studio::App::show_setup));
306         button_about->signal_clicked().connect(sigc::ptr_fun(studio::App::dialog_about));
307         button_help->signal_clicked().connect(sigc::ptr_fun(studio::App::dialog_help));
308         button_new->signal_clicked().connect(sigc::ptr_fun(studio::App::new_instance));
309         button_open->signal_clicked().connect(sigc::bind(sigc::ptr_fun(studio::App::dialog_open), ""));
310         button_save->signal_clicked().connect(sigc::ptr_fun(save_selected_instance));
311         button_saveas->signal_clicked().connect(sigc::ptr_fun(save_as_selected_instance));
312         button_save_all->signal_clicked().connect(sigc::ptr_fun(save_all));
313         button_undo->signal_clicked().connect(sigc::ptr_fun(studio::App::undo));
314         button_redo->signal_clicked().connect(sigc::ptr_fun(studio::App::redo));
315
316         // Create the file button cluster
317         Gtk::Table *file_buttons=manage(new class Gtk::Table());
318
319         file_buttons->attach(*button_new,      0,1, 0,1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
320         file_buttons->attach(*button_open,     1,2, 0,1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
321         file_buttons->attach(*button_save,     2,3, 0,1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
322         file_buttons->attach(*button_saveas,   3,4, 0,1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
323         file_buttons->attach(*button_save_all, 4,5, 0,1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
324
325         file_buttons->attach(*button_undo,     0,1, 1,2, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
326         file_buttons->attach(*button_redo,     1,2, 1,2, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
327         file_buttons->attach(*button_setup,    2,3, 1,2, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
328         file_buttons->attach(*button_about,    3,4, 1,2, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
329         file_buttons->attach(*button_help,     4,5, 1,2, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
330
331         file_buttons->show();
332
333         tool_table=manage(new class Gtk::Table());
334         tool_table->show();
335         Gtk::HandleBox* handle_tools(manage(new Gtk::HandleBox()));
336         handle_tools->add(*tool_table);
337         handle_tools->show();
338         handle_tools->set_handle_position(Gtk::POS_TOP);
339         handle_tools->set_snap_edge(Gtk::POS_TOP);
340
341         Widget_Defaults* widget_defaults(manage(new Widget_Defaults()));
342         widget_defaults->show();
343         Gtk::HandleBox* handle_defaults(manage(new Gtk::HandleBox()));
344         handle_defaults->add(*widget_defaults);
345         handle_defaults->show();
346         handle_defaults->set_handle_position(Gtk::POS_TOP);
347         handle_defaults->set_snap_edge(Gtk::POS_TOP);
348
349         // Create the toplevel table
350         Gtk::Table *table1 = manage(new class Gtk::Table(1, 2, false));
351         table1->set_row_spacings(0);
352         table1->set_col_spacings(0);
353         table1->attach(*menubar1,        0,1, 0,1, Gtk::FILL|Gtk::EXPAND, Gtk::SHRINK, 0, 0);
354         table1->attach(*file_buttons,    0,1, 1,2, Gtk::FILL|Gtk::EXPAND,Gtk::EXPAND|Gtk::FILL, 0, 0);
355         //table1->attach(*manage(new Gtk::Label(_("Tools"))), 0, 1, 2, 3, Gtk::FILL|Gtk::EXPAND,Gtk::EXPAND|Gtk::FILL, 0, 0);
356         table1->attach(*handle_tools,    0,1, 3,4, Gtk::FILL|Gtk::EXPAND,Gtk::EXPAND|Gtk::FILL, 0, 0);
357         table1->attach(*handle_defaults, 0,1, 4,5, Gtk::FILL|Gtk::EXPAND,Gtk::EXPAND|Gtk::FILL, 0, 0);
358         table1->show_all();
359
360
361
362         // Set the parameters for this window
363         add(*table1);
364         set_title(_("Synfig Studio"));
365         set_modal(false);
366         property_window_position().set_value(Gtk::WIN_POS_NONE);
367         signal_delete_event().connect(sigc::ptr_fun(App::shutdown_request));
368         set_resizable(false);
369
370
371
372         App::signal_instance_selected().connect(
373                 sigc::hide(
374                         sigc::mem_fun(*this,&studio::Toolbox::update_undo_redo)
375                 )
376         );
377
378         App::signal_recent_files_changed().connect(
379                         sigc::mem_fun(*this,&studio::Toolbox::on_recent_files_changed)
380         );
381
382         button_undo->set_sensitive(false);
383         button_redo->set_sensitive(false);
384
385         std::list<Gtk::TargetEntry> listTargets;
386         listTargets.push_back( Gtk::TargetEntry("text/plain") );
387         listTargets.push_back( Gtk::TargetEntry("image") );
388 //      listTargets.push_back( Gtk::TargetEntry("image/x-sif") );
389
390         drag_dest_set(listTargets);
391         signal_drag_data_received().connect( sigc::mem_fun(*this, &studio::Toolbox::on_drop_drag_data_received) );
392
393         App::dock_manager->signal_dockable_registered().connect(sigc::mem_fun(*this,&Toolbox::dockable_registered));
394
395         changing_state_=false;
396
397
398         add_accel_group(App::ui_manager()->get_accel_group());
399
400         App::signal_present_all().connect(sigc::mem_fun0(*this,&Toolbox::present));
401 }
402
403 Toolbox::~Toolbox()
404 {
405         hide();
406         //studio::App::cb.task(_("Toolbox: I was nailed!"));
407         //studio::App::quit();
408
409         if(studio::App::toolbox==this)
410                 studio::App::toolbox=NULL;
411
412 }
413
414 void
415 Toolbox::set_active_state(const synfig::String& statename)
416 {
417         std::map<synfig::String,Gtk::ToggleButton *>::iterator iter;
418
419         changing_state_=true;
420
421         synfigapp::Main::set_state(statename);
422
423         try
424         {
425
426                 for(iter=state_button_map.begin();iter!=state_button_map.end();++iter)
427                 {
428                         if(iter->first==statename)
429                         {
430                                 if(!iter->second->get_active())
431                                         iter->second->set_active(true);
432                         }
433                         else
434                         {
435                                 if(iter->second->get_active())
436                                         iter->second->set_active(false);
437                         }
438                 }
439         }
440         catch(...)
441         {
442                 changing_state_=false;
443                 throw;
444         }
445         changing_state_=false;
446 }
447
448 void
449 Toolbox::change_state(const synfig::String& statename)
450 {
451         etl::handle<studio::CanvasView> canvas_view(studio::App::get_selected_canvas_view());
452         if(canvas_view)
453         {
454                 if(statename==canvas_view->get_smach().get_state_name())
455                 {
456                         return;
457                 }
458
459                 if(state_button_map.count(statename))
460                 {
461                         state_button_map[statename]->clicked();
462                 }
463                 else
464                 {
465                         synfig::error("Unknown state \"%s\"",statename.c_str());
466                 }
467         }
468 }
469
470 void
471 Toolbox::change_state_(const Smach::state_base *state)
472 {
473         if(changing_state_)
474                 return;
475         changing_state_=true;
476
477         try
478         {
479                 etl::handle<studio::CanvasView> canvas_view(studio::App::get_selected_canvas_view());
480                 if(canvas_view)
481                                 canvas_view->get_smach().enter(state);
482                 else
483                         refresh();
484         }
485         catch(...)
486         {
487                 changing_state_=false;
488                 throw;
489         }
490
491         changing_state_=false;
492 }
493
494 void
495 Toolbox::add_state(const Smach::state_base *state)
496 {
497         Gtk::Image *icon;
498
499         assert(state);
500
501         String name=state->get_name();
502
503         Gtk::StockItem stock_item;
504         Gtk::Stock::lookup(Gtk::StockID("synfig-"+name),stock_item);
505
506         Gtk::ToggleButton* button;
507         button=manage(new class Gtk::ToggleButton());
508
509         icon=manage(new Gtk::Image(stock_item.get_stock_id(),Gtk::IconSize(4)));
510         button->add(*icon);
511         tooltips.set_tip(*button,stock_item.get_label());
512         icon->show();
513         button->show();
514
515         int row=state_button_map.size()/5;
516         int col=state_button_map.size()%5;
517
518         tool_table->attach(*button,col,col+1,row,row+1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
519
520         state_button_map[name]=button;
521
522         button->signal_clicked().connect(
523                 sigc::bind(
524                         sigc::mem_fun(*this,&studio::Toolbox::change_state_),
525                         state
526                 )
527         );
528
529         refresh();
530 }
531
532
533 void
534 Toolbox::update_undo_redo()
535 {
536         etl::handle<Instance> instance=App::get_selected_instance();
537         if(instance)
538         {
539                 button_undo->set_sensitive(instance->get_undo_status());
540                 button_redo->set_sensitive(instance->get_redo_status());
541         }
542
543         // This should probably go elsewhere, but it should
544         // work fine here with no troubles.
545         // These next several lines just adjust the tool buttons
546         // so that they are only clickable when they should be.
547         if(instance && App::get_selected_canvas_view())
548         {
549                 std::map<synfig::String,Gtk::ToggleButton *>::iterator iter;
550
551                 for(iter=state_button_map.begin();iter!=state_button_map.end();++iter)
552                         iter->second->set_sensitive(true);
553         }
554         else
555         {
556                 std::map<synfig::String,Gtk::ToggleButton *>::iterator iter;
557
558                 for(iter=state_button_map.begin();iter!=state_button_map.end();++iter)
559                         iter->second->set_sensitive(false);
560         }
561
562         etl::handle<CanvasView> canvas_view=App::get_selected_canvas_view();
563         if(canvas_view && canvas_view->get_smach().get_state_name())
564         {
565                 set_active_state(canvas_view->get_smach().get_state_name());
566         }
567         else
568                 set_active_state("none");
569
570 }
571
572 void
573 Toolbox::on_recent_files_changed()
574 {
575         while(recent_files_menu->get_children().size())
576                 recent_files_menu->remove(**recent_files_menu->get_children().begin());
577
578         list<string>::const_iterator iter;
579         for(iter=App::get_recent_files().begin();iter!=App::get_recent_files().end();iter++)
580         {
581                 string raw = basename(*iter), quoted;
582                 size_t pos = 0, last_pos = 0;
583
584                 // replace _ in filenames by __ or it won't show up in the menu
585                 for (pos = last_pos = 0; (pos = raw.find('_', pos)) != string::npos; last_pos = pos)
586                         quoted += raw.substr(last_pos, ++pos - last_pos) + '_';
587                 quoted += raw.substr(last_pos);
588
589                 recent_files_menu->items().push_back(Gtk::Menu_Helpers::MenuElem(quoted,
590                         sigc::hide_return(sigc::bind(sigc::ptr_fun(&App::open),*iter))
591                 ));
592         }
593
594         // HACK
595         show();
596 }
597
598 void
599 Toolbox::on_drop_drag_data_received(const Glib::RefPtr<Gdk::DragContext>& context, int /*x*/, int /*y*/, const Gtk::SelectionData& selection_data_, guint /*info*/, guint time)
600 {
601         // We will make this true once we have a solid drop
602         bool success(false);
603
604         if ((selection_data_.get_length() >= 0) && (selection_data_.get_format() == 8))
605         {
606                 synfig::String selection_data((gchar *)(selection_data_.get_data()));
607
608                 // For some reason, GTK hands us a list of URLs separated
609                 // by not only Carriage-Returns, but also Line-Feeds.
610                 // Line-Feeds will mess us up. Remove all the line-feeds.
611                 while(selection_data.find_first_of('\r')!=synfig::String::npos)
612                         selection_data.erase(selection_data.begin()+selection_data.find_first_of('\r'));
613
614                 std::stringstream stream(selection_data);
615
616                 while(stream)
617                 {
618                         synfig::String filename,URI;
619                         getline(stream,filename);
620
621                         // If we don't have a filename, move on.
622                         if(filename.empty())
623                                 continue;
624
625                         // Make sure this URL is of the "file://" type.
626                         URI=String(filename.begin(),filename.begin()+sizeof("file://")-1);
627                         if(URI!="file://")
628                         {
629                                 synfig::warning("Unknown URI (%s) in \"%s\"",URI.c_str(),filename.c_str());
630                                 continue;
631                         }
632
633                         // Strip the "file://" part from the filename
634                         filename=synfig::String(filename.begin()+sizeof("file://")-1,filename.end());
635
636                         synfig::info("Attempting to open "+filename);
637                         if(App::open(filename))
638                                 success=true;
639                         else
640                                 synfig::error("Drop failed: Unable to open "+filename);
641                 }
642         }
643         else
644                 synfig::error("Drop failed: bad selection data");
645
646         // Finish the drag
647         context->drag_finish(success, false, time);
648 }
649
650 void
651 Toolbox::dockable_registered(Dockable* x)
652 {
653         dock_dialogs->items().push_back(
654                 Gtk::Menu_Helpers::MenuElem(
655                         x->get_local_name(),
656                         sigc::mem_fun(
657                                 *x,
658                                 &Dockable::present
659                         )
660                 )
661         );
662 }