Debian Codegraph @

gdk/x11/gdkselection-x11.c

raw
1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
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.
8 *
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.
13 *
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.
18 */
19
20 /*
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/.
25 */
26
27 #include "config.h"
28 #include <X11/Xlib.h>
29 #include <X11/Xatom.h>
30 #include <string.h>
31
32 #include "gdkx.h"
33 #include "gdkproperty.h"
34 #include "gdkselection.h"
35 #include "gdkprivate.h"
36 #include "gdkprivate-x11.h"
37 #include "gdkdisplay-x11.h"
38 #include "gdkalias.h"
39
40 typedef struct _OwnerInfo OwnerInfo;
41
42 struct _OwnerInfo
43 {
44 GdkAtom selection;
45 GdkWindow *owner;
46 gulong serial;
47 };
48
49 static GSList *owner_list;
50
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,
54 * low code solution
55 */
56 void
57 _gdk_selection_window_destroyed (GdkWindow *window)
58 {
59 GSList *tmp_list = owner_list;
60 while (tmp_list)
61 {
62 OwnerInfo *info = tmp_list->data;
63 tmp_list = tmp_list->next;
64
65 if (info->owner == window)
66 {
67 owner_list = g_slist_remove (owner_list, info);
68 g_free (info);
69 }
70 }
71 }
72
73 /* We only pass through those SelectionClear events that actually
74 * reflect changes to the selection owner that we didn't make ourself.
75 */
76 gboolean
77 _gdk_selection_filter_clear_event (XSelectionClearEvent *event)
78 {
79 GSList *tmp_list = owner_list;
80 GdkDisplay *display = gdk_x11_lookup_xdisplay (event->display);
81
82 while (tmp_list)
83 {
84 OwnerInfo *info = tmp_list->data;
85
86 if (gdk_drawable_get_display (info->owner) == display &&
87 info->selection == gdk_x11_xatom_to_atom_for_display (display, event->selection))
88 {
89 if ((GDK_DRAWABLE_XID (info->owner) == event->window &&
90 event->serial >= info->serial))
91 {
92 owner_list = g_slist_remove (owner_list, info);
93 g_free (info);
94 return TRUE;
95 }
96 else
97 return FALSE;
98 }
99 tmp_list = tmp_list->next;
100 }
101
102 return FALSE;
103 }
104 /**
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.
115 *
116 * Sets the #GdkWindow @owner as the current owner of the selection @selection.
117 *
118 * Returns: %TRUE if the selection owner was successfully changed to owner,
119 * otherwise %FALSE.
120 *
121 * Since: 2.2
122 */
123 gboolean
124 gdk_selection_owner_set_for_display (GdkDisplay *display,
125 GdkWindow *owner,
126 GdkAtom selection,
127 guint32 time,
128 gboolean send_event)
129 {
130 Display *xdisplay;
131 Window xwindow;
132 Atom xselection;
133 GSList *tmp_list;
134 OwnerInfo *info;
135
136 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
137 g_return_val_if_fail (selection != GDK_NONE, FALSE);
138
139 if (display->closed)
140 return FALSE;
141
142 if (owner)
143 {
144 if (GDK_WINDOW_DESTROYED (owner) || !GDK_WINDOW_IS_X11 (owner))
145 return FALSE;
146
147 gdk_window_ensure_native (owner);
148 xdisplay = GDK_WINDOW_XDISPLAY (owner);
149 xwindow = GDK_WINDOW_XID (owner);
150 }
151 else
152 {
153 xdisplay = GDK_DISPLAY_XDISPLAY (display);
154 xwindow = None;
155 }
156
157 xselection = gdk_x11_atom_to_xatom_for_display (display, selection);
158
159 tmp_list = owner_list;
160 while (tmp_list)
161 {
162 info = tmp_list->data;
163 if (info->selection == selection)
164 {
165 owner_list = g_slist_remove (owner_list, info);
166 g_free (info);
167 break;
168 }
169 tmp_list = tmp_list->next;
170 }
171
172 if (owner)
173 {
174 info = g_new (OwnerInfo, 1);
175 info->owner = owner;
176 info->serial = NextRequest (GDK_WINDOW_XDISPLAY (owner));
177 info->selection = selection;
178
179 owner_list = g_slist_prepend (owner_list, info);
180 }
181
182 XSetSelectionOwner (xdisplay, xselection, xwindow, time);
183
184 return (XGetSelectionOwner (xdisplay, xselection) == xwindow);
185 }
186
187 /**
188 * gdk_selection_owner_get_for_display:
189 * @display: a #GdkDisplay.
190 * @selection: an atom indentifying a selection.
191 *
192 * Determine the owner of the given selection.
193 *
194 * Note that the return value may be owned by a different
195 * process if a foreign window was previously created for that
196 * window, but a new foreign window will never be created by this call.
197 *
198 * Returns: if there is a selection owner for this window, and it is a
199 * window known to the current process, the #GdkWindow that owns the
200 * selection, otherwise %NULL.
201 *
202 * Since: 2.2
203 */
204 GdkWindow *
205 gdk_selection_owner_get_for_display (GdkDisplay *display,
206 GdkAtom selection)
207 {
208 Window xwindow;
209
210 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
211 g_return_val_if_fail (selection != GDK_NONE, NULL);
212
213 if (display->closed)
214 return NULL;
215
216 xwindow = XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display),
217 gdk_x11_atom_to_xatom_for_display (display,
218 selection));
219 if (xwindow == None)
220 return NULL;
221
222 return gdk_window_lookup_for_display (display, xwindow);
223 }
224
225 void
226 gdk_selection_convert (GdkWindow *requestor,
227 GdkAtom selection,
228 GdkAtom target,
229 guint32 time)
230 {
231 GdkDisplay *display;
232
233 g_return_if_fail (selection != GDK_NONE);
234
235 if (GDK_WINDOW_DESTROYED (requestor) || !GDK_WINDOW_IS_X11 (requestor))
236 return;
237
238 gdk_window_ensure_native (requestor);
239 display = GDK_WINDOW_DISPLAY (requestor);
240
241 XConvertSelection (GDK_WINDOW_XDISPLAY (requestor),
242 gdk_x11_atom_to_xatom_for_display (display, selection),
243 gdk_x11_atom_to_xatom_for_display (display, target),
244 gdk_x11_atom_to_xatom_for_display (display, _gdk_selection_property),
245 GDK_WINDOW_XID (requestor), time);
246 }
247
248 /**
249 * gdk_selection_property_get:
250 * @requestor: the window on which the data is stored
251 * @data: location to store a pointer to the retrieved data.
252 If the retrieval failed, %NULL we be stored here, otherwise, it
253 will be non-%NULL and the returned data should be freed with g_free()
254 when you are finished using it. The length of the
255 allocated memory is one more than the length
256 of the returned data, and the final byte will always
257 be zero, to ensure nul-termination of strings.
258 * @prop_type: location to store the type of the property.
259 * @prop_format: location to store the format of the property.
260 *
261 * Retrieves selection data that was stored by the selection
262 * data in response to a call to gdk_selection_convert(). This function
263 * will not be used by applications, who should use the #GtkClipboard
264 * API instead.
265 *
266 * Return value: the length of the retrieved data.
267 **/
268 gint
269 gdk_selection_property_get (GdkWindow *requestor,
270 guchar **data,
271 GdkAtom *ret_type,
272 gint *ret_format)
273 {
274 gulong nitems;
275 gulong nbytes;
276 gulong length = 0; /* Quiet GCC */
277 Atom prop_type;
278 gint prop_format;
279 guchar *t = NULL;
280 GdkDisplay *display;
281
282 g_return_val_if_fail (requestor != NULL, 0);
283 g_return_val_if_fail (GDK_IS_WINDOW (requestor), 0);
284 g_return_val_if_fail (GDK_WINDOW_IS_X11 (requestor), 0);
285
286 display = GDK_WINDOW_DISPLAY (requestor);
287
288 if (GDK_WINDOW_DESTROYED (requestor) || !GDK_WINDOW_IS_X11 (requestor))
289 goto err;
290
291 t = NULL;
292
293 /* We can't delete the selection here, because it might be the INCR
294 protocol, in which case the client has to make sure they'll be
295 notified of PropertyChange events _before_ the property is deleted.
296 Otherwise there's no guarantee we'll win the race ... */
297 if (XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (requestor),
298 GDK_DRAWABLE_XID (requestor),
299 gdk_x11_atom_to_xatom_for_display (display, _gdk_selection_property),
300 0, 0x1FFFFFFF /* MAXINT32 / 4 */, False,
301 AnyPropertyType, &prop_type, &prop_format,
302 &nitems, &nbytes, &t) != Success)
303 goto err;
304
305 if (prop_type != None)
306 {
307 if (ret_type)
308 *ret_type = gdk_x11_xatom_to_atom_for_display (display, prop_type);
309 if (ret_format)
310 *ret_format = prop_format;
311
312 if (prop_type == XA_ATOM ||
313 prop_type == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR"))
314 {
315 Atom* atoms = (Atom*) t;
316 GdkAtom* atoms_dest;
317 gint num_atom, i;
318
319 if (prop_format != 32)
320 goto err;
321
322 num_atom = nitems;
323 length = sizeof (GdkAtom) * num_atom + 1;
324
325 if (data)
326 {
327 *data = g_malloc (length);
328 (*data)[length - 1] = '\0';
329 atoms_dest = (GdkAtom *)(*data);
330
331 for (i=0; i < num_atom; i++)
332 atoms_dest[i] = gdk_x11_xatom_to_atom_for_display (display, atoms[i]);
333 }
334 }
335 else
336 {
337 switch (prop_format)
338 {
339 case 8:
340 length = nitems;
341 break;
342 case 16:
343 length = sizeof(short) * nitems;
344 break;
345 case 32:
346 length = sizeof(long) * nitems;
347 break;
348 default:
349 g_assert_not_reached ();
350 break;
351 }
352
353 /* Add on an extra byte to handle null termination. X guarantees
354 that t will be 1 longer than nitems and null terminated */
355 length += 1;
356
357 if (data)
358 *data = g_memdup (t, length);
359 }
360
361 if (t)
362 XFree (t);
363
364 return length - 1;
365 }
366
367 err:
368 if (ret_type)
369 *ret_type = GDK_NONE;
370 if (ret_format)
371 *ret_format = 0;
372 if (data)
373 *data = NULL;
374
375 return 0;
376 }
377
378 /**
379 * gdk_selection_send_notify_for_display:
380 * @display: the #GdkDisplay where @requestor is realized
381 * @requestor: window to which to deliver response.
382 * @selection: selection that was requested.
383 * @target: target that was selected.
384 * @property: property in which the selection owner stored the data,
385 * or %GDK_NONE to indicate that the request was rejected.
386 * @time_: timestamp.
387 *
388 * Send a response to SelectionRequest event.
389 *
390 * Since: 2.2
391 **/
392 void
393 gdk_selection_send_notify_for_display (GdkDisplay *display,
394 GdkNativeWindow requestor,
395 GdkAtom selection,
396 GdkAtom target,
397 GdkAtom property,
398 guint32 time)
399 {
400 XSelectionEvent xevent;
401
402 g_return_if_fail (GDK_IS_DISPLAY (display));
403
404 xevent.type = SelectionNotify;
405 xevent.serial = 0;
406 xevent.send_event = True;
407 xevent.requestor = requestor;
408 xevent.selection = gdk_x11_atom_to_xatom_for_display (display, selection);
409 xevent.target = gdk_x11_atom_to_xatom_for_display (display, target);
410 if (property == GDK_NONE)
411 xevent.property = None;
412 else
413 xevent.property = gdk_x11_atom_to_xatom_for_display (display, property);
414 xevent.time = time;
415
416 _gdk_send_xevent (display, requestor, False, NoEventMask, (XEvent*) & xevent);
417 }
418
419 /**
420 * gdk_text_property_to_text_list_for_display:
421 * @display: The #GdkDisplay where the encoding is defined.
422 * @encoding: an atom representing the encoding. The most
423 * common values for this are STRING, or COMPOUND_TEXT.
424 * This is value used as the type for the property.
425 * @format: the format of the property.
426 * @text: The text data.
427 * @length: The number of items to transform.
428 * @list: location to store a terminated array of strings in
429 * the encoding of the current locale. This array should be
430 * freed using gdk_free_text_list().
431 *
432 * Convert a text string from the encoding as it is stored
433 * in a property into an array of strings in the encoding of
434 * the current locale. (The elements of the array represent the
435 * nul-separated elements of the original text string.)
436 *
437 * Returns: the number of strings stored in list, or 0,
438 * if the conversion failed.
439 *
440 * Since: 2.2
441 *
442 * Deprecated:2.24: Use gdk_x11_display_text_property_to_text_list()
443 */
444 gint
445 gdk_text_property_to_text_list_for_display (GdkDisplay *display,
446 GdkAtom encoding,
447 gint format,
448 const guchar *text,
449 gint length,
450 gchar ***list)
451 {
452 return gdk_x11_display_text_property_to_text_list (display,
453 encoding,
454 format,
455 text,
456 length,
457 list);
458 }
459
460 gint
461 gdk_x11_display_text_property_to_text_list (GdkDisplay *display,
462 GdkAtom encoding,
463 gint format,
464 const guchar *text,
465 gint length,
466 gchar ***list)
467 {
468 XTextProperty property;
469 gint count = 0;
470 gint res;
471 gchar **local_list;
472 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
473
474 if (list)
475 *list = NULL;
476
477 if (display->closed)
478 return 0;
479
480 property.value = (guchar *)text;
481 property.encoding = gdk_x11_atom_to_xatom_for_display (display, encoding);
482 property.format = format;
483 property.nitems = length;
484 res = XmbTextPropertyToTextList (GDK_DISPLAY_XDISPLAY (display), &property,
485 &local_list, &count);
486 if (res == XNoMemory || res == XLocaleNotSupported || res == XConverterNotFound)
487 return 0;
488 else
489 {
490 if (list)
491 *list = local_list;
492 else
493 XFreeStringList (local_list);
494
495 return count;
496 }
497 }
498
499 void
500 gdk_free_text_list (gchar **list)
501 {
502 gdk_x11_free_text_list (list);
503 }
504
505 void
506 gdk_x11_free_text_list (gchar **list)
507 {
508 g_return_if_fail (list != NULL);
509
510 XFreeStringList (list);
511 }
512
513 static gint
514 make_list (const gchar *text,
515 gint length,
516 gboolean latin1,
517 gchar ***list)
518 {
519 GSList *strings = NULL;
520 gint n_strings = 0;
521 gint i;
522 const gchar *p = text;
523 const gchar *q;
524 GSList *tmp_list;
525 GError *error = NULL;
526
527 while (p < text + length)
528 {
529 gchar *str;
530
531 q = p;
532 while (*q && q < text + length)
533 q++;
534
535 if (latin1)
536 {
537 str = g_convert (p, q - p,
538 "UTF-8", "ISO-8859-1",
539 NULL, NULL, &error);
540
541 if (!str)
542 {
543 g_warning ("Error converting selection from STRING: %s",
544 error->message);
545 g_error_free (error);
546 }
547 }
548 else
549 {
550 str = g_strndup (p, q - p);
551 if (!g_utf8_validate (str, -1, NULL))
552 {
553 g_warning ("Error converting selection from UTF8_STRING");
554 g_free (str);
555 str = NULL;
556 }
557 }
558
559 if (str)
560 {
561 strings = g_slist_prepend (strings, str);
562 n_strings++;
563 }
564
565 p = q + 1;
566 }
567
568 if (list)
569 {
570 *list = g_new (gchar *, n_strings + 1);
571 (*list)[n_strings] = NULL;
572 }
573
574 i = n_strings;
575 tmp_list = strings;
576 while (tmp_list)
577 {
578 if (list)
579 (*list)[--i] = tmp_list->data;
580 else
581 g_free (tmp_list->data);
582
583 tmp_list = tmp_list->next;
584 }
585
586 g_slist_free (strings);
587
588 return n_strings;
589 }
590
591 /**
592 * gdk_text_property_to_utf8_list_for_display:
593 * @display: a #GdkDisplay
594 * @encoding: an atom representing the encoding of the text
595 * @format: the format of the property
596 * @text: the text to convert
597 * @length: the length of @text, in bytes
598 * @list: location to store the list of strings or %NULL. The
599 * list should be freed with g_strfreev().
600 *
601 * Converts a text property in the given encoding to
602 * a list of UTF-8 strings.
603 *
604 * Return value: the number of strings in the resulting
605 * list.
606 *
607 * Since: 2.2
608 **/
609 gint
610 gdk_text_property_to_utf8_list_for_display (GdkDisplay *display,
611 GdkAtom encoding,
612 gint format,
613 const guchar *text,
614 gint length,
615 gchar ***list)
616 {
617 g_return_val_if_fail (text != NULL, 0);
618 g_return_val_if_fail (length >= 0, 0);
619 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
620
621 if (encoding == GDK_TARGET_STRING)
622 {
623 return make_list ((gchar *)text, length, TRUE, list);
624 }
625 else if (encoding == gdk_atom_intern_static_string ("UTF8_STRING"))
626 {
627 return make_list ((gchar *)text, length, FALSE, list);
628 }
629 else
630 {
631 gchar **local_list;
632 gint local_count;
633 gint i;
634 const gchar *charset = NULL;
635 gboolean need_conversion = !g_get_charset (&charset);
636 gint count = 0;
637 GError *error = NULL;
638
639 /* Probably COMPOUND text, we fall back to Xlib routines
640 */
641 local_count = gdk_text_property_to_text_list_for_display (display,
642 encoding,
643 format,
644 text,
645 length,
646 &local_list);
647 if (list)
648 *list = g_new (gchar *, local_count + 1);
649
650 for (i=0; i<local_count; i++)
651 {
652 /* list contains stuff in our default encoding
653 */
654 if (need_conversion)
655 {
656 gchar *utf = g_convert (local_list[i], -1,
657 "UTF-8", charset,
658 NULL, NULL, &error);
659 if (utf)
660 {
661 if (list)
662 (*list)[count++] = utf;
663 else
664 g_free (utf);
665 }
666 else
667 {
668 g_warning ("Error converting to UTF-8 from '%s': %s",
669 charset, error->message);
670 g_error_free (error);
671 error = NULL;
672 }
673 }
674 else
675 {
676 if (list)
677 {
678 if (g_utf8_validate (local_list[i], -1, NULL))
679 (*list)[count++] = g_strdup (local_list[i]);
680 else
681 g_warning ("Error converting selection");
682 }
683 }
684 }
685
686 if (local_count)
687 gdk_free_text_list (local_list);
688
689 if (list)
690 (*list)[count] = NULL;
691
692 return count;
693 }
694 }
695
696 /**
697 * gdk_string_to_compound_text_for_display:
698 * @display: the #GdkDisplay where the encoding is defined.
699 * @str: a nul-terminated string.
700 * @encoding: location to store the encoding atom
701 * (to be used as the type for the property).
702 * @format: location to store the format of the property
703 * @ctext: location to store newly allocated data for the property.
704 * @length: the length of @text, in bytes
705 *
706 * Convert a string from the encoding of the current
707 * locale into a form suitable for storing in a window property.
708 *
709 * Returns: 0 upon success, non-zero upon failure.
710 *
711 * Since: 2.2
712 *
713 * Deprecated:2.24: Use gdk_x11_display_string_to_compound_text()
714 **/
715 gint
716 gdk_string_to_compound_text_for_display (GdkDisplay *display,
717 const gchar *str,
718 GdkAtom *encoding,
719 gint *format,
720 guchar **ctext,
721 gint *length)
722 {
723 return gdk_x11_display_string_to_compound_text (display, str, encoding, format, ctext, length);
724 }
725
726 gint
727 gdk_x11_display_string_to_compound_text (GdkDisplay *display,
728 const gchar *str,
729 GdkAtom *encoding,
730 gint *format,
731 guchar **ctext,
732 gint *length)
733 {
734 gint res;
735 XTextProperty property;
736
737 g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
738
739 if (display->closed)
740 res = XLocaleNotSupported;
741 else
742 res = XmbTextListToTextProperty (GDK_DISPLAY_XDISPLAY (display),
743 (char **)&str, 1, XCompoundTextStyle,
744 &property);
745 if (res != Success)
746 {
747 property.encoding = None;
748 property.format = None;
749 property.value = NULL;
750 property.nitems = 0;
751 }
752
753 if (encoding)
754 *encoding = gdk_x11_xatom_to_atom_for_display (display, property.encoding);
755 if (format)
756 *format = property.format;
757 if (ctext)
758 *ctext = property.value;
759 if (length)
760 *length = property.nitems;
761
762 return res;
763 }
764
765 /* The specifications for COMPOUND_TEXT and STRING specify that C0 and
766 * C1 are not allowed except for \n and \t, however the X conversions
767 * routines for COMPOUND_TEXT only enforce this in one direction,
768 * causing cut-and-paste of \r and \r\n separated text to fail.
769 * This routine strips out all non-allowed C0 and C1 characters
770 * from the input string and also canonicalizes \r, and \r\n to \n
771 */
772 static gchar *
773 sanitize_utf8 (const gchar *src,
774 gboolean return_latin1)
775 {
776 gint len = strlen (src);
777 GString *result = g_string_sized_new (len);
778 const gchar *p = src;
779
780 while (*p)
781 {
782 if (*p == '\r')
783 {
784 p++;
785 if (*p == '\n')
786 p++;
787
788 g_string_append_c (result, '\n');
789 }
790 else
791 {
792 gunichar ch = g_utf8_get_char (p);
793
794 if (!((ch < 0x20 && ch != '\t' && ch != '\n') || (ch >= 0x7f && ch < 0xa0)))
795 {
796 if (return_latin1)
797 {
798 if (ch <= 0xff)
799 g_string_append_c (result, ch);
800 else
801 g_string_append_printf (result,
802 ch < 0x10000 ? "\\u%04x" : "\\U%08x",
803 ch);
804 }
805 else
806 {
807 char buf[7];
808 gint buflen;
809
810 buflen = g_unichar_to_utf8 (ch, buf);
811 g_string_append_len (result, buf, buflen);
812 }
813 }
814
815 p = g_utf8_next_char (p);
816 }
817 }
818
819 return g_string_free (result, FALSE);
820 }
821
822 /**
823 * gdk_utf8_to_string_target:
824 * @str: a UTF-8 string
825 *
826 * Converts an UTF-8 string into the best possible representation
827 * as a STRING. The representation of characters not in STRING
828 * is not specified; it may be as pseudo-escape sequences
829 * \x{ABCD}, or it may be in some other form of approximation.
830 *
831 * Return value: the newly-allocated string, or %NULL if the
832 * conversion failed. (It should not fail for
833 * any properly formed UTF-8 string unless system
834 * limits like memory or file descriptors are exceeded.)
835 **/
836 gchar *
837 gdk_utf8_to_string_target (const gchar *str)
838 {
839 return sanitize_utf8 (str, TRUE);
840 }
841
842 /**
843 * gdk_utf8_to_compound_text_for_display:
844 * @display: a #GdkDisplay
845 * @str: a UTF-8 string
846 * @encoding: location to store resulting encoding
847 * @format: location to store format of the result
848 * @ctext: location to store the data of the result
849 * @length: location to store the length of the data
850 * stored in @ctext
851 *
852 * Converts from UTF-8 to compound text.
853 *
854 * Return value: %TRUE if the conversion succeeded, otherwise
855 * %FALSE.
856 *
857 * Since: 2.2
858 *
859 * Deprecated:2.24: Use gdk_x11_display_utf8_to_compound_text()
860 **/
861 gboolean
862 gdk_utf8_to_compound_text_for_display (GdkDisplay *display,
863 const gchar *str,
864 GdkAtom *encoding,
865 gint *format,
866 guchar **ctext,
867 gint *length)
868 {
869 return gdk_x11_display_utf8_to_compound_text (display, str, encoding, format, ctext, length);
870 }
871
872 gboolean
873 gdk_x11_display_utf8_to_compound_text (GdkDisplay *display,
874 const gchar *str,
875 GdkAtom *encoding,
876 gint *format,
877 guchar **ctext,
878 gint *length)
879 {
880 gboolean need_conversion;
881 const gchar *charset;
882 gchar *locale_str, *tmp_str;
883 GError *error = NULL;
884 gboolean result;
885
886 g_return_val_if_fail (str != NULL, FALSE);
887 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
888
889 need_conversion = !g_get_charset (&charset);
890
891 tmp_str = sanitize_utf8 (str, FALSE);
892
893 if (need_conversion)
894 {
895 locale_str = g_convert (tmp_str, -1,
896 charset, "UTF-8",
897 NULL, NULL, &error);
898 g_free (tmp_str);
899
900 if (!locale_str)
901 {
902 if (!(error->domain = G_CONVERT_ERROR &&
903 error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
904 {
905 g_warning ("Error converting from UTF-8 to '%s': %s",
906 charset, error->message);
907 }
908 g_error_free (error);
909
910 if (encoding)
911 *encoding = None;
912 if (format)
913 *format = None;
914 if (ctext)
915 *ctext = NULL;
916 if (length)
917 *length = 0;
918
919 return FALSE;
920 }
921 }
922 else
923 locale_str = tmp_str;
924
925 result = gdk_string_to_compound_text_for_display (display, locale_str,
926 encoding, format,
927 ctext, length);
928 result = (result == Success? TRUE : FALSE);
929
930 g_free (locale_str);
931
932 return result;
933 }
934
935 void gdk_free_compound_text (guchar *ctext)
936 {
937 gdk_x11_free_compound_text (ctext);
938 }
939
940 void gdk_x11_free_compound_text (guchar *ctext)
941 {
942 if (ctext)
943 XFree (ctext);
944 }
945
946 #define __GDK_SELECTION_X11_C__
947 #include "gdkaliasdef.c"