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/.
28 #include <X11/Xatom.h>
31 #include "gdkproperty.h"
32 #include "gdkselection.h"
33 #include "gdkprivate.h"
34 #include "gdkprivate-x11.h"
36 typedef struct _OwnerInfo OwnerInfo;
45 static GSList *owner_list;
47 /* When a window is destroyed we check if it is the owner
48 * of any selections. This is somewhat inefficient, but
49 * owner_list is typically short, and it is a low memory,
53 _gdk_selection_window_destroyed (GdkWindow *window)
55 GSList *tmp_list = owner_list;
58 OwnerInfo *info = tmp_list->data;
59 tmp_list = tmp_list->next;
61 if (info->owner == window)
63 owner_list = g_slist_remove (owner_list, info);
69 /* We only pass through those SelectionClear events that actually
70 * reflect changes to the selection owner that we didn't make ourself.
73 _gdk_selection_filter_clear_event (XSelectionClearEvent *event)
75 GSList *tmp_list = owner_list;
79 OwnerInfo *info = tmp_list->data;
80 if (info->selection == gdk_x11_xatom_to_atom (event->selection))
82 if ((GDK_DRAWABLE_XID (info->owner) == event->window &&
83 event->serial >= info->serial))
85 owner_list = g_slist_remove (owner_list, info);
92 tmp_list = tmp_list->next;
99 gdk_selection_owner_set (GdkWindow *owner,
110 xselection = gdk_x11_atom_to_xatom (selection);
114 if (GDK_WINDOW_DESTROYED (owner))
117 xdisplay = GDK_WINDOW_XDISPLAY (owner);
118 xwindow = GDK_WINDOW_XID (owner);
122 xdisplay = gdk_display;
126 tmp_list = owner_list;
129 info = tmp_list->data;
130 if (info->selection == selection)
132 owner_list = g_slist_remove (owner_list, info);
136 tmp_list = tmp_list->next;
141 info = g_new (OwnerInfo, 1);
143 info->serial = NextRequest (GDK_WINDOW_XDISPLAY (owner));
144 info->selection = selection;
146 owner_list = g_slist_prepend (owner_list, info);
149 XSetSelectionOwner (xdisplay, xselection, xwindow, time);
151 return (XGetSelectionOwner (xdisplay, xselection) == xwindow);
155 gdk_selection_owner_get (GdkAtom selection)
159 xwindow = XGetSelectionOwner (gdk_display, gdk_x11_atom_to_xatom (selection));
163 return gdk_window_lookup (xwindow);
167 gdk_selection_convert (GdkWindow *requestor,
172 if (GDK_WINDOW_DESTROYED (requestor))
175 XConvertSelection (GDK_WINDOW_XDISPLAY (requestor),
176 gdk_x11_atom_to_xatom (selection),
177 gdk_x11_atom_to_xatom (target),
178 gdk_x11_atom_to_xatom (_gdk_selection_property),
179 GDK_WINDOW_XID (requestor), time);
183 gdk_selection_property_get (GdkWindow *requestor,
195 g_return_val_if_fail (requestor != NULL, 0);
196 g_return_val_if_fail (GDK_IS_WINDOW (requestor), 0);
198 /* If retrieved chunks are typically small, (and the ICCCM says the
199 should be) it would be a win to try first with a buffer of
200 moderate length, to avoid two round trips to the server */
202 if (GDK_WINDOW_DESTROYED (requestor))
206 XGetWindowProperty (GDK_WINDOW_XDISPLAY (requestor),
207 GDK_WINDOW_XID (requestor),
208 gdk_x11_atom_to_xatom (_gdk_selection_property),
210 AnyPropertyType, &prop_type, &prop_format,
211 &nitems, &nbytes, &t);
214 *ret_type = gdk_x11_xatom_to_atom (prop_type);
216 *ret_format = prop_format;
218 if (prop_type == None)
230 /* Add on an extra byte to handle null termination. X guarantees
231 that t will be 1 longer than nbytes and null terminated */
234 /* We can't delete the selection here, because it might be the INCR
235 protocol, in which case the client has to make sure they'll be
236 notified of PropertyChange events _before_ the property is deleted.
237 Otherwise there's no guarantee we'll win the race ... */
238 XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (requestor),
239 GDK_DRAWABLE_XID (requestor),
240 gdk_x11_atom_to_xatom (_gdk_selection_property),
241 0, (nbytes + 3) / 4, False,
242 AnyPropertyType, &prop_type, &prop_format,
243 &nitems, &nbytes, &t);
245 if (prop_type != None)
247 *data = g_new (guchar, length);
249 if (prop_type == XA_ATOM || prop_type == gdk_x11_get_xatom_by_name ("ATOM_PAIR"))
251 Atom* atoms = (Atom*) t;
252 GdkAtom* atoms_dest = (GdkAtom*) *data;
255 num_atom = (length - 1) / sizeof (GdkAtom);
256 for (i=0; i < num_atom; i++)
257 atoms_dest[i] = gdk_x11_xatom_to_atom (atoms[i]);
261 memcpy (*data, t, length);
277 gdk_selection_send_notify (guint32 requestor,
283 XSelectionEvent xevent;
285 xevent.type = SelectionNotify;
287 xevent.send_event = True;
288 xevent.display = gdk_display;
289 xevent.requestor = requestor;
290 xevent.selection = gdk_x11_atom_to_xatom (selection);
291 xevent.target = gdk_x11_atom_to_xatom (target);
292 xevent.property = gdk_x11_atom_to_xatom (property);
295 gdk_send_xevent (requestor, False, NoEventMask, (XEvent*) &xevent);
299 gdk_text_property_to_text_list (GdkAtom encoding,
305 XTextProperty property;
310 property.value = (guchar *)text;
311 property.encoding = gdk_x11_atom_to_xatom (encoding);
312 property.format = format;
313 property.nitems = length;
314 res = XmbTextPropertyToTextList (GDK_DISPLAY(), &property, &local_list, &count);
316 if (res == XNoMemory || res == XLocaleNotSupported || res == XConverterNotFound)
328 XFreeStringList (local_list);
335 gdk_free_text_list (gchar **list)
337 g_return_if_fail (list != NULL);
339 XFreeStringList (list);
343 make_list (const gchar *text,
348 GSList *strings = NULL;
351 const gchar *p = text;
354 GError *error = NULL;
356 while (p < text + length)
361 while (*q && q < text + length)
366 str = g_convert (p, q - p,
367 "UTF-8", "ISO-8859-1",
372 g_warning ("Error converting selection from STRING: %s",
374 g_error_free (error);
378 str = g_strndup (p, q - p);
382 strings = g_slist_prepend (strings, str);
390 *list = g_new (gchar *, n_strings + 1);
392 (*list)[n_strings] = NULL;
399 (*list)[--i] = tmp_list->data;
401 g_free (tmp_list->data);
403 tmp_list = tmp_list->next;
406 g_slist_free (strings);
412 * gdk_text_property_to_utf8_list:
413 * @encoding: an atom representing the encoding of the text
414 * @format: the format of the property
415 * @text: the text to convert
416 * @length: the length of @text, in bytes
417 * @list: location to store the list of strings or %NULL. The
418 * list should be freed with g_strfreev().
420 * Converts a text property in the giving encoding to
421 * a list of UTF-8 strings.
423 * Return value: the number of strings in the resulting
427 gdk_text_property_to_utf8_list (GdkAtom encoding,
433 g_return_val_if_fail (text != NULL, 0);
434 g_return_val_if_fail (length >= 0, 0);
436 if (encoding == GDK_TARGET_STRING)
438 return make_list ((gchar *)text, length, TRUE, list);
440 else if (encoding == gdk_atom_intern ("UTF8_STRING", FALSE))
442 return make_list ((gchar *)text, length, FALSE, list);
449 const gchar *charset = NULL;
450 gboolean need_conversion = !g_get_charset (&charset);
452 GError *error = NULL;
454 /* Probably COMPOUND text, we fall back to Xlib routines
456 local_count = gdk_text_property_to_text_list (encoding,
462 *list = g_new (gchar *, local_count + 1);
464 for (i=0; i<local_count; i++)
466 /* list contains stuff in our default encoding
470 gchar *utf = g_convert (local_list[i], -1,
476 (*list)[count++] = utf;
482 g_warning ("Error converting to UTF-8 from '%s': %s",
483 charset, error->message);
484 g_error_free (error);
491 (*list)[count++] = g_strdup (local_list[i]);
496 gdk_free_text_list (local_list);
498 (*list)[count] = NULL;
505 gdk_string_to_compound_text (const gchar *str,
512 XTextProperty property;
514 res = XmbTextListToTextProperty (GDK_DISPLAY(),
515 (char **)&str, 1, XCompoundTextStyle,
519 property.encoding = None;
520 property.format = None;
521 property.value = NULL;
526 *encoding = gdk_x11_xatom_to_atom (property.encoding);
528 *format = property.format;
530 *ctext = property.value;
532 *length = property.nitems;
537 /* The specifications for COMPOUND_TEXT and STRING specify that C0 and
538 * C1 are not allowed except for \n and \t, however the X conversions
539 * routines for COMPOUND_TEXT only enforce this in one direction,
540 * causing cut-and-paste of \r and \r\n separated text to fail.
541 * This routine strips out all non-allowed C0 and C1 characters
542 * from the input string and also canonicalizes \r, and \r\n to \n
545 sanitize_utf8 (const gchar *src)
547 gint len = strlen (src);
548 GString *result = g_string_sized_new (len);
549 const gchar *p = src;
559 g_string_append_c (result, '\n');
563 gunichar ch = g_utf8_get_char (p);
567 if (!((ch < 0x20 && ch != '\t' && ch != '\n') || (ch >= 0x7f && ch < 0xa0)))
569 buflen = g_unichar_to_utf8 (ch, buf);
570 g_string_append_len (result, buf, buflen);
573 p = g_utf8_next_char (p);
577 return g_string_free (result, FALSE);
581 * gdk_utf8_to_string_target:
582 * @str: a UTF-8 string
584 * Converts an UTF-8 string into the best possible representation
585 * as a STRING. The representation of characters not in STRING
586 * is not specified; it may be as pseudo-escape sequences
587 * \x{ABCD}, or it may be in some other form of approximation.
589 * Return value: the newly-allocated string, or %NULL if the
590 * conversion failed. (It should not fail for
591 * any properly formed UTF-8 string.)
594 gdk_utf8_to_string_target (const gchar *str)
596 GError *error = NULL;
598 gchar *tmp_str = sanitize_utf8 (str);
599 gchar *result = g_convert_with_fallback (tmp_str, -1,
600 "ISO-8859-1", "UTF-8",
601 NULL, NULL, NULL, &error);
604 g_warning ("Error converting from UTF-8 to STRING: %s",
606 g_error_free (error);
614 * gdk_utf8_to_compound_text:
615 * @str: a UTF-8 string
616 * @encoding: location to store resulting encoding
617 * @format: location to store format of the result
618 * @ctext: location to store the data of the result
619 * @length: location to store the length of the data
622 * Converts from UTF-8 to compound text.
624 * Return value: %TRUE if the conversion succeeded, otherwise
628 gdk_utf8_to_compound_text (const gchar *str,
634 gboolean need_conversion;
635 const gchar *charset;
636 gchar *locale_str, *tmp_str;
637 GError *error = NULL;
640 g_return_val_if_fail (str != NULL, FALSE);
642 need_conversion = !g_get_charset (&charset);
644 tmp_str = sanitize_utf8 (str);
648 locale_str = g_convert_with_fallback (tmp_str, -1,
650 NULL, NULL, NULL, &error);
655 g_warning ("Error converting from UTF-8 to '%s': %s",
656 charset, error->message);
657 g_error_free (error);
672 locale_str = tmp_str;
674 result = gdk_string_to_compound_text (locale_str,
675 encoding, format, ctext, length);
676 result = (result == Success? TRUE : FALSE);
683 void gdk_free_compound_text (guchar *ctext)