Extract translatable strings from some more files
[synfig.git] / synfig-studio / trunk / src / gtkmm / state_gradient.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_gradient.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 <gtkmm/dialog.h>
33 #include <gtkmm/entry.h>
34
35 #include <synfig/valuenode_dynamiclist.h>
36 #include <synfigapp/action_system.h>
37
38 #include "state_gradient.h"
39 #include "canvasview.h"
40 #include "workarea.h"
41 #include "app.h"
42
43 #include <synfigapp/action.h>
44 #include "event_mouse.h"
45 #include "event_layerclick.h"
46 #include "toolbox.h"
47 #include "dialog_tooloptions.h"
48 #include <gtkmm/optionmenu.h>
49 #include "duck.h"
50
51 #include "widget_enum.h"
52 #include <synfigapp/main.h>
53
54 #include "general.h"
55
56 #endif
57
58 /* === U S I N G =========================================================== */
59
60 using namespace std;
61 using namespace etl;
62 using namespace synfig;
63 using namespace studio;
64
65 /* === M A C R O S ========================================================= */
66
67 enum
68 {
69         GRADIENT_INTERPOLATION_LINEAR=0,
70         GRADIENT_RADIAL=1,
71         GRADIENT_CONICAL=2,
72         GRADIENT_SPIRAL=3
73 };
74
75 /* === G L O B A L S ======================================================= */
76
77 StateGradient studio::state_gradient;
78
79 /* === C L A S S E S & S T R U C T S ======================================= */
80
81 class studio::StateGradient_Context : public sigc::trackable
82 {
83         etl::handle<CanvasView> canvas_view_;
84         CanvasView::IsWorking is_working;
85
86         Duckmatic::Push duckmatic_push;
87
88         synfigapp::Settings& settings;
89
90         Point point_holder;
91
92         etl::handle<Duck> point2_duck;
93
94         void refresh_ducks();
95
96         bool prev_workarea_layer_status_;
97
98         Gtk::Table options_table;
99         Gtk::Entry entry_id;
100         Widget_Enum enum_type;
101 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
102         Widget_Enum     enum_blend;
103 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
104
105 public:
106         synfig::String get_id()const { return entry_id.get_text(); }
107         void set_id(const synfig::String& x) { return entry_id.set_text(x); }
108
109         int get_type()const { return enum_type.get_value(); }
110         void set_type(int x) { return enum_type.set_value(x); }
111
112 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
113         int get_blend()const { return enum_blend.get_value(); }
114         void set_blend(int x) { return enum_blend.set_value(x); }
115 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
116
117         Smach::event_result event_stop_handler(const Smach::event& x);
118
119         Smach::event_result event_refresh_handler(const Smach::event& x);
120
121         Smach::event_result event_mouse_click_handler(const Smach::event& x);
122         Smach::event_result event_refresh_tool_options(const Smach::event& x);
123
124         void refresh_tool_options();
125
126         StateGradient_Context(CanvasView* canvas_view);
127
128         ~StateGradient_Context();
129
130         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
131         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
132         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
133         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
134
135         //void on_user_click(synfig::Point point);
136         void load_settings();
137         void save_settings();
138         void reset();
139         void increment_id();
140
141         void make_gradient(const Point& p1, const Point& p2);
142         bool egress_on_selection_change;
143         Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
144         {
145                 if(egress_on_selection_change)
146                         throw Smach::egress_exception();
147                 return Smach::RESULT_OK;
148         }
149
150 };      // END of class StateGradient_Context
151
152 /* === M E T H O D S ======================================================= */
153
154 StateGradient::StateGradient():
155         Smach::state<StateGradient_Context>("gradient")
156 {
157         insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StateGradient_Context::event_layer_selection_changed_handler));
158         insert(event_def(EVENT_STOP,&StateGradient_Context::event_stop_handler));
159         insert(event_def(EVENT_TABLES_SHOW,&StateGradient_Context::event_stop_handler));
160         insert(event_def(EVENT_REFRESH,&StateGradient_Context::event_refresh_handler));
161         insert(event_def(EVENT_REFRESH_DUCKS,&StateGradient_Context::event_refresh_handler));
162         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateGradient_Context::event_mouse_click_handler));
163         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateGradient_Context::event_mouse_click_handler));
164         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateGradient_Context::event_mouse_click_handler));
165         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateGradient_Context::event_refresh_tool_options));
166 }
167
168 StateGradient::~StateGradient()
169 {
170 }
171
172 void
173 StateGradient_Context::load_settings()
174 {
175         String value;
176
177         if(settings.get_value("gradient.id",value))
178                 set_id(value);
179         else
180                 set_id("Gradient");
181
182         if(settings.get_value("gradient.type",value))
183                 set_type(atoi(value.c_str()));
184         else
185                 set_type(GRADIENT_INTERPOLATION_LINEAR);
186
187 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
188         if(settings.get_value("gradient.blend",value))
189                 set_blend(atoi(value.c_str()));
190         else
191                 set_blend(Color::BLEND_COMPOSITE);
192 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
193 }
194
195 void
196 StateGradient_Context::save_settings()
197 {
198         settings.set_value("gradient.id",get_id().c_str());
199         settings.set_value("gradient.type",strprintf("%d",get_type()));
200 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
201         settings.set_value("gradient.blend",strprintf("%d",get_blend()));
202 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
203 }
204
205 void
206 StateGradient_Context::reset()
207 {
208         refresh_ducks();
209 }
210
211 void
212 StateGradient_Context::increment_id()
213 {
214         String id(get_id());
215         int number=1;
216         int digits=0;
217
218         if(id.empty())
219                 id="Gradient";
220
221         // If there is a number
222         // already at the end of the
223         // id, then remove it.
224         if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
225         {
226                 // figure out how many digits it is
227                 for (digits = 0;
228                          (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
229                          digits++)
230                         ;
231
232                 String str_number;
233                 str_number=String(id,id.size()-digits,id.size());
234                 id=String(id,0,id.size()-digits);
235
236                 number=atoi(str_number.c_str());
237         }
238         else
239         {
240                 number=1;
241                 digits=3;
242         }
243
244         number++;
245
246         // Add the number back onto the id
247         {
248                 const String format(strprintf("%%0%dd",digits));
249                 id+=strprintf(format.c_str(),number);
250         }
251
252         // Set the ID
253         set_id(id);
254 }
255
256 StateGradient_Context::StateGradient_Context(CanvasView* canvas_view):
257         canvas_view_(canvas_view),
258         is_working(*canvas_view),
259         duckmatic_push(get_work_area()),
260         settings(synfigapp::Main::get_selected_input_device()->settings()),
261         prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
262         entry_id()
263 {
264         egress_on_selection_change=true;
265         // Set up the tool options dialog
266         options_table.attach(*manage(new Gtk::Label(_("Gradient Tool"))),       0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
267         options_table.attach(entry_id,                                                                          0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
268
269         enum_type.set_param_desc(ParamDesc("type")
270                 .set_local_name(_("Gradient Type"))
271                 .set_description(_("Determines the type of Gradient used"))
272                 .set_hint("enum")
273                 .add_enum_value(GRADIENT_INTERPOLATION_LINEAR,"linear",_("Linear"))
274                 .add_enum_value(GRADIENT_RADIAL,"radial",_("Radial"))
275                 .add_enum_value(GRADIENT_CONICAL,"conical",_("Conical"))
276                 .add_enum_value(GRADIENT_SPIRAL,"spiral",_("Spiral")));
277
278 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
279         enum_blend.set_param_desc(ParamDesc(Color::BLEND_COMPOSITE,"blend_method")
280                 .set_local_name(_("Blend Method"))
281                 .set_description(_("The blend method the gradient will use")));
282 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
283
284         load_settings();
285
286         options_table.attach(enum_type, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
287 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
288         options_table.attach(enum_blend, 0, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
289 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
290
291         options_table.show_all();
292         refresh_tool_options();
293         App::dialog_tool_options->present();
294
295
296         // Turn off layer clicking
297         get_work_area()->set_allow_layer_clicks(false);
298
299         get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
300
301         // clear out the ducks
302         get_work_area()->clear_ducks();
303
304         // Refresh the work area
305         get_work_area()->queue_draw();
306
307         get_work_area()->refresh_cursor();
308
309         // Hide the tables if they are showing
310         get_canvas_view()->hide_tables();
311
312         // Disable the time bar
313         //get_canvas_view()->set_sensitive_timebar(false);
314
315         // Connect a signal
316         //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateGradient_Context::on_user_click));
317
318         App::toolbox->refresh();
319 }
320
321 void
322 StateGradient_Context::refresh_tool_options()
323 {
324         App::dialog_tool_options->clear();
325         App::dialog_tool_options->set_widget(options_table);
326         App::dialog_tool_options->set_local_name(_("Gradient Tool"));
327         App::dialog_tool_options->set_name("gradient");
328 }
329
330 Smach::event_result
331 StateGradient_Context::event_refresh_tool_options(const Smach::event& /*x*/)
332 {
333         refresh_tool_options();
334         return Smach::RESULT_ACCEPT;
335 }
336
337 StateGradient_Context::~StateGradient_Context()
338 {
339         save_settings();
340
341         // Restore layer clicking
342 //      get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
343         get_work_area()->set_allow_layer_clicks(true);
344         get_canvas_view()->work_area->reset_cursor();
345
346         App::dialog_tool_options->clear();
347
348         // Enable the time bar
349         //get_canvas_view()->set_sensitive_timebar(true);
350
351         // Bring back the tables if they were out before
352         //if(prev_table_status)get_canvas_view()->show_tables();
353
354         // Refresh the work area
355         get_work_area()->queue_draw();
356
357         get_canvas_view()->queue_rebuild_ducks();
358
359         //get_canvas_view()->show_tables();
360
361         get_work_area()->refresh_cursor();
362
363         App::toolbox->refresh();
364 }
365
366 Smach::event_result
367 StateGradient_Context::event_stop_handler(const Smach::event& /*x*/)
368 {
369         throw Smach::egress_exception();
370 }
371
372 Smach::event_result
373 StateGradient_Context::event_refresh_handler(const Smach::event& /*x*/)
374 {
375         refresh_ducks();
376         return Smach::RESULT_ACCEPT;
377 }
378
379 void
380 StateGradient_Context::make_gradient(const Point& _p1, const Point& _p2)
381 {
382         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Gradient"));
383         synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
384
385         Layer::Handle layer;
386
387         Canvas::Handle canvas(get_canvas_view()->get_canvas());
388         int depth(0);
389
390         // we are temporarily using the layer to hold something
391         layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
392         if(layer)
393         {
394                 depth=layer->get_depth();
395                 canvas=layer->get_canvas();
396         }
397         const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
398         const Point p1(transform.unperform(_p1));
399         const Point p2(transform.unperform(_p2));
400
401         switch(get_type())
402         {
403         case GRADIENT_INTERPOLATION_LINEAR:
404
405                 layer=get_canvas_interface()->add_layer_to("linear_gradient",canvas,depth);
406                 layer->set_param("p1",p1);
407                 get_canvas_interface()->signal_layer_param_changed()(layer,"p1");
408                 layer->set_param("p2",p2);
409                 get_canvas_interface()->signal_layer_param_changed()(layer,"p2");
410                 break;
411         case GRADIENT_RADIAL:
412                 layer=get_canvas_interface()->add_layer_to("radial_gradient",canvas,depth);
413                 layer->set_param("center",p1);
414                 get_canvas_interface()->signal_layer_param_changed()(layer,"center");
415                 layer->set_param("radius",(p2-p1).mag());
416                 get_canvas_interface()->signal_layer_param_changed()(layer,"radius");
417                 break;
418         case GRADIENT_CONICAL:
419                 layer=get_canvas_interface()->add_layer_to("conical_gradient",canvas,depth);
420                 layer->set_param("center",p1);
421                 get_canvas_interface()->signal_layer_param_changed()(layer,"center");
422                 {
423                         Vector diff(p2-p1);
424                         layer->set_param("angle",Angle::tan(diff[1],diff[0]));
425                         get_canvas_interface()->signal_layer_param_changed()(layer,"angle");
426                 }
427                 break;
428         case GRADIENT_SPIRAL:
429                 layer=get_canvas_interface()->add_layer_to("spiral_gradient",canvas,depth);
430                 layer->set_param("center",p1);
431                 get_canvas_interface()->signal_layer_param_changed()(layer,"center");
432                 layer->set_param("radius",(p2-p1).mag());
433                 get_canvas_interface()->signal_layer_param_changed()(layer,"radius");
434                 {
435                         Vector diff(p2-p1);
436                         layer->set_param("angle",Angle::tan(diff[1],diff[0]));
437                         get_canvas_interface()->signal_layer_param_changed()(layer,"angle");
438                 }
439                 break;
440
441         default:
442                 return;
443         }
444
445 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
446         layer->set_param("blend_method",get_blend());
447         get_canvas_interface()->signal_layer_param_changed()(layer,"blend_method");
448 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
449
450         layer->set_description(get_id());
451         get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
452
453         egress_on_selection_change=false;
454         synfigapp::SelectionManager::LayerList layer_selection;
455         if (!getenv("SYNFIG_TOOLS_CLEAR_SELECTION"))
456                 layer_selection = get_canvas_view()->get_selection_manager()->get_selected_layers();
457         get_canvas_interface()->get_selection_manager()->clear_selected_layers();
458         layer_selection.push_back(layer);
459         get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
460         egress_on_selection_change=true;
461
462         reset();
463         increment_id();
464 }
465
466 Smach::event_result
467 StateGradient_Context::event_mouse_click_handler(const Smach::event& x)
468 {
469         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
470
471         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DOWN && event.button==BUTTON_LEFT)
472         {
473                 point_holder=get_work_area()->snap_point_to_grid(event.pos);
474                 etl::handle<Duck> duck=new Duck();
475                 duck->set_point(point_holder);
476                 duck->set_name("p1");
477                 duck->set_type(Duck::TYPE_POSITION);
478                 get_work_area()->add_duck(duck);
479
480                 point2_duck=new Duck();
481                 point2_duck->set_point(point_holder);
482                 point2_duck->set_name("p2");
483                 point2_duck->set_type(Duck::TYPE_POSITION);
484                 get_work_area()->add_duck(point2_duck);
485
486                 handle<Duckmatic::Bezier> bezier(new Duckmatic::Bezier());
487                 bezier->p1=bezier->c1=duck;
488                 bezier->p2=bezier->c2=point2_duck;
489                 get_work_area()->add_bezier(bezier);
490
491                 return Smach::RESULT_ACCEPT;
492         }
493
494         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DRAG && event.button==BUTTON_LEFT)
495         {
496                 point2_duck->set_point(get_work_area()->snap_point_to_grid(event.pos));
497                 get_work_area()->queue_draw();
498                 return Smach::RESULT_ACCEPT;
499         }
500
501         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_UP && event.button==BUTTON_LEFT)
502         {
503                 make_gradient(point_holder, get_work_area()->snap_point_to_grid(event.pos));
504                 get_work_area()->clear_ducks();
505                 return Smach::RESULT_ACCEPT;
506         }
507
508         return Smach::RESULT_OK;
509 }
510
511
512 void
513 StateGradient_Context::refresh_ducks()
514 {
515         get_work_area()->clear_ducks();
516         get_work_area()->queue_draw();
517 }