Fix a crash when running single-threaded and dragging the time slider.
[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 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 #endif
48
49 /* === U S I N G =========================================================== */
50
51 using namespace std;
52 using namespace etl;
53 using namespace synfig;
54 using namespace studio;
55
56 /* === M A C R O S ========================================================= */
57 #define COLUMNID_JUMP           (787584)
58 #define ColumnID        int
59
60 /* === G L O B A L S ======================================================= */
61
62 /* === P R O C E D U R E S ================================================= */
63
64 /* === M E T H O D S ======================================================= */
65
66 Dock_History::Dock_History():
67         Dock_CanvasSpecific("history",_("History"),Gtk::StockID("gtk-undo")),
68         action_group(Gtk::ActionGroup::create())
69 {
70         App::signal_instance_deleted().connect(sigc::mem_fun(*this,&studio::Dock_History::delete_instance));
71         App::signal_instance_selected().connect(sigc::mem_fun(*this,&studio::Dock_History::set_selected_instance_signal));
72
73         action_group->add(Gtk::Action::create(
74                 "clear-undo",
75                 Gtk::StockID("synfig-clear_undo"),
76                 _("Clear Undo Stack"),
77                 _("Clear the undo stack")
78         ),
79                 sigc::mem_fun(
80                         *this,
81                         &Dock_History::clear_undo
82                 )
83         );
84         action_group->add(Gtk::Action::create(
85                 "clear-redo",
86                 Gtk::StockID("synfig-clear_redo"),
87                 _("Clear Redo Stack"),
88                 _("Clear the redo stack")
89         ),
90                 sigc::mem_fun(
91                         *this,
92                         &Dock_History::clear_redo
93                 )
94         );
95         action_group->add(Gtk::Action::create(
96                 "undo",
97                 Gtk::StockID("gtk-undo"),
98                 _("Undo"),
99                 _("Undo the previous action")
100         ),
101                 sigc::ptr_fun(studio::App::undo)
102         );
103         action_group->add(Gtk::Action::create(
104                 "redo",
105                 Gtk::StockID("gtk-redo"),
106                 _("Redo"),
107                 _("Redo the previously undone action")
108         ),
109                 sigc::ptr_fun(studio::App::redo)
110         );
111
112         action_group->add( Gtk::Action::create("toolbar-history", "History") );
113         App::ui_manager()->insert_action_group(action_group);
114
115         Glib::ustring ui_info =
116         "<ui>"
117         "       <toolbar action='toolbar-history'>"
118         "       <toolitem action='undo' />"
119         "       <toolitem action='redo' />"
120         "       <toolitem action='clear-undo' />"
121         "       <toolitem action='clear-redo' />"
122         "       </toolbar>"
123         "</ui>"
124         ;
125
126         App::ui_manager()->add_ui_from_string(ui_info);
127
128         action_group->set_sensitive(false);
129
130         set_toolbar(*dynamic_cast<Gtk::Toolbar*>(App::ui_manager()->get_widget("/toolbar-history")));
131         add(*create_action_tree());
132
133         /*
134         add_button(
135                 Gtk::StockID("synfig-clear_undo"),
136                 _("Clear the UNDO Stack")
137         )->signal_clicked().connect(
138                 sigc::mem_fun(
139                         *this,
140                         &Dock_History::clear_undo
141                 )
142         );
143         add_button(
144                 Gtk::StockID("synfig-clear_redo"),
145                 _("Clear the REDO Stack")
146         )->signal_clicked().connect(
147                 sigc::mem_fun(
148                         *this,
149                         &Dock_History::clear_redo
150                 )
151         );
152         */
153 }
154
155 Dock_History::~Dock_History()
156 {
157 }
158
159 void
160 Dock_History::init_instance_vfunc(etl::loose_handle<Instance> instance)
161 {
162         instance->signal_undo_redo_status_changed().connect(
163                 sigc::mem_fun(*this,&Dock_History::update_undo_redo)
164         );
165 }
166
167 Gtk::Widget*
168 Dock_History::create_action_tree()
169 {
170         studio::HistoryTreeStore::Model history_tree_model;
171         action_tree=manage(new class Gtk::TreeView());
172         {
173                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column("") );
174
175                 Gtk::CellRendererToggle* toggle_cr = Gtk::manage( new Gtk::CellRendererToggle() );
176                 toggle_cr->signal_toggled().connect(sigc::mem_fun(*this, &studio::Dock_History::on_action_toggle) );
177
178                 column->pack_start(*toggle_cr); //false = don't expand.
179                 column->add_attribute(toggle_cr->property_active(),history_tree_model.is_active);
180                 column->set_resizable();
181                 column->set_clickable();
182
183                 action_tree->append_column(*column);
184         }
185         /*{
186                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column("Canvas") );
187                 Gtk::CellRendererText *text_cr=Gtk::manage(new Gtk::CellRendererText());
188                 text_cr->property_foreground()=Glib::ustring("#7f7f7f");
189
190                 column->pack_start(*text_cr);
191                 column->add_attribute(text_cr->property_text(),history_tree_model.canvas_id);
192                 column->add_attribute(text_cr->property_foreground_set(),history_tree_model.is_redo);
193
194                 action_tree->append_column(*column);
195         }*/
196         {
197                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Jump")) );
198
199                 Gtk::CellRendererText* cell_renderer_jump=Gtk::manage(new Gtk::CellRendererText());
200                 column->pack_start(*cell_renderer_jump,true);
201
202                 cell_renderer_jump->property_text()="(JMP)";
203                 cell_renderer_jump->property_foreground()="#003a7f";
204
205                 column->set_resizable();
206                 column->set_clickable();
207
208                 column->set_sort_column_id(COLUMNID_JUMP);
209
210                 action_tree->append_column(*column);
211                 //column->clicked();
212         }
213         {
214                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column("Action") );
215
216                 Gtk::CellRendererText *text_cr=Gtk::manage(new Gtk::CellRendererText());
217                 text_cr->property_foreground()=Glib::ustring("#7f7f7f");
218
219
220
221                 //column->pack_start(history_tree_model.icon, false); //false = don't expand.
222                 column->pack_start(*text_cr);
223                 column->add_attribute(text_cr->property_text(),history_tree_model.name);
224                 column->add_attribute(text_cr->property_foreground_set(),history_tree_model.is_redo);
225
226                 action_tree->append_column(*column);
227         }
228
229
230         action_tree->set_rules_hint();
231 //      action_tree->signal_row_activated().connect(sigc::mem_fun(*this,&Dock_History::on_row_activate));
232         action_tree->signal_event().connect(sigc::mem_fun(*this,&Dock_History::on_action_event));
233 //      action_tree->add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
234 //      action_tree->add_events(Gdk::BUTTON1_MOTION_MASK);
235         action_tree->show();
236
237         Gtk::ScrolledWindow *scrolledwindow = manage(new class Gtk::ScrolledWindow());
238         scrolledwindow->set_flags(Gtk::CAN_FOCUS);
239         scrolledwindow->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
240         scrolledwindow->add(*action_tree);
241         scrolledwindow->set_shadow_type(Gtk::SHADOW_ETCHED_IN);
242         scrolledwindow->show_all();
243
244 /*      {
245                 Gtk::Widget& widget(*action_tree);
246                 Pango::FontDescription font(widget.get_modifier_style()->get_font());
247                 font.set_size(Pango::SCALE*5);
248                 widget.get_modifier_style()->set_font(font);
249                 widget.modify_font(font);
250         }
251 */
252         return scrolledwindow;
253 }
254
255 void
256 Dock_History::clear_undo()
257 {
258         if(selected_instance && App::dialog_yes_no(_("Clear History"), _("You will not be able to undo any changes that you have made!\nAre you sure you want to clear the undo stack?")))
259         {
260                 selected_instance->clear_undo_stack();
261         }
262 }
263
264 void
265 Dock_History::clear_redo()
266 {
267         if(selected_instance && App::dialog_yes_no(_("Clear History"), _("You will not be able to redo any changes that you have made!\nAre you sure you want to clear the redo stack?")))
268         {
269                 selected_instance->clear_redo_stack();
270         }
271 }
272
273 void
274 Dock_History::update_undo_redo()
275 {
276         etl::handle<Instance> instance=App::get_selected_instance();
277         if(instance)
278         {
279                 action_group->get_action("undo")->set_sensitive(instance->get_undo_status());
280                 action_group->get_action("clear-undo")->set_sensitive(instance->get_undo_status());
281                 action_group->get_action("redo")->set_sensitive(instance->get_redo_status());
282                 action_group->get_action("clear-redo")->set_sensitive(instance->get_redo_status());
283         }
284 }
285
286 void
287 Dock_History::set_selected_instance_(etl::handle<studio::Instance> instance)
288 {
289         if(studio::App::shutdown_in_progress)
290                 return;
291
292         selected_instance=instance;
293         if(instance)
294         {
295                 action_tree->set_model(instance->history_tree_store());
296                 action_tree->show();
297                 update_undo_redo();
298                 action_group->set_sensitive(true);
299         }
300         else
301         {
302                 action_tree->set_model(Glib::RefPtr< Gtk::TreeModel >());
303                 action_tree->hide();
304                 action_group->set_sensitive(false);
305         }
306 }
307
308 void
309 Dock_History::set_selected_instance_signal(etl::handle<studio::Instance> x)
310 {
311         set_selected_instance(x);
312 }
313
314 void
315 Dock_History::set_selected_instance(etl::loose_handle<studio::Instance> x)
316 {
317         if(studio::App::shutdown_in_progress)
318                 return;
319
320         // if it's already selected, don't select it again
321         if (x==selected_instance)
322                 return;
323
324         std::list<etl::handle<studio::Instance> >::iterator iter;
325
326         set_selected_instance_(x);
327 }
328
329 void
330 Dock_History::delete_instance(etl::handle<studio::Instance> instance)
331 {
332         if(studio::App::shutdown_in_progress)
333                 return;
334
335         if(selected_instance==instance)
336         {
337                 set_selected_instance(0);
338         }
339 }
340
341 bool
342 Dock_History::on_action_event(GdkEvent *event)
343 {
344         studio::HistoryTreeStore::Model model;
345     switch(event->type)
346     {
347         case GDK_BUTTON_PRESS:
348         case GDK_2BUTTON_PRESS:
349                 {
350                         Gtk::TreeModel::Path path;
351                         Gtk::TreeViewColumn *column;
352                         int cell_x, cell_y;
353                         if(!action_tree->get_path_at_pos(
354                                 int(event->button.x),int(event->button.y),      // x, y
355                                 path, // TreeModel::Path&
356                                 column, //TreeViewColumn*&
357                                 cell_x,cell_y //int&cell_x,int&cell_y
358                                 )
359                         ) break;
360                         const Gtk::TreeRow row = *(action_tree->get_model()->get_iter(path));
361
362                         //signal_user_click()(event->button.button,row,(ColumnID)column->get_sort_column_id());
363                         if((ColumnID)column->get_sort_column_id()==COLUMNID_JUMP)
364                         {
365                                 etl::handle<synfigapp::Action::Undoable> action(row[model.action]);
366                                 try{
367                                 if((bool)row[model.is_undo])
368                                 {
369                                         while(get_selected_instance()->undo_action_stack().size() && get_selected_instance()->undo_action_stack().front()!=action)
370                                                 if(get_selected_instance()->undo()==false)
371                                                         throw int();
372                                 }
373                                 else if((bool)row[model.is_redo])
374                                 {
375                                         while(get_selected_instance()->redo_action_stack().size() && get_selected_instance()->undo_action_stack().front()!=action)
376                                                 if(get_selected_instance()->redo()==false)
377                                                         throw int();
378                                 }
379                                 }
380                                 catch(int)
381                                 {
382                                         return true;
383                                 }
384                         }
385                 }
386
387         case GDK_BUTTON_RELEASE:
388                 break;
389         default:
390                 break;
391         }
392         return false;
393 }
394
395 void
396 Dock_History::on_action_toggle(const Glib::ustring& path_string)
397 {
398         studio::HistoryTreeStore::Model history_tree_model;
399
400         Gtk::TreePath path(path_string);
401
402         const Gtk::TreeRow row = *(selected_instance->history_tree_store()->get_iter(path));
403
404         handle<synfigapp::Action::Undoable> action=row[history_tree_model.action];
405
406         selected_instance->synfigapp::Instance::set_action_status(action,!action->is_active());
407 }