Author: matt
Date: 2008-10-19 13:35:32 -0700 (Sun, 19 Oct 2008)
New Revision: 6475
Log:
Improved stability of fl_read_image (STR #2021)

Modified:
   branches/branch-1.1/CHANGES
   branches/branch-1.1/src/fl_read_image.cxx

Modified: branches/branch-1.1/CHANGES
===================================================================
--- branches/branch-1.1/CHANGES 2008-10-19 20:15:48 UTC (rev 6474)
+++ branches/branch-1.1/CHANGES 2008-10-19 20:35:32 UTC (rev 6475)
@@ -11,6 +11,7 @@
          resizing their children (STR #2032)
        - Fixed adding an idle handler during
          a draw() call (STR #1950)
+       - Improved stability of fl_read_image (STR #2021)
 
 CHANGES IN FLTK 1.1.9
 

Modified: branches/branch-1.1/src/fl_read_image.cxx
===================================================================
--- branches/branch-1.1/src/fl_read_image.cxx   2008-10-19 20:15:48 UTC (rev 
6474)
+++ branches/branch-1.1/src/fl_read_image.cxx   2008-10-19 20:35:32 UTC (rev 
6475)
@@ -77,6 +77,11 @@
   return off;
 }
 
+// this handler will catch and ignore exceptions during XGetImage
+// to avoid an application crash
+static int xgetimageerrhandler(Display *display, XErrorEvent *error) {
+  return 0;
+}
 
 //
 // 'fl_read_image()' - Read an image from the current window.
@@ -135,7 +140,11 @@
     }
     if (!win || (dx >= sx && dy >= sy && dx + w <= sw && dy + h <= sh)) {
       // the image is fully contained, we can use the traditional method
+      // however, if the window is obscured etc. the function will still fail. 
Make sure we
+      // catch the error and continue, otherwise an exception will be thrown.
+      XErrorHandler old_handler = XSetErrorHandler(xgetimageerrhandler);
       image = XGetImage(fl_display, fl_window, X, Y, w, h, AllPlanes, ZPixmap);
+      XSetErrorHandler(old_handler);
     } else {
       // image is crossing borders, determine visible region
       int nw, nh, noffx, noffy;
@@ -151,10 +160,13 @@
       if (!image) {
        if (buf) free(buf);
        return 0;
-  }
+      }
 
-      if (!XGetSubImage(fl_display, fl_window, X + noffx, Y + noffy,
-             nw, nh, AllPlanes, ZPixmap, image, noffx, noffy)) {
+      XErrorHandler old_handler = XSetErrorHandler(xgetimageerrhandler);
+      XImage *subimg = XGetSubImage(fl_display, fl_window, X + noffx, Y + 
noffy,
+                                   nw, nh, AllPlanes, ZPixmap, image, noffx, 
noffy);
+      XSetErrorHandler(old_handler);
+      if (!subimg) {
        XDestroyImage(image);
        return 0;
       }

_______________________________________________
fltk-commit mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-commit

Reply via email to