[gtk+/wip/attente/popup-at: 1/5] gdkwindow: add gdk_window_move_to_rect ()



commit 92432bd31939c0e2d34ddea375843b04bf8a01bf
Author: William Hua <william hua canonical com>
Date:   Wed Jun 15 11:00:38 2016 -0400

    gdkwindow: add gdk_window_move_to_rect ()

 gdk/gdk-private.c      |    3 +-
 gdk/gdk-private.h      |   18 ++++
 gdk/gdkmarshalers.list |    1 +
 gdk/gdkwindow.c        |   52 ++++++++++
 gdk/gdkwindow.h        |   12 +++
 gdk/gdkwindowimpl.c    |  252 ++++++++++++++++++++++++++++++++++++++++++++++++
 gdk/gdkwindowimpl.h    |    8 ++
 7 files changed, 345 insertions(+), 1 deletions(-)
---
diff --git a/gdk/gdk-private.c b/gdk/gdk-private.c
index 7b62cc3..37a4ee6 100644
--- a/gdk/gdk-private.c
+++ b/gdk/gdk-private.c
@@ -16,7 +16,8 @@ gdk__private__ (void)
     gdk_display_get_rendering_mode,
     gdk_display_set_rendering_mode,
     gdk_display_get_debug_updates,
-    gdk_display_set_debug_updates
+    gdk_display_set_debug_updates,
+    gdk_window_move_to_rect
   };
 
   return &table;
diff --git a/gdk/gdk-private.h b/gdk/gdk-private.h
index 474172d..32e3aaa 100644
--- a/gdk/gdk-private.h
+++ b/gdk/gdk-private.h
@@ -31,6 +31,15 @@ gboolean         gdk_display_get_debug_updates (GdkDisplay *display);
 void             gdk_display_set_debug_updates (GdkDisplay *display,
                                                 gboolean    debug_updates);
 
+void            gdk_window_move_to_rect         (GdkWindow          *window,
+                                                 GdkWindow          *transient_for,
+                                                 const GdkRectangle *rect,
+                                                 GdkGravity          rect_anchor,
+                                                 GdkGravity          window_anchor,
+                                                 GdkAnchorHints      anchor_hints,
+                                                 gint                rect_anchor_dx,
+                                                 gint                rect_anchor_dy);
+
 typedef struct {
   /* add all private functions here, initialize them in gdk-private.c */
   gboolean (* gdk_device_grab_info) (GdkDisplay  *display,
@@ -56,6 +65,15 @@ typedef struct {
   gboolean         (* gdk_display_get_debug_updates) (GdkDisplay *display);
   void             (* gdk_display_set_debug_updates) (GdkDisplay *display,
                                                       gboolean    debug_updates);
+
+  void (* gdk_window_move_to_rect) (GdkWindow          *window,
+                                    GdkWindow          *transient_for,
+                                    const GdkRectangle *rect,
+                                    GdkGravity          rect_anchor,
+                                    GdkGravity          window_anchor,
+                                    GdkAnchorHints      anchor_hints,
+                                    gint                rect_anchor_dx,
+                                    gint                rect_anchor_dy);
 } GdkPrivateVTable;
 
 GDK_AVAILABLE_IN_ALL
diff --git a/gdk/gdkmarshalers.list b/gdk/gdkmarshalers.list
index cb42499..adc37b9 100644
--- a/gdk/gdkmarshalers.list
+++ b/gdk/gdkmarshalers.list
@@ -5,3 +5,4 @@ OBJECT:VOID
 OBJECT:DOUBLE,DOUBLE
 BOXED:INT,INT
 VOID:DOUBLE,DOUBLE,POINTER,POINTER
+VOID:POINTER,POINTER,BOOLEAN,BOOLEAN
diff --git a/gdk/gdkwindow.c b/gdk/gdkwindow.c
index d5ffc53..0f26755 100644
--- a/gdk/gdkwindow.c
+++ b/gdk/gdkwindow.c
@@ -148,6 +148,7 @@ enum {
   TO_EMBEDDER,
   FROM_EMBEDDER,
   CREATE_SURFACE,
+  MOVED_TO_RECT,
   LAST_SIGNAL
 };
 
@@ -476,6 +477,21 @@ gdk_window_class_init (GdkWindowClass *klass)
                   2,
                   G_TYPE_INT,
                   G_TYPE_INT);
+
+  signals[MOVED_TO_RECT] =
+    g_signal_new (g_intern_static_string ("moved-to-rect"),
+                  G_OBJECT_CLASS_TYPE (object_class),
+                  G_SIGNAL_RUN_FIRST,
+                  0,
+                  NULL,
+                  NULL,
+                  _gdk_marshal_VOID__POINTER_POINTER_BOOLEAN_BOOLEAN,
+                  G_TYPE_NONE,
+                  4,
+                  G_TYPE_POINTER,
+                  G_TYPE_POINTER,
+                  G_TYPE_BOOLEAN,
+                  G_TYPE_BOOLEAN);
 }
 
 static void
@@ -6137,6 +6153,42 @@ gdk_window_move_resize (GdkWindow *window,
   gdk_window_move_resize_internal (window, TRUE, x, y, width, height);
 }
 
+void
+gdk_window_move_to_rect (GdkWindow          *window,
+                         GdkWindow          *transient_for,
+                         const GdkRectangle *rect,
+                         GdkGravity          rect_anchor,
+                         GdkGravity          window_anchor,
+                         GdkAnchorHints      anchor_hints,
+                         gint                rect_anchor_dx,
+                         gint                rect_anchor_dy)
+{
+  GdkWindowImplClass *impl_class;
+  GdkRectangle transformed_rect;
+
+  g_return_if_fail (GDK_IS_WINDOW (window));
+  g_return_if_fail (GDK_IS_WINDOW (transient_for));
+  g_return_if_fail (rect);
+
+  transformed_rect = *rect;
+
+  while (!gdk_window_has_native (transient_for) && gdk_window_get_effective_parent (transient_for))
+    {
+      transformed_rect.x += transient_for->x;
+      transformed_rect.y += transient_for->y;
+      transient_for = gdk_window_get_effective_parent (transient_for);
+    }
+
+  impl_class = GDK_WINDOW_IMPL_GET_CLASS (window->impl);
+  impl_class->move_to_rect (window,
+                            transient_for,
+                            &transformed_rect,
+                            rect_anchor,
+                            window_anchor,
+                            anchor_hints,
+                            rect_anchor_dx,
+                            rect_anchor_dy);
+}
 
 /**
  * gdk_window_scroll:
diff --git a/gdk/gdkwindow.h b/gdk/gdkwindow.h
index 4d98e07..80082aa 100644
--- a/gdk/gdkwindow.h
+++ b/gdk/gdkwindow.h
@@ -245,6 +245,18 @@ typedef enum
   GDK_GRAVITY_STATIC
 } GdkGravity;
 
+typedef enum
+{
+  GDK_ANCHOR_FLIP_X   = 1 << 0,
+  GDK_ANCHOR_FLIP_Y   = 1 << 1,
+  GDK_ANCHOR_SLIDE_X  = 1 << 2,
+  GDK_ANCHOR_SLIDE_Y  = 1 << 3,
+  GDK_ANCHOR_RESIZE_X = 1 << 4,
+  GDK_ANCHOR_RESIZE_Y = 1 << 5,
+  GDK_ANCHOR_FLIP     = GDK_ANCHOR_FLIP_X | GDK_ANCHOR_FLIP_Y,
+  GDK_ANCHOR_SLIDE    = GDK_ANCHOR_SLIDE_X | GDK_ANCHOR_SLIDE_Y,
+  GDK_ANCHOR_RESIZE   = GDK_ANCHOR_RESIZE_X | GDK_ANCHOR_RESIZE_Y
+} GdkAnchorHints;
 
 /**
  * GdkWindowEdge:
diff --git a/gdk/gdkwindowimpl.c b/gdk/gdkwindowimpl.c
index b7ec4ed..bdd96f5 100644
--- a/gdk/gdkwindowimpl.c
+++ b/gdk/gdkwindowimpl.c
@@ -39,6 +39,257 @@ gdk_window_impl_beep (GdkWindow *window)
   return FALSE;
 }
 
+static GdkDisplay *
+get_display_for_window (GdkWindow *primary,
+                        GdkWindow *secondary)
+{
+  GdkDisplay *display = gdk_window_get_display (primary);
+
+  if (display)
+    return display;
+
+  display = gdk_window_get_display (secondary);
+
+  if (display)
+    return display;
+
+  return gdk_display_get_default ();
+}
+
+static GdkMonitor *
+get_monitor_for_rect (GdkDisplay         *display,
+                      const GdkRectangle *rect)
+{
+  gint biggest_area = G_MININT;
+  GdkMonitor *best_monitor = NULL;
+  GdkMonitor *monitor;
+  GdkRectangle workarea;
+  GdkRectangle intersection;
+  gint x;
+  gint y;
+  gint i;
+
+  for (i = 0; i < gdk_display_get_n_monitors (display); i++)
+    {
+      monitor = gdk_display_get_monitor (display, i);
+      gdk_monitor_get_workarea (monitor, &workarea);
+
+      if (gdk_rectangle_intersect (&workarea, rect, &intersection))
+        {
+          if (intersection.width * intersection.height > biggest_area)
+            {
+              biggest_area = intersection.width * intersection.height;
+              best_monitor = monitor;
+            }
+        }
+    }
+
+  if (best_monitor)
+    return best_monitor;
+
+  x = rect->x + rect->width / 2;
+  y = rect->y + rect->height / 2;
+
+  return gdk_display_get_monitor_at_point (display, x, y);
+}
+
+static gint
+get_anchor_x (GdkGravity anchor)
+{
+  switch (anchor)
+    {
+    case GDK_GRAVITY_STATIC:
+    case GDK_GRAVITY_NORTH_WEST:
+    case GDK_GRAVITY_WEST:
+    case GDK_GRAVITY_SOUTH_WEST:
+      return -1;
+
+    default:
+    case GDK_GRAVITY_NORTH:
+    case GDK_GRAVITY_CENTER:
+    case GDK_GRAVITY_SOUTH:
+      return 0;
+
+    case GDK_GRAVITY_NORTH_EAST:
+    case GDK_GRAVITY_EAST:
+    case GDK_GRAVITY_SOUTH_EAST:
+      return 1;
+    }
+}
+
+static gint
+get_anchor_y (GdkGravity anchor)
+{
+  switch (anchor)
+    {
+    case GDK_GRAVITY_STATIC:
+    case GDK_GRAVITY_NORTH_WEST:
+    case GDK_GRAVITY_NORTH:
+    case GDK_GRAVITY_NORTH_EAST:
+      return -1;
+
+    default:
+    case GDK_GRAVITY_WEST:
+    case GDK_GRAVITY_CENTER:
+    case GDK_GRAVITY_EAST:
+      return 0;
+
+    case GDK_GRAVITY_SOUTH_WEST:
+    case GDK_GRAVITY_SOUTH:
+    case GDK_GRAVITY_SOUTH_EAST:
+      return 1;
+    }
+}
+
+static gint
+choose_position (gint      bounds_x,
+                 gint      bounds_width,
+                 gint      rect_x,
+                 gint      rect_width,
+                 gint      window_width,
+                 gint      rect_anchor,
+                 gint      window_anchor,
+                 gint      rect_anchor_dx,
+                 gboolean  flip,
+                 gboolean *flipped)
+{
+  gint x;
+
+  *flipped = FALSE;
+  x = rect_x + (1 + rect_anchor) * rect_width / 2 + rect_anchor_dx - (1 + window_anchor) * window_width / 2;
+
+  if (!flip || (x >= bounds_x && x + window_width <= bounds_x + bounds_width))
+    return x;
+
+  *flipped = TRUE;
+  x = rect_x + (1 - rect_anchor) * rect_width / 2 - rect_anchor_dx - (1 - window_anchor) * window_width / 2;
+
+  if (x >= bounds_x && x + window_width <= bounds_x + bounds_width)
+    return x;
+
+  *flipped = FALSE;
+  return rect_x + (1 + rect_anchor) * rect_width / 2 + rect_anchor_dx - (1 + window_anchor) * window_width / 
2;
+}
+
+static void
+gdk_window_impl_move_to_rect (GdkWindow          *window,
+                              GdkWindow          *transient_for,
+                              const GdkRectangle *rect,
+                              GdkGravity          rect_anchor,
+                              GdkGravity          window_anchor,
+                              GdkAnchorHints      anchor_hints,
+                              gint                rect_anchor_dx,
+                              gint                rect_anchor_dy)
+{
+  GdkDisplay *display;
+  GdkMonitor *monitor;
+  GdkRectangle bounds;
+  GdkRectangle root_rect = *rect;
+  GdkRectangle flipped_rect;
+  GdkRectangle slid_rect;
+  gboolean flipped_x;
+  gboolean flipped_y;
+
+  gdk_window_get_root_coords (transient_for,
+                              root_rect.x,
+                              root_rect.y,
+                              &root_rect.x,
+                              &root_rect.y);
+
+  display = get_display_for_window (window, transient_for);
+  monitor = get_monitor_for_rect (display, &root_rect);
+  gdk_monitor_get_workarea (monitor, &bounds);
+
+  flipped_rect.width = window->width - window->shadow_left - window->shadow_right;
+  flipped_rect.height = window->height - window->shadow_top - window->shadow_bottom;
+  flipped_rect.x = choose_position (bounds.x,
+                                    bounds.width,
+                                    root_rect.x,
+                                    root_rect.width,
+                                    flipped_rect.width,
+                                    get_anchor_x (rect_anchor),
+                                    get_anchor_x (window_anchor),
+                                    rect_anchor_dx,
+                                    anchor_hints & GDK_ANCHOR_FLIP_X,
+                                    &flipped_x);
+  flipped_rect.y = choose_position (bounds.y,
+                                    bounds.height,
+                                    root_rect.y,
+                                    root_rect.height,
+                                    flipped_rect.height,
+                                    get_anchor_y (rect_anchor),
+                                    get_anchor_y (window_anchor),
+                                    rect_anchor_dy,
+                                    anchor_hints & GDK_ANCHOR_FLIP_Y,
+                                    &flipped_y);
+
+  slid_rect = flipped_rect;
+
+  if (anchor_hints & GDK_ANCHOR_SLIDE_X)
+    {
+      if (slid_rect.x + slid_rect.width > bounds.x + bounds.width)
+        slid_rect.x = bounds.x + bounds.width - slid_rect.width;
+
+      if (slid_rect.x < bounds.x)
+        slid_rect.x = bounds.x;
+    }
+
+  if (anchor_hints & GDK_ANCHOR_SLIDE_Y)
+    {
+      if (slid_rect.y + slid_rect.height > bounds.y + bounds.height)
+        slid_rect.y = bounds.y + bounds.height - slid_rect.height;
+
+      if (slid_rect.y < bounds.y)
+        slid_rect.y = bounds.y;
+    }
+
+  if (anchor_hints & GDK_ANCHOR_RESIZE_X)
+    {
+      if (slid_rect.x < bounds.x)
+        {
+          slid_rect.width -= bounds.x - slid_rect.x;
+          slid_rect.x = bounds.x;
+        }
+
+      if (slid_rect.x + slid_rect.width > bounds.x + bounds.width)
+        slid_rect.width = bounds.x + bounds.width - slid_rect.x;
+    }
+
+  if (anchor_hints & GDK_ANCHOR_RESIZE_Y)
+    {
+      if (slid_rect.y < bounds.y)
+        {
+          slid_rect.height -= bounds.y - slid_rect.y;
+          slid_rect.y = bounds.y;
+        }
+
+      if (slid_rect.y + slid_rect.height > bounds.y + bounds.height)
+        slid_rect.height = bounds.y + bounds.height - slid_rect.y;
+    }
+
+  flipped_rect.x -= window->shadow_left;
+  flipped_rect.y -= window->shadow_top;
+  flipped_rect.width += window->shadow_left + window->shadow_right;
+  flipped_rect.height += window->shadow_top + window->shadow_bottom;
+
+  slid_rect.x -= window->shadow_left;
+  slid_rect.y -= window->shadow_top;
+  slid_rect.width += window->shadow_left + window->shadow_right;
+  slid_rect.height += window->shadow_top + window->shadow_bottom;
+
+  if (slid_rect.width != window->width || slid_rect.height != window->height)
+    gdk_window_move_resize (window, slid_rect.x, slid_rect.y, slid_rect.width, slid_rect.height);
+  else
+    gdk_window_move (window, slid_rect.x, slid_rect.y);
+
+  g_signal_emit_by_name (window,
+                         "moved-to-rect",
+                         &flipped_rect,
+                         &slid_rect,
+                         flipped_x,
+                         flipped_y);
+}
+
 static void
 gdk_window_impl_process_updates_recurse (GdkWindow      *window,
                                          cairo_region_t *region)
@@ -50,6 +301,7 @@ static void
 gdk_window_impl_class_init (GdkWindowImplClass *impl_class)
 {
   impl_class->beep = gdk_window_impl_beep;
+  impl_class->move_to_rect = gdk_window_impl_move_to_rect;
   impl_class->process_updates_recurse = gdk_window_impl_process_updates_recurse;
 }
 
diff --git a/gdk/gdkwindowimpl.h b/gdk/gdkwindowimpl.h
index 4ccdc67..61915e9 100644
--- a/gdk/gdkwindowimpl.h
+++ b/gdk/gdkwindowimpl.h
@@ -75,6 +75,14 @@ struct _GdkWindowImplClass
                                          gint             y,
                                          gint             width,
                                          gint             height);
+  void         (* move_to_rect)         (GdkWindow       *window,
+                                         GdkWindow       *transient_for,
+                                         const GdkRectangle *rect,
+                                         GdkGravity       rect_anchor,
+                                         GdkGravity       window_anchor,
+                                         GdkAnchorHints   anchor_hints,
+                                         gint             rect_anchor_dx,
+                                         gint             rect_anchor_dy);
   void         (* set_background)       (GdkWindow       *window,
                                          cairo_pattern_t *pattern);
 


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