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