[gtk+/popovers: 4/26] bubblewindow: Make it relative to GtkWidget coordinates



commit d1560906537d99a438a2b7f60ab9e7f3978d6909
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon Nov 11 14:27:19 2013 +0100

    bubblewindow: Make it relative to GtkWidget coordinates
    
    GdkWindows are gone now from the API, the pointed_to rectangle
    is from now on relative to the widget allocation. GtkTextView
    and GtkEntry were updated to adapt to this change.

 gtk/gtkbubblewindow.c        |   52 ++++++++++++++---------------------------
 gtk/gtkbubblewindowprivate.h |   10 ++++----
 gtk/gtkentry.c               |   16 +++++++-----
 gtk/gtktextview.c            |    6 +---
 4 files changed, 34 insertions(+), 50 deletions(-)
---
diff --git a/gtk/gtkbubblewindow.c b/gtk/gtkbubblewindow.c
index bb99650..efceffa 100644
--- a/gtk/gtkbubblewindow.c
+++ b/gtk/gtkbubblewindow.c
@@ -59,7 +59,6 @@ struct _GtkBubbleWindowPrivate
   GdkDevice *device;
   GtkWidget *widget;
   GtkWindow *window;
-  GdkWindow *relative_to;
   cairo_rectangle_int_t pointing_to;
   gint win_x;
   gint win_y;
@@ -118,7 +117,7 @@ gtk_bubble_window_get_property (GObject    *object,
   switch (prop_id)
     {
     case PROP_RELATIVE_TO:
-      g_value_set_object (value, priv->relative_to);
+      g_value_set_object (value, priv->widget);
       break;
     case PROP_POINTING_TO:
       g_value_set_boxed (value, &priv->pointing_to);
@@ -135,13 +134,9 @@ static void
 gtk_bubble_window_finalize (GObject *object)
 {
   GtkBubbleWindow *window = GTK_BUBBLE_WINDOW (object);
-  GtkBubbleWindowPrivate *priv = window->priv;
 
   _gtk_bubble_window_popdown (window);
 
-  if (priv->relative_to)
-    g_object_unref (priv->relative_to);
-
   G_OBJECT_CLASS (_gtk_bubble_window_parent_class)->finalize (object);
 }
 
@@ -827,7 +822,7 @@ _gtk_bubble_window_class_init (GtkBubbleWindowClass *klass)
                                    g_param_spec_object ("relative-to",
                                                         P_("Relative to"),
                                                         P_("Window the bubble window points to"),
-                                                        GDK_TYPE_WINDOW,
+                                                        GTK_TYPE_WIDGET,
                                                         GTK_PARAM_READWRITE));
   g_object_class_install_property (object_class,
                                    PROP_POINTING_TO,
@@ -847,38 +842,27 @@ _gtk_bubble_window_class_init (GtkBubbleWindowClass *klass)
 
 static void
 gtk_bubble_window_update_relative_to (GtkBubbleWindow *window,
-                                      GdkWindow       *relative_to)
+                                      GtkWidget       *relative_to)
 {
   GtkBubbleWindowPrivate *priv;
 
   priv = window->priv;
 
-  if (priv->relative_to == relative_to)
+  if (priv->widget == relative_to)
     return;
 
-  if (priv->relative_to)
-    g_object_unref (priv->relative_to);
-
   if (priv->window)
     {
       gtk_window_remove_popover (priv->window, GTK_WIDGET (window));
       priv->window = NULL;
     }
 
-  priv->relative_to = (relative_to) ? g_object_ref (relative_to) : NULL;
+  priv->widget = relative_to;
   g_object_notify (G_OBJECT (window), "relative-to");
 
-  if (priv->relative_to)
-    {
-      gdk_window_get_user_data (priv->relative_to, (gpointer*) &priv->widget);
-      priv->window =
-        GTK_WINDOW (gtk_widget_get_ancestor (priv->widget, GTK_TYPE_WINDOW));
-    }
-  else
-    {
-      priv->window = NULL;
-      priv->widget = NULL;
-    }
+  if (priv->widget)
+    priv->window =
+      GTK_WINDOW (gtk_widget_get_ancestor (priv->widget, GTK_TYPE_WINDOW));
 
   if (priv->window)
     gtk_window_add_popover (priv->window, GTK_WIDGET (window));
@@ -917,9 +901,11 @@ gtk_bubble_window_update_preferred_position (GtkBubbleWindow *window,
  * Since: 3.8
  */
 GtkWidget *
-_gtk_bubble_window_new (void)
+_gtk_bubble_window_new (GtkWidget *relative_to)
 {
-  return g_object_new (GTK_TYPE_BUBBLE_WINDOW, NULL);
+  return g_object_new (GTK_TYPE_BUBBLE_WINDOW,
+                       "relative-to", relative_to,
+                       NULL);
 }
 
 /*
@@ -938,10 +924,10 @@ _gtk_bubble_window_new (void)
  */
 void
 _gtk_bubble_window_set_relative_to (GtkBubbleWindow *window,
-                                    GdkWindow       *relative_to)
+                                    GtkWidget       *relative_to)
 {
   g_return_if_fail (GTK_IS_BUBBLE_WINDOW (window));
-  g_return_if_fail (!relative_to || GDK_IS_WINDOW (relative_to));
+  g_return_if_fail (GTK_IS_WIDGET (relative_to));
 
   gtk_bubble_window_update_relative_to (window, relative_to);
 
@@ -961,7 +947,7 @@ _gtk_bubble_window_set_relative_to (GtkBubbleWindow *window,
  *
  * Since: 3.8
  */
-GdkWindow *
+GtkWidget *
 _gtk_bubble_window_get_relative_to (GtkBubbleWindow *window)
 {
   GtkBubbleWindowPrivate *priv;
@@ -970,7 +956,7 @@ _gtk_bubble_window_get_relative_to (GtkBubbleWindow *window)
 
   priv = window->priv;
 
-  return priv->relative_to;
+  return priv->widget;
 }
 
 /*
@@ -1092,12 +1078,12 @@ _gtk_bubble_window_get_position (GtkBubbleWindow *window)
  */
 void
 _gtk_bubble_window_popup (GtkBubbleWindow       *window,
-                          GdkWindow             *relative_to,
+                          GtkWidget             *relative_to,
                           cairo_rectangle_int_t *pointing_to,
                           GtkPositionType        position)
 {
   g_return_if_fail (GTK_IS_BUBBLE_WINDOW (window));
-  g_return_if_fail (!relative_to || GDK_IS_WINDOW (relative_to));
+  g_return_if_fail (GTK_IS_WIDGET (relative_to));
   g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
   g_return_if_fail (pointing_to != NULL);
 
@@ -1122,8 +1108,6 @@ _gtk_bubble_window_popup (GtkBubbleWindow       *window,
 void
 _gtk_bubble_window_popdown (GtkBubbleWindow *window)
 {
-  GtkBubbleWindowPrivate *priv = window->priv;
-
   g_return_if_fail (GTK_IS_BUBBLE_WINDOW (window));
 
   if (gtk_widget_get_visible (GTK_WIDGET (window)))
diff --git a/gtk/gtkbubblewindowprivate.h b/gtk/gtkbubblewindowprivate.h
index eda6431..5e5f032 100644
--- a/gtk/gtkbubblewindowprivate.h
+++ b/gtk/gtkbubblewindowprivate.h
@@ -47,11 +47,11 @@ struct _GtkBubbleWindowClass
 
 GType       _gtk_bubble_window_get_type        (void) G_GNUC_CONST;
 
-GtkWidget * _gtk_bubble_window_new             (void);
+GtkWidget * _gtk_bubble_window_new             (GtkWidget             *relative_to);
 
-void        _gtk_bubble_window_set_relative_to (GtkBubbleWindow *window,
-                                                GdkWindow       *relative_to);
-GdkWindow * _gtk_bubble_window_get_relative_to (GtkBubbleWindow *window);
+void        _gtk_bubble_window_set_relative_to (GtkBubbleWindow       *window,
+                                                GtkWidget             *relative_to);
+GtkWidget * _gtk_bubble_window_get_relative_to (GtkBubbleWindow       *window);
 
 void        _gtk_bubble_window_set_pointing_to (GtkBubbleWindow       *window,
                                                 cairo_rectangle_int_t *rect);
@@ -64,7 +64,7 @@ GtkPositionType
             _gtk_bubble_window_get_position    (GtkBubbleWindow       *window);
 
 void        _gtk_bubble_window_popup           (GtkBubbleWindow       *window,
-                                                GdkWindow             *relative_to,
+                                                GtkWidget             *relative_to,
                                                 cairo_rectangle_int_t *pointing_to,
                                                 GtkPositionType        position);
 
diff --git a/gtk/gtkentry.c b/gtk/gtkentry.c
index d19addb..c081557 100644
--- a/gtk/gtkentry.c
+++ b/gtk/gtkentry.c
@@ -9387,7 +9387,7 @@ bubble_targets_received (GtkClipboard     *clipboard,
   GtkEntry *entry = user_data;
   GtkEntryPrivate *priv = entry->priv;
   cairo_rectangle_int_t rect;
-  GtkAllocation allocation;
+  GtkAllocation allocation, primary, secondary;
   gint start_x, end_x;
   gboolean has_selection;
   gboolean has_clipboard;
@@ -9405,7 +9405,7 @@ bubble_targets_received (GtkClipboard     *clipboard,
   if (priv->selection_bubble)
     gtk_widget_destroy (priv->selection_bubble);
 
-  priv->selection_bubble = _gtk_bubble_window_new ();
+  priv->selection_bubble = _gtk_bubble_window_new (GTK_WIDGET (entry));
   toolbar = GTK_WIDGET (gtk_toolbar_new ());
   gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_TEXT);
   gtk_toolbar_set_show_arrow (GTK_TOOLBAR (toolbar), FALSE);
@@ -9434,7 +9434,9 @@ bubble_targets_received (GtkClipboard     *clipboard,
   start_x -= priv->scroll_offset;
   start_x = CLAMP (start_x, 0, gdk_window_get_width (priv->text_area));
 
-  rect.y = 0;
+  gtk_entry_get_text_area_size (entry, &rect.x, &rect.y, NULL, NULL);
+  get_icon_allocations (entry, &primary, &secondary);
+  rect.x += primary.width;
   rect.height = gdk_window_get_height (priv->text_area);
 
   if (has_selection)
@@ -9442,17 +9444,17 @@ bubble_targets_received (GtkClipboard     *clipboard,
       end_x = gtk_entry_get_selection_bound_location (entry) - priv->scroll_offset;
       end_x = CLAMP (end_x, 0, gdk_window_get_width (priv->text_area));
 
-      rect.x = MIN (start_x, end_x);
-      rect.width = MAX (start_x, end_x) - rect.x;
+      rect.x += MIN (start_x, end_x);
+      rect.width = MAX (start_x, end_x) - MIN (start_x, end_x);
     }
   else
     {
-      rect.x = start_x;
+      rect.x += start_x;
       rect.width = 0;
     }
 
   _gtk_bubble_window_popup (GTK_BUBBLE_WINDOW (priv->selection_bubble),
-                            priv->text_area, &rect, GTK_POS_TOP);
+                            GTK_WIDGET (entry), &rect, GTK_POS_TOP);
 
   priv->selection_bubble_timeout_id = 0;
 }
diff --git a/gtk/gtktextview.c b/gtk/gtktextview.c
index a2030c9..2f485e9 100644
--- a/gtk/gtktextview.c
+++ b/gtk/gtktextview.c
@@ -8861,7 +8861,6 @@ bubble_targets_received (GtkClipboard     *clipboard,
   gboolean can_insert;
   GtkTextIter iter;
   GtkTextIter sel_start, sel_end;
-  GdkWindow *window;
   GtkWidget *toolbar;
 
   has_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
@@ -8875,8 +8874,7 @@ bubble_targets_received (GtkClipboard     *clipboard,
   if (priv->selection_bubble)
     gtk_widget_destroy (priv->selection_bubble);
 
-  window = gtk_widget_get_window (GTK_WIDGET (text_view));
-  priv->selection_bubble = _gtk_bubble_window_new ();
+  priv->selection_bubble = _gtk_bubble_window_new (GTK_WIDGET (text_view));
   toolbar = GTK_WIDGET (gtk_toolbar_new ());
   gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_TEXT);
   gtk_toolbar_set_show_arrow (GTK_TOOLBAR (toolbar), FALSE);
@@ -8904,7 +8902,7 @@ bubble_targets_received (GtkClipboard     *clipboard,
   rect.x -= priv->xoffset;
   rect.y -= priv->yoffset;
   _gtk_bubble_window_popup (GTK_BUBBLE_WINDOW (priv->selection_bubble),
-                           window, &rect, GTK_POS_TOP);
+                            GTK_WIDGET (text_view), &rect, GTK_POS_TOP);
 
   priv->selection_bubble_timeout_id = 0;
 }


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