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