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