Fix for bug #240
Christian Walther contributed Cocoa cursor code. --HG-- extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%401897
This commit is contained in:
parent
642baf6edb
commit
d856d38658
2 changed files with 60 additions and 29 deletions
|
@ -25,49 +25,69 @@
|
||||||
|
|
||||||
|
|
||||||
struct WMcursor {
|
struct WMcursor {
|
||||||
Cursor curs;
|
NSCursor *nscursor;
|
||||||
};
|
};
|
||||||
|
|
||||||
void QZ_FreeWMCursor (_THIS, WMcursor *cursor) {
|
void QZ_FreeWMCursor (_THIS, WMcursor *cursor) {
|
||||||
|
|
||||||
if ( cursor != NULL )
|
if ( cursor != NULL ) {
|
||||||
|
[ cursor->nscursor release ];
|
||||||
free (cursor);
|
free (cursor);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Use the Carbon cursor routines for now */
|
|
||||||
WMcursor* QZ_CreateWMCursor (_THIS, Uint8 *data, Uint8 *mask,
|
WMcursor* QZ_CreateWMCursor (_THIS, Uint8 *data, Uint8 *mask,
|
||||||
int w, int h, int hot_x, int hot_y) {
|
int w, int h, int hot_x, int hot_y) {
|
||||||
WMcursor *cursor;
|
WMcursor *cursor;
|
||||||
int row, bytes;
|
NSBitmapImageRep *imgrep;
|
||||||
|
NSImage *img;
|
||||||
|
unsigned char *planes[5];
|
||||||
|
int i;
|
||||||
|
NSAutoreleasePool *pool;
|
||||||
|
|
||||||
|
pool = [ [ NSAutoreleasePool alloc ] init ];
|
||||||
|
|
||||||
/* Allocate the cursor memory */
|
/* Allocate the cursor memory */
|
||||||
cursor = (WMcursor *)SDL_malloc(sizeof(WMcursor));
|
cursor = (WMcursor *)SDL_malloc(sizeof(WMcursor));
|
||||||
if ( cursor == NULL ) {
|
if (cursor == NULL) goto outOfMemory;
|
||||||
SDL_OutOfMemory();
|
|
||||||
return(NULL);
|
/* create the image representation and get the pointers to its storage */
|
||||||
|
imgrep = [ [ [ NSBitmapImageRep alloc ] initWithBitmapDataPlanes: NULL pixelsWide: w pixelsHigh: h bitsPerSample: 1 samplesPerPixel: 2 hasAlpha: YES isPlanar: YES colorSpaceName: NSDeviceBlackColorSpace bytesPerRow: (w+7)/8 bitsPerPixel: 0 ] autorelease ];
|
||||||
|
if (imgrep == nil) goto outOfMemory;
|
||||||
|
[ imgrep getBitmapDataPlanes: planes ];
|
||||||
|
|
||||||
|
/* copy data and mask, extending the mask to all black pixels because the inversion effect doesn't work with Cocoa's alpha-blended cursors */
|
||||||
|
for (i = 0; i < (w+7)/8*h; i++) {
|
||||||
|
planes[0][i] = data[i];
|
||||||
|
planes[1][i] = mask[i] | data[i];
|
||||||
}
|
}
|
||||||
SDL_memset(cursor, 0, sizeof(*cursor));
|
|
||||||
|
|
||||||
if (w > 16)
|
/* create image and cursor */
|
||||||
w = 16;
|
img = [ [ [ NSImage alloc ] initWithSize: NSMakeSize(w, h) ] autorelease ];
|
||||||
|
if (img == nil) goto outOfMemory;
|
||||||
if (h > 16)
|
[ img addRepresentation: imgrep ];
|
||||||
h = 16;
|
if (system_version < 0x1030) { /* on 10.2, cursors must be 16*16 */
|
||||||
|
if (w > 16 || h > 16) { /* too big: scale it down */
|
||||||
bytes = (w+7)/8;
|
[ img setScalesWhenResized: YES ];
|
||||||
|
hot_x = hot_x*16/w;
|
||||||
for ( row=0; row<h; ++row ) {
|
hot_y = hot_y*16/h;
|
||||||
SDL_memcpy(&cursor->curs.data[row], data, bytes);
|
}
|
||||||
data += bytes;
|
else { /* too small (or just right): extend it (from the bottom left corner, so hot_y must be adjusted) */
|
||||||
|
hot_y += 16 - h;
|
||||||
|
}
|
||||||
|
[ img setSize: NSMakeSize(16, 16) ];
|
||||||
}
|
}
|
||||||
for ( row=0; row<h; ++row ) {
|
cursor->nscursor = [ [ NSCursor alloc ] initWithImage: img hotSpot: NSMakePoint(hot_x, hot_y) ];
|
||||||
SDL_memcpy(&cursor->curs.mask[row], mask, bytes);
|
if (cursor->nscursor == nil) goto outOfMemory;
|
||||||
mask += bytes;
|
|
||||||
}
|
|
||||||
cursor->curs.hotSpot.h = hot_x;
|
|
||||||
cursor->curs.hotSpot.v = hot_y;
|
|
||||||
|
|
||||||
|
[ pool release ];
|
||||||
return(cursor);
|
return(cursor);
|
||||||
|
|
||||||
|
outOfMemory:
|
||||||
|
[ pool release ];
|
||||||
|
if (cursor != NULL) SDL_free(cursor);
|
||||||
|
SDL_OutOfMemory();
|
||||||
|
return(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QZ_ShowMouse (_THIS) {
|
void QZ_ShowMouse (_THIS) {
|
||||||
|
@ -103,7 +123,7 @@ int QZ_ShowWMCursor (_THIS, WMcursor *cursor) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
SetCursor(&cursor->curs);
|
[ cursor->nscursor set ];
|
||||||
if ( ! cursor_should_be_visible ) {
|
if ( ! cursor_should_be_visible ) {
|
||||||
QZ_ShowMouse (this);
|
QZ_ShowMouse (this);
|
||||||
cursor_should_be_visible = YES;
|
cursor_should_be_visible = YES;
|
||||||
|
|
|
@ -55,6 +55,10 @@ Uint16 cursor_mask[16]={
|
||||||
0xff00
|
0xff00
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* another test cursor: smaller than 16x16, and with an odd height */
|
||||||
|
|
||||||
|
Uint8 small_cursor_data[11] = { 0x00, 0x18, 0x08, 0x38, 0x44, 0x54, 0x44, 0x38, 0x20, 0x20, 0x00 };
|
||||||
|
Uint8 small_cursor_mask[11] = { 0x3C, 0x3C, 0x3C, 0x7C, 0xC6, 0xC6, 0xC6, 0x7C, 0x78, 0x70, 0x70 };
|
||||||
|
|
||||||
/* XPM */
|
/* XPM */
|
||||||
static const char *arrow[] = {
|
static const char *arrow[] = {
|
||||||
|
@ -139,7 +143,7 @@ int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
SDL_Surface *screen;
|
SDL_Surface *screen;
|
||||||
SDL_bool quit = SDL_FALSE, first_time = SDL_TRUE;
|
SDL_bool quit = SDL_FALSE, first_time = SDL_TRUE;
|
||||||
SDL_Cursor *cursor[2];
|
SDL_Cursor *cursor[3];
|
||||||
int current;
|
int current;
|
||||||
|
|
||||||
/* Load the SDL library */
|
/* Load the SDL library */
|
||||||
|
@ -170,6 +174,13 @@ int main(int argc, char *argv[])
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
|
cursor[2] = SDL_CreateCursor(small_cursor_data, small_cursor_mask,
|
||||||
|
8, 11, 3, 5);
|
||||||
|
if (cursor[2]==NULL) {
|
||||||
|
fprintf(stderr, "Couldn't initialize test cursor: %s\n",SDL_GetError());
|
||||||
|
SDL_Quit();
|
||||||
|
return(1);
|
||||||
|
}
|
||||||
|
|
||||||
current = 0;
|
current = 0;
|
||||||
SDL_SetCursor(cursor[current]);
|
SDL_SetCursor(cursor[current]);
|
||||||
|
@ -179,7 +190,7 @@ int main(int argc, char *argv[])
|
||||||
while (SDL_PollEvent(&event)) {
|
while (SDL_PollEvent(&event)) {
|
||||||
switch(event.type) {
|
switch(event.type) {
|
||||||
case SDL_MOUSEBUTTONDOWN:
|
case SDL_MOUSEBUTTONDOWN:
|
||||||
current = !current;
|
current = (current + 1)%3;
|
||||||
SDL_SetCursor(cursor[current]);
|
SDL_SetCursor(cursor[current]);
|
||||||
break;
|
break;
|
||||||
case SDL_KEYDOWN:
|
case SDL_KEYDOWN:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue