-// drag request
-
-void gtk_drag_callback( GtkWidget *widget, GdkEvent *event, wxDragSource *drag )
-{
- printf( "OnDragRequest.\n" );
-
- gtk_widget_dnd_data_set( widget, event, drag->m_data, drag->m_size );
-};
-
-wxDragSource::wxDragSource( wxWindow *win )
-{
- g_blockEventsOnDrag = TRUE;
-
- m_window = win;
- m_widget = win->m_widget;
- if (win->m_wxwindow) m_widget = win->m_wxwindow;
-
- m_data = NULL;
- m_size = 0;
-
- m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY );
- m_goaheadCursor = wxCursor( wxCURSOR_HAND );
-};
-
-wxDragSource::~wxDragSource(void)
-{
- g_blockEventsOnDrag = FALSE;
-};
-
-void wxDragSource::SetData( char *data, const long size )
-{
- m_size = size;
- m_data = data;
-};
-
-void wxDragSource::Start( int x, int y )
-{
- if (gdk_dnd.dnd_grabbed) return;
- if (gdk_dnd.drag_really) return;
- if (m_size == 0) return;
- if (!m_data) return;
-
- GdkWindowPrivate *wp = (GdkWindowPrivate*) m_widget->window;
-
- RegisterWindow();
- ConnectWindow();
-
- gdk_dnd.drag_perhaps = TRUE;
-
- gdk_dnd.dnd_drag_start.x = 5;
- gdk_dnd.dnd_drag_start.y = 5;
- gdk_dnd.real_sw = wp;
-
- if (gdk_dnd.drag_startwindows)
- {
- g_free( gdk_dnd.drag_startwindows );
- gdk_dnd.drag_startwindows = NULL;
- };
- gdk_dnd.drag_numwindows = gdk_dnd.drag_really = 0;
-
- XWindowAttributes dnd_winattr;
- XGetWindowAttributes( gdk_display, wp->xwindow, &dnd_winattr );
- wp->dnd_drag_savedeventmask = dnd_winattr.your_event_mask;
-
- gdk_dnd_drag_addwindow( m_widget->window );
-
- GdkEventDragBegin ev;
- ev.type = GDK_DRAG_BEGIN;
- ev.window = m_widget->window;
- ev.u.allflags = 0;
- ev.u.flags.protocol_version = DND_PROTOCOL_VERSION;
-
- gdk_event_put( (GdkEvent*)&ev );
-
- XGrabPointer( gdk_display, wp->xwindow, False,
- ButtonMotionMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
- GrabModeAsync, GrabModeAsync, gdk_root_window, None, CurrentTime );
-
- gdk_dnd_set_drag_cursors( m_defaultCursor.GetCursor(), m_goaheadCursor.GetCursor() );
-
- gdk_dnd.dnd_grabbed = TRUE;
- gdk_dnd.drag_really = 1;
- gdk_dnd_display_drag_cursor( x, y, FALSE, TRUE );
-
- while (gdk_dnd.drag_really || gdk_dnd.drag_perhaps) wxYield();
-
- UnconnectWindow();
- UnregisterWindow();
-};
-
-void wxDragSource::ConnectWindow(void)
-{
- gtk_signal_connect( GTK_OBJECT(m_widget), "drag_request_event",
- GTK_SIGNAL_FUNC(gtk_drag_callback), (gpointer)this );
-};
-
-void wxDragSource::UnconnectWindow(void)
-{
- if (!m_widget) return;
-
- gtk_signal_disconnect_by_data( GTK_OBJECT(m_widget), (gpointer)this );
-};
-
-void wxDragSource::UnregisterWindow(void)
-{
- if (!m_widget) return;
-
- gtk_widget_dnd_drag_set( m_widget, FALSE, NULL, 0 );
-};
-
-//-------------------------------------------------------------------------
-// wxTextDragSource
-//-------------------------------------------------------------------------
-
-void wxTextDragSource::SetTextData( const wxString &text )
-{
- m_tmp = text;
- SetData( WXSTRINGCAST(m_tmp), m_tmp.Length()+1 );
-};
-
-void wxTextDragSource::RegisterWindow(void)
-{
- if (!m_widget) return;
-
- char *accepted_drop_types[] = { "text/plain" };
- gtk_widget_dnd_drag_set( m_widget, TRUE, accepted_drop_types, 1 );
-};
+// "configure_event" from m_iconWindow
+//-----------------------------------------------------------------------------
+
+static gint
+gtk_dnd_window_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *WXUNUSED(event), wxDropSource *source )
+{
+ if (g_isIdle)
+ wxapp_install_idle_handler();
+
+ wxDragResult action = wxDragNone;
+ if (source->m_dragContext->action == GDK_ACTION_COPY) action = wxDragCopy;
+ if (source->m_dragContext->action == GDK_ACTION_MOVE) action = wxDragMove;
+
+ source->GiveFeedback( action );
+
+ return 0;
+}
+
+//---------------------------------------------------------------------------
+// wxDropSource
+//---------------------------------------------------------------------------
+
+wxDropSource::wxDropSource( wxWindow *win, const wxIcon &icon )
+{
+ g_blockEventsOnDrag = TRUE;
+ m_waiting = TRUE;
+
+ m_iconWindow = (GtkWidget*) NULL;
+
+ m_window = win;
+ m_widget = win->m_widget;
+ if (win->m_wxwindow) m_widget = win->m_wxwindow;
+
+ m_retValue = wxDragCancel;
+
+ m_icon = icon;
+ if (wxNullIcon == icon) m_icon = wxIcon( page_xpm );
+}
+
+wxDropSource::wxDropSource( wxDataObject& data, wxWindow *win, const wxIcon &icon )
+{
+ m_waiting = TRUE;
+
+ SetData( data );
+
+ m_iconWindow = (GtkWidget*) NULL;
+
+ m_window = win;
+ m_widget = win->m_widget;
+ if (win->m_wxwindow) m_widget = win->m_wxwindow;
+
+ m_retValue = wxDragCancel;
+
+ m_icon = icon;
+ if (wxNullIcon == icon) m_icon = wxIcon( page_xpm );
+}
+
+wxDropSource::~wxDropSource()
+{
+ g_blockEventsOnDrag = FALSE;
+}
+
+void wxDropSource::PrepareIcon( int hot_x, int hot_y, GdkDragContext *context )
+{
+ GdkBitmap *mask = (GdkBitmap *) NULL;
+ if (m_icon.GetMask()) mask = m_icon.GetMask()->GetBitmap();
+ GdkPixmap *pixmap = m_icon.GetPixmap();
+
+ gint width,height;
+ gdk_window_get_size (pixmap, &width, &height);
+
+ GdkColormap *colormap = gtk_widget_get_colormap( m_widget );
+ gtk_widget_push_visual (gdk_colormap_get_visual (colormap));
+ gtk_widget_push_colormap (colormap);
+
+ m_iconWindow = gtk_window_new (GTK_WINDOW_POPUP);
+ gtk_widget_set_events (m_iconWindow, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
+ gtk_widget_set_app_paintable (GTK_WIDGET (m_iconWindow), TRUE);
+
+ gtk_widget_pop_visual ();
+ gtk_widget_pop_colormap ();
+
+ gtk_widget_set_usize (m_iconWindow, width, height);
+ gtk_widget_realize (m_iconWindow);
+
+ gtk_signal_connect( GTK_OBJECT(m_iconWindow), "configure_event",
+ GTK_SIGNAL_FUNC(gtk_dnd_window_configure_callback), (gpointer)this );
+
+ gdk_window_set_back_pixmap (m_iconWindow->window, pixmap, FALSE);
+
+ if (mask)
+ gtk_widget_shape_combine_mask (m_iconWindow, mask, 0, 0);
+
+ gtk_drag_set_icon_widget( context, m_iconWindow, hot_x, hot_y );
+}
+
+wxDragResult wxDropSource::DoDragDrop( bool allowMove )
+{
+ wxASSERT_MSG( m_data, wxT("Drop source: no data") );
+
+ if (!m_data)
+ return (wxDragResult) wxDragNone;
+
+ if (m_data->GetFormatCount() == 0)
+ return (wxDragResult) wxDragNone;
+
+ g_blockEventsOnDrag = TRUE;
+
+ RegisterWindow();
+
+ m_waiting = TRUE;
+
+ GtkTargetList *target_list = gtk_target_list_new( (GtkTargetEntry*) NULL, 0 );
+
+ wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
+ m_data->GetAllFormats( array );
+ for (size_t i = 0; i < m_data->GetFormatCount(); i++)
+ {
+ GdkAtom atom = array[i];
+ wxLogDebug( wxT("Drop source: Supported atom %s"), gdk_atom_name( atom ) );
+ gtk_target_list_add( target_list, atom, 0, 0 );
+ }
+ delete[] array;
+
+ GdkEventMotion event;
+ event.window = m_widget->window;
+ int x = 0;
+ int y = 0;
+ GdkModifierType state;
+ gdk_window_get_pointer( event.window, &x, &y, &state );
+ event.x = x;
+ event.y = y;
+ event.state = state;
+ event.time = (guint32)GDK_CURRENT_TIME;
+
+ /* GTK wants to know which button was pressed which caused the dragging */
+ int button_number = 0;
+ if (event.state & GDK_BUTTON1_MASK) button_number = 1;
+ else if (event.state & GDK_BUTTON2_MASK) button_number = 2;
+ else if (event.state & GDK_BUTTON3_MASK) button_number = 3;
+
+#if wxUSE_THREADS
+ /* disable GUI threads */
+ wxapp_uninstall_thread_wakeup();
+#endif
+
+ /* don't start dragging if no button is down */
+ if (button_number)
+ {
+ GdkDragAction action = GDK_ACTION_COPY;
+ if (allowMove) action = (GdkDragAction)(GDK_ACTION_MOVE|GDK_ACTION_COPY);
+ GdkDragContext *context = gtk_drag_begin( m_widget,
+ target_list,
+ action,
+ button_number, /* number of mouse button which started drag */
+ (GdkEvent*) &event );
+
+ m_dragContext = context;
+
+ PrepareIcon( 0, 0, context );
+
+ while (m_waiting) gtk_main_iteration();;
+
+ if (context->action == GDK_ACTION_COPY)
+ return m_retValue = wxDragCopy;
+ if (context->action == GDK_ACTION_MOVE)
+ return m_retValue = wxDragMove;
+ }
+
+#if wxUSE_THREADS
+ /* re-enable GUI threads */
+ wxapp_install_thread_wakeup();
+#endif
+
+ g_blockEventsOnDrag = FALSE;
+
+ UnregisterWindow();
+
+ return m_retValue;
+}
+
+void wxDropSource::RegisterWindow()
+{
+ if (!m_widget) return;
+
+ gtk_signal_connect( GTK_OBJECT(m_widget), "drag_data_get",
+ GTK_SIGNAL_FUNC (source_drag_data_get), (gpointer) this);
+ gtk_signal_connect (GTK_OBJECT(m_widget), "drag_data_delete",
+ GTK_SIGNAL_FUNC (source_drag_data_delete), (gpointer) this );
+ gtk_signal_connect (GTK_OBJECT(m_widget), "drag_begin",
+ GTK_SIGNAL_FUNC (source_drag_begin), (gpointer) this );
+ gtk_signal_connect (GTK_OBJECT(m_widget), "drag_end",
+ GTK_SIGNAL_FUNC (source_drag_end), (gpointer) this );
+
+}
+
+void wxDropSource::UnregisterWindow()
+{
+ if (!m_widget) return;
+
+ gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
+ GTK_SIGNAL_FUNC(source_drag_data_get), (gpointer) this );
+ gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
+ GTK_SIGNAL_FUNC(source_drag_data_delete), (gpointer) this );
+ gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
+ GTK_SIGNAL_FUNC(source_drag_begin), (gpointer) this );
+ gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
+ GTK_SIGNAL_FUNC(source_drag_end), (gpointer) this );
+}
+
+#endif