]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dragimag.cpp
applied patch 411059 (mingw win32 api version check)
[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());
68be9f09
JS
81 if ( m_hCursorImageList )
82 ImageList_Destroy((HIMAGELIST) m_hCursorImageList);
7cf83330
JS
83}
84
68be9f09
JS
85void wxDragImage::Init()
86{
87 m_hImageList = 0;
88 m_hCursorImageList = 0;
89 m_window = (wxWindow*) NULL;
90 m_fullScreen = FALSE;
91}
7cf83330
JS
92
93// Attributes
94////////////////////////////////////////////////////////////////////////////
95
96
97// Operations
98////////////////////////////////////////////////////////////////////////////
99
100// Create a drag image from a bitmap and optional cursor
101bool wxDragImage::Create(const wxBitmap& image, const wxCursor& cursor, const wxPoint& hotspot)
102{
23f681ec
VZ
103 if ( m_hImageList )
104 ImageList_Destroy(GetHimageList());
105 m_hImageList = 0;
7cf83330 106
68be9f09
JS
107 UINT flags = 0 ;
108 if (image.GetDepth() <= 4)
109 flags = ILC_COLOR4;
110 else if (image.GetDepth() <= 8)
111 flags = ILC_COLOR8;
112 else if (image.GetDepth() <= 16)
113 flags = ILC_COLOR16;
114 else if (image.GetDepth() <= 24)
115 flags = ILC_COLOR24;
116 else
117 flags = ILC_COLOR32;
118
119 bool mask = (image.GetMask() != 0);
23f681ec
VZ
120 if ( mask )
121 flags |= ILC_MASK;
7cf83330 122
23f681ec 123 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
7cf83330 124
68be9f09
JS
125 int index;
126 if (!mask)
127 {
128 HBITMAP hBitmap1 = (HBITMAP) image.GetHBITMAP();
129 index = ImageList_Add(GetHimageList(), hBitmap1, 0);
130 }
131 else
132 {
133 HBITMAP hBitmap1 = (HBITMAP) image.GetHBITMAP();
134 HBITMAP hBitmap2 = (HBITMAP) image.GetMask()->GetMaskBitmap();
135 HBITMAP hbmpMask = wxInvertMask(hBitmap2);
7cf83330 136
68be9f09
JS
137 index = ImageList_Add(GetHimageList(), hBitmap1, hbmpMask);
138 ::DeleteObject(hbmpMask);
139 }
23f681ec 140 if ( index == -1 )
7cf83330
JS
141 {
142 wxLogError(_("Couldn't add an image to the image list."));
143 }
7cf83330
JS
144 m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
145 m_hotspot = hotspot;
146
147 return (index != -1) ;
148}
23f681ec 149
7cf83330
JS
150// Create a drag image from an icon and optional cursor
151bool wxDragImage::Create(const wxIcon& image, const wxCursor& cursor, const wxPoint& hotspot)
152{
23f681ec
VZ
153 if ( m_hImageList )
154 ImageList_Destroy(GetHimageList());
155 m_hImageList = 0;
7cf83330 156
68be9f09
JS
157 UINT flags = 0 ;
158 if (image.GetDepth() <= 4)
159 flags = ILC_COLOR4;
160 else if (image.GetDepth() <= 8)
161 flags = ILC_COLOR8;
162 else if (image.GetDepth() <= 16)
163 flags = ILC_COLOR16;
164 else if (image.GetDepth() <= 24)
165 flags = ILC_COLOR24;
166 else
167 flags = ILC_COLOR32;
168 bool mask = TRUE;
23f681ec
VZ
169 if ( mask )
170 flags |= ILC_MASK;
7cf83330 171
23f681ec 172 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
7cf83330 173
23f681ec 174 HICON hIcon = (HICON) image.GetHICON();
7cf83330 175
23f681ec
VZ
176 int index = ImageList_AddIcon(GetHimageList(), hIcon);
177 if ( index == -1 )
7cf83330
JS
178 {
179 wxLogError(_("Couldn't add an image to the image list."));
180 }
181
182 m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
183 m_hotspot = hotspot;
184
185 return (index != -1) ;
186}
23f681ec 187
7cf83330
JS
188// Create a drag image from a string and optional cursor
189bool wxDragImage::Create(const wxString& str, const wxCursor& cursor, const wxPoint& hotspot)
190{
191 wxFont font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
192
193 long w, h;
194 wxScreenDC dc;
195 dc.SetFont(font);
196 dc.GetTextExtent(str, & w, & h);
68be9f09 197 dc.SetFont(wxNullFont);
7cf83330
JS
198
199 wxMemoryDC dc2;
200 dc2.SetFont(font);
68be9f09 201 wxBitmap bitmap((int) w+2, (int) h+2);
7cf83330
JS
202 dc2.SelectObject(bitmap);
203
204 dc2.SetBackground(* wxWHITE_BRUSH);
205 dc2.Clear();
68be9f09
JS
206 dc2.SetBackgroundMode(wxTRANSPARENT);
207 dc2.SetTextForeground(* wxLIGHT_GREY);
7cf83330 208 dc2.DrawText(str, 0, 0);
68be9f09
JS
209 dc2.DrawText(str, 1, 0);
210 dc2.DrawText(str, 2, 0);
211 dc2.DrawText(str, 1, 1);
212 dc2.DrawText(str, 2, 1);
213 dc2.DrawText(str, 1, 2);
214 dc2.DrawText(str, 2, 2);
215 dc2.SetTextForeground(* wxBLACK);
216 dc2.DrawText(str, 1, 1);
7cf83330
JS
217
218 dc2.SelectObject(wxNullBitmap);
219
68be9f09
JS
220 // Make the bitmap masked
221 wxImage image(bitmap);
222 image.SetMaskColour(255, 255, 255);
223 bitmap = image.ConvertToBitmap();
224
7cf83330
JS
225 return Create(bitmap, cursor, hotspot);
226}
227
228// Create a drag image for the given tree control item
229bool wxDragImage::Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id)
230{
23f681ec
VZ
231 if ( m_hImageList )
232 ImageList_Destroy(GetHimageList());
7cf83330
JS
233 m_hImageList = (WXHIMAGELIST) TreeView_CreateDragImage((HWND) treeCtrl.GetHWND(), (HTREEITEM) (WXHTREEITEM) id);
234 return TRUE;
235}
236
237// Create a drag image for the given list control item
238bool wxDragImage::Create(const wxListCtrl& listCtrl, long id)
239{
23f681ec
VZ
240 if ( m_hImageList )
241 ImageList_Destroy(GetHimageList());
7cf83330
JS
242 POINT pt;
243 pt.x = 0; pt.y = 0;
244 m_hImageList = (WXHIMAGELIST) ListView_CreateDragImage((HWND) listCtrl.GetHWND(), id, & pt);
245 return TRUE;
246}
247
248// Begin drag
68be9f09 249bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, bool fullScreen, wxRect* rect)
7cf83330 250{
223d09f6 251 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in BeginDrag."));
68be9f09
JS
252 wxASSERT_MSG( (window != 0), wxT("Window must not be null in BeginDrag."));
253
254 m_fullScreen = fullScreen;
255 if (rect)
256 m_boundingRect = * rect;
7cf83330 257
23f681ec 258 bool ret = (ImageList_BeginDrag(GetHimageList(), 0, hotspot.x, hotspot.y) != 0);
7cf83330
JS
259
260 if (!ret)
23f681ec
VZ
261 {
262 wxFAIL_MSG( _T("BeginDrag failed.") );
263
7cf83330 264 return FALSE;
23f681ec 265 }
7cf83330
JS
266
267 if (m_cursor.Ok())
268 {
68be9f09
JS
269 if (!m_hCursorImageList)
270 {
271 int cxCursor = GetSystemMetrics(SM_CXCURSOR);
272 int cyCursor = GetSystemMetrics(SM_CYCURSOR);
273
274 m_hCursorImageList = (WXHIMAGELIST) ImageList_Create(cxCursor, cyCursor, ILC_MASK, 1, 1);
275 }
276
7cf83330 277 // First add the cursor to the image list
68be9f09
JS
278 HCURSOR hCursor = (HCURSOR) m_cursor.GetHCURSOR();
279 int cursorIndex = ImageList_AddIcon((HIMAGELIST) m_hCursorImageList, (HICON) hCursor);
7cf83330 280
223d09f6 281 wxASSERT_MSG( (cursorIndex != -1), wxT("ImageList_AddIcon failed in BeginDrag."));
7cf83330
JS
282
283 if (cursorIndex != -1)
284 {
68be9f09 285 ImageList_SetDragCursorImage((HIMAGELIST) m_hCursorImageList, cursorIndex, m_hotspot.x, m_hotspot.y);
7cf83330
JS
286 }
287 }
288
68be9f09 289 m_window = window;
7cf83330 290 ::ShowCursor(FALSE);
68be9f09 291
23f681ec 292 ::SetCapture(GetHwndOf(window));
7cf83330
JS
293
294 return TRUE;
295}
23f681ec 296
68be9f09
JS
297// Begin drag. hotspot is the location of the drag position relative to the upper-left
298// corner of the image. This is full screen only. fullScreenRect gives the
299// position of the window on the screen, to restrict the drag to.
300bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, wxWindow* fullScreenRect)
301{
302 wxRect rect;
303
304 int x = fullScreenRect->GetPosition().x;
305 int y = fullScreenRect->GetPosition().y;
306
307 wxSize sz = fullScreenRect->GetSize();
308
309 if (fullScreenRect->GetParent() && !fullScreenRect->IsKindOf(CLASSINFO(wxFrame)))
310 fullScreenRect->GetParent()->ClientToScreen(& x, & y);
311
312 rect.x = x; rect.y = y;
313 rect.width = sz.x; rect.height = sz.y;
314
315 return BeginDrag(hotspot, window, TRUE, & rect);
316}
317
7cf83330 318// End drag
68be9f09 319bool wxDragImage::EndDrag()
7cf83330 320{
223d09f6 321 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in EndDrag."));
7cf83330
JS
322
323 ImageList_EndDrag();
324
23f681ec
VZ
325 if ( !::ReleaseCapture() )
326 {
f6bcfd97 327 wxLogLastError(wxT("ReleaseCapture"));
23f681ec
VZ
328 }
329
7cf83330 330 ::ShowCursor(TRUE);
68be9f09 331 m_window = (wxWindow*) NULL;
7cf83330
JS
332
333 return TRUE;
334}
23f681ec 335
7cf83330
JS
336// Move the image: call from OnMouseMove. Pt is in window client coordinates if window
337// is non-NULL, or in screen coordinates if NULL.
68be9f09 338bool wxDragImage::Move(const wxPoint& pt)
7cf83330 339{
223d09f6 340 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Move."));
7cf83330
JS
341
342 // TODO: what coordinates are these in: window, client, or screen?
343 bool ret = (ImageList_DragMove( pt.x, pt.y ) != 0);
344
345 m_position = pt;
346
347 return ret;
348}
349
68be9f09 350bool wxDragImage::Show()
7cf83330 351{
223d09f6 352 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Show."));
7cf83330
JS
353
354 HWND hWnd = 0;
68be9f09
JS
355 if (m_window && !m_fullScreen)
356 hWnd = (HWND) m_window->GetHWND();
7cf83330
JS
357
358 bool ret = (ImageList_DragEnter( hWnd, m_position.x, m_position.y ) != 0);
359
360 return ret;
361}
362
68be9f09 363bool wxDragImage::Hide()
7cf83330 364{
223d09f6 365 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Hide."));
7cf83330
JS
366
367 HWND hWnd = 0;
68be9f09
JS
368 if (m_window && !m_fullScreen)
369 hWnd = (HWND) m_window->GetHWND();
7cf83330
JS
370
371 bool ret = (ImageList_DragLeave( hWnd ) != 0);
372
373 return ret;
374}
375
376#endif
377 // __WIN95__
378