]> Pileus Git - ~andy/gtk/blob - gtk/gtkquartz.c
[Quartz Bug 663182] NSImage throws an exception from _gtk_quartz_create_image_from_pi...
[~andy/gtk] / gtk / gtkquartz.c
1 /* gtkquartz.c: Utility functions used by the Quartz port
2  *
3  * Copyright (C) 2006 Imendio AB
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22
23 #include "gtkquartz.h"
24 #include "gtkselectionprivate.h"
25 #include <gdk/quartz/gdkquartz.h>
26
27 NSImage *
28 _gtk_quartz_create_image_from_pixbuf (GdkPixbuf *pixbuf)
29 {
30   CGColorSpaceRef colorspace;
31   CGDataProviderRef data_provider;
32   CGContextRef context;
33   CGImageRef image;
34   void *data;
35   int rowstride, pixbuf_width, pixbuf_height;
36   gboolean has_alpha;
37   NSImage *nsimage;
38   NSSize nsimage_size;
39
40   pixbuf_width = gdk_pixbuf_get_width (pixbuf);
41   pixbuf_height = gdk_pixbuf_get_height (pixbuf);
42   g_return_val_if_fail (pixbuf_width == 0 || pixbuf_height == 0, NULL);
43   rowstride = gdk_pixbuf_get_rowstride (pixbuf);
44   has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
45
46   data = gdk_pixbuf_get_pixels (pixbuf);
47
48   colorspace = CGColorSpaceCreateDeviceRGB ();
49   data_provider = CGDataProviderCreateWithData (NULL, data, pixbuf_height * rowstride, NULL);
50
51   image = CGImageCreate (pixbuf_width, pixbuf_height, 8,
52                          has_alpha ? 32 : 24, rowstride, 
53                          colorspace, 
54                          has_alpha ? kCGImageAlphaLast : 0,
55                          data_provider, NULL, FALSE, 
56                          kCGRenderingIntentDefault);
57
58   CGDataProviderRelease (data_provider);
59   CGColorSpaceRelease (colorspace);
60
61   nsimage = [[NSImage alloc] initWithSize:NSMakeSize (pixbuf_width, pixbuf_height)];
62   nsimage_size = [nsimage size];
63   if (nsimage_size.width == 0.0 && nsimage_size.height == 0.0)
64     {
65       [nsimage release];
66       g_critical ("%s returned a zero-sized image", G_STRFUNC);
67       return NULL;
68     }
69   [nsimage lockFocus];
70
71   context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
72   CGContextDrawImage (context, CGRectMake (0, 0, pixbuf_width, pixbuf_height), image);
73  
74   [nsimage unlockFocus];
75
76   CGImageRelease (image);
77
78   return nsimage;
79 }
80
81 static NSString *
82 target_to_pasteboard_type (const char *target)
83 {
84   if (strcmp (target, "UTF8_STRING") == 0)
85     return NSStringPboardType;
86   else if (strcmp (target, "image/tiff") == 0)
87     return NSTIFFPboardType;
88   else if (strcmp (target, "application/x-color") == 0)
89     return NSColorPboardType;
90   else if (strcmp (target, "text/uri-list") == 0)
91     return NSURLPboardType;
92   else
93     return [NSString stringWithUTF8String:target];
94 }
95
96 NSSet *
97 _gtk_quartz_target_list_to_pasteboard_types (GtkTargetList *target_list)
98 {
99   NSMutableSet *set = [[NSMutableSet alloc] init];
100   GList *list;
101
102   for (list = target_list->list; list; list = list->next)
103     {
104       GtkTargetPair *pair = list->data;
105       gchar *target = gdk_atom_name (pair->target);
106       [set addObject:target_to_pasteboard_type (target)];
107       g_free (target);
108     }
109
110   return set;
111 }
112
113 NSSet *
114 _gtk_quartz_target_entries_to_pasteboard_types (const GtkTargetEntry *targets,
115                                                 guint                 n_targets)
116 {
117   NSMutableSet *set = [[NSMutableSet alloc] init];
118   int i;
119
120   for (i = 0; i < n_targets; i++)
121     {
122       [set addObject:target_to_pasteboard_type (targets[i].target)];
123     }
124
125   return set;
126 }
127
128 GdkAtom 
129 _gtk_quartz_pasteboard_type_to_atom (NSString *type)
130 {
131   if ([type isEqualToString:NSStringPboardType])
132     return gdk_atom_intern_static_string ("UTF8_STRING");
133   else if ([type isEqualToString:NSTIFFPboardType])
134     return gdk_atom_intern_static_string ("image/tiff");
135   else if ([type isEqualToString:NSColorPboardType])
136     return gdk_atom_intern_static_string ("application/x-color");
137   else if ([type isEqualToString:NSURLPboardType])
138     return gdk_atom_intern_static_string ("text/uri-list");
139   else
140     return gdk_atom_intern ([type UTF8String], FALSE);  
141 }
142
143 GList *
144 _gtk_quartz_pasteboard_types_to_atom_list (NSArray *array)
145 {
146   GList *result = NULL;
147   int i;
148   int count;
149
150   count = [array count];
151
152   for (i = 0; i < count; i++) 
153     {
154       GdkAtom atom = _gtk_quartz_pasteboard_type_to_atom ([array objectAtIndex:i]);
155
156       result = g_list_prepend (result, GDK_ATOM_TO_POINTER (atom));
157     }
158
159   return result;
160 }
161
162 GtkSelectionData *
163 _gtk_quartz_get_selection_data_from_pasteboard (NSPasteboard *pasteboard,
164                                                 GdkAtom       target,
165                                                 GdkAtom       selection)
166 {
167   GtkSelectionData *selection_data = NULL;
168
169   selection_data = g_slice_new0 (GtkSelectionData);
170   selection_data->selection = selection;
171   selection_data->target = target;
172   if (!selection_data->display)
173     selection_data->display = gdk_display_get_default ();
174   if (target == gdk_atom_intern_static_string ("UTF8_STRING"))
175     {
176       NSString *s = [pasteboard stringForType:NSStringPboardType];
177
178       if (s)
179         {
180           const char *utf8_string = [s UTF8String];
181
182           gtk_selection_data_set (selection_data,
183                                   target, 8,
184                                   (guchar *)utf8_string, strlen (utf8_string));
185         }
186     }
187   else if (target == gdk_atom_intern_static_string ("application/x-color"))
188     {
189       NSColor *nscolor = [[NSColor colorFromPasteboard:pasteboard]
190                           colorUsingColorSpaceName:NSDeviceRGBColorSpace];
191       
192       guint16 color[4];
193       
194       selection_data->target = target;
195
196       color[0] = 0xffff * [nscolor redComponent];
197       color[1] = 0xffff * [nscolor greenComponent];
198       color[2] = 0xffff * [nscolor blueComponent];
199       color[3] = 0xffff * [nscolor alphaComponent];
200
201       gtk_selection_data_set (selection_data, target, 16, (guchar *)color, 8);
202     }
203   else if (target == gdk_atom_intern_static_string ("text/uri-list"))
204     {
205       if ([[pasteboard types] containsObject:NSFilenamesPboardType])
206         {
207            gchar **uris;
208            NSArray *files = [pasteboard propertyListForType:NSFilenamesPboardType];
209            int n_files = [files count];
210            int i;
211
212            selection_data->target = gdk_atom_intern_static_string ("text/uri-list");
213
214            uris = (gchar **) g_malloc (sizeof (gchar*) * (n_files + 1));
215            for (i = 0; i < n_files; ++i)
216              {
217                NSString* uriString = [files objectAtIndex:i];
218                uriString = [@"file://" stringByAppendingString:uriString];
219                uriString = [uriString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
220                uris[i] = (gchar *) [uriString cStringUsingEncoding:NSUTF8StringEncoding];
221              }
222            uris[i] = NULL;
223
224            gtk_selection_data_set_uris (selection_data, uris);
225            g_free (uris);
226          }
227       else if ([[pasteboard types] containsObject:NSURLPboardType])
228         {
229           gchar *uris[2];
230           NSURL *url = [NSURL URLFromPasteboard:pasteboard];
231
232           selection_data->target = gdk_atom_intern_static_string ("text/uri-list");
233
234           uris[0] = (gchar *) [[url description] UTF8String];
235
236           uris[1] = NULL;
237           gtk_selection_data_set_uris (selection_data, uris);
238         }
239     }
240   else
241     {
242       NSData *data;
243       gchar *name;
244
245       name = gdk_atom_name (target);
246
247       if (strcmp (name, "image/tiff") == 0)
248         data = [pasteboard dataForType:NSTIFFPboardType];
249       else
250         data = [pasteboard dataForType:[NSString stringWithUTF8String:name]];
251
252       g_free (name);
253
254       if (data)
255         {
256           gtk_selection_data_set (selection_data,
257                                   target, 8,
258                                   [data bytes], [data length]);
259         }
260     }
261
262   return selection_data;
263 }
264
265 void
266 _gtk_quartz_set_selection_data_for_pasteboard (NSPasteboard     *pasteboard,
267                                                GtkSelectionData *selection_data)
268 {
269   NSString *type;
270   gchar *target;
271   GdkDisplay *display;
272   gint format;
273   const guchar *data;
274   NSUInteger length;
275
276   target = gdk_atom_name (gtk_selection_data_get_target (selection_data));
277   display = gtk_selection_data_get_display (selection_data);
278   format = gtk_selection_data_get_format (selection_data);
279   data = gtk_selection_data_get_data (selection_data);
280   length = gtk_selection_data_get_length (selection_data);
281
282   type = target_to_pasteboard_type (target);
283   g_free (target);
284
285   if ([type isEqualTo:NSStringPboardType]) 
286     [pasteboard setString:[NSString stringWithUTF8String:(const char *)data]
287                   forType:type];
288   else if ([type isEqualTo:NSColorPboardType])
289     {
290       guint16 *color = (guint16 *)data;
291       float red, green, blue, alpha;
292       NSColor *nscolor;
293
294       red   = (float)color[0] / 0xffff;
295       green = (float)color[1] / 0xffff;
296       blue  = (float)color[2] / 0xffff;
297       alpha = (float)color[3] / 0xffff;
298
299       nscolor = [NSColor colorWithDeviceRed:red green:green blue:blue alpha:alpha];
300       [nscolor writeToPasteboard:pasteboard];
301     }
302   else if ([type isEqualTo:NSURLPboardType])
303     {
304       gchar **uris;
305
306       uris = gtk_selection_data_get_uris (selection_data);
307       if (uris != NULL)
308         {
309           NSURL *url;
310
311           url = [NSURL URLWithString:[NSString stringWithUTF8String:uris[0]]];
312           [url writeToPasteboard:pasteboard];
313         }
314       g_strfreev (uris);
315     }
316   else
317     [pasteboard setData:[NSData dataWithBytesNoCopy:(void *)data
318                                              length:length
319                                        freeWhenDone:NO]
320                                             forType:type];
321 }
322
323 #ifdef QUARTZ_RELOCATION
324
325 /* Bundle-based functions for various directories. These almost work
326  * even when the application isn't in a bundle, becuase mainBundle
327  * paths point to the bin directory in that case. It's a simple matter
328  * to test for that and remove the last element.
329  */
330
331 static const gchar *
332 get_bundle_path (void)
333 {
334   static gchar *path = NULL;
335
336   if (path == NULL)
337     {
338       NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
339       gchar *resource_path = g_strdup ([[[NSBundle mainBundle] resourcePath] UTF8String]);
340       gchar *base;
341       [pool drain];
342
343       base = g_path_get_basename (resource_path);
344       if (strcmp (base, "bin") == 0)
345         path = g_path_get_dirname (resource_path);
346       else
347         path = strdup (resource_path);
348
349       g_free (resource_path);
350       g_free (base);
351     }
352
353   return path;
354 }
355
356 const gchar *
357 _gtk_get_datadir (void)
358 {
359   static gchar *path = NULL;
360
361   if (path == NULL)
362     path = g_build_filename (get_bundle_path (), "share", NULL);
363
364   return path;
365 }
366
367 const gchar *
368 _gtk_get_libdir (void)
369 {
370   static gchar *path = NULL;
371
372   if (path == NULL)
373     path = g_build_filename (get_bundle_path (), "lib", NULL);
374
375   return path;
376 }
377
378 const gchar *
379 _gtk_get_localedir (void)
380 {
381   static gchar *path = NULL;
382
383   if (path == NULL)
384     path = g_build_filename (get_bundle_path (), "share", "locale", NULL);
385
386   return path;
387 }
388
389 const gchar *
390 _gtk_get_sysconfdir (void)
391 {
392   static gchar *path = NULL;
393
394   if (path == NULL)
395     path = g_build_filename (get_bundle_path (), "etc", NULL);
396
397   return path;
398 }
399
400 const gchar *
401 _gtk_get_data_prefix (void)
402 {
403   return get_bundle_path ();
404 }
405
406 #endif /* QUARTZ_RELOCATION */