]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dragimag.cpp
fixed compilation under Unix where SQL_DATETIME is not defined
[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 // ----------------------------------------------------------------------------
59 // macros
60 // ----------------------------------------------------------------------------
61
62 IMPLEMENT_DYNAMIC_CLASS(wxDragImage, wxObject)
63
64 #define GetHimageList() ((HIMAGELIST) m_hImageList)
65
66 // ============================================================================
67 // implementation
68 // ============================================================================
69
70 // ----------------------------------------------------------------------------
71 // wxDragImage ctors/dtor
72 // ----------------------------------------------------------------------------
73
74 wxDragImage::wxDragImage()
75 {
76 Init();
77 }
78
79 wxDragImage::~wxDragImage()
80 {
81 if ( m_hImageList )
82 ImageList_Destroy(GetHimageList());
83 #if !wxUSE_SIMPLER_DRAGIMAGE
84 if ( m_hCursorImageList )
85 ImageList_Destroy((HIMAGELIST) m_hCursorImageList);
86 #endif
87 }
88
89 void wxDragImage::Init()
90 {
91 m_hImageList = 0;
92 #if !wxUSE_SIMPLER_DRAGIMAGE
93 m_hCursorImageList = 0;
94 #endif
95 m_window = (wxWindow*) NULL;
96 m_fullScreen = FALSE;
97 }
98
99 // Attributes
100 ////////////////////////////////////////////////////////////////////////////
101
102
103 // Operations
104 ////////////////////////////////////////////////////////////////////////////
105
106 // Create a drag image from a bitmap and optional cursor
107 bool wxDragImage::Create(const wxBitmap& image, const wxCursor& cursor)
108 {
109 if ( m_hImageList )
110 ImageList_Destroy(GetHimageList());
111 m_hImageList = 0;
112
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);
126 if ( mask )
127 flags |= ILC_MASK;
128
129 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
130
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);
142
143 index = ImageList_Add(GetHimageList(), hBitmap1, hbmpMask);
144 ::DeleteObject(hbmpMask);
145 }
146 if ( index == -1 )
147 {
148 wxLogError(_("Couldn't add an image to the image list."));
149 }
150 m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
151
152 return (index != -1) ;
153 }
154
155 // Create a drag image from an icon and optional cursor
156 bool wxDragImage::Create(const wxIcon& image, const wxCursor& cursor)
157 {
158 if ( m_hImageList )
159 ImageList_Destroy(GetHimageList());
160 m_hImageList = 0;
161
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;
174 if ( mask )
175 flags |= ILC_MASK;
176
177 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
178
179 HICON hIcon = (HICON) image.GetHICON();
180
181 int index = ImageList_AddIcon(GetHimageList(), hIcon);
182 if ( index == -1 )
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.
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)
194 {
195 wxFont font(wxSystemSettings::GetFont(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.ConvertToImage();
226 image.SetMaskColour(255, 255, 255);
227 return Create(wxBitmap(image), cursor);
228 }
229
230 // Create a drag image for the given tree control item
231 bool wxDragImage::Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id)
232 {
233 if ( m_hImageList )
234 ImageList_Destroy(GetHimageList());
235 m_hImageList = (WXHIMAGELIST) TreeView_CreateDragImage((HWND) treeCtrl.GetHWND(), (HTREEITEM) (WXHTREEITEM) id);
236 return TRUE;
237 }
238
239 // Create a drag image for the given list control item
240 bool wxDragImage::Create(const wxListCtrl& listCtrl, long id)
241 {
242 if ( m_hImageList )
243 ImageList_Destroy(GetHimageList());
244 POINT pt;
245 pt.x = 0; pt.y = 0;
246 m_hImageList = (WXHIMAGELIST) ListView_CreateDragImage((HWND) listCtrl.GetHWND(), id, & pt);
247 return TRUE;
248 }
249
250 // Begin drag
251 bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, bool fullScreen, wxRect* rect)
252 {
253 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in BeginDrag."));
254 wxASSERT_MSG( (window != 0), wxT("Window must not be null in BeginDrag."));
255
256 m_fullScreen = fullScreen;
257 if (rect)
258 m_boundingRect = * rect;
259
260 bool ret = (ImageList_BeginDrag(GetHimageList(), 0, hotspot.x, hotspot.y) != 0);
261
262 if (!ret)
263 {
264 wxFAIL_MSG( _T("BeginDrag failed.") );
265
266 return FALSE;
267 }
268
269 if (m_cursor.Ok())
270 {
271 #if wxUSE_SIMPLER_DRAGIMAGE
272 m_oldCursor = window->GetCursor();
273 window->SetCursor(m_cursor);
274 #else
275 if (!m_hCursorImageList)
276 {
277 int cxCursor = GetSystemMetrics(SM_CXCURSOR);
278 int cyCursor = GetSystemMetrics(SM_CYCURSOR);
279
280 m_hCursorImageList = (WXHIMAGELIST) ImageList_Create(cxCursor, cyCursor, ILC_MASK, 1, 1);
281 }
282
283 // See if we can find the cursor hotspot
284 wxPoint curHotSpot(hotspot);
285
286 // Although it seems to produce the right position, when the hotspot goeos
287 // negative it has strange effects on the image.
288 // How do we stop the cursor jumping right and below of where it should be?
289 #if 0
290 ICONINFO iconInfo;
291 if (::GetIconInfo((HICON) (HCURSOR) m_cursor.GetHCURSOR(), & iconInfo) != 0)
292 {
293 curHotSpot.x -= iconInfo.xHotspot;
294 curHotSpot.y -= iconInfo.yHotspot;
295 }
296 #endif
297 //wxString msg;
298 //msg.Printf("Hotspot = %d, %d", curHotSpot.x, curHotSpot.y);
299 //wxLogDebug(msg);
300
301 // First add the cursor to the image list
302 HCURSOR hCursor = (HCURSOR) m_cursor.GetHCURSOR();
303 int cursorIndex = ImageList_AddIcon((HIMAGELIST) m_hCursorImageList, (HICON) hCursor);
304
305 wxASSERT_MSG( (cursorIndex != -1), wxT("ImageList_AddIcon failed in BeginDrag."));
306
307 if (cursorIndex != -1)
308 {
309 ImageList_SetDragCursorImage((HIMAGELIST) m_hCursorImageList, cursorIndex, curHotSpot.x, curHotSpot.y);
310 }
311 #endif
312 }
313
314 #if !wxUSE_SIMPLER_DRAGIMAGE
315 if (m_cursor.Ok())
316 ::ShowCursor(FALSE);
317 #endif
318
319 m_window = window;
320
321 ::SetCapture(GetHwndOf(window));
322
323 return TRUE;
324 }
325
326 // Begin drag. hotspot is the location of the drag position relative to the upper-left
327 // corner of the image. This is full screen only. fullScreenRect gives the
328 // position of the window on the screen, to restrict the drag to.
329 bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, wxWindow* fullScreenRect)
330 {
331 wxRect rect;
332
333 int x = fullScreenRect->GetPosition().x;
334 int y = fullScreenRect->GetPosition().y;
335
336 wxSize sz = fullScreenRect->GetSize();
337
338 if (fullScreenRect->GetParent() && !fullScreenRect->IsKindOf(CLASSINFO(wxFrame)))
339 fullScreenRect->GetParent()->ClientToScreen(& x, & y);
340
341 rect.x = x; rect.y = y;
342 rect.width = sz.x; rect.height = sz.y;
343
344 return BeginDrag(hotspot, window, TRUE, & rect);
345 }
346
347 // End drag
348 bool wxDragImage::EndDrag()
349 {
350 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in EndDrag."));
351
352 ImageList_EndDrag();
353
354 if ( !::ReleaseCapture() )
355 {
356 wxLogLastError(wxT("ReleaseCapture"));
357 }
358
359 #if wxUSE_SIMPLER_DRAGIMAGE
360 if (m_cursor.Ok() && m_oldCursor.Ok())
361 m_window->SetCursor(m_oldCursor);
362 #else
363 ::ShowCursor(TRUE);
364 #endif
365
366 m_window = (wxWindow*) NULL;
367
368 return TRUE;
369 }
370
371 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
372 // is non-NULL, or in screen coordinates if NULL.
373 bool wxDragImage::Move(const wxPoint& pt)
374 {
375 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Move."));
376
377 // These are in window, not client coordinates.
378 // So need to convert to client coordinates.
379 wxPoint pt2(pt);
380 if (m_window && !m_fullScreen)
381 {
382 RECT rect;
383 rect.left = 0; rect.top = 0;
384 rect.right = 0; rect.bottom = 0;
385 DWORD style = ::GetWindowLong((HWND) m_window->GetHWND(), GWL_STYLE);
386 #ifdef __WIN32__
387 DWORD exStyle = ::GetWindowLong((HWND) m_window->GetHWND(), GWL_EXSTYLE);
388 ::AdjustWindowRectEx(& rect, style, FALSE, exStyle);
389 #else
390 ::AdjustWindowRect(& rect, style, FALSE);
391 #endif
392 // Subtract the (negative) values, i.e. add a small increment
393 pt2.x -= rect.left; pt2.y -= rect.top;
394 }
395 else if (m_window && m_fullScreen)
396 {
397 pt2 = m_window->ClientToScreen(pt2);
398 }
399
400 bool ret = (ImageList_DragMove( pt2.x, pt2.y ) != 0);
401
402 m_position = pt2;
403
404 return ret;
405 }
406
407 bool wxDragImage::Show()
408 {
409 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Show."));
410
411 HWND hWnd = 0;
412 if (m_window && !m_fullScreen)
413 hWnd = (HWND) m_window->GetHWND();
414
415 bool ret = (ImageList_DragEnter( hWnd, m_position.x, m_position.y ) != 0);
416
417 return ret;
418 }
419
420 bool wxDragImage::Hide()
421 {
422 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Hide."));
423
424 HWND hWnd = 0;
425 if (m_window && !m_fullScreen)
426 hWnd = (HWND) m_window->GetHWND();
427
428 bool ret = (ImageList_DragLeave( hWnd ) != 0);
429
430 return ret;
431 }
432
433 #endif
434 // __WIN95__
435
436 #endif // wxUSE_DRAGIMAGE