]>
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 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 #ifdef __WXWINCE__ // for SM_CXCURSOR and SM_CYCURSOR
55 #include "wx/msw/wince/missing.h"
58 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
62 // Wine doesn't have this yet
63 #ifndef ListView_CreateDragImage
64 #define ListView_CreateDragImage(hwnd, i, lpptUpLeft) \
65 (HIMAGELIST)SNDMSG((hwnd), LVM_CREATEDRAGIMAGE, (WPARAM)(int)(i), (LPARAM)(LPPOINT)(lpptUpLeft))
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 IMPLEMENT_DYNAMIC_CLASS(wxDragImage
, wxObject
)
74 #define GetHimageList() ((HIMAGELIST) m_hImageList)
76 // ============================================================================
78 // ============================================================================
80 // ----------------------------------------------------------------------------
81 // wxDragImage ctors/dtor
82 // ----------------------------------------------------------------------------
84 wxDragImage::wxDragImage()
89 wxDragImage::~wxDragImage()
92 ImageList_Destroy(GetHimageList());
93 #if !wxUSE_SIMPLER_DRAGIMAGE
94 if ( m_hCursorImageList
)
95 ImageList_Destroy((HIMAGELIST
) m_hCursorImageList
);
99 void wxDragImage::Init()
102 #if !wxUSE_SIMPLER_DRAGIMAGE
103 m_hCursorImageList
= 0;
105 m_window
= (wxWindow
*) NULL
;
106 m_fullScreen
= FALSE
;
110 ////////////////////////////////////////////////////////////////////////////
114 ////////////////////////////////////////////////////////////////////////////
116 // Create a drag image from a bitmap and optional cursor
117 bool wxDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
)
120 ImageList_Destroy(GetHimageList());
127 if (image
.GetDepth() <= 4)
129 else if (image
.GetDepth() <= 8)
131 else if (image
.GetDepth() <= 16)
133 else if (image
.GetDepth() <= 24)
139 bool mask
= (image
.GetMask() != 0);
141 // Curiously, even if the image doesn't have a mask,
142 // we still have to use ILC_MASK or the image won't show
147 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
152 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
153 index
= ImageList_Add(GetHimageList(), hBitmap1
, 0);
157 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
158 HBITMAP hBitmap2
= (HBITMAP
) image
.GetMask()->GetMaskBitmap();
159 HBITMAP hbmpMask
= wxInvertMask(hBitmap2
);
161 index
= ImageList_Add(GetHimageList(), hBitmap1
, hbmpMask
);
162 ::DeleteObject(hbmpMask
);
166 wxLogError(_("Couldn't add an image to the image list."));
168 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
170 return (index
!= -1) ;
173 // Create a drag image from an icon and optional cursor
174 bool wxDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
)
177 ImageList_Destroy(GetHimageList());
184 if (image
.GetDepth() <= 4)
186 else if (image
.GetDepth() <= 8)
188 else if (image
.GetDepth() <= 16)
190 else if (image
.GetDepth() <= 24)
199 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
201 HICON hIcon
= (HICON
) image
.GetHICON();
203 int index
= ImageList_AddIcon(GetHimageList(), hIcon
);
206 wxLogError(_("Couldn't add an image to the image list."));
209 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
211 return (index
!= -1) ;
214 // Create a drag image from a string and optional cursor
215 bool wxDragImage::Create(const wxString
& str
, const wxCursor
& cursor
)
217 wxFont
font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
222 dc
.GetTextExtent(str
, & w
, & h
);
223 dc
.SetFont(wxNullFont
);
227 wxBitmap
bitmap((int) w
+2, (int) h
+2);
228 dc2
.SelectObject(bitmap
);
230 dc2
.SetBackground(* wxWHITE_BRUSH
);
232 dc2
.SetBackgroundMode(wxTRANSPARENT
);
233 dc2
.SetTextForeground(* wxLIGHT_GREY
);
234 dc2
.DrawText(str
, 0, 0);
235 dc2
.DrawText(str
, 1, 0);
236 dc2
.DrawText(str
, 2, 0);
237 dc2
.DrawText(str
, 1, 1);
238 dc2
.DrawText(str
, 2, 1);
239 dc2
.DrawText(str
, 1, 2);
240 dc2
.DrawText(str
, 2, 2);
241 dc2
.SetTextForeground(* wxBLACK
);
242 dc2
.DrawText(str
, 1, 1);
244 dc2
.SelectObject(wxNullBitmap
);
246 // Make the bitmap masked
247 wxImage image
= bitmap
.ConvertToImage();
248 image
.SetMaskColour(255, 255, 255);
249 return Create(wxBitmap(image
), cursor
);
253 // Create a drag image for the given tree control item
254 bool wxDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
257 ImageList_Destroy(GetHimageList());
258 m_hImageList
= (WXHIMAGELIST
)
259 TreeView_CreateDragImage(GetHwndOf(&treeCtrl
), (HTREEITEM
) id
.m_pItem
);
260 return m_hImageList
!= 0;
265 // Create a drag image for the given list control item
266 bool wxDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
269 ImageList_Destroy(GetHimageList());
272 m_hImageList
= (WXHIMAGELIST
) ListView_CreateDragImage((HWND
) listCtrl
.GetHWND(), id
, & pt
);
278 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, bool fullScreen
, wxRect
* rect
)
280 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in BeginDrag."));
281 wxASSERT_MSG( (window
!= 0), wxT("Window must not be null in BeginDrag."));
283 m_fullScreen
= fullScreen
;
285 m_boundingRect
= * rect
;
287 bool ret
= (ImageList_BeginDrag(GetHimageList(), 0, hotspot
.x
, hotspot
.y
) != 0);
291 wxFAIL_MSG( _T("BeginDrag failed.") );
298 #if wxUSE_SIMPLER_DRAGIMAGE
299 m_oldCursor
= window
->GetCursor();
300 window
->SetCursor(m_cursor
);
302 if (!m_hCursorImageList
)
304 int cxCursor
= GetSystemMetrics(SM_CXCURSOR
);
305 int cyCursor
= GetSystemMetrics(SM_CYCURSOR
);
307 m_hCursorImageList
= (WXHIMAGELIST
) ImageList_Create(cxCursor
, cyCursor
, ILC_MASK
, 1, 1);
310 // See if we can find the cursor hotspot
311 wxPoint
curHotSpot(hotspot
);
313 // Although it seems to produce the right position, when the hotspot goeos
314 // negative it has strange effects on the image.
315 // How do we stop the cursor jumping right and below of where it should be?
318 if (::GetIconInfo((HICON
) (HCURSOR
) m_cursor
.GetHCURSOR(), & iconInfo
) != 0)
320 curHotSpot
.x
-= iconInfo
.xHotspot
;
321 curHotSpot
.y
-= iconInfo
.yHotspot
;
325 //msg.Printf("Hotspot = %d, %d", curHotSpot.x, curHotSpot.y);
328 // First add the cursor to the image list
329 HCURSOR hCursor
= (HCURSOR
) m_cursor
.GetHCURSOR();
330 int cursorIndex
= ImageList_AddIcon((HIMAGELIST
) m_hCursorImageList
, (HICON
) hCursor
);
332 wxASSERT_MSG( (cursorIndex
!= -1), wxT("ImageList_AddIcon failed in BeginDrag."));
334 if (cursorIndex
!= -1)
336 ImageList_SetDragCursorImage((HIMAGELIST
) m_hCursorImageList
, cursorIndex
, curHotSpot
.x
, curHotSpot
.y
);
341 #if !wxUSE_SIMPLER_DRAGIMAGE
348 ::SetCapture(GetHwndOf(window
));
353 // Begin drag. hotspot is the location of the drag position relative to the upper-left
354 // corner of the image. This is full screen only. fullScreenRect gives the
355 // position of the window on the screen, to restrict the drag to.
356 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
360 int x
= fullScreenRect
->GetPosition().x
;
361 int y
= fullScreenRect
->GetPosition().y
;
363 wxSize sz
= fullScreenRect
->GetSize();
365 if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
)))
366 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
368 rect
.x
= x
; rect
.y
= y
;
369 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
371 return BeginDrag(hotspot
, window
, TRUE
, & rect
);
375 bool wxDragImage::EndDrag()
377 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in EndDrag."));
381 if ( !::ReleaseCapture() )
383 wxLogLastError(wxT("ReleaseCapture"));
386 #if wxUSE_SIMPLER_DRAGIMAGE
387 if (m_cursor
.Ok() && m_oldCursor
.Ok())
388 m_window
->SetCursor(m_oldCursor
);
393 m_window
= (wxWindow
*) NULL
;
398 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
399 // is non-NULL, or in screen coordinates if NULL.
400 bool wxDragImage::Move(const wxPoint
& pt
)
402 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Move."));
404 // These are in window, not client coordinates.
405 // So need to convert to client coordinates.
407 if (m_window
&& !m_fullScreen
)
410 rect
.left
= 0; rect
.top
= 0;
411 rect
.right
= 0; rect
.bottom
= 0;
412 DWORD style
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_STYLE
);
414 DWORD exStyle
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_EXSTYLE
);
415 ::AdjustWindowRectEx(& rect
, style
, FALSE
, exStyle
);
417 ::AdjustWindowRect(& rect
, style
, FALSE
);
419 // Subtract the (negative) values, i.e. add a small increment
420 pt2
.x
-= rect
.left
; pt2
.y
-= rect
.top
;
422 else if (m_window
&& m_fullScreen
)
424 pt2
= m_window
->ClientToScreen(pt2
);
427 bool ret
= (ImageList_DragMove( pt2
.x
, pt2
.y
) != 0);
434 bool wxDragImage::Show()
436 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Show."));
439 if (m_window
&& !m_fullScreen
)
440 hWnd
= (HWND
) m_window
->GetHWND();
442 bool ret
= (ImageList_DragEnter( hWnd
, m_position
.x
, m_position
.y
) != 0);
447 bool wxDragImage::Hide()
449 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Hide."));
452 if (m_window
&& !m_fullScreen
)
453 hWnd
= (HWND
) m_window
->GetHWND();
455 bool ret
= (ImageList_DragLeave( hWnd
) != 0);
463 #endif // wxUSE_DRAGIMAGE