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