]>
Commit | Line | Data |
---|---|---|
7cf83330 JS |
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 | |
23f681ec | 9 | // Licence: wxWindows licence |
7cf83330 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
23f681ec VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
7cf83330 JS |
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" | |
ff0ea71c | 26 | #include "wx/msw/private.h" |
7cf83330 JS |
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" | |
2662e49e RR |
39 | #include "wx/dcscreen.h" |
40 | #include "wx/dcmemory.h" | |
41 | #include "wx/settings.h" | |
7cf83330 JS |
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 | ||
23f681ec | 50 | #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) |
7cf83330 JS |
51 | #include <commctrl.h> |
52 | #endif | |
53 | ||
23f681ec VZ |
54 | // ---------------------------------------------------------------------------- |
55 | // macros | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
7cf83330 | 58 | IMPLEMENT_DYNAMIC_CLASS(wxDragImage, wxObject) |
7cf83330 | 59 | |
23f681ec VZ |
60 | #define GetHimageList() ((HIMAGELIST) m_hImageList) |
61 | ||
62 | // ============================================================================ | |
63 | // implementation | |
64 | // ============================================================================ | |
65 | ||
66 | // ---------------------------------------------------------------------------- | |
67 | // wxDragImage ctors/dtor | |
68 | // ---------------------------------------------------------------------------- | |
69 | ||
7cf83330 JS |
70 | wxDragImage::wxDragImage() |
71 | { | |
72 | m_hImageList = 0; | |
73 | } | |
74 | ||
75 | wxDragImage::~wxDragImage() | |
76 | { | |
23f681ec VZ |
77 | if ( m_hImageList ) |
78 | ImageList_Destroy(GetHimageList()); | |
7cf83330 JS |
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 | { | |
23f681ec VZ |
92 | if ( m_hImageList ) |
93 | ImageList_Destroy(GetHimageList()); | |
94 | m_hImageList = 0; | |
7cf83330 | 95 | |
23f681ec | 96 | UINT flags = 0; |
7cf83330 | 97 | bool mask = TRUE; // ? |
23f681ec VZ |
98 | if ( mask ) |
99 | flags |= ILC_MASK; | |
7cf83330 | 100 | |
23f681ec | 101 | m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1); |
7cf83330 | 102 | |
23f681ec VZ |
103 | HBITMAP hBitmap1 = (HBITMAP) image.GetHBITMAP(); |
104 | HBITMAP hBitmap2 = 0; | |
105 | if ( image.GetMask() ) | |
106 | hBitmap2 = (HBITMAP) image.GetMask()->GetMaskBitmap(); | |
7cf83330 | 107 | |
23f681ec VZ |
108 | int index = ImageList_Add(GetHimageList(), hBitmap1, hBitmap2); |
109 | if ( index == -1 ) | |
7cf83330 JS |
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 | } | |
23f681ec | 119 | |
7cf83330 JS |
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 | { | |
23f681ec VZ |
123 | if ( m_hImageList ) |
124 | ImageList_Destroy(GetHimageList()); | |
125 | m_hImageList = 0; | |
7cf83330 | 126 | |
23f681ec | 127 | UINT flags = 0; |
7cf83330 | 128 | bool mask = TRUE; // ? |
23f681ec VZ |
129 | if ( mask ) |
130 | flags |= ILC_MASK; | |
7cf83330 | 131 | |
23f681ec | 132 | m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1); |
7cf83330 | 133 | |
23f681ec | 134 | HICON hIcon = (HICON) image.GetHICON(); |
7cf83330 | 135 | |
23f681ec VZ |
136 | int index = ImageList_AddIcon(GetHimageList(), hIcon); |
137 | if ( index == -1 ) | |
7cf83330 JS |
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 | } | |
23f681ec | 147 | |
7cf83330 JS |
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 | { | |
23f681ec VZ |
175 | if ( m_hImageList ) |
176 | ImageList_Destroy(GetHimageList()); | |
7cf83330 JS |
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 | { | |
23f681ec VZ |
184 | if ( m_hImageList ) |
185 | ImageList_Destroy(GetHimageList()); | |
7cf83330 JS |
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 | |
23f681ec | 193 | bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window) |
7cf83330 | 194 | { |
223d09f6 | 195 | wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in BeginDrag.")); |
7cf83330 | 196 | |
23f681ec | 197 | bool ret = (ImageList_BeginDrag(GetHimageList(), 0, hotspot.x, hotspot.y) != 0); |
7cf83330 JS |
198 | |
199 | if (!ret) | |
23f681ec VZ |
200 | { |
201 | wxFAIL_MSG( _T("BeginDrag failed.") ); | |
202 | ||
7cf83330 | 203 | return FALSE; |
23f681ec | 204 | } |
7cf83330 JS |
205 | |
206 | if (m_cursor.Ok()) | |
207 | { | |
208 | // First add the cursor to the image list | |
23f681ec | 209 | int cursorIndex = ImageList_AddIcon(GetHimageList(), (HICON) m_cursor.GetHCURSOR()); |
7cf83330 | 210 | |
223d09f6 | 211 | wxASSERT_MSG( (cursorIndex != -1), wxT("ImageList_AddIcon failed in BeginDrag.")); |
7cf83330 JS |
212 | |
213 | if (cursorIndex != -1) | |
214 | { | |
23f681ec | 215 | ImageList_SetDragCursorImage(GetHimageList(), cursorIndex, m_hotspot.x, m_hotspot.y); |
7cf83330 JS |
216 | } |
217 | } | |
218 | ||
219 | ::ShowCursor(FALSE); | |
23f681ec | 220 | ::SetCapture(GetHwndOf(window)); |
7cf83330 JS |
221 | |
222 | return TRUE; | |
223 | } | |
23f681ec | 224 | |
7cf83330 JS |
225 | // End drag |
226 | bool wxDragImage::EndDrag(wxWindow* WXUNUSED(window)) | |
227 | { | |
223d09f6 | 228 | wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in EndDrag.")); |
7cf83330 JS |
229 | |
230 | ImageList_EndDrag(); | |
231 | ||
23f681ec VZ |
232 | if ( !::ReleaseCapture() ) |
233 | { | |
234 | wxLogLastError("ReleaseCapture"); | |
235 | } | |
236 | ||
7cf83330 JS |
237 | ::ShowCursor(TRUE); |
238 | ||
239 | return TRUE; | |
240 | } | |
23f681ec | 241 | |
7cf83330 JS |
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 | { | |
223d09f6 | 246 | wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Move.")); |
7cf83330 JS |
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 | { | |
223d09f6 | 258 | wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Show.")); |
7cf83330 JS |
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 | { | |
223d09f6 | 271 | wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Hide.")); |
7cf83330 JS |
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 |