Fixed some wxDragImage bugs

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@9711 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Julian Smart 2001-04-10 21:01:40 +00:00
parent 1af9e5c56d
commit aa2d25a57a
6 changed files with 184 additions and 87 deletions

View File

@ -92,7 +92,7 @@ have a mask.}
\docparam{cursor}{Optional cursor to combine with the image.}
\docparam{hotspot}{Optional position of the hotspot in the given cursor. This parameter is deprecated.}
\docparam{hotspot}{This parameter is deprecated.}
\docparam{treeCtrl}{Tree control for constructing a tree drag image.}
@ -183,8 +183,7 @@ Call this to move the image to a new position. The image will only be shown if
\helpref{wxDragImage::Show}{wxdragimageshow} has been called previously (for example
at the start of the drag).
{\it pt} is the position in window coordinates (or screen coordinates if no
window was specified to BeginDrag.
{\it pt} is the position in client coordinates (relative to the window specified in BeginDrag).
You can move the image either when the image is hidden or shown, but in general dragging
will be smoother if you move the image when it is shown.

View File

@ -97,35 +97,71 @@ public:
// Ctors & dtor
////////////////////////////////////////////////////////////////////////////
wxGenericDragImage(const wxCursor& cursor = wxNullCursor, const wxPoint& hotspot = wxPoint(0, 0))
wxGenericDragImage(const wxCursor& cursor = wxNullCursor)
{
Init();
Create(cursor, hotspot);
Create(cursor);
}
wxGenericDragImage(const wxBitmap& image, const wxCursor& cursor = wxNullCursor, const wxPoint& hotspot = wxPoint(0, 0))
// Deprecated version of the above
wxGenericDragImage(const wxCursor& cursor, const wxPoint& cursorHotspot)
{
Init();
Create(cursor, cursorHotspot);
}
wxGenericDragImage(const wxBitmap& image, const wxCursor& cursor = wxNullCursor)
{
Init();
Create(image, cursor, hotspot);
Create(image, cursor);
}
wxGenericDragImage(const wxIcon& image, const wxCursor& cursor = wxNullCursor, const wxPoint& hotspot = wxPoint(0, 0))
// Deprecated version of the above
wxGenericDragImage(const wxBitmap& image, const wxCursor& cursor, const wxPoint& cursorHotspot)
{
Init();
Create(image, cursor, hotspot);
Create(image, cursor, cursorHotspot);
}
wxGenericDragImage(const wxString& str, const wxCursor& cursor = wxNullCursor, const wxPoint& hotspot = wxPoint(0, 0))
wxGenericDragImage(const wxIcon& image, const wxCursor& cursor = wxNullCursor)
{
Init();
Create(str, cursor, hotspot);
Create(image, cursor);
}
// Deprecated version of the above
wxGenericDragImage(const wxIcon& image, const wxCursor& cursor, const wxPoint& cursorHotspot)
{
Init();
Create(image, cursor, cursorHotspot);
}
wxGenericDragImage(const wxString& str, const wxCursor& cursor = wxNullCursor)
{
Init();
Create(str, cursor);
}
// Deprecated version of the above
wxGenericDragImage(const wxString& str, const wxCursor& cursor, const wxPoint& cursorHotspot)
{
Init();
Create(str, cursor, cursorHotspot);
}
wxGenericDragImage(const wxTreeCtrl& treeCtrl, wxTreeItemId& id)
{
Init();
Create(treeCtrl, id);
}
wxGenericDragImage(const wxListCtrl& listCtrl, long id)
{
Init();
@ -145,16 +181,36 @@ public:
////////////////////////////////////////////////////////////////////////////
// Create a drag image with a virtual image (need to override DoDrawImage, GetImageRect)
bool Create(const wxCursor& cursor = wxNullCursor, const wxPoint& hotspot = wxPoint(0, 0));
bool Create(const wxCursor& cursor = wxNullCursor);
bool Create(const wxCursor& cursor, const wxPoint& WXUNUSED(cursorHotspot))
{
wxLogDebug(wxT("wxDragImage::Create: use of a cursor hotspot is now deprecated. Please omit this argument."));
return Create(cursor);
}
// Create a drag image from a bitmap and optional cursor
bool Create(const wxBitmap& image, const wxCursor& cursor = wxNullCursor, const wxPoint& hotspot = wxPoint(0, 0));
bool Create(const wxBitmap& image, const wxCursor& cursor = wxNullCursor);
bool Create(const wxBitmap& image, const wxCursor& cursor, const wxPoint& cursorHotspot)
{
wxLogDebug(wxT("wxDragImage::Create: use of a cursor hotspot is now deprecated. Please omit this argument."));
return Create(image, cursor);
}
// Create a drag image from an icon and optional cursor
bool Create(const wxIcon& image, const wxCursor& cursor = wxNullCursor, const wxPoint& hotspot = wxPoint(0, 0));
bool Create(const wxIcon& image, const wxCursor& cursor = wxNullCursor);
bool Create(const wxIcon& image, const wxCursor& cursor, const wxPoint& cursorHotspot)
{
wxLogDebug(wxT("wxDragImage::Create: use of a cursor hotspot is now deprecated. Please omit this argument."));
return Create(image, cursor);
}
// Create a drag image from a string and optional cursor
bool Create(const wxString& str, const wxCursor& cursor = wxNullCursor, const wxPoint& hotspot = wxPoint(0, 0));
bool Create(const wxString& str, const wxCursor& cursor = wxNullCursor);
bool Create(const wxString& str, const wxCursor& cursor, const wxPoint& cursorHotspot)
{
wxLogDebug(wxT("wxDragImage::Create: use of a cursor hotspot is now deprecated. Please omit this argument."));
return Create(str, cursor);
}
// Create a drag image for the given tree control item
bool Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id);
@ -211,7 +267,7 @@ protected:
wxIcon m_icon;
wxCursor m_cursor;
wxCursor m_oldCursor;
wxPoint m_hotspot;
// wxPoint m_hotspot;
wxPoint m_offset; // The hostpot value passed to BeginDrag
wxPoint m_position;
bool m_isDirty;

View File

@ -23,9 +23,8 @@
#include "wx/treectrl.h"
#include "wx/listctrl.h"
// If 1, use a simple wxCursor instead of ImageList_SetDragCursorImage,
// and some other simplifications
#define wxUSE_SIMPLER_DRAGIMAGE 1
// If 1, use a simple wxCursor instead of ImageList_SetDragCursorImage
#define wxUSE_SIMPLER_DRAGIMAGE 0
/*
To use this class, create a wxDragImage when you start dragging, for example:
@ -110,36 +109,65 @@ public:
////////////////////////////////////////////////////////////////////////////
wxDragImage();
wxDragImage(const wxBitmap& image, const wxCursor& cursor = wxNullCursor, const wxPoint& cursorHotspot = wxPoint(0, 0))
wxDragImage(const wxBitmap& image, const wxCursor& cursor = wxNullCursor)
{
Init();
Create(image, cursor);
}
// Deprecated form of the above
wxDragImage(const wxBitmap& image, const wxCursor& cursor, const wxPoint& cursorHotspot)
{
Init();
Create(image, cursor, cursorHotspot);
}
wxDragImage(const wxIcon& image, const wxCursor& cursor = wxNullCursor, const wxPoint& cursorHotspot = wxPoint(0, 0))
wxDragImage(const wxIcon& image, const wxCursor& cursor = wxNullCursor)
{
Init();
Create(image, cursor);
}
// Deprecated form of the above
wxDragImage(const wxIcon& image, const wxCursor& cursor, const wxPoint& cursorHotspot)
{
Init();
Create(image, cursor, cursorHotspot);
}
wxDragImage(const wxString& str, const wxCursor& cursor = wxNullCursor, const wxPoint& cursorHotspot = wxPoint(0, 0))
wxDragImage(const wxString& str, const wxCursor& cursor = wxNullCursor)
{
Init();
Create(str, cursor);
}
// Deprecated form of the above
wxDragImage(const wxString& str, const wxCursor& cursor, const wxPoint& cursorHotspot)
{
Init();
Create(str, cursor, cursorHotspot);
}
wxDragImage(const wxTreeCtrl& treeCtrl, wxTreeItemId& id)
{
Init();
Create(treeCtrl, id);
}
wxDragImage(const wxListCtrl& listCtrl, long id)
{
Init();
Create(listCtrl, id);
}
~wxDragImage();
// Attributes
@ -149,13 +177,28 @@ public:
////////////////////////////////////////////////////////////////////////////
// Create a drag image from a bitmap and optional cursor
bool Create(const wxBitmap& image, const wxCursor& cursor = wxNullCursor, const wxPoint& cursorHotspot = wxPoint(0, 0));
bool Create(const wxBitmap& image, const wxCursor& cursor = wxNullCursor);
bool Create(const wxBitmap& image, const wxCursor& cursor, const wxPoint& WXUNUSED(cursorHotspot))
{
wxLogDebug(wxT("wxDragImage::Create: use of a cursor hotspot is now deprecated. Please omit this argument."));
return Create(image, cursor);
}
// Create a drag image from an icon and optional cursor
bool Create(const wxIcon& image, const wxCursor& cursor = wxNullCursor, const wxPoint& cursorHotspot = wxPoint(0, 0));
bool Create(const wxIcon& image, const wxCursor& cursor = wxNullCursor);
bool Create(const wxIcon& image, const wxCursor& cursor, const wxPoint& WXUNUSED(cursorHotspot))
{
wxLogDebug(wxT("wxDragImage::Create: use of a cursor hotspot is now deprecated. Please omit this argument."));
return Create(image, cursor);
}
// Create a drag image from a string and optional cursor
bool Create(const wxString& str, const wxCursor& cursor = wxNullCursor, const wxPoint& cursorHotspot = wxPoint(0, 0));
bool Create(const wxString& str, const wxCursor& cursor = wxNullCursor);
bool Create(const wxString& str, const wxCursor& cursor, const wxPoint& WXUNUSED(cursorHotspot))
{
wxLogDebug(wxT("wxDragImage::Create: use of a cursor hotspot is now deprecated. Please omit this argument."));
return Create(str, cursor);
}
// Create a drag image for the given tree control item
bool Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id);
@ -209,7 +252,7 @@ protected:
#endif
wxCursor m_cursor;
wxPoint m_cursorHotspot;
// wxPoint m_cursorHotspot; // Obsolete
wxPoint m_position;
wxWindow* m_window;
wxRect m_boundingRect;

View File

@ -25,7 +25,7 @@
// Under Windows, change this to 1
// to use wxGenericDragImage
#define wxUSE_GENERIC_DRAGIMAGE 0
#define wxUSE_GENERIC_DRAGIMAGE 1
#if wxUSE_GENERIC_DRAGIMAGE
#include "wx/generic/dragimgg.h"
@ -131,10 +131,8 @@ void MyCanvas::OnMouseEvent(wxMouseEvent& event)
if (!m_draggedShape || !m_dragImage)
return;
wxPoint newPos(m_draggedShape->GetPosition().x + (event.GetPosition().x - m_dragStartPos.x),
m_draggedShape->GetPosition().y + (event.GetPosition().y - m_dragStartPos.y));
m_draggedShape->SetPosition(newPos);
m_draggedShape->SetPosition(m_draggedShape->GetPosition()
+ event.GetPosition() - m_dragStartPos);
m_dragImage->Hide();
m_dragImage->EndDrag();
@ -165,9 +163,6 @@ void MyCanvas::OnMouseEvent(wxMouseEvent& event)
if (dx <= tolerance && dy <= tolerance)
return;
wxPoint newPos(m_draggedShape->GetPosition().x + (event.GetPosition().x - m_dragStartPos.x),
m_draggedShape->GetPosition().y + (event.GetPosition().y - m_dragStartPos.y));
// Start the drag.
m_dragMode = TEST_DRAG_DRAGGING;
@ -184,20 +179,16 @@ void MyCanvas::OnMouseEvent(wxMouseEvent& event)
{
case SHAPE_DRAG_BITMAP:
{
wxPoint hotSpot(event.GetPosition().x - newPos.x, event.GetPosition().y - newPos.y);
m_dragImage = new wxDragImage(m_draggedShape->GetBitmap(), wxCursor(wxCURSOR_HAND), hotSpot);
m_dragImage = new wxDragImage(m_draggedShape->GetBitmap(), wxCursor(wxCURSOR_HAND));
break;
}
case SHAPE_DRAG_TEXT:
{
wxPoint hotSpot(event.GetPosition().x - newPos.x, event.GetPosition().y - newPos.y);
m_dragImage = new wxDragImage("Dragging some test text", wxCursor(wxCURSOR_HAND), hotSpot);
m_dragImage = new wxDragImage("Dragging some test text", wxCursor(wxCURSOR_HAND));
break;
}
case SHAPE_DRAG_ICON:
{
wxPoint hotSpot(event.GetPosition().x - newPos.x, event.GetPosition().y - newPos.y);
// Can anyone explain why this test is necessary,
// to prevent a gcc error?
#ifdef __WXMOTIF__
@ -206,36 +197,33 @@ void MyCanvas::OnMouseEvent(wxMouseEvent& event)
wxIcon icon(wxICON(dragicon));
#endif
m_dragImage = new wxDragImage(icon, wxCursor(wxCURSOR_HAND), hotSpot);
m_dragImage = new wxDragImage(icon, wxCursor(wxCURSOR_HAND));
break;
}
}
bool fullScreen = FALSE;
if (wxGetApp().GetUseScreen())
{
newPos = ClientToScreen(newPos);
fullScreen = TRUE;
}
bool fullScreen = wxGetApp().GetUseScreen();
bool retValue;
// The offset between the top-left of the shape image and the current shape position
wxPoint beginDragHotSpot = m_dragStartPos - m_draggedShape->GetPosition();
if (fullScreen)
// This line uses the whole screen...
retValue = m_dragImage->BeginDrag(wxPoint(0, 0), this, TRUE);
// while this line restricts dragging to the parent frame.
// retValue = m_dragImage->BeginDrag(wxPoint(0, 0), this, GetParent());
else
retValue = m_dragImage->BeginDrag(wxPoint(0, 0), this);
// Now we do this inside the implementation: always assume
// coordinates relative to the capture window (client coordinates)
if (!retValue)
//if (fullScreen)
// beginDragHotSpot -= ClientToScreen(wxPoint(0, 0));
if (!m_dragImage->BeginDrag(beginDragHotSpot, this, fullScreen))
{
delete m_dragImage;
m_dragImage = (wxDragImage*) NULL;
m_dragMode = TEST_DRAG_NONE;
} else
{
m_dragImage->Move(event.GetPosition());
m_dragImage->Show();
}
m_dragImage->Move(newPos);
m_dragImage->Show();
}
else if (m_dragMode == TEST_DRAG_DRAGGING)
{
@ -272,16 +260,8 @@ void MyCanvas::OnMouseEvent(wxMouseEvent& event)
m_currentlyHighlighted->Draw(clientDC, wxINVERT);
}
wxPoint newPos(m_draggedShape->GetPosition().x + (event.GetPosition().x - m_dragStartPos.x),
m_draggedShape->GetPosition().y + (event.GetPosition().y - m_dragStartPos.y));
if (wxGetApp().GetUseScreen())
{
newPos = ClientToScreen(newPos);
}
// Move and show the image again
m_dragImage->Move(newPos);
m_dragImage->Move(event.GetPosition());
if (mustUnhighlightOld || mustHighlightNew)
m_dragImage->Show();

View File

@ -94,42 +94,39 @@ void wxGenericDragImage::Init()
////////////////////////////////////////////////////////////////////////////
// Create a drag image with a virtual image (need to override DoDrawImage, GetImageRect)
bool wxGenericDragImage::Create(const wxCursor& cursor, const wxPoint& hotspot)
bool wxGenericDragImage::Create(const wxCursor& cursor)
{
m_cursor = cursor;
m_hotspot = hotspot;
return TRUE;
}
// Create a drag image from a bitmap and optional cursor
bool wxGenericDragImage::Create(const wxBitmap& image, const wxCursor& cursor, const wxPoint& hotspot)
bool wxGenericDragImage::Create(const wxBitmap& image, const wxCursor& cursor)
{
// We don't have to combine the cursor explicitly since we simply show the cursor
// as we drag. This currently will only work within one window.
m_cursor = cursor;
m_hotspot = hotspot;
m_bitmap = image;
return TRUE ;
}
// Create a drag image from an icon and optional cursor
bool wxGenericDragImage::Create(const wxIcon& image, const wxCursor& cursor, const wxPoint& hotspot)
bool wxGenericDragImage::Create(const wxIcon& image, const wxCursor& cursor)
{
// We don't have to combine the cursor explicitly since we simply show the cursor
// as we drag. This currently will only work within one window.
m_cursor = cursor;
m_hotspot = hotspot;
m_icon = image;
return TRUE ;
}
// Create a drag image from a string and optional cursor
bool wxGenericDragImage::Create(const wxString& str, const wxCursor& cursor, const wxPoint& hotspot)
bool wxGenericDragImage::Create(const wxString& str, const wxCursor& cursor)
{
wxFont font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
@ -170,7 +167,7 @@ bool wxGenericDragImage::Create(const wxString& str, const wxCursor& cursor, con
bitmap = image.ConvertToBitmap();
#endif
return Create(bitmap, cursor, hotspot);
return Create(bitmap, cursor);
}
// Create a drag image for the given tree control item
@ -319,15 +316,19 @@ bool wxGenericDragImage::Move(const wxPoint& pt)
{
wxASSERT_MSG( (m_windowDC != (wxDC*) NULL), wxT("No window DC in wxGenericDragImage::Move()") );
wxPoint pt2(pt);
if (m_fullScreen)
pt2 = m_window->ClientToScreen(pt);
// Erase at old position, then show at the current position
wxPoint oldPos = m_position;
bool eraseOldImage = (m_isDirty && m_isShown);
if (m_isShown)
RedrawImage(oldPos - m_offset, pt - m_offset, eraseOldImage, TRUE);
RedrawImage(oldPos - m_offset, pt2 - m_offset, eraseOldImage, TRUE);
m_position = pt;
m_position = pt2;
if (m_isShown)
m_isDirty = TRUE;

View File

@ -102,7 +102,7 @@ void wxDragImage::Init()
////////////////////////////////////////////////////////////////////////////
// Create a drag image from a bitmap and optional cursor
bool wxDragImage::Create(const wxBitmap& image, const wxCursor& cursor, const wxPoint& cursorHotspot)
bool wxDragImage::Create(const wxBitmap& image, const wxCursor& cursor)
{
if ( m_hImageList )
ImageList_Destroy(GetHimageList());
@ -146,13 +146,12 @@ bool wxDragImage::Create(const wxBitmap& image, const wxCursor& cursor, const wx
wxLogError(_("Couldn't add an image to the image list."));
}
m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
m_cursorHotspot = cursorHotspot;
return (index != -1) ;
}
// Create a drag image from an icon and optional cursor
bool wxDragImage::Create(const wxIcon& image, const wxCursor& cursor, const wxPoint& cursorHotspot)
bool wxDragImage::Create(const wxIcon& image, const wxCursor& cursor)
{
if ( m_hImageList )
ImageList_Destroy(GetHimageList());
@ -184,13 +183,12 @@ bool wxDragImage::Create(const wxIcon& image, const wxCursor& cursor, const wxPo
}
m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
m_cursorHotspot = cursorHotspot;
return (index != -1) ;
}
// Create a drag image from a string and optional cursor
bool wxDragImage::Create(const wxString& str, const wxCursor& cursor, const wxPoint& cursorHotspot)
bool wxDragImage::Create(const wxString& str, const wxCursor& cursor)
{
wxFont font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
@ -226,7 +224,7 @@ bool wxDragImage::Create(const wxString& str, const wxCursor& cursor, const wxPo
image.SetMaskColour(255, 255, 255);
bitmap = image.ConvertToBitmap();
return Create(bitmap, cursor, cursorHotspot);
return Create(bitmap, cursor);
}
// Create a drag image for the given tree control item
@ -260,6 +258,7 @@ bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, bool fullS
m_boundingRect = * rect;
bool ret = (ImageList_BeginDrag(GetHimageList(), 0, hotspot.x, hotspot.y) != 0);
//bool ret = (ImageList_BeginDrag(GetHimageList(), 0, 0, 0) != 0);
if (!ret)
{
@ -282,6 +281,21 @@ bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, bool fullS
m_hCursorImageList = (WXHIMAGELIST) ImageList_Create(cxCursor, cyCursor, ILC_MASK, 1, 1);
}
// See if we can find the cursor hotspot
wxPoint curHotSpot(hotspot);
#if 0
ICONINFO iconInfo;
if (::GetIconInfo((HICON) (HCURSOR) m_cursor.GetHCURSOR(), & iconInfo) != 0)
{
curHotSpot.x -= iconInfo.xHotspot;
curHotSpot.y -= iconInfo.yHotspot;
}
#endif
wxString msg;
msg.Printf("Hotspot = %d, %d", curHotSpot.x, curHotSpot.y);
wxLogDebug(msg);
// First add the cursor to the image list
HCURSOR hCursor = (HCURSOR) m_cursor.GetHCURSOR();
int cursorIndex = ImageList_AddIcon((HIMAGELIST) m_hCursorImageList, (HICON) hCursor);
@ -290,18 +304,18 @@ bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, bool fullS
if (cursorIndex != -1)
{
ImageList_SetDragCursorImage((HIMAGELIST) m_hCursorImageList, cursorIndex, m_cursorHotspot.x, m_cursorHotspot.y);
ImageList_SetDragCursorImage((HIMAGELIST) m_hCursorImageList, cursorIndex, curHotSpot.x, curHotSpot.y);
}
#endif
}
m_window = window;
#if !wxUSE_SIMPLER_DRAGIMAGE
if (m_cursor.Ok())
::ShowCursor(FALSE);
#endif
m_window = window;
::SetCapture(GetHwndOf(window));
return TRUE;
@ -376,6 +390,10 @@ bool wxDragImage::Move(const wxPoint& pt)
// Subtract the (negative) values, i.e. add a small increment
pt2.x -= rect.left; pt2.y -= rect.top;
}
else if (m_window && m_fullScreen)
{
pt2 = m_window->ClientToScreen(pt2);
}
bool ret = (ImageList_DragMove( pt2.x, pt2.y ) != 0);