2006-03-21 16:50:47 +00:00
|
|
|
/* gtkquartz.c: Utility functions used by the Quartz port
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Imendio AB
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2008-06-22 14:28:52 +00:00
|
|
|
#include "config.h"
|
2006-03-21 16:50:47 +00:00
|
|
|
|
|
|
|
#include "gtkquartz.h"
|
2010-12-22 14:44:58 +00:00
|
|
|
#include "gtkselectionprivate.h"
|
2010-06-27 08:32:12 +00:00
|
|
|
#include <gdk/quartz/gdkquartz.h>
|
2006-03-21 16:50:47 +00:00
|
|
|
|
|
|
|
NSImage *
|
|
|
|
_gtk_quartz_create_image_from_pixbuf (GdkPixbuf *pixbuf)
|
|
|
|
{
|
|
|
|
CGColorSpaceRef colorspace;
|
|
|
|
CGDataProviderRef data_provider;
|
|
|
|
CGContextRef context;
|
|
|
|
CGImageRef image;
|
|
|
|
void *data;
|
|
|
|
int rowstride, pixbuf_width, pixbuf_height;
|
|
|
|
gboolean has_alpha;
|
|
|
|
NSImage *nsimage;
|
2011-11-07 22:25:08 +00:00
|
|
|
NSSize nsimage_size;
|
2006-03-21 16:50:47 +00:00
|
|
|
|
|
|
|
pixbuf_width = gdk_pixbuf_get_width (pixbuf);
|
|
|
|
pixbuf_height = gdk_pixbuf_get_height (pixbuf);
|
2011-11-07 22:25:08 +00:00
|
|
|
g_return_val_if_fail (pixbuf_width == 0 || pixbuf_height == 0, NULL);
|
2006-03-21 16:50:47 +00:00
|
|
|
rowstride = gdk_pixbuf_get_rowstride (pixbuf);
|
|
|
|
has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
|
|
|
|
|
|
|
|
data = gdk_pixbuf_get_pixels (pixbuf);
|
|
|
|
|
|
|
|
colorspace = CGColorSpaceCreateDeviceRGB ();
|
|
|
|
data_provider = CGDataProviderCreateWithData (NULL, data, pixbuf_height * rowstride, NULL);
|
|
|
|
|
|
|
|
image = CGImageCreate (pixbuf_width, pixbuf_height, 8,
|
|
|
|
has_alpha ? 32 : 24, rowstride,
|
|
|
|
colorspace,
|
|
|
|
has_alpha ? kCGImageAlphaLast : 0,
|
|
|
|
data_provider, NULL, FALSE,
|
|
|
|
kCGRenderingIntentDefault);
|
|
|
|
|
|
|
|
CGDataProviderRelease (data_provider);
|
|
|
|
CGColorSpaceRelease (colorspace);
|
|
|
|
|
|
|
|
nsimage = [[NSImage alloc] initWithSize:NSMakeSize (pixbuf_width, pixbuf_height)];
|
2011-11-07 22:25:08 +00:00
|
|
|
nsimage_size = [nsimage size];
|
|
|
|
if (nsimage_size.width == 0.0 && nsimage_size.height == 0.0)
|
|
|
|
{
|
|
|
|
[nsimage release];
|
|
|
|
g_critical ("%s returned a zero-sized image", G_STRFUNC);
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-03-21 16:50:47 +00:00
|
|
|
[nsimage lockFocus];
|
|
|
|
|
|
|
|
context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
|
|
|
|
CGContextDrawImage (context, CGRectMake (0, 0, pixbuf_width, pixbuf_height), image);
|
|
|
|
|
|
|
|
[nsimage unlockFocus];
|
|
|
|
|
|
|
|
CGImageRelease (image);
|
|
|
|
|
|
|
|
return nsimage;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSString *
|
|
|
|
target_to_pasteboard_type (const char *target)
|
|
|
|
{
|
|
|
|
if (strcmp (target, "UTF8_STRING") == 0)
|
|
|
|
return NSStringPboardType;
|
|
|
|
else if (strcmp (target, "image/tiff") == 0)
|
|
|
|
return NSTIFFPboardType;
|
|
|
|
else if (strcmp (target, "application/x-color") == 0)
|
|
|
|
return NSColorPboardType;
|
|
|
|
else if (strcmp (target, "text/uri-list") == 0)
|
|
|
|
return NSURLPboardType;
|
|
|
|
else
|
|
|
|
return [NSString stringWithUTF8String:target];
|
|
|
|
}
|
|
|
|
|
2010-01-01 19:39:57 +00:00
|
|
|
NSSet *
|
2006-03-21 16:50:47 +00:00
|
|
|
_gtk_quartz_target_list_to_pasteboard_types (GtkTargetList *target_list)
|
|
|
|
{
|
|
|
|
NSMutableSet *set = [[NSMutableSet alloc] init];
|
|
|
|
GList *list;
|
|
|
|
|
|
|
|
for (list = target_list->list; list; list = list->next)
|
|
|
|
{
|
|
|
|
GtkTargetPair *pair = list->data;
|
|
|
|
gchar *target = gdk_atom_name (pair->target);
|
|
|
|
[set addObject:target_to_pasteboard_type (target)];
|
|
|
|
g_free (target);
|
|
|
|
}
|
|
|
|
|
2010-01-01 19:39:57 +00:00
|
|
|
return set;
|
2006-03-21 16:50:47 +00:00
|
|
|
}
|
|
|
|
|
2010-01-01 19:39:57 +00:00
|
|
|
NSSet *
|
2006-03-21 16:50:47 +00:00
|
|
|
_gtk_quartz_target_entries_to_pasteboard_types (const GtkTargetEntry *targets,
|
|
|
|
guint n_targets)
|
|
|
|
{
|
|
|
|
NSMutableSet *set = [[NSMutableSet alloc] init];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < n_targets; i++)
|
|
|
|
{
|
|
|
|
[set addObject:target_to_pasteboard_type (targets[i].target)];
|
|
|
|
}
|
|
|
|
|
2010-01-01 19:39:57 +00:00
|
|
|
return set;
|
2006-03-21 16:50:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GdkAtom
|
|
|
|
_gtk_quartz_pasteboard_type_to_atom (NSString *type)
|
|
|
|
{
|
|
|
|
if ([type isEqualToString:NSStringPboardType])
|
|
|
|
return gdk_atom_intern_static_string ("UTF8_STRING");
|
|
|
|
else if ([type isEqualToString:NSTIFFPboardType])
|
|
|
|
return gdk_atom_intern_static_string ("image/tiff");
|
|
|
|
else if ([type isEqualToString:NSColorPboardType])
|
|
|
|
return gdk_atom_intern_static_string ("application/x-color");
|
|
|
|
else if ([type isEqualToString:NSURLPboardType])
|
|
|
|
return gdk_atom_intern_static_string ("text/uri-list");
|
|
|
|
else
|
|
|
|
return gdk_atom_intern ([type UTF8String], FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
GList *
|
|
|
|
_gtk_quartz_pasteboard_types_to_atom_list (NSArray *array)
|
|
|
|
{
|
|
|
|
GList *result = NULL;
|
|
|
|
int i;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
count = [array count];
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
GdkAtom atom = _gtk_quartz_pasteboard_type_to_atom ([array objectAtIndex:i]);
|
|
|
|
|
|
|
|
result = g_list_prepend (result, GDK_ATOM_TO_POINTER (atom));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkSelectionData *
|
|
|
|
_gtk_quartz_get_selection_data_from_pasteboard (NSPasteboard *pasteboard,
|
|
|
|
GdkAtom target,
|
|
|
|
GdkAtom selection)
|
|
|
|
{
|
|
|
|
GtkSelectionData *selection_data = NULL;
|
|
|
|
|
2008-02-12 16:10:39 +00:00
|
|
|
selection_data = g_slice_new0 (GtkSelectionData);
|
2006-03-21 16:50:47 +00:00
|
|
|
selection_data->selection = selection;
|
|
|
|
selection_data->target = target;
|
2011-07-03 23:40:03 +00:00
|
|
|
if (!selection_data->display)
|
|
|
|
selection_data->display = gdk_display_get_default ();
|
2006-03-21 16:50:47 +00:00
|
|
|
if (target == gdk_atom_intern_static_string ("UTF8_STRING"))
|
|
|
|
{
|
|
|
|
NSString *s = [pasteboard stringForType:NSStringPboardType];
|
|
|
|
|
|
|
|
if (s)
|
|
|
|
{
|
2008-05-26 07:40:17 +00:00
|
|
|
const char *utf8_string = [s UTF8String];
|
2007-10-23 19:07:32 +00:00
|
|
|
|
2008-06-26 13:01:40 +00:00
|
|
|
gtk_selection_data_set (selection_data,
|
|
|
|
target, 8,
|
|
|
|
(guchar *)utf8_string, strlen (utf8_string));
|
2006-03-21 16:50:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (target == gdk_atom_intern_static_string ("application/x-color"))
|
|
|
|
{
|
2007-10-23 19:07:32 +00:00
|
|
|
NSColor *nscolor = [[NSColor colorFromPasteboard:pasteboard]
|
|
|
|
colorUsingColorSpaceName:NSDeviceRGBColorSpace];
|
2006-03-21 16:50:47 +00:00
|
|
|
|
|
|
|
guint16 color[4];
|
|
|
|
|
|
|
|
selection_data->target = target;
|
|
|
|
|
|
|
|
color[0] = 0xffff * [nscolor redComponent];
|
|
|
|
color[1] = 0xffff * [nscolor greenComponent];
|
|
|
|
color[2] = 0xffff * [nscolor blueComponent];
|
|
|
|
color[3] = 0xffff * [nscolor alphaComponent];
|
|
|
|
|
|
|
|
gtk_selection_data_set (selection_data, target, 16, (guchar *)color, 8);
|
|
|
|
}
|
|
|
|
else if (target == gdk_atom_intern_static_string ("text/uri-list"))
|
|
|
|
{
|
2007-10-23 19:07:32 +00:00
|
|
|
if ([[pasteboard types] containsObject:NSFilenamesPboardType])
|
|
|
|
{
|
|
|
|
gchar **uris;
|
|
|
|
NSArray *files = [pasteboard propertyListForType:NSFilenamesPboardType];
|
|
|
|
int n_files = [files count];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
selection_data->target = gdk_atom_intern_static_string ("text/uri-list");
|
|
|
|
|
|
|
|
uris = (gchar **) g_malloc (sizeof (gchar*) * (n_files + 1));
|
|
|
|
for (i = 0; i < n_files; ++i)
|
|
|
|
{
|
|
|
|
NSString* uriString = [files objectAtIndex:i];
|
|
|
|
uriString = [@"file://" stringByAppendingString:uriString];
|
|
|
|
uriString = [uriString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
uris[i] = (gchar *) [uriString cStringUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
}
|
|
|
|
uris[i] = NULL;
|
|
|
|
|
|
|
|
gtk_selection_data_set_uris (selection_data, uris);
|
|
|
|
g_free (uris);
|
|
|
|
}
|
|
|
|
else if ([[pasteboard types] containsObject:NSURLPboardType])
|
|
|
|
{
|
|
|
|
gchar *uris[2];
|
|
|
|
NSURL *url = [NSURL URLFromPasteboard:pasteboard];
|
|
|
|
|
|
|
|
selection_data->target = gdk_atom_intern_static_string ("text/uri-list");
|
2007-12-03 19:51:20 +00:00
|
|
|
|
2007-10-23 19:07:32 +00:00
|
|
|
uris[0] = (gchar *) [[url description] UTF8String];
|
2008-05-25 15:15:02 +00:00
|
|
|
|
2007-10-23 19:07:32 +00:00
|
|
|
uris[1] = NULL;
|
|
|
|
gtk_selection_data_set_uris (selection_data, uris);
|
|
|
|
}
|
2006-03-21 16:50:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSData *data;
|
|
|
|
gchar *name;
|
|
|
|
|
|
|
|
name = gdk_atom_name (target);
|
|
|
|
|
|
|
|
if (strcmp (name, "image/tiff") == 0)
|
|
|
|
data = [pasteboard dataForType:NSTIFFPboardType];
|
|
|
|
else
|
|
|
|
data = [pasteboard dataForType:[NSString stringWithUTF8String:name]];
|
|
|
|
|
|
|
|
g_free (name);
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
{
|
2008-06-26 13:01:40 +00:00
|
|
|
gtk_selection_data_set (selection_data,
|
|
|
|
target, 8,
|
|
|
|
[data bytes], [data length]);
|
2006-03-21 16:50:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return selection_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-06-26 13:01:40 +00:00
|
|
|
_gtk_quartz_set_selection_data_for_pasteboard (NSPasteboard *pasteboard,
|
2006-03-21 16:50:47 +00:00
|
|
|
GtkSelectionData *selection_data)
|
|
|
|
{
|
|
|
|
NSString *type;
|
2008-06-26 13:01:40 +00:00
|
|
|
gchar *target;
|
|
|
|
GdkDisplay *display;
|
|
|
|
gint format;
|
|
|
|
const guchar *data;
|
2009-12-30 13:21:31 +00:00
|
|
|
NSUInteger length;
|
2008-06-26 13:01:40 +00:00
|
|
|
|
|
|
|
target = gdk_atom_name (gtk_selection_data_get_target (selection_data));
|
|
|
|
display = gtk_selection_data_get_display (selection_data);
|
|
|
|
format = gtk_selection_data_get_format (selection_data);
|
2008-08-29 04:16:56 +00:00
|
|
|
data = gtk_selection_data_get_data (selection_data);
|
|
|
|
length = gtk_selection_data_get_length (selection_data);
|
2006-03-21 16:50:47 +00:00
|
|
|
|
|
|
|
type = target_to_pasteboard_type (target);
|
|
|
|
g_free (target);
|
2008-05-25 15:15:02 +00:00
|
|
|
|
2006-03-21 16:50:47 +00:00
|
|
|
if ([type isEqualTo:NSStringPboardType])
|
2008-06-26 13:01:40 +00:00
|
|
|
[pasteboard setString:[NSString stringWithUTF8String:(const char *)data]
|
2006-03-21 16:50:47 +00:00
|
|
|
forType:type];
|
|
|
|
else if ([type isEqualTo:NSColorPboardType])
|
|
|
|
{
|
2008-06-26 13:01:40 +00:00
|
|
|
guint16 *color = (guint16 *)data;
|
2006-03-21 16:50:47 +00:00
|
|
|
float red, green, blue, alpha;
|
2008-05-25 15:15:02 +00:00
|
|
|
NSColor *nscolor;
|
2006-03-21 16:50:47 +00:00
|
|
|
|
|
|
|
red = (float)color[0] / 0xffff;
|
|
|
|
green = (float)color[1] / 0xffff;
|
|
|
|
blue = (float)color[2] / 0xffff;
|
|
|
|
alpha = (float)color[3] / 0xffff;
|
2008-06-26 13:01:40 +00:00
|
|
|
|
2008-05-25 15:15:02 +00:00
|
|
|
nscolor = [NSColor colorWithDeviceRed:red green:green blue:blue alpha:alpha];
|
2006-03-21 16:50:47 +00:00
|
|
|
[nscolor writeToPasteboard:pasteboard];
|
|
|
|
}
|
|
|
|
else if ([type isEqualTo:NSURLPboardType])
|
|
|
|
{
|
2010-12-17 02:45:57 +00:00
|
|
|
gchar **uris;
|
2006-03-21 16:50:47 +00:00
|
|
|
|
2010-12-17 02:45:57 +00:00
|
|
|
uris = gtk_selection_data_get_uris (selection_data);
|
|
|
|
if (uris != NULL)
|
2007-12-03 19:51:20 +00:00
|
|
|
{
|
2008-05-25 15:15:02 +00:00
|
|
|
NSURL *url;
|
|
|
|
|
2010-12-17 02:45:57 +00:00
|
|
|
url = [NSURL URLWithString:[NSString stringWithUTF8String:uris[0]]];
|
2007-12-03 19:51:20 +00:00
|
|
|
[url writeToPasteboard:pasteboard];
|
|
|
|
}
|
2010-12-17 02:45:57 +00:00
|
|
|
g_strfreev (uris);
|
2006-03-21 16:50:47 +00:00
|
|
|
}
|
|
|
|
else
|
2009-12-30 13:21:31 +00:00
|
|
|
[pasteboard setData:[NSData dataWithBytesNoCopy:(void *)data
|
|
|
|
length:length
|
|
|
|
freeWhenDone:NO]
|
|
|
|
forType:type];
|
2006-03-21 16:50:47 +00:00
|
|
|
}
|
2011-09-11 23:45:06 +00:00
|
|
|
|
2011-10-22 06:48:13 +00:00
|
|
|
#ifdef QUARTZ_RELOCATION
|
|
|
|
|
|
|
|
/* Bundle-based functions for various directories. These almost work
|
2011-09-11 23:45:06 +00:00
|
|
|
* even when the application isn't in a bundle, becuase mainBundle
|
|
|
|
* paths point to the bin directory in that case. It's a simple matter
|
|
|
|
* to test for that and remove the last element.
|
|
|
|
*/
|
|
|
|
|
2011-10-22 21:08:32 +00:00
|
|
|
static const gchar *
|
|
|
|
get_bundle_path (void)
|
2011-09-11 23:45:06 +00:00
|
|
|
{
|
2011-10-14 23:39:37 +00:00
|
|
|
static gchar *path = NULL;
|
2011-10-22 21:08:32 +00:00
|
|
|
|
2011-10-14 23:39:37 +00:00
|
|
|
if (path == NULL)
|
|
|
|
{
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
gchar *resource_path = g_strdup ([[[NSBundle mainBundle] resourcePath] UTF8String]);
|
2011-10-22 21:08:32 +00:00
|
|
|
gchar *base;
|
2011-10-14 23:39:37 +00:00
|
|
|
[pool drain];
|
2011-10-22 21:08:32 +00:00
|
|
|
|
2011-10-14 23:39:37 +00:00
|
|
|
base = g_path_get_basename (resource_path);
|
|
|
|
if (strcmp (base, "bin") == 0)
|
|
|
|
path = g_path_get_dirname (resource_path);
|
|
|
|
else
|
|
|
|
path = strdup (resource_path);
|
2011-10-22 21:08:32 +00:00
|
|
|
|
2011-10-14 23:39:37 +00:00
|
|
|
g_free (resource_path);
|
|
|
|
g_free (base);
|
|
|
|
}
|
2011-10-22 21:08:32 +00:00
|
|
|
|
2011-09-11 23:45:06 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
const gchar *
|
|
|
|
_gtk_get_datadir (void)
|
|
|
|
{
|
2011-10-14 23:39:37 +00:00
|
|
|
static gchar *path = NULL;
|
2011-10-22 21:08:32 +00:00
|
|
|
|
2011-10-14 23:39:37 +00:00
|
|
|
if (path == NULL)
|
2011-10-22 21:08:32 +00:00
|
|
|
path = g_build_filename (get_bundle_path (), "share", NULL);
|
|
|
|
|
2011-10-14 23:39:37 +00:00
|
|
|
return path;
|
2011-09-11 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const gchar *
|
|
|
|
_gtk_get_libdir (void)
|
|
|
|
{
|
2011-10-14 23:39:37 +00:00
|
|
|
static gchar *path = NULL;
|
2011-10-22 21:08:32 +00:00
|
|
|
|
2011-10-14 23:39:37 +00:00
|
|
|
if (path == NULL)
|
2011-10-22 21:08:32 +00:00
|
|
|
path = g_build_filename (get_bundle_path (), "lib", NULL);
|
|
|
|
|
2011-10-14 23:39:37 +00:00
|
|
|
return path;
|
2011-09-11 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const gchar *
|
|
|
|
_gtk_get_localedir (void)
|
|
|
|
{
|
2011-10-14 23:39:37 +00:00
|
|
|
static gchar *path = NULL;
|
2011-10-22 21:08:32 +00:00
|
|
|
|
2011-10-14 23:39:37 +00:00
|
|
|
if (path == NULL)
|
2011-10-22 21:08:32 +00:00
|
|
|
path = g_build_filename (get_bundle_path (), "share", "locale", NULL);
|
|
|
|
|
2011-10-14 23:39:37 +00:00
|
|
|
return path;
|
2011-09-11 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const gchar *
|
|
|
|
_gtk_get_sysconfdir (void)
|
|
|
|
{
|
2011-10-14 23:39:37 +00:00
|
|
|
static gchar *path = NULL;
|
2011-10-22 21:08:32 +00:00
|
|
|
|
2011-10-14 23:39:37 +00:00
|
|
|
if (path == NULL)
|
2011-10-22 21:08:32 +00:00
|
|
|
path = g_build_filename (get_bundle_path (), "etc", NULL);
|
|
|
|
|
2011-10-14 23:39:37 +00:00
|
|
|
return path;
|
2011-09-11 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const gchar *
|
|
|
|
_gtk_get_data_prefix (void)
|
|
|
|
{
|
2011-10-14 23:39:37 +00:00
|
|
|
return get_bundle_path ();
|
2011-09-11 23:45:06 +00:00
|
|
|
}
|
2011-10-22 06:48:13 +00:00
|
|
|
|
|
|
|
#endif /* QUARTZ_RELOCATION */
|