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