]>
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 /////////////////////////////////////////////////////////////////////////////
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 // Wine doesn't have this yet
59 #ifndef ListView_CreateDragImage
60 #define ListView_CreateDragImage(hwnd, i, lpptUpLeft) \
61 (HIMAGELIST)SNDMSG((hwnd), LVM_CREATEDRAGIMAGE, (WPARAM)(int)(i), (LPARAM)(LPPOINT)(lpptUpLeft))
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 IMPLEMENT_DYNAMIC_CLASS(wxDragImage
, wxObject
)
70 #define GetHimageList() ((HIMAGELIST) m_hImageList)
72 // ============================================================================
74 // ============================================================================
76 // ----------------------------------------------------------------------------
77 // wxDragImage ctors/dtor
78 // ----------------------------------------------------------------------------
80 wxDragImage::wxDragImage()
85 wxDragImage::~wxDragImage()
88 ImageList_Destroy(GetHimageList());
89 #if !wxUSE_SIMPLER_DRAGIMAGE
90 if ( m_hCursorImageList
)
91 ImageList_Destroy((HIMAGELIST
) m_hCursorImageList
);
95 void wxDragImage::Init()
98 #if !wxUSE_SIMPLER_DRAGIMAGE
99 m_hCursorImageList
= 0;
101 m_window
= (wxWindow
*) NULL
;
102 m_fullScreen
= FALSE
;
106 ////////////////////////////////////////////////////////////////////////////
110 ////////////////////////////////////////////////////////////////////////////
112 // Create a drag image from a bitmap and optional cursor
113 bool wxDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
)
116 ImageList_Destroy(GetHimageList());
120 if (image
.GetDepth() <= 4)
122 else if (image
.GetDepth() <= 8)
124 else if (image
.GetDepth() <= 16)
126 else if (image
.GetDepth() <= 24)
131 bool mask
= (image
.GetMask() != 0);
135 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
140 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
141 index
= ImageList_Add(GetHimageList(), hBitmap1
, 0);
145 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
146 HBITMAP hBitmap2
= (HBITMAP
) image
.GetMask()->GetMaskBitmap();
147 HBITMAP hbmpMask
= wxInvertMask(hBitmap2
);
149 index
= ImageList_Add(GetHimageList(), hBitmap1
, hbmpMask
);
150 ::DeleteObject(hbmpMask
);
154 wxLogError(_("Couldn't add an image to the image list."));
156 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
158 return (index
!= -1) ;
161 // Create a drag image from an icon and optional cursor
162 bool wxDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
)
165 ImageList_Destroy(GetHimageList());
169 if (image
.GetDepth() <= 4)
171 else if (image
.GetDepth() <= 8)
173 else if (image
.GetDepth() <= 16)
175 else if (image
.GetDepth() <= 24)
183 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
185 HICON hIcon
= (HICON
) image
.GetHICON();
187 int index
= ImageList_AddIcon(GetHimageList(), hIcon
);
190 wxLogError(_("Couldn't add an image to the image list."));
193 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
195 return (index
!= -1) ;
198 // Create a drag image from a string and optional cursor
199 bool wxDragImage::Create(const wxString
& str
, const wxCursor
& cursor
)
201 wxFont
font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
206 dc
.GetTextExtent(str
, & w
, & h
);
207 dc
.SetFont(wxNullFont
);
211 wxBitmap
bitmap((int) w
+2, (int) h
+2);
212 dc2
.SelectObject(bitmap
);
214 dc2
.SetBackground(* wxWHITE_BRUSH
);
216 dc2
.SetBackgroundMode(wxTRANSPARENT
);
217 dc2
.SetTextForeground(* wxLIGHT_GREY
);
218 dc2
.DrawText(str
, 0, 0);
219 dc2
.DrawText(str
, 1, 0);
220 dc2
.DrawText(str
, 2, 0);
221 dc2
.DrawText(str
, 1, 1);
222 dc2
.DrawText(str
, 2, 1);
223 dc2
.DrawText(str
, 1, 2);
224 dc2
.DrawText(str
, 2, 2);
225 dc2
.SetTextForeground(* wxBLACK
);
226 dc2
.DrawText(str
, 1, 1);
228 dc2
.SelectObject(wxNullBitmap
);
230 // Make the bitmap masked
231 wxImage image
= bitmap
.ConvertToImage();
232 image
.SetMaskColour(255, 255, 255);
233 return Create(wxBitmap(image
), cursor
);
236 // Create a drag image for the given tree control item
237 bool wxDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
240 ImageList_Destroy(GetHimageList());
241 m_hImageList
= (WXHIMAGELIST
) TreeView_CreateDragImage((HWND
) treeCtrl
.GetHWND(), (HTREEITEM
) (WXHTREEITEM
) id
);
245 // Create a drag image for the given list control item
246 bool wxDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
249 ImageList_Destroy(GetHimageList());
252 m_hImageList
= (WXHIMAGELIST
) ListView_CreateDragImage((HWND
) listCtrl
.GetHWND(), id
, & pt
);
257 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, bool fullScreen
, wxRect
* rect
)
259 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in BeginDrag."));
260 wxASSERT_MSG( (window
!= 0), wxT("Window must not be null in BeginDrag."));
262 m_fullScreen
= fullScreen
;
264 m_boundingRect
= * rect
;
266 bool ret
= (ImageList_BeginDrag(GetHimageList(), 0, hotspot
.x
, hotspot
.y
) != 0);
270 wxFAIL_MSG( _T("BeginDrag failed.") );
277 #if wxUSE_SIMPLER_DRAGIMAGE
278 m_oldCursor
= window
->GetCursor();
279 window
->SetCursor(m_cursor
);
281 if (!m_hCursorImageList
)
283 int cxCursor
= GetSystemMetrics(SM_CXCURSOR
);
284 int cyCursor
= GetSystemMetrics(SM_CYCURSOR
);
286 m_hCursorImageList
= (WXHIMAGELIST
) ImageList_Create(cxCursor
, cyCursor
, ILC_MASK
, 1, 1);
289 // See if we can find the cursor hotspot
290 wxPoint
curHotSpot(hotspot
);
292 // Although it seems to produce the right position, when the hotspot goeos
293 // negative it has strange effects on the image.
294 // How do we stop the cursor jumping right and below of where it should be?
297 if (::GetIconInfo((HICON
) (HCURSOR
) m_cursor
.GetHCURSOR(), & iconInfo
) != 0)
299 curHotSpot
.x
-= iconInfo
.xHotspot
;
300 curHotSpot
.y
-= iconInfo
.yHotspot
;
304 //msg.Printf("Hotspot = %d, %d", curHotSpot.x, curHotSpot.y);
307 // First add the cursor to the image list
308 HCURSOR hCursor
= (HCURSOR
) m_cursor
.GetHCURSOR();
309 int cursorIndex
= ImageList_AddIcon((HIMAGELIST
) m_hCursorImageList
, (HICON
) hCursor
);
311 wxASSERT_MSG( (cursorIndex
!= -1), wxT("ImageList_AddIcon failed in BeginDrag."));
313 if (cursorIndex
!= -1)
315 ImageList_SetDragCursorImage((HIMAGELIST
) m_hCursorImageList
, cursorIndex
, curHotSpot
.x
, curHotSpot
.y
);
320 #if !wxUSE_SIMPLER_DRAGIMAGE
327 ::SetCapture(GetHwndOf(window
));
332 // Begin drag. hotspot is the location of the drag position relative to the upper-left
333 // corner of the image. This is full screen only. fullScreenRect gives the
334 // position of the window on the screen, to restrict the drag to.
335 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
339 int x
= fullScreenRect
->GetPosition().x
;
340 int y
= fullScreenRect
->GetPosition().y
;
342 wxSize sz
= fullScreenRect
->GetSize();
344 if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
)))
345 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
347 rect
.x
= x
; rect
.y
= y
;
348 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
350 return BeginDrag(hotspot
, window
, TRUE
, & rect
);
354 bool wxDragImage::EndDrag()
356 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in EndDrag."));
360 if ( !::ReleaseCapture() )
362 wxLogLastError(wxT("ReleaseCapture"));
365 #if wxUSE_SIMPLER_DRAGIMAGE
366 if (m_cursor
.Ok() && m_oldCursor
.Ok())
367 m_window
->SetCursor(m_oldCursor
);
372 m_window
= (wxWindow
*) NULL
;
377 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
378 // is non-NULL, or in screen coordinates if NULL.
379 bool wxDragImage::Move(const wxPoint
& pt
)
381 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Move."));
383 // These are in window, not client coordinates.
384 // So need to convert to client coordinates.
386 if (m_window
&& !m_fullScreen
)
389 rect
.left
= 0; rect
.top
= 0;
390 rect
.right
= 0; rect
.bottom
= 0;
391 DWORD style
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_STYLE
);
393 DWORD exStyle
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_EXSTYLE
);
394 ::AdjustWindowRectEx(& rect
, style
, FALSE
, exStyle
);
396 ::AdjustWindowRect(& rect
, style
, FALSE
);
398 // Subtract the (negative) values, i.e. add a small increment
399 pt2
.x
-= rect
.left
; pt2
.y
-= rect
.top
;
401 else if (m_window
&& m_fullScreen
)
403 pt2
= m_window
->ClientToScreen(pt2
);
406 bool ret
= (ImageList_DragMove( pt2
.x
, pt2
.y
) != 0);
413 bool wxDragImage::Show()
415 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Show."));
418 if (m_window
&& !m_fullScreen
)
419 hWnd
= (HWND
) m_window
->GetHWND();
421 bool ret
= (ImageList_DragEnter( hWnd
, m_position
.x
, m_position
.y
) != 0);
426 bool wxDragImage::Hide()
428 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Hide."));
431 if (m_window
&& !m_fullScreen
)
432 hWnd
= (HWND
) m_window
->GetHWND();
434 bool ret
= (ImageList_DragLeave( hWnd
) != 0);
442 #endif // wxUSE_DRAGIMAGE