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