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 "canvasview.h"
41 #include <synfigapp/action.h>
42 #include "event_mouse.h"
43 #include "event_layerclick.h"
45 #include "dialog_tooloptions.h"
46 #include <gtkmm/optionmenu.h>
48 #include "widget_enum.h"
49 #include <synfigapp/main.h>
55 /* === U S I N G =========================================================== */
59 using namespace synfig;
60 using namespace studio;
62 /* === M A C R O S ========================================================= */
64 /* === G L O B A L S ======================================================= */
66 StateText studio::state_text;
68 /* === C L A S S E S & S T R U C T S ======================================= */
70 class studio::StateText_Context
72 etl::handle<CanvasView> canvas_view;
73 CanvasView::IsWorking is_working;
75 Duckmatic::Push duckmatic_push;
79 bool prev_workarea_layer_status_;
82 synfigapp::Settings& settings;
85 Gtk::Table options_table;
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;
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); }
97 bool get_paragraph_flag()const { return checkbutton_paragraph.get_active(); }
98 void set_paragraph_flag(bool x) { return checkbutton_paragraph.set_active(x); }
100 Vector get_size() { return widget_size.get_value(); }
101 void set_size(Vector s) { return widget_size.set_value(s); }
103 Vector get_orientation() { return widget_orientation.get_value(); }
104 void set_orientation(Vector s) { return widget_orientation.set_value(s); }
106 String get_family()const { return entry_family.get_text(); }
107 void set_family(String s) { return entry_family.set_text(s); }
109 void refresh_tool_options(); //to refresh the toolbox
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);
118 //constructor destructor
119 StateText_Context(CanvasView *canvas_view);
120 ~StateText_Context();
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();}
126 //Modifying settings etc.
127 void load_settings();
128 void save_settings();
131 bool egress_on_selection_change;
132 Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
134 if(egress_on_selection_change)
135 throw Smach::egress_exception();
136 return Smach::RESULT_OK;
139 void make_text(const Point& point);
141 }; // END of class StateText_Context
143 /* === P R O C E D U R E S ================================================= */
145 /* === M E T H O D S ======================================================= */
147 StateText::StateText():
148 Smach::state<StateText_Context>("text")
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));
158 StateText::~StateText()
163 StateText_Context::load_settings()
168 //parse the arguments yargh!
169 if(settings.get_value("text.id",value))
174 if(settings.get_value("text.paragraph",value) && value=="1")
175 set_paragraph_flag(true);
177 set_paragraph_flag(false);
179 if(settings.get_value("text.size_x",value))
180 v[0] = atof(value.c_str());
183 if(settings.get_value("text.size_y",value))
184 v[1] = atof(value.c_str());
189 if(settings.get_value("text.orient_x",value))
190 v[0] = atof(value.c_str());
193 if(settings.get_value("text.orient_y",value))
194 v[1] = atof(value.c_str());
199 if(settings.get_value("text.family",value))
202 set_family("Sans Serif");
206 StateText_Context::save_settings()
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());
218 StateText_Context::reset()
224 StateText_Context::increment_id()
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')
238 // figure out how many digits it is
240 (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
245 str_number=String(id,id.size()-digits,id.size());
246 id=String(id,0,id.size()-digits);
248 number=atoi(str_number.c_str());
258 // Add the number back onto the id
260 const String format(strprintf("%%0%dd",digits));
261 id+=strprintf(format.c_str(),number);
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()),
275 checkbutton_paragraph(_("Multiline Editor"))
277 egress_on_selection_change=true;
279 widget_size.set_digits(2);
280 widget_size.set_canvas(canvas_view->get_canvas());
282 widget_orientation.set_digits(2);
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);
296 options_table.show_all();
298 refresh_tool_options();
299 App::dialog_tool_options->present();
301 // Turn off layer clicking
302 get_work_area()->set_allow_layer_clicks(false);
304 // clear out the ducks
305 get_work_area()->clear_ducks();
307 // Refresh the work area
308 get_work_area()->queue_draw();
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();
314 // Disable the time bar
315 //get_canvas_view()->set_sensitive_timebar(false);
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);
321 App::toolbox->refresh();
325 StateText_Context::refresh_tool_options()
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");
334 StateText_Context::event_refresh_tool_options(const Smach::event& /*x*/)
336 refresh_tool_options();
337 return Smach::RESULT_ACCEPT;
340 StateText_Context::~StateText_Context()
344 // Restore layer clicking
345 get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
346 get_canvas_view()->work_area->reset_cursor();
348 App::dialog_tool_options->clear();
350 get_work_area()->queue_draw();
352 get_canvas_view()->queue_rebuild_ducks();
354 App::toolbox->refresh();
358 StateText_Context::event_stop_handler(const Smach::event& /*x*/)
360 throw Smach::egress_exception();
364 StateText_Context::event_refresh_handler(const Smach::event& /*x*/)
367 return Smach::RESULT_ACCEPT;
371 StateText_Context::make_text(const Point& _point)
373 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Text"));
374 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
378 Canvas::Handle canvas(get_canvas_view()->get_canvas());
381 // we are temporarily using the layer to hold something
382 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
385 depth=layer->get_depth();
386 canvas=layer->get_canvas();
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();
393 const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
394 const Point point(transform.unperform(_point));
397 if (get_paragraph_flag())
398 App::dialog_paragraph(_("Text Paragraph"), _("Enter text here:"), text);
400 App::dialog_entry(_("Text Entry"), _("Enter text here:"), text);
402 layer=get_canvas_interface()->add_layer_to("text",canvas,depth);
405 get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
409 layer_selection.push_back(layer);
411 layer->set_param("origin",point);
412 get_canvas_interface()->signal_layer_param_changed()(layer,"origin");
414 layer->set_param("text",text);
415 get_canvas_interface()->signal_layer_param_changed()(layer,"text");
417 layer->set_param("size",get_size());
418 get_canvas_interface()->signal_layer_param_changed()(layer,"size");
420 layer->set_param("orient",get_orientation());
421 get_canvas_interface()->signal_layer_param_changed()(layer,"orient");
423 layer->set_param("family",get_family());
424 get_canvas_interface()->signal_layer_param_changed()(layer,"family");
426 layer->set_description(get_id());
427 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
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;
439 StateText_Context::event_workarea_mouse_button_down_handler(const Smach::event& x)
441 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
442 if(event.button==BUTTON_LEFT)
444 make_text(get_work_area()->snap_point_to_grid(event.pos));
446 get_work_area()->clear_ducks();
447 return Smach::RESULT_ACCEPT;
449 return Smach::RESULT_OK;
453 StateText_Context::refresh_ducks()
455 get_work_area()->clear_ducks();
456 get_work_area()->queue_draw();