]>
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"
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"
39 #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 // include <commctrl.h> "properly"
53 #include "wx/msw/wrapcctl.h"
55 // Wine doesn't have this yet
56 #ifndef ListView_CreateDragImage
57 #define ListView_CreateDragImage(hwnd, i, lpptUpLeft) \
58 (HIMAGELIST)SNDMSG((hwnd), LVM_CREATEDRAGIMAGE, (WPARAM)(int)(i), (LPARAM)(LPPOINT)(lpptUpLeft))
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 IMPLEMENT_DYNAMIC_CLASS(wxDragImage
, wxObject
)
67 #define GetHimageList() ((HIMAGELIST) m_hImageList)
69 // ============================================================================
71 // ============================================================================
73 // ----------------------------------------------------------------------------
74 // wxDragImage ctors/dtor
75 // ----------------------------------------------------------------------------
77 wxDragImage::wxDragImage()
82 wxDragImage::~wxDragImage()
85 ImageList_Destroy(GetHimageList());
86 #if !wxUSE_SIMPLER_DRAGIMAGE
87 if ( m_hCursorImageList
)
88 ImageList_Destroy((HIMAGELIST
) m_hCursorImageList
);
92 void wxDragImage::Init()
95 #if !wxUSE_SIMPLER_DRAGIMAGE
96 m_hCursorImageList
= 0;
98 m_window
= (wxWindow
*) NULL
;
103 ////////////////////////////////////////////////////////////////////////////
107 ////////////////////////////////////////////////////////////////////////////
109 // Create a drag image from a bitmap and optional cursor
110 bool wxDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
)
113 ImageList_Destroy(GetHimageList());
117 UINT flags
= ILC_COLOR
;
119 UINT flags
wxDUMMY_INITIALIZE(0) ;
120 if (image
.GetDepth() <= 4)
122 else if (image
.GetDepth() <= 8)
124 else if (image
.GetDepth() <= 16)
126 else if (image
.GetDepth() <= 24)
132 bool mask
= (image
.GetMask() != 0);
134 // Curiously, even if the image doesn't have a mask,
135 // we still have to use ILC_MASK or the image won't show
140 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
145 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
146 index
= ImageList_Add(GetHimageList(), hBitmap1
, 0);
150 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
151 HBITMAP hBitmap2
= (HBITMAP
) image
.GetMask()->GetMaskBitmap();
152 HBITMAP hbmpMask
= wxInvertMask(hBitmap2
);
154 index
= ImageList_Add(GetHimageList(), hBitmap1
, hbmpMask
);
155 ::DeleteObject(hbmpMask
);
159 wxLogError(_("Couldn't add an image to the image list."));
161 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
163 return (index
!= -1) ;
166 // Create a drag image from an icon and optional cursor
167 bool wxDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
)
170 ImageList_Destroy(GetHimageList());
174 UINT flags
= ILC_COLOR
;
176 UINT flags
wxDUMMY_INITIALIZE(0) ;
177 if (image
.GetDepth() <= 4)
179 else if (image
.GetDepth() <= 8)
181 else if (image
.GetDepth() <= 16)
183 else if (image
.GetDepth() <= 24)
192 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
194 HICON hIcon
= (HICON
) image
.GetHICON();
196 int index
= ImageList_AddIcon(GetHimageList(), hIcon
);
199 wxLogError(_("Couldn't add an image to the image list."));
202 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
204 return (index
!= -1) ;
207 // Create a drag image from a string and optional cursor
208 bool wxDragImage::Create(const wxString
& str
, const wxCursor
& cursor
)
210 wxFont
font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
215 dc
.GetTextExtent(str
, & w
, & h
);
216 dc
.SetFont(wxNullFont
);
220 wxBitmap
bitmap((int) w
+2, (int) h
+2);
221 dc2
.SelectObject(bitmap
);
223 dc2
.SetBackground(* wxWHITE_BRUSH
);
225 dc2
.SetBackgroundMode(wxTRANSPARENT
);
226 dc2
.SetTextForeground(* wxLIGHT_GREY
);
227 dc2
.DrawText(str
, 0, 0);
228 dc2
.DrawText(str
, 1, 0);
229 dc2
.DrawText(str
, 2, 0);
230 dc2
.DrawText(str
, 1, 1);
231 dc2
.DrawText(str
, 2, 1);
232 dc2
.DrawText(str
, 1, 2);
233 dc2
.DrawText(str
, 2, 2);
234 dc2
.SetTextForeground(* wxBLACK
);
235 dc2
.DrawText(str
, 1, 1);
237 dc2
.SelectObject(wxNullBitmap
);
239 // Make the bitmap masked
240 wxImage image
= bitmap
.ConvertToImage();
241 image
.SetMaskColour(255, 255, 255);
242 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
);
253 return m_hImageList
!= 0;
258 // Create a drag image for the given list control item
259 bool wxDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
262 ImageList_Destroy(GetHimageList());
265 m_hImageList
= (WXHIMAGELIST
) ListView_CreateDragImage((HWND
) listCtrl
.GetHWND(), id
, & pt
);
271 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, bool fullScreen
, wxRect
* rect
)
273 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in BeginDrag."));
274 wxASSERT_MSG( (window
!= 0), wxT("Window must not be null in BeginDrag."));
276 m_fullScreen
= fullScreen
;
278 m_boundingRect
= * rect
;
280 bool ret
= (ImageList_BeginDrag(GetHimageList(), 0, hotspot
.x
, hotspot
.y
) != 0);
284 wxFAIL_MSG( _T("BeginDrag failed.") );
291 #if wxUSE_SIMPLER_DRAGIMAGE
292 m_oldCursor
= window
->GetCursor();
293 window
->SetCursor(m_cursor
);
295 if (!m_hCursorImageList
)
297 int cxCursor
= ::GetSystemMetrics(SM_CXCURSOR
);
298 int cyCursor
= ::GetSystemMetrics(SM_CYCURSOR
);
300 m_hCursorImageList
= (WXHIMAGELIST
) ImageList_Create(cxCursor
, cyCursor
, ILC_MASK
, 1, 1);
303 // See if we can find the cursor hotspot
304 wxPoint
curHotSpot(hotspot
);
306 // Although it seems to produce the right position, when the hotspot goeos
307 // negative it has strange effects on the image.
308 // How do we stop the cursor jumping right and below of where it should be?
311 if (::GetIconInfo((HICON
) (HCURSOR
) m_cursor
.GetHCURSOR(), & iconInfo
) != 0)
313 curHotSpot
.x
-= iconInfo
.xHotspot
;
314 curHotSpot
.y
-= iconInfo
.yHotspot
;
318 //msg.Printf("Hotspot = %d, %d", curHotSpot.x, curHotSpot.y);
321 // First add the cursor to the image list
322 HCURSOR hCursor
= (HCURSOR
) m_cursor
.GetHCURSOR();
323 int cursorIndex
= ImageList_AddIcon((HIMAGELIST
) m_hCursorImageList
, (HICON
) hCursor
);
325 wxASSERT_MSG( (cursorIndex
!= -1), wxT("ImageList_AddIcon failed in BeginDrag."));
327 if (cursorIndex
!= -1)
329 ImageList_SetDragCursorImage((HIMAGELIST
) m_hCursorImageList
, cursorIndex
, curHotSpot
.x
, curHotSpot
.y
);
334 #if !wxUSE_SIMPLER_DRAGIMAGE
341 ::SetCapture(GetHwndOf(window
));
346 // Begin drag. hotspot is the location of the drag position relative to the upper-left
347 // corner of the image. This is full screen only. fullScreenRect gives the
348 // position of the window on the screen, to restrict the drag to.
349 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
353 int x
= fullScreenRect
->GetPosition().x
;
354 int y
= fullScreenRect
->GetPosition().y
;
356 wxSize sz
= fullScreenRect
->GetSize();
358 if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
)))
359 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
361 rect
.x
= x
; rect
.y
= y
;
362 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
364 return BeginDrag(hotspot
, window
, true, & rect
);
368 bool wxDragImage::EndDrag()
370 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in EndDrag."));
374 if ( !::ReleaseCapture() )
376 wxLogLastError(wxT("ReleaseCapture"));
379 #if wxUSE_SIMPLER_DRAGIMAGE
380 if (m_cursor
.Ok() && m_oldCursor
.Ok())
381 m_window
->SetCursor(m_oldCursor
);
386 m_window
= (wxWindow
*) NULL
;
391 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
392 // is non-NULL, or in screen coordinates if NULL.
393 bool wxDragImage::Move(const wxPoint
& pt
)
395 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Move."));
397 // These are in window, not client coordinates.
398 // So need to convert to client coordinates.
400 if (m_window
&& !m_fullScreen
)
403 rect
.left
= 0; rect
.top
= 0;
404 rect
.right
= 0; rect
.bottom
= 0;
405 DWORD style
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_STYLE
);
407 DWORD exStyle
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_EXSTYLE
);
408 ::AdjustWindowRectEx(& rect
, style
, FALSE
, exStyle
);
410 ::AdjustWindowRect(& rect
, style
, FALSE
);
412 // Subtract the (negative) values, i.e. add a small increment
413 pt2
.x
-= rect
.left
; pt2
.y
-= rect
.top
;
415 else if (m_window
&& m_fullScreen
)
417 pt2
= m_window
->ClientToScreen(pt2
);
420 bool ret
= (ImageList_DragMove( pt2
.x
, pt2
.y
) != 0);
427 bool wxDragImage::Show()
429 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Show."));
432 if (m_window
&& !m_fullScreen
)
433 hWnd
= (HWND
) m_window
->GetHWND();
435 bool ret
= (ImageList_DragEnter( hWnd
, m_position
.x
, m_position
.y
) != 0);
440 bool wxDragImage::Hide()
442 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Hide."));
445 if (m_window
&& !m_fullScreen
)
446 hWnd
= (HWND
) m_window
->GetHWND();
448 bool ret
= (ImageList_DragLeave( hWnd
) != 0);
453 #endif // wxUSE_DRAGIMAGE