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