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