]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk/dnd.cpp | |
3 | // Purpose: wxDropTarget class | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #if wxUSE_DRAG_AND_DROP | |
14 | ||
15 | #include "wx/dnd.h" | |
16 | ||
17 | #ifndef WX_PRECOMP | |
18 | #include "wx/intl.h" | |
19 | #include "wx/log.h" | |
20 | #include "wx/app.h" | |
21 | #include "wx/utils.h" | |
22 | #include "wx/window.h" | |
23 | #endif | |
24 | ||
25 | #include "wx/gdicmn.h" | |
26 | ||
27 | #include "wx/gtk/private.h" | |
28 | ||
29 | #include <gdk/gdkprivate.h> | |
30 | ||
31 | #include <gtk/gtkdnd.h> | |
32 | #include <gtk/gtkselection.h> | |
33 | ||
34 | //---------------------------------------------------------------------------- | |
35 | // global data | |
36 | //---------------------------------------------------------------------------- | |
37 | ||
38 | extern bool g_blockEventsOnDrag; | |
39 | ||
40 | // the flags used for the last DoDragDrop() | |
41 | static long gs_flagsForDrag = 0; | |
42 | ||
43 | // the trace mask we use with wxLogTrace() - call | |
44 | // wxLog::AddTraceMask(TRACE_DND) to enable the trace messages from here | |
45 | // (there are quite a few of them, so don't enable this by default) | |
46 | static const wxChar *TRACE_DND = _T("dnd"); | |
47 | ||
48 | //---------------------------------------------------------------------------- | |
49 | // standard icons | |
50 | //---------------------------------------------------------------------------- | |
51 | ||
52 | /* Copyright (c) Julian Smart */ | |
53 | static const char * page_xpm[] = { | |
54 | /* columns rows colors chars-per-pixel */ | |
55 | "32 32 37 1", | |
56 | "5 c #7198D9", | |
57 | ", c #769CDA", | |
58 | "2 c #DCE6F6", | |
59 | "i c #FFFFFF", | |
60 | "e c #779DDB", | |
61 | ": c #9AB6E4", | |
62 | "9 c #EAF0FA", | |
63 | "- c #B1C7EB", | |
64 | "$ c #6992D7", | |
65 | "y c #F7F9FD", | |
66 | "= c #BED0EE", | |
67 | "q c #F0F5FC", | |
68 | "; c #A8C0E8", | |
69 | "@ c #366BC2", | |
70 | " c None", | |
71 | "u c #FDFEFF", | |
72 | "8 c #5987D3", | |
73 | "* c #C4D5F0", | |
74 | "7 c #7CA0DC", | |
75 | "O c #487BCE", | |
76 | "< c #6B94D7", | |
77 | "& c #CCDAF2", | |
78 | "> c #89A9DF", | |
79 | "3 c #5584D1", | |
80 | "w c #82A5DE", | |
81 | "1 c #3F74CB", | |
82 | "+ c #3A70CA", | |
83 | ". c #3569BF", | |
84 | "% c #D2DFF4", | |
85 | "# c #3366BB", | |
86 | "r c #F5F8FD", | |
87 | "0 c #FAFCFE", | |
88 | "4 c #DFE8F7", | |
89 | "X c #5E8AD4", | |
90 | "o c #5282D0", | |
91 | "t c #B8CCEC", | |
92 | "6 c #E5EDF9", | |
93 | /* pixels */ | |
94 | " ", | |
95 | " ", | |
96 | " ", | |
97 | " ", | |
98 | " ", | |
99 | " .XXXooOO++@# ", | |
100 | " $%&*=-;::>,<1 ", | |
101 | " $2%&*=-;::><:3 ", | |
102 | " $42%&*=-;::<&:3 ", | |
103 | " 56477<<<<8<<9&:X ", | |
104 | " 59642%&*=-;<09&:5 ", | |
105 | " 5q9642%&*=-<<<<<# ", | |
106 | " 5qqw777<<<<<88:>+ ", | |
107 | " erqq9642%&*=t;::+ ", | |
108 | " eyrqq9642%&*=t;:O ", | |
109 | " eyywwww777<<<<t;O ", | |
110 | " e0yyrqq9642%&*=to ", | |
111 | " e00yyrqq9642%&*=o ", | |
112 | " eu0wwwwwww777<&*X ", | |
113 | " euu00yyrqq9642%&X ", | |
114 | " eiuu00yyrqq9642%X ", | |
115 | " eiiwwwwwwwwww742$ ", | |
116 | " eiiiuu00yyrqq964$ ", | |
117 | " eiiiiuu00yyrqq96$ ", | |
118 | " eiiiiiuu00yyrqq95 ", | |
119 | " eiiiiiiuu00yyrqq5 ", | |
120 | " eeeeeeeeeeeeee55e ", | |
121 | " ", | |
122 | " ", | |
123 | " ", | |
124 | " ", | |
125 | " " | |
126 | }; | |
127 | ||
128 | ||
129 | // ============================================================================ | |
130 | // private functions | |
131 | // ============================================================================ | |
132 | ||
133 | // ---------------------------------------------------------------------------- | |
134 | // convert between GTK+ and wxWidgets DND constants | |
135 | // ---------------------------------------------------------------------------- | |
136 | ||
137 | static wxDragResult ConvertFromGTK(long action) | |
138 | { | |
139 | switch ( action ) | |
140 | { | |
141 | case GDK_ACTION_COPY: | |
142 | return wxDragCopy; | |
143 | ||
144 | case GDK_ACTION_LINK: | |
145 | return wxDragLink; | |
146 | ||
147 | case GDK_ACTION_MOVE: | |
148 | return wxDragMove; | |
149 | } | |
150 | ||
151 | return wxDragNone; | |
152 | } | |
153 | ||
154 | // ---------------------------------------------------------------------------- | |
155 | // "drag_leave" | |
156 | // ---------------------------------------------------------------------------- | |
157 | ||
158 | extern "C" { | |
159 | static void target_drag_leave( GtkWidget *WXUNUSED(widget), | |
160 | GdkDragContext *context, | |
161 | guint WXUNUSED(time), | |
162 | wxDropTarget *drop_target ) | |
163 | { | |
164 | if (g_isIdle) wxapp_install_idle_handler(); | |
165 | ||
166 | /* inform the wxDropTarget about the current GdkDragContext. | |
167 | this is only valid for the duration of this call */ | |
168 | drop_target->SetDragContext( context ); | |
169 | ||
170 | /* we don't need return values. this event is just for | |
171 | information */ | |
172 | drop_target->OnLeave(); | |
173 | ||
174 | /* this has to be done because GDK has no "drag_enter" event */ | |
175 | drop_target->m_firstMotion = true; | |
176 | ||
177 | /* after this, invalidate the drop_target's GdkDragContext */ | |
178 | drop_target->SetDragContext( (GdkDragContext*) NULL ); | |
179 | } | |
180 | } | |
181 | ||
182 | // ---------------------------------------------------------------------------- | |
183 | // "drag_motion" | |
184 | // ---------------------------------------------------------------------------- | |
185 | ||
186 | extern "C" { | |
187 | static gboolean target_drag_motion( GtkWidget *WXUNUSED(widget), | |
188 | GdkDragContext *context, | |
189 | gint x, | |
190 | gint y, | |
191 | guint time, | |
192 | wxDropTarget *drop_target ) | |
193 | { | |
194 | if (g_isIdle) wxapp_install_idle_handler(); | |
195 | ||
196 | /* Owen Taylor: "if the coordinates not in a drop zone, | |
197 | return FALSE, otherwise call gtk_drag_status() and | |
198 | return TRUE" */ | |
199 | ||
200 | /* inform the wxDropTarget about the current GdkDragContext. | |
201 | this is only valid for the duration of this call */ | |
202 | drop_target->SetDragContext( context ); | |
203 | ||
204 | // GTK+ always supposes that we want to copy the data by default while we | |
205 | // might want to move it, so examine not only suggested_action - which is | |
206 | // only good if we don't have our own preferences - but also the actions | |
207 | // field | |
208 | wxDragResult result; | |
209 | if (drop_target->GetDefaultAction() == wxDragNone) | |
210 | { | |
211 | // use default action set by wxDropSource::DoDragDrop() | |
212 | if ( (gs_flagsForDrag & wxDrag_DefaultMove) == wxDrag_DefaultMove && | |
213 | (context->actions & GDK_ACTION_MOVE ) ) | |
214 | { | |
215 | // move is requested by the program and allowed by GTK+ - do it, even | |
216 | // though suggested_action may be currently wxDragCopy | |
217 | result = wxDragMove; | |
218 | } | |
219 | else // use whatever GTK+ says we should | |
220 | { | |
221 | result = ConvertFromGTK(context->suggested_action); | |
222 | ||
223 | if ( (result == wxDragMove) && !(gs_flagsForDrag & wxDrag_AllowMove) ) | |
224 | { | |
225 | // we're requested to move but we can't | |
226 | result = wxDragCopy; | |
227 | } | |
228 | } | |
229 | } | |
230 | else if (drop_target->GetDefaultAction() == wxDragMove && | |
231 | (context->actions & GDK_ACTION_MOVE)) | |
232 | { | |
233 | result = wxDragMove; | |
234 | } | |
235 | else | |
236 | { | |
237 | if (context->actions & GDK_ACTION_COPY) | |
238 | result = wxDragCopy; | |
239 | else if (context->actions & GDK_ACTION_MOVE) | |
240 | result = wxDragMove; | |
241 | else | |
242 | result = wxDragNone; | |
243 | } | |
244 | ||
245 | if (drop_target->m_firstMotion) | |
246 | { | |
247 | /* the first "drag_motion" event substitutes a "drag_enter" event */ | |
248 | result = drop_target->OnEnter( x, y, result ); | |
249 | } | |
250 | else | |
251 | { | |
252 | /* give program a chance to react (i.e. to say no by returning FALSE) */ | |
253 | result = drop_target->OnDragOver( x, y, result ); | |
254 | } | |
255 | ||
256 | bool ret = wxIsDragResultOk( result ); | |
257 | if (ret) | |
258 | { | |
259 | GdkDragAction action; | |
260 | if (result == wxDragCopy) | |
261 | action = GDK_ACTION_COPY; | |
262 | else if (result == wxDragLink) | |
263 | action = GDK_ACTION_LINK; | |
264 | else | |
265 | action = GDK_ACTION_MOVE; | |
266 | ||
267 | gdk_drag_status( context, action, time ); | |
268 | } | |
269 | ||
270 | /* after this, invalidate the drop_target's GdkDragContext */ | |
271 | drop_target->SetDragContext( (GdkDragContext*) NULL ); | |
272 | ||
273 | /* this has to be done because GDK has no "drag_enter" event */ | |
274 | drop_target->m_firstMotion = false; | |
275 | ||
276 | return ret; | |
277 | } | |
278 | } | |
279 | ||
280 | // ---------------------------------------------------------------------------- | |
281 | // "drag_drop" | |
282 | // ---------------------------------------------------------------------------- | |
283 | ||
284 | extern "C" { | |
285 | static gboolean target_drag_drop( GtkWidget *widget, | |
286 | GdkDragContext *context, | |
287 | gint x, | |
288 | gint y, | |
289 | guint time, | |
290 | wxDropTarget *drop_target ) | |
291 | { | |
292 | if (g_isIdle) wxapp_install_idle_handler(); | |
293 | ||
294 | /* Owen Taylor: "if the drop is not in a drop zone, | |
295 | return FALSE, otherwise, if you aren't accepting | |
296 | the drop, call gtk_drag_finish() with success == FALSE | |
297 | otherwise call gtk_drag_data_get()" */ | |
298 | ||
299 | // printf( "drop.\n" ); | |
300 | ||
301 | /* this seems to make a difference between not accepting | |
302 | due to wrong target area and due to wrong format. let | |
303 | us hope that this is not required.. */ | |
304 | ||
305 | /* inform the wxDropTarget about the current GdkDragContext. | |
306 | this is only valid for the duration of this call */ | |
307 | drop_target->SetDragContext( context ); | |
308 | ||
309 | /* inform the wxDropTarget about the current drag widget. | |
310 | this is only valid for the duration of this call */ | |
311 | drop_target->SetDragWidget( widget ); | |
312 | ||
313 | /* inform the wxDropTarget about the current drag time. | |
314 | this is only valid for the duration of this call */ | |
315 | drop_target->SetDragTime( time ); | |
316 | ||
317 | /* | |
318 | wxDragResult result = wxDragMove; | |
319 | if (context->suggested_action == GDK_ACTION_COPY) result = wxDragCopy; | |
320 | */ | |
321 | ||
322 | /* reset the block here as someone might very well | |
323 | show a dialog as a reaction to a drop and this | |
324 | wouldn't work without events */ | |
325 | g_blockEventsOnDrag = false; | |
326 | ||
327 | bool ret = drop_target->OnDrop( x, y ); | |
328 | ||
329 | if (!ret) | |
330 | { | |
331 | wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned FALSE") ); | |
332 | ||
333 | /* cancel the whole thing */ | |
334 | gtk_drag_finish( context, | |
335 | FALSE, /* no success */ | |
336 | FALSE, /* don't delete data on dropping side */ | |
337 | time ); | |
338 | } | |
339 | else | |
340 | { | |
341 | wxLogTrace(TRACE_DND, wxT( "Drop target: OnDrop returned true") ); | |
342 | ||
343 | #if wxUSE_THREADS | |
344 | /* disable GUI threads */ | |
345 | #endif | |
346 | ||
347 | GdkAtom format = drop_target->GetMatchingPair(); | |
348 | ||
349 | // this does happen somehow, see bug 555111 | |
350 | wxCHECK_MSG( format, FALSE, _T("no matching GdkAtom for format?") ); | |
351 | ||
352 | /* | |
353 | GdkDragAction action = GDK_ACTION_MOVE; | |
354 | if (result == wxDragCopy) action == GDK_ACTION_COPY; | |
355 | context->action = action; | |
356 | */ | |
357 | /* this should trigger an "drag_data_received" event */ | |
358 | gtk_drag_get_data( widget, | |
359 | context, | |
360 | format, | |
361 | time ); | |
362 | ||
363 | #if wxUSE_THREADS | |
364 | /* re-enable GUI threads */ | |
365 | #endif | |
366 | } | |
367 | ||
368 | /* after this, invalidate the drop_target's GdkDragContext */ | |
369 | drop_target->SetDragContext( (GdkDragContext*) NULL ); | |
370 | ||
371 | /* after this, invalidate the drop_target's drag widget */ | |
372 | drop_target->SetDragWidget( (GtkWidget*) NULL ); | |
373 | ||
374 | /* this has to be done because GDK has no "drag_enter" event */ | |
375 | drop_target->m_firstMotion = true; | |
376 | ||
377 | return ret; | |
378 | } | |
379 | } | |
380 | ||
381 | // ---------------------------------------------------------------------------- | |
382 | // "drag_data_received" | |
383 | // ---------------------------------------------------------------------------- | |
384 | ||
385 | extern "C" { | |
386 | static void target_drag_data_received( GtkWidget *WXUNUSED(widget), | |
387 | GdkDragContext *context, | |
388 | gint x, | |
389 | gint y, | |
390 | GtkSelectionData *data, | |
391 | guint WXUNUSED(info), | |
392 | guint time, | |
393 | wxDropTarget *drop_target ) | |
394 | { | |
395 | if (g_isIdle) wxapp_install_idle_handler(); | |
396 | ||
397 | /* Owen Taylor: "call gtk_drag_finish() with | |
398 | success == TRUE" */ | |
399 | ||
400 | if ((data->length <= 0) || (data->format != 8)) | |
401 | { | |
402 | /* negative data length and non 8-bit data format | |
403 | qualifies for junk */ | |
404 | gtk_drag_finish (context, FALSE, FALSE, time); | |
405 | ||
406 | return; | |
407 | } | |
408 | ||
409 | wxLogTrace(TRACE_DND, wxT( "Drop target: data received event") ); | |
410 | ||
411 | /* inform the wxDropTarget about the current GtkSelectionData. | |
412 | this is only valid for the duration of this call */ | |
413 | drop_target->SetDragData( data ); | |
414 | ||
415 | wxDragResult result = ConvertFromGTK(context->action); | |
416 | ||
417 | if ( wxIsDragResultOk( drop_target->OnData( x, y, result ) ) ) | |
418 | { | |
419 | wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned true") ); | |
420 | ||
421 | /* tell GTK that data transfer was successful */ | |
422 | gtk_drag_finish( context, TRUE, FALSE, time ); | |
423 | } | |
424 | else | |
425 | { | |
426 | wxLogTrace(TRACE_DND, wxT( "Drop target: OnData returned FALSE") ); | |
427 | ||
428 | /* tell GTK that data transfer was not successful */ | |
429 | gtk_drag_finish( context, FALSE, FALSE, time ); | |
430 | } | |
431 | ||
432 | /* after this, invalidate the drop_target's drag data */ | |
433 | drop_target->SetDragData( (GtkSelectionData*) NULL ); | |
434 | } | |
435 | } | |
436 | ||
437 | //---------------------------------------------------------------------------- | |
438 | // wxDropTarget | |
439 | //---------------------------------------------------------------------------- | |
440 | ||
441 | wxDropTarget::wxDropTarget( wxDataObject *data ) | |
442 | : wxDropTargetBase( data ) | |
443 | { | |
444 | m_firstMotion = true; | |
445 | m_dragContext = (GdkDragContext*) NULL; | |
446 | m_dragWidget = (GtkWidget*) NULL; | |
447 | m_dragData = (GtkSelectionData*) NULL; | |
448 | m_dragTime = 0; | |
449 | } | |
450 | ||
451 | wxDragResult wxDropTarget::OnDragOver( wxCoord WXUNUSED(x), | |
452 | wxCoord WXUNUSED(y), | |
453 | wxDragResult def ) | |
454 | { | |
455 | // GetMatchingPair() checks for m_dataObject too, no need to do it here | |
456 | ||
457 | // disable the debug message from GetMatchingPair() - there are too many | |
458 | // of them otherwise | |
459 | #ifdef __WXDEBUG__ | |
460 | wxLogNull noLog; | |
461 | #endif // Debug | |
462 | ||
463 | return (GetMatchingPair() != (GdkAtom) 0) ? def : wxDragNone; | |
464 | } | |
465 | ||
466 | bool wxDropTarget::OnDrop( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) ) | |
467 | { | |
468 | if (!m_dataObject) | |
469 | return false; | |
470 | ||
471 | return (GetMatchingPair() != (GdkAtom) 0); | |
472 | } | |
473 | ||
474 | wxDragResult wxDropTarget::OnData( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), | |
475 | wxDragResult def ) | |
476 | { | |
477 | if (!m_dataObject) | |
478 | return wxDragNone; | |
479 | ||
480 | if (GetMatchingPair() == (GdkAtom) 0) | |
481 | return wxDragNone; | |
482 | ||
483 | return GetData() ? def : wxDragNone; | |
484 | } | |
485 | ||
486 | GdkAtom wxDropTarget::GetMatchingPair() | |
487 | { | |
488 | if (!m_dataObject) | |
489 | return (GdkAtom) 0; | |
490 | ||
491 | if (!m_dragContext) | |
492 | return (GdkAtom) 0; | |
493 | ||
494 | GList *child = m_dragContext->targets; | |
495 | while (child) | |
496 | { | |
497 | GdkAtom formatAtom = (GdkAtom)(child->data); | |
498 | wxDataFormat format( formatAtom ); | |
499 | ||
500 | #ifdef __WXDEBUG__ | |
501 | wxLogTrace(TRACE_DND, wxT("Drop target: drag has format: %s"), | |
502 | format.GetId().c_str()); | |
503 | #endif // Debug | |
504 | ||
505 | if (m_dataObject->IsSupportedFormat( format )) | |
506 | return formatAtom; | |
507 | ||
508 | child = child->next; | |
509 | } | |
510 | ||
511 | return (GdkAtom) 0; | |
512 | } | |
513 | ||
514 | bool wxDropTarget::GetData() | |
515 | { | |
516 | if (!m_dragData) | |
517 | return false; | |
518 | ||
519 | if (!m_dataObject) | |
520 | return false; | |
521 | ||
522 | wxDataFormat dragFormat( m_dragData->target ); | |
523 | ||
524 | if (!m_dataObject->IsSupportedFormat( dragFormat )) | |
525 | return false; | |
526 | ||
527 | m_dataObject->SetData( dragFormat, (size_t)m_dragData->length, (const void*)m_dragData->data ); | |
528 | ||
529 | return true; | |
530 | } | |
531 | ||
532 | void wxDropTarget::UnregisterWidget( GtkWidget *widget ) | |
533 | { | |
534 | wxCHECK_RET( widget != NULL, wxT("unregister widget is NULL") ); | |
535 | ||
536 | gtk_drag_dest_unset( widget ); | |
537 | ||
538 | g_signal_handlers_disconnect_by_func (widget, | |
539 | (gpointer) target_drag_leave, this); | |
540 | g_signal_handlers_disconnect_by_func (widget, | |
541 | (gpointer) target_drag_motion, this); | |
542 | g_signal_handlers_disconnect_by_func (widget, | |
543 | (gpointer) target_drag_drop, this); | |
544 | g_signal_handlers_disconnect_by_func (widget, | |
545 | (gpointer) target_drag_data_received, this); | |
546 | } | |
547 | ||
548 | void wxDropTarget::RegisterWidget( GtkWidget *widget ) | |
549 | { | |
550 | wxCHECK_RET( widget != NULL, wxT("register widget is NULL") ); | |
551 | ||
552 | /* gtk_drag_dest_set() determines what default behaviour we'd like | |
553 | GTK to supply. we don't want to specify out targets (=formats) | |
554 | or actions in advance (i.e. not GTK_DEST_DEFAULT_MOTION and | |
555 | not GTK_DEST_DEFAULT_DROP). instead we react individually to | |
556 | "drag_motion" and "drag_drop" events. this makes it possible | |
557 | to allow dropping on only a small area. we should set | |
558 | GTK_DEST_DEFAULT_HIGHLIGHT as this will switch on the nice | |
559 | highlighting if dragging over standard controls, but this | |
560 | seems to be broken without the other two. */ | |
561 | ||
562 | gtk_drag_dest_set( widget, | |
563 | (GtkDestDefaults) 0, /* no default behaviour */ | |
564 | (GtkTargetEntry*) NULL, /* we don't supply any formats here */ | |
565 | 0, /* number of targets = 0 */ | |
566 | (GdkDragAction) 0 ); /* we don't supply any actions here */ | |
567 | ||
568 | g_signal_connect (widget, "drag_leave", | |
569 | G_CALLBACK (target_drag_leave), this); | |
570 | ||
571 | g_signal_connect (widget, "drag_motion", | |
572 | G_CALLBACK (target_drag_motion), this); | |
573 | ||
574 | g_signal_connect (widget, "drag_drop", | |
575 | G_CALLBACK (target_drag_drop), this); | |
576 | ||
577 | g_signal_connect (widget, "drag_data_received", | |
578 | G_CALLBACK (target_drag_data_received), this); | |
579 | } | |
580 | ||
581 | //---------------------------------------------------------------------------- | |
582 | // "drag_data_get" | |
583 | //---------------------------------------------------------------------------- | |
584 | ||
585 | extern "C" { | |
586 | static void | |
587 | source_drag_data_get (GtkWidget *WXUNUSED(widget), | |
588 | GdkDragContext *WXUNUSED(context), | |
589 | GtkSelectionData *selection_data, | |
590 | guint WXUNUSED(info), | |
591 | guint WXUNUSED(time), | |
592 | wxDropSource *drop_source ) | |
593 | { | |
594 | if (g_isIdle) wxapp_install_idle_handler(); | |
595 | ||
596 | wxDataFormat format( selection_data->target ); | |
597 | ||
598 | wxLogTrace(TRACE_DND, wxT("Drop source: format requested: %s"), | |
599 | format.GetId().c_str()); | |
600 | ||
601 | drop_source->m_retValue = wxDragCancel; | |
602 | ||
603 | wxDataObject *data = drop_source->GetDataObject(); | |
604 | ||
605 | if (!data) | |
606 | { | |
607 | wxLogTrace(TRACE_DND, wxT("Drop source: no data object") ); | |
608 | return; | |
609 | } | |
610 | ||
611 | if (!data->IsSupportedFormat(format)) | |
612 | { | |
613 | wxLogTrace(TRACE_DND, wxT("Drop source: unsupported format") ); | |
614 | return; | |
615 | } | |
616 | ||
617 | if (data->GetDataSize(format) == 0) | |
618 | { | |
619 | wxLogTrace(TRACE_DND, wxT("Drop source: empty data") ); | |
620 | return; | |
621 | } | |
622 | ||
623 | size_t size = data->GetDataSize(format); | |
624 | ||
625 | // printf( "data size: %d.\n", (int)data_size ); | |
626 | ||
627 | guchar *d = new guchar[size]; | |
628 | ||
629 | if (!data->GetDataHere( format, (void*)d )) | |
630 | { | |
631 | delete[] d; | |
632 | return; | |
633 | } | |
634 | ||
635 | #if wxUSE_THREADS | |
636 | /* disable GUI threads */ | |
637 | #endif | |
638 | ||
639 | gtk_selection_data_set( selection_data, | |
640 | selection_data->target, | |
641 | 8, // 8-bit | |
642 | d, | |
643 | size ); | |
644 | ||
645 | #if wxUSE_THREADS | |
646 | /* enable GUI threads */ | |
647 | #endif | |
648 | ||
649 | delete[] d; | |
650 | } | |
651 | } | |
652 | ||
653 | //---------------------------------------------------------------------------- | |
654 | // "drag_data_delete" | |
655 | //---------------------------------------------------------------------------- | |
656 | ||
657 | extern "C" { | |
658 | static void source_drag_data_delete( GtkWidget *WXUNUSED(widget), | |
659 | GdkDragContext *context, | |
660 | wxDropSource *WXUNUSED(drop_source) ) | |
661 | { | |
662 | if (g_isIdle) | |
663 | wxapp_install_idle_handler(); | |
664 | ||
665 | // printf( "Drag source: drag_data_delete\n" ); | |
666 | } | |
667 | } | |
668 | ||
669 | //---------------------------------------------------------------------------- | |
670 | // "drag_begin" | |
671 | //---------------------------------------------------------------------------- | |
672 | ||
673 | extern "C" { | |
674 | static void source_drag_begin( GtkWidget *WXUNUSED(widget), | |
675 | GdkDragContext *WXUNUSED(context), | |
676 | wxDropSource *WXUNUSED(drop_source) ) | |
677 | { | |
678 | if (g_isIdle) | |
679 | wxapp_install_idle_handler(); | |
680 | ||
681 | // printf( "Drag source: drag_begin.\n" ); | |
682 | } | |
683 | } | |
684 | ||
685 | //---------------------------------------------------------------------------- | |
686 | // "drag_end" | |
687 | //---------------------------------------------------------------------------- | |
688 | ||
689 | extern "C" { | |
690 | static void source_drag_end( GtkWidget *WXUNUSED(widget), | |
691 | GdkDragContext *WXUNUSED(context), | |
692 | wxDropSource *drop_source ) | |
693 | { | |
694 | if (g_isIdle) wxapp_install_idle_handler(); | |
695 | ||
696 | // printf( "Drag source: drag_end.\n" ); | |
697 | ||
698 | drop_source->m_waiting = false; | |
699 | } | |
700 | } | |
701 | ||
702 | //----------------------------------------------------------------------------- | |
703 | // "configure_event" from m_iconWindow | |
704 | //----------------------------------------------------------------------------- | |
705 | ||
706 | extern "C" { | |
707 | static gint | |
708 | gtk_dnd_window_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *WXUNUSED(event), wxDropSource *source ) | |
709 | { | |
710 | if (g_isIdle) | |
711 | wxapp_install_idle_handler(); | |
712 | ||
713 | source->GiveFeedback( ConvertFromGTK(source->m_dragContext->action) ); | |
714 | ||
715 | return 0; | |
716 | } | |
717 | } | |
718 | ||
719 | //--------------------------------------------------------------------------- | |
720 | // wxDropSource | |
721 | //--------------------------------------------------------------------------- | |
722 | ||
723 | wxDropSource::wxDropSource(wxWindow *win, | |
724 | const wxIcon &iconCopy, | |
725 | const wxIcon &iconMove, | |
726 | const wxIcon &iconNone) | |
727 | { | |
728 | m_waiting = true; | |
729 | ||
730 | m_iconWindow = (GtkWidget*) NULL; | |
731 | ||
732 | m_window = win; | |
733 | m_widget = win->m_widget; | |
734 | if (win->m_wxwindow) m_widget = win->m_wxwindow; | |
735 | ||
736 | m_retValue = wxDragCancel; | |
737 | ||
738 | SetIcons(iconCopy, iconMove, iconNone); | |
739 | } | |
740 | ||
741 | wxDropSource::wxDropSource(wxDataObject& data, | |
742 | wxWindow *win, | |
743 | const wxIcon &iconCopy, | |
744 | const wxIcon &iconMove, | |
745 | const wxIcon &iconNone) | |
746 | { | |
747 | m_waiting = true; | |
748 | ||
749 | SetData( data ); | |
750 | ||
751 | m_iconWindow = (GtkWidget*) NULL; | |
752 | ||
753 | m_window = win; | |
754 | m_widget = win->m_widget; | |
755 | if (win->m_wxwindow) m_widget = win->m_wxwindow; | |
756 | ||
757 | m_retValue = wxDragCancel; | |
758 | ||
759 | SetIcons(iconCopy, iconMove, iconNone); | |
760 | } | |
761 | ||
762 | void wxDropSource::SetIcons(const wxIcon &iconCopy, | |
763 | const wxIcon &iconMove, | |
764 | const wxIcon &iconNone) | |
765 | { | |
766 | m_iconCopy = iconCopy; | |
767 | m_iconMove = iconMove; | |
768 | m_iconNone = iconNone; | |
769 | ||
770 | if ( !m_iconCopy.Ok() ) | |
771 | m_iconCopy = wxIcon(page_xpm); | |
772 | if ( !m_iconMove.Ok() ) | |
773 | m_iconMove = m_iconCopy; | |
774 | if ( !m_iconNone.Ok() ) | |
775 | m_iconNone = m_iconCopy; | |
776 | } | |
777 | ||
778 | wxDropSource::~wxDropSource() | |
779 | { | |
780 | } | |
781 | ||
782 | void wxDropSource::PrepareIcon( int action, GdkDragContext *context ) | |
783 | { | |
784 | // get the right icon to display | |
785 | wxIcon *icon = NULL; | |
786 | if ( action & GDK_ACTION_MOVE ) | |
787 | icon = &m_iconMove; | |
788 | else if ( action & GDK_ACTION_COPY ) | |
789 | icon = &m_iconCopy; | |
790 | else | |
791 | icon = &m_iconNone; | |
792 | ||
793 | GdkBitmap *mask; | |
794 | if ( icon->GetMask() ) | |
795 | mask = icon->GetMask()->GetBitmap(); | |
796 | else | |
797 | mask = (GdkBitmap *)NULL; | |
798 | ||
799 | GdkPixmap *pixmap = icon->GetPixmap(); | |
800 | ||
801 | gint width,height; | |
802 | gdk_drawable_get_size (pixmap, &width, &height); | |
803 | ||
804 | GdkColormap *colormap = gtk_widget_get_colormap( m_widget ); | |
805 | gtk_widget_push_colormap (colormap); | |
806 | ||
807 | m_iconWindow = gtk_window_new (GTK_WINDOW_POPUP); | |
808 | gtk_widget_set_events (m_iconWindow, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK); | |
809 | gtk_widget_set_app_paintable (GTK_WIDGET (m_iconWindow), TRUE); | |
810 | ||
811 | gtk_widget_pop_colormap (); | |
812 | ||
813 | gtk_widget_set_size_request (m_iconWindow, width, height); | |
814 | gtk_widget_realize (m_iconWindow); | |
815 | ||
816 | g_signal_connect (m_iconWindow, "configure_event", | |
817 | G_CALLBACK (gtk_dnd_window_configure_callback), this); | |
818 | ||
819 | gdk_window_set_back_pixmap (m_iconWindow->window, pixmap, FALSE); | |
820 | ||
821 | if (mask) | |
822 | gtk_widget_shape_combine_mask (m_iconWindow, mask, 0, 0); | |
823 | ||
824 | gtk_drag_set_icon_widget( context, m_iconWindow, 0, 0 ); | |
825 | } | |
826 | ||
827 | wxDragResult wxDropSource::DoDragDrop(int flags) | |
828 | { | |
829 | wxCHECK_MSG( m_data && m_data->GetFormatCount(), wxDragNone, | |
830 | wxT("Drop source: no data") ); | |
831 | ||
832 | // still in drag | |
833 | if (g_blockEventsOnDrag) | |
834 | return wxDragNone; | |
835 | ||
836 | // disabled for now | |
837 | g_blockEventsOnDrag = true; | |
838 | ||
839 | RegisterWindow(); | |
840 | ||
841 | m_waiting = true; | |
842 | ||
843 | GtkTargetList *target_list = gtk_target_list_new( (GtkTargetEntry*) NULL, 0 ); | |
844 | ||
845 | wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ]; | |
846 | m_data->GetAllFormats( array ); | |
847 | size_t count = m_data->GetFormatCount(); | |
848 | for (size_t i = 0; i < count; i++) | |
849 | { | |
850 | GdkAtom atom = array[i]; | |
851 | wxLogTrace(TRACE_DND, wxT("Drop source: Supported atom %s"), gdk_atom_name( atom )); | |
852 | gtk_target_list_add( target_list, atom, 0, 0 ); | |
853 | } | |
854 | delete[] array; | |
855 | ||
856 | GdkEventMotion event; | |
857 | event.window = m_widget->window; | |
858 | int x = 0; | |
859 | int y = 0; | |
860 | GdkModifierType state; | |
861 | gdk_window_get_pointer( event.window, &x, &y, &state ); | |
862 | event.x = x; | |
863 | event.y = y; | |
864 | event.state = state; | |
865 | event.time = (guint32)GDK_CURRENT_TIME; | |
866 | ||
867 | /* GTK wants to know which button was pressed which caused the dragging */ | |
868 | int button_number = 0; | |
869 | if (event.state & GDK_BUTTON1_MASK) button_number = 1; | |
870 | else if (event.state & GDK_BUTTON2_MASK) button_number = 2; | |
871 | else if (event.state & GDK_BUTTON3_MASK) button_number = 3; | |
872 | ||
873 | #if wxUSE_THREADS | |
874 | /* disable GUI threads */ | |
875 | #endif | |
876 | ||
877 | /* don't start dragging if no button is down */ | |
878 | if (button_number) | |
879 | { | |
880 | int action = GDK_ACTION_COPY; | |
881 | if ( flags & wxDrag_AllowMove ) | |
882 | action |= GDK_ACTION_MOVE; | |
883 | ||
884 | // VZ: as we already use g_blockEventsOnDrag it shouldn't be that bad | |
885 | // to use a global to pass the flags to the drop target but I'd | |
886 | // surely prefer a better way to do it | |
887 | gs_flagsForDrag = flags; | |
888 | ||
889 | GdkDragContext *context = gtk_drag_begin( m_widget, | |
890 | target_list, | |
891 | (GdkDragAction)action, | |
892 | button_number, /* number of mouse button which started drag */ | |
893 | (GdkEvent*) &event ); | |
894 | ||
895 | m_dragContext = context; | |
896 | ||
897 | PrepareIcon( action, context ); | |
898 | ||
899 | while (m_waiting) | |
900 | gtk_main_iteration(); | |
901 | ||
902 | m_retValue = ConvertFromGTK(context->action); | |
903 | if ( m_retValue == wxDragNone ) | |
904 | m_retValue = wxDragCancel; | |
905 | } | |
906 | ||
907 | #if wxUSE_THREADS | |
908 | /* re-enable GUI threads */ | |
909 | #endif | |
910 | ||
911 | g_blockEventsOnDrag = false; | |
912 | ||
913 | UnregisterWindow(); | |
914 | ||
915 | return m_retValue; | |
916 | } | |
917 | ||
918 | void wxDropSource::RegisterWindow() | |
919 | { | |
920 | if (!m_widget) return; | |
921 | ||
922 | g_signal_connect (m_widget, "drag_data_get", | |
923 | G_CALLBACK (source_drag_data_get), this); | |
924 | g_signal_connect (m_widget, "drag_data_delete", | |
925 | G_CALLBACK (source_drag_data_delete), this); | |
926 | g_signal_connect (m_widget, "drag_begin", | |
927 | G_CALLBACK (source_drag_begin), this); | |
928 | g_signal_connect (m_widget, "drag_end", | |
929 | G_CALLBACK (source_drag_end), this); | |
930 | ||
931 | } | |
932 | ||
933 | void wxDropSource::UnregisterWindow() | |
934 | { | |
935 | if (!m_widget) | |
936 | return; | |
937 | ||
938 | g_signal_handlers_disconnect_by_func (m_widget, | |
939 | (gpointer) source_drag_data_get, | |
940 | this); | |
941 | g_signal_handlers_disconnect_by_func (m_widget, | |
942 | (gpointer) source_drag_data_delete, | |
943 | this); | |
944 | g_signal_handlers_disconnect_by_func (m_widget, | |
945 | (gpointer) source_drag_begin, | |
946 | this); | |
947 | g_signal_handlers_disconnect_by_func (m_widget, | |
948 | (gpointer) source_drag_end, | |
949 | this); | |
950 | } | |
951 | ||
952 | #endif | |
953 | // wxUSE_DRAG_AND_DROP |