From 97c79de27c368de18afab3ca655bc48a4d53fc30 Mon Sep 17 00:00:00 2001 From: Robert Roebling Date: Thu, 21 Oct 1999 13:09:06 +0000 Subject: [PATCH] More DnD. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@4100 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/clipbrd.h | 9 ----- include/wx/dnd.h | 6 ++-- include/wx/gtk/dnd.h | 33 ++++++++++++++++-- include/wx/gtk1/dnd.h | 33 ++++++++++++++++-- setup.h.in | 5 ++- src/common/appcmn.cpp | 11 ------ src/common/clipcmn.cpp | 5 ++- src/gtk/dnd.cpp | 79 +++++++++++++++++++++++++++++++++++------- src/gtk1/dnd.cpp | 79 +++++++++++++++++++++++++++++++++++------- 9 files changed, 206 insertions(+), 54 deletions(-) diff --git a/include/wx/clipbrd.h b/include/wx/clipbrd.h index 7f79269aed..47f0af66b5 100644 --- a/include/wx/clipbrd.h +++ b/include/wx/clipbrd.h @@ -73,15 +73,6 @@ public: // X11 has two clipboards which get selected by this call. Empty on MSW. virtual void UsePrimarySelection( bool WXUNUSED(primary) = FALSE ) { } -#ifdef WXWIN_COMPATIBILITY_2 - // deprecated version - bool GetData(wxDataObject *data) - { - wxCHECK_MSG(data, FALSE, wxT("NULL pointer in wxClipboard")); - - return GetData(*data); - } -#endif // WXWIN_COMPATIBILITY_2 }; // ---------------------------------------------------------------------------- diff --git a/include/wx/dnd.h b/include/wx/dnd.h index 82fb690739..3bee4fc7c3 100644 --- a/include/wx/dnd.h +++ b/include/wx/dnd.h @@ -45,8 +45,10 @@ public: // set the data which is transfered by drag and drop void SetData(wxDataObject& data) - { if (m_data) delete m_data; - m_data = &data; } + { m_data = &data; } + + wxDataObject *GetDataObject() + { return m_data; } // start drag action, see enum wxDragResult for return value description // diff --git a/include/wx/gtk/dnd.h b/include/wx/gtk/dnd.h index edc321382d..aa45735342 100644 --- a/include/wx/gtk/dnd.h +++ b/include/wx/gtk/dnd.h @@ -33,7 +33,6 @@ class wxWindow; class wxDropTarget; class wxTextDropTarget; class wxFileDropTarget; -class wxPrivateDropTarget; class wxDropSource; @@ -70,6 +69,37 @@ public: void SetDragTime( guint time ) { m_dragTime = time; } }; +// ---------------------------------------------------------------------------- +// A simple wxDropTarget derived class for text data: you only need to +// override OnDropText() to get something working +// ---------------------------------------------------------------------------- + +class wxTextDropTarget : public wxDropTarget +{ +public: + wxTextDropTarget(); + + virtual bool OnDropText(wxCoord x, wxCoord y, const wxString& text) = 0; + + virtual bool OnData(wxCoord x, wxCoord y); +}; + +// ---------------------------------------------------------------------------- +// A drop target which accepts files (dragged from File Manager or Explorer) +// ---------------------------------------------------------------------------- + +class wxFileDropTarget : public wxDropTarget +{ +public: + wxFileDropTarget(); + + // parameters are the number of files and the array of file names + virtual bool OnDropFiles(wxCoord x, wxCoord y, + const wxArrayString& filenames) = 0; + + virtual bool OnData(wxCoord x, wxCoord y); +}; + //------------------------------------------------------------------------- // wxDropSource //------------------------------------------------------------------------- @@ -100,7 +130,6 @@ public: GtkWidget *m_widget; wxWindow *m_window; wxDragResult m_retValue; - wxDataObject *m_data; wxCursor m_defaultCursor; wxCursor m_goaheadCursor; diff --git a/include/wx/gtk1/dnd.h b/include/wx/gtk1/dnd.h index edc321382d..aa45735342 100644 --- a/include/wx/gtk1/dnd.h +++ b/include/wx/gtk1/dnd.h @@ -33,7 +33,6 @@ class wxWindow; class wxDropTarget; class wxTextDropTarget; class wxFileDropTarget; -class wxPrivateDropTarget; class wxDropSource; @@ -70,6 +69,37 @@ public: void SetDragTime( guint time ) { m_dragTime = time; } }; +// ---------------------------------------------------------------------------- +// A simple wxDropTarget derived class for text data: you only need to +// override OnDropText() to get something working +// ---------------------------------------------------------------------------- + +class wxTextDropTarget : public wxDropTarget +{ +public: + wxTextDropTarget(); + + virtual bool OnDropText(wxCoord x, wxCoord y, const wxString& text) = 0; + + virtual bool OnData(wxCoord x, wxCoord y); +}; + +// ---------------------------------------------------------------------------- +// A drop target which accepts files (dragged from File Manager or Explorer) +// ---------------------------------------------------------------------------- + +class wxFileDropTarget : public wxDropTarget +{ +public: + wxFileDropTarget(); + + // parameters are the number of files and the array of file names + virtual bool OnDropFiles(wxCoord x, wxCoord y, + const wxArrayString& filenames) = 0; + + virtual bool OnData(wxCoord x, wxCoord y); +}; + //------------------------------------------------------------------------- // wxDropSource //------------------------------------------------------------------------- @@ -100,7 +130,6 @@ public: GtkWidget *m_widget; wxWindow *m_window; wxDragResult m_retValue; - wxDataObject *m_data; wxCursor m_defaultCursor; wxCursor m_goaheadCursor; diff --git a/setup.h.in b/setup.h.in index bee58e8a1c..cdcdbb7b0d 100644 --- a/setup.h.in +++ b/setup.h.in @@ -479,7 +479,10 @@ * Use Apple Ieee-double converter */ #define wxUSE_APPLE_IEEE 0 - +/* + * Compatibility with 2.0 API. + */ +#define WXWIN_COMPATIBILITY_2 1 /* * Enables debugging: memory tracing, assert, etc., contains debug level */ diff --git a/src/common/appcmn.cpp b/src/common/appcmn.cpp index 8cb28408e7..7cf023dc9f 100644 --- a/src/common/appcmn.cpp +++ b/src/common/appcmn.cpp @@ -33,17 +33,6 @@ #endif #include "wx/thread.h" -#include "wx/clipbrd.h" - -// =========================================================================== -// implementation -// =========================================================================== - -// ---------------------------------------------------------------------------- -// some global data defined here -// ---------------------------------------------------------------------------- - -IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule, wxModule) // --------------------------------------------------------------------------- // wxAppBase diff --git a/src/common/clipcmn.cpp b/src/common/clipcmn.cpp index 7dc2ef2030..94dbdd07ef 100644 --- a/src/common/clipcmn.cpp +++ b/src/common/clipcmn.cpp @@ -42,12 +42,13 @@ wxClipboardBase::wxClipboardBase() // wxClipboardModule: module responsible for initializing the global clipboard // object // -// NB: IMPLEMENT_DYNAMIC_CLASS() for it is in common/appcmn.cpp // ---------------------------------------------------------------------------- class wxClipboardModule : public wxModule { public: + wxClipboardModule() { } + bool OnInit() { wxTheClipboard = new wxClipboard; return TRUE; } void OnExit() @@ -57,3 +58,5 @@ private: DECLARE_DYNAMIC_CLASS(wxClipboardModule) }; +IMPLEMENT_DYNAMIC_CLASS(wxClipboardModule,wxModule) + diff --git a/src/gtk/dnd.cpp b/src/gtk/dnd.cpp index f59b86d330..a22731b164 100644 --- a/src/gtk/dnd.cpp +++ b/src/gtk/dnd.cpp @@ -256,6 +256,8 @@ static gboolean target_drag_drop( GtkWidget *widget, if (!ret) { + wxLogDebug( wxT( "Drop target: OnDrop returned TRUE") ); + /* cancel the whole thing */ gtk_drag_finish( context, FALSE, /* no success */ @@ -264,6 +266,8 @@ static gboolean target_drag_drop( GtkWidget *widget, } else { + wxLogDebug( wxT( "Drop target: OnDrop returned TRUE") ); + #if wxUSE_THREADS /* disable GUI threads */ wxapp_uninstall_thread_wakeup(); @@ -324,7 +328,7 @@ static void target_drag_data_received( GtkWidget *WXUNUSED(widget), return; } - wxLogDebug( wxT( "Drop target: data received") ); + wxLogDebug( wxT( "Drop target: data received event") ); /* inform the wxDropTarget about the current GtkSelectionData. this is only valid for the duration of this call */ @@ -332,11 +336,15 @@ static void target_drag_data_received( GtkWidget *WXUNUSED(widget), if (drop_target->OnData( x, y )) { + wxLogDebug( wxT( "Drop target: OnData returned TRUE") ); + /* tell GTK that data transfer was successfull */ gtk_drag_finish( context, TRUE, FALSE, time ); } else { + wxLogDebug( wxT( "Drop target: OnData returned FALSE") ); + /* tell GTK that data transfer was not successfull */ gtk_drag_finish( context, FALSE, FALSE, time ); } @@ -448,7 +456,9 @@ bool wxDropTarget::GetData() return TRUE; } - return FALSE; + m_dataObject->SetData( dragFormat, (size_t)m_dragData->length, (const void*)m_dragData->data ); + + return TRUE; } void wxDropTarget::UnregisterWidget( GtkWidget *widget ) @@ -503,6 +513,41 @@ void wxDropTarget::RegisterWidget( GtkWidget *widget ) GTK_SIGNAL_FUNC(target_drag_data_received), (gpointer) this ); } +// ---------------------------------------------------------------------------- +// wxTextDropTarget +// ---------------------------------------------------------------------------- + +wxTextDropTarget::wxTextDropTarget() + : wxDropTarget(new wxTextDataObject) +{ +} + +bool wxTextDropTarget::OnData(wxCoord x, wxCoord y) +{ + if ( !GetData() ) + return FALSE; + + return OnDropText(x, y, ((wxTextDataObject *)m_dataObject)->GetText()); +} + +// ---------------------------------------------------------------------------- +// wxFileDropTarget +// ---------------------------------------------------------------------------- + +wxFileDropTarget::wxFileDropTarget() + : wxDropTarget(new wxFileDataObject) +{ +} + +bool wxFileDropTarget::OnData(wxCoord x, wxCoord y) +{ + if ( !GetData() ) + return FALSE; + + return OnDropFiles(x, y, + ((wxFileDataObject *)m_dataObject)->GetFilenames()); +} + //---------------------------------------------------------------------------- // "drag_data_get" //---------------------------------------------------------------------------- @@ -517,33 +562,41 @@ source_drag_data_get (GtkWidget *WXUNUSED(widget), { if (g_isIdle) wxapp_install_idle_handler(); -#ifdef __WXDEBUG__ - char *name = gdk_atom_name( selection_data->target ); - if (name) wxLogDebug( wxT("Drop source: format requested: %s"), name ); -#endif + wxDataFormat format( selection_data->target ); + + wxLogDebug( wxT("Drop source: format requested: %s"), format.GetId().c_str() ); drop_source->m_retValue = wxDragCancel; - wxDataObject *data = drop_source->m_data; + wxDataObject *data = drop_source->GetDataObject(); if (!data) + { + wxLogDebug( wxT("Drop source: no data object") ); return; + } - if (!data->IsSupportedFormat(selection_data->target)) + if (!data->IsSupportedFormat(format)) + { + wxLogDebug( wxT("Drop source: unsupported format") ); return; + } - if (data->GetDataSize(selection_data->target) == 0) + if (data->GetDataSize(format) == 0) + { + wxLogDebug( wxT("Drop source: empty data") ); return; + } - size_t size = data->GetDataSize(selection_data->target); + size_t size = data->GetDataSize(format); // printf( "data size: %d.\n", (int)data_size ); guchar *d = new guchar[size]; - if (!data->GetDataHere( selection_data->target, (void*)d )) + if (!data->GetDataHere( format, (void*)d )) { - free( d ); + delete[] d; return; } @@ -563,7 +616,7 @@ source_drag_data_get (GtkWidget *WXUNUSED(widget), wxapp_install_thread_wakeup(); #endif - free( d ); + delete[] d; /* so far only copy, no moves. TODO. */ drop_source->m_retValue = wxDragCopy; diff --git a/src/gtk1/dnd.cpp b/src/gtk1/dnd.cpp index f59b86d330..a22731b164 100644 --- a/src/gtk1/dnd.cpp +++ b/src/gtk1/dnd.cpp @@ -256,6 +256,8 @@ static gboolean target_drag_drop( GtkWidget *widget, if (!ret) { + wxLogDebug( wxT( "Drop target: OnDrop returned TRUE") ); + /* cancel the whole thing */ gtk_drag_finish( context, FALSE, /* no success */ @@ -264,6 +266,8 @@ static gboolean target_drag_drop( GtkWidget *widget, } else { + wxLogDebug( wxT( "Drop target: OnDrop returned TRUE") ); + #if wxUSE_THREADS /* disable GUI threads */ wxapp_uninstall_thread_wakeup(); @@ -324,7 +328,7 @@ static void target_drag_data_received( GtkWidget *WXUNUSED(widget), return; } - wxLogDebug( wxT( "Drop target: data received") ); + wxLogDebug( wxT( "Drop target: data received event") ); /* inform the wxDropTarget about the current GtkSelectionData. this is only valid for the duration of this call */ @@ -332,11 +336,15 @@ static void target_drag_data_received( GtkWidget *WXUNUSED(widget), if (drop_target->OnData( x, y )) { + wxLogDebug( wxT( "Drop target: OnData returned TRUE") ); + /* tell GTK that data transfer was successfull */ gtk_drag_finish( context, TRUE, FALSE, time ); } else { + wxLogDebug( wxT( "Drop target: OnData returned FALSE") ); + /* tell GTK that data transfer was not successfull */ gtk_drag_finish( context, FALSE, FALSE, time ); } @@ -448,7 +456,9 @@ bool wxDropTarget::GetData() return TRUE; } - return FALSE; + m_dataObject->SetData( dragFormat, (size_t)m_dragData->length, (const void*)m_dragData->data ); + + return TRUE; } void wxDropTarget::UnregisterWidget( GtkWidget *widget ) @@ -503,6 +513,41 @@ void wxDropTarget::RegisterWidget( GtkWidget *widget ) GTK_SIGNAL_FUNC(target_drag_data_received), (gpointer) this ); } +// ---------------------------------------------------------------------------- +// wxTextDropTarget +// ---------------------------------------------------------------------------- + +wxTextDropTarget::wxTextDropTarget() + : wxDropTarget(new wxTextDataObject) +{ +} + +bool wxTextDropTarget::OnData(wxCoord x, wxCoord y) +{ + if ( !GetData() ) + return FALSE; + + return OnDropText(x, y, ((wxTextDataObject *)m_dataObject)->GetText()); +} + +// ---------------------------------------------------------------------------- +// wxFileDropTarget +// ---------------------------------------------------------------------------- + +wxFileDropTarget::wxFileDropTarget() + : wxDropTarget(new wxFileDataObject) +{ +} + +bool wxFileDropTarget::OnData(wxCoord x, wxCoord y) +{ + if ( !GetData() ) + return FALSE; + + return OnDropFiles(x, y, + ((wxFileDataObject *)m_dataObject)->GetFilenames()); +} + //---------------------------------------------------------------------------- // "drag_data_get" //---------------------------------------------------------------------------- @@ -517,33 +562,41 @@ source_drag_data_get (GtkWidget *WXUNUSED(widget), { if (g_isIdle) wxapp_install_idle_handler(); -#ifdef __WXDEBUG__ - char *name = gdk_atom_name( selection_data->target ); - if (name) wxLogDebug( wxT("Drop source: format requested: %s"), name ); -#endif + wxDataFormat format( selection_data->target ); + + wxLogDebug( wxT("Drop source: format requested: %s"), format.GetId().c_str() ); drop_source->m_retValue = wxDragCancel; - wxDataObject *data = drop_source->m_data; + wxDataObject *data = drop_source->GetDataObject(); if (!data) + { + wxLogDebug( wxT("Drop source: no data object") ); return; + } - if (!data->IsSupportedFormat(selection_data->target)) + if (!data->IsSupportedFormat(format)) + { + wxLogDebug( wxT("Drop source: unsupported format") ); return; + } - if (data->GetDataSize(selection_data->target) == 0) + if (data->GetDataSize(format) == 0) + { + wxLogDebug( wxT("Drop source: empty data") ); return; + } - size_t size = data->GetDataSize(selection_data->target); + size_t size = data->GetDataSize(format); // printf( "data size: %d.\n", (int)data_size ); guchar *d = new guchar[size]; - if (!data->GetDataHere( selection_data->target, (void*)d )) + if (!data->GetDataHere( format, (void*)d )) { - free( d ); + delete[] d; return; } @@ -563,7 +616,7 @@ source_drag_data_get (GtkWidget *WXUNUSED(widget), wxapp_install_thread_wakeup(); #endif - free( d ); + delete[] d; /* so far only copy, no moves. TODO. */ drop_source->m_retValue = wxDragCopy; -- 2.47.2