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