[libdazzle] pathbar: add DzlPathBar



commit cb1dab337176a2218ed83a4535023004fd56445c
Author: Christian Hergert <chergert redhat com>
Date:   Fri Jun 9 15:18:49 2017 -0700

    pathbar: add DzlPathBar
    
    This pathbar widget is a fairly simple implementation to allow
    building a DzlPath of elements and associate that with the
    DzlPathBar widget.
    
    Generally, these are "immutable" in a sense, in that it is not
    intended that you mutate a DzlPath after creating it.

 data/themes/shared.css                |    1 +
 data/themes/shared/shared-pathbar.css |   34 +++
 src/dazzle.gresources.xml             |    1 +
 src/dazzle.h                          |    3 +
 src/meson.build                       |    8 +
 src/pathbar/dzl-path-bar.c            |  451 +++++++++++++++++++++++++++++++++
 src/pathbar/dzl-path-bar.h            |   40 +++
 src/pathbar/dzl-path-element.c        |  189 ++++++++++++++
 src/pathbar/dzl-path-element.h        |   40 +++
 src/pathbar/dzl-path.c                |  183 +++++++++++++
 src/pathbar/dzl-path.h                |   48 ++++
 tests/meson.build                     |    6 +
 tests/test-path-bar.c                 |  107 ++++++++
 13 files changed, 1111 insertions(+), 0 deletions(-)
---
diff --git a/data/themes/shared.css b/data/themes/shared.css
index 36e29ce..45f0fcb 100644
--- a/data/themes/shared.css
+++ b/data/themes/shared.css
@@ -1,6 +1,7 @@
 /* Theme agnostic or base-layer CSS styling */
 @import url("resource:///org/gnome/dazzle/themes/shared/shared-graphs.css");
 @import url("resource:///org/gnome/dazzle/themes/shared/shared-panels.css");
+@import url("resource:///org/gnome/dazzle/themes/shared/shared-pathbar.css");
 @import url("resource:///org/gnome/dazzle/themes/shared/shared-pillbox.css");
 @import url("resource:///org/gnome/dazzle/themes/shared/shared-preferences.css");
 @import url("resource:///org/gnome/dazzle/themes/shared/shared-progressbutton.css");
diff --git a/data/themes/shared/shared-pathbar.css b/data/themes/shared/shared-pathbar.css
new file mode 100644
index 0000000..836b75b
--- /dev/null
+++ b/data/themes/shared/shared-pathbar.css
@@ -0,0 +1,34 @@
+dzlpathbar button {
+  background: none;
+  border: none;
+  box-shadow: none;
+  border-radius: 0px;
+  border-top: 2px solid transparent;
+  border-bottom: 2px solid transparent;
+  opacity: 0.75;
+  padding: 3px 3px 0px 3px;
+}
+
+dzlpathbar button image {
+  color: alpha(currentColor, 0.8);
+}
+
+dzlpathbar label.separator {
+  font-size: 0.83333em;
+  opacity: 0.55;
+}
+
+dzlpathbar button:checked {
+  border-bottom: 2px solid @theme_selected_bg_color;
+  font-weight: bold;
+  opacity: 1;
+}
+
+dzlpathbar button:checked:backdrop {
+  border-bottom-color: alpha(@borders,.75);
+}
+
+dzlpathbar button:hover {
+  opacity: 0.9;
+  border-bottom-color: @borders;
+}
diff --git a/src/dazzle.gresources.xml b/src/dazzle.gresources.xml
index 0d90dae..558309c 100644
--- a/src/dazzle.gresources.xml
+++ b/src/dazzle.gresources.xml
@@ -45,6 +45,7 @@
     <file compressed="true" alias="shared.css">../data/themes/shared.css</file>
     <file compressed="true" alias="shared/shared-graphs.css">../data/themes/shared/shared-graphs.css</file>
     <file compressed="true" alias="shared/shared-panels.css">../data/themes/shared/shared-panels.css</file>
+    <file compressed="true" alias="shared/shared-pathbar.css">../data/themes/shared/shared-pathbar.css</file>
     <file compressed="true" alias="shared/shared-pillbox.css">../data/themes/shared/shared-pillbox.css</file>
     <file compressed="true" 
alias="shared/shared-preferences.css">../data/themes/shared/shared-preferences.css</file>
     <file compressed="true" 
alias="shared/shared-progressbutton.css">../data/themes/shared/shared-progressbutton.css</file>
diff --git a/src/dazzle.h b/src/dazzle.h
index 2c2b406..473cdd8 100644
--- a/src/dazzle.h
+++ b/src/dazzle.h
@@ -68,6 +68,9 @@ G_BEGIN_DECLS
 #include "panel/dzl-dock.h"
 #include "panel/dzl-tab-strip.h"
 #include "panel/dzl-tab.h"
+#include "pathbar/dzl-path.h"
+#include "pathbar/dzl-path-bar.h"
+#include "pathbar/dzl-path-element.h"
 #include "prefs/dzl-preferences-bin.h"
 #include "prefs/dzl-preferences-entry.h"
 #include "prefs/dzl-preferences-file-chooser-button.h"
diff --git a/src/meson.build b/src/meson.build
index d8948e3..6f8cf91 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -77,6 +77,10 @@ libdazzle_public_headers = [
   'panel/dzl-tab-strip.h',
   'panel/dzl-tab.h',
 
+  'pathbar/dzl-path.h',
+  'pathbar/dzl-path-bar.h',
+  'pathbar/dzl-path-element.h',
+
   'prefs/dzl-preferences-bin.h',
   'prefs/dzl-preferences-entry.h',
   'prefs/dzl-preferences-file-chooser-button.h',
@@ -215,6 +219,10 @@ libdazzle_public_sources = [
   'panel/dzl-tab-strip.c',
   'panel/dzl-tab.c',
 
+  'pathbar/dzl-path.c',
+  'pathbar/dzl-path-bar.c',
+  'pathbar/dzl-path-element.c',
+
   'prefs/dzl-preferences-bin.c',
   'prefs/dzl-preferences-entry.c',
   'prefs/dzl-preferences-file-chooser-button.c',
diff --git a/src/pathbar/dzl-path-bar.c b/src/pathbar/dzl-path-bar.c
new file mode 100644
index 0000000..8b29798
--- /dev/null
+++ b/src/pathbar/dzl-path-bar.c
@@ -0,0 +1,451 @@
+/* dzl-path-bar.c
+ *
+ * Copyright (C) 2016-2017 Christian Hergert <chergert redhat com>
+ *
+ * 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/>.
+ */
+
+#define G_LOG_DOMAIN "dzl-path-bar"
+
+#include <dazzle.h>
+#include <string.h>
+
+#include "dzl-path.h"
+#include "dzl-path-bar.h"
+#include "dzl-path-element.h"
+
+struct _DzlPathBar
+{
+  GtkBox    parent_instance;
+  DzlPath *path;
+};
+
+G_DEFINE_TYPE (DzlPathBar, dzl_path_bar, GTK_TYPE_BOX)
+
+enum {
+  PROP_0,
+  PROP_PATH,
+  N_PROPS
+};
+
+enum {
+  ELEMENT_SELECTED,
+  POPULATE_MENU,
+  N_SIGNALS
+};
+
+static GParamSpec *properties [N_PROPS];
+static guint signals [N_SIGNALS];
+
+static void dzl_path_bar_button_clicked (DzlPathBar     *self,
+                                          GtkToggleButton *button);
+
+GtkWidget *
+dzl_path_bar_new (void)
+{
+  return g_object_new (DZL_TYPE_PATH_BAR, NULL);
+}
+
+static void
+dzl_path_bar_finalize (GObject *object)
+{
+  DzlPathBar *self = (DzlPathBar *)object;
+
+  g_clear_object (&self->path);
+
+  G_OBJECT_CLASS (dzl_path_bar_parent_class)->finalize (object);
+}
+
+static void
+dzl_path_bar_get_property (GObject    *object,
+                           guint       prop_id,
+                           GValue     *value,
+                           GParamSpec *pspec)
+{
+  DzlPathBar *self = DZL_PATH_BAR (object);
+
+  switch (prop_id)
+    {
+    case PROP_PATH:
+      g_value_set_object (value, self->path);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+dzl_path_bar_set_property (GObject      *object,
+                           guint         prop_id,
+                           const GValue *value,
+                           GParamSpec   *pspec)
+{
+  DzlPathBar *self = DZL_PATH_BAR (object);
+
+  switch (prop_id)
+    {
+    case PROP_PATH:
+      dzl_path_bar_set_path (self, g_value_get_object (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+dzl_path_bar_class_init (DzlPathBarClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  object_class->finalize = dzl_path_bar_finalize;
+  object_class->get_property = dzl_path_bar_get_property;
+  object_class->set_property = dzl_path_bar_set_property;
+
+  properties [PROP_PATH] =
+    g_param_spec_object ("path",
+                         "Path",
+                         "Path",
+                         DZL_TYPE_PATH,
+                         (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+
+  signals [ELEMENT_SELECTED] =
+    g_signal_new ("element-selected",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_LAST,
+                  0, NULL, NULL, NULL,
+                  G_TYPE_NONE, 2, DZL_TYPE_PATH, DZL_TYPE_PATH_ELEMENT);
+
+  signals [POPULATE_MENU] =
+    g_signal_new ("populate-menu",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_LAST,
+                  0, NULL, NULL, NULL,
+                  G_TYPE_NONE, 3, DZL_TYPE_PATH, DZL_TYPE_PATH_ELEMENT, G_TYPE_MENU);
+
+  gtk_widget_class_set_css_name (widget_class, "dzlpathbar");
+}
+
+static void
+dzl_path_bar_init (DzlPathBar *self)
+{
+  gtk_box_set_spacing (GTK_BOX (self), 12);
+}
+
+static void
+dzl_path_bar_buttons_foreach_cb (GtkWidget *widget,
+                                 gpointer   user_data)
+{
+  struct {
+    GtkCallback callback;
+    gpointer user_data;
+  } *closure = user_data;
+
+  g_assert (GTK_IS_WIDGET (widget));
+  g_assert (closure != NULL);
+  g_assert (closure->callback != NULL);
+
+  if (GTK_IS_BOX (widget))
+    gtk_container_foreach (GTK_CONTAINER (widget),
+                           dzl_path_bar_buttons_foreach_cb,
+                           closure);
+  else if (GTK_IS_TOGGLE_BUTTON (widget))
+    closure->callback (widget, closure->user_data);
+}
+
+static void
+dzl_path_bar_buttons_foreach (DzlPathBar  *self,
+                              GtkCallback  callback,
+                              gpointer     user_data)
+{
+  struct {
+    GtkCallback callback;
+    gpointer user_data;
+  } closure = { callback, user_data };
+
+  g_assert (DZL_IS_PATH_BAR (self));
+  g_assert (callback != NULL);
+
+  gtk_container_foreach (GTK_CONTAINER (self),
+                         dzl_path_bar_buttons_foreach_cb,
+                         &closure);
+}
+
+static void
+dzl_path_bar_set_blocked_cb (GtkWidget *widget,
+                             gpointer   user_data)
+{
+  struct {
+    DzlPathBar *self;
+    gboolean     block;
+  } *block_data = user_data;
+
+  g_assert (GTK_IS_WIDGET (widget));
+  g_assert (block_data != NULL);
+  g_assert (DZL_IS_PATH_BAR (block_data->self));
+
+  if (block_data->block)
+    g_signal_handlers_block_by_func (widget,
+                                     dzl_path_bar_button_clicked,
+                                     block_data->self);
+  else
+    g_signal_handlers_unblock_by_func (widget,
+                                       dzl_path_bar_button_clicked,
+                                       block_data->self);
+}
+
+static void
+dzl_path_bar_set_blocked (DzlPathBar *self,
+                          gboolean    blocked)
+{
+  struct {
+    DzlPathBar *self;
+    gboolean     block;
+  } block_data = { self, blocked };
+
+  g_assert (DZL_IS_PATH_BAR (self));
+
+  dzl_path_bar_buttons_foreach (self,
+                                 dzl_path_bar_set_blocked_cb,
+                                 &block_data);
+}
+
+static void
+dzl_path_bar_set_active_cb (GtkWidget *widget,
+                            gpointer   user_data)
+{
+  GtkWidget *active = user_data;
+
+  g_assert (GTK_IS_TOGGLE_BUTTON (widget));
+  g_assert (GTK_IS_TOGGLE_BUTTON (active));
+
+  if (active != widget)
+    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), FALSE);
+}
+
+static void
+dzl_path_bar_button_clicked (DzlPathBar      *self,
+                             GtkToggleButton *button)
+{
+  g_autoptr(GMenu) menu = NULL;
+  DzlPathElement *element;
+
+  g_assert (DZL_IS_PATH_BAR (self));
+  g_assert (GTK_IS_TOGGLE_BUTTON (button));
+
+  /* block all other clicked events while we process */
+  dzl_path_bar_set_blocked (self, TRUE);
+
+  /* don't allow unselecting the item */
+  if (!gtk_toggle_button_get_active (button))
+    gtk_toggle_button_set_active (button, TRUE);
+
+  /* set all the other items as disabled */
+  dzl_path_bar_buttons_foreach (self,
+                                 dzl_path_bar_set_active_cb,
+                                 button);
+
+  /* possibly show a menu for this item */
+  menu = g_menu_new ();
+  element = g_object_get_data (G_OBJECT (button), "DZL_PATH_ELEMENT");
+  g_assert (DZL_IS_PATH_ELEMENT (element));
+  g_signal_emit (self, signals [POPULATE_MENU], 0, self->path, element, menu);
+  if (g_menu_model_get_n_items (G_MENU_MODEL (menu)) != 0)
+    {
+      GtkPopover *popover;
+
+      popover = g_object_new (GTK_TYPE_POPOVER,
+                              "modal", TRUE,
+                              "relative-to", button,
+                              "position", GTK_POS_BOTTOM,
+                              NULL);
+      gtk_popover_bind_model (popover, G_MENU_MODEL (menu), NULL);
+      g_signal_connect (popover,
+                        "closed",
+                        G_CALLBACK (gtk_widget_destroy),
+                        NULL);
+      gtk_widget_show (GTK_WIDGET (popover));
+    }
+
+  /* unblock clicked events again */
+  dzl_path_bar_set_blocked (self, FALSE);
+
+  /* notify handlers that an item was selected */
+  g_signal_emit (self, signals [ELEMENT_SELECTED], 0, self->path, element);
+}
+
+static GtkWidget *
+dzl_path_bar_create_element (DzlPathBar     *self,
+                             DzlPathElement *element,
+                             gboolean        is_last)
+{
+  const gchar *title;
+  const gchar *icon_name;
+  GtkToggleButton *button;
+  GtkLabel *slash;
+  GtkLabel *label;
+  GtkImage *image;
+  GtkBox *box;
+  GtkBox *box2;
+
+  g_assert (DZL_IS_PATH_BAR (self));
+  g_assert (DZL_IS_PATH_ELEMENT (element));
+
+  title = dzl_path_element_get_title (element);
+  icon_name = dzl_path_element_get_icon_name (element);
+
+  box = g_object_new (GTK_TYPE_BOX,
+                      "orientation", GTK_ORIENTATION_HORIZONTAL,
+                      "spacing", 12,
+                      "visible", TRUE,
+                      "valign", GTK_ALIGN_BASELINE,
+                      NULL);
+
+  button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
+                         "active", is_last == TRUE,
+                         "focus-on-click", FALSE,
+                         "visible", TRUE,
+                         "valign", GTK_ALIGN_BASELINE,
+                         NULL);
+  g_object_set_data_full (G_OBJECT (button),
+                          "DZL_PATH_ELEMENT",
+                          g_object_ref (element),
+                          g_object_unref);
+  g_signal_connect_object (button,
+                           "clicked",
+                           G_CALLBACK (dzl_path_bar_button_clicked),
+                           self,
+                           G_CONNECT_SWAPPED | G_CONNECT_AFTER);
+  gtk_container_add (GTK_CONTAINER (box), GTK_WIDGET (button));
+
+  slash = g_object_new (GTK_TYPE_LABEL,
+                        "label", "/",
+                        "valign", GTK_ALIGN_BASELINE,
+                        "visible", is_last == FALSE,
+                        NULL);
+  gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (slash)), "separator");
+  gtk_container_add (GTK_CONTAINER (box), GTK_WIDGET (slash));
+
+  box2 = g_object_new (GTK_TYPE_BOX,
+                       "orientation", GTK_ORIENTATION_HORIZONTAL,
+                       "spacing", 6,
+                       "valign", GTK_ALIGN_BASELINE,
+                       "visible", TRUE,
+                       NULL);
+  gtk_container_add (GTK_CONTAINER (button), GTK_WIDGET (box2));
+
+  if (icon_name != NULL)
+    {
+      image = g_object_new (GTK_TYPE_IMAGE,
+                            "icon-name", icon_name,
+                            "pixel-size", 16,
+                            "valign", GTK_ALIGN_BASELINE,
+                            "visible", TRUE,
+                            NULL);
+      gtk_container_add (GTK_CONTAINER (box2), GTK_WIDGET (image));
+    }
+
+  label = g_object_new (DZL_TYPE_BOLDING_LABEL,
+                        "label", title,
+                        "valign", GTK_ALIGN_BASELINE,
+                        "visible", TRUE,
+                        NULL);
+  gtk_container_add (GTK_CONTAINER (box2), GTK_WIDGET (label));
+
+  return GTK_WIDGET (box);
+}
+
+/**
+ * dzl_path_bar_get_path:
+ *
+ * Gets the path for the view.
+ *
+ * Returns: (transfer none): A #DzlPath.
+ */
+DzlPath *
+dzl_path_bar_get_path (DzlPathBar *self)
+{
+  g_return_val_if_fail (DZL_IS_PATH_BAR (self), NULL);
+
+  return self->path;
+}
+
+void
+dzl_path_bar_set_path (DzlPathBar *self,
+                       DzlPath    *path)
+{
+  g_return_if_fail (DZL_IS_PATH_BAR (self));
+
+  if (g_set_object (&self->path, path))
+    {
+      gtk_container_foreach (GTK_CONTAINER (self), (GtkCallback)gtk_widget_destroy, NULL);
+
+      if (path != NULL)
+        {
+          GList *items;
+
+          items = dzl_path_get_elements (path);
+
+          for (; items != NULL; items = items->next)
+            {
+              DzlPathElement *element = items->data;
+              GtkWidget *widget;
+
+              widget = dzl_path_bar_create_element (self, element, items->next == NULL);
+              gtk_container_add (GTK_CONTAINER (self), widget);
+            }
+        }
+    }
+}
+
+static void
+dzl_path_bar_set_selected_index_cb (GtkWidget *widget,
+                                    gpointer   user_data)
+{
+  struct {
+    GtkWidget *button;
+    guint      index;
+  } *lookup = user_data;
+
+  g_assert (GTK_IS_WIDGET (widget));
+  g_assert (lookup != NULL);
+
+  if (lookup->button == NULL && lookup->index == 0)
+    lookup->button = widget;
+  else
+    lookup->index--;
+}
+
+void
+dzl_path_bar_set_selected_index (DzlPathBar *self,
+                                 guint       index)
+{
+  struct {
+    GtkWidget *button;
+    guint      index;
+  } lookup = { NULL, index };
+
+  g_return_if_fail (DZL_IS_PATH_BAR (self));
+
+  dzl_path_bar_buttons_foreach (self,
+                                 dzl_path_bar_set_selected_index_cb,
+                                 &lookup);
+
+  if (lookup.button)
+    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (lookup.button), TRUE);
+}
diff --git a/src/pathbar/dzl-path-bar.h b/src/pathbar/dzl-path-bar.h
new file mode 100644
index 0000000..1035cef
--- /dev/null
+++ b/src/pathbar/dzl-path-bar.h
@@ -0,0 +1,40 @@
+/* dzl-path-bar.h
+ *
+ * Copyright (C) 2016-2017 Christian Hergert <chergert redhat com>
+ *
+ * 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 DZL_PATH_BAR_H
+#define DZL_PATH_BAR_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define DZL_TYPE_PATH_BAR (dzl_path_bar_get_type())
+
+G_DECLARE_FINAL_TYPE (DzlPathBar, dzl_path_bar, DZL, PATH_BAR, GtkBox)
+
+GtkWidget *dzl_path_bar_new                (void);
+DzlPath  *dzl_path_bar_get_path           (DzlPathBar *self);
+void       dzl_path_bar_set_path           (DzlPathBar *self,
+                                            DzlPath   *path);
+void       dzl_path_bar_set_selected_index (DzlPathBar *self,
+                                            guint       index);
+
+G_END_DECLS
+
+#endif /* DZL_PATH_BAR_H */
+
diff --git a/src/pathbar/dzl-path-element.c b/src/pathbar/dzl-path-element.c
new file mode 100644
index 0000000..ea8b17f
--- /dev/null
+++ b/src/pathbar/dzl-path-element.c
@@ -0,0 +1,189 @@
+/* dzl-path-element.c
+ *
+ * Copyright (C) 2016-2017 Christian Hergert <chergert redhat com>
+ *
+ * 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 "dzl-path-element.h"
+
+struct _DzlPathElement
+{
+  GObject parent_instance;
+  gchar *icon_name;
+  gchar *id;
+  gchar *title;
+};
+
+G_DEFINE_TYPE (DzlPathElement, dzl_path_element, G_TYPE_OBJECT)
+
+enum {
+  PROP_0,
+  PROP_ICON_NAME,
+  PROP_ID,
+  PROP_TITLE,
+  N_PROPS
+};
+
+static GParamSpec *properties [N_PROPS];
+
+/**
+ * dzl_path_element_new:
+ * @id: (nullable): An id for the path element.
+ * @icon_name: (nullable): An optional icon name for the element
+ * @title: The title of the element.
+ *
+ * Creates a new path element for an #RtfmPath.
+ *
+ * Returns: (transfer full): A #DzlPathElement
+ */
+DzlPathElement *
+dzl_path_element_new (const gchar *id,
+                      const gchar *icon_name,
+                      const gchar *title)
+{
+  return g_object_new (DZL_TYPE_PATH_ELEMENT,
+                       "icon-name", icon_name,
+                       "id", id,
+                       "title", title,
+                       NULL);
+}
+
+static void
+dzl_path_element_finalize (GObject *object)
+{
+  DzlPathElement *self = (DzlPathElement *)object;
+
+  g_clear_pointer (&self->icon_name, g_free);
+  g_clear_pointer (&self->id, g_free);
+  g_clear_pointer (&self->title, g_free);
+
+  G_OBJECT_CLASS (dzl_path_element_parent_class)->finalize (object);
+}
+
+static void
+dzl_path_element_get_property (GObject    *object,
+                               guint       prop_id,
+                               GValue     *value,
+                               GParamSpec *pspec)
+{
+  DzlPathElement *self = DZL_PATH_ELEMENT (object);
+
+  switch (prop_id)
+    {
+    case PROP_ICON_NAME:
+      g_value_set_string (value, self->icon_name);
+      break;
+
+    case PROP_ID:
+      g_value_set_string (value, self->id);
+      break;
+
+    case PROP_TITLE:
+      g_value_set_string (value, self->title);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+dzl_path_element_set_property (GObject      *object,
+                               guint         prop_id,
+                               const GValue *value,
+                               GParamSpec   *pspec)
+{
+  DzlPathElement *self = DZL_PATH_ELEMENT (object);
+
+  switch (prop_id)
+    {
+    case PROP_ICON_NAME:
+      self->icon_name = g_value_dup_string (value);
+      break;
+
+    case PROP_ID:
+      self->id = g_value_dup_string (value);
+      break;
+
+    case PROP_TITLE:
+      self->title = g_value_dup_string (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+dzl_path_element_class_init (DzlPathElementClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = dzl_path_element_finalize;
+  object_class->get_property = dzl_path_element_get_property;
+  object_class->set_property = dzl_path_element_set_property;
+
+  properties [PROP_ICON_NAME] =
+    g_param_spec_string ("icon-name",
+                         "Icon Name",
+                         "The icon name for the path element",
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY| G_PARAM_STATIC_STRINGS));
+
+  properties [PROP_ID] =
+    g_param_spec_string ("id",
+                         "Identifier",
+                         "Identifier",
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY| G_PARAM_STATIC_STRINGS));
+
+  properties [PROP_TITLE] =
+    g_param_spec_string ("title",
+                         "Title",
+                         "Title",
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+dzl_path_element_init (DzlPathElement *self)
+{
+}
+
+const gchar *
+dzl_path_element_get_id (DzlPathElement *self)
+{
+  g_return_val_if_fail (DZL_IS_PATH_ELEMENT (self), NULL);
+
+  return self->id;
+}
+
+const gchar *
+dzl_path_element_get_icon_name (DzlPathElement *self)
+{
+  g_return_val_if_fail (DZL_IS_PATH_ELEMENT (self), NULL);
+
+  return self->icon_name;
+}
+
+const gchar *
+dzl_path_element_get_title (DzlPathElement *self)
+{
+  g_return_val_if_fail (DZL_IS_PATH_ELEMENT (self), NULL);
+
+  return self->title;
+}
diff --git a/src/pathbar/dzl-path-element.h b/src/pathbar/dzl-path-element.h
new file mode 100644
index 0000000..0b7f8fc
--- /dev/null
+++ b/src/pathbar/dzl-path-element.h
@@ -0,0 +1,40 @@
+/* dzl-path-element.h
+ *
+ * Copyright (C) 2016-2017 Christian Hergert <chergert redhat com>
+ *
+ * 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 DZL_PATH_ELEMENT_H
+#define DZL_PATH_ELEMENT_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define DZL_TYPE_PATH_ELEMENT (dzl_path_element_get_type())
+
+G_DECLARE_FINAL_TYPE (DzlPathElement, dzl_path_element, DZL, PATH_ELEMENT, GObject)
+
+DzlPathElement *dzl_path_element_new           (const gchar    *id,
+                                                const gchar    *icon_name,
+                                                const gchar    *title);
+const gchar    *dzl_path_element_get_title     (DzlPathElement *self);
+const gchar    *dzl_path_element_get_id        (DzlPathElement *self);
+const gchar    *dzl_path_element_get_icon_name (DzlPathElement *self);
+
+G_END_DECLS
+
+#endif /* DZL_PATH_ELEMENT_H */
+
diff --git a/src/pathbar/dzl-path.c b/src/pathbar/dzl-path.c
new file mode 100644
index 0000000..da5fe94
--- /dev/null
+++ b/src/pathbar/dzl-path.c
@@ -0,0 +1,183 @@
+/* dzl-path.c
+ *
+ * Copyright (C) 2016-2017 Christian Hergert <chergert redhat com>
+ *
+ * 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/>.
+ */
+
+#define G_LOG_DOMAIN "dzl-path"
+
+#include "dzl-path.h"
+#include "dzl-path-element.h"
+
+struct _DzlPath
+{
+  GObject  parent_instance;
+  GQueue  *elements;
+};
+
+G_DEFINE_TYPE (DzlPath, dzl_path, G_TYPE_OBJECT)
+
+static void
+dzl_path_finalize (GObject *object)
+{
+  DzlPath *self = (DzlPath *)object;
+
+  g_queue_free_full (self->elements, g_object_unref);
+  self->elements = NULL;
+
+  G_OBJECT_CLASS (dzl_path_parent_class)->finalize (object);
+}
+
+static void
+dzl_path_class_init (DzlPathClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = dzl_path_finalize;
+}
+
+static void
+dzl_path_init (DzlPath *self)
+{
+  self->elements = g_queue_new ();
+}
+
+/**
+ * dzl_path_get_elements:
+ *
+ * Returns: (transfer none) (element-type Rtfm.PathElement): The elements of the path.
+ */
+GList *
+dzl_path_get_elements (DzlPath *self)
+{
+  g_return_val_if_fail (DZL_IS_PATH (self), NULL);
+
+  return self->elements->head;
+}
+
+DzlPath *
+dzl_path_new (void)
+{
+  return g_object_new (DZL_TYPE_PATH, NULL);
+}
+
+void
+dzl_path_append (DzlPath        *self,
+                 DzlPathElement *element)
+{
+  g_return_if_fail (DZL_IS_PATH (self));
+  g_return_if_fail (DZL_IS_PATH_ELEMENT (element));
+
+  g_queue_push_tail (self->elements, g_object_ref (element));
+}
+
+void
+dzl_path_prepend (DzlPath        *self,
+                  DzlPathElement *element)
+{
+  g_return_if_fail (DZL_IS_PATH (self));
+  g_return_if_fail (DZL_IS_PATH_ELEMENT (element));
+
+  g_queue_push_head (self->elements, g_object_ref (element));
+}
+
+gboolean
+dzl_path_has_prefix (DzlPath *self,
+                     DzlPath *prefix)
+{
+  const GList *iter;
+  const GList *spec;
+
+  g_return_val_if_fail (DZL_IS_PATH (self), FALSE);
+  g_return_val_if_fail (DZL_IS_PATH (prefix), FALSE);
+
+  if (self->elements->length < prefix->elements->length)
+    return FALSE;
+
+  for (iter = self->elements->head, spec = prefix->elements->head;
+       iter != NULL && spec != NULL;
+       iter = iter->next, spec = spec->next)
+    {
+      DzlPathElement *spec_element = spec->data;
+      DzlPathElement *iter_element = iter->data;
+      const gchar *spec_id = dzl_path_element_get_id (spec_element);
+      const gchar *iter_id = dzl_path_element_get_id (iter_element);
+
+      if (g_strcmp0 (spec_id, iter_id) == 0)
+        continue;
+
+      return FALSE;
+    }
+
+  return TRUE;
+}
+
+guint
+dzl_path_get_length (DzlPath *self)
+{
+  g_return_val_if_fail (DZL_IS_PATH (self), 0);
+
+  return self->elements->length;
+}
+
+gchar *
+dzl_path_printf (DzlPath *self)
+{
+  const GList *iter;
+  GString *str;
+
+  g_return_val_if_fail (DZL_IS_PATH (self), NULL);
+
+  str = g_string_new (NULL);
+
+  for (iter = self->elements->head; iter != NULL; iter = iter->next)
+    {
+      DzlPathElement *element = iter->data;
+      const gchar *id = dzl_path_element_get_id (element);
+
+      g_string_append (str, id);
+      if (iter->next != NULL)
+        g_string_append_c (str, ',');
+    }
+
+  return g_string_free (str, FALSE);
+}
+
+gboolean
+dzl_path_is_empty (DzlPath *self)
+{
+  g_return_val_if_fail (DZL_IS_PATH (self), FALSE);
+
+  return self->elements->length == 0;
+}
+
+/**
+ * dzl_path_get_element:
+ *
+ * Gets the path element found at @index.
+ *
+ * Indexes start from zero.
+ *
+ * Returns: (nullable) (transfer none): An #DzlPathElement.
+ */
+DzlPathElement *
+dzl_path_get_element (DzlPath *self,
+                      guint     index)
+{
+  g_return_val_if_fail (DZL_IS_PATH (self), NULL);
+  g_return_val_if_fail (index < self->elements->length, NULL);
+
+  return g_queue_peek_nth (self->elements, index);
+}
diff --git a/src/pathbar/dzl-path.h b/src/pathbar/dzl-path.h
new file mode 100644
index 0000000..ebba914
--- /dev/null
+++ b/src/pathbar/dzl-path.h
@@ -0,0 +1,48 @@
+/* dzl-path.h
+ *
+ * Copyright (C) 2016-2017 Christian Hergert <chergert redhat com>
+ *
+ * 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 DZL_PATH_H
+#define DZL_PATH_H
+
+#include <glib-object.h>
+
+#include "dzl-path-element.h"
+
+G_BEGIN_DECLS
+
+#define DZL_TYPE_PATH (dzl_path_get_type())
+
+G_DECLARE_FINAL_TYPE (DzlPath, dzl_path, DZL, PATH, GObject)
+
+DzlPath        *dzl_path_new          (void);
+void            dzl_path_prepend      (DzlPath        *self,
+                                       DzlPathElement *element);
+void            dzl_path_append       (DzlPath        *self,
+                                       DzlPathElement *element);
+GList          *dzl_path_get_elements (DzlPath        *self);
+gboolean        dzl_path_has_prefix   (DzlPath        *self,
+                                       DzlPath        *prefix);
+guint           dzl_path_get_length   (DzlPath        *self);
+DzlPathElement *dzl_path_get_element  (DzlPath        *self,
+                                       guint           index);
+gchar          *dzl_path_printf       (DzlPath        *self);
+gboolean        dzl_path_is_empty     (DzlPath        *self);
+
+G_END_DECLS
+
+#endif /* DZL_PATH_H */
diff --git a/tests/meson.build b/tests/meson.build
index ba1b813..472b05b 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -244,3 +244,9 @@ test_int_pair = executable('test-int-pair', 'test-int-pair.c',
      link_args: test_link_args,
   dependencies: libdazzle_deps + [libdazzle_dep],
 )
+
+test_path_bar = executable('test-path-bar', 'test-path-bar.c',
+        c_args: test_cflags,
+     link_args: test_link_args,
+  dependencies: libdazzle_deps + [libdazzle_dep],
+)
diff --git a/tests/test-path-bar.c b/tests/test-path-bar.c
new file mode 100644
index 0000000..8092b0b
--- /dev/null
+++ b/tests/test-path-bar.c
@@ -0,0 +1,107 @@
+#include <dazzle.h>
+
+static void
+load_css (void)
+{
+  g_autoptr(GtkCssProvider) provider = NULL;
+
+  provider = dzl_css_provider_new ("/org/gnome/dazzle/themes");
+  gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
+                                             GTK_STYLE_PROVIDER (provider),
+                                             GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+}
+
+gint
+main (gint argc,
+      gchar *argv[])
+{
+  GtkWidget *window;
+  GtkWidget *headerbar;
+  GtkWidget *pathbar;
+  DzlPath *path;
+  DzlPathElement *ele0;
+  DzlPathElement *ele1;
+  DzlPathElement *ele2;
+  DzlPathElement *ele3;
+
+  gtk_init (&argc, &argv);
+
+  load_css ();
+
+  window = g_object_new (GTK_TYPE_WINDOW,
+                         "default-width", 600,
+                         "default-height", 200,
+                         "title", "Test Path Bar",
+                         NULL);
+  headerbar = g_object_new (GTK_TYPE_HEADER_BAR,
+                            "show-close-button", TRUE,
+                            "visible", TRUE,
+                            NULL);
+  gtk_window_set_titlebar (GTK_WINDOW (window), headerbar);
+
+  pathbar = g_object_new (DZL_TYPE_PATH_BAR,
+                          "hexpand", TRUE,
+                          "visible", TRUE,
+                          NULL);
+  gtk_container_add (GTK_CONTAINER (headerbar), pathbar);
+
+  path = dzl_path_new ();
+  g_assert (dzl_path_is_empty (path));
+  g_assert_cmpint (dzl_path_get_length (path), ==, 0);
+  g_assert (NULL == dzl_path_get_elements (path));
+
+  ele1 = dzl_path_element_new ("home", "user-home-symbolic", "Home");
+  dzl_path_append (path, ele1);
+  g_assert (NULL != dzl_path_get_elements (path));
+  g_assert (dzl_path_get_elements (path)->data == ele1);
+
+  ele2 = dzl_path_element_new ("christian", NULL, "Xtian");
+  dzl_path_append (path, ele2);
+  g_assert (dzl_path_get_elements (path)->next->data == ele2);
+  g_assert (dzl_path_get_elements (path)->next->next == NULL);
+
+  ele3 = dzl_path_element_new ("music", NULL, "Music");
+  dzl_path_append (path, ele3);
+  g_assert (dzl_path_get_elements (path)->data == ele1);
+  g_assert (dzl_path_get_elements (path)->next->data == ele2);
+  g_assert (dzl_path_get_elements (path)->next->next->data == ele3);
+  g_assert (dzl_path_get_elements (path)->next->next->next == NULL);
+
+  ele0 = dzl_path_element_new ("computer", "computer-symbolic", "Computer");
+  dzl_path_prepend (path, ele0);
+  g_assert (dzl_path_get_elements (path)->data == ele0);
+  g_assert (dzl_path_get_elements (path)->next->data == ele1);
+
+  dzl_path_bar_set_path (DZL_PATH_BAR (pathbar), path);
+
+  g_signal_connect (window, "delete-event", gtk_main_quit, NULL);
+  gtk_window_present (GTK_WINDOW (window));
+
+  g_object_add_weak_pointer (G_OBJECT (pathbar), (gpointer *)&pathbar);
+
+  gtk_main ();
+
+  g_object_add_weak_pointer (G_OBJECT (path), (gpointer *)&path);
+  g_object_add_weak_pointer (G_OBJECT (ele0), (gpointer *)&ele0);
+  g_object_add_weak_pointer (G_OBJECT (ele1), (gpointer *)&ele1);
+  g_object_add_weak_pointer (G_OBJECT (ele2), (gpointer *)&ele2);
+  g_object_add_weak_pointer (G_OBJECT (ele3), (gpointer *)&ele3);
+
+  /* ensure we clear path references */
+  if (pathbar)
+    dzl_path_bar_set_path (DZL_PATH_BAR (pathbar), NULL);
+
+  g_object_unref (path);
+  g_object_unref (ele0);
+  g_object_unref (ele1);
+  g_object_unref (ele2);
+  g_object_unref (ele3);
+
+  g_assert (path == NULL);
+  g_assert (ele0 == NULL);
+  g_assert (ele1 == NULL);
+  g_assert (ele2 == NULL);
+  g_assert (ele3 == NULL);
+
+  return 0;
+}



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