1 /* gtkquartz.c: Utility functions used by the Quartz port
3 * Copyright (C) 2006 Imendio AB
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.
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.
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.
23 #include "gtkquartz.h"
24 #include "gtkselectionprivate.h"
25 #include <gdk/quartz/gdkquartz.h>
28 _gtk_quartz_create_image_from_pixbuf (GdkPixbuf *pixbuf)
30 CGColorSpaceRef colorspace;
31 CGDataProviderRef data_provider;
35 int rowstride, pixbuf_width, pixbuf_height;
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);
44 data = gdk_pixbuf_get_pixels (pixbuf);
46 colorspace = CGColorSpaceCreateDeviceRGB ();
47 data_provider = CGDataProviderCreateWithData (NULL, data, pixbuf_height * rowstride, NULL);
49 image = CGImageCreate (pixbuf_width, pixbuf_height, 8,
50 has_alpha ? 32 : 24, rowstride,
52 has_alpha ? kCGImageAlphaLast : 0,
53 data_provider, NULL, FALSE,
54 kCGRenderingIntentDefault);
56 CGDataProviderRelease (data_provider);
57 CGColorSpaceRelease (colorspace);
59 nsimage = [[NSImage alloc] initWithSize:NSMakeSize (pixbuf_width, pixbuf_height)];
62 context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
63 CGContextDrawImage (context, CGRectMake (0, 0, pixbuf_width, pixbuf_height), image);
65 [nsimage unlockFocus];
67 CGImageRelease (image);
73 target_to_pasteboard_type (const char *target)
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;
84 return [NSString stringWithUTF8String:target];
88 _gtk_quartz_target_list_to_pasteboard_types (GtkTargetList *target_list)
90 NSMutableSet *set = [[NSMutableSet alloc] init];
93 for (list = target_list->list; list; list = list->next)
95 GtkTargetPair *pair = list->data;
96 gchar *target = gdk_atom_name (pair->target);
97 [set addObject:target_to_pasteboard_type (target)];
105 _gtk_quartz_target_entries_to_pasteboard_types (const GtkTargetEntry *targets,
108 NSMutableSet *set = [[NSMutableSet alloc] init];
111 for (i = 0; i < n_targets; i++)
113 [set addObject:target_to_pasteboard_type (targets[i].target)];
120 _gtk_quartz_pasteboard_type_to_atom (NSString *type)
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");
131 return gdk_atom_intern ([type UTF8String], FALSE);
135 _gtk_quartz_pasteboard_types_to_atom_list (NSArray *array)
137 GList *result = NULL;
141 count = [array count];
143 for (i = 0; i < count; i++)
145 GdkAtom atom = _gtk_quartz_pasteboard_type_to_atom ([array objectAtIndex:i]);
147 result = g_list_prepend (result, GDK_ATOM_TO_POINTER (atom));
154 _gtk_quartz_get_selection_data_from_pasteboard (NSPasteboard *pasteboard,
158 GtkSelectionData *selection_data = NULL;
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"))
167 NSString *s = [pasteboard stringForType:NSStringPboardType];
171 const char *utf8_string = [s UTF8String];
173 gtk_selection_data_set (selection_data,
175 (guchar *)utf8_string, strlen (utf8_string));
178 else if (target == gdk_atom_intern_static_string ("application/x-color"))
180 NSColor *nscolor = [[NSColor colorFromPasteboard:pasteboard]
181 colorUsingColorSpaceName:NSDeviceRGBColorSpace];
185 selection_data->target = target;
187 color[0] = 0xffff * [nscolor redComponent];
188 color[1] = 0xffff * [nscolor greenComponent];
189 color[2] = 0xffff * [nscolor blueComponent];
190 color[3] = 0xffff * [nscolor alphaComponent];
192 gtk_selection_data_set (selection_data, target, 16, (guchar *)color, 8);
194 else if (target == gdk_atom_intern_static_string ("text/uri-list"))
196 if ([[pasteboard types] containsObject:NSFilenamesPboardType])
199 NSArray *files = [pasteboard propertyListForType:NSFilenamesPboardType];
200 int n_files = [files count];
203 selection_data->target = gdk_atom_intern_static_string ("text/uri-list");
205 uris = (gchar **) g_malloc (sizeof (gchar*) * (n_files + 1));
206 for (i = 0; i < n_files; ++i)
208 NSString* uriString = [files objectAtIndex:i];
209 uriString = [@"file://" stringByAppendingString:uriString];
210 uriString = [uriString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
211 uris[i] = (gchar *) [uriString cStringUsingEncoding:NSUTF8StringEncoding];
215 gtk_selection_data_set_uris (selection_data, uris);
218 else if ([[pasteboard types] containsObject:NSURLPboardType])
221 NSURL *url = [NSURL URLFromPasteboard:pasteboard];
223 selection_data->target = gdk_atom_intern_static_string ("text/uri-list");
225 uris[0] = (gchar *) [[url description] UTF8String];
228 gtk_selection_data_set_uris (selection_data, uris);
236 name = gdk_atom_name (target);
238 if (strcmp (name, "image/tiff") == 0)
239 data = [pasteboard dataForType:NSTIFFPboardType];
241 data = [pasteboard dataForType:[NSString stringWithUTF8String:name]];
247 gtk_selection_data_set (selection_data,
249 [data bytes], [data length]);
253 return selection_data;
257 _gtk_quartz_set_selection_data_for_pasteboard (NSPasteboard *pasteboard,
258 GtkSelectionData *selection_data)
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);
273 type = target_to_pasteboard_type (target);
276 if ([type isEqualTo:NSStringPboardType])
277 [pasteboard setString:[NSString stringWithUTF8String:(const char *)data]
279 else if ([type isEqualTo:NSColorPboardType])
281 guint16 *color = (guint16 *)data;
282 float red, green, blue, alpha;
285 red = (float)color[0] / 0xffff;
286 green = (float)color[1] / 0xffff;
287 blue = (float)color[2] / 0xffff;
288 alpha = (float)color[3] / 0xffff;
290 nscolor = [NSColor colorWithDeviceRed:red green:green blue:blue alpha:alpha];
291 [nscolor writeToPasteboard:pasteboard];
293 else if ([type isEqualTo:NSURLPboardType])
297 uris = gtk_selection_data_get_uris (selection_data);
302 url = [NSURL URLWithString:[NSString stringWithUTF8String:uris[0]]];
303 [url writeToPasteboard:pasteboard];
308 [pasteboard setData:[NSData dataWithBytesNoCopy:(void *)data
315 * Bundle-based functions for various directories. These almost work
316 * even when the application isn't in a bundle, becuase mainBundle
317 * paths point to the bin directory in that case. It's a simple matter
318 * to test for that and remove the last element.
325 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
326 gchar *resource_path = g_strdup([[[NSBundle mainBundle] resourcePath] UTF8String]);
328 base = g_path_get_basename(resource_path);
329 if (strcmp(base, "bin") == 0)
330 path = g_path_get_dirname(resource_path);
332 path = strdup(resource_path);
333 g_free(resource_path);
339 _gtk_get_datadir (void)
341 gchar *resource_dir = get_bundle_path();
342 gchar *retval = g_build_filename(resource_dir, "share", NULL);
343 g_free(resource_dir);
348 _gtk_get_libdir (void)
350 gchar *resource_dir = get_bundle_path();
351 gchar *retval = g_build_filename(resource_dir, "lib", NULL);
352 g_free(resource_dir);
357 _gtk_get_localedir (void)
360 gchar *resource_dir = get_bundle_path();
361 gchar *retval = g_build_filename(resource_dir, "share", "locale", NULL);
362 g_free(resource_dir);
367 _gtk_get_sysconfdir (void)
369 gchar *resource_dir = get_bundle_path();
370 gchar *retval = g_build_filename(resource_dir, "etc", NULL);
371 g_free(resource_dir);
376 _gtk_get_data_prefix (void)
378 return get_bundle_path();