681b1dbbc3bc7a4bec9cb67f8fe7bf17a0339ed7
1 /** \file
2 * Gtkmm facade/wrapper around SPRuler code that formerly lived
3 * in desktop-events.cpp
4 */
5 /*
6 * Authors:
7 * Ralf Stephan <ralf@ark.in-berlin.de>
8 * Lauris Kaplinski
9 *
10 * Copyright (C) 2005 Ralf Stephan
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
19 #include <glibmm/i18n.h>
21 #include <gtkmm/ruler.h>
22 #include "helper/units.h"
23 #include "widgets/ruler.h"
24 #include "ui/widget/ruler.h"
26 #include "xml/repr.h"
27 #include "display/guideline.h"
28 #include "desktop.h"
29 #include "desktop-handles.h"
30 #include "document.h"
31 #include "sp-namedview.h"
32 #include "verbs.h"
34 namespace Inkscape {
35 namespace UI {
36 namespace Widget {
38 void
39 Ruler::init(SPDesktop *dt, Gtk::Widget &w)
40 {
41 _dt = dt;
42 _canvas_widget = &w;
43 _dragging = false;
44 _guide = 0;
45 sp_ruler_set_metric(GTK_RULER(_r->gobj()), SP_PT);
46 _r->set_range(-500, 500, 0, 1000);
47 }
49 void
50 Ruler::get_range(double &lo, double &up, double &pos, double &max)
51 {
52 _r->get_range(lo, up, pos, max);
53 }
55 void
56 Ruler::set_range(double lo, double up, double pos, double max)
57 {
58 _r->set_range(lo, up, pos, max);
59 }
61 /// Set metric from namedview
62 void
63 Ruler::update_metric()
64 {
65 if (!_dt) return;
66 sp_ruler_set_metric(GTK_RULER(_r->gobj()), _dt->namedview->getDefaultMetric());
67 }
69 /// Returns text to be used for tooltip for ruler.
70 /// \todo incorrect
71 Glib::ustring
72 Ruler::get_tip()
73 {
74 return gettext(sp_unit_get_plural( _dt
75 ? _dt->namedview->doc_units
76 : &sp_unit_get_by_id(SP_UNIT_PT) ));
77 }
79 /// Helper that gets mouse coordinates relative to canvas widget.
80 void
81 Ruler::canvas_get_pointer(int &x, int &y)
82 {
83 Gdk::ModifierType mask;
84 (void) _canvas_widget->get_window()->get_pointer(x, y, mask);
85 }
87 NR::Point
88 Ruler::get_event_dt()
89 {
90 int wx, wy;
91 canvas_get_pointer(wx, wy);
92 NR::Point const event_win(wx, wy);
93 NR::Point const event_w(sp_canvas_window_to_world(_dt->canvas, event_win));
94 return _dt->w2d(event_w);
95 }
97 bool
98 Ruler::on_button_press_event(GdkEventButton *evb)
99 {
100 g_assert(_dt);
101 NR::Point const &event_dt = get_event_dt();
102 Inkscape::XML::Node *repr = SP_OBJECT_REPR(_dt->namedview);
104 if (evb->button == 1) {
105 _dragging = true;
106 sp_repr_set_boolean(repr, "showguides", TRUE);
107 sp_repr_set_boolean(repr, "inkscape:guide-bbox", TRUE);
108 double const guide_pos_dt = event_dt[ _horiz_f ? NR::Y : NR::X ];
109 _guide = sp_guideline_new(_dt->guides, guide_pos_dt, !_horiz_f);
110 sp_guideline_set_color(SP_GUIDELINE(_guide), _dt->namedview->guidehicolor);
111 (void) get_window()->pointer_grab(false,
112 Gdk::BUTTON_RELEASE_MASK |
113 Gdk::POINTER_MOTION_MASK |
114 Gdk::POINTER_MOTION_HINT_MASK,
115 evb->time);
116 }
117 return false;
118 }
120 bool
121 Ruler::on_motion_notify_event(GdkEventMotion *)
122 {
123 g_assert(_dt);
124 NR::Point const &event_dt = get_event_dt();
126 if (_dragging) {
127 double const guide_pos_dt = event_dt[ _horiz_f ? NR::Y : NR::X ];
128 sp_guideline_set_position(SP_GUIDELINE(_guide), guide_pos_dt);
129 _dt->set_coordinate_status(event_dt);
130 _dt->setPosition(event_dt);
131 }
132 return false;
133 }
135 bool
136 Ruler::on_button_release_event(GdkEventButton *evb)
137 {
138 g_assert(_dt);
139 int wx, wy;
140 canvas_get_pointer(wx, wy);
141 NR::Point const &event_dt = get_event_dt();
143 if (_dragging && evb->button == 1) {
144 Gdk::Window::pointer_ungrab(evb->time);
145 gtk_object_destroy(GTK_OBJECT(_guide));
146 _guide = 0;
147 _dragging = false;
149 if ( (_horiz_f ? wy : wx ) >= 0 ) {
150 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(_dt->doc());
151 Inkscape::XML::Node *repr = xml_doc->createElement("sodipodi:guide");
152 repr->setAttribute("orientation", _horiz_f ? "horizontal" : "vertical");
153 double const guide_pos_dt = event_dt[ _horiz_f ? NR::Y : NR::X ];
154 sp_repr_set_svg_double(repr, "position", guide_pos_dt);
155 SP_OBJECT_REPR(_dt->namedview)->appendChild(repr);
156 Inkscape::GC::release(repr);
157 sp_document_done(sp_desktop_document(_dt), SP_VERB_NONE,
158 /* TODO: annotate */ "ruler.cpp:157");
159 }
160 _dt->set_coordinate_status(event_dt);
161 }
162 return false;
163 }
165 //------------------------------------------------------------
166 HRuler::HRuler()
167 {
168 _dt = 0;
169 _r = static_cast<Gtk::HRuler*>(Glib::wrap(static_cast<GtkWidget*> (sp_hruler_new())));
170 add(*_r);
171 _horiz_f = true;
172 }
174 HRuler::~HRuler()
175 {
176 }
179 VRuler::VRuler()
180 {
181 _dt = 0;
182 _r = static_cast<Gtk::VRuler*>(Glib::wrap(static_cast<GtkWidget*> (sp_vruler_new())));
183 add(*_r);
184 _horiz_f = false;
185 }
187 VRuler::~VRuler()
188 {
189 }
191 }}}
193 /*
194 Local Variables:
195 mode:c++
196 c-file-style:"stroustrup"
197 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
198 indent-tabs-mode:nil
199 fill-column:99
200 End:
201 */
202 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :