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