1 /* === S Y N F I G ========================================================= */
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 ** === N O T E S ===========================================================
23 ** ========================================================================= */
25 /* === H E A D E R S ======================================================= */
35 #include <gtkmm/uimanager.h>
37 #include <gtkmm/ruler.h>
38 #include <gtkmm/arrow.h>
39 #include <gtkmm/image.h>
40 #include <gdkmm/pixbufloader.h>
41 #include <gtkmm/viewport.h>
42 #include <gtkmm/adjustment.h>
43 #include <gtkmm/scrolledwindow.h>
44 #include <gtkmm/table.h>
45 #include <gtkmm/statusbar.h>
46 #include <gtkmm/menubar.h>
47 #include <gtkmm/menu.h>
48 #include <gtkmm/button.h>
49 #include <gtkmm/toolbar.h>
50 #include <gtkmm/box.h>
51 #include <gtkmm/image.h>
52 #include <gtkmm/stock.h>
53 #include <gtkmm/handlebox.h>
55 #include <gtkmm/inputdialog.h>
57 #include <sigc++/signal.h>
58 #include <sigc++/hide.h>
59 #include <sigc++/slot.h>
60 #include <sigc++/retype_return.h>
61 #include <sigc++/retype.h>
68 #include "canvasview.h"
69 #include "dialog_gradient.h"
70 #include "dialog_color.h"
71 #include "dialog_tooloptions.h"
72 #include "dialog_preview.h"
74 #include "dockmanager.h"
75 #include "dockdialog.h"
77 #include "widget_defaults.h"
79 #include <synfigapp/main.h>
85 using namespace synfig;
86 using namespace studio;
89 /* === M A C R O S ========================================================= */
91 #define GRAB_HINT_DATA(y,default) { \
93 if(synfigapp::Main::settings().get_value(String("pref.")+y+"_hints",x)) \
95 set_type_hint((Gdk::WindowTypeHint)atoi(x.c_str())); \
97 set_type_hint(default); \
101 /* === G L O B A L S ======================================================= */
103 /* === P R O C E D U R E S ================================================= */
105 /* === M E T H O D S ======================================================= */
107 #define TOGGLE_TOOLBOX_BUTTON(button,stockid,tooltip) \
108 button = manage(new class Gtk::ToggleButton()); \
109 icon=manage(new Gtk::Image(Gtk::StockID(stockid),Gtk::IconSize(4))); \
110 button->add(*icon); \
111 tooltips.set_tip(*button,tooltip); \
115 #define TOOLBOX_BUTTON(button,stockid,tooltip) \
116 button = manage(new class Gtk::Button()); \
117 icon=manage(new Gtk::Image(Gtk::StockID(stockid),Gtk::IconSize(4))); \
118 button->add(*icon); \
119 tooltips.set_tip(*button,tooltip); \
123 #define ADD_TOOLBOX_BUTTON(button,stockid,tooltip) Gtk::Button *TOOLBOX_BUTTON(button,stockid,tooltip)
126 save_selected_instance()
128 if(!studio::App::get_selected_instance())
130 App::dialog_error_blocking("Cannot save","Nothing to save");
134 if(!studio::App::get_selected_instance()->save())
135 App::dialog_error_blocking("Save - Error","Unable to save file");
139 save_as_selected_instance()
141 if(!studio::App::get_selected_instance())
143 App::dialog_error_blocking("Cannot save as","Nothing to save");
147 studio::App::get_selected_instance()->dialog_save_as();
151 close_selected_instance()
153 etl::handle<studio::Instance> instance=studio::App::get_selected_instance();
157 App::dialog_error_blocking("Cannot close","Nothing to close");
161 instance->safe_close();
163 //assert(instance.unique());
170 App::dialog_input->present();
173 void _create_stock_dialog1()
175 DockDialog* dock_dialog(new DockDialog);
176 dock_dialog->set_contents("canvases history");
177 dock_dialog->set_composition_selector(true);
178 dock_dialog->present();
180 void _create_stock_dialog2()
182 DockDialog* dock_dialog(new DockDialog);
183 dock_dialog->set_contents("layers children keyframes | params");
184 dock_dialog->present();
190 App::dialog_color->present();
194 Gtk::Window(Gtk::WINDOW_TOPLEVEL),
195 dialog_settings(this,"toolbox")
200 Gdk::WINDOW_TYPE_HINT_NORMAL
202 // Gdk::WINDOW_TYPE_HINT_UTILITY
205 set_keep_above(false);
210 recent_files_menu= manage(new class Gtk::Menu());
212 Gtk::Menu *filemenu =manage(new class Gtk::Menu());
214 dock_dialogs=manage(new class Gtk::Menu());
216 dock_dialogs->items().push_back(Gtk::Menu_Helpers::MenuElem("Canvases, History",sigc::ptr_fun(_create_stock_dialog1)));
217 dock_dialogs->items().push_back(Gtk::Menu_Helpers::MenuElem("Layers, Children , Params",sigc::ptr_fun(_create_stock_dialog2)));
218 dock_dialogs->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
221 filemenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::Stock::NEW,
222 sigc::ptr_fun(&studio::App::new_instance)));
223 filemenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::Stock::OPEN,
224 sigc::ptr_fun(&studio::App::dialog_open)));
226 filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Open Recent"),*recent_files_menu));
228 filemenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("synfig-saveall"),
229 sigc::ptr_fun(&studio::App::dialog_not_implemented)));
230 filemenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::Stock::CLOSE,
231 sigc::ptr_fun(close_selected_instance)));
232 filemenu->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
233 filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Dialogs"),*dock_dialogs));
235 //filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Canvas Browser..."),
236 // sigc::mem_fun(studio::App::show_comp_view)));
237 //filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Gradient Editor..."),
238 // sigc::mem_fun(show_dialog_gradient)));
239 //filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Tool Options"),
240 // sigc::mem_fun(show_dialog_tool_options)));
241 //filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Colors..."),
242 // sigc::mem_fun(show_dialog_color)));
243 //filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Color Palette..."),
244 // sigc::mem_fun(show_dialog_palette)));
245 filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Input Devices..."),
246 sigc::ptr_fun(&show_dialog_input)));
247 filemenu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Setup..."),
248 sigc::ptr_fun(&studio::App::show_setup)));
250 filemenu->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
251 filemenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID(Gtk::Stock::QUIT),
252 sigc::ptr_fun(studio::App::quit)));
254 Gtk::Menu *helpmenu = manage(new class Gtk::Menu());
255 helpmenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::Stock::HELP,
256 sigc::ptr_fun(studio::App::dialog_not_implemented)));
257 helpmenu->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
258 helpmenu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("synfig-about"),
259 sigc::ptr_fun(studio::App::dialog_about)));
261 Gtk::MenuBar *menubar1 = manage(new class Gtk::MenuBar());
262 menubar1->items().push_back(Gtk::Menu_Helpers::MenuElem("_File",*filemenu));
263 menubar1->items().push_back(Gtk::Menu_Helpers::MenuElem("_Help",*helpmenu));
270 ADD_TOOLBOX_BUTTON(button_new,"gtk-new","New");
271 ADD_TOOLBOX_BUTTON(button_open,"gtk-open","Open");
272 ADD_TOOLBOX_BUTTON(button_save,"gtk-save","Save");
273 ADD_TOOLBOX_BUTTON(button_saveas,"gtk-save-as","SaveAs");
274 ADD_TOOLBOX_BUTTON(button_save_all,"synfig-saveall","Save All");
275 TOOLBOX_BUTTON(button_undo,"gtk-undo","Undo");
276 TOOLBOX_BUTTON(button_redo,"gtk-redo","Redo");
277 ADD_TOOLBOX_BUTTON(button_about,"synfig-about","About Synfig Studio");
278 ADD_TOOLBOX_BUTTON(button_color,"synfig-color","Color Dialog");
280 TOOLBOX_BUTTON(button_rotoscope_bline,"synfig-rotoscope_bline",_("Old Rotoscope BLine"));
281 TOOLBOX_BUTTON(button_rotoscope_polygon,"synfig-rotoscope_polygon",_("Rotoscope Polygon"));
282 TOOLBOX_BUTTON(button_eyedrop,"synfig-eyedrop",_("Eyedrop Tool"));
283 TOOLBOX_BUTTON(button_rotoscope,"synfig-rotoscope_bline",_("Rotoscope 2"));
287 button_about->signal_clicked().connect(sigc::ptr_fun(studio::App::dialog_about));
288 button_new->signal_clicked().connect(sigc::ptr_fun(studio::App::new_instance));
289 button_open->signal_clicked().connect(sigc::ptr_fun(studio::App::dialog_open));
290 button_save->signal_clicked().connect(sigc::ptr_fun(save_selected_instance));
291 button_saveas->signal_clicked().connect(sigc::ptr_fun(save_as_selected_instance));
292 button_save_all->signal_clicked().connect(sigc::ptr_fun(studio::App::dialog_not_implemented));
293 button_undo->signal_clicked().connect(sigc::ptr_fun(studio::App::undo));
294 button_redo->signal_clicked().connect(sigc::ptr_fun(studio::App::redo));
295 button_color->signal_clicked().connect(sigc::ptr_fun(show_dialog_color));
297 // Create the file button cluster
298 Gtk::Table *file_buttons=manage(new class Gtk::Table(4, 4, false));
299 file_buttons->attach(*button_new,0,1,0,1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
300 file_buttons->attach(*button_open,1,2,0,1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
301 file_buttons->attach(*button_save,2,3,0,1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
302 file_buttons->attach(*button_saveas,3,4,0,1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
303 file_buttons->attach(*button_save_all,0,1,1,2, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
304 file_buttons->attach(*button_undo,1,2,1,2, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
305 file_buttons->attach(*button_redo,2,3,1,2, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
306 file_buttons->attach(*button_about,3,4,1,2, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
307 //file_buttons->attach(*button_color,0,1,2,3, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
308 file_buttons->show();
310 tool_table=manage(new class Gtk::Table(4, 4, false));
312 Gtk::HandleBox* handle_tools(manage(new Gtk::HandleBox()));
313 handle_tools->add(*tool_table);
314 handle_tools->show();
315 handle_tools->set_handle_position(Gtk::POS_TOP);
316 handle_tools->set_snap_edge(Gtk::POS_TOP);
318 Widget_Defaults* widget_defaults(manage(new Widget_Defaults()));
319 widget_defaults->show();
320 Gtk::HandleBox* handle_defaults(manage(new Gtk::HandleBox()));
321 handle_defaults->add(*widget_defaults);
322 handle_defaults->show();
323 handle_defaults->set_handle_position(Gtk::POS_TOP);
324 handle_defaults->set_snap_edge(Gtk::POS_TOP);
326 // Create the toplevel table
327 Gtk::Table *table1 = manage(new class Gtk::Table(1, 2, false));
328 table1->set_row_spacings(0);
329 table1->set_col_spacings(0);
330 table1->attach(*menubar1, 0, 1, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::SHRINK, 0, 0);
331 table1->attach(*file_buttons, 0, 1, 1, 2, Gtk::FILL|Gtk::EXPAND,Gtk::EXPAND|Gtk::FILL, 0, 0);
332 //table1->attach(*manage(new Gtk::Label(_("Tools"))), 0, 1, 2, 3, Gtk::FILL|Gtk::EXPAND,Gtk::EXPAND|Gtk::FILL, 0, 0);
333 table1->attach(*handle_tools, 0, 1, 3, 4, Gtk::FILL|Gtk::EXPAND,Gtk::EXPAND|Gtk::FILL, 0, 0);
334 table1->attach(*handle_defaults, 0, 1, 4, 5, Gtk::FILL|Gtk::EXPAND,Gtk::EXPAND|Gtk::FILL, 0, 0);
339 // Set the parameters for this window
341 set_title("Synfig Studio");
343 property_window_position().set_value(Gtk::WIN_POS_NONE);
344 signal_delete_event().connect(sigc::ptr_fun(App::shutdown_request));
345 set_resizable(false);
349 App::signal_instance_selected().connect(
351 sigc::mem_fun(*this,&studio::Toolbox::update_undo_redo)
355 App::signal_recent_files_changed().connect(
356 sigc::mem_fun(*this,&studio::Toolbox::on_recent_files_changed)
359 button_undo->set_sensitive(false);
360 button_redo->set_sensitive(false);
361 button_rotoscope_bline->set_sensitive(false);
362 button_rotoscope->set_sensitive(false);
363 button_rotoscope_polygon->set_sensitive(false);
364 button_eyedrop->set_sensitive(false);
367 std::list<Gtk::TargetEntry> listTargets;
368 listTargets.push_back( Gtk::TargetEntry("text/plain") );
369 listTargets.push_back( Gtk::TargetEntry("image") );
370 // listTargets.push_back( Gtk::TargetEntry("image/x-sif") );
372 drag_dest_set(listTargets);
373 signal_drag_data_received().connect( sigc::mem_fun(*this, &studio::Toolbox::on_drop_drag_data_received) );
375 App::dock_manager->signal_dockable_registered().connect(sigc::mem_fun(*this,&Toolbox::dockable_registered));
377 changing_state_=false;
380 add_accel_group(App::ui_manager()->get_accel_group());
382 App::signal_present_all().connect(sigc::mem_fun0(*this,&Toolbox::present));
388 //studio::App::cb.task("Toolbox: I was nailed!");
389 //studio::App::quit();
391 if(studio::App::toolbox==this)
392 studio::App::toolbox=NULL;
397 Toolbox::set_active_state(const String& statename)
399 std::map<synfig::String,Gtk::ToggleButton *>::iterator iter;
401 changing_state_=true;
403 synfigapp::Main::set_state(statename);
408 for(iter=state_button_map.begin();iter!=state_button_map.end();++iter)
410 if(iter->first==statename)
412 if(!iter->second->get_active())
413 iter->second->set_active(true);
417 if(iter->second->get_active())
418 iter->second->set_active(false);
424 changing_state_=false;
427 changing_state_=false;
431 Toolbox::change_state(const synfig::String& statename)
433 etl::handle<studio::CanvasView> canvas_view(studio::App::get_selected_canvas_view());
436 if(statename==canvas_view->get_smach().get_state_name())
441 if(state_button_map.count(statename))
443 state_button_map[statename]->clicked();
447 synfig::error("Unknown state \"%s\"",statename.c_str());
453 Toolbox::change_state_(const Smach::state_base *state)
457 changing_state_=true;
461 etl::handle<studio::CanvasView> canvas_view(studio::App::get_selected_canvas_view());
464 if(state->get_name()==String("normal"))
466 canvas_view->get_smach().egress();
470 canvas_view->get_smach().enter(state);
478 changing_state_=false;
482 changing_state_=false;
486 Toolbox::add_state(const Smach::state_base *state)
492 String name=state->get_name();
494 Gtk::ToggleButton* button;
495 button=manage(new class Gtk::ToggleButton());
497 icon=manage(new Gtk::Image(Gtk::StockID("synfig-"+name),Gtk::IconSize(4)));
499 tooltips.set_tip(*button,name);
506 int row=state_button_map.size()/4;
507 int col=state_button_map.size()%4;
509 tool_table->attach(*button,col,col+1,row,row+1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
511 state_button_map[name]=button;
513 button->signal_clicked().connect(
515 sigc::mem_fun(*this,&studio::Toolbox::change_state_),
526 Toolbox::update_undo_redo()
528 etl::handle<Instance> instance=App::get_selected_instance();
531 button_undo->set_sensitive(instance->get_undo_status());
532 button_redo->set_sensitive(instance->get_redo_status());
535 // This should probably go elsewhere, but it should
536 // work fine here with no troubles.
537 // These next several lines just adjust the rotoscope buttons
538 // so that they are only clickable when they should be.
539 if(instance && App::get_selected_canvas_view())
541 std::map<synfig::String,Gtk::ToggleButton *>::iterator iter;
543 for(iter=state_button_map.begin();iter!=state_button_map.end();++iter)
544 iter->second->set_sensitive(true);
548 std::map<synfig::String,Gtk::ToggleButton *>::iterator iter;
550 for(iter=state_button_map.begin();iter!=state_button_map.end();++iter)
551 iter->second->set_sensitive(false);
554 etl::handle<CanvasView> canvas_view=App::get_selected_canvas_view();
555 if(canvas_view && canvas_view->get_smach().get_state_name())
557 set_active_state(canvas_view->get_smach().get_state_name());
560 set_active_state("none");
565 Toolbox::on_recent_files_changed()
567 while(recent_files_menu->get_children().size())
568 recent_files_menu->remove(**recent_files_menu->get_children().begin());
570 list<string>::const_iterator iter;
571 // Check to see if the file is already on the list.
572 // If it is, then remove it from the list
573 for(iter=App::get_recent_files().begin();iter!=App::get_recent_files().end();iter++)
574 recent_files_menu->items().push_back(Gtk::Menu_Helpers::MenuElem(basename(*iter),
575 sigc::hide_return(sigc::bind(sigc::ptr_fun(&App::open),*iter))
583 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)
585 // We will make this true once we have a solid drop
588 if ((selection_data_.get_length() >= 0) && (selection_data_.get_format() == 8))
590 synfig::String selection_data((gchar *)(selection_data_.get_data()));
592 // For some reason, GTK hands us a list of URL's seperated
593 // by not only Carrage-Returns, but also Line-Feeds.
594 // Line-Feeds will mess us up. Remove all the line-feeds.
595 while(selection_data.find_first_of('\r')!=synfig::String::npos)
596 selection_data.erase(selection_data.begin()+selection_data.find_first_of('\r'));
598 std::stringstream stream(selection_data);
602 synfig::String filename,URI;
603 getline(stream,filename);
605 // If we don't have a filename, move on.
609 // Make sure this URL is of the "file://" type.
610 URI=String(filename.begin(),filename.begin()+sizeof("file://")-1);
613 synfig::warning("Unknown URI (%s) in \"%s\"",URI.c_str(),filename.c_str());
617 // Strip the "file://" part from the filename
618 filename=synfig::String(filename.begin()+sizeof("file://")-1,filename.end());
620 synfig::info("Attempting to open "+filename);
621 if(App::open(filename))
624 synfig::error("Drop failed: Unable to open "+filename);
628 synfig::error("Drop failed: bad selection data");
631 context->drag_finish(success, false, time);
635 Toolbox::dockable_registered(Dockable* x)
637 dock_dialogs->items().push_back(
638 Gtk::Menu_Helpers::MenuElem(