]> Pileus Git - ~andy/gtk/blob - gdk/win32/gdkselection-win32.c
New file, hand-written wrapper for the Wintab library.
[~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  *
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 <string.h>
28
29 #include "gdkproperty.h"
30 #include "gdkselection.h"
31 #include "gdkprivate-win32.h"
32
33 /* We emulate the GDK_SELECTION window properties by storing
34  * it's data in a per-window hashtable.
35  */
36
37 typedef struct {
38   guchar *data;
39   gint length;
40   gint format;
41   GdkAtom type;
42 } GdkSelProp;
43
44 static GHashTable *sel_prop_table = NULL;
45
46 void
47 _gdk_win32_selection_init (void)
48 {
49   if (sel_prop_table == NULL)
50     sel_prop_table = g_hash_table_new (g_int_hash, g_int_equal);
51 }
52
53 void
54 _gdk_selection_property_store (GdkWindow *owner,
55                                GdkAtom    type,
56                                gint       format,
57                                guchar    *data,
58                                gint       length)
59 {
60   GdkSelProp *prop;
61
62   prop = g_hash_table_lookup (sel_prop_table, &GDK_WINDOW_HWND (owner));
63   if (prop != NULL)
64     {
65       g_free (prop->data);
66       g_hash_table_remove (sel_prop_table, &GDK_WINDOW_HWND (owner));
67     }
68   prop = g_new (GdkSelProp, 1);
69   prop->data = data;
70   prop->length = length;
71   prop->format = format;
72   prop->type = type;
73   g_hash_table_insert (sel_prop_table, &GDK_WINDOW_HWND (owner), prop);
74 }
75
76 gboolean
77 gdk_selection_owner_set (GdkWindow *owner,
78                          GdkAtom    selection,
79                          guint32    time,
80                          gboolean   send_event)
81 {
82   gchar *sel_name;
83   HWND xwindow;
84
85   GDK_NOTE (MISC,
86             (sel_name = gdk_atom_name (selection),
87              g_print ("gdk_selection_owner_set: %#x %#x (%s)\n",
88                       (owner ? (guint) GDK_WINDOW_HWND (owner) : 0),
89                       (guint) selection, sel_name),
90              g_free (sel_name)));
91
92   if (selection != gdk_clipboard_atom)
93     {
94       if (!owner)
95         return FALSE;
96       _gdk_selection_property_store (owner, selection, 0, 0, 0);
97       return TRUE;
98     }
99
100   if (owner != NULL)
101     {
102       if (GDK_WINDOW_DESTROYED (owner))
103         return FALSE;
104
105       xwindow = GDK_WINDOW_HWND (owner);
106     }
107   else
108     xwindow = NULL;
109
110   GDK_NOTE (MISC, g_print ("...OpenClipboard(%#x)\n", (guint) xwindow));
111   if (!OpenClipboard (xwindow))
112     {
113       WIN32_API_FAILED ("OpenClipboard");
114       return FALSE;
115     }
116   GDK_NOTE (MISC, g_print ("...EmptyClipboard()\n"));
117   if (!EmptyClipboard ())
118     {
119       WIN32_API_FAILED ("EmptyClipboard");
120       CloseClipboard ();
121       return FALSE;
122     }
123 #if 0
124   /* No delayed rendering */
125   if (xwindow != NULL)
126     SetClipboardData (CF_TEXT, NULL);
127 #endif
128   GDK_NOTE (MISC, g_print ("...CloseClipboard()\n"));
129   if (!CloseClipboard ())
130     {
131       WIN32_API_FAILED ("CloseClipboard");
132       return FALSE;
133     }
134   if (owner != NULL)
135     {
136       /* Send ourselves an ersatz selection request message so that
137        * gdk_property_change will be called to store the clipboard data.
138        */
139       SendMessage (xwindow, gdk_selection_request_msg,
140                    GPOINTER_TO_UINT (GDK_ATOM_TO_POINTER (selection)), 0);
141     }
142
143   return TRUE;
144 }
145
146 /* callback for g_hash_table_for_each */
147 typedef struct {
148   GdkAtom         atom;
149   GdkNativeWindow hwnd;
150 } SelectionAndHwnd;
151
152 static void
153 window_from_selection (gpointer key,
154                  gpointer value,
155                  gpointer user_data)
156 {
157   GdkSelProp *selprop = (GdkSelProp *)value;  
158   SelectionAndHwnd *sah = (SelectionAndHwnd *) user_data;
159
160   if (selprop->type == sah->atom)
161     sah->hwnd = *(GdkNativeWindow*) key;
162 }
163
164 GdkWindow*
165 gdk_selection_owner_get (GdkAtom selection)
166 {
167   GdkWindow *window;
168   gchar *sel_name;
169
170 #if 0
171   /* XXX Hmm, gtk selections seem to work best with this. This causes
172    * gtk to always get the clipboard contents from Windows, and not
173    * from the editable's own stashed-away copy.
174    */
175   return NULL;
176 #else
177   /* HB: The above is no longer true with recent changes to get
178    * inter-app drag&drop working ...
179    */
180   if (selection != gdk_clipboard_atom)
181     {
182       SelectionAndHwnd sah;
183       sah.atom = selection;
184       sah.hwnd = 0;
185  
186       g_hash_table_foreach (sel_prop_table, window_from_selection, &sah);
187
188       window = gdk_win32_handle_table_lookup (sah.hwnd);
189     }
190   else
191     window = gdk_win32_handle_table_lookup ((GdkNativeWindow) GetClipboardOwner ());
192
193 #endif
194
195   GDK_NOTE (MISC,
196             (sel_name = gdk_atom_name (selection),
197              g_print ("gdk_selection_owner_get: %#x (%s) = %#x\n",
198                       (guint) selection, sel_name,
199                       (window ? (guint) GDK_WINDOW_HWND (window) : 0)),
200              g_free (sel_name)));
201
202   return window;
203 }
204
205 void
206 gdk_selection_convert (GdkWindow *requestor,
207                        GdkAtom    selection,
208                        GdkAtom    target,
209                        guint32    time)
210 {
211   HGLOBAL hdata;
212   guchar *ptr, *data, *datap, *p;
213   guint i, length, slength;
214   gchar *sel_name, *tgt_name;
215
216   g_return_if_fail (requestor != NULL);
217   if (GDK_WINDOW_DESTROYED (requestor))
218     return;
219
220   GDK_NOTE (MISC,
221             (sel_name = gdk_atom_name (selection),
222              tgt_name = gdk_atom_name (target),
223              g_print ("gdk_selection_convert: %#x %#x (%s) %#x (%s)\n",
224                       (guint) GDK_WINDOW_HWND (requestor),
225                       (guint) selection, sel_name,
226                       (guint) target, tgt_name),
227              g_free (sel_name),
228              g_free (tgt_name)));
229
230   if (selection == gdk_clipboard_atom)
231     {
232       /* Converting the CLIPBOARD selection means he wants the
233        * contents of the clipboard. Get the clipboard data,
234        * and store it for later.
235        */
236       GDK_NOTE (MISC, g_print ("...OpenClipboard(%#x)\n",
237                                (guint) GDK_WINDOW_HWND (requestor)));
238       if (!OpenClipboard (GDK_WINDOW_HWND (requestor)))
239         {
240           WIN32_API_FAILED ("OpenClipboard");
241           return;
242         }
243
244       GDK_NOTE (MISC, g_print ("...GetClipboardData(CF_TEXT)\n"));
245       if ((hdata = GetClipboardData (CF_TEXT)) != NULL)
246         {
247           if ((ptr = GlobalLock (hdata)) != NULL)
248             {
249               length = GlobalSize (hdata);
250               
251               GDK_NOTE (MISC, g_print ("...got data: %d bytes: %.10s\n",
252                                        length, ptr));
253               
254               slength = 0;
255               p = ptr;
256               for (i = 0; i < length; i++)
257                 {
258                   if (*p == '\0')
259                     break;
260                   else if (*p != '\r')
261                     slength++;
262                   p++;
263                 }
264               
265               data = datap = g_malloc (slength + 1);
266               p = ptr;
267               for (i = 0; i < length; i++)
268                 {
269                   if (*p == '\0')
270                     break;
271                   else if (*p != '\r')
272                     *datap++ = *p;
273                   p++;
274                 }
275               *datap++ = '\0';
276               _gdk_selection_property_store (requestor, GDK_TARGET_STRING, 8,
277                                   data, strlen (data) + 1);
278               
279               GlobalUnlock (hdata);
280             }
281         }
282       GDK_NOTE (MISC, g_print ("...CloseClipboard()\n"));
283       CloseClipboard ();
284
285
286       /* Send ourselves an ersatz selection notify message so that we actually
287        * fetch the data.
288        */
289       SendMessage (GDK_WINDOW_HWND (requestor), gdk_selection_notify_msg, 
290                    GPOINTER_TO_UINT (GDK_ATOM_TO_POINTER (selection)), 
291                    GPOINTER_TO_UINT (GDK_ATOM_TO_POINTER (target)));
292     }
293   else if (selection == gdk_win32_dropfiles_atom)
294     {
295       /* This means he wants the names of the dropped files.
296        * gdk_dropfiles_filter already has stored the text/uri-list
297        * data, tempoarily on gdk_root_parent's selection "property".
298        */
299       GdkSelProp *prop;
300
301       prop = g_hash_table_lookup (sel_prop_table,
302                                   &GDK_WINDOW_HWND (_gdk_parent_root));
303
304       if (prop != NULL)
305         {
306           g_hash_table_remove (sel_prop_table,
307                                &GDK_WINDOW_HWND (_gdk_parent_root));
308           _gdk_selection_property_store (requestor, prop->type, prop->format,
309                               prop->data, prop->length);
310           g_free (prop);
311           SendMessage (GDK_WINDOW_HWND (requestor), gdk_selection_notify_msg, 
312                      GPOINTER_TO_UINT (GDK_ATOM_TO_POINTER (selection)), 
313                      GPOINTER_TO_UINT (GDK_ATOM_TO_POINTER (target)));
314         }
315     }
316   else
317     {
318       g_warning ("gdk_selection_convert: General case not implemented");
319     }
320 }
321
322 gint
323 gdk_selection_property_get (GdkWindow  *requestor,
324                             guchar    **data,
325                             GdkAtom    *ret_type,
326                             gint       *ret_format)
327 {
328   GdkSelProp *prop;
329
330   g_return_val_if_fail (requestor != NULL, 0);
331   g_return_val_if_fail (GDK_IS_WINDOW (requestor), 0);
332
333   if (GDK_WINDOW_DESTROYED (requestor))
334     return 0;
335   
336   GDK_NOTE (MISC, g_print ("gdk_selection_property_get: %#x\n",
337                            (guint) GDK_WINDOW_HWND (requestor)));
338
339   prop = g_hash_table_lookup (sel_prop_table, &GDK_WINDOW_HWND (requestor));
340
341   if (prop == NULL)
342     {
343       *data = NULL;
344       return 0;
345     }
346   *data = g_malloc (prop->length);
347   if (prop->length > 0)
348     memmove (*data, prop->data, prop->length);
349   if (ret_type)
350     *ret_type = prop->type;
351   if (ret_format)
352     *ret_format = prop->format;
353
354   return prop->length;
355 }
356
357 void
358 _gdk_selection_property_delete (GdkWindow *window)
359 {
360   GdkSelProp *prop;
361   
362   prop = g_hash_table_lookup (sel_prop_table, &GDK_WINDOW_HWND (window));
363   if (prop != NULL)
364     {
365       g_free (prop->data);
366       g_hash_table_remove (sel_prop_table, &GDK_WINDOW_HWND (window));
367     }
368   else
369     g_warning ("huh?");
370 }
371
372 void
373 gdk_selection_send_notify (guint32  requestor,
374                            GdkAtom  selection,
375                            GdkAtom  target,
376                            GdkAtom  property,
377                            guint32  time)
378 {
379   gchar *sel_name, *tgt_name, *prop_name;
380
381   GDK_NOTE (MISC,
382             (sel_name = gdk_atom_name (selection),
383              tgt_name = gdk_atom_name (target),
384              prop_name = gdk_atom_name (property),
385              g_print ("gdk_selection_send_notify: %#x %#x (%s) %#x (%s) %#x (%s)\n",
386                       requestor,
387                       (guint) selection, sel_name,
388                       (guint) target, tgt_name,
389                       (guint) property, prop_name),
390              g_free (sel_name),
391              g_free (tgt_name),
392              g_free (prop_name)));
393
394   /* Send ourselves a selection clear message so that gtk thinks we don't
395    * have the selection, and will claim it anew when needed, and
396    * we thus get a chance to store data in the Windows clipboard.
397    * Otherwise, if a gtkeditable does a copy to clipboard several times
398    * only the first one actually gets copied to the Windows clipboard,
399    * as only he first one causes a call to gdk_property_change.
400    *
401    * Hmm, there is something fishy with this. Cut and paste inside the
402    * same app didn't work, the gtkeditable immediately forgot the
403    * clipboard contents in gtk_editable_selection_clear as a result of
404    * this message. OTOH, when I changed gdk_selection_owner_get to
405    * always return NULL, it works. Sigh.
406    */
407
408   SendMessage ((HWND) requestor, 
409                gdk_selection_clear_msg, 
410                GPOINTER_TO_UINT (GDK_ATOM_TO_POINTER (selection)), 
411                GPOINTER_TO_UINT (GDK_ATOM_TO_POINTER (target)));
412 }
413
414 gint
415 gdk_text_property_to_text_list (GdkAtom       encoding,
416                                 gint          format, 
417                                 const guchar *text,
418                                 gint          length,
419                                 gchar      ***list)
420 {
421   GDK_NOTE (MISC,
422             g_print ("gdk_text_property_to_text_list not implemented\n"));
423   
424   return 0;
425 }
426
427 void
428 gdk_free_text_list (gchar **list)
429 {
430   g_return_if_fail (list != NULL);
431
432   /* ??? */
433 }
434
435 gint
436 gdk_string_to_compound_text (const gchar *str,
437                              GdkAtom     *encoding,
438                              gint        *format,
439                              guchar     **ctext,
440                              gint        *length)
441 {
442   g_warning ("gdk_string_to_compound_text: Not implemented");
443
444   return 0;
445 }
446
447 void
448 gdk_free_compound_text (guchar *ctext)
449 {
450   g_warning ("gdk_free_compound_text: Not implemented");
451 }
452
453 /* These are lifted from gdkselection-x11.c, just to get GTK+ to build.
454  * These functions probably don't make much sense at all in Windows.
455  */
456
457 /* FIXME */
458
459 static gint
460 make_list (const gchar  *text,
461            gint          length,
462            gboolean      latin1,
463            gchar      ***list)
464 {
465   GSList *strings = NULL;
466   gint n_strings = 0;
467   gint i;
468   const gchar *p = text;
469   const gchar *q;
470   GSList *tmp_list;
471   GError *error = NULL;
472
473   while (p < text + length)
474     {
475       gchar *str;
476       
477       q = p;
478       while (*q && q < text + length)
479         q++;
480
481       if (latin1)
482         {
483           str = g_convert (p, q - p,
484                            "UTF-8", "ISO-8859-1",
485                            NULL, NULL, &error);
486
487           if (!str)
488             {
489               g_warning ("Error converting selection from STRING: %s",
490                          error->message);
491               g_error_free (error);
492             }
493         }
494       else
495         str = g_strndup (p, q - p);
496
497       if (str)
498         {
499           strings = g_slist_prepend (strings, str);
500           n_strings++;
501         }
502
503       p = q + 1;
504     }
505
506   if (list)
507     *list = g_new (gchar *, n_strings + 1);
508
509   (*list)[n_strings] = NULL;
510   
511   i = n_strings;
512   tmp_list = strings;
513   while (tmp_list)
514     {
515       if (list)
516         (*list)[--i] = tmp_list->data;
517       else
518         g_free (tmp_list->data);
519
520       tmp_list = tmp_list->next;
521     }
522
523   g_slist_free (strings);
524
525   return n_strings;
526 }
527
528 /**
529  * gdk_text_property_to_utf8_list:
530  * @encoding: an atom representing the encoding of the text
531  * @format:   the format of the property
532  * @text:     the text to convert
533  * @length:   the length of @text, in bytes
534  * @list:     location to store the list of strings or %NULL. The
535  *            list should be freed with g_strfreev().
536  * 
537  * Convert a text property in the giving encoding to
538  * a list of UTF-8 strings. 
539  * 
540  * Return value: the number of strings in the resulting
541  *               list.
542  **/
543 gint 
544 gdk_text_property_to_utf8_list (GdkAtom        encoding,
545                                 gint           format,
546                                 const guchar  *text,
547                                 gint           length,
548                                 gchar       ***list)
549 {
550   g_return_val_if_fail (text != NULL, 0);
551   g_return_val_if_fail (length >= 0, 0);
552   
553   if (encoding == GDK_TARGET_STRING)
554     {
555       return make_list ((gchar *)text, length, TRUE, list);
556     }
557   else if (encoding == gdk_atom_intern ("UTF8_STRING", FALSE))
558     {
559       return make_list ((gchar *)text, length, FALSE, list);
560     }
561   else
562     {
563       gchar **local_list;
564       gint local_count;
565       gint i;
566       gchar *charset = NULL;
567       gboolean need_conversion= g_get_charset (&charset);
568       gint count = 0;
569       GError *error = NULL;
570       
571       /* Probably COMPOUND text, we fall back to Xlib routines
572        */
573       local_count = gdk_text_property_to_text_list (encoding,
574                                                     format, 
575                                                     text,
576                                                     length,
577                                                     &local_list);
578       if (list)
579         *list = g_new (gchar *, local_count + 1);
580       
581       for (i=0; i<local_count; i++)
582         {
583           /* list contains stuff in our default encoding
584            */
585           if (need_conversion)
586             {
587               gchar *utf = g_convert (local_list[i], -1,
588                                       "UTF-8", charset,
589                                       NULL, NULL, &error);
590               if (utf)
591                 {
592                   if (list)
593                     (*list)[count++] = utf;
594                   else
595                     g_free (utf);
596                 }
597               else
598                 {
599                   g_warning ("Error converting to UTF-8 from '%s': %s",
600                              charset, error->message);
601                   g_error_free (error);
602                   error = NULL;
603                 }
604             }
605           else
606             {
607               if (list)
608                 (*list)[count++] = g_strdup (local_list[i]);
609             }
610         }
611       
612       gdk_free_text_list (local_list);
613       (*list)[count] = NULL;
614
615       return count;
616     }
617 }
618
619 /* The specifications for COMPOUND_TEXT and STRING specify that C0 and
620  * C1 are not allowed except for \n and \t, however the X conversions
621  * routines for COMPOUND_TEXT only enforce this in one direction,
622  * causing cut-and-paste of \r and \r\n separated text to fail.
623  * This routine strips out all non-allowed C0 and C1 characters
624  * from the input string and also canonicalizes \r, \r\n, and \n\r to \n
625  */
626 static gchar * 
627 sanitize_utf8 (const gchar *src)
628 {
629   gint len = strlen (src);
630   GString *result = g_string_sized_new (len);
631   const gchar *p = src;
632
633   while (*p)
634     {
635       if (*p == '\r' || *p == '\n')
636         {
637           p++;
638           if (*p == '\r' || *p == '\n')
639             p++;
640
641           g_string_append_c (result, '\n');
642         }
643       else
644         {
645           gunichar ch = g_utf8_get_char (p);
646           char buf[7];
647           gint buflen;
648           
649           if (!((ch < 0x20 && ch != '\t') || (ch >= 0x7f && ch < 0xa0)))
650             {
651               buflen = g_unichar_to_utf8 (ch, buf);
652               g_string_append_len (result, buf, buflen);
653             }
654
655           p = g_utf8_next_char (p);
656         }
657     }
658
659   return g_string_free (result, FALSE);
660 }
661
662 /**
663  * gdk_utf8_to_string_target:
664  * @str: a UTF-8 string
665  * 
666  * Convert an UTF-8 string into the best possible representation
667  * as a STRING. The representation of characters not in STRING
668  * is not specified; it may be as pseudo-escape sequences
669  * \x{ABCD}, or it may be in some other form of approximation.
670  * 
671  * Return value: the newly allocated string, or %NULL if the
672  *               conversion failed. (It should not fail for
673  *               any properly formed UTF-8 string.)
674  **/
675 gchar *
676 gdk_utf8_to_string_target (const gchar *str)
677 {
678   GError *error = NULL;
679   
680   gchar *tmp_str = sanitize_utf8 (str);
681   gchar *result =  g_convert_with_fallback (tmp_str, -1,
682                                             "ISO-8859-1", "UTF-8",
683                                             NULL, NULL, NULL, &error);
684   if (!result)
685     {
686       g_warning ("Error converting from UTF-8 to STRING: %s",
687                  error->message);
688       g_error_free (error);
689     }
690   
691   g_free (tmp_str);
692   return result;
693 }
694
695 /**
696  * gdk_utf8_to_compound_text:
697  * @str:      a UTF-8 string
698  * @encoding: location to store resulting encoding
699  * @format:   location to store format of the result
700  * @ctext:    location to store the data of the result
701  * @length:   location to store the length of the data
702  *            stored in @ctext
703  * 
704  * Convert from UTF-8 to compound text. 
705  * 
706  * Return value: %TRUE if the conversion succeeded, otherwise
707  *               false.
708  **/
709 gboolean
710 gdk_utf8_to_compound_text (const gchar *str,
711                            GdkAtom     *encoding,
712                            gint        *format,
713                            guchar     **ctext,
714                            gint        *length)
715 {
716   gboolean need_conversion;
717   gchar *charset;
718   gchar *locale_str, *tmp_str;
719   GError *error = NULL;
720   gboolean result;
721
722   g_return_val_if_fail (str != NULL, FALSE);
723
724   need_conversion = g_get_charset (&charset);
725
726   tmp_str = sanitize_utf8 (str);
727
728   if (need_conversion)
729     {
730       locale_str = g_convert_with_fallback (tmp_str, -1,
731                                             charset, "UTF-8",
732                                             NULL, NULL, NULL, &error);
733       g_free (tmp_str);
734
735       if (!locale_str)
736         {
737           g_warning ("Error converting from UTF-8 to '%s': %s",
738                      charset, error->message);
739           g_error_free (error);
740
741           if (encoding)
742             *encoding = GDK_NONE;
743           if (format)
744             *format = GPOINTER_TO_UINT (GDK_ATOM_TO_POINTER (GDK_NONE));
745           if (ctext)
746             *ctext = NULL;
747           if (length)
748             *length = 0;
749
750           return FALSE;
751         }
752     }
753   else
754     locale_str = tmp_str;
755     
756   result = gdk_string_to_compound_text (locale_str,
757                                         encoding, format, ctext, length);
758   
759   g_free (locale_str);
760
761   return result;
762 }