[latexila/structure-actions] Structure action: comment (not finished)



commit 1feef13bbc5a904e215812d2507051925c1da046
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Mon Jun 27 00:50:56 2011 +0200

    Structure action: comment (not finished)
    
    What still need to be done for the comment:
    - get end_iter for sections
    - remove commented item from the model

 src/document_structure.vala |   54 ++++++++++++++++++++++++++++++++++++++++
 src/main_window.vala        |   58 +++++++++++++++++++++++++++++++++++++-----
 src/structure.vala          |   56 ++++++++++++++++++-----------------------
 src/structure_model.vala    |   30 +++++++++++++---------
 4 files changed, 148 insertions(+), 50 deletions(-)
---
diff --git a/src/document_structure.vala b/src/document_structure.vala
index ee5168a..a087abe 100644
--- a/src/document_structure.vala
+++ b/src/document_structure.vala
@@ -93,6 +93,9 @@ public class DocumentStructure : GLib.Object
         return _model;
     }
 
+    /*************************************************************************/
+    // Parsing stuff
+
     // Parse the document. Returns false if finished, true otherwise.
     private bool parse_impl ()
     {
@@ -473,4 +476,55 @@ public class DocumentStructure : GLib.Object
                 return null;
         }
     }
+
+    /*************************************************************************/
+    // Actions: cut, copy, delete, select, comment, shift left/right
+
+    public void do_action (StructAction action_type, TreeIter tree_iter)
+    {
+        if (action_type != StructAction.COMMENT)
+            return;
+
+        StructType type;
+        TextMark start_mark = null;
+        TextMark end_mark = null;
+
+        _model.get (tree_iter,
+            StructColumn.TYPE, out type,
+            StructColumn.START_MARK, out start_mark,
+            StructColumn.END_MARK, out end_mark,
+            -1);
+
+        if (Structure.is_section (type))
+            return;
+
+        TextIter start_iter;
+        TextIter? end_iter = null;
+
+        _doc.get_iter_at_mark (out start_iter, start_mark);
+
+        if (end_mark != null)
+            _doc.get_iter_at_mark (out end_iter, end_mark);
+
+        comment (start_iter, end_iter);
+    }
+
+    // comment the lines between start_iter and end_iter included
+    private void comment (TextIter start_iter, TextIter? end_iter)
+    {
+        int start_line = start_iter.get_line ();
+        int end_line = start_line;
+
+        if (end_iter != null)
+            end_line = end_iter.get_line ();
+
+        _doc.begin_user_action ();
+        for (int line_index = start_line ; line_index <= end_line ; line_index++)
+        {
+            TextIter iter;
+            _doc.get_iter_at_line (out iter, line_index);
+            _doc.insert (iter, "% ", -1);
+        }
+        _doc.end_user_action ();
+    }
 }
diff --git a/src/main_window.vala b/src/main_window.vala
index beef23b..7f4270a 100644
--- a/src/main_window.vala
+++ b/src/main_window.vala
@@ -134,22 +134,22 @@ public class MainWindow : Window
         // Structure
         { "Structure", null, N_("S_tructure") },
         { "StructureCut", Stock.CUT, null, "",
-            N_("Cut the selected structure item"), Structure.on_cut },
+            N_("Cut the selected structure item"), on_structure_cut },
         { "StructureCopy", Stock.COPY, null, "",
-            N_("Copy the selected structure item"), Structure.on_copy },
+            N_("Copy the selected structure item"), on_structure_copy },
         { "StructureDelete", Stock.DELETE, null, "",
-            N_("Delete the selected structure item"), Structure.on_delete },
+            N_("Delete the selected structure item"), on_structure_delete },
         { "StructureSelect", Stock.SELECT_ALL, N_("_Select"), "",
             N_("Select the contents of the selected structure item"),
-            Structure.on_select },
+            on_structure_select },
         { "StructureComment", null, N_("_Comment"), null,
-            N_("Comment the selected structure item"), Structure.on_comment },
+            N_("Comment the selected structure item"), on_structure_comment },
         { "StructureShiftLeft", Stock.GO_BACK, N_("Shift _Left"), "",
             N_("Shift the selected structure item to the left (e.g. section â chapter"),
-            Structure.on_shift_left },
+            on_structure_shift_left },
         { "StructureShiftRight", Stock.GO_FORWARD, N_("Shift _Right"), "",
             N_("Shift the selected structure item to the right (e.g. chapter â section"),
-            Structure.on_shift_right },
+            on_structure_shift_right },
 
         // Help
         { "Help", null, N_("_Help") },
@@ -1792,6 +1792,50 @@ public class MainWindow : Window
         ProjectDialogs.manage_projects (this);
     }
 
+    /* Structure */
+
+    public void on_structure_cut ()
+    {
+        return_if_fail (_structure != null);
+        _structure.do_action (StructAction.CUT);
+    }
+
+    public void on_structure_copy ()
+    {
+        return_if_fail (_structure != null);
+        _structure.do_action (StructAction.COPY);
+    }
+
+    public void on_structure_delete ()
+    {
+        return_if_fail (_structure != null);
+        _structure.do_action (StructAction.DELETE);
+    }
+
+    public void on_structure_select ()
+    {
+        return_if_fail (_structure != null);
+        _structure.do_action (StructAction.SELECT);
+    }
+
+    public void on_structure_comment ()
+    {
+        return_if_fail (_structure != null);
+        _structure.do_action (StructAction.COMMENT);
+    }
+
+    public void on_structure_shift_left ()
+    {
+        return_if_fail (_structure != null);
+        _structure.do_action (StructAction.SHIFT_LEFT);
+    }
+
+    public void on_structure_shift_right ()
+    {
+        return_if_fail (_structure != null);
+        _structure.do_action (StructAction.SHIFT_RIGHT);
+    }
+
     /* Help */
 
     public void on_help_latex_reference ()
diff --git a/src/structure.vala b/src/structure.vala
index 835f056..ca173b9 100644
--- a/src/structure.vala
+++ b/src/structure.vala
@@ -38,6 +38,17 @@ public enum StructType
     N_TYPES
 }
 
+public enum StructAction
+{
+    CUT,
+    COPY,
+    DELETE,
+    SELECT,
+    COMMENT,
+    SHIFT_LEFT,
+    SHIFT_RIGHT
+}
+
 public class Structure : VBox
 {
     private unowned MainWindow _main_window;
@@ -55,6 +66,7 @@ public class Structure : VBox
     private VPaned _vpaned;
 
     private TreeView _tree_view;
+    private DocumentStructure _document_structure = null;
     private StructureModel? _model = null;
 
     private TreeView _list_view;
@@ -384,7 +396,7 @@ public class Structure : VBox
         TextMark mark;
         StructType type;
         _model.get (tree_iter,
-            StructColumn.MARK, out mark,
+            StructColumn.START_MARK, out mark,
             StructColumn.TYPE, out type,
             -1);
 
@@ -454,18 +466,18 @@ public class Structure : VBox
         if (doc == null)
             return;
 
-        DocumentStructure doc_struct = doc.get_structure ();
+        _document_structure = doc.get_structure ();
 
         if (force_parse)
-            doc_struct.parse ();
+            _document_structure.parse ();
 
-        if (doc_struct.parsing_done)
-            set_model (doc_struct.get_model ());
+        if (_document_structure.parsing_done)
+            set_model (_document_structure.get_model ());
 
-        doc_struct.notify["parsing-done"].connect (() =>
+        _document_structure.notify["parsing-done"].connect (() =>
         {
-            if (doc_struct.parsing_done)
-                set_model (doc_struct.get_model ());
+            if (_document_structure.parsing_done)
+                set_model (_document_structure.get_model ());
         });
     }
 
@@ -524,32 +536,14 @@ public class Structure : VBox
         _action_shift_right.sensitive = type < StructType.SUBPARAGRAPH;
     }
 
-    public static void on_cut ()
-    {
-    }
-
-    public static void on_copy ()
+    public void do_action (StructAction action_type)
     {
-    }
+        TreeIter selected_iter;
+        int selected_row = Utils.get_selected_row (_tree_view, out selected_iter);
 
-    public static void on_delete ()
-    {
-    }
+        return_if_fail (selected_row != -1);
 
-    public static void on_select ()
-    {
-    }
-
-    public static void on_comment ()
-    {
-    }
-
-    public static void on_shift_left ()
-    {
-    }
-
-    public static void on_shift_right ()
-    {
+        _document_structure.do_action (action_type, selected_iter);
     }
 
     public static string get_icon_from_type (StructType type)
diff --git a/src/structure_model.vala b/src/structure_model.vala
index 7bdefe9..4dfaf45 100644
--- a/src/structure_model.vala
+++ b/src/structure_model.vala
@@ -32,7 +32,8 @@ public enum StructColumn
     PIXBUF,
     TEXT,
     TOOLTIP,
-    MARK,
+    START_MARK,
+    END_MARK,
     TYPE,
     N_COLUMNS
 }
@@ -60,22 +61,23 @@ public class StructureModel : TreeModel, GLib.Object
     public StructureModel ()
     {
         _column_types = new Type[StructColumn.N_COLUMNS];
-        _column_types[StructColumn.PIXBUF]  = typeof (string);
-        _column_types[StructColumn.TEXT]    = typeof (string);
-        _column_types[StructColumn.TOOLTIP] = typeof (string);
-        _column_types[StructColumn.MARK]    = typeof (TextMark);
-        _column_types[StructColumn.TYPE]    = typeof (StructType);
+        _column_types[StructColumn.PIXBUF]      = typeof (string);
+        _column_types[StructColumn.TEXT]        = typeof (string);
+        _column_types[StructColumn.TOOLTIP]     = typeof (string);
+        _column_types[StructColumn.START_MARK]  = typeof (TextMark);
+        _column_types[StructColumn.END_MARK]    = typeof (TextMark);
+        _column_types[StructColumn.TYPE]        = typeof (StructType);
 
         StructData empty_data = {};
         _tree = new Node<StructData?> (empty_data);
 
         new_stamp ();
 
-        _list_labels = new Gee.ArrayList<unowned Node<StructData?>> ();
-        _list_includes = new Gee.ArrayList<unowned Node<StructData?>> ();
-        _list_tables = new Gee.ArrayList<unowned Node<StructData?>> ();
-        _list_figures = new Gee.ArrayList<unowned Node<StructData?>> ();
-        _list_todo_and_fixme = new Gee.ArrayList<unowned Node<StructData?>> ();
+        _list_labels            = new Gee.ArrayList<unowned Node<StructData?>> ();
+        _list_includes          = new Gee.ArrayList<unowned Node<StructData?>> ();
+        _list_tables            = new Gee.ArrayList<unowned Node<StructData?>> ();
+        _list_figures           = new Gee.ArrayList<unowned Node<StructData?>> ();
+        _list_todo_and_fixme    = new Gee.ArrayList<unowned Node<StructData?>> ();
     }
 
     // A new stamp should be generated each time the data in the model change
@@ -192,10 +194,14 @@ public class StructureModel : TreeModel, GLib.Object
                 val = data.text;
                 break;
 
-            case StructColumn.MARK:
+            case StructColumn.START_MARK:
                 val = data.start_mark;
                 break;
 
+            case StructColumn.END_MARK:
+                val = data.end_mark;
+                break;
+
             case StructColumn.TYPE:
                 val = data.type;
                 break;



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