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