Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_04 / synfig-studio / src / synfigapp / actions / layeractivate.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layeractivate.cpp
3 **      \brief Template File
4 **
5 **      $Id: layeractivate.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
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 "layeractivate.h"
33 #include <synfigapp/canvasinterface.h>
34
35 #endif
36
37 using namespace std;
38 using namespace etl;
39 using namespace synfig;
40 using namespace synfigapp;
41 using namespace Action;
42
43 /* === M A C R O S ========================================================= */
44 #define ACTION_INIT2(class) \
45         Action::Base* class::create() { return new class(); }   \
46         synfig::String class::get_name()const { return name__; }        
47
48 ACTION_INIT2(Action::LayerActivate);
49 ACTION_SET_NAME(Action::LayerActivate,"layer_activate");
50 ACTION_SET_LOCAL_NAME(Action::LayerActivate,_("Activate Layer"));
51 ACTION_SET_TASK(Action::LayerActivate,"activate");
52 ACTION_SET_CATEGORY(Action::LayerActivate,Action::CATEGORY_LAYER);
53 ACTION_SET_PRIORITY(Action::LayerActivate,0);
54 ACTION_SET_VERSION(Action::LayerActivate,"0.0");
55 ACTION_SET_CVS_ID(Action::LayerActivate,"$Id: layeractivate.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $");
56
57 /* === G L O B A L S ======================================================= */
58
59 /* === P R O C E D U R E S ================================================= */
60
61 /* === M E T H O D S ======================================================= */
62
63 Action::LayerActivate::LayerActivate()
64 {
65 }
66
67 synfig::String
68 Action::LayerActivate::get_local_name()const
69 {
70         if(!layer)
71                 return _("Activate Layer");
72         String name;
73         if(layer->get_description().empty())
74                 name=layer->get_local_name();
75         else
76                 name=layer->get_description();
77
78         return (new_status?_("Activate "):_("Deactivate "))+name;
79 }
80
81 Action::ParamVocab
82 Action::LayerActivate::get_param_vocab()
83 {
84         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
85         
86         ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
87                 .set_local_name(_("Layer"))
88         );
89
90         ret.push_back(ParamDesc("new_status",Param::TYPE_BOOL)
91                 .set_local_name(_("New Status"))
92                 .set_desc(_("The new status of the layer"))
93         );
94         
95         return ret;
96 }
97
98 bool
99 Action::LayerActivate::is_canidate(const ParamList &x)
100 {
101         return canidate_check(get_param_vocab(),x);
102 }
103
104 bool
105 Action::LayerActivate::set_param(const synfig::String& name, const Action::Param &param)
106 {
107         if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
108         {
109                 layer=param.get_layer();
110                 
111                 return true;
112         }
113
114         if(name=="new_status" && param.get_type()==Param::TYPE_BOOL)
115         {
116                 new_status=param.get_bool();
117                 
118                 return true;
119         }
120
121         return Action::CanvasSpecific::set_param(name,param);
122 }
123
124 bool
125 Action::LayerActivate::is_ready()const
126 {
127         if(!layer)
128                 return false;
129         return Action::CanvasSpecific::is_ready();
130 }
131
132 void
133 Action::LayerActivate::perform()
134 {
135         Canvas::Handle subcanvas(layer->get_canvas());
136         
137         // Find the iterator for the layer
138         Canvas::iterator iter=find(subcanvas->begin(),subcanvas->end(),layer);
139         
140         // If we couldn't find the layer in the canvas, then bail
141         if(*iter!=layer)
142                 throw Error(_("This layer doesn't exist anymore."));
143
144         // If the subcanvas isn't the same as the canvas,
145         // then it had better be an inline canvas. If not,
146         // bail
147         //if(get_canvas()!=subcanvas && !subcanvas->is_inline())
148         //if(get_canvas()->get_root()!=subcanvas->get_root())
149         //      throw Error(_("This layer doesn't belong to this composition"));
150         
151         old_status=layer->active();
152         
153         // If we are changing the status to what it already is,
154         // the go ahead and return
155         if(new_status==old_status)
156         {
157                 set_dirty(false);
158                 return;
159         }
160         else
161                 set_dirty();
162         
163         if(new_status)
164                 layer->enable();
165         else
166                 layer->disable();
167
168         if(get_canvas_interface())
169         {
170                 get_canvas_interface()->signal_layer_status_changed()(layer,new_status);
171         }
172         else synfig::warning("CanvasInterface not set on action");
173 }
174
175 void
176 Action::LayerActivate::undo()
177 {
178         // If we are changing the status to what it already is,
179         // the go ahead and return
180         if(new_status==old_status)
181         {
182                 set_dirty(false);
183                 return;
184         }
185         else
186                 set_dirty();
187
188         // restore the old status
189         if(old_status)
190                 layer->enable();
191         else
192                 layer->disable();
193         
194         if(get_canvas_interface())
195         {
196                 get_canvas_interface()->signal_layer_status_changed()(layer,old_status);
197         }
198         else synfig::warning("CanvasInterface not set on action");
199 }