1 /* === S Y N F I G ========================================================= */
2 /*! \file dock_history.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include "docks/dock_history.h"
38 #include <gtkmm/scrolledwindow.h>
41 #include <sigc++/signal.h>
42 #include <sigc++/hide.h>
43 #include <sigc++/slot.h>
44 #include <synfigapp/action.h>
45 #include "trees/historytreestore.h"
51 /* === U S I N G =========================================================== */
55 using namespace synfig;
56 using namespace studio;
58 /* === M A C R O S ========================================================= */
59 #define COLUMNID_JUMP (787584)
62 /* === G L O B A L S ======================================================= */
64 /* === P R O C E D U R E S ================================================= */
66 /* === M E T H O D S ======================================================= */
68 Dock_History::Dock_History():
69 Dock_CanvasSpecific("history",_("History"),Gtk::StockID("gtk-undo")),
70 action_group(Gtk::ActionGroup::create("action_group_dock_history"))
72 App::signal_instance_deleted().connect(sigc::mem_fun(*this,&studio::Dock_History::delete_instance));
73 App::signal_instance_selected().connect(sigc::mem_fun(*this,&studio::Dock_History::set_selected_instance_signal));
75 action_group->add(Gtk::Action::create(
77 Gtk::StockID("synfig-clear_undo"),
78 _("Clear Undo Stack"),
79 _("Clear the undo stack")
83 &Dock_History::clear_undo
86 action_group->add(Gtk::Action::create(
88 Gtk::StockID("synfig-clear_redo"),
89 _("Clear Redo Stack"),
90 _("Clear the redo stack")
94 &Dock_History::clear_redo
97 action_group->add(Gtk::Action::create(
98 "clear-undo-and-redo",
100 _("Clear Undo and Redo Stacks"),
101 _("Clear the undo and redo stacks")
105 &Dock_History::clear_undo_and_redo
108 action_group->add(Gtk::Action::create(
110 Gtk::StockID("gtk-undo"),
112 _("Undo the previous action")
114 sigc::ptr_fun(studio::App::undo)
116 action_group->add(Gtk::Action::create(
118 Gtk::StockID("gtk-redo"),
120 _("Redo the previously undone action")
122 sigc::ptr_fun(studio::App::redo)
125 action_group->add( Gtk::Action::create("toolbar-history", _("History")) );
126 App::ui_manager()->insert_action_group(action_group);
128 Glib::ustring ui_info =
130 " <toolbar action='toolbar-history'>"
131 " <toolitem action='undo' />"
132 " <toolitem action='redo' />"
133 " <toolitem action='clear-undo' />"
134 " <toolitem action='clear-redo' />"
135 " <toolitem action='clear-undo-and-redo' />"
140 App::ui_manager()->add_ui_from_string(ui_info);
142 action_group->set_sensitive(false);
144 set_toolbar(*dynamic_cast<Gtk::Toolbar*>(App::ui_manager()->get_widget("/toolbar-history")));
145 add(*create_action_tree());
148 Dock_History::~Dock_History()
153 Dock_History::init_instance_vfunc(etl::loose_handle<Instance> instance)
155 instance->signal_undo_redo_status_changed().connect(
156 sigc::mem_fun(*this,&Dock_History::update_undo_redo)
161 Dock_History::create_action_tree()
163 studio::HistoryTreeStore::Model history_tree_model;
164 action_tree=manage(new class Gtk::TreeView());
166 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column("") );
168 Gtk::CellRendererToggle* toggle_cr = Gtk::manage( new Gtk::CellRendererToggle() );
169 toggle_cr->signal_toggled().connect(sigc::mem_fun(*this, &studio::Dock_History::on_action_toggle) );
171 column->pack_start(*toggle_cr); //false = don't expand.
172 column->add_attribute(toggle_cr->property_active(),history_tree_model.is_active);
173 column->set_resizable();
174 column->set_clickable();
176 action_tree->append_column(*column);
179 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Canvas")) );
180 Gtk::CellRendererText *text_cr=Gtk::manage(new Gtk::CellRendererText());
181 text_cr->property_foreground()=Glib::ustring("#7f7f7f");
183 column->pack_start(*text_cr);
184 column->add_attribute(text_cr->property_text(),history_tree_model.canvas_id);
185 column->add_attribute(text_cr->property_foreground_set(),history_tree_model.is_redo);
187 action_tree->append_column(*column);
190 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Jump")) );
192 Gtk::CellRendererText* cell_renderer_jump=Gtk::manage(new Gtk::CellRendererText());
193 column->pack_start(*cell_renderer_jump,true);
195 cell_renderer_jump->property_text()=_("(JMP)");
196 cell_renderer_jump->property_foreground()="#003a7f";
198 column->set_resizable();
199 column->set_clickable();
201 column->set_sort_column(COLUMNID_JUMP);
203 action_tree->append_column(*column);
207 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Action")) );
209 Gtk::CellRendererText *text_cr=Gtk::manage(new Gtk::CellRendererText());
210 text_cr->property_foreground()=Glib::ustring("#7f7f7f");
214 //column->pack_start(history_tree_model.icon, false); //false = don't expand.
215 column->pack_start(*text_cr);
216 column->add_attribute(text_cr->property_text(),history_tree_model.name);
217 column->add_attribute(text_cr->property_foreground_set(),history_tree_model.is_redo);
219 action_tree->append_column(*column);
222 action_tree->set_enable_search(true);
223 action_tree->set_search_column(history_tree_model.name);
224 action_tree->set_search_equal_func(sigc::ptr_fun(&studio::HistoryTreeStore::search_func));
226 action_tree->set_rules_hint();
227 // action_tree->signal_row_activated().connect(sigc::mem_fun(*this,&Dock_History::on_row_activate));
228 action_tree->signal_event().connect(sigc::mem_fun(*this,&Dock_History::on_action_event));
229 // action_tree->add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
230 // action_tree->add_events(Gdk::BUTTON1_MOTION_MASK);
233 Gtk::ScrolledWindow *scrolledwindow = manage(new class Gtk::ScrolledWindow());
234 scrolledwindow->set_flags(Gtk::CAN_FOCUS);
235 scrolledwindow->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
236 scrolledwindow->add(*action_tree);
237 scrolledwindow->set_shadow_type(Gtk::SHADOW_ETCHED_IN);
238 scrolledwindow->show_all();
241 Gtk::Widget& widget(*action_tree);
242 Pango::FontDescription font(widget.get_modifier_style()->get_font());
243 font.set_size(Pango::SCALE*5);
244 widget.get_modifier_style()->set_font(font);
245 widget.modify_font(font);
248 return scrolledwindow;
252 Dock_History::clear_undo()
254 if(selected_instance && App::dialog_yes_no(_("Clear History"),
255 _("You will not be able to undo any changes that you have made!\nAre you sure you want to clear the undo stack?")))
256 selected_instance->clear_undo_stack();
260 Dock_History::clear_redo()
262 if(selected_instance && App::dialog_yes_no(_("Clear History"),
263 _("You will not be able to redo any changes that you have made!\nAre you sure you want to clear the redo stack?")))
264 selected_instance->clear_redo_stack();
268 Dock_History::clear_undo_and_redo()
270 if(selected_instance && App::dialog_yes_no(_("Clear History"),
271 _("You will not be able to undo or redo any changes that you have made!\nAre you sure you want to clear the undo and redo stacks?")))
273 selected_instance->clear_undo_stack();
274 selected_instance->clear_redo_stack();
279 Dock_History::update_undo_redo()
281 etl::handle<Instance> instance=App::get_selected_instance();
284 action_group->get_action("undo")->set_sensitive(instance->get_undo_status());
285 action_group->get_action("clear-undo")->set_sensitive(instance->get_undo_status());
286 action_group->get_action("redo")->set_sensitive(instance->get_redo_status());
287 action_group->get_action("clear-redo")->set_sensitive(instance->get_redo_status());
288 action_group->get_action("clear-undo-and-redo")->set_sensitive(instance->get_undo_status() || instance->get_redo_status());
293 Dock_History::on_undo_tree_changed()
295 Gtk::TreeModel::Children children(selected_instance->history_tree_store()->children());
297 if (!children.size())
300 studio::HistoryTreeStore::Model model;
302 Gtk::TreeModel::Children::iterator iter, prev = children.end();
303 for (iter = children.begin(); iter != children.end(); prev = iter++)
304 if ((*iter)[model.is_redo])
306 if (prev == children.end())
307 action_tree->get_selection()->unselect_all();
310 action_tree->scroll_to_row(Gtk::TreePath(prev), 0.5);
311 action_tree->get_selection()->select(prev);
316 action_tree->scroll_to_row(Gtk::TreePath(prev), 0.5);
317 action_tree->get_selection()->select(prev);
321 Dock_History::set_selected_instance_(etl::handle<studio::Instance> instance)
323 if(studio::App::shutdown_in_progress)
326 if (on_undo_tree_changed_connection)
327 on_undo_tree_changed_connection.disconnect();
329 selected_instance=instance;
332 on_undo_tree_changed_connection = selected_instance->history_tree_store()->signal_undo_tree_changed().connect(
333 sigc::mem_fun(*this,&Dock_History::on_undo_tree_changed));
335 action_tree->set_model(instance->history_tree_store());
338 action_group->set_sensitive(true);
342 action_tree->set_model(Glib::RefPtr< Gtk::TreeModel >());
344 action_group->set_sensitive(false);
349 Dock_History::set_selected_instance_signal(etl::handle<studio::Instance> x)
351 set_selected_instance(x);
355 Dock_History::set_selected_instance(etl::loose_handle<studio::Instance> x)
357 if(studio::App::shutdown_in_progress)
360 // if it's already selected, don't select it again
361 if (x==selected_instance)
364 std::list<etl::handle<studio::Instance> >::iterator iter;
366 set_selected_instance_(x);
370 Dock_History::delete_instance(etl::handle<studio::Instance> instance)
372 if(studio::App::shutdown_in_progress)
375 if(selected_instance==instance)
377 set_selected_instance(0);
382 Dock_History::on_action_event(GdkEvent *event)
384 studio::HistoryTreeStore::Model model;
387 case GDK_BUTTON_PRESS:
388 case GDK_2BUTTON_PRESS:
390 Gtk::TreeModel::Path path;
391 Gtk::TreeViewColumn *column;
393 if(!action_tree->get_path_at_pos(
394 int(event->button.x),int(event->button.y), // x, y
395 path, // TreeModel::Path&
396 column, //TreeViewColumn*&
397 cell_x,cell_y //int&cell_x,int&cell_y
400 const Gtk::TreeRow row = *(action_tree->get_model()->get_iter(path));
402 //signal_user_click()(event->button.button,row,(ColumnID)column->get_sort_column_id());
403 if((ColumnID)column->get_sort_column_id()==COLUMNID_JUMP)
405 etl::handle<synfigapp::Action::Undoable> action(row[model.action]);
407 if((bool)row[model.is_undo])
409 while(get_selected_instance()->undo_action_stack().size() && get_selected_instance()->undo_action_stack().front()!=action)
410 if(get_selected_instance()->undo()==false)
413 else if((bool)row[model.is_redo])
415 while(get_selected_instance()->redo_action_stack().size() && get_selected_instance()->undo_action_stack().front()!=action)
416 if(get_selected_instance()->redo()==false)
427 case GDK_BUTTON_RELEASE:
436 Dock_History::on_action_toggle(const Glib::ustring& path_string)
438 studio::HistoryTreeStore::Model history_tree_model;
440 Gtk::TreePath path(path_string);
442 const Gtk::TreeRow row = *(selected_instance->history_tree_store()->get_iter(path));
444 handle<synfigapp::Action::Undoable> action=row[history_tree_model.action];
446 selected_instance->synfigapp::Instance::set_action_status(action,!action->is_active());