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