[latexila] Some UI improvements



commit 0de72d2a90e3a1cb0bda98de63d0a137c3e370ce
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Fri Jul 29 18:40:20 2011 +0200

    Some UI improvements
    
    Templates and Projects dialogs are more well-spaced.
    
    Also, the functions in utils.vala are now grouped in several categories.

 src/project_dialogs.vala |   79 +++++------
 src/templates.vala       |   58 ++------
 src/utils.vala           |  360 +++++++++++++++++++++++++---------------------
 3 files changed, 248 insertions(+), 249 deletions(-)
---
diff --git a/src/project_dialogs.vala b/src/project_dialogs.vala
index e1ccd36..c4024ad 100644
--- a/src/project_dialogs.vala
+++ b/src/project_dialogs.vala
@@ -30,60 +30,50 @@ namespace ProjectDialogs
             null);
 
         /* create dialog widgets */
-        VBox content_area = (VBox) dialog.get_content_area ();
+        VBox content_area = dialog.get_content_area () as VBox;
 
-        HBox hbox = new HBox (false, 6);
-        VBox vbox1 = new VBox (true, 6);
-        VBox vbox2 = new VBox (true, 6);
-        hbox.pack_start (vbox1, false, false);
-        hbox.pack_start (vbox2);
-        hbox.border_width = 6;
-
-        Label label1 = new Label (null);
-        label1.set_markup ("<b>" + _("Directory:") + "</b>");
-        Label label2 = new Label (null);
-        label2.set_markup ("<b>" + _("Main File:") + "</b>");
-        vbox1.pack_start (label1);
-        vbox1.pack_start (label2);
-
-        FileChooserButton file_chooser_button1 = new FileChooserButton (_("Directory"),
+        // directory
+        FileChooserButton directory_chooser = new FileChooserButton (_("Directory"),
             FileChooserAction.SELECT_FOLDER);
-        FileChooserButton file_chooser_button2 = new FileChooserButton (_("Main File"),
-            FileChooserAction.OPEN);
+        directory_chooser.set_size_request (250, -1);
+        Widget component = Utils.get_dialog_component (_("Directory"), directory_chooser);
+        content_area.pack_start (component, false);
 
-        vbox2.pack_start (file_chooser_button1);
-        vbox2.pack_start (file_chooser_button2);
+        // main file
+        FileChooserButton main_file_chooser = new FileChooserButton (_("Main File"),
+            FileChooserAction.OPEN);
+        component = Utils.get_dialog_component (_("Main File"), main_file_chooser);
+        content_area.pack_start (component, false);
 
-        content_area.pack_start (hbox);
         content_area.show_all ();
 
         /* callbacks */
-        file_chooser_button1.file_set.connect (() =>
+        directory_chooser.file_set.connect (() =>
         {
-            File dir = file_chooser_button1.get_file ();
+            File dir = directory_chooser.get_file ();
             try
             {
-                file_chooser_button2.set_current_folder_file (dir);
+                main_file_chooser.set_current_folder_file (dir);
             }
             catch (Error e) {}
         });
 
         /* if a document is opened, go to the document's directory */
         Document? doc = main_window.active_document;
-        if (doc != null)
+        if (doc != null && doc.location != null)
         {
             try
             {
-                file_chooser_button1.set_file (doc.location.get_parent ());
-                file_chooser_button2.set_file (doc.location);
+                directory_chooser.set_file (doc.location.get_parent ());
+                main_file_chooser.set_file (doc.location);
             }
             catch (GLib.Error e) {}
         }
 
         while (dialog.run () == ResponseType.OK)
         {
-            File? directory = file_chooser_button1.get_file ();
-            File? main_file = file_chooser_button2.get_file ();
+            File? directory = directory_chooser.get_file ();
+            File? main_file = main_file_chooser.get_file ();
 
             if (directory == null || main_file == null)
                 continue;
@@ -129,30 +119,29 @@ namespace ProjectDialogs
             null);
 
         /* create dialog widgets */
-        VBox content_area = (VBox) dialog.get_content_area ();
+        VBox content_area = dialog.get_content_area () as VBox;
 
-        Label location = new Label (_("Location of the project: %s").printf (
-            Utils.replace_home_dir_with_tilde (project.directory.get_parse_name ())
-            + "/"));
+        // directory
+        string project_dir = project.directory.get_parse_name ();
+        project_dir = Utils.replace_home_dir_with_tilde (project_dir) + "/";
+        Label location = new Label (project_dir);
         location.set_line_wrap (true);
 
-        content_area.pack_start (location, false, false, 6);
-
-        HBox hbox = new HBox (false, 6);
-        content_area.pack_start (hbox);
-
-        Label label = new Label (_("Main File:"));
-        hbox.pack_start (label, false, false);
+        Widget component = Utils.get_dialog_component (_("Location of the project"),
+            location);
+        content_area.pack_start (component, false);
 
-        FileChooserButton file_chooser_button = new FileChooserButton (_("Main File"),
+        // main file
+        FileChooserButton main_file_chooser = new FileChooserButton (_("Main File"),
             FileChooserAction.OPEN);
-        hbox.pack_start (file_chooser_button);
+        component = Utils.get_dialog_component (_("Main File"), main_file_chooser);
+        content_area.pack_start (component, false);
 
         content_area.show_all ();
 
         try
         {
-            file_chooser_button.set_file (project.main_file);
+            main_file_chooser.set_file (project.main_file);
         }
         catch (Error e) {}
 
@@ -160,7 +149,7 @@ namespace ProjectDialogs
         bool ret = false;
         while (dialog.run () == ResponseType.OK)
         {
-            File? main_file = file_chooser_button.get_file ();
+            File? main_file = main_file_chooser.get_file ();
 
             if (main_file == null)
                 continue;
diff --git a/src/templates.vala b/src/templates.vala
index 82ca740..899a805 100644
--- a/src/templates.vala
+++ b/src/templates.vala
@@ -124,14 +124,14 @@ public class Templates : GLib.Object
 
         /* icon view for the default templates */
         IconView icon_view_default_templates = create_icon_view (default_store);
-        Widget component = get_dialog_component (_("Default templates"),
-            icon_view_default_templates);
+        Widget scrollbar = Utils.add_scrollbar (icon_view_default_templates);
+        Widget component = Utils.get_dialog_component (_("Default templates"), scrollbar);
         vpaned.pack1 (component, true, true);
 
         /* icon view for the personal templates */
         IconView icon_view_personal_templates = create_icon_view (personal_store);
-        component = get_dialog_component (_("Your personal templates"),
-            icon_view_personal_templates);
+        scrollbar = Utils.add_scrollbar (icon_view_personal_templates);
+        component = Utils.get_dialog_component (_("Your personal templates"), scrollbar);
         vpaned.pack2 (component, false, true);
 
         content_area.show_all ();
@@ -205,31 +205,6 @@ public class Templates : GLib.Object
         dialog.destroy ();
     }
 
-    private Widget get_dialog_component (string title, Widget widget)
-    {
-        // title in bold at the left
-        // widget is below, with a left margin, with scrollbars
-
-        VBox vbox = new VBox (false, 6);
-
-        Label label = new Label (null);
-        label.set_markup ("<b>" + title + "</b>");
-        label.xalign = (float) 0.0;
-        vbox.pack_start (label, false, false);
-
-        Alignment alignment = new Alignment ((float) 0.5, (float) 0.5, (float) 1.0,
-            (float) 1.0);
-        alignment.left_padding = 12;
-        vbox.pack_start (alignment);
-
-        // with a scrollbar (without that there is a problem for resizing the
-        // dialog, we can make it bigger but not smaller...)
-        Widget scrollbar = Utils.add_scrollbar (widget);
-        alignment.add (scrollbar);
-
-        return vbox;
-    }
-
     public void show_dialog_create (MainWindow parent)
     {
         return_if_fail (parent.active_tab != null);
@@ -239,26 +214,23 @@ public class Templates : GLib.Object
             Stock.CANCEL, ResponseType.REJECT,
             null);
 
-        dialog.set_default_size (400, 330);
+        dialog.set_default_size (420, 370);
 
-        Box content_area = (Box) dialog.get_content_area ();
+        Box content_area = dialog.get_content_area () as Box;
+        content_area.homogeneous = false;
 
         /* name */
-        HBox hbox = new HBox (false, 5);
-        Label label = new Label (_("Name of the new template:"));
         Entry entry = new Entry ();
-
-        hbox.pack_start (label, false, false, 0);
-        hbox.pack_start (entry, false, false, 0);
-        content_area.pack_start (hbox, false, false, 10);
+        Widget component = Utils.get_dialog_component (_("Name of the new template"),
+            entry);
+        content_area.pack_start (component, false);
 
         /* icon */
         // we take the default store because it contains all the icons
         IconView icon_view = create_icon_view (default_store);
         Widget scrollbar = Utils.add_scrollbar (icon_view);
-        Frame frame = new Frame (_("Choose an icon:"));
-        frame.add (scrollbar);
-        content_area.pack_start (frame, true, true, 10);
+        component = Utils.get_dialog_component (_("Choose an icon"), scrollbar);
+        content_area.pack_start (component);
 
         content_area.show_all ();
 
@@ -312,8 +284,10 @@ public class Templates : GLib.Object
         /* icon view for the personal templates */
         IconView icon_view = create_icon_view (personal_store);
         icon_view.set_selection_mode (SelectionMode.MULTIPLE);
-        Widget component = get_dialog_component (_("Personal templates"), icon_view);
-        content_area.pack_start (component, true, true, 10);
+        Widget scrollbar = Utils.add_scrollbar (icon_view);
+        Widget component = Utils.get_dialog_component (_("Personal templates"),
+            scrollbar);
+        content_area.pack_start (component);
         content_area.show_all ();
 
         int nb_personal_templates_before = nb_personal_templates;
diff --git a/src/utils.vala b/src/utils.vala
index c68bfb1..d30d2e8 100644
--- a/src/utils.vala
+++ b/src/utils.vala
@@ -21,11 +21,8 @@ using Gtk;
 
 namespace Utils
 {
-    public void flush_queue ()
-    {
-        while (Gtk.events_pending ())
-            Gtk.main_iteration ();
-    }
+    /*************************************************************************/
+    // String utilities
 
     public string str_middle_truncate (string str, uint max_length)
     {
@@ -37,6 +34,51 @@ namespace Utils
         return str[0:half_length] + "..." + str[l-half_length:l];
     }
 
+    public bool char_is_escaped (string text, long char_index)
+    {
+        return_val_if_fail (char_index < text.length, false);
+
+        int index = (int) char_index;
+        if (! string_get_prev_char (text, ref index, null))
+            return false;
+
+        bool escaped = false;
+        while (true)
+        {
+            unichar cur_char;
+            bool first_char = ! string_get_prev_char (text, ref index, out cur_char);
+
+            if (cur_char != '\\')
+                break;
+
+            escaped = ! escaped;
+
+            if (first_char)
+                break;
+        }
+
+        return escaped;
+    }
+
+    // The opposite of string.get_next_char ().
+    // TODO remove this function when it is included upstream
+    // See https://bugzilla.gnome.org/show_bug.cgi?id=655185
+    private bool string_get_prev_char (string str, ref int index, out unichar c)
+    {
+        c = str.get_char (index);
+        if (index <= 0 || c == '\0')
+            return false;
+
+        unowned string str_at_index = (string) ((char*) str + index);
+        unowned string str_prev = str_at_index.prev_char ();
+        index = (int) ((char*) str_prev - (char*) str);
+        return true;
+    }
+
+
+    /*************************************************************************/
+    // URI, File or Path utilities
+
     public string replace_home_dir_with_tilde (string uri)
     {
         return_val_if_fail (uri != null, null);
@@ -110,97 +152,97 @@ namespace Utils
         return l;
     }
 
-    public const uint ALL_WORKSPACES = 0xffffff;
+    public void delete_file (File file)
+    {
+        try
+        {
+            file.delete ();
+        }
+        catch (Error e)
+        {
+            stderr.printf ("Warning: delete file \"%s\" failed: %s\n",
+                file.get_parse_name (), e.message);
+        }
+    }
 
-    /* Get the workspace the window is on
-     *
-     * This function gets the workspace that the #GtkWindow is visible on,
-     * it returns ALL_WORKSPACES if the window is sticky, or if
-     * the window manager doesn't support this function.
-     */
-    public uint get_window_workspace (Gtk.Window gtkwindow)
+    // origin can be equal to common_dir, but target must be different
+    public string? get_relative_path (File origin, File target, File common_dir)
     {
-        return_val_if_fail (gtkwindow.get_realized (), 0);
+        File? origin_dir;
+        if (origin.equal (common_dir))
+            origin_dir = origin;
+        else
+            origin_dir = origin.get_parent ();
 
-        uint ret = ALL_WORKSPACES;
+        File? target_parent = target.get_parent ();
 
-        Gdk.Window window = gtkwindow.get_window ();
-        Gdk.Display display = window.get_display ();
-        unowned X.Display x_display = Gdk.x11_display_get_xdisplay (display);
+        return_val_if_fail (origin_dir != null, null);
+        return_val_if_fail (target_parent != null, null);
 
-        X.Atom type;
-        int format;
-        ulong nitems;
-        ulong bytes_after;
-        uint *workspace;
+        // The origin is in the same directory as the target.
+        if (target_parent.equal (origin_dir))
+            return target.get_basename ();
 
-        Gdk.error_trap_push ();
+        // Get a list of parent directories. Stop at the common dir.
+        List<File> target_dirs = new List<File> ();
+        List<File> origin_dirs = new List<File> ();
 
-        int result = x_display.get_window_property (Gdk.x11_drawable_get_xid (window),
-            Gdk.x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"),
-            0, long.MAX, false, X.XA_CARDINAL, out type, out format, out nitems,
-            out bytes_after, out workspace);
+        while (target_parent != null && ! target_parent.equal (common_dir))
+        {
+            target_dirs.prepend (target_parent);
+            target_parent = target_parent.get_parent ();
+        }
 
-        int err = Gdk.error_trap_pop ();
+        while (origin_dir != null && ! origin_dir.equal (common_dir))
+        {
+            origin_dirs.prepend (origin_dir);
+            origin_dir = origin_dir.get_parent ();
+        }
 
-        if (err != X.Success || result != X.Success)
-            return ret;
+        // Get number of common dirs
+        uint dir_index = 0;
+        while (dir_index < target_dirs.length () && dir_index < origin_dirs.length ())
+        {
+            File cur_target_dir = target_dirs.nth_data (dir_index);
+            File cur_origin_dir = origin_dirs.nth_data (dir_index);
+            if (! cur_target_dir.equal (cur_origin_dir))
+                break;
 
-        if (type == X.XA_CARDINAL && format == 32 && nitems > 0)
-            ret = workspace[0];
+            dir_index++;
+        }
 
-        X.free (workspace);
-        return ret;
-    }
+        uint nb_common_dirs = dir_index;
 
-    public Widget add_scrollbar (Widget child)
-    {
-        ScrolledWindow scrollbar = new ScrolledWindow (null, null);
-        scrollbar.set_policy (PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
-        scrollbar.add (child);
-        return scrollbar;
-    }
+        /* Build the relative path */
+        string relative_path = "";
 
-    public void print_build_tool (BuildTool build_tool)
-    {
-        stdout.printf ("\n=== Build Tool ===\n");
-        stdout.printf ("desc: %s\n", build_tool.description);
-        stdout.printf ("ext: %s\n", build_tool.extensions);
-        stdout.printf ("label: %s\n", build_tool.label);
-        stdout.printf ("icon: %s\n\n", build_tool.icon);
+        // go to the common dir
+        uint nb_remaining_origin_dirs = origin_dirs.length () - nb_common_dirs;
+        for (uint i = 0 ; i < nb_remaining_origin_dirs ; i++)
+            relative_path += "../";
 
-        foreach (BuildJob build_job in build_tool.jobs)
+        // go to the target dir
+        for (uint i = nb_common_dirs ; i < target_dirs.length () ; i++)
         {
-            stdout.printf ("== Build Job ==\n");
-            stdout.printf ("must succeed: %s\n", build_job.must_succeed.to_string ());
-            stdout.printf ("post processor: %s\n",
-                BuildTools.get_post_processor_name_from_type (build_job.post_processor));
-            stdout.printf ("command: %s\n\n", build_job.command);
+            File cur_target_dir = target_dirs.nth_data (i);
+            relative_path += cur_target_dir.get_basename () + "/";
         }
-    }
 
-    public void delete_file (File file)
-    {
-        try
-        {
-            file.delete ();
-        }
-        catch (Error e)
-        {
-            stderr.printf ("Warning: delete file \"%s\" failed: %s\n",
-                file.get_parse_name (), e.message);
-        }
+        // add the target basename
+        relative_path += target.get_basename ();
+        return relative_path;
     }
 
-    public bool tree_model_iter_prev (TreeModel model, ref TreeIter iter)
+
+    /*************************************************************************/
+    // UI stuff
+
+    public Widget add_scrollbar (Widget child)
     {
-        TreePath path = model.get_path (iter);
-        if (path.prev ())
-        {
-            bool ret = model.get_iter (out iter, path);
-            return ret;
-        }
-        return false;
+        ScrolledWindow scrollbar = new ScrolledWindow (null, null);
+        scrollbar.set_policy (PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
+        scrollbar.add (child);
+        return scrollbar;
     }
 
     public void set_entry_error (Widget entry, bool error)
@@ -220,6 +262,17 @@ namespace Utils
         }
     }
 
+    public bool tree_model_iter_prev (TreeModel model, ref TreeIter iter)
+    {
+        TreePath path = model.get_path (iter);
+        if (path.prev ())
+        {
+            bool ret = model.get_iter (out iter, path);
+            return ret;
+        }
+        return false;
+    }
+
     // get indice of selected row in the treeview
     // returns -1 if no row is selected
     public int get_selected_row (TreeView view, out TreeIter iter = null)
@@ -265,112 +318,95 @@ namespace Utils
         return button;
     }
 
-    public bool char_is_escaped (string text, long char_index)
+    public Widget get_dialog_component (string title, Widget widget)
     {
-        return_val_if_fail (char_index < text.length, false);
-
-        int index = (int) char_index;
-        if (! string_get_prev_char (text, ref index, null))
-            return false;
-
-        bool escaped = false;
-        while (true)
-        {
-            unichar cur_char;
-            bool first_char = ! string_get_prev_char (text, ref index, out cur_char);
+        VBox vbox = new VBox (false, 6);
+        vbox.border_width = 6;
+
+        // title in bold, left aligned
+        Label label = new Label (null);
+        label.set_markup ("<b>" + title + "</b>");
+        label.xalign = (float) 0.0;
+        vbox.pack_start (label, false, false);
+
+        // left margin for the widget
+        Alignment alignment = new Alignment ((float) 0.5, (float) 0.5, (float) 1.0,
+            (float) 1.0);
+        alignment.left_padding = 12;
+        alignment.add (widget);
+        vbox.pack_start (alignment);
+
+        return vbox;
+    }
 
-            if (cur_char != '\\')
-                break;
 
-            escaped = ! escaped;
+    /*************************************************************************/
+    // Misc
 
-            if (first_char)
-                break;
-        }
-
-        return escaped;
-    }
-
-    // The opposite of string.get_next_char ().
-    // TODO remove this function when it is included upstream
-    // See https://bugzilla.gnome.org/show_bug.cgi?id=655185
-    private bool string_get_prev_char (string str, ref int index, out unichar c)
+    public void flush_queue ()
     {
-        c = str.get_char (index);
-        if (index <= 0 || c == '\0')
-            return false;
-
-        unowned string str_at_index = (string) ((char*) str + index);
-        unowned string str_prev = str_at_index.prev_char ();
-        index = (int) ((char*) str_prev - (char*) str);
-        return true;
+        while (Gtk.events_pending ())
+            Gtk.main_iteration ();
     }
 
-    // origin can be equal to common_dir, but target must be different
-    public string? get_relative_path (File origin, File target, File common_dir)
-    {
-        File? origin_dir;
-        if (origin.equal (common_dir))
-            origin_dir = origin;
-        else
-            origin_dir = origin.get_parent ();
+    public const uint ALL_WORKSPACES = 0xffffff;
 
-        File? target_parent = target.get_parent ();
+    /* Get the workspace the window is on
+     *
+     * This function gets the workspace that the #GtkWindow is visible on,
+     * it returns ALL_WORKSPACES if the window is sticky, or if
+     * the window manager doesn't support this function.
+     */
+    public uint get_window_workspace (Gtk.Window gtkwindow)
+    {
+        return_val_if_fail (gtkwindow.get_realized (), 0);
 
-        return_val_if_fail (origin_dir != null, null);
-        return_val_if_fail (target_parent != null, null);
+        uint ret = ALL_WORKSPACES;
 
-        // The origin is in the same directory as the target.
-        if (target_parent.equal (origin_dir))
-            return target.get_basename ();
+        Gdk.Window window = gtkwindow.get_window ();
+        Gdk.Display display = window.get_display ();
+        unowned X.Display x_display = Gdk.x11_display_get_xdisplay (display);
 
-        // Get a list of parent directories. Stop at the common dir.
-        List<File> target_dirs = new List<File> ();
-        List<File> origin_dirs = new List<File> ();
+        X.Atom type;
+        int format;
+        ulong nitems;
+        ulong bytes_after;
+        uint *workspace;
 
-        while (target_parent != null && ! target_parent.equal (common_dir))
-        {
-            target_dirs.prepend (target_parent);
-            target_parent = target_parent.get_parent ();
-        }
+        Gdk.error_trap_push ();
 
-        while (origin_dir != null && ! origin_dir.equal (common_dir))
-        {
-            origin_dirs.prepend (origin_dir);
-            origin_dir = origin_dir.get_parent ();
-        }
+        int result = x_display.get_window_property (Gdk.x11_drawable_get_xid (window),
+            Gdk.x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"),
+            0, long.MAX, false, X.XA_CARDINAL, out type, out format, out nitems,
+            out bytes_after, out workspace);
 
-        // Get number of common dirs
-        uint dir_index = 0;
-        while (dir_index < target_dirs.length () && dir_index < origin_dirs.length ())
-        {
-            File cur_target_dir = target_dirs.nth_data (dir_index);
-            File cur_origin_dir = origin_dirs.nth_data (dir_index);
-            if (! cur_target_dir.equal (cur_origin_dir))
-                break;
+        int err = Gdk.error_trap_pop ();
 
-            dir_index++;
-        }
+        if (err != X.Success || result != X.Success)
+            return ret;
 
-        uint nb_common_dirs = dir_index;
+        if (type == X.XA_CARDINAL && format == 32 && nitems > 0)
+            ret = workspace[0];
 
-        /* Build the relative path */
-        string relative_path = "";
+        X.free (workspace);
+        return ret;
+    }
 
-        // go to the common dir
-        uint nb_remaining_origin_dirs = origin_dirs.length () - nb_common_dirs;
-        for (uint i = 0 ; i < nb_remaining_origin_dirs ; i++)
-            relative_path += "../";
+    public void print_build_tool (BuildTool build_tool)
+    {
+        stdout.printf ("\n=== Build Tool ===\n");
+        stdout.printf ("desc: %s\n", build_tool.description);
+        stdout.printf ("ext: %s\n", build_tool.extensions);
+        stdout.printf ("label: %s\n", build_tool.label);
+        stdout.printf ("icon: %s\n\n", build_tool.icon);
 
-        // go to the target dir
-        for (uint i = nb_common_dirs ; i < target_dirs.length () ; i++)
+        foreach (BuildJob build_job in build_tool.jobs)
         {
-            File cur_target_dir = target_dirs.nth_data (i);
-            relative_path += cur_target_dir.get_basename () + "/";
+            stdout.printf ("== Build Job ==\n");
+            stdout.printf ("must succeed: %s\n", build_job.must_succeed.to_string ());
+            stdout.printf ("post processor: %s\n",
+                BuildTools.get_post_processor_name_from_type (build_job.post_processor));
+            stdout.printf ("command: %s\n\n", build_job.command);
         }
-
-        // add the target basename
-        relative_path += target.get_basename ();
-        return relative_path;
     }
 }



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