[gimp] Bug 753175 - color picker locks down the GUI



commit 0d88399eb8e9fda504c99111bc9c66c141f4c898
Author: Kristian Rietveld <kris loopnest org>
Date:   Sat Sep 26 14:27:50 2015 +0200

    Bug 753175 - color picker locks down the GUI
    
    The common code relies on X11 pointer grabbing semantics, which does
    not work well on OS X. An attempt using event taps also proved
    problematic, in particular with regard to setting the mouse cursor.
    
    This patch implements a fully separate code for use on OS X platforms.
    It works by simply overlaying the desktop with big transparent windows
    on which the mouse cursor is set and motion events are captured. Evil,
    but it works.

 libgimpwidgets/Makefile.am              |   12 +
 libgimpwidgets/gimppickbutton-default.c |  280 +++++++++++++++++++++
 libgimpwidgets/gimppickbutton-quartz.c  |  414 +++++++++++++++++++++++++++++++
 libgimpwidgets/gimppickbutton.c         |  279 +--------------------
 4 files changed, 709 insertions(+), 276 deletions(-)
---
diff --git a/libgimpwidgets/Makefile.am b/libgimpwidgets/Makefile.am
index c5357b4..b88eed8 100644
--- a/libgimpwidgets/Makefile.am
+++ b/libgimpwidgets/Makefile.am
@@ -281,6 +281,18 @@ EXTRA_DIST = \
        gimpwidgets.def                 \
        $(libgimpwidgets_extra_sources)
 
+#
+# platform-dependent source files
+#
+
+
+if PLATFORM_OSX
+libgimpwidgets_sources += gimppickbutton-quartz.c
+AM_CPPFLAGS += "-xobjective-c"
+else
+libgimpwidgets_sources += gimppickbutton-default.c
+endif
+
 
 #
 # rules to generate built sources
diff --git a/libgimpwidgets/gimppickbutton-default.c b/libgimpwidgets/gimppickbutton-default.c
new file mode 100644
index 0000000..2118f64
--- /dev/null
+++ b/libgimpwidgets/gimppickbutton-default.c
@@ -0,0 +1,280 @@
+/* LIBGIMP - The GIMP Library
+ * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
+ *
+ * gimppickbutton.c
+ * Copyright (C) 2002 Michael Natterer <mitch gimp org>
+ *
+ * based on gtk+/gtk/gtkcolorsel.c
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <gegl.h>
+#include <gtk/gtk.h>
+#include <gdk/gdkkeysyms.h>
+
+#include "libgimpcolor/gimpcolor.h"
+
+#include "gimpwidgetstypes.h"
+
+#include "gimpcairo-utils.h"
+#include "gimphelpui.h"
+#include "gimppickbutton.h"
+#include "gimpstock.h"
+
+#include "cursors/gimp-color-picker-cursors.c"
+
+#include "libgimp/libgimp-intl.h"
+
+static gboolean   gimp_pick_button_mouse_press   (GtkWidget      *invisible,
+                                                  GdkEventButton *event,
+                                                  GimpPickButton *button);
+static gboolean   gimp_pick_button_key_press     (GtkWidget      *invisible,
+                                                  GdkEventKey    *event,
+                                                  GimpPickButton *button);
+static gboolean   gimp_pick_button_mouse_motion  (GtkWidget      *invisible,
+                                                  GdkEventMotion *event,
+                                                  GimpPickButton *button);
+static gboolean   gimp_pick_button_mouse_release (GtkWidget      *invisible,
+                                                  GdkEventButton *event,
+                                                  GimpPickButton *button);
+static void       gimp_pick_button_shutdown      (GimpPickButton *button);
+static void       gimp_pick_button_pick          (GdkScreen      *screen,
+                                                  gint            x_root,
+                                                  gint            y_root,
+                                                  GimpPickButton *button);
+
+
+void              gimp_pick_button_clicked       (GtkButton      *gtk_button);
+
+static GdkCursor *
+make_cursor (GdkDisplay *display)
+{
+  GdkPixbuf *pixbuf;
+  GError    *error = NULL;
+
+  pixbuf = gdk_pixbuf_new_from_resource ("/org/gimp/color-picker-cursors/cursor-color-picker.png",
+                                         &error);
+
+  if (pixbuf)
+    {
+      GdkCursor *cursor = gdk_cursor_new_from_pixbuf (display, pixbuf, 1, 30);
+
+      g_object_unref (pixbuf);
+
+      return cursor;
+    }
+  else
+    {
+      g_critical ("Failed to create cursor image: %s", error->message);
+      g_clear_error (&error);
+    }
+
+  return NULL;
+}
+
+static gboolean
+gimp_pick_button_mouse_press (GtkWidget      *invisible,
+                              GdkEventButton *event,
+                              GimpPickButton *button)
+{
+  if (event->type == GDK_BUTTON_PRESS && event->button == 1)
+    {
+      g_signal_connect (invisible, "motion-notify-event",
+                        G_CALLBACK (gimp_pick_button_mouse_motion),
+                        button);
+      g_signal_connect (invisible, "button-release-event",
+                        G_CALLBACK (gimp_pick_button_mouse_release),
+                        button);
+
+      g_signal_handlers_disconnect_by_func (invisible,
+                                            gimp_pick_button_mouse_press,
+                                            button);
+      g_signal_handlers_disconnect_by_func (invisible,
+                                            gimp_pick_button_key_press,
+                                            button);
+
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
+static gboolean
+gimp_pick_button_key_press (GtkWidget      *invisible,
+                            GdkEventKey    *event,
+                            GimpPickButton *button)
+{
+  if (event->keyval == GDK_KEY_Escape)
+    {
+      gimp_pick_button_shutdown (button);
+
+      g_signal_handlers_disconnect_by_func (invisible,
+                                            gimp_pick_button_mouse_press,
+                                            button);
+      g_signal_handlers_disconnect_by_func (invisible,
+                                            gimp_pick_button_key_press,
+                                            button);
+
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
+static gboolean
+gimp_pick_button_mouse_motion (GtkWidget      *invisible,
+                               GdkEventMotion *event,
+                               GimpPickButton *button)
+{
+  gint x_root;
+  gint y_root;
+
+  gdk_window_get_origin (event->window, &x_root, &y_root);
+  x_root += event->x;
+  y_root += event->y;
+
+  gimp_pick_button_pick (gdk_event_get_screen ((GdkEvent *) event),
+                         x_root, y_root, button);
+
+  return TRUE;
+}
+
+static gboolean
+gimp_pick_button_mouse_release (GtkWidget      *invisible,
+                                GdkEventButton *event,
+                                GimpPickButton *button)
+{
+  gint x_root;
+  gint y_root;
+
+  if (event->button != 1)
+    return FALSE;
+
+  gdk_window_get_origin (event->window, &x_root, &y_root);
+  x_root += event->x;
+  y_root += event->y;
+
+  gimp_pick_button_pick (gdk_event_get_screen ((GdkEvent *) event),
+                         x_root, y_root, button);
+
+  gimp_pick_button_shutdown (button);
+
+  g_signal_handlers_disconnect_by_func (invisible,
+                                        gimp_pick_button_mouse_motion,
+                                        button);
+  g_signal_handlers_disconnect_by_func (invisible,
+                                        gimp_pick_button_mouse_release,
+                                        button);
+
+  return TRUE;
+}
+
+static void
+gimp_pick_button_shutdown (GimpPickButton *button)
+{
+  GdkDisplay *display   = gtk_widget_get_display (button->grab_widget);
+  guint32     timestamp = gtk_get_current_event_time ();
+
+  gdk_display_keyboard_ungrab (display, timestamp);
+  gdk_display_pointer_ungrab (display, timestamp);
+
+  gtk_grab_remove (button->grab_widget);
+}
+
+static void
+gimp_pick_button_pick (GdkScreen      *screen,
+                       gint            x_root,
+                       gint            y_root,
+                       GimpPickButton *button)
+{
+  GdkWindow       *root_window = gdk_screen_get_root_window (screen);
+  cairo_surface_t *image;
+  cairo_t         *cr;
+  guchar          *data;
+  guchar           color[3];
+  GimpRGB          rgb;
+
+  image = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 1, 1);
+
+  cr = cairo_create (image);
+
+  gdk_cairo_set_source_window (cr, root_window, -x_root, -y_root);
+  cairo_paint (cr);
+
+  cairo_destroy (cr);
+
+  data = cairo_image_surface_get_data (image);
+  GIMP_CAIRO_RGB24_GET_PIXEL (data, color[0], color[1], color[2]);
+
+  cairo_surface_destroy (image);
+
+  gimp_rgba_set_uchar (&rgb, color[0], color[1], color[2], 255);
+
+  g_signal_emit_by_name (button, "color-picked", 0, &rgb);
+}
+
+/* entry point to this file, called from gimppickbutton.c */
+void
+gimp_pick_button_clicked (GtkButton *gtk_button)
+{
+  GimpPickButton *button = GIMP_PICK_BUTTON (gtk_button);
+  GtkWidget      *widget;
+  guint32         timestamp;
+
+  if (! button->cursor)
+    button->cursor = make_cursor (gtk_widget_get_display (GTK_WIDGET (gtk_button)));
+
+  if (! button->grab_widget)
+    {
+      button->grab_widget = gtk_invisible_new ();
+
+      gtk_widget_add_events (button->grab_widget,
+                             GDK_BUTTON_RELEASE_MASK |
+                             GDK_BUTTON_PRESS_MASK   |
+                             GDK_POINTER_MOTION_MASK);
+
+      gtk_widget_show (button->grab_widget);
+    }
+
+  widget = button->grab_widget;
+  timestamp = gtk_get_current_event_time ();
+
+  if (gdk_keyboard_grab (gtk_widget_get_window (widget), FALSE,
+                         timestamp) != GDK_GRAB_SUCCESS)
+    {
+      g_warning ("Failed to grab keyboard to do eyedropper");
+      return;
+    }
+
+  if (gdk_pointer_grab (gtk_widget_get_window (widget), FALSE,
+                        GDK_BUTTON_RELEASE_MASK |
+                        GDK_BUTTON_PRESS_MASK   |
+                        GDK_POINTER_MOTION_MASK,
+                        NULL,
+                        button->cursor,
+                        timestamp) != GDK_GRAB_SUCCESS)
+    {
+      gdk_display_keyboard_ungrab (gtk_widget_get_display (widget), timestamp);
+      g_warning ("Failed to grab pointer to do eyedropper");
+      return;
+    }
+
+  gtk_grab_add (widget);
+
+  g_signal_connect (widget, "button-press-event",
+                    G_CALLBACK (gimp_pick_button_mouse_press),
+                    button);
+  g_signal_connect (widget, "key-press-event",
+                    G_CALLBACK (gimp_pick_button_key_press),
+                    button);
+}
diff --git a/libgimpwidgets/gimppickbutton-quartz.c b/libgimpwidgets/gimppickbutton-quartz.c
new file mode 100644
index 0000000..a14cd90
--- /dev/null
+++ b/libgimpwidgets/gimppickbutton-quartz.c
@@ -0,0 +1,414 @@
+/* LIBGIMP - The GIMP Library
+ * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
+ *
+ * gimppickbutton-quartz.c
+ * Copyright (C) 2015 Kristian Rietveld <kris loopnest org>
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <gegl.h>
+#include <gtk/gtk.h>
+#include <gdk/gdkkeysyms.h>
+
+#include "libgimpcolor/gimpcolor.h"
+
+#include "gimpwidgetstypes.h"
+#include "gimppickbutton.h"
+
+#include "cursors/gimp-color-picker-cursors.c"
+
+#ifdef GDK_WINDOWING_QUARTZ
+#import <AppKit/AppKit.h>
+#include <Carbon/Carbon.h>  /* For virtual key codes ... */
+#include <ApplicationServices/ApplicationServices.h>
+#endif
+
+
+void              gimp_pick_button_clicked       (GtkButton      *gtk_button);
+
+
+ interface GimpPickWindowController : NSObject
+{
+  GimpPickButton *button;
+  NSMutableArray *windows;
+}
+
+ property (nonatomic, assign) BOOL firstBecameKey;
+ property (readonly, retain) NSCursor *cursor;
+
+- (id)initWithButton:(GimpPickButton *)_button;
+- (void)updateKeyWindow;
+- (void)shutdown;
+ end
+
+ interface GimpPickView : NSView
+{
+  GimpPickButton *button;
+  GimpPickWindowController *controller;
+}
+
+ property (readonly,assign) NSTrackingArea *area;
+
+- (id)initWithButton:(GimpPickButton *)_button controller:(GimpPickWindowController *)controller;
+ end
+
+ implementation GimpPickView
+
+ synthesize area;
+
+- (id)initWithButton:(GimpPickButton *)_button controller:(GimpPickWindowController *)_controller
+{
+  self = [super init];
+
+  if (self)
+    {
+      button = _button;
+      controller = _controller;
+    }
+
+  return self;
+}
+
+- (void)dealloc
+{
+  [self removeTrackingArea:self.area];
+
+  [super dealloc];
+}
+
+- (void)viewDidMoveToWindow
+{
+  NSTrackingAreaOptions options;
+
+  [super viewDidMoveToWindow];
+
+  if ([self window] == nil)
+    return;
+
+  options = NSTrackingMouseEnteredAndExited |
+            NSTrackingMouseMoved |
+            NSTrackingActiveAlways;
+
+  /* Add assume inside if mouse pointer is above this window */
+  if (NSPointInRect ([NSEvent mouseLocation], self.window.frame))
+    options |= NSTrackingAssumeInside;
+
+  area = [[NSTrackingArea alloc] initWithRect:self.bounds
+                                 options:options
+                                 owner:self
+                                 userInfo:nil];
+  [self addTrackingArea:self.area];
+}
+
+- (void)mouseEntered:(NSEvent *)event
+{
+  /* We handle the mouse cursor manually, see also the comment in
+   * [GimpPickWindow windowDidBecomeMain below].
+   */
+  if (controller.cursor)
+    [controller.cursor push];
+}
+
+- (void)mouseExited:(NSEvent *)event
+{
+  if (controller.cursor)
+    [controller.cursor pop];
+
+  [controller updateKeyWindow];
+}
+
+- (void)mouseMoved:(NSEvent *)event
+{
+  [self pickColor:event];
+}
+
+- (void)mouseUp:(NSEvent *)event
+{
+  [self pickColor:event];
+
+  [controller shutdown];
+}
+
+- (void)rightMouseUp:(NSEvent *)event
+{
+  [self mouseUp:event];
+}
+
+- (void)otherMouseUp:(NSEvent *)event
+{
+  [self mouseUp:event];
+}
+
+- (void)keyDown:(NSEvent *)event
+{
+  if (event.keyCode == kVK_Escape)
+    [controller shutdown];
+}
+
+- (void)pickColor:(NSEvent *)event
+{
+  CGImageRef    root_image_ref;
+  CFDataRef     pixel_data;
+  const guchar *data;
+  GimpRGB       rgb;
+  NSPoint       point;
+
+  /* The event gives us a point in Cocoa window coordinates. The function
+   * CGWindowListCreateImage expects a rectangle in screen coordinates
+   * with the origin in the upper left (contrary to Cocoa). The origin is
+   * on the screen showing the menu bar (this is the screen at index 0 in the
+   * screens array). So, after convering the rectangle to Cocoa screen
+   * coordinates, we use the height of this particular screen to translate
+   * to the coordinate space expected by CGWindowListCreateImage.
+   */
+  point = event.locationInWindow;
+  NSRect rect = NSMakeRect (point.x, point.y,
+                            1, 1);
+  rect = [self.window convertRectToScreen:rect];
+  rect.origin.y = [[[NSScreen screens] objectAtIndex:0] frame].size.height - rect.origin.y;
+
+  root_image_ref = CGWindowListCreateImage (rect,
+                                            kCGWindowListOptionOnScreenOnly,
+                                            kCGNullWindowID,
+                                            kCGWindowImageDefault);
+  pixel_data = CGDataProviderCopyData (CGImageGetDataProvider (root_image_ref));
+  data = CFDataGetBytePtr (pixel_data);
+
+  gimp_rgba_set_uchar (&rgb, data[2], data[1], data[0], 255);
+
+  CGImageRelease (root_image_ref);
+  CFRelease (pixel_data);
+
+  g_signal_emit_by_name (button, "color-picked", &rgb);
+}
+ end
+
+
+ interface GimpPickWindow : NSWindow <NSWindowDelegate>
+{
+  GimpPickWindowController *controller;
+}
+
+- (id)initWithButton:(GimpPickButton *)button forScreen:(NSScreen *)screen 
withController:(GimpPickWindowController *)_controller;
+ end
+
+ implementation GimpPickWindow
+- (id)initWithButton:(GimpPickButton *)button forScreen:(NSScreen *)screen 
withController:(GimpPickWindowController *)_controller
+{
+  self = [super initWithContentRect:screen.frame
+                styleMask:NSBorderlessWindowMask
+                backing:NSBackingStoreBuffered
+                defer:NO];
+
+  if (self)
+    {
+      GimpPickView *view;
+
+      controller = _controller;
+
+      [self setDelegate:self];
+
+      [self setAlphaValue:0.0];
+#if 0
+      /* Useful for debugging purposes */
+      [self setBackgroundColor:[NSColor redColor]];
+      [self setAlphaValue:0.2];
+#endif
+      [self setIgnoresMouseEvents:NO];
+      [self setAcceptsMouseMovedEvents:YES];
+      [self setHasShadow:NO];
+      [self setOpaque:NO];
+
+      /* Set the highest level, so on top of everything */
+      [self setLevel:NSScreenSaverWindowLevel];
+
+      view = [[GimpPickView alloc] initWithButton:button controller:controller];
+      [self setContentView:view];
+      [self makeFirstResponder:view];
+      [view release];
+
+      [self disableCursorRects];
+    }
+
+  return self;
+}
+
+/* Borderless windows cannot become key/main by default, so we force it
+ * to make it so. We need this to receive events.
+ */
+- (BOOL)canBecomeKeyWindow
+{
+  return YES;
+}
+
+- (BOOL)canBecomeMainWindow
+{
+  return YES;
+}
+
+- (void)windowDidBecomeKey:(NSNotification *)aNotification
+{
+  /* We cannot use the usual Cocoa method for handling cursor updates,
+   * since the GDK Quartz backend is interfering. Additionally, because
+   * one of the screen-spanning windows pops up under the mouse pointer this
+   * view will not receive a MouseEntered event. So, we synthesize such
+   * an event here and the view can set the mouse pointer in response to
+   * this. So, this event only has to be synthesized once and only for
+   * the window that pops up under the mouse cursor. Synthesizing multiple
+   * times messes up the mouse cursor stack.
+   *
+   * We cannot set the mouse pointer at this moment, because the GDK window
+   * will still receive an MouseExited event in which turn it will modify
+   * the cursor. So, with this synthesized event we also ensure we set
+   * the mouse cursor *after* the GDK window has manipulated the cursor.
+   */
+  NSEvent *event;
+
+  if (!controller.firstBecameKey ||
+      !NSPointInRect ([NSEvent mouseLocation], self.frame))
+    return;
+
+  controller.firstBecameKey = NO;
+
+  event = [NSEvent enterExitEventWithType:NSMouseEntered
+                   location:[self mouseLocationOutsideOfEventStream]
+                   modifierFlags:0
+                   timestamp:[[NSApp currentEvent] timestamp]
+                   windowNumber:self.windowNumber
+                   context:nil
+                   eventNumber:0
+                   trackingNumber:(NSInteger)[[self contentView] area]
+                   userData:nil];
+
+  [NSApp postEvent:event atStart:NO];
+}
+ end
+
+
+/* To properly handle multi-monitor setups we need to create a
+ * GimpPickWindow for each monitor (NSScreen). This is necessary because
+ * a window on Mac OS X (tested on 10.9) cannot span more than one
+ * monitor, so any approach that attempts to create one large window
+ * spanning all monitors cannot work. So, we have to create multiple
+ * windows in case of multi-monitor setups and these different windows
+ * are managed by GimpPickWindowController.
+ */
+ implementation GimpPickWindowController
+
+ synthesize firstBecameKey;
+ synthesize cursor;
+
+- (id)initWithButton:(GimpPickButton *)_button;
+{
+  self = [super init];
+
+  if (self)
+    {
+      firstBecameKey = YES;
+      button = _button;
+      cursor = [GimpPickWindowController makePickCursor];
+
+      windows = [[NSMutableArray alloc] init];
+
+      for (NSScreen *screen in [NSScreen screens])
+        {
+          GimpPickWindow *window;
+
+          window = [[GimpPickWindow alloc] initWithButton:button
+                                           forScreen:screen
+                                           withController:self];
+
+          [window orderFrontRegardless];
+          [window makeMainWindow];
+
+          [windows addObject:window];
+        }
+
+      [self updateKeyWindow];
+    }
+
+  return self;
+}
+
+- (void)updateKeyWindow
+{
+  for (GimpPickWindow *window in windows)
+    {
+      if (NSPointInRect ([NSEvent mouseLocation], window.frame))
+        [window makeKeyWindow];
+    }
+}
+
+- (void)shutdown
+{
+  GtkWidget *window;
+
+  for (GimpPickWindow *window in windows)
+    [window close];
+
+  [windows release];
+
+  if (cursor)
+    [cursor release];
+
+  /* Give focus back to the window containing the pick button */
+  window = gtk_widget_get_toplevel (GTK_WIDGET (button));
+  gtk_window_present_with_time (GTK_WINDOW (window), GDK_CURRENT_TIME);
+
+  [self release];
+}
+
++ (NSCursor *)makePickCursor
+{
+  GBytes    *bytes = NULL;
+  GError    *error = NULL;
+
+  bytes = g_resources_lookup_data ("/org/gimp/color-picker-cursors-raw/cursor-color-picker.png",
+                                   G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
+
+  if (bytes)
+    {
+      NSData   *data = [NSData dataWithBytes:g_bytes_get_data (bytes, NULL)
+                               length:g_bytes_get_size (bytes)];
+      NSImage  *image = [[NSImage alloc] initWithData:data];
+      NSCursor *cursor = [[NSCursor alloc] initWithImage:image hotSpot:NSMakePoint(1, 30)];
+
+      [image release];
+      g_bytes_unref (bytes);
+
+      return [cursor retain];
+    }
+  else
+    {
+      g_critical ("Failed to create cursor image: %s", error->message);
+      g_clear_error (&error);
+    }
+
+  return NULL;
+}
+ end
+
+/* entrypoint to this file, called from gimppickbutton.c */
+void
+gimp_pick_button_clicked (GtkButton *gtk_button)
+{
+  GimpPickButton           *button = GIMP_PICK_BUTTON (gtk_button);
+  GimpPickWindowController *controller;
+  NSAutoreleasePool        *pool;
+
+  pool = [[NSAutoreleasePool alloc] init];
+
+  controller = [[GimpPickWindowController alloc] initWithButton:button];
+
+  [pool release];
+}
diff --git a/libgimpwidgets/gimppickbutton.c b/libgimpwidgets/gimppickbutton.c
index 2cfea92..94d5db8 100644
--- a/libgimpwidgets/gimppickbutton.c
+++ b/libgimpwidgets/gimppickbutton.c
@@ -31,14 +31,8 @@
 #include "gimppickbutton.h"
 #include "gimpstock.h"
 
-#include "cursors/gimp-color-picker-cursors.c"
-
 #include "libgimp/libgimp-intl.h"
 
-#ifdef GDK_WINDOWING_QUARTZ
-#include <ApplicationServices/ApplicationServices.h>
-#endif
-
 /**
  * SECTION: gimppickbutton
  * @title: GimpPickButton
@@ -56,29 +50,11 @@ enum
   LAST_SIGNAL
 };
 
+/* entry point to gimppickbutton-{default,quartz}.c */
+extern void       gimp_pick_button_clicked       (GtkButton      *gtk_button);
 
-static void       gimp_pick_button_dispose       (GObject        *object);
-
-static void       gimp_pick_button_clicked       (GtkButton      *button);
-
-static gboolean   gimp_pick_button_mouse_press   (GtkWidget      *invisible,
-                                                  GdkEventButton *event,
-                                                  GimpPickButton *button);
-static gboolean   gimp_pick_button_key_press     (GtkWidget      *invisible,
-                                                  GdkEventKey    *event,
-                                                  GimpPickButton *button);
-static gboolean   gimp_pick_button_mouse_motion  (GtkWidget      *invisible,
-                                                  GdkEventMotion *event,
-                                                  GimpPickButton *button);
-static gboolean   gimp_pick_button_mouse_release (GtkWidget      *invisible,
-                                                  GdkEventButton *event,
-                                                  GimpPickButton *button);
-static void       gimp_pick_button_shutdown      (GimpPickButton *button);
-static void       gimp_pick_button_pick          (GdkScreen      *screen,
-                                                  gint            x_root,
-                                                  gint            y_root,
-                                                  GimpPickButton *button);
 
+static void       gimp_pick_button_dispose       (GObject        *object);
 
 G_DEFINE_TYPE (GimpPickButton, gimp_pick_button, GTK_TYPE_BUTTON)
 
@@ -168,252 +144,3 @@ gimp_pick_button_new (void)
 {
   return g_object_new (GIMP_TYPE_PICK_BUTTON, NULL);
 }
-
-
-/*  private functions  */
-
-static GdkCursor *
-make_cursor (GdkDisplay *display)
-{
-  GdkPixbuf *pixbuf;
-  GError    *error = NULL;
-
-  pixbuf = gdk_pixbuf_new_from_resource ("/org/gimp/color-picker-cursors/cursor-color-picker.png",
-                                         &error);
-
-  if (pixbuf)
-    {
-      GdkCursor *cursor = gdk_cursor_new_from_pixbuf (display, pixbuf, 1, 30);
-
-      g_object_unref (pixbuf);
-
-      return cursor;
-    }
-  else
-    {
-      g_critical ("Failed to create cursor image: %s", error->message);
-      g_clear_error (&error);
-    }
-
-  return NULL;
-}
-
-static void
-gimp_pick_button_clicked (GtkButton *gtk_button)
-{
-  GimpPickButton *button = GIMP_PICK_BUTTON (gtk_button);
-  GtkWidget      *widget;
-  guint32         timestamp;
-
-  if (! button->cursor)
-    button->cursor = make_cursor (gtk_widget_get_display (GTK_WIDGET (gtk_button)));
-
-  if (! button->grab_widget)
-    {
-      button->grab_widget = gtk_invisible_new ();
-
-      gtk_widget_add_events (button->grab_widget,
-                             GDK_BUTTON_RELEASE_MASK |
-                             GDK_BUTTON_PRESS_MASK   |
-                             GDK_POINTER_MOTION_MASK);
-
-      gtk_widget_show (button->grab_widget);
-    }
-
-  widget = button->grab_widget;
-  timestamp = gtk_get_current_event_time ();
-
-  if (gdk_keyboard_grab (gtk_widget_get_window (widget), FALSE,
-                         timestamp) != GDK_GRAB_SUCCESS)
-    {
-      g_warning ("Failed to grab keyboard to do eyedropper");
-      return;
-    }
-
-  if (gdk_pointer_grab (gtk_widget_get_window (widget), FALSE,
-                        GDK_BUTTON_RELEASE_MASK |
-                        GDK_BUTTON_PRESS_MASK   |
-                        GDK_POINTER_MOTION_MASK,
-                        NULL,
-                        button->cursor,
-                        timestamp) != GDK_GRAB_SUCCESS)
-    {
-      gdk_display_keyboard_ungrab (gtk_widget_get_display (widget), timestamp);
-      g_warning ("Failed to grab pointer to do eyedropper");
-      return;
-    }
-
-  gtk_grab_add (widget);
-
-  g_signal_connect (widget, "button-press-event",
-                    G_CALLBACK (gimp_pick_button_mouse_press),
-                    button);
-  g_signal_connect (widget, "key-press-event",
-                    G_CALLBACK (gimp_pick_button_key_press),
-                    button);
-}
-
-static gboolean
-gimp_pick_button_mouse_press (GtkWidget      *invisible,
-                              GdkEventButton *event,
-                              GimpPickButton *button)
-{
-  if (event->type == GDK_BUTTON_PRESS && event->button == 1)
-    {
-      g_signal_connect (invisible, "motion-notify-event",
-                        G_CALLBACK (gimp_pick_button_mouse_motion),
-                        button);
-      g_signal_connect (invisible, "button-release-event",
-                        G_CALLBACK (gimp_pick_button_mouse_release),
-                        button);
-
-      g_signal_handlers_disconnect_by_func (invisible,
-                                            gimp_pick_button_mouse_press,
-                                            button);
-      g_signal_handlers_disconnect_by_func (invisible,
-                                            gimp_pick_button_key_press,
-                                            button);
-
-      return TRUE;
-    }
-
-  return FALSE;
-}
-
-static gboolean
-gimp_pick_button_key_press (GtkWidget      *invisible,
-                            GdkEventKey    *event,
-                            GimpPickButton *button)
-{
-  if (event->keyval == GDK_KEY_Escape)
-    {
-      gimp_pick_button_shutdown (button);
-
-      g_signal_handlers_disconnect_by_func (invisible,
-                                            gimp_pick_button_mouse_press,
-                                            button);
-      g_signal_handlers_disconnect_by_func (invisible,
-                                            gimp_pick_button_key_press,
-                                            button);
-
-      return TRUE;
-    }
-
-  return FALSE;
-}
-
-static gboolean
-gimp_pick_button_mouse_motion (GtkWidget      *invisible,
-                               GdkEventMotion *event,
-                               GimpPickButton *button)
-{
-  gint x_root;
-  gint y_root;
-
-  gdk_window_get_origin (event->window, &x_root, &y_root);
-  x_root += event->x;
-  y_root += event->y;
-
-  gimp_pick_button_pick (gdk_event_get_screen ((GdkEvent *) event),
-                         x_root, y_root, button);
-
-  return TRUE;
-}
-
-static gboolean
-gimp_pick_button_mouse_release (GtkWidget      *invisible,
-                                GdkEventButton *event,
-                                GimpPickButton *button)
-{
-  gint x_root;
-  gint y_root;
-
-  if (event->button != 1)
-    return FALSE;
-
-  gdk_window_get_origin (event->window, &x_root, &y_root);
-  x_root += event->x;
-  y_root += event->y;
-
-  gimp_pick_button_pick (gdk_event_get_screen ((GdkEvent *) event),
-                         x_root, y_root, button);
-
-  gimp_pick_button_shutdown (button);
-
-  g_signal_handlers_disconnect_by_func (invisible,
-                                        gimp_pick_button_mouse_motion,
-                                        button);
-  g_signal_handlers_disconnect_by_func (invisible,
-                                        gimp_pick_button_mouse_release,
-                                        button);
-
-  return TRUE;
-}
-
-static void
-gimp_pick_button_shutdown (GimpPickButton *button)
-{
-  GdkDisplay *display   = gtk_widget_get_display (button->grab_widget);
-  guint32     timestamp = gtk_get_current_event_time ();
-
-  gdk_display_keyboard_ungrab (display, timestamp);
-  gdk_display_pointer_ungrab (display, timestamp);
-
-  gtk_grab_remove (button->grab_widget);
-}
-
-static void
-gimp_pick_button_pick (GdkScreen      *screen,
-                       gint            x_root,
-                       gint            y_root,
-                       GimpPickButton *button)
-{
-#ifndef GDK_WINDOWING_QUARTZ
-
-  GdkWindow       *root_window = gdk_screen_get_root_window (screen);
-  cairo_surface_t *image;
-  cairo_t         *cr;
-  guchar          *data;
-  guchar           color[3];
-  GimpRGB          rgb;
-
-  image = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 1, 1);
-
-  cr = cairo_create (image);
-
-  gdk_cairo_set_source_window (cr, root_window, -x_root, -y_root);
-  cairo_paint (cr);
-
-  cairo_destroy (cr);
-
-  data = cairo_image_surface_get_data (image);
-  GIMP_CAIRO_RGB24_GET_PIXEL (data, color[0], color[1], color[2]);
-
-  cairo_surface_destroy (image);
-
-  gimp_rgba_set_uchar (&rgb, color[0], color[1], color[2], 255);
-
-#else /* GDK_WINDOWING_QUARTZ */
-
-  CGImageRef    root_image_ref;
-  CFDataRef     pixel_data;
-  const guchar *data;
-  GimpRGB       rgb;
-
-  CGRect rect = CGRectMake (x_root, y_root, 1, 1);
-  root_image_ref = CGWindowListCreateImage (rect,
-                                            kCGWindowListOptionOnScreenOnly,
-                                            kCGNullWindowID,
-                                            kCGWindowImageDefault);
-  pixel_data = CGDataProviderCopyData(CGImageGetDataProvider(root_image_ref));
-  data = CFDataGetBytePtr(pixel_data);
-
-  gimp_rgba_set_uchar (&rgb, data[2], data[1], data[0], 255);
-
-  CGImageRelease (root_image_ref);
-  CFRelease (pixel_data);
-
-#endif /* GDK_WINDOWING_QUARTZ */
-
-  g_signal_emit (button, pick_button_signals[COLOR_PICKED], 0, &rgb);
-}


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