]>
Commit | Line | Data |
---|---|---|
7cf83330 | 1 | ///////////////////////////////////////////////////////////////////////////// |
a71d815b | 2 | // Name: src/msw/dragimag.cpp |
7cf83330 JS |
3 | // Purpose: wxDragImage |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 08/04/99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
65571936 | 9 | // Licence: wxWindows licence |
7cf83330 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
23f681ec VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
7cf83330 JS |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
88a7a4e1 | 24 | #pragma hdrstop |
7cf83330 JS |
25 | #endif |
26 | ||
fa8dca8d | 27 | #if wxUSE_DRAGIMAGE |
1e6feb95 | 28 | |
7cf83330 | 29 | #ifndef WX_PRECOMP |
57bd4c60 | 30 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" |
88a7a4e1 WS |
31 | #include <stdio.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" | |
37 | #include "wx/intl.h" | |
e4db172a | 38 | #include "wx/log.h" |
76b49cf4 | 39 | #include "wx/frame.h" |
155ecd4c | 40 | #include "wx/image.h" |
7cf83330 JS |
41 | #endif |
42 | ||
1e6feb95 | 43 | #include "wx/msw/private.h" |
7cf83330 JS |
44 | |
45 | #include "wx/msw/dragimag.h" | |
46 | #include "wx/msw/private.h" | |
47 | ||
eae4425d JS |
48 | #ifdef __WXWINCE__ // for SM_CXCURSOR and SM_CYCURSOR |
49 | #include "wx/msw/wince/missing.h" | |
50 | #endif // __WXWINCE__ | |
51 | ||
3bce6687 JS |
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)) | |
56 | #endif | |
57 | ||
23f681ec VZ |
58 | // ---------------------------------------------------------------------------- |
59 | // macros | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
7cf83330 | 62 | IMPLEMENT_DYNAMIC_CLASS(wxDragImage, wxObject) |
7cf83330 | 63 | |
23f681ec VZ |
64 | #define GetHimageList() ((HIMAGELIST) m_hImageList) |
65 | ||
66 | // ============================================================================ | |
67 | // implementation | |
68 | // ============================================================================ | |
69 | ||
70 | // ---------------------------------------------------------------------------- | |
71 | // wxDragImage ctors/dtor | |
72 | // ---------------------------------------------------------------------------- | |
73 | ||
7cf83330 JS |
74 | wxDragImage::wxDragImage() |
75 | { | |
68be9f09 | 76 | Init(); |
7cf83330 JS |
77 | } |
78 | ||
79 | wxDragImage::~wxDragImage() | |
80 | { | |
23f681ec VZ |
81 | if ( m_hImageList ) |
82 | ImageList_Destroy(GetHimageList()); | |
6ea5c52d | 83 | #if !wxUSE_SIMPLER_DRAGIMAGE |
68be9f09 JS |
84 | if ( m_hCursorImageList ) |
85 | ImageList_Destroy((HIMAGELIST) m_hCursorImageList); | |
6ea5c52d | 86 | #endif |
7cf83330 JS |
87 | } |
88 | ||
68be9f09 JS |
89 | void wxDragImage::Init() |
90 | { | |
91 | m_hImageList = 0; | |
6ea5c52d | 92 | #if !wxUSE_SIMPLER_DRAGIMAGE |
68be9f09 | 93 | m_hCursorImageList = 0; |
6ea5c52d | 94 | #endif |
68be9f09 | 95 | m_window = (wxWindow*) NULL; |
d71cc120 | 96 | m_fullScreen = false; |
68be9f09 | 97 | } |
7cf83330 JS |
98 | |
99 | // Attributes | |
100 | //////////////////////////////////////////////////////////////////////////// | |
101 | ||
102 | ||
103 | // Operations | |
104 | //////////////////////////////////////////////////////////////////////////// | |
105 | ||
106 | // Create a drag image from a bitmap and optional cursor | |
aa2d25a5 | 107 | bool wxDragImage::Create(const wxBitmap& image, const wxCursor& cursor) |
7cf83330 | 108 | { |
23f681ec VZ |
109 | if ( m_hImageList ) |
110 | ImageList_Destroy(GetHimageList()); | |
111 | m_hImageList = 0; | |
7cf83330 | 112 | |
4676948b | 113 | #ifdef __WXWINCE__ |
8a476ece | 114 | UINT flags = ILC_COLOR; |
4676948b | 115 | #else |
5cb598ae | 116 | UINT flags wxDUMMY_INITIALIZE(0) ; |
68be9f09 JS |
117 | if (image.GetDepth() <= 4) |
118 | flags = ILC_COLOR4; | |
119 | else if (image.GetDepth() <= 8) | |
120 | flags = ILC_COLOR8; | |
121 | else if (image.GetDepth() <= 16) | |
122 | flags = ILC_COLOR16; | |
123 | else if (image.GetDepth() <= 24) | |
124 | flags = ILC_COLOR24; | |
125 | else | |
126 | flags = ILC_COLOR32; | |
4676948b | 127 | #endif |
68be9f09 JS |
128 | |
129 | bool mask = (image.GetMask() != 0); | |
2b5f62a0 VZ |
130 | |
131 | // Curiously, even if the image doesn't have a mask, | |
132 | // we still have to use ILC_MASK or the image won't show | |
133 | // up when dragged. | |
134 | // if ( mask ) | |
135 | flags |= ILC_MASK; | |
7cf83330 | 136 | |
23f681ec | 137 | m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1); |
7cf83330 | 138 | |
68be9f09 JS |
139 | int index; |
140 | if (!mask) | |
141 | { | |
142 | HBITMAP hBitmap1 = (HBITMAP) image.GetHBITMAP(); | |
143 | index = ImageList_Add(GetHimageList(), hBitmap1, 0); | |
144 | } | |
145 | else | |
146 | { | |
147 | HBITMAP hBitmap1 = (HBITMAP) image.GetHBITMAP(); | |
148 | HBITMAP hBitmap2 = (HBITMAP) image.GetMask()->GetMaskBitmap(); | |
149 | HBITMAP hbmpMask = wxInvertMask(hBitmap2); | |
7cf83330 | 150 | |
68be9f09 JS |
151 | index = ImageList_Add(GetHimageList(), hBitmap1, hbmpMask); |
152 | ::DeleteObject(hbmpMask); | |
153 | } | |
23f681ec | 154 | if ( index == -1 ) |
7cf83330 JS |
155 | { |
156 | wxLogError(_("Couldn't add an image to the image list.")); | |
157 | } | |
7cf83330 | 158 | m_cursor = cursor; // Can only combine with drag image after calling BeginDrag. |
7cf83330 JS |
159 | |
160 | return (index != -1) ; | |
161 | } | |
23f681ec | 162 | |
7cf83330 | 163 | // Create a drag image from an icon and optional cursor |
aa2d25a5 | 164 | bool wxDragImage::Create(const wxIcon& image, const wxCursor& cursor) |
7cf83330 | 165 | { |
23f681ec VZ |
166 | if ( m_hImageList ) |
167 | ImageList_Destroy(GetHimageList()); | |
168 | m_hImageList = 0; | |
7cf83330 | 169 | |
4676948b | 170 | #ifdef __WXWINCE__ |
5cb598ae | 171 | UINT flags = ILC_COLOR; |
4676948b | 172 | #else |
5cb598ae | 173 | UINT flags wxDUMMY_INITIALIZE(0) ; |
68be9f09 JS |
174 | if (image.GetDepth() <= 4) |
175 | flags = ILC_COLOR4; | |
176 | else if (image.GetDepth() <= 8) | |
177 | flags = ILC_COLOR8; | |
178 | else if (image.GetDepth() <= 16) | |
179 | flags = ILC_COLOR16; | |
180 | else if (image.GetDepth() <= 24) | |
181 | flags = ILC_COLOR24; | |
182 | else | |
183 | flags = ILC_COLOR32; | |
4676948b | 184 | #endif |
d71cc120 | 185 | bool mask = true; |
23f681ec VZ |
186 | if ( mask ) |
187 | flags |= ILC_MASK; | |
7cf83330 | 188 | |
23f681ec | 189 | m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1); |
7cf83330 | 190 | |
23f681ec | 191 | HICON hIcon = (HICON) image.GetHICON(); |
7cf83330 | 192 | |
23f681ec VZ |
193 | int index = ImageList_AddIcon(GetHimageList(), hIcon); |
194 | if ( index == -1 ) | |
7cf83330 JS |
195 | { |
196 | wxLogError(_("Couldn't add an image to the image list.")); | |
197 | } | |
198 | ||
199 | m_cursor = cursor; // Can only combine with drag image after calling BeginDrag. | |
7cf83330 JS |
200 | |
201 | return (index != -1) ; | |
202 | } | |
23f681ec | 203 | |
7cf83330 | 204 | // Create a drag image from a string and optional cursor |
aa2d25a5 | 205 | bool wxDragImage::Create(const wxString& str, const wxCursor& cursor) |
7cf83330 | 206 | { |
a756f210 | 207 | wxFont font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); |
7cf83330 | 208 | |
8d7eaf91 | 209 | long w = 0, h = 0; |
7cf83330 JS |
210 | wxScreenDC dc; |
211 | dc.SetFont(font); | |
212 | dc.GetTextExtent(str, & w, & h); | |
68be9f09 | 213 | dc.SetFont(wxNullFont); |
7cf83330 JS |
214 | |
215 | wxMemoryDC dc2; | |
216 | dc2.SetFont(font); | |
68be9f09 | 217 | wxBitmap bitmap((int) w+2, (int) h+2); |
7cf83330 JS |
218 | dc2.SelectObject(bitmap); |
219 | ||
220 | dc2.SetBackground(* wxWHITE_BRUSH); | |
221 | dc2.Clear(); | |
68be9f09 JS |
222 | dc2.SetBackgroundMode(wxTRANSPARENT); |
223 | dc2.SetTextForeground(* wxLIGHT_GREY); | |
7cf83330 | 224 | dc2.DrawText(str, 0, 0); |
68be9f09 JS |
225 | dc2.DrawText(str, 1, 0); |
226 | dc2.DrawText(str, 2, 0); | |
227 | dc2.DrawText(str, 1, 1); | |
228 | dc2.DrawText(str, 2, 1); | |
229 | dc2.DrawText(str, 1, 2); | |
230 | dc2.DrawText(str, 2, 2); | |
231 | dc2.SetTextForeground(* wxBLACK); | |
232 | dc2.DrawText(str, 1, 1); | |
7cf83330 JS |
233 | |
234 | dc2.SelectObject(wxNullBitmap); | |
235 | ||
64c288fa | 236 | #if wxUSE_WXDIB |
68be9f09 | 237 | // Make the bitmap masked |
368d59f0 | 238 | wxImage image = bitmap.ConvertToImage(); |
68be9f09 | 239 | image.SetMaskColour(255, 255, 255); |
368d59f0 | 240 | return Create(wxBitmap(image), cursor); |
64c288fa JS |
241 | #else |
242 | return false; | |
243 | #endif | |
7cf83330 JS |
244 | } |
245 | ||
3080bf59 | 246 | #if wxUSE_TREECTRL |
7cf83330 JS |
247 | // Create a drag image for the given tree control item |
248 | bool wxDragImage::Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id) | |
249 | { | |
23f681ec VZ |
250 | if ( m_hImageList ) |
251 | ImageList_Destroy(GetHimageList()); | |
ee4b2721 VZ |
252 | m_hImageList = (WXHIMAGELIST) |
253 | TreeView_CreateDragImage(GetHwndOf(&treeCtrl), (HTREEITEM) id.m_pItem); | |
254 | return m_hImageList != 0; | |
7cf83330 | 255 | } |
3080bf59 | 256 | #endif |
7cf83330 | 257 | |
3080bf59 | 258 | #if wxUSE_LISTCTRL |
7cf83330 JS |
259 | // Create a drag image for the given list control item |
260 | bool wxDragImage::Create(const wxListCtrl& listCtrl, long id) | |
261 | { | |
23f681ec VZ |
262 | if ( m_hImageList ) |
263 | ImageList_Destroy(GetHimageList()); | |
7cf83330 JS |
264 | POINT pt; |
265 | pt.x = 0; pt.y = 0; | |
266 | m_hImageList = (WXHIMAGELIST) ListView_CreateDragImage((HWND) listCtrl.GetHWND(), id, & pt); | |
d71cc120 | 267 | return true; |
7cf83330 | 268 | } |
3080bf59 | 269 | #endif |
7cf83330 JS |
270 | |
271 | // Begin drag | |
68be9f09 | 272 | bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, bool fullScreen, wxRect* rect) |
7cf83330 | 273 | { |
223d09f6 | 274 | wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in BeginDrag.")); |
68be9f09 JS |
275 | wxASSERT_MSG( (window != 0), wxT("Window must not be null in BeginDrag.")); |
276 | ||
277 | m_fullScreen = fullScreen; | |
278 | if (rect) | |
279 | m_boundingRect = * rect; | |
7cf83330 | 280 | |
23f681ec | 281 | bool ret = (ImageList_BeginDrag(GetHimageList(), 0, hotspot.x, hotspot.y) != 0); |
7cf83330 JS |
282 | |
283 | if (!ret) | |
23f681ec VZ |
284 | { |
285 | wxFAIL_MSG( _T("BeginDrag failed.") ); | |
286 | ||
d71cc120 | 287 | return false; |
23f681ec | 288 | } |
7cf83330 JS |
289 | |
290 | if (m_cursor.Ok()) | |
291 | { | |
6ea5c52d | 292 | #if wxUSE_SIMPLER_DRAGIMAGE |
d71cc120 WS |
293 | m_oldCursor = window->GetCursor(); |
294 | window->SetCursor(m_cursor); | |
6ea5c52d | 295 | #else |
68be9f09 | 296 | if (!m_hCursorImageList) |
d71cc120 | 297 | { |
c1dfa9eb | 298 | #ifndef SM_CXCURSOR |
88a7a4e1 WS |
299 | // Smartphone may not have these metric symbol |
300 | int cxCursor = 16; | |
c1dfa9eb JS |
301 | int cyCursor = 16; |
302 | #else | |
46697f31 WS |
303 | int cxCursor = ::GetSystemMetrics(SM_CXCURSOR); |
304 | int cyCursor = ::GetSystemMetrics(SM_CYCURSOR); | |
c1dfa9eb | 305 | #endif |
68be9f09 JS |
306 | m_hCursorImageList = (WXHIMAGELIST) ImageList_Create(cxCursor, cyCursor, ILC_MASK, 1, 1); |
307 | } | |
308 | ||
aa2d25a5 JS |
309 | // See if we can find the cursor hotspot |
310 | wxPoint curHotSpot(hotspot); | |
311 | ||
6da8e679 JS |
312 | // Although it seems to produce the right position, when the hotspot goeos |
313 | // negative it has strange effects on the image. | |
314 | // How do we stop the cursor jumping right and below of where it should be? | |
aa2d25a5 JS |
315 | #if 0 |
316 | ICONINFO iconInfo; | |
317 | if (::GetIconInfo((HICON) (HCURSOR) m_cursor.GetHCURSOR(), & iconInfo) != 0) | |
318 | { | |
319 | curHotSpot.x -= iconInfo.xHotspot; | |
320 | curHotSpot.y -= iconInfo.yHotspot; | |
321 | } | |
322 | #endif | |
6da8e679 JS |
323 | //wxString msg; |
324 | //msg.Printf("Hotspot = %d, %d", curHotSpot.x, curHotSpot.y); | |
325 | //wxLogDebug(msg); | |
aa2d25a5 | 326 | |
7cf83330 | 327 | // First add the cursor to the image list |
68be9f09 JS |
328 | HCURSOR hCursor = (HCURSOR) m_cursor.GetHCURSOR(); |
329 | int cursorIndex = ImageList_AddIcon((HIMAGELIST) m_hCursorImageList, (HICON) hCursor); | |
7cf83330 | 330 | |
223d09f6 | 331 | wxASSERT_MSG( (cursorIndex != -1), wxT("ImageList_AddIcon failed in BeginDrag.")); |
7cf83330 JS |
332 | |
333 | if (cursorIndex != -1) | |
334 | { | |
aa2d25a5 | 335 | ImageList_SetDragCursorImage((HIMAGELIST) m_hCursorImageList, cursorIndex, curHotSpot.x, curHotSpot.y); |
7cf83330 | 336 | } |
6ea5c52d | 337 | #endif |
7cf83330 JS |
338 | } |
339 | ||
6ea5c52d JS |
340 | #if !wxUSE_SIMPLER_DRAGIMAGE |
341 | if (m_cursor.Ok()) | |
342 | ::ShowCursor(FALSE); | |
343 | #endif | |
68be9f09 | 344 | |
aa2d25a5 JS |
345 | m_window = window; |
346 | ||
23f681ec | 347 | ::SetCapture(GetHwndOf(window)); |
7cf83330 | 348 | |
d71cc120 | 349 | return true; |
7cf83330 | 350 | } |
23f681ec | 351 | |
68be9f09 JS |
352 | // Begin drag. hotspot is the location of the drag position relative to the upper-left |
353 | // corner of the image. This is full screen only. fullScreenRect gives the | |
354 | // position of the window on the screen, to restrict the drag to. | |
355 | bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, wxWindow* fullScreenRect) | |
356 | { | |
357 | wxRect rect; | |
358 | ||
359 | int x = fullScreenRect->GetPosition().x; | |
360 | int y = fullScreenRect->GetPosition().y; | |
d71cc120 | 361 | |
68be9f09 JS |
362 | wxSize sz = fullScreenRect->GetSize(); |
363 | ||
364 | if (fullScreenRect->GetParent() && !fullScreenRect->IsKindOf(CLASSINFO(wxFrame))) | |
365 | fullScreenRect->GetParent()->ClientToScreen(& x, & y); | |
366 | ||
367 | rect.x = x; rect.y = y; | |
368 | rect.width = sz.x; rect.height = sz.y; | |
369 | ||
d71cc120 | 370 | return BeginDrag(hotspot, window, true, & rect); |
68be9f09 JS |
371 | } |
372 | ||
7cf83330 | 373 | // End drag |
68be9f09 | 374 | bool wxDragImage::EndDrag() |
7cf83330 | 375 | { |
223d09f6 | 376 | wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in EndDrag.")); |
7cf83330 JS |
377 | |
378 | ImageList_EndDrag(); | |
379 | ||
23f681ec VZ |
380 | if ( !::ReleaseCapture() ) |
381 | { | |
f6bcfd97 | 382 | wxLogLastError(wxT("ReleaseCapture")); |
23f681ec VZ |
383 | } |
384 | ||
6ea5c52d JS |
385 | #if wxUSE_SIMPLER_DRAGIMAGE |
386 | if (m_cursor.Ok() && m_oldCursor.Ok()) | |
d71cc120 | 387 | m_window->SetCursor(m_oldCursor); |
6ea5c52d | 388 | #else |
7cf83330 | 389 | ::ShowCursor(TRUE); |
6ea5c52d JS |
390 | #endif |
391 | ||
68be9f09 | 392 | m_window = (wxWindow*) NULL; |
7cf83330 | 393 | |
d71cc120 | 394 | return true; |
7cf83330 | 395 | } |
23f681ec | 396 | |
7cf83330 JS |
397 | // Move the image: call from OnMouseMove. Pt is in window client coordinates if window |
398 | // is non-NULL, or in screen coordinates if NULL. | |
68be9f09 | 399 | bool wxDragImage::Move(const wxPoint& pt) |
7cf83330 | 400 | { |
223d09f6 | 401 | wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Move.")); |
7cf83330 | 402 | |
6ea5c52d JS |
403 | // These are in window, not client coordinates. |
404 | // So need to convert to client coordinates. | |
405 | wxPoint pt2(pt); | |
0c96db12 | 406 | if (m_window && !m_fullScreen) |
6ea5c52d JS |
407 | { |
408 | RECT rect; | |
409 | rect.left = 0; rect.top = 0; | |
410 | rect.right = 0; rect.bottom = 0; | |
411 | DWORD style = ::GetWindowLong((HWND) m_window->GetHWND(), GWL_STYLE); | |
412 | #ifdef __WIN32__ | |
413 | DWORD exStyle = ::GetWindowLong((HWND) m_window->GetHWND(), GWL_EXSTYLE); | |
414 | ::AdjustWindowRectEx(& rect, style, FALSE, exStyle); | |
415 | #else | |
416 | ::AdjustWindowRect(& rect, style, FALSE); | |
417 | #endif | |
418 | // Subtract the (negative) values, i.e. add a small increment | |
419 | pt2.x -= rect.left; pt2.y -= rect.top; | |
420 | } | |
aa2d25a5 JS |
421 | else if (m_window && m_fullScreen) |
422 | { | |
423 | pt2 = m_window->ClientToScreen(pt2); | |
424 | } | |
6ea5c52d JS |
425 | |
426 | bool ret = (ImageList_DragMove( pt2.x, pt2.y ) != 0); | |
7cf83330 | 427 | |
0c96db12 | 428 | m_position = pt2; |
7cf83330 JS |
429 | |
430 | return ret; | |
431 | } | |
432 | ||
68be9f09 | 433 | bool wxDragImage::Show() |
7cf83330 | 434 | { |
223d09f6 | 435 | wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Show.")); |
7cf83330 JS |
436 | |
437 | HWND hWnd = 0; | |
68be9f09 JS |
438 | if (m_window && !m_fullScreen) |
439 | hWnd = (HWND) m_window->GetHWND(); | |
7cf83330 JS |
440 | |
441 | bool ret = (ImageList_DragEnter( hWnd, m_position.x, m_position.y ) != 0); | |
442 | ||
443 | return ret; | |
444 | } | |
445 | ||
68be9f09 | 446 | bool wxDragImage::Hide() |
7cf83330 | 447 | { |
223d09f6 | 448 | wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Hide.")); |
7cf83330 JS |
449 | |
450 | HWND hWnd = 0; | |
68be9f09 JS |
451 | if (m_window && !m_fullScreen) |
452 | hWnd = (HWND) m_window->GetHWND(); | |
7cf83330 JS |
453 | |
454 | bool ret = (ImageList_DragLeave( hWnd ) != 0); | |
455 | ||
456 | return ret; | |
457 | } | |
458 | ||
fa8dca8d | 459 | #endif // wxUSE_DRAGIMAGE |