[latexila] Structure: open referenced file



commit 9d5593e9e3d5d501c81950a551277c9d22de5a30
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Tue Jul 24 01:54:54 2012 +0200

    Structure: open referenced file
    
    Open the file referenced by the selected structure item.
    
    Fixes bug #670549:
    https://bugzilla.gnome.org/show_bug.cgi?id=670549

 src/main_window_structure.vala |   17 ++++++++-
 src/structure.vala             |   82 +++++++++++++++++++++++++++++++++++++++-
 src/ui/ui.xml                  |    4 ++
 3 files changed, 101 insertions(+), 2 deletions(-)
---
diff --git a/src/main_window_structure.vala b/src/main_window_structure.vala
index e2698e6..fe00d61 100644
--- a/src/main_window_structure.vala
+++ b/src/main_window_structure.vala
@@ -50,7 +50,11 @@ public class MainWindowStructure
 
         { "StructureShiftRight", Stock.GO_FORWARD, N_("Shift _Right"), "",
             N_("Shift the selected structure item to the right (e.g. chapter â section)"),
-            on_shift_right }
+            on_shift_right },
+
+        { "StructureOpenFile", Stock.OPEN, N_("_Open File"), "",
+            N_("Open the file referenced by the selected structure item"),
+            on_open_file }
     };
 
     private UIManager _ui_manager;
@@ -101,6 +105,11 @@ public class MainWindowStructure
             _ui_manager.get_action ("/StructurePopup/StructureShiftRight");
 
         shift_right.sensitive = StructType.PART <= type && type < StructType.SUBPARAGRAPH;
+
+        Gtk.Action open_file =
+            _ui_manager.get_action ("/StructurePopup/StructureOpenFile");
+
+        open_file.sensitive = type == StructType.INCLUDE || type == StructType.IMAGE;
     }
 
     /* Gtk.Action callbacks */
@@ -146,4 +155,10 @@ public class MainWindowStructure
         return_if_fail (_structure != null);
         _structure.do_action (StructAction.SHIFT_RIGHT);
     }
+
+    public void on_open_file ()
+    {
+        return_if_fail (_structure != null);
+        _structure.do_action (StructAction.OPEN_FILE);
+    }
 }
diff --git a/src/structure.vala b/src/structure.vala
index b8a3ad3..97d8a66 100644
--- a/src/structure.vala
+++ b/src/structure.vala
@@ -85,6 +85,7 @@ public enum StructAction
     COMMENT,
     SHIFT_LEFT,
     SHIFT_RIGHT,
+    OPEN_FILE,
     NB_ACTIONS
 }
 
@@ -428,6 +429,8 @@ public class Structure : Grid
         // scroll to cursor, line at the top (no horizontal scroll)
         _main_window.active_view.scroll_to_mark (doc.get_insert (), 0, true, 1, 0);
 
+        item_selected (type);
+
         /* select the corresponding item in the simple list */
         if (! first_select)
             return true;
@@ -435,7 +438,6 @@ public class Structure : Grid
         select_simple_list_item (tree_iter);
 
         // the row is selected
-        item_selected (type);
         return true;
     }
 
@@ -589,6 +591,12 @@ public class Structure : Grid
 
         return_if_fail (selected_row != -1);
 
+        if (action_type == StructAction.OPEN_FILE)
+        {
+            open_referenced_file (selected_iter);
+            return;
+        }
+
         bool refresh_simple_list = false;
 
         try
@@ -621,6 +629,77 @@ public class Structure : Grid
             populate_simple_list ();
     }
 
+    private void open_referenced_file (TreeIter iter)
+    {
+        return_if_fail (_main_window.active_document != null);
+
+        StructType type;
+        string filename;
+
+        _model.get (iter,
+            StructColumn.TYPE, out type,
+            StructColumn.TEXT, out filename
+        );
+
+        File? doc_location = _main_window.active_document.location;
+        if (doc_location == null)
+            return;
+
+        File? parent = doc_location.get_parent ();
+        return_if_fail (parent != null);
+
+        File referenced_file = parent.get_child (filename);
+
+        switch (type)
+        {
+            case StructType.INCLUDE:
+                open_included_file (referenced_file);
+                break;
+
+            case StructType.IMAGE:
+                open_image (referenced_file);
+                break;
+
+            default:
+                return_if_reached ();
+        }
+    }
+
+    private void open_included_file (File referenced_file)
+    {
+        File file_to_open;
+
+        if (referenced_file.query_exists ())
+            file_to_open = referenced_file;
+        else
+        {
+            // LaTeX supports to omit the file's extension. It is most probably .tex.
+            string uri = referenced_file.get_uri ();
+            file_to_open = File.new_for_uri (uri + ".tex");
+
+            if (! file_to_open.query_exists ())
+            {
+                warning ("Structure: the file '%s' doesn't exist.",
+                    file_to_open.get_parse_name ());
+                return;
+            }
+        }
+
+        _main_window.open_document (file_to_open);
+    }
+
+    private void open_image (File referenced_file)
+    {
+        try
+        {
+            show_uri (get_screen (), referenced_file.get_uri (), Gdk.CURRENT_TIME);
+        }
+        catch (Error e)
+        {
+            warning ("Structure: can not open image: %s", e.message);
+        }
+    }
+
     private static string get_action_name (StructAction action_type)
     {
         if (_action_names == null)
@@ -638,6 +717,7 @@ public class Structure : Grid
             _action_names[StructAction.SHIFT_LEFT]  = _("shift left");
             // Translators: it's a verb
             _action_names[StructAction.SHIFT_RIGHT] = _("shift right");
+            _action_names[StructAction.OPEN_FILE]   = _("open file");
         }
 
         return _action_names[action_type];
diff --git a/src/ui/ui.xml b/src/ui/ui.xml
index 7d056fb..efed28d 100644
--- a/src/ui/ui.xml
+++ b/src/ui/ui.xml
@@ -377,6 +377,8 @@ along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
       <separator />
       <menuitem action="StructureShiftLeft" />
       <menuitem action="StructureShiftRight" />
+      <separator />
+      <menuitem action="StructureOpenFile" />
     </menu>
 
     <menu action="Help">
@@ -521,5 +523,7 @@ along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
     <separator />
     <menuitem action="StructureShiftLeft" />
     <menuitem action="StructureShiftRight" />
+    <separator />
+    <menuitem action="StructureOpenFile" />
   </popup>
 </ui>



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