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