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