Debian Codegraph @

gdk/win32/gdkselection-win32.c

raw
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 "config.h"
29 #include <string.h>
30 #include <stdlib.h>
31
32 #include "gdkproperty.h"
33 #include "gdkselection.h"
34 #include "gdkdisplay.h"
35 #include "gdkprivate-win32.h"
36
37 /* We emulate the GDK_SELECTION window properties of windows (as used
38 * in the X11 backend) by using a hash table from window handles to
39 * GdkSelProp structs.
40 */
41
42 typedef struct {
43 guchar *data;
44 gsize length;
45 gint format;
46 GdkAtom type;
47 } GdkSelProp;
48
49 static GHashTable *sel_prop_table = NULL;
50
51 static GdkSelProp *dropfiles_prop = NULL;
52
53 /* We store the owner of each selection in this table. Obviously, this only
54 * is valid intra-app, and in fact it is necessary for the intra-app DND to work.
55 */
56 static GHashTable *sel_owner_table = NULL;
57
58 /* GdkAtoms for well-known image formats */
59 static GdkAtom *known_pixbuf_formats;
60 static int n_known_pixbuf_formats;
61
62 /* GdkAtoms for well-known text formats */
63 static GdkAtom text_plain;
64 static GdkAtom text_plain_charset_utf_8;
65 static GdkAtom text_plain_charset_CP1252;
66
67 void
68 _gdk_win32_selection_init (void)
69 {
70 GSList *pixbuf_formats;
71 GSList *rover;
72
73 sel_prop_table = g_hash_table_new (NULL, NULL);
74 sel_owner_table = g_hash_table_new (NULL, NULL);
75 _format_atom_table = g_hash_table_new (NULL, NULL);
76
77 pixbuf_formats = gdk_pixbuf_get_formats ();
78
79 n_known_pixbuf_formats = 0;
80 for (rover = pixbuf_formats; rover != NULL; rover = rover->next)
81 {
82 gchar **mime_types =
83 gdk_pixbuf_format_get_mime_types ((GdkPixbufFormat *) rover->data);
84
85 gchar **mime_type;
86
87 for (mime_type = mime_types; *mime_type != NULL; mime_type++)
88 n_known_pixbuf_formats++;
89 }
90
91 known_pixbuf_formats = g_new (GdkAtom, n_known_pixbuf_formats);
92
93 n_known_pixbuf_formats = 0;
94 for (rover = pixbuf_formats; rover != NULL; rover = rover->next)
95 {
96 gchar **mime_types =
97 gdk_pixbuf_format_get_mime_types ((GdkPixbufFormat *) rover->data);
98
99 gchar **mime_type;
100
101 for (mime_type = mime_types; *mime_type != NULL; mime_type++)
102 known_pixbuf_formats[n_known_pixbuf_formats++] = gdk_atom_intern (*mime_type, FALSE);
103 }
104
105 g_slist_free (pixbuf_formats);
106
107 text_plain = gdk_atom_intern ("text/plain", FALSE);
108 text_plain_charset_utf_8= gdk_atom_intern ("text/plain;charset=utf-8", FALSE);
109 text_plain_charset_CP1252 = gdk_atom_intern ("text/plain;charset=CP1252", FALSE);
110
111 g_hash_table_replace (_format_atom_table,
112 GINT_TO_POINTER (_cf_png),
113 _image_png);
114
115 g_hash_table_replace (_format_atom_table,
116 GINT_TO_POINTER (CF_DIB),
117 _image_bmp);
118 }
119
120 /* The specifications for COMPOUND_TEXT and STRING specify that C0 and
121 * C1 are not allowed except for \n and \t, however the X conversions
122 * routines for COMPOUND_TEXT only enforce this in one direction,
123 * causing cut-and-paste of \r and \r\n separated text to fail.
124 * This routine strips out all non-allowed C0 and C1 characters
125 * from the input string and also canonicalizes \r, and \r\n to \n
126 */
127 static gchar *
128 sanitize_utf8 (const gchar *src,
129 gint length)
130 {
131 GString *result = g_string_sized_new (length + 1);
132 const gchar *p = src;
133 const gchar *endp = src + length;
134
135 while (p < endp)
136 {
137 if (*p == '\r')
138 {
139 p++;
140 if (*p == '\n')
141 p++;
142
143 g_string_append_c (result, '\n');
144 }
145 else
146 {
147 gunichar ch = g_utf8_get_char (p);
148 char buf[7];
149 gint buflen;
150
151 if (!((ch < 0x20 && ch != '\t' && ch != '\n') || (ch >= 0x7f && ch < 0xa0)))
152 {
153 buflen = g_unichar_to_utf8 (ch, buf);
154 g_string_append_len (result, buf, buflen);
155 }
156
157 p = g_utf8_next_char (p);
158 }
159 }
160 g_string_append_c (result, '\0');
161
162 return g_string_free (result, FALSE);
163 }
164
165 static gchar *
166 _gdk_utf8_to_string_target_internal (const gchar *str,
167 gint length)
168 {
169 GError *error = NULL;
170
171 gchar *tmp_str = sanitize_utf8 (str, length);
172 gchar *result = g_convert_with_fallback (tmp_str, -1,
173 "ISO-8859-1", "UTF-8",
174 NULL, NULL, NULL, &error);
175 if (!result)
176 {
177 g_warning ("Error converting from UTF-8 to STRING: %s",
178 error->message);
179 g_error_free (error);
180 }
181
182 g_free (tmp_str);
183 return result;
184 }
185
186 static void
187 selection_property_store (GdkWindow *owner,
188 GdkAtom type,
189 gint format,
190 guchar *data,
191 gint length)
192 {
193 GdkSelProp *prop;
194
195 g_return_if_fail (type != GDK_TARGET_STRING);
196
197 prop = g_hash_table_lookup (sel_prop_table, GDK_WINDOW_HWND (owner));
198
199 if (prop != NULL)
200 {
201 g_free (prop->data);
202 g_free (prop);
203 g_hash_table_remove (sel_prop_table, GDK_WINDOW_HWND (owner));
204 }
205
206 prop = g_new (GdkSelProp, 1);
207
208 prop->data = data;
209 prop->length = length;
210 prop->format = format;
211 prop->type = type;
212
213 g_hash_table_insert (sel_prop_table, GDK_WINDOW_HWND (owner), prop);
214 }
215
216 void
217 _gdk_dropfiles_store (gchar *data)
218 {
219 if (data != NULL)
220 {
221 g_assert (dropfiles_prop == NULL);
222
223 dropfiles_prop = g_new (GdkSelProp, 1);
224 dropfiles_prop->data = (guchar *) data;
225 dropfiles_prop->length = strlen (data) + 1;
226 dropfiles_prop->format = 8;
227 dropfiles_prop->type = _text_uri_list;
228 }
229 else
230 {
231 if (dropfiles_prop != NULL)
232 {
233 g_free (dropfiles_prop->data);
234 g_free (dropfiles_prop);
235 }
236 dropfiles_prop = NULL;
237 }
238 }
239
240 static gchar *
241 get_mapped_gdk_atom_name (GdkAtom gdk_target)
242 {
243 if (gdk_target == _image_png)
244 return g_strdup ("PNG");
245
246 if (gdk_target == _image_jpeg)
247 return g_strdup ("JFIF");
248
249 if (gdk_target == _image_gif)
250 return g_strdup ("GIF");
251
252 return gdk_atom_name (gdk_target);
253 }
254
255 gboolean
256 gdk_selection_owner_set_for_display (GdkDisplay *display,
257 GdkWindow *owner,
258 GdkAtom selection,
259 guint32 time,
260 gboolean send_event)
261 {
262 HWND hwnd;
263 GdkEvent tmp_event;
264
265 g_return_val_if_fail (display == _gdk_display, FALSE);
266 g_return_val_if_fail (selection != GDK_NONE, FALSE);
267
268 GDK_NOTE (DND, {
269 gchar *sel_name = gdk_atom_name (selection);
270
271 g_print ("gdk_selection_owner_set_for_display: %p %s\n",
272 (owner ? GDK_WINDOW_HWND (owner) : NULL),
273 sel_name);
274 g_free (sel_name);
275 });
276
277 if (selection != GDK_SELECTION_CLIPBOARD)
278 {
279 if (owner != NULL)
280 g_hash_table_insert (sel_owner_table, selection, GDK_WINDOW_HWND (owner));
281 else
282 g_hash_table_remove (sel_owner_table, selection);
283 return TRUE;
284 }
285
286 /* Rest of this function handles the CLIPBOARD selection */
287 if (owner != NULL)
288 {
289 if (GDK_WINDOW_DESTROYED (owner))
290 return FALSE;
291
292 hwnd = GDK_WINDOW_HWND (owner);
293 }
294 else
295 hwnd = NULL;
296
297 if (!API_CALL (OpenClipboard, (hwnd)))
298 return FALSE;
299
300 _ignore_destroy_clipboard = TRUE;
301 GDK_NOTE (DND, g_print ("... EmptyClipboard()\n"));
302 if (!API_CALL (EmptyClipboard, ()))
303 {
304 _ignore_destroy_clipboard = FALSE;
305 API_CALL (CloseClipboard, ());
306 return FALSE;
307 }
308 _ignore_destroy_clipboard = FALSE;
309
310 if (!API_CALL (CloseClipboard, ()))
311 return FALSE;
312
313 if (owner != NULL)
314 {
315 /* Send ourselves a selection request message so that
316 * gdk_property_change will be called to store the clipboard
317 * data.
318 */
319 GDK_NOTE (DND, g_print ("... sending GDK_SELECTION_REQUEST to ourselves\n"));
320 tmp_event.selection.type = GDK_SELECTION_REQUEST;
321 tmp_event.selection.window = owner;
322 tmp_event.selection.send_event = FALSE;
323 tmp_event.selection.selection = selection;
324 tmp_event.selection.target = _utf8_string;
325 tmp_event.selection.property = _gdk_selection;
326 tmp_event.selection.requestor = hwnd;
327 tmp_event.selection.time = time;
328
329 gdk_event_put (&tmp_event);
330 }
331
332 return TRUE;
333 }
334
335 GdkWindow*
336 gdk_selection_owner_get_for_display (GdkDisplay *display,
337 GdkAtom selection)
338 {
339 GdkWindow *window;
340
341 g_return_val_if_fail (display == _gdk_display, NULL);
342 g_return_val_if_fail (selection != GDK_NONE, NULL);
343
344 if (selection == GDK_SELECTION_CLIPBOARD)
345 {
346 HWND owner = GetClipboardOwner ();
347
348 if (owner == NULL)
349 return NULL;
350
351 return gdk_win32_handle_table_lookup ((GdkNativeWindow) owner);
352 }
353
354 window = gdk_window_lookup ((GdkNativeWindow) g_hash_table_lookup (sel_owner_table, selection));
355
356 GDK_NOTE (DND, {
357 gchar *sel_name = gdk_atom_name (selection);
358
359 g_print ("gdk_selection_owner_get: %s = %p\n",
360 sel_name,
361 (window ? GDK_WINDOW_HWND (window) : NULL));
362 g_free (sel_name);
363 });
364
365 return window;
366 }
367
368 static void
369 generate_selection_notify (GdkWindow *requestor,
370 GdkAtom selection,
371 GdkAtom target,
372 GdkAtom property,
373 guint32 time)
374 {
375 GdkEvent tmp_event;
376
377 tmp_event.selection.type = GDK_SELECTION_NOTIFY;
378 tmp_event.selection.window = requestor;
379 tmp_event.selection.send_event = FALSE;
380 tmp_event.selection.selection = selection;
381 tmp_event.selection.target = target;
382 tmp_event.selection.property = property;
383 tmp_event.selection.requestor = 0;
384 tmp_event.selection.time = time;
385
386 gdk_event_put (&tmp_event);
387 }
388
389 void
390 gdk_selection_convert (GdkWindow *requestor,
391 GdkAtom selection,
392 GdkAtom target,
393 guint32 time)
394 {
395 HGLOBAL hdata;
396 GdkAtom property = _gdk_selection;
397
398 g_return_if_fail (selection != GDK_NONE);
399 g_return_if_fail (requestor != NULL);
400
401 if (GDK_WINDOW_DESTROYED (requestor))
402 return;
403
404 GDK_NOTE (DND, {
405 gchar *sel_name = gdk_atom_name (selection);
406 gchar *tgt_name = gdk_atom_name (target);
407
408 g_print ("gdk_selection_convert: %p %s %s\n",
409 GDK_WINDOW_HWND (requestor),
410 sel_name, tgt_name);
411 g_free (sel_name);
412 g_free (tgt_name);
413 });
414
415 if (selection == GDK_SELECTION_CLIPBOARD && target == _targets)
416 {
417 gint ntargets, fmt;
418 GdkAtom *targets;
419 gboolean has_text = FALSE;
420 gboolean has_png = FALSE;
421 gboolean has_bmp = FALSE;
422
423 if (!API_CALL (OpenClipboard, (GDK_WINDOW_HWND (requestor))))
424 return;
425
426 targets = g_new (GdkAtom, CountClipboardFormats ());
427 ntargets = 0;
428
429 for (fmt = 0; 0 != (fmt = EnumClipboardFormats (fmt)); )
430 {
431 if (fmt == _cf_png)
432 {
433 targets[ntargets++] = _image_png;
434 has_png = TRUE;
435 }
436 }
437
438 for (fmt = 0; 0 != (fmt = EnumClipboardFormats (fmt)); )
439 {
440 gchar sFormat[80];
441
442 if (fmt == CF_UNICODETEXT || fmt == CF_TEXT)
443 {
444 /* Advertise text to GDK always as UTF8_STRING */
445 if (!has_text)
446 targets[ntargets++] = _utf8_string;
447 has_text = TRUE;
448 }
449 else if (fmt == _cf_png)
450 {
451 /* Already handled above */
452 }
453 else if (fmt == CF_DIB ||
454 fmt == CF_DIBV5)
455 {
456 /* Don't bother telling that a bitmap is present if there is
457 * also PNG, which is much more reliable in transferring
458 * transparency.
459 */
460 if (!has_bmp && !has_png)
461 targets[ntargets++] = _image_bmp;
462 has_bmp = TRUE;
463 }
464 else if (fmt == _cf_jfif)
465 {
466 /* Ditto for JPEG */
467 if (!has_png)
468 targets[ntargets++] = _image_jpeg;
469 }
470 else if (fmt == _cf_gif)
471 {
472 /* Ditto for GIF.
473 */
474 if (!has_png)
475 targets[ntargets++] = _image_gif;
476 }
477 else if (GetClipboardFormatName (fmt, sFormat, 80) > 0)
478 {
479 if (strcmp (sFormat, "image/bmp") == 0 ||
480 strcmp (sFormat, "image/x-bmp") == 0 ||
481 strcmp (sFormat, "image/x-MS-bmp") == 0 ||
482 strcmp (sFormat, "image/x-icon") == 0 ||
483 strcmp (sFormat, "image/x-ico") == 0 ||
484 strcmp (sFormat, "image/x-win-bitmap") == 0)
485 {
486 /* Ignore these (from older GTK+ versions
487 * presumably), as the same image in the CF_DIB
488 * format will also be on the clipboard anyway.
489 */
490 }
491 else
492 targets[ntargets++] = gdk_atom_intern (sFormat, FALSE);
493 }
494 }
495
496 GDK_NOTE (DND, {
497 int i;
498
499 g_print ("... ");
500 for (i = 0; i < ntargets; i++)
501 {
502 gchar *atom_name = gdk_atom_name (targets[i]);
503
504 g_print ("%s", atom_name);
505 g_free (atom_name);
506 if (i < ntargets - 1)
507 g_print (", ");
508 }
509 g_print ("\n");
510 });
511
512 if (ntargets > 0)
513 selection_property_store (requestor, GDK_SELECTION_TYPE_ATOM,
514 32, (guchar *) targets,
515 ntargets * sizeof (GdkAtom));
516 else
517 property = GDK_NONE;
518
519 API_CALL (CloseClipboard, ());
520 }
521 else if (selection == GDK_SELECTION_CLIPBOARD && target == _utf8_string)
522 {
523 /* Converting the CLIPBOARD selection means he wants the
524 * contents of the clipboard. Get the clipboard data, and store
525 * it for later.
526 */
527 if (!API_CALL (OpenClipboard, (GDK_WINDOW_HWND (requestor))))
528 return;
529
530 if ((hdata = GetClipboardData (CF_UNICODETEXT)) != NULL)
531 {
532 wchar_t *ptr, *p, *q;
533 guchar *data;
534 glong length, wclen;
535
536 if ((ptr = GlobalLock (hdata)) != NULL)
537 {
538 length = GlobalSize (hdata);
539
540 GDK_NOTE (DND, g_print ("... CF_UNICODETEXT: %ld bytes\n",
541 length));
542
543 /* Strip out \r */
544 p = ptr;
545 q = ptr;
546 wclen = 0;
547 while (p < ptr + length / 2)
548 {
549 if (*p != '\r')
550 {
551 *q++ = *p;
552 wclen++;
553 }
554 p++;
555 }
556
557 data = g_utf16_to_utf8 (ptr, wclen, NULL, NULL, NULL);
558
559 if (data)
560 selection_property_store (requestor, _utf8_string, 8,
561 data, strlen (data) + 1);
562 GlobalUnlock (hdata);
563 }
564 }
565 else
566 property = GDK_NONE;
567
568 API_CALL (CloseClipboard, ());
569 }
570 else if (selection == GDK_SELECTION_CLIPBOARD && target == _image_bmp)
571 {
572 if (!API_CALL (OpenClipboard, (GDK_WINDOW_HWND (requestor))))
573 return;
574
575 if ((hdata = GetClipboardData (CF_DIB)) != NULL)
576 {
577 BITMAPINFOHEADER *bi;
578
579 if ((bi = GlobalLock (hdata)) != NULL)
580 {
581 /* Need to add a BMP file header so gdk-pixbuf can load
582 * it.
583 *
584 * If the data is from Mozilla Firefox or IE7, and
585 * starts with an "old fashioned" BITMAPINFOHEADER,
586 * i.e. with biSize==40, and biCompression == BI_RGB and
587 * biBitCount==32, we assume that the "extra" byte in
588 * each pixel in fact is alpha.
589 *
590 * The gdk-pixbuf bmp loader doesn't trust 32-bit BI_RGB
591 * bitmaps to in fact have alpha, so we have to convince
592 * it by changing the bitmap header to a version 5
593 * BI_BITFIELDS one with explicit alpha mask indicated.
594 *
595 * The RGB bytes that are in bitmaps on the clipboard
596 * originating from Firefox or IE7 seem to be
597 * premultiplied with alpha. The gdk-pixbuf bmp loader
598 * of course doesn't expect that, so we have to undo the
599 * premultiplication before feeding the bitmap to the
600 * bmp loader.
601 *
602 * Note that for some reason the bmp loader used to want
603 * the alpha bytes in its input to actually be
604 * 255-alpha, but here we assume that this has been
605 * fixed before this is committed.
606 */
607 BITMAPFILEHEADER *bf;
608 gpointer data;
609 gint data_length = GlobalSize (hdata);
610 gint new_length;
611 gboolean make_dibv5 = FALSE;
612
613 GDK_NOTE (DND, g_print ("... CF_DIB: %d bytes\n", data_length));
614
615 if (bi->biSize == sizeof (BITMAPINFOHEADER) &&
616 bi->biPlanes == 1 &&
617 bi->biBitCount == 32 &&
618 bi->biCompression == BI_RGB &&
619 #if 0
620 /* Maybe check explicitly for Mozilla or IE7?
621 *
622 * If the clipboard format
623 * application/x-moz-nativeimage is present, that is
624 * a reliable indicator that the data is offered by
625 * Mozilla one would think. For IE7,
626 * UniformResourceLocatorW is presumably not that
627 * uniqie, so probably need to do some
628 * GetClipboardOwner(), GetWindowThreadProcessId(),
629 * OpenProcess(), GetModuleFileNameEx() dance to
630 * check?
631 */
632 (IsClipboardFormatAvailable
633 (RegisterClipboardFormat ("application/x-moz-nativeimage")) ||
634 IsClipboardFormatAvailable
635 (RegisterClipboardFormat ("UniformResourceLocatorW"))) &&
636 #endif
637 TRUE)
638 {
639 /* We turn the BITMAPINFOHEADER into a
640 * BITMAPV5HEADER before feeding it to gdk-pixbuf.
641 */
642 new_length = (data_length +
643 sizeof (BITMAPFILEHEADER) +
644 (sizeof (BITMAPV5HEADER) - sizeof (BITMAPINFOHEADER)));
645 make_dibv5 = TRUE;
646 }
647 else
648 {
649 new_length = data_length + sizeof (BITMAPFILEHEADER);
650 }
651
652 data = g_try_malloc (new_length);
653
654 if (data)
655 {
656 bf = (BITMAPFILEHEADER *)data;
657 bf->bfType = 0x4d42; /* "BM" */
658 bf->bfSize = new_length;
659 bf->bfReserved1 = 0;
660 bf->bfReserved2 = 0;
661
662 if (make_dibv5)
663 {
664 BITMAPV5HEADER *bV5 = (BITMAPV5HEADER *) ((char *) data + sizeof (BITMAPFILEHEADER));
665 guchar *p;
666 int i;
667
668 bV5->bV5Size = sizeof (BITMAPV5HEADER);
669 bV5->bV5Width = bi->biWidth;
670 bV5->bV5Height = bi->biHeight;
671 bV5->bV5Planes = 1;
672 bV5->bV5BitCount = 32;
673 bV5->bV5Compression = BI_BITFIELDS;
674 bV5->bV5SizeImage = 4 * bV5->bV5Width * ABS (bV5->bV5Height);
675 bV5->bV5XPelsPerMeter = bi->biXPelsPerMeter;
676 bV5->bV5YPelsPerMeter = bi->biYPelsPerMeter;
677 bV5->bV5ClrUsed = 0;
678 bV5->bV5ClrImportant = 0;
679 /* Now the added mask fields */
680 bV5->bV5RedMask = 0x00ff0000;
681 bV5->bV5GreenMask = 0x0000ff00;
682 bV5->bV5BlueMask = 0x000000ff;
683 bV5->bV5AlphaMask = 0xff000000;
684 ((char *) &bV5->bV5CSType)[3] = 's';
685 ((char *) &bV5->bV5CSType)[2] = 'R';
686 ((char *) &bV5->bV5CSType)[1] = 'G';
687 ((char *) &bV5->bV5CSType)[0] = 'B';
688 /* Ignore colorspace and profile fields */
689 bV5->bV5Intent = LCS_GM_GRAPHICS;
690 bV5->bV5Reserved = 0;
691
692 bf->bfOffBits = (sizeof (BITMAPFILEHEADER) +
693 bV5->bV5Size);
694
695 p = ((guchar *) data) + sizeof (BITMAPFILEHEADER) + sizeof (BITMAPV5HEADER);
696 memcpy (p, ((char *) bi) + bi->biSize,
697 data_length - sizeof (BITMAPINFOHEADER));
698
699 for (i = 0; i < bV5->bV5SizeImage/4; i++)
700 {
701 if (p[3] != 0)
702 {
703 gdouble inverse_alpha = 255./p[3];
704
705 p[0] = p[0] * inverse_alpha + 0.5;
706 p[1] = p[1] * inverse_alpha + 0.5;
707 p[2] = p[2] * inverse_alpha + 0.5;
708 }
709
710 p += 4;
711 }
712 }
713 else
714 {
715 bf->bfOffBits = (sizeof (BITMAPFILEHEADER) +
716 bi->biSize +
717 bi->biClrUsed * sizeof (RGBQUAD));
718
719 if (bi->biCompression == BI_BITFIELDS && bi->biBitCount >= 16)
720 {
721 /* Screenshots taken with PrintScreen or
722 * Alt + PrintScreen are found on the clipboard in
723 * this format. In this case the BITMAPINFOHEADER is
724 * followed by three DWORD specifying the masks of the
725 * red green and blue components, so adjust the offset
726 * accordingly. */
727 bf->bfOffBits += (3 * sizeof (DWORD));
728 }
729
730 memcpy ((char *) data + sizeof (BITMAPFILEHEADER),
731 bi,
732 data_length);
733 }
734
735 selection_property_store (requestor, _image_bmp, 8,
736 data, new_length);
737 }
738 GlobalUnlock (hdata);
739 }
740 }
741
742 API_CALL (CloseClipboard, ());
743 }
744 else if (selection == GDK_SELECTION_CLIPBOARD)
745 {
746 gchar *mapped_target_name;
747 UINT fmt = 0;
748
749 if (!API_CALL (OpenClipboard, (GDK_WINDOW_HWND (requestor))))
750 return;
751
752 mapped_target_name = get_mapped_gdk_atom_name (target);
753
754 /* Check if it's available. We could simply call
755 * GetClipboardData (RegisterClipboardFormat (targetname)), but
756 * the global custom format ID space is limited,
757 * (0xC000~0xFFFF), and we better not waste an format ID if we
758 * are just a requestor.
759 */
760 for ( ; 0 != (fmt = EnumClipboardFormats (fmt)); )
761 {
762 char sFormat[80];
763
764 if (GetClipboardFormatName (fmt, sFormat, 80) > 0 &&
765 strcmp (sFormat, mapped_target_name) == 0)
766 {
767 if ((hdata = GetClipboardData (fmt)) != NULL)
768 {
769 /* Simply get it without conversion */
770 guchar *ptr;
771 gint length;
772
773 if ((ptr = GlobalLock (hdata)) != NULL)
774 {
775 length = GlobalSize (hdata);
776
777 GDK_NOTE (DND, g_print ("... %s: %d bytes\n", mapped_target_name, length));
778
779 selection_property_store (requestor, target, 8,
780 g_memdup (ptr, length), length);
781 GlobalUnlock (hdata);
782 break;
783 }
784 }
785 }
786 }
787 g_free (mapped_target_name);
788 API_CALL (CloseClipboard, ());
789 }
790 else if (selection == _gdk_win32_dropfiles)
791 {
792 /* This means he wants the names of the dropped files.
793 * gdk_dropfiles_filter already has stored the text/uri-list
794 * data temporarily in dropfiles_prop.
795 */
796 if (dropfiles_prop != NULL)
797 {
798 selection_property_store
799 (requestor, dropfiles_prop->type, dropfiles_prop->format,
800 dropfiles_prop->data, dropfiles_prop->length);
801 g_free (dropfiles_prop);
802 dropfiles_prop = NULL;
803 }
804 }
805 else
806 property = GDK_NONE;
807
808 /* Generate a selection notify message so that we actually fetch the
809 * data (if property == _gdk_selection) or indicating failure (if
810 * property == GDK_NONE).
811 */
812 generate_selection_notify (requestor, selection, target, property, time);
813 }
814
815 gint
816 gdk_selection_property_get (GdkWindow *requestor,
817 guchar **data,
818 GdkAtom *ret_type,
819 gint *ret_format)
820 {
821 GdkSelProp *prop;
822
823 g_return_val_if_fail (requestor != NULL, 0);
824 g_return_val_if_fail (GDK_IS_WINDOW (requestor), 0);
825
826 if (GDK_WINDOW_DESTROYED (requestor))
827 return 0;
828
829 GDK_NOTE (DND, g_print ("gdk_selection_property_get: %p",
830 GDK_WINDOW_HWND (requestor)));
831
832 prop = g_hash_table_lookup (sel_prop_table, GDK_WINDOW_HWND (requestor));
833
834 if (prop == NULL)
835 {
836 GDK_NOTE (DND, g_print (" (nothing)\n"));
837 *data = NULL;
838
839 return 0;
840 }
841
842 *data = g_malloc (prop->length + 1);
843 (*data)[prop->length] = '\0';
844 if (prop->length > 0)
845 memmove (*data, prop->data, prop->length);
846
847 GDK_NOTE (DND, {
848 gchar *type_name = gdk_atom_name (prop->type);
849
850 g_print (" %s format:%d length:%d\n", type_name, prop->format, prop->length);
851 g_free (type_name);
852 });
853
854 if (ret_type)
855 *ret_type = prop->type;
856
857 if (ret_format)
858 *ret_format = prop->format;
859
860 return prop->length;
861 }
862
863 void
864 _gdk_selection_property_delete (GdkWindow *window)
865 {
866 GdkSelProp *prop;
867
868 GDK_NOTE (DND, g_print ("_gdk_selection_property_delete: %p (no-op)\n",
869 GDK_WINDOW_HWND (window)));
870
871 #if 1 /* without this we can only paste the first image from clipboard */
872 prop = g_hash_table_lookup (sel_prop_table, GDK_WINDOW_HWND (window));
873 if (prop != NULL)
874 {
875 g_free (prop->data);
876 g_free (prop);
877 g_hash_table_remove (sel_prop_table, GDK_WINDOW_HWND (window));
878 }
879 #endif
880 }
881
882 void
883 gdk_selection_send_notify_for_display (GdkDisplay *display,
884 GdkNativeWindow requestor,
885 GdkAtom selection,
886 GdkAtom target,
887 GdkAtom property,
888 guint32 time)
889 {
890 g_return_if_fail (display == _gdk_display);
891
892 GDK_NOTE (DND, {
893 gchar *sel_name = gdk_atom_name (selection);
894 gchar *tgt_name = gdk_atom_name (target);
895 gchar *prop_name = gdk_atom_name (property);
896
897 g_print ("gdk_selection_send_notify_for_display: %p %s %s %s (no-op)\n",
898 requestor, sel_name, tgt_name, prop_name);
899 g_free (sel_name);
900 g_free (tgt_name);
901 g_free (prop_name);
902 });
903 }
904
905 /* It's hard to say whether implementing this actually is of any use
906 * on the Win32 platform? gtk calls only
907 * gdk_text_property_to_utf8_list_for_display().
908 */
909 gint
910 gdk_text_property_to_text_list_for_display (GdkDisplay *display,
911 GdkAtom encoding,
912 gint format,
913 const guchar *text,
914 gint length,
915 gchar ***list)
916 {
917 gchar *result;
918 const gchar *charset;
919 gchar *source_charset;
920
921 g_return_val_if_fail (display == _gdk_display, 0);
922
923 GDK_NOTE (DND, {
924 gchar *enc_name = gdk_atom_name (encoding);
925
926 g_print ("gdk_text_property_to_text_list_for_display: %s %d %.20s %d\n",
927 enc_name, format, text, length);
928 g_free (enc_name);
929 });
930
931 if (!list)
932 return 0;
933
934 if (encoding == GDK_TARGET_STRING)
935 source_charset = g_strdup ("ISO-8859-1");
936 else if (encoding == _utf8_string)
937 source_charset = g_strdup ("UTF-8");
938 else
939 source_charset = gdk_atom_name (encoding);
940
941 g_get_charset (&charset);
942
943 result = g_convert (text, length, charset, source_charset,
944 NULL, NULL, NULL);
945 g_free (source_charset);
946
947 if (!result)
948 return 0;
949
950 *list = g_new (gchar *, 1);
951 **list = result;
952
953 return 1;
954 }
955
956 void
957 gdk_free_text_list (gchar **list)
958 {
959 g_return_if_fail (list != NULL);
960
961 g_free (*list);
962 g_free (list);
963 }
964
965 static gint
966 make_list (const gchar *text,
967 gint length,
968 gboolean latin1,
969 gchar ***list)
970 {
971 GSList *strings = NULL;
972 gint n_strings = 0;
973 gint i;
974 const gchar *p = text;
975 const gchar *q;
976 GSList *tmp_list;
977 GError *error = NULL;
978
979 while (p < text + length)
980 {
981 gchar *str;
982
983 q = p;
984 while (*q && q < text + length)
985 q++;
986
987 if (latin1)
988 {
989 str = g_convert (p, q - p,
990 "UTF-8", "ISO-8859-1",
991 NULL, NULL, &error);
992
993 if (!str)
994 {
995 g_warning ("Error converting selection from STRING: %s",
996 error->message);
997 g_error_free (error);
998 }
999 }
1000 else
1001 str = g_strndup (p, q - p);
1002
1003 if (str)
1004 {
1005 strings = g_slist_prepend (strings, str);
1006 n_strings++;
1007 }
1008
1009 p = q + 1;
1010 }
1011
1012 if (list)
1013 *list = g_new (gchar *, n_strings + 1);
1014
1015 (*list)[n_strings] = NULL;
1016
1017 i = n_strings;
1018 tmp_list = strings;
1019 while (tmp_list)
1020 {
1021 if (list)
1022 (*list)[--i] = tmp_list->data;
1023 else
1024 g_free (tmp_list->data);
1025
1026 tmp_list = tmp_list->next;
1027 }
1028
1029 g_slist_free (strings);
1030
1031 return n_strings;
1032 }
1033
1034 gint
1035 gdk_text_property_to_utf8_list_for_display (GdkDisplay *display,
1036 GdkAtom encoding,
1037 gint format,
1038 const guchar *text,
1039 gint length,
1040 gchar ***list)
1041 {
1042 g_return_val_if_fail (text != NULL, 0);
1043 g_return_val_if_fail (length >= 0, 0);
1044 g_return_val_if_fail (display == _gdk_display, 0);
1045
1046 if (encoding == GDK_TARGET_STRING)
1047 {
1048 return make_list ((gchar *)text, length, TRUE, list);
1049 }
1050 else if (encoding == _utf8_string)
1051 {
1052 return make_list ((gchar *)text, length, FALSE, list);
1053 }
1054 else
1055 {
1056 gchar *enc_name = gdk_atom_name (encoding);
1057
1058 g_warning ("gdk_text_property_to_utf8_list_for_display: encoding %s not handled\n", enc_name);
1059 g_free (enc_name);
1060
1061 if (list)
1062 *list = NULL;
1063
1064 return 0;
1065 }
1066 }
1067
1068 gint
1069 gdk_string_to_compound_text_for_display (GdkDisplay *display,
1070 const gchar *str,
1071 GdkAtom *encoding,
1072 gint *format,
1073 guchar **ctext,
1074 gint *length)
1075 {
1076 g_return_val_if_fail (str != NULL, 0);
1077 g_return_val_if_fail (length >= 0, 0);
1078 g_return_val_if_fail (display == _gdk_display, 0);
1079
1080 GDK_NOTE (DND, g_print ("gdk_string_to_compound_text_for_display: %.20s\n", str));
1081
1082 /* Always fail on Win32. No COMPOUND_TEXT support. */
1083
1084 if (encoding)
1085 *encoding = GDK_NONE;
1086
1087 if (format)
1088 *format = 0;
1089
1090 if (ctext)
1091 *ctext = NULL;
1092
1093 if (length)
1094 *length = 0;
1095
1096 return -1;
1097 }
1098
1099 gchar *
1100 gdk_utf8_to_string_target (const gchar *str)
1101 {
1102 return _gdk_utf8_to_string_target_internal (str, strlen (str));
1103 }
1104
1105 gboolean
1106 gdk_utf8_to_compound_text_for_display (GdkDisplay *display,
1107 const gchar *str,
1108 GdkAtom *encoding,
1109 gint *format,
1110 guchar **ctext,
1111 gint *length)
1112 {
1113 g_return_val_if_fail (str != NULL, FALSE);
1114 g_return_val_if_fail (display == _gdk_display, FALSE);
1115
1116 GDK_NOTE (DND, g_print ("gdk_utf8_to_compound_text_for_display: %.20s\n", str));
1117
1118 /* Always fail on Win32. No COMPOUND_TEXT support. */
1119
1120 if (encoding)
1121 *encoding = GDK_NONE;
1122
1123 if (format)
1124 *format = 0;
1125
1126 if (ctext)
1127 *ctext = NULL;
1128
1129 if (length)
1130 *length = 0;
1131
1132 return FALSE;
1133 }
1134
1135 void
1136 gdk_free_compound_text (guchar *ctext)
1137 {
1138 /* As we never generate anything claimed to be COMPOUND_TEXT, this
1139 * should never be called. Or if it is called, ctext should be the
1140 * NULL returned for conversions to COMPOUND_TEXT above.
1141 */
1142 g_return_if_fail (ctext == NULL);
1143 }
1144
1145 /* This function is called from gtk_selection_add_target() and
1146 * gtk_selection_add_targets() in gtkselection.c. It is this function
1147 * that takes care of setting those clipboard formats for which we use
1148 * delayed rendering. Formats copied directly to the clipboard are
1149 * handled in gdk_property_change() in gdkproperty-win32.c.
1150 */
1151
1152 void
1153 gdk_win32_selection_add_targets (GdkWindow *owner,
1154 GdkAtom selection,
1155 gint n_targets,
1156 GdkAtom *targets)
1157 {
1158 HWND hwnd = NULL;
1159 gboolean has_image = FALSE;
1160 gint i;
1161
1162 GDK_NOTE (DND, {
1163 gchar *sel_name = gdk_atom_name (selection);
1164
1165 g_print ("gdk_win32_selection_add_targets: %p: %s: ",
1166 owner ? GDK_WINDOW_HWND (owner) : NULL,
1167 sel_name);
1168 g_free (sel_name);
1169
1170 for (i = 0; i < n_targets; i++)
1171 {
1172 gchar *tgt_name = gdk_atom_name (targets[i]);
1173
1174 g_print ("%s", tgt_name);
1175 g_free (tgt_name);
1176 if (i < n_targets - 1)
1177 g_print (", ");
1178 }
1179 g_print ("\n");
1180 });
1181
1182 if (selection != GDK_SELECTION_CLIPBOARD)
1183 return;
1184
1185 if (owner != NULL)
1186 {
1187 if (GDK_WINDOW_DESTROYED (owner))
1188 return;
1189 hwnd = GDK_WINDOW_HWND (owner);
1190 }
1191
1192 if (!API_CALL (OpenClipboard, (hwnd)))
1193 return;
1194
1195 /* We have a very simple strategy: If some kind of pixmap image
1196 * format is being added, actually advertise just PNG and DIB. PNG
1197 * is our preferred format because it can losslessly represent any
1198 * image that gdk-pixbuf formats in general can, even with alpha,
1199 * unambiguously. CF_DIB is also advertised because of the general
1200 * support for it in Windows software, but note that alpha won't be
1201 * handled.
1202 */
1203 for (i = 0; !has_image && i < n_targets; ++i)
1204 {
1205 UINT cf;
1206 gchar *target_name;
1207 int j;
1208
1209 for (j = 0; j < n_known_pixbuf_formats; j++)
1210 if (targets[i] == known_pixbuf_formats[j])
1211 {
1212 if (!has_image)
1213 {
1214 GDK_NOTE (DND, g_print ("... SetClipboardData(PNG,NULL)\n"));
1215 SetClipboardData (_cf_png, NULL);
1216
1217 GDK_NOTE (DND, g_print ("... SetClipboardData(CF_DIB,NULL)\n"));
1218 SetClipboardData (CF_DIB, NULL);
1219
1220 has_image = TRUE;
1221 }
1222 break;
1223 }
1224
1225 /* If it is one of the pixmap formats, already handled or not
1226 * needed.
1227 */
1228 if (j < n_known_pixbuf_formats)
1229 continue;
1230
1231 /* We don't bother registering and advertising clipboard formats
1232 * that are X11 specific or no non-GTK+ apps will have ever
1233 * heard of, and when there are equivalent clipboard formats
1234 * that are commonly used.
1235 */
1236 if (targets[i] == _save_targets ||
1237 targets[i] == _utf8_string ||
1238 targets[i] == GDK_TARGET_STRING ||
1239 targets[i] == _compound_text ||
1240 targets[i] == _text ||
1241 targets[i] == text_plain_charset_utf_8 ||
1242 targets[i] == text_plain_charset_CP1252 ||
1243 targets[i] == text_plain)
1244 continue;
1245
1246 target_name = gdk_atom_name (targets[i]);
1247
1248 if (g_str_has_prefix (target_name, "text/plain;charset="))
1249 {
1250 g_free (target_name);
1251 continue;
1252 }
1253
1254 cf = RegisterClipboardFormat (target_name);
1255
1256 g_hash_table_replace (_format_atom_table,
1257 GINT_TO_POINTER (cf),
1258 targets[i]);
1259
1260 GDK_NOTE (DND, g_print ("... SetClipboardData(%s,NULL)\n",
1261 _gdk_win32_cf_to_string (cf)));
1262 SetClipboardData (cf, NULL);
1263
1264 g_free (target_name);
1265 }
1266 API_CALL (CloseClipboard, ());
1267 }
1268
1269 /* Convert from types such as "image/jpg" or "image/png" to DIB using
1270 * gdk-pixbuf so that image copied from GTK+ apps can be pasted in
1271 * native apps like mspaint.exe
1272 */
1273 HGLOBAL
1274 _gdk_win32_selection_convert_to_dib (HGLOBAL hdata,
1275 GdkAtom target)
1276 {
1277 GDK_NOTE (DND, {
1278 gchar *target_name = gdk_atom_name (target);
1279
1280 g_print ("_gdk_win32_selection_convert_to_dib: %p %s\n",
1281 hdata, target_name);
1282 g_free (target_name);
1283 });
1284
1285 if (target == _image_bmp)
1286 {
1287 HGLOBAL hdatanew;
1288 SIZE_T size;
1289 guchar *ptr;
1290
1291 g_return_val_if_fail (GlobalSize (hdata) >= sizeof (BITMAPFILEHEADER), NULL);
1292
1293 /* No conversion is needed, just strip the BITMAPFILEHEADER */
1294 size = GlobalSize (hdata) - sizeof (BITMAPFILEHEADER);
1295 ptr = GlobalLock (hdata);
1296
1297 memmove (ptr, ptr + sizeof (BITMAPFILEHEADER), size);
1298 GlobalUnlock (hdata);
1299
1300 if ((hdatanew = GlobalReAlloc (hdata, size, GMEM_MOVEABLE)) == NULL)
1301 {
1302 WIN32_API_FAILED ("GlobalReAlloc");
1303 GlobalFree (hdata); /* The old hdata is not freed if error */
1304 }
1305 return hdatanew;
1306 }
1307
1308 g_warning ("Should not happen: We provide some image format but not CF_DIB and CF_DIB is requested.");
1309
1310 return NULL;
1311 }