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