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