]>
Commit | Line | Data |
---|---|---|
2646f485 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: cursor.cpp | |
3 | // Purpose: wxCursor class | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Stefan Csomor | |
65571936 | 9 | // Licence: wxWindows licence |
2646f485 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "cursor.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/defs.h" | |
17 | ||
18 | #include "wx/app.h" | |
19 | #include "wx/cursor.h" | |
20 | #include "wx/icon.h" | |
21 | #include "wx/image.h" | |
22 | #include "wx/xpmdecod.h" | |
23 | ||
24 | #include "wx/mac/private.h" | |
25 | ||
26 | #if !USE_SHARED_LIBRARIES | |
27 | IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxBitmap) | |
28 | #endif | |
29 | ||
30 | const short kwxCursorBullseye = 10 ; | |
31 | const short kwxCursorBlank = 11 ; | |
32 | const short kwxCursorPencil = 12 ; | |
33 | const short kwxCursorMagnifier = 13 ; | |
34 | const short kwxCursorNoEntry = 14 ; | |
35 | const short kwxCursorPaintBrush = 15 ; | |
36 | const short kwxCursorPointRight = 16 ; | |
37 | const short kwxCursorPointLeft = 17 ; | |
38 | const short kwxCursorQuestionArrow = 18 ; | |
39 | const short kwxCursorRightArrow = 19 ; | |
40 | const short kwxCursorSizeNS = 20 ; | |
41 | const short kwxCursorSize = 21 ; | |
42 | const short kwxCursorSizeNESW = 22 ; | |
43 | const short kwxCursorSizeNWSE = 23 ; | |
44 | const short kwxCursorRoller = 24 ; | |
45 | ||
46 | wxCursor gMacCurrentCursor ; | |
47 | ||
48 | wxCursorRefData::wxCursorRefData() | |
49 | { | |
50 | m_width = 16; | |
51 | m_height = 16; | |
52 | m_hCursor = NULL ; | |
53 | m_disposeHandle = false ; | |
54 | m_releaseHandle = false ; | |
55 | m_isColorCursor = false ; | |
56 | m_themeCursor = -1 ; | |
57 | } | |
58 | ||
59 | wxCursorRefData::~wxCursorRefData() | |
60 | { | |
61 | if ( m_isColorCursor ) | |
62 | { | |
63 | ::DisposeCCursor( (CCrsrHandle) m_hCursor ) ; | |
64 | } | |
65 | else if ( m_disposeHandle ) | |
66 | { | |
67 | ::DisposeHandle( (Handle ) m_hCursor ) ; | |
68 | } | |
69 | else if ( m_releaseHandle ) | |
70 | { | |
71 | // we don't release the resource since it may already | |
72 | // be in use again | |
73 | } | |
74 | } | |
75 | ||
76 | // Cursors | |
77 | wxCursor::wxCursor() | |
78 | { | |
79 | } | |
80 | ||
81 | wxCursor::wxCursor(const char WXUNUSED(bits)[], int WXUNUSED(width), int WXUNUSED(height), | |
82 | int WXUNUSED(hotSpotX), int WXUNUSED(hotSpotY), const char WXUNUSED(maskBits)[]) | |
83 | { | |
84 | } | |
85 | ||
86 | wxCursor::wxCursor( const wxImage &image ) | |
87 | { | |
88 | CreateFromImage( image ) ; | |
89 | } | |
90 | ||
91 | wxCursor::wxCursor(const char **bits) | |
92 | { | |
93 | (void) CreateFromXpm(bits); | |
94 | } | |
95 | ||
96 | wxCursor::wxCursor(char **bits) | |
97 | { | |
98 | (void) CreateFromXpm((const char **)bits); | |
99 | } | |
100 | ||
101 | bool wxCursor::CreateFromXpm(const char **bits) | |
102 | { | |
103 | wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid cursor data") ) | |
104 | wxXPMDecoder decoder; | |
105 | wxImage img = decoder.ReadData(bits); | |
106 | wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid cursor data") ) | |
107 | CreateFromImage( img ) ; | |
108 | return TRUE; | |
109 | } | |
110 | ||
111 | short GetCTabIndex( CTabHandle colors , RGBColor *col ) | |
112 | { | |
113 | short retval = 0 ; | |
114 | unsigned long bestdiff = 0xFFFF ; | |
115 | for ( int i = 0 ; i < (**colors).ctSize ; ++i ) | |
116 | { | |
117 | unsigned long diff = abs(col->red - (**colors).ctTable[i].rgb.red ) + | |
118 | abs(col->green - (**colors).ctTable[i].rgb.green ) + | |
119 | abs(col->blue - (**colors).ctTable[i].rgb.blue ) ; | |
120 | if ( diff < bestdiff ) | |
121 | { | |
122 | bestdiff = diff ; | |
123 | retval = (**colors).ctTable[i].value ; | |
124 | } | |
125 | } | |
126 | return retval ; | |
127 | } | |
128 | ||
129 | void wxCursor::CreateFromImage(const wxImage & image) | |
130 | { | |
131 | m_refData = new wxCursorRefData; | |
132 | ||
b737ad10 RR |
133 | int w = 16; |
134 | int h = 16; | |
2646f485 | 135 | |
b737ad10 RR |
136 | int hotSpotX = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X); |
137 | int hotSpotY = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y); | |
138 | int image_w = image.GetWidth(); | |
139 | int image_h = image.GetHeight(); | |
2646f485 | 140 | |
b737ad10 RR |
141 | wxASSERT_MSG( hotSpotX >= 0 && hotSpotX < image_w && |
142 | hotSpotY >= 0 && hotSpotY < image_h, | |
143 | _T("invalid cursor hot spot coordinates") ); | |
144 | ||
145 | wxImage image16(image); // final image of correct size | |
146 | ||
147 | // if image is too small then place it in the center, resize it if too big | |
148 | if ((w > image_w) && (h > image_h)) | |
149 | { | |
150 | wxPoint offset((w - image_w)/2, (h - image_h)/2); | |
151 | hotSpotX = hotSpotX + offset.x; | |
152 | hotSpotY = hotSpotY + offset.y; | |
153 | ||
154 | image16 = image.Size(wxSize(w, h), offset); | |
155 | } | |
156 | else if ((w != image_w) || (h != image_h)) | |
157 | { | |
158 | hotSpotX = int(hotSpotX * double(w) / double(image_w)); | |
159 | hotSpotY = int(hotSpotY * double(h) / double(image_h)); | |
2646f485 | 160 | |
b737ad10 RR |
161 | image16 = image.Scale(w, h); |
162 | } | |
163 | ||
164 | unsigned char * rgbBits = image16.GetData(); | |
165 | bool bHasMask = image16.HasMask() ; | |
2646f485 SC |
166 | |
167 | #if 0 | |
168 | // monochrome implementation | |
169 | M_CURSORDATA->m_hCursor = NewHandle( sizeof( Cursor ) ) ; | |
170 | M_CURSORDATA->m_disposeHandle = true ; | |
171 | HLock( (Handle) M_CURSORDATA->m_hCursor ) ; | |
172 | CursPtr cp = *(CursHandle)M_CURSORDATA->m_hCursor ; | |
173 | memset( cp->data , 0 , sizeof( Bits16 ) ) ; | |
174 | memset( cp->mask , 0 , sizeof( Bits16 ) ) ; | |
175 | ||
176 | unsigned char mr = image16.GetMaskRed() ; | |
177 | unsigned char mg = image16.GetMaskGreen() ; | |
178 | unsigned char mb = image16.GetMaskBlue() ; | |
179 | for ( int y = 0 ; y < h ; ++y ) | |
180 | { | |
181 | short rowbits = 0 ; | |
182 | short maskbits = 0 ; | |
183 | ||
184 | for ( int x = 0 ; x < w ; ++x ) | |
185 | { | |
186 | long pos = (y * w + x) * 3; | |
187 | ||
188 | unsigned char r = rgbBits[pos] ; | |
189 | unsigned char g = rgbBits[pos+1] ; | |
190 | unsigned char b = rgbBits[pos+2] ; | |
191 | if ( bHasMask && r==mr && g==mg && b==mb ) | |
192 | { | |
193 | // masked area, does not appear anywhere | |
194 | } | |
195 | else | |
196 | { | |
197 | if ( (int)r + (int)g + (int)b < 0x0200 ) | |
198 | { | |
199 | rowbits |= ( 1 << (15-x) ) ; | |
200 | } | |
201 | maskbits |= ( 1 << (15-x) ) ; | |
202 | } | |
203 | } | |
204 | cp->data[y] = rowbits ; | |
205 | cp->mask[y] = maskbits ; | |
206 | } | |
207 | if ( !bHasMask ) | |
208 | { | |
209 | memcpy( cp->mask , cp->data , sizeof( Bits16) ) ; | |
210 | } | |
211 | cp->hotSpot.h = hotSpotX ; | |
212 | cp->hotSpot.v = hotSpotY ; | |
213 | HUnlock( (Handle) M_CURSORDATA->m_hCursor ) ; | |
214 | #else | |
215 | PixMapHandle pm = (PixMapHandle) NewHandleClear( sizeof (PixMap)) ; | |
216 | short extent = 16 ; | |
217 | short bytesPerPixel = 1 ; | |
218 | short depth = 8 ; | |
219 | Rect bounds = { 0 , 0 , extent , extent } ; | |
220 | CCrsrHandle ch = (CCrsrHandle) NewHandleClear ( sizeof( CCrsr ) ) ; | |
221 | CTabHandle newColors = GetCTable( 8 ) ; | |
222 | HandToHand((Handle *) &newColors); | |
223 | // set the values to the indices | |
224 | for ( int i = 0 ; i < (**newColors).ctSize ; ++i ) | |
225 | { | |
226 | (**newColors).ctTable[i].value = i ; | |
227 | } | |
228 | HLock( (Handle) ch) ; | |
229 | (**ch).crsrType = 0x8001 ; // color cursors | |
230 | (**ch).crsrMap = pm ; | |
231 | short bytesPerRow = bytesPerPixel * extent ; | |
232 | ||
233 | (**pm).baseAddr = 0; | |
234 | (**pm).rowBytes = bytesPerRow | 0x8000; | |
235 | (**pm).bounds = bounds; | |
236 | (**pm).pmVersion = 0; | |
237 | (**pm).packType = 0; | |
238 | (**pm).packSize = 0; | |
239 | (**pm).hRes = 0x00480000; /* 72 DPI default res */ | |
240 | (**pm).vRes = 0x00480000; /* 72 DPI default res */ | |
241 | (**pm).pixelSize = depth; | |
242 | (**pm).pixelType = 0; | |
243 | (**pm).cmpCount = 1; | |
244 | (**pm).cmpSize = depth; | |
245 | (**pm).pmTable = newColors; | |
246 | ||
247 | (**ch).crsrData = NewHandleClear( extent * bytesPerRow ) ; | |
248 | (**ch).crsrXData = NULL ; | |
249 | (**ch).crsrXValid = 0; | |
250 | (**ch).crsrXHandle = NULL; | |
251 | ||
252 | (**ch).crsrHotSpot.h = hotSpotX ; | |
253 | (**ch).crsrHotSpot.v = hotSpotY ; | |
254 | (**ch).crsrXTable = NULL ; | |
255 | (**ch).crsrID = GetCTSeed() ; | |
256 | ||
257 | memset( (**ch).crsr1Data , 0 , sizeof( Bits16 ) ) ; | |
258 | memset( (**ch).crsrMask , 0 , sizeof( Bits16 ) ) ; | |
259 | ||
260 | unsigned char mr = image16.GetMaskRed() ; | |
261 | unsigned char mg = image16.GetMaskGreen() ; | |
262 | unsigned char mb = image16.GetMaskBlue() ; | |
263 | for ( int y = 0 ; y < h ; ++y ) | |
264 | { | |
265 | short rowbits = 0 ; | |
266 | short maskbits = 0 ; | |
267 | ||
268 | for ( int x = 0 ; x < w ; ++x ) | |
269 | { | |
270 | long pos = (y * w + x) * 3; | |
271 | ||
272 | unsigned char r = rgbBits[pos] ; | |
273 | unsigned char g = rgbBits[pos+1] ; | |
274 | unsigned char b = rgbBits[pos+2] ; | |
275 | RGBColor col = { 0xFFFF ,0xFFFF, 0xFFFF } ; | |
276 | ||
277 | if ( bHasMask && r==mr && g==mg && b==mb ) | |
278 | { | |
279 | // masked area, does not appear anywhere | |
280 | } | |
281 | else | |
282 | { | |
283 | if ( (int)r + (int)g + (int)b < 0x0200 ) | |
284 | { | |
285 | rowbits |= ( 1 << (15-x) ) ; | |
286 | } | |
287 | maskbits |= ( 1 << (15-x) ) ; | |
288 | ||
289 | col = *((RGBColor*) wxColor( r , g , b ).GetPixel()) ; | |
290 | } | |
291 | *((*(**ch).crsrData) + y * bytesPerRow + x) = | |
292 | GetCTabIndex( newColors , &col) ; | |
293 | } | |
294 | (**ch).crsr1Data[y] = rowbits ; | |
295 | (**ch).crsrMask[y] = maskbits ; | |
296 | } | |
297 | if ( !bHasMask ) | |
298 | { | |
299 | memcpy( (**ch).crsrMask , (**ch).crsr1Data , sizeof( Bits16) ) ; | |
300 | } | |
301 | ||
302 | HUnlock((Handle) ch) ; | |
303 | M_CURSORDATA->m_hCursor = ch ; | |
304 | M_CURSORDATA->m_isColorCursor = true ; | |
305 | #endif | |
306 | } | |
307 | ||
308 | wxCursor::wxCursor(const wxString& cursor_file, long flags, int hotSpotX, int hotSpotY) | |
309 | { | |
310 | m_refData = new wxCursorRefData; | |
311 | if ( flags == wxBITMAP_TYPE_MACCURSOR_RESOURCE ) | |
312 | { | |
313 | Str255 theName ; | |
314 | wxMacStringToPascal( cursor_file , theName ) ; | |
315 | ||
316 | wxStAppResource resload ; | |
317 | Handle resHandle = ::GetNamedResource( 'crsr' , theName ) ; | |
318 | if ( resHandle ) | |
319 | { | |
320 | short theId = -1 ; | |
321 | OSType theType ; | |
322 | GetResInfo( resHandle , &theId , &theType , theName ) ; | |
323 | ReleaseResource( resHandle ) ; | |
324 | M_CURSORDATA->m_hCursor = GetCCursor( theId ) ; | |
325 | if ( M_CURSORDATA->m_hCursor ) | |
326 | M_CURSORDATA->m_isColorCursor = true ; | |
327 | } | |
328 | else | |
329 | { | |
330 | Handle resHandle = ::GetNamedResource( 'CURS' , theName ) ; | |
331 | if ( resHandle ) | |
332 | { | |
333 | short theId = -1 ; | |
334 | OSType theType ; | |
335 | GetResInfo( resHandle , &theId , &theType , theName ) ; | |
336 | ReleaseResource( resHandle ) ; | |
337 | M_CURSORDATA->m_hCursor = GetCursor( theId ) ; | |
338 | if ( M_CURSORDATA->m_hCursor ) | |
339 | M_CURSORDATA->m_releaseHandle = true ; | |
340 | } | |
341 | } | |
342 | } | |
343 | else | |
344 | { | |
345 | wxImage image ; | |
346 | image.LoadFile( cursor_file , flags ) ; | |
347 | if( image.Ok() ) | |
348 | { | |
349 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X,hotSpotX ) ; | |
350 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y,hotSpotY ) ; | |
351 | delete m_refData ; | |
352 | CreateFromImage(image) ; | |
353 | } | |
354 | } | |
355 | } | |
356 | ||
357 | // Cursors by stock number | |
358 | wxCursor::wxCursor(int cursor_type) | |
359 | { | |
360 | m_refData = new wxCursorRefData; | |
361 | ||
362 | switch (cursor_type) | |
363 | { | |
364 | case wxCURSOR_COPY_ARROW: | |
365 | M_CURSORDATA->m_themeCursor = kThemeCopyArrowCursor ; | |
366 | break; | |
367 | case wxCURSOR_WAIT: | |
368 | M_CURSORDATA->m_themeCursor = kThemeWatchCursor ; | |
369 | break; | |
370 | case wxCURSOR_IBEAM: | |
371 | M_CURSORDATA->m_themeCursor = kThemeIBeamCursor ; | |
372 | break; | |
373 | case wxCURSOR_CROSS: | |
374 | M_CURSORDATA->m_themeCursor = kThemeCrossCursor; | |
375 | break; | |
376 | case wxCURSOR_SIZENWSE: | |
377 | { | |
378 | wxStAppResource resload ; | |
379 | M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorSizeNWSE); | |
380 | } | |
381 | break; | |
382 | case wxCURSOR_SIZENESW: | |
383 | { | |
384 | wxStAppResource resload ; | |
385 | M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorSizeNESW); | |
386 | } | |
387 | break; | |
388 | case wxCURSOR_SIZEWE: | |
389 | { | |
390 | M_CURSORDATA->m_themeCursor = kThemeResizeLeftRightCursor; | |
391 | } | |
392 | break; | |
393 | case wxCURSOR_SIZENS: | |
394 | { | |
395 | wxStAppResource resload ; | |
396 | M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorSizeNS); | |
397 | } | |
398 | break; | |
399 | case wxCURSOR_SIZING: | |
400 | { | |
401 | wxStAppResource resload ; | |
402 | M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorSize); | |
403 | } | |
404 | break; | |
405 | case wxCURSOR_HAND: | |
406 | { | |
407 | M_CURSORDATA->m_themeCursor = kThemePointingHandCursor; | |
408 | } | |
409 | break; | |
410 | case wxCURSOR_BULLSEYE: | |
411 | { | |
412 | wxStAppResource resload ; | |
413 | M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorBullseye); | |
414 | } | |
415 | break; | |
416 | case wxCURSOR_PENCIL: | |
417 | { | |
418 | wxStAppResource resload ; | |
419 | M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorPencil); | |
420 | } | |
421 | break; | |
422 | case wxCURSOR_MAGNIFIER: | |
423 | { | |
424 | wxStAppResource resload ; | |
425 | M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorMagnifier); | |
426 | } | |
427 | break; | |
428 | case wxCURSOR_NO_ENTRY: | |
429 | { | |
430 | wxStAppResource resload ; | |
431 | M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorNoEntry); | |
432 | } | |
433 | break; | |
434 | case wxCURSOR_WATCH: | |
435 | { | |
436 | M_CURSORDATA->m_themeCursor = kThemeWatchCursor; | |
437 | break; | |
438 | } | |
439 | case wxCURSOR_PAINT_BRUSH: | |
440 | { | |
441 | wxStAppResource resload ; | |
442 | M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorPaintBrush); | |
443 | break; | |
444 | } | |
445 | case wxCURSOR_POINT_LEFT: | |
446 | { | |
447 | wxStAppResource resload ; | |
448 | M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorPointLeft); | |
449 | break; | |
450 | } | |
451 | case wxCURSOR_POINT_RIGHT: | |
452 | { | |
453 | wxStAppResource resload ; | |
454 | M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorPointRight); | |
455 | break; | |
456 | } | |
457 | case wxCURSOR_QUESTION_ARROW: | |
458 | { | |
459 | wxStAppResource resload ; | |
460 | M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorQuestionArrow); | |
461 | break; | |
462 | } | |
463 | case wxCURSOR_BLANK: | |
464 | { | |
465 | wxStAppResource resload ; | |
466 | M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorBlank); | |
467 | break; | |
468 | } | |
469 | case wxCURSOR_RIGHT_ARROW: | |
470 | { | |
471 | wxStAppResource resload ; | |
472 | M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorRightArrow); | |
473 | break; | |
474 | } | |
475 | case wxCURSOR_SPRAYCAN: | |
476 | { | |
477 | wxStAppResource resload ; | |
478 | M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorRoller); | |
479 | break; | |
480 | } | |
481 | case wxCURSOR_CHAR: | |
482 | case wxCURSOR_ARROW: | |
483 | case wxCURSOR_LEFT_BUTTON: | |
484 | case wxCURSOR_RIGHT_BUTTON: | |
485 | case wxCURSOR_MIDDLE_BUTTON: | |
486 | default: | |
487 | M_CURSORDATA->m_themeCursor = kThemeArrowCursor ; | |
488 | break; | |
489 | } | |
490 | if ( M_CURSORDATA->m_themeCursor == -1 ) | |
491 | M_CURSORDATA->m_releaseHandle = true ; | |
492 | } | |
493 | ||
494 | void wxCursor::MacInstall() const | |
495 | { | |
496 | gMacCurrentCursor = *this ; | |
497 | if ( m_refData && M_CURSORDATA->m_themeCursor != -1 ) | |
498 | { | |
499 | SetThemeCursor( M_CURSORDATA->m_themeCursor ) ; | |
500 | } | |
501 | else if ( m_refData && M_CURSORDATA->m_hCursor ) | |
502 | { | |
503 | if ( M_CURSORDATA->m_isColorCursor ) | |
504 | ::SetCCursor( (CCrsrHandle) M_CURSORDATA->m_hCursor ) ; | |
505 | else | |
506 | ::SetCursor( * (CursHandle) M_CURSORDATA->m_hCursor ) ; | |
507 | } | |
508 | else | |
509 | { | |
510 | SetThemeCursor( kThemeArrowCursor ) ; | |
511 | } | |
512 | } | |
513 | ||
514 | wxCursor::~wxCursor() | |
515 | { | |
516 | } | |
517 | ||
518 | // Global cursor setting | |
519 | void wxSetCursor(const wxCursor& cursor) | |
520 | { | |
521 | cursor.MacInstall() ; | |
522 | } | |
523 | ||
524 |