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