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