Added copyright lines for files I've edited this year.
[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 **      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
109 void
110 HistoryTreeStore::insert_action(Gtk::TreeRow row,etl::handle<synfigapp::Action::Undoable> action, bool /*is_active*/, bool is_undo, bool is_redo)
111 {
112         assert(action);
113
114         row[model.action] = action;
115         row[model.name] = static_cast<Glib::ustring>(action->get_local_name());
116         row[model.is_active] = action->is_active();
117         row[model.is_undo] = is_undo;
118         row[model.is_redo] = is_redo;
119
120         synfigapp::Action::CanvasSpecific *specific_action;
121         specific_action=dynamic_cast<synfigapp::Action::CanvasSpecific*>(action.get());
122         if(specific_action)
123         {
124                 row[model.canvas] = specific_action->get_canvas();
125                 row[model.canvas_id] = specific_action->get_canvas()->get_id();
126         }
127
128         etl::handle<synfigapp::Action::Group> group;
129         group=etl::handle<synfigapp::Action::Group>::cast_dynamic(action);
130         if(group)
131         {
132                 synfigapp::Action::ActionList::const_iterator iter;
133                 for(iter=group->action_list().begin();iter!=group->action_list().end();++iter)
134                 {
135                         Gtk::TreeRow child_row = *(append(row.children()));
136                         insert_action(child_row,*iter,true,is_undo,is_redo);
137                 }
138         }
139
140         //row[model.icon] = Gtk::Button().render_icon(Gtk::StockID("synfig-canvas"),Gtk::ICON_SIZE_SMALL_TOOLBAR);
141 }
142
143
144 void
145 HistoryTreeStore::on_undo()
146 {
147         refresh();
148 }
149
150 void
151 HistoryTreeStore::on_redo()
152 {
153         refresh();
154 }
155
156 void
157 HistoryTreeStore::on_undo_stack_cleared()
158 {
159         Gtk::TreeModel::Children::iterator iter,next;
160         Gtk::TreeModel::Children children_(children());
161
162         for(next=children_.begin(),iter=next++; iter != children_.end(); iter=(next!=children_.end())?next++:next)
163         {
164                 Gtk::TreeModel::Row row = *iter;
165                 if(row[model.is_undo])
166                         erase(iter);
167         }
168 }
169
170 void
171 HistoryTreeStore::on_redo_stack_cleared()
172 {
173         Gtk::TreeModel::Children::iterator iter,next;
174         Gtk::TreeModel::Children children_(children());
175
176         for(next=children_.begin(),iter=next++; iter != children_.end(); iter=(next!=children_.end())?next++:next)
177         {
178                 Gtk::TreeModel::Row row = *iter;
179                 if(row[model.is_redo])
180                         erase(iter);
181         }
182 }
183
184 void
185 HistoryTreeStore::on_new_action(etl::handle<synfigapp::Action::Undoable> action)
186 {
187 //      Gtk::TreeRow row = *(append());
188         Gtk::TreeRow row;
189         Gtk::TreeModel::Children::iterator iter;
190         for(iter=children().begin(); iter != children().end(); ++iter)
191         {
192                 Gtk::TreeModel::Row row = *iter;
193                 if(row[model.is_redo])
194                 {
195                         break;
196                 }
197         }
198
199         row=*insert(iter);
200
201         insert_action(row,action);
202 }
203
204 void
205 HistoryTreeStore::on_action_status_changed(etl::handle<synfigapp::Action::Undoable> action)
206 {
207         Gtk::TreeModel::Children::iterator iter;
208         Gtk::TreeModel::Children children_(children());
209
210         for(iter=children_.begin(); iter != children_.end(); ++iter)
211         {
212                 Gtk::TreeModel::Row row = *iter;
213                 if(action == (etl::handle<synfigapp::Action::Undoable>)row[model.action])
214                 {
215                         row[model.is_active]=action->is_active();
216                         return;
217                 }
218         }
219 }
220
221 bool
222 HistoryTreeStore::search_func(const Glib::RefPtr<Gtk::TreeModel>&,int,const Glib::ustring& x,const Gtk::TreeModel::iterator& iter)
223 {
224         const Model model;
225
226         Glib::ustring substr(x.uppercase());
227         Glib::ustring name((*iter)[model.name]);
228         name=name.uppercase();
229
230         return name.find(substr)==Glib::ustring::npos;
231 }