[gnome-builder/wip/vim] prefs: start plumbing an preferences dialog.



commit a1f2f60743c5002583d15a8118f00dbfbb7055e8
Author: Christian Hergert <christian hergert me>
Date:   Thu Oct 2 18:42:37 2014 -0700

    prefs: start plumbing an preferences dialog.
    
    Once we can get this in place enough to enable VIM, we can merge this
    to master.

 src/app/gb-application.c                  |   27 ++
 src/gnome-builder.mk                      |    6 +
 src/preferences/gb-preferences-window.c   |  105 ++++++
 src/preferences/gb-preferences-window.h   |   56 +++
 src/resources/css/builder.Adwaita.css     |   41 ++-
 src/resources/gnome-builder.gresource.xml |    1 +
 src/resources/ui/gb-preferences-window.ui |   51 +++
 src/sidebar/gb-sidebar.c                  |  522 +++++++++++++++++++++++++++++
 src/sidebar/gb-sidebar.h                  |   69 ++++
 9 files changed, 864 insertions(+), 14 deletions(-)
---
diff --git a/src/app/gb-application.c b/src/app/gb-application.c
index c46cd72..83548dd 100644
--- a/src/app/gb-application.c
+++ b/src/app/gb-application.c
@@ -29,6 +29,7 @@
 #include "gb-editor-workspace.h"
 #include "gb-log.h"
 #include "gb-keybindings.h"
+#include "gb-preferences-window.h"
 #include "gb-resources.h"
 #include "gb-workbench.h"
 
@@ -261,6 +262,31 @@ gb_application_activate_quit_action (GSimpleAction *action,
 }
 
 static void
+gb_application_activate_preferences_action (GSimpleAction *action,
+                                            GVariant      *parameter,
+                                            gpointer       user_data)
+{
+  GbApplication *application = user_data;
+  GbPreferencesWindow *window;
+  GbWorkbench *workbench = NULL;
+  GList *list;
+
+  g_return_if_fail (GB_IS_APPLICATION (application));
+
+  list = gtk_application_get_windows (GTK_APPLICATION (application));
+
+  for (; list; list = list->next)
+    if (GB_IS_WORKBENCH (list->data))
+      workbench = GB_WORKBENCH (list->data);
+
+  window = g_object_new (GB_TYPE_PREFERENCES_WINDOW,
+                         "transient-for", workbench,
+                         NULL);
+
+  gtk_window_present (GTK_WINDOW (window));
+}
+
+static void
 gb_application_activate_about_action (GSimpleAction *action,
                                       GVariant      *parameter,
                                       gpointer       user_data)
@@ -317,6 +343,7 @@ gb_application_register_actions (GbApplication *self)
 {
   static const GActionEntry action_entries[] = {
     { "about", gb_application_activate_about_action },
+    { "preferences", gb_application_activate_preferences_action },
     { "quit", gb_application_activate_quit_action },
   };
 
diff --git a/src/gnome-builder.mk b/src/gnome-builder.mk
index 60e0e77..f9dfc86 100644
--- a/src/gnome-builder.mk
+++ b/src/gnome-builder.mk
@@ -55,6 +55,10 @@ libgnome_builder_la_SOURCES = \
        src/navigation/gb-navigation-list.c \
        src/navigation/gb-navigation-item.h \
        src/navigation/gb-navigation-item.c \
+       src/preferences/gb-preferences-window.c \
+       src/preferences/gb-preferences-window.h \
+       src/sidebar/gb-sidebar.c \
+       src/sidebar/gb-sidebar.h \
        src/snippets/gb-source-snippet-chunk.c \
        src/snippets/gb-source-snippet-chunk.h \
        src/snippets/gb-source-snippet-completion-item.c \
@@ -141,8 +145,10 @@ libgnome_builder_la_CFLAGS = \
        -I$(top_srcdir)/src/markdown \
        -I$(top_srcdir)/src/nautilus \
        -I$(top_srcdir)/src/navigation \
+       -I$(top_srcdir)/src/preferences \
        -I$(top_srcdir)/src/resources \
        -I$(top_builddir)/src/resources \
+       -I$(top_srcdir)/src/sidebar \
        -I$(top_srcdir)/src/snippets \
        -I$(top_srcdir)/src/tabs \
        -I$(top_srcdir)/src/trie \
diff --git a/src/preferences/gb-preferences-window.c b/src/preferences/gb-preferences-window.c
new file mode 100644
index 0000000..a7b5bcf
--- /dev/null
+++ b/src/preferences/gb-preferences-window.c
@@ -0,0 +1,105 @@
+/* gb-preferences-window.c
+ *
+ * Copyright (C) 2014 Christian Hergert <christian hergert me>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <glib/gi18n.h>
+
+#include "gb-preferences-window.h"
+#include "gb-sidebar.h"
+
+struct _GbPreferencesWindowPrivate
+{
+  void *d;
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (GbPreferencesWindow, gb_preferences_window,
+                            GTK_TYPE_WINDOW)
+
+enum {
+  PROP_0,
+  LAST_PROP
+};
+
+static GParamSpec *gParamSpecs [LAST_PROP];
+
+GtkWidget *
+gb_preferences_window_new (void)
+{
+  return g_object_new (GB_TYPE_PREFERENCES_WINDOW, NULL);
+}
+
+static void
+gb_preferences_window_finalize (GObject *object)
+{
+  GbPreferencesWindowPrivate *priv = GB_PREFERENCES_WINDOW (object)->priv;
+
+  G_OBJECT_CLASS (gb_preferences_window_parent_class)->finalize (object);
+}
+
+static void
+gb_preferences_window_get_property (GObject    *object,
+                                    guint       prop_id,
+                                    GValue     *value,
+                                    GParamSpec *pspec)
+{
+  GbPreferencesWindow *self = GB_PREFERENCES_WINDOW (object);
+
+  switch (prop_id)
+    {
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gb_preferences_window_set_property (GObject      *object,
+                                    guint         prop_id,
+                                    const GValue *value,
+                                    GParamSpec   *pspec)
+{
+  GbPreferencesWindow *self = GB_PREFERENCES_WINDOW (object);
+
+  switch (prop_id)
+    {
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gb_preferences_window_class_init (GbPreferencesWindowClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  object_class->finalize = gb_preferences_window_finalize;
+  object_class->get_property = gb_preferences_window_get_property;
+  object_class->set_property = gb_preferences_window_set_property;
+
+  gtk_widget_class_set_template_from_resource (widget_class,
+                                               "/org/gnome/builder/ui/gb-preferences-window.ui");
+
+  g_type_ensure (GB_TYPE_SIDEBAR);
+}
+
+static void
+gb_preferences_window_init (GbPreferencesWindow *self)
+{
+  self->priv = gb_preferences_window_get_instance_private (self);
+
+  gtk_widget_init_template (GTK_WIDGET (self));
+}
diff --git a/src/preferences/gb-preferences-window.h b/src/preferences/gb-preferences-window.h
new file mode 100644
index 0000000..d97f56c
--- /dev/null
+++ b/src/preferences/gb-preferences-window.h
@@ -0,0 +1,56 @@
+/* gb-preferences-window.h
+ *
+ * Copyright (C) 2014 Christian Hergert <christian hergert me>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GB_PREFERENCES_WINDOW_H
+#define GB_PREFERENCES_WINDOW_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GB_TYPE_PREFERENCES_WINDOW            (gb_preferences_window_get_type())
+#define GB_PREFERENCES_WINDOW(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), 
GB_TYPE_PREFERENCES_WINDOW, GbPreferencesWindow))
+#define GB_PREFERENCES_WINDOW_CONST(obj)      (G_TYPE_CHECK_INSTANCE_CAST ((obj), 
GB_TYPE_PREFERENCES_WINDOW, GbPreferencesWindow const))
+#define GB_PREFERENCES_WINDOW_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass),  
GB_TYPE_PREFERENCES_WINDOW, GbPreferencesWindowClass))
+#define GB_IS_PREFERENCES_WINDOW(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), 
GB_TYPE_PREFERENCES_WINDOW))
+#define GB_IS_PREFERENCES_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),  
GB_TYPE_PREFERENCES_WINDOW))
+#define GB_PREFERENCES_WINDOW_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),  
GB_TYPE_PREFERENCES_WINDOW, GbPreferencesWindowClass))
+
+typedef struct _GbPreferencesWindow        GbPreferencesWindow;
+typedef struct _GbPreferencesWindowClass   GbPreferencesWindowClass;
+typedef struct _GbPreferencesWindowPrivate GbPreferencesWindowPrivate;
+
+struct _GbPreferencesWindow
+{
+  GtkWindow parent;
+
+  /*< private >*/
+  GbPreferencesWindowPrivate *priv;
+};
+
+struct _GbPreferencesWindowClass
+{
+  GtkWindowClass parent;
+};
+
+GType      gb_preferences_window_get_type (void) G_GNUC_CONST;
+GtkWidget *gb_preferences_window_new      (void);
+
+G_END_DECLS
+
+#endif /* GB_PREFERENCES_WINDOW_H */
diff --git a/src/resources/css/builder.Adwaita.css b/src/resources/css/builder.Adwaita.css
index 5b89352..d1dd56b 100644
--- a/src/resources/css/builder.Adwaita.css
+++ b/src/resources/css/builder.Adwaita.css
@@ -1,5 +1,7 @@
+/*
+ * Nautilus floating bar used for cursor status in editor.
+ */
 @define-color floating_bar_inset_color alpha(white, 0.45);
-
 .floating-bar {
     background-image: linear-gradient(to bottom,
                                       @theme_base_color 20%,
@@ -16,46 +18,38 @@
     border-style: solid;
     box-shadow: inset 1px 1px @floating_bar_inset_color, -1px -1px @floating_bar_inset_color;
 }
-
 .floating-bar.top {
     border-top-width: 0;
     border-top-right-radius: 0;
     border-top-left-radius: 0;
 }
-
 .floating-bar.right {
     border-right-width: 0;
     border-top-right-radius: 0;
     border-bottom-right-radius: 0;
 }
-
 .floating-bar.bottom {
     border-bottom-width: 0;
     border-bottom-right-radius: 0;
     border-bottom-left-radius: 0;
 }
-
 .floating-bar.left {
     border-left-width: 0;
     border-top-left-radius: 0;
     border-bottom-left-radius: 0;
 }
-
 .floating-bar.bottom.right {
     box-shadow: inset 1px 1px @floating_bar_inset_color;
 }
-
 .floating-bar.bottom.left {
     box-shadow: inset -1px 1px @floating_bar_inset_color;
 }
-
 .floating-bar:backdrop {
     background-color: @theme_unfocused_base_color;
     border-color: shade(@theme_unfocused_base_color, 0.9);
     background-image: none;
     box-shadow: none;
 }
-
 .floating-bar .button {
     background-color: transparent;
     background-image: none;
@@ -67,6 +61,10 @@
     -GtkButton-inner-border: 0;
 }
 
+
+/*
+ * Search entry that slides down from top of editor.
+ */
 GbEditorTab .gb-search-slider {
     background-color: @theme_base_color;
     padding: 6px;
@@ -75,13 +73,16 @@ GbEditorTab .gb-search-slider {
     border-width: 0 1px 1px 1px;
     border-style: solid;
 }
-
 .gb-search-entry-occurrences-tag {
     color: shade (@theme_unfocused_fg_color, 0.8);
     margin: 2px;
     padding: 2px;
 }
 
+
+/*
+ * Vim command entry that slides up from the buttom.
+ */
 GbEditorTab .gb-vim-command-slider {
     background-color: shade (@theme_base_color, 0.8);
     padding: 6px;
@@ -91,6 +92,10 @@ GbEditorTab .gb-vim-command-slider {
     border-style: solid;
 }
 
+
+/*
+ * Workspace switcher, thin-panel on left side.
+ */
 GtkStackSwitcher.gb-workspace-switcher > GtkRadioButton {
     background-color: transparent;
     background-image: none;
@@ -102,23 +107,19 @@ GtkStackSwitcher.gb-workspace-switcher > GtkRadioButton {
     -GtkButton-inner-border: 0;
     padding: 4px 1px 4px 1px;
 }
-
 GtkStackSwitcher.gb-workspace-switcher > GtkRadioButton:checked {
     background-color: shade(@theme_base_color, 0.70);
     color: #ffffff;
 }
-
 GtkStackSwitcher.gb-workspace-switcher > GtkRadioButton:checked:hover {
     border-color: shade(@theme_base_color, 0.60);
 }
-
 GtkStackSwitcher.gb-workspace-switcher > GtkRadioButton:hover {
     border-style: solid;
     border-width: 1px 1px 1px 1px;
     border-color: shade(@theme_base_color, 0.80);
     transition-duration: 0ms;
 }
-
 GtkStackSwitcher.gb-workspace-switcher > GtkRadioButton:active {
     background-color: shade(@theme_base_color, 0.80);
     border-style: solid;
@@ -126,3 +127,15 @@ GtkStackSwitcher.gb-workspace-switcher > GtkRadioButton:active {
     border-color: shade(@theme_base_color, 0.60);
     transition-duration: 0ms;
 }
+
+
+/*
+ * Preferences sidebar style.
+ */
+.preferences.sidebar GtkListBox {
+    background-color: shade(@theme_bg_color, 0.98);
+}
+.preferences.sidebar GtkListBoxRow {
+    padding: 10px;
+}
+
diff --git a/src/resources/gnome-builder.gresource.xml b/src/resources/gnome-builder.gresource.xml
index 866602e..15a2508 100644
--- a/src/resources/gnome-builder.gresource.xml
+++ b/src/resources/gnome-builder.gresource.xml
@@ -19,6 +19,7 @@
 
     <file>ui/gb-devhelp-tab.ui</file>
     <file>ui/gb-editor-tab.ui</file>
+    <file>ui/gb-preferences-window.ui</file>
     <file>ui/gb-tab-label.ui</file>
     <file>ui/gb-workbench.ui</file>
   </gresource>
diff --git a/src/resources/ui/gb-preferences-window.ui b/src/resources/ui/gb-preferences-window.ui
new file mode 100644
index 0000000..b2d98ac
--- /dev/null
+++ b/src/resources/ui/gb-preferences-window.ui
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <!-- interface-requires gtk+ 3.8 -->
+  <template class="GbPreferencesWindow" parent="GtkWindow">
+    <property name="title" translatable="yes">Preferences</property>
+    <property name="icon-name">builder</property>
+    <property name="default-width">800</property>
+    <property name="default-height">500</property>
+    <property name="window-position">GTK_WIN_POS_CENTER_ON_PARENT</property>
+    <child>
+      <object class="GtkBox" id="hbox1">
+        <property name="expand">True</property>
+        <property name="visible">True</property>
+        <property name="orientation">horizontal</property>
+        <child>
+          <object class="GbSidebar" id="sidebar">
+            <property name="visible">True</property>
+            <property name="width-request">215</property>
+            <property name="stack">stack</property>
+            <style>
+              <class name="preferences" />
+            </style>
+          </object>
+        </child>
+        <child>
+          <object class="GtkSeparator" id="separator1">
+            <property name="orientation">vertical</property>
+            <property name="visible">True</property>
+            <property name="expand">False</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkStack" id="stack">
+            <property name="visible">True</property>
+            <property name="expand">True</property>
+            <child>
+              <object class="GtkGrid" id="editor_grid">
+                <property name="visible">True</property>
+                <property name="expand">True</property>
+              </object>
+              <packing>
+                <property name="name">editor</property>
+                <property name="title" translatable="yes">Editor</property>
+              </packing>
+            </child>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/sidebar/gb-sidebar.c b/src/sidebar/gb-sidebar.c
new file mode 100644
index 0000000..7d58b3b
--- /dev/null
+++ b/src/sidebar/gb-sidebar.c
@@ -0,0 +1,522 @@
+/*
+ * Copyright (c) 2014 Intel Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author:
+ *      Ikey Doherty <michael i doherty intel com>
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <glib/gi18n.h>
+
+#include "gb-sidebar.h"
+
+/**
+ * SECTION:gb-sidebar
+ * @short_description: An automatic sidebar widget
+ * @title: GbSidebar
+ *
+ * A GbSidebarWindow enables you to quickly and easily provide a consistent
+ * "sidebar" object for your user interface.
+ *
+ * In order to use a GbSidebar, you simply use a GtkStack to organise
+ * your UI flow, and add the sidebar to your sidebar area. You can use
+ * gb_sidebar_set_stack() to connect the #GbSidebar to the #GtkStack.
+ *
+ * Since: 3.16
+ */
+
+struct _GbSidebarPrivate
+{
+  GtkListBox *list;
+  GtkStack *stack;
+  GHashTable *rows;
+  gboolean in_child_changed;
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (GbSidebar, gb_sidebar, GTK_TYPE_BIN)
+
+enum
+{
+  PROP_0,
+  PROP_STACK,
+  N_PROPERTIES
+};
+static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
+
+static void
+gb_sidebar_set_property (GObject    *object,
+                          guint       prop_id,
+                          const       GValue *value,
+                          GParamSpec *pspec)
+{
+  switch (prop_id)
+    {
+    case PROP_STACK:
+      gb_sidebar_set_stack (GB_SIDEBAR (object), g_value_get_object (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+gb_sidebar_get_property (GObject    *object,
+                          guint       prop_id,
+                          GValue     *value,
+                          GParamSpec *pspec)
+{
+  GbSidebarPrivate *priv = gb_sidebar_get_instance_private (GB_SIDEBAR (object));
+
+  switch (prop_id)
+    {
+    case PROP_STACK:
+      g_value_set_object (value, priv->stack);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+update_header (GtkListBoxRow *row,
+               GtkListBoxRow *before,
+               gpointer       userdata)
+{
+  GtkWidget *ret = NULL;
+
+  if (before && !gtk_list_box_row_get_header (row))
+    {
+      ret = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
+      gtk_list_box_row_set_header (row, ret);
+    }
+}
+
+static gint
+sort_list (GtkListBoxRow *row1,
+           GtkListBoxRow *row2,
+           gpointer       userdata)
+{
+  GbSidebar *sidebar = GB_SIDEBAR (userdata);
+  GbSidebarPrivate *priv = gb_sidebar_get_instance_private (sidebar);
+  GtkWidget *item;
+  GtkWidget *widget;
+  gint left = 0; gint right = 0;
+
+
+  if (row1)
+    {
+      item = gtk_bin_get_child (GTK_BIN (row1));
+      widget = g_object_get_data (G_OBJECT (item), "stack-child");
+      gtk_container_child_get (GTK_CONTAINER (priv->stack), widget,
+                               "position", &left,
+                               NULL);
+    }
+
+  if (row2)
+    {
+      item = gtk_bin_get_child (GTK_BIN (row2));
+      widget = g_object_get_data (G_OBJECT (item), "stack-child");
+      gtk_container_child_get (GTK_CONTAINER (priv->stack), widget,
+                               "position", &right,
+                               NULL);
+    }
+
+  if (left < right)
+    return  -1;
+
+  if (left == right)
+    return 0;
+
+  return 1;
+}
+
+static void
+gb_sidebar_row_selected (GtkListBox    *box,
+                          GtkListBoxRow *row,
+                          gpointer       userdata)
+{
+  GbSidebar *sidebar = GB_SIDEBAR (userdata);
+  GbSidebarPrivate *priv = gb_sidebar_get_instance_private (sidebar);
+  GtkWidget *item;
+  GtkWidget *widget;
+
+  if (priv->in_child_changed)
+    return;
+
+  if (!row)
+    return;
+
+  item = gtk_bin_get_child (GTK_BIN (row));
+  widget = g_object_get_data (G_OBJECT (item), "stack-child");
+  gtk_stack_set_visible_child (priv->stack, widget);
+}
+
+static void
+gb_sidebar_init (GbSidebar *sidebar)
+{
+  GtkStyleContext *style;
+  GbSidebarPrivate *priv;
+  GtkWidget *sw;
+
+  priv = gb_sidebar_get_instance_private (sidebar);
+
+  sw = gtk_scrolled_window_new (NULL, NULL);
+  gtk_widget_show (sw);
+  gtk_widget_set_no_show_all (sw, TRUE);
+  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
+                                  GTK_POLICY_NEVER,
+                                  GTK_POLICY_AUTOMATIC);
+
+#if 1
+  gtk_container_add (GTK_CONTAINER (sidebar), sw);
+#else
+  _gtk_bin_set_child (GTK_BIN (sidebar), sw);
+  gtk_widget_set_parent (sw, GTK_WIDGET (sidebar));
+#endif
+
+  priv->list = GTK_LIST_BOX (gtk_list_box_new ());
+  gtk_widget_show (GTK_WIDGET (priv->list));
+
+  gtk_container_add (GTK_CONTAINER (sw), GTK_WIDGET (priv->list));
+
+  gtk_list_box_set_header_func (priv->list, update_header, sidebar, NULL);
+  gtk_list_box_set_sort_func (priv->list, sort_list, sidebar, NULL);
+
+  g_signal_connect (priv->list, "row-selected",
+                    G_CALLBACK (gb_sidebar_row_selected), sidebar);
+
+  style = gtk_widget_get_style_context (GTK_WIDGET (sidebar));
+  gtk_style_context_add_class (style, "sidebar");
+
+  priv->rows = g_hash_table_new (NULL, NULL);
+}
+
+static void
+update_row (GbSidebar *sidebar,
+            GtkWidget  *widget,
+            GtkWidget  *row)
+{
+  GbSidebarPrivate *priv = gb_sidebar_get_instance_private (sidebar);
+  GtkWidget *item;
+  gchar *title;
+  gboolean needs_attention;
+  GtkStyleContext *context;
+
+  gtk_container_child_get (GTK_CONTAINER (priv->stack), widget,
+                           "title", &title,
+                           "needs-attention", &needs_attention,
+                           NULL);
+
+  item = gtk_bin_get_child (GTK_BIN (row));
+  gtk_label_set_text (GTK_LABEL (item), title);
+
+  gtk_widget_set_visible (row, gtk_widget_get_visible (widget) && title != NULL);
+
+  context = gtk_widget_get_style_context (row);
+  if (needs_attention)
+     gtk_style_context_add_class (context, GTK_STYLE_CLASS_NEEDS_ATTENTION);
+  else
+    gtk_style_context_remove_class (context, GTK_STYLE_CLASS_NEEDS_ATTENTION);
+
+  g_free (title);
+}
+
+static void
+on_position_updated (GtkWidget  *widget,
+                     GParamSpec *pspec,
+                     GbSidebar *sidebar)
+{
+  GbSidebarPrivate *priv = gb_sidebar_get_instance_private (sidebar);
+
+  gtk_list_box_invalidate_sort (priv->list);
+}
+
+static void
+on_child_updated (GtkWidget  *widget,
+                  GParamSpec *pspec,
+                  GbSidebar *sidebar)
+{
+  GbSidebarPrivate *priv = gb_sidebar_get_instance_private (sidebar);
+  GtkWidget *row;
+
+  row = g_hash_table_lookup (priv->rows, widget);
+  update_row (sidebar, widget, row);
+}
+
+static void
+add_child (GtkWidget  *widget,
+           GbSidebar *sidebar)
+{
+  GbSidebarPrivate *priv = gb_sidebar_get_instance_private (sidebar);
+  GtkStyleContext *style;
+  GtkWidget *item;
+  GtkWidget *row;
+
+  /* Check we don't actually already know about this widget */
+  if (g_hash_table_lookup (priv->rows, widget))
+    return;
+
+  /* Make a pretty item when we add kids */
+  item = gtk_label_new ("");
+  gtk_widget_set_halign (item, GTK_ALIGN_START);
+  gtk_widget_set_valign (item, GTK_ALIGN_CENTER);
+  row = gtk_list_box_row_new ();
+  gtk_container_add (GTK_CONTAINER (row), item);
+  gtk_widget_show (item);
+
+  update_row (sidebar, widget, row);
+
+  /* Fix up styling */
+  style = gtk_widget_get_style_context (row);
+  gtk_style_context_add_class (style, "sidebar-item");
+
+  /* Hook up for events */
+  g_signal_connect (widget, "child-notify::title",
+                    G_CALLBACK (on_child_updated), sidebar);
+  g_signal_connect (widget, "child-notify::needs-attention",
+                    G_CALLBACK (on_child_updated), sidebar);
+  g_signal_connect (widget, "notify::visible",
+                    G_CALLBACK (on_child_updated), sidebar);
+  g_signal_connect (widget, "child-notify::position",
+                    G_CALLBACK (on_position_updated), sidebar);
+
+  g_object_set_data (G_OBJECT (item), "stack-child", widget);
+  g_hash_table_insert (priv->rows, widget, row);
+  gtk_container_add (GTK_CONTAINER (priv->list), row);
+}
+
+static void
+remove_child (GtkWidget  *widget,
+              GbSidebar *sidebar)
+{
+  GbSidebarPrivate *priv = gb_sidebar_get_instance_private (sidebar);
+  GtkWidget *row;
+
+  row = g_hash_table_lookup (priv->rows, widget);
+  if (!row)
+    return;
+
+  g_signal_handlers_disconnect_by_func (widget, on_child_updated, sidebar);
+  g_signal_handlers_disconnect_by_func (widget, on_position_updated, sidebar);
+
+  gtk_container_remove (GTK_CONTAINER (priv->list), row);
+  g_hash_table_remove (priv->rows, widget);
+}
+
+static void
+populate_sidebar (GbSidebar *sidebar)
+{
+  GbSidebarPrivate *priv = gb_sidebar_get_instance_private (sidebar);
+
+  gtk_container_foreach (GTK_CONTAINER (priv->stack), (GtkCallback)add_child, sidebar);
+}
+
+static void
+clear_sidebar (GbSidebar *sidebar)
+{
+  GbSidebarPrivate *priv = gb_sidebar_get_instance_private (sidebar);
+
+  gtk_container_foreach (GTK_CONTAINER (priv->stack), (GtkCallback)remove_child, sidebar);
+}
+
+static void
+on_child_changed (GtkWidget  *widget,
+                  GParamSpec *pspec,
+                  GbSidebar *sidebar)
+{
+  GbSidebarPrivate *priv = gb_sidebar_get_instance_private (sidebar);
+  GtkWidget *child;
+  GtkWidget *row;
+
+  child = gtk_stack_get_visible_child (GTK_STACK (widget));
+  row = g_hash_table_lookup (priv->rows, child);
+  if (row != NULL)
+    {
+      priv->in_child_changed = TRUE;
+      gtk_list_box_select_row (priv->list, GTK_LIST_BOX_ROW (row));
+      priv->in_child_changed = FALSE;
+    }
+}
+
+static void
+on_stack_child_added (GtkContainer *container,
+                      GtkWidget    *widget,
+                      GbSidebar   *sidebar)
+{
+  add_child (widget, sidebar);
+}
+
+static void
+on_stack_child_removed (GtkContainer *container,
+                        GtkWidget    *widget,
+                        GbSidebar   *sidebar)
+{
+  remove_child (widget, sidebar);
+}
+
+static void
+disconnect_stack_signals (GbSidebar *sidebar)
+{
+  GbSidebarPrivate *priv = gb_sidebar_get_instance_private (sidebar);
+
+  g_signal_handlers_disconnect_by_func (priv->stack, on_stack_child_added, sidebar);
+  g_signal_handlers_disconnect_by_func (priv->stack, on_stack_child_removed, sidebar);
+  g_signal_handlers_disconnect_by_func (priv->stack, on_child_changed, sidebar);
+  g_signal_handlers_disconnect_by_func (priv->stack, disconnect_stack_signals, sidebar);
+}
+
+static void
+connect_stack_signals (GbSidebar *sidebar)
+{
+  GbSidebarPrivate *priv = gb_sidebar_get_instance_private (sidebar);
+
+  g_signal_connect_after (priv->stack, "add",
+                          G_CALLBACK (on_stack_child_added), sidebar);
+  g_signal_connect_after (priv->stack, "remove",
+                          G_CALLBACK (on_stack_child_removed), sidebar);
+  g_signal_connect (priv->stack, "notify::visible-child",
+                    G_CALLBACK (on_child_changed), sidebar);
+  g_signal_connect_swapped (priv->stack, "destroy",
+                            G_CALLBACK (disconnect_stack_signals), sidebar);
+}
+
+static void
+gb_sidebar_dispose (GObject *object)
+{
+  GbSidebar *sidebar = GB_SIDEBAR (object);
+
+  gb_sidebar_set_stack (sidebar, NULL);
+
+  G_OBJECT_CLASS (gb_sidebar_parent_class)->dispose (object);
+}
+
+static void
+gb_sidebar_finalize (GObject *object)
+{
+  GbSidebar *sidebar = GB_SIDEBAR (object);
+  GbSidebarPrivate *priv = gb_sidebar_get_instance_private (sidebar);
+
+  g_hash_table_destroy (priv->rows);
+
+  G_OBJECT_CLASS (gb_sidebar_parent_class)->finalize (object);
+}
+
+static void
+gb_sidebar_class_init (GbSidebarClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->dispose = gb_sidebar_dispose;
+  object_class->finalize = gb_sidebar_finalize;
+  object_class->set_property = gb_sidebar_set_property;
+  object_class->get_property = gb_sidebar_get_property;
+
+  obj_properties[PROP_STACK] =
+      g_param_spec_object ("stack",
+                           _("Stack"),
+                           _("Associated stack for this GbSidebar"),
+                           GTK_TYPE_STACK,
+                           G_PARAM_READWRITE);
+
+  g_object_class_install_properties (object_class, N_PROPERTIES, obj_properties);
+}
+
+/**
+ * gb_sidebar_new:
+ *
+ * Creates a new sidebar.
+ *
+ * Returns: the new #GbSidebar
+ *
+ * Since: 3.16
+ */
+GtkWidget *
+gb_sidebar_new (void)
+{
+  return GTK_WIDGET (g_object_new (GB_TYPE_SIDEBAR, NULL));
+}
+
+/**
+ * gb_sidebar_set_stack:
+ * @sidebar: a #GbSidebar
+ * @stack: a #GtkStack
+ *
+ * Set the #GtkStack associated with this #GbSidebar.
+ *
+ * The sidebar widget will automatically update according to the order
+ * (packing) and items within the given #GtkStack.
+ *
+ * Since: 3.16
+ */
+void
+gb_sidebar_set_stack (GbSidebar *sidebar,
+                       GtkStack   *stack)
+{
+  GbSidebarPrivate *priv;
+
+  g_return_if_fail (GB_IS_SIDEBAR (sidebar));
+  g_return_if_fail (GTK_IS_STACK (stack) || stack == NULL);
+
+  priv = gb_sidebar_get_instance_private (sidebar);
+
+  if (priv->stack == stack)
+    return;
+
+  if (priv->stack)
+    {
+      disconnect_stack_signals (sidebar);
+      clear_sidebar (sidebar);
+      g_clear_object (&priv->stack);
+    }
+  if (stack)
+    {
+      priv->stack = g_object_ref (stack);
+      populate_sidebar (sidebar);
+      connect_stack_signals (sidebar);
+    }
+
+  gtk_widget_queue_resize (GTK_WIDGET (sidebar));
+
+  g_object_notify (G_OBJECT (sidebar), "stack");
+}
+
+/**
+ * gb_sidebar_get_stack:
+ * @sidebar: a #GbSidebar
+ *
+ * Returns: (transfer full): the associated #GtkStack
+ *
+ * Since: 3.16
+ */
+GtkStack *
+gb_sidebar_get_stack (GbSidebar *sidebar)
+{
+  GbSidebarPrivate *priv;
+
+  g_return_if_fail (GB_IS_SIDEBAR (sidebar));
+
+  priv = gb_sidebar_get_instance_private (sidebar);
+
+  return GTK_STACK (priv->stack);
+}
diff --git a/src/sidebar/gb-sidebar.h b/src/sidebar/gb-sidebar.h
new file mode 100644
index 0000000..474ce9e
--- /dev/null
+++ b/src/sidebar/gb-sidebar.h
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2014 Intel Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author:
+ *      Ikey Doherty <michael i doherty intel com>
+ */
+
+/*
+ * This file is taken from Gb+, and modified so we can use it until
+ * it is available as a dependency. Change to Gb for 3.16.
+ */
+
+#ifndef __GB_SIDEBAR_H__
+#define __GB_SIDEBAR_H__
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GB_TYPE_SIDEBAR                 (gb_sidebar_get_type ())
+#define GB_SIDEBAR(obj)                 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GB_TYPE_SIDEBAR, GbSidebar))
+#define GB_IS_SIDEBAR(obj)              (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GB_TYPE_SIDEBAR))
+#define GB_SIDEBAR_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass), GB_TYPE_SIDEBAR, GbSidebarClass))
+#define GB_IS_SIDEBAR_CLASS(klass)      (G_TYPE_CHECK_CLASS_TYPE ((klass), GB_TYPE_SIDEBAR))
+#define GB_SIDEBAR_GET_CLASS(obj)       (G_TYPE_INSTANCE_GET_CLASS ((obj), GB_TYPE_SIDEBAR, GbSidebarClass))
+
+typedef struct _GbSidebar        GbSidebar;
+typedef struct _GbSidebarPrivate GbSidebarPrivate;
+typedef struct _GbSidebarClass   GbSidebarClass;
+
+struct _GbSidebar
+{
+  GtkBin parent;
+};
+
+struct _GbSidebarClass
+{
+  GtkBinClass parent_class;
+
+  /* Padding for future expansion */
+  void (*_gb_reserved1) (void);
+  void (*_gb_reserved2) (void);
+  void (*_gb_reserved3) (void);
+  void (*_gb_reserved4) (void);
+};
+
+GType      gb_sidebar_get_type  (void) G_GNUC_CONST;
+GtkWidget *gb_sidebar_new       (void);
+void       gb_sidebar_set_stack (GbSidebar *sidebar,
+                                 GtkStack  *stack);
+GtkStack  *gb_sidebar_get_stack (GbSidebar *sidebar);
+
+G_END_DECLS
+
+#endif /* __GB_SIDEBAR_H__ */


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