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