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