]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dragimag.cpp
Add wxDataViewCtrl::GTKPathToItem() function and use it.
[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
JS
98
99// Attributes
100////////////////////////////////////////////////////////////////////////////
101
102
103// Operations
104////////////////////////////////////////////////////////////////////////////
105
106// Create a drag image from a bitmap and optional cursor
aa2d25a5 107bool 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 164bool 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
d19ebfaa
PC
185
186 flags |= ILC_MASK;
7cf83330 187
23f681ec 188 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
7cf83330 189
23f681ec 190 HICON hIcon = (HICON) image.GetHICON();
7cf83330 191
23f681ec
VZ
192 int index = ImageList_AddIcon(GetHimageList(), hIcon);
193 if ( index == -1 )
7cf83330
JS
194 {
195 wxLogError(_("Couldn't add an image to the image list."));
196 }
197
198 m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
7cf83330
JS
199
200 return (index != -1) ;
201}
23f681ec 202
7cf83330 203// Create a drag image from a string and optional cursor
aa2d25a5 204bool wxDragImage::Create(const wxString& str, const wxCursor& cursor)
7cf83330 205{
a756f210 206 wxFont font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
7cf83330 207
f3a8b1b6 208 wxCoord w = 0, h = 0;
7cf83330
JS
209 wxScreenDC dc;
210 dc.SetFont(font);
211 dc.GetTextExtent(str, & w, & h);
68be9f09 212 dc.SetFont(wxNullFont);
7cf83330
JS
213
214 wxMemoryDC dc2;
215 dc2.SetFont(font);
68be9f09 216 wxBitmap bitmap((int) w+2, (int) h+2);
7cf83330
JS
217 dc2.SelectObject(bitmap);
218
219 dc2.SetBackground(* wxWHITE_BRUSH);
220 dc2.Clear();
cb129171 221 dc2.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
68be9f09 222 dc2.SetTextForeground(* wxLIGHT_GREY);
7cf83330 223 dc2.DrawText(str, 0, 0);
68be9f09
JS
224 dc2.DrawText(str, 1, 0);
225 dc2.DrawText(str, 2, 0);
226 dc2.DrawText(str, 1, 1);
227 dc2.DrawText(str, 2, 1);
228 dc2.DrawText(str, 1, 2);
229 dc2.DrawText(str, 2, 2);
230 dc2.SetTextForeground(* wxBLACK);
231 dc2.DrawText(str, 1, 1);
7cf83330
JS
232
233 dc2.SelectObject(wxNullBitmap);
234
64c288fa 235#if wxUSE_WXDIB
68be9f09 236 // Make the bitmap masked
368d59f0 237 wxImage image = bitmap.ConvertToImage();
68be9f09 238 image.SetMaskColour(255, 255, 255);
368d59f0 239 return Create(wxBitmap(image), cursor);
64c288fa
JS
240#else
241 return false;
242#endif
7cf83330
JS
243}
244
3080bf59 245#if wxUSE_TREECTRL
7cf83330
JS
246// Create a drag image for the given tree control item
247bool wxDragImage::Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id)
248{
23f681ec
VZ
249 if ( m_hImageList )
250 ImageList_Destroy(GetHimageList());
ee4b2721
VZ
251 m_hImageList = (WXHIMAGELIST)
252 TreeView_CreateDragImage(GetHwndOf(&treeCtrl), (HTREEITEM) id.m_pItem);
19225888
VZ
253 if ( !m_hImageList )
254 {
255 // fall back on just the item text if there is no image
256 return Create(treeCtrl.GetItemText(id));
257 }
258
259 return true;
7cf83330 260}
3080bf59 261#endif
7cf83330 262
3080bf59 263#if wxUSE_LISTCTRL
7cf83330
JS
264// Create a drag image for the given list control item
265bool wxDragImage::Create(const wxListCtrl& listCtrl, long id)
266{
23f681ec
VZ
267 if ( m_hImageList )
268 ImageList_Destroy(GetHimageList());
7cf83330 269 POINT pt;
19225888
VZ
270 pt.x =
271 pt.y = 0;
272 m_hImageList = (WXHIMAGELIST)
273 ListView_CreateDragImage(GetHwndOf(&listCtrl), id, &pt);
274
275 if ( !m_hImageList )
276 {
277 // as for wxTreeCtrl, fall back on dragging just the item text
278 return Create(listCtrl.GetItemText(id));
279 }
280
d71cc120 281 return true;
7cf83330 282}
3080bf59 283#endif
7cf83330
JS
284
285// Begin drag
68be9f09 286bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, bool fullScreen, wxRect* rect)
7cf83330 287{
223d09f6 288 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in BeginDrag."));
68be9f09
JS
289 wxASSERT_MSG( (window != 0), wxT("Window must not be null in BeginDrag."));
290
291 m_fullScreen = fullScreen;
292 if (rect)
293 m_boundingRect = * rect;
7cf83330 294
23f681ec 295 bool ret = (ImageList_BeginDrag(GetHimageList(), 0, hotspot.x, hotspot.y) != 0);
7cf83330
JS
296
297 if (!ret)
23f681ec 298 {
9a83f860 299 wxFAIL_MSG( wxT("BeginDrag failed.") );
23f681ec 300
d71cc120 301 return false;
23f681ec 302 }
7cf83330
JS
303
304 if (m_cursor.Ok())
305 {
6ea5c52d 306#if wxUSE_SIMPLER_DRAGIMAGE
d71cc120
WS
307 m_oldCursor = window->GetCursor();
308 window->SetCursor(m_cursor);
6ea5c52d 309#else
68be9f09 310 if (!m_hCursorImageList)
d71cc120 311 {
c1dfa9eb 312#ifndef SM_CXCURSOR
88a7a4e1
WS
313 // Smartphone may not have these metric symbol
314 int cxCursor = 16;
c1dfa9eb
JS
315 int cyCursor = 16;
316#else
46697f31
WS
317 int cxCursor = ::GetSystemMetrics(SM_CXCURSOR);
318 int cyCursor = ::GetSystemMetrics(SM_CYCURSOR);
c1dfa9eb 319#endif
68be9f09
JS
320 m_hCursorImageList = (WXHIMAGELIST) ImageList_Create(cxCursor, cyCursor, ILC_MASK, 1, 1);
321 }
322
aa2d25a5
JS
323 // See if we can find the cursor hotspot
324 wxPoint curHotSpot(hotspot);
325
6da8e679
JS
326 // Although it seems to produce the right position, when the hotspot goeos
327 // negative it has strange effects on the image.
328 // How do we stop the cursor jumping right and below of where it should be?
aa2d25a5
JS
329#if 0
330 ICONINFO iconInfo;
331 if (::GetIconInfo((HICON) (HCURSOR) m_cursor.GetHCURSOR(), & iconInfo) != 0)
332 {
333 curHotSpot.x -= iconInfo.xHotspot;
334 curHotSpot.y -= iconInfo.yHotspot;
335 }
336#endif
6da8e679
JS
337 //wxString msg;
338 //msg.Printf("Hotspot = %d, %d", curHotSpot.x, curHotSpot.y);
339 //wxLogDebug(msg);
aa2d25a5 340
7cf83330 341 // First add the cursor to the image list
68be9f09
JS
342 HCURSOR hCursor = (HCURSOR) m_cursor.GetHCURSOR();
343 int cursorIndex = ImageList_AddIcon((HIMAGELIST) m_hCursorImageList, (HICON) hCursor);
7cf83330 344
223d09f6 345 wxASSERT_MSG( (cursorIndex != -1), wxT("ImageList_AddIcon failed in BeginDrag."));
7cf83330
JS
346
347 if (cursorIndex != -1)
348 {
aa2d25a5 349 ImageList_SetDragCursorImage((HIMAGELIST) m_hCursorImageList, cursorIndex, curHotSpot.x, curHotSpot.y);
7cf83330 350 }
6ea5c52d 351#endif
7cf83330
JS
352 }
353
6ea5c52d
JS
354#if !wxUSE_SIMPLER_DRAGIMAGE
355 if (m_cursor.Ok())
356 ::ShowCursor(FALSE);
357#endif
68be9f09 358
aa2d25a5
JS
359 m_window = window;
360
23f681ec 361 ::SetCapture(GetHwndOf(window));
7cf83330 362
d71cc120 363 return true;
7cf83330 364}
23f681ec 365
68be9f09
JS
366// Begin drag. hotspot is the location of the drag position relative to the upper-left
367// corner of the image. This is full screen only. fullScreenRect gives the
368// position of the window on the screen, to restrict the drag to.
369bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, wxWindow* fullScreenRect)
370{
371 wxRect rect;
372
373 int x = fullScreenRect->GetPosition().x;
374 int y = fullScreenRect->GetPosition().y;
d71cc120 375
68be9f09
JS
376 wxSize sz = fullScreenRect->GetSize();
377
378 if (fullScreenRect->GetParent() && !fullScreenRect->IsKindOf(CLASSINFO(wxFrame)))
379 fullScreenRect->GetParent()->ClientToScreen(& x, & y);
380
381 rect.x = x; rect.y = y;
382 rect.width = sz.x; rect.height = sz.y;
383
d71cc120 384 return BeginDrag(hotspot, window, true, & rect);
68be9f09
JS
385}
386
7cf83330 387// End drag
68be9f09 388bool wxDragImage::EndDrag()
7cf83330 389{
223d09f6 390 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in EndDrag."));
7cf83330
JS
391
392 ImageList_EndDrag();
393
23f681ec
VZ
394 if ( !::ReleaseCapture() )
395 {
f6bcfd97 396 wxLogLastError(wxT("ReleaseCapture"));
23f681ec
VZ
397 }
398
6ea5c52d
JS
399#if wxUSE_SIMPLER_DRAGIMAGE
400 if (m_cursor.Ok() && m_oldCursor.Ok())
d71cc120 401 m_window->SetCursor(m_oldCursor);
6ea5c52d 402#else
7cf83330 403 ::ShowCursor(TRUE);
6ea5c52d
JS
404#endif
405
d3b9f782 406 m_window = NULL;
7cf83330 407
d71cc120 408 return true;
7cf83330 409}
23f681ec 410
7cf83330
JS
411// Move the image: call from OnMouseMove. Pt is in window client coordinates if window
412// is non-NULL, or in screen coordinates if NULL.
68be9f09 413bool wxDragImage::Move(const wxPoint& pt)
7cf83330 414{
223d09f6 415 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Move."));
7cf83330 416
6ea5c52d
JS
417 // These are in window, not client coordinates.
418 // So need to convert to client coordinates.
419 wxPoint pt2(pt);
0c96db12 420 if (m_window && !m_fullScreen)
6ea5c52d
JS
421 {
422 RECT rect;
423 rect.left = 0; rect.top = 0;
424 rect.right = 0; rect.bottom = 0;
425 DWORD style = ::GetWindowLong((HWND) m_window->GetHWND(), GWL_STYLE);
426#ifdef __WIN32__
427 DWORD exStyle = ::GetWindowLong((HWND) m_window->GetHWND(), GWL_EXSTYLE);
428 ::AdjustWindowRectEx(& rect, style, FALSE, exStyle);
429#else
430 ::AdjustWindowRect(& rect, style, FALSE);
431#endif
432 // Subtract the (negative) values, i.e. add a small increment
433 pt2.x -= rect.left; pt2.y -= rect.top;
434 }
aa2d25a5
JS
435 else if (m_window && m_fullScreen)
436 {
437 pt2 = m_window->ClientToScreen(pt2);
438 }
6ea5c52d
JS
439
440 bool ret = (ImageList_DragMove( pt2.x, pt2.y ) != 0);
7cf83330 441
0c96db12 442 m_position = pt2;
7cf83330
JS
443
444 return ret;
445}
446
68be9f09 447bool wxDragImage::Show()
7cf83330 448{
223d09f6 449 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Show."));
7cf83330
JS
450
451 HWND hWnd = 0;
68be9f09
JS
452 if (m_window && !m_fullScreen)
453 hWnd = (HWND) m_window->GetHWND();
7cf83330
JS
454
455 bool ret = (ImageList_DragEnter( hWnd, m_position.x, m_position.y ) != 0);
456
457 return ret;
458}
459
68be9f09 460bool wxDragImage::Hide()
7cf83330 461{
223d09f6 462 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Hide."));
7cf83330
JS
463
464 HWND hWnd = 0;
68be9f09
JS
465 if (m_window && !m_fullScreen)
466 hWnd = (HWND) m_window->GetHWND();
7cf83330
JS
467
468 bool ret = (ImageList_DragLeave( hWnd ) != 0);
469
470 return ret;
471}
472
fa8dca8d 473#endif // wxUSE_DRAGIMAGE