Implemented Chuck Messenger's naming and simplification improvements,
[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 #include "wx/msw/private.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 #if defined(__WIN95__)
33
34 #ifndef WX_PRECOMP
35 #include <stdio.h>
36 #include "wx/setup.h"
37 #include "wx/window.h"
38 #include "wx/dcclient.h"
39 #include "wx/dcscreen.h"
40 #include "wx/dcmemory.h"
41 #include "wx/settings.h"
42 #endif
43
44 #include "wx/log.h"
45 #include "wx/intl.h"
46 #include "wx/frame.h"
47 #include "wx/image.h"
48
49 #include "wx/msw/dragimag.h"
50 #include "wx/msw/private.h"
51
52 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
53 #include <commctrl.h>
54 #endif
55
56 // ----------------------------------------------------------------------------
57 // macros
58 // ----------------------------------------------------------------------------
59
60 IMPLEMENT_DYNAMIC_CLASS(wxDragImage, wxObject)
61
62 #define GetHimageList() ((HIMAGELIST) m_hImageList)
63
64 // ============================================================================
65 // implementation
66 // ============================================================================
67
68 // ----------------------------------------------------------------------------
69 // wxDragImage ctors/dtor
70 // ----------------------------------------------------------------------------
71
72 wxDragImage::wxDragImage()
73 {
74 Init();
75 }
76
77 wxDragImage::~wxDragImage()
78 {
79 if ( m_hImageList )
80 ImageList_Destroy(GetHimageList());
81 #if !wxUSE_SIMPLER_DRAGIMAGE
82 if ( m_hCursorImageList )
83 ImageList_Destroy((HIMAGELIST) m_hCursorImageList);
84 #endif
85 }
86
87 void wxDragImage::Init()
88 {
89 m_hImageList = 0;
90 #if !wxUSE_SIMPLER_DRAGIMAGE
91 m_hCursorImageList = 0;
92 #endif
93 m_window = (wxWindow*) NULL;
94 m_fullScreen = FALSE;
95 }
96
97 // Attributes
98 ////////////////////////////////////////////////////////////////////////////
99
100
101 // Operations
102 ////////////////////////////////////////////////////////////////////////////
103
104 // Create a drag image from a bitmap and optional cursor
105 bool wxDragImage::Create(const wxBitmap& image, const wxCursor& cursor, const wxPoint& cursorHotspot)
106 {
107 if ( m_hImageList )
108 ImageList_Destroy(GetHimageList());
109 m_hImageList = 0;
110
111 UINT flags = 0 ;
112 if (image.GetDepth() <= 4)
113 flags = ILC_COLOR4;
114 else if (image.GetDepth() <= 8)
115 flags = ILC_COLOR8;
116 else if (image.GetDepth() <= 16)
117 flags = ILC_COLOR16;
118 else if (image.GetDepth() <= 24)
119 flags = ILC_COLOR24;
120 else
121 flags = ILC_COLOR32;
122
123 bool mask = (image.GetMask() != 0);
124 if ( mask )
125 flags |= ILC_MASK;
126
127 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
128
129 int index;
130 if (!mask)
131 {
132 HBITMAP hBitmap1 = (HBITMAP) image.GetHBITMAP();
133 index = ImageList_Add(GetHimageList(), hBitmap1, 0);
134 }
135 else
136 {
137 HBITMAP hBitmap1 = (HBITMAP) image.GetHBITMAP();
138 HBITMAP hBitmap2 = (HBITMAP) image.GetMask()->GetMaskBitmap();
139 HBITMAP hbmpMask = wxInvertMask(hBitmap2);
140
141 index = ImageList_Add(GetHimageList(), hBitmap1, hbmpMask);
142 ::DeleteObject(hbmpMask);
143 }
144 if ( index == -1 )
145 {
146 wxLogError(_("Couldn't add an image to the image list."));
147 }
148 m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
149 m_cursorHotspot = cursorHotspot;
150
151 return (index != -1) ;
152 }
153
154 // Create a drag image from an icon and optional cursor
155 bool wxDragImage::Create(const wxIcon& image, const wxCursor& cursor, const wxPoint& cursorHotspot)
156 {
157 if ( m_hImageList )
158 ImageList_Destroy(GetHimageList());
159 m_hImageList = 0;
160
161 UINT flags = 0 ;
162 if (image.GetDepth() <= 4)
163 flags = ILC_COLOR4;
164 else if (image.GetDepth() <= 8)
165 flags = ILC_COLOR8;
166 else if (image.GetDepth() <= 16)
167 flags = ILC_COLOR16;
168 else if (image.GetDepth() <= 24)
169 flags = ILC_COLOR24;
170 else
171 flags = ILC_COLOR32;
172 bool mask = TRUE;
173 if ( mask )
174 flags |= ILC_MASK;
175
176 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
177
178 HICON hIcon = (HICON) image.GetHICON();
179
180 int index = ImageList_AddIcon(GetHimageList(), hIcon);
181 if ( index == -1 )
182 {
183 wxLogError(_("Couldn't add an image to the image list."));
184 }
185
186 m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
187 m_cursorHotspot = cursorHotspot;
188
189 return (index != -1) ;
190 }
191
192 // Create a drag image from a string and optional cursor
193 bool wxDragImage::Create(const wxString& str, const wxCursor& cursor, const wxPoint& cursorHotspot)
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);
201 dc.SetFont(wxNullFont);
202
203 wxMemoryDC dc2;
204 dc2.SetFont(font);
205 wxBitmap bitmap((int) w+2, (int) h+2);
206 dc2.SelectObject(bitmap);
207
208 dc2.SetBackground(* wxWHITE_BRUSH);
209 dc2.Clear();
210 dc2.SetBackgroundMode(wxTRANSPARENT);
211 dc2.SetTextForeground(* wxLIGHT_GREY);
212 dc2.DrawText(str, 0, 0);
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);
221
222 dc2.SelectObject(wxNullBitmap);
223
224 // Make the bitmap masked
225 wxImage image(bitmap);
226 image.SetMaskColour(255, 255, 255);
227 bitmap = image.ConvertToBitmap();
228
229 return Create(bitmap, cursor, cursorHotspot);
230 }
231
232 // Create a drag image for the given tree control item
233 bool wxDragImage::Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id)
234 {
235 if ( m_hImageList )
236 ImageList_Destroy(GetHimageList());
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 {
244 if ( m_hImageList )
245 ImageList_Destroy(GetHimageList());
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
253 bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, bool fullScreen, wxRect* rect)
254 {
255 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in BeginDrag."));
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;
261
262 bool ret = (ImageList_BeginDrag(GetHimageList(), 0, hotspot.x, hotspot.y) != 0);
263
264 if (!ret)
265 {
266 wxFAIL_MSG( _T("BeginDrag failed.") );
267
268 return FALSE;
269 }
270
271 if (m_cursor.Ok())
272 {
273 #if wxUSE_SIMPLER_DRAGIMAGE
274 m_oldCursor = window->GetCursor();
275 window->SetCursor(m_cursor);
276 #else
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
285 // First add the cursor to the image list
286 HCURSOR hCursor = (HCURSOR) m_cursor.GetHCURSOR();
287 int cursorIndex = ImageList_AddIcon((HIMAGELIST) m_hCursorImageList, (HICON) hCursor);
288
289 wxASSERT_MSG( (cursorIndex != -1), wxT("ImageList_AddIcon failed in BeginDrag."));
290
291 if (cursorIndex != -1)
292 {
293 ImageList_SetDragCursorImage((HIMAGELIST) m_hCursorImageList, cursorIndex, m_cursorHotspot.x, m_cursorHotspot.y);
294 }
295 #endif
296 }
297
298 m_window = window;
299
300 #if !wxUSE_SIMPLER_DRAGIMAGE
301 if (m_cursor.Ok())
302 ::ShowCursor(FALSE);
303 #endif
304
305 ::SetCapture(GetHwndOf(window));
306
307 return TRUE;
308 }
309
310 // Begin drag. hotspot is the location of the drag position relative to the upper-left
311 // corner of the image. This is full screen only. fullScreenRect gives the
312 // position of the window on the screen, to restrict the drag to.
313 bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, wxWindow* fullScreenRect)
314 {
315 wxRect rect;
316
317 int x = fullScreenRect->GetPosition().x;
318 int y = fullScreenRect->GetPosition().y;
319
320 wxSize sz = fullScreenRect->GetSize();
321
322 if (fullScreenRect->GetParent() && !fullScreenRect->IsKindOf(CLASSINFO(wxFrame)))
323 fullScreenRect->GetParent()->ClientToScreen(& x, & y);
324
325 rect.x = x; rect.y = y;
326 rect.width = sz.x; rect.height = sz.y;
327
328 return BeginDrag(hotspot, window, TRUE, & rect);
329 }
330
331 // End drag
332 bool wxDragImage::EndDrag()
333 {
334 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in EndDrag."));
335
336 ImageList_EndDrag();
337
338 if ( !::ReleaseCapture() )
339 {
340 wxLogLastError(wxT("ReleaseCapture"));
341 }
342
343 #if wxUSE_SIMPLER_DRAGIMAGE
344 if (m_cursor.Ok() && m_oldCursor.Ok())
345 m_window->SetCursor(m_oldCursor);
346 #else
347 ::ShowCursor(TRUE);
348 #endif
349
350 m_window = (wxWindow*) NULL;
351
352 return TRUE;
353 }
354
355 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
356 // is non-NULL, or in screen coordinates if NULL.
357 bool wxDragImage::Move(const wxPoint& pt)
358 {
359 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Move."));
360
361 // These are in window, not client coordinates.
362 // So need to convert to client coordinates.
363 wxPoint pt2(pt);
364 if (m_window)
365 {
366 RECT rect;
367 rect.left = 0; rect.top = 0;
368 rect.right = 0; rect.bottom = 0;
369 DWORD style = ::GetWindowLong((HWND) m_window->GetHWND(), GWL_STYLE);
370 #ifdef __WIN32__
371 DWORD exStyle = ::GetWindowLong((HWND) m_window->GetHWND(), GWL_EXSTYLE);
372 ::AdjustWindowRectEx(& rect, style, FALSE, exStyle);
373 #else
374 ::AdjustWindowRect(& rect, style, FALSE);
375 #endif
376 // Subtract the (negative) values, i.e. add a small increment
377 pt2.x -= rect.left; pt2.y -= rect.top;
378 }
379
380 bool ret = (ImageList_DragMove( pt2.x, pt2.y ) != 0);
381
382 m_position = pt;
383
384 return ret;
385 }
386
387 bool wxDragImage::Show()
388 {
389 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Show."));
390
391 HWND hWnd = 0;
392 if (m_window && !m_fullScreen)
393 hWnd = (HWND) m_window->GetHWND();
394
395 bool ret = (ImageList_DragEnter( hWnd, m_position.x, m_position.y ) != 0);
396
397 return ret;
398 }
399
400 bool wxDragImage::Hide()
401 {
402 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Hide."));
403
404 HWND hWnd = 0;
405 if (m_window && !m_fullScreen)
406 hWnd = (HWND) m_window->GetHWND();
407
408 bool ret = (ImageList_DragLeave( hWnd ) != 0);
409
410 return ret;
411 }
412
413 #endif
414 // __WIN95__
415