Add an option "Browser Command" to allow the user to specify which web browser to...
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / activepointseton.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file activepointseton.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 "activepointseton.h"
33 #include "activepointsetsmart.h"
34 #include "valuenodelinkconnect.h"
35 #include "valuenodereplace.h"
36
37 #include "activepointset.h"
38 #include "activepointadd.h"
39
40 #include "valuedescconnect.h"
41 #include <synfigapp/canvasinterface.h>
42
43 #include <synfigapp/general.h>
44
45 #endif
46
47 using namespace std;
48 using namespace etl;
49 using namespace synfig;
50 using namespace synfigapp;
51 using namespace Action;
52
53 /* === M A C R O S ========================================================= */
54
55 ACTION_INIT(Action::ActivepointSetOn);
56 ACTION_SET_NAME(Action::ActivepointSetOn,"activepoint_set_on");
57 ACTION_SET_LOCAL_NAME(Action::ActivepointSetOn,N_("Mark Activepoint as \"On\""));
58 ACTION_SET_TASK(Action::ActivepointSetOn,"set_on");
59 ACTION_SET_CATEGORY(Action::ActivepointSetOn,Action::CATEGORY_ACTIVEPOINT|Action::CATEGORY_VALUEDESC);
60 ACTION_SET_PRIORITY(Action::ActivepointSetOn,-10);
61 ACTION_SET_VERSION(Action::ActivepointSetOn,"0.0");
62 ACTION_SET_CVS_ID(Action::ActivepointSetOn,"$Id$");
63
64 /* === G L O B A L S ======================================================= */
65
66 /* === P R O C E D U R E S ================================================= */
67
68 /* === M E T H O D S ======================================================= */
69
70 Action::ActivepointSetOn::ActivepointSetOn()
71 {
72         activepoint.set_time(Time::begin()-1);
73         time_set=false;
74         set_dirty(true);
75 }
76
77 Action::ParamVocab
78 Action::ActivepointSetOn::get_param_vocab()
79 {
80         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
81
82         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
83                 .set_local_name(_("ValueDesc"))
84         );
85
86         ret.push_back(ParamDesc("activepoint",Param::TYPE_ACTIVEPOINT)
87                 .set_local_name(_("Activepoint"))
88                 .set_optional()
89         );
90
91         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
92                 .set_local_name(_("Time"))
93                 .set_optional()
94         );
95
96         return ret;
97 }
98
99 bool
100 Action::ActivepointSetOn::is_candidate(const ParamList &x)
101 {
102         if(candidate_check(get_param_vocab(),x))
103         {
104                 ValueDesc value_desc(x.find("value_desc")->second.get_value_desc());
105                 if(!value_desc.parent_is_value_node() || !ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node()))
106                         return false;
107
108                 // We are only a candidate if this canvas is animated.
109                 Canvas::Handle canvas(x.find("canvas")->second.get_canvas());
110                 if(canvas->rend_desc().get_time_start()==canvas->rend_desc().get_time_end())
111                         return false;
112
113                 // We need either a activepoint or a time.
114                 if(x.count("activepoint") || x.count("time"))
115                         return true;
116         }
117         return false;
118 }
119
120 bool
121 Action::ActivepointSetOn::set_param(const synfig::String& name, const Action::Param &param)
122 {
123         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
124         {
125                 value_desc=param.get_value_desc();
126
127                 if(!value_desc.parent_is_value_node())
128                         return false;
129
130                 value_node=ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node());
131
132                 if(!value_node)
133                         return false;
134
135                 index=value_desc.get_index();
136
137                 if(time_set)
138                         calc_activepoint();
139
140                 return true;
141         }
142         if(name=="activepoint" && param.get_type()==Param::TYPE_ACTIVEPOINT && !time_set)
143         {
144                 activepoint=param.get_activepoint();
145
146                 return true;
147         }
148         if(name=="time" && param.get_type()==Param::TYPE_TIME && activepoint.get_time()==Time::begin()-1)
149         {
150                 activepoint.set_time(param.get_time());
151                 time_set=true;
152
153                 if(value_node)
154                         calc_activepoint();
155
156                 return true;
157         }
158
159         return Action::CanvasSpecific::set_param(name,param);
160 }
161
162 bool
163 Action::ActivepointSetOn::is_ready()const
164 {
165         if(!value_node)
166                 synfig::error("Missing value_node");
167
168         if(activepoint.get_time()==(Time::begin()-1))
169                 synfig::error("Missing activepoint");
170
171         if(!value_node || activepoint.get_time()==(Time::begin()-1))
172                 return false;
173         return Action::CanvasSpecific::is_ready();
174 }
175
176 // This function is called if a time is specified, but not
177 // a activepoint. In this case, we need to calculate the value
178 // of the activepoint
179 void
180 Action::ActivepointSetOn::calc_activepoint()
181 {
182         const Time time(activepoint.get_time());
183
184         try { activepoint=*value_node->list[index].find(time); }
185         catch(...)
186         {
187                 activepoint.set_time(time);
188                 activepoint.set_state(value_node->list[index].status_at_time(time));
189                 activepoint.set_priority(0);
190         }
191 }
192
193 void
194 Action::ActivepointSetOn::prepare()
195 {
196         clear();
197
198         // Turn the activepoint off
199         activepoint.set_state(true);
200
201         Action::Handle action(ActivepointSetSmart::create());
202
203         action->set_param("edit_mode",get_edit_mode());
204         action->set_param("canvas",get_canvas());
205         action->set_param("canvas_interface",get_canvas_interface());
206         action->set_param("value_desc",value_desc);
207         action->set_param("activepoint",activepoint);
208
209         assert(action->is_ready());
210         if(!action->is_ready())
211                 throw Error(Error::TYPE_NOTREADY);
212
213         add_action_front(action);
214
215 }