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



commit daecbd324bf4467537711cf10d6401c93cddc5ce
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 |  753 ++++++++++++++++++++++++++++++++++++++++++++++
 libgd/gd-main-icon-box.h |   44 +++
 2 files changed, 797 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..06eeabe
--- /dev/null
+++ b/libgd/gd-main-icon-box.c
@@ -0,0 +1,753 @@
+/*
+ * 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"
+#include "gd-main-box-item.h"
+
+typedef struct _GdMainIconBoxPrivate GdMainIconBoxPrivate;
+
+struct _GdMainIconBoxPrivate
+{
+  GListModel *model;
+  GdMainIconBoxChild *child_button_released;
+  gboolean selection_changed;
+  gboolean key_pressed;
+  gboolean key_shift_pressed;
+  gboolean left_button_released;
+  gboolean left_button_shift_released;
+  gboolean selection_mode;
+  gchar *last_selected_id;
+};
+
+enum
+{
+  PROP_LAST_SELECTED_ID = 1,
+  PROP_MODEL,
+  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_FLOW_BOX,
+                         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_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. */
+  priv = gd_main_icon_box_get_instance_private (self);
+  priv->child_button_released = child;
+}
+
+static void
+gd_main_icon_box_update_last_selected_id (GdMainIconBox *self, GdMainBoxChild *child)
+{
+  GdMainIconBoxPrivate *priv;
+  GdMainBoxItem *item;
+  const gchar *id;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  g_free (priv->last_selected_id);
+
+  item = gd_main_box_child_get_item (child);
+  id = gd_main_box_item_get_id (item);
+  priv->last_selected_id = g_strdup (id);
+
+  g_object_notify (G_OBJECT (self), "last-selected-id");
+}
+
+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 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 (self), index);
+  return GD_MAIN_BOX_CHILD (child);
+}
+
+static const gchar *
+gd_main_icon_box_get_last_selected_id (GdMainBoxGeneric *generic)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (generic);
+  GdMainIconBoxPrivate *priv;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+  return priv->last_selected_id;
+}
+
+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 (self));
+  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_generic (GdMainBoxGeneric *generic)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (generic);
+  g_signal_emit_by_name (self, "select-all");
+}
+
+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 (self), 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 (self),
+                           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;
+
+  g_clear_pointer (&priv->last_selected_id, g_free);
+
+  priv->selection_mode = selection_mode;
+  if (priv->selection_mode)
+    gtk_flow_box_set_selection_mode (GTK_FLOW_BOX (self), GTK_SELECTION_MULTIPLE);
+  else
+    gtk_flow_box_set_selection_mode (GTK_FLOW_BOX (self), 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 (self), i);
+      gd_main_box_child_set_selection_mode (GD_MAIN_BOX_CHILD (child), priv->selection_mode);
+    }
+
+  g_object_notify (G_OBJECT (self), "last-selected-id");
+  g_object_notify (G_OBJECT (self), "selection-mode");
+}
+
+static void
+gd_main_icon_box_unselect_all_generic (GdMainBoxGeneric *generic)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (generic);
+  g_signal_emit_by_name (self, "unselect-all");
+}
+
+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 (self), GTK_FLOW_BOX_CHILD (child));
+}
+
+static void
+gd_main_icon_box_activate_cursor_child (GtkFlowBox *flow_box)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (flow_box);
+  GdMainIconBoxPrivate *priv;
+  GdkEvent *event = NULL;
+  gboolean initiating = FALSE;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  /* Use GtkFlowBox::activate-cursor-child instead of
+   * GtkWidget::key-press-event to catch key presses because it is
+   * easier to filter out non-activation keys.
+   */
+
+  event = gtk_get_current_event ();
+  if (event == NULL)
+    goto out;
+
+  if (event->type != GDK_KEY_PRESS)
+    goto out;
+
+  if (!priv->selection_mode && (event->key.state & GDK_CONTROL_MASK) != 0)
+    {
+      g_signal_emit_by_name (self, "selection-mode-request");
+      initiating = TRUE;
+    }
+
+  if (priv->selection_mode)
+    {
+      if (!initiating && (event->key.state & GDK_SHIFT_MASK) != 0)
+        priv->key_shift_pressed = TRUE;
+
+      priv->key_pressed = TRUE;
+    }
+
+ out:
+  GTK_FLOW_BOX_CLASS (gd_main_icon_box_parent_class)->activate_cursor_child (flow_box);
+  g_clear_pointer (&event, (GDestroyNotify) gdk_event_free);
+}
+
+static gboolean
+gd_main_icon_box_button_release_event (GtkWidget *widget, GdkEventButton *event)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (widget);
+  GdMainIconBoxPrivate *priv;
+  gboolean initiating = FALSE;
+  gboolean res;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  if (event->type != GDK_BUTTON_RELEASE)
+    {
+      res = GDK_EVENT_STOP;
+      goto out;
+    }
+
+  if (!priv->selection_mode &&
+      (event->button == 1 && (event->state & GDK_CONTROL_MASK) != 0 ||
+       event->button == 3))
+    {
+      g_signal_emit_by_name (self, "selection-mode-request");
+      initiating = TRUE;
+    }
+
+  if (priv->selection_mode)
+    {
+      if (event->button == 1)
+        {
+          if (!initiating && (event->state & GDK_SHIFT_MASK) != 0)
+            priv->left_button_shift_released = TRUE;
+
+          priv->left_button_released = TRUE;
+        }
+      else if (event->button == 3)
+        {
+          /* GtkFlowBox completely ignores the right mouse
+           * button. Hence, all the right-click handling is done
+           * here.
+           */
+
+          gd_main_box_generic_toggle_selection_for_child (GD_MAIN_BOX_GENERIC (self),
+                                                          GD_MAIN_BOX_CHILD (priv->child_button_released),
+                                                          (!initiating && (event->state & GDK_SHIFT_MASK) != 
0));
+
+          if (priv->selection_changed)
+            {
+              g_signal_emit_by_name (self, "selection-changed");
+              gd_main_icon_box_update_last_selected_id (self, GD_MAIN_BOX_CHILD 
(priv->child_button_released));
+              priv->selection_changed = FALSE;
+            }
+        }
+    }
+
+  res = GTK_WIDGET_CLASS (gd_main_icon_box_parent_class)->button_release_event (widget, event);
+
+ out:
+  return res;
+}
+
+static void
+gd_main_icon_box_child_activated (GtkFlowBox *flow_box, GtkFlowBoxChild *child)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (flow_box);
+  GdMainIconBoxPrivate *priv;
+  GdkEvent *event = NULL;
+
+  g_return_if_fail (GD_IS_MAIN_BOX_CHILD (child));
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  if (!priv->selection_mode)
+    {
+      g_signal_emit_by_name (self, "item-activated", GD_MAIN_BOX_CHILD (child));
+      goto out;
+    }
+
+  event = gtk_get_current_event ();
+  if (event == NULL)
+    goto out;
+
+  if (priv->left_button_released && !priv->selection_changed)
+    {
+      /* If a selected child is left-clicked, GtkFlowBox will activate
+       * it without unselecting it.
+       */
+      gd_main_box_generic_toggle_selection_for_child (GD_MAIN_BOX_GENERIC (self),
+                                                      GD_MAIN_BOX_CHILD (child),
+                                                      FALSE); /* One cannot unselect a range. */
+      priv->left_button_released = FALSE;
+      g_signal_emit_by_name (self, "selection-changed");
+    }
+  else if (priv->key_pressed && !priv->selection_changed)
+    {
+      /* If a selected child is activated by a keybinding, GtkFlowBox
+       * will not unselect it.
+       */
+      gd_main_box_generic_toggle_selection_for_child (GD_MAIN_BOX_GENERIC (self), GD_MAIN_BOX_CHILD (child), 
FALSE);
+      g_signal_emit_by_name (self, "selection-changed");
+    }
+  else if (priv->left_button_shift_released || priv->key_shift_pressed)
+    {
+      /* GtkFlowBox doesn't do range selection and simply selects a
+       * single child. We handle it by unselecting the child and then
+       * selecting the range.
+       */
+      gd_main_box_generic_toggle_selection_for_child (GD_MAIN_BOX_GENERIC (self), GD_MAIN_BOX_CHILD (child), 
FALSE);
+      priv->left_button_shift_released = FALSE;
+      priv->key_shift_pressed = FALSE;
+      gd_main_box_generic_toggle_selection_for_child (GD_MAIN_BOX_GENERIC (self), GD_MAIN_BOX_CHILD (child), 
TRUE);
+      g_signal_emit_by_name (self, "selection-changed");
+    }
+  else if (priv->selection_changed)
+    {
+      /* This is for non-shift left-clicks and keyboard activation of
+       * unselected children.
+       */
+      g_signal_emit_by_name (self, "selection-changed");
+    }
+
+  g_signal_emit_by_name (self, "item-activated", GD_MAIN_BOX_CHILD (child));
+
+  if (priv->selection_changed)
+    {
+      gd_main_icon_box_update_last_selected_id (self, GD_MAIN_BOX_CHILD (child));
+      priv->selection_changed = FALSE;
+    }
+
+ out:
+  g_clear_pointer (&event, (GDestroyNotify) gdk_event_free);
+}
+
+static gboolean
+gd_main_icon_box_focus (GtkWidget *widget, GtkDirectionType direction)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (widget);
+  GdMainIconBoxPrivate *priv;
+  GdkEvent *event = NULL;
+  GdkEvent *fake_event = NULL;
+  gboolean res;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  if (!priv->selection_mode)
+    {
+      res = GTK_WIDGET_CLASS (gd_main_icon_box_parent_class)->focus (widget, direction);
+      goto out;
+    }
+
+  event = gtk_get_current_event ();
+  if (event->type != GDK_KEY_PRESS && event->type != GDK_KEY_RELEASE)
+    {
+      res = GTK_WIDGET_CLASS (gd_main_icon_box_parent_class)->focus (widget, direction);
+      goto out;
+    }
+
+  if ((event->key.state & GDK_CONTROL_MASK) != 0 && (event->key.state & GDK_SHIFT_MASK) == 0)
+    {
+      res = GTK_WIDGET_CLASS (gd_main_icon_box_parent_class)->focus (widget, direction);
+      goto out;
+    }
+
+  fake_event = gdk_event_copy (event);
+  fake_event->key.state |= GDK_CONTROL_MASK;
+  fake_event->key.state &= ~GDK_SHIFT_MASK;
+
+  gtk_main_do_event (fake_event);
+  res = GDK_EVENT_STOP;
+
+ out:
+  g_clear_pointer (&fake_event, (GDestroyNotify) gdk_event_free);
+  g_clear_pointer (&event, (GDestroyNotify) gdk_event_free);
+  return res;
+}
+
+static gboolean
+gd_main_icon_box_move_cursor (GtkFlowBox *flow_box, GtkMovementStep step, gint count)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (flow_box);
+  GdMainIconBoxPrivate *priv;
+  GdkEvent *event = NULL;
+  GdkEvent *fake_event = NULL;
+  gboolean res;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  if (!priv->selection_mode)
+    {
+      res = GTK_FLOW_BOX_CLASS (gd_main_icon_box_parent_class)->move_cursor (flow_box, step, count);
+      goto out;
+    }
+
+  event = gtk_get_current_event ();
+  if (event->type != GDK_KEY_PRESS && event->type != GDK_KEY_RELEASE)
+    {
+      res = GTK_FLOW_BOX_CLASS (gd_main_icon_box_parent_class)->move_cursor (flow_box, step, count);
+      goto out;
+    }
+
+  if ((event->key.state & GDK_CONTROL_MASK) != 0 && (event->key.state & GDK_SHIFT_MASK) == 0)
+    {
+      res = GTK_FLOW_BOX_CLASS (gd_main_icon_box_parent_class)->move_cursor (flow_box, step, count);
+      goto out;
+    }
+
+  fake_event = gdk_event_copy (event);
+  fake_event->key.state |= GDK_CONTROL_MASK;
+  fake_event->key.state &= ~GDK_SHIFT_MASK;
+
+  gtk_main_do_event (fake_event);
+  res = GDK_EVENT_STOP;
+
+ out:
+  g_clear_pointer (&fake_event, (GDestroyNotify) gdk_event_free);
+  g_clear_pointer (&event, (GDestroyNotify) gdk_event_free);
+  return res;
+}
+
+static void
+gd_main_icon_box_select_all_flow_box (GtkFlowBox *flow_box)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (flow_box);
+  GdMainIconBoxPrivate *priv;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  GTK_FLOW_BOX_CLASS (gd_main_icon_box_parent_class)->select_all (flow_box);
+
+  if (priv->selection_changed)
+    {
+      g_signal_emit_by_name (self, "selection-changed");
+      priv->selection_changed = FALSE;
+    }
+}
+
+static void
+gd_main_icon_box_selected_children_changed (GtkFlowBox *flow_box)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (flow_box);
+  GdMainIconBoxPrivate *priv;
+  gint i;
+  gint n_items;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  /* GtkFlowBox triggers selected-children-changed during dispose if
+   * there was any selected child. This means that we have to
+   * explicitly disconnect the signal handler to prevent it from being
+   * called in an inconsistent state.
+   *
+   * See https://bugzilla.gnome.org/show_bug.cgi?id=776012
+   */
+  if (gtk_widget_in_destruction (GTK_WIDGET (self)))
+    return;
+
+  GTK_FLOW_BOX_CLASS (gd_main_icon_box_parent_class)->selected_children_changed (flow_box);
+
+  priv->selection_changed = TRUE;
+
+  /* When a range selection is attempted, we override GtkFlowBox's
+   * default behaviour by changing the selection ourselves. Therefore,
+   * there is no need to update the check buttons until the final
+   * selection is available.
+   */
+  if (!priv->key_shift_pressed && !priv->left_button_shift_released)
+    {
+      /* 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 (self), i);
+          selected = gtk_flow_box_child_is_selected (child);
+          gd_main_box_child_set_selected (GD_MAIN_BOX_CHILD (child), selected);
+        }
+    }
+}
+
+static void
+gd_main_icon_box_unselect_all_flow_box (GtkFlowBox *flow_box)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (flow_box);
+  GdMainIconBoxPrivate *priv;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  GTK_FLOW_BOX_CLASS (gd_main_icon_box_parent_class)->unselect_all (flow_box);
+
+  if (priv->selection_changed)
+    {
+      g_signal_emit_by_name (self, "selection-changed");
+      priv->selection_changed = FALSE;
+    }
+}
+
+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_flow_box_set_selection_mode (GTK_FLOW_BOX (self), 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_finalize (GObject *obj)
+{
+  GdMainIconBox *self = GD_MAIN_ICON_BOX (obj);
+  GdMainIconBoxPrivate *priv;
+
+  priv = gd_main_icon_box_get_instance_private (self);
+
+  g_free (priv->last_selected_id);
+
+  G_OBJECT_CLASS (gd_main_icon_box_parent_class)->finalize (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_LAST_SELECTED_ID:
+      g_value_set_string (value, gd_main_icon_box_get_last_selected_id (GD_MAIN_BOX_GENERIC (self)));
+      break;
+    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);
+
+  gtk_widget_set_can_focus (GTK_WIDGET (self), TRUE);
+  gtk_flow_box_set_homogeneous (GTK_FLOW_BOX (self), TRUE);
+  gtk_flow_box_set_min_children_per_line (GTK_FLOW_BOX (self), 3);
+}
+
+static void
+gd_main_icon_box_class_init (GdMainIconBoxClass *klass)
+{
+  GObjectClass *oclass = G_OBJECT_CLASS (klass);
+  GtkFlowBoxClass *fbclass = GTK_FLOW_BOX_CLASS (klass);
+  GtkWidgetClass *wclass = GTK_WIDGET_CLASS (klass);
+  GtkBindingSet *binding_set;
+  GdkModifierType activate_modifiers[] = { 0, /* Otherwise it will go to GtkFlowBoxChild::activate. */
+                                           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->finalize = gd_main_icon_box_finalize;
+  oclass->get_property = gd_main_icon_box_get_property;
+  oclass->set_property = gd_main_icon_box_set_property;
+  wclass->button_release_event = gd_main_icon_box_button_release_event;
+  wclass->focus = gd_main_icon_box_focus;
+  fbclass->activate_cursor_child = gd_main_icon_box_activate_cursor_child;
+  fbclass->child_activated = gd_main_icon_box_child_activated;
+  fbclass->move_cursor = gd_main_icon_box_move_cursor;
+  fbclass->select_all = gd_main_icon_box_select_all_flow_box;
+  fbclass->selected_children_changed = gd_main_icon_box_selected_children_changed;
+  fbclass->unselect_all = gd_main_icon_box_unselect_all_flow_box;
+
+  g_object_class_override_property (oclass, PROP_LAST_SELECTED_ID, "last-selected-id");
+  g_object_class_override_property (oclass, PROP_MODEL, "model");
+  g_object_class_override_property (oclass, PROP_SELECTION_MODE, "gd-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_last_selected_id = gd_main_icon_box_get_last_selected_id;
+  iface->get_selected_children = gd_main_icon_box_get_selected_children;
+  iface->select_all = gd_main_icon_box_select_all_generic;
+  iface->select_child = gd_main_icon_box_select_child;
+  iface->unselect_all = gd_main_icon_box_unselect_all_generic;
+  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..5dc60fe
--- /dev/null
+++ b/libgd/gd-main-icon-box.h
@@ -0,0 +1,44 @@
+/*
+ * 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, GtkFlowBox)
+
+struct _GdMainIconBoxClass
+{
+  GtkFlowBoxClass parent_class;
+
+  /* signals */
+  gboolean  (* move_cursor)            (GdMainIconBox *self, GtkMovementStep step, gint count);
+};
+
+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]