Debian Codegraph @

gdk/quartz/gdkselection-quartz.c

raw
1 /* gdkselection-quartz.c
2 *
3 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * Copyright (C) 1998-2002 Tor Lillqvist
5 * Copyright (C) 2005 Imendio AB
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #include "gdkselection.h"
24 #include "gdkproperty.h"
25 #include "gdkquartz.h"
26 #include "gdkinternal-quartz.h"
27 #include "gdkquartz-gtk-only.h"
28 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 110000
29 #include <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
30 #endif
31
32 gboolean
33 _gdk_quartz_display_set_selection_owner (GdkDisplay *display,
34 GdkWindow *owner,
35 GdkAtom selection,
36 guint32 time,
37 gint send_event)
38 {
39 /* FIXME: Implement */
40 return TRUE;
41 }
42
43 GdkWindow*
44 _gdk_quartz_display_get_selection_owner (GdkDisplay *display,
45 GdkAtom selection)
46 {
47 /* FIXME: Implement */
48 return NULL;
49 }
50
51 void
52 _gdk_quartz_display_convert_selection (GdkDisplay *display,
53 GdkWindow *requestor,
54 GdkAtom selection,
55 GdkAtom target,
56 guint32 time)
57 {
58 /* FIXME: Implement */
59 }
60
61 gint
62 _gdk_quartz_display_get_selection_property (GdkDisplay *display,
63 GdkWindow *requestor,
64 guchar **data,
65 GdkAtom *ret_type,
66 gint *ret_format)
67 {
68 /* FIXME: Implement */
69 return 0;
70 }
71
72 gchar *
73 _gdk_quartz_display_utf8_to_string_target (GdkDisplay *display,
74 const gchar *str)
75 {
76 /* FIXME: Implement */
77 return NULL;
78 }
79
80 static gint
81 make_list (const gchar *text,
82 gint length,
83 gboolean latin1,
84 gchar ***list)
85 {
86 GSList *strings = NULL;
87 gint n_strings = 0;
88 gint i;
89 const gchar *p = text;
90 const gchar *q;
91 GSList *tmp_list;
92 GError *error = NULL;
93
94 while (p < text + length)
95 {
96 gchar *str;
97
98 q = p;
99 while (*q && q < text + length)
100 q++;
101
102 if (latin1)
103 {
104 str = g_convert (p, q - p,
105 "UTF-8", "ISO-8859-1",
106 NULL, NULL, &error);
107
108 if (!str)
109 {
110 g_warning ("Error converting selection from STRING: %s",
111 error->message);
112 g_error_free (error);
113 }
114 }
115 else
116 str = g_strndup (p, q - p);
117
118 if (str)
119 {
120 strings = g_slist_prepend (strings, str);
121 n_strings++;
122 }
123
124 p = q + 1;
125 }
126
127 if (list)
128 *list = g_new0 (gchar *, n_strings + 1);
129
130 i = n_strings;
131 tmp_list = strings;
132 while (tmp_list)
133 {
134 if (list)
135 (*list)[--i] = tmp_list->data;
136 else
137 g_free (tmp_list->data);
138
139 tmp_list = tmp_list->next;
140 }
141
142 g_slist_free (strings);
143
144 return n_strings;
145 }
146
147 gint
148 _gdk_quartz_display_text_property_to_utf8_list (GdkDisplay *display,
149 GdkAtom encoding,
150 gint format,
151 const guchar *text,
152 gint length,
153 gchar ***list)
154 {
155 g_return_val_if_fail (text != NULL, 0);
156 g_return_val_if_fail (length >= 0, 0);
157
158 if (encoding == GDK_TARGET_STRING)
159 {
160 return make_list ((gchar *)text, length, TRUE, list);
161 }
162 else if (encoding == gdk_atom_intern_static_string ("UTF8_STRING"))
163 {
164 return make_list ((gchar *)text, length, FALSE, list);
165 }
166 else
167 {
168 gchar *enc_name = gdk_atom_name (encoding);
169
170 g_warning ("gdk_text_property_to_utf8_list_for_display: encoding %s not handled", enc_name);
171 g_free (enc_name);
172
173 if (list)
174 *list = NULL;
175
176 return 0;
177 }
178 }
179
180 GdkAtom
181 gdk_quartz_pasteboard_type_to_atom_libgtk_only (NSString *type)
182 {
183 if ([type isEqualToString:GDK_QUARTZ_STRING_PBOARD_TYPE])
184 return gdk_atom_intern_static_string ("UTF8_STRING");
185 else if ([type isEqualToString:GDK_QUARTZ_TIFF_PBOARD_TYPE])
186 return gdk_atom_intern_static_string ("image/tiff");
187 else if ([type isEqualToString:GDK_QUARTZ_COLOR_PBOARD_TYPE])
188 return gdk_atom_intern_static_string ("application/x-color");
189 else if ([type isEqualToString:GDK_QUARTZ_URL_PBOARD_TYPE] ||
190 [type isEqualToString:GDK_QUARTZ_FILE_PBOARD_TYPE])
191 return gdk_atom_intern_static_string ("text/uri-list");
192 else
193 {
194 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 110000
195 if (gdk_quartz_osx_version() >= GDK_OSX_BIGSUR)
196 {
197 UTType* uti = [UTType typeWithIdentifier:type];
198 if (uti != nil)
199 return gdk_atom_intern (uti.preferredMIMEType.UTF8String, FALSE);
200 }
201 #endif
202 return gdk_atom_intern ([type UTF8String], FALSE);
203 }
204 }
205
206 NSString *
207 gdk_quartz_target_to_pasteboard_type_libgtk_only (const char *target)
208 {
209 if (strcmp (target, "UTF8_STRING") == 0)
210 return GDK_QUARTZ_STRING_PBOARD_TYPE;
211 else if (strcmp (target, "image/tiff") == 0)
212 return GDK_QUARTZ_TIFF_PBOARD_TYPE;
213 else if (strcmp (target, "application/x-color") == 0)
214 return GDK_QUARTZ_COLOR_PBOARD_TYPE;
215 else if (strcmp (target, "text/uri-list") == 0)
216 return GDK_QUARTZ_URL_PBOARD_TYPE;
217 else
218 {
219 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 110000
220 if (gdk_quartz_osx_version() >= GDK_OSX_BIGSUR)
221 {
222 UTType* type = [UTType typeWithMIMEType: [NSString stringWithUTF8String:target]];
223 if (type != nil)
224 return type.identifier;
225 }
226 #endif
227 return [NSString stringWithUTF8String:target];
228 }
229 }
230
231 NSString *
232 gdk_quartz_atom_to_pasteboard_type_libgtk_only (GdkAtom atom)
233 {
234 gchar *target = gdk_atom_name (atom);
235 NSString *ret = gdk_quartz_target_to_pasteboard_type_libgtk_only (target);
236 g_free (target);
237
238 return ret;
239 }