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