desktop-events.cpp revision cedead99dd5eab3aea7d8f23cc2dd97b041c1793
5488N/A/** @file
5488N/A * @brief Event handlers for SPDesktop
5488N/A */
5488N/A/* Author:
5488N/A * Lauris Kaplinski <lauris@kaplinski.com>
5488N/A *
5488N/A * Copyright (C) 1999-2002 Lauris Kaplinski
5488N/A *
5488N/A * Released under GNU GPL, read the file 'COPYING' for more information
5488N/A */
5488N/A
5488N/A#ifdef HAVE_CONFIG_H
5488N/A# include <config.h>
5488N/A#endif
5488N/A#include <map>
5488N/A#include <string>
5488N/A#include <2geom/line.h>
5488N/A#include <glibmm/i18n.h>
5488N/A
5488N/A#include "desktop.h"
5488N/A#include "desktop-handles.h"
5488N/A#include "dialogs/dialog-events.h"
5488N/A#include "display/canvas-axonomgrid.h"
5488N/A#include "display/canvas-grid.h"
5488N/A#include "display/guideline.h"
5488N/A#include "display/snap-indicator.h"
5488N/A#include "document.h"
5488N/A#include "event-context.h"
5488N/A#include "helper/action.h"
5488N/A#include "helper/unit-menu.h"
5488N/A#include "helper/units.h"
5488N/A#include "message-context.h"
5488N/A#include "preferences.h"
5488N/A#include "snap.h"
5488N/A#include "sp-guide.h"
5488N/A#include "sp-metrics.h"
5488N/A#include "sp-namedview.h"
5488N/A#include "tools-switch.h"
5488N/A#include "ui/dialog/guides.h"
5488N/A#include "widgets/desktop-widget.h"
5488N/A#include "xml/repr.h"
5488N/A
5488N/Astatic void snoop_extended(GdkEvent* event, SPDesktop *desktop);
5488N/Astatic void init_extended();
5488N/A
5488N/A/* Root item handler */
5488N/A
5488N/Aint sp_desktop_root_handler(SPCanvasItem */*item*/, GdkEvent *event, SPDesktop *desktop)
5488N/A{
5488N/A static bool watch = false;
5488N/A static bool first = true;
5488N/A
5488N/A if ( first ) {
5488N/A Inkscape::Preferences *prefs = Inkscape::Preferences::get();
5488N/A if ( prefs->getBool("/options/useextinput/value", true)
5488N/A && prefs->getBool("/options/switchonextinput/value") ) {
5488N/A watch = true;
5488N/A init_extended();
5488N/A }
5488N/A first = false;
5488N/A }
5488N/A if ( watch ) {
5488N/A snoop_extended(event, desktop);
5488N/A }
5488N/A
5488N/A return sp_event_context_root_handler(desktop->event_context, event);
5488N/A}
5488N/A
5488N/A
5488N/Astatic gint sp_dt_ruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw, bool horiz)
5488N/A{
5488N/A static bool dragging = false;
5488N/A static SPCanvasItem *guide = NULL;
5488N/A static Geom::Point normal;
5488N/A static bool snap_window_temporarily_open = false;
5488N/A int wx, wy;
5488N/A
5488N/A SPDesktop *desktop = dtw->desktop;
5488N/A Inkscape::XML::Node *repr = SP_OBJECT_REPR(desktop->namedview);
5488N/A
5488N/A gdk_window_get_pointer(GTK_WIDGET(dtw->canvas)->window, &wx, &wy, NULL);
5488N/A Geom::Point const event_win(wx, wy);
5488N/A
5488N/A gint width, height;
5488N/A gdk_window_get_geometry(GTK_WIDGET(dtw->canvas)->window, NULL /*x*/, NULL /*y*/, &width, &height, NULL/*depth*/);
5488N/A
5488N/A switch (event->type) {
5488N/A case GDK_BUTTON_PRESS:
5488N/A if (event->button.button == 1) {
5488N/A dragging = true;
5488N/A
5488N/A // FIXME: The snap delay mechanism won't work here, because it has been implemented for the event context. Dragging
5488N/A // guides off the ruler will send event to the ruler and not to the context, which bypasses sp_event_context_snap_delay_handler
5488N/A // The snap manager will not notice the difference, so it'll check if the snap delay has been activated (This check
5488N/A // is only needed for catching coding errors, i.e. to warn if the snap window has not been implemented properly
5488N/A // in some context)
5488N/A if (desktop->event_context->_snap_window_open == false) {
5488N/A // A dt_ruler_event might be emitted when dragging a guide off the rulers while drawing a Bezier curve
5488N/A // In such a situation, we're already in that specific context and the snap delay is already active. We should
5488N/A // not set the snap delay to active again, because that will trigger a similar warning to the one above
5488N/A sp_event_context_snap_window_open(desktop->event_context);
5488N/A snap_window_temporarily_open = true;
5488N/A }
5488N/A
5488N/A Geom::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
5488N/A Geom::Point const event_dt(desktop->w2d(event_w));
5488N/A
5488N/A // explicitly show guidelines; if I draw a guide, I want them on
5488N/A sp_repr_set_boolean(repr, "showguides", TRUE);
sp_repr_set_boolean(repr, "inkscape:guide-bbox", TRUE);
// calculate the normal of the guidelines when dragged from the edges of rulers.
Geom::Point normal_bl_to_tr(-1.,1.); //bottomleft to topright
Geom::Point normal_tr_to_bl(1.,1.); //topright to bottomleft
normal_bl_to_tr.normalize();
normal_tr_to_bl.normalize();
Inkscape::CanvasGrid * grid = sp_namedview_get_first_enabled_grid(desktop->namedview);
if ( grid && grid->getGridType() == Inkscape::GRID_AXONOMETRIC ) {
Inkscape::CanvasAxonomGrid *axonomgrid = dynamic_cast<Inkscape::CanvasAxonomGrid *>(grid);
if (event->button.state & GDK_CONTROL_MASK) {
// guidelines normal to gridlines
normal_bl_to_tr = Geom::Point::polar(-axonomgrid->angle_rad[0], 1.0);
normal_tr_to_bl = Geom::Point::polar(axonomgrid->angle_rad[2], 1.0);
} else {
normal_bl_to_tr = rot90(Geom::Point::polar(axonomgrid->angle_rad[2], 1.0));
normal_tr_to_bl = rot90(Geom::Point::polar(-axonomgrid->angle_rad[0], 1.0));
}
}
if (horiz) {
if (wx < 50) {
normal = normal_bl_to_tr;
} else if (wx > width - 50) {
normal = normal_tr_to_bl;
} else {
normal = Geom::Point(0.,1.);
}
} else {
if (wy < 50) {
normal = normal_bl_to_tr;
} else if (wy > height - 50) {
normal = normal_tr_to_bl;
} else {
normal = Geom::Point(1.,0.);
}
}
guide = sp_guideline_new(desktop->guides, event_dt, normal);
sp_guideline_set_color(SP_GUIDELINE(guide), desktop->namedview->guidehicolor);
gdk_pointer_grab(widget->window, FALSE,
(GdkEventMask)(GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK ),
NULL, NULL,
event->button.time);
}
break;
case GDK_MOTION_NOTIFY:
if (dragging) {
Geom::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
Geom::Point event_dt(desktop->w2d(event_w));
SnapManager &m = desktop->namedview->snap_manager;
m.setup(desktop);
// We only have a temporary guide which is not stored in our document yet. Because the guide snapper only looks
// in the document for guides to snap to, we don't have to worry about a guide snapping to itself here
m.guideSnap(event_dt, normal);
sp_guideline_set_position(SP_GUIDELINE(guide), from_2geom(event_dt));
desktop->set_coordinate_status(to_2geom(event_dt));
desktop->setPosition(to_2geom(event_dt));
}
break;
case GDK_BUTTON_RELEASE:
if (dragging && event->button.button == 1) {
gdk_pointer_ungrab(event->button.time);
Geom::Point const event_w(sp_canvas_window_to_world(dtw->canvas, event_win));
Geom::Point event_dt(desktop->w2d(event_w));
SnapManager &m = desktop->namedview->snap_manager;
m.setup(desktop);
// We only have a temporary guide which is not stored in our document yet. Because the guide snapper only looks
// in the document for guides to snap to, we don't have to worry about a guide snapping to itself here
m.guideSnap(event_dt, normal);
dragging = false;
// See the comments in GDK_BUTTON_PRESS
if (snap_window_temporarily_open) {
sp_event_context_snap_window_closed(desktop->event_context);
snap_window_temporarily_open = false;
}
gtk_object_destroy(GTK_OBJECT(guide));
guide = NULL;
if ((horiz ? wy : wx) >= 0) {
Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
Inkscape::XML::Node *repr = xml_doc->createElement("sodipodi:guide");
sp_repr_set_point(repr, "orientation", normal);
sp_repr_set_point(repr, "position", from_2geom(event_dt));
SP_OBJECT_REPR(desktop->namedview)->appendChild(repr);
Inkscape::GC::release(repr);
sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
_("Create guide"));
}
desktop->set_coordinate_status(from_2geom(event_dt));
// A dt_ruler_event might be emitted when dragging a guide of the rulers while drawing a Bezier curve
// In such a situation, we're already in that specific context and the snap delay is already active. We should
// interfere with that context and we should therefore leave the snap delay status as it is. So although it might
// have been set to active above on GDK_BUTTON_PRESS, we should not set it back to inactive here. That must be
// done by the context
}
default:
break;
}
return FALSE;
}
int sp_dt_hruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
{
return sp_dt_ruler_event(widget, event, dtw, true);
}
int sp_dt_vruler_event(GtkWidget *widget, GdkEvent *event, SPDesktopWidget *dtw)
{
return sp_dt_ruler_event(widget, event, dtw, false);
}
/* Guides */
static Geom::Point drag_origin;
enum SPGuideDragType {
SP_DRAG_TRANSLATE,
SP_DRAG_TRANSLATE_CONSTRAINED,
SP_DRAG_ROTATE,
SP_DRAG_MOVE_ORIGIN,
SP_DRAG_NONE
};
static SPGuideDragType drag_type = SP_DRAG_NONE;
gint sp_dt_guide_event(SPCanvasItem *item, GdkEvent *event, gpointer data)
{
static bool moved = false;
gint ret = FALSE;
SPGuide *guide = SP_GUIDE(data);
SPDesktop *desktop = static_cast<SPDesktop*>(gtk_object_get_data(GTK_OBJECT(item->canvas), "SPDesktop"));
switch (event->type) {
case GDK_2BUTTON_PRESS:
if (event->button.button == 1) {
drag_type = SP_DRAG_NONE;
sp_event_context_snap_window_closed(desktop->event_context);
sp_canvas_item_ungrab(item, event->button.time);
Inkscape::UI::Dialogs::GuidelinePropertiesDialog::showDialog(guide, desktop);
ret = TRUE;
}
break;
case GDK_BUTTON_PRESS:
if (event->button.button == 1) {
if (event->button.state & GDK_CONTROL_MASK) {
SPDocument *doc = SP_OBJECT_DOCUMENT(guide);
sp_guide_remove(guide);
sp_document_done(doc, SP_VERB_NONE, _("Delete guide"));
ret = TRUE;
break;
}
sp_event_context_snap_window_open(desktop->event_context);
double tol = 40.0; // Measured in screenpixels
Geom::Point const event_w(event->button.x, event->button.y);
Geom::Point const event_dt(desktop->w2d(event_w));
// Due to the tolerance allowed when grabbing a guide, event_dt will generally
// be close to the guide but not just exactly on it. The drag origin calculated
// here must be exactly on the guide line though, otherwise
// small errors will occur once we snap, see
// https://bugs.launchpad.net/inkscape/+bug/333762
drag_origin = Geom::projection(event_dt, Geom::Line(guide->point_on_line, guide->angle()));
if (Geom::L2(guide->point_on_line - event_dt) < tol/desktop->current_zoom()) {
// the click was on the guide 'anchor'
drag_type = (event->button.state & GDK_SHIFT_MASK) ? SP_DRAG_MOVE_ORIGIN : SP_DRAG_TRANSLATE;
} else {
drag_type = (event->button.state & GDK_SHIFT_MASK) ? SP_DRAG_ROTATE : SP_DRAG_TRANSLATE;
sp_canvas_item_grab(item,
( GDK_BUTTON_RELEASE_MASK |
GDK_BUTTON_PRESS_MASK |
GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK ),
NULL,
event->button.time);
}
ret = TRUE;
}
break;
case GDK_MOTION_NOTIFY:
if (drag_type != SP_DRAG_NONE) {
Geom::Point const motion_w(event->motion.x,
event->motion.y);
Geom::Point motion_dt(desktop->w2d(motion_w));
// This is for snapping while dragging existing guidelines. New guidelines,
// which are dragged off the ruler, are being snapped in sp_dt_ruler_event
SnapManager &m = desktop->namedview->snap_manager;
m.setup(desktop, true, NULL, NULL, guide);
m.guideSnap(motion_dt, to_2geom(guide->normal_to_line));
switch (drag_type) {
case SP_DRAG_TRANSLATE:
{
sp_guide_moveto(*guide, guide->point_on_line + motion_dt - drag_origin, false);
break;
}
case SP_DRAG_TRANSLATE_CONSTRAINED:
{
Geom::Point pt_constr = Geom::constrain_angle(guide->point_on_line, motion_dt);
sp_guide_moveto(*guide, pt_constr, false);
break;
}
case SP_DRAG_ROTATE:
{
double angle = angle_between(drag_origin - guide->point_on_line, motion_dt - guide->point_on_line);
sp_guide_set_normal(*guide, guide->normal_to_line * Geom::Rotate(angle), false);
break;
}
case SP_DRAG_MOVE_ORIGIN:
{
Geom::Line line(guide->point_on_line, guide->angle());
Geom::Coord t = line.nearestPoint(motion_dt);
sp_guide_moveto(*guide, line.pointAt(t), false);
break;
}
case SP_DRAG_NONE:
g_assert_not_reached();
break;
}
moved = true;
desktop->set_coordinate_status(from_2geom(motion_dt));
desktop->setPosition(from_2geom(motion_dt));
ret = TRUE;
}
break;
case GDK_BUTTON_RELEASE:
if (drag_type != SP_DRAG_NONE && event->button.button == 1) {
if (moved) {
Geom::Point const event_w(event->button.x,
event->button.y);
Geom::Point event_dt(desktop->w2d(event_w));
SnapManager &m = desktop->namedview->snap_manager;
m.setup(desktop, true, NULL, NULL, guide);
m.guideSnap(event_dt, guide->normal_to_line);
if (sp_canvas_world_pt_inside_window(item->canvas, event_w)) {
switch (drag_type) {
case SP_DRAG_TRANSLATE:
{
sp_guide_moveto(*guide, guide->point_on_line + event_dt - drag_origin, true);
break;
}
case SP_DRAG_TRANSLATE_CONSTRAINED:
{
Geom::Point pt_constr = Geom::constrain_angle(guide->point_on_line, event_dt);
sp_guide_moveto(*guide, pt_constr, true);
break;
}
case SP_DRAG_ROTATE:
{
double angle = angle_between(drag_origin - guide->point_on_line, event_dt - guide->point_on_line);
sp_guide_set_normal(*guide, guide->normal_to_line * Geom::Rotate(angle), true);
break;
}
case SP_DRAG_MOVE_ORIGIN:
{
Geom::Line line(guide->point_on_line, guide->angle());
Geom::Coord t = line.nearestPoint(event_dt);
sp_guide_moveto(*guide, line.pointAt(t), true);
break;
}
case SP_DRAG_NONE:
g_assert_not_reached();
break;
}
sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
_("Move guide"));
} else {
/* Undo movement of any attached shapes. */
sp_guide_moveto(*guide, guide->point_on_line, false);
sp_guide_set_normal(*guide, guide->normal_to_line, false);
sp_guide_remove(guide);
sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE,
_("Delete guide"));
}
moved = false;
desktop->set_coordinate_status(from_2geom(event_dt));
desktop->setPosition (from_2geom(event_dt));
}
drag_type = SP_DRAG_NONE;
sp_event_context_snap_window_closed(desktop->event_context);
sp_canvas_item_ungrab(item, event->button.time);
ret=TRUE;
}
case GDK_ENTER_NOTIFY:
{
sp_guideline_set_color(SP_GUIDELINE(item), guide->hicolor);
char *guide_description = sp_guide_description(guide);
desktop->guidesMessageContext()->setF(Inkscape::NORMAL_MESSAGE, _("<b>Guideline</b>: %s"), guide_description);
g_free(guide_description);
break;
}
case GDK_LEAVE_NOTIFY:
sp_guideline_set_color(SP_GUIDELINE(item), guide->color);
desktop->guidesMessageContext()->clear();
break;
default:
break;
}
return ret;
}
//static std::map<GdkInputSource, std::string> switchMap;
static std::map<std::string, int> toolToUse;
static std::string lastName;
static GdkInputSource lastType = GDK_SOURCE_MOUSE;
static void init_extended()
{
std::string avoidName = "pad";
GList* devices = gdk_devices_list();
if ( devices ) {
for ( GList* curr = devices; curr; curr = g_list_next(curr) ) {
GdkDevice* dev = reinterpret_cast<GdkDevice*>(curr->data);
if ( dev->name
&& (avoidName != dev->name)
&& (dev->source != GDK_SOURCE_MOUSE) ) {
// g_message("Adding '%s' as [%d]", dev->name, dev->source);
// Set the initial tool for the device
switch ( dev->source ) {
case GDK_SOURCE_PEN:
toolToUse[dev->name] = TOOLS_CALLIGRAPHIC;
break;
case GDK_SOURCE_ERASER:
toolToUse[dev->name] = TOOLS_ERASER;
break;
case GDK_SOURCE_CURSOR:
toolToUse[dev->name] = TOOLS_SELECT;
break;
default:
; // do not add
}
// } else if (dev->name) {
// g_message("Skippn '%s' as [%s]", dev->name, dev->source);
}
}
}
}
void snoop_extended(GdkEvent* event, SPDesktop *desktop)
{
GdkInputSource source = GDK_SOURCE_MOUSE;
std::string name;
switch ( event->type ) {
case GDK_MOTION_NOTIFY:
{
GdkEventMotion* event2 = reinterpret_cast<GdkEventMotion*>(event);
if ( event2->device ) {
source = event2->device->source;
name = event2->device->name;
}
}
break;
case GDK_BUTTON_PRESS:
case GDK_2BUTTON_PRESS:
case GDK_3BUTTON_PRESS:
case GDK_BUTTON_RELEASE:
{
GdkEventButton* event2 = reinterpret_cast<GdkEventButton*>(event);
if ( event2->device ) {
source = event2->device->source;
name = event2->device->name;
}
}
break;
case GDK_SCROLL:
{
GdkEventScroll* event2 = reinterpret_cast<GdkEventScroll*>(event);
if ( event2->device ) {
source = event2->device->source;
name = event2->device->name;
}
}
break;
case GDK_PROXIMITY_IN:
case GDK_PROXIMITY_OUT:
{
GdkEventProximity* event2 = reinterpret_cast<GdkEventProximity*>(event);
if ( event2->device ) {
source = event2->device->source;
name = event2->device->name;
}
}
break;
default:
;
}
if (!name.empty()) {
if ( lastType != source || lastName != name ) {
// The device switched. See if it is one we 'count'
//g_message("Changed device %s -> %s", lastName.c_str(), name.c_str());
std::map<std::string, int>::iterator it = toolToUse.find(lastName);
if (it != toolToUse.end()) {
// Save the tool currently selected for next time the input
// device shows up.
it->second = tools_active(desktop);
}
it = toolToUse.find(name);
if (it != toolToUse.end() ) {
tools_switch(desktop, it->second);
}
lastName = name;
lastType = source;
}
}
}
/*
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:encoding=utf-8:textwidth=99 :