GPencil: Layers with alpha = 0 should not be editable
[blender.git] / source / blender / editors / gpencil / gpencil_edit.c
index 949f92c68e94fee9142d35c0027ae6d262fc3d9a..03d5ed3e24f87531f629f9e6d8abe4d07023db57 100644 (file)
  * Contributor(s): Joshua Leung
  *
  * ***** END GPL LICENSE BLOCK *****
+ *
+ * Operators for editing Grease Pencil strokes
  */
 
 /** \file blender/editors/gpencil/gpencil_edit.c
  *  \ingroup edgpencil
  */
 
 
 #include <stdio.h>
 #include <string.h>
 
 #include "MEM_guardedalloc.h"
 
-
 #include "BLI_math.h"
 #include "BLI_blenlib.h"
 #include "BLI_utildefines.h"
 
-#include "DNA_curve_types.h"
+#include "BLT_translation.h"
+
 #include "DNA_object_types.h"
-#include "DNA_node_types.h"
 #include "DNA_scene_types.h"
 #include "DNA_screen_types.h"
 #include "DNA_space_types.h"
 #include "DNA_gpencil_types.h"
 
 #include "BKE_context.h"
-#include "BKE_curve.h"
+#include "BKE_global.h"
 #include "BKE_gpencil.h"
 #include "BKE_library.h"
-#include "BKE_object.h"
 #include "BKE_report.h"
-#include "BKE_tracking.h"
+#include "BKE_screen.h"
 
+#include "UI_interface.h"
+#include "UI_resources.h"
 
 #include "WM_api.h"
 #include "WM_types.h"
 
 #include "RNA_access.h"
 #include "RNA_define.h"
+#include "RNA_enum_types.h"
 
 #include "UI_view2d.h"
 
 #include "ED_gpencil.h"
+#include "ED_object.h"
 #include "ED_view3d.h"
-#include "ED_clip.h"
 
 #include "gpencil_intern.h"
 
 /* ************************************************ */
-/* Context Wrangling... */
+/* Stroke Edit Mode Management */
 
-/* Get pointer to active Grease Pencil datablock, and an RNA-pointer to trace back to whatever owns it */
-bGPdata **gpencil_data_get_pointers(const bContext *C, PointerRNA *ptr)
+static int gpencil_editmode_toggle_poll(bContext *C)
 {
-       ID *screen_id = (ID *)CTX_wm_screen(C);
-       Scene *scene = CTX_data_scene(C);
-       ScrArea *sa = CTX_wm_area(C);
+       return ED_gpencil_data_get_active(C) != NULL;
+}
+
+static int gpencil_editmode_toggle_exec(bContext *C, wmOperator *UNUSED(op))
+{
+       bGPdata *gpd = ED_gpencil_data_get_active(C);
        
-       /* if there's an active area, check if the particular editor may
-        * have defined any special Grease Pencil context for editing...
-        */
-       if (sa) {
-               switch (sa->spacetype) {
-                       case SPACE_VIEW3D: /* 3D-View */
-                       {
-                               Object *ob = CTX_data_active_object(C);
-                               
-                               /* TODO: we can include other data-types such as bones later if need be... */
+       if (gpd == NULL)
+               return OPERATOR_CANCELLED;
+       
+       /* Just toggle editmode flag... */
+       gpd->flag ^= GP_DATA_STROKE_EDITMODE;
+       
+       WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | ND_GPENCIL_EDITMODE, NULL);
+       WM_event_add_notifier(C, NC_SCENE | ND_MODE, NULL);
+       
+       return OPERATOR_FINISHED;
+}
 
-                               /* just in case no active object */
-                               if (ob) {
-                                       /* for now, as long as there's an object, default to using that in 3D-View */
-                                       if (ptr) RNA_id_pointer_create(&ob->id, ptr);
-                                       return &ob->gpd;
-                               }
+void GPENCIL_OT_editmode_toggle(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name = "Strokes Edit Mode Toggle";
+       ot->idname = "GPENCIL_OT_editmode_toggle";
+       ot->description = "Enter/Exit edit mode for Grease Pencil strokes";
+       
+       /* callbacks */
+       ot->exec = gpencil_editmode_toggle_exec;
+       ot->poll = gpencil_editmode_toggle_poll;
+       
+       /* flags */
+       ot->flag = OPTYPE_UNDO | OPTYPE_REGISTER;
+}
+
+/* ************************************************ */
+/* Stroke Editing Operators */
+
+/* poll callback for all stroke editing operators */
+static int gp_stroke_edit_poll(bContext *C)
+{
+       /* NOTE: this is a bit slower, but is the most accurate... */
+       return CTX_DATA_COUNT(C, editable_gpencil_strokes) != 0;
+}
+
+/* ************** Duplicate Selected Strokes **************** */
+
+/* Make copies of selected point segments in a selected stroke */
+static void gp_duplicate_points(const bGPDstroke *gps, ListBase *new_strokes)
+{
+       bGPDspoint *pt;
+       int i;
+       
+       int start_idx = -1;
+       
+       
+       /* Step through the original stroke's points:
+        * - We accumulate selected points (from start_idx to current index)
+        *   and then convert that to a new stroke
+        */
+       for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
+               /* searching for start, are waiting for end? */
+               if (start_idx == -1) {
+                       /* is this the first selected point for a new island? */
+                       if (pt->flag & GP_SPOINT_SELECT) {
+                               start_idx = i;
                        }
-                       break;
+               }
+               else {
+                       size_t len = 0;
                        
-                       case SPACE_NODE: /* Nodes Editor */
-                       {
-                               SpaceNode *snode = (SpaceNode *)CTX_wm_space_data(C);
-                               
-                               /* return the GP data for the active node block/node */
-                               if (snode && snode->nodetree) {
-                                       /* for now, as long as there's an active node tree, default to using that in the Nodes Editor */
-                                       if (ptr) RNA_id_pointer_create(&snode->nodetree->id, ptr);
-                                       return &snode->nodetree->gpd;
-                               }
-                               else {
-                                       /* even when there is no node-tree, don't allow this to flow to scene */
-                                       return NULL;
-                               }
+                       /* is this the end of current island yet?
+                        * 1) Point i-1 was the last one that was selected
+                        * 2) Point i is the last in the array
+                        */
+                       if ((pt->flag & GP_SPOINT_SELECT) == 0) {
+                               len = i - start_idx;
                        }
-                       break;
+                       else if (i == gps->totpoints - 1) {
+                               len = i - start_idx + 1;
+                       }
+                       //printf("copying from %d to %d = %d\n", start_idx, i, len);
+               
+                       /* make copies of the relevant data */
+                       if (len) {
+                               bGPDstroke *gpsd;
                                
-                       case SPACE_SEQ: /* Sequencer */
-                       {
-                               SpaceSeq *sseq = (SpaceSeq *)CTX_wm_space_data(C);
+                               /* make a stupid copy first of the entire stroke (to get the flags too) */
+                               gpsd = MEM_dupallocN(gps);
                                
-                               /* for now, Grease Pencil data is associated with the space (actually preview region only) */
-                               /* XXX our convention for everything else is to link to data though... */
-                               if (ptr) RNA_pointer_create(screen_id, &RNA_SpaceSequenceEditor, sseq, ptr);
-                               return &sseq->gpd;
-                       }
-                       break;
-
-                       case SPACE_IMAGE: /* Image/UV Editor */
-                       {
-                               SpaceImage *sima = (SpaceImage *)CTX_wm_space_data(C);
-
-                               /* for now, Grease Pencil data is associated with the space... */
-                               /* XXX our convention for everything else is to link to data though... */
-                               if (ptr) RNA_pointer_create(screen_id, &RNA_SpaceImageEditor, sima, ptr);
-                               return &sima->gpd;
-                       }
-                       break;
+                               /* now, make a new points array, and copy of the relevant parts */
+                               gpsd->points = MEM_callocN(sizeof(bGPDspoint) * len, "gps stroke points copy");
+                               memcpy(gpsd->points, gps->points + start_idx, sizeof(bGPDspoint) * len);
+                               gpsd->totpoints = len;
                                
-                       case SPACE_CLIP: /* Nodes Editor */
-                       {
-                               SpaceClip *sc = (SpaceClip *)CTX_wm_space_data(C);
-                               MovieClip *clip = ED_space_clip_get_clip(sc);
+                               /* add to temp buffer */
+                               gpsd->next = gpsd->prev = NULL;
+                               BLI_addtail(new_strokes, gpsd);
                                
-                               if (clip) {
-                                       if (sc->gpencil_src == SC_GPENCIL_SRC_TRACK) {
-                                               MovieTrackingTrack *track = BKE_tracking_track_get_active(&clip->tracking);
-
-                                               if (!track)
-                                                       return NULL;
-
-                                               if (ptr)
-                                                       RNA_pointer_create(&clip->id, &RNA_MovieTrackingTrack, track, ptr);
-
-                                               return &track->gpd;
-                                       }
-                                       else {
-                                               if (ptr)
-                                                       RNA_id_pointer_create(&clip->id, ptr);
+                               /* cleanup + reset for next */
+                               start_idx = -1;
+                       }
+               }
+       }
+}
 
-                                               return &clip->gpd;
-                                       }
+static int gp_duplicate_exec(bContext *C, wmOperator *op)
+{
+       bGPdata *gpd = ED_gpencil_data_get_active(C);
+       
+       if (gpd == NULL) {
+               BKE_report(op->reports, RPT_ERROR, "No Grease Pencil data");
+               return OPERATOR_CANCELLED;
+       }
+       
+       /* for each visible (and editable) layer's selected strokes,
+        * copy the strokes into a temporary buffer, then append
+        * once all done
+        */
+       CTX_DATA_BEGIN(C, bGPDlayer *, gpl, editable_gpencil_layers)
+       {
+               ListBase new_strokes = {NULL, NULL};
+               bGPDframe *gpf = gpl->actframe;
+               bGPDstroke *gps;
+               
+               if (gpf == NULL)
+                       continue;
+               
+               /* make copies of selected strokes, and deselect these once we're done */
+               for (gps = gpf->strokes.first; gps; gps = gps->next) {
+                       /* skip strokes that are invalid for current view */
+                       if (ED_gpencil_stroke_can_use(C, gps) == false)
+                               continue;
+                       
+                       if (gps->flag & GP_STROKE_SELECT) {
+                               if (gps->totpoints == 1) {
+                                       /* Special Case: If there's just a single point in this stroke... */
+                                       bGPDstroke *gpsd;
+                                       
+                                       /* make direct copies of the stroke and its points */
+                                       gpsd = MEM_dupallocN(gps);
+                                       gpsd->points = MEM_dupallocN(gps->points);
+                                       
+                                       /* add to temp buffer */
+                                       gpsd->next = gpsd->prev = NULL;
+                                       BLI_addtail(&new_strokes, gpsd);
+                               }
+                               else {
+                                       /* delegate to a helper, as there's too much to fit in here (for copying subsets)... */
+                                       gp_duplicate_points(gps, &new_strokes);
                                }
-                       }
-                       break;
                                
-                       default: /* unsupported space */
-                               return NULL;
+                               /* deselect original stroke, or else the originals get moved too
+                                * (when using the copy + move macro)
+                                */
+                               gps->flag &= ~GP_STROKE_SELECT;
+                       }
                }
+               
+               /* add all new strokes in temp buffer to the frame (preventing double-copies) */
+               BLI_movelisttolist(&gpf->strokes, &new_strokes);
+               BLI_assert(new_strokes.first == NULL);
        }
+       CTX_DATA_END;
+       
+       /* updates */
+       WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
        
-       /* just fall back on the scene's GP data */
-       if (ptr) RNA_id_pointer_create((ID *)scene, ptr);
-       return (scene) ? &scene->gpd : NULL;
+       return OPERATOR_FINISHED;
 }
 
-/* Get the active Grease Pencil datablock */
-bGPdata *gpencil_data_get_active(const bContext *C)
+void GPENCIL_OT_duplicate(wmOperatorType *ot)
 {
-       bGPdata **gpd_ptr = gpencil_data_get_pointers(C, NULL);
-       return (gpd_ptr) ? *(gpd_ptr) : NULL;
+       /* identifiers */
+       ot->name = "Duplicate Strokes";
+       ot->idname = "GPENCIL_OT_duplicate";
+       ot->description = "Duplicate the selected Grease Pencil strokes";
+       
+       /* callbacks */
+       ot->exec = gp_duplicate_exec;
+       ot->poll = gp_stroke_edit_poll;
+       
+       /* flags */
+       ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 }
 
-/* needed for offscreen rendering */
-bGPdata *gpencil_data_get_active_v3d(Scene *scene)
-{
-       bGPdata *gpd = scene->basact ? scene->basact->object->gpd : NULL;
-       return gpd ? gpd : scene->gpd;
-}
+/* ******************* Copy/Paste Strokes ************************* */
+/* Grease Pencil stroke data copy/paste buffer:
+ * - The copy operation collects all segments of selected strokes,
+ *   dumping "ready to be copied" copies of the strokes into the buffer.
+ * - The paste operation makes a copy of those elements, and adds them
+ *   to the active layer. This effectively flattens down the strokes
+ *   from several different layers into a single layer.
+ */
 
-/* ************************************************ */
-/* Panel Operators */
+/* list of bGPDstroke instances */
+/* NOTE: is exposed within the editors/gpencil module so that other tools can use it too */
+ListBase gp_strokes_copypastebuf = {NULL, NULL};
 
-/* poll callback for adding data/layers - special */
-static int gp_add_poll(bContext *C)
+/* Free copy/paste buffer data */
+void ED_gpencil_strokes_copybuf_free(void)
 {
-       /* the base line we have is that we have somewhere to add Grease Pencil data */
-       return gpencil_data_get_pointers(C, NULL) != NULL;
+       bGPDstroke *gps, *gpsn;
+       
+       for (gps = gp_strokes_copypastebuf.first; gps; gps = gpsn) {
+               gpsn = gps->next;
+               
+               MEM_freeN(gps->points);
+               BLI_freelinkN(&gp_strokes_copypastebuf, gps);
+       }
+       
+       BLI_listbase_clear(&gp_strokes_copypastebuf);
 }
 
-/* ******************* Add New Data ************************ */
+/* --------------------- */
+/* Copy selected strokes */
 
-/* add new datablock - wrapper around API */
-static int gp_data_add_exec(bContext *C, wmOperator *op)
+static int gp_strokes_copy_exec(bContext *C, wmOperator *op)
 {
-       bGPdata **gpd_ptr = gpencil_data_get_pointers(C, NULL);
+       bGPdata *gpd = ED_gpencil_data_get_active(C);
        
-       if (gpd_ptr == NULL) {
-               BKE_report(op->reports, RPT_ERROR, "Nowhere for Grease Pencil data to go");
+       if (gpd == NULL) {
+               BKE_report(op->reports, RPT_ERROR, "No Grease Pencil data");
                return OPERATOR_CANCELLED;
        }
-       else {
-               /* decrement user count and add new datablock */
-               bGPdata *gpd = (*gpd_ptr);
+       
+       /* clear the buffer first */
+       ED_gpencil_strokes_copybuf_free();
+       
+       /* for each visible (and editable) layer's selected strokes,
+        * copy the strokes into a temporary buffer, then append
+        * once all done
+        */
+       CTX_DATA_BEGIN(C, bGPDlayer *, gpl, editable_gpencil_layers)
+       {
+               bGPDframe *gpf = gpl->actframe;
+               bGPDstroke *gps;
+               
+               if (gpf == NULL)
+                       continue;
                
-               id_us_min(&gpd->id);
-               *gpd_ptr = gpencil_data_addnew("GPencil");
+               /* make copies of selected strokes, and deselect these once we're done */
+               for (gps = gpf->strokes.first; gps; gps = gps->next) {
+                       /* skip strokes that are invalid for current view */
+                       if (ED_gpencil_stroke_can_use(C, gps) == false)
+                               continue;
+                       
+                       if (gps->flag & GP_STROKE_SELECT) {
+                               if (gps->totpoints == 1) {
+                                       /* Special Case: If there's just a single point in this stroke... */
+                                       bGPDstroke *gpsd;
+                                       
+                                       /* make direct copies of the stroke and its points */
+                                       gpsd = MEM_dupallocN(gps);
+                                       gpsd->points = MEM_dupallocN(gps->points);
+                                       
+                                       /* add to temp buffer */
+                                       gpsd->next = gpsd->prev = NULL;
+                                       BLI_addtail(&gp_strokes_copypastebuf, gpsd);
+                               }
+                               else {
+                                       /* delegate to a helper, as there's too much to fit in here (for copying subsets)... */
+                                       gp_duplicate_points(gps, &gp_strokes_copypastebuf);
+                               }
+                       }
+               }
        }
+       CTX_DATA_END;
        
-       /* notifiers */
-       WM_event_add_notifier(C, NC_SCREEN | ND_GPENCIL | NA_EDITED, NULL); // XXX need a nicer one that will work
-       
+       /* done - no updates needed */
        return OPERATOR_FINISHED;
 }
 
-void GPENCIL_OT_data_add(wmOperatorType *ot)
+void GPENCIL_OT_copy(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name = "Grease Pencil Add New";
-       ot->idname = "GPENCIL_OT_data_add";
-       ot->description = "Add new Grease Pencil datablock";
-       ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+       ot->name = "Copy Strokes";
+       ot->idname = "GPENCIL_OT_copy";
+       ot->description = "Copy selected Grease Pencil points and strokes";
        
        /* callbacks */
-       ot->exec = gp_data_add_exec;
-       ot->poll = gp_add_poll;
-}
-
-/* ******************* Unlink Data ************************ */
-
-/* poll callback for adding data/layers - special */
-static int gp_data_unlink_poll(bContext *C)
-{
-       bGPdata **gpd_ptr = gpencil_data_get_pointers(C, NULL);
+       ot->exec = gp_strokes_copy_exec;
+       ot->poll = gp_stroke_edit_poll;
        
-       /* if we have access to some active data, make sure there's a datablock before enabling this */
-       return (gpd_ptr && *gpd_ptr);
+       /* flags */
+       //ot->flag = OPTYPE_REGISTER;
 }
 
+/* --------------------- */
+/* Paste selected strokes */
 
-/* unlink datablock - wrapper around API */
-static int gp_data_unlink_exec(bContext *C, wmOperator *op)
+static int gp_strokes_paste_exec(bContext *C, wmOperator *op)
 {
-       bGPdata **gpd_ptr = gpencil_data_get_pointers(C, NULL);
+       Scene *scene = CTX_data_scene(C);
+       bGPdata *gpd = ED_gpencil_data_get_active(C);
+       bGPDlayer *gpl = CTX_data_active_gpencil_layer(C);
+       bGPDframe *gpf;
        
-       if (gpd_ptr == NULL) {
-               BKE_report(op->reports, RPT_ERROR, "Nowhere for Grease Pencil data to go");
+       /* check for various error conditions */
+       if (gpd == NULL) {
+               BKE_report(op->reports, RPT_ERROR, "No Grease Pencil data");
+               return OPERATOR_CANCELLED;
+       }
+       else if (BLI_listbase_is_empty(&gp_strokes_copypastebuf)) {
+               BKE_report(op->reports, RPT_ERROR, "No strokes to paste, select and copy some points before trying again");
+               return OPERATOR_CANCELLED;
+       }
+       else if (gpl == NULL) {
+               /* no active layer - let's just create one */
+               gpl = gpencil_layer_addnew(gpd, DATA_("GP_Layer"), true);
+       }
+       else if (gpencil_layer_is_editable(gpl) == false) {
+               BKE_report(op->reports, RPT_ERROR, "Can not paste strokes when active layer is hidden or locked");
                return OPERATOR_CANCELLED;
        }
        else {
-               /* just unlink datablock now, decreasing its user count */
-               bGPdata *gpd = (*gpd_ptr);
+               /* Check that some of the strokes in the buffer can be used */
+               bGPDstroke *gps;
+               bool ok = false;
                
-               id_us_min(&gpd->id);
-               *gpd_ptr = NULL;
+               for (gps = gp_strokes_copypastebuf.first; gps; gps = gps->next) {
+                       if (ED_gpencil_stroke_can_use(C, gps)) {
+                               ok = true;
+                               break;
+                       }
+               }
+               
+               if (ok == false) {
+                       /* XXX: this check is not 100% accurate (i.e. image editor is incompatible with normal 2D strokes),
+                        * but should be enough to give users a good idea of what's going on
+                        */
+                       if (CTX_wm_area(C)->spacetype == SPACE_VIEW3D)
+                               BKE_report(op->reports, RPT_ERROR, "Cannot paste 2D strokes in 3D View");
+                       else
+                               BKE_report(op->reports, RPT_ERROR, "Cannot paste 3D strokes in 2D editors");
+                               
+                       return OPERATOR_CANCELLED;
+               }
        }
        
-       /* notifiers */
-       WM_event_add_notifier(C, NC_SCREEN | ND_GPENCIL | NA_EDITED, NULL); // XXX need a nicer one that will work
+       /* Deselect all strokes first */
+       CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes)
+       {
+               bGPDspoint *pt;
+               int i;
+               
+               for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
+                       pt->flag &= ~GP_SPOINT_SELECT;
+               }
+               
+               gps->flag &= ~GP_STROKE_SELECT;
+       }
+       CTX_DATA_END;
+       
+       /* Ensure we have a frame to draw into
+        * NOTE: Since this is an op which creates strokes,
+        *       we are obliged to add a new frame if one
+        *       doesn't exist already
+        */
+       gpf = gpencil_layer_getframe(gpl, CFRA, true);
+       
+       if (gpf) {
+               bGPDstroke *gps;
+               
+               /* Copy each stroke into the layer */
+               for (gps = gp_strokes_copypastebuf.first; gps; gps = gps->next) {
+                       if (ED_gpencil_stroke_can_use(C, gps)) {
+                               bGPDstroke *new_stroke = MEM_dupallocN(gps);
+                               
+                               new_stroke->points = MEM_dupallocN(gps->points);
+                               new_stroke->next = new_stroke->prev = NULL;
+                               
+                               BLI_addtail(&gpf->strokes, new_stroke);
+                       }
+               }
+       }
+       
+       /* updates */
+       WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
        
        return OPERATOR_FINISHED;
 }
 
-void GPENCIL_OT_data_unlink(wmOperatorType *ot)
+void GPENCIL_OT_paste(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name = "Grease Pencil Unlink";
-       ot->idname = "GPENCIL_OT_data_unlink";
-       ot->description = "Unlink active Grease Pencil datablock";
-       ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+       ot->name = "Paste Strokes";
+       ot->idname = "GPENCIL_OT_paste";
+       ot->description = "Paste previously copied strokes into active layer";
        
        /* callbacks */
-       ot->exec = gp_data_unlink_exec;
-       ot->poll = gp_data_unlink_poll;
+       ot->exec = gp_strokes_paste_exec;
+       ot->poll = gp_stroke_edit_poll;
+       
+       /* flags */
+       ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 }
 
-/* ******************* Add New Layer ************************ */
+/* ******************* Move To Layer ****************************** */
 
-/* add new layer - wrapper around API */
-static int gp_layer_add_exec(bContext *C, wmOperator *op)
+static int gp_move_to_layer_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(evt))
 {
-       bGPdata **gpd_ptr = gpencil_data_get_pointers(C, NULL);
+       uiPopupMenu *pup;
+       uiLayout *layout;
        
-       /* if there's no existing Grease-Pencil data there, add some */
-       if (gpd_ptr == NULL) {
-               BKE_report(op->reports, RPT_ERROR, "Nowhere for Grease Pencil data to go");
-               return OPERATOR_CANCELLED;
+       /* call the menu, which will call this operator again, hence the canceled */
+       pup = UI_popup_menu_begin(C, op->type->name, ICON_NONE);
+       layout = UI_popup_menu_layout(pup);
+       uiItemsEnumO(layout, "GPENCIL_OT_move_to_layer", "layer");
+       UI_popup_menu_end(C, pup);
+       
+       return OPERATOR_INTERFACE;
+}
+
+// FIXME: allow moving partial strokes
+static int gp_move_to_layer_exec(bContext *C, wmOperator *op)
+{
+       bGPdata *gpd = CTX_data_gpencil_data(C);
+       bGPDlayer *target_layer = NULL;
+       ListBase strokes = {NULL, NULL};
+       int layer_num = RNA_enum_get(op->ptr, "layer");
+       
+       /* Get layer or create new one */
+       if (layer_num == -1) {
+               /* Create layer */
+               target_layer = gpencil_layer_addnew(gpd, DATA_("GP_Layer"), true);
        }
-       if (*gpd_ptr == NULL)
-               *gpd_ptr = gpencil_data_addnew("GPencil");
+       else {
+               /* Try to get layer */
+               target_layer = BLI_findlink(&gpd->layers, layer_num);
                
-       /* add new layer now */
-       gpencil_layer_addnew(*gpd_ptr);
+               if (target_layer == NULL) {
+                       BKE_reportf(op->reports, RPT_ERROR, "There is no layer number %d", layer_num);
+                       return OPERATOR_CANCELLED;
+               }
+       }
        
-       /* notifiers */
-       WM_event_add_notifier(C, NC_SCREEN | ND_GPENCIL | NA_EDITED, NULL); // XXX please work!
+       /* Extract all strokes to move to this layer
+        * NOTE: We need to do this in a two-pass system to avoid conflicts with strokes
+        *       getting repeatedly moved
+        */
+       CTX_DATA_BEGIN(C, bGPDlayer *, gpl, editable_gpencil_layers)
+       {
+               bGPDframe *gpf = gpl->actframe;
+               bGPDstroke *gps, *gpsn;
+               
+               /* skip if no frame with strokes, or if this is the layer we're moving strokes to */
+               if ((gpl == target_layer) || (gpf == NULL))
+                       continue;
+               
+               /* make copies of selected strokes, and deselect these once we're done */
+               for (gps = gpf->strokes.first; gps; gps = gpsn) {
+                       gpsn = gps->next;
+                       
+                       /* skip strokes that are invalid for current view */
+                       if (ED_gpencil_stroke_can_use(C, gps) == false)
+                               continue;
+                       
+                       /* TODO: Don't just move entire strokes - instead, only copy the selected portions... */
+                       if (gps->flag & GP_STROKE_SELECT) {
+                               BLI_remlink(&gpf->strokes, gps);
+                               BLI_addtail(&strokes, gps);
+                       }
+               }
+       }
+       CTX_DATA_END;
+       
+       /* Paste them all in one go */
+       if (strokes.first) {
+               Scene *scene = CTX_data_scene(C);
+               bGPDframe *gpf = gpencil_layer_getframe(target_layer, CFRA, true);
+               
+               BLI_movelisttolist(&gpf->strokes, &strokes);
+               BLI_assert((strokes.first == strokes.last) && (strokes.first == NULL));
+       }
+       
+       /* updates */
+       WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
        
        return OPERATOR_FINISHED;
 }
 
-void GPENCIL_OT_layer_add(wmOperatorType *ot)
+void GPENCIL_OT_move_to_layer(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name = "Add New Layer";
-       ot->idname = "GPENCIL_OT_layer_add";
-       ot->description = "Add new Grease Pencil layer for the active Grease Pencil datablock";
-       ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+       ot->name = "Move Strokes to Layer";
+       ot->idname = "GPENCIL_OT_move_to_layer";
+       ot->description = "Move selected strokes to another layer"; // XXX: allow moving individual points too?
        
        /* callbacks */
-       ot->exec = gp_layer_add_exec;
-       ot->poll = gp_add_poll;
+       ot->invoke = gp_move_to_layer_invoke;
+       ot->exec = gp_move_to_layer_exec;
+       ot->poll = gp_stroke_edit_poll; // XXX?
+       
+       /* flags */
+       ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+       
+       /* gp layer to use (dynamic enum) */
+       ot->prop = RNA_def_enum(ot->srna, "layer", DummyRNA_DEFAULT_items, 0, "Grease Pencil Layer", "");
+       RNA_def_enum_funcs(ot->prop, ED_gpencil_layers_with_new_enum_itemf);
 }
 
 /* ******************* Delete Active Frame ************************ */
 
 static int gp_actframe_delete_poll(bContext *C)
 {
-       bGPdata *gpd = gpencil_data_get_active(C);
+       bGPdata *gpd = ED_gpencil_data_get_active(C);
        bGPDlayer *gpl = gpencil_layer_getactive(gpd);
        
        /* only if there's an active layer with an active frame */
@@ -342,13 +597,13 @@ static int gp_actframe_delete_poll(bContext *C)
 static int gp_actframe_delete_exec(bContext *C, wmOperator *op)
 {
        Scene *scene = CTX_data_scene(C);
-       bGPdata *gpd = gpencil_data_get_active(C);
+       bGPdata *gpd = ED_gpencil_data_get_active(C);
        bGPDlayer *gpl = gpencil_layer_getactive(gpd);
        bGPDframe *gpf = gpencil_layer_getframe(gpl, CFRA, 0);
        
        /* if there's no existing Grease-Pencil data there, add some */
        if (gpd == NULL) {
-               BKE_report(op->reports, RPT_ERROR, "No Grease Pencil data");
+               BKE_report(op->reports, RPT_ERROR, "No grease pencil data");
                return OPERATOR_CANCELLED;
        }
        if (ELEM(NULL, gpl, gpf)) {
@@ -360,7 +615,7 @@ static int gp_actframe_delete_exec(bContext *C, wmOperator *op)
        gpencil_layer_delframe(gpl, gpf);
        
        /* notifiers */
-       WM_event_add_notifier(C, NC_SCREEN | ND_GPENCIL | NA_EDITED, NULL); // XXX please work!
+       WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
        
        return OPERATOR_FINISHED;
 }
@@ -371,6 +626,7 @@ void GPENCIL_OT_active_frame_delete(wmOperatorType *ot)
        ot->name = "Delete Active Frame";
        ot->idname = "GPENCIL_OT_active_frame_delete";
        ot->description = "Delete the active frame for the active Grease Pencil datablock";
+       
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
        
        /* callbacks */
@@ -378,294 +634,560 @@ void GPENCIL_OT_active_frame_delete(wmOperatorType *ot)
        ot->poll = gp_actframe_delete_poll;
 }
 
-/* ************************************************ */
-/* Grease Pencil to Data Operator */
-
-/* defines for possible modes */
-enum {
-       GP_STROKECONVERT_PATH = 1,
-       GP_STROKECONVERT_CURVE,
-};
+/* ******************* Delete Operator ************************ */
 
-/* RNA enum define */
-static EnumPropertyItem prop_gpencil_convertmodes[] = {
-       {GP_STROKECONVERT_PATH, "PATH", 0, "Path", ""},
-       {GP_STROKECONVERT_CURVE, "CURVE", 0, "Bezier Curve", ""},
-       {0, NULL, 0, NULL, NULL}
-};
+typedef enum eGP_DeleteMode {
+       /* delete selected stroke points */
+       GP_DELETEOP_POINTS          = 0,
+       /* delete selected strokes */
+       GP_DELETEOP_STROKES         = 1,
+       /* delete active frame */
+       GP_DELETEOP_FRAME           = 2,
+} eGP_DeleteMode;
 
-/* --- */
+/* ----------------------------------- */
 
-/* convert the coordinates from the given stroke point into 3d-coordinates 
- *     - assumes that the active space is the 3D-View
- */
-static void gp_strokepoint_convertcoords(bContext *C, bGPDstroke *gps, bGPDspoint *pt, float p3d[3], rctf *subrect)
+/* Delete selected strokes */
+static int gp_delete_selected_strokes(bContext *C)
 {
-       Scene *scene = CTX_data_scene(C);
-       View3D *v3d = CTX_wm_view3d(C);
-       ARegion *ar = CTX_wm_region(C);
+       bool changed = false;
        
-       if (gps->flag & GP_STROKE_3DSPACE) {
-               /* directly use 3d-coordinates */
-               copy_v3_v3(p3d, &pt->x);
-       }
-       else {
-               float *fp = give_cursor(scene, v3d);
-               float mvalf[2];
+       CTX_DATA_BEGIN(C, bGPDlayer *, gpl, editable_gpencil_layers)
+       {
+               bGPDframe *gpf = gpl->actframe;
+               bGPDstroke *gps, *gpsn;
                
-               /* get screen coordinate */
-               if (gps->flag & GP_STROKE_2DSPACE) {
-                       int mvali[2];
-                       View2D *v2d = &ar->v2d;
-                       UI_view2d_view_to_region(v2d, pt->x, pt->y, mvali, mvali + 1);
-                       VECCOPY2D(mvalf, mvali);
-               }
-               else {
-                       if (subrect) {
-                               mvalf[0] = (((float)pt->x / 100.0f) * (subrect->xmax - subrect->xmin)) + subrect->xmin;
-                               mvalf[1] = (((float)pt->y / 100.0f) * (subrect->ymax - subrect->ymin)) + subrect->ymin;
-                       }
-                       else {
-                               mvalf[0] = (float)pt->x / 100.0f * ar->winx;
-                               mvalf[1] = (float)pt->y / 100.0f * ar->winy;
+               if (gpf == NULL)
+                       continue;
+               
+               /* simply delete strokes which are selected */
+               for (gps = gpf->strokes.first; gps; gps = gpsn) {
+                       gpsn = gps->next;
+                       
+                       /* skip strokes that are invalid for current view */
+                       if (ED_gpencil_stroke_can_use(C, gps) == false)
+                               continue;
+                       
+                       /* free stroke if selected */
+                       if (gps->flag & GP_STROKE_SELECT) {
+                               /* free stroke memory arrays, then stroke itself */
+                               if (gps->points) MEM_freeN(gps->points);
+                               BLI_freelinkN(&gpf->strokes, gps);
+                               
+                               changed = true;
                        }
                }
+       }
+       CTX_DATA_END;
+       
+       if (changed) {
+               WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
+               return OPERATOR_FINISHED;
+       }
+       else {
+               return OPERATOR_CANCELLED;
+       }
+}
+
+/* ----------------------------------- */
 
-               /* convert screen coordinate to 3d coordinates 
-                *      - method taken from editview.c - mouse_cursor() 
+/* Delete selected points but keep the stroke */
+static int gp_dissolve_selected_points(bContext *C)
+{
+       bool changed = false;
+       
+       CTX_DATA_BEGIN(C, bGPDlayer *, gpl, editable_gpencil_layers)
+       {
+               bGPDframe *gpf = gpl->actframe;
+               bGPDstroke *gps, *gpsn;
+               
+               if (gpf == NULL)
+                       continue;
+               
+               /* simply delete points from selected strokes
+                * NOTE: we may still have to remove the stroke if it ends up having no points!
                 */
-               ED_view3d_win_to_3d(ar, fp, mvalf, p3d);
+               for (gps = gpf->strokes.first; gps; gps = gpsn) {
+                       gpsn = gps->next;
+                       
+                       /* skip strokes that are invalid for current view */
+                       if (ED_gpencil_stroke_can_use(C, gps) == false)
+                               continue;
+                       
+                       if (gps->flag & GP_STROKE_SELECT) {
+                               bGPDspoint *pt;
+                               int i;
+                               
+                               int tot = gps->totpoints; /* number of points in new buffer */
+                               
+                               /* First Pass: Count how many points are selected (i.e. how many to remove) */
+                               for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
+                                       if (pt->flag & GP_SPOINT_SELECT) {
+                                               /* selected point - one of the points to remove */
+                                               tot--;
+                                       }
+                               }
+                               
+                               /* if no points are left, we simply delete the entire stroke */
+                               if (tot <= 0) {
+                                       /* remove the entire stroke */
+                                       MEM_freeN(gps->points);
+                                       BLI_freelinkN(&gpf->strokes, gps);
+                               }
+                               else {
+                                       /* just copy all unselected into a smaller buffer */
+                                       bGPDspoint *new_points = MEM_callocN(sizeof(bGPDspoint) * tot, "new gp stroke points copy");
+                                       bGPDspoint *npt        = new_points;
+                                       
+                                       for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
+                                               if ((pt->flag & GP_SPOINT_SELECT) == 0) {
+                                                       *npt = *pt;
+                                                       npt++;
+                                               }
+                                       }
+                                       
+                                       /* free the old buffer */
+                                       MEM_freeN(gps->points);
+                                       
+                                       /* save the new buffer */
+                                       gps->points = new_points;
+                                       gps->totpoints = tot;
+                                       
+                                       /* deselect the stroke, since none of its selected points will still be selected */
+                                       gps->flag &= ~GP_STROKE_SELECT;
+                               }
+                               
+                               changed = true;
+                       }
+               }
+       }
+       CTX_DATA_END;
+       
+       if (changed) {
+               WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
+               return OPERATOR_FINISHED;
+       }
+       else {
+               return OPERATOR_CANCELLED;
        }
 }
 
-/* --- */
+/* ----------------------------------- */
+
+/* Temp data for storing information about an "island" of points
+ * that should be kept when splitting up a stroke. Used in:
+ * gp_stroke_delete_tagged_points()
+ */
+typedef struct tGPDeleteIsland {
+       int start_idx;
+       int end_idx;
+} tGPDeleteIsland;
+
 
-/* convert stroke to 3d path */
-static void gp_stroke_to_path(bContext *C, bGPDlayer *gpl, bGPDstroke *gps, Curve *cu, rctf *subrect)
+/* Split the given stroke into several new strokes, partitioning
+ * it based on whether the stroke points have a particular flag
+ * is set (e.g. "GP_SPOINT_SELECT" in most cases, but not always)
+ *
+ * The algorithm used here is as follows:
+ * 1) We firstly identify the number of "islands" of non-tagged points
+ *    which will all end up being in new strokes.
+ *    - In the most extreme case (i.e. every other vert is a 1-vert island),
+ *      we have at most n / 2 islands
+ *    - Once we start having larger islands than that, the number required
+ *      becomes much less
+ * 2) Each island gets converted to a new stroke
+ */
+void gp_stroke_delete_tagged_points(bGPDframe *gpf, bGPDstroke *gps, bGPDstroke *next_stroke, int tag_flags)
 {
+       tGPDeleteIsland *islands = MEM_callocN(sizeof(tGPDeleteIsland) * (gps->totpoints + 1) / 2, "gp_point_islands");
+       bool in_island  = false;
+       int num_islands = 0;
+       
        bGPDspoint *pt;
-       Nurb *nu;
-       BPoint *bp;
        int i;
-
-       /* create new 'nurb' within the curve */
-       nu = (Nurb *)MEM_callocN(sizeof(Nurb), "gpstroke_to_path(nurb)");
-       
-       nu->pntsu = gps->totpoints;
-       nu->pntsv = 1;
-       nu->orderu = gps->totpoints;
-       nu->flagu = CU_NURB_ENDPOINT;
-       nu->resolu = 32;
        
-       nu->bp = (BPoint *)MEM_callocN(sizeof(BPoint) * gps->totpoints, "bpoints");
+       /* First Pass: Identify start/end of islands */
+       for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
+               if (pt->flag & tag_flags) {
+                       /* selected - stop accumulating to island */
+                       in_island = false;
+               }
+               else {
+                       /* unselected - start of a new island? */
+                       int idx;
+                       
+                       if (in_island) {
+                               /* extend existing island */
+                               idx = num_islands - 1;
+                               islands[idx].end_idx = i;
+                       }
+                       else {
+                               /* start of new island */
+                               in_island = true;
+                               num_islands++;
+                               
+                               idx = num_islands - 1;
+                               islands[idx].start_idx = islands[idx].end_idx = i;
+                       }
+               }
+       }
        
-       /* add points */
-       for (i = 0, pt = gps->points, bp = nu->bp; i < gps->totpoints; i++, pt++, bp++) {
-               float p3d[3];
+       /* Watch out for special case where No islands = All points selected = Delete Stroke only */
+       if (num_islands) {
+               /* there are islands, so create a series of new strokes, adding them before the "next" stroke */
+               int idx;
                
-               /* get coordinates to add at */
-               gp_strokepoint_convertcoords(C, gps, pt, p3d, subrect);
-               copy_v3_v3(bp->vec, p3d);
-               
-               /* set settings */
-               bp->f1 = SELECT;
-               bp->radius = bp->weight = pt->pressure * gpl->thickness;
+               /* Create each new stroke... */
+               for (idx = 0; idx < num_islands; idx++) {
+                       tGPDeleteIsland *island = &islands[idx];
+                       bGPDstroke *new_stroke  = MEM_dupallocN(gps);
+                       
+                       /* Compute new buffer size (+ 1 needed as the endpoint index is "inclusive") */
+                       new_stroke->totpoints = island->end_idx - island->start_idx + 1;
+                       new_stroke->points    = MEM_callocN(sizeof(bGPDspoint) * new_stroke->totpoints, "gp delete stroke fragment");
+                       
+                       /* Copy over the relevant points */
+                       memcpy(new_stroke->points, gps->points + island->start_idx, sizeof(bGPDspoint) * new_stroke->totpoints);
+                       
+                       
+                       /* Each island corresponds to a new stroke. We must adjust the 
+                        * timings of these new strokes:
+                        *
+                        * Each point's timing data is a delta from stroke's inittime, so as we erase some points from
+                        * the start of the stroke, we have to offset this inittime and all remaining points' delta values.
+                        * This way we get a new stroke with exactly the same timing as if user had started drawing from
+                        * the first non-removed point...
+                        */
+                       {
+                               bGPDspoint *pts;
+                               float delta = gps->points[island->start_idx].time;
+                               int j;
+                               
+                               new_stroke->inittime += (double)delta;
+                               
+                               pts = new_stroke->points;
+                               for (j = 0; j < new_stroke->totpoints; j++, pts++) {
+                                       pts->time -= delta;
+                               }
+                       }
+                       
+                       /* Add new stroke to the frame */
+                       if (next_stroke) {
+                               BLI_insertlinkbefore(&gpf->strokes, next_stroke, new_stroke);
+                       }
+                       else {
+                               BLI_addtail(&gpf->strokes, new_stroke);
+                       }
+               }
        }
        
-       /* add nurb to curve */
-       BLI_addtail(&cu->nurb, nu);
+       /* free islands */
+       MEM_freeN(islands);
+       
+       /* Delete the old stroke */
+       MEM_freeN(gps->points);
+       BLI_freelinkN(&gpf->strokes, gps);
 }
 
-static int gp_camera_view_subrect(bContext *C, rctf *subrect)
-{
-       View3D *v3d = CTX_wm_view3d(C);
-       ARegion *ar = CTX_wm_region(C);
 
-       if (v3d) {
-               RegionView3D *rv3d = ar->regiondata;
+/* Split selected strokes into segments, splitting on selected points */
+static int gp_delete_selected_points(bContext *C)
+{
+       bool changed = false;
+       
+       CTX_DATA_BEGIN(C, bGPDlayer *, gpl, editable_gpencil_layers)
+       {
+               bGPDframe *gpf = gpl->actframe;
+               bGPDstroke *gps, *gpsn;
+               
+               if (gpf == NULL)
+                       continue;
                
-               /* for camera view set the subrect */
-               if (rv3d->persp == RV3D_CAMOB) {
-                       Scene *scene = CTX_data_scene(C);
-                       ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, subrect, TRUE); /* no shift */
-                       return 1;
+               /* simply delete strokes which are selected */
+               for (gps = gpf->strokes.first; gps; gps = gpsn) {
+                       gpsn = gps->next;
+                       
+                       /* skip strokes that are invalid for current view */
+                       if (ED_gpencil_stroke_can_use(C, gps) == false)
+                               continue;
+                       
+                       
+                       if (gps->flag & GP_STROKE_SELECT) {
+                               /* deselect old stroke, since it will be used as template for the new strokes */
+                               gps->flag &= ~GP_STROKE_SELECT;
+                               
+                               /* delete unwanted points by splitting stroke into several smaller ones */
+                               gp_stroke_delete_tagged_points(gpf, gps, gpsn, GP_SPOINT_SELECT);
+                               
+                               changed = true;
+                       }
                }
        }
-
-       return 0;
+       CTX_DATA_END;
+       
+       if (changed) {
+               WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
+               return OPERATOR_FINISHED;
+       }
+       else {
+               return OPERATOR_CANCELLED;
+       }
 }
 
-/* convert stroke to 3d bezier */
-static void gp_stroke_to_bezier(bContext *C, bGPDlayer *gpl, bGPDstroke *gps, Curve *cu, rctf *subrect)
-{
-       bGPDspoint *pt;
-       Nurb *nu;
-       BezTriple *bezt;
-       int i, tot;
-       float p3d_cur[3], p3d_prev[3], p3d_next[3];
-
-       /* create new 'nurb' within the curve */
-       nu = (Nurb *)MEM_callocN(sizeof(Nurb), "gpstroke_to_bezier(nurb)");
-
-       nu->pntsu = gps->totpoints;
-       nu->resolu = 12;
-       nu->resolv = 12;
-       nu->type = CU_BEZIER;
-       nu->bezt = (BezTriple *)MEM_callocN(gps->totpoints * sizeof(BezTriple), "bezts");
-
-       tot = gps->totpoints;
-
-       /* get initial coordinates */
-       pt = gps->points;
-       if (tot) {
-               gp_strokepoint_convertcoords(C, gps, pt, p3d_cur, subrect);
-               if (tot > 1) {
-                       gp_strokepoint_convertcoords(C, gps, pt + 1, p3d_next, subrect);
-               }
-       }
+/* ----------------------------------- */
 
-       /* add points */
-       for (i = 0, bezt = nu->bezt; i < tot; i++, pt++, bezt++) {
-               float h1[3], h2[3];
-               
-               if (i) interp_v3_v3v3(h1, p3d_cur, p3d_prev, 0.3);
-               else interp_v3_v3v3(h1, p3d_cur, p3d_next, -0.3);
-               
-               if (i < tot - 1) interp_v3_v3v3(h2, p3d_cur, p3d_next, 0.3);
-               else interp_v3_v3v3(h2, p3d_cur, p3d_prev, -0.3);
-               
-               copy_v3_v3(bezt->vec[0], h1);
-               copy_v3_v3(bezt->vec[1], p3d_cur);
-               copy_v3_v3(bezt->vec[2], h2);
-               
-               /* set settings */
-               bezt->h1 = bezt->h2 = HD_FREE;
-               bezt->f1 = bezt->f2 = bezt->f3 = SELECT;
-               bezt->radius = bezt->weight = pt->pressure * gpl->thickness * 0.1f;
-               
-               /* shift coord vects */
-               copy_v3_v3(p3d_prev, p3d_cur);
-               copy_v3_v3(p3d_cur, p3d_next);
+static int gp_delete_exec(bContext *C, wmOperator *op)
+{
+       eGP_DeleteMode mode = RNA_enum_get(op->ptr, "type");
+       int result = OPERATOR_CANCELLED;
+       
+       switch (mode) {
+               case GP_DELETEOP_STROKES:       /* selected strokes */
+                       result = gp_delete_selected_strokes(C);
+                       break;
                
-               if (i + 2 < tot) {
-                       gp_strokepoint_convertcoords(C, gps, pt + 2, p3d_next, subrect);
-               }
+               case GP_DELETEOP_POINTS:        /* selected points (breaks the stroke into segments) */
+                       result = gp_delete_selected_points(C);
+                       break;
+
+               case GP_DELETEOP_FRAME:         /* active frame */
+                       result = gp_actframe_delete_exec(C, op);
+                       break;
        }
+       
+       return result;
+}
 
-       /* must calculate handles or else we crash */
-       BKE_nurb_handles_calc(nu);
+void GPENCIL_OT_delete(wmOperatorType *ot)
+{
+       static EnumPropertyItem prop_gpencil_delete_types[] = {
+               {GP_DELETEOP_POINTS, "POINTS", 0, "Points", "Delete selected points and split strokes into segments"},
+               {GP_DELETEOP_STROKES, "STROKES", 0, "Strokes", "Delete selected strokes"},
+               {GP_DELETEOP_FRAME, "FRAME", 0, "Frame", "Delete active frame"},
+               {0, NULL, 0, NULL, NULL}
+       };
+       
+       /* identifiers */
+       ot->name = "Delete...";
+       ot->idname = "GPENCIL_OT_delete";
+       ot->description = "Delete selected Grease Pencil strokes, vertices, or frames";
+       
+       /* callbacks */
+       ot->invoke = WM_menu_invoke;
+       ot->exec = gp_delete_exec;
+       ot->poll = gp_stroke_edit_poll;
+       
+       /* flags */
+       ot->flag = OPTYPE_UNDO | OPTYPE_REGISTER;
+       
+       /* props */
+       ot->prop = RNA_def_enum(ot->srna, "type", prop_gpencil_delete_types, 0, "Type", "Method used for deleting Grease Pencil data");
+}
 
-       /* add nurb to curve */
-       BLI_addtail(&cu->nurb, nu);
+static int gp_dissolve_exec(bContext *C, wmOperator *UNUSED(op))
+{
+       return gp_dissolve_selected_points(C);
 }
 
-/* convert a given grease-pencil layer to a 3d-curve representation (using current view if appropriate) */
-static void gp_layer_to_curve(bContext *C, bGPdata *gpd, bGPDlayer *gpl, short mode)
+void GPENCIL_OT_dissolve(wmOperatorType *ot)
 {
-       Scene *scene = CTX_data_scene(C);
-       bGPDframe *gpf = gpencil_layer_getframe(gpl, CFRA, 0);
-       bGPDstroke *gps;
-       Object *ob;
-       Curve *cu;
+       /* identifiers */
+       ot->name = "Dissolve";
+       ot->idname = "GPENCIL_OT_dissolve";
+       ot->description = "Delete selected points without splitting strokes";
 
-       /* camera framing */
-       rctf subrect, *subrect_ptr = NULL;
+       /* callbacks */
+       ot->exec = gp_dissolve_exec;
+       ot->poll = gp_stroke_edit_poll;
 
-       /* error checking */
-       if (ELEM3(NULL, gpd, gpl, gpf))
-               return;
-               
-       /* only convert if there are any strokes on this layer's frame to convert */
-       if (gpf->strokes.first == NULL)
-               return;
+       /* flags */
+       ot->flag = OPTYPE_UNDO | OPTYPE_REGISTER;
+}
 
-       /* initialize camera framing */
-       if (gp_camera_view_subrect(C, &subrect)) {
-               subrect_ptr = &subrect;
-       }
+/* ****************** Snapping - Strokes <-> Cursor ************************ */
 
-       /* init the curve object (remove rotation and get curve data from it)
-        *      - must clear transforms set on object, as those skew our results
-        */
-       ob = BKE_object_add(scene, OB_CURVE);
-       zero_v3(ob->loc);
-       zero_v3(ob->rot);
-       cu = ob->data;
-       cu->flag |= CU_3D;
-       
-       /* rename object and curve to layer name */
-       rename_id((ID *)ob, gpl->info);
-       rename_id((ID *)cu, gpl->info);
-       
-       /* add points to curve */
-       for (gps = gpf->strokes.first; gps; gps = gps->next) {
-               switch (mode) {
-                       case GP_STROKECONVERT_PATH: 
-                               gp_stroke_to_path(C, gpl, gps, cu, subrect_ptr);
-                               break;
-                       case GP_STROKECONVERT_CURVE:
-                               gp_stroke_to_bezier(C, gpl, gps, cu, subrect_ptr);
-                               break;
-                       default:
-                               BLI_assert(!"invalid mode");
-                               break;
+/* Poll callback for snap operators */
+/* NOTE: For now, we only allow these in the 3D view, as other editors do not
+ *       define a cursor or gridstep which can be used
+ */
+static int gp_snap_poll(bContext *C)
+{
+       bGPdata *gpd = CTX_data_gpencil_data(C);
+       ScrArea *sa = CTX_wm_area(C);
+       
+       return (gpd != NULL) && ((sa != NULL) && (sa->spacetype == SPACE_VIEW3D));
+}
+
+/* --------------------------------- */
+
+static int gp_snap_to_grid(bContext *C, wmOperator *UNUSED(op))
+{
+       RegionView3D *rv3d = CTX_wm_region_data(C);
+       float gridf = rv3d->gridview;
+       
+       CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes)
+       {
+               bGPDspoint *pt;
+               int i;
+               
+               // TOOD: if entire stroke is selected, offset entire stroke by same amount?
+               
+               for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
+                       /* only if point is selected.. */
+                       if (pt->flag & GP_SPOINT_SELECT) {
+                               pt->x = gridf * floorf(0.5f + pt->x / gridf);
+                               pt->y = gridf * floorf(0.5f + pt->y / gridf);
+                               pt->z = gridf * floorf(0.5f + pt->z / gridf);
+                       }
                }
        }
+       CTX_DATA_END;
+       
+       WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
+       return OPERATOR_FINISHED;
+}
+
+void GPENCIL_OT_snap_to_grid(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name = "Snap Selection to Grid";
+       ot->idname = "GPENCIL_OT_snap_to_grid";
+       ot->description = "Snap selected points to the nearest grid points";
+       
+       /* callbacks */
+       ot->exec = gp_snap_to_grid;
+       ot->poll = gp_snap_poll;
+       
+       /* flags */
+       ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 }
 
-/* --- */
+/* ------------------------------- */
 
-static int gp_convert_poll(bContext *C)
+static int gp_snap_to_cursor(bContext *C, wmOperator *op)
 {
-       bGPdata *gpd = gpencil_data_get_active(C);
-       ScrArea *sa = CTX_wm_area(C);
        Scene *scene = CTX_data_scene(C);
+       View3D *v3d = CTX_wm_view3d(C);
+       
+       const bool use_offset = RNA_boolean_get(op->ptr, "use_offset");
+       const float *cursor_global = ED_view3d_cursor3d_get(scene, v3d);
+       
+       CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes)
+       {
+               bGPDspoint *pt;
+               int i;
+               
+               /* only continue if this stroke is selected (editable doesn't guarantee this)... */
+               if ((gps->flag & GP_STROKE_SELECT) == 0)
+                       continue;
+               
+               if (use_offset) {
+                       float offset[3];
+                       
+                       /* compute offset from first point of stroke to cursor */
+                       /* TODO: Allow using midpoint instead? */
+                       sub_v3_v3v3(offset, cursor_global, &gps->points->x);
+                       
+                       /* apply offset to all points in the stroke */
+                       for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
+                               add_v3_v3(&pt->x, offset);
+                       }
+               }
+               else {
+                       /* affect each selected point */
+                       for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
+                               if (pt->flag & GP_SPOINT_SELECT) {
+                                       copy_v3_v3(&pt->x, cursor_global);
+                               }
+                       }
+               }
+       }
+       CTX_DATA_END;
+       
+       WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
+       return OPERATOR_FINISHED;
+}
 
-       /* only if there's valid data, and the current view is 3D View */
-       return ((sa && sa->spacetype == SPACE_VIEW3D) && gpencil_layer_getactive(gpd) && (scene->obedit == NULL));
+void GPENCIL_OT_snap_to_cursor(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name = "Snap Selection to Cursor";
+       ot->idname = "GPENCIL_OT_snap_to_cursor";
+       ot->description = "Snap selected points/strokes to the cursor";
+       
+       /* callbacks */
+       ot->exec = gp_snap_to_cursor;
+       ot->poll = gp_snap_poll;
+       
+       /* flags */
+       ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+       
+       /* props */
+       ot->prop = RNA_def_boolean(ot->srna, "use_offset", true, "With Offset",
+                                  "Offset the entire stroke instead of selected points only");
 }
 
-static int gp_convert_layer_exec(bContext *C, wmOperator *op)
+/* ------------------------------- */
+
+static int gp_snap_cursor_to_sel(bContext *C, wmOperator *UNUSED(op))
 {
-       bGPdata *gpd = gpencil_data_get_active(C);
-       bGPDlayer *gpl = gpencil_layer_getactive(gpd);
        Scene *scene = CTX_data_scene(C);
-       int mode = RNA_enum_get(op->ptr, "type");
-
-       /* check if there's data to work with */
-       if (gpd == NULL) {
-               BKE_report(op->reports, RPT_ERROR, "No Grease Pencil data to work on");
-               return OPERATOR_CANCELLED;
+       View3D *v3d = CTX_wm_view3d(C);
+       
+       float *cursor = ED_view3d_cursor3d_get(scene, v3d);
+       float centroid[3] = {0.0f};
+       float min[3], max[3];
+       size_t count = 0;
+       
+       INIT_MINMAX(min, max);
+       
+       /* calculate midpoints from selected points */
+       CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes)
+       {
+               bGPDspoint *pt;
+               int i;
+               
+               /* only continue if this stroke is selected (editable doesn't guarantee this)... */
+               if ((gps->flag & GP_STROKE_SELECT) == 0)
+                       continue;
+               
+               for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
+                       if (pt->flag & GP_SPOINT_SELECT) {
+                               add_v3_v3(centroid, &pt->x);
+                               minmax_v3v3_v3(min, max, &pt->x);
+                               count++;
+                       }
+               }
+       }
+       CTX_DATA_END;
+       
+       if (v3d->around == V3D_AROUND_CENTER_MEAN && count) {
+               mul_v3_fl(centroid, 1.0f / (float)count);
+               copy_v3_v3(cursor, centroid);
+       }
+       else {
+               mid_v3_v3v3(cursor, min, max);
        }
 
-       gp_layer_to_curve(C, gpd, gpl, mode);
-
-       /* notifiers */
-       WM_event_add_notifier(C, NC_OBJECT | NA_ADDED, NULL);
-       WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
-
-       /* done */
+       
+       WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
        return OPERATOR_FINISHED;
 }
 
-void GPENCIL_OT_convert(wmOperatorType *ot)
+void GPENCIL_OT_snap_cursor_to_selected(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name = "Convert Grease Pencil";
-       ot->idname = "GPENCIL_OT_convert";
-       ot->description = "Convert the active Grease Pencil layer to a new Object";
-       ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+       ot->name = "Snap Cursor to Selected Points";
+       ot->idname = "GPENCIL_OT_snap_cursor_to_selected";
+       ot->description = "Snap cursor to center of selected points";
        
        /* callbacks */
-       ot->invoke = WM_menu_invoke;
-       ot->exec = gp_convert_layer_exec;
-       ot->poll = gp_convert_poll;
+       ot->exec = gp_snap_cursor_to_sel;
+       ot->poll = gp_snap_poll;
        
        /* flags */
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
-       
-       /* properties */
-       ot->prop = RNA_def_enum(ot->srna, "type", prop_gpencil_convertmodes, 0, "Type", "");
 }
 
+
 /* ************************************************ */