[gnome-flashback/wip/segeiger/gnome-3-18-inputmethods: 2/7] input-sources: implement candidate area



commit 3fe080d09927e7b61e9a2c2b3d7b6fca16ad631b
Author: Sebastian Geiger <sbastig gmx net>
Date:   Tue Jan 12 08:24:36 2016 +0100

    input-sources: implement candidate area

 gnome-flashback/libinput-sources/Makefile.am       |    2 +
 .../libinput-sources/gf-candidate-area.c           |  272 ++++++++++++++++++++
 .../libinput-sources/gf-candidate-area.h           |   49 ++++
 3 files changed, 323 insertions(+), 0 deletions(-)
---
diff --git a/gnome-flashback/libinput-sources/Makefile.am b/gnome-flashback/libinput-sources/Makefile.am
index a26eb5a..8b9f181 100644
--- a/gnome-flashback/libinput-sources/Makefile.am
+++ b/gnome-flashback/libinput-sources/Makefile.am
@@ -17,6 +17,8 @@ libinput_sources_la_CFLAGS = \
 libinput_sources_la_SOURCES = \
        gf-candidate-box.c \
        gf-candidate-box.h \
+       gf-candidate-area.c \
+       gf-candidate-area.h \
        gf-candidate-popup.c \
        gf-candidate-popup.h \
        gf-ibus-manager.c \
diff --git a/gnome-flashback/libinput-sources/gf-candidate-area.c 
b/gnome-flashback/libinput-sources/gf-candidate-area.c
new file mode 100644
index 0000000..6cb2da3
--- /dev/null
+++ b/gnome-flashback/libinput-sources/gf-candidate-area.c
@@ -0,0 +1,272 @@
+/*
+ * Copyright (C) 2016 Sebastian Geiger
+ *
+ * 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 "config.h"
+
+#include "gf-candidate-area.h"
+#include "gf-candidate-box.h"
+
+#define MAX_CANDIDATES_PER_PAGE 16
+
+const gchar* DEFAULT_INDEX_LABELS[] =
+  {
+    "1", "2", "3", "4", "5", "6", "7", "8",
+    "9", "0", "a", "b", "c", "d", "e", "f"
+  };
+
+struct _GfCandidateArea
+{
+  GtkBox           parent;
+
+  GtkWidget       *button_box;
+  GtkWidget       *prev_button;
+  GtkWidget       *next_button;
+
+  GSList          *candidate_boxes;
+
+  IBusOrientation  orientation;
+  guint            cursor_position;
+};
+
+enum
+{
+  SIGNAL_CANDIDATE_CLICKED,
+  SIGNAL_PREV_PAGE,
+  SIGNAL_NEXT_PAGE,
+
+  SIGNAL_LAST
+};
+
+static guint signals[SIGNAL_LAST] = { 0 };
+
+G_DEFINE_TYPE (GfCandidateArea, gf_candidate_area, GTK_TYPE_BOX)
+
+static gboolean
+button_clicked_cb (GtkWidget       *widget,
+                   GdkEvent        *event,
+                   GfCandidateArea *area)
+{
+  int index;
+
+  index = gf_candidate_box_get_index (GF_CANDIDATE_BOX (widget));
+
+  g_signal_emit (area,  signals[SIGNAL_CANDIDATE_CLICKED], 0, index, event);
+
+  return GDK_EVENT_PROPAGATE;
+}
+
+static gboolean
+prev_button_clicked_cb (GtkButton       *button,
+                        GfCandidateArea *area)
+{
+  g_signal_emit (area, signals[SIGNAL_PREV_PAGE], 0);
+
+  return GDK_EVENT_PROPAGATE;
+}
+
+static gboolean
+next_button_clicked_cb (GtkButton       *button,
+                        GfCandidateArea *area)
+{
+  g_signal_emit (area, signals[SIGNAL_NEXT_PAGE], 0);
+
+  return GDK_EVENT_PROPAGATE;
+}
+
+void
+gf_candidate_area_set_orientation (GfCandidateArea *area,
+                                   IBusOrientation  orientation)
+{
+  GtkWidget *image;
+
+  if (area->orientation == orientation)
+    return;
+
+  area->orientation = orientation;
+
+  if (area->orientation == IBUS_ORIENTATION_HORIZONTAL)
+    {
+      gtk_orientable_set_orientation (GTK_ORIENTABLE (area->button_box),
+                                      GTK_ORIENTATION_HORIZONTAL);
+
+      image = gtk_button_get_image(GTK_BUTTON (area->prev_button));
+      gtk_image_set_from_icon_name(GTK_IMAGE (image), "go-previous-symbolic",
+                                   GTK_ICON_SIZE_BUTTON);
+
+      image = gtk_button_get_image(GTK_BUTTON (area->next_button));
+      gtk_image_set_from_icon_name(GTK_IMAGE (image), "go-next-symbolic",
+                                   GTK_ICON_SIZE_BUTTON);
+    }
+  else
+    {
+      gtk_orientable_set_orientation (GTK_ORIENTABLE (area->button_box),
+                                      GTK_ORIENTATION_VERTICAL);
+
+      image = gtk_button_get_image(GTK_BUTTON (area->prev_button));
+      gtk_image_set_from_icon_name(GTK_IMAGE (image), "go-up-symbolic",
+                                   GTK_ICON_SIZE_BUTTON);
+
+      image = gtk_button_get_image(GTK_BUTTON (area->next_button));
+      gtk_image_set_from_icon_name(GTK_IMAGE (image), "go-down-symbolic",
+                                   GTK_ICON_SIZE_BUTTON);
+    }
+}
+
+void
+gf_candidate_area_set_candidates (GfCandidateArea *area,
+                                  GSList          *indexes,
+                                  GSList          *candidates,
+                                  guint            cursor_position,
+                                  gboolean         cursor_visible)
+{
+  guint i;
+
+  for (i = 0; i < MAX_CANDIDATES_PER_PAGE; i++)
+    {
+      gboolean visible;
+      GtkWidget *candidate_box;
+      const gchar *index_text;
+
+      visible = i < g_slist_length (candidates);
+      candidate_box = g_slist_nth_data (area->candidate_boxes, i);
+
+      gtk_widget_set_visible (candidate_box, visible);
+
+      if (!visible)
+        continue;
+
+      if (indexes && g_slist_nth_data (indexes, i))
+          index_text = g_slist_nth_data (indexes, i);
+      else
+          index_text = DEFAULT_INDEX_LABELS [i];
+
+      gf_candidate_box_set_index_label (GF_CANDIDATE_BOX (candidate_box),
+                                        index_text);
+
+      gf_candidate_box_set_candidate_label (GF_CANDIDATE_BOX (candidate_box),
+                                            g_slist_nth_data (candidates, i));
+    }
+
+  area->cursor_position = cursor_position;
+}
+
+void
+gf_candidate_area_update_buttons (GfCandidateArea *area,
+                                  gboolean         wraps_around,
+                                  gint             page,
+                                  gint             n_pages)
+{
+  gtk_widget_set_visible (area->button_box, n_pages > 1);
+
+  if (n_pages < 2)
+    return;
+
+  gtk_widget_set_sensitive (area->prev_button, wraps_around || page > 0);
+  gtk_widget_set_sensitive (area->next_button,
+                            wraps_around || page < n_pages - 1);
+}
+
+static void
+gf_candidate_area_finalize (GObject *object)
+{
+  GfCandidateArea *area;
+
+  area = GF_CANDIDATE_AREA (object);
+
+  g_slist_free (area->candidate_boxes);
+
+  G_OBJECT_CLASS (gf_candidate_area_parent_class)->finalize (object);
+}
+
+static void
+gf_candidate_area_class_init (GfCandidateAreaClass *area_class)
+{
+  GObjectClass *object_class;
+
+  object_class = G_OBJECT_CLASS (area_class);
+
+  object_class->finalize = gf_candidate_area_finalize;
+
+  signals[SIGNAL_CANDIDATE_CLICKED] =
+    g_signal_new ("candidate-clicked", G_OBJECT_CLASS_TYPE (area_class),
+                  G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE,
+                  2, G_TYPE_UINT, GDK_TYPE_EVENT);
+
+  signals[SIGNAL_PREV_PAGE] =
+    g_signal_new ("previous-page", G_OBJECT_CLASS_TYPE (area_class),
+                  G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
+
+  signals[SIGNAL_NEXT_PAGE] =
+    g_signal_new ("next-page", G_OBJECT_CLASS_TYPE (area_class),
+                  G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
+}
+
+static void
+gf_candidate_area_init (GfCandidateArea *area)
+{
+  GtkWidget *prev_image, *next_image;
+  guint i;
+
+  for (i = 0; i < MAX_CANDIDATES_PER_PAGE; i++)
+    {
+      GtkWidget *candidate_box;
+
+      candidate_box = gf_candidate_box_new (i);
+      gtk_container_add (GTK_CONTAINER (area), candidate_box);
+
+      area->candidate_boxes = g_slist_append (area->candidate_boxes,
+                                              candidate_box);
+
+      g_signal_connect (candidate_box, "button-release-event",
+                        G_CALLBACK (button_clicked_cb), area);
+    }
+
+  area->button_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
+
+  gtk_container_add (GTK_CONTAINER (area), area->button_box);
+
+  prev_image = gtk_image_new_from_icon_name ("go-previous-symbolic",
+                                             GTK_ICON_SIZE_BUTTON);
+  area->prev_button = gtk_button_new_with_label ("Prev");
+  gtk_button_set_image (GTK_BUTTON (area->prev_button), prev_image);
+
+  next_image = gtk_image_new_from_icon_name ("go-next-symbolic",
+                                             GTK_ICON_SIZE_BUTTON);
+  area->next_button = gtk_button_new_with_label ("Next");
+  gtk_button_set_image (GTK_BUTTON (area->next_button), next_image);
+
+  gtk_container_add (GTK_CONTAINER (area->button_box), area->prev_button);
+  gtk_container_add (GTK_CONTAINER (area->button_box), area->next_button);
+
+  gtk_widget_show_all (GTK_WIDGET (area));
+
+  g_signal_connect (area->prev_button, "clicked",
+                    G_CALLBACK (prev_button_clicked_cb), area);
+  g_signal_connect (area->next_button, "clicked",
+                    G_CALLBACK (next_button_clicked_cb), area);
+
+  area->orientation = IBUS_ORIENTATION_HORIZONTAL;
+  area->cursor_position = 0;
+}
+
+GtkWidget*
+gf_candidate_area_new (void)
+{
+  return g_object_new (GF_TYPE_CANDIDATE_AREA,
+                       "orientation", GTK_ORIENTATION_VERTICAL,
+                       NULL);
+}
diff --git a/gnome-flashback/libinput-sources/gf-candidate-area.h 
b/gnome-flashback/libinput-sources/gf-candidate-area.h
new file mode 100644
index 0000000..9a41e53
--- /dev/null
+++ b/gnome-flashback/libinput-sources/gf-candidate-area.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2016 Sebastian Geiger
+ *
+ * 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 GF_CANDIDATE_AREA_H
+#define GF_CANDIDATE_AREA_H
+
+#include <glib-object.h>
+#include <gtk/gtk.h>
+#include <ibus-1.0/ibus.h>
+
+G_BEGIN_DECLS
+
+#define GF_TYPE_CANDIDATE_AREA gf_candidate_area_get_type ()
+G_DECLARE_FINAL_TYPE (GfCandidateArea, gf_candidate_area,
+                      GF, CANDIDATE_AREA, GtkBox)
+
+GtkWidget* gf_candidate_area_new             (void);
+
+void       gf_candidate_area_set_orientation (GfCandidateArea *area,
+                                              IBusOrientation  orientation);
+
+void       gf_candidate_area_set_candidates  (GfCandidateArea *area,
+                                              GSList          *indexes,
+                                              GSList          *candidates,
+                                              guint            cursor_position,
+                                              gboolean         cursor_visible);
+
+void       gf_candidate_area_update_buttons  (GfCandidateArea *area,
+                                              gboolean         wraps_around,
+                                              gint             page,
+                                              gint             n_pages);
+
+G_END_DECLS
+
+#endif


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