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