1 /*! ========================================================================
4 ** $Id: toolbox.cpp,v 1.3 2005/01/13 20:23:01 darco Exp $
6 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** General Public License for more details.
18 ** === N O T E S ===========================================================
20 ** ========================================================================= */
22 /* === H E A D E R S ======================================================= */
32 #include <gtkmm/uimanager.h>
34 #include <gtkmm/ruler.h>
35 #include <gtkmm/arrow.h>
36 #include <gtkmm/image.h>
37 #include <gdkmm/pixbufloader.h>
38 #include <gtkmm/viewport.h>
39 #include <gtkmm/adjustment.h>
40 #include <gtkmm/scrolledwindow.h>
41 #include <gtkmm/table.h>
42 #include <gtkmm/statusbar.h>
43 #include <gtkmm/menubar.h>
44 #include <gtkmm/menu.h>
45 #include <gtkmm/button.h>
46 #include <gtkmm/toolbar.h>
47 #include <gtkmm/box.h>
48 #include <gtkmm/image.h>
49 #include <gtkmm/stock.h>
50 #include <gtkmm/handlebox.h>
52 #include <gtkmm/inputdialog.h>
54 #include <sigc++/signal.h>
55 #include <sigc++/hide.h>
56 #include <sigc++/slot.h>
57 #include <sigc++/retype_return.h>
58 #include <sigc++/retype.h>
65 #include "canvasview.h"
66 #include "dialog_gradient.h"
67 #include "dialog_color.h"
68 #include "dialog_tooloptions.h"
69 #include "dialog_preview.h"
71 #include "dockmanager.h"
72 #include "dockdialog.h"
74 #include "widget_defaults.h"
76 #include <synfigapp/main.h>
82 using namespace synfig;
83 using namespace studio;
86 /* === M A C R O S ========================================================= */
88 #define GRAB_HINT_DATA(y,default) { \
90 if(synfigapp::Main::settings().get_value(String("pref.")+y+"_hints",x)) \
92 set_type_hint((Gdk::WindowTypeHint)atoi(x.c_str())); \
94 set_type_hint(default); \
98 /* === G L O B A L S ======================================================= */
100 /* === P R O C E D U R E S ================================================= */
102 /* === M E T H O D S ======================================================= */
104 #define TOGGLE_TOOLBOX_BUTTON(button,stockid,tooltip) \
105 button = manage(new class Gtk::ToggleButton()); \
106 icon=manage(new Gtk::Image(Gtk::StockID(stockid),Gtk::IconSize(4))); \
107 button->add(*icon); \
108 tooltips.set_tip(*button,tooltip); \
112 #define TOOLBOX_BUTTON(button,stockid,tooltip) \
113 button = manage(new class Gtk::Button()); \
114 icon=manage(new Gtk::Image(Gtk::StockID(stockid),Gtk::IconSize(4))); \
115 button->add(*icon); \
116 tooltips.set_tip(*button,tooltip); \
120 #define ADD_TOOLBOX_BUTTON(button,stockid,tooltip) Gtk::Button *TOOLBOX_BUTTON(button,stockid,tooltip)
123 save_selected_instance()
125 if(!studio::App::get_selected_instance())
127 App::dialog_error_blocking("Cannot save","Nothing to save");
131 if(!studio::App::get_selected_instance()->save())
132 App::dialog_error_blocking("Save - Error","Unable to save file");
136 save_as_selected_instance()
138 if(!studio::App::get_selected_instance())
140 App::dialog_error_blocking("Cannot save as","Nothing to save");
144 studio::App::get_selected_instance()->dialog_save_as();
148 close_selected_instance()
150 etl::handle<studio::Instance> instance=studio::App::get_selected_instance();
154 App::dialog_error_blocking("Cannot close","Nothing to close");
158 instance->safe_close();
160 //assert(instance.unique());
167 App::dialog_input->present();
170 void _create_stock_dialog1()
172 DockDialog* dock_dialog(new DockDialog);
173 dock_dialog->set_contents("canvases history");
174 dock_dialog->set_composition_selector(true);
175 dock_dialog->present();
177 void _create_stock_dialog2()
179 DockDialog* dock_dialog(new DockDialog);
180 dock_dialog->set_contents("layers children keyframes | params");
181 dock_dialog->present();
187 App::dialog_color->present();
191 Gtk::Window(Gtk::WINDOW_TOPLEVEL),
192 dialog_settings(this,"toolbox")
197 Gdk::WINDOW_TYPE_HINT_NORMAL
199 // Gdk::WINDOW_TYPE_HINT_UTILITY
202 set_keep_above(false);
207 recent_files_menu= manage(new class Gtk::Menu());
209 Gtk::Menu *filemenu =manage(new class Gtk::Menu());
211 dock_dialogs=manage(new class Gtk::Menu());
213 dock_dialogs->items().push_back(Gtk::Menu_Helpers::MenuElem("Canvases, History",sigc::ptr_fun(_create_stock_dialog1)));
214 dock_dialogs->items().push_back(Gtk::Menu_Helpers::MenuElem("Layers, Children , Params",sigc::ptr_fun(_create_stock_dialog2)));
215 dock_dialogs->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
218 filemenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::Stock::NEW,
219 sigc::ptr_fun(&studio::App::new_instance)));
220 filemenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::Stock::OPEN,
221 sigc::ptr_fun(&studio::App::dialog_open)));
223 filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Open Recent"),*recent_files_menu));
225 filemenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("synfig-saveall"),
226 sigc::ptr_fun(&studio::App::dialog_not_implemented)));
227 filemenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::Stock::CLOSE,
228 sigc::ptr_fun(close_selected_instance)));
229 filemenu->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
230 filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Dialogs"),*dock_dialogs));
232 //filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Canvas Browser..."),
233 // sigc::mem_fun(studio::App::show_comp_view)));
234 //filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Gradient Editor..."),
235 // sigc::mem_fun(show_dialog_gradient)));
236 //filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Tool Options"),
237 // sigc::mem_fun(show_dialog_tool_options)));
238 //filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Colors..."),
239 // sigc::mem_fun(show_dialog_color)));
240 //filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Color Palette..."),
241 // sigc::mem_fun(show_dialog_palette)));
242 filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Input Devices..."),
243 sigc::ptr_fun(&show_dialog_input)));
244 filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Setup..."),
245 sigc::ptr_fun(&studio::App::show_setup)));
247 filemenu->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
248 filemenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID(Gtk::Stock::QUIT),
249 sigc::ptr_fun(studio::App::quit)));
251 Gtk::Menu *helpmenu = manage(new class Gtk::Menu());
252 helpmenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::Stock::HELP,
253 sigc::ptr_fun(studio::App::dialog_not_implemented)));
254 helpmenu->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
255 helpmenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("synfig-about"),
256 sigc::ptr_fun(studio::App::dialog_about)));
258 Gtk::MenuBar *menubar1 = manage(new class Gtk::MenuBar());
259 menubar1->items().push_back(Gtk::Menu_Helpers::MenuElem("_File",*filemenu));
260 menubar1->items().push_back(Gtk::Menu_Helpers::MenuElem("_Help",*helpmenu));
267 ADD_TOOLBOX_BUTTON(button_new,"gtk-new","New");
268 ADD_TOOLBOX_BUTTON(button_open,"gtk-open","Open");
269 ADD_TOOLBOX_BUTTON(button_save,"gtk-save","Save");
270 ADD_TOOLBOX_BUTTON(button_saveas,"gtk-save-as","SaveAs");
271 ADD_TOOLBOX_BUTTON(button_save_all,"synfig-saveall","Save All");
272 TOOLBOX_BUTTON(button_undo,"gtk-undo","Undo");
273 TOOLBOX_BUTTON(button_redo,"gtk-redo","Redo");
274 ADD_TOOLBOX_BUTTON(button_about,"synfig-about","About Synfig Studio");
275 ADD_TOOLBOX_BUTTON(button_color,"synfig-color","Color Dialog");
277 TOOLBOX_BUTTON(button_rotoscope_bline,"synfig-rotoscope_bline",_("Old Rotoscope BLine"));
278 TOOLBOX_BUTTON(button_rotoscope_polygon,"synfig-rotoscope_polygon",_("Rotoscope Polygon"));
279 TOOLBOX_BUTTON(button_eyedrop,"synfig-eyedrop",_("Eyedrop Tool"));
280 TOOLBOX_BUTTON(button_rotoscope,"synfig-rotoscope_bline",_("Rotoscope 2"));
284 button_about->signal_clicked().connect(sigc::ptr_fun(studio::App::dialog_about));
285 button_new->signal_clicked().connect(sigc::ptr_fun(studio::App::new_instance));
286 button_open->signal_clicked().connect(sigc::ptr_fun(studio::App::dialog_open));
287 button_save->signal_clicked().connect(sigc::ptr_fun(save_selected_instance));
288 button_saveas->signal_clicked().connect(sigc::ptr_fun(save_as_selected_instance));
289 button_save_all->signal_clicked().connect(sigc::ptr_fun(studio::App::dialog_not_implemented));
290 button_undo->signal_clicked().connect(sigc::ptr_fun(studio::App::undo));
291 button_redo->signal_clicked().connect(sigc::ptr_fun(studio::App::redo));
292 button_color->signal_clicked().connect(sigc::ptr_fun(show_dialog_color));
294 // Create the file button cluster
295 Gtk::Table *file_buttons=manage(new class Gtk::Table(4, 4, false));
296 file_buttons->attach(*button_new,0,1,0,1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
297 file_buttons->attach(*button_open,1,2,0,1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
298 file_buttons->attach(*button_save,2,3,0,1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
299 file_buttons->attach(*button_saveas,3,4,0,1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
300 file_buttons->attach(*button_save_all,0,1,1,2, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
301 file_buttons->attach(*button_undo,1,2,1,2, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
302 file_buttons->attach(*button_redo,2,3,1,2, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
303 file_buttons->attach(*button_about,3,4,1,2, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
304 //file_buttons->attach(*button_color,0,1,2,3, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
305 file_buttons->show();
307 tool_table=manage(new class Gtk::Table(4, 4, false));
309 Gtk::HandleBox* handle_tools(manage(new Gtk::HandleBox()));
310 handle_tools->add(*tool_table);
311 handle_tools->show();
312 handle_tools->set_handle_position(Gtk::POS_TOP);
313 handle_tools->set_snap_edge(Gtk::POS_TOP);
315 Widget_Defaults* widget_defaults(manage(new Widget_Defaults()));
316 widget_defaults->show();
317 Gtk::HandleBox* handle_defaults(manage(new Gtk::HandleBox()));
318 handle_defaults->add(*widget_defaults);
319 handle_defaults->show();
320 handle_defaults->set_handle_position(Gtk::POS_TOP);
321 handle_defaults->set_snap_edge(Gtk::POS_TOP);
323 // Create the toplevel table
324 Gtk::Table *table1 = manage(new class Gtk::Table(1, 2, false));
325 table1->set_row_spacings(0);
326 table1->set_col_spacings(0);
327 table1->attach(*menubar1, 0, 1, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::SHRINK, 0, 0);
328 table1->attach(*file_buttons, 0, 1, 1, 2, Gtk::FILL|Gtk::EXPAND,Gtk::EXPAND|Gtk::FILL, 0, 0);
329 //table1->attach(*manage(new Gtk::Label(_("Tools"))), 0, 1, 2, 3, Gtk::FILL|Gtk::EXPAND,Gtk::EXPAND|Gtk::FILL, 0, 0);
330 table1->attach(*handle_tools, 0, 1, 3, 4, Gtk::FILL|Gtk::EXPAND,Gtk::EXPAND|Gtk::FILL, 0, 0);
331 table1->attach(*handle_defaults, 0, 1, 4, 5, Gtk::FILL|Gtk::EXPAND,Gtk::EXPAND|Gtk::FILL, 0, 0);
336 // Set the parameters for this window
338 set_title("Synfig Studio");
340 property_window_position().set_value(Gtk::WIN_POS_NONE);
341 signal_delete_event().connect(sigc::ptr_fun(App::shutdown_request));
342 set_resizable(false);
346 App::signal_instance_selected().connect(
348 sigc::mem_fun(*this,&studio::Toolbox::update_undo_redo)
352 App::signal_recent_files_changed().connect(
353 sigc::mem_fun(*this,&studio::Toolbox::on_recent_files_changed)
356 button_undo->set_sensitive(false);
357 button_redo->set_sensitive(false);
358 button_rotoscope_bline->set_sensitive(false);
359 button_rotoscope->set_sensitive(false);
360 button_rotoscope_polygon->set_sensitive(false);
361 button_eyedrop->set_sensitive(false);
364 std::list<Gtk::TargetEntry> listTargets;
365 listTargets.push_back( Gtk::TargetEntry("text/plain") );
366 listTargets.push_back( Gtk::TargetEntry("image") );
367 // listTargets.push_back( Gtk::TargetEntry("image/x-sif") );
369 drag_dest_set(listTargets);
370 signal_drag_data_received().connect( sigc::mem_fun(*this, &studio::Toolbox::on_drop_drag_data_received) );
372 App::dock_manager->signal_dockable_registered().connect(sigc::mem_fun(*this,&Toolbox::dockable_registered));
374 changing_state_=false;
377 add_accel_group(App::ui_manager()->get_accel_group());
379 App::signal_present_all().connect(sigc::mem_fun0(*this,&Toolbox::present));
385 //studio::App::cb.task("Toolbox: I was nailed!");
386 //studio::App::quit();
388 if(studio::App::toolbox==this)
389 studio::App::toolbox=NULL;
394 Toolbox::set_active_state(const String& statename)
396 std::map<synfig::String,Gtk::ToggleButton *>::iterator iter;
398 changing_state_=true;
400 synfigapp::Main::set_state(statename);
405 for(iter=state_button_map.begin();iter!=state_button_map.end();++iter)
407 if(iter->first==statename)
409 if(!iter->second->get_active())
410 iter->second->set_active(true);
414 if(iter->second->get_active())
415 iter->second->set_active(false);
421 changing_state_=false;
424 changing_state_=false;
428 Toolbox::change_state(const synfig::String& statename)
430 etl::handle<studio::CanvasView> canvas_view(studio::App::get_selected_canvas_view());
433 if(statename==canvas_view->get_smach().get_state_name())
438 if(state_button_map.count(statename))
440 state_button_map[statename]->clicked();
444 synfig::error("Unknown state \"%s\"",statename.c_str());
450 Toolbox::change_state_(const Smach::state_base *state)
454 changing_state_=true;
458 etl::handle<studio::CanvasView> canvas_view(studio::App::get_selected_canvas_view());
461 if(state->get_name()==String("normal"))
463 canvas_view->get_smach().egress();
467 canvas_view->get_smach().enter(state);
475 changing_state_=false;
479 changing_state_=false;
483 Toolbox::add_state(const Smach::state_base *state)
489 String name=state->get_name();
491 Gtk::ToggleButton* button;
492 button=manage(new class Gtk::ToggleButton());
494 icon=manage(new Gtk::Image(Gtk::StockID("synfig-"+name),Gtk::IconSize(4)));
496 tooltips.set_tip(*button,name);
503 int row=state_button_map.size()/4;
504 int col=state_button_map.size()%4;
506 tool_table->attach(*button,col,col+1,row,row+1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
508 state_button_map[name]=button;
510 button->signal_clicked().connect(
512 sigc::mem_fun(*this,&studio::Toolbox::change_state_),
523 Toolbox::update_undo_redo()
525 etl::handle<Instance> instance=App::get_selected_instance();
528 button_undo->set_sensitive(instance->get_undo_status());
529 button_redo->set_sensitive(instance->get_redo_status());
532 // This should probably go elsewhere, but it should
533 // work fine here with no troubles.
534 // These next several lines just adjust the rotoscope buttons
535 // so that they are only clickable when they should be.
536 if(instance && App::get_selected_canvas_view())
538 std::map<synfig::String,Gtk::ToggleButton *>::iterator iter;
540 for(iter=state_button_map.begin();iter!=state_button_map.end();++iter)
541 iter->second->set_sensitive(true);
545 std::map<synfig::String,Gtk::ToggleButton *>::iterator iter;
547 for(iter=state_button_map.begin();iter!=state_button_map.end();++iter)
548 iter->second->set_sensitive(false);
551 etl::handle<CanvasView> canvas_view=App::get_selected_canvas_view();
552 if(canvas_view && canvas_view->get_smach().get_state_name())
554 set_active_state(canvas_view->get_smach().get_state_name());
557 set_active_state("none");
562 Toolbox::on_recent_files_changed()
564 while(recent_files_menu->get_children().size())
565 recent_files_menu->remove(**recent_files_menu->get_children().begin());
567 list<string>::const_iterator iter;
568 // Check to see if the file is already on the list.
569 // If it is, then remove it from the list
570 for(iter=App::get_recent_files().begin();iter!=App::get_recent_files().end();iter++)
571 recent_files_menu->items().push_back(Gtk::Menu_Helpers::MenuElem(basename(*iter),
572 sigc::hide_return(sigc::bind(sigc::ptr_fun(&App::open),*iter))
580 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)
582 // We will make this true once we have a solid drop
585 if ((selection_data_.get_length() >= 0) && (selection_data_.get_format() == 8))
587 synfig::String selection_data((gchar *)(selection_data_.get_data()));
589 // For some reason, GTK hands us a list of URL's seperated
590 // by not only Carrage-Returns, but also Line-Feeds.
591 // Line-Feeds will mess us up. Remove all the line-feeds.
592 while(selection_data.find_first_of('\r')!=synfig::String::npos)
593 selection_data.erase(selection_data.begin()+selection_data.find_first_of('\r'));
595 std::stringstream stream(selection_data);
599 synfig::String filename,URI;
600 getline(stream,filename);
602 // If we don't have a filename, move on.
606 // Make sure this URL is of the "file://" type.
607 URI=String(filename.begin(),filename.begin()+sizeof("file://")-1);
610 synfig::warning("Unknown URI (%s) in \"%s\"",URI.c_str(),filename.c_str());
614 // Strip the "file://" part from the filename
615 filename=synfig::String(filename.begin()+sizeof("file://")-1,filename.end());
617 synfig::info("Attempting to open "+filename);
618 if(App::open(filename))
621 synfig::error("Drop failed: Unable to open "+filename);
625 synfig::error("Drop failed: bad selection data");
628 context->drag_finish(success, false, time);
632 Toolbox::dockable_registered(Dockable* x)
634 dock_dialogs->items().push_back(
635 Gtk::Menu_Helpers::MenuElem(