]> git.saurik.com Git - wxWidgets.git/blob - src/msw/cursor.cpp
VC++ 1.5 makefile fixes (lib split, C objs)
[wxWidgets.git] / src / msw / cursor.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cursor.cpp
3 // Purpose: wxCursor class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "cursor.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 #ifndef WX_PRECOMP
24 #include <stdio.h>
25 #include "wx/setup.h"
26 #include "wx/list.h"
27 #include "wx/utils.h"
28 #include "wx/app.h"
29 #include "wx/cursor.h"
30 #include "wx/icon.h"
31 #endif
32
33 #include "wx/msw/private.h"
34 #include "wx/msw/dib.h"
35
36 #include "assert.h"
37
38 #if wxUSE_RESOURCE_LOADING_IN_MSW
39 #include "wx/msw/curico.h"
40 #include "wx/msw/curicop.h"
41 #endif
42
43 #if !USE_SHARED_LIBRARIES
44 IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxBitmap)
45 #endif
46
47 wxCursorRefData::wxCursorRefData(void)
48 {
49 m_width = 32; m_height = 32;
50 m_hCursor = 0 ;
51 m_destroyCursor = FALSE;
52 }
53
54 wxCursorRefData::~wxCursorRefData(void)
55 {
56 if ( m_hCursor && m_destroyCursor)
57 #ifdef __WXWINE__
58 ::DestroyCursor((HCURSOR) m_hCursor);
59 #else
60 ::DestroyCursor((HICON) m_hCursor);
61 #endif
62 }
63
64 // Cursors
65 wxCursor::wxCursor(void)
66 {
67 }
68
69 wxCursor::wxCursor(const char WXUNUSED(bits)[], int WXUNUSED(width), int WXUNUSED(height),
70 int WXUNUSED(hotSpotX), int WXUNUSED(hotSpotY), const char WXUNUSED(maskBits)[])
71 {
72 }
73
74 wxCursor::wxCursor(const wxString& cursor_file, long flags, int hotSpotX, int hotSpotY)
75 {
76 m_refData = new wxCursorRefData;
77
78 M_CURSORDATA->m_destroyCursor = FALSE;
79 M_CURSORDATA->m_hCursor = 0;
80 M_CURSORDATA->m_ok = FALSE;
81 if (flags & wxBITMAP_TYPE_CUR_RESOURCE)
82 {
83 #ifdef __WIN95__
84 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadImage(wxGetInstance(), cursor_file, IMAGE_CURSOR, 0, 0, 0);
85 #else
86 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), cursor_file);
87 #endif
88 if (M_CURSORDATA->m_hCursor)
89 M_CURSORDATA->m_ok = TRUE;
90 else
91 M_CURSORDATA->m_ok = FALSE;
92 }
93 else if (flags & wxBITMAP_TYPE_CUR)
94 {
95 #ifdef __WIN95__
96 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadImage(wxGetInstance(), cursor_file, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
97 #else
98 #if wxUSE_RESOURCE_LOADING_IN_MSW
99 M_CURSORDATA->m_hCursor = (WXHCURSOR) ReadCursorFile(WXSTRINGCAST cursor_file, wxGetInstance(), &M_CURSORDATA->m_width, &M_CURSORDATA->m_height);
100 M_CURSORDATA->m_destroyCursor = TRUE;
101 #endif
102 #endif
103 }
104 else if (flags & wxBITMAP_TYPE_ICO)
105 {
106 #if wxUSE_RESOURCE_LOADING_IN_MSW
107 M_CURSORDATA->m_hCursor = (WXHCURSOR) IconToCursor(WXSTRINGCAST cursor_file, wxGetInstance(), hotSpotX, hotSpotY, &M_CURSORDATA->m_width, &M_CURSORDATA->m_height);
108 M_CURSORDATA->m_destroyCursor = TRUE;
109 #endif
110 }
111 else if (flags & wxBITMAP_TYPE_BMP)
112 {
113 #if wxUSE_RESOURCE_LOADING_IN_MSW
114 HBITMAP hBitmap = 0;
115 HPALETTE hPalette = 0;
116 bool success = wxReadDIB(WXSTRINGCAST cursor_file, &hBitmap, &hPalette) != 0;
117 if (!success)
118 return;
119 if (hPalette)
120 DeleteObject(hPalette);
121 POINT pnt;
122 pnt.x = hotSpotX;
123 pnt.y = hotSpotY;
124 M_CURSORDATA->m_hCursor = (WXHCURSOR) MakeCursorFromBitmap(wxGetInstance(), hBitmap, &pnt);
125 M_CURSORDATA->m_destroyCursor = TRUE;
126 DeleteObject(hBitmap);
127 if (M_CURSORDATA->m_hCursor)
128 M_CURSORDATA->m_ok = TRUE;
129 #endif
130 }
131 }
132
133 // Cursors by stock number
134 wxCursor::wxCursor(int cursor_type)
135 {
136 m_refData = new wxCursorRefData;
137
138 switch (cursor_type)
139 {
140 case wxCURSOR_WAIT:
141 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_WAIT);
142 break;
143 case wxCURSOR_IBEAM:
144 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_IBEAM);
145 break;
146 case wxCURSOR_CROSS:
147 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_CROSS);
148 break;
149 case wxCURSOR_SIZENWSE:
150 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZENWSE);
151 break;
152 case wxCURSOR_SIZENESW:
153 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZENESW);
154 break;
155 case wxCURSOR_SIZEWE:
156 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZEWE);
157 break;
158 case wxCURSOR_SIZENS:
159 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZENS);
160 break;
161 case wxCURSOR_CHAR:
162 {
163 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
164 break;
165 }
166 case wxCURSOR_HAND:
167 {
168 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_HAND"));
169 break;
170 }
171 case wxCURSOR_BULLSEYE:
172 {
173 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_BULLSEYE"));
174 break;
175 }
176 case wxCURSOR_PENCIL:
177 {
178 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PENCIL"));
179 break;
180 }
181 case wxCURSOR_MAGNIFIER:
182 {
183 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_MAGNIFIER"));
184 break;
185 }
186 case wxCURSOR_NO_ENTRY:
187 {
188 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_NO_ENTRY"));
189 break;
190 }
191 case wxCURSOR_LEFT_BUTTON:
192 {
193 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
194 break;
195 }
196 case wxCURSOR_RIGHT_BUTTON:
197 {
198 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
199 break;
200 }
201 case wxCURSOR_MIDDLE_BUTTON:
202 {
203 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
204 break;
205 }
206 case wxCURSOR_SIZING:
207 {
208 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_SIZING"));
209 break;
210 }
211 case wxCURSOR_WATCH:
212 {
213 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_WATCH"));
214 break;
215 }
216 case wxCURSOR_SPRAYCAN:
217 {
218 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_ROLLER"));
219 break;
220 }
221 case wxCURSOR_PAINT_BRUSH:
222 {
223 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PBRUSH"));
224 break;
225 }
226 case wxCURSOR_POINT_LEFT:
227 {
228 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PLEFT"));
229 break;
230 }
231 case wxCURSOR_POINT_RIGHT:
232 {
233 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PRIGHT"));
234 break;
235 }
236 case wxCURSOR_QUESTION_ARROW:
237 {
238 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_QARROW"));
239 break;
240 }
241 case wxCURSOR_BLANK:
242 {
243 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_BLANK"));
244 break;
245 }
246 default:
247 case wxCURSOR_ARROW:
248 M_CURSORDATA->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
249 break;
250 }
251 }
252
253 wxCursor::~wxCursor(void)
254 {
255 // FreeResource(TRUE);
256 }
257
258 bool wxCursor::FreeResource(bool WXUNUSED(force))
259 {
260 if (M_CURSORDATA && M_CURSORDATA->m_hCursor && M_CURSORDATA->m_destroyCursor)
261 {
262 DestroyCursor((HCURSOR) M_CURSORDATA->m_hCursor);
263 M_CURSORDATA->m_hCursor = 0;
264 }
265 return TRUE;
266 }
267
268 void wxCursor::SetHCURSOR(WXHCURSOR cursor)
269 {
270 if ( !M_CURSORDATA )
271 m_refData = new wxCursorRefData;
272
273 M_CURSORDATA->m_hCursor = cursor;
274 }
275
276 // Global cursor setting
277 void wxSetCursor(const wxCursor& cursor)
278 {
279 extern wxCursor *g_globalCursor;
280
281 if ( cursor.Ok() && cursor.GetHCURSOR() )
282 {
283 ::SetCursor((HCURSOR) cursor.GetHCURSOR());
284
285 if ( g_globalCursor )
286 (*g_globalCursor) = cursor;
287 }
288 }
289
290