]>
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;
99 #if WXWIN_COMPATIBILITY_2_8
100 wxDragImage::wxDragImage(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
104 Create(image
, cursor
);
107 wxDragImage::wxDragImage(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
111 Create(image
, cursor
);
114 wxDragImage::wxDragImage(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
121 bool wxDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
123 return Create(image
, cursor
);
126 bool wxDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
128 return Create(image
, cursor
);
131 bool wxDragImage::Create(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& WXUNUSED(cursorHotspot
))
133 return Create(str
, cursor
);
135 #endif // WXWIN_COMPATIBILITY_2_8
138 ////////////////////////////////////////////////////////////////////////////
142 ////////////////////////////////////////////////////////////////////////////
144 // Create a drag image from a bitmap and optional cursor
145 bool wxDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
)
148 ImageList_Destroy(GetHimageList());
152 UINT flags
= ILC_COLOR
;
154 UINT flags
wxDUMMY_INITIALIZE(0) ;
155 if (image
.GetDepth() <= 4)
157 else if (image
.GetDepth() <= 8)
159 else if (image
.GetDepth() <= 16)
161 else if (image
.GetDepth() <= 24)
167 bool mask
= (image
.GetMask() != 0);
169 // Curiously, even if the image doesn't have a mask,
170 // we still have to use ILC_MASK or the image won't show
175 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
180 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
181 index
= ImageList_Add(GetHimageList(), hBitmap1
, 0);
185 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
186 HBITMAP hBitmap2
= (HBITMAP
) image
.GetMask()->GetMaskBitmap();
187 HBITMAP hbmpMask
= wxInvertMask(hBitmap2
);
189 index
= ImageList_Add(GetHimageList(), hBitmap1
, hbmpMask
);
190 ::DeleteObject(hbmpMask
);
194 wxLogError(_("Couldn't add an image to the image list."));
196 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
198 return (index
!= -1) ;
201 // Create a drag image from an icon and optional cursor
202 bool wxDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
)
205 ImageList_Destroy(GetHimageList());
209 UINT flags
= ILC_COLOR
;
211 UINT flags
wxDUMMY_INITIALIZE(0) ;
212 if (image
.GetDepth() <= 4)
214 else if (image
.GetDepth() <= 8)
216 else if (image
.GetDepth() <= 16)
218 else if (image
.GetDepth() <= 24)
226 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
228 HICON hIcon
= (HICON
) image
.GetHICON();
230 int index
= ImageList_AddIcon(GetHimageList(), hIcon
);
233 wxLogError(_("Couldn't add an image to the image list."));
236 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
238 return (index
!= -1) ;
241 // Create a drag image from a string and optional cursor
242 bool wxDragImage::Create(const wxString
& str
, const wxCursor
& cursor
)
244 wxFont
font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
246 wxCoord w
= 0, h
= 0;
249 dc
.GetTextExtent(str
, & w
, & h
);
250 dc
.SetFont(wxNullFont
);
254 wxBitmap
bitmap((int) w
+2, (int) h
+2);
255 dc2
.SelectObject(bitmap
);
257 dc2
.SetBackground(* wxWHITE_BRUSH
);
259 dc2
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
260 dc2
.SetTextForeground(* wxLIGHT_GREY
);
261 dc2
.DrawText(str
, 0, 0);
262 dc2
.DrawText(str
, 1, 0);
263 dc2
.DrawText(str
, 2, 0);
264 dc2
.DrawText(str
, 1, 1);
265 dc2
.DrawText(str
, 2, 1);
266 dc2
.DrawText(str
, 1, 2);
267 dc2
.DrawText(str
, 2, 2);
268 dc2
.SetTextForeground(* wxBLACK
);
269 dc2
.DrawText(str
, 1, 1);
271 dc2
.SelectObject(wxNullBitmap
);
274 // Make the bitmap masked
275 wxImage image
= bitmap
.ConvertToImage();
276 image
.SetMaskColour(255, 255, 255);
277 return Create(wxBitmap(image
), cursor
);
284 // Create a drag image for the given tree control item
285 bool wxDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
288 ImageList_Destroy(GetHimageList());
289 m_hImageList
= (WXHIMAGELIST
)
290 TreeView_CreateDragImage(GetHwndOf(&treeCtrl
), (HTREEITEM
) id
.m_pItem
);
293 // fall back on just the item text if there is no image
294 return Create(treeCtrl
.GetItemText(id
));
302 // Create a drag image for the given list control item
303 bool wxDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
306 ImageList_Destroy(GetHimageList());
310 m_hImageList
= (WXHIMAGELIST
)
311 ListView_CreateDragImage(GetHwndOf(&listCtrl
), id
, &pt
);
315 // as for wxTreeCtrl, fall back on dragging just the item text
316 return Create(listCtrl
.GetItemText(id
));
324 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, bool fullScreen
, wxRect
* rect
)
326 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in BeginDrag."));
327 wxASSERT_MSG( (window
!= 0), wxT("Window must not be null in BeginDrag."));
329 m_fullScreen
= fullScreen
;
331 m_boundingRect
= * rect
;
333 bool ret
= (ImageList_BeginDrag(GetHimageList(), 0, hotspot
.x
, hotspot
.y
) != 0);
337 wxFAIL_MSG( wxT("BeginDrag failed.") );
344 #if wxUSE_SIMPLER_DRAGIMAGE
345 m_oldCursor
= window
->GetCursor();
346 window
->SetCursor(m_cursor
);
348 if (!m_hCursorImageList
)
351 // Smartphone may not have these metric symbol
355 int cxCursor
= ::GetSystemMetrics(SM_CXCURSOR
);
356 int cyCursor
= ::GetSystemMetrics(SM_CYCURSOR
);
358 m_hCursorImageList
= (WXHIMAGELIST
) ImageList_Create(cxCursor
, cyCursor
, ILC_MASK
, 1, 1);
361 // See if we can find the cursor hotspot
362 wxPoint
curHotSpot(hotspot
);
364 // Although it seems to produce the right position, when the hotspot goeos
365 // negative it has strange effects on the image.
366 // How do we stop the cursor jumping right and below of where it should be?
369 if (::GetIconInfo((HICON
) (HCURSOR
) m_cursor
.GetHCURSOR(), & iconInfo
) != 0)
371 curHotSpot
.x
-= iconInfo
.xHotspot
;
372 curHotSpot
.y
-= iconInfo
.yHotspot
;
376 //msg.Printf("Hotspot = %d, %d", curHotSpot.x, curHotSpot.y);
379 // First add the cursor to the image list
380 HCURSOR hCursor
= (HCURSOR
) m_cursor
.GetHCURSOR();
381 int cursorIndex
= ImageList_AddIcon((HIMAGELIST
) m_hCursorImageList
, (HICON
) hCursor
);
383 wxASSERT_MSG( (cursorIndex
!= -1), wxT("ImageList_AddIcon failed in BeginDrag."));
385 if (cursorIndex
!= -1)
387 ImageList_SetDragCursorImage((HIMAGELIST
) m_hCursorImageList
, cursorIndex
, curHotSpot
.x
, curHotSpot
.y
);
392 #if !wxUSE_SIMPLER_DRAGIMAGE
399 ::SetCapture(GetHwndOf(window
));
404 // Begin drag. hotspot is the location of the drag position relative to the upper-left
405 // corner of the image. This is full screen only. fullScreenRect gives the
406 // position of the window on the screen, to restrict the drag to.
407 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
411 int x
= fullScreenRect
->GetPosition().x
;
412 int y
= fullScreenRect
->GetPosition().y
;
414 wxSize sz
= fullScreenRect
->GetSize();
416 if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
)))
417 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
419 rect
.x
= x
; rect
.y
= y
;
420 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
422 return BeginDrag(hotspot
, window
, true, & rect
);
426 bool wxDragImage::EndDrag()
428 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in EndDrag."));
432 if ( !::ReleaseCapture() )
434 wxLogLastError(wxT("ReleaseCapture"));
437 #if wxUSE_SIMPLER_DRAGIMAGE
438 if (m_cursor
.IsOk() && m_oldCursor
.IsOk())
439 m_window
->SetCursor(m_oldCursor
);
449 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
450 // is non-NULL, or in screen coordinates if NULL.
451 bool wxDragImage::Move(const wxPoint
& pt
)
453 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Move."));
455 // These are in window, not client coordinates.
456 // So need to convert to client coordinates.
458 if (m_window
&& !m_fullScreen
)
461 rect
.left
= 0; rect
.top
= 0;
462 rect
.right
= 0; rect
.bottom
= 0;
463 DWORD style
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_STYLE
);
465 DWORD exStyle
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_EXSTYLE
);
466 ::AdjustWindowRectEx(& rect
, style
, FALSE
, exStyle
);
468 ::AdjustWindowRect(& rect
, style
, FALSE
);
470 // Subtract the (negative) values, i.e. add a small increment
471 pt2
.x
-= rect
.left
; pt2
.y
-= rect
.top
;
473 else if (m_window
&& m_fullScreen
)
475 pt2
= m_window
->ClientToScreen(pt2
);
478 bool ret
= (ImageList_DragMove( pt2
.x
, pt2
.y
) != 0);
485 bool wxDragImage::Show()
487 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Show."));
490 if (m_window
&& !m_fullScreen
)
491 hWnd
= (HWND
) m_window
->GetHWND();
493 bool ret
= (ImageList_DragEnter( hWnd
, m_position
.x
, m_position
.y
) != 0);
498 bool wxDragImage::Hide()
500 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Hide."));
503 if (m_window
&& !m_fullScreen
)
504 hWnd
= (HWND
) m_window
->GetHWND();
506 bool ret
= (ImageList_DragLeave( hWnd
) != 0);
511 #endif // wxUSE_DRAGIMAGE