[latexila/structure-actions] Structure actions: cut, copy, delete



commit 2b366ff4f30fd0d8d8da5a883b02da9448750dc1
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Fri Jul 1 23:42:07 2011 +0200

    Structure actions: cut, copy, delete
    
    Cut, copy and delete are very similar to select. When we have the two
    bounds, we simply do a different action.
    
    When we comment, cut or delete an item, the item is removed from the
    model.

 TODO                        |    2 -
 src/document_structure.vala |   48 +++++++++++++++++++++++++++++++++---------
 src/structure_model.vala    |   10 +++++++++
 3 files changed, 47 insertions(+), 13 deletions(-)
---
diff --git a/TODO b/TODO
index 163c22e..267960d 100644
--- a/TODO
+++ b/TODO
@@ -8,8 +8,6 @@ LaTeXila 2.2
 
 - Structure (list of chapters, sections, etc. to easily navigate in a document):
 	- Right click:
-		- cut, copy, delete
-		- comment: remove the item(s) from the model when the item is commented
 		- shift left
 		- shift right:
 			- display a warning if a subparagraph already exists
diff --git a/src/document_structure.vala b/src/document_structure.vala
index 9113fef..b603e41 100644
--- a/src/document_structure.vala
+++ b/src/document_structure.vala
@@ -562,28 +562,53 @@ public class DocumentStructure : GLib.Object
     {
         if (action_type == StructAction.COMMENT)
         {
-            comment_item (tree_iter);
+            bool item_commented = comment_item (tree_iter);
+            if (item_commented)
+                _model.delete (tree_iter);
             return;
         }
 
-        if (action_type != StructAction.SELECT)
+        if (action_type == StructAction.SHIFT_LEFT
+            || action_type == StructAction.SHIFT_RIGHT)
             return;
 
         TextIter? start_iter;
         TextIter? end_iter;
-        if (get_exact_item_bounds (tree_iter, out start_iter, out end_iter))
+        bool found = get_exact_item_bounds (tree_iter, out start_iter, out end_iter);
+
+        if (! found)
+            return;
+
+        if (start_iter.get_line () != end_iter.get_line ())
         {
-            if (start_iter.get_line () != end_iter.get_line ())
-            {
-                backward_indentation (ref start_iter);
-                backward_indentation (ref end_iter);
-            }
+            backward_indentation (ref start_iter);
+            backward_indentation (ref end_iter);
+        }
 
+        if (action_type == StructAction.SELECT)
+        {
             _doc.select_range (start_iter, end_iter);
+            return;
+        }
+
+        if (action_type == StructAction.COPY || action_type == StructAction.CUT)
+        {
+            string data = _doc.get_text (start_iter, end_iter, false);
+            Clipboard clipboard = Clipboard.get (Gdk.SELECTION_CLIPBOARD);
+            clipboard.set_text (data, -1);
+        }
+
+        if (action_type == StructAction.DELETE || action_type == StructAction.CUT)
+        {
+            _doc.begin_user_action ();
+            _doc.delete (start_iter, end_iter);
+            _doc.end_user_action ();
+            _model.delete (tree_iter);
         }
     }
 
-    private void comment_item (TreeIter tree_iter)
+    // Returns true only if the item is correctly commented
+    private bool comment_item (TreeIter tree_iter)
     {
         StructType type;
         TextMark start_mark = null;
@@ -607,7 +632,7 @@ public class DocumentStructure : GLib.Object
         if (! Structure.is_section (type))
         {
             comment_between (start_iter, end_iter);
-            return;
+            return true;
         }
 
         /* comment a section */
@@ -621,7 +646,7 @@ public class DocumentStructure : GLib.Object
         catch (StructError e)
         {
             stderr.printf ("Structure: get next sibling or parent: %s\n", e.message);
-            return;
+            return false;
         }
 
         bool go_one_line_backward = true;
@@ -650,6 +675,7 @@ public class DocumentStructure : GLib.Object
         }
 
         comment_between (start_iter, end_iter);
+        return true;
     }
 
     // comment the lines between start_iter and end_iter included
diff --git a/src/structure_model.vala b/src/structure_model.vala
index 9854189..91b21ba 100644
--- a/src/structure_model.vala
+++ b/src/structure_model.vala
@@ -456,6 +456,16 @@ public class StructureModel : TreeModel, GLib.Object
         return null;
     }
 
+    public void delete (TreeIter iter)
+    {
+        return_if_fail (iter_is_valid (iter));
+
+        TreePath path = get_path (iter);
+        unowned Node<StructData?> node = get_node_from_iter (iter);
+        node.unlink ();
+        row_deleted (path);
+    }
+
     private void insert_item_at_position (StructData item, Node<StructData?> parent,
         int pos)
     {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]