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