[gnome-utils] screenshot: refactor window rect code



commit 6576afc3c60d0f84be97f99a455de90d8e3b7144
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Mon Jun 13 17:36:02 2011 -0400

    screenshot: refactor window rect code
    
    Separate the common parts into functions which can be used by both
    get_pixbuf() and get_window_rect() methods.

 gnome-screenshot/screenshot-utils.c |   86 ++++++++++++++++-------------------
 1 files changed, 39 insertions(+), 47 deletions(-)
---
diff --git a/gnome-screenshot/screenshot-utils.c b/gnome-screenshot/screenshot-utils.c
index 22d502a..a3afcf9 100644
--- a/gnome-screenshot/screenshot-utils.c
+++ b/gnome-screenshot/screenshot-utils.c
@@ -650,15 +650,12 @@ mask_monitors (GdkPixbuf *pixbuf, GdkWindow *root_window)
   cairo_region_destroy (invisible_region);
 }
 
-void
-screenshot_get_window_rect (GdkWindow *window,
-                            GdkRectangle *rect)
+static GdkWindow *
+screenshot_ensure_wm_window (GdkWindow *window)
 {
-  gint x_real_orig, y_real_orig, x_orig, y_orig;
-  gint width, real_width, height, real_height;
+  GdkWindow *retval = window;
 
   /* If the screenshot should include the border, we look for the WM window. */
-
   if (window != gdk_get_default_root_window ())
     {
       Window xid, wm;
@@ -667,10 +664,24 @@ screenshot_get_window_rect (GdkWindow *window,
       wm = find_wm_window (xid);
 
       if (wm != None)
-        window = gdk_x11_window_foreign_new_for_display (gdk_window_get_display (window), wm);
+        retval = gdk_x11_window_foreign_new_for_display (gdk_window_get_display (window), wm);
+
       /* fallback to no border if we can't find the WM window. */
     }
 
+  return retval;
+}
+
+static void
+screenshot_get_window_rect_coords (GdkWindow *window,
+                                   gint *x_orig_out,
+                                   gint *y_orig_out,
+                                   gint *width_out,
+                                   gint *height_out)
+{
+  gint x_real_orig, y_real_orig, x_orig, y_orig;
+  gint width, real_width, height, real_height;
+
   real_width = gdk_window_get_width (window);
   real_height = gdk_window_get_height (window);
   gdk_window_get_origin (window, &x_real_orig, &y_real_orig);
@@ -698,10 +709,22 @@ screenshot_get_window_rect (GdkWindow *window,
   if (y_orig + height > gdk_screen_height ())
     height = gdk_screen_height () - y_orig;
 
-  rect->x = x_orig;
-  rect->y = y_orig;
-  rect->width = width;
-  rect->height = height;
+  *x_orig_out = x_orig;
+  *y_orig_out = y_orig;
+  *width_out = width;
+  *height_out = height;
+}
+
+void
+screenshot_get_window_rect (GdkWindow *window,
+                            GdkRectangle *rect)
+{
+  GdkWindow *wm_window;
+
+  wm_window = screenshot_ensure_wm_window (window);
+  screenshot_get_window_rect_coords (wm_window,
+                                     &rect->x, &rect->y,
+                                     &rect->width, &rect->height);
 }
 
 GdkPixbuf *
@@ -715,49 +738,18 @@ screenshot_get_pixbuf (GdkWindow    *window,
   gint x_real_orig, y_real_orig, x_orig, y_orig;
   gint width, real_width, height, real_height;
 
-  /* If the screenshot should include the border, we look for the WM window. */
-
   if (include_border)
-    {
-      Window xid, wm;
-
-      xid = GDK_WINDOW_XID (window);
-      wm = find_wm_window (xid);
-
-      if (wm != None)
-        window = gdk_x11_window_foreign_new_for_display (gdk_window_get_display (window), wm);
-
-      /* fallback to no border if we can't find the WM window. */
-    }
+    window = screenshot_ensure_wm_window (window);
 
   root = gdk_get_default_root_window ();
 
+  gdk_window_get_origin (window, &x_real_orig, &y_real_orig);
   real_width = gdk_window_get_width (window);
   real_height = gdk_window_get_height (window);
-  gdk_window_get_origin (window, &x_real_orig, &y_real_orig);
 
-  x_orig = x_real_orig;
-  y_orig = y_real_orig;
-  width  = real_width;
-  height = real_height;
-
-  if (x_orig < 0)
-    {
-      width = width + x_orig;
-      x_orig = 0;
-    }
-
-  if (y_orig < 0)
-    {
-      height = height + y_orig;
-      y_orig = 0;
-    }
-
-  if (x_orig + width > gdk_screen_width ())
-    width = gdk_screen_width () - x_orig;
-
-  if (y_orig + height > gdk_screen_height ())
-    height = gdk_screen_height () - y_orig;
+  screenshot_get_window_rect_coords (window, 
+                                     &x_orig, &y_orig,
+                                     &width, &height);
 
   if (rectangle)
     {



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