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



commit 434fb748b367c8039fe44f77ca1a5ee7e3132098
Author: Sebastian Geiger <sbastig gmx net>
Date:   Sun Jan 17 23:57:32 2016 +0100

    input-sources: implement candidate popup

 .../libinput-sources/gf-candidate-popup.c          |  240 ++++++++++++++++++--
 1 files changed, 224 insertions(+), 16 deletions(-)
---
diff --git a/gnome-flashback/libinput-sources/gf-candidate-popup.c 
b/gnome-flashback/libinput-sources/gf-candidate-popup.c
index a0c1a5c..538417c 100644
--- a/gnome-flashback/libinput-sources/gf-candidate-popup.c
+++ b/gnome-flashback/libinput-sources/gf-candidate-popup.c
@@ -17,6 +17,9 @@
 
 #include "config.h"
 
+#include <math.h>
+
+#include "gf-candidate-area.h"
 #include "gf-candidate-popup.h"
 
 struct _GfCandidatePopup
@@ -24,18 +27,92 @@ struct _GfCandidatePopup
   GfPopupWindow     parent;
 
   IBusPanelService *service;
+
+  GtkWidget        *box_layout;
+
+  GtkWidget        *pre_edit_text;
+  GtkWidget        *aux_text;
+  GtkWidget        *candidate_area;
+
+  gint              cursor_x_pos;
+  gint              cursor_y_pos;
+  gint              cursor_height;
+  gint              cursor_width;
 };
 
 G_DEFINE_TYPE (GfCandidatePopup, gf_candidate_popup, GF_TYPE_POPUP_WINDOW)
 
 static void
+set_text_attributes (GfCandidatePopup *popup, IBusAttrList *attributes)
+{
+  IBusAttribute *attribute;
+  guint i, start, end;
+
+  for (i = 0; (attribute = ibus_attr_list_get (attributes, i)) != NULL; i++)
+    {
+      if (ibus_attribute_get_type () != IBUS_ATTR_TYPE_BACKGROUND)
+        continue;
+
+      start = ibus_attribute_get_start_index (attribute);
+      end = ibus_attribute_get_end_index (attribute);
+      gtk_label_select_region (GTK_LABEL (popup->pre_edit_text),
+                               start, end);
+    }
+}
+
+static void
 set_cursor_location_cb (IBusPanelService *service,
                         gint              x,
                         gint              y,
                         gint              w,
                         gint              h,
-                        gpointer          user_data)
+                        GfCandidatePopup *popup)
 {
+  popup->cursor_x_pos = x;
+  popup->cursor_y_pos = y;
+  popup->cursor_height = h;
+  popup->cursor_width = w;
+}
+
+static void
+update_window_location (GfCandidatePopup *popup)
+{
+  gint popup_height, popup_width;
+  GdkScreen *screen;
+  GdkWindow *window;
+  GdkRectangle monitor_geometry;
+  GtkRequisition preferred_window_size;
+  gint monitor;
+  gint x, y;
+
+  gtk_window_get_size (GTK_WINDOW (popup), &popup_width, &popup_height);
+
+  window = gtk_widget_get_window (GTK_WIDGET (popup));
+
+  if (window == NULL)
+    return;
+
+  screen = gdk_window_get_screen (window);
+  monitor = gdk_screen_get_monitor_at_window (screen, window);
+  gdk_screen_get_monitor_geometry (screen, monitor, &monitor_geometry);
+
+  x = popup->cursor_x_pos;
+  y = popup->cursor_y_pos;
+
+  if (x + popup_width > monitor_geometry.width)
+    x = x - popup_width + popup->cursor_width;
+
+  if (y + popup_height > monitor_geometry.height)
+    y = y - popup_height;
+  else
+    y = y + popup->cursor_height;
+
+  gtk_window_move (GTK_WINDOW (popup), x, y);
+
+  gtk_widget_get_preferred_size (GTK_WIDGET (popup), NULL,
+                                 &preferred_window_size);
+  gtk_window_resize (GTK_WINDOW (popup),
+                     preferred_window_size.width, preferred_window_size.height);
 }
 
 static void
@@ -43,67 +120,170 @@ update_preedit_text_cb (IBusPanelService *service,
                         IBusText         *text,
                         guint             cursor_pos,
                         gboolean          visible,
-                        gpointer          user_data)
+                        GfCandidatePopup *popup)
 {
+  IBusAttrList *attributes;
+
+  gtk_widget_set_visible (popup->pre_edit_text, visible);
+
+  gtk_label_set_text (GTK_LABEL (popup->pre_edit_text),
+                      ibus_text_get_text (text));
+
+  attributes = ibus_text_get_attributes (text);
+
+  if (attributes)
+    set_text_attributes (popup, attributes);
 }
 
 static void
 show_preedit_text_cb (IBusPanelService *service,
-                      gpointer          user_data)
+                      GfCandidatePopup *popup)
 {
+  gtk_widget_show (popup->pre_edit_text);
 }
 
 static void
 hide_preedit_text_cb (IBusPanelService *service,
-                      gpointer          user_data)
+                      GfCandidatePopup *popup)
 {
+  gtk_widget_hide (popup->pre_edit_text);
 }
 
 static void
 update_auxiliary_text_cb (IBusPanelService *service,
                           IBusText         *text,
                           gboolean          visible,
-                          gpointer          user_data)
+                          GfCandidatePopup *popup)
 {
+  gtk_widget_set_visible (popup->aux_text, visible);
+
+  gtk_label_set_text (GTK_LABEL (popup->aux_text), ibus_text_get_text (text));
 }
 
 static void
 show_auxiliary_text_cb (IBusPanelService *service,
-                        gpointer          user_data)
+                        GfCandidatePopup *popup)
 {
+  gtk_widget_show (popup->aux_text);
 }
 
 static void
 hide_auxiliary_text_cb (IBusPanelService *service,
-                        gpointer          user_data)
+                        GfCandidatePopup *popup)
 {
+  gtk_widget_hide (popup->aux_text);
 }
 
 static void
 update_lookup_table_cb (IBusPanelService *service,
                         IBusLookupTable  *lookup_table,
                         gboolean          visible,
-                        gpointer          user_data)
+                        GfCandidatePopup *popup)
 {
+  guint n_candidates,
+        cursor_position,
+        page_size, n_pages, page,
+        start_index, end_index,
+        i;
+  GSList *indexes, *candidates;
+  IBusText *index_label;
+  gint orientation;
+
+  indexes = NULL;
+  candidates = NULL;
+
+  gtk_widget_set_visible (GTK_WIDGET (popup), visible);
+  update_window_location (popup);
+  n_candidates = ibus_lookup_table_get_number_of_candidates (lookup_table);
+  cursor_position = ibus_lookup_table_get_cursor_pos (lookup_table);
+  page_size = ibus_lookup_table_get_page_size (lookup_table);
+  n_pages = (guint) ceil (n_candidates / ((gdouble) page_size));
+  page = ((cursor_position == 0) ? 0 :
+          (guint) floor (cursor_position / ((gdouble) page_size)));
+  start_index = page * page_size;
+  end_index = MIN ((page + 1) * page_size, n_candidates);
+
+  for (i = 0; (index_label = ibus_lookup_table_get_label (lookup_table, i)) != NULL; i++)
+    {
+      const gchar *text;
+
+      text = ibus_text_get_text (index_label);
+      indexes = g_slist_append (indexes, (gpointer) text);
+    }
+
+  for (i = start_index; i < end_index; i++)
+    {
+      IBusText *ibus_text;
+      const gchar* text;
+
+      ibus_text = ibus_lookup_table_get_candidate (lookup_table, i);
+      text = ibus_text_get_text (ibus_text);
+      candidates = g_slist_append (candidates, (gpointer) text);
+    }
+
+  gf_candidate_area_set_candidates (GF_CANDIDATE_AREA (popup->candidate_area),
+                                    indexes, candidates, cursor_position,
+                                    visible);
+
+  orientation = ibus_lookup_table_get_orientation (lookup_table);
+
+  gf_candidate_area_set_orientation (GF_CANDIDATE_AREA (popup->candidate_area),
+                                     (IBusOrientation) orientation);
+
+  gf_candidate_area_update_buttons (GF_CANDIDATE_AREA (popup->candidate_area),
+                                    ibus_lookup_table_is_round (lookup_table),
+                                    page, n_pages);
+
+  g_slist_free (indexes);
+  g_slist_free (candidates);
 }
 
 static void
 show_lookup_table_cb (IBusPanelService *service,
-                      gpointer          user_data)
+                      GfCandidatePopup *popup)
 {
+  gtk_widget_show (GTK_WIDGET (popup));
+  update_window_location (popup);
 }
 
 static void
 hide_lookup_table_cb (IBusPanelService *service,
-                      gpointer          user_data)
+                      GfCandidatePopup *popup)
+{
+  gtk_widget_hide (GTK_WIDGET (popup));
+  update_window_location (popup);
+}
+
+static void
+area_page_prev_cb (GfCandidateArea  *area,
+                   GfCandidatePopup *popup)
 {
+  ibus_panel_service_page_up (popup->service);
 }
 
 static void
-focus_out_cb (IBusPanelService *service,
-              const gchar      *input_context_path,
-              gpointer          user_data)
+area_page_next_cb (GfCandidateArea  *area,
+                   GfCandidatePopup *popup)
 {
+  ibus_panel_service_page_down (popup->service);
+}
+
+static void
+area_candidate_clicked_cb (GfCandidateArea  *area,
+                           guint             index,
+                           GdkEvent         *event,
+                           GfCandidatePopup *popup)
+{
+  guint button;
+  GdkModifierType state;
+
+  gdk_event_get_button (event, &button);
+  gdk_event_get_state (event, &state);
+
+  ibus_panel_service_candidate_clicked (popup->service,
+                                        index,
+                                        button,
+                                        state);
 }
 
 static void
@@ -131,12 +311,42 @@ gf_candidate_popup_class_init (GfCandidatePopupClass *popup_class)
 static void
 gf_candidate_popup_init (GfCandidatePopup *popup)
 {
+  gtk_window_set_focus_on_map (GTK_WINDOW (popup), TRUE);
+  gtk_window_set_type_hint (GTK_WINDOW (popup), GDK_WINDOW_TYPE_HINT_NORMAL);
+
+  popup->candidate_area = gf_candidate_area_new();
+
+  gtk_widget_set_size_request (GTK_WIDGET (popup), 1, 1);
+
+  popup->box_layout = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
+  gtk_container_add (GTK_CONTAINER (popup), popup->box_layout);
+
+  popup->pre_edit_text = gtk_label_new (NULL);
+  popup->aux_text = gtk_label_new (NULL);
+
+  gtk_container_add (GTK_CONTAINER (popup->box_layout), popup->pre_edit_text);
+  gtk_container_add (GTK_CONTAINER (popup->box_layout), popup->aux_text);
+
+  gtk_container_add (GTK_CONTAINER (popup->box_layout),
+                     popup->candidate_area);
+
+  gtk_widget_show (popup->box_layout);
+  gtk_widget_show (popup->candidate_area);
+
+  g_signal_connect (popup->candidate_area, "previous-page",
+                    G_CALLBACK (area_page_prev_cb), popup);
+  g_signal_connect (popup->candidate_area, "next-page",
+                    G_CALLBACK (area_page_next_cb), popup);
+  g_signal_connect (popup->candidate_area, "candidate-clicked",
+                    G_CALLBACK (area_candidate_clicked_cb), popup);
 }
 
 GfCandidatePopup *
 gf_candidate_popup_new (void)
 {
-  return g_object_new (GF_TYPE_CANDIDATE_POPUP, NULL);
+  return g_object_new (GF_TYPE_CANDIDATE_POPUP,
+                       "type", GTK_WINDOW_POPUP,
+                       NULL);
 }
 
 void
@@ -170,6 +380,4 @@ gf_candidate_popup_set_panel_service (GfCandidatePopup *popup,
                     G_CALLBACK (show_lookup_table_cb), popup);
   g_signal_connect (service, "hide-lookup-table",
                     G_CALLBACK (hide_lookup_table_cb), popup);
-  g_signal_connect (service, "focus-out",
-                    G_CALLBACK (focus_out_cb), popup);
 }


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