]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dragimag.cpp
Added $(NEW_WXLIBNAME) to allow changing the library or DLL name
[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 #ifdef __GNUG__
13 #pragma implementation "dragimag.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if defined(__WIN95__)
24
25 #ifndef WX_PRECOMP
26 #include <stdio.h>
27 #include "wx/setup.h"
28 #include "wx/window.h"
29 #include "wx/dcclient.h"
30 #endif
31
32 #include "wx/log.h"
33 #include "wx/intl.h"
34
35 #include "wx/msw/dragimag.h"
36 #include "wx/msw/private.h"
37
38 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
39 #include <commctrl.h>
40 #endif
41
42 #if !USE_SHARED_LIBRARY
43 IMPLEMENT_DYNAMIC_CLASS(wxDragImage, wxObject)
44 #endif
45
46 wxDragImage::wxDragImage()
47 {
48 m_hImageList = 0;
49 }
50
51 wxDragImage::~wxDragImage()
52 {
53 if ( m_hImageList )
54 ImageList_Destroy((HIMAGELIST) m_hImageList);
55 m_hImageList = 0;
56 }
57
58
59 // Attributes
60 ////////////////////////////////////////////////////////////////////////////
61
62
63 // Operations
64 ////////////////////////////////////////////////////////////////////////////
65
66 // Create a drag image from a bitmap and optional cursor
67 bool wxDragImage::Create(const wxBitmap& image, const wxCursor& cursor, const wxPoint& hotspot)
68 {
69 if ( m_hImageList )
70 ImageList_Destroy((HIMAGELIST) m_hImageList);
71 m_hImageList = 0;
72
73 UINT flags = 0;
74 bool mask = TRUE; // ?
75 if ( mask )
76 flags |= ILC_MASK;
77
78 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
79
80 HBITMAP hBitmap1 = (HBITMAP) image.GetHBITMAP();
81 HBITMAP hBitmap2 = 0;
82 if ( image.GetMask() )
83 hBitmap2 = (HBITMAP) image.GetMask()->GetMaskBitmap();
84
85 int index = ImageList_Add((HIMAGELIST) m_hImageList, hBitmap1, hBitmap2);
86 if ( index == -1 )
87 {
88 wxLogError(_("Couldn't add an image to the image list."));
89 }
90
91 m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
92 m_hotspot = hotspot;
93
94 return (index != -1) ;
95 }
96
97 // Create a drag image from an icon and optional cursor
98 bool wxDragImage::Create(const wxIcon& image, const wxCursor& cursor, const wxPoint& hotspot)
99 {
100 if ( m_hImageList )
101 ImageList_Destroy((HIMAGELIST) m_hImageList);
102 m_hImageList = 0;
103
104 UINT flags = 0;
105 bool mask = TRUE; // ?
106 if ( mask )
107 flags |= ILC_MASK;
108
109 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
110
111 HICON hIcon = (HICON) image.GetHICON();
112
113 int index = ImageList_AddIcon((HIMAGELIST) m_hImageList, hIcon);
114 if ( index == -1 )
115 {
116 wxLogError(_("Couldn't add an image to the image list."));
117 }
118
119 m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
120 m_hotspot = hotspot;
121
122 return (index != -1) ;
123 }
124
125 // Create a drag image from a string and optional cursor
126 bool wxDragImage::Create(const wxString& str, const wxCursor& cursor, const wxPoint& hotspot)
127 {
128 wxFont font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
129
130 long w, h;
131 wxScreenDC dc;
132 dc.SetFont(font);
133 dc.GetTextExtent(str, & w, & h);
134
135 wxMemoryDC dc2;
136 dc2.SetFont(font);
137 wxBitmap bitmap((int) w, (int) h);
138 dc2.SelectObject(bitmap);
139
140 dc2.SetBackground(* wxWHITE_BRUSH);
141 dc2.Clear();
142 dc2.DrawText(str, 0, 0);
143
144 dc2.SelectObject(wxNullBitmap);
145
146 return Create(bitmap, cursor, hotspot);
147 }
148
149 // Create a drag image for the given tree control item
150 bool wxDragImage::Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id)
151 {
152 if ( m_hImageList )
153 ImageList_Destroy((HIMAGELIST) m_hImageList);
154 m_hImageList = (WXHIMAGELIST) TreeView_CreateDragImage((HWND) treeCtrl.GetHWND(), (HTREEITEM) (WXHTREEITEM) id);
155 return TRUE;
156 }
157
158 // Create a drag image for the given list control item
159 bool wxDragImage::Create(const wxListCtrl& listCtrl, long id)
160 {
161 if ( m_hImageList )
162 ImageList_Destroy((HIMAGELIST) m_hImageList);
163 POINT pt;
164 pt.x = 0; pt.y = 0;
165 m_hImageList = (WXHIMAGELIST) ListView_CreateDragImage((HWND) listCtrl.GetHWND(), id, & pt);
166 return TRUE;
167 }
168
169 // Begin drag
170 bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* WXUNUSED(window))
171 {
172 wxASSERT_MSG( (m_hImageList != 0), "Image list must not be null in BeginDrag.");
173
174 bool ret = (ImageList_BeginDrag((HIMAGELIST) m_hImageList, 0, hotspot.x, hotspot.y) != 0);
175
176 wxASSERT_MSG( (ret), "BeginDrag failed.");
177
178 if (!ret)
179 return FALSE;
180
181 if (m_cursor.Ok())
182 {
183 // First add the cursor to the image list
184 int cursorIndex = ImageList_AddIcon((HIMAGELIST) m_hImageList, (HICON) m_cursor.GetHCURSOR());
185
186 wxASSERT_MSG( (cursorIndex != -1), "ImageList_AddIcon failed in BeginDrag.");
187
188 if (cursorIndex != -1)
189 {
190 ImageList_SetDragCursorImage((HIMAGELIST) m_hImageList, cursorIndex, m_hotspot.x, m_hotspot.y);
191 }
192 }
193
194 ::ShowCursor(FALSE);
195
196 return TRUE;
197 }
198
199 // End drag
200 bool wxDragImage::EndDrag(wxWindow* WXUNUSED(window))
201 {
202 wxASSERT_MSG( (m_hImageList != 0), "Image list must not be null in EndDrag.");
203
204 ImageList_EndDrag();
205
206 ::ShowCursor(TRUE);
207
208 return TRUE;
209 }
210
211 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
212 // is non-NULL, or in screen coordinates if NULL.
213 bool wxDragImage::Move(const wxPoint& pt, wxWindow* window)
214 {
215 wxASSERT_MSG( (m_hImageList != 0), "Image list must not be null in Move.");
216
217 // TODO: what coordinates are these in: window, client, or screen?
218 bool ret = (ImageList_DragMove( pt.x, pt.y ) != 0);
219
220 m_position = pt;
221
222 return ret;
223 }
224
225 bool wxDragImage::Show(wxWindow* window)
226 {
227 wxASSERT_MSG( (m_hImageList != 0), "Image list must not be null in Show.");
228
229 HWND hWnd = 0;
230 if (window)
231 hWnd = (HWND) window->GetHWND();
232
233 bool ret = (ImageList_DragEnter( hWnd, m_position.x, m_position.y ) != 0);
234
235 return ret;
236 }
237
238 bool wxDragImage::Hide(wxWindow* window)
239 {
240 wxASSERT_MSG( (m_hImageList != 0), "Image list must not be null in Hide.");
241
242 HWND hWnd = 0;
243 if (window)
244 hWnd = (HWND) window->GetHWND();
245
246 bool ret = (ImageList_DragLeave( hWnd ) != 0);
247
248 return ret;
249 }
250
251 #endif
252 // __WIN95__
253