event-context.cpp revision f89564b0a3e48e96545f258fdcd675c666066ece
/*
* Main event handling, and related helper functions.
*
* Authors:
* Lauris Kaplinski <lauris@kaplinski.com>
* Frank Felfe <innerspace@iname.com>
* bulia byak <buliabyak@users.sf.net>
* Jon A. Cruz <jon@joncruz.org>
* Kris De Gussem <Kris.DeGussem@gmail.com>
*
* Copyright (C) 1999-2012 authors
* Copyright (C) 2001-2002 Ximian, Inc.
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "file.h"
#include "event-context.h"
#include <string.h>
#include <gdk/gdkkeysyms.h>
#include <cstring>
#include <string>
#include "display/sp-canvas.h"
#include "xml/node-event-vector.h"
#include "sp-cursor.h"
#include "shortcuts.h"
#include "desktop.h"
#include "desktop-handles.h"
#include "desktop-events.h"
#include "desktop-style.h"
#include "widgets/desktop-widget.h"
#include "sp-namedview.h"
#include "selection.h"
#include "interface.h"
#include "macros.h"
#include "tools-switch.h"
#include "preferences.h"
#include "message-context.h"
#include "gradient-drag.h"
#include "attributes.h"
#include "rubberband.h"
#include "selcue.h"
#include "lpe-tool-context.h"
#include "ui/tool/control-point.h"
#include "shape-editor.h"
#include "sp-guide.h"
#include "color.h"
static GObjectClass *parent_class;
// globals for temporary switching to selector by space
static bool selector_toggled = FALSE;
static int switch_selector_to = 0;
// globals for temporary switching to dropper by 'D'
static bool dropper_toggled = FALSE;
static int switch_dropper_to = 0;
static bool within_tolerance = false;
// globals for keeping track of keyboard scroll events in order to accelerate
static guint32 scroll_event_time = 0;
static guint scroll_keyval = 0;
/**
* Registers the SPEventContext class with Glib and returns its type number.
*/
GType sp_event_context_get_type(void) {
if (!type) {
sizeof(SPEventContext), 4,
};
(GTypeFlags) 0);
}
return type;
}
/**
* Callback to set up the SPEventContext vtable.
*/
}
/**
* Clears all SPEventContext object members.
*/
event_context->space_panning = false;
event_context->_dse_callback_in_process = false;
}
/**
* Callback to free and null member variables of SPEventContext object.
*/
if (ec->_message_context) {
delete ec->_message_context;
}
#if GTK_CHECK_VERSION(3,0,0)
#else
#endif
}
}
if (ec->pref_observer) {
delete ec->pref_observer;
}
if (ec->_delayed_snap_event) {
delete ec->_delayed_snap_event;
}
}
/**
* Recreates and draws cursor on desktop related to SPEventContext.
*/
if (gtk_widget_get_window (w)) {
/* fixme: */
if (ec->cursor_shape) {
if (ec->tool_url && gdk_display_supports_cursor_alpha(display) && gdk_display_supports_cursor_color(display)) {
bool fillHasColor=false, strokeHasColor=false;
double fillOpacity = fillHasColor ? sp_desktop_get_opacity_tool(ec->desktop, ec->tool_url, true) : 0;
double strokeOpacity = strokeHasColor ? sp_desktop_get_opacity_tool(ec->desktop, ec->tool_url, false) : 0;
SP_RGBA32_U_COMPOSE(SP_RGBA32_R_U(fillColor),SP_RGBA32_G_U(fillColor),SP_RGBA32_B_U(fillColor),SP_COLOR_F_TO_U(fillOpacity)),
SP_RGBA32_U_COMPOSE(SP_RGBA32_R_U(strokeColor),SP_RGBA32_G_U(strokeColor),SP_RGBA32_B_U(strokeColor),SP_COLOR_F_TO_U(strokeOpacity))
);
#if GTK_CHECK_VERSION(3,0,0)
#else
#endif
}
}
} else {
if (pixbuf) {
#if GTK_CHECK_VERSION(3,0,0)
#else
#endif
}
}
}
}
gdk_flush();
}
}
/**
* Callback that gets called on initialization of SPEventContext object.
* Redraws mouse cursor, at the moment.
*/
}
/**
* Gobbles next key events on the queue with the same keyval and mask. Returns the number of events consumed.
*/
gint i = 0;
event_next = gdk_event_get();
// while the next event is also a key notify with the same keyval and mask,
i++;
// kill it
// get next
event_next = gdk_event_get();
}
// otherwise, put it back onto the queue
if (event_next)
return i;
}
/**
* Gobbles next motion notify events on the queue with the same mask. Returns the number of events consumed.
*/
gint i = 0;
event_next = gdk_event_get();
// while the next event is also a key notify with the same keyval and mask,
// kill it
// get next
event_next = gdk_event_get();
i++;
}
// otherwise, put it back onto the queue
if (event_next)
return i;
}
/**
* Toggles current tool between active tool and selector tool.
* Subroutine of sp_event_context_private_root_handler().
*/
if (!dt->event_context)
return;
if (selector_toggled) {
if (switch_selector_to)
} else
return;
} else {
}
}
/**
* Toggles current tool between active tool and dropper tool.
* Subroutine of sp_event_context_private_root_handler().
*/
if (!dt->event_context)
return;
if (dropper_toggled) {
if (switch_dropper_to)
} else
return;
} else {
}
}
/**
* Calculates and keeps track of scroll acceleration.
* Subroutine of sp_event_context_private_root_handler().
*/
SPCanvas */*canvas*/) {
/* key pressed within 500ms ? (1/2 second) */
} else {
}
return scroll_multiply;
}
/**
* Main event dispatch, gets called from Gdk.
*/
static unsigned int panning = 0;
static unsigned int zoom_rb = 0;
/// @todo REmove redundant /value in preference keys
case GDK_2BUTTON_PRESS:
if (panning) {
panning = 0;
} else {
/* sp_desktop_dialog(); */
}
break;
case GDK_BUTTON_PRESS:
// save drag origin
within_tolerance = true;
case 1:
if (event_context->space_panning) {
// When starting panning, make sure there are no snap events pending because these might disable the panning again
panning = 1;
}
break;
case 2:
zoom_rb = 2;
} else {
// When starting panning, make sure there are no snap events pending because these might disable the panning again
panning = 2;
}
break;
case 3:
& GDK_CONTROL_MASK) {
// When starting panning, make sure there are no snap events pending because these might disable the panning again
panning = 3;
} else {
}
break;
default:
break;
}
break;
case GDK_MOTION_NOTIFY:
if (panning) {
/* Gdk seems to lose button release for us sometimes :-( */
panning = 0;
} else {
< tolerance)) {
// do not drag if we're within tolerance from origin
break;
}
// Once the user has moved farther than tolerance from
// the original location (indicating they intend to move
// the object, not click), then always process the motion
// notify coordinates as given (no snapping back to origin)
within_tolerance = false;
// gobble subsequent motion events to prevent "sticking"
// when scrolling is slow
}
} else if (zoom_rb) {
< tolerance)) {
break; // do not drag if we're within tolerance from origin
}
// Once the user has moved farther than tolerance from the original location
// (indicating they intend to move the object, not click), then always process the
// motion notify coordinates as given (no snapping back to origin)
within_tolerance = false;
} else {
}
if (zoom_rb == 2)
}
break;
case GDK_BUTTON_RELEASE:
zoom_rb = 0;
if (panning) {
panning = 0;
}
panning = 0;
// in slow complex drawings, some of the motion events are lost;
// to make up for this, we scroll it once again to the button-up event coordinates
// (i.e. canvas will always get scrolled all the way to the mouse release point,
// even if few intermediate steps were visible)
zoom_rb = 0;
if (b && !within_tolerance) {
}
}
break;
case GDK_KEY_PRESS: {
"/options/scrollingacceleration/value", 0, 0, 6);
10, 0, 1000);
// GDK insists on stealing these keys (F1 for no idea what, tab for cycling widgets
// in the editing window). So we resteal them back and run our regular shortcut
// invoker on them.
unsigned int shortcut;
case GDK_KEY_Tab:
case GDK_KEY_ISO_Left_Tab:
case GDK_KEY_F1:
break;
case GDK_KEY_D:
case GDK_KEY_d:
}
break;
case GDK_KEY_Q:
case GDK_KEY_q:
if (desktop->quick_zoomed()) {
}
desktop->zoom_quick(true);
}
break;
case GDK_KEY_W:
case GDK_KEY_w:
case GDK_KEY_F4:
/* Close view */
if (MOD__CTRL_ONLY) {
}
break;
case GDK_KEY_Left: // Ctrl Left
case GDK_KEY_KP_Left:
case GDK_KEY_KP_4:
if (MOD__CTRL_ONLY) {
}
break;
case GDK_KEY_Up: // Ctrl Up
case GDK_KEY_KP_Up:
case GDK_KEY_KP_8:
if (MOD__CTRL_ONLY) {
}
break;
case GDK_KEY_Right: // Ctrl Right
case GDK_KEY_KP_Right:
case GDK_KEY_KP_6:
if (MOD__CTRL_ONLY) {
}
break;
case GDK_KEY_Down: // Ctrl Down
case GDK_KEY_KP_Down:
case GDK_KEY_KP_2:
if (MOD__CTRL_ONLY) {
}
break;
case GDK_KEY_F10:
if (MOD__SHIFT_ONLY) {
}
break;
case GDK_KEY_space:
event_context->space_panning = true;
_("<b>Space+mouse drag</b> to pan canvas"));
} else {
}
break;
case GDK_KEY_z:
case GDK_KEY_Z:
if (MOD__ALT_ONLY) {
}
break;
default:
break;
}
}
break;
case GDK_KEY_RELEASE:
case GDK_KEY_space:
if (event_context->space_panning) {
event_context->space_panning = false;
if (panning == 1) {
panning = 0;
}
}
break;
case GDK_KEY_Q:
case GDK_KEY_q:
if (desktop->quick_zoomed()) {
desktop->zoom_quick(false);
}
break;
default:
break;
}
break;
case GDK_SCROLL: {
/* shift + wheel, pan left--right */
case GDK_SCROLL_UP:
break;
case GDK_SCROLL_DOWN:
break;
default:
break;
}
/* ctrl + wheel, zoom in--out */
double rel_zoom;
case GDK_SCROLL_UP:
break;
case GDK_SCROLL_DOWN:
break;
default:
rel_zoom = 0.0;
break;
}
if (rel_zoom != 0.0) {
}
/* no modifier, pan up--down (left--right on multiwheel mice?) */
} else {
case GDK_SCROLL_UP:
break;
case GDK_SCROLL_DOWN:
break;
case GDK_SCROLL_LEFT:
break;
case GDK_SCROLL_RIGHT:
break;
}
}
break;
}
default:
break;
}
return ret;
}
/**
* Handles item specific events. Gets called from Gdk.
*
* Only reacts to right mouse button at the moment.
* \todo Fixme: do context sensitive popup menu on items.
*/
case GDK_BUTTON_PRESS:
}
break;
default:
break;
}
return ret;
}
/**
* Returns true if we're hovering above a knot (needed because we don't want to pre-snap in that case).
*/
{
if (ec->shape_editor) {
}
return false;
}
/**
* An observer that relays pref changes to the derived classes.
*/
public:
}
}
}
private:
SPEventContext * const _ec;
};
/**
* Creates new SPEventContext object and calls its virtual setup() function.
* @todo This is bogus. pref_path should be a private property of the inheriting objects.
*/
unsigned int key) {
if (pref_path) {
}
return ec;
}
/**
* Finishes SPEventContext.
*/
ec->enableSelectionCue(false);
g_warning("Finishing event context with active link\n");
}
}
//-------------------------------member functions
/**
*/
if (enable) {
if (!_selcue) {
}
} else {
delete _selcue;
}
}
/**
*/
if (enable) {
if (!_grdrag) {
}
} else {
if (_grdrag) {
delete _grdrag;
}
}
}
/**
* Calls virtual set() function of SPEventContext.
*/
}
}
/**
* Calls virtual activate() function of SPEventContext.
*/
// Make sure no delayed snapping events are carried over after switching contexts
// (this is only an additional safety measure against sloppy coding, because each
// context should take care of this by itself.
}
/**
* Calls virtual deactivate() function of SPEventContext.
*/
}
/**
* Calls virtual root_handler(), the main event handling function.
*/
{
case GDK_MOTION_NOTIFY:
(GdkEventMotion *) event,
break;
case GDK_BUTTON_RELEASE:
// If we have any pending snapping action, then invoke it now
}
break;
case GDK_BUTTON_PRESS:
case GDK_2BUTTON_PRESS:
case GDK_3BUTTON_PRESS:
// Snapping will be on hold if we're moving the mouse at high speeds. When starting
// drawing a new shape we really should snap though.
false);
break;
default:
break;
}
}
if (event_context) { // If no event-context is available then do nothing, otherwise Inkscape would crash
// (see the comment in SPDesktop::set_event_context, and bug LP #622350)
ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->root_handler(event_context, event);
}
return ret;
}
/**
* Calls virtual item_handler(), the item event handling function.
*/
case GDK_MOTION_NOTIFY:
sp_event_context_snap_delay_handler(event_context, (gpointer) item, NULL, (GdkEventMotion *) event, DelayedSnapEvent::EVENTCONTEXT_ITEM_HANDLER);
break;
case GDK_BUTTON_RELEASE:
// If we have any pending snapping action, then invoke it now
}
break;
case GDK_BUTTON_PRESS:
case GDK_2BUTTON_PRESS:
case GDK_3BUTTON_PRESS:
// Snapping will be on hold if we're moving the mouse at high speeds. When starting
// drawing a new shape we really should snap though.
break;
default:
break;
}
}
gint sp_event_context_virtual_item_handler(SPEventContext * event_context, SPItem * item, GdkEvent * event) {
if (event_context) { // If no event-context is available then do nothing, otherwise Inkscape would crash
// (see the comment in SPDesktop::set_event_context, and bug LP #622350)
ret = ((SPEventContextClass *) G_OBJECT_GET_CLASS(event_context))->item_handler(event_context, item, event);
if (!ret) {
} else {
}
}
return ret;
}
/**
* Emits 'position_set' signal on desktop and shows coordinates on status bar.
*/
return;
}
}
//-------------------------------------------------------------------
/**
* Create popup menu and tell Gtk to show it.
*/
/* fixme: This is not what I want but works for now (Lauris) */
}
case GDK_BUTTON_PRESS:
break;
case GDK_KEY_PRESS:
break;
default:
break;
}
}
/**
* Show tool context specific modifier tip.
*/
== GDK_KEY_Control_R));
== GDK_KEY_Shift_R));
}
}
/**
* Return the keyval corresponding to the key event in group 0, i.e.,
* in the main (English) layout.
*
* Use this instead of simply event->keyval, so that your keyboard shortcuts
* work regardless of layouts (e.g., in Cyrillic).
*/
return keyval;
}
/**
* Returns item at point p in desktop.
*
* If state includes alt key mask, cyclically selects under; honors
* into_groups.
*/
bool select_under, bool into_groups)
{
if (select_under) {
}
} else {
}
return item;
}
/**
* Returns item if it is under point p in desktop, at any depth; otherwise returns NULL.
*
* Honors into_groups.
*/
SPItem *
return item_at_point;
}
return ec->shape_editor;
}
case GDK_BUTTON_PRESS:
g_print("GDK_BUTTON_PRESS");
break;
case GDK_2BUTTON_PRESS:
g_print("GDK_2BUTTON_PRESS");
break;
case GDK_3BUTTON_PRESS:
g_print("GDK_3BUTTON_PRESS");
break;
case GDK_MOTION_NOTIFY:
g_print("GDK_MOTION_NOTIFY");
break;
case GDK_ENTER_NOTIFY:
g_print("GDK_ENTER_NOTIFY");
break;
case GDK_LEAVE_NOTIFY:
g_print("GDK_LEAVE_NOTIFY");
break;
case GDK_BUTTON_RELEASE:
g_print("GDK_BUTTON_RELEASE");
break;
case GDK_KEY_PRESS:
break;
case GDK_KEY_RELEASE:
break;
default:
//g_print ("even type not recognized");
break;
}
if (print_return) {
g_print("\n");
}
}
/**
* Analyses the current event, calculates the mouse speed, turns snapping off (temporarily) if the
* mouse speed is above a threshold, and stores the current event such that it can be re-triggered when needed
* (re-triggering is controlled by a watchdog timer).
*
* @param ec Pointer to the event context.
* @param dse_item Pointer that store a reference to a canvas or to an item.
* @param dse_item2 Another pointer, storing a reference to a knot or controlpoint.
* @param event Pointer to the motion event.
* @param origin Identifier (enum) specifying where the delay (and the call to this method) were initiated.
*/
{
if (ec->_dse_callback_in_process) {
return;
}
// Snapping occurs when dragging with the left mouse button down, or when hovering e.g. in the pen tool with left mouse button up
bool const c1 = event->state & GDK_BUTTON2_MASK; // We shouldn't hold back any events when other mouse buttons have been
bool const c2 = event->state & GDK_BUTTON3_MASK; // pressed, e.g. when scrolling with the middle mouse button; if we do then
// Inkscape will get stuck in an unresponsive state
// The snap delay will repeat the last motion event, which will lead to
// erroneous points in the calligraphy context. And because we don't snap
// in this context, we might just as well disable the snap delay all together
// Make sure that we don't send any pending snap events to a context if we know in advance
// that we're not going to snap any way (e.g. while scrolling with middle mouse button)
// Any motion event might affect the state of the context, leading to unexpected behavior
// Snap when speed drops below e.g. 0.02 px/msec, or when no motion events have occurred for some period.
// i.e. snap when we're at stand still. A speed threshold enforces snapping for tablets, which might never
// be fully at stand still and might keep spitting out motion events.
ec->desktop->namedview->snap_manager.snapprefs.setSnapPostponedGlobally(true); // put snapping on hold
if (prev_pos) {
// We're moving fast, so postpone any snapping until the next GDK_MOTION_NOTIFY event. We
// will keep on postponing the snapping as long as the speed is high.
// We must snap at some point in time though, so set a watchdog timer at some time from
// now, just in case there's no future motion event that drops under the speed limit (when
// stopping abruptly)
delete ec->_delayed_snap_event;
// If the watchdog expires before a new motion event is received, we will snap (as explained
// above). This means however that when the timer is too short, we will always snap and that the
// speed threshold is ineffective. In the extreme case the delay is set to zero, and snapping will
// be immediate, as it used to be in the old days ;-).
} else { // Speed is very low, so we're virtually at stand still
// But if we're really standing still, then we should snap now. We could use some low-pass filtering,
// otherwise snapping occurs for each jitter movement. For this filtering we'll leave the watchdog to expire,
// snap, and set a new watchdog again.
// it might have already expired, so we'll set a new one; the snapping frequency will be limited this way
} // else: watchdog has been set before and we'll wait for it to expire
}
} else {
// This is the first GDK_MOTION_NOTIFY event, so postpone snapping and set the watchdog
}
}
}
/**
* When the snap delay watchdog timer barks, this method will be called and will re-inject the last motion
* event in an appropriate place, with snapping being turned on again.
*/
// Snap NOW! For this the "postponed" flag will be reset and the last motion event will be repeated
// This might occur when this method is called directly, i.e. not through the timer
// E.g. on GDK_BUTTON_RELEASE in sp_event_context_root_handler()
return FALSE;
}
delete dse;
return false;
}
delete dse;
return false;
}
ec->_dse_callback_in_process = true;
// Depending on where the delayed snap event originated from, we will inject it back at it's origin
// The switch below takes care of that and prepares the relevant parameters
break;
}
}
break;
case DelayedSnapEvent::KNOT_HANDLER: {
}
}
break;
case DelayedSnapEvent::CONTROL_POINT_HANDLER: {
if (!pitem2)
{
delete dse;
return false;
}
if (point) {
}
else {
//workaround:
//[Bug 781893] Crash after moving a Bezier node after Knot path effect?
// --> at some time, some point with X = 0 and Y = nan (not a number) is created ...
// even so, the desktop pointer is invalid and equal to 0xff
g_warning ("encountered non finite point when evaluating snapping callback");
}
}
}
break;
case DelayedSnapEvent::GUIDE_HANDLER: {
}
}
break;
case DelayedSnapEvent::GUIDE_HRULER:
case DelayedSnapEvent::GUIDE_VRULER: {
} else {
}
}
}
break;
default:
g_warning("Origin of snap-delay event has not been defined!;");
break;
}
delete dse;
ec->_dse_callback_in_process = false;
return FALSE; //Kills the timer and stops it from executing this callback over and over again.
}
delete ec->_delayed_snap_event;
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :