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