]>
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
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
29 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
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"
42 #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 // Wine doesn't have this yet
52 #ifndef ListView_CreateDragImage
53 #define ListView_CreateDragImage(hwnd, i, lpptUpLeft) \
54 (HIMAGELIST)SNDMSG((hwnd), LVM_CREATEDRAGIMAGE, (WPARAM)(int)(i), (LPARAM)(LPPOINT)(lpptUpLeft))
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 IMPLEMENT_DYNAMIC_CLASS(wxDragImage
, wxObject
)
63 #define GetHimageList() ((HIMAGELIST) m_hImageList)
65 // ============================================================================
67 // ============================================================================
69 // ----------------------------------------------------------------------------
70 // wxDragImage ctors/dtor
71 // ----------------------------------------------------------------------------
73 wxDragImage::wxDragImage()
78 wxDragImage::~wxDragImage()
81 ImageList_Destroy(GetHimageList());
82 #if !wxUSE_SIMPLER_DRAGIMAGE
83 if ( m_hCursorImageList
)
84 ImageList_Destroy((HIMAGELIST
) m_hCursorImageList
);
88 void wxDragImage::Init()
91 #if !wxUSE_SIMPLER_DRAGIMAGE
92 m_hCursorImageList
= 0;
98 #if WXWIN_COMPATIBILITY_2_8
99 wxDragImage::wxDragImage(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
103 Create(image
, cursor
);
106 wxDragImage::wxDragImage(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
110 Create(image
, cursor
);
113 wxDragImage::wxDragImage(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
120 bool wxDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
122 return Create(image
, cursor
);
125 bool wxDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
127 return Create(image
, cursor
);
130 bool wxDragImage::Create(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
132 return Create(str
, cursor
);
134 #endif // WXWIN_COMPATIBILITY_2_8
137 ////////////////////////////////////////////////////////////////////////////
141 ////////////////////////////////////////////////////////////////////////////
143 // Create a drag image from a bitmap and optional cursor
144 bool wxDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
)
147 ImageList_Destroy(GetHimageList());
151 UINT flags
= ILC_COLOR
;
153 UINT flags
wxDUMMY_INITIALIZE(0) ;
154 if (image
.GetDepth() <= 4)
156 else if (image
.GetDepth() <= 8)
158 else if (image
.GetDepth() <= 16)
160 else if (image
.GetDepth() <= 24)
166 bool mask
= (image
.GetMask() != 0);
168 // Curiously, even if the image doesn't have a mask,
169 // we still have to use ILC_MASK or the image won't show
174 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
179 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
180 index
= ImageList_Add(GetHimageList(), hBitmap1
, 0);
184 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
185 HBITMAP hBitmap2
= (HBITMAP
) image
.GetMask()->GetMaskBitmap();
186 HBITMAP hbmpMask
= wxInvertMask(hBitmap2
);
188 index
= ImageList_Add(GetHimageList(), hBitmap1
, hbmpMask
);
189 ::DeleteObject(hbmpMask
);
193 wxLogError(_("Couldn't add an image to the image list."));
195 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
197 return (index
!= -1) ;
200 // Create a drag image from an icon and optional cursor
201 bool wxDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
)
204 ImageList_Destroy(GetHimageList());
208 UINT flags
= ILC_COLOR
;
210 UINT flags
wxDUMMY_INITIALIZE(0) ;
211 if (image
.GetDepth() <= 4)
213 else if (image
.GetDepth() <= 8)
215 else if (image
.GetDepth() <= 16)
217 else if (image
.GetDepth() <= 24)
225 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
227 HICON hIcon
= (HICON
) image
.GetHICON();
229 int index
= ImageList_AddIcon(GetHimageList(), hIcon
);
232 wxLogError(_("Couldn't add an image to the image list."));
235 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
237 return (index
!= -1) ;
240 // Create a drag image from a string and optional cursor
241 bool wxDragImage::Create(const wxString
& str
, const wxCursor
& cursor
)
243 wxFont
font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
245 wxCoord w
= 0, h
= 0;
248 dc
.GetTextExtent(str
, & w
, & h
);
249 dc
.SetFont(wxNullFont
);
253 wxBitmap
bitmap((int) w
+2, (int) h
+2);
254 dc2
.SelectObject(bitmap
);
256 dc2
.SetBackground(* wxWHITE_BRUSH
);
258 dc2
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
259 dc2
.SetTextForeground(* wxLIGHT_GREY
);
260 dc2
.DrawText(str
, 0, 0);
261 dc2
.DrawText(str
, 1, 0);
262 dc2
.DrawText(str
, 2, 0);
263 dc2
.DrawText(str
, 1, 1);
264 dc2
.DrawText(str
, 2, 1);
265 dc2
.DrawText(str
, 1, 2);
266 dc2
.DrawText(str
, 2, 2);
267 dc2
.SetTextForeground(* wxBLACK
);
268 dc2
.DrawText(str
, 1, 1);
270 dc2
.SelectObject(wxNullBitmap
);
273 // Make the bitmap masked
274 wxImage image
= bitmap
.ConvertToImage();
275 image
.SetMaskColour(255, 255, 255);
276 return Create(wxBitmap(image
), cursor
);
283 // Create a drag image for the given tree control item
284 bool wxDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
287 ImageList_Destroy(GetHimageList());
288 m_hImageList
= (WXHIMAGELIST
)
289 TreeView_CreateDragImage(GetHwndOf(&treeCtrl
), (HTREEITEM
) id
.m_pItem
);
292 // fall back on just the item text if there is no image
293 return Create(treeCtrl
.GetItemText(id
));
301 // Create a drag image for the given list control item
302 bool wxDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
305 ImageList_Destroy(GetHimageList());
309 m_hImageList
= (WXHIMAGELIST
)
310 ListView_CreateDragImage(GetHwndOf(&listCtrl
), id
, &pt
);
314 // as for wxTreeCtrl, fall back on dragging just the item text
315 return Create(listCtrl
.GetItemText(id
));
323 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, bool fullScreen
, wxRect
* rect
)
325 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in BeginDrag."));
326 wxASSERT_MSG( (window
!= 0), wxT("Window must not be null in BeginDrag."));
328 m_fullScreen
= fullScreen
;
330 m_boundingRect
= * rect
;
332 bool ret
= (ImageList_BeginDrag(GetHimageList(), 0, hotspot
.x
, hotspot
.y
) != 0);
336 wxFAIL_MSG( wxT("BeginDrag failed.") );
343 #if wxUSE_SIMPLER_DRAGIMAGE
344 m_oldCursor
= window
->GetCursor();
345 window
->SetCursor(m_cursor
);
347 if (!m_hCursorImageList
)
350 // Smartphone may not have these metric symbol
354 int cxCursor
= ::GetSystemMetrics(SM_CXCURSOR
);
355 int cyCursor
= ::GetSystemMetrics(SM_CYCURSOR
);
357 m_hCursorImageList
= (WXHIMAGELIST
) ImageList_Create(cxCursor
, cyCursor
, ILC_MASK
, 1, 1);
360 // See if we can find the cursor hotspot
361 wxPoint
curHotSpot(hotspot
);
363 // Although it seems to produce the right position, when the hotspot goeos
364 // negative it has strange effects on the image.
365 // How do we stop the cursor jumping right and below of where it should be?
368 if (::GetIconInfo((HICON
) (HCURSOR
) m_cursor
.GetHCURSOR(), & iconInfo
) != 0)
370 curHotSpot
.x
-= iconInfo
.xHotspot
;
371 curHotSpot
.y
-= iconInfo
.yHotspot
;
375 //msg.Printf("Hotspot = %d, %d", curHotSpot.x, curHotSpot.y);
378 // First add the cursor to the image list
379 HCURSOR hCursor
= (HCURSOR
) m_cursor
.GetHCURSOR();
380 int cursorIndex
= ImageList_AddIcon((HIMAGELIST
) m_hCursorImageList
, (HICON
) hCursor
);
382 wxASSERT_MSG( (cursorIndex
!= -1), wxT("ImageList_AddIcon failed in BeginDrag."));
384 if (cursorIndex
!= -1)
386 ImageList_SetDragCursorImage((HIMAGELIST
) m_hCursorImageList
, cursorIndex
, curHotSpot
.x
, curHotSpot
.y
);
391 #if !wxUSE_SIMPLER_DRAGIMAGE
398 ::SetCapture(GetHwndOf(window
));
403 // Begin drag. hotspot is the location of the drag position relative to the upper-left
404 // corner of the image. This is full screen only. fullScreenRect gives the
405 // position of the window on the screen, to restrict the drag to.
406 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
410 int x
= fullScreenRect
->GetPosition().x
;
411 int y
= fullScreenRect
->GetPosition().y
;
413 wxSize sz
= fullScreenRect
->GetSize();
415 if (fullScreenRect
->GetParent() && !wxDynamicCast(fullScreenRect
, wxFrame
))
416 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
418 rect
.x
= x
; rect
.y
= y
;
419 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
421 return BeginDrag(hotspot
, window
, true, & rect
);
425 bool wxDragImage::EndDrag()
427 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in EndDrag."));
431 if ( !::ReleaseCapture() )
433 wxLogLastError(wxT("ReleaseCapture"));
436 #if wxUSE_SIMPLER_DRAGIMAGE
437 if (m_cursor
.IsOk() && m_oldCursor
.IsOk())
438 m_window
->SetCursor(m_oldCursor
);
448 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
449 // is non-NULL, or in screen coordinates if NULL.
450 bool wxDragImage::Move(const wxPoint
& pt
)
452 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Move."));
454 // These are in window, not client coordinates.
455 // So need to convert to client coordinates.
457 if (m_window
&& !m_fullScreen
)
460 rect
.left
= 0; rect
.top
= 0;
461 rect
.right
= 0; rect
.bottom
= 0;
462 DWORD style
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_STYLE
);
464 DWORD exStyle
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_EXSTYLE
);
465 ::AdjustWindowRectEx(& rect
, style
, FALSE
, exStyle
);
467 ::AdjustWindowRect(& rect
, style
, FALSE
);
469 // Subtract the (negative) values, i.e. add a small increment
470 pt2
.x
-= rect
.left
; pt2
.y
-= rect
.top
;
472 else if (m_window
&& m_fullScreen
)
474 pt2
= m_window
->ClientToScreen(pt2
);
477 bool ret
= (ImageList_DragMove( pt2
.x
, pt2
.y
) != 0);
484 bool wxDragImage::Show()
486 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Show."));
489 if (m_window
&& !m_fullScreen
)
490 hWnd
= (HWND
) m_window
->GetHWND();
492 bool ret
= (ImageList_DragEnter( hWnd
, m_position
.x
, m_position
.y
) != 0);
497 bool wxDragImage::Hide()
499 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Hide."));
502 if (m_window
&& !m_fullScreen
)
503 hWnd
= (HWND
) m_window
->GetHWND();
505 bool ret
= (ImageList_DragLeave( hWnd
) != 0);
510 #endif // wxUSE_DRAGIMAGE