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