]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/dragimag.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDragImage
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dragimag.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #if defined(__WIN95__)
28 #include "wx/window.h"
29 #include "wx/dcclient.h"
35 #include "wx/msw/dragimag.h"
36 #include "wx/msw/private.h"
38 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
42 #if !USE_SHARED_LIBRARY
43 IMPLEMENT_DYNAMIC_CLASS(wxDragImage
, wxObject
)
46 wxDragImage::wxDragImage()
51 wxDragImage::~wxDragImage()
54 ImageList_Destroy((HIMAGELIST
) m_hImageList
);
60 ////////////////////////////////////////////////////////////////////////////
64 ////////////////////////////////////////////////////////////////////////////
66 // Create a drag image from a bitmap and optional cursor
67 bool wxDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& hotspot
)
70 ImageList_Destroy((HIMAGELIST
) m_hImageList
);
74 bool mask
= TRUE
; // ?
78 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
80 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
82 if ( image
.GetMask() )
83 hBitmap2
= (HBITMAP
) image
.GetMask()->GetMaskBitmap();
85 int index
= ImageList_Add((HIMAGELIST
) m_hImageList
, hBitmap1
, hBitmap2
);
88 wxLogError(_("Couldn't add an image to the image list."));
91 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
94 return (index
!= -1) ;
97 // Create a drag image from an icon and optional cursor
98 bool wxDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& hotspot
)
101 ImageList_Destroy((HIMAGELIST
) m_hImageList
);
105 bool mask
= TRUE
; // ?
109 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
111 HICON hIcon
= (HICON
) image
.GetHICON();
113 int index
= ImageList_AddIcon((HIMAGELIST
) m_hImageList
, hIcon
);
116 wxLogError(_("Couldn't add an image to the image list."));
119 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
122 return (index
!= -1) ;
125 // Create a drag image from a string and optional cursor
126 bool wxDragImage::Create(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& hotspot
)
128 wxFont
font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
133 dc
.GetTextExtent(str
, & w
, & h
);
137 wxBitmap
bitmap((int) w
, (int) h
);
138 dc2
.SelectObject(bitmap
);
140 dc2
.SetBackground(* wxWHITE_BRUSH
);
142 dc2
.DrawText(str
, 0, 0);
144 dc2
.SelectObject(wxNullBitmap
);
146 return Create(bitmap
, cursor
, hotspot
);
149 // Create a drag image for the given tree control item
150 bool wxDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
153 ImageList_Destroy((HIMAGELIST
) m_hImageList
);
154 m_hImageList
= (WXHIMAGELIST
) TreeView_CreateDragImage((HWND
) treeCtrl
.GetHWND(), (HTREEITEM
) (WXHTREEITEM
) id
);
158 // Create a drag image for the given list control item
159 bool wxDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
162 ImageList_Destroy((HIMAGELIST
) m_hImageList
);
165 m_hImageList
= (WXHIMAGELIST
) ListView_CreateDragImage((HWND
) listCtrl
.GetHWND(), id
, & pt
);
170 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* WXUNUSED(window
))
172 wxASSERT_MSG( (m_hImageList
!= 0), "Image list must not be null in BeginDrag.");
174 bool ret
= (ImageList_BeginDrag((HIMAGELIST
) m_hImageList
, 0, hotspot
.x
, hotspot
.y
) != 0);
176 wxASSERT_MSG( (ret
), "BeginDrag failed.");
183 // First add the cursor to the image list
184 int cursorIndex
= ImageList_AddIcon((HIMAGELIST
) m_hImageList
, (HICON
) m_cursor
.GetHCURSOR());
186 wxASSERT_MSG( (cursorIndex
!= -1), "ImageList_AddIcon failed in BeginDrag.");
188 if (cursorIndex
!= -1)
190 ImageList_SetDragCursorImage((HIMAGELIST
) m_hImageList
, cursorIndex
, m_hotspot
.x
, m_hotspot
.y
);
200 bool wxDragImage::EndDrag(wxWindow
* WXUNUSED(window
))
202 wxASSERT_MSG( (m_hImageList
!= 0), "Image list must not be null in EndDrag.");
211 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
212 // is non-NULL, or in screen coordinates if NULL.
213 bool wxDragImage::Move(const wxPoint
& pt
, wxWindow
* window
)
215 wxASSERT_MSG( (m_hImageList
!= 0), "Image list must not be null in Move.");
217 // TODO: what coordinates are these in: window, client, or screen?
218 bool ret
= (ImageList_DragMove( pt
.x
, pt
.y
) != 0);
225 bool wxDragImage::Show(wxWindow
* window
)
227 wxASSERT_MSG( (m_hImageList
!= 0), "Image list must not be null in Show.");
231 hWnd
= (HWND
) window
->GetHWND();
233 bool ret
= (ImageList_DragEnter( hWnd
, m_position
.x
, m_position
.y
) != 0);
238 bool wxDragImage::Hide(wxWindow
* window
)
240 wxASSERT_MSG( (m_hImageList
!= 0), "Image list must not be null in Hide.");
244 hWnd
= (HWND
) window
->GetHWND();
246 bool ret
= (ImageList_DragLeave( hWnd
) != 0);