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