Code

Split SPCanvasItem and SPCanvasGroup to individual .h files. Removed forward header.
[inkscape.git] / src / select-context.cpp
index a9e1ff62318bd0e381bd5eaa3112260471fdda52..4a8b38c3725fb4ae20b541ec91d9d7feb1ca18df 100644 (file)
@@ -1,12 +1,13 @@
-#define __SP_SELECT_CONTEXT_C__
-
 /*
  * Selection and transformation context
  *
  * Authors:
  *   Lauris Kaplinski <lauris@kaplinski.com>
  *   bulia byak <buliabyak@users.sf.net>
+ *   Abhishek Sharma
+ *   Jon A. Cruz <jon@joncruz.org>
  *
+ * Copyright (C) 2010      authors
  * Copyright (C) 2006      Johan Engelen <johan@shouraizou.nl>
  * Copyright (C) 1999-2005 Authors
  *
 #include "selection-describer.h"
 #include "seltrans.h"
 #include "box3d.h"
+#include "display/sp-canvas.h"
+#include "display/nr-arena-item.h"
+
+using Inkscape::DocumentUndo;
 
 static void sp_select_context_class_init(SPSelectContextClass *klass);
 static void sp_select_context_init(SPSelectContext *select_context);
@@ -108,6 +113,10 @@ sp_select_context_init(SPSelectContext *sc)
     sc->button_press_shift = false;
     sc->button_press_ctrl = false;
     sc->button_press_alt = false;
+    sc->is_cycling = false;
+    sc->cycling_items = NULL;
+    sc->cycling_items_selected_before = NULL;
+    sc->cycling_cur_item = NULL;
     sc->_seltrans = NULL;
     sc->_describer = NULL;
 
@@ -175,7 +184,7 @@ sp_select_context_setup(SPEventContext *ec)
                 desktop->selection, 
                 desktop->messageStack(),
                 _("Click selection to toggle scale/rotation handles"),
-                _("No objects selected. Click, Shift+click, or drag around objects to select.")
+                _("No objects selected. Click, Shift+click, Alt+scroll mouse on top of objects, or drag around objects to select.")
         );
 
     select_context->_seltrans = new Inkscape::SelTrans(desktop);
@@ -222,7 +231,7 @@ sp_select_context_abort(SPEventContext *event_context)
             if (sc->item) {
                 // only undo if the item is still valid
                 if (SP_OBJECT_DOCUMENT( SP_OBJECT(sc->item))) {
-                    sp_document_undo(sp_desktop_document(desktop));
+                    DocumentUndo::undo(sp_desktop_document(desktop));
                 }
 
                 sp_object_unref( SP_OBJECT(sc->item), NULL);
@@ -230,7 +239,7 @@ sp_select_context_abort(SPEventContext *event_context)
                 // NOTE:  This is a workaround to a bug.
                 // When the ctrl key is held, sc->item is not defined
                 // so in this case (only), we skip the object doc check
-                sp_document_undo(sp_desktop_document(desktop));
+                DocumentUndo::undo(sp_desktop_document(desktop));
             }
             sc->item = NULL;
 
@@ -400,6 +409,43 @@ sp_select_context_item_handler(SPEventContext *event_context, SPItem *item, GdkE
     return ret;
 }
 
+static void
+sp_select_context_cycle_through_items(SPSelectContext *sc, Inkscape::Selection *selection, GdkEventScroll *scroll_event, bool shift_pressed) {
+    if (!sc->cycling_cur_item)
+        return;
+
+    NRArenaItem *arenaitem;
+    SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
+    SPItem *item = SP_ITEM(sc->cycling_cur_item->data);
+
+    // Deactivate current item
+    if (!g_list_find(sc->cycling_items_selected_before, item) && selection->includes(item))
+        selection->remove(item);
+    arenaitem = item->get_arenaitem(desktop->dkey);
+    nr_arena_item_set_opacity (arenaitem, 0.3);
+
+    // Find next item and activate it
+    GList *next;
+    if (scroll_event->direction == GDK_SCROLL_UP) {
+        next = sc->cycling_cur_item->next;
+        if (next == NULL)
+            next = sc->cycling_items;
+    } else {
+        next = sc->cycling_cur_item->prev;
+        if (next == NULL)
+            next = g_list_last(sc->cycling_items);
+    }
+    sc->cycling_cur_item = next;
+    item = SP_ITEM(sc->cycling_cur_item->data);
+    arenaitem = item->get_arenaitem(desktop->dkey);
+    nr_arena_item_set_opacity (arenaitem, 1.0);
+
+    if (shift_pressed)
+        selection->add(item);
+    else
+        selection->set(item);
+}
+
 static gint
 sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
 {
@@ -480,6 +526,22 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
             break;
 
         case GDK_MOTION_NOTIFY:
+        {
+            SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
+            if (sc->is_cycling) {
+                // Abort cycle-select
+                NRArenaItem *arenaitem;
+                for (GList *l = sc->cycling_items; l != NULL; l = g_list_next(l)) {
+                    arenaitem = item->get_arenaitem(desktop->dkey);
+                    nr_arena_item_set_opacity (arenaitem, 1.0);
+                }
+                g_list_free(sc->cycling_items);
+                g_list_free(sc->cycling_items_selected_before);
+                sc->cycling_items = NULL;
+                sc->cycling_cur_item = NULL;
+                sc->is_cycling = false;
+            }
+
                tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
                if (event->motion.state & GDK_BUTTON1_MASK && !event_context->space_panning) {
                 Geom::Point const motion_pt(event->motion.x, event->motion.y);
@@ -510,14 +572,14 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                     // and also when we started within tolerance, but trespassed tolerance outside of item
                     Inkscape::Rubberband::get(desktop)->stop();
                     SP_EVENT_CONTEXT(sc)->defaultMessageContext()->clear();
-                    item_at_point = desktop->item_at_point(Geom::Point(event->button.x, event->button.y), FALSE);
+                    item_at_point = desktop->getItemAtPoint(Geom::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(Geom::Point(xp, yp), FALSE);
+                        item_at_point = desktop->getItemAtPoint(Geom::Point(xp, yp), FALSE);
                     if (item_at_point || sc->moved || sc->button_press_alt) {
                         // drag only if starting from an item, or if something is already grabbed, or if alt-dragging
                         if (!sc->moved) {
-                            item_in_group = desktop->item_at_point(Geom::Point(event->button.x, event->button.y), TRUE);
-                            group_at_point = desktop->group_at_point(Geom::Point(event->button.x, event->button.y));
+                            item_in_group = desktop->getItemAtPoint(Geom::Point(event->button.x, event->button.y), TRUE);
+                            group_at_point = desktop->getGroupAtPoint(Geom::Point(event->button.x, event->button.y));
                             if (SP_IS_LAYER(selection->single()))
                                 group_at_point = SP_GROUP(selection->single());
 
@@ -566,6 +628,7 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                 }
             }
             break;
+        }
         case GDK_BUTTON_RELEASE:
             xp = yp = 0;
             if ((event->button.button == 1) && (sc->grabbed) && !event_context->space_panning) {
@@ -608,15 +671,15 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                     }
                     sc->item = NULL;
                 } else {
-                    Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get(desktop);
+                    Inkscape::Rubberband *r = Inkscape::Rubberband::get(desktop);
                     if (r->is_started() && !within_tolerance) {
                         // this was a rubberband drag
                         GSList *items = NULL;
                         if (r->getMode() == RUBBERBAND_MODE_RECT) {
                             Geom::OptRect const b = r->getRectangle();
-                            items = sp_document_items_in_box(sp_desktop_document(desktop), desktop->dkey, *b);
+                            items = sp_desktop_document(desktop)->getItemsInBox(desktop->dkey, *b);
                         } else if (r->getMode() == RUBBERBAND_MODE_TOUCHPATH) {
-                            items = sp_document_items_at_points(sp_desktop_document(desktop), desktop->dkey, r->getPoints());
+                            items = sp_desktop_document(desktop)->getItemsAtPoints(desktop->dkey, r->getPoints());
                         }
 
                         seltrans->resetState();
@@ -698,6 +761,42 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
             sc->button_press_alt = false;
             break;
 
+        case GDK_SCROLL:
+        {
+            SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
+            GdkEventScroll *scroll_event = (GdkEventScroll*) event;
+
+            if (scroll_event->state & GDK_MOD1_MASK) { // alt modified pressed
+                bool shift_pressed = scroll_event->state & GDK_SHIFT_MASK;
+
+                if(sc->is_cycling == false) {
+                    // Build a list of items underneath the mouse pointer (for cycle-selection) and reduce their opacity
+                    Geom::Point p = desktop->d2w(desktop->point());
+                    SPItem *item = desktop->getItemAtPoint(p, true, NULL);
+                    while(item != NULL) {
+                        sc->cycling_items = g_list_append(sc->cycling_items, item);
+                        if (selection->includes(item)) {
+                            // already selected items are stored separately, too
+                            sc->cycling_items_selected_before = g_list_append(sc->cycling_items_selected_before, item);
+                        }
+                        NRArenaItem *arenaitem = item->get_arenaitem(desktop->dkey);
+                        nr_arena_item_set_opacity (arenaitem, 0.3);
+
+                        item = desktop->getItemAtPoint(p, true, item);
+                    }
+                    // Here we set the current item to the bottommost one so that the cycling step below re-starts at the top
+                    sc->cycling_cur_item = g_list_last(sc->cycling_items);
+                    sc->is_cycling = true;
+                }
+
+                // Cycle through the items underneath the mouse pointer, one-by-one
+                sp_select_context_cycle_through_items(sc, selection, scroll_event, shift_pressed);
+
+                ret = TRUE;
+            }
+            break;
+        }
+
         case GDK_KEY_PRESS: // keybindings for select context
 
                        {
@@ -726,7 +825,7 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                     sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
                                                 _("<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"));
+                                                _("<b>Alt</b>: click to select under; scroll mouse-wheel to cycle-select; drag to move selected or select by touch"));
                     // if Alt and nonempty selection, show moving cursor ("move selected"):
                     if (alt && !selection->isEmpty() && !desktop->isWaitingCursor()) {
                         gdk_window_set_cursor(GTK_WIDGET(sp_desktop_canvas(desktop))->window, CursorSelectDragging);
@@ -814,6 +913,7 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                         selection->clear();
                     ret = TRUE;
                     break;
+
                 case GDK_a:
                 case GDK_A:
                     if (MOD__CTRL_ONLY) {
@@ -949,7 +1049,24 @@ sp_select_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                 if (alt) {
                     Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_RECT);
                 }
+            } else {
+                if (alt && sc->is_cycling) {
+                    // quit cycle-selection and reset opacities
+                    SPSelectContext *sc = SP_SELECT_CONTEXT(event_context);
+                    NRArenaItem *arenaitem;
+                    for (GList *l = sc->cycling_items; l != NULL; l = g_list_next(l)) {
+                        arenaitem = SP_ITEM(l->data)->get_arenaitem(desktop->dkey);
+                        nr_arena_item_set_opacity (arenaitem, 1.0);
+                    }
+                    g_list_free(sc->cycling_items);
+                    g_list_free(sc->cycling_items_selected_before);
+                    sc->cycling_items = NULL;
+                    sc->cycling_items_selected_before = NULL;
+                    sc->cycling_cur_item = NULL;
+                    sc->is_cycling = false;
+                }
             }
+
             }
             // set cursor to default.
             if (!desktop->isWaitingCursor()) {