]> Pileus Git - ~andy/gtk/blob - gdk/quartz/gdkutils-quartz.c
122a31cb07c07ebe4ced60f449085e7e4f298955
[~andy/gtk] / gdk / quartz / gdkutils-quartz.c
1 /* gdkutils-quartz.c
2  *
3  * Copyright (C) 2005 Imendio AB
4  * Copyright (C) 2010  Kristian Rietveld  <kris@gtk.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "config.h"
23
24 #include <AppKit/AppKit.h>
25
26 #include <gdkquartzutils.h>
27 #include "gdkprivate-quartz.h"
28
29 NSImage *
30 gdk_quartz_pixbuf_to_ns_image_libgtk_only (GdkPixbuf *pixbuf)
31 {
32   NSBitmapImageRep  *bitmap_rep;
33   NSImage           *image;
34   gboolean           has_alpha;
35   
36   has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
37   
38   /* Create a bitmap image rep */
39   bitmap_rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL 
40                                          pixelsWide:gdk_pixbuf_get_width (pixbuf)
41                                          pixelsHigh:gdk_pixbuf_get_height (pixbuf)
42                                          bitsPerSample:8 samplesPerPixel:has_alpha ? 4 : 3
43                                          hasAlpha:has_alpha isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace
44                                          bytesPerRow:0 bitsPerPixel:0];
45         
46   {
47     /* Add pixel data to bitmap rep */
48     guchar *src, *dst;
49     int src_stride, dst_stride;
50     int x, y;
51                 
52     src_stride = gdk_pixbuf_get_rowstride (pixbuf);
53     dst_stride = [bitmap_rep bytesPerRow];
54                 
55     for (y = 0; y < gdk_pixbuf_get_height (pixbuf); y++) 
56       {
57         src = gdk_pixbuf_get_pixels (pixbuf) + y * src_stride;
58         dst = [bitmap_rep bitmapData] + y * dst_stride;
59         
60         for (x = 0; x < gdk_pixbuf_get_width (pixbuf); x++)
61           {
62             if (has_alpha)
63               {
64                 guchar red, green, blue, alpha;
65                 
66                 red = *src++;
67                 green = *src++;
68                 blue = *src++;
69                 alpha = *src++;
70                 
71                 *dst++ = (red * alpha) / 255;
72                 *dst++ = (green * alpha) / 255;
73                 *dst++ = (blue * alpha) / 255;
74                 *dst++ = alpha;
75               }
76             else
77              {
78                *dst++ = *src++;
79                *dst++ = *src++;
80                *dst++ = *src++;
81              }
82           }
83       } 
84   }
85         
86   image = [[NSImage alloc] init];
87   [image addRepresentation:bitmap_rep];
88   [bitmap_rep release];
89   [image autorelease];
90         
91   return image;
92 }
93
94 NSEvent *
95 gdk_quartz_event_get_nsevent (GdkEvent *event)
96 {
97   /* FIXME: If the event here is unallocated, we crash. */
98   return ((GdkEventPrivate *) event)->windowing_data;
99 }