]>
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"
30 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/window.h"
33 #include "wx/dcclient.h"
34 #include "wx/dcscreen.h"
35 #include "wx/dcmemory.h"
36 #include "wx/settings.h"
43 #include "wx/msw/private.h"
45 #include "wx/msw/dragimag.h"
46 #include "wx/msw/private.h"
48 #ifdef __WXWINCE__ // for SM_CXCURSOR and SM_CYCURSOR
49 #include "wx/msw/wince/missing.h"
52 // Wine doesn't have this yet
53 #ifndef ListView_CreateDragImage
54 #define ListView_CreateDragImage(hwnd, i, lpptUpLeft) \
55 (HIMAGELIST)SNDMSG((hwnd), LVM_CREATEDRAGIMAGE, (WPARAM)(int)(i), (LPARAM)(LPPOINT)(lpptUpLeft))
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;
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 UINT flags
= ILC_COLOR
;
116 UINT flags
wxDUMMY_INITIALIZE(0) ;
117 if (image
.GetDepth() <= 4)
119 else if (image
.GetDepth() <= 8)
121 else if (image
.GetDepth() <= 16)
123 else if (image
.GetDepth() <= 24)
129 bool mask
= (image
.GetMask() != 0);
131 // Curiously, even if the image doesn't have a mask,
132 // we still have to use ILC_MASK or the image won't show
137 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
142 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
143 index
= ImageList_Add(GetHimageList(), hBitmap1
, 0);
147 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
148 HBITMAP hBitmap2
= (HBITMAP
) image
.GetMask()->GetMaskBitmap();
149 HBITMAP hbmpMask
= wxInvertMask(hBitmap2
);
151 index
= ImageList_Add(GetHimageList(), hBitmap1
, hbmpMask
);
152 ::DeleteObject(hbmpMask
);
156 wxLogError(_("Couldn't add an image to the image list."));
158 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
160 return (index
!= -1) ;
163 // Create a drag image from an icon and optional cursor
164 bool wxDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
)
167 ImageList_Destroy(GetHimageList());
171 UINT flags
= ILC_COLOR
;
173 UINT flags
wxDUMMY_INITIALIZE(0) ;
174 if (image
.GetDepth() <= 4)
176 else if (image
.GetDepth() <= 8)
178 else if (image
.GetDepth() <= 16)
180 else if (image
.GetDepth() <= 24)
188 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
190 HICON hIcon
= (HICON
) image
.GetHICON();
192 int index
= ImageList_AddIcon(GetHimageList(), hIcon
);
195 wxLogError(_("Couldn't add an image to the image list."));
198 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
200 return (index
!= -1) ;
203 // Create a drag image from a string and optional cursor
204 bool wxDragImage::Create(const wxString
& str
, const wxCursor
& cursor
)
206 wxFont
font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
208 wxCoord w
= 0, h
= 0;
211 dc
.GetTextExtent(str
, & w
, & h
);
212 dc
.SetFont(wxNullFont
);
216 wxBitmap
bitmap((int) w
+2, (int) h
+2);
217 dc2
.SelectObject(bitmap
);
219 dc2
.SetBackground(* wxWHITE_BRUSH
);
221 dc2
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
222 dc2
.SetTextForeground(* wxLIGHT_GREY
);
223 dc2
.DrawText(str
, 0, 0);
224 dc2
.DrawText(str
, 1, 0);
225 dc2
.DrawText(str
, 2, 0);
226 dc2
.DrawText(str
, 1, 1);
227 dc2
.DrawText(str
, 2, 1);
228 dc2
.DrawText(str
, 1, 2);
229 dc2
.DrawText(str
, 2, 2);
230 dc2
.SetTextForeground(* wxBLACK
);
231 dc2
.DrawText(str
, 1, 1);
233 dc2
.SelectObject(wxNullBitmap
);
236 // Make the bitmap masked
237 wxImage image
= bitmap
.ConvertToImage();
238 image
.SetMaskColour(255, 255, 255);
239 return Create(wxBitmap(image
), cursor
);
246 // Create a drag image for the given tree control item
247 bool wxDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
250 ImageList_Destroy(GetHimageList());
251 m_hImageList
= (WXHIMAGELIST
)
252 TreeView_CreateDragImage(GetHwndOf(&treeCtrl
), (HTREEITEM
) id
.m_pItem
);
255 // fall back on just the item text if there is no image
256 return Create(treeCtrl
.GetItemText(id
));
264 // Create a drag image for the given list control item
265 bool wxDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
268 ImageList_Destroy(GetHimageList());
272 m_hImageList
= (WXHIMAGELIST
)
273 ListView_CreateDragImage(GetHwndOf(&listCtrl
), id
, &pt
);
277 // as for wxTreeCtrl, fall back on dragging just the item text
278 return Create(listCtrl
.GetItemText(id
));
286 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, bool fullScreen
, wxRect
* rect
)
288 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in BeginDrag."));
289 wxASSERT_MSG( (window
!= 0), wxT("Window must not be null in BeginDrag."));
291 m_fullScreen
= fullScreen
;
293 m_boundingRect
= * rect
;
295 bool ret
= (ImageList_BeginDrag(GetHimageList(), 0, hotspot
.x
, hotspot
.y
) != 0);
299 wxFAIL_MSG( wxT("BeginDrag failed.") );
306 #if wxUSE_SIMPLER_DRAGIMAGE
307 m_oldCursor
= window
->GetCursor();
308 window
->SetCursor(m_cursor
);
310 if (!m_hCursorImageList
)
313 // Smartphone may not have these metric symbol
317 int cxCursor
= ::GetSystemMetrics(SM_CXCURSOR
);
318 int cyCursor
= ::GetSystemMetrics(SM_CYCURSOR
);
320 m_hCursorImageList
= (WXHIMAGELIST
) ImageList_Create(cxCursor
, cyCursor
, ILC_MASK
, 1, 1);
323 // See if we can find the cursor hotspot
324 wxPoint
curHotSpot(hotspot
);
326 // Although it seems to produce the right position, when the hotspot goeos
327 // negative it has strange effects on the image.
328 // How do we stop the cursor jumping right and below of where it should be?
331 if (::GetIconInfo((HICON
) (HCURSOR
) m_cursor
.GetHCURSOR(), & iconInfo
) != 0)
333 curHotSpot
.x
-= iconInfo
.xHotspot
;
334 curHotSpot
.y
-= iconInfo
.yHotspot
;
338 //msg.Printf("Hotspot = %d, %d", curHotSpot.x, curHotSpot.y);
341 // First add the cursor to the image list
342 HCURSOR hCursor
= (HCURSOR
) m_cursor
.GetHCURSOR();
343 int cursorIndex
= ImageList_AddIcon((HIMAGELIST
) m_hCursorImageList
, (HICON
) hCursor
);
345 wxASSERT_MSG( (cursorIndex
!= -1), wxT("ImageList_AddIcon failed in BeginDrag."));
347 if (cursorIndex
!= -1)
349 ImageList_SetDragCursorImage((HIMAGELIST
) m_hCursorImageList
, cursorIndex
, curHotSpot
.x
, curHotSpot
.y
);
354 #if !wxUSE_SIMPLER_DRAGIMAGE
361 ::SetCapture(GetHwndOf(window
));
366 // Begin drag. hotspot is the location of the drag position relative to the upper-left
367 // corner of the image. This is full screen only. fullScreenRect gives the
368 // position of the window on the screen, to restrict the drag to.
369 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
373 int x
= fullScreenRect
->GetPosition().x
;
374 int y
= fullScreenRect
->GetPosition().y
;
376 wxSize sz
= fullScreenRect
->GetSize();
378 if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
)))
379 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
381 rect
.x
= x
; rect
.y
= y
;
382 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
384 return BeginDrag(hotspot
, window
, true, & rect
);
388 bool wxDragImage::EndDrag()
390 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in EndDrag."));
394 if ( !::ReleaseCapture() )
396 wxLogLastError(wxT("ReleaseCapture"));
399 #if wxUSE_SIMPLER_DRAGIMAGE
400 if (m_cursor
.Ok() && m_oldCursor
.Ok())
401 m_window
->SetCursor(m_oldCursor
);
411 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
412 // is non-NULL, or in screen coordinates if NULL.
413 bool wxDragImage::Move(const wxPoint
& pt
)
415 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Move."));
417 // These are in window, not client coordinates.
418 // So need to convert to client coordinates.
420 if (m_window
&& !m_fullScreen
)
423 rect
.left
= 0; rect
.top
= 0;
424 rect
.right
= 0; rect
.bottom
= 0;
425 DWORD style
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_STYLE
);
427 DWORD exStyle
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_EXSTYLE
);
428 ::AdjustWindowRectEx(& rect
, style
, FALSE
, exStyle
);
430 ::AdjustWindowRect(& rect
, style
, FALSE
);
432 // Subtract the (negative) values, i.e. add a small increment
433 pt2
.x
-= rect
.left
; pt2
.y
-= rect
.top
;
435 else if (m_window
&& m_fullScreen
)
437 pt2
= m_window
->ClientToScreen(pt2
);
440 bool ret
= (ImageList_DragMove( pt2
.x
, pt2
.y
) != 0);
447 bool wxDragImage::Show()
449 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Show."));
452 if (m_window
&& !m_fullScreen
)
453 hWnd
= (HWND
) m_window
->GetHWND();
455 bool ret
= (ImageList_DragEnter( hWnd
, m_position
.x
, m_position
.y
) != 0);
460 bool wxDragImage::Hide()
462 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Hide."));
465 if (m_window
&& !m_fullScreen
)
466 hWnd
= (HWND
) m_window
->GetHWND();
468 bool ret
= (ImageList_DragLeave( hWnd
) != 0);
473 #endif // wxUSE_DRAGIMAGE