Add 2 new valuenode types: "Angle String" and "Int String".
[synfig.git] / synfig-studio / trunk / src / gtkmm / dock_history.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file dock_history.cpp
3 **      \brief Template File
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 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "dock_history.h"
34 #include "app.h"
35
36
37
38 #include <gtkmm/scrolledwindow.h>
39 #include <cassert>
40 #include "instance.h"
41 #include <sigc++/signal.h>
42 #include <sigc++/hide.h>
43 #include <sigc++/slot.h>
44 #include <synfigapp/action.h>
45 #include "historytreestore.h"
46
47 #include "general.h"
48
49 #endif
50
51 /* === U S I N G =========================================================== */
52
53 using namespace std;
54 using namespace etl;
55 using namespace synfig;
56 using namespace studio;
57
58 /* === M A C R O S ========================================================= */
59 #define COLUMNID_JUMP           (787584)
60 #define ColumnID        int
61
62 /* === G L O B A L S ======================================================= */
63
64 /* === P R O C E D U R E S ================================================= */
65
66 /* === M E T H O D S ======================================================= */
67
68 Dock_History::Dock_History():
69         Dock_CanvasSpecific("history",_("History"),Gtk::StockID("gtk-undo")),
70         action_group(Gtk::ActionGroup::create())
71 {
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));
74
75         action_group->add(Gtk::Action::create(
76                 "clear-undo",
77                 Gtk::StockID("synfig-clear_undo"),
78                 _("Clear Undo Stack"),
79                 _("Clear the undo stack")
80         ),
81                 sigc::mem_fun(
82                         *this,
83                         &Dock_History::clear_undo
84                 )
85         );
86         action_group->add(Gtk::Action::create(
87                 "clear-redo",
88                 Gtk::StockID("synfig-clear_redo"),
89                 _("Clear Redo Stack"),
90                 _("Clear the redo stack")
91         ),
92                 sigc::mem_fun(
93                         *this,
94                         &Dock_History::clear_redo
95                 )
96         );
97         action_group->add(Gtk::Action::create(
98                 "clear-undo-and-redo",
99                 Gtk::Stock::CLEAR,
100                 _("Clear Undo and Redo Stacks"),
101                 _("Clear the undo and redo stacks")
102         ),
103                 sigc::mem_fun(
104                         *this,
105                         &Dock_History::clear_undo_and_redo
106                 )
107         );
108         action_group->add(Gtk::Action::create(
109                 "undo",
110                 Gtk::StockID("gtk-undo"),
111                 _("Undo"),
112                 _("Undo the previous action")
113         ),
114                 sigc::ptr_fun(studio::App::undo)
115         );
116         action_group->add(Gtk::Action::create(
117                 "redo",
118                 Gtk::StockID("gtk-redo"),
119                 _("Redo"),
120                 _("Redo the previously undone action")
121         ),
122                 sigc::ptr_fun(studio::App::redo)
123         );
124
125         action_group->add( Gtk::Action::create("toolbar-history", _("History")) );
126         App::ui_manager()->insert_action_group(action_group);
127
128         Glib::ustring ui_info =
129         "<ui>"
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' />"
136         "       </toolbar>"
137         "</ui>"
138         ;
139
140         App::ui_manager()->add_ui_from_string(ui_info);
141
142         action_group->set_sensitive(false);
143
144         set_toolbar(*dynamic_cast<Gtk::Toolbar*>(App::ui_manager()->get_widget("/toolbar-history")));
145         add(*create_action_tree());
146
147         /*
148         add_button(
149                 Gtk::StockID("synfig-clear_undo"),
150                 _("Clear the UNDO Stack")
151         )->signal_clicked().connect(
152                 sigc::mem_fun(
153                         *this,
154                         &Dock_History::clear_undo
155                 )
156         );
157         add_button(
158                 Gtk::StockID("synfig-clear_redo"),
159                 _("Clear the REDO Stack")
160         )->signal_clicked().connect(
161                 sigc::mem_fun(
162                         *this,
163                         &Dock_History::clear_redo
164                 )
165         );
166         */
167 }
168
169 Dock_History::~Dock_History()
170 {
171 }
172
173 void
174 Dock_History::init_instance_vfunc(etl::loose_handle<Instance> instance)
175 {
176         instance->signal_undo_redo_status_changed().connect(
177                 sigc::mem_fun(*this,&Dock_History::update_undo_redo)
178         );
179 }
180
181 Gtk::Widget*
182 Dock_History::create_action_tree()
183 {
184         studio::HistoryTreeStore::Model history_tree_model;
185         action_tree=manage(new class Gtk::TreeView());
186         {
187                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column("") );
188
189                 Gtk::CellRendererToggle* toggle_cr = Gtk::manage( new Gtk::CellRendererToggle() );
190                 toggle_cr->signal_toggled().connect(sigc::mem_fun(*this, &studio::Dock_History::on_action_toggle) );
191
192                 column->pack_start(*toggle_cr); //false = don't expand.
193                 column->add_attribute(toggle_cr->property_active(),history_tree_model.is_active);
194                 column->set_resizable();
195                 column->set_clickable();
196
197                 action_tree->append_column(*column);
198         }
199         /*{
200                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Canvas")) );
201                 Gtk::CellRendererText *text_cr=Gtk::manage(new Gtk::CellRendererText());
202                 text_cr->property_foreground()=Glib::ustring("#7f7f7f");
203
204                 column->pack_start(*text_cr);
205                 column->add_attribute(text_cr->property_text(),history_tree_model.canvas_id);
206                 column->add_attribute(text_cr->property_foreground_set(),history_tree_model.is_redo);
207
208                 action_tree->append_column(*column);
209         }*/
210         {
211                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Jump")) );
212
213                 Gtk::CellRendererText* cell_renderer_jump=Gtk::manage(new Gtk::CellRendererText());
214                 column->pack_start(*cell_renderer_jump,true);
215
216                 cell_renderer_jump->property_text()=_("(JMP)");
217                 cell_renderer_jump->property_foreground()="#003a7f";
218
219                 column->set_resizable();
220                 column->set_clickable();
221
222                 column->set_sort_column(COLUMNID_JUMP);
223
224                 action_tree->append_column(*column);
225                 //column->clicked();
226         }
227         {
228                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Action")) );
229
230                 Gtk::CellRendererText *text_cr=Gtk::manage(new Gtk::CellRendererText());
231                 text_cr->property_foreground()=Glib::ustring("#7f7f7f");
232
233
234
235                 //column->pack_start(history_tree_model.icon, false); //false = don't expand.
236                 column->pack_start(*text_cr);
237                 column->add_attribute(text_cr->property_text(),history_tree_model.name);
238                 column->add_attribute(text_cr->property_foreground_set(),history_tree_model.is_redo);
239
240                 action_tree->append_column(*column);
241         }
242
243         action_tree->set_enable_search(true);
244         action_tree->set_search_column(history_tree_model.name);
245         action_tree->set_search_equal_func(sigc::ptr_fun(&studio::HistoryTreeStore::search_func));
246
247         action_tree->set_rules_hint();
248 //      action_tree->signal_row_activated().connect(sigc::mem_fun(*this,&Dock_History::on_row_activate));
249         action_tree->signal_event().connect(sigc::mem_fun(*this,&Dock_History::on_action_event));
250 //      action_tree->add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
251 //      action_tree->add_events(Gdk::BUTTON1_MOTION_MASK);
252         action_tree->show();
253
254         Gtk::ScrolledWindow *scrolledwindow = manage(new class Gtk::ScrolledWindow());
255         scrolledwindow->set_flags(Gtk::CAN_FOCUS);
256         scrolledwindow->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
257         scrolledwindow->add(*action_tree);
258         scrolledwindow->set_shadow_type(Gtk::SHADOW_ETCHED_IN);
259         scrolledwindow->show_all();
260
261 /*      {
262                 Gtk::Widget& widget(*action_tree);
263                 Pango::FontDescription font(widget.get_modifier_style()->get_font());
264                 font.set_size(Pango::SCALE*5);
265                 widget.get_modifier_style()->set_font(font);
266                 widget.modify_font(font);
267         }
268 */
269         return scrolledwindow;
270 }
271
272 void
273 Dock_History::clear_undo()
274 {
275         if(selected_instance && App::dialog_yes_no(_("Clear History"),
276                         _("You will not be able to undo any changes that you have made!\nAre you sure you want to clear the undo stack?")))
277                 selected_instance->clear_undo_stack();
278 }
279
280 void
281 Dock_History::clear_redo()
282 {
283         if(selected_instance && App::dialog_yes_no(_("Clear History"),
284                         _("You will not be able to redo any changes that you have made!\nAre you sure you want to clear the redo stack?")))
285                 selected_instance->clear_redo_stack();
286 }
287
288 void
289 Dock_History::clear_undo_and_redo()
290 {
291         if(selected_instance && App::dialog_yes_no(_("Clear History"),
292                         _("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?")))
293         {
294                 selected_instance->clear_undo_stack();
295                 selected_instance->clear_redo_stack();
296         }
297 }
298
299 void
300 Dock_History::update_undo_redo()
301 {
302         etl::handle<Instance> instance=App::get_selected_instance();
303         if(instance)
304         {
305                 action_group->get_action("undo")->set_sensitive(instance->get_undo_status());
306                 action_group->get_action("clear-undo")->set_sensitive(instance->get_undo_status());
307                 action_group->get_action("redo")->set_sensitive(instance->get_redo_status());
308                 action_group->get_action("clear-redo")->set_sensitive(instance->get_redo_status());
309                 action_group->get_action("clear-undo-and-redo")->set_sensitive(instance->get_undo_status() || instance->get_redo_status());
310         }
311 }
312
313 void
314 Dock_History::on_undo_tree_changed()
315 {
316         Gtk::TreeModel::Children children(selected_instance->history_tree_store()->children());
317
318         if (!children.size())
319                 return;
320
321         studio::HistoryTreeStore::Model model;
322
323         Gtk::TreeModel::Children::iterator iter, prev = children.end();
324         for (iter = children.begin(); iter != children.end(); prev = iter++)
325                 if ((*iter)[model.is_redo])
326                 {
327                         if (prev == children.end())
328                                 action_tree->get_selection()->unselect_all();
329                         else
330                         {
331                                 action_tree->scroll_to_row(Gtk::TreePath(prev), 0.5);
332                                 action_tree->get_selection()->select(prev);
333                         }
334                         return;
335                 }
336
337         action_tree->scroll_to_row(Gtk::TreePath(prev), 0.5);
338         action_tree->get_selection()->select(prev);
339 }
340
341 void
342 Dock_History::set_selected_instance_(etl::handle<studio::Instance> instance)
343 {
344         if(studio::App::shutdown_in_progress)
345                 return;
346
347         if (on_undo_tree_changed_connection)
348                 on_undo_tree_changed_connection.disconnect();
349
350         selected_instance=instance;
351         if(instance)
352         {
353                 on_undo_tree_changed_connection = selected_instance->history_tree_store()->signal_undo_tree_changed().connect(
354                         sigc::mem_fun(*this,&Dock_History::on_undo_tree_changed));
355
356                 action_tree->set_model(instance->history_tree_store());
357                 action_tree->show();
358                 update_undo_redo();
359                 action_group->set_sensitive(true);
360         }
361         else
362         {
363                 action_tree->set_model(Glib::RefPtr< Gtk::TreeModel >());
364                 action_tree->hide();
365                 action_group->set_sensitive(false);
366         }
367 }
368
369 void
370 Dock_History::set_selected_instance_signal(etl::handle<studio::Instance> x)
371 {
372         set_selected_instance(x);
373 }
374
375 void
376 Dock_History::set_selected_instance(etl::loose_handle<studio::Instance> x)
377 {
378         if(studio::App::shutdown_in_progress)
379                 return;
380
381         // if it's already selected, don't select it again
382         if (x==selected_instance)
383                 return;
384
385         std::list<etl::handle<studio::Instance> >::iterator iter;
386
387         set_selected_instance_(x);
388 }
389
390 void
391 Dock_History::delete_instance(etl::handle<studio::Instance> instance)
392 {
393         if(studio::App::shutdown_in_progress)
394                 return;
395
396         if(selected_instance==instance)
397         {
398                 set_selected_instance(0);
399         }
400 }
401
402 bool
403 Dock_History::on_action_event(GdkEvent *event)
404 {
405         studio::HistoryTreeStore::Model model;
406     switch(event->type)
407     {
408         case GDK_BUTTON_PRESS:
409         case GDK_2BUTTON_PRESS:
410                 {
411                         Gtk::TreeModel::Path path;
412                         Gtk::TreeViewColumn *column;
413                         int cell_x, cell_y;
414                         if(!action_tree->get_path_at_pos(
415                                 int(event->button.x),int(event->button.y),      // x, y
416                                 path, // TreeModel::Path&
417                                 column, //TreeViewColumn*&
418                                 cell_x,cell_y //int&cell_x,int&cell_y
419                                 )
420                         ) break;
421                         const Gtk::TreeRow row = *(action_tree->get_model()->get_iter(path));
422
423                         //signal_user_click()(event->button.button,row,(ColumnID)column->get_sort_column_id());
424                         if((ColumnID)column->get_sort_column_id()==COLUMNID_JUMP)
425                         {
426                                 etl::handle<synfigapp::Action::Undoable> action(row[model.action]);
427                                 try{
428                                 if((bool)row[model.is_undo])
429                                 {
430                                         while(get_selected_instance()->undo_action_stack().size() && get_selected_instance()->undo_action_stack().front()!=action)
431                                                 if(get_selected_instance()->undo()==false)
432                                                         throw int();
433                                 }
434                                 else if((bool)row[model.is_redo])
435                                 {
436                                         while(get_selected_instance()->redo_action_stack().size() && get_selected_instance()->undo_action_stack().front()!=action)
437                                                 if(get_selected_instance()->redo()==false)
438                                                         throw int();
439                                 }
440                                 }
441                                 catch(int)
442                                 {
443                                         return true;
444                                 }
445                         }
446                 }
447
448         case GDK_BUTTON_RELEASE:
449                 break;
450         default:
451                 break;
452         }
453         return false;
454 }
455
456 void
457 Dock_History::on_action_toggle(const Glib::ustring& path_string)
458 {
459         studio::HistoryTreeStore::Model history_tree_model;
460
461         Gtk::TreePath path(path_string);
462
463         const Gtk::TreeRow row = *(selected_instance->history_tree_store()->get_iter(path));
464
465         handle<synfigapp::Action::Undoable> action=row[history_tree_model.action];
466
467         selected_instance->synfigapp::Instance::set_action_status(action,!action->is_active());
468 }