]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dragimag.cpp
1. corrected HAVE_PW_GECOS detection in configure (which never worked)
[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 ( m_hCursorImageList )
82 ImageList_Destroy((HIMAGELIST) m_hCursorImageList);
83 }
84
85 void wxDragImage::Init()
86 {
87 m_hImageList = 0;
88 m_hCursorImageList = 0;
89 m_window = (wxWindow*) NULL;
90 m_fullScreen = FALSE;
91 }
92
93 // Attributes
94 ////////////////////////////////////////////////////////////////////////////
95
96
97 // Operations
98 ////////////////////////////////////////////////////////////////////////////
99
100 // Create a drag image from a bitmap and optional cursor
101 bool wxDragImage::Create(const wxBitmap& image, const wxCursor& cursor, const wxPoint& hotspot)
102 {
103 if ( m_hImageList )
104 ImageList_Destroy(GetHimageList());
105 m_hImageList = 0;
106
107 UINT flags = 0 ;
108 if (image.GetDepth() <= 4)
109 flags = ILC_COLOR4;
110 else if (image.GetDepth() <= 8)
111 flags = ILC_COLOR8;
112 else if (image.GetDepth() <= 16)
113 flags = ILC_COLOR16;
114 else if (image.GetDepth() <= 24)
115 flags = ILC_COLOR24;
116 else
117 flags = ILC_COLOR32;
118
119 bool mask = (image.GetMask() != 0);
120 if ( mask )
121 flags |= ILC_MASK;
122
123 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
124
125 int index;
126 if (!mask)
127 {
128 HBITMAP hBitmap1 = (HBITMAP) image.GetHBITMAP();
129 index = ImageList_Add(GetHimageList(), hBitmap1, 0);
130 }
131 else
132 {
133 HBITMAP hBitmap1 = (HBITMAP) image.GetHBITMAP();
134 HBITMAP hBitmap2 = (HBITMAP) image.GetMask()->GetMaskBitmap();
135 HBITMAP hbmpMask = wxInvertMask(hBitmap2);
136
137 index = ImageList_Add(GetHimageList(), hBitmap1, hbmpMask);
138 ::DeleteObject(hbmpMask);
139 }
140 if ( index == -1 )
141 {
142 wxLogError(_("Couldn't add an image to the image list."));
143 }
144 m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
145 m_hotspot = hotspot;
146
147 return (index != -1) ;
148 }
149
150 // Create a drag image from an icon and optional cursor
151 bool wxDragImage::Create(const wxIcon& image, const wxCursor& cursor, const wxPoint& hotspot)
152 {
153 if ( m_hImageList )
154 ImageList_Destroy(GetHimageList());
155 m_hImageList = 0;
156
157 UINT flags = 0 ;
158 if (image.GetDepth() <= 4)
159 flags = ILC_COLOR4;
160 else if (image.GetDepth() <= 8)
161 flags = ILC_COLOR8;
162 else if (image.GetDepth() <= 16)
163 flags = ILC_COLOR16;
164 else if (image.GetDepth() <= 24)
165 flags = ILC_COLOR24;
166 else
167 flags = ILC_COLOR32;
168 bool mask = TRUE;
169 if ( mask )
170 flags |= ILC_MASK;
171
172 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
173
174 HICON hIcon = (HICON) image.GetHICON();
175
176 int index = ImageList_AddIcon(GetHimageList(), hIcon);
177 if ( index == -1 )
178 {
179 wxLogError(_("Couldn't add an image to the image list."));
180 }
181
182 m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
183 m_hotspot = hotspot;
184
185 return (index != -1) ;
186 }
187
188 // Create a drag image from a string and optional cursor
189 bool wxDragImage::Create(const wxString& str, const wxCursor& cursor, const wxPoint& hotspot)
190 {
191 wxFont font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
192
193 long w, h;
194 wxScreenDC dc;
195 dc.SetFont(font);
196 dc.GetTextExtent(str, & w, & h);
197 dc.SetFont(wxNullFont);
198
199 wxMemoryDC dc2;
200 dc2.SetFont(font);
201 wxBitmap bitmap((int) w+2, (int) h+2);
202 dc2.SelectObject(bitmap);
203
204 dc2.SetBackground(* wxWHITE_BRUSH);
205 dc2.Clear();
206 dc2.SetBackgroundMode(wxTRANSPARENT);
207 dc2.SetTextForeground(* wxLIGHT_GREY);
208 dc2.DrawText(str, 0, 0);
209 dc2.DrawText(str, 1, 0);
210 dc2.DrawText(str, 2, 0);
211 dc2.DrawText(str, 1, 1);
212 dc2.DrawText(str, 2, 1);
213 dc2.DrawText(str, 1, 2);
214 dc2.DrawText(str, 2, 2);
215 dc2.SetTextForeground(* wxBLACK);
216 dc2.DrawText(str, 1, 1);
217
218 dc2.SelectObject(wxNullBitmap);
219
220 // Make the bitmap masked
221 wxImage image(bitmap);
222 image.SetMaskColour(255, 255, 255);
223 bitmap = image.ConvertToBitmap();
224
225 return Create(bitmap, cursor, hotspot);
226 }
227
228 // Create a drag image for the given tree control item
229 bool wxDragImage::Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id)
230 {
231 if ( m_hImageList )
232 ImageList_Destroy(GetHimageList());
233 m_hImageList = (WXHIMAGELIST) TreeView_CreateDragImage((HWND) treeCtrl.GetHWND(), (HTREEITEM) (WXHTREEITEM) id);
234 return TRUE;
235 }
236
237 // Create a drag image for the given list control item
238 bool wxDragImage::Create(const wxListCtrl& listCtrl, long id)
239 {
240 if ( m_hImageList )
241 ImageList_Destroy(GetHimageList());
242 POINT pt;
243 pt.x = 0; pt.y = 0;
244 m_hImageList = (WXHIMAGELIST) ListView_CreateDragImage((HWND) listCtrl.GetHWND(), id, & pt);
245 return TRUE;
246 }
247
248 // Begin drag
249 bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, bool fullScreen, wxRect* rect)
250 {
251 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in BeginDrag."));
252 wxASSERT_MSG( (window != 0), wxT("Window must not be null in BeginDrag."));
253
254 m_fullScreen = fullScreen;
255 if (rect)
256 m_boundingRect = * rect;
257
258 bool ret = (ImageList_BeginDrag(GetHimageList(), 0, hotspot.x, hotspot.y) != 0);
259
260 if (!ret)
261 {
262 wxFAIL_MSG( _T("BeginDrag failed.") );
263
264 return FALSE;
265 }
266
267 if (m_cursor.Ok())
268 {
269 if (!m_hCursorImageList)
270 {
271 int cxCursor = GetSystemMetrics(SM_CXCURSOR);
272 int cyCursor = GetSystemMetrics(SM_CYCURSOR);
273
274 m_hCursorImageList = (WXHIMAGELIST) ImageList_Create(cxCursor, cyCursor, ILC_MASK, 1, 1);
275 }
276
277 // First add the cursor to the image list
278 HCURSOR hCursor = (HCURSOR) m_cursor.GetHCURSOR();
279 int cursorIndex = ImageList_AddIcon((HIMAGELIST) m_hCursorImageList, (HICON) hCursor);
280
281 wxASSERT_MSG( (cursorIndex != -1), wxT("ImageList_AddIcon failed in BeginDrag."));
282
283 if (cursorIndex != -1)
284 {
285 ImageList_SetDragCursorImage((HIMAGELIST) m_hCursorImageList, cursorIndex, m_hotspot.x, m_hotspot.y);
286 }
287 }
288
289 m_window = window;
290 ::ShowCursor(FALSE);
291
292 ::SetCapture(GetHwndOf(window));
293
294 return TRUE;
295 }
296
297 // Begin drag. hotspot is the location of the drag position relative to the upper-left
298 // corner of the image. This is full screen only. fullScreenRect gives the
299 // position of the window on the screen, to restrict the drag to.
300 bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, wxWindow* fullScreenRect)
301 {
302 wxRect rect;
303
304 int x = fullScreenRect->GetPosition().x;
305 int y = fullScreenRect->GetPosition().y;
306
307 wxSize sz = fullScreenRect->GetSize();
308
309 if (fullScreenRect->GetParent() && !fullScreenRect->IsKindOf(CLASSINFO(wxFrame)))
310 fullScreenRect->GetParent()->ClientToScreen(& x, & y);
311
312 rect.x = x; rect.y = y;
313 rect.width = sz.x; rect.height = sz.y;
314
315 return BeginDrag(hotspot, window, TRUE, & rect);
316 }
317
318 // End drag
319 bool wxDragImage::EndDrag()
320 {
321 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in EndDrag."));
322
323 ImageList_EndDrag();
324
325 if ( !::ReleaseCapture() )
326 {
327 wxLogLastError("ReleaseCapture");
328 }
329
330 ::ShowCursor(TRUE);
331 m_window = (wxWindow*) NULL;
332
333 return TRUE;
334 }
335
336 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
337 // is non-NULL, or in screen coordinates if NULL.
338 bool wxDragImage::Move(const wxPoint& pt)
339 {
340 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Move."));
341
342 // TODO: what coordinates are these in: window, client, or screen?
343 bool ret = (ImageList_DragMove( pt.x, pt.y ) != 0);
344
345 m_position = pt;
346
347 return ret;
348 }
349
350 bool wxDragImage::Show()
351 {
352 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Show."));
353
354 HWND hWnd = 0;
355 if (m_window && !m_fullScreen)
356 hWnd = (HWND) m_window->GetHWND();
357
358 bool ret = (ImageList_DragEnter( hWnd, m_position.x, m_position.y ) != 0);
359
360 return ret;
361 }
362
363 bool wxDragImage::Hide()
364 {
365 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Hide."));
366
367 HWND hWnd = 0;
368 if (m_window && !m_fullScreen)
369 hWnd = (HWND) m_window->GetHWND();
370
371 bool ret = (ImageList_DragLeave( hWnd ) != 0);
372
373 return ret;
374 }
375
376 #endif
377 // __WIN95__
378