[gnome-builder] egg: add EggSearchBar



commit fbeaf40e21b1107e028796d0444df096cf93ec6a
Author: Christian Hergert <christian hergert me>
Date:   Mon May 4 00:35:27 2015 -0700

    egg: add EggSearchBar
    
    GtkSearchBar has a few annoyances that we seem to be working around
    multiple times.
    
     1) It always renders an extra thick line at the bottom due to a Gtk bug
     2) We manually have to connect everything together to do type-ahead
    
    This addresses both of those issues. I consider this a workaround until
    upstream Gtk fixes #1 and makes #2 take considerable less code.
    
    The goal is to use this in various places in Builder where type-ahead
    search is needed (project selector, app preferences, project properties,
    etc).

 configure.ac                 |    3 +-
 contrib/egg/Makefile.am      |    2 +
 contrib/egg/egg-search-bar.c |  426 ++++++++++++++++++++++++++++++++++++++++++
 contrib/egg/egg-search-bar.h |   45 +++++
 4 files changed, 475 insertions(+), 1 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 09f19bf..e6722ea 100644
--- a/configure.ac
+++ b/configure.ac
@@ -125,7 +125,8 @@ PKG_CHECK_MODULES(BUILDER, [gtk+-3.0 >= gtk_required_version
                             gtksourceview-3.0 >= gtksourceview_required_version
                             libdevhelp-3.0 >= devhelp_required_version
                             libgit2-glib-1.0 >= ggit_required_version])
-PKG_CHECK_MODULES(EGG,     [glib-2.0 >= glib_required_version])
+PKG_CHECK_MODULES(EGG,     [glib-2.0 >= glib_required_version
+                           gtk+-3.0 >= gtk_required_version])
 PKG_CHECK_MODULES(LIBIDE,  [gio-2.0 >= glib_required_version
                             gio-unix-2.0 >= glib_required_version
                             gtksourceview-3.0 >= gtksourceview_required_version
diff --git a/contrib/egg/Makefile.am b/contrib/egg/Makefile.am
index dc6b3bf..dc7d0d5 100644
--- a/contrib/egg/Makefile.am
+++ b/contrib/egg/Makefile.am
@@ -3,6 +3,8 @@ noinst_LTLIBRARIES = libegg.la
 libegg_la_SOURCES = \
         egg-binding-set.c \
         egg-binding-set.h \
+        egg-search-bar.c \
+        egg-search-bar.h \
        egg-signal-group.c \
        egg-signal-group.h \
        $(NULL)
diff --git a/contrib/egg/egg-search-bar.c b/contrib/egg/egg-search-bar.c
new file mode 100644
index 0000000..55455a9
--- /dev/null
+++ b/contrib/egg/egg-search-bar.c
@@ -0,0 +1,426 @@
+/* egg-search-bar.c
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * This file 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 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This file 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 General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "egg-search-bar"
+
+#include <glib/gi18n.h>
+
+#include "egg-search-bar.h"
+
+typedef struct
+{
+  GtkRevealer    *revealer;
+  GtkBox         *box;
+  GtkEntry       *entry;
+  GtkButton      *close_button;
+
+  gulong          key_press_event_handler;
+
+  guint           search_mode_enabled : 1;
+} EggSearchBarPrivate;
+
+static void egg_search_bar_init_buildable (GtkBuildableIface *iface);
+
+G_DEFINE_TYPE_WITH_CODE (EggSearchBar, egg_search_bar, GTK_TYPE_BIN,
+                         G_ADD_PRIVATE (EggSearchBar)
+                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
+                                                egg_search_bar_init_buildable))
+
+enum {
+  PROP_0,
+  PROP_SHOW_CLOSE_BUTTON,
+  PROP_SEARCH_MODE_ENABLED,
+  LAST_PROP
+};
+
+enum {
+  ACTIVATE,
+  LAST_SIGNAL
+};
+
+static GParamSpec *gParamSpecs [LAST_PROP];
+static guint       gSignals [LAST_SIGNAL];
+
+static void
+egg_search_bar__entry_activate (EggSearchBar *self,
+                                GtkEntry     *entry)
+{
+  g_assert (EGG_IS_SEARCH_BAR (self));
+  g_assert (GTK_IS_ENTRY (entry));
+
+  g_signal_emit (self, gSignals [ACTIVATE], 0);
+}
+
+static gboolean
+is_modifier_key (const GdkEventKey *event)
+{
+  static const guint modifier_keyvals[] = {
+    GDK_KEY_Shift_L, GDK_KEY_Shift_R, GDK_KEY_Shift_Lock,
+    GDK_KEY_Caps_Lock, GDK_KEY_ISO_Lock, GDK_KEY_Control_L,
+    GDK_KEY_Control_R, GDK_KEY_Meta_L, GDK_KEY_Meta_R,
+    GDK_KEY_Alt_L, GDK_KEY_Alt_R, GDK_KEY_Super_L, GDK_KEY_Super_R,
+    GDK_KEY_Hyper_L, GDK_KEY_Hyper_R, GDK_KEY_ISO_Level3_Shift,
+    GDK_KEY_ISO_Next_Group, GDK_KEY_ISO_Prev_Group,
+    GDK_KEY_ISO_First_Group, GDK_KEY_ISO_Last_Group,
+    GDK_KEY_Mode_switch, GDK_KEY_Num_Lock, GDK_KEY_Multi_key,
+    GDK_KEY_Scroll_Lock,
+    0
+  };
+  const guint *ac_val;
+
+  g_return_val_if_fail (event != NULL, FALSE);
+
+  ac_val = modifier_keyvals;
+
+  while (*ac_val)
+    {
+      if (event->keyval == *ac_val++)
+        return TRUE;
+    }
+
+  return FALSE;
+}
+
+static gboolean
+egg_search_bar__toplevel_key_press_event (EggSearchBar *self,
+                                          GdkEventKey  *event,
+                                          GtkWindow    *toplevel)
+{
+  EggSearchBarPrivate *priv = egg_search_bar_get_instance_private (self);
+  GtkWidget *entry;
+  GtkWidget *focus;
+
+  g_assert (EGG_IS_SEARCH_BAR (self));
+  g_assert (event != NULL);
+  g_assert (GTK_IS_WINDOW (toplevel));
+
+  focus = gtk_window_get_focus (toplevel);
+  entry = GTK_WIDGET (priv->entry);
+
+  if (GTK_IS_EDITABLE (focus) && (focus != entry))
+    return GDK_EVENT_PROPAGATE;
+
+  switch (event->keyval)
+    {
+    case GDK_KEY_Escape:
+      if (priv->search_mode_enabled)
+        {
+          egg_search_bar_set_search_mode_enabled (self, FALSE);
+          return GDK_EVENT_STOP;
+        }
+      break;
+
+    case GDK_KEY_Up:
+    case GDK_KEY_KP_Up:
+    case GDK_KEY_Down:
+    case GDK_KEY_KP_Down:
+    case GDK_KEY_Left:
+    case GDK_KEY_KP_Left:
+    case GDK_KEY_Right:
+    case GDK_KEY_KP_Right:
+    case GDK_KEY_Home:
+    case GDK_KEY_KP_Home:
+    case GDK_KEY_End:
+    case GDK_KEY_KP_End:
+    case GDK_KEY_Page_Up:
+    case GDK_KEY_KP_Page_Up:
+    case GDK_KEY_Page_Down:
+    case GDK_KEY_KP_Page_Down:
+    case GDK_KEY_KP_Tab:
+    case GDK_KEY_Tab:
+      /* ignore keynav */
+      break;
+
+    default:
+      if (((event->state & GDK_MOD1_MASK) != 0) ||
+          ((event->state & GDK_CONTROL_MASK) != 0) ||
+          priv->search_mode_enabled ||
+          is_modifier_key (event))
+        break;
+
+      egg_search_bar_set_search_mode_enabled (self, TRUE);
+      gtk_widget_grab_focus (entry);
+
+      return GTK_WIDGET_GET_CLASS (entry)->key_press_event (entry, event);
+    }
+
+  return GDK_EVENT_PROPAGATE;
+}
+
+static void
+egg_search_bar_hierarchy_changed (GtkWidget *widget,
+                                  GtkWidget *old_toplevel)
+{
+  EggSearchBar *self = (EggSearchBar *)widget;
+  EggSearchBarPrivate *priv = egg_search_bar_get_instance_private (self);
+  GtkWidget *toplevel;
+
+  g_assert (EGG_IS_SEARCH_BAR (self));
+
+  toplevel = gtk_widget_get_toplevel (widget);
+
+  if (GTK_IS_WINDOW (old_toplevel))
+    {
+      g_signal_handler_disconnect (old_toplevel, priv->key_press_event_handler);
+      priv->key_press_event_handler = 0;
+    }
+
+  if (GTK_IS_WINDOW (toplevel))
+    {
+      priv->key_press_event_handler =
+        g_signal_connect_object (toplevel,
+                                 "key-press-event",
+                                 G_CALLBACK (egg_search_bar__toplevel_key_press_event),
+                                 self,
+                                 G_CONNECT_SWAPPED);
+    }
+}
+
+static GObject *
+egg_search_bar_get_internal_child (GtkBuildable *buildable,
+                                   GtkBuilder   *builder,
+                                   const gchar  *childname)
+{
+  EggSearchBar *self = (EggSearchBar *)buildable;
+  EggSearchBarPrivate *priv = egg_search_bar_get_instance_private (self);
+
+  g_assert (GTK_IS_BUILDABLE (buildable));
+  g_assert (EGG_IS_SEARCH_BAR (self));
+  g_assert (GTK_IS_BUILDER (builder));
+  g_assert (childname != NULL);
+
+  if (g_strcmp0 (childname, "entry") == 0)
+    return G_OBJECT (priv->entry);
+  else if (g_strcmp0 (childname, "revealer") == 0)
+    return G_OBJECT (priv->revealer);
+
+  return NULL;
+}
+
+static void
+egg_search_bar_finalize (GObject *object)
+{
+  EggSearchBar *self = (EggSearchBar *)object;
+  EggSearchBarPrivate *priv = egg_search_bar_get_instance_private (self);
+
+  G_OBJECT_CLASS (egg_search_bar_parent_class)->finalize (object);
+}
+
+static void
+egg_search_bar_get_property (GObject    *object,
+                             guint       prop_id,
+                             GValue     *value,
+                             GParamSpec *pspec)
+{
+  EggSearchBar *self = EGG_SEARCH_BAR (object);
+
+  switch (prop_id)
+    {
+    case PROP_SEARCH_MODE_ENABLED:
+      g_value_set_boolean (value, egg_search_bar_get_search_mode_enabled (self));
+      break;
+
+    case PROP_SHOW_CLOSE_BUTTON:
+      g_value_set_boolean (value, egg_search_bar_get_show_close_button (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+egg_search_bar_set_property (GObject      *object,
+                             guint         prop_id,
+                             const GValue *value,
+                             GParamSpec   *pspec)
+{
+  EggSearchBar *self = EGG_SEARCH_BAR (object);
+
+  switch (prop_id)
+    {
+    case PROP_SEARCH_MODE_ENABLED:
+      egg_search_bar_set_search_mode_enabled (self, g_value_get_boolean (value));
+      break;
+
+    case PROP_SHOW_CLOSE_BUTTON:
+      egg_search_bar_set_show_close_button (self, g_value_get_boolean (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+egg_search_bar_class_init (EggSearchBarClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  object_class->finalize = egg_search_bar_finalize;
+  object_class->get_property = egg_search_bar_get_property;
+  object_class->set_property = egg_search_bar_set_property;
+
+  widget_class->hierarchy_changed = egg_search_bar_hierarchy_changed;
+
+  gParamSpecs [PROP_SEARCH_MODE_ENABLED] =
+    g_param_spec_boolean ("search-mode-enabled",
+                          _("Search Mode Enabled"),
+                          _("Search Mode Enabled"),
+                          FALSE,
+                          (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  gParamSpecs [PROP_SHOW_CLOSE_BUTTON] =
+    g_param_spec_boolean ("show-close-button",
+                          _("Show Close Button"),
+                          _("Show Close Button"),
+                          FALSE,
+                          (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, LAST_PROP, gParamSpecs);
+
+  gSignals [ACTIVATE] =
+    g_signal_new ("activate",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+                  0, NULL, NULL, NULL, G_TYPE_NONE, 0);
+}
+
+static void
+egg_search_bar_init_buildable (GtkBuildableIface *iface)
+{
+  iface->get_internal_child = egg_search_bar_get_internal_child;
+}
+
+static void
+egg_search_bar_init (EggSearchBar *self)
+{
+  EggSearchBarPrivate *priv = egg_search_bar_get_instance_private (self);
+  GtkStyleContext *style_context;
+  GtkBox *vbox;
+  GtkSeparator *sep;
+
+  priv->revealer =
+    g_object_new (GTK_TYPE_REVEALER,
+                  "transition-type", GTK_REVEALER_TRANSITION_TYPE_SLIDE_DOWN,
+                  "visible", TRUE,
+                  NULL);
+  vbox =
+    g_object_new (GTK_TYPE_BOX,
+                  "orientation", GTK_ORIENTATION_VERTICAL,
+                  "visible", TRUE,
+                  NULL);
+  priv->box =
+    g_object_new (GTK_TYPE_BOX,
+                  "margin", 6,
+                  "visible", TRUE,
+                  NULL);
+  priv->entry =
+    g_object_connect (g_object_new (GTK_TYPE_ENTRY,
+                                    "placeholder-text", _("Search"),
+                                    "visible", TRUE,
+                                    NULL),
+                      "swapped_object_signal::activate", egg_search_bar__entry_activate, self,
+                      NULL);
+  sep =
+    g_object_new (GTK_TYPE_SEPARATOR,
+                  "orientation", GTK_ORIENTATION_HORIZONTAL,
+                  "visible", TRUE,
+                  NULL);
+  priv->close_button =
+    g_object_new (GTK_TYPE_BUTTON,
+                  "child", g_object_new (GTK_TYPE_IMAGE,
+                                         "icon-name", "window-close-symbolic",
+                                         "visible", TRUE,
+                                         NULL),
+                  "visible", FALSE,
+                  NULL);
+
+  style_context = gtk_widget_get_style_context (GTK_WIDGET (vbox));
+  gtk_style_context_add_class (style_context, "notebook");
+  gtk_style_context_add_class (style_context, "header");
+
+  gtk_container_add (GTK_CONTAINER (priv->revealer), GTK_WIDGET (vbox));
+  gtk_container_add (GTK_CONTAINER (vbox), GTK_WIDGET (priv->box));
+  gtk_container_add (GTK_CONTAINER (vbox), GTK_WIDGET (sep));
+  gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (priv->revealer));
+  gtk_container_add_with_properties (GTK_CONTAINER (priv->box),
+                                     GTK_WIDGET (priv->close_button),
+                                     "fill", FALSE,
+                                     "pack-type", GTK_PACK_END,
+                                     NULL);
+  gtk_box_set_center_widget (priv->box, GTK_WIDGET (priv->entry));
+}
+
+GtkWidget *
+egg_search_bar_new (void)
+{
+  return g_object_new (EGG_TYPE_SEARCH_BAR, NULL);
+}
+
+gboolean
+egg_search_bar_get_search_mode_enabled (EggSearchBar *self)
+{
+  EggSearchBarPrivate *priv = egg_search_bar_get_instance_private (self);
+
+  g_return_val_if_fail (EGG_IS_SEARCH_BAR (self), FALSE);
+
+  return priv->search_mode_enabled;
+}
+
+void
+egg_search_bar_set_search_mode_enabled (EggSearchBar *self,
+                                        gboolean      search_mode_enabled)
+{
+  EggSearchBarPrivate *priv = egg_search_bar_get_instance_private (self);
+
+  g_return_if_fail (EGG_IS_SEARCH_BAR (self));
+
+  search_mode_enabled = !!search_mode_enabled;
+
+  if (search_mode_enabled != priv->search_mode_enabled)
+    {
+      priv->search_mode_enabled = search_mode_enabled;
+      gtk_revealer_set_reveal_child (priv->revealer, search_mode_enabled);
+      gtk_entry_set_text (priv->entry, "");
+      g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_SEARCH_MODE_ENABLED]);
+    }
+}
+
+gboolean
+egg_search_bar_get_show_close_button (EggSearchBar *self)
+{
+  EggSearchBarPrivate *priv = egg_search_bar_get_instance_private (self);
+
+  g_return_val_if_fail (EGG_IS_SEARCH_BAR (self), FALSE);
+
+  return gtk_widget_get_visible (GTK_WIDGET (priv->close_button));
+}
+
+void
+egg_search_bar_set_show_close_button (EggSearchBar *self,
+                                      gboolean      show_close_button)
+{
+  EggSearchBarPrivate *priv = egg_search_bar_get_instance_private (self);
+
+  g_return_if_fail (EGG_IS_SEARCH_BAR (self));
+
+  gtk_widget_set_visible (GTK_WIDGET (priv->close_button), show_close_button);
+  g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_SHOW_CLOSE_BUTTON]);
+}
diff --git a/contrib/egg/egg-search-bar.h b/contrib/egg/egg-search-bar.h
new file mode 100644
index 0000000..4f39bed
--- /dev/null
+++ b/contrib/egg/egg-search-bar.h
@@ -0,0 +1,45 @@
+/* egg-search-bar.h
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * This file 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 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This file 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 General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef EGG_SEARCH_BAR_H
+#define EGG_SEARCH_BAR_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define EGG_TYPE_SEARCH_BAR (egg_search_bar_get_type())
+
+G_DECLARE_DERIVABLE_TYPE (EggSearchBar, egg_search_bar, EGG, SEARCH_BAR, GtkBin)
+
+struct _EggSearchBarClass
+{
+  GtkBinClass parent_class;
+};
+
+GtkWidget *egg_search_bar_new                     (void);
+gboolean   egg_search_bar_get_search_mode_enabled (EggSearchBar *self);
+void       egg_search_bar_set_search_mode_enabled (EggSearchBar *self,
+                                                   gboolean      search_mode_enabled);
+gboolean   egg_search_bar_get_show_close_button   (EggSearchBar *self);
+void       egg_search_bar_set_show_close_button   (EggSearchBar *self,
+                                                   gboolean      show_close_button);
+
+G_END_DECLS
+
+#endif /* EGG_SEARCH_BAR_H */


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