]> Pileus Git - ~andy/gtk/blob - gtk/gtkquartz.c
Rework a previous commit to not crash in addition to fixing leaks
[~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 "gtkalias.h"
25
26 NSImage *
27 _gtk_quartz_create_image_from_pixbuf (GdkPixbuf *pixbuf)
28 {
29   CGColorSpaceRef colorspace;
30   CGDataProviderRef data_provider;
31   CGContextRef context;
32   CGImageRef image;
33   void *data;
34   int rowstride, pixbuf_width, pixbuf_height;
35   gboolean has_alpha;
36   NSImage *nsimage;
37
38   pixbuf_width = gdk_pixbuf_get_width (pixbuf);
39   pixbuf_height = gdk_pixbuf_get_height (pixbuf);
40   rowstride = gdk_pixbuf_get_rowstride (pixbuf);
41   has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
42
43   data = gdk_pixbuf_get_pixels (pixbuf);
44
45   colorspace = CGColorSpaceCreateDeviceRGB ();
46   data_provider = CGDataProviderCreateWithData (NULL, data, pixbuf_height * rowstride, NULL);
47
48   image = CGImageCreate (pixbuf_width, pixbuf_height, 8,
49                          has_alpha ? 32 : 24, rowstride, 
50                          colorspace, 
51                          has_alpha ? kCGImageAlphaLast : 0,
52                          data_provider, NULL, FALSE, 
53                          kCGRenderingIntentDefault);
54
55   CGDataProviderRelease (data_provider);
56   CGColorSpaceRelease (colorspace);
57
58   nsimage = [[NSImage alloc] initWithSize:NSMakeSize (pixbuf_width, pixbuf_height)];
59   [nsimage lockFocus];
60
61   context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
62   CGContextDrawImage (context, CGRectMake (0, 0, pixbuf_width, pixbuf_height), image);
63  
64   [nsimage unlockFocus];
65
66   CGImageRelease (image);
67
68   return nsimage;
69 }
70
71 static NSString *
72 target_to_pasteboard_type (const char *target)
73 {
74   if (strcmp (target, "UTF8_STRING") == 0)
75     return NSStringPboardType;
76   else if (strcmp (target, "image/tiff") == 0)
77     return NSTIFFPboardType;
78   else if (strcmp (target, "application/x-color") == 0)
79     return NSColorPboardType;
80   else if (strcmp (target, "text/uri-list") == 0)
81     return NSURLPboardType;
82   else
83     return [NSString stringWithUTF8String:target];
84 }
85
86 NSSet *
87 _gtk_quartz_target_list_to_pasteboard_types (GtkTargetList *target_list)
88 {
89   NSMutableSet *set = [[NSMutableSet alloc] init];
90   GList *list;
91
92   for (list = target_list->list; list; list = list->next)
93     {
94       GtkTargetPair *pair = list->data;
95       gchar *target = gdk_atom_name (pair->target);
96       [set addObject:target_to_pasteboard_type (target)];
97       g_free (target);
98     }
99
100   return set;
101 }
102
103 NSSet *
104 _gtk_quartz_target_entries_to_pasteboard_types (const GtkTargetEntry *targets,
105                                                 guint                 n_targets)
106 {
107   NSMutableSet *set = [[NSMutableSet alloc] init];
108   int i;
109
110   for (i = 0; i < n_targets; i++)
111     {
112       [set addObject:target_to_pasteboard_type (targets[i].target)];
113     }
114
115   return set;
116 }
117
118 GdkAtom 
119 _gtk_quartz_pasteboard_type_to_atom (NSString *type)
120 {
121   if ([type isEqualToString:NSStringPboardType])
122     return gdk_atom_intern_static_string ("UTF8_STRING");
123   else if ([type isEqualToString:NSTIFFPboardType])
124     return gdk_atom_intern_static_string ("image/tiff");
125   else if ([type isEqualToString:NSColorPboardType])
126     return gdk_atom_intern_static_string ("application/x-color");
127   else if ([type isEqualToString:NSURLPboardType])
128     return gdk_atom_intern_static_string ("text/uri-list");
129   else
130     return gdk_atom_intern ([type UTF8String], FALSE);  
131 }
132
133 GList *
134 _gtk_quartz_pasteboard_types_to_atom_list (NSArray *array)
135 {
136   GList *result = NULL;
137   int i;
138   int count;
139
140   count = [array count];
141
142   for (i = 0; i < count; i++) 
143     {
144       GdkAtom atom = _gtk_quartz_pasteboard_type_to_atom ([array objectAtIndex:i]);
145
146       result = g_list_prepend (result, GDK_ATOM_TO_POINTER (atom));
147     }
148
149   return result;
150 }
151
152 GtkSelectionData *
153 _gtk_quartz_get_selection_data_from_pasteboard (NSPasteboard *pasteboard,
154                                                 GdkAtom       target,
155                                                 GdkAtom       selection)
156 {
157   GtkSelectionData *selection_data = NULL;
158
159   selection_data = g_slice_new0 (GtkSelectionData);
160   selection_data->selection = selection;
161   selection_data->target = target;
162
163   if (target == gdk_atom_intern_static_string ("UTF8_STRING"))
164     {
165       NSString *s = [pasteboard stringForType:NSStringPboardType];
166
167       if (s)
168         {
169           const char *utf8_string = [s UTF8String];
170
171           gtk_selection_data_set (selection_data,
172                                   target, 8,
173                                   (guchar *)utf8_string, strlen (utf8_string));
174         }
175     }
176   else if (target == gdk_atom_intern_static_string ("application/x-color"))
177     {
178       NSColor *nscolor = [[NSColor colorFromPasteboard:pasteboard]
179                           colorUsingColorSpaceName:NSDeviceRGBColorSpace];
180       
181       guint16 color[4];
182       
183       selection_data->target = target;
184
185       color[0] = 0xffff * [nscolor redComponent];
186       color[1] = 0xffff * [nscolor greenComponent];
187       color[2] = 0xffff * [nscolor blueComponent];
188       color[3] = 0xffff * [nscolor alphaComponent];
189
190       gtk_selection_data_set (selection_data, target, 16, (guchar *)color, 8);
191     }
192   else if (target == gdk_atom_intern_static_string ("text/uri-list"))
193     {
194       if ([[pasteboard types] containsObject:NSFilenamesPboardType])
195         {
196            gchar **uris;
197            NSArray *files = [pasteboard propertyListForType:NSFilenamesPboardType];
198            int n_files = [files count];
199            int i;
200
201            selection_data->target = gdk_atom_intern_static_string ("text/uri-list");
202
203            uris = (gchar **) g_malloc (sizeof (gchar*) * (n_files + 1));
204            for (i = 0; i < n_files; ++i)
205              {
206                NSString* uriString = [files objectAtIndex:i];
207                uriString = [@"file://" stringByAppendingString:uriString];
208                uriString = [uriString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
209                uris[i] = (gchar *) [uriString cStringUsingEncoding:NSUTF8StringEncoding];
210              }
211            uris[i] = NULL;
212
213            gtk_selection_data_set_uris (selection_data, uris);
214            g_free (uris);
215          }
216       else if ([[pasteboard types] containsObject:NSURLPboardType])
217         {
218           gchar *uris[2];
219           NSURL *url = [NSURL URLFromPasteboard:pasteboard];
220
221           selection_data->target = gdk_atom_intern_static_string ("text/uri-list");
222
223           uris[0] = (gchar *) [[url description] UTF8String];
224
225           uris[1] = NULL;
226           gtk_selection_data_set_uris (selection_data, uris);
227         }
228     }
229   else
230     {
231       NSData *data;
232       gchar *name;
233
234       name = gdk_atom_name (target);
235
236       if (strcmp (name, "image/tiff") == 0)
237         data = [pasteboard dataForType:NSTIFFPboardType];
238       else
239         data = [pasteboard dataForType:[NSString stringWithUTF8String:name]];
240
241       g_free (name);
242
243       if (data)
244         {
245           gtk_selection_data_set (selection_data,
246                                   target, 8,
247                                   [data bytes], [data length]);
248         }
249     }
250
251   return selection_data;
252 }
253
254 void
255 _gtk_quartz_set_selection_data_for_pasteboard (NSPasteboard     *pasteboard,
256                                                GtkSelectionData *selection_data)
257 {
258   NSString *type;
259   gchar *target;
260   GdkDisplay *display;
261   gint format;
262   const guchar *data;
263   NSUInteger length;
264
265   target = gdk_atom_name (gtk_selection_data_get_target (selection_data));
266   display = gtk_selection_data_get_display (selection_data);
267   format = gtk_selection_data_get_format (selection_data);
268   data = gtk_selection_data_get_data (selection_data);
269   length = gtk_selection_data_get_length (selection_data);
270
271   type = target_to_pasteboard_type (target);
272   g_free (target);
273
274   if ([type isEqualTo:NSStringPboardType]) 
275     [pasteboard setString:[NSString stringWithUTF8String:(const char *)data]
276                   forType:type];
277   else if ([type isEqualTo:NSColorPboardType])
278     {
279       guint16 *color = (guint16 *)data;
280       float red, green, blue, alpha;
281       NSColor *nscolor;
282
283       red   = (float)color[0] / 0xffff;
284       green = (float)color[1] / 0xffff;
285       blue  = (float)color[2] / 0xffff;
286       alpha = (float)color[3] / 0xffff;
287
288       nscolor = [NSColor colorWithDeviceRed:red green:green blue:blue alpha:alpha];
289       [nscolor writeToPasteboard:pasteboard];
290     }
291   else if ([type isEqualTo:NSURLPboardType])
292     {
293       gchar **list = NULL;
294       int count;
295
296       count = gdk_text_property_to_utf8_list_for_display (display,
297                                                           gdk_atom_intern_static_string ("UTF8_STRING"),
298                                                           format,
299                                                           data,
300                                                           length,
301                                                           &list);
302
303       if (count > 0)
304         {
305           gchar **result;
306           NSURL *url;
307
308           result = g_uri_list_extract_uris (list[0]);
309
310           url = [NSURL URLWithString:[NSString stringWithUTF8String:result[0]]];
311           [url writeToPasteboard:pasteboard];
312
313           g_strfreev (result);
314         }
315
316       g_strfreev (list);
317     }
318   else
319     [pasteboard setData:[NSData dataWithBytesNoCopy:(void *)data
320                                              length:length
321                                        freeWhenDone:NO]
322                                             forType:type];
323 }