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