]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/dragimag.cpp
Source cleaning: whitespaces,-1/wxID_ANY,TRUE/true,FALSE/false.
[wxWidgets.git] / src / msw / dragimag.cpp
... / ...
CommitLineData
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
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
31#if wxUSE_DRAGIMAGE
32
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"
40#include "wx/dcscreen.h"
41#include "wx/dcmemory.h"
42#include "wx/settings.h"
43#endif
44
45#include "wx/msw/private.h"
46#include "wx/log.h"
47#include "wx/intl.h"
48#include "wx/frame.h"
49#include "wx/image.h"
50
51#include "wx/msw/dragimag.h"
52#include "wx/msw/private.h"
53
54#ifdef __WXWINCE__ // for SM_CXCURSOR and SM_CYCURSOR
55#include "wx/msw/wince/missing.h"
56#endif // __WXWINCE__
57
58#if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
59#include <commctrl.h>
60#endif
61
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
68// ----------------------------------------------------------------------------
69// macros
70// ----------------------------------------------------------------------------
71
72IMPLEMENT_DYNAMIC_CLASS(wxDragImage, wxObject)
73
74#define GetHimageList() ((HIMAGELIST) m_hImageList)
75
76// ============================================================================
77// implementation
78// ============================================================================
79
80// ----------------------------------------------------------------------------
81// wxDragImage ctors/dtor
82// ----------------------------------------------------------------------------
83
84wxDragImage::wxDragImage()
85{
86 Init();
87}
88
89wxDragImage::~wxDragImage()
90{
91 if ( m_hImageList )
92 ImageList_Destroy(GetHimageList());
93#if !wxUSE_SIMPLER_DRAGIMAGE
94 if ( m_hCursorImageList )
95 ImageList_Destroy((HIMAGELIST) m_hCursorImageList);
96#endif
97}
98
99void wxDragImage::Init()
100{
101 m_hImageList = 0;
102#if !wxUSE_SIMPLER_DRAGIMAGE
103 m_hCursorImageList = 0;
104#endif
105 m_window = (wxWindow*) NULL;
106 m_fullScreen = FALSE;
107}
108
109// Attributes
110////////////////////////////////////////////////////////////////////////////
111
112
113// Operations
114////////////////////////////////////////////////////////////////////////////
115
116// Create a drag image from a bitmap and optional cursor
117bool wxDragImage::Create(const wxBitmap& image, const wxCursor& cursor)
118{
119 if ( m_hImageList )
120 ImageList_Destroy(GetHimageList());
121 m_hImageList = 0;
122
123#ifdef __WXWINCE__
124 UINT flags = ILC_COLOR;
125#else
126 UINT flags wxDUMMY_INITIALIZE(0) ;
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;
137#endif
138
139 bool mask = (image.GetMask() != 0);
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;
146
147 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
148
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);
160
161 index = ImageList_Add(GetHimageList(), hBitmap1, hbmpMask);
162 ::DeleteObject(hbmpMask);
163 }
164 if ( index == -1 )
165 {
166 wxLogError(_("Couldn't add an image to the image list."));
167 }
168 m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
169
170 return (index != -1) ;
171}
172
173// Create a drag image from an icon and optional cursor
174bool wxDragImage::Create(const wxIcon& image, const wxCursor& cursor)
175{
176 if ( m_hImageList )
177 ImageList_Destroy(GetHimageList());
178 m_hImageList = 0;
179
180#ifdef __WXWINCE__
181 UINT flags = ILC_COLOR;
182#else
183 UINT flags wxDUMMY_INITIALIZE(0) ;
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;
194#endif
195 bool mask = TRUE;
196 if ( mask )
197 flags |= ILC_MASK;
198
199 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
200
201 HICON hIcon = (HICON) image.GetHICON();
202
203 int index = ImageList_AddIcon(GetHimageList(), hIcon);
204 if ( index == -1 )
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.
210
211 return (index != -1) ;
212}
213
214// Create a drag image from a string and optional cursor
215bool wxDragImage::Create(const wxString& str, const wxCursor& cursor)
216{
217 wxFont font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
218
219 long w, h;
220 wxScreenDC dc;
221 dc.SetFont(font);
222 dc.GetTextExtent(str, & w, & h);
223 dc.SetFont(wxNullFont);
224
225 wxMemoryDC dc2;
226 dc2.SetFont(font);
227 wxBitmap bitmap((int) w+2, (int) h+2);
228 dc2.SelectObject(bitmap);
229
230 dc2.SetBackground(* wxWHITE_BRUSH);
231 dc2.Clear();
232 dc2.SetBackgroundMode(wxTRANSPARENT);
233 dc2.SetTextForeground(* wxLIGHT_GREY);
234 dc2.DrawText(str, 0, 0);
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);
243
244 dc2.SelectObject(wxNullBitmap);
245
246 // Make the bitmap masked
247 wxImage image = bitmap.ConvertToImage();
248 image.SetMaskColour(255, 255, 255);
249 return Create(wxBitmap(image), cursor);
250}
251
252#if wxUSE_TREECTRL
253// Create a drag image for the given tree control item
254bool wxDragImage::Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id)
255{
256 if ( m_hImageList )
257 ImageList_Destroy(GetHimageList());
258 m_hImageList = (WXHIMAGELIST)
259 TreeView_CreateDragImage(GetHwndOf(&treeCtrl), (HTREEITEM) id.m_pItem);
260 return m_hImageList != 0;
261}
262#endif
263
264#if wxUSE_LISTCTRL
265// Create a drag image for the given list control item
266bool wxDragImage::Create(const wxListCtrl& listCtrl, long id)
267{
268 if ( m_hImageList )
269 ImageList_Destroy(GetHimageList());
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}
275#endif
276
277// Begin drag
278bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, bool fullScreen, wxRect* rect)
279{
280 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in BeginDrag."));
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;
286
287 bool ret = (ImageList_BeginDrag(GetHimageList(), 0, hotspot.x, hotspot.y) != 0);
288
289 if (!ret)
290 {
291 wxFAIL_MSG( _T("BeginDrag failed.") );
292
293 return FALSE;
294 }
295
296 if (m_cursor.Ok())
297 {
298#if wxUSE_SIMPLER_DRAGIMAGE
299 m_oldCursor = window->GetCursor();
300 window->SetCursor(m_cursor);
301#else
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
310 // See if we can find the cursor hotspot
311 wxPoint curHotSpot(hotspot);
312
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?
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
324 //wxString msg;
325 //msg.Printf("Hotspot = %d, %d", curHotSpot.x, curHotSpot.y);
326 //wxLogDebug(msg);
327
328 // First add the cursor to the image list
329 HCURSOR hCursor = (HCURSOR) m_cursor.GetHCURSOR();
330 int cursorIndex = ImageList_AddIcon((HIMAGELIST) m_hCursorImageList, (HICON) hCursor);
331
332 wxASSERT_MSG( (cursorIndex != -1), wxT("ImageList_AddIcon failed in BeginDrag."));
333
334 if (cursorIndex != -1)
335 {
336 ImageList_SetDragCursorImage((HIMAGELIST) m_hCursorImageList, cursorIndex, curHotSpot.x, curHotSpot.y);
337 }
338#endif
339 }
340
341#if !wxUSE_SIMPLER_DRAGIMAGE
342 if (m_cursor.Ok())
343 ::ShowCursor(FALSE);
344#endif
345
346 m_window = window;
347
348 ::SetCapture(GetHwndOf(window));
349
350 return TRUE;
351}
352
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
374// End drag
375bool wxDragImage::EndDrag()
376{
377 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in EndDrag."));
378
379 ImageList_EndDrag();
380
381 if ( !::ReleaseCapture() )
382 {
383 wxLogLastError(wxT("ReleaseCapture"));
384 }
385
386#if wxUSE_SIMPLER_DRAGIMAGE
387 if (m_cursor.Ok() && m_oldCursor.Ok())
388 m_window->SetCursor(m_oldCursor);
389#else
390 ::ShowCursor(TRUE);
391#endif
392
393 m_window = (wxWindow*) NULL;
394
395 return TRUE;
396}
397
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.
400bool wxDragImage::Move(const wxPoint& pt)
401{
402 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Move."));
403
404 // These are in window, not client coordinates.
405 // So need to convert to client coordinates.
406 wxPoint pt2(pt);
407 if (m_window && !m_fullScreen)
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 }
422 else if (m_window && m_fullScreen)
423 {
424 pt2 = m_window->ClientToScreen(pt2);
425 }
426
427 bool ret = (ImageList_DragMove( pt2.x, pt2.y ) != 0);
428
429 m_position = pt2;
430
431 return ret;
432}
433
434bool wxDragImage::Show()
435{
436 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Show."));
437
438 HWND hWnd = 0;
439 if (m_window && !m_fullScreen)
440 hWnd = (HWND) m_window->GetHWND();
441
442 bool ret = (ImageList_DragEnter( hWnd, m_position.x, m_position.y ) != 0);
443
444 return ret;
445}
446
447bool wxDragImage::Hide()
448{
449 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Hide."));
450
451 HWND hWnd = 0;
452 if (m_window && !m_fullScreen)
453 hWnd = (HWND) m_window->GetHWND();
454
455 bool ret = (ImageList_DragLeave( hWnd ) != 0);
456
457 return ret;
458}
459
460#endif
461 // __WIN95__
462
463#endif // wxUSE_DRAGIMAGE