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