1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDragImage
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "dragimag.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #if defined(__WIN95__)
38 #include "wx/window.h"
39 #include "wx/dcclient.h"
40 #include "wx/dcscreen.h"
41 #include "wx/dcmemory.h"
42 #include "wx/settings.h"
45 #include "wx/msw/private.h"
51 #include "wx/msw/dragimag.h"
52 #include "wx/msw/private.h"
54 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 IMPLEMENT_DYNAMIC_CLASS(wxDragImage
, wxObject
)
64 #define GetHimageList() ((HIMAGELIST) m_hImageList)
66 // ============================================================================
68 // ============================================================================
70 // ----------------------------------------------------------------------------
71 // wxDragImage ctors/dtor
72 // ----------------------------------------------------------------------------
74 wxDragImage::wxDragImage()
79 wxDragImage::~wxDragImage()
82 ImageList_Destroy(GetHimageList());
83 #if !wxUSE_SIMPLER_DRAGIMAGE
84 if ( m_hCursorImageList
)
85 ImageList_Destroy((HIMAGELIST
) m_hCursorImageList
);
89 void wxDragImage::Init()
92 #if !wxUSE_SIMPLER_DRAGIMAGE
93 m_hCursorImageList
= 0;
95 m_window
= (wxWindow
*) NULL
;
100 ////////////////////////////////////////////////////////////////////////////
104 ////////////////////////////////////////////////////////////////////////////
106 // Create a drag image from a bitmap and optional cursor
107 bool wxDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
)
110 ImageList_Destroy(GetHimageList());
114 if (image
.GetDepth() <= 4)
116 else if (image
.GetDepth() <= 8)
118 else if (image
.GetDepth() <= 16)
120 else if (image
.GetDepth() <= 24)
125 bool mask
= (image
.GetMask() != 0);
129 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
134 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
135 index
= ImageList_Add(GetHimageList(), hBitmap1
, 0);
139 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
140 HBITMAP hBitmap2
= (HBITMAP
) image
.GetMask()->GetMaskBitmap();
141 HBITMAP hbmpMask
= wxInvertMask(hBitmap2
);
143 index
= ImageList_Add(GetHimageList(), hBitmap1
, hbmpMask
);
144 ::DeleteObject(hbmpMask
);
148 wxLogError(_("Couldn't add an image to the image list."));
150 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
152 return (index
!= -1) ;
155 // Create a drag image from an icon and optional cursor
156 bool wxDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
)
159 ImageList_Destroy(GetHimageList());
163 if (image
.GetDepth() <= 4)
165 else if (image
.GetDepth() <= 8)
167 else if (image
.GetDepth() <= 16)
169 else if (image
.GetDepth() <= 24)
177 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
179 HICON hIcon
= (HICON
) image
.GetHICON();
181 int index
= ImageList_AddIcon(GetHimageList(), hIcon
);
184 wxLogError(_("Couldn't add an image to the image list."));
187 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
189 return (index
!= -1) ;
192 // Create a drag image from a string and optional cursor
193 bool wxDragImage::Create(const wxString
& str
, const wxCursor
& cursor
)
195 wxFont
font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
200 dc
.GetTextExtent(str
, & w
, & h
);
201 dc
.SetFont(wxNullFont
);
205 wxBitmap
bitmap((int) w
+2, (int) h
+2);
206 dc2
.SelectObject(bitmap
);
208 dc2
.SetBackground(* wxWHITE_BRUSH
);
210 dc2
.SetBackgroundMode(wxTRANSPARENT
);
211 dc2
.SetTextForeground(* wxLIGHT_GREY
);
212 dc2
.DrawText(str
, 0, 0);
213 dc2
.DrawText(str
, 1, 0);
214 dc2
.DrawText(str
, 2, 0);
215 dc2
.DrawText(str
, 1, 1);
216 dc2
.DrawText(str
, 2, 1);
217 dc2
.DrawText(str
, 1, 2);
218 dc2
.DrawText(str
, 2, 2);
219 dc2
.SetTextForeground(* wxBLACK
);
220 dc2
.DrawText(str
, 1, 1);
222 dc2
.SelectObject(wxNullBitmap
);
224 // Make the bitmap masked
225 wxImage
image(bitmap
);
226 image
.SetMaskColour(255, 255, 255);
227 bitmap
= image
.ConvertToBitmap();
229 return Create(bitmap
, cursor
);
232 // Create a drag image for the given tree control item
233 bool wxDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
236 ImageList_Destroy(GetHimageList());
237 m_hImageList
= (WXHIMAGELIST
) TreeView_CreateDragImage((HWND
) treeCtrl
.GetHWND(), (HTREEITEM
) (WXHTREEITEM
) id
);
241 // Create a drag image for the given list control item
242 bool wxDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
245 ImageList_Destroy(GetHimageList());
248 m_hImageList
= (WXHIMAGELIST
) ListView_CreateDragImage((HWND
) listCtrl
.GetHWND(), id
, & pt
);
253 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, bool fullScreen
, wxRect
* rect
)
255 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in BeginDrag."));
256 wxASSERT_MSG( (window
!= 0), wxT("Window must not be null in BeginDrag."));
258 m_fullScreen
= fullScreen
;
260 m_boundingRect
= * rect
;
262 bool ret
= (ImageList_BeginDrag(GetHimageList(), 0, hotspot
.x
, hotspot
.y
) != 0);
266 wxFAIL_MSG( _T("BeginDrag failed.") );
273 #if wxUSE_SIMPLER_DRAGIMAGE
274 m_oldCursor
= window
->GetCursor();
275 window
->SetCursor(m_cursor
);
277 if (!m_hCursorImageList
)
279 int cxCursor
= GetSystemMetrics(SM_CXCURSOR
);
280 int cyCursor
= GetSystemMetrics(SM_CYCURSOR
);
282 m_hCursorImageList
= (WXHIMAGELIST
) ImageList_Create(cxCursor
, cyCursor
, ILC_MASK
, 1, 1);
285 // See if we can find the cursor hotspot
286 wxPoint
curHotSpot(hotspot
);
288 // Although it seems to produce the right position, when the hotspot goeos
289 // negative it has strange effects on the image.
290 // How do we stop the cursor jumping right and below of where it should be?
293 if (::GetIconInfo((HICON
) (HCURSOR
) m_cursor
.GetHCURSOR(), & iconInfo
) != 0)
295 curHotSpot
.x
-= iconInfo
.xHotspot
;
296 curHotSpot
.y
-= iconInfo
.yHotspot
;
300 //msg.Printf("Hotspot = %d, %d", curHotSpot.x, curHotSpot.y);
303 // First add the cursor to the image list
304 HCURSOR hCursor
= (HCURSOR
) m_cursor
.GetHCURSOR();
305 int cursorIndex
= ImageList_AddIcon((HIMAGELIST
) m_hCursorImageList
, (HICON
) hCursor
);
307 wxASSERT_MSG( (cursorIndex
!= -1), wxT("ImageList_AddIcon failed in BeginDrag."));
309 if (cursorIndex
!= -1)
311 ImageList_SetDragCursorImage((HIMAGELIST
) m_hCursorImageList
, cursorIndex
, curHotSpot
.x
, curHotSpot
.y
);
316 #if !wxUSE_SIMPLER_DRAGIMAGE
323 ::SetCapture(GetHwndOf(window
));
328 // Begin drag. hotspot is the location of the drag position relative to the upper-left
329 // corner of the image. This is full screen only. fullScreenRect gives the
330 // position of the window on the screen, to restrict the drag to.
331 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
335 int x
= fullScreenRect
->GetPosition().x
;
336 int y
= fullScreenRect
->GetPosition().y
;
338 wxSize sz
= fullScreenRect
->GetSize();
340 if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
)))
341 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
343 rect
.x
= x
; rect
.y
= y
;
344 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
346 return BeginDrag(hotspot
, window
, TRUE
, & rect
);
350 bool wxDragImage::EndDrag()
352 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in EndDrag."));
356 if ( !::ReleaseCapture() )
358 wxLogLastError(wxT("ReleaseCapture"));
361 #if wxUSE_SIMPLER_DRAGIMAGE
362 if (m_cursor
.Ok() && m_oldCursor
.Ok())
363 m_window
->SetCursor(m_oldCursor
);
368 m_window
= (wxWindow
*) NULL
;
373 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
374 // is non-NULL, or in screen coordinates if NULL.
375 bool wxDragImage::Move(const wxPoint
& pt
)
377 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Move."));
379 // These are in window, not client coordinates.
380 // So need to convert to client coordinates.
382 if (m_window
&& !m_fullScreen
)
385 rect
.left
= 0; rect
.top
= 0;
386 rect
.right
= 0; rect
.bottom
= 0;
387 DWORD style
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_STYLE
);
389 DWORD exStyle
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_EXSTYLE
);
390 ::AdjustWindowRectEx(& rect
, style
, FALSE
, exStyle
);
392 ::AdjustWindowRect(& rect
, style
, FALSE
);
394 // Subtract the (negative) values, i.e. add a small increment
395 pt2
.x
-= rect
.left
; pt2
.y
-= rect
.top
;
397 else if (m_window
&& m_fullScreen
)
399 pt2
= m_window
->ClientToScreen(pt2
);
402 bool ret
= (ImageList_DragMove( pt2
.x
, pt2
.y
) != 0);
409 bool wxDragImage::Show()
411 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Show."));
414 if (m_window
&& !m_fullScreen
)
415 hWnd
= (HWND
) m_window
->GetHWND();
417 bool ret
= (ImageList_DragEnter( hWnd
, m_position
.x
, m_position
.y
) != 0);
422 bool wxDragImage::Hide()
424 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Hide."));
427 if (m_window
&& !m_fullScreen
)
428 hWnd
= (HWND
) m_window
->GetHWND();
430 bool ret
= (ImageList_DragLeave( hWnd
) != 0);
438 #endif // wxUSE_DRAGIMAGE