]> Pileus Git - ~andy/gtk/blob - gdk/win32/gdkselection-win32.c
don't reduce the scroll rect size by the scroll amount
[~andy/gtk] / gdk / win32 / gdkselection-win32.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  * Copyright (C) 1998-2002 Tor Lillqvist
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 #include <string.h>
29 #include <stdlib.h>
30
31 #include "gdkproperty.h"
32 #include "gdkselection.h"
33 #include "gdkprivate-win32.h"
34
35 /* We emulate the GDK_SELECTION window properties of windows (as used
36  * in the X11 backend) by using a hash table from GdkWindows to
37  * GdkSelProp structs.
38  */
39
40 typedef struct {
41   guchar *data;
42   gint length;
43   gint format;
44   GdkAtom type;
45 } GdkSelProp;
46
47 static GHashTable *sel_prop_table = NULL;
48
49 static GdkSelProp *dropfiles_prop = NULL;
50
51 /* We store the owner of each selection in this table. Obviously, this only
52  * is valid intra-app, and in fact it is necessary for the intra-app DND to work.
53  */
54 static GHashTable *sel_owner_table = NULL;
55
56 void
57 _gdk_win32_selection_init (void)
58 {
59   sel_prop_table = g_hash_table_new (NULL, NULL);
60   sel_owner_table = g_hash_table_new (NULL, NULL);
61 }
62
63 void
64 _gdk_selection_property_store (GdkWindow *owner,
65                                GdkAtom    type,
66                                gint       format,
67                                guchar    *data,
68                                gint       length)
69 {
70   GdkSelProp *prop;
71
72   prop = g_hash_table_lookup (sel_prop_table, GDK_WINDOW_HWND (owner));
73   if (prop != NULL)
74     {
75       g_free (prop->data);
76       g_hash_table_remove (sel_prop_table, GDK_WINDOW_HWND (owner));
77     }
78   prop = g_new (GdkSelProp, 1);
79   prop->data = data;
80   prop->length = length;
81   prop->format = format;
82   prop->type = type;
83   g_hash_table_insert (sel_prop_table, GDK_WINDOW_HWND (owner), prop);
84 }
85
86 void
87 _gdk_dropfiles_store (gchar *data)
88 {
89   if (data != NULL)
90     {
91       g_assert (dropfiles_prop == NULL);
92
93       dropfiles_prop = g_new (GdkSelProp, 1);
94       dropfiles_prop->data = data;
95       dropfiles_prop->length = strlen (data);
96       dropfiles_prop->format = 8;
97       dropfiles_prop->type = text_uri_list;
98     }
99   else
100     {
101       if (dropfiles_prop != NULL)
102         {
103           g_free (dropfiles_prop->data);
104           g_free (dropfiles_prop);
105         }
106       dropfiles_prop = NULL;
107     }
108 }
109
110 gboolean
111 gdk_selection_owner_set (GdkWindow *owner,
112                          GdkAtom    selection,
113                          guint32    time,
114                          gboolean   send_event)
115 {
116   HWND xwindow;
117   GdkEvent tmp_event;
118   gchar *sel_name;
119
120   GDK_NOTE (DND,
121             (sel_name = gdk_atom_name (selection),
122              g_print ("gdk_selection_owner_set: %#x %#x (%s)\n",
123                       (owner ? (guint) GDK_WINDOW_HWND (owner) : 0),
124                       (guint) selection, sel_name),
125              g_free (sel_name)));
126
127   if (selection != GDK_SELECTION_CLIPBOARD)
128     {
129       if (owner != NULL)
130         g_hash_table_insert (sel_owner_table, selection, GDK_WINDOW_HWND (owner));
131       else
132         g_hash_table_remove (sel_owner_table, selection);
133       return TRUE;
134     }
135
136   /* Rest of this function handles the CLIPBOARD selection */
137   if (owner != NULL)
138     {
139       if (GDK_WINDOW_DESTROYED (owner))
140         return FALSE;
141
142       xwindow = GDK_WINDOW_HWND (owner);
143     }
144   else
145     xwindow = NULL;
146
147   if (!OpenClipboard (xwindow))
148     {
149       WIN32_API_FAILED ("OpenClipboard");
150       return FALSE;
151     }
152   if (!EmptyClipboard ())
153     {
154       WIN32_API_FAILED ("EmptyClipboard");
155       CloseClipboard ();
156       return FALSE;
157     }
158 #if 0
159   /* No delayed rendering */
160   if (xwindow != NULL)
161     SetClipboardData (CF_TEXT, NULL);
162 #endif
163   if (!CloseClipboard ())
164     {
165       WIN32_API_FAILED ("CloseClipboard");
166       return FALSE;
167     }
168
169   if (owner != NULL)
170     {
171       /* Send ourselves a selection request message so that
172        * gdk_property_change will be called to store the clipboard
173        * data.
174        */
175       GDK_NOTE (DND, g_print ("...sending GDK_SELECTION_REQUEST to ourselves\n"));
176       tmp_event.selection.type = GDK_SELECTION_REQUEST;
177       tmp_event.selection.window = owner;
178       tmp_event.selection.send_event = FALSE;
179       tmp_event.selection.selection = selection;
180       tmp_event.selection.target = GDK_TARGET_STRING;
181       tmp_event.selection.property = _gdk_selection_property;
182       tmp_event.selection.requestor = (guint32) xwindow;
183       tmp_event.selection.time = time;
184
185       gdk_event_put (&tmp_event);
186     }
187
188   return TRUE;
189 }
190
191 GdkWindow*
192 gdk_selection_owner_get (GdkAtom selection)
193 {
194   GdkWindow *window;
195   gchar *sel_name;
196
197   /* Return NULL for CLIPBOARD, because otherwise cut&paste
198    * inside the same application doesn't work. We must pretend to gtk
199    * that we don't have the selection, so that we always fetch it from
200    * the Windows clipboard. See also comments in
201    * gdk_selection_send_notify().
202    */
203   if (selection == GDK_SELECTION_CLIPBOARD)
204     return NULL;
205
206   window = gdk_window_lookup ((GdkNativeWindow) g_hash_table_lookup (sel_owner_table, selection));
207
208   GDK_NOTE (DND,
209             (sel_name = gdk_atom_name (selection),
210              g_print ("gdk_selection_owner_get: %#x (%s) = %#x\n",
211                       (guint) selection, sel_name,
212                       (window ? (guint) GDK_WINDOW_HWND (window) : 0)),
213              g_free (sel_name)));
214
215   return window;
216 }
217
218 static void
219 generate_selection_notify (GdkWindow *requestor,
220                            GdkAtom    selection,
221                            GdkAtom    target,
222                            GdkAtom    property,
223                            guint32    time)
224 {
225   GdkEvent tmp_event;
226
227   tmp_event.selection.type = GDK_SELECTION_NOTIFY;
228   tmp_event.selection.window = requestor;
229   tmp_event.selection.send_event = FALSE;
230   tmp_event.selection.selection = selection;
231   tmp_event.selection.target = target;
232   tmp_event.selection.property = property;
233   tmp_event.selection.requestor = 0;
234   tmp_event.selection.time = time;
235
236   gdk_event_put (&tmp_event);
237 }
238
239 void
240 gdk_selection_convert (GdkWindow *requestor,
241                        GdkAtom    selection,
242                        GdkAtom    target,
243                        guint32    time)
244 {
245   HGLOBAL hdata;
246   GdkAtom property = _gdk_selection_property;
247   gchar *sel_name, *tgt_name;
248
249   g_return_if_fail (requestor != NULL);
250   if (GDK_WINDOW_DESTROYED (requestor))
251     return;
252
253   GDK_NOTE (DND,
254             (sel_name = gdk_atom_name (selection),
255              tgt_name = gdk_atom_name (target),
256              g_print ("gdk_selection_convert: %#x %#x (%s) %#x (%s)\n",
257                       (guint) GDK_WINDOW_HWND (requestor),
258                       (guint) selection, sel_name,
259                       (guint) target, tgt_name),
260              g_free (sel_name),
261              g_free (tgt_name)));
262
263   if (selection == GDK_SELECTION_CLIPBOARD &&
264       target == gdk_atom_intern ("TARGETS", FALSE))
265     {
266       /* He wants to know what formats are on the clipboard.  If there
267        * is some kind of text, tell him so.
268        */
269       if (!OpenClipboard (GDK_WINDOW_HWND (requestor)))
270         {
271           WIN32_API_FAILED ("OpenClipboard");
272           return;
273         }
274
275       if (IsClipboardFormatAvailable (CF_UNICODETEXT) ||
276           IsClipboardFormatAvailable (cf_utf8_string) ||
277           IsClipboardFormatAvailable (CF_TEXT))
278         {
279           GdkAtom *data = g_new (GdkAtom, 1);
280           *data = GDK_TARGET_STRING;
281           _gdk_selection_property_store (requestor, GDK_SELECTION_TYPE_ATOM,
282                                          32, (guchar *) data, 1 * sizeof (GdkAtom));
283         }
284       else
285         property = GDK_NONE;
286     }
287   else if (selection == GDK_SELECTION_CLIPBOARD &&
288            (target == compound_text ||
289             target == GDK_TARGET_STRING))
290     {
291       /* Converting the CLIPBOARD selection means he wants the
292        * contents of the clipboard. Get the clipboard data,
293        * and store it for later.
294        */
295       if (!OpenClipboard (GDK_WINDOW_HWND (requestor)))
296         {
297           WIN32_API_FAILED ("OpenClipboard");
298           return;
299         }
300
301       /* Try various formats. First the simplest, CF_UNICODETEXT. */
302       if ((hdata = GetClipboardData (CF_UNICODETEXT)) != NULL)
303         {
304           wchar_t *ptr, *wcs, *p, *q;
305           guchar *data;
306           gint length, wclen;
307
308           if ((ptr = GlobalLock (hdata)) != NULL)
309             {
310               length = GlobalSize (hdata);
311               
312               GDK_NOTE (DND, g_print ("...CF_UNICODETEXT: %d bytes\n",
313                                       length));
314
315               /* Strip out \r */
316               wcs = g_new (wchar_t, (length + 1) * 2);
317               p = ptr;
318               q = wcs;
319               wclen = 0;
320               while (*p)
321                 {
322                   if (*p != '\r')
323                     {
324                       *q++ = *p;
325                       wclen++;
326                     }
327                   p++;
328                 }
329
330               data = _gdk_ucs2_to_utf8 (wcs, wclen);
331               g_free (wcs);
332               
333               _gdk_selection_property_store (requestor, target, 8,
334                                              data, strlen (data) + 1);
335               GlobalUnlock (hdata);
336             }
337         }
338       else if ((hdata = GetClipboardData (cf_utf8_string)) != NULL)
339         {
340           /* UTF8_STRING is a format we store ourselves when necessary */
341           guchar *ptr;
342           gint length;
343
344           if ((ptr = GlobalLock (hdata)) != NULL)
345             {
346               length = GlobalSize (hdata);
347               
348               GDK_NOTE (DND, g_print ("...UTF8_STRING: %d bytes: %.10s\n",
349                                       length, ptr));
350               
351               _gdk_selection_property_store (requestor, target, 8,
352                                              g_strdup (ptr), strlen (ptr) + 1);
353               GlobalUnlock (hdata);
354             }
355         }
356       else if ((hdata = GetClipboardData (CF_TEXT)) != NULL)
357         {
358           /* We must always assume the data can contain non-ASCII
359            * in either the current code page, or if there is CF_LOCALE
360            * data, in that locale's default code page.
361            */
362           HGLOBAL hlcid;
363           UINT cp = CP_ACP;
364           wchar_t *wcs, *wcs2, *p, *q;
365           guchar *ptr, *data;
366           gint length, wclen;
367
368           if ((ptr = GlobalLock (hdata)) != NULL)
369             {
370               length = GlobalSize (hdata);
371               
372               GDK_NOTE (DND, g_print ("...CF_TEXT: %d bytes: %.10s\n",
373                                        length, ptr));
374               
375               if ((hlcid = GetClipboardData (CF_LOCALE)) != NULL)
376                 {
377                   gchar buf[10];
378                   LCID *lcidptr = GlobalLock (hlcid);
379                   if (GetLocaleInfo (*lcidptr, LOCALE_IDEFAULTANSICODEPAGE,
380                                      buf, sizeof (buf)))
381                     {
382                       cp = atoi (buf);
383                       GDK_NOTE (DND, g_print ("...CF_LOCALE: %#lx cp:%d\n",
384                                               *lcidptr, cp));
385                     }
386                   GlobalUnlock (hlcid);
387                 }
388
389               wcs = g_new (wchar_t, length + 1);
390               wclen = MultiByteToWideChar (cp, 0, ptr, length,
391                                            wcs, length + 1);
392
393               /* Strip out \r */
394               wcs2 = g_new (wchar_t, wclen);
395               p = wcs;
396               q = wcs2;
397               wclen = 0;
398               while (*p)
399                 {
400                   if (*p != '\r')
401                     {
402                       *q++ = *p;
403                       wclen++;
404                     }
405                   p++;
406                 }
407               g_free (wcs);
408
409               data = _gdk_ucs2_to_utf8 (wcs2, wclen);
410               g_free (wcs2);
411               
412               _gdk_selection_property_store (requestor, target, 8,
413                                              data, strlen (data) + 1);
414               GlobalUnlock (hdata);
415             }
416         }
417       else
418         property = GDK_NONE;
419
420       CloseClipboard ();
421     }
422   else if (selection == gdk_win32_dropfiles)
423     {
424       /* This means he wants the names of the dropped files.
425        * gdk_dropfiles_filter already has stored the text/uri-list
426        * data temporarily in dropfiles_prop.
427        */
428       if (dropfiles_prop != NULL)
429         {
430           _gdk_selection_property_store
431             (requestor, selection, dropfiles_prop->format,
432              dropfiles_prop->data, dropfiles_prop->length);
433           g_free (dropfiles_prop);
434           dropfiles_prop = NULL;
435         }
436     }
437   else
438     property = GDK_NONE;
439
440   /* Generate a selection notify message so that we actually fetch
441    * the data (if property == _gdk_selection_property) or indicating failure
442    * (if property == GDK_NONE).
443    */
444   generate_selection_notify (requestor, selection, target, property, time);
445 }
446
447 gint
448 gdk_selection_property_get (GdkWindow  *requestor,
449                             guchar    **data,
450                             GdkAtom    *ret_type,
451                             gint       *ret_format)
452 {
453   GdkSelProp *prop;
454
455   g_return_val_if_fail (requestor != NULL, 0);
456   g_return_val_if_fail (GDK_IS_WINDOW (requestor), 0);
457
458   if (GDK_WINDOW_DESTROYED (requestor))
459     return 0;
460   
461   GDK_NOTE (DND, g_print ("gdk_selection_property_get: %#x\n",
462                            (guint) GDK_WINDOW_HWND (requestor)));
463
464   prop = g_hash_table_lookup (sel_prop_table, GDK_WINDOW_HWND (requestor));
465
466   if (prop == NULL)
467     {
468       *data = NULL;
469       return 0;
470     }
471
472   *data = g_malloc (prop->length);
473   if (prop->length > 0)
474     memmove (*data, prop->data, prop->length);
475
476   if (ret_type)
477     *ret_type = prop->type;
478
479   if (ret_format)
480     *ret_format = prop->format;
481
482   return prop->length;
483 }
484
485 void
486 _gdk_selection_property_delete (GdkWindow *window)
487 {
488   GdkSelProp *prop;
489   
490   GDK_NOTE (DND, g_print ("_gdk_selection_property_delete: %#x\n",
491                            (guint) GDK_WINDOW_HWND (window)));
492
493   prop = g_hash_table_lookup (sel_prop_table, GDK_WINDOW_HWND (window));
494   if (prop != NULL)
495     {
496       g_free (prop->data);
497       g_hash_table_remove (sel_prop_table, GDK_WINDOW_HWND (window));
498     }
499 }
500
501 void
502 gdk_selection_send_notify (guint32  requestor,
503                            GdkAtom  selection,
504                            GdkAtom  target,
505                            GdkAtom  property,
506                            guint32  time)
507 {
508   GdkEvent tmp_event;
509   gchar *sel_name, *tgt_name, *prop_name;
510
511   GDK_NOTE (DND,
512             (sel_name = gdk_atom_name (selection),
513              tgt_name = gdk_atom_name (target),
514              prop_name = gdk_atom_name (property),
515              g_print ("gdk_selection_send_notify: %#x %#x (%s) %#x (%s) %#x (%s)\n",
516                       requestor,
517                       (guint) selection, sel_name,
518                       (guint) target, tgt_name,
519                       (guint) property, prop_name),
520              g_free (sel_name),
521              g_free (tgt_name),
522              g_free (prop_name)));
523
524   /* Send ourselves a selection clear message so that gtk thinks we don't
525    * have the selection, and will claim it anew when needed, and
526    * we thus get a chance to store data in the Windows clipboard.
527    * Otherwise, if a gtkeditable does a copy to CLIPBOARD several times
528    * only the first one actually gets copied to the Windows clipboard,
529    * as only the first one causes a call to gdk_property_change().
530    *
531    * Hmm, there is something fishy with this. Cut and paste inside the
532    * same app didn't work, the gtkeditable immediately forgot the
533    * clipboard contents in gtk_editable_selection_clear() as a result
534    * of this message. OTOH, when I changed gdk_selection_owner_get to
535    * return NULL for CLIPBOARD, it works. Sigh.
536    */
537
538   tmp_event.selection.type = GDK_SELECTION_CLEAR;
539   tmp_event.selection.window = gdk_window_lookup (requestor);
540   tmp_event.selection.send_event = FALSE;
541   tmp_event.selection.selection = selection;
542   tmp_event.selection.target = 0;
543   tmp_event.selection.property = 0;
544   tmp_event.selection.requestor = 0;
545   tmp_event.selection.time = time;
546
547   gdk_event_put (&tmp_event);
548 }
549
550 /* Simplistic implementations of text list and compound text functions */
551
552 gint
553 gdk_text_property_to_text_list (GdkAtom       encoding,
554                                 gint          format, 
555                                 const guchar *text,
556                                 gint          length,
557                                 gchar      ***list)
558 {
559   gchar *enc_name;
560
561   GDK_NOTE (DND, (enc_name = gdk_atom_name (encoding),
562                   g_print ("gdk_text_property_to_text_list: %s %d %.20s %d\n",
563                            enc_name, format, text, length),
564                   g_free (enc_name)));
565
566   if (!list)
567     return 0;
568
569   *list = g_new (gchar *, 1);
570   **list = g_strdup (text);
571   
572   return 1;
573 }
574
575 void
576 gdk_free_text_list (gchar **list)
577 {
578   g_return_if_fail (list != NULL);
579
580   g_free (*list);
581   g_free (list);
582 }
583
584 gint
585 gdk_string_to_compound_text (const gchar *str,
586                              GdkAtom     *encoding,
587                              gint        *format,
588                              guchar     **ctext,
589                              gint        *length)
590 {
591   GDK_NOTE (DND, g_print ("gdk_string_to_compound_text: %.20s\n", str));
592
593   if (encoding)
594     *encoding = compound_text;
595
596   if (format)
597     *format = 8;
598
599   if (ctext)
600     *ctext = g_strdup (str);
601
602   if (length)
603     *length = strlen (str);
604
605   return 0;
606 }
607
608 void
609 gdk_free_compound_text (guchar *ctext)
610 {
611   g_free (ctext);
612 }
613
614 /* These are lifted from gdkselection-x11.c, just to get GTK+ to build.
615  * These functions probably don't make much sense at all in Windows.
616  */
617
618 /* FIXME */
619
620 static gint
621 make_list (const gchar  *text,
622            gint          length,
623            gboolean      latin1,
624            gchar      ***list)
625 {
626   GSList *strings = NULL;
627   gint n_strings = 0;
628   gint i;
629   const gchar *p = text;
630   const gchar *q;
631   GSList *tmp_list;
632   GError *error = NULL;
633
634   while (p < text + length)
635     {
636       gchar *str;
637       
638       q = p;
639       while (*q && q < text + length)
640         q++;
641
642       if (latin1)
643         {
644           str = g_convert (p, q - p,
645                            "UTF-8", "ISO-8859-1",
646                            NULL, NULL, &error);
647
648           if (!str)
649             {
650               g_warning ("Error converting selection from STRING: %s",
651                          error->message);
652               g_error_free (error);
653             }
654         }
655       else
656         str = g_strndup (p, q - p);
657
658       if (str)
659         {
660           strings = g_slist_prepend (strings, str);
661           n_strings++;
662         }
663
664       p = q + 1;
665     }
666
667   if (list)
668     *list = g_new (gchar *, n_strings + 1);
669
670   (*list)[n_strings] = NULL;
671   
672   i = n_strings;
673   tmp_list = strings;
674   while (tmp_list)
675     {
676       if (list)
677         (*list)[--i] = tmp_list->data;
678       else
679         g_free (tmp_list->data);
680
681       tmp_list = tmp_list->next;
682     }
683
684   g_slist_free (strings);
685
686   return n_strings;
687 }
688
689 /**
690  * gdk_text_property_to_utf8_list:
691  * @encoding: an atom representing the encoding of the text
692  * @format:   the format of the property
693  * @text:     the text to convert
694  * @length:   the length of @text, in bytes
695  * @list:     location to store the list of strings or %NULL. The
696  *            list should be freed with g_strfreev().
697  * 
698  * Converts a text property in the giving encoding to
699  * a list of UTF-8 strings. 
700  * 
701  * Return value: the number of strings in the resulting
702  *               list.
703  **/
704 gint 
705 gdk_text_property_to_utf8_list (GdkAtom        encoding,
706                                 gint           format,
707                                 const guchar  *text,
708                                 gint           length,
709                                 gchar       ***list)
710 {
711   g_return_val_if_fail (text != NULL, 0);
712   g_return_val_if_fail (length >= 0, 0);
713   
714   if (encoding == GDK_TARGET_STRING)
715     {
716       return make_list ((gchar *)text, length, TRUE, list);
717     }
718   else if (encoding == utf8_string)
719     {
720       return make_list ((gchar *)text, length, FALSE, list);
721     }
722   else
723     {
724       gchar **local_list;
725       gint local_count;
726       gint i;
727       const gchar *charset = NULL;
728       gboolean need_conversion = g_get_charset (&charset);
729       gint count = 0;
730       GError *error = NULL;
731       
732       /* Probably COMPOUND text, we fall back to Xlib routines
733        */
734       local_count = gdk_text_property_to_text_list (encoding,
735                                                     format, 
736                                                     text,
737                                                     length,
738                                                     &local_list);
739       if (list)
740         *list = g_new (gchar *, local_count + 1);
741       
742       for (i=0; i<local_count; i++)
743         {
744           /* list contains stuff in our default encoding
745            */
746           if (need_conversion)
747             {
748               gchar *utf = g_convert (local_list[i], -1,
749                                       "UTF-8", charset,
750                                       NULL, NULL, &error);
751               if (utf)
752                 {
753                   if (list)
754                     (*list)[count++] = utf;
755                   else
756                     g_free (utf);
757                 }
758               else
759                 {
760                   g_warning ("Error converting to UTF-8 from '%s': %s",
761                              charset, error->message);
762                   g_error_free (error);
763                   error = NULL;
764                 }
765             }
766           else
767             {
768               if (list)
769                 (*list)[count++] = g_strdup (local_list[i]);
770             }
771         }
772       
773       gdk_free_text_list (local_list);
774       (*list)[count] = NULL;
775
776       return count;
777     }
778 }
779
780 /* The specifications for COMPOUND_TEXT and STRING specify that C0 and
781  * C1 are not allowed except for \n and \t, however the X conversions
782  * routines for COMPOUND_TEXT only enforce this in one direction,
783  * causing cut-and-paste of \r and \r\n separated text to fail.
784  * This routine strips out all non-allowed C0 and C1 characters
785  * from the input string and also canonicalizes \r, \r\n, and \n\r to \n
786  */
787 static gchar * 
788 sanitize_utf8 (const gchar *src)
789 {
790   gint len = strlen (src);
791   GString *result = g_string_sized_new (len);
792   const gchar *p = src;
793
794   while (*p)
795     {
796       if (*p == '\r' || *p == '\n')
797         {
798           p++;
799           if (*p == '\r' || *p == '\n')
800             p++;
801
802           g_string_append_c (result, '\n');
803         }
804       else
805         {
806           gunichar ch = g_utf8_get_char (p);
807           char buf[7];
808           gint buflen;
809           
810           if (!((ch < 0x20 && ch != '\t') || (ch >= 0x7f && ch < 0xa0)))
811             {
812               buflen = g_unichar_to_utf8 (ch, buf);
813               g_string_append_len (result, buf, buflen);
814             }
815
816           p = g_utf8_next_char (p);
817         }
818     }
819
820   return g_string_free (result, FALSE);
821 }
822
823 /**
824  * gdk_utf8_to_string_target:
825  * @str: a UTF-8 string
826  * 
827  * Converts an UTF-8 string into the best possible representation
828  * as a STRING. The representation of characters not in STRING
829  * is not specified; it may be as pseudo-escape sequences
830  * \x{ABCD}, or it may be in some other form of approximation.
831  * 
832  * Return value: the newly allocated string, or %NULL if the
833  *               conversion failed. (It should not fail for
834  *               any properly formed UTF-8 string.)
835  **/
836 gchar *
837 gdk_utf8_to_string_target (const gchar *str)
838 {
839   return sanitize_utf8 (str);
840 }
841
842 /**
843  * gdk_utf8_to_compound_text:
844  * @str:      a UTF-8 string
845  * @encoding: location to store resulting encoding
846  * @format:   location to store format of the result
847  * @ctext:    location to store the data of the result
848  * @length:   location to store the length of the data
849  *            stored in @ctext
850  * 
851  * Converts from UTF-8 to compound text. 
852  * 
853  * Return value: %TRUE if the conversion succeeded, otherwise
854  *               %FALSE.
855  **/
856 gboolean
857 gdk_utf8_to_compound_text (const gchar *str,
858                            GdkAtom     *encoding,
859                            gint        *format,
860                            guchar     **ctext,
861                            gint        *length)
862 {
863   gboolean need_conversion;
864   const gchar *charset;
865   gchar *locale_str, *tmp_str;
866   GError *error = NULL;
867   gboolean result;
868
869   g_return_val_if_fail (str != NULL, FALSE);
870
871   need_conversion = !g_get_charset (&charset);
872
873   tmp_str = sanitize_utf8 (str);
874
875   if (need_conversion)
876     {
877       locale_str = g_convert_with_fallback (tmp_str, -1,
878                                             charset, "UTF-8",
879                                             NULL, NULL, NULL, &error);
880       g_free (tmp_str);
881
882       if (!locale_str)
883         {
884           g_warning ("Error converting from UTF-8 to '%s': %s",
885                      charset, error->message);
886           g_error_free (error);
887
888           if (encoding)
889             *encoding = GDK_NONE;
890           if (format)
891             *format = GPOINTER_TO_UINT (GDK_ATOM_TO_POINTER (GDK_NONE));
892           if (ctext)
893             *ctext = NULL;
894           if (length)
895             *length = 0;
896
897           return FALSE;
898         }
899     }
900   else
901     locale_str = tmp_str;
902     
903   result = gdk_string_to_compound_text (locale_str,
904                                         encoding, format, ctext, length);
905   
906   g_free (locale_str);
907
908   return result;
909 }