[latexila] Structure: figures and tables located at the \begin{env}



commit 7f484910d031aecf98cdd70fe2f7e8d0918aace9
Author: Sébastien Wilmet <swilmet src gnome org>
Date:   Mon Jun 13 00:34:55 2011 +0200

    Structure: figures and tables located at the \begin{env}
    
    instead of \caption{}.
    
    A second TextMark is created for the \end{env} (it will be useful
    later).
    
    The contents of the caption is maximum 60 characters.

 TODO                        |    9 +---
 src/document_structure.vala |  105 ++++++++++++++++++++++++++++++++++---------
 src/file_browser.vala       |   13 +++--
 src/structure_model.vala    |    9 ++--
 4 files changed, 99 insertions(+), 37 deletions(-)
---
diff --git a/TODO b/TODO
index 8d7b5e6..078c950 100644
--- a/TODO
+++ b/TODO
@@ -7,14 +7,11 @@ LaTeXila 2.2
 ============
 
 - Structure (list of chapters, sections, etc. to easily navigate in a document):
-	- Figures and Tables:
-		- the TextMark should point to the \begin{env} line
-		- a second TextMark for the \end{env}
-		- contents of the item: first caption (max N characters)
-		- contents of the tooltip: all the caption
-
 	- New item type: graphic file (\includegraphics)
 
+	- Figure and Table env:
+		- items between start_mark and end_mark of the env: children
+
 	- Right click:
 		- cut, copy, delete, select
 		- comment
diff --git a/src/document_structure.vala b/src/document_structure.vala
index b235099..46b8e02 100644
--- a/src/document_structure.vala
+++ b/src/document_structure.vala
@@ -32,10 +32,13 @@ public class DocumentStructure : GLib.Object
     private static Regex? _comment_regex = null;
     private static Regex? _command_name_regex = null;
 
-    private bool _in_figure_env = false;
-    private bool _in_table_env = false;
     private bool _in_verbatim_env = false;
 
+    // we can not take all data for figures and tables at once
+    private StructData? _figure_data = null;
+    private StructData? _table_data = null;
+    private static const int CAPTION_MAX_LENGTH = 60;
+
     private static const int MAX_NB_LINES_TO_PARSE = 2000;
     private int _start_parsing_line = 0;
     private TextIter _cur_line_iter;
@@ -72,8 +75,8 @@ public class DocumentStructure : GLib.Object
         // reset
         parsing_done = false;
         _model = new StructureModel ();
-        _in_figure_env = false;
-        _in_table_env = false;
+        _figure_data = null;
+        _table_data = null;
         clear_all_structure_marks ();
         _start_parsing_line = 0;
 
@@ -265,15 +268,39 @@ public class DocumentStructure : GLib.Object
         if (_in_verbatim_env)
             return;
 
+        bool is_caption = name == "caption";
+
         StructType? type = get_markup_type (name);
-        if (type == null)
+        if (type == null && ! is_caption)
             return;
 
         string? contents = get_markup_contents (line, begin_contents_index);
+
         if (contents == null)
             return;
 
-        add_item (type, contents);
+        if (is_caption)
+            handle_caption (contents);
+        else
+            add_item (type, contents);
+    }
+
+    private void handle_caption (string contents)
+    {
+        string? text = null;
+        if (contents.length > CAPTION_MAX_LENGTH)
+            text = contents.substring (0, CAPTION_MAX_LENGTH);
+
+        // if there are several captions in the same environment, take the first
+
+        if (_figure_data != null && _figure_data.text == null)
+        {
+            _figure_data.text = text ?? contents;
+        }
+        else if (_table_data != null && _table_data.text == null)
+        {
+            _table_data.text = text ?? contents;
+        }
     }
 
     private void search_env (string line, int begin_contents_index, bool is_begin_env)
@@ -291,15 +318,50 @@ public class DocumentStructure : GLib.Object
         if (_in_verbatim_env)
             return;
 
-        switch (contents)
+        if (contents == "figure")
         {
-            case "figure":
-                _in_figure_env = is_begin_env;
-                break;
+            if (is_begin_env)
+            {
+                _figure_data = StructData ();
+                _figure_data.type = StructType.FIGURE;
+                _figure_data.text = null;
+                _figure_data.start_mark = create_text_mark_from_iter (_cur_line_iter);
+                _figure_data.end_mark = null;
+            }
+            else if (_figure_data != null)
+            {
+                if (_figure_data.text == null)
+                    _figure_data.text = "";
+
+                _figure_data.end_mark = create_text_mark_from_iter (_cur_line_iter);
 
-            case "table":
-                _in_table_env = is_begin_env;
-                break;
+                add_item_data (_figure_data, true);
+
+                _figure_data = null;
+            }
+        }
+
+        else if (contents == "table")
+        {
+            if (is_begin_env)
+            {
+                _table_data = StructData ();
+                _table_data.type = StructType.TABLE;
+                _table_data.text = null;
+                _table_data.start_mark = create_text_mark_from_iter (_cur_line_iter);
+                _table_data.end_mark = null;
+            }
+            else if (_table_data != null)
+            {
+                if (_table_data.text == null)
+                    _table_data.text = "";
+
+                _table_data.end_mark = create_text_mark_from_iter (_cur_line_iter);
+
+                add_item_data (_table_data, true);
+
+                _table_data = null;
+            }
         }
     }
 
@@ -329,9 +391,15 @@ public class DocumentStructure : GLib.Object
         StructData data = {};
         data.type = type;
         data.text = text;
-        data.mark = create_text_mark_from_iter (_cur_line_iter);
+        data.start_mark = create_text_mark_from_iter (_cur_line_iter);
+        data.end_mark = null;
+
+        add_item_data (data);
+    }
 
-        if (_insert_at_end)
+    private void add_item_data (StructData data, bool force_insert_in_middle = false)
+    {
+        if (_insert_at_end && ! force_insert_in_middle)
             _model.add_item_at_end (data);
         else
             _model.add_item_in_middle (data);
@@ -399,13 +467,6 @@ public class DocumentStructure : GLib.Object
             case "include":
                 return StructType.INCLUDE;
 
-            case "caption":
-                if (_in_figure_env)
-                    return StructType.FIGURE;
-                else if (_in_table_env)
-                    return StructType.TABLE;
-                return null;
-
             default:
                 return null;
         }
diff --git a/src/file_browser.vala b/src/file_browser.vala
index 2f086a7..71f5166 100644
--- a/src/file_browser.vala
+++ b/src/file_browser.vala
@@ -42,6 +42,7 @@ public class FileBrowser : VBox
     private BuildView build_view;
     private ListStore parent_dir_store;
     private ListStore list_store;
+    private TreeView _list_view;
     private ComboBox combo_box;
     private File current_directory;
     private Button parent_button;
@@ -214,11 +215,11 @@ public class FileBrowser : VBox
         list_store.set_sort_func (0, on_sort);
         list_store.set_sort_column_id (0, SortType.ASCENDING);
 
-        TreeView tree_view = new TreeView.with_model (list_store);
-        tree_view.headers_visible = false;
+        _list_view = new TreeView.with_model (list_store);
+        _list_view.headers_visible = false;
 
         TreeViewColumn column = new TreeViewColumn ();
-        tree_view.append_column (column);
+        _list_view.append_column (column);
 
         // icon
         CellRendererPixbuf pixbuf_renderer = new CellRendererPixbuf ();
@@ -231,10 +232,10 @@ public class FileBrowser : VBox
         column.set_attributes (text_renderer, "text", FileColumn.NAME, null);
 
         // with a scrollbar
-        Widget sw = Utils.add_scrollbar (tree_view);
+        Widget sw = Utils.add_scrollbar (_list_view);
         pack_start (sw);
 
-        tree_view.row_activated.connect ((path) =>
+        _list_view.row_activated.connect ((path) =>
         {
             TreeModel model = (TreeModel) list_store;
             TreeIter iter;
@@ -302,6 +303,8 @@ public class FileBrowser : VBox
         list_store.clear ();
         parent_dir_store.clear ();
 
+        _list_view.columns_autosize ();
+
         /* files list store */
 
         File? directory = dir;
diff --git a/src/structure_model.vala b/src/structure_model.vala
index aa169eb..95e1117 100644
--- a/src/structure_model.vala
+++ b/src/structure_model.vala
@@ -23,7 +23,8 @@ public struct StructData
 {
     StructType type;
     string text;
-    TextMark mark;
+    TextMark start_mark;
+    TextMark? end_mark;
 }
 
 public enum StructColumn
@@ -192,7 +193,7 @@ public class StructureModel : TreeModel, GLib.Object
                 break;
 
             case StructColumn.MARK:
-                val = data.mark;
+                val = data.start_mark;
                 break;
 
             case StructColumn.TYPE:
@@ -374,7 +375,7 @@ public class StructureModel : TreeModel, GLib.Object
             return;
         }
 
-        int pos = get_position_from_mark (item.mark);
+        int pos = get_position_from_mark (item.start_mark);
         unowned Node<StructData?> cur_parent = _tree;
         while (true)
         {
@@ -382,7 +383,7 @@ public class StructureModel : TreeModel, GLib.Object
             int child_index = 0;
             while (true)
             {
-                int cur_pos = get_position_from_mark (cur_child.data.mark);
+                int cur_pos = get_position_from_mark (cur_child.data.start_mark);
 
                 if (cur_pos > pos)
                 {



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