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