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