1 #ifndef __SP_EVENT_CONTEXT_H__
2 #define __SP_EVENT_CONTEXT_H__
4 /** \file
5 * SPEventContext: base class for event processors
6 *
7 * This is per desktop object, which (its derivatives) implements
8 * different actions bound to mouse events.
9 *
10 * Authors:
11 * Lauris Kaplinski <lauris@kaplinski.com>
12 * Frank Felfe <innerspace@iname.com>
13 *
14 * Copyright (C) 1999-2002 authors
15 * Copyright (C) 2001-2002 Ximian, Inc.
16 *
17 * Released under GNU GPL, read the file 'COPYING' for more information
18 */
20 #include <glib-object.h>
21 #include <gdk/gdktypes.h>
22 #include <gdk/gdkevents.h>
23 #include "knot.h"
25 #include "2geom/forward.h"
26 #include "preferences.h"
28 struct GrDrag;
29 struct SPDesktop;
30 struct SPItem;
31 class ShapeEditor;
32 struct SPEventContext;
34 namespace Inkscape {
35 class MessageContext;
36 class SelCue;
37 namespace XML {
38 class Node;
39 }
40 }
42 gboolean sp_event_context_snap_watchdog_callback(gpointer data);
43 void sp_event_context_discard_delayed_snap_event(SPEventContext *ec);
45 class DelayedSnapEvent
46 {
47 public:
48 enum DelayedSnapEventOrigin {
49 UNDEFINED_HANDLER = 0,
50 EVENTCONTEXT_ROOT_HANDLER,
51 EVENTCONTEXT_ITEM_HANDLER,
52 KNOT_HANDLER
53 };
55 DelayedSnapEvent(SPEventContext *event_context, SPItem* const item, SPKnot* knot, GdkEventMotion const *event, DelayedSnapEvent::DelayedSnapEventOrigin const origin)
56 : _timer_id(0), _event(NULL), _item(item), _knot(knot), _origin(origin), _event_context(event_context)
57 {
58 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
59 double value = prefs->getDoubleLimited("/options/snapdelay/value", 0, 0, 1000);
60 _timer_id = g_timeout_add(value, &sp_event_context_snap_watchdog_callback, this);
61 _event = gdk_event_copy((GdkEvent*) event);
62 ((GdkEventMotion *)_event)->time = GDK_CURRENT_TIME;
63 }
65 ~DelayedSnapEvent() {
66 if (_timer_id > 0) g_source_remove(_timer_id); // Kill the watchdog
67 if (_event != NULL) gdk_event_free(_event); // Remove the copy of the original event
68 }
70 SPEventContext* getEventContext() {return _event_context;}
71 DelayedSnapEventOrigin getOrigin() {return _origin;}
72 GdkEvent* getEvent() {return _event;}
73 SPItem* getItem() {return _item;}
74 SPKnot* getKnot() {return _knot;}
76 private:
77 guint _timer_id;
78 GdkEvent* _event;
79 SPItem* _item;
80 SPKnot* _knot;
81 DelayedSnapEventOrigin _origin;
82 SPEventContext* _event_context;
83 };
85 void sp_event_context_snap_delay_handler(SPEventContext *ec, SPItem* const item, SPKnot* const knot, GdkEventMotion *event, DelayedSnapEvent::DelayedSnapEventOrigin origin);
87 /**
88 * Base class for Event processors.
89 */
90 struct SPEventContext : public GObject {
91 void enableSelectionCue (bool enable=true);
92 void enableGrDrag (bool enable=true);
94 /// Desktop eventcontext stack
95 SPEventContext *next;
96 unsigned key;
97 SPDesktop *desktop;
98 Inkscape::Preferences::Observer *pref_observer;
99 gchar const *const *cursor_shape;
100 gint hot_x, hot_y; ///< indicates the cursor's hot spot
101 GdkCursor *cursor;
103 gint xp, yp; ///< where drag started
104 gint tolerance;
105 bool within_tolerance; ///< are we still within tolerance of origin
107 SPItem *item_to_select; ///< the item where mouse_press occurred, to
108 ///< be selected if this is a click not drag
110 Inkscape::MessageContext *defaultMessageContext() {
111 return _message_context;
112 }
114 Inkscape::MessageContext *_message_context;
116 Inkscape::SelCue *_selcue;
118 GrDrag *_grdrag;
119 GrDrag *get_drag () {return _grdrag;}
121 ShapeEditor* shape_editor;
123 bool space_panning;
125 DelayedSnapEvent *_delayed_snap_event;
126 };
128 /**
129 * The SPEvent vtable.
130 */
131 struct SPEventContextClass : public GObjectClass {
132 void (* setup)(SPEventContext *ec);
133 void (* finish)(SPEventContext *ec);
134 void (* set)(SPEventContext *ec, Inkscape::Preferences::Entry *val);
135 void (* activate)(SPEventContext *ec);
136 void (* deactivate)(SPEventContext *ec);
137 gint (* root_handler)(SPEventContext *ec, GdkEvent *event);
138 gint (* item_handler)(SPEventContext *ec, SPItem *item, GdkEvent *event);
139 };
141 #define SP_EVENT_CONTEXT_DESKTOP(e) (SP_EVENT_CONTEXT(e)->desktop)
142 #define SP_EVENT_CONTEXT_DOCUMENT(e) ((SP_EVENT_CONTEXT_DESKTOP(e))->doc())
144 #define SP_EVENT_CONTEXT_STATIC 0
146 SPEventContext *sp_event_context_new(GType type, SPDesktop *desktop, gchar const *pref_path, unsigned key);
147 void sp_event_context_finish(SPEventContext *ec);
148 void sp_event_context_read(SPEventContext *ec, gchar const *key);
149 void sp_event_context_activate(SPEventContext *ec);
150 void sp_event_context_deactivate(SPEventContext *ec);
152 gint sp_event_context_root_handler(SPEventContext *ec, GdkEvent *event);
153 gint sp_event_context_virtual_root_handler(SPEventContext *ec, GdkEvent *event);
154 gint sp_event_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event);
155 gint sp_event_context_virtual_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event);
157 void sp_event_root_menu_popup(SPDesktop *desktop, SPItem *item, GdkEvent *event);
159 gint gobble_key_events(guint keyval, gint mask);
160 gint gobble_motion_events(gint mask);
162 void sp_event_context_update_cursor(SPEventContext *ec);
164 void sp_event_show_modifier_tip(Inkscape::MessageContext *message_context, GdkEvent *event,
165 gchar const *ctrl_tip, gchar const *shift_tip, gchar const *alt_tip);
167 guint get_group0_keyval(GdkEventKey *event);
169 SPItem *sp_event_context_find_item (SPDesktop *desktop, Geom::Point const &p, bool select_under, bool into_groups);
170 SPItem *sp_event_context_over_item (SPDesktop *desktop, SPItem *item, Geom::Point const &p);
172 ShapeEditor *sp_event_context_get_shape_editor (SPEventContext *ec);
174 void ec_shape_event_attr_changed(Inkscape::XML::Node *shape_repr,
175 gchar const *name, gchar const *old_value, gchar const *new_value,
176 bool const is_interactive, gpointer const data);
178 void event_context_print_event_info(GdkEvent *event, bool print_return = true);
180 #endif
183 /*
184 Local Variables:
185 mode:c++
186 c-file-style:"stroustrup"
187 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
188 indent-tabs-mode:nil
189 fill-column:99
190 End:
191 */
192 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :