1 /* === S Y N F I G ========================================================= */
2 /*! \file state_text.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2008 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include <gtkmm/dialog.h>
34 #include <gtkmm/entry.h>
36 #include "state_text.h"
37 #include "state_normal.h"
38 #include "canvasview.h"
42 #include <synfigapp/action.h>
43 #include "event_mouse.h"
44 #include "event_layerclick.h"
46 #include "docks/dialog_tooloptions.h"
47 #include <gtkmm/optionmenu.h>
49 #include "widgets/widget_enum.h"
50 #include <synfigapp/main.h>
56 /* === U S I N G =========================================================== */
60 using namespace synfig;
61 using namespace studio;
63 /* === M A C R O S ========================================================= */
65 /* === G L O B A L S ======================================================= */
67 StateText studio::state_text;
69 /* === C L A S S E S & S T R U C T S ======================================= */
71 class studio::StateText_Context
73 etl::handle<CanvasView> canvas_view;
74 CanvasView::IsWorking is_working;
76 Duckmatic::Push duckmatic_push;
80 bool prev_workarea_layer_status_;
83 synfigapp::Settings& settings;
86 Gtk::Table options_table;
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;
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); }
98 bool get_paragraph_flag()const { return checkbutton_paragraph.get_active(); }
99 void set_paragraph_flag(bool x) { return checkbutton_paragraph.set_active(x); }
101 Vector get_size() { return widget_size.get_value(); }
102 void set_size(Vector s) { return widget_size.set_value(s); }
104 Vector get_orientation() { return widget_orientation.get_value(); }
105 void set_orientation(Vector s) { return widget_orientation.set_value(s); }
107 String get_family()const { return entry_family.get_text(); }
108 void set_family(String s) { return entry_family.set_text(s); }
110 void refresh_tool_options(); //to refresh the toolbox
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);
119 //constructor destructor
120 StateText_Context(CanvasView *canvas_view);
121 ~StateText_Context();
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();}
127 //Modifying settings etc.
128 void load_settings();
129 void save_settings();
132 bool egress_on_selection_change;
133 Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
135 if(egress_on_selection_change)
136 throw &state_normal; //throw Smach::egress_exception();
137 return Smach::RESULT_OK;
140 void make_text(const Point& point);
142 }; // END of class StateText_Context
144 /* === P R O C E D U R E S ================================================= */
146 /* === M E T H O D S ======================================================= */
148 StateText::StateText():
149 Smach::state<StateText_Context>("text")
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));
159 StateText::~StateText()
164 StateText_Context::load_settings()
168 synfig::ChangeLocale change_locale(LC_NUMERIC, "C");
172 //parse the arguments yargh!
173 if(settings.get_value("text.id",value))
178 if(settings.get_value("text.paragraph",value) && value=="1")
179 set_paragraph_flag(true);
181 set_paragraph_flag(false);
183 if(settings.get_value("text.size_x",value))
184 v[0] = atof(value.c_str());
187 if(settings.get_value("text.size_y",value))
188 v[1] = atof(value.c_str());
193 if(settings.get_value("text.orient_x",value))
194 v[0] = atof(value.c_str());
197 if(settings.get_value("text.orient_y",value))
198 v[1] = atof(value.c_str());
203 if(settings.get_value("text.family",value))
206 set_family("Sans Serif");
210 synfig::warning("State Text: Caught exception when attempting to load settings.");
215 StateText_Context::save_settings()
219 synfig::ChangeLocale change_locale(LC_NUMERIC, "C");
220 settings.set_value("text.id",get_id());
221 settings.set_value("text.paragraph",get_paragraph_flag()?"1":"0");
222 settings.set_value("text.size_x",strprintf("%f",(float)get_size()[0]));
223 settings.set_value("text.size_y",strprintf("%f",(float)get_size()[1]));
224 settings.set_value("text.orient_x",strprintf("%f",(float)get_orientation()[0]));
225 settings.set_value("text.orient_y",strprintf("%f",(float)get_orientation()[1]));
226 settings.set_value("text.family",get_family());
230 synfig::warning("State Text: Caught exception when attempting to save settings.");
235 StateText_Context::reset()
241 StateText_Context::increment_id()
250 // If there is a number
251 // already at the end of the
252 // id, then remove it.
253 if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
255 // figure out how many digits it is
257 (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
262 str_number=String(id,id.size()-digits,id.size());
263 id=String(id,0,id.size()-digits);
265 number=atoi(str_number.c_str());
275 // Add the number back onto the id
277 const String format(strprintf("%%0%dd",digits));
278 id+=strprintf(format.c_str(),number);
285 StateText_Context::StateText_Context(CanvasView *canvas_view):
286 canvas_view(canvas_view),
287 is_working(*canvas_view),
288 duckmatic_push(get_work_area()),
289 prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
290 settings(synfigapp::Main::get_selected_input_device()->settings()),
292 checkbutton_paragraph(_("Multiline Editor"))
294 egress_on_selection_change=true;
296 widget_size.set_digits(2);
297 widget_size.set_canvas(canvas_view->get_canvas());
299 widget_orientation.set_digits(2);
301 options_table.attach(*manage(new Gtk::Label(_("Text Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
302 options_table.attach(entry_id, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
303 options_table.attach(checkbutton_paragraph, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
304 options_table.attach(*manage(new Gtk::Label(_("Size:"))), 0, 1, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
305 options_table.attach(widget_size, 1, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
306 options_table.attach(*manage(new Gtk::Label(_("Orientation:"))), 0, 1, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
307 options_table.attach(widget_orientation, 1, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
308 options_table.attach(*manage(new Gtk::Label(_("Family:"))), 0, 1, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
309 options_table.attach(entry_family, 1, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
313 options_table.show_all();
315 refresh_tool_options();
316 App::dialog_tool_options->present();
318 // Turn off layer clicking
319 get_work_area()->set_allow_layer_clicks(false);
321 // clear out the ducks
322 get_work_area()->clear_ducks();
324 // Refresh the work area
325 get_work_area()->queue_draw();
327 // Hide the tables if they are showing
328 //prev_table_status=get_canvas_view()->tables_are_visible();
329 //if(prev_table_status)get_canvas_view()->hide_tables();
331 // Disable the time bar
332 //get_canvas_view()->set_sensitive_timebar(false);
335 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateText_Context::on_user_click));
336 get_work_area()->set_cursor(Gdk::XTERM);
338 App::toolbox->refresh();
342 StateText_Context::refresh_tool_options()
344 App::dialog_tool_options->clear();
345 App::dialog_tool_options->set_widget(options_table);
346 App::dialog_tool_options->set_local_name(_("Text Tool"));
347 App::dialog_tool_options->set_name("text");
351 StateText_Context::event_refresh_tool_options(const Smach::event& /*x*/)
353 refresh_tool_options();
354 return Smach::RESULT_ACCEPT;
357 StateText_Context::~StateText_Context()
361 // Restore layer clicking
362 get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
363 get_work_area()->reset_cursor();
365 App::dialog_tool_options->clear();
367 get_work_area()->queue_draw();
369 get_canvas_view()->queue_rebuild_ducks();
371 App::toolbox->refresh();
375 StateText_Context::event_stop_handler(const Smach::event& /*x*/)
377 //throw Smach::egress_exception();
379 return Smach::RESULT_OK;
383 StateText_Context::event_refresh_handler(const Smach::event& /*x*/)
386 return Smach::RESULT_ACCEPT;
390 StateText_Context::make_text(const Point& _point)
392 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Text"));
393 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
397 Canvas::Handle canvas(get_canvas_view()->get_canvas());
400 // we are temporarily using the layer to hold something
401 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
404 depth=layer->get_depth();
405 canvas=layer->get_canvas();
408 synfigapp::SelectionManager::LayerList layer_selection;
409 if (!getenv("SYNFIG_TOOLS_CLEAR_SELECTION"))
410 layer_selection = get_canvas_view()->get_selection_manager()->get_selected_layers();
412 const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
413 const Point point(transform.unperform(_point));
416 if (get_paragraph_flag())
417 App::dialog_paragraph(_("Text Paragraph"), _("Enter text here:"), text);
419 App::dialog_entry(_("Text Entry"), _("Enter text here:"), text);
421 layer=get_canvas_interface()->add_layer_to("text",canvas,depth);
424 get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
428 layer_selection.push_back(layer);
430 layer->set_param("origin",point);
431 get_canvas_interface()->signal_layer_param_changed()(layer,"origin");
433 layer->set_param("text",text);
434 get_canvas_interface()->signal_layer_param_changed()(layer,"text");
436 layer->set_param("size",get_size());
437 get_canvas_interface()->signal_layer_param_changed()(layer,"size");
439 layer->set_param("orient",get_orientation());
440 get_canvas_interface()->signal_layer_param_changed()(layer,"orient");
442 layer->set_param("family",get_family());
443 get_canvas_interface()->signal_layer_param_changed()(layer,"family");
445 layer->set_description(get_id());
446 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
448 egress_on_selection_change=false;
449 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
450 get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
451 egress_on_selection_change=true;
458 StateText_Context::event_workarea_mouse_button_down_handler(const Smach::event& x)
460 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
461 if(event.button==BUTTON_LEFT)
463 make_text(get_work_area()->snap_point_to_grid(event.pos));
465 get_work_area()->clear_ducks();
466 return Smach::RESULT_ACCEPT;
468 return Smach::RESULT_OK;
472 StateText_Context::refresh_ducks()
474 get_work_area()->clear_ducks();
475 get_work_area()->queue_draw();