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