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