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 <X11/Xatom.h>
33 #include "gdkproperty.h"
34 #include "gdkselection.h"
35 #include "gdkprivate.h"
36 #include "gdkprivate-x11.h"
37 #include "gdkdisplay-x11.h"
40 typedef struct _OwnerInfo OwnerInfo;
49 static GSList *owner_list;
51 /* When a window is destroyed we check if it is the owner
52 * of any selections. This is somewhat inefficient, but
53 * owner_list is typically short, and it is a low memory,
57 _gdk_selection_window_destroyed (GdkWindow *window)
59 GSList *tmp_list = owner_list;
62 OwnerInfo *info = tmp_list->data;
63 tmp_list = tmp_list->next;
65 if (info->owner == window)
67 owner_list = g_slist_remove (owner_list, info);
73 /* We only pass through those SelectionClear events that actually
74 * reflect changes to the selection owner that we didn't make ourself.
77 _gdk_selection_filter_clear_event (XSelectionClearEvent *event)
79 GSList *tmp_list = owner_list;
80 GdkDisplay *display = gdk_x11_lookup_xdisplay (event->display);
84 OwnerInfo *info = tmp_list->data;
86 if (gdk_drawable_get_display (info->owner) == display &&
87 info->selection == gdk_x11_xatom_to_atom_for_display (display, event->selection))
89 if ((GDK_DRAWABLE_XID (info->owner) == event->window &&
90 event->serial >= info->serial))
92 owner_list = g_slist_remove (owner_list, info);
99 tmp_list = tmp_list->next;
105 * gdk_selection_owner_set_for_display:
106 * @display: the #GdkDisplay.
107 * @owner: a #GdkWindow or %NULL to indicate that the owner for
108 * the given should be unset.
109 * @selection: an atom identifying a selection.
110 * @time_: timestamp to use when setting the selection.
111 * If this is older than the timestamp given last time the owner was
112 * set for the given selection, the request will be ignored.
113 * @send_event: if %TRUE, and the new owner is different from the current
114 * owner, the current owner will be sent a SelectionClear event.
116 * Sets the #GdkWindow @owner as the current owner of the selection @selection.
118 * Returns: %TRUE if the selection owner was successfully changed to owner,
124 gdk_selection_owner_set_for_display (GdkDisplay *display,
136 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
137 g_return_val_if_fail (selection != GDK_NONE, FALSE);
144 if (GDK_WINDOW_DESTROYED (owner))
147 xdisplay = GDK_WINDOW_XDISPLAY (owner);
148 xwindow = GDK_WINDOW_XID (owner);
152 xdisplay = GDK_DISPLAY_XDISPLAY (display);
156 xselection = gdk_x11_atom_to_xatom_for_display (display, selection);
158 tmp_list = owner_list;
161 info = tmp_list->data;
162 if (info->selection == selection)
164 owner_list = g_slist_remove (owner_list, info);
168 tmp_list = tmp_list->next;
173 info = g_new (OwnerInfo, 1);
175 info->serial = NextRequest (GDK_WINDOW_XDISPLAY (owner));
176 info->selection = selection;
178 owner_list = g_slist_prepend (owner_list, info);
181 XSetSelectionOwner (xdisplay, xselection, xwindow, time);
183 return (XGetSelectionOwner (xdisplay, xselection) == xwindow);
187 * gdk_selection_owner_get_for_display:
188 * @display: a #GdkDisplay.
189 * @selection: an atom indentifying a selection.
191 * Determine the owner of the given selection.
193 * Note that the return value may be owned by a different
194 * process if a foreign window was previously created for that
195 * window, but a new foreign window will never be created by this call.
197 * Returns: if there is a selection owner for this window, and it is a
198 * window known to the current process, the #GdkWindow that owns the
199 * selection, otherwise %NULL.
204 gdk_selection_owner_get_for_display (GdkDisplay *display,
209 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
210 g_return_val_if_fail (selection != GDK_NONE, NULL);
215 xwindow = XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display),
216 gdk_x11_atom_to_xatom_for_display (display,
221 return gdk_window_lookup_for_display (display, xwindow);
225 gdk_selection_convert (GdkWindow *requestor,
232 g_return_if_fail (selection != GDK_NONE);
234 if (GDK_WINDOW_DESTROYED (requestor))
237 display = GDK_WINDOW_DISPLAY (requestor);
239 XConvertSelection (GDK_WINDOW_XDISPLAY (requestor),
240 gdk_x11_atom_to_xatom_for_display (display, selection),
241 gdk_x11_atom_to_xatom_for_display (display, target),
242 gdk_x11_atom_to_xatom_for_display (display, _gdk_selection_property),
243 GDK_WINDOW_XID (requestor), time);
247 * gdk_selection_property_get:
248 * @requestor: the window on which the data is stored
249 * @data: location to store a pointer to the retrieved data.
250 If the retrieval failed, %NULL we be stored here, otherwise, it
251 will be non-%NULL and the returned data should be freed with g_free()
252 when you are finished using it. The length of the
253 allocated memory is one more than the length
254 of the returned data, and the final byte will always
255 be zero, to ensure nul-termination of strings.
256 * @prop_type: location to store the type of the property.
257 * @prop_format: location to store the format of the property.
259 * Retrieves selection data that was stored by the selection
260 * data in response to a call to gdk_selection_convert(). This function
261 * will not be used by applications, who should use the #GtkClipboard
264 * Return value: the length of the retrieved data.
267 gdk_selection_property_get (GdkWindow *requestor,
274 gulong length = 0; /* Quiet GCC */
280 g_return_val_if_fail (requestor != NULL, 0);
281 g_return_val_if_fail (GDK_IS_WINDOW (requestor), 0);
283 display = GDK_WINDOW_DISPLAY (requestor);
285 if (GDK_WINDOW_DESTROYED (requestor))
290 /* We can't delete the selection here, because it might be the INCR
291 protocol, in which case the client has to make sure they'll be
292 notified of PropertyChange events _before_ the property is deleted.
293 Otherwise there's no guarantee we'll win the race ... */
294 if (XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (requestor),
295 GDK_DRAWABLE_XID (requestor),
296 gdk_x11_atom_to_xatom_for_display (display, _gdk_selection_property),
297 0, 0x1FFFFFFF /* MAXINT32 / 4 */, False,
298 AnyPropertyType, &prop_type, &prop_format,
299 &nitems, &nbytes, &t) != Success)
302 if (prop_type != None)
305 *ret_type = gdk_x11_xatom_to_atom_for_display (display, prop_type);
307 *ret_format = prop_format;
309 if (prop_type == XA_ATOM ||
310 prop_type == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
312 Atom* atoms = (Atom*) t;
316 if (prop_format != 32)
320 length = sizeof (GdkAtom) * num_atom + 1;
324 *data = g_malloc (length);
325 (*data)[length - 1] = '\0';
326 atoms_dest = (GdkAtom *)(*data);
328 for (i=0; i < num_atom; i++)
329 atoms_dest[i] = gdk_x11_xatom_to_atom_for_display (display, atoms[i]);
340 length = sizeof(short) * nitems;
343 length = sizeof(long) * nitems;
346 g_assert_not_reached ();
350 /* Add on an extra byte to handle null termination. X guarantees
351 that t will be 1 longer than nitems and null terminated */
355 *data = g_memdup (t, length);
366 *ret_type = GDK_NONE;
376 * gdk_selection_send_notify_for_display:
377 * @display: the #GdkDisplay where @requestor is realized
378 * @requestor: window to which to deliver response.
379 * @selection: selection that was requested.
380 * @target: target that was selected.
381 * @property: property in which the selection owner stored the data,
382 * or %GDK_NONE to indicate that the request was rejected.
385 * Send a response to SelectionRequest event.
390 gdk_selection_send_notify_for_display (GdkDisplay *display,
391 GdkNativeWindow requestor,
397 XSelectionEvent xevent;
399 g_return_if_fail (GDK_IS_DISPLAY (display));
401 xevent.type = SelectionNotify;
403 xevent.send_event = True;
404 xevent.requestor = requestor;
405 xevent.selection = gdk_x11_atom_to_xatom_for_display (display, selection);
406 xevent.target = gdk_x11_atom_to_xatom_for_display (display, target);
407 xevent.property = gdk_x11_atom_to_xatom_for_display (display, property);
410 _gdk_send_xevent (display, requestor, False, NoEventMask, (XEvent*) & xevent);
414 * gdk_text_property_to_text_list_for_display:
415 * @display: The #GdkDisplay where the encoding is defined.
416 * @encoding: an atom representing the encoding. The most
417 * common values for this are STRING, or COMPOUND_TEXT.
418 * This is value used as the type for the property.
419 * @format: the format of the property.
420 * @text: The text data.
421 * @length: The number of items to transform.
422 * @list: location to store a terminated array of strings in
423 * the encoding of the current locale. This array should be
424 * freed using gdk_free_text_list().
426 * Convert a text string from the encoding as it is stored
427 * in a property into an array of strings in the encoding of
428 * the current locale. (The elements of the array represent the
429 * nul-separated elements of the original text string.)
431 * Returns: the number of strings stored in list, or 0,
432 * if the conversion failed.
437 gdk_text_property_to_text_list_for_display (GdkDisplay *display,
444 XTextProperty property;
448 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
456 property.value = (guchar *)text;
457 property.encoding = gdk_x11_atom_to_xatom_for_display (display, encoding);
458 property.format = format;
459 property.nitems = length;
460 res = XmbTextPropertyToTextList (GDK_DISPLAY_XDISPLAY (display), &property,
461 &local_list, &count);
462 if (res == XNoMemory || res == XLocaleNotSupported || res == XConverterNotFound)
469 XFreeStringList (local_list);
476 gdk_free_text_list (gchar **list)
478 g_return_if_fail (list != NULL);
480 XFreeStringList (list);
484 make_list (const gchar *text,
489 GSList *strings = NULL;
492 const gchar *p = text;
495 GError *error = NULL;
497 while (p < text + length)
502 while (*q && q < text + length)
507 str = g_convert (p, q - p,
508 "UTF-8", "ISO-8859-1",
513 g_warning ("Error converting selection from STRING: %s",
515 g_error_free (error);
520 str = g_strndup (p, q - p);
521 if (!g_utf8_validate (str, -1, NULL))
523 g_warning ("Error converting selection from UTF8_STRING");
531 strings = g_slist_prepend (strings, str);
540 *list = g_new (gchar *, n_strings + 1);
541 (*list)[n_strings] = NULL;
549 (*list)[--i] = tmp_list->data;
551 g_free (tmp_list->data);
553 tmp_list = tmp_list->next;
556 g_slist_free (strings);
562 * gdk_text_property_to_utf8_list_for_display:
563 * @display: a #GdkDisplay
564 * @encoding: an atom representing the encoding of the text
565 * @format: the format of the property
566 * @text: the text to convert
567 * @length: the length of @text, in bytes
568 * @list: location to store the list of strings or %NULL. The
569 * list should be freed with g_strfreev().
571 * Converts a text property in the given encoding to
572 * a list of UTF-8 strings.
574 * Return value: the number of strings in the resulting
580 gdk_text_property_to_utf8_list_for_display (GdkDisplay *display,
587 g_return_val_if_fail (text != NULL, 0);
588 g_return_val_if_fail (length >= 0, 0);
589 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
591 if (encoding == GDK_TARGET_STRING)
593 return make_list ((gchar *)text, length, TRUE, list);
595 else if (encoding == gdk_atom_intern_static_string ("UTF8_STRING"))
597 return make_list ((gchar *)text, length, FALSE, list);
604 const gchar *charset = NULL;
605 gboolean need_conversion = !g_get_charset (&charset);
607 GError *error = NULL;
609 /* Probably COMPOUND text, we fall back to Xlib routines
611 local_count = gdk_text_property_to_text_list_for_display (display,
618 *list = g_new (gchar *, local_count + 1);
620 for (i=0; i<local_count; i++)
622 /* list contains stuff in our default encoding
626 gchar *utf = g_convert (local_list[i], -1,
632 (*list)[count++] = utf;
638 g_warning ("Error converting to UTF-8 from '%s': %s",
639 charset, error->message);
640 g_error_free (error);
648 if (g_utf8_validate (local_list[i], -1, NULL))
649 (*list)[count++] = g_strdup (local_list[i]);
651 g_warning ("Error converting selection");
657 gdk_free_text_list (local_list);
660 (*list)[count] = NULL;
667 * gdk_string_to_compound_text_for_display:
668 * @display: the #GdkDisplay where the encoding is defined.
669 * @str: a nul-terminated string.
670 * @encoding: location to store the encoding atom
671 * (to be used as the type for the property).
672 * @format: location to store the format of the property
673 * @ctext: location to store newly allocated data for the property.
674 * @length: the length of @text, in bytes
676 * Convert a string from the encoding of the current
677 * locale into a form suitable for storing in a window property.
679 * Returns: 0 upon success, non-zero upon failure.
684 gdk_string_to_compound_text_for_display (GdkDisplay *display,
692 XTextProperty property;
694 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
697 res = XLocaleNotSupported;
699 res = XmbTextListToTextProperty (GDK_DISPLAY_XDISPLAY (display),
700 (char **)&str, 1, XCompoundTextStyle,
704 property.encoding = None;
705 property.format = None;
706 property.value = NULL;
711 *encoding = gdk_x11_xatom_to_atom_for_display (display, property.encoding);
713 *format = property.format;
715 *ctext = property.value;
717 *length = property.nitems;
722 /* The specifications for COMPOUND_TEXT and STRING specify that C0 and
723 * C1 are not allowed except for \n and \t, however the X conversions
724 * routines for COMPOUND_TEXT only enforce this in one direction,
725 * causing cut-and-paste of \r and \r\n separated text to fail.
726 * This routine strips out all non-allowed C0 and C1 characters
727 * from the input string and also canonicalizes \r, and \r\n to \n
730 sanitize_utf8 (const gchar *src,
731 gboolean return_latin1)
733 gint len = strlen (src);
734 GString *result = g_string_sized_new (len);
735 const gchar *p = src;
745 g_string_append_c (result, '\n');
749 gunichar ch = g_utf8_get_char (p);
751 if (!((ch < 0x20 && ch != '\t' && ch != '\n') || (ch >= 0x7f && ch < 0xa0)))
756 g_string_append_c (result, ch);
758 g_string_append_printf (result,
759 ch < 0x10000 ? "\\u%04x" : "\\U%08x",
767 buflen = g_unichar_to_utf8 (ch, buf);
768 g_string_append_len (result, buf, buflen);
772 p = g_utf8_next_char (p);
776 return g_string_free (result, FALSE);
780 * gdk_utf8_to_string_target:
781 * @str: a UTF-8 string
783 * Converts an UTF-8 string into the best possible representation
784 * as a STRING. The representation of characters not in STRING
785 * is not specified; it may be as pseudo-escape sequences
786 * \x{ABCD}, or it may be in some other form of approximation.
788 * Return value: the newly-allocated string, or %NULL if the
789 * conversion failed. (It should not fail for
790 * any properly formed UTF-8 string unless system
791 * limits like memory or file descriptors are exceeded.)
794 gdk_utf8_to_string_target (const gchar *str)
796 return sanitize_utf8 (str, TRUE);
800 * gdk_utf8_to_compound_text_for_display:
801 * @display: a #GdkDisplay
802 * @str: a UTF-8 string
803 * @encoding: location to store resulting encoding
804 * @format: location to store format of the result
805 * @ctext: location to store the data of the result
806 * @length: location to store the length of the data
809 * Converts from UTF-8 to compound text.
811 * Return value: %TRUE if the conversion succeeded, otherwise
817 gdk_utf8_to_compound_text_for_display (GdkDisplay *display,
824 gboolean need_conversion;
825 const gchar *charset;
826 gchar *locale_str, *tmp_str;
827 GError *error = NULL;
830 g_return_val_if_fail (str != NULL, FALSE);
831 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
833 need_conversion = !g_get_charset (&charset);
835 tmp_str = sanitize_utf8 (str, FALSE);
839 locale_str = g_convert (tmp_str, -1,
846 if (!(error->domain = G_CONVERT_ERROR &&
847 error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
849 g_warning ("Error converting from UTF-8 to '%s': %s",
850 charset, error->message);
852 g_error_free (error);
867 locale_str = tmp_str;
869 result = gdk_string_to_compound_text_for_display (display, locale_str,
872 result = (result == Success? TRUE : FALSE);
879 void gdk_free_compound_text (guchar *ctext)
885 #define __GDK_SELECTION_X11_C__
886 #include "gdkaliasdef.c"