1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gdkproperty.h"
30 #include "gdkselection.h"
31 #include "gdkprivate.h"
32 #include "gdkprivate-fb.h"
35 typedef struct _OwnerInfo OwnerInfo;
45 /* When a window is destroyed we check if it is the owner
46 * of any selections. This is somewhat inefficient, but
47 * owner_list is typically short, and it is a low memory,
51 _gdk_selection_window_destroyed (GdkWindow *window)
53 GSList *tmp_list = owner_list;
56 OwnerInfo *info = tmp_list->data;
57 tmp_list = tmp_list->next;
59 if (info->owner == window)
61 owner_list = g_slist_remove (owner_list, info);
68 gdk_selection_owner_set (GdkWindow *owner,
76 tmp_list = owner_list;
79 info = tmp_list->data;
80 if (info->selection == selection)
82 owner_list = g_slist_remove (owner_list, info);
86 tmp_list = tmp_list->next;
91 info = g_new (OwnerInfo, 1);
93 info->selection = selection;
95 owner_list = g_slist_prepend (owner_list, info);
102 gdk_selection_owner_get (GdkAtom selection)
107 tmp_list = owner_list;
110 info = tmp_list->data;
111 if (info->selection == selection)
115 tmp_list = tmp_list->next;
121 gdk_selection_convert (GdkWindow *requestor,
128 GdkWindow *event_window;
130 owner = gdk_selection_owner_get (selection);
134 event_window = gdk_fb_other_event_window (owner, GDK_SELECTION_REQUEST);
137 event = gdk_event_make (event_window, GDK_SELECTION_REQUEST, TRUE);
138 event->selection.requestor = requestor;
139 event->selection.selection = selection;
140 event->selection.target = target;
141 event->selection.property = _gdk_selection_property;
146 /* If no owner for the specified selection exists, the X server
147 * generates a SelectionNotify event to the requestor with property None.
149 gdk_selection_send_notify ((guint32)requestor,
158 gdk_selection_property_get (GdkWindow *requestor,
168 g_return_val_if_fail (requestor != NULL, 0);
169 g_return_val_if_fail (GDK_IS_WINDOW (requestor), 0);
171 if (!gdk_property_get (requestor,
172 _gdk_selection_property,
173 0/*AnyPropertyType?*/,
176 &prop_type, &prop_format, &prop_len,
184 *ret_type = prop_type;
186 *ret_format = prop_format;
188 if (!gdk_property_get (requestor,
189 _gdk_selection_property,
190 0/*AnyPropertyType?*/,
193 &prop_type, &prop_format, &prop_len,
207 gdk_selection_send_notify (guint32 requestor,
214 GdkWindow *event_window;
216 event_window = gdk_fb_other_event_window (gdk_window_lookup (requestor), GDK_SELECTION_NOTIFY);
219 event = gdk_event_make (event_window, GDK_SELECTION_NOTIFY, TRUE);
220 event->selection.selection = selection;
221 event->selection.target = target;
222 event->selection.property = property;
223 event->selection.requestor = (GdkNativeWindow) requestor;
228 gdk_text_property_to_text_list (GdkAtom encoding, gint format,
229 const guchar *text, gint length,
232 g_warning ("gdk_text_property_to_text_list() not implemented\n");
237 gdk_free_text_list (gchar **list)
239 g_return_if_fail (list != NULL);
240 g_warning ("gdk_free_text_list() not implemented\n");
244 gdk_string_to_compound_text (const gchar *str,
245 GdkAtom *encoding, gint *format,
246 guchar **ctext, gint *length)
248 g_warning ("gdk_string_to_compound_text() not implemented\n");
252 void gdk_free_compound_text (guchar *ctext)
254 g_warning ("gdk_free_compound_text() not implemented\n");
258 * gdk_utf8_to_string_target:
259 * @str: a UTF-8 string
261 * Convert an UTF-8 string into the best possible representation
262 * as a STRING. The representation of characters not in STRING
263 * is not specified; it may be as pseudo-escape sequences
264 * \x{ABCD}, or it may be in some other form of approximation.
266 * Return value: the newly allocated string, or %NULL if the
267 * conversion failed. (It should not fail for
268 * any properly formed UTF-8 string.)
271 gdk_utf8_to_string_target (const gchar *str)
273 g_warning ("gdk_utf8_to_string_target() not implemented\n");
278 * gdk_utf8_to_compound_text:
279 * @str: a UTF-8 string
280 * @encoding: location to store resulting encoding
281 * @format: location to store format of the result
282 * @ctext: location to store the data of the result
283 * @length: location to store the length of the data
286 * Convert from UTF-8 to compound text.
288 * Return value: %TRUE if the conversion succeeded, otherwise
292 gdk_utf8_to_compound_text (const gchar *str,
298 g_warning ("gdk_utf8_to_compound_text() not implemented\n");
303 make_list (const gchar *text,
308 GSList *strings = NULL;
311 const gchar *p = text;
314 GError *error = NULL;
316 while (p < text + length)
321 while (*q && q < text + length)
326 str = g_convert (p, q - p,
327 "UTF-8", "ISO-8859-1",
332 g_warning ("Error converting selection from STRING: %s",
334 g_error_free (error);
338 str = g_strndup (p, q - p);
342 strings = g_slist_prepend (strings, str);
350 *list = g_new (gchar *, n_strings + 1);
352 (*list)[n_strings] = NULL;
359 (*list)[--i] = tmp_list->data;
361 g_free (tmp_list->data);
363 tmp_list = tmp_list->next;
366 g_slist_free (strings);
373 * gdk_text_property_to_utf8_list:
374 * @encoding: an atom representing the encoding of the text
375 * @format: the format of the property
376 * @text: the text to convert
377 * @length: the length of @text, in bytes
378 * @list: location to store the list of strings or %NULL. The
379 * list should be freed with g_strfreev().
381 * Convert a text property in the giving encoding to
382 * a list of UTF-8 strings.
384 * Return value: the number of strings in the resulting
388 gdk_text_property_to_utf8_list (GdkAtom encoding,
394 g_return_val_if_fail (text != NULL, 0);
395 g_return_val_if_fail (length >= 0, 0);
397 if (encoding == GDK_TARGET_STRING)
399 return make_list ((gchar *)text, length, TRUE, list);
401 else if (encoding == gdk_atom_intern ("UTF8_STRING", FALSE))
403 return make_list ((gchar *)text, length, FALSE, list);
410 const gchar *charset = NULL;
411 gboolean need_conversion = !g_get_charset (&charset);
413 GError *error = NULL;
415 /* Probably COMPOUND text, we fall back to Xlib routines
417 local_count = gdk_text_property_to_text_list (encoding,
423 *list = g_new (gchar *, local_count + 1);
425 for (i = 0; i < local_count; i++)
427 /* list contains stuff in our default encoding
431 gchar *utf = g_convert (local_list[i], -1,
437 (*list)[count++] = utf;
443 g_warning ("Error converting to UTF-8 from '%s': %s",
444 charset, error->message);
445 g_error_free (error);
452 (*list)[count++] = g_strdup (local_list[i]);
456 gdk_free_text_list (local_list);
457 (*list)[count] = NULL;