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