]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dragimag.cpp
Added #include "wx/bitmap.h"
[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
47 #include "wx/msw/dragimag.h"
48 #include "wx/msw/private.h"
49
50 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
51 #include <commctrl.h>
52 #endif
53
54 // ----------------------------------------------------------------------------
55 // macros
56 // ----------------------------------------------------------------------------
57
58 IMPLEMENT_DYNAMIC_CLASS(wxDragImage, wxObject)
59
60 #define GetHimageList() ((HIMAGELIST) m_hImageList)
61
62 // ============================================================================
63 // implementation
64 // ============================================================================
65
66 // ----------------------------------------------------------------------------
67 // wxDragImage ctors/dtor
68 // ----------------------------------------------------------------------------
69
70 wxDragImage::wxDragImage()
71 {
72 m_hImageList = 0;
73 }
74
75 wxDragImage::~wxDragImage()
76 {
77 if ( m_hImageList )
78 ImageList_Destroy(GetHimageList());
79 }
80
81
82 // Attributes
83 ////////////////////////////////////////////////////////////////////////////
84
85
86 // Operations
87 ////////////////////////////////////////////////////////////////////////////
88
89 // Create a drag image from a bitmap and optional cursor
90 bool wxDragImage::Create(const wxBitmap& image, const wxCursor& cursor, const wxPoint& hotspot)
91 {
92 if ( m_hImageList )
93 ImageList_Destroy(GetHimageList());
94 m_hImageList = 0;
95
96 UINT flags = 0;
97 bool mask = TRUE; // ?
98 if ( mask )
99 flags |= ILC_MASK;
100
101 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
102
103 HBITMAP hBitmap1 = (HBITMAP) image.GetHBITMAP();
104 HBITMAP hBitmap2 = 0;
105 if ( image.GetMask() )
106 hBitmap2 = (HBITMAP) image.GetMask()->GetMaskBitmap();
107
108 int index = ImageList_Add(GetHimageList(), hBitmap1, hBitmap2);
109 if ( index == -1 )
110 {
111 wxLogError(_("Couldn't add an image to the image list."));
112 }
113
114 m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
115 m_hotspot = hotspot;
116
117 return (index != -1) ;
118 }
119
120 // Create a drag image from an icon and optional cursor
121 bool wxDragImage::Create(const wxIcon& image, const wxCursor& cursor, const wxPoint& hotspot)
122 {
123 if ( m_hImageList )
124 ImageList_Destroy(GetHimageList());
125 m_hImageList = 0;
126
127 UINT flags = 0;
128 bool mask = TRUE; // ?
129 if ( mask )
130 flags |= ILC_MASK;
131
132 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
133
134 HICON hIcon = (HICON) image.GetHICON();
135
136 int index = ImageList_AddIcon(GetHimageList(), hIcon);
137 if ( index == -1 )
138 {
139 wxLogError(_("Couldn't add an image to the image list."));
140 }
141
142 m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
143 m_hotspot = hotspot;
144
145 return (index != -1) ;
146 }
147
148 // Create a drag image from a string and optional cursor
149 bool wxDragImage::Create(const wxString& str, const wxCursor& cursor, const wxPoint& hotspot)
150 {
151 wxFont font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
152
153 long w, h;
154 wxScreenDC dc;
155 dc.SetFont(font);
156 dc.GetTextExtent(str, & w, & h);
157
158 wxMemoryDC dc2;
159 dc2.SetFont(font);
160 wxBitmap bitmap((int) w, (int) h);
161 dc2.SelectObject(bitmap);
162
163 dc2.SetBackground(* wxWHITE_BRUSH);
164 dc2.Clear();
165 dc2.DrawText(str, 0, 0);
166
167 dc2.SelectObject(wxNullBitmap);
168
169 return Create(bitmap, cursor, hotspot);
170 }
171
172 // Create a drag image for the given tree control item
173 bool wxDragImage::Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id)
174 {
175 if ( m_hImageList )
176 ImageList_Destroy(GetHimageList());
177 m_hImageList = (WXHIMAGELIST) TreeView_CreateDragImage((HWND) treeCtrl.GetHWND(), (HTREEITEM) (WXHTREEITEM) id);
178 return TRUE;
179 }
180
181 // Create a drag image for the given list control item
182 bool wxDragImage::Create(const wxListCtrl& listCtrl, long id)
183 {
184 if ( m_hImageList )
185 ImageList_Destroy(GetHimageList());
186 POINT pt;
187 pt.x = 0; pt.y = 0;
188 m_hImageList = (WXHIMAGELIST) ListView_CreateDragImage((HWND) listCtrl.GetHWND(), id, & pt);
189 return TRUE;
190 }
191
192 // Begin drag
193 bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window)
194 {
195 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in BeginDrag."));
196
197 bool ret = (ImageList_BeginDrag(GetHimageList(), 0, hotspot.x, hotspot.y) != 0);
198
199 if (!ret)
200 {
201 wxFAIL_MSG( _T("BeginDrag failed.") );
202
203 return FALSE;
204 }
205
206 if (m_cursor.Ok())
207 {
208 // First add the cursor to the image list
209 int cursorIndex = ImageList_AddIcon(GetHimageList(), (HICON) m_cursor.GetHCURSOR());
210
211 wxASSERT_MSG( (cursorIndex != -1), wxT("ImageList_AddIcon failed in BeginDrag."));
212
213 if (cursorIndex != -1)
214 {
215 ImageList_SetDragCursorImage(GetHimageList(), cursorIndex, m_hotspot.x, m_hotspot.y);
216 }
217 }
218
219 ::ShowCursor(FALSE);
220 ::SetCapture(GetHwndOf(window));
221
222 return TRUE;
223 }
224
225 // End drag
226 bool wxDragImage::EndDrag(wxWindow* WXUNUSED(window))
227 {
228 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in EndDrag."));
229
230 ImageList_EndDrag();
231
232 if ( !::ReleaseCapture() )
233 {
234 wxLogLastError("ReleaseCapture");
235 }
236
237 ::ShowCursor(TRUE);
238
239 return TRUE;
240 }
241
242 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
243 // is non-NULL, or in screen coordinates if NULL.
244 bool wxDragImage::Move(const wxPoint& pt, wxWindow* window)
245 {
246 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Move."));
247
248 // TODO: what coordinates are these in: window, client, or screen?
249 bool ret = (ImageList_DragMove( pt.x, pt.y ) != 0);
250
251 m_position = pt;
252
253 return ret;
254 }
255
256 bool wxDragImage::Show(wxWindow* window)
257 {
258 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Show."));
259
260 HWND hWnd = 0;
261 if (window)
262 hWnd = (HWND) window->GetHWND();
263
264 bool ret = (ImageList_DragEnter( hWnd, m_position.x, m_position.y ) != 0);
265
266 return ret;
267 }
268
269 bool wxDragImage::Hide(wxWindow* window)
270 {
271 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Hide."));
272
273 HWND hWnd = 0;
274 if (window)
275 hWnd = (HWND) window->GetHWND();
276
277 bool ret = (ImageList_DragLeave( hWnd ) != 0);
278
279 return ret;
280 }
281
282 #endif
283 // __WIN95__
284