Code

Spelling fix
[inkscape.git] / src / pencil-context.cpp
index a75b4c561e085887ee320616377b7ba436ba93fe..1ee39d5309f9427ccc14007eb6a1fb3b1e7783b5 100644 (file)
@@ -1,4 +1,4 @@
-/** \file 
+/** \file
  * Pencil event context implementation.
  */
 
 #include "libnr/in-svg-plane.h"
 #include "libnr/n-art-bpath.h"
 #include "context-fns.h"
+#include "sp-namedview.h"
+#include "xml/repr.h"
+#include "document.h"
+#include "desktop-style.h"
 
 static void sp_pencil_context_class_init(SPPencilContextClass *klass);
 static void sp_pencil_context_init(SPPencilContext *pc);
@@ -110,7 +114,7 @@ sp_pencil_context_init(SPPencilContext *pc)
     event_context->cursor_shape = cursor_pencil_xpm;
     event_context->hot_x = 4;
     event_context->hot_y = 4;
-    
+
     pc->npoints = 0;
     pc->state = SP_PENCIL_CONTEXT_IDLE;
     pc->req_tangent = NR::Point(0, 0);
@@ -196,18 +200,19 @@ static gint
 pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &bevent)
 {
     gint ret = FALSE;
-    if ( bevent.button == 1 ) {
+    SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
+    if ( bevent.button == 1  && !event_context->space_panning) {
 
         SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
         SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
-        Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
+        Inkscape::Selection *selection = sp_desktop_selection(desktop);
 
         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
             return TRUE;
         }
 
         NR::Point const button_w(bevent.x, bevent.y);
-        
+
         /* Find desktop coordinates */
         NR::Point p = pc->desktop->w2d(button_w);
 
@@ -221,6 +226,30 @@ pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &beve
                 break;
             default:
                 /* Set first point of sequence */
+                if (bevent.state & GDK_CONTROL_MASK) {
+                    /* Create object */
+                    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+                    Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
+                    repr->setAttribute("sodipodi:type", "arc");
+                    SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
+                    Inkscape::GC::release(repr);
+                    NR::Matrix const i2d (sp_item_i2d_affine (item));
+                    NR::Point pp = p * i2d;
+                    sp_repr_set_svg_double (repr, "sodipodi:cx", pp[NR::X]);
+                    sp_repr_set_svg_double (repr, "sodipodi:cy", pp[NR::Y]);
+                    sp_repr_set_int (repr, "sodipodi:rx", 10);
+                    sp_repr_set_int (repr, "sodipodi:ry", 10);
+
+                    /* Set style */
+                    sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.arc", false);
+
+                    item->updateRepr();
+                    desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating single point"));
+                    sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_PENCIL, _("Create single point"));
+                    ret = true;
+                    break;
+                    
+                }
                 if (anchor) {
                     p = anchor->dp;
                     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
@@ -233,8 +262,8 @@ pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &beve
                         // anchor, which is handled by the sibling branch above)
                         selection->clear();
                         desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
-                        SnapManager const m(desktop->namedview);
-                        p = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, p, NULL).getPoint();
+                        SnapManager const &m = desktop->namedview->snap_manager;
+                        p = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL).getPoint();
                     } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
                         desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
                     }
@@ -253,11 +282,17 @@ pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &beve
 static gint
 pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mevent)
 {
+    if (mevent.state & GDK_CONTROL_MASK) {
+        // mouse was accidentally moved during Ctrl+click;
+        // ignore the motion and create a single point
+        return TRUE;
+    }
     gint ret = FALSE;
     SPDesktop *const dt = pc->desktop;
 
-    if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
-        // allow middle-button scrolling
+    SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
+    if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
+        // allow scrolling
         return FALSE;
     }
 
@@ -295,15 +330,15 @@ pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mev
                     /* Create green anchor */
                     pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, pc->p[0]);
                 }
-                /** \todo 
-                 * fixme: I am not sure whether we want to snap to anchors 
+                /** \todo
+                 * fixme: I am not sure whether we want to snap to anchors
                  * in middle of freehand (Lauris)
                  */
                 if (anchor) {
                     p = anchor->dp;
                 } else if ((mevent.state & GDK_SHIFT_MASK) == 0) {
-                    SnapManager const m(dt->namedview);
-                    p = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, p, NULL).getPoint();
+                    SnapManager const &m = dt->namedview->snap_manager;
+                    p = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p, NULL).getPoint();
                 }
                 if ( pc->npoints != 0 ) { // buttonpress may have happened before we entered draw context!
                     spdc_add_freehand_point(pc, p, mevent.state);
@@ -339,7 +374,8 @@ pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &re
 {
     gint ret = FALSE;
 
-    if ( revent.button == 1 && pc->is_drawing) {
+    SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
+    if ( revent.button == 1 && pc->is_drawing && !event_context->space_panning) {
         SPDesktop *const dt = pc->desktop;
 
         pc->is_drawing = false;
@@ -355,7 +391,10 @@ pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &re
             case SP_PENCIL_CONTEXT_IDLE:
                 /* Releasing button in idle mode means single click */
                 /* We have already set up start point/anchor in button_press */
-                pc->state = SP_PENCIL_CONTEXT_ADDLINE;
+                if (!(revent.state & GDK_CONTROL_MASK)) {
+                    // Ctrl+click creates a single point so only set context in ADDLINE mode when Ctrl isn't pressed
+                    pc->state = SP_PENCIL_CONTEXT_ADDLINE;
+                }
                 ret = TRUE;
                 break;
             case SP_PENCIL_CONTEXT_ADDLINE:
@@ -401,12 +440,42 @@ pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &re
             pc->grab = NULL;
         }
 
-        pc->grab = NULL;
         ret = TRUE;
     }
     return ret;
 }
 
+static void
+pencil_cancel (SPPencilContext *const pc) 
+{
+    if (pc->grab) {
+        /* Release grab now */
+        sp_canvas_item_ungrab(pc->grab, 0);
+        pc->grab = NULL;
+    }
+
+    pc->is_drawing = false;
+
+    pc->state = SP_PENCIL_CONTEXT_IDLE;
+
+    sp_curve_reset(pc->red_curve);
+    sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
+    while (pc->green_bpaths) {
+        gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
+        pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
+    }
+    sp_curve_reset(pc->green_curve);
+    if (pc->green_anchor) {
+        pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
+    }
+
+    pc->_message_context->clear();
+    pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
+
+    sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
+}
+
+
 static gint
 pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint const state)
 {
@@ -421,6 +490,25 @@ pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint con
                 ret = TRUE;
             }
             break;
+        case GDK_Escape:
+            if (pc->npoints != 0) {
+                // if drawing, cancel, otherwise pass it up for deselecting
+                if (pc->is_drawing) {
+                    pencil_cancel (pc);
+                    ret = TRUE;
+                }
+            }
+            break;
+        case GDK_z:
+        case GDK_Z:
+            if (mod_ctrl_only(state) && pc->npoints != 0) {
+                // if drawing, cancel, otherwise pass it up for undo
+                if (pc->is_drawing) {
+                    pencil_cancel (pc);
+                    ret = TRUE;
+                }
+            }
+            break;
         default:
             break;
     }
@@ -480,8 +568,8 @@ spdc_set_endpoint(SPPencilContext *const pc, NR::Point const p)
 
 /**
  * Finalize addline.
- * 
- * \todo 
+ *
+ * \todo
  * fixme: I'd like remove red reset from concat colors (lauris).
  * Still not sure, how it will make most sense.
  */
@@ -503,7 +591,7 @@ spdc_finish_endpoint(SPPencilContext *const pc)
 }
 
 static void
-spdc_add_freehand_point(SPPencilContext *pc, NR::Point p, guint state)
+spdc_add_freehand_point(SPPencilContext *pc, NR::Point p, guint /*state*/)
 {
     g_assert( pc->npoints > 0 );
     g_return_if_fail(unsigned(pc->npoints) < G_N_ELEMENTS(pc->p));
@@ -567,8 +655,8 @@ fit_and_split(SPPencilContext *pc)
         sp_curve_append_continuous(pc->green_curve, pc->red_curve, 0.0625);
         SPCurve *curve = sp_curve_copy(pc->red_curve);
 
-        /// \todo fixme: 
-        SPCanvasItem *cshape = sp_canvas_bpath_new(SP_DT_SKETCH(pc->desktop), curve);
+        /// \todo fixme:
+        SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
         sp_curve_unref(curve);
         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);