]> Pileus Git - ~andy/gtk/blob - gtk/gtkquartz.c
Fix compiler warning
[~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   NSArray *ret;
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   ret = [set allObjects];
102
103   [set release];
104
105   return ret;
106 }
107
108 NSArray *
109 _gtk_quartz_target_entries_to_pasteboard_types (const GtkTargetEntry *targets,
110                                                 guint                 n_targets)
111 {
112   NSMutableSet *set = [[NSMutableSet alloc] init];
113   NSArray *ret;
114   int i;
115
116   for (i = 0; i < n_targets; i++)
117     {
118       [set addObject:target_to_pasteboard_type (targets[i].target)];
119     }
120
121   ret = [set allObjects];
122
123   [set release];
124
125   return ret;
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
173   if (target == gdk_atom_intern_static_string ("UTF8_STRING"))
174     {
175       NSString *s = [pasteboard stringForType:NSStringPboardType];
176
177       if (s)
178         {
179           const char *utf8_string = [s UTF8String];
180
181           gtk_selection_data_set (selection_data,
182                                   target, 8,
183                                   (guchar *)utf8_string, strlen (utf8_string));
184         }
185     }
186   else if (target == gdk_atom_intern_static_string ("application/x-color"))
187     {
188       NSColor *nscolor = [[NSColor colorFromPasteboard:pasteboard]
189                           colorUsingColorSpaceName:NSDeviceRGBColorSpace];
190       
191       guint16 color[4];
192       
193       selection_data->target = target;
194
195       color[0] = 0xffff * [nscolor redComponent];
196       color[1] = 0xffff * [nscolor greenComponent];
197       color[2] = 0xffff * [nscolor blueComponent];
198       color[3] = 0xffff * [nscolor alphaComponent];
199
200       gtk_selection_data_set (selection_data, target, 16, (guchar *)color, 8);
201     }
202   else if (target == gdk_atom_intern_static_string ("text/uri-list"))
203     {
204       if ([[pasteboard types] containsObject:NSFilenamesPboardType])
205         {
206            gchar **uris;
207            NSArray *files = [pasteboard propertyListForType:NSFilenamesPboardType];
208            int n_files = [files count];
209            int i;
210
211            selection_data->target = gdk_atom_intern_static_string ("text/uri-list");
212
213            uris = (gchar **) g_malloc (sizeof (gchar*) * (n_files + 1));
214            for (i = 0; i < n_files; ++i)
215              {
216                NSString* uriString = [files objectAtIndex:i];
217                uriString = [@"file://" stringByAppendingString:uriString];
218                uriString = [uriString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
219                uris[i] = (gchar *) [uriString cStringUsingEncoding:NSUTF8StringEncoding];
220              }
221            uris[i] = NULL;
222
223            gtk_selection_data_set_uris (selection_data, uris);
224            g_free (uris);
225          }
226       else if ([[pasteboard types] containsObject:NSURLPboardType])
227         {
228           gchar *uris[2];
229           NSURL *url = [NSURL URLFromPasteboard:pasteboard];
230
231           selection_data->target = gdk_atom_intern_static_string ("text/uri-list");
232
233           uris[0] = (gchar *) [[url description] UTF8String];
234
235           uris[1] = NULL;
236           gtk_selection_data_set_uris (selection_data, uris);
237         }
238     }
239   else
240     {
241       NSData *data;
242       gchar *name;
243
244       name = gdk_atom_name (target);
245
246       if (strcmp (name, "image/tiff") == 0)
247         data = [pasteboard dataForType:NSTIFFPboardType];
248       else
249         data = [pasteboard dataForType:[NSString stringWithUTF8String:name]];
250
251       g_free (name);
252
253       if (data)
254         {
255           gtk_selection_data_set (selection_data,
256                                   target, 8,
257                                   [data bytes], [data length]);
258         }
259     }
260
261   return selection_data;
262 }
263
264 void
265 _gtk_quartz_set_selection_data_for_pasteboard (NSPasteboard     *pasteboard,
266                                                GtkSelectionData *selection_data)
267 {
268   NSString *type;
269   gchar *target;
270   GdkDisplay *display;
271   gint format;
272   const guchar *data;
273   NSUInteger length;
274
275   target = gdk_atom_name (gtk_selection_data_get_target (selection_data));
276   display = gtk_selection_data_get_display (selection_data);
277   format = gtk_selection_data_get_format (selection_data);
278   data = gtk_selection_data_get_data (selection_data);
279   length = gtk_selection_data_get_length (selection_data);
280
281   type = target_to_pasteboard_type (target);
282   g_free (target);
283
284   if ([type isEqualTo:NSStringPboardType]) 
285     [pasteboard setString:[NSString stringWithUTF8String:(const char *)data]
286                   forType:type];
287   else if ([type isEqualTo:NSColorPboardType])
288     {
289       guint16 *color = (guint16 *)data;
290       float red, green, blue, alpha;
291       NSColor *nscolor;
292
293       red   = (float)color[0] / 0xffff;
294       green = (float)color[1] / 0xffff;
295       blue  = (float)color[2] / 0xffff;
296       alpha = (float)color[3] / 0xffff;
297
298       nscolor = [NSColor colorWithDeviceRed:red green:green blue:blue alpha:alpha];
299       [nscolor writeToPasteboard:pasteboard];
300     }
301   else if ([type isEqualTo:NSURLPboardType])
302     {
303       gchar **list = NULL;
304       int count;
305
306       count = gdk_text_property_to_utf8_list_for_display (display,
307                                                           gdk_atom_intern_static_string ("UTF8_STRING"),
308                                                           format,
309                                                           data,
310                                                           length,
311                                                           &list);
312
313       if (count > 0)
314         {
315           gchar **result;
316           NSURL *url;
317
318           result = g_uri_list_extract_uris (list[0]);
319
320           url = [NSURL URLWithString:[NSString stringWithUTF8String:result[0]]];
321           [url writeToPasteboard:pasteboard];
322
323           g_strfreev (result);
324         }
325
326       g_strfreev (list);
327     }
328   else
329     [pasteboard setData:[NSData dataWithBytesNoCopy:(void *)data
330                                              length:length
331                                        freeWhenDone:NO]
332                                             forType:type];
333 }