]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/dragimag.cpp
Corrected some wxAccessible mistakes
[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#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
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#if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
55#include <commctrl.h>
56#endif
57
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
64// ----------------------------------------------------------------------------
65// macros
66// ----------------------------------------------------------------------------
67
68IMPLEMENT_DYNAMIC_CLASS(wxDragImage, wxObject)
69
70#define GetHimageList() ((HIMAGELIST) m_hImageList)
71
72// ============================================================================
73// implementation
74// ============================================================================
75
76// ----------------------------------------------------------------------------
77// wxDragImage ctors/dtor
78// ----------------------------------------------------------------------------
79
80wxDragImage::wxDragImage()
81{
82 Init();
83}
84
85wxDragImage::~wxDragImage()
86{
87 if ( m_hImageList )
88 ImageList_Destroy(GetHimageList());
89#if !wxUSE_SIMPLER_DRAGIMAGE
90 if ( m_hCursorImageList )
91 ImageList_Destroy((HIMAGELIST) m_hCursorImageList);
92#endif
93}
94
95void wxDragImage::Init()
96{
97 m_hImageList = 0;
98#if !wxUSE_SIMPLER_DRAGIMAGE
99 m_hCursorImageList = 0;
100#endif
101 m_window = (wxWindow*) NULL;
102 m_fullScreen = FALSE;
103}
104
105// Attributes
106////////////////////////////////////////////////////////////////////////////
107
108
109// Operations
110////////////////////////////////////////////////////////////////////////////
111
112// Create a drag image from a bitmap and optional cursor
113bool wxDragImage::Create(const wxBitmap& image, const wxCursor& cursor)
114{
115 if ( m_hImageList )
116 ImageList_Destroy(GetHimageList());
117 m_hImageList = 0;
118
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);
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;
138
139 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
140
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);
152
153 index = ImageList_Add(GetHimageList(), hBitmap1, hbmpMask);
154 ::DeleteObject(hbmpMask);
155 }
156 if ( index == -1 )
157 {
158 wxLogError(_("Couldn't add an image to the image list."));
159 }
160 m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
161
162 return (index != -1) ;
163}
164
165// Create a drag image from an icon and optional cursor
166bool wxDragImage::Create(const wxIcon& image, const wxCursor& cursor)
167{
168 if ( m_hImageList )
169 ImageList_Destroy(GetHimageList());
170 m_hImageList = 0;
171
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;
184 if ( mask )
185 flags |= ILC_MASK;
186
187 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
188
189 HICON hIcon = (HICON) image.GetHICON();
190
191 int index = ImageList_AddIcon(GetHimageList(), hIcon);
192 if ( index == -1 )
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.
198
199 return (index != -1) ;
200}
201
202// Create a drag image from a string and optional cursor
203bool wxDragImage::Create(const wxString& str, const wxCursor& cursor)
204{
205 wxFont font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
206
207 long w, h;
208 wxScreenDC dc;
209 dc.SetFont(font);
210 dc.GetTextExtent(str, & w, & h);
211 dc.SetFont(wxNullFont);
212
213 wxMemoryDC dc2;
214 dc2.SetFont(font);
215 wxBitmap bitmap((int) w+2, (int) h+2);
216 dc2.SelectObject(bitmap);
217
218 dc2.SetBackground(* wxWHITE_BRUSH);
219 dc2.Clear();
220 dc2.SetBackgroundMode(wxTRANSPARENT);
221 dc2.SetTextForeground(* wxLIGHT_GREY);
222 dc2.DrawText(str, 0, 0);
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);
231
232 dc2.SelectObject(wxNullBitmap);
233
234 // Make the bitmap masked
235 wxImage image = bitmap.ConvertToImage();
236 image.SetMaskColour(255, 255, 255);
237 return Create(wxBitmap(image), cursor);
238}
239
240// Create a drag image for the given tree control item
241bool wxDragImage::Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id)
242{
243 if ( m_hImageList )
244 ImageList_Destroy(GetHimageList());
245 m_hImageList = (WXHIMAGELIST) TreeView_CreateDragImage((HWND) treeCtrl.GetHWND(), (HTREEITEM) (WXHTREEITEM) id);
246 return TRUE;
247}
248
249// Create a drag image for the given list control item
250bool wxDragImage::Create(const wxListCtrl& listCtrl, long id)
251{
252 if ( m_hImageList )
253 ImageList_Destroy(GetHimageList());
254 POINT pt;
255 pt.x = 0; pt.y = 0;
256 m_hImageList = (WXHIMAGELIST) ListView_CreateDragImage((HWND) listCtrl.GetHWND(), id, & pt);
257 return TRUE;
258}
259
260// Begin drag
261bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, bool fullScreen, wxRect* rect)
262{
263 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in BeginDrag."));
264 wxASSERT_MSG( (window != 0), wxT("Window must not be null in BeginDrag."));
265
266 m_fullScreen = fullScreen;
267 if (rect)
268 m_boundingRect = * rect;
269
270 bool ret = (ImageList_BeginDrag(GetHimageList(), 0, hotspot.x, hotspot.y) != 0);
271
272 if (!ret)
273 {
274 wxFAIL_MSG( _T("BeginDrag failed.") );
275
276 return FALSE;
277 }
278
279 if (m_cursor.Ok())
280 {
281#if wxUSE_SIMPLER_DRAGIMAGE
282 m_oldCursor = window->GetCursor();
283 window->SetCursor(m_cursor);
284#else
285 if (!m_hCursorImageList)
286 {
287 int cxCursor = GetSystemMetrics(SM_CXCURSOR);
288 int cyCursor = GetSystemMetrics(SM_CYCURSOR);
289
290 m_hCursorImageList = (WXHIMAGELIST) ImageList_Create(cxCursor, cyCursor, ILC_MASK, 1, 1);
291 }
292
293 // See if we can find the cursor hotspot
294 wxPoint curHotSpot(hotspot);
295
296 // Although it seems to produce the right position, when the hotspot goeos
297 // negative it has strange effects on the image.
298 // How do we stop the cursor jumping right and below of where it should be?
299#if 0
300 ICONINFO iconInfo;
301 if (::GetIconInfo((HICON) (HCURSOR) m_cursor.GetHCURSOR(), & iconInfo) != 0)
302 {
303 curHotSpot.x -= iconInfo.xHotspot;
304 curHotSpot.y -= iconInfo.yHotspot;
305 }
306#endif
307 //wxString msg;
308 //msg.Printf("Hotspot = %d, %d", curHotSpot.x, curHotSpot.y);
309 //wxLogDebug(msg);
310
311 // First add the cursor to the image list
312 HCURSOR hCursor = (HCURSOR) m_cursor.GetHCURSOR();
313 int cursorIndex = ImageList_AddIcon((HIMAGELIST) m_hCursorImageList, (HICON) hCursor);
314
315 wxASSERT_MSG( (cursorIndex != -1), wxT("ImageList_AddIcon failed in BeginDrag."));
316
317 if (cursorIndex != -1)
318 {
319 ImageList_SetDragCursorImage((HIMAGELIST) m_hCursorImageList, cursorIndex, curHotSpot.x, curHotSpot.y);
320 }
321#endif
322 }
323
324#if !wxUSE_SIMPLER_DRAGIMAGE
325 if (m_cursor.Ok())
326 ::ShowCursor(FALSE);
327#endif
328
329 m_window = window;
330
331 ::SetCapture(GetHwndOf(window));
332
333 return TRUE;
334}
335
336// Begin drag. hotspot is the location of the drag position relative to the upper-left
337// corner of the image. This is full screen only. fullScreenRect gives the
338// position of the window on the screen, to restrict the drag to.
339bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, wxWindow* fullScreenRect)
340{
341 wxRect rect;
342
343 int x = fullScreenRect->GetPosition().x;
344 int y = fullScreenRect->GetPosition().y;
345
346 wxSize sz = fullScreenRect->GetSize();
347
348 if (fullScreenRect->GetParent() && !fullScreenRect->IsKindOf(CLASSINFO(wxFrame)))
349 fullScreenRect->GetParent()->ClientToScreen(& x, & y);
350
351 rect.x = x; rect.y = y;
352 rect.width = sz.x; rect.height = sz.y;
353
354 return BeginDrag(hotspot, window, TRUE, & rect);
355}
356
357// End drag
358bool wxDragImage::EndDrag()
359{
360 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in EndDrag."));
361
362 ImageList_EndDrag();
363
364 if ( !::ReleaseCapture() )
365 {
366 wxLogLastError(wxT("ReleaseCapture"));
367 }
368
369#if wxUSE_SIMPLER_DRAGIMAGE
370 if (m_cursor.Ok() && m_oldCursor.Ok())
371 m_window->SetCursor(m_oldCursor);
372#else
373 ::ShowCursor(TRUE);
374#endif
375
376 m_window = (wxWindow*) NULL;
377
378 return TRUE;
379}
380
381// Move the image: call from OnMouseMove. Pt is in window client coordinates if window
382// is non-NULL, or in screen coordinates if NULL.
383bool wxDragImage::Move(const wxPoint& pt)
384{
385 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Move."));
386
387 // These are in window, not client coordinates.
388 // So need to convert to client coordinates.
389 wxPoint pt2(pt);
390 if (m_window && !m_fullScreen)
391 {
392 RECT rect;
393 rect.left = 0; rect.top = 0;
394 rect.right = 0; rect.bottom = 0;
395 DWORD style = ::GetWindowLong((HWND) m_window->GetHWND(), GWL_STYLE);
396#ifdef __WIN32__
397 DWORD exStyle = ::GetWindowLong((HWND) m_window->GetHWND(), GWL_EXSTYLE);
398 ::AdjustWindowRectEx(& rect, style, FALSE, exStyle);
399#else
400 ::AdjustWindowRect(& rect, style, FALSE);
401#endif
402 // Subtract the (negative) values, i.e. add a small increment
403 pt2.x -= rect.left; pt2.y -= rect.top;
404 }
405 else if (m_window && m_fullScreen)
406 {
407 pt2 = m_window->ClientToScreen(pt2);
408 }
409
410 bool ret = (ImageList_DragMove( pt2.x, pt2.y ) != 0);
411
412 m_position = pt2;
413
414 return ret;
415}
416
417bool wxDragImage::Show()
418{
419 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Show."));
420
421 HWND hWnd = 0;
422 if (m_window && !m_fullScreen)
423 hWnd = (HWND) m_window->GetHWND();
424
425 bool ret = (ImageList_DragEnter( hWnd, m_position.x, m_position.y ) != 0);
426
427 return ret;
428}
429
430bool wxDragImage::Hide()
431{
432 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Hide."));
433
434 HWND hWnd = 0;
435 if (m_window && !m_fullScreen)
436 hWnd = (HWND) m_window->GetHWND();
437
438 bool ret = (ImageList_DragLeave( hWnd ) != 0);
439
440 return ret;
441}
442
443#endif
444 // __WIN95__
445
446#endif // wxUSE_DRAGIMAGE