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