]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dragimag.cpp
Test using wxString::ToCDouble() in wxAny.
[wxWidgets.git] / src / msw / dragimag.cpp
CommitLineData
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 62IMPLEMENT_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
74wxDragImage::wxDragImage()
75{
68be9f09 76 Init();
7cf83330
JS
77}
78
79wxDragImage::~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
89void wxDragImage::Init()
90{
91 m_hImageList = 0;
6ea5c52d 92#if !wxUSE_SIMPLER_DRAGIMAGE
68be9f09 93 m_hCursorImageList = 0;
6ea5c52d 94#endif
d3b9f782 95 m_window = NULL;
d71cc120 96 m_fullScreen = false;
68be9f09 97}
7cf83330 98
82b30473
VZ
99#if WXWIN_COMPATIBILITY_2_8
100wxDragImage::wxDragImage(const wxBitmap& image, const wxCursor& cursor, const wxPoint& WXUNUSED(cursorHotspot))
101{
102 Init();
103
104 Create(image, cursor);
105}
106
107wxDragImage::wxDragImage(const wxIcon& image, const wxCursor& cursor, const wxPoint& WXUNUSED(cursorHotspot))
108{
109 Init();
110
111 Create(image, cursor);
112}
113
114wxDragImage::wxDragImage(const wxString& str, const wxCursor& cursor, const wxPoint& WXUNUSED(cursorHotspot))
115{
116 Init();
117
118 Create(str, cursor);
119}
120
121bool wxDragImage::Create(const wxBitmap& image, const wxCursor& cursor, const wxPoint& WXUNUSED(cursorHotspot))
122{
123 return Create(image, cursor);
124}
125
126bool wxDragImage::Create(const wxIcon& image, const wxCursor& cursor, const wxPoint& WXUNUSED(cursorHotspot))
127{
128 return Create(image, cursor);
129}
130
131bool wxDragImage::Create(const wxString& str, const wxCursor& cursor, const wxPoint& WXUNUSED(cursorHotspot))
132{
133 return Create(str, cursor);
134}
135#endif // WXWIN_COMPATIBILITY_2_8
136
7cf83330
JS
137// Attributes
138////////////////////////////////////////////////////////////////////////////
139
140
141// Operations
142////////////////////////////////////////////////////////////////////////////
143
144// Create a drag image from a bitmap and optional cursor
aa2d25a5 145bool wxDragImage::Create(const wxBitmap& image, const wxCursor& cursor)
7cf83330 146{
23f681ec
VZ
147 if ( m_hImageList )
148 ImageList_Destroy(GetHimageList());
149 m_hImageList = 0;
7cf83330 150
4676948b 151#ifdef __WXWINCE__
8a476ece 152 UINT flags = ILC_COLOR;
4676948b 153#else
5cb598ae 154 UINT flags wxDUMMY_INITIALIZE(0) ;
68be9f09
JS
155 if (image.GetDepth() <= 4)
156 flags = ILC_COLOR4;
157 else if (image.GetDepth() <= 8)
158 flags = ILC_COLOR8;
159 else if (image.GetDepth() <= 16)
160 flags = ILC_COLOR16;
161 else if (image.GetDepth() <= 24)
162 flags = ILC_COLOR24;
163 else
164 flags = ILC_COLOR32;
4676948b 165#endif
68be9f09
JS
166
167 bool mask = (image.GetMask() != 0);
2b5f62a0
VZ
168
169 // Curiously, even if the image doesn't have a mask,
170 // we still have to use ILC_MASK or the image won't show
171 // up when dragged.
172// if ( mask )
173 flags |= ILC_MASK;
7cf83330 174
23f681ec 175 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
7cf83330 176
68be9f09
JS
177 int index;
178 if (!mask)
179 {
180 HBITMAP hBitmap1 = (HBITMAP) image.GetHBITMAP();
181 index = ImageList_Add(GetHimageList(), hBitmap1, 0);
182 }
183 else
184 {
185 HBITMAP hBitmap1 = (HBITMAP) image.GetHBITMAP();
186 HBITMAP hBitmap2 = (HBITMAP) image.GetMask()->GetMaskBitmap();
187 HBITMAP hbmpMask = wxInvertMask(hBitmap2);
7cf83330 188
68be9f09
JS
189 index = ImageList_Add(GetHimageList(), hBitmap1, hbmpMask);
190 ::DeleteObject(hbmpMask);
191 }
23f681ec 192 if ( index == -1 )
7cf83330
JS
193 {
194 wxLogError(_("Couldn't add an image to the image list."));
195 }
7cf83330 196 m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
7cf83330
JS
197
198 return (index != -1) ;
199}
23f681ec 200
7cf83330 201// Create a drag image from an icon and optional cursor
aa2d25a5 202bool wxDragImage::Create(const wxIcon& image, const wxCursor& cursor)
7cf83330 203{
23f681ec
VZ
204 if ( m_hImageList )
205 ImageList_Destroy(GetHimageList());
206 m_hImageList = 0;
7cf83330 207
4676948b 208#ifdef __WXWINCE__
5cb598ae 209 UINT flags = ILC_COLOR;
4676948b 210#else
5cb598ae 211 UINT flags wxDUMMY_INITIALIZE(0) ;
68be9f09
JS
212 if (image.GetDepth() <= 4)
213 flags = ILC_COLOR4;
214 else if (image.GetDepth() <= 8)
215 flags = ILC_COLOR8;
216 else if (image.GetDepth() <= 16)
217 flags = ILC_COLOR16;
218 else if (image.GetDepth() <= 24)
219 flags = ILC_COLOR24;
220 else
221 flags = ILC_COLOR32;
4676948b 222#endif
d19ebfaa
PC
223
224 flags |= ILC_MASK;
7cf83330 225
23f681ec 226 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
7cf83330 227
23f681ec 228 HICON hIcon = (HICON) image.GetHICON();
7cf83330 229
23f681ec
VZ
230 int index = ImageList_AddIcon(GetHimageList(), hIcon);
231 if ( index == -1 )
7cf83330
JS
232 {
233 wxLogError(_("Couldn't add an image to the image list."));
234 }
235
236 m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
7cf83330
JS
237
238 return (index != -1) ;
239}
23f681ec 240
7cf83330 241// Create a drag image from a string and optional cursor
aa2d25a5 242bool wxDragImage::Create(const wxString& str, const wxCursor& cursor)
7cf83330 243{
a756f210 244 wxFont font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
7cf83330 245
f3a8b1b6 246 wxCoord w = 0, h = 0;
7cf83330
JS
247 wxScreenDC dc;
248 dc.SetFont(font);
249 dc.GetTextExtent(str, & w, & h);
68be9f09 250 dc.SetFont(wxNullFont);
7cf83330
JS
251
252 wxMemoryDC dc2;
253 dc2.SetFont(font);
68be9f09 254 wxBitmap bitmap((int) w+2, (int) h+2);
7cf83330
JS
255 dc2.SelectObject(bitmap);
256
257 dc2.SetBackground(* wxWHITE_BRUSH);
258 dc2.Clear();
cb129171 259 dc2.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
68be9f09 260 dc2.SetTextForeground(* wxLIGHT_GREY);
7cf83330 261 dc2.DrawText(str, 0, 0);
68be9f09
JS
262 dc2.DrawText(str, 1, 0);
263 dc2.DrawText(str, 2, 0);
264 dc2.DrawText(str, 1, 1);
265 dc2.DrawText(str, 2, 1);
266 dc2.DrawText(str, 1, 2);
267 dc2.DrawText(str, 2, 2);
268 dc2.SetTextForeground(* wxBLACK);
269 dc2.DrawText(str, 1, 1);
7cf83330
JS
270
271 dc2.SelectObject(wxNullBitmap);
272
64c288fa 273#if wxUSE_WXDIB
68be9f09 274 // Make the bitmap masked
368d59f0 275 wxImage image = bitmap.ConvertToImage();
68be9f09 276 image.SetMaskColour(255, 255, 255);
368d59f0 277 return Create(wxBitmap(image), cursor);
64c288fa
JS
278#else
279 return false;
280#endif
7cf83330
JS
281}
282
3080bf59 283#if wxUSE_TREECTRL
7cf83330
JS
284// Create a drag image for the given tree control item
285bool wxDragImage::Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id)
286{
23f681ec
VZ
287 if ( m_hImageList )
288 ImageList_Destroy(GetHimageList());
ee4b2721
VZ
289 m_hImageList = (WXHIMAGELIST)
290 TreeView_CreateDragImage(GetHwndOf(&treeCtrl), (HTREEITEM) id.m_pItem);
19225888
VZ
291 if ( !m_hImageList )
292 {
293 // fall back on just the item text if there is no image
294 return Create(treeCtrl.GetItemText(id));
295 }
296
297 return true;
7cf83330 298}
3080bf59 299#endif
7cf83330 300
3080bf59 301#if wxUSE_LISTCTRL
7cf83330
JS
302// Create a drag image for the given list control item
303bool wxDragImage::Create(const wxListCtrl& listCtrl, long id)
304{
23f681ec
VZ
305 if ( m_hImageList )
306 ImageList_Destroy(GetHimageList());
7cf83330 307 POINT pt;
19225888
VZ
308 pt.x =
309 pt.y = 0;
310 m_hImageList = (WXHIMAGELIST)
311 ListView_CreateDragImage(GetHwndOf(&listCtrl), id, &pt);
312
313 if ( !m_hImageList )
314 {
315 // as for wxTreeCtrl, fall back on dragging just the item text
316 return Create(listCtrl.GetItemText(id));
317 }
318
d71cc120 319 return true;
7cf83330 320}
3080bf59 321#endif
7cf83330
JS
322
323// Begin drag
68be9f09 324bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, bool fullScreen, wxRect* rect)
7cf83330 325{
223d09f6 326 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in BeginDrag."));
68be9f09
JS
327 wxASSERT_MSG( (window != 0), wxT("Window must not be null in BeginDrag."));
328
329 m_fullScreen = fullScreen;
330 if (rect)
331 m_boundingRect = * rect;
7cf83330 332
23f681ec 333 bool ret = (ImageList_BeginDrag(GetHimageList(), 0, hotspot.x, hotspot.y) != 0);
7cf83330
JS
334
335 if (!ret)
23f681ec 336 {
9a83f860 337 wxFAIL_MSG( wxT("BeginDrag failed.") );
23f681ec 338
d71cc120 339 return false;
23f681ec 340 }
7cf83330 341
a1b806b9 342 if (m_cursor.IsOk())
7cf83330 343 {
6ea5c52d 344#if wxUSE_SIMPLER_DRAGIMAGE
d71cc120
WS
345 m_oldCursor = window->GetCursor();
346 window->SetCursor(m_cursor);
6ea5c52d 347#else
68be9f09 348 if (!m_hCursorImageList)
d71cc120 349 {
c1dfa9eb 350#ifndef SM_CXCURSOR
88a7a4e1
WS
351 // Smartphone may not have these metric symbol
352 int cxCursor = 16;
c1dfa9eb
JS
353 int cyCursor = 16;
354#else
46697f31
WS
355 int cxCursor = ::GetSystemMetrics(SM_CXCURSOR);
356 int cyCursor = ::GetSystemMetrics(SM_CYCURSOR);
c1dfa9eb 357#endif
68be9f09
JS
358 m_hCursorImageList = (WXHIMAGELIST) ImageList_Create(cxCursor, cyCursor, ILC_MASK, 1, 1);
359 }
360
aa2d25a5
JS
361 // See if we can find the cursor hotspot
362 wxPoint curHotSpot(hotspot);
363
6da8e679
JS
364 // Although it seems to produce the right position, when the hotspot goeos
365 // negative it has strange effects on the image.
366 // How do we stop the cursor jumping right and below of where it should be?
aa2d25a5
JS
367#if 0
368 ICONINFO iconInfo;
369 if (::GetIconInfo((HICON) (HCURSOR) m_cursor.GetHCURSOR(), & iconInfo) != 0)
370 {
371 curHotSpot.x -= iconInfo.xHotspot;
372 curHotSpot.y -= iconInfo.yHotspot;
373 }
374#endif
6da8e679
JS
375 //wxString msg;
376 //msg.Printf("Hotspot = %d, %d", curHotSpot.x, curHotSpot.y);
377 //wxLogDebug(msg);
aa2d25a5 378
7cf83330 379 // First add the cursor to the image list
68be9f09
JS
380 HCURSOR hCursor = (HCURSOR) m_cursor.GetHCURSOR();
381 int cursorIndex = ImageList_AddIcon((HIMAGELIST) m_hCursorImageList, (HICON) hCursor);
7cf83330 382
223d09f6 383 wxASSERT_MSG( (cursorIndex != -1), wxT("ImageList_AddIcon failed in BeginDrag."));
7cf83330
JS
384
385 if (cursorIndex != -1)
386 {
aa2d25a5 387 ImageList_SetDragCursorImage((HIMAGELIST) m_hCursorImageList, cursorIndex, curHotSpot.x, curHotSpot.y);
7cf83330 388 }
6ea5c52d 389#endif
7cf83330
JS
390 }
391
6ea5c52d 392#if !wxUSE_SIMPLER_DRAGIMAGE
a1b806b9 393 if (m_cursor.IsOk())
6ea5c52d
JS
394 ::ShowCursor(FALSE);
395#endif
68be9f09 396
aa2d25a5
JS
397 m_window = window;
398
23f681ec 399 ::SetCapture(GetHwndOf(window));
7cf83330 400
d71cc120 401 return true;
7cf83330 402}
23f681ec 403
68be9f09
JS
404// Begin drag. hotspot is the location of the drag position relative to the upper-left
405// corner of the image. This is full screen only. fullScreenRect gives the
406// position of the window on the screen, to restrict the drag to.
407bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, wxWindow* fullScreenRect)
408{
409 wxRect rect;
410
411 int x = fullScreenRect->GetPosition().x;
412 int y = fullScreenRect->GetPosition().y;
d71cc120 413
68be9f09
JS
414 wxSize sz = fullScreenRect->GetSize();
415
345c78ca 416 if (fullScreenRect->GetParent() && !wxDynamicCast(fullScreenRect, wxFrame))
68be9f09
JS
417 fullScreenRect->GetParent()->ClientToScreen(& x, & y);
418
419 rect.x = x; rect.y = y;
420 rect.width = sz.x; rect.height = sz.y;
421
d71cc120 422 return BeginDrag(hotspot, window, true, & rect);
68be9f09
JS
423}
424
7cf83330 425// End drag
68be9f09 426bool wxDragImage::EndDrag()
7cf83330 427{
223d09f6 428 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in EndDrag."));
7cf83330
JS
429
430 ImageList_EndDrag();
431
23f681ec
VZ
432 if ( !::ReleaseCapture() )
433 {
f6bcfd97 434 wxLogLastError(wxT("ReleaseCapture"));
23f681ec
VZ
435 }
436
6ea5c52d 437#if wxUSE_SIMPLER_DRAGIMAGE
a1b806b9 438 if (m_cursor.IsOk() && m_oldCursor.IsOk())
d71cc120 439 m_window->SetCursor(m_oldCursor);
6ea5c52d 440#else
7cf83330 441 ::ShowCursor(TRUE);
6ea5c52d
JS
442#endif
443
d3b9f782 444 m_window = NULL;
7cf83330 445
d71cc120 446 return true;
7cf83330 447}
23f681ec 448
7cf83330
JS
449// Move the image: call from OnMouseMove. Pt is in window client coordinates if window
450// is non-NULL, or in screen coordinates if NULL.
68be9f09 451bool wxDragImage::Move(const wxPoint& pt)
7cf83330 452{
223d09f6 453 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Move."));
7cf83330 454
6ea5c52d
JS
455 // These are in window, not client coordinates.
456 // So need to convert to client coordinates.
457 wxPoint pt2(pt);
0c96db12 458 if (m_window && !m_fullScreen)
6ea5c52d
JS
459 {
460 RECT rect;
461 rect.left = 0; rect.top = 0;
462 rect.right = 0; rect.bottom = 0;
463 DWORD style = ::GetWindowLong((HWND) m_window->GetHWND(), GWL_STYLE);
464#ifdef __WIN32__
465 DWORD exStyle = ::GetWindowLong((HWND) m_window->GetHWND(), GWL_EXSTYLE);
466 ::AdjustWindowRectEx(& rect, style, FALSE, exStyle);
467#else
468 ::AdjustWindowRect(& rect, style, FALSE);
469#endif
470 // Subtract the (negative) values, i.e. add a small increment
471 pt2.x -= rect.left; pt2.y -= rect.top;
472 }
aa2d25a5
JS
473 else if (m_window && m_fullScreen)
474 {
475 pt2 = m_window->ClientToScreen(pt2);
476 }
6ea5c52d
JS
477
478 bool ret = (ImageList_DragMove( pt2.x, pt2.y ) != 0);
7cf83330 479
0c96db12 480 m_position = pt2;
7cf83330
JS
481
482 return ret;
483}
484
68be9f09 485bool wxDragImage::Show()
7cf83330 486{
223d09f6 487 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Show."));
7cf83330
JS
488
489 HWND hWnd = 0;
68be9f09
JS
490 if (m_window && !m_fullScreen)
491 hWnd = (HWND) m_window->GetHWND();
7cf83330
JS
492
493 bool ret = (ImageList_DragEnter( hWnd, m_position.x, m_position.y ) != 0);
494
495 return ret;
496}
497
68be9f09 498bool wxDragImage::Hide()
7cf83330 499{
223d09f6 500 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Hide."));
7cf83330
JS
501
502 HWND hWnd = 0;
68be9f09
JS
503 if (m_window && !m_fullScreen)
504 hWnd = (HWND) m_window->GetHWND();
7cf83330
JS
505
506 bool ret = (ImageList_DragLeave( hWnd ) != 0);
507
508 return ret;
509}
510
fa8dca8d 511#endif // wxUSE_DRAGIMAGE