]>
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());
123 if (image
.GetDepth() <= 4)
125 else if (image
.GetDepth() <= 8)
127 else if (image
.GetDepth() <= 16)
129 else if (image
.GetDepth() <= 24)
135 bool mask
= (image
.GetMask() != 0);
137 // Curiously, even if the image doesn't have a mask,
138 // we still have to use ILC_MASK or the image won't show
143 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
148 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
149 index
= ImageList_Add(GetHimageList(), hBitmap1
, 0);
153 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
154 HBITMAP hBitmap2
= (HBITMAP
) image
.GetMask()->GetMaskBitmap();
155 HBITMAP hbmpMask
= wxInvertMask(hBitmap2
);
157 index
= ImageList_Add(GetHimageList(), hBitmap1
, hbmpMask
);
158 ::DeleteObject(hbmpMask
);
162 wxLogError(_("Couldn't add an image to the image list."));
164 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
166 return (index
!= -1) ;
169 // Create a drag image from an icon and optional cursor
170 bool wxDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
)
173 ImageList_Destroy(GetHimageList());
180 if (image
.GetDepth() <= 4)
182 else if (image
.GetDepth() <= 8)
184 else if (image
.GetDepth() <= 16)
186 else if (image
.GetDepth() <= 24)
195 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
197 HICON hIcon
= (HICON
) image
.GetHICON();
199 int index
= ImageList_AddIcon(GetHimageList(), hIcon
);
202 wxLogError(_("Couldn't add an image to the image list."));
205 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
207 return (index
!= -1) ;
210 // Create a drag image from a string and optional cursor
211 bool wxDragImage::Create(const wxString
& str
, const wxCursor
& cursor
)
213 wxFont
font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
218 dc
.GetTextExtent(str
, & w
, & h
);
219 dc
.SetFont(wxNullFont
);
223 wxBitmap
bitmap((int) w
+2, (int) h
+2);
224 dc2
.SelectObject(bitmap
);
226 dc2
.SetBackground(* wxWHITE_BRUSH
);
228 dc2
.SetBackgroundMode(wxTRANSPARENT
);
229 dc2
.SetTextForeground(* wxLIGHT_GREY
);
230 dc2
.DrawText(str
, 0, 0);
231 dc2
.DrawText(str
, 1, 0);
232 dc2
.DrawText(str
, 2, 0);
233 dc2
.DrawText(str
, 1, 1);
234 dc2
.DrawText(str
, 2, 1);
235 dc2
.DrawText(str
, 1, 2);
236 dc2
.DrawText(str
, 2, 2);
237 dc2
.SetTextForeground(* wxBLACK
);
238 dc2
.DrawText(str
, 1, 1);
240 dc2
.SelectObject(wxNullBitmap
);
242 // Make the bitmap masked
243 wxImage image
= bitmap
.ConvertToImage();
244 image
.SetMaskColour(255, 255, 255);
245 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
) TreeView_CreateDragImage((HWND
) treeCtrl
.GetHWND(), (HTREEITEM
) (WXHTREEITEM
) id
);
260 // Create a drag image for the given list control item
261 bool wxDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
264 ImageList_Destroy(GetHimageList());
267 m_hImageList
= (WXHIMAGELIST
) ListView_CreateDragImage((HWND
) listCtrl
.GetHWND(), id
, & pt
);
273 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, bool fullScreen
, wxRect
* rect
)
275 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in BeginDrag."));
276 wxASSERT_MSG( (window
!= 0), wxT("Window must not be null in BeginDrag."));
278 m_fullScreen
= fullScreen
;
280 m_boundingRect
= * rect
;
282 bool ret
= (ImageList_BeginDrag(GetHimageList(), 0, hotspot
.x
, hotspot
.y
) != 0);
286 wxFAIL_MSG( _T("BeginDrag failed.") );
293 #if wxUSE_SIMPLER_DRAGIMAGE
294 m_oldCursor
= window
->GetCursor();
295 window
->SetCursor(m_cursor
);
297 if (!m_hCursorImageList
)
299 int cxCursor
= GetSystemMetrics(SM_CXCURSOR
);
300 int cyCursor
= GetSystemMetrics(SM_CYCURSOR
);
302 m_hCursorImageList
= (WXHIMAGELIST
) ImageList_Create(cxCursor
, cyCursor
, ILC_MASK
, 1, 1);
305 // See if we can find the cursor hotspot
306 wxPoint
curHotSpot(hotspot
);
308 // Although it seems to produce the right position, when the hotspot goeos
309 // negative it has strange effects on the image.
310 // How do we stop the cursor jumping right and below of where it should be?
313 if (::GetIconInfo((HICON
) (HCURSOR
) m_cursor
.GetHCURSOR(), & iconInfo
) != 0)
315 curHotSpot
.x
-= iconInfo
.xHotspot
;
316 curHotSpot
.y
-= iconInfo
.yHotspot
;
320 //msg.Printf("Hotspot = %d, %d", curHotSpot.x, curHotSpot.y);
323 // First add the cursor to the image list
324 HCURSOR hCursor
= (HCURSOR
) m_cursor
.GetHCURSOR();
325 int cursorIndex
= ImageList_AddIcon((HIMAGELIST
) m_hCursorImageList
, (HICON
) hCursor
);
327 wxASSERT_MSG( (cursorIndex
!= -1), wxT("ImageList_AddIcon failed in BeginDrag."));
329 if (cursorIndex
!= -1)
331 ImageList_SetDragCursorImage((HIMAGELIST
) m_hCursorImageList
, cursorIndex
, curHotSpot
.x
, curHotSpot
.y
);
336 #if !wxUSE_SIMPLER_DRAGIMAGE
343 ::SetCapture(GetHwndOf(window
));
348 // Begin drag. hotspot is the location of the drag position relative to the upper-left
349 // corner of the image. This is full screen only. fullScreenRect gives the
350 // position of the window on the screen, to restrict the drag to.
351 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
355 int x
= fullScreenRect
->GetPosition().x
;
356 int y
= fullScreenRect
->GetPosition().y
;
358 wxSize sz
= fullScreenRect
->GetSize();
360 if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
)))
361 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
363 rect
.x
= x
; rect
.y
= y
;
364 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
366 return BeginDrag(hotspot
, window
, TRUE
, & rect
);
370 bool wxDragImage::EndDrag()
372 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in EndDrag."));
376 if ( !::ReleaseCapture() )
378 wxLogLastError(wxT("ReleaseCapture"));
381 #if wxUSE_SIMPLER_DRAGIMAGE
382 if (m_cursor
.Ok() && m_oldCursor
.Ok())
383 m_window
->SetCursor(m_oldCursor
);
388 m_window
= (wxWindow
*) NULL
;
393 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
394 // is non-NULL, or in screen coordinates if NULL.
395 bool wxDragImage::Move(const wxPoint
& pt
)
397 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Move."));
399 // These are in window, not client coordinates.
400 // So need to convert to client coordinates.
402 if (m_window
&& !m_fullScreen
)
405 rect
.left
= 0; rect
.top
= 0;
406 rect
.right
= 0; rect
.bottom
= 0;
407 DWORD style
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_STYLE
);
409 DWORD exStyle
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_EXSTYLE
);
410 ::AdjustWindowRectEx(& rect
, style
, FALSE
, exStyle
);
412 ::AdjustWindowRect(& rect
, style
, FALSE
);
414 // Subtract the (negative) values, i.e. add a small increment
415 pt2
.x
-= rect
.left
; pt2
.y
-= rect
.top
;
417 else if (m_window
&& m_fullScreen
)
419 pt2
= m_window
->ClientToScreen(pt2
);
422 bool ret
= (ImageList_DragMove( pt2
.x
, pt2
.y
) != 0);
429 bool wxDragImage::Show()
431 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Show."));
434 if (m_window
&& !m_fullScreen
)
435 hWnd
= (HWND
) m_window
->GetHWND();
437 bool ret
= (ImageList_DragEnter( hWnd
, m_position
.x
, m_position
.y
) != 0);
442 bool wxDragImage::Hide()
444 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Hide."));
447 if (m_window
&& !m_fullScreen
)
448 hWnd
= (HWND
) m_window
->GetHWND();
450 bool ret
= (ImageList_DragLeave( hWnd
) != 0);
458 #endif // wxUSE_DRAGIMAGE