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 "dialog_tooloptions.h"
47 #include <gtkmm/optionmenu.h>
49 #include "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()
169 //parse the arguments yargh!
170 if(settings.get_value("text.id",value))
175 if(settings.get_value("text.paragraph",value) && value=="1")
176 set_paragraph_flag(true);
178 set_paragraph_flag(false);
180 if(settings.get_value("text.size_x",value))
181 v[0] = atof(value.c_str());
184 if(settings.get_value("text.size_y",value))
185 v[1] = atof(value.c_str());
190 if(settings.get_value("text.orient_x",value))
191 v[0] = atof(value.c_str());
194 if(settings.get_value("text.orient_y",value))
195 v[1] = atof(value.c_str());
200 if(settings.get_value("text.family",value))
203 set_family("Sans Serif");
207 StateText_Context::save_settings()
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());
219 StateText_Context::reset()
225 StateText_Context::increment_id()
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')
239 // figure out how many digits it is
241 (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
246 str_number=String(id,id.size()-digits,id.size());
247 id=String(id,0,id.size()-digits);
249 number=atoi(str_number.c_str());
259 // Add the number back onto the id
261 const String format(strprintf("%%0%dd",digits));
262 id+=strprintf(format.c_str(),number);
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()),
276 checkbutton_paragraph(_("Multiline Editor"))
278 egress_on_selection_change=true;
280 widget_size.set_digits(2);
281 widget_size.set_canvas(canvas_view->get_canvas());
283 widget_orientation.set_digits(2);
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);
297 options_table.show_all();
299 refresh_tool_options();
300 App::dialog_tool_options->present();
302 // Turn off layer clicking
303 get_work_area()->set_allow_layer_clicks(false);
305 // clear out the ducks
306 get_work_area()->clear_ducks();
308 // Refresh the work area
309 get_work_area()->queue_draw();
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();
315 // Disable the time bar
316 //get_canvas_view()->set_sensitive_timebar(false);
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);
322 App::toolbox->refresh();
326 StateText_Context::refresh_tool_options()
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");
335 StateText_Context::event_refresh_tool_options(const Smach::event& /*x*/)
337 refresh_tool_options();
338 return Smach::RESULT_ACCEPT;
341 StateText_Context::~StateText_Context()
345 // Restore layer clicking
346 get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
347 get_work_area()->reset_cursor();
349 App::dialog_tool_options->clear();
351 get_work_area()->queue_draw();
353 get_canvas_view()->queue_rebuild_ducks();
355 App::toolbox->refresh();
359 StateText_Context::event_stop_handler(const Smach::event& /*x*/)
361 //throw Smach::egress_exception();
363 return Smach::RESULT_OK;
367 StateText_Context::event_refresh_handler(const Smach::event& /*x*/)
370 return Smach::RESULT_ACCEPT;
374 StateText_Context::make_text(const Point& _point)
376 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Text"));
377 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
381 Canvas::Handle canvas(get_canvas_view()->get_canvas());
384 // we are temporarily using the layer to hold something
385 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
388 depth=layer->get_depth();
389 canvas=layer->get_canvas();
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();
396 const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
397 const Point point(transform.unperform(_point));
400 if (get_paragraph_flag())
401 App::dialog_paragraph(_("Text Paragraph"), _("Enter text here:"), text);
403 App::dialog_entry(_("Text Entry"), _("Enter text here:"), text);
405 layer=get_canvas_interface()->add_layer_to("text",canvas,depth);
408 get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
412 layer_selection.push_back(layer);
414 layer->set_param("origin",point);
415 get_canvas_interface()->signal_layer_param_changed()(layer,"origin");
417 layer->set_param("text",text);
418 get_canvas_interface()->signal_layer_param_changed()(layer,"text");
420 layer->set_param("size",get_size());
421 get_canvas_interface()->signal_layer_param_changed()(layer,"size");
423 layer->set_param("orient",get_orientation());
424 get_canvas_interface()->signal_layer_param_changed()(layer,"orient");
426 layer->set_param("family",get_family());
427 get_canvas_interface()->signal_layer_param_changed()(layer,"family");
429 layer->set_description(get_id());
430 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
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;
442 StateText_Context::event_workarea_mouse_button_down_handler(const Smach::event& x)
444 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
445 if(event.button==BUTTON_LEFT)
447 make_text(get_work_area()->snap_point_to_grid(event.pos));
449 get_work_area()->clear_ducks();
450 return Smach::RESULT_ACCEPT;
452 return Smach::RESULT_OK;
456 StateText_Context::refresh_ducks()
458 get_work_area()->clear_ducks();
459 get_work_area()->queue_draw();