Implements PXEGeek's http://wiki.synfig.com/Wish_list entry: "Optionally display...
[synfig.git] / synfig-studio / trunk / src / gtkmm / historytreestore.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file historytreestore.cpp
3 **      \brief Template File
4 **
5 **      $Id$
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 "historytreestore.h"
33 #include <synfig/valuenode.h>
34 #include "iconcontroler.h"
35 #include <synfig/valuenode_timedswap.h>
36 #include <gtkmm/button.h>
37 #include <synfigapp/action.h>
38 #include "instance.h"
39
40 #endif
41
42 /* === U S I N G =========================================================== */
43
44 using namespace std;
45 using namespace etl;
46 using namespace synfig;
47 using namespace studio;
48
49 /* === M A C R O S ========================================================= */
50
51 /* === G L O B A L S ======================================================= */
52
53 /* === P R O C E D U R E S ================================================= */
54
55 /* === M E T H O D S ======================================================= */
56
57 static HistoryTreeStore::Model& ModelHack()
58 {
59         static HistoryTreeStore::Model* model(0);
60         if(!model)model=new HistoryTreeStore::Model;
61         return *model;
62 }
63
64 HistoryTreeStore::HistoryTreeStore(etl::loose_handle<studio::Instance> instance_):
65         Gtk::TreeStore  (ModelHack()),
66         instance_               (instance_)
67 {
68         instance_->signal_undo().connect(sigc::mem_fun(*this,&studio::HistoryTreeStore::on_undo));
69         instance_->signal_redo().connect(sigc::mem_fun(*this,&studio::HistoryTreeStore::on_redo));
70         instance_->signal_undo_stack_cleared().connect(sigc::mem_fun(*this,&studio::HistoryTreeStore::on_undo_stack_cleared));
71         instance_->signal_redo_stack_cleared().connect(sigc::mem_fun(*this,&studio::HistoryTreeStore::on_redo_stack_cleared));
72         instance_->signal_new_action().connect(sigc::mem_fun(*this,&studio::HistoryTreeStore::on_new_action));
73         instance_->signal_action_status_changed().connect(sigc::mem_fun(*this,&studio::HistoryTreeStore::on_action_status_changed));
74 }
75
76 HistoryTreeStore::~HistoryTreeStore()
77 {
78         synfig::info("HistoryTreeStore::~HistoryTreeStore(): Deleted");
79 }
80
81 Glib::RefPtr<HistoryTreeStore>
82 HistoryTreeStore::create(etl::loose_handle<studio::Instance> instance_)
83 {
84         return Glib::RefPtr<HistoryTreeStore>(new HistoryTreeStore(instance_));
85 }
86
87 void
88 HistoryTreeStore::rebuild()
89 {
90         synfigapp::Action::Stack::const_iterator iter;
91
92         clear();
93
94         for(iter=instance()->undo_action_stack().begin();iter!=instance()->undo_action_stack().end();++iter)
95         {
96                 insert_action(*(prepend()),*iter,true,true,false);
97         }
98         curr_row=*children().end();
99         for(iter=instance()->redo_action_stack().begin();iter!=instance()->redo_action_stack().end();++iter)
100         {
101                 insert_action(*(append()),*iter,true,false,true);
102         }
103 }
104
105 void
106 HistoryTreeStore::insert_action(Gtk::TreeRow row,etl::handle<synfigapp::Action::Undoable> action, bool is_active, bool is_undo, bool is_redo)
107 {
108         assert(action);
109
110         row[model.action] = action;
111         row[model.name] = static_cast<Glib::ustring>(action->get_local_name());
112         row[model.is_active] = action->is_active();
113         row[model.is_undo] = is_undo;
114         row[model.is_redo] = is_redo;
115
116         synfigapp::Action::CanvasSpecific *specific_action;
117         specific_action=dynamic_cast<synfigapp::Action::CanvasSpecific*>(action.get());
118         if(specific_action)
119         {
120                 row[model.canvas] = specific_action->get_canvas();
121                 row[model.canvas_id] = specific_action->get_canvas()->get_id();
122         }
123
124         etl::handle<synfigapp::Action::Group> group;
125         group=etl::handle<synfigapp::Action::Group>::cast_dynamic(action);
126         if(group)
127         {
128                 synfigapp::Action::ActionList::const_iterator iter;
129                 for(iter=group->action_list().begin();iter!=group->action_list().end();++iter)
130                 {
131                         Gtk::TreeRow child_row = *(append(row.children()));
132                         insert_action(child_row,*iter,true,is_undo,is_redo);
133                 }
134         }
135
136         //row[model.icon] = Gtk::Button().render_icon(Gtk::StockID("synfig-canvas"),Gtk::ICON_SIZE_SMALL_TOOLBAR);
137 }
138
139
140 void
141 HistoryTreeStore::on_undo()
142 {
143         refresh();
144 }
145
146 void
147 HistoryTreeStore::on_redo()
148 {
149         refresh();
150 }
151
152 void
153 HistoryTreeStore::on_undo_stack_cleared()
154 {
155         Gtk::TreeModel::Children::iterator iter,next;
156         Gtk::TreeModel::Children children_(children());
157
158         for(next=children_.begin(),iter=next++; iter != children_.end(); iter=(next!=children_.end())?next++:next)
159         {
160                 Gtk::TreeModel::Row row = *iter;
161                 if(row[model.is_undo])
162                         erase(iter);
163         }
164 }
165
166 void
167 HistoryTreeStore::on_redo_stack_cleared()
168 {
169         Gtk::TreeModel::Children::iterator iter,next;
170         Gtk::TreeModel::Children children_(children());
171
172         for(next=children_.begin(),iter=next++; iter != children_.end(); iter=(next!=children_.end())?next++:next)
173         {
174                 Gtk::TreeModel::Row row = *iter;
175                 if(row[model.is_redo])
176                         erase(iter);
177         }
178 }
179
180 void
181 HistoryTreeStore::on_new_action(etl::handle<synfigapp::Action::Undoable> action)
182 {
183 //      Gtk::TreeRow row = *(append());
184         Gtk::TreeRow row;
185         Gtk::TreeModel::Children::iterator iter;
186         for(iter=children().begin(); iter != children().end(); ++iter)
187         {
188                 Gtk::TreeModel::Row row = *iter;
189                 if(row[model.is_redo])
190                 {
191                         break;
192                 }
193         }
194
195         row=*insert(iter);
196
197         insert_action(row,action);
198 }
199
200 void
201 HistoryTreeStore::on_action_status_changed(etl::handle<synfigapp::Action::Undoable> action)
202 {
203         Gtk::TreeModel::Children::iterator iter;
204         Gtk::TreeModel::Children children_(children());
205
206         for(iter=children_.begin(); iter != children_.end(); ++iter)
207         {
208                 Gtk::TreeModel::Row row = *iter;
209                 if(action == (etl::handle<synfigapp::Action::Undoable>)row[model.action])
210                 {
211                         row[model.is_active]=action->is_active();
212                         return;
213                 }
214         }
215 }