Code

Set up toolbox so that paint bucket defaults can be reset
[inkscape.git] / src / desktop-events.cpp
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::Document *xml_doc = sp_document_repr_doc(desktop->doc());
102                     Inkscape::XML::Node *repr = xml_doc->createElement("sodipodi:guide");
103                     repr->setAttribute("orientation", (horiz) ? "horizontal" : "vertical");
104                     double const guide_pos_dt = event_dt[ horiz
105                                                           ? NR::Y
106                                                           : NR::X ];
107                     sp_repr_set_svg_double(repr, "position", guide_pos_dt);
108                     SP_OBJECT_REPR(desktop->namedview)->appendChild(repr);
109                     Inkscape::GC::release(repr);
110                     sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, 
111                                      _("Create guide"));
112                 }
113                 desktop->set_coordinate_status(event_dt);
114             }
115         default:
116             break;
117     }
119     return FALSE;
122 int sp_dt_hruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
124     return sp_dt_ruler_event(widget, event, dtw, true);
127 int sp_dt_vruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
129     return sp_dt_ruler_event(widget, event, dtw, false);
132 /* Guides */
134 gint sp_dt_guide_event(SPCanvasItem *item, GdkEvent *event, gpointer data)
136     static bool dragging = false;
137     static bool moved = false;
138     gint ret = FALSE;
140     SPGuide *guide = SP_GUIDE(data);
141     SPDesktop *desktop = static_cast<SPDesktop*>(gtk_object_get_data(GTK_OBJECT(item->canvas), "SPDesktop"));
143     switch (event->type) {
144         case GDK_2BUTTON_PRESS:
145             if (event->button.button == 1) {
146                 dragging = false;
147                 sp_canvas_item_ungrab(item, event->button.time);
148                 Inkscape::UI::Dialogs::GuidelinePropertiesDialog::showDialog(guide, desktop);
149                 ret = TRUE;
150             }
151             break;
152         case GDK_BUTTON_PRESS:
153             if (event->button.button == 1) {
154                 dragging = true;
155                 sp_canvas_item_grab(item,
156                                     ( GDK_BUTTON_RELEASE_MASK  |
157                                       GDK_BUTTON_PRESS_MASK    |
158                                       GDK_POINTER_MOTION_MASK ),
159                                     NULL,
160                                     event->button.time);
161                 ret = TRUE;
162             }
163             break;
164         case GDK_MOTION_NOTIFY:
165             if (dragging) {
166                 NR::Point const motion_w(event->motion.x,
167                                          event->motion.y);
168                 NR::Point const motion_dt(desktop->w2d(motion_w));
169                 sp_guide_moveto(*guide, sp_guide_position_from_pt(guide, motion_dt), false);
170                 moved = true;
171                 desktop->set_coordinate_status(motion_dt);
172                 desktop->setPosition (motion_dt);
173                 ret = TRUE;
174             }
175             break;
176         case GDK_BUTTON_RELEASE:
177             if (dragging && event->button.button == 1) {
178                 if (moved) {
179                     NR::Point const event_w(event->button.x,
180                                             event->button.y);
181                     NR::Point const event_dt(desktop->w2d(event_w));
182                     if (sp_canvas_world_pt_inside_window(item->canvas, event_w)) {
183                         sp_guide_moveto(*guide, sp_guide_position_from_pt(guide, event_dt), true);
184                         sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
185                                      _("Move guide"));
186                     } else {
187                         /* Undo movement of any attached shapes. */
188                         sp_guide_moveto(*guide, guide->position, false);
189                         sp_guide_remove(guide);
190                         sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
191                                      _("Delete guide"));
192                     }
193                     moved = false;
194                     desktop->set_coordinate_status(event_dt);
195                     desktop->setPosition (event_dt);
196                 }
197                 dragging = false;
198                 sp_canvas_item_ungrab(item, event->button.time);
199                 ret=TRUE;
200             }
201         case GDK_ENTER_NOTIFY:
202         {
204             sp_guideline_set_color(SP_GUIDELINE(item), guide->hicolor);
206             GString *position_string = SP_PX_TO_METRIC_STRING(guide->position, desktop->namedview->getDefaultMetric());
207             char *guide_description = sp_guide_description(guide);
209             desktop->guidesMessageContext()->setF(Inkscape::NORMAL_MESSAGE, _("%s at %s"), guide_description, position_string->str);
211             g_free(guide_description);
212             g_string_free(position_string, TRUE);
213             break;
214         }
215         case GDK_LEAVE_NOTIFY:
216             sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
217             desktop->guidesMessageContext()->clear();
218             break;
219         default:
220             break;
221     }
223     return ret;
227 /*
228   Local Variables:
229   mode:c++
230   c-file-style:"stroustrup"
231   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
232   indent-tabs-mode:nil
233   fill-column:99
234   End:
235 */
236 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :