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
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include <gtkmm/dialog.h>
33 #include <gtkmm/entry.h>
35 #include "state_text.h"
36 #include "canvasview.h"
40 #include <synfigapp/action.h>
41 #include "event_mouse.h"
42 #include "event_layerclick.h"
44 #include "dialog_tooloptions.h"
45 #include <gtkmm/optionmenu.h>
47 #include "widget_enum.h"
48 #include <synfigapp/main.h>
54 /* === U S I N G =========================================================== */
58 using namespace synfig;
59 using namespace studio;
61 /* === M A C R O S ========================================================= */
63 /* === G L O B A L S ======================================================= */
65 StateText studio::state_text;
67 /* === C L A S S E S & S T R U C T S ======================================= */
69 class studio::StateText_Context
71 etl::handle<CanvasView> canvas_view;
72 CanvasView::IsWorking is_working;
74 Duckmatic::Push duckmatic_push;
78 bool prev_workarea_layer_status_;
81 synfigapp::Settings& settings;
84 Gtk::Table options_table;
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;
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); }
96 bool get_paragraph_flag()const { return checkbutton_paragraph.get_active(); }
97 void set_paragraph_flag(bool x) { return checkbutton_paragraph.set_active(x); }
99 Vector get_size() { return widget_size.get_value(); }
100 void set_size(Vector s) { return widget_size.set_value(s); }
102 Vector get_orientation() { return widget_orientation.get_value(); }
103 void set_orientation(Vector s) { return widget_orientation.set_value(s); }
105 String get_family()const { return entry_family.get_text(); }
106 void set_family(String s) { return entry_family.set_text(s); }
108 void refresh_tool_options(); //to refresh the toolbox
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);
117 //constructor destructor
118 StateText_Context(CanvasView *canvas_view);
119 ~StateText_Context();
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();}
125 //Modifying settings etc.
126 void load_settings();
127 void save_settings();
130 bool egress_on_selection_change;
131 Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
133 if(egress_on_selection_change)
134 throw Smach::egress_exception();
135 return Smach::RESULT_OK;
138 void make_text(const Point& point);
140 }; // END of class StateText_Context
142 /* === P R O C E D U R E S ================================================= */
144 /* === M E T H O D S ======================================================= */
146 StateText::StateText():
147 Smach::state<StateText_Context>("text")
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));
157 StateText::~StateText()
162 StateText_Context::load_settings()
167 //parse the arguments yargh!
168 if(settings.get_value("text.id",value))
173 if(settings.get_value("text.paragraph",value) && value=="1")
174 set_paragraph_flag(true);
176 set_paragraph_flag(false);
178 if(settings.get_value("text.size_x",value))
179 v[0] = atof(value.c_str());
182 if(settings.get_value("text.size_y",value))
183 v[1] = atof(value.c_str());
188 if(settings.get_value("text.orient_x",value))
189 v[0] = atof(value.c_str());
192 if(settings.get_value("text.orient_y",value))
193 v[1] = atof(value.c_str());
198 if(settings.get_value("text.family",value))
201 set_family("Sans Serif");
205 StateText_Context::save_settings()
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());
217 StateText_Context::reset()
223 StateText_Context::increment_id()
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')
237 // figure out how many digits it is
239 (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
244 str_number=String(id,id.size()-digits,id.size());
245 id=String(id,0,id.size()-digits);
247 number=atoi(str_number.c_str());
257 // Add the number back onto the id
259 const String format(strprintf("%%0%dd",digits));
260 id+=strprintf(format.c_str(),number);
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()),
274 checkbutton_paragraph(_("Multiline Editor"))
276 egress_on_selection_change=true;
278 widget_size.set_digits(2);
279 widget_size.set_canvas(canvas_view->get_canvas());
281 widget_orientation.set_digits(2);
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);
295 options_table.show_all();
297 refresh_tool_options();
298 App::dialog_tool_options->present();
300 // Turn off layer clicking
301 get_work_area()->set_allow_layer_clicks(false);
303 // clear out the ducks
304 get_work_area()->clear_ducks();
306 // Refresh the work area
307 get_work_area()->queue_draw();
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();
313 // Disable the time bar
314 //get_canvas_view()->set_sensitive_timebar(false);
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);
320 App::toolbox->refresh();
324 StateText_Context::refresh_tool_options()
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");
333 StateText_Context::event_refresh_tool_options(const Smach::event& /*x*/)
335 refresh_tool_options();
336 return Smach::RESULT_ACCEPT;
339 StateText_Context::~StateText_Context()
343 // Restore layer clicking
344 get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
345 get_canvas_view()->work_area->reset_cursor();
347 App::dialog_tool_options->clear();
349 get_work_area()->queue_draw();
351 get_canvas_view()->queue_rebuild_ducks();
353 App::toolbox->refresh();
357 StateText_Context::event_stop_handler(const Smach::event& /*x*/)
359 throw Smach::egress_exception();
363 StateText_Context::event_refresh_handler(const Smach::event& /*x*/)
366 return Smach::RESULT_ACCEPT;
370 StateText_Context::make_text(const Point& _point)
372 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Text"));
373 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
377 Canvas::Handle canvas(get_canvas_view()->get_canvas());
380 // we are temporarily using the layer to hold something
381 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
384 depth=layer->get_depth();
385 canvas=layer->get_canvas();
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();
392 const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
393 const Point point(transform.unperform(_point));
396 if (get_paragraph_flag())
397 App::dialog_paragraph(_("Text Paragraph"), _("Enter text here:"), text);
399 App::dialog_entry(_("Text Entry"), _("Enter text here:"), text);
401 layer=get_canvas_interface()->add_layer_to("text",canvas,depth);
402 layer_selection.push_back(layer);
404 layer->set_param("origin",point);
405 get_canvas_interface()->signal_layer_param_changed()(layer,"origin");
407 layer->set_param("text",text);
408 get_canvas_interface()->signal_layer_param_changed()(layer,"text");
410 layer->set_param("size",get_size());
411 get_canvas_interface()->signal_layer_param_changed()(layer,"size");
413 layer->set_param("orient",get_orientation());
414 get_canvas_interface()->signal_layer_param_changed()(layer,"orient");
416 layer->set_param("family",get_family());
417 get_canvas_interface()->signal_layer_param_changed()(layer,"family");
419 layer->set_description(get_id());
420 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
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;
432 StateText_Context::event_workarea_mouse_button_down_handler(const Smach::event& x)
434 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
435 if(event.button==BUTTON_LEFT)
437 make_text(get_work_area()->snap_point_to_grid(event.pos));
439 get_work_area()->clear_ducks();
440 return Smach::RESULT_ACCEPT;
442 return Smach::RESULT_OK;
446 StateText_Context::refresh_ducks()
448 get_work_area()->clear_ducks();
449 get_work_area()->queue_draw();