[latexila/gnome-3] Side Panel: code clean-up, homogeneous margins



commit 86037431a4689604ecd34c498d408b0cb5d62f47
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Mon Apr 2 01:52:55 2012 +0200

    Side Panel: code clean-up, homogeneous margins

 src/file_browser.vala |    9 +--
 src/main_window.vala  |   26 ++++-----
 src/side_panel.vala   |  148 +++++++++++++++++++++++-------------------------
 src/symbols.vala      |    3 -
 4 files changed, 86 insertions(+), 100 deletions(-)
---
diff --git a/src/file_browser.vala b/src/file_browser.vala
index ba6418b..0e110c4 100644
--- a/src/file_browser.vala
+++ b/src/file_browser.vala
@@ -51,10 +51,11 @@ public class FileBrowser : Grid
 
     public FileBrowser (MainWindow main_window)
     {
+        _main_window = main_window;
+        _build_view = main_window.get_build_view ();
+
         row_spacing = 5;
         orientation = Orientation.VERTICAL;
-        this._main_window = main_window;
-        this._build_view = main_window.get_build_view ();
 
         init_toolbar ();
         init_combo_box ();
@@ -97,8 +98,6 @@ public class FileBrowser : Grid
         Grid grid = new Grid ();
         grid.set_orientation (Orientation.HORIZONTAL);
         grid.column_homogeneous = true;
-        grid.set_margin_left (3);
-        grid.set_margin_right (3);
         add (grid);
 
         Button home_button = Utils.get_toolbar_button (Stock.HOME);
@@ -170,8 +169,6 @@ public class FileBrowser : Grid
             typeof (File));
 
         _combo_box = new ComboBox.with_model (_parent_dir_store);
-        _combo_box.set_margin_left (3);
-        _combo_box.set_margin_right (3);
         add (_combo_box);
 
         // pixbuf
diff --git a/src/main_window.vala b/src/main_window.vala
index 793cd9c..bcbb0ca 100644
--- a/src/main_window.vala
+++ b/src/main_window.vala
@@ -188,7 +188,7 @@ public class MainWindow : Window
     private BuildView build_view;
     private Toolbar main_toolbar;
     private Toolbar edit_toolbar;
-    private SidePanel side_panel;
+    private SidePanel _side_panel;
     private Symbols symbols;
     private FileBrowser file_browser;
     private Structure _structure;
@@ -306,19 +306,19 @@ public class MainWindow : Window
 
         // side panel
         ToggleAction action_view_side_panel =
-            (ToggleAction) action_group.get_action ("ViewSidePanel");
-        side_panel = new SidePanel (this, action_view_side_panel);
+            action_group.get_action ("ViewSidePanel") as ToggleAction;
+        _side_panel = new SidePanel (action_view_side_panel);
 
         symbols = new Symbols (this);
-        side_panel.add_component (_("Symbols"), "symbol_alpha", symbols);
+        _side_panel.add_component (_("Symbols"), "symbol_alpha", symbols);
 
         file_browser = new FileBrowser (this);
-        side_panel.add_component (_("File Browser"), Stock.OPEN, file_browser);
+        _side_panel.add_component (_("File Browser"), Stock.OPEN, file_browser);
 
         _structure = new Structure (this, ui_manager);
-        side_panel.add_component (_("Structure"), Stock.INDEX, _structure);
+        _side_panel.add_component (_("Structure"), Stock.INDEX, _structure);
 
-        side_panel.restore_state ();
+        _side_panel.restore_state ();
 
         /* signal handlers */
 
@@ -488,10 +488,10 @@ public class MainWindow : Window
         vpaned.pack1 (vgrid_source_view, true, true);
         vpaned.pack2 (build_view, false, true);
 
-        main_hpaned.add1 (side_panel);
+        main_hpaned.add1 (_side_panel);
         main_hpaned.add2 (vpaned);
 
-        side_panel.show ();
+        _side_panel.show ();
         vpaned.show ();
 
         main_vgrid.add (statusbar);
@@ -638,7 +638,7 @@ public class MainWindow : Window
         show = settings.get_boolean ("side-panel-visible");
 
         if (! show)
-            side_panel.hide ();
+            _side_panel.hide ();
 
         action = (ToggleAction) action_group.get_action ("ViewSidePanel");
         action.set_active (show);
@@ -1153,8 +1153,6 @@ public class MainWindow : Window
         action = (ToggleAction) action_group.get_action ("ViewBottomPanel");
         settings_ui.set_boolean ("bottom-panel-visible", action.active);
 
-        settings_ui.set_int ("side-panel-component", side_panel.get_active_component ());
-
         action = (ToggleAction) action_group.get_action ("BuildShowWarnings");
         settings_ui.set_boolean ("show-build-warnings", action.active);
 
@@ -1695,9 +1693,9 @@ public class MainWindow : Window
     {
         bool show = (action as ToggleAction).active;
         if (show)
-            side_panel.show ();
+            _side_panel.show ();
         else
-            side_panel.hide ();
+            _side_panel.hide ();
     }
 
     public void on_show_bottom_panel (Gtk.Action action)
diff --git a/src/side_panel.vala b/src/side_panel.vala
index 78e29e3..4a87ce3 100644
--- a/src/side_panel.vala
+++ b/src/side_panel.vala
@@ -1,7 +1,7 @@
 /*
  * This file is part of LaTeXila.
  *
- * Copyright  2010-2011 SÃbastien Wilmet
+ * Copyright  2010-2012 SÃbastien Wilmet
  *
  * LaTeXila is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -21,63 +21,75 @@ using Gtk;
 
 public class SidePanel : Grid
 {
-    private unowned MainWindow main_window;
-    private unowned ToggleAction action_view_side_panel;
+    private enum SidePanelColumn
+    {
+        PIXBUF,
+        NAME,
+        N_COLUMNS
+    }
 
-    private Grid[] components;
-    private ComboBox combo_box;
-    private ListStore list_store;
+    private GLib.Settings _settings;
+    private Gee.ArrayList<Grid?> _components;
+    private ComboBox _combo_box;
+    private ListStore _list_store;
+    private int _current_component = -1;
 
-    public SidePanel (MainWindow main_window, ToggleAction action_view_side_panel)
+    public SidePanel (ToggleAction action_view_side_panel)
     {
-        orientation = Orientation.VERTICAL;
-        this.main_window = main_window;
-        this.action_view_side_panel = action_view_side_panel;
-
-        Grid grid = new Grid ();
-        grid.set_orientation (Orientation.HORIZONTAL);
-        grid.column_spacing = 3;
-        grid.border_width = 3;
-        add (grid);
-
-        combo_box = get_combo_box ();
-        combo_box.set_hexpand (true);
-        grid.add (combo_box);
-        grid.add (get_close_button ());
-        show_all ();
+        _settings = new GLib.Settings ("org.gnome.latexila.preferences.ui");
+        _components = new Gee.ArrayList<Grid?> ();
 
-        show.connect (show_active_component);
-        hide.connect (hide_all_components);
-    }
+        margin_left = 6;
+        margin_top = 3;
+        column_spacing = 3;
+        row_spacing = 3;
 
-    public void add_component (string name, string stock_id, Grid component)
-    {
-        TreeIter iter;
-        list_store.append (out iter);
-        list_store.set (iter,
-            SidePanelColumn.PIXBUF, stock_id,
-            SidePanelColumn.NAME, name,
-            -1);
+        init_combo_box ();
+        Button close_button = get_close_button (action_view_side_panel);
 
-        add (component);
-        components += component;
+        attach (_combo_box, 0, 0, 1, 1);
+        attach (close_button, 1, 0, 1, 1);
+        show_all ();
     }
 
-    public void restore_state ()
+    private void init_combo_box ()
     {
-        GLib.Settings settings = new GLib.Settings ("org.gnome.latexila.preferences.ui");
-        int num = settings.get_int ("side-panel-component");
-        num = num.clamp (0, components.length - 1);
-        combo_box.set_active (num);
+        _list_store = new ListStore (SidePanelColumn.N_COLUMNS,
+            typeof (string), // pixbuf (stock-id)
+            typeof (string)  // name
+        );
+
+        _combo_box = new ComboBox.with_model (_list_store);
+        _combo_box.hexpand = true;
+
+        CellRendererPixbuf pixbuf_renderer = new CellRendererPixbuf ();
+        _combo_box.pack_start (pixbuf_renderer, false);
+        _combo_box.set_attributes (pixbuf_renderer,
+            "stock-id", SidePanelColumn.PIXBUF, null);
+
+        CellRendererText text_renderer = new CellRendererText ();
+        text_renderer.ellipsize_set = true;
+        text_renderer.ellipsize = Pango.EllipsizeMode.END;
+        _combo_box.pack_start (text_renderer, true);
+        _combo_box.set_attributes (text_renderer, "text", SidePanelColumn.NAME, null);
+
+        /* signals */
+        _combo_box.changed.connect (show_active_component);
+
+        // Save which component is displayed. Since the component can be different
+        // on each window, we make only a SET (not a GET).
+        _settings.bind ("side-panel-component", _combo_box, "active",
+            SettingsBindFlags.SET);
     }
 
-    private Button get_close_button ()
+    private Button get_close_button (ToggleAction action_view_side_panel)
     {
         Button close_button = new Button ();
         close_button.relief = ReliefStyle.NONE;
         close_button.focus_on_click = false;
         close_button.tooltip_text = _("Hide panel");
         close_button.add (new Image.from_stock (Stock.CLOSE, IconSize.MENU));
+
         close_button.clicked.connect (() =>
         {
             this.hide ();
@@ -87,52 +99,34 @@ public class SidePanel : Grid
         return close_button;
     }
 
-    private enum SidePanelColumn
-    {
-        PIXBUF,
-        NAME,
-        N_COLUMNS
-    }
-
-    private ComboBox get_combo_box ()
+    public void add_component (string name, string stock_id, Grid component)
     {
-        list_store = new ListStore (SidePanelColumn.N_COLUMNS, typeof (string),
-            typeof (string));
-
-        ComboBox combo_box = new ComboBox.with_model (list_store);
-
-        CellRendererPixbuf pixbuf_renderer = new CellRendererPixbuf ();
-        combo_box.pack_start (pixbuf_renderer, false);
-        combo_box.set_attributes (pixbuf_renderer,
-            "stock-id", SidePanelColumn.PIXBUF, null);
-
-        CellRendererText text_renderer = new CellRendererText ();
-        text_renderer.ellipsize_set = true;
-        text_renderer.ellipsize = Pango.EllipsizeMode.END;
-        combo_box.pack_start (text_renderer, true);
-        combo_box.set_attributes (text_renderer, "text", SidePanelColumn.NAME, null);
-
-        combo_box.changed.connect (show_active_component);
-
-        return combo_box;
-    }
+        TreeIter iter;
+        _list_store.append (out iter);
+        _list_store.set (iter,
+            SidePanelColumn.PIXBUF, stock_id,
+            SidePanelColumn.NAME, name);
 
-    public int get_active_component ()
-    {
-        return combo_box.get_active ();
+        _components.add (component);
+        attach (component, 0, _components.size, 2, 1);
     }
 
-    private void hide_all_components ()
+    public void restore_state ()
     {
-        foreach (Grid component in components)
+        foreach (Grid component in _components)
             component.hide ();
+
+        int num = _settings.get_int ("side-panel-component");
+        num = num.clamp (0, _components.size - 1);
+        _combo_box.set_active (num);
     }
 
     private void show_active_component ()
     {
-        hide_all_components ();
+        if (0 <= _current_component && _current_component < _components.size)
+            _components[_current_component].hide ();
 
-        int active = get_active_component ();
-            components[active].show ();
+        _current_component = _combo_box.active;
+        _components[_current_component].show ();
     }
 }
diff --git a/src/symbols.vala b/src/symbols.vala
index 5848d30..09df126 100644
--- a/src/symbols.vala
+++ b/src/symbols.vala
@@ -786,8 +786,6 @@ public class Symbols : Grid
     private void create_combo_box ()
     {
         ComboBox combo_box = new ComboBox.with_model (categories_store);
-        combo_box.set_margin_left (3);
-        combo_box.set_margin_right (3);
         combo_box.hexpand = true;
 
         CellRendererPixbuf pixbuf_renderer = new CellRendererPixbuf ();
@@ -837,7 +835,6 @@ public class Symbols : Grid
 
         /* clear button (for most used symbols) */
         _clear_button = new Button.from_stock (Stock.CLEAR);
-        _clear_button.margin = 2;
         add (_clear_button);
 
         /* signals */



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