1 #define __SP_DESKTOP_EVENTS_C__
3 /*
4 * Event handlers for SPDesktop
5 *
6 * Author:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * Copyright (C) 1999-2002 Lauris Kaplinski
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
17 #include "display/guideline.h"
18 #include "helper/unit-menu.h"
19 #include "helper/units.h"
20 #include "desktop.h"
21 #include "document.h"
22 #include "sp-guide.h"
23 #include "sp-namedview.h"
24 #include "desktop-handles.h"
25 #include "event-context.h"
26 #include "widgets/desktop-widget.h"
27 #include "sp-metrics.h"
28 #include <glibmm/i18n.h>
29 #include "dialogs/dialog-events.h"
30 #include "message-context.h"
31 #include "xml/repr.h"
32 #include "dialogs/guidelinedialog.h"
34 /* Root item handler */
36 int sp_desktop_root_handler(SPCanvasItem *item, GdkEvent *event, SPDesktop *desktop)
37 {
38 return sp_event_context_root_handler(desktop->event_context, event);
39 }
42 static gint sp_dt_ruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw, bool horiz)
43 {
44 static bool dragging = false;
45 static SPCanvasItem *guide = NULL;
46 int wx, wy;
48 SPDesktop *desktop = dtw->desktop;
49 Inkscape::XML::Node *repr = SP_OBJECT_REPR(desktop->namedview);
51 gdk_window_get_pointer(GTK_WIDGET(dtw->canvas)->window, &wx, &wy, NULL);
52 NR::Point const event_win(wx, wy);
54 switch (event->type) {
55 case GDK_BUTTON_PRESS:
56 if (event->button.button == 1) {
57 dragging = true;
58 NR::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
59 NR::Point const event_dt(desktop->w2d(event_w));
61 // explicitly show guidelines; if I draw a guide, I want them on
62 sp_repr_set_boolean(repr, "showguides", TRUE);
63 sp_repr_set_boolean(repr, "inkscape:guide-bbox", TRUE);
65 double const guide_pos_dt = event_dt[ horiz
66 ? NR::Y
67 : NR::X ];
68 guide = sp_guideline_new(desktop->guides, guide_pos_dt, !horiz);
69 sp_guideline_set_color(SP_GUIDELINE(guide), desktop->namedview->guidehicolor);
70 gdk_pointer_grab(widget->window, FALSE,
71 (GdkEventMask)(GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK ),
72 NULL, NULL,
73 event->button.time);
74 }
75 break;
76 case GDK_MOTION_NOTIFY:
77 if (dragging) {
78 NR::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
79 NR::Point const event_dt(desktop->w2d(event_w));
80 double const guide_pos_dt = event_dt[ horiz
81 ? NR::Y
82 : NR::X ];
83 sp_guideline_set_position(SP_GUIDELINE(guide), guide_pos_dt);
84 desktop->set_coordinate_status(event_dt);
85 desktop->setPosition (event_dt);
86 }
87 break;
88 case GDK_BUTTON_RELEASE:
89 if (dragging && event->button.button == 1) {
90 gdk_pointer_ungrab(event->button.time);
91 NR::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
92 NR::Point const event_dt(desktop->w2d(event_w));
93 dragging = false;
94 gtk_object_destroy(GTK_OBJECT(guide));
95 guide = NULL;
96 if ( ( horiz
97 ? wy
98 : wx )
99 >= 0 )
100 {
101 Inkscape::XML::Node *repr = sp_repr_new("sodipodi:guide");
102 repr->setAttribute("orientation", (horiz) ? "horizontal" : "vertical");
103 double const guide_pos_dt = event_dt[ horiz
104 ? NR::Y
105 : NR::X ];
106 sp_repr_set_svg_double(repr, "position", guide_pos_dt);
107 SP_OBJECT_REPR(desktop->namedview)->appendChild(repr);
108 Inkscape::GC::release(repr);
109 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
110 _("Create guide"));
111 }
112 desktop->set_coordinate_status(event_dt);
113 }
114 default:
115 break;
116 }
118 return FALSE;
119 }
121 int sp_dt_hruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
122 {
123 return sp_dt_ruler_event(widget, event, dtw, true);
124 }
126 int sp_dt_vruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
127 {
128 return sp_dt_ruler_event(widget, event, dtw, false);
129 }
131 /* Guides */
133 gint sp_dt_guide_event(SPCanvasItem *item, GdkEvent *event, gpointer data)
134 {
135 static bool dragging = false;
136 static bool moved = false;
137 gint ret = FALSE;
139 SPGuide *guide = SP_GUIDE(data);
140 SPDesktop *desktop = static_cast<SPDesktop*>(gtk_object_get_data(GTK_OBJECT(item->canvas), "SPDesktop"));
142 switch (event->type) {
143 case GDK_2BUTTON_PRESS:
144 if (event->button.button == 1) {
145 dragging = false;
146 sp_canvas_item_ungrab(item, event->button.time);
147 Inkscape::UI::Dialogs::GuidelinePropertiesDialog::showDialog(guide, desktop);
148 ret = TRUE;
149 }
150 break;
151 case GDK_BUTTON_PRESS:
152 if (event->button.button == 1) {
153 dragging = true;
154 sp_canvas_item_grab(item,
155 ( GDK_BUTTON_RELEASE_MASK |
156 GDK_BUTTON_PRESS_MASK |
157 GDK_POINTER_MOTION_MASK ),
158 NULL,
159 event->button.time);
160 ret = TRUE;
161 }
162 break;
163 case GDK_MOTION_NOTIFY:
164 if (dragging) {
165 NR::Point const motion_w(event->motion.x,
166 event->motion.y);
167 NR::Point const motion_dt(desktop->w2d(motion_w));
168 sp_guide_moveto(*guide, sp_guide_position_from_pt(guide, motion_dt), false);
169 moved = true;
170 desktop->set_coordinate_status(motion_dt);
171 desktop->setPosition (motion_dt);
172 ret = TRUE;
173 }
174 break;
175 case GDK_BUTTON_RELEASE:
176 if (dragging && event->button.button == 1) {
177 if (moved) {
178 NR::Point const event_w(event->button.x,
179 event->button.y);
180 NR::Point const event_dt(desktop->w2d(event_w));
181 if (sp_canvas_world_pt_inside_window(item->canvas, event_w)) {
182 sp_guide_moveto(*guide, sp_guide_position_from_pt(guide, event_dt), true);
183 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
184 _("Move guide"));
185 } else {
186 /* Undo movement of any attached shapes. */
187 sp_guide_moveto(*guide, guide->position, false);
188 sp_guide_remove(guide);
189 sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
190 _("Delete guide"));
191 }
192 moved = false;
193 desktop->set_coordinate_status(event_dt);
194 desktop->setPosition (event_dt);
195 }
196 dragging = false;
197 sp_canvas_item_ungrab(item, event->button.time);
198 ret=TRUE;
199 }
200 case GDK_ENTER_NOTIFY:
201 {
203 sp_guideline_set_color(SP_GUIDELINE(item), guide->hicolor);
205 GString *position_string = SP_PX_TO_METRIC_STRING(guide->position, desktop->namedview->getDefaultMetric());
206 char *guide_description = sp_guide_description(guide);
208 desktop->guidesMessageContext()->setF(Inkscape::NORMAL_MESSAGE, _("%s at %s"), guide_description, position_string->str);
210 g_free(guide_description);
211 g_string_free(position_string, TRUE);
212 break;
213 }
214 case GDK_LEAVE_NOTIFY:
215 sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
216 desktop->guidesMessageContext()->clear();
217 break;
218 default:
219 break;
220 }
222 return ret;
223 }
226 /*
227 Local Variables:
228 mode:c++
229 c-file-style:"stroustrup"
230 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
231 indent-tabs-mode:nil
232 fill-column:99
233 End:
234 */
235 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :