Documentation
[synfig.git] / synfig-studio / src / gui / states / state_text.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_text.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 "state_text.h"
37 #include "state_normal.h"
38 #include "canvasview.h"
39 #include "workarea.h"
40 #include "app.h"
41
42 #include <synfigapp/action.h>
43 #include "event_mouse.h"
44 #include "event_layerclick.h"
45 #include "toolbox.h"
46 #include "docks/dialog_tooloptions.h"
47 #include <gtkmm/optionmenu.h>
48 #include "duck.h"
49 #include "widgets/widget_enum.h"
50 #include <synfigapp/main.h>
51
52 #include "general.h"
53
54 #endif
55
56 /* === U S I N G =========================================================== */
57
58 using namespace std;
59 using namespace etl;
60 using namespace synfig;
61 using namespace studio;
62
63 /* === M A C R O S ========================================================= */
64
65 /* === G L O B A L S ======================================================= */
66
67 StateText studio::state_text;
68
69 /* === C L A S S E S & S T R U C T S ======================================= */
70
71 class studio::StateText_Context
72 {
73         etl::handle<CanvasView> canvas_view;
74         CanvasView::IsWorking is_working;
75
76         Duckmatic::Push duckmatic_push;
77
78         void refresh_ducks();
79
80         bool prev_workarea_layer_status_;
81
82         //Toolbox settings
83         synfigapp::Settings& settings;
84
85         //Toolbox display
86         Gtk::Table options_table;
87
88         Gtk::Entry              entry_id; //what to name the layer
89         Gtk::Entry              entry_family;
90         Widget_Vector   widget_size;
91         Widget_Vector   widget_orientation;
92         Gtk::CheckButton checkbutton_paragraph;
93
94 public:
95         synfig::String get_id()const { return entry_id.get_text(); }
96         void set_id(const synfig::String& x) { return entry_id.set_text(x); }
97
98         bool get_paragraph_flag()const { return checkbutton_paragraph.get_active(); }
99         void set_paragraph_flag(bool x) { return checkbutton_paragraph.set_active(x); }
100
101         Vector get_size() { return widget_size.get_value(); }
102         void set_size(Vector s) { return widget_size.set_value(s); }
103
104         Vector get_orientation() { return widget_orientation.get_value(); }
105         void set_orientation(Vector s) { return widget_orientation.set_value(s); }
106
107         String get_family()const { return entry_family.get_text(); }
108         void set_family(String s) { return entry_family.set_text(s); }
109
110         void refresh_tool_options(); //to refresh the toolbox
111
112         //events
113         Smach::event_result event_stop_handler(const Smach::event& x);
114         Smach::event_result event_refresh_handler(const Smach::event& x);
115         Smach::event_result event_mouse_click_handler(const Smach::event& x);
116         Smach::event_result event_refresh_tool_options(const Smach::event& x);
117         Smach::event_result event_workarea_mouse_button_down_handler(const Smach::event& x);
118
119         //constructor destructor
120         StateText_Context(CanvasView *canvas_view);
121         ~StateText_Context();
122
123         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view;}
124         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view->canvas_interface();}
125         WorkArea * get_work_area()const{return canvas_view->get_work_area();}
126
127         //Modifying settings etc.
128         void load_settings();
129         void save_settings();
130         void reset();
131         void increment_id();
132         bool egress_on_selection_change;
133         Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
134         {
135                 if(egress_on_selection_change)
136                         throw &state_normal; //throw Smach::egress_exception();
137                 return Smach::RESULT_OK;
138         }
139
140         void make_text(const Point& point);
141
142 }; // END of class StateText_Context
143
144 /* === P R O C E D U R E S ================================================= */
145
146 /* === M E T H O D S ======================================================= */
147
148 StateText::StateText():
149         Smach::state<StateText_Context>("text")
150 {
151         insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StateText_Context::event_layer_selection_changed_handler));
152         insert(event_def(EVENT_STOP,&StateText_Context::event_stop_handler));
153         insert(event_def(EVENT_REFRESH,&StateText_Context::event_refresh_handler));
154         insert(event_def(EVENT_REFRESH_DUCKS,&StateText_Context::event_refresh_handler));
155         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateText_Context::event_workarea_mouse_button_down_handler));
156         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateText_Context::event_refresh_tool_options));
157 }
158
159 StateText::~StateText()
160 {
161 }
162
163 void
164 StateText_Context::load_settings()
165 {
166         String value;
167         Vector v;
168
169         //parse the arguments yargh!
170         if(settings.get_value("text.id",value))
171                 set_id(value);
172         else
173                 set_id("Text");
174
175         if(settings.get_value("text.paragraph",value) && value=="1")
176                 set_paragraph_flag(true);
177         else
178                 set_paragraph_flag(false);
179
180         if(settings.get_value("text.size_x",value))
181                 v[0] = atof(value.c_str());
182         else
183                 v[0] = 0.25;
184         if(settings.get_value("text.size_y",value))
185                 v[1] = atof(value.c_str());
186         else
187                 v[1] = 0.25;
188         set_size(v);
189
190         if(settings.get_value("text.orient_x",value))
191                 v[0] = atof(value.c_str());
192         else
193                 v[0] = 0.5;
194         if(settings.get_value("text.orient_y",value))
195                 v[1] = atof(value.c_str());
196         else
197                 v[1] = 0.5;
198         set_orientation(v);
199
200         if(settings.get_value("text.family",value))
201                 set_family(value);
202         else
203                 set_family("Sans Serif");
204 }
205
206 void
207 StateText_Context::save_settings()
208 {
209         settings.set_value("text.id",get_id());
210         settings.set_value("text.paragraph",get_paragraph_flag()?"1":"0");
211         settings.set_value("text.size_x",strprintf("%f",(float)get_size()[0]));
212         settings.set_value("text.size_y",strprintf("%f",(float)get_size()[1]));
213         settings.set_value("text.orient_x",strprintf("%f",(float)get_orientation()[0]));
214         settings.set_value("text.orient_y",strprintf("%f",(float)get_orientation()[1]));
215         settings.set_value("text.family",get_family());
216 }
217
218 void
219 StateText_Context::reset()
220 {
221         refresh_ducks();
222 }
223
224 void
225 StateText_Context::increment_id()
226 {
227         String id(get_id());
228         int number=1;
229         int digits=0;
230
231         if(id.empty())
232                 id="Text";
233
234         // If there is a number
235         // already at the end of the
236         // id, then remove it.
237         if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
238         {
239                 // figure out how many digits it is
240                 for (digits = 0;
241                          (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
242                          digits++)
243                         ;
244
245                 String str_number;
246                 str_number=String(id,id.size()-digits,id.size());
247                 id=String(id,0,id.size()-digits);
248
249                 number=atoi(str_number.c_str());
250         }
251         else
252         {
253                 number=1;
254                 digits=3;
255         }
256
257         number++;
258
259         // Add the number back onto the id
260         {
261                 const String format(strprintf("%%0%dd",digits));
262                 id+=strprintf(format.c_str(),number);
263         }
264
265         // Set the ID
266         set_id(id);
267 }
268
269 StateText_Context::StateText_Context(CanvasView *canvas_view):
270         canvas_view(canvas_view),
271         is_working(*canvas_view),
272         duckmatic_push(get_work_area()),
273         prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
274         settings(synfigapp::Main::get_selected_input_device()->settings()),
275         entry_id(),
276         checkbutton_paragraph(_("Multiline Editor"))
277 {
278         egress_on_selection_change=true;
279
280         widget_size.set_digits(2);
281         widget_size.set_canvas(canvas_view->get_canvas());
282
283         widget_orientation.set_digits(2);
284
285         options_table.attach(*manage(new Gtk::Label(_("Text Tool"))),           0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
286         options_table.attach(entry_id,                                                                          0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
287         options_table.attach(checkbutton_paragraph,                                                     0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
288         options_table.attach(*manage(new Gtk::Label(_("Size:"))),                       0, 1, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
289         options_table.attach(widget_size,                                                                       1, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
290         options_table.attach(*manage(new Gtk::Label(_("Orientation:"))),        0, 1, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
291         options_table.attach(widget_orientation,                                                        1, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
292         options_table.attach(*manage(new Gtk::Label(_("Family:"))),                     0, 1, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
293         options_table.attach(entry_family,                                                                      1, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
294
295         load_settings();
296
297         options_table.show_all();
298
299         refresh_tool_options();
300         App::dialog_tool_options->present();
301
302         // Turn off layer clicking
303         get_work_area()->set_allow_layer_clicks(false);
304
305         // clear out the ducks
306         get_work_area()->clear_ducks();
307
308         // Refresh the work area
309         get_work_area()->queue_draw();
310
311         // Hide the tables if they are showing
312         //prev_table_status=get_canvas_view()->tables_are_visible();
313         //if(prev_table_status)get_canvas_view()->hide_tables();
314
315         // Disable the time bar
316         //get_canvas_view()->set_sensitive_timebar(false);
317
318         // Connect a signal
319         //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateText_Context::on_user_click));
320         get_work_area()->set_cursor(Gdk::XTERM);
321
322         App::toolbox->refresh();
323 }
324
325 void
326 StateText_Context::refresh_tool_options()
327 {
328         App::dialog_tool_options->clear();
329         App::dialog_tool_options->set_widget(options_table);
330         App::dialog_tool_options->set_local_name(_("Text Tool"));
331         App::dialog_tool_options->set_name("text");
332 }
333
334 Smach::event_result
335 StateText_Context::event_refresh_tool_options(const Smach::event& /*x*/)
336 {
337         refresh_tool_options();
338         return Smach::RESULT_ACCEPT;
339 }
340
341 StateText_Context::~StateText_Context()
342 {
343         save_settings();
344
345         // Restore layer clicking
346         get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
347         get_work_area()->reset_cursor();
348
349         App::dialog_tool_options->clear();
350
351         get_work_area()->queue_draw();
352
353         get_canvas_view()->queue_rebuild_ducks();
354
355         App::toolbox->refresh();
356 }
357
358 Smach::event_result
359 StateText_Context::event_stop_handler(const Smach::event& /*x*/)
360 {
361         //throw Smach::egress_exception();
362         throw &state_normal;
363         return Smach::RESULT_OK;
364 }
365
366 Smach::event_result
367 StateText_Context::event_refresh_handler(const Smach::event& /*x*/)
368 {
369         refresh_ducks();
370         return Smach::RESULT_ACCEPT;
371 }
372
373 void
374 StateText_Context::make_text(const Point& _point)
375 {
376         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Text"));
377         synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
378
379         Layer::Handle layer;
380
381         Canvas::Handle canvas(get_canvas_view()->get_canvas());
382         int depth(0);
383
384         // we are temporarily using the layer to hold something
385         layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
386         if(layer)
387         {
388                 depth=layer->get_depth();
389                 canvas=layer->get_canvas();
390         }
391
392         synfigapp::SelectionManager::LayerList layer_selection;
393         if (!getenv("SYNFIG_TOOLS_CLEAR_SELECTION"))
394                 layer_selection = get_canvas_view()->get_selection_manager()->get_selected_layers();
395
396         const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
397         const Point point(transform.unperform(_point));
398
399         String text;
400         if (get_paragraph_flag())
401                 App::dialog_paragraph(_("Text Paragraph"), _("Enter text here:"), text);
402         else
403                 App::dialog_entry(_("Text Entry"), _("Enter text here:"), text);
404
405         layer=get_canvas_interface()->add_layer_to("text",canvas,depth);
406         if (!layer)
407         {
408                 get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
409                 group.cancel();
410                 return;
411         }
412         layer_selection.push_back(layer);
413
414         layer->set_param("origin",point);
415         get_canvas_interface()->signal_layer_param_changed()(layer,"origin");
416
417         layer->set_param("text",text);
418         get_canvas_interface()->signal_layer_param_changed()(layer,"text");
419
420         layer->set_param("size",get_size());
421         get_canvas_interface()->signal_layer_param_changed()(layer,"size");
422
423         layer->set_param("orient",get_orientation());
424         get_canvas_interface()->signal_layer_param_changed()(layer,"orient");
425
426         layer->set_param("family",get_family());
427         get_canvas_interface()->signal_layer_param_changed()(layer,"family");
428
429         layer->set_description(get_id());
430         get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
431
432         egress_on_selection_change=false;
433         get_canvas_interface()->get_selection_manager()->clear_selected_layers();
434         get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
435         egress_on_selection_change=true;
436
437         reset();
438         increment_id();
439 }
440
441 Smach::event_result
442 StateText_Context::event_workarea_mouse_button_down_handler(const Smach::event& x)
443 {
444         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
445         if(event.button==BUTTON_LEFT)
446         {
447                 make_text(get_work_area()->snap_point_to_grid(event.pos));
448
449                 get_work_area()->clear_ducks();
450                 return Smach::RESULT_ACCEPT;
451         }
452         return Smach::RESULT_OK;
453 }
454
455 void
456 StateText_Context::refresh_ducks()
457 {
458         get_work_area()->clear_ducks();
459         get_work_area()->queue_draw();
460 }