[libgd/wip/rishi/main-box: 6/9] Add GdMainIconBox



commit 89b19402b836cf236a232b098b683133796fce77
Author: Debarshi Ray <debarshir gnome org>
Date:   Wed Nov 23 16:46:50 2016 +0100

    Add GdMainIconBox
    
    https://bugzilla.gnome.org/show_bug.cgi?id=774914

 libgd/gd-main-icon-box.c |  417 ++++++++++++++++++++++++++++++++++++++++++++++
 libgd/gd-main-icon-box.h |   41 +++++
 2 files changed, 458 insertions(+), 0 deletions(-)
---
diff --git a/libgd/gd-main-icon-box.c b/libgd/gd-main-icon-box.c
new file mode 100644
index 0000000..386ffaf
--- /dev/null
+++ b/libgd/gd-main-icon-box.c
@@ -0,0 +1,417 @@
+/*
+ * Copyright (c) 2016 Red Hat, Inc.
+ *
+ * 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: Debarshi Ray <debarshir gnome org>
+ *
+ */
+
+#include <gio/gio.h>
+
+#include "gd-main-icon-box.h"
+#include "gd-main-icon-box-child.h"
+#include "gd-main-box-child.h"
+#include "gd-main-box-generic.h"
+
+#define BOX_COLUMN_SPACING 20
+#define BOX_MARGIN 16
+
+typedef struct _GdMainIconBoxPrivate GdMainIconBoxPrivate;
+
+struct _GdMainIconBoxPrivate
+{
+  GListModel *model;
+  GdMainIconBoxChild *child_button_released;
+  GtkWidget *flow_box;
+  gboolean selection_mode;
+};
+
+enum
+{
+  PROP_MODEL = 1,
+  PROP_SELECTION_MODE,
+  NUM_PROPERTIES
+};
+
+static void gd_main_box_generic_interface_init (GdMainBoxGenericInterface *iface);
+G_DEFINE_TYPE_WITH_CODE (GdMainIconBox, gd_main_icon_box, GTK_TYPE_BIN,
+                         G_ADD_PRIVATE (GdMainIconBox)
+                         G_IMPLEMENT_INTERFACE (GD_TYPE_MAIN_BOX_GENERIC, 
gd_main_box_generic_interface_init))
+
+static void
+gd_main_icon_box_child_activated_cb (GdMainIconBox *self, GtkFlowBoxChild *child)
+{
+  g_return_if_fail (GD_IS_MAIN_BOX_CHILD (child));
+  g_signal_emit_by_name (self, "child-activated", GD_MAIN_BOX_CHILD (child));
+}
+
+static void
+gd_main_icon_box_child_button_released_cb (GdMainIconBoxChild *child, gpointer user_data)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (user_data);
+  GdMainIconBoxPrivate *priv;
+
+  /* Workaround for lack of gtk_flow_box_get_child_at_pos. */
+  g_message ("gd_main_icon_box_child_button_released_cb");
+  priv = gd_main_icon_box_get_instance_private (self);
+  priv->child_button_released = child;
+}
+
+GtkWidget *
+gd_main_icon_box_create_widget_func (gpointer item, gpointer user_data)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (user_data);
+  GdMainIconBoxPrivate *priv;
+  GtkWidget *child;
+
+  g_return_val_if_fail (GD_IS_MAIN_BOX_ITEM (item), NULL);
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  child = gd_main_icon_box_child_new (GD_MAIN_BOX_ITEM (item), priv->selection_mode);
+  gtk_widget_show_all (child);
+
+  g_signal_connect (child, "button-released", G_CALLBACK (gd_main_icon_box_child_button_released_cb), self);
+
+  return child;
+}
+
+static void
+gd_main_icon_box_selected_children_changed_cb (GdMainIconBox *self)
+{
+  GdMainIconBoxPrivate *priv;
+  gint i;
+  gint n_items;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  /* Work around https://bugzilla.gnome.org/show_bug.cgi?id=775525. */
+  n_items = (gint) g_list_model_get_n_items (G_LIST_MODEL (priv->model));
+  for (i = 0; i < n_items; i++)
+    {
+      GtkFlowBoxChild *child;
+      gboolean selected;
+
+      /* Work around the fact that GtkFlowBoxChild:selected is not a
+       * property.
+       */
+      child = gtk_flow_box_get_child_at_index (GTK_FLOW_BOX (priv->flow_box), i);
+      selected = gtk_flow_box_child_is_selected (child);
+      gd_main_box_child_set_selected (GD_MAIN_BOX_CHILD (child), selected);
+    }
+
+  g_signal_emit_by_name (self, "selected-children-changed");
+}
+
+static GdMainBoxChild *
+gd_main_icon_box_get_child_at_index (GdMainBoxGeneric *generic, gint index)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (generic);
+  GdMainIconBoxPrivate *priv;
+  GtkFlowBoxChild *child;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  child = gtk_flow_box_get_child_at_index (GTK_FLOW_BOX (priv->flow_box), index);
+  return GD_MAIN_BOX_CHILD (child);
+}
+
+static GdMainBoxChild *
+gd_main_icon_box_get_child_for_event (GdMainBoxGeneric *generic, GdkEvent *event)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (generic);
+  GdMainIconBoxPrivate *priv;
+  GdMainBoxChild *child = NULL;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  switch (event->type)
+    {
+    case GDK_BUTTON_RELEASE:
+      child = GD_MAIN_BOX_CHILD (priv->child_button_released);
+      break;
+
+    default:
+      break;
+    }
+
+  return child;
+}
+
+static GListModel *
+gd_main_icon_box_get_model (GdMainIconBox *self)
+{
+  GdMainIconBoxPrivate *priv;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+  return priv->model;
+}
+
+static GList *
+gd_main_icon_box_get_selected_children (GdMainBoxGeneric *generic)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (generic);
+  GdMainIconBoxPrivate *priv;
+  GList *selected_children;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  selected_children = gtk_flow_box_get_selected_children (GTK_FLOW_BOX (priv->flow_box));
+  return selected_children;
+}
+
+static gboolean
+gd_main_icon_box_get_selection_mode (GdMainIconBox *self)
+{
+  GdMainIconBoxPrivate *priv;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+  return priv->selection_mode;
+}
+
+static void
+gd_main_icon_box_select_all (GdMainBoxGeneric *generic)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (generic);
+  GdMainIconBoxPrivate *priv;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+  gtk_flow_box_select_all (GTK_FLOW_BOX (priv->flow_box));
+}
+
+static void
+gd_main_icon_box_select_child (GdMainBoxGeneric *generic, GdMainBoxChild *child)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (generic);
+  GdMainIconBoxPrivate *priv;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+  gtk_flow_box_select_child (GTK_FLOW_BOX (priv->flow_box), GTK_FLOW_BOX_CHILD (child));
+}
+
+static void
+gd_main_icon_box_set_model (GdMainIconBox *self, GListModel *model)
+{
+  GdMainIconBoxPrivate *priv;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  if (!g_set_object (&priv->model, model))
+    return;
+
+  gtk_flow_box_bind_model (GTK_FLOW_BOX (priv->flow_box),
+                           priv->model,
+                           gd_main_icon_box_create_widget_func,
+                           self,
+                           NULL);
+
+  g_object_notify (G_OBJECT (self), "model");
+}
+
+static void
+gd_main_icon_box_set_selection_mode (GdMainIconBox *self, gboolean selection_mode)
+{
+  GdMainIconBoxPrivate *priv;
+  gint i;
+  gint n_items;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  if (priv->selection_mode == selection_mode)
+    return;
+
+  priv->selection_mode = selection_mode;
+  if (priv->selection_mode)
+    gtk_flow_box_set_selection_mode (GTK_FLOW_BOX (priv->flow_box), GTK_SELECTION_MULTIPLE);
+  else
+    gtk_flow_box_set_selection_mode (GTK_FLOW_BOX (priv->flow_box), GTK_SELECTION_NONE);
+
+  /* Work around https://bugzilla.gnome.org/show_bug.cgi?id=775525. */
+  n_items = (gint) g_list_model_get_n_items (G_LIST_MODEL (priv->model));
+  for (i = 0; i < n_items; i++)
+    {
+      GtkFlowBoxChild *child;
+
+      child = gtk_flow_box_get_child_at_index (GTK_FLOW_BOX (priv->flow_box), i);
+      gd_main_box_child_set_selection_mode (GD_MAIN_BOX_CHILD (child), priv->selection_mode);
+    }
+
+  g_object_notify (G_OBJECT (self), "selection-mode");
+}
+
+static void
+gd_main_icon_box_unselect_all (GdMainBoxGeneric *generic)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (generic);
+  GdMainIconBoxPrivate *priv;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+  gtk_flow_box_unselect_all (GTK_FLOW_BOX (priv->flow_box));
+}
+
+static void
+gd_main_icon_box_unselect_child (GdMainBoxGeneric *generic, GdMainBoxChild *child)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (generic);
+  GdMainIconBoxPrivate *priv;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+  gtk_flow_box_unselect_child (GTK_FLOW_BOX (priv->flow_box), GTK_FLOW_BOX_CHILD (child));
+}
+
+static void
+gd_main_icon_box_constructed (GObject *obj)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (obj);
+  GdMainIconBoxPrivate *priv;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  G_OBJECT_CLASS (gd_main_icon_box_parent_class)->constructed (obj);
+
+  gtk_widget_set_hexpand (priv->flow_box, TRUE);
+  gtk_widget_set_vexpand (priv->flow_box, TRUE);
+  gtk_widget_set_margin_bottom (priv->flow_box, BOX_MARGIN);
+  gtk_widget_set_margin_end (priv->flow_box, BOX_MARGIN);
+  gtk_widget_set_margin_start (priv->flow_box, BOX_MARGIN);
+  gtk_widget_set_margin_top (priv->flow_box, BOX_MARGIN);
+  gtk_flow_box_set_column_spacing (GTK_FLOW_BOX (priv->flow_box), BOX_COLUMN_SPACING);
+  gtk_flow_box_set_selection_mode (GTK_FLOW_BOX (priv->flow_box), GTK_SELECTION_NONE);
+}
+
+static void
+gd_main_icon_box_dispose (GObject *obj)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (obj);
+  GdMainIconBoxPrivate *priv;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  g_clear_object (&priv->model);
+
+  G_OBJECT_CLASS (gd_main_icon_box_parent_class)->dispose (obj);
+}
+
+static void
+gd_main_icon_box_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (object);
+
+  switch (property_id)
+    {
+    case PROP_MODEL:
+      g_value_set_object (value, gd_main_icon_box_get_model (self));
+      break;
+    case PROP_SELECTION_MODE:
+      g_value_set_boolean (value, gd_main_icon_box_get_selection_mode (self));
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+static void
+gd_main_icon_box_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (object);
+
+  switch (property_id)
+    {
+    case PROP_MODEL:
+      gd_main_icon_box_set_model (self, g_value_get_object (value));
+      break;
+    case PROP_SELECTION_MODE:
+      gd_main_icon_box_set_selection_mode (self, g_value_get_boolean (value));
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+static void
+gd_main_icon_box_init (GdMainIconBox *self)
+{
+  GdMainIconBoxPrivate *priv;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  priv->flow_box = gtk_flow_box_new ();
+  gtk_flow_box_set_homogeneous (GTK_FLOW_BOX (priv->flow_box), TRUE);
+  gtk_flow_box_set_min_children_per_line (GTK_FLOW_BOX (priv->flow_box), 3);
+  gtk_container_add (GTK_CONTAINER (self), priv->flow_box);
+
+  g_signal_connect_swapped (priv->flow_box,
+                            "child-activated",
+                            G_CALLBACK (gd_main_icon_box_child_activated_cb),
+                            self);
+  g_signal_connect_swapped (priv->flow_box,
+                            "selected-children-changed",
+                            G_CALLBACK (gd_main_icon_box_selected_children_changed_cb),
+                            self);
+}
+
+static void
+gd_main_icon_box_class_init (GdMainIconBoxClass *klass)
+{
+  GObjectClass *oclass = G_OBJECT_CLASS (klass);
+  GtkBindingSet *binding_set;
+  GdkModifierType activate_modifiers[] = { GDK_SHIFT_MASK, GDK_CONTROL_MASK, GDK_SHIFT_MASK | 
GDK_CONTROL_MASK };
+  guint i;
+
+  binding_set = gtk_binding_set_by_class (klass);
+
+  oclass->constructed = gd_main_icon_box_constructed;
+  oclass->dispose = gd_main_icon_box_dispose;
+  oclass->get_property = gd_main_icon_box_get_property;
+  oclass->set_property = gd_main_icon_box_set_property;
+
+  g_object_class_override_property (oclass, PROP_MODEL, "model");
+  g_object_class_override_property (oclass, PROP_SELECTION_MODE, "selection-mode");
+
+  for (i = 0; i < G_N_ELEMENTS (activate_modifiers); i++)
+    {
+      gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, activate_modifiers[i],
+                                   "activate-cursor-child", 0);
+      gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Space, activate_modifiers[i],
+                                   "activate-cursor-child", 0);
+      gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, activate_modifiers[i],
+                                   "activate-cursor-child", 0);
+      gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, activate_modifiers[i],
+                                   "activate-cursor-child", 0);
+      gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, activate_modifiers[i],
+                                   "activate-cursor-child", 0);
+    }
+}
+
+static void
+gd_main_box_generic_interface_init (GdMainBoxGenericInterface *iface)
+{
+  iface->get_child_at_index = gd_main_icon_box_get_child_at_index;
+  iface->get_child_for_event = gd_main_icon_box_get_child_for_event;
+  iface->get_selected_children = gd_main_icon_box_get_selected_children;
+  iface->select_all = gd_main_icon_box_select_all;
+  iface->select_child = gd_main_icon_box_select_child;
+  iface->unselect_all = gd_main_icon_box_unselect_all;
+  iface->unselect_child = gd_main_icon_box_unselect_child;
+}
+
+GtkWidget *
+gd_main_icon_box_new (void)
+{
+  return g_object_new (GD_TYPE_MAIN_ICON_BOX, NULL);
+}
diff --git a/libgd/gd-main-icon-box.h b/libgd/gd-main-icon-box.h
new file mode 100644
index 0000000..fbf5e37
--- /dev/null
+++ b/libgd/gd-main-icon-box.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2016 Red Hat, Inc.
+ *
+ * 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: Debarshi Ray <debarshir gnome org>
+ *
+ */
+
+#ifndef __GD_MAIN_ICON_BOX_H__
+#define __GD_MAIN_ICON_BOX_H__
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GD_TYPE_MAIN_ICON_BOX gd_main_icon_box_get_type()
+G_DECLARE_DERIVABLE_TYPE (GdMainIconBox, gd_main_icon_box, GD, MAIN_ICON_BOX, GtkBin)
+
+struct _GdMainIconBoxClass
+{
+  GtkBinClass parent_class;
+};
+
+GtkWidget * gd_main_icon_box_new (void);
+
+G_END_DECLS
+
+#endif /* __GD_MAIN_ICON_BOX_H__ */


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