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_column(NULL),
m_pos(-1,-1),
m_isDraggable(false),
m_dragDataSize(-1)
m_dataObject(NULL)
{ }
wxDataViewEvent(const wxDataViewEvent& event)
@@ -756,8 +755,7 @@ public:
m_value(event.m_value),
m_column(event.m_column),
m_pos(m_pos),
m_isDraggable(event.m_isDraggable),
m_dragDataSize(event.m_dragDataSize)
m_dataObject(event.m_dataObject)
{ }
wxDataViewItem GetItem() const { return m_item; }
@@ -780,15 +778,9 @@ public:
wxPoint GetPosition() const { return m_pos; }
void SetPosition( int x, int y ) { m_pos.x = x; m_pos.y = y; }
// For Drag operations
bool IsDraggable() const { return m_isDraggable; }
void SetDraggable( bool can_drag = true ) { m_isDraggable = can_drag; }
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; }
// For DnD operations
void SetDataObject( wxDataObject *obj ) { m_dataObject = obj; }
wxDataObject *GetDataObject() { return m_dataObject; }
virtual wxEvent *Clone() const { return new wxDataViewEvent(*this); }
@@ -799,11 +791,7 @@ protected:
wxVariant m_value;
wxDataViewColumn *m_column;
wxPoint m_pos;
bool m_isDraggable;
int m_dragDataSize;
void *m_dragDataBuffer;
wxDataFormat m_dataFormat;
wxDataObject *m_dataObject;
private:
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_REORDERED, wxDataViewEvent )
wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_COMMAND_DATAVIEW_ITEM_DRAGGABLE, 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 )
wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, 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_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_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)
#define EVT_DATAVIEW_ITEM_BEGIN_DRAG(id, fn) wx__DECLARE_DATAVIEWEVT(ITEM_BEGIN_DRAG, id, fn)
#ifdef wxHAS_GENERIC_DATAVIEWCTRL
// 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.
@event{EVT_DATAVIEW_COLUMN_REORDERED(id, func)}
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
@library{wxadv}
@@ -2197,14 +2199,11 @@ public:
class wxDataViewEvent : public wxNotifyEvent
{
public:
//@{
/**
Constructor. Typically used by wxWidgets internals only.
*/
wxDataViewEvent(wxEventType commandType = wxEVT_NULL,
int winid = 0);
wxDataViewEvent(const wxDataViewEvent& event);
//@}
/**
Returns the position of the column in the control or -1
@@ -2252,5 +2251,15 @@ public:
Sets the value associated with this event.
*/
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 OnAddMany( wxCommandEvent &event);
// DnD
void OnDraggable( wxDataViewEvent &event );
void OnGetDragDataSize( wxDataViewEvent &event );
void OnGetDragData( wxDataViewEvent &event );
void OnBeginDrag( wxDataViewEvent &event );
private:
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_DRAGGABLE( ID_MUSIC_CTRL, MyFrame::OnDraggable )
EVT_DATAVIEW_ITEM_GET_DRAG_DATA_SIZE( ID_MUSIC_CTRL, MyFrame::OnGetDragDataSize )
EVT_DATAVIEW_ITEM_GET_DRAG_DATA( ID_MUSIC_CTRL, MyFrame::OnGetDragData )
EVT_DATAVIEW_ITEM_BEGIN_DRAG( ID_MUSIC_CTRL, MyFrame::OnBeginDrag )
EVT_RIGHT_UP(MyFrame::OnRightClick)
END_EVENT_TABLE()
@@ -1160,41 +1155,19 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
wxAboutBox(info);
}
void MyFrame::OnDraggable( wxDataViewEvent &event )
void MyFrame::OnBeginDrag( wxDataViewEvent &event )
{
wxDataViewItem item( event.GetItem() );
// only allow drags for item, not containers
event.SetDraggable( !m_music_model->IsContainer( event.GetItem() ) );
}
void MyFrame::OnGetDragDataSize( wxDataViewEvent &event )
if (m_music_model->IsContainer( item ) )
{
if (event.GetDataFormat() == wxDF_TEXT)
{
wxDataViewItem item( event.GetItem() );
MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
wxTextDataObject obj;
obj.SetText( node->m_artist );
size_t size = obj.GetDataSize( wxDF_TEXT );
event.SetDragDataSize( size );
event.Veto();
return;
}
event.Skip();
}
void MyFrame::OnGetDragData( wxDataViewEvent &event )
{
if (event.GetDataFormat() == wxDF_TEXT)
{
wxDataViewItem item( event.GetItem() );
MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
wxTextDataObject obj;
obj.SetText( node->m_artist );
obj.GetDataHere( wxDF_TEXT, event.GetDragDataBuffer() );
return;
}
event.Skip();
wxTextDataObject *obj = new wxTextDataObject;
obj->SetText( node->m_title );
event.SetDataObject( obj );
}

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_REORDERED, wxDataViewEvent )
wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_DRAGGABLE, wxDataViewEvent )
wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_GET_DRAG_DATA_SIZE, wxDataViewEvent )
wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_GET_DRAG_DATA, wxDataViewEvent )
wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, wxDataViewEvent )
// -------------------------------------
// wxDataViewSpinRenderer

View File

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