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