minor fix
[synfig.git] / synfig-studio / trunk / src / synfigapp / main.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file main.cpp
3 **      \brief Template File
4 **
5 **      $Id: main.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 "main.h"
33 #include "action.h"
34
35 #include <synfig/color.h>
36 #include <synfig/gradient.h>
37
38 #include <ETL/trivial>
39
40 #include <list>
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 synfigapp;
50
51 /* === M A C R O S ========================================================= */
52
53 /* === S T A T I C S ======================================================= */
54
55 static etl::reference_counter synfigapp_ref_count_(0);
56 static synfigapp::Action::Main* action_main;
57
58 static Color foreground_;
59 static Color background_;
60 static Gradient gradient_;
61 static bool gradient_default_colors_;
62
63 static synfig::Distance bline_width_;
64
65 static Color::BlendMethod blend_method_;
66 static Real opacity_;
67
68 static synfigapp::InputDevice::Handle selected_input_device_;
69 static list<synfigapp::InputDevice::Handle> input_devices_;
70
71 trivial<sigc::signal<void> > signal_foreground_color_changed_;
72 trivial<sigc::signal<void> > signal_background_color_changed_;
73 trivial<sigc::signal<void> > signal_gradient_changed_;
74 trivial<sigc::signal<void> > signal_bline_width_changed_;
75 trivial<sigc::signal<void> > signal_blend_method_changed_;
76 trivial<sigc::signal<void> > signal_opacity_changed_;
77 trivial<sigc::signal<void> > signal_interpolation_changed_;
78
79 trivial<Settings> settings_;
80
81 static synfig::Waypoint::Interpolation interpolation_;
82
83 /* === P R O C E D U R E S ================================================= */
84
85 /* === M E T H O D S ======================================================= */
86
87 synfigapp::Main::Main(const synfig::String &basepath,ProgressCallback *cb):
88         synfig::Main(basepath,cb),
89         ref_count_(synfigapp_ref_count_)
90 {
91         if(ref_count_.count())
92                 return;
93
94         synfigapp_ref_count_.reset();
95         ref_count_=synfigapp_ref_count_;
96         
97         // Add initialization after this point
98         action_main=new synfigapp::Action::Main();
99
100         settings_.construct();
101         
102         signal_foreground_color_changed_.construct();
103         signal_background_color_changed_.construct();
104         signal_gradient_changed_.construct();
105         signal_opacity_changed_.construct();
106         signal_blend_method_changed_.construct();
107         signal_interpolation_changed_.construct();
108         
109         set_foreground_color(Color::black());
110         set_background_color(Color::white());
111         set_gradient_default_colors();
112         set_bline_width(Distance(1,Distance::SYSTEM_POINTS));
113         set_opacity(1.0);
114         set_blend_method(Color::BLEND_COMPOSITE);
115 }
116
117 synfigapp::Main::~Main()
118 {
119         ref_count_.detach();
120         if(!synfigapp_ref_count_.unique())
121                 return;
122         synfigapp_ref_count_.detach();
123         
124         // Add deinitialization after this point
125
126         delete action_main;     
127         
128         selected_input_device_=0;
129         input_devices_.clear();
130         
131         settings_.destruct();
132         signal_foreground_color_changed_.destruct();
133         signal_background_color_changed_.destruct();
134         signal_gradient_changed_.destruct();
135
136         signal_opacity_changed_.destruct();
137         signal_blend_method_changed_.destruct();
138         signal_interpolation_changed_.destruct();
139 }
140
141 Settings&
142 synfigapp::Main::settings()
143 {
144         return settings_;
145 }
146
147 sigc::signal<void>&
148 synfigapp::Main::signal_foreground_color_changed()
149 {
150         return signal_foreground_color_changed_;
151 }
152
153 sigc::signal<void>&
154 synfigapp::Main::signal_background_color_changed()
155 {
156         return signal_background_color_changed_;
157 }
158
159 sigc::signal<void>&
160 synfigapp::Main::signal_gradient_changed()
161 {
162         return signal_gradient_changed_;
163 }
164
165 sigc::signal<void>&
166 synfigapp::Main::signal_bline_width_changed()
167 {
168         return signal_bline_width_changed_;
169 }
170
171 sigc::signal<void>&
172 synfigapp::Main::signal_blend_method_changed()
173 {
174         return signal_blend_method_changed_;
175 }
176
177 sigc::signal<void>&
178 synfigapp::Main::signal_opacity_changed()
179 {
180         return signal_opacity_changed_;
181 }
182
183 sigc::signal<void>&
184 synfigapp::Main::signal_interpolation_changed()
185 {
186         return signal_interpolation_changed_;
187 }
188
189 const synfig::Color&
190 synfigapp::Main::get_foreground_color()
191 {
192         return foreground_;
193 }
194
195 const synfig::Color&
196 synfigapp::Main::get_background_color()
197 {
198         return background_;
199 }
200
201 const synfig::Gradient&
202 synfigapp::Main::get_gradient()
203 {
204         return gradient_;
205 }
206
207 const synfig::Real&
208 synfigapp::Main::get_opacity()
209 {
210         return opacity_;
211 }
212
213 synfig::Color::BlendMethod
214 synfigapp::Main::get_blend_method()
215 {
216         return blend_method_;
217 }
218
219 void
220 synfigapp::Main::set_foreground_color(synfig::Color color)
221 {
222         foreground_=color;
223         signal_foreground_color_changed()();
224         if(selected_input_device_)
225                 selected_input_device_->set_foreground_color(foreground_);
226         if(gradient_default_colors_)
227         {
228                 gradient_=Gradient(foreground_,background_);
229                 signal_gradient_changed()();
230         }
231 }
232
233 void
234 synfigapp::Main::set_background_color(synfig::Color color)
235 {
236         background_=color;
237         signal_background_color_changed()();
238
239         if(selected_input_device_)
240                 selected_input_device_->set_background_color(background_);
241
242         if(gradient_default_colors_)
243         {
244                 gradient_=Gradient(foreground_,background_);
245                 signal_gradient_changed()();
246         }
247 }
248
249 void
250 synfigapp::Main::set_gradient(synfig::Gradient gradient)
251 {
252         gradient_=gradient;
253         gradient_default_colors_=false;
254         signal_gradient_changed()();
255 }
256
257 void
258 synfigapp::Main::set_gradient_default_colors()
259 {
260         gradient_default_colors_=true;
261         gradient_=Gradient(foreground_,background_);
262         signal_gradient_changed()();
263 }
264
265 void
266 synfigapp::Main::color_swap()
267 {
268         const Color tmp(foreground_);
269         foreground_=background_;
270         background_=tmp;
271
272         if(selected_input_device_)
273         {
274                 selected_input_device_->set_foreground_color(foreground_);
275                 selected_input_device_->set_background_color(background_);
276         }
277
278         signal_foreground_color_changed()();
279         signal_background_color_changed()();
280
281         if(gradient_default_colors_)
282         {
283                 gradient_=Gradient(foreground_,background_);
284                 signal_gradient_changed()();
285         }
286 }
287
288 synfig::Waypoint::Interpolation
289 synfigapp::Main::get_interpolation()
290 {
291         return interpolation_;
292 }
293
294
295 void
296 synfigapp::Main::set_interpolation(synfig::Waypoint::Interpolation x)
297 {
298         if(interpolation_!=x)
299         {
300                 interpolation_=x;
301                 
302                 signal_interpolation_changed();
303         }
304 }
305
306 const synfig::Distance&
307 synfigapp::Main::get_bline_width()
308 {
309         return bline_width_;
310 }
311
312 void
313 synfigapp::Main::set_bline_width(synfig::Distance x)
314 {
315         if(x<0)x=0;
316         if(x!=bline_width_)
317         {
318                 bline_width_=x;
319                 
320                 if(selected_input_device_)
321                         selected_input_device_->set_bline_width(x);
322                 
323                 signal_bline_width_changed()();
324         }
325 }
326
327 void
328 synfigapp::Main::set_opacity(synfig::Real x)
329 {
330         opacity_=x;
331         if(selected_input_device_)
332                 selected_input_device_->set_opacity(opacity_);
333         signal_opacity_changed()();
334 }
335
336 void
337 synfigapp::Main::set_blend_method(synfig::Color::BlendMethod x)
338 {
339         blend_method_=x;
340         if(selected_input_device_)
341                 selected_input_device_->set_blend_method(x);
342         signal_blend_method_changed()();
343 }
344
345
346 InputDevice::Handle
347 synfigapp::Main::add_input_device(const synfig::String id, InputDevice::Type type)
348 {
349         input_devices_.push_back(new InputDevice(id,type));
350         return input_devices_.back();
351 }
352
353 InputDevice::Handle
354 synfigapp::Main::find_input_device(const synfig::String id)
355 {
356         list<InputDevice::Handle>::iterator iter;
357         for(iter=input_devices_.begin();iter!=input_devices_.end();++iter)
358                 if((*iter)->get_id()==id)
359                         return *iter;
360         return 0;
361 }
362
363 InputDevice::Handle
364 synfigapp::Main::select_input_device(const synfig::String id)
365 {
366         InputDevice::Handle input_device(find_input_device(id));
367         if(!input_device)
368                 return 0;
369         if(!select_input_device(input_device))
370                 return 0;
371         return input_device;
372 }
373
374 bool
375 synfigapp::Main::select_input_device(InputDevice::Handle input_device)
376 {
377         assert(input_device);
378         
379         synfig::info("Input device changed to \"%s\"",input_device->get_id().c_str());
380         
381         selected_input_device_=input_device;
382
383         set_bline_width(input_device->get_bline_width());       
384         set_foreground_color(input_device->get_foreground_color());
385         set_opacity(input_device->get_opacity());       
386         set_blend_method(input_device->get_blend_method());     
387         
388         return true;
389 }
390
391 InputDevice::Handle
392 synfigapp::Main::get_selected_input_device()
393 {
394         return selected_input_device_;  
395 }
396
397 void
398 synfigapp::Main::set_state(synfig::String state)
399 {
400         if(selected_input_device_)
401                 selected_input_device_->set_state(state);
402 }