]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dragimag.cpp
Adding support for item font/style/color customization, unfortunately, it does not...
[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 bool mask = true;
186 if ( mask )
187 flags |= ILC_MASK;
188
189 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
190
191 HICON hIcon = (HICON) image.GetHICON();
192
193 int index = ImageList_AddIcon(GetHimageList(), hIcon);
194 if ( index == -1 )
195 {
196 wxLogError(_("Couldn't add an image to the image list."));
197 }
198
199 m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
200
201 return (index != -1) ;
202 }
203
204 // Create a drag image from a string and optional cursor
205 bool wxDragImage::Create(const wxString& str, const wxCursor& cursor)
206 {
207 wxFont font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
208
209 long w = 0, h = 0;
210 wxScreenDC dc;
211 dc.SetFont(font);
212 dc.GetTextExtent(str, & w, & h);
213 dc.SetFont(wxNullFont);
214
215 wxMemoryDC dc2;
216 dc2.SetFont(font);
217 wxBitmap bitmap((int) w+2, (int) h+2);
218 dc2.SelectObject(bitmap);
219
220 dc2.SetBackground(* wxWHITE_BRUSH);
221 dc2.Clear();
222 dc2.SetBackgroundMode(wxTRANSPARENT);
223 dc2.SetTextForeground(* wxLIGHT_GREY);
224 dc2.DrawText(str, 0, 0);
225 dc2.DrawText(str, 1, 0);
226 dc2.DrawText(str, 2, 0);
227 dc2.DrawText(str, 1, 1);
228 dc2.DrawText(str, 2, 1);
229 dc2.DrawText(str, 1, 2);
230 dc2.DrawText(str, 2, 2);
231 dc2.SetTextForeground(* wxBLACK);
232 dc2.DrawText(str, 1, 1);
233
234 dc2.SelectObject(wxNullBitmap);
235
236 #if wxUSE_WXDIB
237 // Make the bitmap masked
238 wxImage image = bitmap.ConvertToImage();
239 image.SetMaskColour(255, 255, 255);
240 return Create(wxBitmap(image), cursor);
241 #else
242 return false;
243 #endif
244 }
245
246 #if wxUSE_TREECTRL
247 // Create a drag image for the given tree control item
248 bool wxDragImage::Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id)
249 {
250 if ( m_hImageList )
251 ImageList_Destroy(GetHimageList());
252 m_hImageList = (WXHIMAGELIST)
253 TreeView_CreateDragImage(GetHwndOf(&treeCtrl), (HTREEITEM) id.m_pItem);
254 return m_hImageList != 0;
255 }
256 #endif
257
258 #if wxUSE_LISTCTRL
259 // Create a drag image for the given list control item
260 bool wxDragImage::Create(const wxListCtrl& listCtrl, long id)
261 {
262 if ( m_hImageList )
263 ImageList_Destroy(GetHimageList());
264 POINT pt;
265 pt.x = 0; pt.y = 0;
266 m_hImageList = (WXHIMAGELIST) ListView_CreateDragImage((HWND) listCtrl.GetHWND(), id, & pt);
267 return true;
268 }
269 #endif
270
271 // Begin drag
272 bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, bool fullScreen, wxRect* rect)
273 {
274 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in BeginDrag."));
275 wxASSERT_MSG( (window != 0), wxT("Window must not be null in BeginDrag."));
276
277 m_fullScreen = fullScreen;
278 if (rect)
279 m_boundingRect = * rect;
280
281 bool ret = (ImageList_BeginDrag(GetHimageList(), 0, hotspot.x, hotspot.y) != 0);
282
283 if (!ret)
284 {
285 wxFAIL_MSG( _T("BeginDrag failed.") );
286
287 return false;
288 }
289
290 if (m_cursor.Ok())
291 {
292 #if wxUSE_SIMPLER_DRAGIMAGE
293 m_oldCursor = window->GetCursor();
294 window->SetCursor(m_cursor);
295 #else
296 if (!m_hCursorImageList)
297 {
298 #ifndef SM_CXCURSOR
299 // Smartphone may not have these metric symbol
300 int cxCursor = 16;
301 int cyCursor = 16;
302 #else
303 int cxCursor = ::GetSystemMetrics(SM_CXCURSOR);
304 int cyCursor = ::GetSystemMetrics(SM_CYCURSOR);
305 #endif
306 m_hCursorImageList = (WXHIMAGELIST) ImageList_Create(cxCursor, cyCursor, ILC_MASK, 1, 1);
307 }
308
309 // See if we can find the cursor hotspot
310 wxPoint curHotSpot(hotspot);
311
312 // Although it seems to produce the right position, when the hotspot goeos
313 // negative it has strange effects on the image.
314 // How do we stop the cursor jumping right and below of where it should be?
315 #if 0
316 ICONINFO iconInfo;
317 if (::GetIconInfo((HICON) (HCURSOR) m_cursor.GetHCURSOR(), & iconInfo) != 0)
318 {
319 curHotSpot.x -= iconInfo.xHotspot;
320 curHotSpot.y -= iconInfo.yHotspot;
321 }
322 #endif
323 //wxString msg;
324 //msg.Printf("Hotspot = %d, %d", curHotSpot.x, curHotSpot.y);
325 //wxLogDebug(msg);
326
327 // First add the cursor to the image list
328 HCURSOR hCursor = (HCURSOR) m_cursor.GetHCURSOR();
329 int cursorIndex = ImageList_AddIcon((HIMAGELIST) m_hCursorImageList, (HICON) hCursor);
330
331 wxASSERT_MSG( (cursorIndex != -1), wxT("ImageList_AddIcon failed in BeginDrag."));
332
333 if (cursorIndex != -1)
334 {
335 ImageList_SetDragCursorImage((HIMAGELIST) m_hCursorImageList, cursorIndex, curHotSpot.x, curHotSpot.y);
336 }
337 #endif
338 }
339
340 #if !wxUSE_SIMPLER_DRAGIMAGE
341 if (m_cursor.Ok())
342 ::ShowCursor(FALSE);
343 #endif
344
345 m_window = window;
346
347 ::SetCapture(GetHwndOf(window));
348
349 return true;
350 }
351
352 // Begin drag. hotspot is the location of the drag position relative to the upper-left
353 // corner of the image. This is full screen only. fullScreenRect gives the
354 // position of the window on the screen, to restrict the drag to.
355 bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, wxWindow* fullScreenRect)
356 {
357 wxRect rect;
358
359 int x = fullScreenRect->GetPosition().x;
360 int y = fullScreenRect->GetPosition().y;
361
362 wxSize sz = fullScreenRect->GetSize();
363
364 if (fullScreenRect->GetParent() && !fullScreenRect->IsKindOf(CLASSINFO(wxFrame)))
365 fullScreenRect->GetParent()->ClientToScreen(& x, & y);
366
367 rect.x = x; rect.y = y;
368 rect.width = sz.x; rect.height = sz.y;
369
370 return BeginDrag(hotspot, window, true, & rect);
371 }
372
373 // End drag
374 bool wxDragImage::EndDrag()
375 {
376 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in EndDrag."));
377
378 ImageList_EndDrag();
379
380 if ( !::ReleaseCapture() )
381 {
382 wxLogLastError(wxT("ReleaseCapture"));
383 }
384
385 #if wxUSE_SIMPLER_DRAGIMAGE
386 if (m_cursor.Ok() && m_oldCursor.Ok())
387 m_window->SetCursor(m_oldCursor);
388 #else
389 ::ShowCursor(TRUE);
390 #endif
391
392 m_window = (wxWindow*) NULL;
393
394 return true;
395 }
396
397 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
398 // is non-NULL, or in screen coordinates if NULL.
399 bool wxDragImage::Move(const wxPoint& pt)
400 {
401 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Move."));
402
403 // These are in window, not client coordinates.
404 // So need to convert to client coordinates.
405 wxPoint pt2(pt);
406 if (m_window && !m_fullScreen)
407 {
408 RECT rect;
409 rect.left = 0; rect.top = 0;
410 rect.right = 0; rect.bottom = 0;
411 DWORD style = ::GetWindowLong((HWND) m_window->GetHWND(), GWL_STYLE);
412 #ifdef __WIN32__
413 DWORD exStyle = ::GetWindowLong((HWND) m_window->GetHWND(), GWL_EXSTYLE);
414 ::AdjustWindowRectEx(& rect, style, FALSE, exStyle);
415 #else
416 ::AdjustWindowRect(& rect, style, FALSE);
417 #endif
418 // Subtract the (negative) values, i.e. add a small increment
419 pt2.x -= rect.left; pt2.y -= rect.top;
420 }
421 else if (m_window && m_fullScreen)
422 {
423 pt2 = m_window->ClientToScreen(pt2);
424 }
425
426 bool ret = (ImageList_DragMove( pt2.x, pt2.y ) != 0);
427
428 m_position = pt2;
429
430 return ret;
431 }
432
433 bool wxDragImage::Show()
434 {
435 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Show."));
436
437 HWND hWnd = 0;
438 if (m_window && !m_fullScreen)
439 hWnd = (HWND) m_window->GetHWND();
440
441 bool ret = (ImageList_DragEnter( hWnd, m_position.x, m_position.y ) != 0);
442
443 return ret;
444 }
445
446 bool wxDragImage::Hide()
447 {
448 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Hide."));
449
450 HWND hWnd = 0;
451 if (m_window && !m_fullScreen)
452 hWnd = (HWND) m_window->GetHWND();
453
454 bool ret = (ImageList_DragLeave( hWnd ) != 0);
455
456 return ret;
457 }
458
459 #endif // wxUSE_DRAGIMAGE