]> Pileus Git - ~andy/gtk/blob - gtk/gtkquartz.c
quartz: gtkquartz.c: add missing include
[~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
164   if (target == gdk_atom_intern_static_string ("UTF8_STRING"))
165     {
166       NSString *s = [pasteboard stringForType:NSStringPboardType];
167
168       if (s)
169         {
170           const char *utf8_string = [s UTF8String];
171
172           gtk_selection_data_set (selection_data,
173                                   target, 8,
174                                   (guchar *)utf8_string, strlen (utf8_string));
175         }
176     }
177   else if (target == gdk_atom_intern_static_string ("application/x-color"))
178     {
179       NSColor *nscolor = [[NSColor colorFromPasteboard:pasteboard]
180                           colorUsingColorSpaceName:NSDeviceRGBColorSpace];
181       
182       guint16 color[4];
183       
184       selection_data->target = target;
185
186       color[0] = 0xffff * [nscolor redComponent];
187       color[1] = 0xffff * [nscolor greenComponent];
188       color[2] = 0xffff * [nscolor blueComponent];
189       color[3] = 0xffff * [nscolor alphaComponent];
190
191       gtk_selection_data_set (selection_data, target, 16, (guchar *)color, 8);
192     }
193   else if (target == gdk_atom_intern_static_string ("text/uri-list"))
194     {
195       if ([[pasteboard types] containsObject:NSFilenamesPboardType])
196         {
197            gchar **uris;
198            NSArray *files = [pasteboard propertyListForType:NSFilenamesPboardType];
199            int n_files = [files count];
200            int i;
201
202            selection_data->target = gdk_atom_intern_static_string ("text/uri-list");
203
204            uris = (gchar **) g_malloc (sizeof (gchar*) * (n_files + 1));
205            for (i = 0; i < n_files; ++i)
206              {
207                NSString* uriString = [files objectAtIndex:i];
208                uriString = [@"file://" stringByAppendingString:uriString];
209                uriString = [uriString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
210                uris[i] = (gchar *) [uriString cStringUsingEncoding:NSUTF8StringEncoding];
211              }
212            uris[i] = NULL;
213
214            gtk_selection_data_set_uris (selection_data, uris);
215            g_free (uris);
216          }
217       else if ([[pasteboard types] containsObject:NSURLPboardType])
218         {
219           gchar *uris[2];
220           NSURL *url = [NSURL URLFromPasteboard:pasteboard];
221
222           selection_data->target = gdk_atom_intern_static_string ("text/uri-list");
223
224           uris[0] = (gchar *) [[url description] UTF8String];
225
226           uris[1] = NULL;
227           gtk_selection_data_set_uris (selection_data, uris);
228         }
229     }
230   else
231     {
232       NSData *data;
233       gchar *name;
234
235       name = gdk_atom_name (target);
236
237       if (strcmp (name, "image/tiff") == 0)
238         data = [pasteboard dataForType:NSTIFFPboardType];
239       else
240         data = [pasteboard dataForType:[NSString stringWithUTF8String:name]];
241
242       g_free (name);
243
244       if (data)
245         {
246           gtk_selection_data_set (selection_data,
247                                   target, 8,
248                                   [data bytes], [data length]);
249         }
250     }
251
252   return selection_data;
253 }
254
255 void
256 _gtk_quartz_set_selection_data_for_pasteboard (NSPasteboard     *pasteboard,
257                                                GtkSelectionData *selection_data)
258 {
259   NSString *type;
260   gchar *target;
261   GdkDisplay *display;
262   gint format;
263   const guchar *data;
264   NSUInteger length;
265
266   target = gdk_atom_name (gtk_selection_data_get_target (selection_data));
267   display = gtk_selection_data_get_display (selection_data);
268   format = gtk_selection_data_get_format (selection_data);
269   data = gtk_selection_data_get_data (selection_data);
270   length = gtk_selection_data_get_length (selection_data);
271
272   type = target_to_pasteboard_type (target);
273   g_free (target);
274
275   if ([type isEqualTo:NSStringPboardType]) 
276     [pasteboard setString:[NSString stringWithUTF8String:(const char *)data]
277                   forType:type];
278   else if ([type isEqualTo:NSColorPboardType])
279     {
280       guint16 *color = (guint16 *)data;
281       float red, green, blue, alpha;
282       NSColor *nscolor;
283
284       red   = (float)color[0] / 0xffff;
285       green = (float)color[1] / 0xffff;
286       blue  = (float)color[2] / 0xffff;
287       alpha = (float)color[3] / 0xffff;
288
289       nscolor = [NSColor colorWithDeviceRed:red green:green blue:blue alpha:alpha];
290       [nscolor writeToPasteboard:pasteboard];
291     }
292   else if ([type isEqualTo:NSURLPboardType])
293     {
294       gchar **uris;
295
296       uris = gtk_selection_data_get_uris (selection_data);
297       if (uris != NULL)
298         {
299           NSURL *url;
300
301           url = [NSURL URLWithString:[NSString stringWithUTF8String:uris[0]]];
302           [url writeToPasteboard:pasteboard];
303         }
304       g_strfreev (uris);
305     }
306   else
307     [pasteboard setData:[NSData dataWithBytesNoCopy:(void *)data
308                                              length:length
309                                        freeWhenDone:NO]
310                                             forType:type];
311 }