]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/cursor.mm
Rewrite conversion from classic Mac OS 'CURS'-style structures to NSCursor.
[wxWidgets.git] / src / cocoa / cursor.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/cursor.mm
3 // Purpose: wxCursor class for wxCocoa
4 // Author: Ryan Norton
5 // David Elliott
6 // Modified by:
7 // Created: 2004-10-05
8 // RCS-ID: $Id$
9 // Copyright: (c) Ryan Norton
10 // 2007, Software 2000 Ltd.
11 // Licence: wxWidgets licence
12 /////////////////////////////////////////////////////////////////////////////
13
14 #include "wx/wxprec.h"
15
16 #include "wx/cursor.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/icon.h"
20 #endif //WX_PRECOMP
21
22 #import <AppKit/NSCursor.h>
23 #import <AppKit/NSImage.h>
24 #include "wx/cocoa/string.h"
25
26 IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxBitmap)
27
28 typedef struct tagClassicCursor
29 {
30 wxUint16 bits[16];
31 wxUint16 mask[16];
32 wxInt16 hotspot[2];
33 }ClassicCursor;
34
35 ///////////////////////////////////////////////////////////////////////////
36 // This is a direct copy from src/mac/carbon/cursor.cpp and should be
37 // changed to use common code if we plan on keeping it this way.
38 // Note that this is basically an array of classic 'CURS' resources.
39
40 const short kwxCursorBullseye = 0 ;
41 const short kwxCursorBlank = 1 ;
42 const short kwxCursorPencil = 2 ;
43 const short kwxCursorMagnifier = 3 ;
44 const short kwxCursorNoEntry = 4 ;
45 const short kwxCursorPaintBrush = 5 ;
46 const short kwxCursorPointRight = 6 ;
47 const short kwxCursorPointLeft = 7 ;
48 const short kwxCursorQuestionArrow = 8 ;
49 const short kwxCursorRightArrow = 9 ;
50 const short kwxCursorSizeNS = 10 ;
51 const short kwxCursorSize = 11 ;
52 const short kwxCursorSizeNESW = 12 ;
53 const short kwxCursorSizeNWSE = 13 ;
54 const short kwxCursorRoller = 14 ;
55 const short kwxCursorLast = kwxCursorRoller ;
56
57 ClassicCursor gMacCursors[kwxCursorLast+1] =
58 {
59
60 {
61 {0x0000, 0x03E0, 0x0630, 0x0808, 0x1004, 0x31C6, 0x2362, 0x2222,
62 0x2362, 0x31C6, 0x1004, 0x0808, 0x0630, 0x03E0, 0x0000, 0x0000},
63 {0x0000, 0x03E0, 0x07F0, 0x0FF8, 0x1FFC, 0x3FFE, 0x3FFE, 0x3FFE,
64 0x3FFE, 0x3FFE, 0x1FFC, 0x0FF8, 0x07F0, 0x03E0, 0x0000, 0x0000},
65 {0x0007, 0x0008}
66 },
67
68 {
69 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
70 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
71 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
72 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
73 {0x0000, 0x0000}
74 },
75
76 {
77 {0x00F0, 0x0088, 0x0108, 0x0190, 0x0270, 0x0220, 0x0440, 0x0440,
78 0x0880, 0x0880, 0x1100, 0x1E00, 0x1C00, 0x1800, 0x1000, 0x0000},
79 {0x00F0, 0x00F8, 0x01F8, 0x01F0, 0x03F0, 0x03E0, 0x07C0, 0x07C0,
80 0x0F80, 0x0F80, 0x1F00, 0x1E00, 0x1C00, 0x1800, 0x1000, 0x0000},
81 {0x000E, 0x0003}
82 },
83
84 {
85 {0x0000, 0x1E00, 0x2100, 0x4080, 0x4080, 0x4080, 0x4080, 0x2180,
86 0x1FC0, 0x00E0, 0x0070, 0x0038, 0x001C, 0x000E, 0x0006, 0x0000},
87 {0x3F00, 0x7F80, 0xFFC0, 0xFFC0, 0xFFC0, 0xFFC0, 0xFFC0, 0x7FC0,
88 0x3FE0, 0x1FF0, 0x00F8, 0x007C, 0x003E, 0x001F, 0x000F, 0x0007},
89 {0x0004, 0x0004}
90 },
91
92 {
93 {0x0000, 0x07E0, 0x1FF0, 0x3838, 0x3C0C, 0x6E0E, 0x6706, 0x6386,
94 0x61C6, 0x60E6, 0x7076, 0x303C, 0x1C1C, 0x0FF8, 0x07E0, 0x0000},
95 {0x0540, 0x0FF0, 0x3FF8, 0x3C3C, 0x7E0E, 0xFF0F, 0x6F86, 0xE7C7,
96 0x63E6, 0xE1F7, 0x70FE, 0x707E, 0x3C3C, 0x1FFC, 0x0FF0, 0x0540},
97 {0x0007, 0x0007}
98 },
99
100 {
101 {0x0000, 0x0380, 0x0380, 0x0380, 0x0380, 0x0380, 0x0380, 0x0FE0,
102 0x1FF0, 0x1FF0, 0x0000, 0x1FF0, 0x1FF0, 0x1550, 0x1550, 0x1550},
103 {0x07C0, 0x07C0, 0x07C0, 0x07C0, 0x07C0, 0x07C0, 0x0FE0, 0x1FF0,
104 0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8},
105 {0x000B, 0x0007}
106 },
107
108 {
109 {0x00C0, 0x0140, 0x0640, 0x08C0, 0x3180, 0x47FE, 0x8001, 0x8001,
110 0x81FE, 0x8040, 0x01C0, 0x0040, 0x03C0, 0xC080, 0x3F80, 0x0000},
111 {0x00C0, 0x01C0, 0x07C0, 0x0FC0, 0x3F80, 0x7FFE, 0xFFFF, 0xFFFF,
112 0xFFFE, 0xFFC0, 0xFFC0, 0xFFC0, 0xFFC0, 0xFF80, 0x3F80, 0x0000},
113 {0x0006, 0x000F}
114 },
115
116 {
117 {0x0100, 0x0280, 0x0260, 0x0310, 0x018C, 0x7FE3, 0x8000, 0x8000,
118 0x7F80, 0x0200, 0x0380, 0x0200, 0x03C0, 0x0107, 0x01F8, 0x0000},
119 {0x0100, 0x0380, 0x03E0, 0x03F0, 0x01FC, 0x7FFF, 0xFFFF, 0xFFFF,
120 0xFFFF, 0x03FF, 0x03FF, 0x03FF, 0x03FF, 0x01FF, 0x01F8, 0x0000},
121 {0x0006, 0x0000}
122 },
123
124 {
125 {0x0000, 0x4078, 0x60FC, 0x71CE, 0x7986, 0x7C06, 0x7E0E, 0x7F1C,
126 0x7FB8, 0x7C30, 0x6C30, 0x4600, 0x0630, 0x0330, 0x0300, 0x0000},
127 {0xC078, 0xE0FC, 0xF1FE, 0xFBFF, 0xFFCF, 0xFF8F, 0xFF1F, 0xFFBE,
128 0xFFFC, 0xFE78, 0xFF78, 0xEFF8, 0xCFF8, 0x87F8, 0x07F8, 0x0300},
129 {0x0001, 0x0001}
130 },
131
132 {
133 {0x0000, 0x0002, 0x0006, 0x000E, 0x001E, 0x003E, 0x007E, 0x00FE,
134 0x01FE, 0x003E, 0x0036, 0x0062, 0x0060, 0x00C0, 0x00C0, 0x0000},
135 {0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF,
136 0x03FF, 0x07FF, 0x007F, 0x00F7, 0x00F3, 0x01E1, 0x01E0, 0x01C0},
137 {0x0001, 0x000E}
138 },
139
140 {
141 {0x0000, 0x0080, 0x01C0, 0x03E0, 0x0080, 0x0080, 0x0080, 0x1FFC,
142 0x1FFC, 0x0080, 0x0080, 0x0080, 0x03E0, 0x01C0, 0x0080, 0x0000},
143 {0x0080, 0x01C0, 0x03E0, 0x07F0, 0x0FF8, 0x01C0, 0x3FFE, 0x3FFE,
144 0x3FFE, 0x3FFE, 0x01C0, 0x0FF8, 0x07F0, 0x03E0, 0x01C0, 0x0080},
145 {0x0007, 0x0008}
146 },
147
148 {
149 {0x0000, 0x0080, 0x01C0, 0x03E0, 0x0080, 0x0888, 0x188C, 0x3FFE,
150 0x188C, 0x0888, 0x0080, 0x03E0, 0x01C0, 0x0080, 0x0000, 0x0000},
151 {0x0080, 0x01C0, 0x03E0, 0x07F0, 0x0BE8, 0x1DDC, 0x3FFE, 0x7FFF,
152 0x3FFE, 0x1DDC, 0x0BE8, 0x07F0, 0x03E0, 0x01C0, 0x0080, 0x0000},
153 {0x0007, 0x0008}
154 },
155
156 {
157 {0x0000, 0x001E, 0x000E, 0x060E, 0x0712, 0x03A0, 0x01C0, 0x00E0,
158 0x0170, 0x1238, 0x1C18, 0x1C00, 0x1E00, 0x0000, 0x0000, 0x0000},
159 {0x007F, 0x003F, 0x0E1F, 0x0F0F, 0x0F97, 0x07E3, 0x03E1, 0x21F0,
160 0x31F8, 0x3A7C, 0x3C3C, 0x3E1C, 0x3F00, 0x3F80, 0x0000, 0x0000},
161 {0x0006, 0x0009}
162 },
163
164 {
165 {0x0000, 0x7800, 0x7000, 0x7060, 0x48E0, 0x05C0, 0x0380, 0x0700,
166 0x0E80, 0x1C48, 0x1838, 0x0038, 0x0078, 0x0000, 0x0000, 0x0000},
167 {0xFE00, 0xFC00, 0xF870, 0xF0F0, 0xE9F0, 0xC7E0, 0x87C0, 0x0F84,
168 0x1F8C, 0x3E5C, 0x3C3C, 0x387C, 0x00FC, 0x01FC, 0x0000, 0x0000},
169 {0x0006, 0x0006}
170 },
171
172 {
173 {0x0006, 0x000E, 0x001C, 0x0018, 0x0020, 0x0040, 0x00F8, 0x0004,
174 0x1FF4, 0x200C, 0x2AA8, 0x1FF0, 0x1F80, 0x3800, 0x6000, 0x8000},
175 {0x000F, 0x001F, 0x003E, 0x007C, 0x0070, 0x00E0, 0x01FC, 0x3FF6,
176 0x7FF6, 0x7FFE, 0x7FFC, 0x7FF8, 0x3FF0, 0x7FC0, 0xF800, 0xE000},
177 {0x000A, 0x0006}
178 },
179
180 } ;
181
182 // End of data copied from src/mac/carbon/cursor.cpp
183 ///////////////////////////////////////////////////////////////////////////
184
185 /* wxStockCursor
186 * Returns a newly allocated (i.e. retainCount == 1) NSCursor based on the
187 * classic Mac OS cursor data in this source file. This allows us to
188 * implement the "stock" wxWidgets cursors which aren't present in Cocoa.
189 * FIXME: This function should be static.
190 * FIXME: This function should use "Alloc" in preference to "Get" to follow
191 * the proper Cocoa naming conventions.
192 */
193 NSCursor* wxGetStockCursor( short sIndex )
194 {
195 ClassicCursor* pCursor = &gMacCursors[sIndex];
196
197 //Classic mac cursors are 1bps 16x16 black and white with a
198 //identical mask that is 1 for on and 0 for off
199 NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0,16.0)];
200
201 NSBitmapImageRep *theRep = [[NSBitmapImageRep alloc]
202 initWithBitmapDataPlanes: NULL // Tell Cocoa to allocate the planes for us.
203 pixelsWide: 16 // All classic cursors are 16x16
204 pixelsHigh: 16
205 bitsPerSample: 1 // All classic cursors are bitmaps with bitmasks
206 samplesPerPixel: 2 // Sample 0:image 1:mask
207 hasAlpha: YES // Identify last sample as a mask
208 isPlanar: YES // Use a separate array for each sample
209 colorSpaceName: NSCalibratedWhiteColorSpace // 0.0=black 1.0=white
210 bytesPerRow: 2 // Rows in each plane are on 2-byte boundaries (no pad)
211 bitsPerPixel: 1]; // same as bitsPerSample since data is planar
212 // XXX: Should we use NSDeviceWhiteColorSpace? Does it matter?
213
214 // Ensure that Cocoa allocated 2 and only 2 of the 5 possible planes
215 unsigned char *planes[5];
216 [theRep getBitmapDataPlanes:planes];
217 wxASSERT(planes[0] != NULL);
218 wxASSERT(planes[1] != NULL);
219 wxASSERT(planes[2] == NULL);
220 wxASSERT(planes[3] == NULL);
221 wxASSERT(planes[4] == NULL);
222
223 // NOTE1: The Cursor's bits field is white=0 black=1.. thus the bitwise-not
224 // Why not use NSCalibratedBlackColorSpace? Because that reverses the
225 // sense of the alpha (mask) plane.
226 // NOTE2: The mask data is 0=off 1=on
227 // NOTE3: Cocoa asks for "premultiplied" color planes. Since we have a
228 // 1-bit color plane and a 1-bit alpha plane we can just do a bitwise-and
229 // on the two. The original cursor bitmaps have 0 (white actually) for
230 // any masked-off pixels. Therefore every masked-off pixel would be wrong
231 // since we bit-flip all of the picture bits. In practice, Cocoa doesn't
232 // seem to care, but we are following the documentation.
233
234 // Fill in the color (black/white) plane
235 for(int i=0; i<16; ++i)
236 {
237 planes[0][2*i ] = (~pCursor->bits[i] & pCursor->mask[i]) >> 8 & 0xff;
238 planes[0][2*i+1] = (~pCursor->bits[i] & pCursor->mask[i]) & 0xff;
239 }
240 // Fill in the alpha (i.e. mask) plane
241 for(int i=0; i<16; ++i)
242 {
243 planes[1][2*i ] = pCursor->mask[i] >> 8 & 0xff;
244 planes[1][2*i+1] = pCursor->mask[i] & 0xff;
245 }
246
247 //add the representation (data) to the image
248 [theImage addRepresentation:theRep];
249
250 //create the new cursor
251 NSCursor* theCursor = [[NSCursor alloc] initWithImage:theImage
252 hotSpot:NSMakePoint(pCursor->hotspot[1], pCursor->hotspot[0])
253 ];
254
255 //do the usual cleanups
256 [theRep release];
257 [theImage release];
258
259 //return the new cursor
260 return theCursor;
261 }
262
263 wxCursorRefData::wxCursorRefData() :
264 m_width(32), m_height(32), m_hCursor(nil)
265 {
266 }
267
268 wxCursorRefData::~wxCursorRefData()
269 {
270 if (m_hCursor)
271 [m_hCursor release];
272 }
273
274 // Cursors
275 wxCursor::wxCursor()
276 {
277 }
278
279 wxCursor::wxCursor(const char WXUNUSED(bits)[], int WXUNUSED(width), int WXUNUSED(height),
280 int WXUNUSED(hotSpotX), int WXUNUSED(hotSpotY), const char WXUNUSED(maskBits)[])
281 {
282 }
283
284 wxCursor::wxCursor(const wxString& cursor_file, long flags, int hotSpotX, int hotSpotY)
285 {
286 m_refData = new wxCursorRefData;
287
288 //TODO: Not sure if this works or not
289 NSImage* theImage;
290
291 if (flags & wxBITMAP_TYPE_MACCURSOR_RESOURCE)
292 {
293 //[NSBundle bundleForClass:[self class]]?
294 theImage = [[NSImage alloc]
295 initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:wxNSStringWithWxString(cursor_file) ofType:nil]
296 ];
297
298 }
299 else
300 theImage = [[NSImage alloc] initByReferencingFile:wxNSStringWithWxString(cursor_file)
301 ];
302
303 wxASSERT(theImage);
304
305 M_CURSORDATA->m_hCursor = [[NSCursor alloc] initWithImage:theImage
306 hotSpot:NSMakePoint(hotSpotX, hotSpotY)
307 ];
308
309 [theImage release];
310 }
311
312 // Cursors by stock number
313 wxCursor::wxCursor(int cursor_type)
314 {
315 m_refData = new wxCursorRefData;
316
317 switch (cursor_type)
318 {
319 case wxCURSOR_IBEAM:
320 M_CURSORDATA->m_hCursor = [[NSCursor IBeamCursor] retain];
321 break;
322 case wxCURSOR_ARROW:
323 M_CURSORDATA->m_hCursor = [[NSCursor arrowCursor] retain];
324 break;
325 /* TODO:
326 case wxCURSOR_COPY_ARROW:
327 M_CURSORDATA->m_themeCursor = kThemeCopyArrowCursor ;
328 break;
329 case wxCURSOR_WAIT:
330 M_CURSORDATA->m_themeCursor = kThemeWatchCursor ;
331 break;
332 case wxCURSOR_CROSS:
333 M_CURSORDATA->m_themeCursor = kThemeCrossCursor;
334 break;
335 case wxCURSOR_SIZENWSE:
336 {
337 M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorSizeNWSE);
338 }
339 break;
340 */
341 case wxCURSOR_SIZENESW:
342 {
343 M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorSizeNESW);
344 }
345 break;
346 /* TODO:
347 case wxCURSOR_SIZEWE:
348 {
349 M_CURSORDATA->m_themeCursor = kThemeResizeLeftRightCursor;
350 }
351 break;
352 */
353 case wxCURSOR_SIZENS:
354 {
355 M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorSizeNS);
356 }
357 break;
358 case wxCURSOR_SIZING:
359 {
360 M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorSize);
361 }
362 break;
363 /* TODO:
364 case wxCURSOR_HAND:
365 {
366 M_CURSORDATA->m_themeCursor = kThemePointingHandCursor;
367 }
368 break;
369 */
370 case wxCURSOR_BULLSEYE:
371 {
372 M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorBullseye);
373 }
374 break;
375 case wxCURSOR_PENCIL:
376 {
377 M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorPencil);
378 }
379 break;
380 case wxCURSOR_MAGNIFIER:
381 {
382 M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorMagnifier);
383 }
384 break;
385 case wxCURSOR_NO_ENTRY:
386 {
387 M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorNoEntry);
388 }
389 break;
390 /* TODO:
391 case wxCURSOR_WATCH:
392 {
393 M_CURSORDATA->m_themeCursor = kThemeWatchCursor;
394 break;
395 }
396 */
397 case wxCURSOR_PAINT_BRUSH:
398 {
399 M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorPaintBrush);
400 break;
401 }
402 case wxCURSOR_POINT_LEFT:
403 {
404 M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorPointLeft);
405 break;
406 }
407 case wxCURSOR_POINT_RIGHT:
408 {
409 M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorPointRight);
410 break;
411 }
412 case wxCURSOR_QUESTION_ARROW:
413 {
414 M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorQuestionArrow);
415 break;
416 }
417 case wxCURSOR_BLANK:
418 {
419 M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorBlank);
420 break;
421 }
422 case wxCURSOR_RIGHT_ARROW:
423 {
424 M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorRightArrow);
425 break;
426 }
427 case wxCURSOR_SPRAYCAN:
428 {
429 M_CURSORDATA->m_hCursor = wxGetStockCursor(kwxCursorRoller);
430 break;
431 }
432 case wxCURSOR_CHAR:
433 case wxCURSOR_LEFT_BUTTON:
434 case wxCURSOR_RIGHT_BUTTON:
435 case wxCURSOR_MIDDLE_BUTTON:
436 default:
437 break;
438 }
439 }
440
441 wxCursor::~wxCursor()
442 {
443 }
444
445 // Global cursor setting
446 void wxSetCursor(const wxCursor& cursor)
447 {
448 if (cursor.GetNSCursor())
449 [cursor.GetNSCursor() push];
450 }
451
452 static int wxBusyCursorCount = 0;
453
454 // Set the cursor to the busy cursor for all windows
455 void wxBeginBusyCursor(const wxCursor *cursor)
456 {
457 wxBusyCursorCount ++;
458 if (wxBusyCursorCount == 1)
459 {
460 // TODO
461 }
462 else
463 {
464 // TODO
465 }
466 }
467
468 // Restore cursor to normal
469 void wxEndBusyCursor()
470 {
471 if (wxBusyCursorCount == 0)
472 return;
473
474 wxBusyCursorCount --;
475 if (wxBusyCursorCount == 0)
476 {
477 // TODO
478 }
479 }
480
481 // true if we're between the above two calls
482 bool wxIsBusy()
483 {
484 return (wxBusyCursorCount > 0);
485 }