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