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