]> Pileus Git - ~andy/gtk/blob - gtk/gtkquartz.c
Use GSlice for GtkSelectionData. (#515060, Christian Persch)
[~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 NSArray *
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 allObjects];
101 }
102
103 NSArray *
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 allObjects];
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           int len = [s length];
170
171           selection_data->type = target;
172           selection_data->format = 8;
173           selection_data->length = len;
174           selection_data->data = g_memdup ([s UTF8String], len + 1);
175         }
176     }
177   else if (target == gdk_atom_intern_static_string ("application/x-color"))
178     {
179       NSColor *nscolor = [[NSColor colorFromPasteboard:pasteboard]
180                           colorUsingColorSpaceName:NSDeviceRGBColorSpace];
181       
182       guint16 color[4];
183       
184       selection_data->target = target;
185
186       color[0] = 0xffff * [nscolor redComponent];
187       color[1] = 0xffff * [nscolor greenComponent];
188       color[2] = 0xffff * [nscolor blueComponent];
189       color[3] = 0xffff * [nscolor alphaComponent];
190
191       gtk_selection_data_set (selection_data, target, 16, (guchar *)color, 8);
192     }
193   else if (target == gdk_atom_intern_static_string ("text/uri-list"))
194     {
195       if ([[pasteboard types] containsObject:NSFilenamesPboardType])
196         {
197            gchar **uris;
198            NSArray *files = [pasteboard propertyListForType:NSFilenamesPboardType];
199            int n_files = [files count];
200            int i;
201
202            selection_data->target = gdk_atom_intern_static_string ("text/uri-list");
203
204            uris = (gchar **) g_malloc (sizeof (gchar*) * (n_files + 1));
205            for (i = 0; i < n_files; ++i)
206              {
207                NSString* uriString = [files objectAtIndex:i];
208                uriString = [@"file://" stringByAppendingString:uriString];
209                uriString = [uriString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
210                uris[i] = (gchar *) [uriString cStringUsingEncoding:NSUTF8StringEncoding];
211              }
212            uris[i] = NULL;
213
214            gtk_selection_data_set_uris (selection_data, uris);
215            g_free (uris);
216          }
217       else if ([[pasteboard types] containsObject:NSURLPboardType])
218         {
219           gchar *uris[2];
220           NSURL *url = [NSURL URLFromPasteboard:pasteboard];
221
222           selection_data->target = gdk_atom_intern_static_string ("text/uri-list");
223
224           uris[0] = (gchar *) [[url description] UTF8String];
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           selection_data->type = target;
246           selection_data->format = 8;
247           selection_data->length = [data length];
248           selection_data->data = g_malloc (selection_data->length + 1);
249           selection_data->data[selection_data->length] = '\0';
250           memcpy(selection_data->data, [data bytes], selection_data->length);
251         }
252     }
253
254   return selection_data;
255 }
256
257 void
258 _gtk_quartz_set_selection_data_for_pasteboard (NSPasteboard *pasteboard,
259                                                GtkSelectionData *selection_data)
260 {
261   NSString *type;
262   gchar *target = gdk_atom_name (selection_data->target);
263
264   type = target_to_pasteboard_type (target);
265   g_free (target);
266   
267   if ([type isEqualTo:NSStringPboardType]) 
268     [pasteboard setString:[NSString stringWithUTF8String:(const char *)selection_data->data]
269                   forType:type];
270   else if ([type isEqualTo:NSColorPboardType])
271     {
272       guint16 *color = (guint16 *)selection_data->data;
273       float red, green, blue, alpha;
274
275       red   = (float)color[0] / 0xffff;
276       green = (float)color[1] / 0xffff;
277       blue  = (float)color[2] / 0xffff;
278       alpha = (float)color[3] / 0xffff;
279       
280       NSColor *nscolor = [NSColor colorWithDeviceRed:red green:green blue:blue alpha:alpha];
281
282       [nscolor writeToPasteboard:pasteboard];
283     }
284   else if ([type isEqualTo:NSURLPboardType])
285     {
286       gchar **list;
287       gchar **result = NULL;
288       NSURL *url;
289
290       int count = gdk_text_property_to_utf8_list_for_display (selection_data->display,
291                                                               gdk_atom_intern_static_string ("UTF8_STRING"),
292                                                               selection_data->format,
293                                                               selection_data->data,
294                                                               selection_data->length,
295                                                               &list);
296
297       if (count > 0)
298         {
299           result = g_uri_list_extract_uris (list[0]);
300
301           url = [NSURL URLWithString:[NSString stringWithUTF8String:result[0]]];
302           [url writeToPasteboard:pasteboard];
303
304           g_strfreev (result);
305         }
306
307       g_strfreev (list);
308     }
309   else
310     [pasteboard setData:[NSData dataWithBytesNoCopy:selection_data->data
311                                              length:selection_data->length
312                                        freeWhenDone:NO]
313                 forType:type];
314 }