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, see <http://www.gnu.org/licenses/>.
17 */
18
19 /*
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
24 */
25
26 /*
27 GTK+ selection works like this:
28 There are three selections that matter - GDK_SELECTION_CLIPBOARD,
29 GDK_SELECTION_PRIMARY and DND. Primary selection is only handled
30 internally by GTK+ (it's not portable to Windows). DND is actually
31 represented by two selections - LOCAL and OLE2, one for each DnD protocol,
32 but they work the same way.
33
34 "Target" is a GdkAtom describing a clipboard format.
35
36 For Clipboard:
37 GTK+ calls gtk_clipboard_set_contents(), which first ensures the
38 clipboard is owned by the clipboard widget (which also indirectly
39 causes an SelectionRequest xevent to be sent to it), then clears the old
40 supported targets from the clipboard, then adds all the
41 targets it's given to the clipboard. No data is sent anywhere.
42
43 gtk_clipboard_set_contents() is also given a callback to invoke when
44 the actual data is needed. This callback is implemented by the widget
45 from which the data can be put into clipboard.
46
47 GTK+ might also call gtk_clipboard_set_can_store(), which sets the
48 targets for which the data can be put into system clipboard, so that
49 it remains usable even if the application is no longer around. Usually
50 all data formats are storable, except for the shortcut formats, which
51 refer to actual widgets directly, and are thus only working while
52 the application is alive.
53
54 ("C:" means clipboard client (requestor), "S:" means clipboard server (provider))
55
56 When something needs to be obtained from clipboard, GTK+ calls
57 C: gtk_selection_convert().
58 That function has a shortcut where it directly gets the selection contents by calling
59 S: gtk_selection_invoke_handler(),
60 asking the widget to provide data, and then calling
61 C: gtk_selection_retrieval_report()
62 to report the data back to the caller.
63
64 If that shortcut isn't possible (selection is owned by another process),
65 gtk_selection_convert() calls
66 C:gdk_selection_convert() (_gdk_x11_display_convert_selection())
67
68 On X11 gdk_selection_convert() just calls
69 C:XConvertSelection(),
70 which sends SelectionRequest xevent to the window that owns the selection.
71 The client gives its clipboard window as the requestor for that event,
72 and gives the property as GDK_SELECTION.
73
74 Server-side GTK+ catches SelectionRequest in a
75 S:_gtk_selection_request()
76 event handler, which calls
77 S:gtk_selection_invoke_handler()
78 to get the data, and then calls
79 S:gdk_property_change() (_gdk_x11_window_change_property())
80 to submit the data, by setting the property given by the message sender
81 (GDK_SELECTION) on the requestor window (our client clipboard window).
82
83 On X11 data submission takes from of
84 S:XChangeProperty()
85 call, which causes SelectionNotify (and PropertyNotify for INCR)
86 xevent to be sent, which client-side GTK+ catches and handles in
87 C:_gtk_selection_notify()
88 (and
89 C:_gtk_selection_property_notify(),
90 for INCR)
91 event handler, which calls
92 C:gtk_selection_retrieval_report()
93 to report back to the caller. The caller gets the property
94 data from the window, and returns it up the stack.
95
96 On X11 the "TARGETS" target might be given in a SelectionRequest xmessage to request
97 all supported targets for a selection.
98
99 If data must be stored on the clipboard, because the application is quitting,
100 GTK+ will call
101 S:gdk_clipboard_store() -> gdk_display_store_clipboard() (gdk_x11_display_store_clipboard())
102 on all the clipboards it owns.
103 X11 gdk_display_store_clipboard() puts a list of storeable targets into GDK_SELECTION
104 property of the clipboard window, then calls
105 S:XConvertSelection()
106 on the clipboard manager window (retrieved from the CLIPBOARD_MANAGER atom),
107 and the clipboard manager responds by requesting all these formats and storing the data,
108 then responds with SelectionNotify xevent to allow the application to quit.
109
110 When clipboard owner changes, the old owner receives SelectionClear xevent,
111 GTK+ handles it by clearing the clipboard object on its own level, GDK
112 is not involved.
113
114 On Windows:
115 Clipboard is opened by OpenClipboard(), emptied by EmptyClipboard() (which also
116 makes the window the clipboard owner), data is put into it by SetClipboardData().
117 Clipboard is closed with CloseClipboard().
118 If SetClipboardData() is given a NULL data value, the owner will later
119 receive WM_RENDERFORMAT message, in response to which it must call
120 SetClipboardData() with the provided handle and the actual data this time.
121 This way applications can avoid storing everything in the clipboard
122 all the time, only putting the data there as it is requested by other applications.
123 At some undefined points of time an application might get WM_RENDERALLFORMATS
124 message, it should respond by opening the clipboard and rendering
125 into it all the data that it offers, as if responding to multiple WM_RENDERFORMAT
126 messages.
127
128 On GDK-Win32:
129 GTK+ calls gtk_clipboard_set_contents(), which first ensures the
130 clipboard is owned by the clipboard widget (calls OpenClipboard(),
131 then EmptyClipboard() to become the owner, then
132 sends a TARGETS GDK_SELECTION_REQUEST to itself, without closing the clipboard),
133 then clears the old supported targets from the clipboard, then adds all the
134 targets it's given to the clipboard. No data is sent anywhere.
135
136 gtk_clipboard_set_contents() is also given a callback to invoke when
137 the actual data is needed. This callback is implemented by the widget
138 from which the data can be put into clipboard.
139
140 GTK+ might also call gtk_clipboard_set_can_store(), which sets the
141 targets for which the data can be put into system clipboard, so that
142 it remains usable even if the application is no longer around. Usually
143 all data formats are storable, except for the shortcut formats, which
144 refer to actual widgets directly, and are thus only working while
145 the application is alive.
146
147 ("C:" means clipboard client (requestor), "S:" means clipboard server (provider))
148 ("transmute" here means "change the format of some data"; this term is used here
149 instead of "convert" to avoid clashing with g(t|d)k_selection_convert(), which
150 is completely unrelated)
151
152 When something needs to be obtained from clipboard, GTK+ calls
153 C: gtk_selection_convert().
154 That function has a shortcut where it directly gets the selection contents by calling
155 S: gtk_selection_invoke_handler(),
156 asking the widget to provide data, and then calling
157 C: gtk_selection_retrieval_report()
158 to report the data back to the caller.
159
160 If that shortcut isn't possible (selection is owned by another process),
161 gtk_selection_convert() calls
162 C:gdk_selection_convert() (_gdk_win32_display_convert_selection())
163
164 On GDK-Win32 gdk_selection_convert() just calls
165 C:OpenClipboard()
166 to open clipboard (if that fails, it shedules a timeout to regularly
167 try to open clipboard for the next 30 seconds, and do the actions
168 outlined below once the clipboard is opened, or notify about
169 conversion failure after 30 seconds),
170 C:EnumClipboardFormats() (2000+)
171 to get the list of supported formats, figures out the format it should
172 use to request the data (first it looks for supported formats with names
173 that match the target name, then looks through compatibility
174 formats for the target and checks whether these are supported).
175 Note that it has no list of supported targets at hand,
176 just the single requested target, and thus it might have
177 to do some transmutation between formats; the caller up the stack
178 either only supports just one format that it asks for,
179 or supports multiple formats and asks for them in sequence (from
180 the most preferred to the least preferred), until one call succeeds,
181 or supports multiple formats and asks for the TARGETS format first,
182 and then figures out what to ask for - GDK can't know that.
183 Either way, GDK has to call
184 C:GetClipboardData()
185 to get the data (this causes WM_RENDERFORMAT to be sent to the owner,
186 if the owner uses delayed rendering for the requested format, otherwise
187 it just picks the data right from the OS)
188
189 Server-side GDK catches WM_RENDERFORMAT, figures out a target
190 to request (this one is easier, as it has the list of supported
191 targets saved up), and posts a GDK_SELECTION_REQUEST event, then runs the main loop,
192 while GTK+ catches the event in a
193 S:_gtk_selection_request()
194 event handler, which calls
195 S:gtk_selection_invoke_handler()
196 to get the data, and then calls
197 S:gdk_property_change() (_gdk_win32_window_change_property())
198 to submit the data, by first transmuting it to the format actually requested
199 by the sender of WM_RENDERFORMAT, and then by returning thedata back up the stack,
200 to the WM_RENDERFORMAT handler, which then calls
201 S:SetClipboardData()
202 with the handle provided by the sender.
203
204 Meanwhile, the client code, still in
205 C:_gdk_win32_display_convert_selection(),
206 gets the data in response to GetClipboardData(),
207 transmutes it (if needed) to the target format, sets the requested
208 window property to that data (unlike change_property!),
209 calls
210 C:CloseClipboard() (if there are no more clipboard opeartions
211 scheduled)
212 and posts a GDK_SELECTION_NOTIFY event, which GTK+ catches in
213 C:_gtk_selection_notify()
214 event handler, which calls
215 C:gtk_selection_retrieval_report()
216 to report back to the caller. The caller gets the property
217 data from the window, and returns it up the stack.
218
219 On GDK-Win32 the "TARGETS" target might be given in a GDK_SELECTION_REQUEST to request
220 all supported targets for a selection.
221 Note that this server side -
222 client side should call gdk_selection_convert() -> gdk_selection_convert() with "TARGETS" target
223 to get the list of targets offered by the clipboard holder. It never causes GDK_SELECTION_REQUEST
224 to be generated, just queries the system clipboard.
225 On server side GDK_SELECTION_REQUEST is only generated internally:
226 in response to WM_RENDERFORMAT (it renders a target),
227 in response to idataobject_getdata() (it renders a target),
228 after DnD ends (with a DELETE target, this is caught by GTK to make it delete the selection),
229 and in response to owner change, with TARGETS target, which makes it register its formats by calling
230 S:SetClipboardData(..., NULL)
231
232 If data must be stored on the clipboard, because the application is quitting,
233 GTK+ will call
234 S:gdk_clipboard_store() -> gdk_display_store_clipboard() (gdk_win32_display_store_clipboard())
235 on all the clipboards it owns.
236 GDK-Win32 gdk_display_store_clipboard() sends WM_RENDERALLFORMATS to the window,
237 then posts a GDK_SELECTION_NOTIFY event allow the application to quit.
238
239 When clipboard owner changes, the old owner receives WM_DESTROYCLIPBOARD message,
240 GDK handles it by posting a GDK_SELECTION_CLEAR event, which
241 GTK+ handles by clearing the clipboard object on its own level.
242
243 Any operations that require OpenClipboard()/CloseClipboard() combo (i.e.
244 everything, except for WM_RENDERFORMAT handling) must be put into a queue,
245 and then a once-per-second-for-up-to-30-seconds timeout must be added.
246 The timeout function must call OpenClipboard(),
247 and then proceed to perform the queued actions on the clipboard, once it opened,
248 or return and try again a second later, as long as there are still items in the queue,
249 and remove the queue items that are older than 30 seconds.
250 Once the queue is empty, the clipboard is closed.
251
252 DND:
253 GDK-Win32:
254 S:idataobject_getdata()
255 sends a GDK_SELECTION_REQUEST event, which results in a call to
256 S:_gdk_win32_window_change_property()
257 which passes clipboard data back via the selection singleton.
258 GDK-Win32 uses delayed rendering for all formats, even text.
259
260 GTK+ will call
261 C:gtk_selection_convert() -> gdk_selection_convert() (_gdk_win32_display_convert_selection())
262 to get the data associated with the drag, when GTK+ apps want to inspect the data,
263 but with a OLE2_DND selection instead of CLIPBOARD selection.
264
265 _gdk_win32_display_convert_selection() queries the droptarget global variable,
266 which should already contain a matched list of supported formats and targets,
267 picks a format there, then queries it from the IDataObject that the droptarget kept around.
268 Then optionally transmutes the data, and sets the property. Then posts GDK_SELECTION_NOTIFY.
269
270 GTK+ catches that event and processes it, causeing "selection-received" signal to
271 be emitted on the selection widget, and its handler is
272 C:gtk_drag_selection_received(),
273 which emits the "drag-data-received" signal for the app.
274 */
275
276 #include "config.h"
277 #include <string.h>
278 #include <stdlib.h>
279
280 /* For C-style COM wrapper macros */
281 #define COBJMACROS
282
283 /* for CIDA */
284 #include <shlobj.h>
285
286 #include "gdkproperty.h"
287 #include "gdkselection.h"
288 #include "gdkdisplay.h"
289 #include "gdkprivate-win32.h"
290 #include "gdkselection-win32.h"
291 #include "gdk/gdkdndprivate.h"
292 #include "gdkwin32dnd-private.h"
293 #include "gdkwin32.h"
294
295 typedef struct _GdkWin32ClipboardQueueInfo GdkWin32ClipboardQueueInfo;
296
297 typedef enum _GdkWin32ClipboardQueueAction GdkWin32ClipboardQueueAction;
298
299 enum _GdkWin32ClipboardQueueAction
300 {
301 GDK_WIN32_CLIPBOARD_QUEUE_ACTION_CONVERT = 0,
302 GDK_WIN32_CLIPBOARD_QUEUE_ACTION_TARGETS
303 };
304
305 struct _GdkWin32ClipboardQueueInfo
306 {
307 GdkDisplay *display;
308 GdkWindow *requestor;
309 GdkAtom selection;
310 GdkAtom target;
311 guint32 time;
312
313 /* Number of seconds since we started our
314 * attempts to open clipboard.
315 */
316 guint32 idle_time;
317
318 /* What to do once clipboard is opened */
319 GdkWin32ClipboardQueueAction action;
320 };
321
322 /* List of GdkWin32ClipboardQueueInfo slices */
323 static GList *clipboard_queue = NULL;
324
325 #define HIDA_GetPIDLFolder(pida) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[0])
326 #define HIDA_GetPIDLItem(pida, i) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[i+1])
327
328 G_DEFINE_TYPE (GdkWin32Selection, gdk_win32_selection, G_TYPE_OBJECT)
329
330 static void
331 gdk_win32_selection_class_init (GdkWin32SelectionClass *klass)
332 {
333 }
334
335 void
336 _gdk_win32_selection_init (void)
337 {
338 _win32_selection = GDK_WIN32_SELECTION (g_object_new (GDK_TYPE_WIN32_SELECTION, NULL));
339 }
340
341 static void
342 gdk_win32_selection_init (GdkWin32Selection *win32_selection)
343 {
344 GArray *atoms;
345 GArray *cfs;
346 GSList *pixbuf_formats;
347 GSList *rover;
348 int i;
349 GArray *comp;
350 GdkSelTargetFormat fmt;
351
352 win32_selection->ignore_destroy_clipboard = FALSE;
353 win32_selection->clipboard_opened_for = INVALID_HANDLE_VALUE;
354
355 win32_selection->dnd_target_state = GDK_WIN32_DND_NONE;
356 win32_selection->dnd_source_state = GDK_WIN32_DND_NONE;
357 win32_selection->dnd_data_object_target = NULL;
358 win32_selection->property_change_format = 0;
359 win32_selection->property_change_data = NULL;
360 win32_selection->property_change_target_atom = 0;
361
362 atoms = g_array_sized_new (FALSE, TRUE, sizeof (GdkAtom), GDK_WIN32_ATOM_INDEX_LAST);
363 g_array_set_size (atoms, GDK_WIN32_ATOM_INDEX_LAST);
364 cfs = g_array_sized_new (FALSE, TRUE, sizeof (UINT), GDK_WIN32_CF_INDEX_LAST);
365 g_array_set_size (cfs, GDK_WIN32_CF_INDEX_LAST);
366
367 win32_selection->known_atoms = atoms;
368 win32_selection->known_clipboard_formats = cfs;
369
370 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_GDK_SELECTION) = gdk_atom_intern_static_string ("GDK_SELECTION");
371 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_CLIPBOARD_MANAGER) = gdk_atom_intern_static_string ("CLIPBOARD_MANAGER");
372 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_WM_TRANSIENT_FOR) = gdk_atom_intern_static_string ("WM_TRANSIENT_FOR");
373 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_TARGETS) = gdk_atom_intern_static_string ("TARGETS");
374 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_DELETE) = gdk_atom_intern_static_string ("DELETE");
375 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_SAVE_TARGETS) = gdk_atom_intern_static_string ("SAVE_TARGETS");
376 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_UTF8_STRING) = gdk_atom_intern_static_string ("UTF8_STRING");
377 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_TEXT) = gdk_atom_intern_static_string ("TEXT");
378 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_COMPOUND_TEXT) = gdk_atom_intern_static_string ("COMPOUND_TEXT");
379 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_TEXT_URI_LIST) = gdk_atom_intern_static_string ("text/uri-list");
380 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_TEXT_HTML) = gdk_atom_intern_static_string ("text/html");
381 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_IMAGE_PNG) = gdk_atom_intern_static_string ("image/png");
382 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_IMAGE_JPEG) = gdk_atom_intern_static_string ("image/jpeg");
383 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_IMAGE_BMP) = gdk_atom_intern_static_string ("image/bmp");
384 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_IMAGE_GIF) = gdk_atom_intern_static_string ("image/gif");
385
386 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_LOCAL_DND_SELECTION) = gdk_atom_intern_static_string ("LocalDndSelection");
387 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_DROPFILES_DND) = gdk_atom_intern_static_string ("DROPFILES_DND");
388 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_OLE2_DND) = gdk_atom_intern_static_string ("OLE2_DND");
389
390 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_PNG)= gdk_atom_intern_static_string ("PNG");
391 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_JFIF) = gdk_atom_intern_static_string ("JFIF");
392 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_GIF) = gdk_atom_intern_static_string ("GIF");
393
394 /* These are a bit unusual. It's here to allow GTK+ applications
395 * to actually support CF_DIB and Shell ID List clipboard formats on their own,
396 * instead of allowing GDK to use them internally for interoperability.
397 */
398 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_CF_DIB) = gdk_atom_intern_static_string ("CF_DIB");
399 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_CFSTR_SHELLIDLIST) = gdk_atom_intern_static_string (CFSTR_SHELLIDLIST);
400 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_CF_UNICODETEXT) = gdk_atom_intern_static_string ("CF_UNICODETEXT");
401 _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_CF_TEXT) = gdk_atom_intern_static_string ("CF_TEXT");
402
403 /* MS Office 2007, at least, offers images in common file formats
404 * using clipboard format names like "PNG" and "JFIF". So we follow
405 * the lead and map the GDK target name "image/png" to the clipboard
406 * format name "PNG" etc.
407 */
408 _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_PNG) = RegisterClipboardFormatA ("PNG");
409 _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_JFIF) = RegisterClipboardFormatA ("JFIF");
410 _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_GIF) = RegisterClipboardFormatA ("GIF");
411
412 _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_UNIFORMRESOURCELOCATORW) = RegisterClipboardFormatA ("UniformResourceLocatorW");
413 _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_CFSTR_SHELLIDLIST) = RegisterClipboardFormatA (CFSTR_SHELLIDLIST);
414 _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_HTML_FORMAT) = RegisterClipboardFormatA ("HTML Format");
415 _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_TEXT_HTML) = RegisterClipboardFormatA ("text/html");
416
417 _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_IMAGE_PNG) = RegisterClipboardFormatA ("image/png");
418 _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_IMAGE_JPEG) = RegisterClipboardFormatA ("image/jpeg");
419 _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_IMAGE_BMP) = RegisterClipboardFormatA ("image/bmp");
420 _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_IMAGE_GIF) = RegisterClipboardFormatA ("image/gif");
421 _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_TEXT_URI_LIST) = RegisterClipboardFormatA ("text/uri-list");
422 _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_UTF8_STRING) = RegisterClipboardFormatA ("UTF8_STRING");
423
424 win32_selection->sel_prop_table = g_hash_table_new (NULL, NULL);
425 win32_selection->sel_owner_table = g_hash_table_new (NULL, NULL);
426
427 pixbuf_formats = gdk_pixbuf_get_formats ();
428
429 win32_selection->n_known_pixbuf_formats = 0;
430 for (rover = pixbuf_formats; rover != NULL; rover = rover->next)
431 {
432 gchar **mime_types =
433 gdk_pixbuf_format_get_mime_types ((GdkPixbufFormat *) rover->data);
434
435 gchar **mime_type;
436
437 for (mime_type = mime_types; *mime_type != NULL; mime_type++)
438 win32_selection->n_known_pixbuf_formats++;
439 }
440
441 win32_selection->known_pixbuf_formats = g_new (GdkAtom, win32_selection->n_known_pixbuf_formats);
442
443 i = 0;
444 for (rover = pixbuf_formats; rover != NULL; rover = rover->next)
445 {
446 gchar **mime_types =
447 gdk_pixbuf_format_get_mime_types ((GdkPixbufFormat *) rover->data);
448
449 gchar **mime_type;
450
451 for (mime_type = mime_types; *mime_type != NULL; mime_type++)
452 win32_selection->known_pixbuf_formats[i++] = gdk_atom_intern (*mime_type, FALSE);
453 }
454
455 g_slist_free (pixbuf_formats);
456
457 win32_selection->dnd_selection_targets = g_array_new (FALSE, FALSE, sizeof (GdkSelTargetFormat));
458 win32_selection->clipboard_selection_targets = g_array_new (FALSE, FALSE, sizeof (GdkSelTargetFormat));
459 win32_selection->compatibility_formats = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify) g_array_unref);
460
461 /* GTK+ actually has more text formats, but it's unlikely that we'd
462 * get anything other than UTF8_STRING these days.
463 * GTKTEXTBUFFERCONTENTS format can potentially be converted to
464 * W32-compatible rich text format, but that's too complex to address right now.
465 */
466 comp = g_array_sized_new (FALSE, FALSE, sizeof (GdkSelTargetFormat), 3);
467 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_UTF8_STRING);
468
469 fmt.format = _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_UTF8_STRING);
470 fmt.transmute = FALSE;
471 g_array_append_val (comp, fmt);
472
473 fmt.format = CF_UNICODETEXT;
474 fmt.transmute = TRUE;
475 g_array_append_val (comp, fmt);
476
477 fmt.format = CF_TEXT;
478 g_array_append_val (comp, fmt);
479
480 g_hash_table_replace (win32_selection->compatibility_formats, fmt.target, comp);
481
482
483 comp = g_array_sized_new (FALSE, FALSE, sizeof (GdkSelTargetFormat), 3);
484 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_IMAGE_PNG);
485
486 fmt.format = _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_IMAGE_PNG);
487 fmt.transmute = FALSE;
488 g_array_append_val (comp, fmt);
489
490 fmt.format = _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_PNG);
491 g_array_append_val (comp, fmt);
492
493 g_hash_table_replace (win32_selection->compatibility_formats, fmt.target, comp);
494
495
496 comp = g_array_sized_new (FALSE, FALSE, sizeof (GdkSelTargetFormat), 4);
497 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_IMAGE_JPEG);
498
499 fmt.format = _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_IMAGE_JPEG);
500 fmt.transmute = FALSE;
501 g_array_append_val (comp, fmt);
502
503 fmt.format = _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_JFIF);
504 g_array_append_val (comp, fmt);
505
506 g_hash_table_replace (win32_selection->compatibility_formats, fmt.target, comp);
507
508
509 comp = g_array_sized_new (FALSE, FALSE, sizeof (GdkSelTargetFormat), 4);
510 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_IMAGE_GIF);
511
512 fmt.format = _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_IMAGE_GIF);
513 fmt.transmute = FALSE;
514 g_array_append_val (comp, fmt);
515
516 fmt.format = _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_GIF);
517 g_array_append_val (comp, fmt);
518
519 g_hash_table_replace (win32_selection->compatibility_formats, fmt.target, comp);
520
521
522 comp = g_array_sized_new (FALSE, FALSE, sizeof (GdkSelTargetFormat), 2);
523 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_IMAGE_BMP);
524
525 fmt.format = _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_IMAGE_BMP);
526 fmt.transmute = FALSE;
527 g_array_append_val (comp, fmt);
528
529 fmt.format = CF_DIB;
530 fmt.transmute = TRUE;
531 g_array_append_val (comp, fmt);
532
533 g_hash_table_replace (win32_selection->compatibility_formats, fmt.target, comp);
534
535
536 /* Not implemented, but definitely possible
537 comp = g_array_sized_new (FALSE, FALSE, 2);
538 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_TEXT_URI_LIST);
539
540 fmt.format = _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_TEXT_URI_LIST);
541 fmt.transmute = FALSE;
542 g_array_append_val (comp, fmt);
543
544 fmt.format = _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_CFSTR_SHELLIDLIST);
545 fmt.transmute = TRUE;
546 g_array_append_val (comp, fmt);
547
548 g_hash_table_replace (win32_selection->compatibility_formats, fmt.target, comp);
549 */
550
551 win32_selection->compatibility_targets = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify) g_array_unref);
552
553 comp = g_array_sized_new (FALSE, FALSE, sizeof (GdkSelTargetFormat), 2);
554 fmt.format = CF_TEXT;
555 fmt.transmute = FALSE;
556
557 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_CF_TEXT);
558 g_array_append_val (comp, fmt);
559
560 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_UTF8_STRING);
561 fmt.transmute = TRUE;
562 g_array_append_val (comp, fmt);
563
564 g_hash_table_replace (win32_selection->compatibility_targets, GINT_TO_POINTER (CF_TEXT), comp);
565
566
567 comp = g_array_sized_new (FALSE, FALSE, sizeof (GdkSelTargetFormat), 2);
568 fmt.format = CF_UNICODETEXT;
569 fmt.transmute = FALSE;
570
571 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_CF_UNICODETEXT);
572 g_array_append_val (comp, fmt);
573
574 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_UTF8_STRING);
575 fmt.transmute = TRUE;
576 g_array_append_val (comp, fmt);
577
578 g_hash_table_replace (win32_selection->compatibility_targets, GINT_TO_POINTER (CF_UNICODETEXT), comp);
579
580
581 comp = g_array_sized_new (FALSE, FALSE, sizeof (GdkSelTargetFormat), 3);
582 fmt.format = _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_PNG);
583 fmt.transmute = FALSE;
584
585 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_PNG);
586 g_array_append_val (comp, fmt);
587
588 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_IMAGE_PNG);
589 g_array_append_val (comp, fmt);
590
591 g_hash_table_replace (win32_selection->compatibility_targets, GINT_TO_POINTER (_gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_PNG)), comp);
592
593
594 comp = g_array_sized_new (FALSE, FALSE, sizeof (GdkSelTargetFormat), 4);
595 fmt.format = _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_JFIF);
596 fmt.transmute = FALSE;
597
598 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_JFIF);
599 g_array_append_val (comp, fmt);
600
601 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_IMAGE_JPEG);
602 g_array_append_val (comp, fmt);
603
604 g_hash_table_replace (win32_selection->compatibility_targets, GINT_TO_POINTER (_gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_JFIF)), comp);
605
606
607 comp = g_array_sized_new (FALSE, FALSE, sizeof (GdkSelTargetFormat), 4);
608 fmt.format = _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_GIF);
609 fmt.transmute = FALSE;
610
611 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_GIF);
612 g_array_append_val (comp, fmt);
613
614 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_IMAGE_GIF);
615 g_array_append_val (comp, fmt);
616
617 g_hash_table_replace (win32_selection->compatibility_targets, GINT_TO_POINTER (_gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_GIF)), comp);
618
619
620 comp = g_array_sized_new (FALSE, FALSE, sizeof (GdkSelTargetFormat), 3);
621 fmt.format = CF_DIB;
622 fmt.transmute = FALSE;
623
624 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_CF_DIB);
625 g_array_append_val (comp, fmt);
626
627 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_IMAGE_BMP);
628 fmt.transmute = TRUE;
629 g_array_append_val (comp, fmt);
630
631 g_hash_table_replace (win32_selection->compatibility_targets, GINT_TO_POINTER (CF_DIB), comp);
632
633
634 comp = g_array_sized_new (FALSE, FALSE, sizeof (GdkSelTargetFormat), 3);
635 fmt.format = _gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_CFSTR_SHELLIDLIST);
636 fmt.transmute = FALSE;
637
638 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_CFSTR_SHELLIDLIST);
639 g_array_append_val (comp, fmt);
640
641 fmt.target = _gdk_atom_array_index (atoms, GDK_WIN32_ATOM_INDEX_TEXT_URI_LIST);
642 fmt.transmute = TRUE;
643 g_array_append_val (comp, fmt);
644
645 g_hash_table_replace (win32_selection->compatibility_targets, GINT_TO_POINTER (_gdk_cf_array_index (cfs, GDK_WIN32_CF_INDEX_CFSTR_SHELLIDLIST)), comp);
646 }
647
648 /* The specifications for COMPOUND_TEXT and STRING specify that C0 and
649 * C1 are not allowed except for \n and \t, however the X conversions
650 * routines for COMPOUND_TEXT only enforce this in one direction,
651 * causing cut-and-paste of \r and \r\n separated text to fail.
652 * This routine strips out all non-allowed C0 and C1 characters
653 * from the input string and also canonicalizes \r, and \r\n to \n
654 */
655 static gchar *
656 sanitize_utf8 (const gchar *src,
657 gint length)
658 {
659 GString *result = g_string_sized_new (length + 1);
660 const gchar *p = src;
661 const gchar *endp = src + length;
662
663 while (p < endp)
664 {
665 if (*p == '\r')
666 {
667 p++;
668 if (*p == '\n')
669 p++;
670
671 g_string_append_c (result, '\n');
672 }
673 else
674 {
675 gunichar ch = g_utf8_get_char (p);
676 char buf[7];
677 gint buflen;
678
679 if (!((ch < 0x20 && ch != '\t' && ch != '\n') || (ch >= 0x7f && ch < 0xa0)))
680 {
681 buflen = g_unichar_to_utf8 (ch, buf);
682 g_string_append_len (result, buf, buflen);
683 }
684
685 p = g_utf8_next_char (p);
686 }
687 }
688 g_string_append_c (result, '\0');
689
690 return g_string_free (result, FALSE);
691 }
692
693 static gchar *
694 _gdk_utf8_to_string_target_internal (const gchar *str,
695 gint length)
696 {
697 GError *error = NULL;
698
699 gchar *tmp_str = sanitize_utf8 (str, length);
700 gchar *result = g_convert_with_fallback (tmp_str, -1,
701 "ISO-8859-1", "UTF-8",
702 NULL, NULL, NULL, &error);
703 if (!result)
704 {
705 g_warning ("Error converting from UTF-8 to STRING: %s",
706 error->message);
707 g_error_free (error);
708 }
709
710 g_free (tmp_str);
711 return result;
712 }
713
714 static void
715 selection_property_store (GdkWindow *owner,
716 GdkAtom type,
717 gint format,
718 guchar *data,
719 gint length)
720 {
721 GdkSelProp *prop;
722 GdkWin32Selection *win32_sel = _gdk_win32_selection_get ();
723
724 prop = g_hash_table_lookup (win32_sel->sel_prop_table, GDK_WINDOW_HWND (owner));
725
726 if (prop != NULL)
727 {
728 g_free (prop->data);
729 g_free (prop);
730 g_hash_table_remove (win32_sel->sel_prop_table, GDK_WINDOW_HWND (owner));
731 }
732
733 prop = g_new (GdkSelProp, 1);
734
735 prop->data = data;
736 prop->length = length;
737 prop->bitness = format;
738 prop->target = type;
739
740 g_hash_table_insert (win32_sel->sel_prop_table, GDK_WINDOW_HWND (owner), prop);
741 }
742
743 void
744 _gdk_dropfiles_store (gchar *data)
745 {
746 GdkWin32Selection *win32_sel = _gdk_win32_selection_get ();
747
748 if (data != NULL)
749 {
750 g_assert (win32_sel->dropfiles_prop == NULL);
751
752 win32_sel->dropfiles_prop = g_new (GdkSelProp, 1);
753 win32_sel->dropfiles_prop->data = (guchar *) data;
754 win32_sel->dropfiles_prop->length = strlen (data) + 1;
755 win32_sel->dropfiles_prop->bitness = 8;
756 win32_sel->dropfiles_prop->target = _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_TEXT_URI_LIST);
757 }
758 else
759 {
760 if (win32_sel->dropfiles_prop != NULL)
761 {
762 g_free (win32_sel->dropfiles_prop->data);
763 g_free (win32_sel->dropfiles_prop);
764 }
765 win32_sel->dropfiles_prop = NULL;
766 }
767 }
768
769 static void
770 generate_selection_notify (GdkWindow *requestor,
771 GdkAtom selection,
772 GdkAtom target,
773 GdkAtom property,
774 guint32 time)
775 {
776 GdkEvent tmp_event;
777
778 memset (&tmp_event, 0, sizeof (tmp_event));
779 tmp_event.selection.type = GDK_SELECTION_NOTIFY;
780 tmp_event.selection.window = requestor;
781 tmp_event.selection.send_event = FALSE;
782 tmp_event.selection.selection = selection;
783 tmp_event.selection.target = target;
784 tmp_event.selection.property = property;
785 tmp_event.selection.requestor = 0;
786 tmp_event.selection.time = time;
787
788 gdk_event_put (&tmp_event);
789 }
790
791 void
792 _gdk_win32_clear_clipboard_queue ()
793 {
794 GList *tmp_list, *next;
795 GdkWin32ClipboardQueueInfo *info;
796 GdkWin32Selection *win32_sel = _gdk_win32_selection_get ();
797
798 GDK_NOTE (DND, g_print ("Clear clipboard queue\n"));
799
800 for (tmp_list = clipboard_queue; tmp_list; tmp_list = next)
801 {
802 info = (GdkWin32ClipboardQueueInfo *) tmp_list->data;
803 next = g_list_next (tmp_list);
804 clipboard_queue = g_list_remove_link (clipboard_queue, tmp_list);
805 g_list_free (tmp_list);
806 switch (info->action)
807 {
808 case GDK_WIN32_CLIPBOARD_QUEUE_ACTION_TARGETS:
809 break;
810 case GDK_WIN32_CLIPBOARD_QUEUE_ACTION_CONVERT:
811 generate_selection_notify (info->requestor, info->selection, info->target, GDK_NONE, info->time);
812 break;
813 }
814 g_clear_object (&info->requestor);
815 g_slice_free (GdkWin32ClipboardQueueInfo, info);
816 }
817
818 win32_sel->targets_request_pending = FALSE;
819 }
820
821 /* Send ourselves a selection request message with
822 * the TARGETS target, we will do multiple SetClipboarData(...,NULL)
823 * calls in response to announce the formats we support.
824 */
825 static void
826 send_targets_request (guint time)
827 {
828 GdkWindow *owner;
829 GdkEvent tmp_event;
830 GdkWin32Selection *win32_sel = _gdk_win32_selection_get ();
831
832 if (win32_sel->targets_request_pending)
833 return;
834
835 owner = _gdk_win32_display_get_selection_owner (gdk_display_get_default (),
836 GDK_SELECTION_CLIPBOARD);
837
838 if (owner == NULL)
839 return;
840
841 if (win32_sel->clipboard_opened_for == INVALID_HANDLE_VALUE)
842 {
843 if (OpenClipboard (GDK_WINDOW_HWND (owner)))
844 {
845 win32_sel->clipboard_opened_for = GDK_WINDOW_HWND (owner);
846 GDK_NOTE (DND, g_print ("Opened clipboard for 0x%p @ %s:%d\n", win32_sel->clipboard_opened_for, __FILE__, __LINE__));
847 }
848 }
849
850 GDK_NOTE (DND, g_print ("... sending GDK_SELECTION_REQUEST to ourselves\n"));
851 memset (&tmp_event, 0, sizeof (tmp_event));
852 tmp_event.selection.type = GDK_SELECTION_REQUEST;
853 tmp_event.selection.window = owner;
854 tmp_event.selection.send_event = FALSE;
855 tmp_event.selection.selection = GDK_SELECTION_CLIPBOARD;
856 tmp_event.selection.target = _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_TARGETS);
857 tmp_event.selection.property = _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_GDK_SELECTION);
858 tmp_event.selection.requestor = owner;
859 tmp_event.selection.time = time;
860 win32_sel->property_change_target_atom = _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_TARGETS);
861
862 gdk_event_put (&tmp_event);
863 win32_sel->targets_request_pending = TRUE;
864 }
865
866 #define CLIPBOARD_IDLE_ABORT_TIME 30
867
868 static const gchar *
869 predefined_name (UINT fmt)
870 {
871 #define CASE(fmt) case fmt: return #fmt
872 switch (fmt)
873 {
874 CASE (CF_TEXT);
875 CASE (CF_BITMAP);
876 CASE (CF_METAFILEPICT);
877 CASE (CF_SYLK);
878 CASE (CF_DIF);
879 CASE (CF_TIFF);
880 CASE (CF_OEMTEXT);
881 CASE (CF_DIB);
882 CASE (CF_PALETTE);
883 CASE (CF_PENDATA);
884 CASE (CF_RIFF);
885 CASE (CF_WAVE);
886 CASE (CF_UNICODETEXT);
887 CASE (CF_ENHMETAFILE);
888 CASE (CF_HDROP);
889 CASE (CF_LOCALE);
890 CASE (CF_DIBV5);
891 CASE (CF_MAX);
892
893 CASE (CF_OWNERDISPLAY);
894 CASE (CF_DSPTEXT);
895 CASE (CF_DSPBITMAP);
896 CASE (CF_DSPMETAFILEPICT);
897 CASE (CF_DSPENHMETAFILE);
898 #undef CASE
899 default:
900 return NULL;
901 }
902 }
903
904 gchar *
905 _gdk_win32_get_clipboard_format_name (UINT fmt,
906 gboolean *is_predefined)
907 {
908 gint registered_name_w_len = 1024;
909 wchar_t *registered_name_w = g_malloc (registered_name_w_len);
910 gchar *registered_name = NULL;
911 int gcfn_result;
912 const gchar *predef = predefined_name (fmt);
913
914 /* FIXME: cache the result in a hash table */
915
916 do
917 {
918 gcfn_result = GetClipboardFormatNameW (fmt, registered_name_w, registered_name_w_len);
919
920 if (gcfn_result > 0 && gcfn_result < registered_name_w_len)
921 {
922 registered_name = g_utf16_to_utf8 (registered_name_w, -1, NULL, NULL, NULL);
923 g_clear_pointer (&registered_name_w, g_free);
924 if (!registered_name)
925 gcfn_result = 0;
926 else
927 *is_predefined = FALSE;
928 break;
929 }
930
931 /* If GetClipboardFormatNameW() used up all the space, it means that
932 * we probably need a bigger buffer, but cap this at 1 kilobyte.
933 */
934 if (gcfn_result == 0 || registered_name_w_len > 1024 * 1024)
935 {
936 gcfn_result = 0;
937 g_clear_pointer (&registered_name_w, g_free);
938 break;
939 }
940
941 registered_name_w_len *= 2;
942 registered_name_w = g_realloc (registered_name_w, registered_name_w_len);
943 gcfn_result = registered_name_w_len;
944 } while (gcfn_result == registered_name_w_len);
945
946 if (registered_name == NULL &&
947 predef != NULL)
948 {
949 registered_name = g_strdup (predef);
950 *is_predefined = TRUE;
951 }
952
953 return registered_name;
954 }
955
956 static GArray *
957 get_compatibility_formats_for_target (GdkAtom target)
958 {
959 GArray *result = NULL;
960 gint i;
961 GdkWin32Selection *win32_sel = _gdk_win32_selection_get ();
962
963 result = g_hash_table_lookup (win32_sel->compatibility_formats, target);
964
965 if (result != NULL)
966 return result;
967
968 for (i = 0; i < win32_sel->n_known_pixbuf_formats; i++)
969 {
970 if (target != win32_sel->known_pixbuf_formats[i])
971 continue;
972
973 /* Any format known to gdk-pixbuf can be presented as PNG or BMP */
974 result = g_hash_table_lookup (win32_sel->compatibility_formats,
975 _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_IMAGE_PNG));
976 break;
977 }
978
979 return result;
980 }
981
982 static GArray *
983 _gdk_win32_selection_get_compatibility_targets_for_format (UINT format)
984 {
985 GArray *result = NULL;
986 GdkWin32Selection *win32_sel = _gdk_win32_selection_get ();
987
988 result = g_hash_table_lookup (win32_sel->compatibility_targets, GINT_TO_POINTER (format));
989
990 if (result != NULL)
991 return result;
992
993 /* TODO: reverse gdk-pixbuf conversion? We have to somehow
994 * match gdk-pixbuf format names to the corresponding clipboard
995 * format names. The former are known only at runtime,
996 * the latter are presently unknown...
997 * Maybe try to get the data and then just feed it to gdk-pixbuf,
998 * see if it knows what it is?
999 */
1000
1001 return result;
1002 }
1003
1004 void
1005 _gdk_win32_add_format_to_targets (UINT format,
1006 GArray *array,
1007 GList **list)
1008 {
1009 gboolean predef;
1010 gchar *format_name = _gdk_win32_get_clipboard_format_name (format, &predef);
1011 GdkAtom target_atom;
1012 GdkSelTargetFormat target_selformat;
1013 GArray *target_selformats;
1014 gint i,j;
1015
1016 if (format_name != NULL)
1017 {
1018 target_atom = gdk_atom_intern (format_name, FALSE);
1019 GDK_NOTE (DND, g_print ("Maybe add as-is format %s (0x%p)\n", format_name, target_atom));
1020 g_free (format_name);
1021 if (array && target_atom != 0)
1022 {
1023 for (j = 0; j < array->len; j++)
1024 if (g_array_index (array, GdkSelTargetFormat, j).target == target_atom)
1025 break;
1026 if (j == array->len)
1027 {
1028 target_selformat.format = format;
1029 target_selformat.target = target_atom;
1030 target_selformat.transmute = FALSE;
1031 g_array_append_val (array, target_selformat);
1032 }
1033 }
1034 if (list && target_atom != 0 && g_list_find (*list, target_atom) == NULL)
1035 *list = g_list_prepend (*list, target_atom);
1036 }
1037
1038 target_selformats = _gdk_win32_selection_get_compatibility_targets_for_format (format);
1039
1040 if (array && target_selformats != NULL)
1041 for (i = 0; i < target_selformats->len; i++)
1042 {
1043 target_selformat = g_array_index (target_selformats, GdkSelTargetFormat, i);
1044
1045 for (j = 0; j < array->len; j++)
1046 if (g_array_index (array, GdkSelTargetFormat, j).target == target_selformat.target &&
1047 g_array_index (array, GdkSelTargetFormat, j).format == target_selformat.format)
1048 break;
1049
1050 if (j == array->len)
1051 g_array_append_val (array, target_selformat);
1052 }
1053
1054 if (list && target_selformats != NULL)
1055 for (i = 0; i < target_selformats->len; i++)
1056 {
1057 target_selformat = g_array_index (target_selformats, GdkSelTargetFormat, i);
1058
1059 if (g_list_find (*list, target_selformat.target) == NULL)
1060 *list = g_list_prepend (*list, target_selformat.target);
1061 }
1062 }
1063
1064 static void
1065 transmute_cf_unicodetext_to_utf8_string (const guchar *data,
1066 gint length,
1067 guchar **set_data,
1068 gint *set_data_length,
1069 GDestroyNotify *set_data_destroy)
1070 {
1071 wchar_t *ptr, *p, *q, *endp;
1072 gchar *result;
1073 glong wclen, u8_len;
1074
1075 /* Replace CR and CR+LF with LF */
1076 ptr = (wchar_t *) data;
1077 p = ptr;
1078 q = ptr;
1079 endp = ptr + length / 2;
1080 wclen = 0;
1081
1082 while (p < endp)
1083 {
1084 if (*p != L'\r')
1085 {
1086 *q++ = *p;
1087 wclen++;
1088 }
1089 else if (p + 1 >= endp || *(p + 1) != L'\n')
1090 {
1091 *q++ = L'\n';
1092 wclen++;
1093 }
1094
1095 p++;
1096 }
1097
1098 result = g_utf16_to_utf8 (ptr, wclen, NULL, &u8_len, NULL);
1099
1100 if (result)
1101 {
1102 *set_data = (guchar *) result;
1103
1104 if (set_data_length)
1105 *set_data_length = u8_len + 1;
1106 if (set_data_destroy)
1107 *set_data_destroy = (GDestroyNotify) g_free;
1108 }
1109 }
1110
1111 static void
1112 transmute_utf8_string_to_cf_unicodetext (const guchar *data,
1113 gint length,
1114 guchar **set_data,
1115 gint *set_data_length,
1116 GDestroyNotify *set_data_destroy)
1117 {
1118 glong wclen;
1119 GError *err = NULL;
1120 glong size;
1121 gint i;
1122 wchar_t *wcptr, *p;
1123
1124 wcptr = g_utf8_to_utf16 ((char *) data, length, NULL, &wclen, &err);
1125 if (err != NULL)
1126 {
1127 g_warning ("Failed to convert utf8: %s", err->message);
1128 g_clear_error (&err);
1129 return;
1130 }
1131
1132 wclen++; /* Terminating 0 */
1133 size = wclen * 2;
1134 for (i = 0; i < wclen; i++)
1135 if (wcptr[i] == L'\n' && (i == 0 || wcptr[i - 1] != L'\r'))
1136 size += 2;
1137
1138 *set_data = g_malloc0 (size);
1139 if (set_data_length)
1140 *set_data_length = size;
1141 if (set_data_destroy)
1142 *set_data_destroy = (GDestroyNotify) g_free;
1143
1144 p = (wchar_t *) *set_data;
1145
1146 for (i = 0; i < wclen; i++)
1147 {
1148 if (wcptr[i] == L'\n' && (i == 0 || wcptr[i - 1] != L'\r'))
1149 *p++ = L'\r';
1150 *p++ = wcptr[i];
1151 }
1152
1153 g_free (wcptr);
1154 }
1155
1156 static int
1157 wchar_to_str (const wchar_t *wstr,
1158 char **retstr,
1159 UINT cp)
1160 {
1161 char *str;
1162 int len;
1163 int lenc;
1164
1165 len = WideCharToMultiByte (cp, 0, wstr, -1, NULL, 0, NULL, NULL);
1166
1167 if (len <= 0)
1168 return -1;
1169
1170 str = g_malloc (sizeof (char) * len);
1171
1172 lenc = WideCharToMultiByte (cp, 0, wstr, -1, str, len, NULL, NULL);
1173
1174 if (lenc != len)
1175 {
1176 g_free (str);
1177
1178 return -3;
1179 }
1180
1181 *retstr = str;
1182
1183 return 0;
1184 }
1185
1186 static void
1187 transmute_utf8_string_to_cf_text (const guchar *data,
1188 gint length,
1189 guchar **set_data,
1190 gint *set_data_length,
1191 GDestroyNotify *set_data_destroy)
1192 {
1193 glong rlen;
1194 GError *err = NULL;
1195 glong size;
1196 gint i;
1197 char *strptr, *p;
1198 wchar_t *wcptr;
1199
1200 wcptr = g_utf8_to_utf16 ((char *) data, length, NULL, NULL, &err);
1201 if (err != NULL)
1202 {
1203 g_warning ("Failed to convert utf8: %s", err->message);
1204 g_clear_error (&err);
1205 return;
1206 }
1207
1208 if (wchar_to_str (wcptr, &strptr, CP_ACP) != 0)
1209 {
1210 g_warning ("Failed to convert utf-16 %S to ACP", wcptr);
1211 g_free (wcptr);
1212 return;
1213 }
1214
1215 rlen = strlen (strptr);
1216 g_free (wcptr);
1217
1218 rlen++; /* Terminating 0 */
1219 size = rlen * sizeof (char);
1220 for (i = 0; i < rlen; i++)
1221 if (strptr[i] == '\n' && (i == 0 || strptr[i - 1] != '\r'))
1222 size += sizeof (char);
1223
1224 *set_data = g_malloc0 (size);
1225 if (set_data_length)
1226 *set_data_length = size;
1227 if (set_data_destroy)
1228 *set_data_destroy = (GDestroyNotify) g_free;
1229
1230 p = (char *) *set_data;
1231
1232 for (i = 0; i < rlen; i++)
1233 {
1234 if (strptr[i] == '\n' && (i == 0 || strptr[i - 1] != '\r'))
1235 *p++ = '\r';
1236 *p++ = strptr[i];
1237 }
1238
1239 g_free (strptr);
1240 }
1241
1242 static int
1243 str_to_wchar (const char *str,
1244 wchar_t **wretstr,
1245 UINT cp)
1246 {
1247 wchar_t *wstr;
1248 int len;
1249 int lenc;
1250
1251 len = MultiByteToWideChar (cp, 0, str, -1, NULL, 0);
1252
1253 if (len <= 0)
1254 return -1;
1255
1256 wstr = g_malloc (sizeof (wchar_t) * len);
1257
1258 lenc = MultiByteToWideChar (cp, 0, str, -1, wstr, len);
1259
1260 if (lenc != len)
1261 {
1262 g_free (wstr);
1263
1264 return -3;
1265 }
1266
1267 *wretstr = wstr;
1268
1269 return 0;
1270 }
1271
1272 static void
1273 transmute_cf_text_to_utf8_string (const guchar *data,
1274 gint length,
1275 guchar **set_data,
1276 gint *set_data_length,
1277 GDestroyNotify *set_data_destroy)
1278 {
1279 char *ptr, *p, *q, *endp;
1280 gchar *result;
1281 glong wclen, u8_len;
1282 wchar_t *wstr;
1283
1284 /* Replace CR and CR+LF with LF */
1285 ptr = (gchar *) data;
1286 p = ptr;
1287 q = ptr;
1288 endp = ptr + length / 2;
1289 wclen = 0;
1290
1291 while (p < endp)
1292 {
1293 if (*p != '\r')
1294 {
1295 *q++ = *p;
1296 wclen++;
1297 }
1298 else if (p + 1 > endp || *(p + 1) != '\n')
1299 {
1300 *q++ = '\n';
1301 wclen++;
1302 }
1303
1304 p++;
1305 }
1306
1307 if (str_to_wchar (ptr, &wstr, CP_ACP) < 0)
1308 return;
1309
1310 result = g_utf16_to_utf8 (wstr, -1, NULL, &u8_len, NULL);
1311
1312 if (result)
1313 {
1314 *set_data = (guchar *) result;
1315
1316 if (set_data_length)
1317 *set_data_length = u8_len + 1;
1318 if (set_data_destroy)
1319 *set_data_destroy = (GDestroyNotify) g_free;
1320 }
1321
1322 g_free (wstr);
1323 }
1324
1325 static void
1326 transmute_cf_dib_to_image_bmp (const guchar *data,
1327 gint length,
1328 guchar **set_data,
1329 gint *set_data_length,
1330 GDestroyNotify *set_data_destroy)
1331 {
1332 /* Need to add a BMP file header so gdk-pixbuf can load
1333 * it.
1334 *
1335 * If the data is from Mozilla Firefox or IE7, and
1336 * starts with an "old fashioned" BITMAPINFOHEADER,
1337 * i.e. with biSize==40, and biCompression == BI_RGB and
1338 * biBitCount==32, we assume that the "extra" byte in
1339 * each pixel in fact is alpha.
1340 *
1341 * The gdk-pixbuf bmp loader doesn't trust 32-bit BI_RGB
1342 * bitmaps to in fact have alpha, so we have to convince
1343 * it by changing the bitmap header to a version 5
1344 * BI_BITFIELDS one with explicit alpha mask indicated.
1345 *
1346 * The RGB bytes that are in bitmaps on the clipboard
1347 * originating from Firefox or IE7 seem to be
1348 * premultiplied with alpha. The gdk-pixbuf bmp loader
1349 * of course doesn't expect that, so we have to undo the
1350 * premultiplication before feeding the bitmap to the
1351 * bmp loader.
1352 *
1353 * Note that for some reason the bmp loader used to want
1354 * the alpha bytes in its input to actually be
1355 * 255-alpha, but here we assume that this has been
1356 * fixed before this is committed.
1357 */
1358 BITMAPINFOHEADER *bi = (BITMAPINFOHEADER *) data;
1359 BITMAPFILEHEADER *bf;
1360 gpointer result;
1361 gint data_length = length;
1362 gint new_length;
1363 gboolean make_dibv5 = FALSE;
1364 BITMAPV5HEADER *bV5;
1365 guchar *p;
1366 guint i;
1367
1368 if (bi->biSize == sizeof (BITMAPINFOHEADER) &&
1369 bi->biPlanes == 1 &&
1370 bi->biBitCount == 32 &&
1371 bi->biCompression == BI_RGB &&
1372 #if 0
1373 /* Maybe check explicitly for Mozilla or IE7?
1374 *
1375 * If the clipboard format
1376 * application/x-moz-nativeimage is present, that is
1377 * a reliable indicator that the data is offered by
1378 * Mozilla one would think. For IE7,
1379 * UniformResourceLocatorW is presumably not that
1380 * uniqie, so probably need to do some
1381 * GetClipboardOwner(), GetWindowThreadProcessId(),
1382 * OpenProcess(), GetModuleFileNameEx() dance to
1383 * check?
1384 */
1385 (IsClipboardFormatAvailable
1386 (RegisterClipboardFormatA ("application/x-moz-nativeimage")) ||
1387 IsClipboardFormatAvailable
1388 (RegisterClipboardFormatA ("UniformResourceLocatorW"))) &&
1389 #endif
1390 TRUE)
1391 {
1392 /* We turn the BITMAPINFOHEADER into a
1393 * BITMAPV5HEADER before feeding it to gdk-pixbuf.
1394 */
1395 new_length = (data_length +
1396 sizeof (BITMAPFILEHEADER) +
1397 (sizeof (BITMAPV5HEADER) - sizeof (BITMAPINFOHEADER)));
1398 make_dibv5 = TRUE;
1399 }
1400 else
1401 {
1402 new_length = data_length + sizeof (BITMAPFILEHEADER);
1403 }
1404
1405 result = g_try_malloc (new_length);
1406
1407 if (result == NULL)
1408 return;
1409
1410 bf = (BITMAPFILEHEADER *) result;
1411 bf->bfType = 0x4d42; /* "BM" */
1412 bf->bfSize = new_length;
1413 bf->bfReserved1 = 0;
1414 bf->bfReserved2 = 0;
1415
1416 *set_data = result;
1417
1418 if (set_data_length)
1419 *set_data_length = new_length;
1420 if (set_data_destroy)
1421 *set_data_destroy = g_free;
1422
1423 if (!make_dibv5)
1424 {
1425 bf->bfOffBits = (sizeof (BITMAPFILEHEADER) +
1426 bi->biSize +
1427 bi->biClrUsed * sizeof (RGBQUAD));
1428
1429 if (bi->biCompression == BI_BITFIELDS && bi->biBitCount >= 16)
1430 {
1431 /* Screenshots taken with PrintScreen or
1432 * Alt + PrintScreen are found on the clipboard in
1433 * this format. In this case the BITMAPINFOHEADER is
1434 * followed by three DWORD specifying the masks of the
1435 * red green and blue components, so adjust the offset
1436 * accordingly. */
1437 bf->bfOffBits += (3 * sizeof (DWORD));
1438 }
1439
1440 memcpy ((char *) result + sizeof (BITMAPFILEHEADER),
1441 bi,
1442 data_length);
1443
1444 return;
1445 }
1446
1447 bV5 = (BITMAPV5HEADER *) ((char *) result + sizeof (BITMAPFILEHEADER));
1448
1449 bV5->bV5Size = sizeof (BITMAPV5HEADER);
1450 bV5->bV5Width = bi->biWidth;
1451 bV5->bV5Height = bi->biHeight;
1452 bV5->bV5Planes = 1;
1453 bV5->bV5BitCount = 32;
1454 bV5->bV5Compression = BI_BITFIELDS;
1455 bV5->bV5SizeImage = 4 * bV5->bV5Width * ABS (bV5->bV5Height);
1456 bV5->bV5XPelsPerMeter = bi->biXPelsPerMeter;
1457 bV5->bV5YPelsPerMeter = bi->biYPelsPerMeter;
1458 bV5->bV5ClrUsed = 0;
1459 bV5->bV5ClrImportant = 0;
1460 /* Now the added mask fields */
1461 bV5->bV5RedMask = 0x00ff0000;
1462 bV5->bV5GreenMask = 0x0000ff00;
1463 bV5->bV5BlueMask = 0x000000ff;
1464 bV5->bV5AlphaMask = 0xff000000;
1465 ((char *) &bV5->bV5CSType)[3] = 's';
1466 ((char *) &bV5->bV5CSType)[2] = 'R';
1467 ((char *) &bV5->bV5CSType)[1] = 'G';
1468 ((char *) &bV5->bV5CSType)[0] = 'B';
1469 /* Ignore colorspace and profile fields */
1470 bV5->bV5Intent = LCS_GM_GRAPHICS;
1471 bV5->bV5Reserved = 0;
1472
1473 bf->bfOffBits = (sizeof (BITMAPFILEHEADER) +
1474 bV5->bV5Size);
1475
1476 p = ((guchar *) result) + sizeof (BITMAPFILEHEADER) + sizeof (BITMAPV5HEADER);
1477 memcpy (p, ((char *) bi) + bi->biSize,
1478 data_length - sizeof (BITMAPINFOHEADER));
1479
1480 for (i = 0; i < bV5->bV5SizeImage/4; i++)
1481 {
1482 if (p[3] != 0)
1483 {
1484 gdouble inverse_alpha = 255./p[3];
1485
1486 p[0] = p[0] * inverse_alpha + 0.5;
1487 p[1] = p[1] * inverse_alpha + 0.5;
1488 p[2] = p[2] * inverse_alpha + 0.5;
1489 }
1490
1491 p += 4;
1492 }
1493 }
1494
1495 static void
1496 transmute_cf_shell_id_list_to_text_uri_list (const guchar *data,
1497 gint length,
1498 guchar **set_data,
1499 gint *set_data_length,
1500 GDestroyNotify *set_data_destroy)
1501 {
1502 guint i;
1503 CIDA *cida = (CIDA *) data;
1504 guint number_of_ids = cida->cidl;
1505 GString *result = g_string_new (NULL);
1506 PCIDLIST_ABSOLUTE folder_id = HIDA_GetPIDLFolder (cida);
1507 wchar_t path_w[MAX_PATH + 1];
1508
1509 for (i = 0; i < number_of_ids; i++)
1510 {
1511 PCUIDLIST_RELATIVE file_id = HIDA_GetPIDLItem (cida, i);
1512 PIDLIST_ABSOLUTE file_id_full = ILCombine (folder_id, file_id);
1513
1514 if (SHGetPathFromIDListW (file_id_full, path_w))
1515 {
1516 gchar *filename = g_utf16_to_utf8 (path_w, -1, NULL, NULL, NULL);
1517
1518 if (filename)
1519 {
1520 gchar *uri = g_filename_to_uri (filename, NULL, NULL);
1521
1522 if (uri)
1523 {
1524 g_string_append (result, uri);
1525 g_string_append (result, "\r\n");
1526 g_free (uri);
1527 }
1528
1529 g_free (filename);
1530 }
1531 }
1532
1533 ILFree (file_id_full);
1534 }
1535
1536 *set_data = (guchar *) result->str;
1537 if (set_data_length)
1538 *set_data_length = result->len;
1539 if (set_data_destroy)
1540 *set_data_destroy = g_free;
1541
1542 g_string_free (result, FALSE);
1543 }
1544
1545 void
1546 transmute_image_bmp_to_cf_dib (const guchar *data,
1547 gint length,
1548 guchar **set_data,
1549 gint *set_data_length,
1550 GDestroyNotify *set_data_destroy)
1551 {
1552 gint size;
1553 guchar *ptr;
1554
1555 g_return_if_fail (length >= sizeof (BITMAPFILEHEADER));
1556
1557 /* No conversion is needed, just strip the BITMAPFILEHEADER */
1558 size = length - sizeof (BITMAPFILEHEADER);
1559 ptr = g_malloc (size);
1560
1561 memcpy (ptr, data + sizeof (BITMAPFILEHEADER), size);
1562
1563 *set_data = ptr;
1564
1565 if (set_data_length)
1566 *set_data_length = size;
1567 if (set_data_destroy)
1568 *set_data_destroy = g_free;
1569 }
1570
1571 static void
1572 transmute_selection_format (UINT from_format,
1573 GdkAtom to_target,
1574 const guchar *data,
1575 gint length,
1576 guchar **set_data,
1577 gint *set_data_length)
1578 {
1579 if ((to_target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_IMAGE_PNG) &&
1580 from_format == _gdk_win32_selection_cf (GDK_WIN32_CF_INDEX_PNG)) ||
1581 (to_target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_IMAGE_JPEG) &&
1582 from_format == _gdk_win32_selection_cf (GDK_WIN32_CF_INDEX_JFIF)) ||
1583 (to_target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_GIF) &&
1584 from_format == _gdk_win32_selection_cf (GDK_WIN32_CF_INDEX_GIF)))
1585 {
1586 /* No transmutation needed */
1587 *set_data = g_memdup (data, length);
1588 *set_data_length = length;
1589 }
1590 else if (to_target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_UTF8_STRING) &&
1591 from_format == CF_UNICODETEXT)
1592 {
1593 transmute_cf_unicodetext_to_utf8_string (data, length, set_data, set_data_length, NULL);
1594 }
1595 else if (to_target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_UTF8_STRING) &&
1596 from_format == CF_TEXT)
1597 {
1598 transmute_cf_text_to_utf8_string (data, length, set_data, set_data_length, NULL);
1599 }
1600 else if (to_target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_IMAGE_BMP) &&
1601 (from_format == CF_DIB || from_format == CF_DIBV5))
1602 {
1603 transmute_cf_dib_to_image_bmp (data, length, set_data, set_data_length, NULL);
1604 }
1605 else if (to_target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_TEXT_URI_LIST) &&
1606 from_format == _gdk_win32_selection_cf (GDK_WIN32_CF_INDEX_CFSTR_SHELLIDLIST))
1607 {
1608 transmute_cf_shell_id_list_to_text_uri_list (data, length, set_data, set_data_length, NULL);
1609 }
1610 else
1611 {
1612 g_warning ("Don't know how to transmute format 0x%x to target 0x%p", from_format, to_target);
1613 }
1614 }
1615
1616 void
1617 transmute_selection_target (GdkAtom from_target,
1618 UINT to_format,
1619 const guchar *data,
1620 gint length,
1621 guchar **set_data,
1622 gint *set_data_length)
1623 {
1624 if ((from_target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_IMAGE_PNG) &&
1625 to_format == _gdk_win32_selection_cf (GDK_WIN32_CF_INDEX_PNG)) ||
1626 (from_target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_IMAGE_JPEG) &&
1627 to_format == _gdk_win32_selection_cf (GDK_WIN32_CF_INDEX_JFIF)) ||
1628 (from_target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_GIF) &&
1629 to_format == _gdk_win32_selection_cf (GDK_WIN32_CF_INDEX_GIF)))
1630 {
1631 /* No conversion needed */
1632 *set_data = g_memdup (data, length);
1633 *set_data_length = length;
1634 }
1635 else if (from_target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_UTF8_STRING) &&
1636 to_format == CF_UNICODETEXT)
1637 {
1638 transmute_utf8_string_to_cf_unicodetext (data, length, set_data, set_data_length, NULL);
1639 }
1640 else if (from_target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_UTF8_STRING) &&
1641 to_format == CF_TEXT)
1642 {
1643 transmute_utf8_string_to_cf_text (data, length, set_data, set_data_length, NULL);
1644 }
1645 else if (from_target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_IMAGE_BMP) &&
1646 to_format == CF_DIB)
1647 {
1648 transmute_image_bmp_to_cf_dib (data, length, set_data, set_data_length, NULL);
1649 }
1650 else if (from_target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_IMAGE_BMP) &&
1651 to_format == CF_DIBV5)
1652 {
1653 transmute_image_bmp_to_cf_dib (data, length, set_data, set_data_length, NULL);
1654 }
1655 /*
1656 else if (from_target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_TEXT_URI_LIST) &&
1657 to_format == _gdk_win32_selection_cf (GDK_WIN32_CF_INDEX_CFSTR_SHELLIDLIST))
1658 {
1659 transmute_text_uri_list_to_shell_id_list (data, length, set_data, set_data_length, NULL);
1660 }
1661 */
1662 else
1663 {
1664 g_warning ("Don't know how to transmute from target 0x%p to format 0x%x", from_target, to_format);
1665 }
1666 }
1667
1668 static GdkAtom
1669 convert_clipboard_selection_to_targets_target (GdkWindow *requestor)
1670 {
1671 gint fmt;
1672 int i;
1673 int format_count = CountClipboardFormats ();
1674 GArray *targets = g_array_sized_new (FALSE, FALSE, sizeof (GdkSelTargetFormat), format_count);
1675
1676 for (fmt = 0; 0 != (fmt = EnumClipboardFormats (fmt)); )
1677 _gdk_win32_add_format_to_targets (fmt, targets, NULL);
1678
1679 GDK_NOTE (DND, {
1680 g_print ("... ");
1681 for (i = 0; i < targets->len; i++)
1682 {
1683 gchar *atom_name = gdk_atom_name (g_array_index (targets, GdkSelTargetFormat, i).target);
1684
1685 g_print ("%s", atom_name);
1686 g_free (atom_name);
1687 if (i < targets->len - 1)
1688 g_print (", ");
1689 }
1690 g_print ("\n");
1691 });
1692
1693 if (targets->len > 0)
1694 {
1695 gint len = targets->len;
1696 GdkAtom *targets_only = g_new0 (GdkAtom, len);
1697
1698 for (i = 0; i < targets->len; i++)
1699 targets_only[i] = g_array_index (targets, GdkSelTargetFormat, i).target;
1700
1701 g_array_free (targets, TRUE);
1702 selection_property_store (requestor, GDK_SELECTION_TYPE_ATOM,
1703 32, (guchar *) targets_only,
1704 len * sizeof (GdkAtom));
1705 return _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_GDK_SELECTION);
1706 }
1707 else
1708 {
1709 g_array_free (targets, TRUE);
1710 return GDK_NONE;
1711 }
1712 }
1713
1714 static GdkAtom
1715 convert_clipboard_selection_to_target (GdkWindow *requestor,
1716 GdkAtom target)
1717 {
1718 UINT format;
1719 HANDLE hdata;
1720 guchar *ptr;
1721 gint length;
1722 gboolean transmute = FALSE;
1723 GdkAtom result = _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_GDK_SELECTION);
1724 gboolean found;
1725 gchar *atom_name;
1726
1727 atom_name = gdk_atom_name (target);
1728
1729 for (format = 0, found = FALSE;
1730 !found && 0 != (format = EnumClipboardFormats (format));
1731 )
1732 {
1733 gboolean predef;
1734 gchar *format_name = _gdk_win32_get_clipboard_format_name (format, &predef);
1735
1736 if (format_name == NULL)
1737 continue;
1738
1739 found = g_strcmp0 (format_name, atom_name) == 0;
1740 g_free (format_name);
1741 }
1742
1743 g_free (atom_name);
1744
1745 if (format == 0)
1746 {
1747 gint i;
1748 GArray *compat_formats = get_compatibility_formats_for_target (target);
1749
1750 for (i = 0; compat_formats != NULL && i < compat_formats->len; i++)
1751 {
1752 if (!IsClipboardFormatAvailable (g_array_index (compat_formats, GdkSelTargetFormat, i).format))
1753 continue;
1754
1755 format = g_array_index (compat_formats, GdkSelTargetFormat, i).format;
1756 transmute = g_array_index (compat_formats, GdkSelTargetFormat, i).transmute;
1757 break;
1758 }
1759 }
1760
1761 if (format == 0)
1762 return GDK_NONE;
1763
1764 if ((hdata = GetClipboardData (format)) == NULL)
1765 return GDK_NONE;
1766
1767 if ((ptr = GlobalLock (hdata)) != NULL)
1768 {
1769 guchar *data = NULL;
1770 gint data_len = 0;
1771 length = GlobalSize (hdata);
1772
1773 GDK_NOTE (DND, g_print ("... format 0x%x: %d bytes\n", format, length));
1774
1775 if (transmute)
1776 {
1777 transmute_selection_format (format, target, ptr, length, &data, &data_len);
1778 }
1779 else
1780 {
1781 data = g_memdup (ptr, length);
1782 data_len = length;
1783 }
1784
1785 if (data)
1786 selection_property_store (requestor, target,
1787 8, data, data_len);
1788 else
1789 result = GDK_NONE;
1790
1791 GlobalUnlock (hdata);
1792 }
1793
1794 return result;
1795 }
1796
1797 static GdkAtom
1798 convert_selection_with_opened_clipboard (GdkDisplay *display,
1799 GdkWindow *requestor,
1800 GdkAtom target,
1801 guint32 time)
1802 {
1803 GdkAtom property;
1804
1805 if (target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_TARGETS))
1806 property = convert_clipboard_selection_to_targets_target (requestor);
1807 else
1808 property = convert_clipboard_selection_to_target (requestor, target);
1809
1810 return property;
1811 }
1812
1813 static void
1814 announce_delayrendered_targets_with_opened_clipboard (GdkWin32Selection *win32_sel)
1815 {
1816 gint i;
1817 /* Announce the formats we support, but don't actually put any data out there.
1818 * Other processes will send us WM_RENDERFORMAT to get the data.
1819 */
1820 for (i = 0; i < win32_sel->clipboard_selection_targets->len; i++)
1821 {
1822 GdkSelTargetFormat *fmt = &g_array_index (win32_sel->clipboard_selection_targets, GdkSelTargetFormat, i);
1823
1824 /* Some calls here may be duplicates, but we don't really care */
1825 if (fmt->format != 0)
1826 SetClipboardData (fmt->format, NULL);
1827 }
1828 }
1829
1830 static gboolean
1831 open_clipboard_timeout (gpointer data)
1832 {
1833 GList *tmp_list, *next;
1834 GdkWin32ClipboardQueueInfo *info;
1835 GdkWin32Selection *win32_sel = _gdk_win32_selection_get ();
1836
1837 GDK_NOTE (DND, g_print ("Open clipboard timeout ticks\n"));
1838
1839 /* Clear out old and invalid entries */
1840 for (tmp_list = clipboard_queue; tmp_list; tmp_list = next)
1841 {
1842 info = (GdkWin32ClipboardQueueInfo *) tmp_list->data;
1843 next = g_list_next (tmp_list);
1844
1845 if (GDK_WINDOW_DESTROYED (info->requestor) ||
1846 info->idle_time >= CLIPBOARD_IDLE_ABORT_TIME)
1847 {
1848 clipboard_queue = g_list_remove_link (clipboard_queue, tmp_list);
1849 g_list_free (tmp_list);
1850 switch (info->action)
1851 {
1852 case GDK_WIN32_CLIPBOARD_QUEUE_ACTION_TARGETS:
1853 break;
1854 case GDK_WIN32_CLIPBOARD_QUEUE_ACTION_CONVERT:
1855 generate_selection_notify (info->requestor, info->selection, info->target, GDK_NONE, info->time);
1856 break;
1857 }
1858 g_clear_object (&info->requestor);
1859 g_slice_free (GdkWin32ClipboardQueueInfo, info);
1860 }
1861 }
1862
1863 if (clipboard_queue == NULL)
1864 {
1865 GDK_NOTE (DND, g_print ("Stopping open clipboard timer\n"));
1866
1867 if (win32_sel->clipboard_opened_for != INVALID_HANDLE_VALUE)
1868 {
1869 API_CALL (CloseClipboard, ());
1870 win32_sel->clipboard_opened_for = INVALID_HANDLE_VALUE;
1871 GDK_NOTE (DND, g_print ("Closed clipboard @ %s:%d\n", __FILE__, __LINE__));
1872 }
1873
1874 return FALSE;
1875 }
1876
1877 for (tmp_list = clipboard_queue; tmp_list; tmp_list = next)
1878 {
1879 GdkAtom property;
1880
1881 info = (GdkWin32ClipboardQueueInfo *) tmp_list->data;
1882 next = g_list_next (tmp_list);
1883
1884 /* CONVERT works with any opened clipboard,
1885 * but TARGETS needs to open the clipboard with the hande of the
1886 * owner window.
1887 */
1888 if (info->action == GDK_WIN32_CLIPBOARD_QUEUE_ACTION_TARGETS &&
1889 win32_sel->clipboard_opened_for == NULL)
1890 {
1891 GDK_NOTE (DND, g_print ("Need to re-open clipboard, closing\n"));
1892 API_CALL (CloseClipboard, ());
1893 win32_sel->clipboard_opened_for = INVALID_HANDLE_VALUE;
1894 }
1895
1896 if (win32_sel->clipboard_opened_for == INVALID_HANDLE_VALUE)
1897 {
1898 if (!OpenClipboard (GDK_WINDOW_HWND (info->requestor)))
1899 {
1900 info->idle_time += 1;
1901 continue;
1902 }
1903 win32_sel->clipboard_opened_for = GDK_WINDOW_HWND (info->requestor);
1904 GDK_NOTE (DND, g_print ("Opened clipboard for 0x%p @ %s:%d\n", win32_sel->clipboard_opened_for, __FILE__, __LINE__));
1905 }
1906
1907 clipboard_queue = g_list_remove_link (clipboard_queue, tmp_list);
1908 g_list_free (tmp_list);
1909
1910 switch (info->action)
1911 {
1912 case GDK_WIN32_CLIPBOARD_QUEUE_ACTION_CONVERT:
1913 property = convert_selection_with_opened_clipboard (info->display,
1914 info->requestor,
1915 info->target,
1916 info->time);
1917 generate_selection_notify (info->requestor,
1918 GDK_SELECTION_CLIPBOARD,
1919 info->target,
1920 property,
1921 info->time);
1922 break;
1923 case GDK_WIN32_CLIPBOARD_QUEUE_ACTION_TARGETS:
1924 announce_delayrendered_targets_with_opened_clipboard (win32_sel);
1925 break;
1926 default:
1927 g_assert_not_reached ();
1928 }
1929
1930 g_clear_object (&info->requestor);
1931 g_slice_free (GdkWin32ClipboardQueueInfo, info);
1932 }
1933
1934 if (clipboard_queue != NULL)
1935 return TRUE;
1936
1937 if (win32_sel->clipboard_opened_for != INVALID_HANDLE_VALUE)
1938 {
1939 API_CALL (CloseClipboard, ());
1940 win32_sel->clipboard_opened_for = INVALID_HANDLE_VALUE;
1941 GDK_NOTE (DND, g_print ("Closed clipboard @ %s:%d\n", __FILE__, __LINE__));
1942 }
1943
1944 GDK_NOTE (DND, g_print ("Stopping open clipboard timer\n"));
1945
1946 return FALSE;
1947 }
1948
1949 static void
1950 queue_open_clipboard (GdkWin32ClipboardQueueAction action,
1951 GdkDisplay *display,
1952 GdkWindow *requestor,
1953 GdkAtom target,
1954 guint32 time)
1955 {
1956 guint id;
1957 GList *tmp_list, *next;
1958 GdkWin32ClipboardQueueInfo *info;
1959 GdkWin32Selection *win32_sel = _gdk_win32_selection_get ();
1960
1961 for (tmp_list = clipboard_queue; tmp_list; tmp_list = next)
1962 {
1963 info = (GdkWin32ClipboardQueueInfo *) tmp_list->data;
1964 next = g_list_next (tmp_list);
1965
1966 if (info->action == action &&
1967 info->requestor == requestor)
1968 return;
1969 }
1970
1971 info = g_slice_new0 (GdkWin32ClipboardQueueInfo);
1972
1973 info->display = display;
1974 info->requestor = g_object_ref (requestor);
1975 info->selection = GDK_SELECTION_CLIPBOARD;
1976 info->target = target;
1977 info->idle_time = 0;
1978 info->time = time;
1979 info->action = action;
1980
1981 GDK_NOTE (DND, g_print ("Queueing open clipboard\n"));
1982
1983 if (win32_sel->clipboard_opened_for == INVALID_HANDLE_VALUE &&
1984 clipboard_queue == NULL)
1985 {
1986 id = gdk_threads_add_timeout_seconds (1, (GSourceFunc) open_clipboard_timeout, NULL);
1987 g_source_set_name_by_id (id, "[gdk-win32] open_clipboard_timeout");
1988 GDK_NOTE (DND, g_print ("Started open clipboard timer\n"));
1989 }
1990
1991 clipboard_queue = g_list_append (clipboard_queue, info);
1992 }
1993
1994 gboolean
1995 _gdk_win32_display_set_selection_owner (GdkDisplay *display,
1996 GdkWindow *owner,
1997 GdkAtom selection,
1998 guint32 time,
1999 gboolean send_event)
2000 {
2001 HWND hwnd;
2002 GdkWin32Selection *win32_sel = _gdk_win32_selection_get ();
2003
2004 g_return_val_if_fail (selection != GDK_NONE, FALSE);
2005
2006 GDK_NOTE (DND, {
2007 gchar *sel_name = gdk_atom_name (selection);
2008
2009 g_print ("gdk_selection_owner_set_for_display: %p %s\n",
2010 (owner ? GDK_WINDOW_HWND (owner) : NULL),
2011 sel_name);
2012 g_free (sel_name);
2013 });
2014
2015 if (selection != GDK_SELECTION_CLIPBOARD)
2016 {
2017 if (owner != NULL)
2018 g_hash_table_insert (win32_sel->sel_owner_table, selection, GDK_WINDOW_HWND (owner));
2019 else
2020 g_hash_table_remove (win32_sel->sel_owner_table, selection);
2021
2022 return TRUE;
2023 }
2024
2025 /* Rest of this function handles the CLIPBOARD selection */
2026 if (owner != NULL)
2027 {
2028 if (GDK_WINDOW_DESTROYED (owner))
2029 return FALSE;
2030
2031 hwnd = GDK_WINDOW_HWND (owner);
2032 }
2033 else
2034 hwnd = NULL;
2035
2036 if (win32_sel->clipboard_opened_for != hwnd &&
2037 win32_sel->clipboard_opened_for != INVALID_HANDLE_VALUE)
2038 {
2039 API_CALL (CloseClipboard, ());
2040 win32_sel->clipboard_opened_for = INVALID_HANDLE_VALUE;
2041 GDK_NOTE (DND, g_print ("Closed clipboard @ %s:%d\n", __FILE__, __LINE__));
2042 }
2043
2044 if (!OpenClipboard (hwnd))
2045 {
2046 if (GetLastError () != ERROR_ACCESS_DENIED)
2047 WIN32_API_FAILED ("OpenClipboard");
2048
2049 return FALSE;
2050 }
2051
2052 win32_sel->clipboard_opened_for = hwnd;
2053 GDK_NOTE (DND, g_print ("Opened clipboard for 0x%p @ %s:%d\n", win32_sel->clipboard_opened_for, __FILE__, __LINE__));
2054 win32_sel->ignore_destroy_clipboard = TRUE;
2055 GDK_NOTE (DND, g_print ("... EmptyClipboard()\n"));
2056 if (!API_CALL (EmptyClipboard, ()))
2057 {
2058 win32_sel->ignore_destroy_clipboard = FALSE;
2059 API_CALL (CloseClipboard, ());
2060 win32_sel->clipboard_opened_for = INVALID_HANDLE_VALUE;
2061 GDK_NOTE (DND, g_print ("Closed clipboard @ %s:%d\n", __FILE__, __LINE__));
2062 return FALSE;
2063 }
2064 win32_sel->ignore_destroy_clipboard = FALSE;
2065
2066 /* Any queued clipboard operations were just made pointless
2067 * by EmptyClipboard().
2068 */
2069 _gdk_win32_clear_clipboard_queue ();
2070
2071 /* This is kind of risky, but we don't close the clipboard
2072 * to ensure that it's still open when GDK_SELECTION_REQUEST
2073 * is handled.
2074 */
2075 if (owner == NULL)
2076 {
2077 if (!API_CALL (CloseClipboard, ()))
2078 return FALSE;
2079 GDK_NOTE (DND, g_print ("Closed clipboard @ %s:%d\n", __FILE__, __LINE__));
2080 win32_sel->clipboard_opened_for = INVALID_HANDLE_VALUE;
2081 }
2082
2083 send_targets_request (time);
2084
2085 return TRUE;
2086 }
2087
2088 GdkWindow*
2089 _gdk_win32_display_get_selection_owner (GdkDisplay *display,
2090 GdkAtom selection)
2091 {
2092 GdkWindow *window;
2093 HWND selection_owner;
2094 GdkWin32Selection *win32_sel = _gdk_win32_selection_get ();
2095
2096 g_return_val_if_fail (selection != GDK_NONE, NULL);
2097
2098 if (selection == GDK_SELECTION_CLIPBOARD)
2099 selection_owner = GetClipboardOwner ();
2100 else
2101 selection_owner = g_hash_table_lookup (win32_sel->sel_owner_table, selection);
2102
2103 if (selection_owner)
2104 window = gdk_win32_window_lookup_for_display (display,
2105 selection_owner);
2106 else
2107 window = NULL;
2108
2109 GDK_NOTE (DND, {
2110 gchar *sel_name = gdk_atom_name (selection);
2111
2112 g_print ("gdk_selection_owner_get: %s = %p\n",
2113 sel_name,
2114 (window ? GDK_WINDOW_HWND (window) : NULL));
2115 g_free (sel_name);
2116 });
2117
2118 return window;
2119 }
2120
2121 static GdkAtom
2122 convert_dnd_selection_to_target (GdkAtom target,
2123 GdkWindow *requestor)
2124 {
2125 GdkWin32Selection *win32_sel = _gdk_win32_selection_get ();
2126 UINT format;
2127 gint i, with_transmute;
2128 guchar *ptr;
2129 gint length;
2130 gboolean transmute = FALSE;
2131 GdkWin32DragContext *context_win32;
2132 FORMATETC fmt;
2133 STGMEDIUM storage;
2134 HRESULT hr;
2135 GdkAtom result = _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_OLE2_DND);
2136
2137 g_assert (win32_sel->target_drag_context != NULL);
2138 g_assert (win32_sel->dnd_data_object_target != NULL);
2139
2140 context_win32 = GDK_WIN32_DRAG_CONTEXT (win32_sel->target_drag_context);
2141
2142 fmt.ptd = NULL;
2143 fmt.dwAspect = DVASPECT_CONTENT;
2144 fmt.lindex = -1;
2145 fmt.tymed = TYMED_HGLOBAL;
2146
2147 /* We rely on GTK+ applications to synthesize the DELETE request
2148 * for themselves, since they do know whether a DnD operation was a
2149 * move and whether was successful. Therefore, we do not need to
2150 * actually send anything here. Just report back without storing
2151 * any data.
2152 */
2153 if (target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_DELETE))
2154 return result;
2155
2156 for (format = 0, with_transmute = 0; format == 0 && with_transmute < 2; with_transmute++)
2157 {
2158 for (i = 0;
2159 i < context_win32->droptarget_format_target_map->len;
2160 i++)
2161 {
2162 GdkSelTargetFormat selformat = g_array_index (context_win32->droptarget_format_target_map, GdkSelTargetFormat, i);
2163
2164 if (selformat.target != target ||
2165 selformat.transmute != (with_transmute == 0 ? FALSE : TRUE))
2166 continue;
2167
2168 fmt.cfFormat = selformat.format;
2169
2170 hr = IDataObject_QueryGetData (win32_sel->dnd_data_object_target, &fmt);
2171
2172 if (!SUCCEEDED (hr) || hr != S_OK)
2173 continue;
2174
2175 format = selformat.format;
2176 transmute = selformat.transmute;
2177 break;
2178 }
2179 }
2180
2181 if (format == 0)
2182 return GDK_NONE;
2183
2184 hr = IDataObject_GetData (win32_sel->dnd_data_object_target, &fmt, &storage);
2185
2186 if (!SUCCEEDED (hr) || hr != S_OK)
2187 return GDK_NONE;
2188
2189 if ((ptr = GlobalLock (storage.hGlobal)) != NULL)
2190 {
2191 guchar *data = NULL;
2192 gint data_len = 0;
2193
2194 SetLastError (0);
2195 length = GlobalSize (storage.hGlobal);
2196
2197 if (GetLastError () == NO_ERROR)
2198 {
2199 if (transmute)
2200 {
2201 transmute_selection_format (format, target, ptr, length, &data, &data_len);
2202 }
2203 else
2204 {
2205 data = g_memdup (ptr, length);
2206 data_len = length;
2207 }
2208
2209 if (data)
2210 selection_property_store (requestor, target, 8,
2211 data, data_len);
2212 else
2213 result = GDK_NONE;
2214 }
2215 else
2216 result = GDK_NONE;
2217
2218 GlobalUnlock (storage.hGlobal);
2219 }
2220 else
2221 result = GDK_NONE;
2222
2223 ReleaseStgMedium (&storage);
2224
2225 return result;
2226 }
2227
2228 void
2229 _gdk_win32_display_convert_selection (GdkDisplay *display,
2230 GdkWindow *requestor,
2231 GdkAtom selection,
2232 GdkAtom target,
2233 guint32 time)
2234 {
2235 GdkWin32Selection *win32_sel = _gdk_win32_selection_get ();
2236 GdkAtom property = _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_GDK_SELECTION);
2237
2238 g_return_if_fail (selection != GDK_NONE);
2239 g_return_if_fail (requestor != NULL);
2240
2241 if (GDK_WINDOW_DESTROYED (requestor))
2242 return;
2243
2244 GDK_NOTE (DND, {
2245 gchar *sel_name = gdk_atom_name (selection);
2246 gchar *tgt_name = gdk_atom_name (target);
2247
2248 g_print ("gdk_selection_convert: %p %s %s\n",
2249 GDK_WINDOW_HWND (requestor),
2250 sel_name, tgt_name);
2251 g_free (sel_name);
2252 g_free (tgt_name);
2253 });
2254
2255 if (selection == GDK_SELECTION_CLIPBOARD)
2256 {
2257 if (win32_sel->clipboard_opened_for != INVALID_HANDLE_VALUE ||
2258 OpenClipboard (GDK_WINDOW_HWND (requestor)))
2259 {
2260 if (win32_sel->clipboard_opened_for == INVALID_HANDLE_VALUE)
2261 {
2262 win32_sel->clipboard_opened_for = GDK_WINDOW_HWND (requestor);
2263 GDK_NOTE (DND, g_print ("Opened clipboard for 0x%p @ %s:%d\n", win32_sel->clipboard_opened_for, __FILE__, __LINE__));
2264 }
2265
2266 queue_open_clipboard (GDK_WIN32_CLIPBOARD_QUEUE_ACTION_CONVERT, display, requestor, target, time);
2267 open_clipboard_timeout (NULL);
2268 return;
2269 }
2270 else
2271 {
2272 queue_open_clipboard (GDK_WIN32_CLIPBOARD_QUEUE_ACTION_CONVERT, display, requestor, target, time);
2273 /* Do not generate a selection notify message */
2274 return;
2275 }
2276 }
2277 else if (selection == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_DROPFILES_DND))
2278 {
2279 /* This means he wants the names of the dropped files.
2280 * gdk_dropfiles_filter already has stored the text/uri-list
2281 * data temporarily in dropfiles_prop.
2282 */
2283 if (win32_sel->dropfiles_prop != NULL)
2284 {
2285 selection_property_store
2286 (requestor, win32_sel->dropfiles_prop->target, win32_sel->dropfiles_prop->bitness,
2287 win32_sel->dropfiles_prop->data, win32_sel->dropfiles_prop->length);
2288 g_clear_pointer (&win32_sel->dropfiles_prop, g_free);
2289 }
2290 }
2291 else if (selection == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_OLE2_DND))
2292 {
2293 property = convert_dnd_selection_to_target (target, requestor);
2294 }
2295 else
2296 property = GDK_NONE;
2297
2298 /* Generate a selection notify message so that we actually fetch the
2299 * data (if property == GDK_SELECTION) or indicating failure (if
2300 * property == GDK_NONE).
2301 */
2302 generate_selection_notify (requestor, selection, target, property, time);
2303 }
2304
2305 void
2306 _gdk_win32_selection_property_change (GdkWin32Selection *win32_sel,
2307 GdkWindow *window,
2308 GdkAtom property,
2309 GdkAtom type,
2310 gint format,
2311 GdkPropMode mode,
2312 const guchar *data,
2313 gint nelements)
2314 {
2315 if (property == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_GDK_SELECTION) &&
2316 win32_sel->property_change_target_atom == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_TARGETS))
2317 {
2318 win32_sel->property_change_target_atom = 0;
2319
2320 if (win32_sel->clipboard_opened_for == INVALID_HANDLE_VALUE &&
2321 OpenClipboard (GDK_WINDOW_HWND (window)))
2322 {
2323 win32_sel->clipboard_opened_for = GDK_WINDOW_HWND (window);
2324 GDK_NOTE (DND, g_print ("Opened clipboard for 0x%p @ %s:%d\n", win32_sel->clipboard_opened_for, __FILE__, __LINE__));
2325 }
2326
2327 if (win32_sel->clipboard_opened_for == INVALID_HANDLE_VALUE)
2328 {
2329 queue_open_clipboard (GDK_WIN32_CLIPBOARD_QUEUE_ACTION_TARGETS, NULL, window, type, GDK_CURRENT_TIME);
2330 return;
2331 }
2332 else
2333 {
2334 queue_open_clipboard (GDK_WIN32_CLIPBOARD_QUEUE_ACTION_TARGETS, NULL, window, type, GDK_CURRENT_TIME);
2335 open_clipboard_timeout (NULL);
2336 }
2337 }
2338 else if ((property == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_OLE2_DND) ||
2339 property == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_LOCAL_DND_SELECTION)) &&
2340 mode == GDK_PROP_MODE_REPLACE &&
2341 win32_sel->property_change_target_atom == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_DELETE))
2342 {
2343 /* no-op on Windows */
2344 win32_sel->property_change_target_atom = 0;
2345 }
2346 else if (mode == GDK_PROP_MODE_REPLACE &&
2347 (win32_sel->property_change_target_atom == 0 ||
2348 win32_sel->property_change_data == NULL ||
2349 win32_sel->property_change_format == 0))
2350 {
2351 g_warning ("Setting selection property with 0x%p == NULL or 0x%x == 0 or 0x%p == 0",
2352 win32_sel->property_change_data,
2353 win32_sel->property_change_format,
2354 win32_sel->property_change_target_atom);
2355 }
2356 else if (mode == GDK_PROP_MODE_REPLACE &&
2357 win32_sel->property_change_data != NULL &&
2358 win32_sel->property_change_format != 0)
2359 {
2360 guchar *set_data = NULL;
2361 gint set_data_length = 0;
2362 gint byte_length = format / 8 * nelements;
2363
2364 if (win32_sel->property_change_transmute)
2365 transmute_selection_target (type,
2366 win32_sel->property_change_format,
2367 data,
2368 byte_length,
2369 &set_data,
2370 &set_data_length);
2371 else
2372 {
2373 set_data_length = byte_length;
2374 set_data = g_memdup (data, set_data_length);
2375 }
2376
2377 if (set_data != NULL && set_data_length > 0)
2378 {
2379 HGLOBAL hdata;
2380
2381 if ((hdata = GlobalAlloc (GMEM_MOVEABLE, set_data_length)) != 0)
2382 {
2383 gchar *ucptr;
2384 win32_sel->property_change_data->tymed = TYMED_HGLOBAL;
2385 win32_sel->property_change_data->pUnkForRelease = NULL;
2386 win32_sel->property_change_data->hGlobal = hdata;
2387 ucptr = GlobalLock (hdata);
2388 memcpy (ucptr, set_data, set_data_length);
2389 GlobalUnlock (hdata);
2390 }
2391 else
2392 {
2393 WIN32_API_FAILED ("GlobalAlloc");
2394 }
2395
2396 g_free (set_data);
2397 }
2398
2399 win32_sel->property_change_format = 0;
2400 win32_sel->property_change_data = 0;
2401 win32_sel->property_change_target_atom = 0;
2402 }
2403 else
2404 {
2405 GDK_NOTE (DND, {
2406 gchar *prop_name = gdk_atom_name (property);
2407 gchar *type_name = gdk_atom_name (type);
2408 gchar *datastring = _gdk_win32_data_to_string (data, MIN (10, format*nelements/8));
2409
2410 g_warning ("Unsupported property change on window 0x%p, %s property %s, %d-bit, target 0x%x of %d bytes: %s",
2411 window,
2412 (mode == GDK_PROP_MODE_REPLACE ? "REPLACE" :
2413 (mode == GDK_PROP_MODE_PREPEND ? "PREPEND" :
2414 (mode == GDK_PROP_MODE_APPEND ? "APPEND" :
2415 "???"))),
2416 prop_name,
2417 format,
2418 type_name,
2419 nelements,
2420 datastring);
2421 g_free (datastring);
2422 g_free (prop_name);
2423 g_free (type_name);
2424 });
2425 }
2426 }
2427
2428 gint
2429 _gdk_win32_display_get_selection_property (GdkDisplay *display,
2430 GdkWindow *requestor,
2431 guchar **data,
2432 GdkAtom *ret_type,
2433 gint *ret_format)
2434 {
2435 GdkWin32Selection *win32_sel = _gdk_win32_selection_get ();
2436 GdkSelProp *prop;
2437
2438 g_return_val_if_fail (requestor != NULL, 0);
2439 g_return_val_if_fail (GDK_IS_WINDOW (requestor), 0);
2440
2441 if (GDK_WINDOW_DESTROYED (requestor))
2442 return 0;
2443
2444 GDK_NOTE (DND, g_print ("gdk_selection_property_get: %p",
2445 GDK_WINDOW_HWND (requestor)));
2446
2447 prop = g_hash_table_lookup (win32_sel->sel_prop_table, GDK_WINDOW_HWND (requestor));
2448
2449 if (prop == NULL)
2450 {
2451 GDK_NOTE (DND, g_print (" (nothing)\n"));
2452 *data = NULL;
2453
2454 return 0;
2455 }
2456
2457 *data = g_malloc (prop->length + 1);
2458 (*data)[prop->length] = '\0';
2459 if (prop->length > 0)
2460 memmove (*data, prop->data, prop->length);
2461
2462 GDK_NOTE (DND, {
2463 gchar *type_name = gdk_atom_name (prop->target);
2464
2465 g_print (" %s format:%d length:%"G_GSIZE_FORMAT"\n", type_name, prop->bitness, prop->length);
2466 g_free (type_name);
2467 });
2468
2469 if (ret_type)
2470 *ret_type = prop->target;
2471
2472 if (ret_format)
2473 *ret_format = prop->bitness;
2474
2475 return prop->length;
2476 }
2477
2478 void
2479 _gdk_selection_property_delete (GdkWindow *window)
2480 {
2481 GDK_NOTE (DND, g_print ("_gdk_selection_property_delete: %p (no-op)\n",
2482 GDK_WINDOW_HWND (window)));
2483
2484 #if 0
2485 prop = g_hash_table_lookup (sel_prop_table, GDK_WINDOW_HWND (window));
2486 if (prop != NULL)
2487 {
2488 g_free (prop->data);
2489 g_free (prop);
2490 g_hash_table_remove (sel_prop_table, GDK_WINDOW_HWND (window));
2491 }
2492 #endif
2493 }
2494
2495 void
2496 _gdk_win32_display_send_selection_notify (GdkDisplay *display,
2497 GdkWindow *requestor,
2498 GdkAtom selection,
2499 GdkAtom target,
2500 GdkAtom property,
2501 guint32 time)
2502 {
2503 GDK_NOTE (DND, {
2504 gchar *sel_name = gdk_atom_name (selection);
2505 gchar *tgt_name = gdk_atom_name (target);
2506 gchar *prop_name = gdk_atom_name (property);
2507
2508 g_print ("gdk_selection_send_notify_for_display: %p %s %s %s (no-op)\n",
2509 requestor, sel_name, tgt_name, prop_name);
2510 g_free (sel_name);
2511 g_free (tgt_name);
2512 g_free (prop_name);
2513 });
2514 }
2515
2516 /* It's hard to say whether implementing this actually is of any use
2517 * on the Win32 platform? gtk calls only
2518 * gdk_text_property_to_utf8_list_for_display().
2519 */
2520 gint
2521 gdk_text_property_to_text_list_for_display (GdkDisplay *display,
2522 GdkAtom encoding,
2523 gint format,
2524 const guchar *text,
2525 gint length,
2526 gchar ***list)
2527 {
2528 gchar *result;
2529 const gchar *charset;
2530 gchar *source_charset;
2531
2532 GDK_NOTE (DND, {
2533 gchar *enc_name = gdk_atom_name (encoding);
2534
2535 g_print ("gdk_text_property_to_text_list_for_display: %s %d %.20s %d\n",
2536 enc_name, format, text, length);
2537 g_free (enc_name);
2538 });
2539
2540 if (!list)
2541 return 0;
2542
2543 if (encoding == GDK_TARGET_STRING)
2544 source_charset = g_strdup ("ISO-8859-1");
2545 else if (encoding == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_UTF8_STRING))
2546 source_charset = g_strdup ("UTF-8");
2547 else
2548 source_charset = gdk_atom_name (encoding);
2549
2550 g_get_charset (&charset);
2551
2552 result = g_convert ((const gchar *) text, length, charset, source_charset,
2553 NULL, NULL, NULL);
2554 g_free (source_charset);
2555
2556 if (!result)
2557 return 0;
2558
2559 *list = g_new (gchar *, 1);
2560 **list = result;
2561
2562 return 1;
2563 }
2564
2565 void
2566 gdk_free_text_list (gchar **list)
2567 {
2568 g_return_if_fail (list != NULL);
2569
2570 g_free (*list);
2571 g_free (list);
2572 }
2573
2574 static gint
2575 make_list (const gchar *text,
2576 gint length,
2577 gboolean latin1,
2578 gchar ***list)
2579 {
2580 GSList *strings = NULL;
2581 gint n_strings = 0;
2582 gint i;
2583 const gchar *p = text;
2584 const gchar *q;
2585 GSList *tmp_list;
2586 GError *error = NULL;
2587
2588 while (p < text + length)
2589 {
2590 gchar *str;
2591
2592 q = p;
2593 while (*q && q < text + length)
2594 q++;
2595
2596 if (latin1)
2597 {
2598 str = g_convert (p, q - p,
2599 "UTF-8", "ISO-8859-1",
2600 NULL, NULL, &error);
2601
2602 if (!str)
2603 {
2604 g_warning ("Error converting selection from STRING: %s",
2605 error->message);
2606 g_error_free (error);
2607 }
2608 }
2609 else
2610 str = g_strndup (p, q - p);
2611
2612 if (str)
2613 {
2614 strings = g_slist_prepend (strings, str);
2615 n_strings++;
2616 }
2617
2618 p = q + 1;
2619 }
2620
2621 if (list)
2622 *list = g_new (gchar *, n_strings + 1);
2623
2624 (*list)[n_strings] = NULL;
2625
2626 i = n_strings;
2627 tmp_list = strings;
2628 while (tmp_list)
2629 {
2630 if (list)
2631 (*list)[--i] = tmp_list->data;
2632 else
2633 g_free (tmp_list->data);
2634
2635 tmp_list = tmp_list->next;
2636 }
2637
2638 g_slist_free (strings);
2639
2640 return n_strings;
2641 }
2642
2643 gint
2644 _gdk_win32_display_text_property_to_utf8_list (GdkDisplay *display,
2645 GdkAtom encoding,
2646 gint format,
2647 const guchar *text,
2648 gint length,
2649 gchar ***list)
2650 {
2651 g_return_val_if_fail (text != NULL, 0);
2652 g_return_val_if_fail (length >= 0, 0);
2653
2654 if (encoding == GDK_TARGET_STRING)
2655 {
2656 return make_list ((gchar *)text, length, TRUE, list);
2657 }
2658 else if (encoding == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_UTF8_STRING))
2659 {
2660 return make_list ((gchar *)text, length, FALSE, list);
2661 }
2662 else
2663 {
2664 gchar *enc_name = gdk_atom_name (encoding);
2665
2666 g_warning ("gdk_text_property_to_utf8_list_for_display: encoding %s not handled\n", enc_name);
2667 g_free (enc_name);
2668
2669 if (list)
2670 *list = NULL;
2671
2672 return 0;
2673 }
2674 }
2675
2676 gchar *
2677 _gdk_win32_display_utf8_to_string_target (GdkDisplay *display,
2678 const gchar *str)
2679 {
2680 return _gdk_utf8_to_string_target_internal (str, strlen (str));
2681 }
2682
2683 void
2684 gdk_win32_selection_clear_targets (GdkDisplay *display,
2685 GdkAtom selection)
2686 {
2687 GdkWin32Selection *win32_sel = _gdk_win32_selection_get ();
2688
2689 if (selection == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_OLE2_DND) ||
2690 selection == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_LOCAL_DND_SELECTION))
2691 {
2692 g_array_set_size (win32_sel->dnd_selection_targets, 0);
2693 }
2694 else if (selection == GDK_SELECTION_CLIPBOARD)
2695 {
2696 g_array_set_size (win32_sel->clipboard_selection_targets, 0);
2697 }
2698 else if (selection == GDK_SELECTION_PRIMARY)
2699 {
2700 /* Do nothing */
2701 }
2702 else
2703 {
2704 gchar *sel_name = gdk_atom_name (selection);
2705
2706 g_warning ("Unsupported generic selection %s (0x%p)", sel_name, selection);
2707 g_free (sel_name);
2708 }
2709 }
2710
2711 gint
2712 _gdk_win32_add_target_to_selformats (GdkAtom target,
2713 GArray *array)
2714 {
2715 gint added_count = 0;
2716 gchar *target_name;
2717 wchar_t *target_name_w;
2718 GdkSelTargetFormat fmt;
2719 gint i;
2720 GArray *compatibility_formats;
2721 gint starting_point;
2722
2723 for (i = 0; i < array->len; i++)
2724 if (g_array_index (array, GdkSelTargetFormat, i).target == target)
2725 break;
2726
2727 /* Don't put duplicates into the array */
2728 if (i < array->len)
2729 return added_count;
2730
2731 if (target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_TARGETS) ||
2732 target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_COMPOUND_TEXT) || target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_SAVE_TARGETS))
2733 {
2734 /* Add the "we don't really support transferring that to
2735 * other processes" format, just to keep the taget around.
2736 */
2737 fmt.target = target;
2738 fmt.format = 0;
2739 fmt.transmute = FALSE;
2740 g_array_append_val (array, fmt);
2741 added_count += 1;
2742 return added_count;
2743 }
2744
2745 /* Only check the newly-added pairs for duplicates,
2746 * all the ones that exist right now have different targets.
2747 */
2748 starting_point = array->len;
2749
2750 target_name = gdk_atom_name (target);
2751 target_name_w = g_utf8_to_utf16 (target_name, -1, NULL, NULL, NULL);
2752 g_free (target_name);
2753
2754 if (target_name_w == NULL)
2755 return added_count;
2756
2757 fmt.format = RegisterClipboardFormatW (target_name_w);
2758 GDK_NOTE (DND, g_print ("Registered clipboard format %S as 0x%x\n", target_name_w, fmt.format));
2759 g_free (target_name_w);
2760 fmt.target = target;
2761 fmt.transmute = FALSE;
2762
2763 /* Add the "as-is" format */
2764 g_array_append_val (array, fmt);
2765 added_count += 1;
2766
2767 compatibility_formats = get_compatibility_formats_for_target (target);
2768 for (i = 0; compatibility_formats != NULL && i < compatibility_formats->len; i++)
2769 {
2770 gint j;
2771
2772 fmt = g_array_index (compatibility_formats, GdkSelTargetFormat, i);
2773
2774 /* Don't put duplicates into the array */
2775 for (j = starting_point; j < array->len; j++)
2776 if (g_array_index (array, GdkSelTargetFormat, j).format == fmt.format)
2777 break;
2778
2779 if (j < array->len)
2780 continue;
2781
2782 /* Add a compatibility format */
2783 g_array_append_val (array, fmt);
2784 added_count += 1;
2785 }
2786
2787 return added_count;
2788 }
2789
2790 /* This function is called from gtk_selection_add_target() and
2791 * gtk_selection_add_targets() in gtkselection.c. It is this function
2792 * that takes care of setting those clipboard formats for which we use
2793 * delayed rendering (that is, all formats, as we use delayed rendering
2794 * for everything). This function only registers the formats, but does
2795 * not announce them as supported. That is handled as a special case
2796 * in gdk_window_property_change().
2797 *
2798 * Implementation detail:
2799 * This function will be called repeatedly, every time the PRIMARY selection changes.
2800 * It will also be called immediately before the CLIPBOARD selection changes.
2801 * We let GTK+ handle the PRIMARY selection internally and do nothing here
2802 * (therefore it's not possible to middle-click-paste between processes,
2803 * unless one process deliberately puts PRIMARY selection contents into
2804 * CLIPBOARD selection, and the other process does paste on middle-click).
2805 */
2806 void
2807 gdk_win32_selection_add_targets (GdkWindow *owner,
2808 GdkAtom selection,
2809 gint n_targets,
2810 GdkAtom *targets)
2811 {
2812 GdkWin32Selection *win32_sel = _gdk_win32_selection_get ();
2813 gint i;
2814
2815 GDK_NOTE (DND, {
2816 gchar *sel_name = gdk_atom_name (selection);
2817
2818 g_print ("gdk_win32_selection_add_targets: %p: %s: ",
2819 owner ? GDK_WINDOW_HWND (owner) : NULL,
2820 sel_name);
2821 g_free (sel_name);
2822
2823 for (i = 0; i < n_targets; i++)
2824 {
2825 gchar *tgt_name = gdk_atom_name (targets[i]);
2826
2827 g_print ("%s", tgt_name);
2828 g_free (tgt_name);
2829 if (i < n_targets - 1)
2830 g_print (", ");
2831 }
2832 g_print ("\n");
2833 });
2834
2835 if (selection == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_OLE2_DND) ||
2836 selection == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_LOCAL_DND_SELECTION) ||
2837 selection == GDK_SELECTION_CLIPBOARD)
2838 {
2839 GArray *fmts = NULL;
2840 gint added_count = 0;
2841
2842 if (selection == GDK_SELECTION_CLIPBOARD)
2843 fmts = win32_sel->clipboard_selection_targets;
2844 else
2845 fmts = win32_sel->dnd_selection_targets;
2846
2847 for (i = 0; i < n_targets; i++)
2848 added_count += _gdk_win32_add_target_to_selformats (targets[i], fmts);
2849
2850 /* Re-announce our list of supported formats */
2851 if (added_count > 0)
2852 send_targets_request (GDK_CURRENT_TIME);
2853 }
2854 else if (selection == GDK_SELECTION_PRIMARY)
2855 {
2856 /* Do nothing */
2857 }
2858 else
2859 {
2860 gchar *sel_name = gdk_atom_name (selection);
2861
2862 g_warning ("Unsupported generic selection %s (0x%p)", sel_name, selection);
2863 g_free (sel_name);
2864 }
2865 }