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