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 "gdkselection.h"
32 #include "gdkproperty.h"
33 #include "gdkprivate.h"
34 #include "gdkprivate-x11.h"
35 #include "gdkdisplay-x11.h"
38 #include <X11/Xatom.h>
42 typedef struct _OwnerInfo OwnerInfo;
51 static GSList *owner_list;
53 /* When a window is destroyed we check if it is the owner
54 * of any selections. This is somewhat inefficient, but
55 * owner_list is typically short, and it is a low memory,
59 _gdk_selection_window_destroyed (GdkWindow *window)
61 GSList *tmp_list = owner_list;
64 OwnerInfo *info = tmp_list->data;
65 tmp_list = tmp_list->next;
67 if (info->owner == window)
69 owner_list = g_slist_remove (owner_list, info);
75 /* We only pass through those SelectionClear events that actually
76 * reflect changes to the selection owner that we didn't make ourself.
79 _gdk_selection_filter_clear_event (XSelectionClearEvent *event)
81 GSList *tmp_list = owner_list;
82 GdkDisplay *display = gdk_x11_lookup_xdisplay (event->display);
86 OwnerInfo *info = tmp_list->data;
88 if (gdk_window_get_display (info->owner) == display &&
89 info->selection == gdk_x11_xatom_to_atom_for_display (display, event->selection))
91 if ((GDK_DRAWABLE_XID (info->owner) == event->window &&
92 event->serial >= info->serial))
94 owner_list = g_slist_remove (owner_list, info);
101 tmp_list = tmp_list->next;
107 * gdk_selection_owner_set_for_display:
108 * @display: the #GdkDisplay.
109 * @owner: a #GdkWindow or %NULL to indicate that the owner for
110 * the given should be unset.
111 * @selection: an atom identifying a selection.
112 * @time_: timestamp to use when setting the selection.
113 * If this is older than the timestamp given last time the owner was
114 * set for the given selection, the request will be ignored.
115 * @send_event: if %TRUE, and the new owner is different from the current
116 * owner, the current owner will be sent a SelectionClear event.
118 * Sets the #GdkWindow @owner as the current owner of the selection @selection.
120 * Returns: %TRUE if the selection owner was successfully changed to owner,
126 gdk_selection_owner_set_for_display (GdkDisplay *display,
138 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
139 g_return_val_if_fail (selection != GDK_NONE, FALSE);
146 if (GDK_WINDOW_DESTROYED (owner) || !GDK_WINDOW_IS_X11 (owner))
149 gdk_window_ensure_native (owner);
150 xdisplay = GDK_WINDOW_XDISPLAY (owner);
151 xwindow = GDK_WINDOW_XID (owner);
155 xdisplay = GDK_DISPLAY_XDISPLAY (display);
159 xselection = gdk_x11_atom_to_xatom_for_display (display, selection);
161 tmp_list = owner_list;
164 info = tmp_list->data;
165 if (info->selection == selection)
167 owner_list = g_slist_remove (owner_list, info);
171 tmp_list = tmp_list->next;
176 info = g_new (OwnerInfo, 1);
178 info->serial = NextRequest (GDK_WINDOW_XDISPLAY (owner));
179 info->selection = selection;
181 owner_list = g_slist_prepend (owner_list, info);
184 XSetSelectionOwner (xdisplay, xselection, xwindow, time);
186 return (XGetSelectionOwner (xdisplay, xselection) == xwindow);
190 * gdk_selection_owner_get_for_display:
191 * @display: a #GdkDisplay.
192 * @selection: an atom indentifying a selection.
194 * Determine the owner of the given selection.
196 * Note that the return value may be owned by a different
197 * process if a foreign window was previously created for that
198 * window, but a new foreign window will never be created by this call.
200 * Returns: if there is a selection owner for this window, and it is a
201 * window known to the current process, the #GdkWindow that owns the
202 * selection, otherwise %NULL.
207 gdk_selection_owner_get_for_display (GdkDisplay *display,
212 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
213 g_return_val_if_fail (selection != GDK_NONE, NULL);
218 xwindow = XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display),
219 gdk_x11_atom_to_xatom_for_display (display,
224 return gdk_window_lookup_for_display (display, xwindow);
228 gdk_selection_convert (GdkWindow *requestor,
235 g_return_if_fail (selection != GDK_NONE);
237 if (GDK_WINDOW_DESTROYED (requestor) || !GDK_WINDOW_IS_X11 (requestor))
240 gdk_window_ensure_native (requestor);
241 display = GDK_WINDOW_DISPLAY (requestor);
243 XConvertSelection (GDK_WINDOW_XDISPLAY (requestor),
244 gdk_x11_atom_to_xatom_for_display (display, selection),
245 gdk_x11_atom_to_xatom_for_display (display, target),
246 gdk_x11_atom_to_xatom_for_display (display, _gdk_selection_property),
247 GDK_WINDOW_XID (requestor), time);
251 * gdk_selection_property_get:
252 * @requestor: the window on which the data is stored
253 * @data: location to store a pointer to the retrieved data.
254 If the retrieval failed, %NULL we be stored here, otherwise, it
255 will be non-%NULL and the returned data should be freed with g_free()
256 when you are finished using it. The length of the
257 allocated memory is one more than the length
258 of the returned data, and the final byte will always
259 be zero, to ensure nul-termination of strings.
260 * @prop_type: location to store the type of the property.
261 * @prop_format: location to store the format of the property.
263 * Retrieves selection data that was stored by the selection
264 * data in response to a call to gdk_selection_convert(). This function
265 * will not be used by applications, who should use the #GtkClipboard
268 * Return value: the length of the retrieved data.
271 gdk_selection_property_get (GdkWindow *requestor,
278 gulong length = 0; /* Quiet GCC */
284 g_return_val_if_fail (requestor != NULL, 0);
285 g_return_val_if_fail (GDK_IS_WINDOW (requestor), 0);
286 g_return_val_if_fail (GDK_WINDOW_IS_X11 (requestor), 0);
288 display = GDK_WINDOW_DISPLAY (requestor);
290 if (GDK_WINDOW_DESTROYED (requestor) || !GDK_WINDOW_IS_X11 (requestor))
295 /* We can't delete the selection here, because it might be the INCR
296 protocol, in which case the client has to make sure they'll be
297 notified of PropertyChange events _before_ the property is deleted.
298 Otherwise there's no guarantee we'll win the race ... */
299 if (XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (requestor),
300 GDK_DRAWABLE_XID (requestor),
301 gdk_x11_atom_to_xatom_for_display (display, _gdk_selection_property),
302 0, 0x1FFFFFFF /* MAXINT32 / 4 */, False,
303 AnyPropertyType, &prop_type, &prop_format,
304 &nitems, &nbytes, &t) != Success)
307 if (prop_type != None)
310 *ret_type = gdk_x11_xatom_to_atom_for_display (display, prop_type);
312 *ret_format = prop_format;
314 if (prop_type == XA_ATOM ||
315 prop_type == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
317 Atom* atoms = (Atom*) t;
321 if (prop_format != 32)
325 length = sizeof (GdkAtom) * num_atom + 1;
329 *data = g_malloc (length);
330 (*data)[length - 1] = '\0';
331 atoms_dest = (GdkAtom *)(*data);
333 for (i=0; i < num_atom; i++)
334 atoms_dest[i] = gdk_x11_xatom_to_atom_for_display (display, atoms[i]);
345 length = sizeof(short) * nitems;
348 length = sizeof(long) * nitems;
351 g_assert_not_reached ();
355 /* Add on an extra byte to handle null termination. X guarantees
356 that t will be 1 longer than nitems and null terminated */
360 *data = g_memdup (t, length);
371 *ret_type = GDK_NONE;
381 * gdk_selection_send_notify_for_display:
382 * @display: the #GdkDisplay where @requestor is realized
383 * @requestor: window to which to deliver response.
384 * @selection: selection that was requested.
385 * @target: target that was selected.
386 * @property: property in which the selection owner stored the data,
387 * or %GDK_NONE to indicate that the request was rejected.
390 * Send a response to SelectionRequest event.
395 gdk_selection_send_notify_for_display (GdkDisplay *display,
396 GdkNativeWindow requestor,
402 XSelectionEvent xevent;
404 g_return_if_fail (GDK_IS_DISPLAY (display));
406 xevent.type = SelectionNotify;
408 xevent.send_event = True;
409 xevent.requestor = requestor;
410 xevent.selection = gdk_x11_atom_to_xatom_for_display (display, selection);
411 xevent.target = gdk_x11_atom_to_xatom_for_display (display, target);
412 if (property == GDK_NONE)
413 xevent.property = None;
415 xevent.property = gdk_x11_atom_to_xatom_for_display (display, property);
418 _gdk_send_xevent (display, requestor, False, NoEventMask, (XEvent*) & xevent);
422 * gdk_text_property_to_text_list_for_display:
423 * @display: The #GdkDisplay where the encoding is defined.
424 * @encoding: an atom representing the encoding. The most
425 * common values for this are STRING, or COMPOUND_TEXT.
426 * This is value used as the type for the property.
427 * @format: the format of the property.
428 * @text: The text data.
429 * @length: The number of items to transform.
430 * @list: location to store a terminated array of strings in
431 * the encoding of the current locale. This array should be
432 * freed using gdk_free_text_list().
434 * Convert a text string from the encoding as it is stored
435 * in a property into an array of strings in the encoding of
436 * the current locale. (The elements of the array represent the
437 * nul-separated elements of the original text string.)
439 * Returns: the number of strings stored in list, or 0,
440 * if the conversion failed.
445 gdk_text_property_to_text_list_for_display (GdkDisplay *display,
452 XTextProperty property;
456 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
464 property.value = (guchar *)text;
465 property.encoding = gdk_x11_atom_to_xatom_for_display (display, encoding);
466 property.format = format;
467 property.nitems = length;
468 res = XmbTextPropertyToTextList (GDK_DISPLAY_XDISPLAY (display), &property,
469 &local_list, &count);
470 if (res == XNoMemory || res == XLocaleNotSupported || res == XConverterNotFound)
477 XFreeStringList (local_list);
484 gdk_free_text_list (gchar **list)
486 g_return_if_fail (list != NULL);
488 XFreeStringList (list);
492 make_list (const gchar *text,
497 GSList *strings = NULL;
500 const gchar *p = text;
503 GError *error = NULL;
505 while (p < text + length)
510 while (*q && q < text + length)
515 str = g_convert (p, q - p,
516 "UTF-8", "ISO-8859-1",
521 g_warning ("Error converting selection from STRING: %s",
523 g_error_free (error);
528 str = g_strndup (p, q - p);
529 if (!g_utf8_validate (str, -1, NULL))
531 g_warning ("Error converting selection from UTF8_STRING");
539 strings = g_slist_prepend (strings, str);
548 *list = g_new (gchar *, n_strings + 1);
549 (*list)[n_strings] = NULL;
557 (*list)[--i] = tmp_list->data;
559 g_free (tmp_list->data);
561 tmp_list = tmp_list->next;
564 g_slist_free (strings);
570 * gdk_text_property_to_utf8_list_for_display:
571 * @display: a #GdkDisplay
572 * @encoding: an atom representing the encoding of the text
573 * @format: the format of the property
574 * @text: the text to convert
575 * @length: the length of @text, in bytes
576 * @list: location to store the list of strings or %NULL. The
577 * list should be freed with g_strfreev().
579 * Converts a text property in the given encoding to
580 * a list of UTF-8 strings.
582 * Return value: the number of strings in the resulting
588 gdk_text_property_to_utf8_list_for_display (GdkDisplay *display,
595 g_return_val_if_fail (text != NULL, 0);
596 g_return_val_if_fail (length >= 0, 0);
597 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
599 if (encoding == GDK_TARGET_STRING)
601 return make_list ((gchar *)text, length, TRUE, list);
603 else if (encoding == gdk_atom_intern_static_string ("UTF8_STRING"))
605 return make_list ((gchar *)text, length, FALSE, list);
612 const gchar *charset = NULL;
613 gboolean need_conversion = !g_get_charset (&charset);
615 GError *error = NULL;
617 /* Probably COMPOUND text, we fall back to Xlib routines
619 local_count = gdk_text_property_to_text_list_for_display (display,
626 *list = g_new (gchar *, local_count + 1);
628 for (i=0; i<local_count; i++)
630 /* list contains stuff in our default encoding
634 gchar *utf = g_convert (local_list[i], -1,
640 (*list)[count++] = utf;
646 g_warning ("Error converting to UTF-8 from '%s': %s",
647 charset, error->message);
648 g_error_free (error);
656 if (g_utf8_validate (local_list[i], -1, NULL))
657 (*list)[count++] = g_strdup (local_list[i]);
659 g_warning ("Error converting selection");
665 gdk_free_text_list (local_list);
668 (*list)[count] = NULL;
675 * gdk_string_to_compound_text_for_display:
676 * @display: the #GdkDisplay where the encoding is defined.
677 * @str: a nul-terminated string.
678 * @encoding: location to store the encoding atom
679 * (to be used as the type for the property).
680 * @format: location to store the format of the property
681 * @ctext: location to store newly allocated data for the property.
682 * @length: the length of @text, in bytes
684 * Convert a string from the encoding of the current
685 * locale into a form suitable for storing in a window property.
687 * Returns: 0 upon success, non-zero upon failure.
692 gdk_string_to_compound_text_for_display (GdkDisplay *display,
700 XTextProperty property;
702 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
705 res = XLocaleNotSupported;
707 res = XmbTextListToTextProperty (GDK_DISPLAY_XDISPLAY (display),
708 (char **)&str, 1, XCompoundTextStyle,
712 property.encoding = None;
713 property.format = None;
714 property.value = NULL;
719 *encoding = gdk_x11_xatom_to_atom_for_display (display, property.encoding);
721 *format = property.format;
723 *ctext = property.value;
725 *length = property.nitems;
730 /* The specifications for COMPOUND_TEXT and STRING specify that C0 and
731 * C1 are not allowed except for \n and \t, however the X conversions
732 * routines for COMPOUND_TEXT only enforce this in one direction,
733 * causing cut-and-paste of \r and \r\n separated text to fail.
734 * This routine strips out all non-allowed C0 and C1 characters
735 * from the input string and also canonicalizes \r, and \r\n to \n
738 sanitize_utf8 (const gchar *src,
739 gboolean return_latin1)
741 gint len = strlen (src);
742 GString *result = g_string_sized_new (len);
743 const gchar *p = src;
753 g_string_append_c (result, '\n');
757 gunichar ch = g_utf8_get_char (p);
759 if (!((ch < 0x20 && ch != '\t' && ch != '\n') || (ch >= 0x7f && ch < 0xa0)))
764 g_string_append_c (result, ch);
766 g_string_append_printf (result,
767 ch < 0x10000 ? "\\u%04x" : "\\U%08x",
775 buflen = g_unichar_to_utf8 (ch, buf);
776 g_string_append_len (result, buf, buflen);
780 p = g_utf8_next_char (p);
784 return g_string_free (result, FALSE);
788 * gdk_utf8_to_string_target:
789 * @str: a UTF-8 string
791 * Converts an UTF-8 string into the best possible representation
792 * as a STRING. The representation of characters not in STRING
793 * is not specified; it may be as pseudo-escape sequences
794 * \x{ABCD}, or it may be in some other form of approximation.
796 * Return value: the newly-allocated string, or %NULL if the
797 * conversion failed. (It should not fail for
798 * any properly formed UTF-8 string unless system
799 * limits like memory or file descriptors are exceeded.)
802 gdk_utf8_to_string_target (const gchar *str)
804 return sanitize_utf8 (str, TRUE);
808 * gdk_utf8_to_compound_text_for_display:
809 * @display: a #GdkDisplay
810 * @str: a UTF-8 string
811 * @encoding: location to store resulting encoding
812 * @format: location to store format of the result
813 * @ctext: location to store the data of the result
814 * @length: location to store the length of the data
817 * Converts from UTF-8 to compound text.
819 * Return value: %TRUE if the conversion succeeded, otherwise
825 gdk_utf8_to_compound_text_for_display (GdkDisplay *display,
832 gboolean need_conversion;
833 const gchar *charset;
834 gchar *locale_str, *tmp_str;
835 GError *error = NULL;
838 g_return_val_if_fail (str != NULL, FALSE);
839 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
841 need_conversion = !g_get_charset (&charset);
843 tmp_str = sanitize_utf8 (str, FALSE);
847 locale_str = g_convert (tmp_str, -1,
854 if (!(error->domain = G_CONVERT_ERROR &&
855 error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
857 g_warning ("Error converting from UTF-8 to '%s': %s",
858 charset, error->message);
860 g_error_free (error);
875 locale_str = tmp_str;
877 result = gdk_string_to_compound_text_for_display (display, locale_str,
880 result = (result == Success? TRUE : FALSE);
887 void gdk_free_compound_text (guchar *ctext)