[gtk/wip/matthiasc/popup4: 23/79] Review and replace uses of get_toplevel



commit 0f2a73816ddc37477592d61fac484159d2965d69
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Mar 17 19:41:26 2019 -0400

    Review and replace uses of get_toplevel
    
    Now that roots can have parent widgets, we need to
    carefully examine all calls of gtk_widget_get_toplevel,
    and replace them with gtk_widget_get_root if we want
    the nearest root, and not the ultimate end of the parent
    chain.

 gtk/a11y/gtkmenuitemaccessible.c |   9 ++--
 gtk/gtkappchooserbutton.c        |  12 ++---
 gtk/gtkcellrendereraccel.c       |   2 +-
 gtk/gtkcolorbutton.c             |   2 +-
 gtk/gtkcoloreditor.c             |   6 +--
 gtk/gtkdnd.c                     |  40 +++++++--------
 gtk/gtkdragdest.c                |  11 ++--
 gtk/gtklabel.c                   |  29 +++++------
 gtk/gtkmain.c                    |  31 +++++-------
 gtk/gtkwidget.c                  | 105 ++++++++++++++++++++-------------------
 gtk/gtkwindow.c                  |   2 +-
 11 files changed, 119 insertions(+), 130 deletions(-)
---
diff --git a/gtk/a11y/gtkmenuitemaccessible.c b/gtk/a11y/gtkmenuitemaccessible.c
index 41cf7417b5..01b13aeed4 100644
--- a/gtk/a11y/gtkmenuitemaccessible.c
+++ b/gtk/a11y/gtkmenuitemaccessible.c
@@ -534,12 +534,11 @@ gtk_menu_item_accessible_get_keybinding (AtkAction *action,
 
       if (GTK_IS_MENU_BAR (parent))
         {
-          GtkWidget *toplevel;
+          GtkRoot *root;
 
-          toplevel = gtk_widget_get_toplevel (parent);
-          if (toplevel && GTK_IS_WINDOW (toplevel))
-            mnemonic_modifier =
-              gtk_window_get_mnemonic_modifier (GTK_WINDOW (toplevel));
+          root = gtk_widget_get_root (parent);
+          if (root && GTK_IS_WINDOW (root))
+            mnemonic_modifier = gtk_window_get_mnemonic_modifier (GTK_WINDOW (root));
         }
 
       child = find_item_label (temp_item);
diff --git a/gtk/gtkappchooserbutton.c b/gtk/gtkappchooserbutton.c
index f8721a0720..a222f17f77 100644
--- a/gtk/gtkappchooserbutton.c
+++ b/gtk/gtkappchooserbutton.c
@@ -259,16 +259,14 @@ other_application_item_activated_cb (GtkAppChooserButton *self)
 {
   GtkAppChooserButtonPrivate *priv = gtk_app_chooser_button_get_instance_private (self);
   GtkWidget *dialog, *widget;
-  GtkWindow *toplevel;
+  GtkRoot *root;
 
-  toplevel = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (self)));
-  dialog = gtk_app_chooser_dialog_new_for_content_type (toplevel,
+  root = gtk_widget_get_root (GTK_WIDGET (self));
+  dialog = gtk_app_chooser_dialog_new_for_content_type (GTK_WINDOW (root),
                                                         GTK_DIALOG_DESTROY_WITH_PARENT,
                                                         priv->content_type);
-
-  gtk_window_set_modal (GTK_WINDOW (dialog), gtk_window_get_modal (toplevel));
-  gtk_app_chooser_dialog_set_heading (GTK_APP_CHOOSER_DIALOG (dialog),
-                                      priv->heading);
+  gtk_window_set_modal (GTK_WINDOW (dialog), gtk_window_get_modal (GTK_WINDOW (root)));
+  gtk_app_chooser_dialog_set_heading (GTK_APP_CHOOSER_DIALOG (dialog), priv->heading);
 
   widget = gtk_app_chooser_dialog_get_widget (GTK_APP_CHOOSER_DIALOG (dialog));
   g_object_set (widget,
diff --git a/gtk/gtkcellrendereraccel.c b/gtk/gtkcellrendereraccel.c
index 14a669b431..201656df17 100644
--- a/gtk/gtkcellrendereraccel.c
+++ b/gtk/gtkcellrendereraccel.c
@@ -452,7 +452,7 @@ gtk_cell_renderer_accel_start_editing (GtkCellRenderer      *cell,
   if (!is_editable)
     return NULL;
 
-  surface = gtk_widget_get_surface (gtk_widget_get_toplevel (widget));
+  surface = gtk_widget_get_surface (widget);
 
   if (event)
     seat = gdk_event_get_seat (event);
diff --git a/gtk/gtkcolorbutton.c b/gtk/gtkcolorbutton.c
index 7e10a78abc..145d9985ba 100644
--- a/gtk/gtkcolorbutton.c
+++ b/gtk/gtkcolorbutton.c
@@ -524,7 +524,7 @@ ensure_dialog (GtkColorButton *button)
   priv->cs_dialog = dialog = gtk_color_chooser_dialog_new (priv->title, NULL);
   gtk_window_set_hide_on_close (GTK_WINDOW (dialog), TRUE);
 
-  if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
+  if (GTK_IS_WINDOW (parent))
   {
     if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (dialog)))
       gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
diff --git a/gtk/gtkcoloreditor.c b/gtk/gtkcoloreditor.c
index e6b4035d1e..5f341ffe58 100644
--- a/gtk/gtkcoloreditor.c
+++ b/gtk/gtkcoloreditor.c
@@ -193,7 +193,7 @@ popup_edit (GtkWidget      *widget,
             GtkColorEditor *editor)
 {
   GtkWidget *popup = NULL;
-  GtkWidget *toplevel;
+  GtkRoot *root;
   GtkWidget *focus;
   gint position;
   gint s, e;
@@ -224,8 +224,8 @@ popup_edit (GtkWidget      *widget,
   else if (popup)
     {
       dismiss_current_popup (editor);
-      toplevel = gtk_widget_get_toplevel (GTK_WIDGET (editor));
-      g_set_object (&editor->priv->popdown_focus, gtk_root_get_focus (GTK_ROOT (toplevel)));
+      root = gtk_widget_get_root (GTK_WIDGET (editor));
+      g_set_object (&editor->priv->popdown_focus, gtk_root_get_focus (root));
       editor->priv->current_popup = popup;
       editor->priv->popup_position = position;
       gtk_widget_show (popup);
diff --git a/gtk/gtkdnd.c b/gtk/gtkdnd.c
index 8c1312c383..89d8f9625d 100644
--- a/gtk/gtkdnd.c
+++ b/gtk/gtkdnd.c
@@ -401,9 +401,7 @@ _gtk_drag_dest_handle_event (GtkWidget *toplevel,
     case GDK_DRAG_MOTION:
     case GDK_DROP_START:
       {
-        GdkSurface *surface;
-        gint tx, ty;
-        double x_root, y_root;
+        double x, y;
         gboolean found;
 
         if (event_type == GDK_DROP_START)
@@ -418,16 +416,13 @@ _gtk_drag_dest_handle_event (GtkWidget *toplevel,
               }
           }
 
-        surface = gtk_widget_get_surface (toplevel);
-
-        gdk_surface_get_position (surface, &tx, &ty);
-        gdk_event_get_root_coords (event, &x_root, &y_root);
+        gdk_event_get_coords (event, &x, &y);
 
         found = gtk_drop_find_widget (toplevel,
                                       drop,
                                       info,
-                                      x_root - tx,
-                                      y_root - ty,
+                                      x,
+                                      y,
                                       time,
                                       (event_type == GDK_DRAG_MOTION) ?
                                       gtk_drag_dest_motion :
@@ -452,7 +447,7 @@ _gtk_drag_dest_handle_event (GtkWidget *toplevel,
 }
 
 static gboolean
-gtk_drop_find_widget (GtkWidget           *widget,
+gtk_drop_find_widget (GtkWidget           *event_widget,
                       GdkDrop             *drop,
                       GtkDragDestInfo     *info,
                       gint                 x,
@@ -460,18 +455,19 @@ gtk_drop_find_widget (GtkWidget           *widget,
                       guint32              time,
                       GtkDragDestCallback  callback)
 {
-  if (!gtk_widget_get_mapped (widget) ||
-      !gtk_widget_get_sensitive (widget))
+  GtkWidget *widget;
+
+  if (!gtk_widget_get_mapped (event_widget) ||
+      !gtk_widget_get_sensitive (event_widget))
     return FALSE;
 
-  /* Get the widget at the pointer coordinates and travel up
-   * the widget hierarchy from there.
-   */
-  widget = _gtk_widget_find_at_coords (gtk_widget_get_surface (widget),
-                                       x, y, &x, &y);
+  widget = gtk_widget_pick (event_widget, x, y, GTK_PICK_DEFAULT);
+
   if (!widget)
     return FALSE;
 
+  gtk_widget_translate_coordinates (event_widget, widget, x, y, &x, &y);
+
   while (widget)
     {
       GtkWidget *parent;
@@ -884,7 +880,7 @@ gtk_drag_begin_internal (GtkWidget          *widget,
                          int                 y)
 {
   GtkDragSourceInfo *info;
-  GtkWidget *toplevel;
+  GtkRoot *root;
   GdkDrag *drag;
   double px, py;
   int dx, dy;
@@ -893,9 +889,9 @@ gtk_drag_begin_internal (GtkWidget          *widget,
   if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
     device = gdk_device_get_associated_device (device);
 
-  toplevel = gtk_widget_get_toplevel (widget);
-  gtk_widget_translate_coordinates (widget, toplevel, x, y, &x, &y);
-  gdk_surface_get_device_position (gtk_widget_get_surface (toplevel),
+  root = gtk_widget_get_root (widget);
+  gtk_widget_translate_coordinates (widget, GTK_WIDGET (root), x, y, &x, &y);
+  gdk_surface_get_device_position (gtk_widget_get_surface (widget),
                                    device,
                                    &px, &py,
                                    NULL);
@@ -906,7 +902,7 @@ gtk_drag_begin_internal (GtkWidget          *widget,
   content->widget = g_object_ref (widget);
   content->formats = gdk_content_formats_ref (target_list);
 
-  drag = gdk_drag_begin (gtk_widget_get_surface (toplevel), device, GDK_CONTENT_PROVIDER (content), actions, 
dx, dy);
+  drag = gdk_drag_begin (gtk_widget_get_surface (GTK_WIDGET (root)), device, GDK_CONTENT_PROVIDER (content), 
actions, dx, dy);
   if (drag == NULL)
     {
       g_object_unref (content);
diff --git a/gtk/gtkdragdest.c b/gtk/gtkdragdest.c
index b2dcd78ef2..5ce9f1e441 100644
--- a/gtk/gtkdragdest.c
+++ b/gtk/gtkdragdest.c
@@ -34,10 +34,9 @@
 static void
 gtk_drag_dest_realized (GtkWidget *widget)
 {
-  GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
+  GtkRoot *root = gtk_widget_get_root (widget);
 
-  if (gtk_widget_is_toplevel (toplevel))
-    gdk_surface_register_dnd (gtk_widget_get_surface (toplevel));
+  gdk_surface_register_dnd (gtk_widget_get_surface (GTK_WIDGET (root)));
 }
 
 static void
@@ -45,10 +44,10 @@ gtk_drag_dest_hierarchy_changed (GtkWidget  *widget,
                                  GParamSpec *pspec,
                                  gpointer    data)
 {
-  GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
+  GtkRoot *root = gtk_widget_get_root (widget);
 
-  if (gtk_widget_is_toplevel (toplevel) && gtk_widget_get_realized (toplevel))
-    gdk_surface_register_dnd (gtk_widget_get_surface (toplevel));
+  if (root && gtk_widget_get_realized (GTK_WIDGET (root)))
+    gdk_surface_register_dnd (gtk_widget_get_surface (GTK_WIDGET (root)));
 }
 
 static void
diff --git a/gtk/gtklabel.c b/gtk/gtklabel.c
index fd894b295b..16ef24e076 100644
--- a/gtk/gtklabel.c
+++ b/gtk/gtklabel.c
@@ -4559,28 +4559,28 @@ static void
 connect_mnemonics_visible_notify (GtkLabel *label)
 {
   GtkLabelPrivate *priv = gtk_label_get_instance_private (label);
-  GtkWidget *toplevel;
+  GtkRoot *root;
   gboolean connected;
 
-  toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
+  root = gtk_widget_get_root (GTK_WIDGET (label));
 
-  if (!GTK_IS_WINDOW (toplevel))
+  if (!GTK_IS_WINDOW (root))
     return;
 
   /* always set up this widgets initial value */
   priv->mnemonics_visible =
-    gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
+    gtk_window_get_mnemonics_visible (GTK_WINDOW (root));
 
   connected =
-    GPOINTER_TO_INT (g_object_get_qdata (G_OBJECT (toplevel), quark_mnemonics_visible_connected));
+    GPOINTER_TO_INT (g_object_get_qdata (G_OBJECT (root), quark_mnemonics_visible_connected));
 
   if (!connected)
     {
-      g_signal_connect (toplevel,
+      g_signal_connect (root,
                         "notify::mnemonics-visible",
                         G_CALLBACK (label_mnemonics_visible_changed),
                         label);
-      g_object_set_qdata (G_OBJECT (toplevel),
+      g_object_set_qdata (G_OBJECT (root),
                           quark_mnemonics_visible_connected,
                           GINT_TO_POINTER (1));
     }
@@ -5865,12 +5865,10 @@ gtk_label_move_cursor (GtkLabel       *label,
                                                  count > 0 ?
                                                  GTK_DIR_RIGHT : GTK_DIR_LEFT))
                     {
-                      GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
+                      GtkRoot *root = gtk_widget_get_root (GTK_WIDGET (label));
 
-                      if (toplevel)
-                        gtk_widget_child_focus (toplevel,
-                                                count > 0 ?
-                                                GTK_DIR_RIGHT : GTK_DIR_LEFT);
+                      if (root)
+                        gtk_widget_child_focus (GTK_WIDGET (root), count > 0 ? GTK_DIR_RIGHT : GTK_DIR_LEFT);
                     }
                 }
               else
@@ -6152,11 +6150,14 @@ gtk_label_activate_link (GtkLabel    *label,
                          const gchar *uri)
 {
   GtkWidget *widget = GTK_WIDGET (label);
-  GtkWidget *top_level = gtk_widget_get_toplevel (widget);
+  GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
   guint32 timestamp = gtk_get_current_event_time ();
   GError *error = NULL;
 
-  if (!gtk_show_uri_on_window (GTK_WINDOW (top_level), uri, timestamp, &error))
+  if (!GTK_IS_WINDOW (toplevel))
+    return FALSE;
+
+  if (!gtk_show_uri_on_window (GTK_WINDOW (toplevel), uri, timestamp, &error))
     {
       g_warning ("Unable to show '%s': %s", uri, error->message);
       g_error_free (error);
diff --git a/gtk/gtkmain.c b/gtk/gtkmain.c
index fd2a443a6f..e24c52f971 100644
--- a/gtk/gtkmain.c
+++ b/gtk/gtkmain.c
@@ -1661,7 +1661,6 @@ handle_pointing_event (GdkEvent *event)
 {
   GtkWidget *target = NULL, *old_target = NULL, *event_widget;
   GtkRoot *toplevel;
-  GtkWidget *toplevel_widget;
   GdkEventSequence *sequence;
   GdkDevice *device;
   gdouble x, y;
@@ -1671,11 +1670,7 @@ handle_pointing_event (GdkEvent *event)
   if (!device || !gdk_event_get_coords (event, &x, &y))
     return event_widget;
 
-  toplevel_widget = gtk_widget_get_toplevel (event_widget);
-  if (!GTK_IS_ROOT (toplevel_widget))
-    return event_widget;
-
-  toplevel = GTK_ROOT (toplevel_widget);
+  toplevel = gtk_widget_get_root (event_widget);
 
   sequence = gdk_event_get_event_sequence (event);
 
@@ -1705,10 +1700,10 @@ handle_pointing_event (GdkEvent *event)
       target = gtk_root_lookup_pointer_focus_implicit_grab (toplevel, device, sequence);
 
       if (!target)
-        target = gtk_widget_pick (toplevel_widget, x, y, GTK_PICK_DEFAULT);
+        target = gtk_widget_pick (GTK_WIDGET (toplevel), x, y, GTK_PICK_DEFAULT);
 
       if (!target)
-        target = toplevel_widget;
+        target = GTK_WIDGET (toplevel);
 
       old_target = update_pointer_focus_state (toplevel, event, target);
 
@@ -1914,7 +1909,7 @@ gtk_main_do_event (GdkEvent *event)
     case GDK_DELETE:
       g_object_ref (target_widget);
       if (!gtk_window_group_get_current_grab (window_group) ||
-          gtk_widget_get_toplevel (gtk_window_group_get_current_grab (window_group)) == target_widget)
+          GTK_WIDGET (gtk_widget_get_root (gtk_window_group_get_current_grab (window_group))) == 
target_widget)
         {
           if (!GTK_IS_WINDOW (target_widget) ||
               !gtk_window_emit_close_request (GTK_WINDOW (target_widget)))
@@ -1947,11 +1942,11 @@ gtk_main_do_event (GdkEvent *event)
     case GDK_KEY_RELEASE:
       /* make focus visible in a window that receives a key event */
       {
-        GtkWidget *window;
+        GtkRoot *root;
 
-        window = gtk_widget_get_toplevel (grab_widget);
-        if (GTK_IS_WINDOW (window))
-          gtk_window_set_focus_visible (GTK_WINDOW (window), TRUE);
+        root = gtk_widget_get_root (grab_widget);
+        if (GTK_IS_WINDOW (root))
+          gtk_window_set_focus_visible (GTK_WINDOW (root), TRUE);
       }
 
       /* Catch alt press to enable auto-mnemonics;
@@ -1963,17 +1958,17 @@ gtk_main_do_event (GdkEvent *event)
           !GTK_IS_MENU_SHELL (grab_widget))
         {
           gboolean mnemonics_visible;
-          GtkWidget *window;
+          GtkRoot *root;
 
           mnemonics_visible = (event->any.type == GDK_KEY_PRESS);
 
-          window = gtk_widget_get_toplevel (grab_widget);
-          if (GTK_IS_WINDOW (window))
+          root = gtk_widget_get_root (grab_widget);
+          if (GTK_IS_WINDOW (root))
             {
               if (mnemonics_visible)
-                _gtk_window_schedule_mnemonics_visible (GTK_WINDOW (window));
+                _gtk_window_schedule_mnemonics_visible (GTK_WINDOW (root));
               else
-                gtk_window_set_mnemonics_visible (GTK_WINDOW (window), FALSE);
+                gtk_window_set_mnemonics_visible (GTK_WINDOW (root), FALSE);
             }
         }
       G_GNUC_FALLTHROUGH;
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index ae95736284..ca8dd1b28d 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -2485,7 +2485,7 @@ _gtk_widget_emulate_press (GtkWidget      *widget,
 
   gdk_event_get_coords (event, &x, &y);
   if (!gtk_widget_compute_point (event_widget,
-                                 gtk_widget_get_toplevel (event_widget),
+                                 GTK_WIDGET (gtk_widget_get_root (event_widget)),
                                  &GRAPHENE_POINT_INIT (x, y),
                                  &p))
       return;
@@ -2947,7 +2947,7 @@ gtk_widget_unparent (GtkWidget *widget)
   GtkWidgetPrivate *priv = gtk_widget_get_instance_private (widget);
   GtkWidget *old_parent;
   GtkWidget *old_prev_sibling;
-  GtkWidget *toplevel;
+  GtkRoot *root;
 
   g_return_if_fail (GTK_IS_WIDGET (widget));
 
@@ -2960,9 +2960,9 @@ gtk_widget_unparent (GtkWidget *widget)
 
   g_object_freeze_notify (G_OBJECT (widget));
 
-  toplevel = _gtk_widget_get_toplevel (widget);
-  if (_gtk_widget_is_toplevel (toplevel))
-    _gtk_window_unset_focus_and_default (GTK_WINDOW (toplevel), widget);
+  root = _gtk_widget_get_root (widget);
+  if (GTK_IS_WINDOW (root))
+    _gtk_window_unset_focus_and_default (GTK_WINDOW (root), widget);
 
   if (gtk_widget_get_focus_child (priv->parent) == widget)
     gtk_widget_set_focus_child (priv->parent, NULL);
@@ -2985,7 +2985,7 @@ gtk_widget_unparent (GtkWidget *widget)
   if (priv->root)
     gtk_widget_unroot (widget);
 
-  toplevel = NULL;
+  root = NULL;
 
   /* Removing a widget from a container restores the child visible
    * flag to the default state, so it doesn't affect the child
@@ -3209,14 +3209,15 @@ gtk_widget_hide (GtkWidget *widget)
 
   if (_gtk_widget_get_visible (widget))
     {
-      GtkWidget *toplevel = _gtk_widget_get_toplevel (widget);
       GtkWidget *parent;
+      GtkRoot *root;
 
       g_object_ref (widget);
       gtk_widget_push_verify_invariants (widget);
 
-      if (toplevel != widget && _gtk_widget_is_toplevel (toplevel))
-        _gtk_window_unset_focus_and_default (GTK_WINDOW (toplevel), widget);
+      root = _gtk_widget_get_root (widget);
+      if (GTK_WIDGET (root) != widget && GTK_IS_WINDOW (root))
+        _gtk_window_unset_focus_and_default (GTK_WINDOW (root), widget);
 
       /* a parent may now be expand=FALSE since we're hidden. */
       if (priv->need_compute_expand ||
@@ -3259,11 +3260,11 @@ gtk_widget_real_hide (GtkWidget *widget)
 static void
 update_cursor_on_state_change (GtkWidget *widget)
 {
-  GtkWidget *toplevel;
+  GtkRoot *root;
 
-  toplevel = gtk_widget_get_toplevel (widget);
-  if (GTK_IS_ROOT (toplevel))
-    gtk_root_update_pointer_focus_on_state_change (GTK_ROOT (toplevel), widget);
+  root = _gtk_widget_get_root (widget);
+  if (root)
+    gtk_root_update_pointer_focus_on_state_change (root, widget);
 }
 
 /**
@@ -3926,14 +3927,14 @@ gtk_widget_get_frame_clock (GtkWidget *widget)
 
   if (priv->realized)
     {
-      /* We use gtk_widget_get_toplevel() here to make it explicit that
+      /* We use gtk_widget_get_root() here to make it explicit that
        * the frame clock is a property of the toplevel that a widget
        * is anchored to; gdk_surface_get_toplevel() will go up the
        * hierarchy anyways, but should squash any funny business with
        * reparenting windows and widgets.
        */
-      GtkWidget *toplevel = _gtk_widget_get_toplevel (widget);
-      GdkSurface *surface = _gtk_widget_get_surface (toplevel);
+      GtkRoot *root = _gtk_widget_get_root (widget);
+      GdkSurface *surface = _gtk_widget_get_surface (GTK_WIDGET (root));
       g_assert (surface != NULL);
 
       return gdk_surface_get_frame_clock (surface);
@@ -4379,7 +4380,7 @@ gtk_widget_translate_coordinates (GtkWidget  *src_widget,
  *
  * Translates the given @point in @widget's coordinates to coordinates
  * relative to @target’s coodinate system. In order to perform this
- * operation, both widgets must share a common ancestor.
+ * operation, both widgets must share a common root.
  *
  * Returns: %TRUE if the point could be determined, %FALSE on failure.
  *   In this case, 0 is stored in @out_point.
@@ -5333,10 +5334,11 @@ static void
 gtk_widget_real_move_focus (GtkWidget         *widget,
                             GtkDirectionType   direction)
 {
-  GtkWidget *toplevel = _gtk_widget_get_toplevel (widget);
+  GtkRoot *root;
 
-  if (widget != toplevel && GTK_IS_WINDOW (toplevel))
-    g_signal_emit (toplevel, widget_signals[MOVE_FOCUS], 0, direction);
+  root = _gtk_widget_get_root (widget);
+  if (widget != GTK_WIDGET (root))
+    g_signal_emit (root, widget_signals[MOVE_FOCUS], 0, direction);
 }
 
 static gboolean
@@ -5459,12 +5461,10 @@ gtk_widget_has_visible_focus (GtkWidget *widget)
 
   if (priv->has_focus)
     {
-      GtkWidget *toplevel;
+      GtkRoot *root = _gtk_widget_get_root (widget);
 
-      toplevel = _gtk_widget_get_toplevel (widget);
-
-      if (GTK_IS_WINDOW (toplevel))
-        draw_focus = gtk_window_get_focus_visible (GTK_WINDOW (toplevel));
+      if (GTK_IS_WINDOW (root))
+        draw_focus = gtk_window_get_focus_visible (GTK_WINDOW (root));
       else
         draw_focus = TRUE;
     }
@@ -5752,7 +5752,8 @@ gtk_widget_device_is_shadowed (GtkWidget *widget,
                                GdkDevice *device)
 {
   GtkWindowGroup *group;
-  GtkWidget *grab_widget, *toplevel;
+  GtkWidget *grab_widget;
+  GtkRoot *root;
 
   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
   g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
@@ -5760,10 +5761,10 @@ gtk_widget_device_is_shadowed (GtkWidget *widget,
   if (!_gtk_widget_get_realized (widget))
     return TRUE;
 
-  toplevel = _gtk_widget_get_toplevel (widget);
+  root = _gtk_widget_get_root (widget);
 
-  if (GTK_IS_WINDOW (toplevel))
-    group = gtk_window_get_group (GTK_WINDOW (toplevel));
+  if (GTK_IS_WINDOW (root))
+    group = gtk_window_get_group (GTK_WINDOW (root));
   else
     group = gtk_window_get_group (NULL);
 
@@ -6127,9 +6128,11 @@ gtk_widget_get_has_surface (GtkWidget *widget)
 gboolean
 gtk_widget_is_toplevel (GtkWidget *widget)
 {
+  GtkWidgetPrivate *priv = gtk_widget_get_instance_private (widget);
+
   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
 
-  return GTK_IS_ROOT (widget);
+  return priv->parent == NULL && GTK_IS_ROOT (widget);
 }
 
 /**
@@ -6302,12 +6305,6 @@ gtk_widget_reposition_after (GtkWidget *widget,
       return;
     }
 
-  if (_gtk_widget_is_toplevel (widget))
-    {
-      g_warning ("Can't set a parent on a toplevel widget");
-      return;
-    }
-
   data.old_scale_factor = gtk_widget_get_scale_factor (widget);
 
   /* keep this function in sync with gtk_menu_attach_to_widget()
@@ -7196,13 +7193,13 @@ gtk_widget_set_child_visible (GtkWidget *widget,
     priv->child_visible = TRUE;
   else
     {
-      GtkWidget *toplevel;
+      GtkRoot *root;
 
       priv->child_visible = FALSE;
 
-      toplevel = _gtk_widget_get_toplevel (widget);
-      if (toplevel != widget && _gtk_widget_is_toplevel (toplevel))
-       _gtk_window_unset_focus_and_default (GTK_WINDOW (toplevel), widget);
+      root = _gtk_widget_get_root (widget);
+      if (GTK_WIDGET (root) != widget && GTK_IS_WINDOW (root))
+       _gtk_window_unset_focus_and_default (GTK_WINDOW (root), widget);
     }
 
   if (priv->parent && _gtk_widget_get_realized (priv->parent))
@@ -7274,7 +7271,7 @@ _gtk_widget_scale_changed (GtkWidget *widget)
 gint
 gtk_widget_get_scale_factor (GtkWidget *widget)
 {
-  GtkWidget *toplevel;
+  GtkRoot *root;
   GdkDisplay *display;
   GdkMonitor *monitor;
 
@@ -7283,9 +7280,9 @@ gtk_widget_get_scale_factor (GtkWidget *widget)
   if (_gtk_widget_get_realized (widget))
     return gdk_surface_get_scale_factor (_gtk_widget_get_surface (widget));
 
-  toplevel = _gtk_widget_get_toplevel (widget);
-  if (toplevel && toplevel != widget)
-    return gtk_widget_get_scale_factor (toplevel);
+  root = _gtk_widget_get_root (widget);
+  if (root && GTK_WIDGET (root) != widget)
+    return gtk_widget_get_scale_factor (GTK_WIDGET (root));
 
   /* else fall back to something that is more likely to be right than
    * just returning 1:
@@ -10110,7 +10107,8 @@ _gtk_widget_buildable_finish_accelerator (GtkWidget *widget,
   if (g_slist_length (accel_groups) == 0)
     {
       accel_group = gtk_accel_group_new ();
-      gtk_window_add_accel_group (GTK_WINDOW (toplevel), accel_group);
+      if (GTK_IS_WINDOW (toplevel))
+        gtk_window_add_accel_group (GTK_WINDOW (toplevel), accel_group);
     }
   else
     {
@@ -10206,14 +10204,14 @@ gtk_widget_buildable_custom_finished (GtkBuildable *buildable,
   if (strcmp (tagname, "accelerator") == 0)
     {
       AccelGroupParserData *accel_data;
-      GtkWidget *toplevel;
+      GtkRoot *root;
 
       accel_data = (AccelGroupParserData*)user_data;
       g_assert (accel_data->object);
 
-      toplevel = _gtk_widget_get_toplevel (GTK_WIDGET (accel_data->object));
+      root = _gtk_widget_get_root (GTK_WIDGET (accel_data->object));
 
-      _gtk_widget_buildable_finish_accelerator (GTK_WIDGET (buildable), toplevel, user_data);
+      _gtk_widget_buildable_finish_accelerator (GTK_WIDGET (buildable), GTK_WIDGET (root), user_data);
     }
   else if (strcmp (tagname, "accessibility") == 0)
     {
@@ -11139,6 +11137,9 @@ gtk_widget_compute_transform (GtkWidget         *widget,
   g_return_val_if_fail (GTK_IS_WIDGET (target), FALSE);
   g_return_val_if_fail (out_transform != NULL, FALSE);
 
+  if (widget->priv->root != target->priv->root)
+    return FALSE;
+
   /* optimization for common case: parent wants coordinates of a direct child */
   if (target == widget->priv->parent)
     {
@@ -13385,7 +13386,7 @@ gtk_widget_set_cursor (GtkWidget *widget,
                        GdkCursor *cursor)
 {
   GtkWidgetPrivate *priv = gtk_widget_get_instance_private (widget);
-  GtkWidget *toplevel;
+  GtkRoot *root;
 
   g_return_if_fail (GTK_IS_WIDGET (widget));
   g_return_if_fail (cursor == NULL || GDK_IS_CURSOR (cursor));
@@ -13393,9 +13394,9 @@ gtk_widget_set_cursor (GtkWidget *widget,
   if (!g_set_object (&priv->cursor, cursor))
     return;
 
-  toplevel = gtk_widget_get_toplevel (widget);
-  if (GTK_IS_WINDOW (toplevel))
-    gtk_window_maybe_update_cursor (GTK_WINDOW (toplevel), widget, NULL);
+  root = _gtk_widget_get_root (widget);
+  if (root)
+    gtk_root_maybe_update_cursor (root, widget, NULL);
 
   g_object_notify_by_pspec (G_OBJECT (widget), widget_props[PROP_CURSOR]);
 }
diff --git a/gtk/gtkwindow.c b/gtk/gtkwindow.c
index fe400ddb91..0e86d264a6 100644
--- a/gtk/gtkwindow.c
+++ b/gtk/gtkwindow.c
@@ -6279,7 +6279,7 @@ gtk_window_propagate_key_event (GtkWindow        *window,
   
   while (!handled &&
          focus && focus != widget &&
-         gtk_widget_get_toplevel (focus) == widget)
+         gtk_widget_get_root (focus) == GTK_ROOT (widget))
     {
       GtkWidget *parent;
       


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