]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/dragimag.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dragimag.cpp
3 // Purpose: wxDragImage
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
31 #include "wx/window.h"
32 #include "wx/dcclient.h"
33 #include "wx/dcscreen.h"
34 #include "wx/dcmemory.h"
35 #include "wx/settings.h"
38 #include "wx/msw/private.h"
44 #include "wx/msw/dragimag.h"
45 #include "wx/msw/private.h"
47 #ifdef __WXWINCE__ // for SM_CXCURSOR and SM_CYCURSOR
48 #include "wx/msw/wince/missing.h"
51 // include <commctrl.h> "properly"
52 #include "wx/msw/wrapcctl.h"
54 // Wine doesn't have this yet
55 #ifndef ListView_CreateDragImage
56 #define ListView_CreateDragImage(hwnd, i, lpptUpLeft) \
57 (HIMAGELIST)SNDMSG((hwnd), LVM_CREATEDRAGIMAGE, (WPARAM)(int)(i), (LPARAM)(LPPOINT)(lpptUpLeft))
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 IMPLEMENT_DYNAMIC_CLASS(wxDragImage
, wxObject
)
66 #define GetHimageList() ((HIMAGELIST) m_hImageList)
68 // ============================================================================
70 // ============================================================================
72 // ----------------------------------------------------------------------------
73 // wxDragImage ctors/dtor
74 // ----------------------------------------------------------------------------
76 wxDragImage::wxDragImage()
81 wxDragImage::~wxDragImage()
84 ImageList_Destroy(GetHimageList());
85 #if !wxUSE_SIMPLER_DRAGIMAGE
86 if ( m_hCursorImageList
)
87 ImageList_Destroy((HIMAGELIST
) m_hCursorImageList
);
91 void wxDragImage::Init()
94 #if !wxUSE_SIMPLER_DRAGIMAGE
95 m_hCursorImageList
= 0;
97 m_window
= (wxWindow
*) NULL
;
102 ////////////////////////////////////////////////////////////////////////////
106 ////////////////////////////////////////////////////////////////////////////
108 // Create a drag image from a bitmap and optional cursor
109 bool wxDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
)
112 ImageList_Destroy(GetHimageList());
116 UINT flags
= ILC_COLOR
;
118 UINT flags
wxDUMMY_INITIALIZE(0) ;
119 if (image
.GetDepth() <= 4)
121 else if (image
.GetDepth() <= 8)
123 else if (image
.GetDepth() <= 16)
125 else if (image
.GetDepth() <= 24)
131 bool mask
= (image
.GetMask() != 0);
133 // Curiously, even if the image doesn't have a mask,
134 // we still have to use ILC_MASK or the image won't show
139 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
144 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
145 index
= ImageList_Add(GetHimageList(), hBitmap1
, 0);
149 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
150 HBITMAP hBitmap2
= (HBITMAP
) image
.GetMask()->GetMaskBitmap();
151 HBITMAP hbmpMask
= wxInvertMask(hBitmap2
);
153 index
= ImageList_Add(GetHimageList(), hBitmap1
, hbmpMask
);
154 ::DeleteObject(hbmpMask
);
158 wxLogError(_("Couldn't add an image to the image list."));
160 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
162 return (index
!= -1) ;
165 // Create a drag image from an icon and optional cursor
166 bool wxDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
)
169 ImageList_Destroy(GetHimageList());
173 UINT flags
= ILC_COLOR
;
175 UINT flags
wxDUMMY_INITIALIZE(0) ;
176 if (image
.GetDepth() <= 4)
178 else if (image
.GetDepth() <= 8)
180 else if (image
.GetDepth() <= 16)
182 else if (image
.GetDepth() <= 24)
191 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
193 HICON hIcon
= (HICON
) image
.GetHICON();
195 int index
= ImageList_AddIcon(GetHimageList(), hIcon
);
198 wxLogError(_("Couldn't add an image to the image list."));
201 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
203 return (index
!= -1) ;
206 // Create a drag image from a string and optional cursor
207 bool wxDragImage::Create(const wxString
& str
, const wxCursor
& cursor
)
209 wxFont
font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
214 dc
.GetTextExtent(str
, & w
, & h
);
215 dc
.SetFont(wxNullFont
);
219 wxBitmap
bitmap((int) w
+2, (int) h
+2);
220 dc2
.SelectObject(bitmap
);
222 dc2
.SetBackground(* wxWHITE_BRUSH
);
224 dc2
.SetBackgroundMode(wxTRANSPARENT
);
225 dc2
.SetTextForeground(* wxLIGHT_GREY
);
226 dc2
.DrawText(str
, 0, 0);
227 dc2
.DrawText(str
, 1, 0);
228 dc2
.DrawText(str
, 2, 0);
229 dc2
.DrawText(str
, 1, 1);
230 dc2
.DrawText(str
, 2, 1);
231 dc2
.DrawText(str
, 1, 2);
232 dc2
.DrawText(str
, 2, 2);
233 dc2
.SetTextForeground(* wxBLACK
);
234 dc2
.DrawText(str
, 1, 1);
236 dc2
.SelectObject(wxNullBitmap
);
239 // Make the bitmap masked
240 wxImage image
= bitmap
.ConvertToImage();
241 image
.SetMaskColour(255, 255, 255);
242 return Create(wxBitmap(image
), cursor
);
249 // Create a drag image for the given tree control item
250 bool wxDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
253 ImageList_Destroy(GetHimageList());
254 m_hImageList
= (WXHIMAGELIST
)
255 TreeView_CreateDragImage(GetHwndOf(&treeCtrl
), (HTREEITEM
) id
.m_pItem
);
256 return m_hImageList
!= 0;
261 // Create a drag image for the given list control item
262 bool wxDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
265 ImageList_Destroy(GetHimageList());
268 m_hImageList
= (WXHIMAGELIST
) ListView_CreateDragImage((HWND
) listCtrl
.GetHWND(), id
, & pt
);
274 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, bool fullScreen
, wxRect
* rect
)
276 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in BeginDrag."));
277 wxASSERT_MSG( (window
!= 0), wxT("Window must not be null in BeginDrag."));
279 m_fullScreen
= fullScreen
;
281 m_boundingRect
= * rect
;
283 bool ret
= (ImageList_BeginDrag(GetHimageList(), 0, hotspot
.x
, hotspot
.y
) != 0);
287 wxFAIL_MSG( _T("BeginDrag failed.") );
294 #if wxUSE_SIMPLER_DRAGIMAGE
295 m_oldCursor
= window
->GetCursor();
296 window
->SetCursor(m_cursor
);
298 if (!m_hCursorImageList
)
301 // Smartphone may not have these metric symbol
305 int cxCursor
= ::GetSystemMetrics(SM_CXCURSOR
);
306 int cyCursor
= ::GetSystemMetrics(SM_CYCURSOR
);
308 m_hCursorImageList
= (WXHIMAGELIST
) ImageList_Create(cxCursor
, cyCursor
, ILC_MASK
, 1, 1);
311 // See if we can find the cursor hotspot
312 wxPoint
curHotSpot(hotspot
);
314 // Although it seems to produce the right position, when the hotspot goeos
315 // negative it has strange effects on the image.
316 // How do we stop the cursor jumping right and below of where it should be?
319 if (::GetIconInfo((HICON
) (HCURSOR
) m_cursor
.GetHCURSOR(), & iconInfo
) != 0)
321 curHotSpot
.x
-= iconInfo
.xHotspot
;
322 curHotSpot
.y
-= iconInfo
.yHotspot
;
326 //msg.Printf("Hotspot = %d, %d", curHotSpot.x, curHotSpot.y);
329 // First add the cursor to the image list
330 HCURSOR hCursor
= (HCURSOR
) m_cursor
.GetHCURSOR();
331 int cursorIndex
= ImageList_AddIcon((HIMAGELIST
) m_hCursorImageList
, (HICON
) hCursor
);
333 wxASSERT_MSG( (cursorIndex
!= -1), wxT("ImageList_AddIcon failed in BeginDrag."));
335 if (cursorIndex
!= -1)
337 ImageList_SetDragCursorImage((HIMAGELIST
) m_hCursorImageList
, cursorIndex
, curHotSpot
.x
, curHotSpot
.y
);
342 #if !wxUSE_SIMPLER_DRAGIMAGE
349 ::SetCapture(GetHwndOf(window
));
354 // Begin drag. hotspot is the location of the drag position relative to the upper-left
355 // corner of the image. This is full screen only. fullScreenRect gives the
356 // position of the window on the screen, to restrict the drag to.
357 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
361 int x
= fullScreenRect
->GetPosition().x
;
362 int y
= fullScreenRect
->GetPosition().y
;
364 wxSize sz
= fullScreenRect
->GetSize();
366 if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
)))
367 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
369 rect
.x
= x
; rect
.y
= y
;
370 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
372 return BeginDrag(hotspot
, window
, true, & rect
);
376 bool wxDragImage::EndDrag()
378 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in EndDrag."));
382 if ( !::ReleaseCapture() )
384 wxLogLastError(wxT("ReleaseCapture"));
387 #if wxUSE_SIMPLER_DRAGIMAGE
388 if (m_cursor
.Ok() && m_oldCursor
.Ok())
389 m_window
->SetCursor(m_oldCursor
);
394 m_window
= (wxWindow
*) NULL
;
399 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
400 // is non-NULL, or in screen coordinates if NULL.
401 bool wxDragImage::Move(const wxPoint
& pt
)
403 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Move."));
405 // These are in window, not client coordinates.
406 // So need to convert to client coordinates.
408 if (m_window
&& !m_fullScreen
)
411 rect
.left
= 0; rect
.top
= 0;
412 rect
.right
= 0; rect
.bottom
= 0;
413 DWORD style
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_STYLE
);
415 DWORD exStyle
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_EXSTYLE
);
416 ::AdjustWindowRectEx(& rect
, style
, FALSE
, exStyle
);
418 ::AdjustWindowRect(& rect
, style
, FALSE
);
420 // Subtract the (negative) values, i.e. add a small increment
421 pt2
.x
-= rect
.left
; pt2
.y
-= rect
.top
;
423 else if (m_window
&& m_fullScreen
)
425 pt2
= m_window
->ClientToScreen(pt2
);
428 bool ret
= (ImageList_DragMove( pt2
.x
, pt2
.y
) != 0);
435 bool wxDragImage::Show()
437 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Show."));
440 if (m_window
&& !m_fullScreen
)
441 hWnd
= (HWND
) m_window
->GetHWND();
443 bool ret
= (ImageList_DragEnter( hWnd
, m_position
.x
, m_position
.y
) != 0);
448 bool wxDragImage::Hide()
450 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Hide."));
453 if (m_window
&& !m_fullScreen
)
454 hWnd
= (HWND
) m_window
->GetHWND();
456 bool ret
= (ImageList_DragLeave( hWnd
) != 0);
461 #endif // wxUSE_DRAGIMAGE