Simplified event based Drag API for wxDataViewCtrl

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@58238 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robert Roebling
2009-01-20 11:58:52 +00:00
parent 8c33ee2549
commit 591cc82deb
5 changed files with 68 additions and 86 deletions

View File

@@ -744,8 +744,7 @@ public:
m_value(wxNullVariant), m_value(wxNullVariant),
m_column(NULL), m_column(NULL),
m_pos(-1,-1), m_pos(-1,-1),
m_isDraggable(false), m_dataObject(NULL)
m_dragDataSize(-1)
{ } { }
wxDataViewEvent(const wxDataViewEvent& event) wxDataViewEvent(const wxDataViewEvent& event)
@@ -756,8 +755,7 @@ public:
m_value(event.m_value), m_value(event.m_value),
m_column(event.m_column), m_column(event.m_column),
m_pos(m_pos), m_pos(m_pos),
m_isDraggable(event.m_isDraggable), m_dataObject(event.m_dataObject)
m_dragDataSize(event.m_dragDataSize)
{ } { }
wxDataViewItem GetItem() const { return m_item; } wxDataViewItem GetItem() const { return m_item; }
@@ -780,15 +778,9 @@ public:
wxPoint GetPosition() const { return m_pos; } wxPoint GetPosition() const { return m_pos; }
void SetPosition( int x, int y ) { m_pos.x = x; m_pos.y = y; } void SetPosition( int x, int y ) { m_pos.x = x; m_pos.y = y; }
// For Drag operations // For DnD operations
bool IsDraggable() const { return m_isDraggable; } void SetDataObject( wxDataObject *obj ) { m_dataObject = obj; }
void SetDraggable( bool can_drag = true ) { m_isDraggable = can_drag; } wxDataObject *GetDataObject() { return m_dataObject; }
int GetDragDataSize() const { return m_dragDataSize; }
void SetDragDataSize( int size ) { m_dragDataSize = size; }
void* GetDragDataBuffer() const { return m_dragDataBuffer; }
void SetDragDataBuffer( void *buffer ) { m_dragDataBuffer = buffer; }
wxDataFormat GetDataFormat() const { return m_dataFormat; }
void SetDataFormat( const wxDataFormat &format ) { m_dataFormat = format; }
virtual wxEvent *Clone() const { return new wxDataViewEvent(*this); } virtual wxEvent *Clone() const { return new wxDataViewEvent(*this); }
@@ -799,11 +791,7 @@ protected:
wxVariant m_value; wxVariant m_value;
wxDataViewColumn *m_column; wxDataViewColumn *m_column;
wxPoint m_pos; wxPoint m_pos;
wxDataObject *m_dataObject;
bool m_isDraggable;
int m_dragDataSize;
void *m_dragDataBuffer;
wxDataFormat m_dataFormat;
private: private:
DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxDataViewEvent) DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxDataViewEvent)
@@ -827,9 +815,7 @@ wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_
wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, wxDataViewEvent ) wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, wxDataViewEvent )
wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_COMMAND_DATAVIEW_COLUMN_REORDERED, wxDataViewEvent ) wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_COMMAND_DATAVIEW_COLUMN_REORDERED, wxDataViewEvent )
wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_COMMAND_DATAVIEW_ITEM_DRAGGABLE, wxDataViewEvent ) wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, wxDataViewEvent )
wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_COMMAND_DATAVIEW_ITEM_GET_DRAG_DATA_SIZE, wxDataViewEvent )
wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_COMMAND_DATAVIEW_ITEM_GET_DRAG_DATA, wxDataViewEvent )
typedef void (wxEvtHandler::*wxDataViewEventFunction)(wxDataViewEvent&); typedef void (wxEvtHandler::*wxDataViewEventFunction)(wxDataViewEvent&);
@@ -857,9 +843,7 @@ typedef void (wxEvtHandler::*wxDataViewEventFunction)(wxDataViewEvent&);
#define EVT_DATAVIEW_COLUMN_SORTED(id, fn) wx__DECLARE_DATAVIEWEVT(COLUMN_SORTED, id, fn) #define EVT_DATAVIEW_COLUMN_SORTED(id, fn) wx__DECLARE_DATAVIEWEVT(COLUMN_SORTED, id, fn)
#define EVT_DATAVIEW_COLUMN_REORDERED(id, fn) wx__DECLARE_DATAVIEWEVT(COLUMN_REORDERED, id, fn) #define EVT_DATAVIEW_COLUMN_REORDERED(id, fn) wx__DECLARE_DATAVIEWEVT(COLUMN_REORDERED, id, fn)
#define EVT_DATAVIEW_ITEM_DRAGGABLE(id, fn) wx__DECLARE_DATAVIEWEVT(ITEM_DRAGGABLE, id, fn) #define EVT_DATAVIEW_ITEM_BEGIN_DRAG(id, fn) wx__DECLARE_DATAVIEWEVT(ITEM_BEGIN_DRAG, id, fn)
#define EVT_DATAVIEW_ITEM_GET_DRAG_DATA_SIZE(id, fn) wx__DECLARE_DATAVIEWEVT(ITEM_GET_DRAG_DATA_SIZE, id, fn)
#define EVT_DATAVIEW_ITEM_GET_DRAG_DATA(id, fn) wx__DECLARE_DATAVIEWEVT(ITEM_GET_DRAG_DATA, id, fn)
#ifdef wxHAS_GENERIC_DATAVIEWCTRL #ifdef wxHAS_GENERIC_DATAVIEWCTRL
// this symbol doesn't follow the convention for wxUSE_XXX symbols which // this symbol doesn't follow the convention for wxUSE_XXX symbols which

View File

@@ -554,6 +554,8 @@ public:
Process a wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED event. Process a wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED event.
@event{EVT_DATAVIEW_COLUMN_REORDERED(id, func)} @event{EVT_DATAVIEW_COLUMN_REORDERED(id, func)}
Process a wxEVT_COMMAND_DATAVIEW_COLUMN_REORDERED event. Process a wxEVT_COMMAND_DATAVIEW_COLUMN_REORDERED event.
@event{EVT_DATAVIEW_ITEM_BEGIN_DRAG(id, func)}
Process a wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG event.
@endEventTable @endEventTable
@library{wxadv} @library{wxadv}
@@ -2197,14 +2199,11 @@ public:
class wxDataViewEvent : public wxNotifyEvent class wxDataViewEvent : public wxNotifyEvent
{ {
public: public:
//@{
/** /**
Constructor. Typically used by wxWidgets internals only. Constructor. Typically used by wxWidgets internals only.
*/ */
wxDataViewEvent(wxEventType commandType = wxEVT_NULL, wxDataViewEvent(wxEventType commandType = wxEVT_NULL,
int winid = 0); int winid = 0);
wxDataViewEvent(const wxDataViewEvent& event);
//@}
/** /**
Returns the position of the column in the control or -1 Returns the position of the column in the control or -1
@@ -2252,5 +2251,15 @@ public:
Sets the value associated with this event. Sets the value associated with this event.
*/ */
void SetValue(const wxVariant& value); void SetValue(const wxVariant& value);
/**
Set wxDataObject for Drag'n'drop data transfer.
*/
void SetDataObject( wxDataObject *obj );
/**
Gets associated wxDataObject for Drag'n'drop data transfer.
*/
wxDataObject *GetDataObject();
}; };

View File

@@ -669,10 +669,7 @@ public:
void OnGoto( wxCommandEvent &event); void OnGoto( wxCommandEvent &event);
void OnAddMany( wxCommandEvent &event); void OnAddMany( wxCommandEvent &event);
// DnD void OnBeginDrag( wxDataViewEvent &event );
void OnDraggable( wxDataViewEvent &event );
void OnGetDragDataSize( wxDataViewEvent &event );
void OnGetDragData( wxDataViewEvent &event );
private: private:
wxDataViewCtrl* m_musicCtrl; wxDataViewCtrl* m_musicCtrl;
@@ -769,9 +766,7 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL, MyFrame::OnContextMenu) EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL, MyFrame::OnContextMenu)
EVT_DATAVIEW_ITEM_DRAGGABLE( ID_MUSIC_CTRL, MyFrame::OnDraggable ) EVT_DATAVIEW_ITEM_BEGIN_DRAG( ID_MUSIC_CTRL, MyFrame::OnBeginDrag )
EVT_DATAVIEW_ITEM_GET_DRAG_DATA_SIZE( ID_MUSIC_CTRL, MyFrame::OnGetDragDataSize )
EVT_DATAVIEW_ITEM_GET_DRAG_DATA( ID_MUSIC_CTRL, MyFrame::OnGetDragData )
EVT_RIGHT_UP(MyFrame::OnRightClick) EVT_RIGHT_UP(MyFrame::OnRightClick)
END_EVENT_TABLE() END_EVENT_TABLE()
@@ -1160,41 +1155,19 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
wxAboutBox(info); wxAboutBox(info);
} }
void MyFrame::OnDraggable( wxDataViewEvent &event ) void MyFrame::OnBeginDrag( wxDataViewEvent &event )
{ {
wxDataViewItem item( event.GetItem() );
// only allow drags for item, not containers // only allow drags for item, not containers
event.SetDraggable( !m_music_model->IsContainer( event.GetItem() ) ); if (m_music_model->IsContainer( item ) )
}
void MyFrame::OnGetDragDataSize( wxDataViewEvent &event )
{
if (event.GetDataFormat() == wxDF_TEXT)
{ {
wxDataViewItem item( event.GetItem() ); event.Veto();
MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
wxTextDataObject obj;
obj.SetText( node->m_artist );
size_t size = obj.GetDataSize( wxDF_TEXT );
event.SetDragDataSize( size );
return; return;
} }
event.Skip();
}
void MyFrame::OnGetDragData( wxDataViewEvent &event )
{
if (event.GetDataFormat() == wxDF_TEXT)
{
wxDataViewItem item( event.GetItem() );
MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
wxTextDataObject *obj = new wxTextDataObject;
wxTextDataObject obj; obj->SetText( node->m_title );
obj.SetText( node->m_artist ); event.SetDataObject( obj );
obj.GetDataHere( wxDF_TEXT, event.GetDragDataBuffer() );
return;
}
event.Skip();
} }

View File

@@ -1235,9 +1235,7 @@ wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, wxDataViewEven
wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, wxDataViewEvent ) wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, wxDataViewEvent )
wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_REORDERED, wxDataViewEvent ) wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_REORDERED, wxDataViewEvent )
wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_DRAGGABLE, wxDataViewEvent ) wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, wxDataViewEvent )
wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_GET_DRAG_DATA_SIZE, wxDataViewEvent )
wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_GET_DRAG_DATA, wxDataViewEvent )
// ------------------------------------- // -------------------------------------
// wxDataViewSpinRenderer // wxDataViewSpinRenderer

View File

@@ -129,6 +129,8 @@ private:
int m_sort_column; int m_sort_column;
GtkTargetEntry m_dragSourceTargetEntry; GtkTargetEntry m_dragSourceTargetEntry;
wxCharBuffer m_dragSourceTargetEntryTarget; wxCharBuffer m_dragSourceTargetEntryTarget;
wxDataObject *m_dragDataObject;
wxDataObject *m_dropDataObject;
}; };
@@ -2878,6 +2880,9 @@ wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl *owner,
m_sort_column = -1; m_sort_column = -1;
m_dataview_sort_column = NULL; m_dataview_sort_column = NULL;
m_dragDataObject = NULL;
m_dropDataObject = NULL;
if (!m_wx_model->IsVirtualListModel()) if (!m_wx_model->IsVirtualListModel())
InitTree(); InitTree();
} }
@@ -2885,6 +2890,9 @@ wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl *owner,
wxDataViewCtrlInternal::~wxDataViewCtrlInternal() wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
{ {
g_object_unref( m_gtk_model ); g_object_unref( m_gtk_model );
delete m_dragDataObject;
delete m_dropDataObject;
} }
void wxDataViewCtrlInternal::InitTree() void wxDataViewCtrlInternal::InitTree()
@@ -2936,16 +2944,28 @@ bool wxDataViewCtrlInternal::EnableDragSource( const wxDataFormat &format )
gboolean wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource *WXUNUSED(drag_source), gboolean wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource *WXUNUSED(drag_source),
GtkTreePath *path ) GtkTreePath *path )
{ {
delete m_dragDataObject;
GtkTreeIter iter; GtkTreeIter iter;
if (!get_iter( &iter, path )) return FALSE; if (!get_iter( &iter, path )) return FALSE;
wxDataViewItem item( (void*) iter.user_data ); wxDataViewItem item( (void*) iter.user_data );
wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_DRAGGABLE, m_owner->GetId() ); wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() );
event.SetItem( item ); event.SetItem( item );
event.SetModel( m_wx_model ); event.SetModel( m_wx_model );
m_owner->HandleWindowEvent( event ); if (!m_owner->HandleWindowEvent( event ))
return FALSE;
return event.IsDraggable(); if (!event.IsAllowed())
return FALSE;
wxDataObject *obj = event.GetDataObject();
if (!obj)
return FALSE;
m_dragDataObject = obj;
return TRUE;
} }
gboolean gboolean
@@ -2962,27 +2982,25 @@ gboolean wxDataViewCtrlInternal::drag_data_get( GtkTreeDragSource *WXUNUSED(drag
if (!get_iter( &iter, path )) return FALSE; if (!get_iter( &iter, path )) return FALSE;
wxDataViewItem item( (void*) iter.user_data ); wxDataViewItem item( (void*) iter.user_data );
wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_GET_DRAG_DATA_SIZE, m_owner->GetId() ); if (!m_dragDataObject->IsSupported( selection_data->target ))
event.SetItem( item ); return FALSE;
event.SetModel( m_wx_model );
event.SetDataFormat( selection_data->target );
m_owner->HandleWindowEvent( event );
if (event.GetDragDataSize() < 1) return FALSE;
size_t size = (size_t) event.GetDragDataSize();
void *data = malloc( size ); size_t size = m_dragDataObject->GetDataSize( selection_data->target );
if (size == 0)
return FALSE;
void *buf = malloc( size );
event.SetEventType( wxEVT_COMMAND_DATAVIEW_ITEM_GET_DRAG_DATA );
event.SetDragDataBuffer( data );
gboolean res = FALSE; gboolean res = FALSE;
if (m_owner->HandleWindowEvent( event )) if (m_dragDataObject->GetDataHere( selection_data->target, buf ))
{ {
gtk_selection_data_set( selection_data, selection_data->target,
8, (const guchar*) data, size );
res = TRUE; res = TRUE;
gtk_selection_data_set( selection_data, selection_data->target,
8, (const guchar*) buf, size );
} }
free( data ); free( buf );
return res; return res;
} }