[gtk/wip/matthiasc/popup5: 31/118] Review and replace uses of get_toplevel



commit 36159a266db4b98104e50ba121a1737b3411d169
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.

 demos/gtk-demo/changedisplay.c   |   2 +-
 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/gtkimcontextxim.c            |   4 +-
 gtk/gtklabel.c                   |  29 ++++++-----
 gtk/gtkmain.c                    |  76 ++++++++++++++++-----------
 gtk/gtktooltip.c                 |   6 +--
 gtk/gtkwidget.c                  | 109 +++++++++++++++++++--------------------
 gtk/gtkwindow.c                  |   2 +-
 gtk/inspector/inspect-button.c   |   4 +-
 gtk/inspector/logs.c             |   4 +-
 gtk/inspector/window.c           |   4 +-
 17 files changed, 166 insertions(+), 156 deletions(-)
---
diff --git a/demos/gtk-demo/changedisplay.c b/demos/gtk-demo/changedisplay.c
index 5f5717c43d..1ef592b21f 100644
--- a/demos/gtk-demo/changedisplay.c
+++ b/demos/gtk-demo/changedisplay.c
@@ -74,7 +74,7 @@ find_toplevel_at_pointer (GdkDisplay *display)
   pointer_window = gdk_device_get_surface_at_position (gtk_get_current_event_device (), NULL, NULL);
 
   if (pointer_window)
-    widget = GTK_WIDGET (gtk_root_get_for_surface (pointer_window));
+    widget = GTK_WIDGET (gtk_native_get_for_surface (pointer_window));
 
   return widget;
 }
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 54ca14c5ba..37d375c686 100644
--- a/gtk/gtkcolorbutton.c
+++ b/gtk/gtkcolorbutton.c
@@ -513,7 +513,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/gtkimcontextxim.c b/gtk/gtkimcontextxim.c
index c203f502df..1157286e4f 100644
--- a/gtk/gtkimcontextxim.c
+++ b/gtk/gtkimcontextxim.c
@@ -22,7 +22,7 @@
 
 #include "gtkimcontextxim.h"
 #include "gtkimmoduleprivate.h"
-#include "gtkroot.h"
+#include "gtknative.h"
 
 #include "gtk/gtkintl.h"
 
@@ -1536,7 +1536,7 @@ update_client_widget (GtkIMContextXIM *context_xim)
   GtkWidget *new_client_widget = NULL;
 
   if (context_xim->client_surface)
-    new_client_widget = gtk_root_get_for_surface (context_xim->client_surface);
+    new_client_widget = gtk_native_get_for_surface (context_xim->client_surface);
 
   if (new_client_widget != context_xim->client_widget)
     {
diff --git a/gtk/gtklabel.c b/gtk/gtklabel.c
index e883b961f3..693293627d 100644
--- a/gtk/gtklabel.c
+++ b/gtk/gtklabel.c
@@ -4553,28 +4553,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));
     }
@@ -5859,12 +5859,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
@@ -6146,11 +6144,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 7ce4880c7d..da2aa2c85e 100644
--- a/gtk/gtkmain.c
+++ b/gtk/gtkmain.c
@@ -132,6 +132,7 @@
 #include "gtkprintbackend.h"
 #include "gtkimmodule.h"
 #include "gtkroot.h"
+#include "gtknative.h"
 
 #include "a11y/gtkaccessibility.h"
 #include "inspector/window.h"
@@ -1327,7 +1328,7 @@ rewrite_event_for_grabs (GdkEvent *event)
     }
 
   event_widget = gtk_get_event_widget (event);
-  grab_widget = gtk_root_get_for_surface (grab_surface);
+  grab_widget = gtk_native_get_for_surface (grab_surface);
 
   if (grab_widget &&
       gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
@@ -1639,6 +1640,20 @@ is_pointing_event (GdkEvent *event)
     }
 }
 
+static gboolean
+is_key_event (GdkEvent *event)
+{
+  switch ((guint) event->any.type)
+    {
+    case GDK_KEY_PRESS:
+    case GDK_KEY_RELEASE:
+      return TRUE;
+      break;
+    default:
+      return FALSE;
+    }
+}
+
 static inline void
 set_widget_active_state (GtkWidget       *target,
                          const gboolean   release)
@@ -1662,21 +1677,18 @@ handle_pointing_event (GdkEvent *event)
 {
   GtkWidget *target = NULL, *old_target = NULL, *event_widget;
   GtkWindow *toplevel;
-  GtkWidget *toplevel_widget;
   GdkEventSequence *sequence;
   GdkDevice *device;
   gdouble x, y;
+  GtkWidget *native;
 
   event_widget = gtk_get_event_widget (event);
   device = gdk_event_get_device (event);
   if (!device || !gdk_event_get_coords (event, &x, &y))
     return event_widget;
 
-  toplevel_widget = gtk_widget_get_toplevel (event_widget);
-  if (!GTK_IS_WINDOW (toplevel_widget))
-    return event_widget;
-
-  toplevel = GTK_WINDOW (toplevel_widget);
+  toplevel = GTK_WINDOW (gtk_widget_get_root (event_widget));
+  native = gtk_widget_get_ancestor (event_widget, GTK_TYPE_NATIVE);
 
   sequence = gdk_event_get_event_sequence (event);
 
@@ -1706,10 +1718,10 @@ handle_pointing_event (GdkEvent *event)
       target = gtk_window_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 (native, x, y, GTK_PICK_DEFAULT);
 
       if (!target)
-        target = toplevel_widget;
+        target = GTK_WIDGET (native);
 
       old_target = update_pointer_focus_state (toplevel, event, target);
 
@@ -1745,7 +1757,7 @@ handle_pointing_event (GdkEvent *event)
       if (event->any.type == GDK_BUTTON_RELEASE)
         {
           GtkWidget *new_target;
-          new_target = gtk_widget_pick (GTK_WIDGET (toplevel), x, y, GTK_PICK_DEFAULT);
+          new_target = gtk_widget_pick (GTK_WIDGET (native), x, y, GTK_PICK_DEFAULT);
           if (new_target == NULL)
             new_target = GTK_WIDGET (toplevel);
           gtk_synthesize_crossing_events (GTK_ROOT (toplevel), target, new_target, event,
@@ -1771,6 +1783,18 @@ handle_pointing_event (GdkEvent *event)
   return target ? target : old_target;
 }
 
+static GtkWidget *
+handle_key_event (GdkEvent *event)
+{
+  GtkWidget *event_widget;
+  GtkWidget *focus_widget;
+
+  event_widget = gtk_get_event_widget (event);
+
+  focus_widget = gtk_root_get_focus (gtk_widget_get_root (event_widget));
+  return focus_widget ? focus_widget : event_widget;
+}
+
 /**
  * gtk_main_do_event:
  * @event: An event to process (normally passed by GDK)
@@ -1850,20 +1874,14 @@ gtk_main_do_event (GdkEvent *event)
 
   if (is_pointing_event (event))
     target_widget = handle_pointing_event (event);
-  else if (GTK_IS_ROOT (target_widget) &&
-           (event->any.type == GDK_KEY_PRESS ||
-            event->any.type == GDK_KEY_RELEASE))
+  else if (is_key_event (event))
     {
-      GtkWidget *focus_widget;
-
       if (event->any.type == GDK_KEY_PRESS &&
           GTK_IS_WINDOW (target_widget) &&
           gtk_window_activate_key (GTK_WINDOW (target_widget), (GdkEventKey *) event))
         goto cleanup;
 
-      focus_widget = gtk_root_get_focus (GTK_ROOT (target_widget));
-      if (focus_widget)
-        target_widget = focus_widget;
+      target_widget = handle_key_event (event);
     }
 
   if (!target_widget)
@@ -1923,7 +1941,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)))
@@ -1956,11 +1974,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;
@@ -1972,17 +1990,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;
@@ -2471,7 +2489,7 @@ gtk_get_event_widget (const GdkEvent *event)
   widget = NULL;
   if (event && event->any.surface &&
       (event->any.type == GDK_DESTROY || !gdk_surface_is_destroyed (event->any.surface)))
-    widget = gtk_root_get_for_surface (event->any.surface);
+    widget = gtk_native_get_for_surface (event->any.surface);
 
   return widget;
 }
diff --git a/gtk/gtktooltip.c b/gtk/gtktooltip.c
index aaf7fab1f5..0012e5039d 100644
--- a/gtk/gtktooltip.c
+++ b/gtk/gtktooltip.c
@@ -35,7 +35,7 @@
 #include "gtkwindowprivate.h"
 #include "gtkwidgetprivate.h"
 #include "gtkaccessible.h"
-#include "gtkroot.h"
+#include "gtknative.h"
 
 #ifdef GDK_WINDOWING_WAYLAND
 #include "wayland/gdkwayland.h"
@@ -429,7 +429,7 @@ _gtk_widget_find_at_coords (GdkSurface *surface,
 
   g_return_val_if_fail (GDK_IS_SURFACE (surface), NULL);
 
-  event_widget = gtk_root_get_for_surface (surface);
+  event_widget = gtk_native_get_for_surface (surface);
 
   if (!event_widget)
     return NULL;
@@ -500,7 +500,7 @@ gtk_tooltip_set_last_surface (GtkTooltip *tooltip,
                               (gpointer *) &tooltip->last_surface);
 
   if (surface)
-    window_widget = gtk_root_get_for_surface (surface);
+    window_widget = gtk_native_get_for_surface (surface);
 
   if (window_widget)
     window_widget = gtk_widget_get_toplevel (window_widget);
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index 499c386519..45e24b070c 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -2441,7 +2441,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;
@@ -2917,7 +2917,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));
 
@@ -2930,9 +2930,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);
@@ -2955,7 +2955,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
@@ -3172,14 +3172,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 ||
@@ -3222,14 +3223,12 @@ 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_WINDOW (toplevel))
-    return;
-
-  gtk_window_update_pointer_focus_on_state_change (GTK_WINDOW (toplevel),
-                                                   widget);
+  root = _gtk_widget_get_root (widget);
+  if (root)
+    gtk_window_update_pointer_focus_on_state_change (GTK_WINDOW (root),
+                                                     widget);
 }
 
 /**
@@ -4158,14 +4157,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);
@@ -4614,7 +4613,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.
@@ -5569,10 +5568,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
@@ -5695,12 +5695,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;
     }
@@ -5916,7 +5914,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);
@@ -5924,10 +5923,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);
 
@@ -6291,9 +6290,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);
 }
 
 /**
@@ -6474,12 +6475,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()
@@ -7229,13 +7224,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))
@@ -7307,7 +7302,7 @@ _gtk_widget_scale_changed (GtkWidget *widget)
 gint
 gtk_widget_get_scale_factor (GtkWidget *widget)
 {
-  GtkWidget *toplevel;
+  GtkRoot *root;
   GdkDisplay *display;
   GdkMonitor *monitor;
 
@@ -7316,9 +7311,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:
@@ -10111,7 +10106,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
     {
@@ -10207,14 +10203,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)
     {
@@ -11140,6 +11136,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)
     {
@@ -13428,7 +13427,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));
@@ -13436,9 +13435,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 5693dc1790..e5e5475cb7 100644
--- a/gtk/gtkwindow.c
+++ b/gtk/gtkwindow.c
@@ -6191,7 +6191,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;
       
diff --git a/gtk/inspector/inspect-button.c b/gtk/inspector/inspect-button.c
index ee3b3c70a4..0ebb226845 100644
--- a/gtk/inspector/inspect-button.c
+++ b/gtk/inspector/inspect-button.c
@@ -35,7 +35,7 @@
 #include "gtkgesturemultipress.h"
 #include "gtkeventcontrollermotion.h"
 #include "gtkeventcontrollerkey.h"
-#include "gtkroot.h"
+#include "gtknative.h"
 
 static GtkWidget *
 find_widget_at_pointer (GdkDevice *device)
@@ -46,7 +46,7 @@ find_widget_at_pointer (GdkDevice *device)
   pointer_surface = gdk_device_get_surface_at_position (device, NULL, NULL);
 
   if (pointer_surface)
-    widget = gtk_root_get_for_surface (pointer_surface);
+    widget = gtk_native_get_for_surface (pointer_surface);
 
   if (widget)
     {
diff --git a/gtk/inspector/logs.c b/gtk/inspector/logs.c
index 1bc73c7629..3b9406402f 100644
--- a/gtk/inspector/logs.c
+++ b/gtk/inspector/logs.c
@@ -35,7 +35,7 @@
 #include "gtkprivate.h"
 #include "gtkdebug.h"
 #include "gdkinternals.h"
-#include "gtkrootprivate.h"
+#include "gtknativeprivate.h"
 #include "gskdebugprivate.h"
 #include "gskrendererprivate.h"
 
@@ -146,7 +146,7 @@ flag_toggled (GtkWidget        *button,
       if (toplevel == gtk_widget_get_toplevel (button)) /* skip the inspector */
         continue;
 
-      renderer = gtk_root_get_renderer (GTK_ROOT (toplevel));
+      renderer = gtk_native_get_renderer (GTK_NATIVE (toplevel));
       if (!renderer)
         continue;
 
diff --git a/gtk/inspector/window.c b/gtk/inspector/window.c
index e7c9ee9fb1..5b9b2f320b 100644
--- a/gtk/inspector/window.c
+++ b/gtk/inspector/window.c
@@ -48,7 +48,7 @@
 #include "gtklabel.h"
 #include "gtkmodulesprivate.h"
 #include "gtkprivate.h"
-#include "gtkrootprivate.h"
+#include "gtknativeprivate.h"
 #include "gtkstack.h"
 #include "gtktreeviewcolumn.h"
 #include "gtkwindowgroup.h"
@@ -293,7 +293,7 @@ gtk_inspector_window_realize (GtkWidget *widget)
 
   GTK_WIDGET_CLASS (gtk_inspector_window_parent_class)->realize (widget);
 
-  renderer = gtk_root_get_renderer (GTK_ROOT (widget));
+  renderer = gtk_native_get_renderer (GTK_NATIVE (widget));
   gsk_renderer_set_debug_flags (renderer, 0);
 }
 


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