Code

NEW: temporary on-canvas indicators
[inkscape.git] / src / live_effects / parameter / point.cpp
1 #define INKSCAPE_LIVEPATHEFFECT_PARAMETER_POINT_CPP
3 /*
4  * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
5  *
6  * Released under GNU GPL, read the file 'COPYING' for more information
7  */
9 #include "live_effects/parameter/point.h"
10 #include "live_effects/effect.h"
11 #include "svg/svg.h"
12 #include "svg/stringstream.h"
13 #include <gtkmm.h>
14 #include "ui/widget/point.h"
15 #include "widgets/icon.h"
16 #include "ui/widget/registered-widget.h"
17 #include "inkscape.h"
18 #include "verbs.h"
20 // needed for on-canvas editting:
21 #include "tools-switch.h"
22 #include "node-context.h"
23 #include "shape-editor.h"
24 #include "desktop.h"
25 #include "selection.h"
27 // temporarily needed for tempitem tryout
28 #include "desktop-handles.h"
29 #include "display/sodipodi-ctrl.h"
30 #include "knot.h"
31 #include "display/canvas-temporary-item-list.h"
33 #define LPEPOINTPARAM_DEBUG // undefine to disable all on-canvas editing code for PointParam
35 namespace Inkscape {
37 namespace LivePathEffect {
39 PointParam::PointParam( const Glib::ustring& label, const Glib::ustring& tip,
40                         const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
41                         Effect* effect, Geom::Point default_value )
42     : Geom::Point(default_value), Parameter(label, tip, key, wr, effect), defvalue(default_value)
43 {
44     knot = NULL;
45 #ifdef LPEPOINTPARAM_DEBUG
46     oncanvas_editable = true;
47 #endif
48 }
50 PointParam::~PointParam()
51 {
52     if (knot)
53         g_object_unref (G_OBJECT (knot));
54 }
56 void
57 PointParam::param_set_default()
58 {
59     param_setValue(defvalue);
60 }
62 bool
63 PointParam::param_readSVGValue(const gchar * strvalue)
64 {
65     gchar ** strarray = g_strsplit(strvalue, ",", 2);
66     double newx, newy;
67     unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
68     success += sp_svg_number_read_d(strarray[1], &newy);
69     g_strfreev (strarray);
70     if (success == 2) {
71         param_setValue( Geom::Point(newx, newy) );
72         return true;
73     }
74     return false;
75 }
77 gchar *
78 PointParam::param_writeSVGValue() const
79 {
80     Inkscape::SVGOStringStream os;
81     os << (*this)[0] << "," << (*this)[1];
82     gchar * str = g_strdup(os.str().c_str());
83     return str;
84 }
86 Gtk::Widget *
87 PointParam::param_newWidget(Gtk::Tooltips * tooltips)
88 {
89     Inkscape::UI::Widget::RegisteredPoint * pointwdg = Gtk::manage(
90         new Inkscape::UI::Widget::RegisteredPoint( param_label,
91                                                    param_tooltip,
92                                                    param_key,
93                                                    *param_wr,
94                                                    param_effect->getRepr(),
95                                                    param_effect->getSPDoc() ) );
96     pointwdg->setValue( (*this)[0], (*this)[1] );
97     pointwdg->clearProgrammatically();
98     pointwdg->set_undo_parameters(SP_VERB_DIALOG_LIVE_PATH_EFFECT, _("Change point parameter"));
100     Gtk::Widget*  pIcon = Gtk::manage( sp_icon_get_icon( "draw_node", Inkscape::ICON_SIZE_BUTTON) );
101     Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
102     pButton->set_relief(Gtk::RELIEF_NONE);
103     pIcon->show();
104     pButton->add(*pIcon);
105     pButton->show();
106     pButton->signal_clicked().connect(sigc::mem_fun(*this, &PointParam::on_button_click));
107 #ifndef LPEPOINTPARAM_DEBUG
108     pButton->set_sensitive(false);
109 #endif
111     Gtk::HBox * hbox = Gtk::manage( new Gtk::HBox() );
112     static_cast<Gtk::HBox*>(hbox)->pack_start(*pButton, true, true);
113     static_cast<Gtk::HBox*>(hbox)->pack_start(*pointwdg, true, true);
114     static_cast<Gtk::HBox*>(hbox)->show_all_children();
116     tooltips->set_tip(*pButton, _("Edit on-canvas"));
118     return dynamic_cast<Gtk::Widget *> (hbox);
121 void
122 PointParam::param_setValue(Geom::Point newpoint)
124     *dynamic_cast<Geom::Point *>( this ) = newpoint;
127 void
128 PointParam::param_set_and_write_new_value (Geom::Point newpoint)
130     Inkscape::SVGOStringStream os;
131     os << newpoint[0] << "," << newpoint[1];
132     gchar * str = g_strdup(os.str().c_str());
133     param_write_to_repr(str);
134     g_free(str);
137 void
138 PointParam::param_editOncanvas(SPItem * item, SPDesktop * dt)
140     // If not already in nodecontext, goto it!
141     if (!tools_isactive(dt, TOOLS_NODES)) {
142         tools_switch_current(TOOLS_NODES);
143     }
145     ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
146     shape_editor->set_item_lpe_point_parameter(item, SP_OBJECT(param_effect->getLPEObj()), param_key.c_str());
149     /* TEMPORARY CODE TO TEST TEMPORARY CANVAS ITEMS */
150     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
151     SPCanvasItem * canvasitem = sp_canvas_item_new( sp_desktop_tempgroup (desktop),
152                                                     SP_TYPE_CTRL,
153                                                     "anchor", GTK_ANCHOR_CENTER,
154                                                     "size", 8.0,
155                                                     "stroked", TRUE,
156                                                     "stroke_color", 0xf000f0ff,
157                                                     "mode", SP_KNOT_MODE_XOR,
158                                                     "shape", SP_KNOT_SHAPE_CROSS,
159                                                     NULL );
160     SP_CTRL(canvasitem)->moveto ( static_cast<Geom::Point> (*this) );
161     desktop->add_temporary_canvasitem(canvasitem, 2000);
162     /* END ----   TEMPORARY CODE TO TEST TEMPORARY CANVAS ITEMS */
167 void
168 PointParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
170     param_set_and_write_new_value( (*this) * postmul );
174 // CALLBACKS:
176 void
177 PointParam::on_button_click()
179     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
180     SPItem * item = sp_desktop_selection(desktop)->singleItem();
181     if (item != NULL) {
182         param_editOncanvas(item, desktop);
183     }
186 } /* namespace LivePathEffect */
188 } /* namespace Inkscape */
190 /*
191   Local Variables:
192   mode:c++
193   c-file-style:"stroustrup"
194   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
195   indent-tabs-mode:nil
196   fill-column:99
197   End:
198 */
199 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :