Code

Don't force focus on the canvas when the desktop is given
[inkscape.git] / src / select-context.cpp
index 7027de1efc66ea9a5d1aa09e5bab7f25209b7c7f..c8186b1fd1ef4b86c15ea6ceb1390c6ba91738c1 100644 (file)
@@ -3,11 +3,12 @@
 /*
  * Selection and transformation context
  *
- * Author:
+ * Authors:
  *   Lauris Kaplinski <lauris@kaplinski.com>
  *   bulia byak <buliabyak@users.sf.net>
-  *
- * Copyright (C) 1999-2005 authors
+ *
+ * Copyright (C) 2006      Johan Engelen <johan@shouraizou.nl>
+ * Copyright (C) 1999-2005 Authors
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
@@ -22,8 +23,8 @@
 #include "selection.h"
 #include "seltrans-handles.h"
 #include "sp-cursor.h"
-//#include "pixmaps/cursor-select-m.xpm" // These aren't used
-//#include "pixmaps/cursor-select-d.xpm"
+#include "pixmaps/cursor-select-m.xpm"
+#include "pixmaps/cursor-select-d.xpm"
 #include "pixmaps/handles.xpm"
 #include <glibmm/i18n.h>
 
@@ -96,8 +97,8 @@ sp_select_context_class_init(SPSelectContextClass *klass)
     event_context_class->item_handler = sp_select_context_item_handler;
 
     // cursors in select context
-//    CursorSelectMouseover = sp_cursor_new_from_xpm(cursor_select_m_xpm , 1, 1);
-//    CursorSelectDragging = sp_cursor_new_from_xpm(cursor_select_d_xpm , 1, 1);
+    CursorSelectMouseover = sp_cursor_new_from_xpm(cursor_select_m_xpm , 1, 1);
+    CursorSelectDragging = sp_cursor_new_from_xpm(cursor_select_d_xpm , 1, 1);
     // selection handles
     handles[0]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_scale_nw_xpm);
     handles[1]  = gdk_pixbuf_new_from_xpm_data((gchar const **)handle_scale_ne_xpm);
@@ -227,10 +228,10 @@ sp_select_context_abort(SPEventContext *event_context)
             return true;
         }
     } else {
-        NR::Maybe<NR::Rect> const b = Inkscape::Rubberband::get()->getRectangle();
-        if (b != NR::Nothing()) {
+        if (Inkscape::Rubberband::get()->is_started()) {
             Inkscape::Rubberband::get()->stop();
             rb_escaped = 1;
+            SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
             SP_EVENT_CONTEXT(sc)->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Selection canceled."));
             return true;
         }
@@ -298,7 +299,7 @@ sp_select_context_item_handler(SPEventContext *event_context, SPItem *item, GdkE
 
     switch (event->type) {
         case GDK_BUTTON_PRESS:
-            if (event->button.button == 1) {
+            if (event->button.button == 1 && !event_context->space_panning) {
                 /* Left mousebutton */
 
                 // save drag origin
@@ -311,26 +312,38 @@ sp_select_context_item_handler(SPEventContext *event_context, SPItem *item, GdkE
                 sc->button_press_ctrl = (event->button.state & GDK_CONTROL_MASK) ? true : false;
                 sc->button_press_alt = (event->button.state & GDK_MOD1_MASK) ? true : false;
 
-                if (event->button.state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
+                if (event->button.state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK)) {
                     // if shift or ctrl was pressed, do not move objects;
                     // pass the event to root handler which will perform rubberband, shift-click, ctrl-click, ctrl-drag
                 } else {
                     sc->dragging = TRUE;
                     sc->moved = FALSE;
 
+                    sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
+                    
                     // remember the clicked item in sc->item:
+                    if (sc->item) {
+                        sp_object_unref(sc->item, NULL);
+                        sc->item = NULL;
+                    }
                     sc->item = sp_event_context_find_item (desktop,
                                               NR::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, FALSE);
                     sp_object_ref(sc->item, NULL);
 
                     rb_escaped = drag_escaped = 0;
 
+                    if (sc->grabbed) {
+                        sp_canvas_item_ungrab(sc->grabbed, event->button.time);
+                        sc->grabbed = NULL;
+                    }
                     sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->drawing),
-                                        GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
+                                        GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
                                         GDK_POINTER_MOTION_MASK,
                                         NULL, event->button.time);
                     sc->grabbed = SP_CANVAS_ITEM(desktop->drawing);
 
+                    sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
+
                     ret = TRUE;
                 }
             } else if (event->button.button == 3) {
@@ -403,6 +416,8 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                         desktop->setCurrentLayer(reinterpret_cast<SPObject *>(clicked_item));
                         sp_desktop_selection(desktop)->clear();
                         sc->dragging = false;
+
+                        sp_canvas_end_forced_full_redraws(desktop->canvas);
                     } else { // switch tool
                         tools_switch_by_item (desktop, clicked_item);
                     }
@@ -413,7 +428,7 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
             }
             break;
         case GDK_BUTTON_PRESS:
-            if (event->button.button == 1) {
+            if (event->button.button == 1 && !event_context->space_panning) {
 
                 // save drag origin
                 xp = (gint) event->button.x;
@@ -422,9 +437,15 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
 
                 NR::Point const button_pt(event->button.x, event->button.y);
                 NR::Point const p(desktop->w2d(button_pt));
+                if (event->button.state & GDK_MOD1_MASK)
+                    Inkscape::Rubberband::get()->setMode(RUBBERBAND_MODE_TOUCHPATH);
                 Inkscape::Rubberband::get()->start(desktop, p);
+                if (sc->grabbed) {
+                    sp_canvas_item_ungrab(sc->grabbed, event->button.time);
+                    sc->grabbed = NULL;
+                }
                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
-                                    GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK,
+                                    GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK,
                                     NULL, event->button.time);
                 sc->grabbed = SP_CANVAS_ITEM(desktop->acetate);
 
@@ -445,7 +466,7 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
             break;
 
         case GDK_MOTION_NOTIFY:
-            if (event->motion.state & GDK_BUTTON1_MASK) {
+            if (event->motion.state & GDK_BUTTON1_MASK && !event_context->space_panning) {
                 NR::Point const motion_pt(event->motion.x, event->motion.y);
                 NR::Point const p(desktop->w2d(motion_pt));
 
@@ -459,14 +480,20 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                 // motion notify coordinates as given (no snapping back to origin)
                 within_tolerance = false;
 
-                if (sc->button_press_ctrl || sc->button_press_alt) // if ctrl or alt was pressed and it's not click, we want to drag rather than rubberband
+                if (sc->button_press_ctrl || (sc->button_press_alt && !sc->button_press_shift && !selection->isEmpty())) {
+                    // if it's not click and ctrl or alt was pressed (the latter with some selection
+                    // but not with shift) we want to drag rather than rubberband
                     sc->dragging = TRUE;
 
+                    sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
+                }
+
                 if (sc->dragging) {
                     /* User has dragged fast, so we get events on root (lauris)*/
                     // not only that; we will end up here when ctrl-dragging as well
                     // and also when we started within tolerance, but trespassed tolerance outside of item
                     Inkscape::Rubberband::get()->stop();
+                    SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
                     item_at_point = desktop->item_at_point(NR::Point(event->button.x, event->button.y), FALSE);
                     if (!item_at_point) // if no item at this point, try at the click point (bug 1012200)
                         item_at_point = desktop->item_at_point(NR::Point(xp, yp), FALSE);
@@ -492,22 +519,30 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                         }
                         if (!seltrans->isEmpty())
                             seltrans->moveTo(p, event->button.state);
-                        if (desktop->scroll_to_point(&p))
-                            // unfortunately in complex drawings, gobbling results in losing grab of the object, for some mysterious reason
-                            ; //gobble_motion_events(GDK_BUTTON1_MASK);
+                        desktop->scroll_to_point(&p);
+                        gobble_motion_events(GDK_BUTTON1_MASK);
                         ret = TRUE;
                     } else {
                         sc->dragging = FALSE;
+
+                        sp_canvas_end_forced_full_redraws(desktop->canvas);
                     }
                 } else {
-                    Inkscape::Rubberband::get()->move(p);
-                    gobble_motion_events(GDK_BUTTON1_MASK);
+                    if (Inkscape::Rubberband::get()->is_started()) {
+                        Inkscape::Rubberband::get()->move(p);
+                        if (Inkscape::Rubberband::get()->getMode() == RUBBERBAND_MODE_TOUCHPATH) {
+                            event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw over</b> objects to select them; release <b>Alt</b> to switch to rubberband selection"));
+                        } else {
+                            event_context->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag around</b> objects to select them; press <b>Alt</b> to switch to touch selection"));
+                        }
+                        gobble_motion_events(GDK_BUTTON1_MASK);
+                    }
                 }
             }
             break;
         case GDK_BUTTON_RELEASE:
             xp = yp = 0;
-            if ((event->button.button == 1) && (sc->grabbed)) {
+            if ((event->button.button == 1) && (sc->grabbed) && !event_context->space_panning) {
                 if (sc->dragging) {
                     if (sc->moved) {
                         // item has been moved
@@ -535,18 +570,29 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                         }
                     }
                     sc->dragging = FALSE;
+
+                    sp_canvas_end_forced_full_redraws(desktop->canvas);
+
                     if (sc->item) {
                         sp_object_unref( SP_OBJECT(sc->item), NULL);
                     }
                     sc->item = NULL;
                 } else {
-                    NR::Maybe<NR::Rect> const b = Inkscape::Rubberband::get()->getRectangle();
-                    if (b != NR::Nothing() && !within_tolerance) {
+                    Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get();
+                    if (r->is_started() && !within_tolerance) {
                         // this was a rubberband drag
-                        Inkscape::Rubberband::get()->stop();
+                        GSList *items = NULL;
+                        if (r->getMode() == RUBBERBAND_MODE_RECT) {
+                            NR::Maybe<NR::Rect> const b = r->getRectangle();
+                            items = sp_document_items_in_box(sp_desktop_document(desktop), desktop->dkey, *b);
+                        } else if (r->getMode() == RUBBERBAND_MODE_TOUCHPATH) {
+                            items = sp_document_items_at_points(sp_desktop_document(desktop), desktop->dkey, r->getPoints());
+                        }
+
                         seltrans->resetState();
-                        // find out affected items:
-                        GSList *items = sp_document_items_in_box(sp_desktop_document(desktop), desktop->dkey, b.assume());
+                        r->stop();
+                        SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
+
                         if (event->button.state & GDK_SHIFT_MASK) {
                             // with shift, add to selection
                             selection->addList (items);
@@ -556,9 +602,9 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                         }
                         g_slist_free (items);
                     } else { // it was just a click, or a too small rubberband
-                        Inkscape::Rubberband::get()->stop();
+                        r->stop();
                         if (sc->button_press_shift && !rb_escaped && !drag_escaped) {
-                            // this was a shift-click, select what was clicked upon
+                            // this was a shift+click or alt+shift+click, select what was clicked upon
 
                             sc->button_press_shift = false;
 
@@ -578,12 +624,13 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                                 item = NULL;
                             }
 
-                        } else if (sc->button_press_ctrl && !rb_escaped && !drag_escaped) { // ctrl-click
-
-                            sc->button_press_ctrl = FALSE;
+                        } else if ((sc->button_press_ctrl || sc->button_press_alt) && !rb_escaped && !drag_escaped) { // ctrl+click, alt+click
 
                             item = sp_event_context_find_item (desktop,
-                                         NR::Point(event->button.x, event->button.y), event->button.state & GDK_MOD1_MASK, TRUE);
+                                         NR::Point(event->button.x, event->button.y), sc->button_press_alt, sc->button_press_ctrl);
+
+                            sc->button_press_ctrl = FALSE;
+                            sc->button_press_alt = FALSE;
 
                             if (item) {
                                 if (selection->includes(item)) {
@@ -610,6 +657,11 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                     sp_canvas_item_ungrab(sc->grabbed, event->button.time);
                     sc->grabbed = NULL;
                 }
+                
+                desktop->updateNow();
+            }
+            if (event->button.button == 1) {
+                Inkscape::Rubberband::get()->stop(); // might have been started in another tool!
             }
             sc->button_press_shift = false;
             sc->button_press_ctrl = false;
@@ -618,15 +670,42 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
 
         case GDK_KEY_PRESS: // keybindings for select context
 
-            if (!key_is_a_modifier (get_group0_keyval (&event->key))) {
+            {
+            guint keyval = get_group0_keyval(&event->key);
+            bool alt = ( MOD__ALT
+                                    || (keyval == GDK_Alt_L)
+                                    || (keyval == GDK_Alt_R)
+                                    || (keyval == GDK_Meta_L)
+                                    || (keyval == GDK_Meta_R));
+
+            if (!key_is_a_modifier (keyval)) {
                     event_context->defaultMessageContext()->clear();
+            } else if (sc->grabbed || seltrans->isGrabbed()) {
+                if (Inkscape::Rubberband::get()->is_started()) {
+                    // if Alt then change cursor to moving cursor:
+                    if (alt) {
+                        Inkscape::Rubberband::get()->setMode(RUBBERBAND_MODE_TOUCHPATH);
+                    }
+                } else {
+                // do not change the statusbar text when mousekey is down to move or transform the object,
+                // because the statusbar text is already updated somewhere else.
+                   break;
+                }
             } else {
                     sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
-                                                _("<b>Ctrl</b>: select in groups, move hor/vert"),
-                                                _("<b>Shift</b>: toggle select, force rubberband, disable snapping"),
-                                                _("<b>Alt</b>: select under, move selected"));
+                                                _("<b>Ctrl</b>: click to select in groups; drag to move hor/vert"),
+                                                _("<b>Shift</b>: click to toggle select; drag for rubberband selection"),
+                                                _("<b>Alt</b>: click to select under; drag to move selected or select by touch"));
+                    // if Alt and nonempty selection, show moving cursor ("move selected"):
+                    if (alt && !selection->isEmpty()) {
+                        GdkCursor *cursor = gdk_cursor_new(GDK_FLEUR);
+                        gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, cursor);
+                        gdk_cursor_destroy(cursor);
+                    }
+                    //*/
                     break;
             }
+            }
 
             switch (get_group0_keyval (&event->key)) {
                 case GDK_Left: // move selection left
@@ -701,18 +780,6 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                         ret = TRUE;
                     }
                     break;
-                case GDK_Tab: // Tab - cycle selection forward
-                    if (!(MOD__CTRL_ONLY || (MOD__CTRL && MOD__SHIFT))) {
-                        sp_selection_item_next();
-                        ret = TRUE;
-                    }
-                    break;
-                case GDK_ISO_Left_Tab: // Shift Tab - cycle selection backward
-                    if (!(MOD__CTRL_ONLY || (MOD__CTRL && MOD__SHIFT))) {
-                        sp_selection_item_prev();
-                        ret = TRUE;
-                    }
-                    break;
                 case GDK_space:
                     /* stamping mode: show outline mode moving */
                     /* FIXME: Is next condition ok? (lauris) */
@@ -790,13 +857,41 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                         ret = TRUE;
                     }
                     break;
+                case GDK_s:
+                case GDK_S:
+                    if (MOD__SHIFT_ONLY) {
+                        if (!selection->isEmpty()) {
+                            seltrans->increaseState();
+                        }
+                        ret = TRUE;
+                    }
+                    break;                  
                 default:
                     break;
             }
             break;
+
         case GDK_KEY_RELEASE:
-            if (key_is_a_modifier (get_group0_keyval (&event->key)))
+            {
+            guint keyval = get_group0_keyval(&event->key);
+            if (key_is_a_modifier (keyval))
                 event_context->defaultMessageContext()->clear();
+
+            bool alt = ( MOD__ALT
+                         || (keyval == GDK_Alt_L)
+                         || (keyval == GDK_Alt_R)
+                         || (keyval == GDK_Meta_L)
+                         || (keyval == GDK_Meta_R));
+
+            if (Inkscape::Rubberband::get()->is_started()) {
+                // if Alt then change cursor to moving cursor:
+                if (alt) {
+                    Inkscape::Rubberband::get()->setMode(RUBBERBAND_MODE_RECT);
+                }
+            }
+            }
+            // set cursor to default.
+            gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, event_context->cursor);
             break;
         default:
             break;