[gtk+/touch-selections: 6/7] entry: Use GtkSelectionWindow for touch text selection



commit 2ae994f7cef9bffa533795304275b0e946dd850f
Author: Carlos Garnacho <carlos lanedo com>
Date:   Fri Jan 11 17:35:32 2013 +0100

    entry: Use GtkSelectionWindow for touch text selection
    
    This enables touch devices to manipulate the text selection

 gtk/gtkentry.c |  147 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 142 insertions(+), 5 deletions(-)
---
diff --git a/gtk/gtkentry.c b/gtk/gtkentry.c
index 8d315c0..8d97aff 100644
--- a/gtk/gtkentry.c
+++ b/gtk/gtkentry.c
@@ -66,6 +66,7 @@
 #include "gtkwidgetprivate.h"
 #include "gtkstylecontextprivate.h"
 #include "gtktexthandleprivate.h"
+#include "gtkselectionwindow.h"
 
 #include "a11y/gtkentryaccessible.h"
 
@@ -158,7 +159,10 @@ struct _GtkEntryPrivate
 
   gchar        *placeholder_text;
 
+  GtkBubbleWindow *bubble_window;
   GtkTextHandle *text_handle;
+  GtkWidget     *selection_bubble;
+  guint          selection_bubble_timeout_id;
 
   gfloat        xalign;
 
@@ -592,6 +596,13 @@ static void         gtk_entry_handle_dragged           (GtkTextHandle         *h
                                                         gint                   x,
                                                         gint                   y,
                                                         GtkEntry              *entry);
+static void         gtk_entry_handle_drag_finished     (GtkTextHandle         *handle,
+                                                        GtkTextHandlePosition  pos,
+                                                        GtkEntry              *entry);
+
+static void         gtk_entry_selection_bubble_popup_set   (GtkEntry *entry,
+                                                            GdkEvent *event);
+static void         gtk_entry_selection_bubble_popup_unset (GtkEntry *entry);
 
 static void         begin_change                       (GtkEntry *entry);
 static void         end_change                         (GtkEntry *entry);
@@ -2575,6 +2586,8 @@ gtk_entry_init (GtkEntry *entry)
   priv->text_handle = _gtk_text_handle_new (GTK_WIDGET (entry));
   g_signal_connect (priv->text_handle, "handle-dragged",
                     G_CALLBACK (gtk_entry_handle_dragged), entry);
+  g_signal_connect (priv->text_handle, "drag-finished",
+                    G_CALLBACK (gtk_entry_handle_drag_finished), entry);
 }
 
 static void
@@ -4013,6 +4026,8 @@ gtk_entry_button_press (GtkWidget      *widget,
   gint sel_start, sel_end;
   gint i;
 
+  gtk_entry_selection_bubble_popup_unset (entry);
+
   for (i = 0; i < MAX_ICONS; i++)
     {
       icon_info = priv->icons[i];
@@ -4217,6 +4232,8 @@ gtk_entry_button_release (GtkWidget      *widget,
   GtkEntry *entry = GTK_ENTRY (widget);
   GtkEntryPrivate *priv = entry->priv;
   EntryIconInfo *icon_info = NULL;
+  gboolean is_touchscreen;
+  GdkDevice *source;
   gint i;
 
   for (i = 0; i < MAX_ICONS; i++)
@@ -4249,21 +4266,24 @@ gtk_entry_button_release (GtkWidget      *widget,
   if (event->window != priv->text_area || priv->button != event->button)
     return FALSE;
 
+  source = gdk_event_get_source_device ((GdkEvent *) event);
+  is_touchscreen = (test_touchscreen ||
+                    gdk_device_get_source (source) == GDK_SOURCE_TOUCHSCREEN);
+
   if (priv->in_drag)
     {
       gint tmp_pos = gtk_entry_find_position (entry, priv->drag_start_x);
-      GdkDevice *source;
 
       gtk_editable_set_position (GTK_EDITABLE (entry), tmp_pos);
 
-      source = gdk_event_get_source_device ((GdkEvent *) event);
-
-      if (test_touchscreen ||
-          gdk_device_get_source (source) == GDK_SOURCE_TOUCHSCREEN)
+      if (is_touchscreen)
         gtk_entry_update_handles (entry, GTK_TEXT_HANDLE_MODE_CURSOR);
 
       priv->in_drag = 0;
     }
+  else if (is_touchscreen &&
+           gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), NULL, NULL))
+    gtk_entry_selection_bubble_popup_set (entry, (GdkEvent *) event);
 
   priv->button = 0;
   priv->device = NULL;
@@ -4488,6 +4508,8 @@ gtk_entry_key_press (GtkWidget   *widget,
   gtk_entry_reset_blink_time (entry);
   gtk_entry_pend_cursor_blink (entry);
 
+  gtk_entry_selection_bubble_popup_unset (entry);
+
   if (!event->send_event)
     _gtk_text_handle_set_mode (priv->text_handle,
                                GTK_TEXT_HANDLE_MODE_NONE);
@@ -4585,6 +4607,7 @@ gtk_entry_focus_out (GtkWidget     *widget,
   GtkEntryCompletion *completion;
   GdkKeymap *keymap;
 
+  gtk_entry_selection_bubble_popup_unset (entry);
   _gtk_text_handle_set_mode (priv->text_handle,
                              GTK_TEXT_HANDLE_MODE_NONE);
 
@@ -5494,6 +5517,8 @@ gtk_entry_cut_clipboard (GtkEntry *entry)
     {
       gtk_widget_error_bell (GTK_WIDGET (entry));
     }
+
+  gtk_entry_selection_bubble_popup_unset (entry);
 }
 
 static void
@@ -6222,6 +6247,8 @@ gtk_entry_handle_dragged (GtkTextHandle         *handle,
   GtkTextHandleMode mode;
   gint *min, *max;
 
+  gtk_entry_selection_bubble_popup_unset (entry);
+
   cursor_pos = priv->current_pos;
   selection_bound_pos = priv->selection_bound;
   mode = _gtk_text_handle_get_mode (handle);
@@ -6274,6 +6301,20 @@ gtk_entry_handle_dragged (GtkTextHandle         *handle,
     }
 }
 
+static void
+gtk_entry_handle_drag_finished (GtkTextHandle         *handle,
+                                GtkTextHandlePosition  pos,
+                                GtkEntry              *entry)
+{
+  GtkEntryPrivate *priv;
+
+  priv = entry->priv;
+
+  if (_gtk_text_handle_get_mode (priv->text_handle) == GTK_TEXT_HANDLE_MODE_SELECTION)
+    gtk_entry_selection_bubble_popup_set (entry, NULL);
+}
+
+
 /**
  * gtk_entry_reset_im_context:
  * @entry: a #GtkEntry
@@ -9214,6 +9255,102 @@ gtk_entry_popup_menu (GtkWidget *widget)
   return TRUE;
 }
 
+static gboolean
+gtk_entry_selection_bubble_popup_cb (gpointer user_data)
+{
+  PopupInfo *info = user_data;
+  GtkEntryPrivate *priv = info->entry->priv;
+  cairo_rectangle_int_t rect;
+  GtkAllocation allocation;
+  gint start_x, end_x;
+
+  if (priv->selection_bubble)
+    gtk_widget_destroy (priv->selection_bubble);
+
+  priv->selection_bubble = gtk_selection_window_new ();
+  g_signal_connect_swapped (priv->selection_bubble, "cut",
+			    G_CALLBACK (gtk_entry_cut_clipboard),
+			    info->entry);
+  g_signal_connect_swapped (priv->selection_bubble, "copy",
+			    G_CALLBACK (gtk_entry_copy_clipboard),
+			    info->entry);
+  g_signal_connect_swapped (priv->selection_bubble, "paste",
+			    G_CALLBACK (gtk_entry_paste_clipboard),
+			    info->entry);
+
+  gtk_widget_get_allocation (GTK_WIDGET (info->entry), &allocation);
+
+  gtk_entry_get_cursor_locations (info->entry, CURSOR_STANDARD, &start_x, NULL);
+  start_x -= priv->scroll_offset;
+  end_x = gtk_entry_get_selection_bound_location (info->entry) - priv->scroll_offset;
+
+  start_x = CLAMP (start_x, 0, gdk_window_get_width (priv->text_area));
+  end_x = CLAMP (end_x, 0, gdk_window_get_width (priv->text_area));
+
+  rect.y = 0;
+  rect.height = gdk_window_get_height (priv->text_area);
+  rect.x = MIN (start_x, end_x);
+  rect.width = MAX (start_x, end_x) - rect.x;
+  gtk_bubble_window_popup (GTK_BUBBLE_WINDOW (priv->selection_bubble),
+                           priv->text_area, &rect, GTK_POS_TOP);
+
+  priv->selection_bubble_timeout_id = 0;
+  return FALSE;
+}
+
+static void
+gtk_entry_selection_bubble_popup_unset (GtkEntry *entry)
+{
+  GtkEntryPrivate *priv;
+
+  priv = entry->priv;
+
+  if (priv->selection_bubble)
+    gtk_bubble_window_popdown (GTK_BUBBLE_WINDOW (priv->selection_bubble));
+
+  if (priv->selection_bubble_timeout_id)
+    {
+      g_source_remove (priv->selection_bubble_timeout_id);
+      priv->selection_bubble_timeout_id = 0;
+    }
+}
+
+static void
+gtk_entry_selection_bubble_popup_set (GtkEntry *entry,
+                                      GdkEvent *event)
+{
+  GtkEntryPrivate *priv;
+  PopupInfo *info;
+  GdkEvent *copy = NULL;
+
+  priv = entry->priv;
+  info = g_new0 (PopupInfo, 1);
+
+  if (priv->selection_bubble_timeout_id)
+    g_source_remove (priv->selection_bubble_timeout_id);
+
+  if (!event)
+    {
+      copy = gtk_get_current_event ();
+
+      if (!copy)
+        return;
+
+      event = copy;
+    }
+
+  info->entry = entry;
+  info->time = gdk_event_get_time (event);
+  info->device = gdk_event_get_device (event);
+
+  priv->selection_bubble_timeout_id =
+    g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, 1,
+                                gtk_entry_selection_bubble_popup_cb,
+                                info, g_free);
+  if (copy)
+    gdk_event_free (copy);
+}
+
 static void
 gtk_entry_drag_begin (GtkWidget      *widget,
                       GdkDragContext *context)



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