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 "gdkproperty.h"
30 #include "gdkselection.h"
31 #include "gdkprivate-win32.h"
33 /* We emulate the GDK_SELECTION window properties by storing
34 * it's data in a per-window hashtable.
44 static GHashTable *sel_prop_table = NULL;
47 _gdk_win32_selection_init (void)
49 if (sel_prop_table == NULL)
50 sel_prop_table = g_hash_table_new (g_int_hash, g_int_equal);
54 _gdk_selection_property_store (GdkWindow *owner,
62 prop = g_hash_table_lookup (sel_prop_table, &GDK_WINDOW_HWND (owner));
66 g_hash_table_remove (sel_prop_table, &GDK_WINDOW_HWND (owner));
68 prop = g_new (GdkSelProp, 1);
70 prop->length = length;
71 prop->format = format;
73 g_hash_table_insert (sel_prop_table, &GDK_WINDOW_HWND (owner), prop);
77 gdk_selection_owner_set (GdkWindow *owner,
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),
92 if (selection != gdk_clipboard_atom)
96 _gdk_selection_property_store (owner, selection, 0, 0, 0);
102 if (GDK_WINDOW_DESTROYED (owner))
105 xwindow = GDK_WINDOW_HWND (owner);
110 GDK_NOTE (MISC, g_print ("...OpenClipboard(%#x)\n", (guint) xwindow));
111 if (!OpenClipboard (xwindow))
113 WIN32_API_FAILED ("OpenClipboard");
116 GDK_NOTE (MISC, g_print ("...EmptyClipboard()\n"));
117 if (!EmptyClipboard ())
119 WIN32_API_FAILED ("EmptyClipboard");
124 /* No delayed rendering */
126 SetClipboardData (CF_TEXT, NULL);
128 GDK_NOTE (MISC, g_print ("...CloseClipboard()\n"));
129 if (!CloseClipboard ())
131 WIN32_API_FAILED ("CloseClipboard");
136 /* Send ourselves an ersatz selection request message so that
137 * gdk_property_change will be called to store the clipboard data.
139 SendMessage (xwindow, gdk_selection_request_msg,
140 GPOINTER_TO_UINT (GDK_ATOM_TO_POINTER (selection)), 0);
146 /* callback for g_hash_table_for_each */
149 GdkNativeWindow hwnd;
153 window_from_selection (gpointer key,
157 GdkSelProp *selprop = (GdkSelProp *)value;
158 SelectionAndHwnd *sah = (SelectionAndHwnd *) user_data;
160 if (selprop->type == sah->atom)
161 sah->hwnd = *(GdkNativeWindow*) key;
165 gdk_selection_owner_get (GdkAtom selection)
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.
177 /* HB: The above is no longer true with recent changes to get
178 * inter-app drag&drop working ...
180 if (selection != gdk_clipboard_atom)
182 SelectionAndHwnd sah;
183 sah.atom = selection;
186 g_hash_table_foreach (sel_prop_table, window_from_selection, &sah);
188 window = gdk_win32_handle_table_lookup (sah.hwnd);
191 window = gdk_win32_handle_table_lookup ((GdkNativeWindow) GetClipboardOwner ());
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)),
206 gdk_selection_convert (GdkWindow *requestor,
212 guchar *ptr, *data, *datap, *p;
213 guint i, length, slength;
214 gchar *sel_name, *tgt_name;
216 g_return_if_fail (requestor != NULL);
217 if (GDK_WINDOW_DESTROYED (requestor))
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),
230 if (selection == gdk_clipboard_atom)
232 /* Converting the CLIPBOARD selection means he wants the
233 * contents of the clipboard. Get the clipboard data,
234 * and store it for later.
236 GDK_NOTE (MISC, g_print ("...OpenClipboard(%#x)\n",
237 (guint) GDK_WINDOW_HWND (requestor)));
238 if (!OpenClipboard (GDK_WINDOW_HWND (requestor)))
240 WIN32_API_FAILED ("OpenClipboard");
244 GDK_NOTE (MISC, g_print ("...GetClipboardData(CF_TEXT)\n"));
245 if ((hdata = GetClipboardData (CF_TEXT)) != NULL)
247 if ((ptr = GlobalLock (hdata)) != NULL)
249 length = GlobalSize (hdata);
251 GDK_NOTE (MISC, g_print ("...got data: %d bytes: %.10s\n",
256 for (i = 0; i < length; i++)
265 data = datap = g_malloc (slength + 1);
267 for (i = 0; i < length; i++)
276 _gdk_selection_property_store (requestor, GDK_TARGET_STRING, 8,
277 data, strlen (data) + 1);
279 GlobalUnlock (hdata);
282 GDK_NOTE (MISC, g_print ("...CloseClipboard()\n"));
286 /* Send ourselves an ersatz selection notify message so that we actually
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)));
293 else if (selection == gdk_win32_dropfiles_atom)
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".
301 prop = g_hash_table_lookup (sel_prop_table,
302 &GDK_WINDOW_HWND (_gdk_parent_root));
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);
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)));
318 g_warning ("gdk_selection_convert: General case not implemented");
323 gdk_selection_property_get (GdkWindow *requestor,
330 g_return_val_if_fail (requestor != NULL, 0);
331 g_return_val_if_fail (GDK_IS_WINDOW (requestor), 0);
333 if (GDK_WINDOW_DESTROYED (requestor))
336 GDK_NOTE (MISC, g_print ("gdk_selection_property_get: %#x\n",
337 (guint) GDK_WINDOW_HWND (requestor)));
339 prop = g_hash_table_lookup (sel_prop_table, &GDK_WINDOW_HWND (requestor));
346 *data = g_malloc (prop->length);
347 if (prop->length > 0)
348 memmove (*data, prop->data, prop->length);
350 *ret_type = prop->type;
352 *ret_format = prop->format;
358 _gdk_selection_property_delete (GdkWindow *window)
362 prop = g_hash_table_lookup (sel_prop_table, &GDK_WINDOW_HWND (window));
366 g_hash_table_remove (sel_prop_table, &GDK_WINDOW_HWND (window));
373 gdk_selection_send_notify (guint32 requestor,
379 gchar *sel_name, *tgt_name, *prop_name;
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",
387 (guint) selection, sel_name,
388 (guint) target, tgt_name,
389 (guint) property, prop_name),
392 g_free (prop_name)));
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.
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.
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)));
415 gdk_text_property_to_text_list (GdkAtom encoding,
422 g_print ("gdk_text_property_to_text_list not implemented\n"));
428 gdk_free_text_list (gchar **list)
430 g_return_if_fail (list != NULL);
436 gdk_string_to_compound_text (const gchar *str,
442 g_warning ("gdk_string_to_compound_text: Not implemented");
448 gdk_free_compound_text (guchar *ctext)
450 g_warning ("gdk_free_compound_text: Not implemented");
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.
460 make_list (const gchar *text,
465 GSList *strings = NULL;
468 const gchar *p = text;
471 GError *error = NULL;
473 while (p < text + length)
478 while (*q && q < text + length)
483 str = g_convert (p, q - p,
484 "UTF-8", "ISO-8859-1",
489 g_warning ("Error converting selection from STRING: %s",
491 g_error_free (error);
495 str = g_strndup (p, q - p);
499 strings = g_slist_prepend (strings, str);
507 *list = g_new (gchar *, n_strings + 1);
509 (*list)[n_strings] = NULL;
516 (*list)[--i] = tmp_list->data;
518 g_free (tmp_list->data);
520 tmp_list = tmp_list->next;
523 g_slist_free (strings);
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().
537 * Convert a text property in the giving encoding to
538 * a list of UTF-8 strings.
540 * Return value: the number of strings in the resulting
544 gdk_text_property_to_utf8_list (GdkAtom encoding,
550 g_return_val_if_fail (text != NULL, 0);
551 g_return_val_if_fail (length >= 0, 0);
553 if (encoding == GDK_TARGET_STRING)
555 return make_list ((gchar *)text, length, TRUE, list);
557 else if (encoding == gdk_atom_intern ("UTF8_STRING", FALSE))
559 return make_list ((gchar *)text, length, FALSE, list);
566 gchar *charset = NULL;
567 gboolean need_conversion= g_get_charset (&charset);
569 GError *error = NULL;
571 /* Probably COMPOUND text, we fall back to Xlib routines
573 local_count = gdk_text_property_to_text_list (encoding,
579 *list = g_new (gchar *, local_count + 1);
581 for (i=0; i<local_count; i++)
583 /* list contains stuff in our default encoding
587 gchar *utf = g_convert (local_list[i], -1,
593 (*list)[count++] = utf;
599 g_warning ("Error converting to UTF-8 from '%s': %s",
600 charset, error->message);
601 g_error_free (error);
608 (*list)[count++] = g_strdup (local_list[i]);
612 gdk_free_text_list (local_list);
613 (*list)[count] = NULL;
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
627 sanitize_utf8 (const gchar *src)
629 gint len = strlen (src);
630 GString *result = g_string_sized_new (len);
631 const gchar *p = src;
635 if (*p == '\r' || *p == '\n')
638 if (*p == '\r' || *p == '\n')
641 g_string_append_c (result, '\n');
645 gunichar ch = g_utf8_get_char (p);
649 if (!((ch < 0x20 && ch != '\t') || (ch >= 0x7f && ch < 0xa0)))
651 buflen = g_unichar_to_utf8 (ch, buf);
652 g_string_append_len (result, buf, buflen);
655 p = g_utf8_next_char (p);
659 return g_string_free (result, FALSE);
663 * gdk_utf8_to_string_target:
664 * @str: a UTF-8 string
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.
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.)
676 gdk_utf8_to_string_target (const gchar *str)
678 GError *error = NULL;
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);
686 g_warning ("Error converting from UTF-8 to STRING: %s",
688 g_error_free (error);
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
704 * Convert from UTF-8 to compound text.
706 * Return value: %TRUE if the conversion succeeded, otherwise
710 gdk_utf8_to_compound_text (const gchar *str,
716 gboolean need_conversion;
718 gchar *locale_str, *tmp_str;
719 GError *error = NULL;
722 g_return_val_if_fail (str != NULL, FALSE);
724 need_conversion = g_get_charset (&charset);
726 tmp_str = sanitize_utf8 (str);
730 locale_str = g_convert_with_fallback (tmp_str, -1,
732 NULL, NULL, NULL, &error);
737 g_warning ("Error converting from UTF-8 to '%s': %s",
738 charset, error->message);
739 g_error_free (error);
742 *encoding = GDK_NONE;
744 *format = GPOINTER_TO_UINT (GDK_ATOM_TO_POINTER (GDK_NONE));
754 locale_str = tmp_str;
756 result = gdk_string_to_compound_text (locale_str,
757 encoding, format, ctext, length);