]>
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(__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);
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 if (image
.GetDepth() <= 4)
175 else if (image
.GetDepth() <= 8)
177 else if (image
.GetDepth() <= 16)
179 else if (image
.GetDepth() <= 24)
187 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
189 HICON hIcon
= (HICON
) image
.GetHICON();
191 int index
= ImageList_AddIcon(GetHimageList(), hIcon
);
194 wxLogError(_("Couldn't add an image to the image list."));
197 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
199 return (index
!= -1) ;
202 // Create a drag image from a string and optional cursor
203 bool wxDragImage::Create(const wxString
& str
, const wxCursor
& cursor
)
205 wxFont
font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
210 dc
.GetTextExtent(str
, & w
, & h
);
211 dc
.SetFont(wxNullFont
);
215 wxBitmap
bitmap((int) w
+2, (int) h
+2);
216 dc2
.SelectObject(bitmap
);
218 dc2
.SetBackground(* wxWHITE_BRUSH
);
220 dc2
.SetBackgroundMode(wxTRANSPARENT
);
221 dc2
.SetTextForeground(* wxLIGHT_GREY
);
222 dc2
.DrawText(str
, 0, 0);
223 dc2
.DrawText(str
, 1, 0);
224 dc2
.DrawText(str
, 2, 0);
225 dc2
.DrawText(str
, 1, 1);
226 dc2
.DrawText(str
, 2, 1);
227 dc2
.DrawText(str
, 1, 2);
228 dc2
.DrawText(str
, 2, 2);
229 dc2
.SetTextForeground(* wxBLACK
);
230 dc2
.DrawText(str
, 1, 1);
232 dc2
.SelectObject(wxNullBitmap
);
234 // Make the bitmap masked
235 wxImage image
= bitmap
.ConvertToImage();
236 image
.SetMaskColour(255, 255, 255);
237 return Create(wxBitmap(image
), cursor
);
241 // Create a drag image for the given tree control item
242 bool wxDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
245 ImageList_Destroy(GetHimageList());
246 m_hImageList
= (WXHIMAGELIST
) TreeView_CreateDragImage((HWND
) treeCtrl
.GetHWND(), (HTREEITEM
) (WXHTREEITEM
) id
);
252 // Create a drag image for the given list control item
253 bool wxDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
256 ImageList_Destroy(GetHimageList());
259 m_hImageList
= (WXHIMAGELIST
) ListView_CreateDragImage((HWND
) listCtrl
.GetHWND(), id
, & pt
);
265 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, bool fullScreen
, wxRect
* rect
)
267 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in BeginDrag."));
268 wxASSERT_MSG( (window
!= 0), wxT("Window must not be null in BeginDrag."));
270 m_fullScreen
= fullScreen
;
272 m_boundingRect
= * rect
;
274 bool ret
= (ImageList_BeginDrag(GetHimageList(), 0, hotspot
.x
, hotspot
.y
) != 0);
278 wxFAIL_MSG( _T("BeginDrag failed.") );
285 #if wxUSE_SIMPLER_DRAGIMAGE
286 m_oldCursor
= window
->GetCursor();
287 window
->SetCursor(m_cursor
);
289 if (!m_hCursorImageList
)
291 int cxCursor
= GetSystemMetrics(SM_CXCURSOR
);
292 int cyCursor
= GetSystemMetrics(SM_CYCURSOR
);
294 m_hCursorImageList
= (WXHIMAGELIST
) ImageList_Create(cxCursor
, cyCursor
, ILC_MASK
, 1, 1);
297 // See if we can find the cursor hotspot
298 wxPoint
curHotSpot(hotspot
);
300 // Although it seems to produce the right position, when the hotspot goeos
301 // negative it has strange effects on the image.
302 // How do we stop the cursor jumping right and below of where it should be?
305 if (::GetIconInfo((HICON
) (HCURSOR
) m_cursor
.GetHCURSOR(), & iconInfo
) != 0)
307 curHotSpot
.x
-= iconInfo
.xHotspot
;
308 curHotSpot
.y
-= iconInfo
.yHotspot
;
312 //msg.Printf("Hotspot = %d, %d", curHotSpot.x, curHotSpot.y);
315 // First add the cursor to the image list
316 HCURSOR hCursor
= (HCURSOR
) m_cursor
.GetHCURSOR();
317 int cursorIndex
= ImageList_AddIcon((HIMAGELIST
) m_hCursorImageList
, (HICON
) hCursor
);
319 wxASSERT_MSG( (cursorIndex
!= -1), wxT("ImageList_AddIcon failed in BeginDrag."));
321 if (cursorIndex
!= -1)
323 ImageList_SetDragCursorImage((HIMAGELIST
) m_hCursorImageList
, cursorIndex
, curHotSpot
.x
, curHotSpot
.y
);
328 #if !wxUSE_SIMPLER_DRAGIMAGE
335 ::SetCapture(GetHwndOf(window
));
340 // Begin drag. hotspot is the location of the drag position relative to the upper-left
341 // corner of the image. This is full screen only. fullScreenRect gives the
342 // position of the window on the screen, to restrict the drag to.
343 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
347 int x
= fullScreenRect
->GetPosition().x
;
348 int y
= fullScreenRect
->GetPosition().y
;
350 wxSize sz
= fullScreenRect
->GetSize();
352 if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
)))
353 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
355 rect
.x
= x
; rect
.y
= y
;
356 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
358 return BeginDrag(hotspot
, window
, TRUE
, & rect
);
362 bool wxDragImage::EndDrag()
364 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in EndDrag."));
368 if ( !::ReleaseCapture() )
370 wxLogLastError(wxT("ReleaseCapture"));
373 #if wxUSE_SIMPLER_DRAGIMAGE
374 if (m_cursor
.Ok() && m_oldCursor
.Ok())
375 m_window
->SetCursor(m_oldCursor
);
380 m_window
= (wxWindow
*) NULL
;
385 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
386 // is non-NULL, or in screen coordinates if NULL.
387 bool wxDragImage::Move(const wxPoint
& pt
)
389 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Move."));
391 // These are in window, not client coordinates.
392 // So need to convert to client coordinates.
394 if (m_window
&& !m_fullScreen
)
397 rect
.left
= 0; rect
.top
= 0;
398 rect
.right
= 0; rect
.bottom
= 0;
399 DWORD style
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_STYLE
);
401 DWORD exStyle
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_EXSTYLE
);
402 ::AdjustWindowRectEx(& rect
, style
, FALSE
, exStyle
);
404 ::AdjustWindowRect(& rect
, style
, FALSE
);
406 // Subtract the (negative) values, i.e. add a small increment
407 pt2
.x
-= rect
.left
; pt2
.y
-= rect
.top
;
409 else if (m_window
&& m_fullScreen
)
411 pt2
= m_window
->ClientToScreen(pt2
);
414 bool ret
= (ImageList_DragMove( pt2
.x
, pt2
.y
) != 0);
421 bool wxDragImage::Show()
423 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Show."));
426 if (m_window
&& !m_fullScreen
)
427 hWnd
= (HWND
) m_window
->GetHWND();
429 bool ret
= (ImageList_DragEnter( hWnd
, m_position
.x
, m_position
.y
) != 0);
434 bool wxDragImage::Hide()
436 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Hide."));
439 if (m_window
&& !m_fullScreen
)
440 hWnd
= (HWND
) m_window
->GetHWND();
442 bool ret
= (ImageList_DragLeave( hWnd
) != 0);
450 #endif // wxUSE_DRAGIMAGE