diff options
| author | Pierre Ossman <ossman@cendio.se> | 2014-06-16 11:17:57 +0000 |
|---|---|---|
| committer | Pierre Ossman <ossman@cendio.se> | 2014-06-16 11:17:57 +0000 |
| commit | 332dc1b7acfedbf80b71cc6f538a14f24d435df3 (patch) | |
| tree | f690751dd9b592b2b00b4d54c803f98073d74b97 /src | |
| parent | b4013ef60225b91af17d664b0938a074b7115202 (diff) | |
Add method to set any custom cursor, based on a Fl_RGB_Image object.
Also change our fallback cursors to use this method, so that fallback
cursors are handled in a platform independent manner. STR #2660.
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@10196 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src')
| -rw-r--r-- | src/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | src/Fl_Window.cxx | 4 | ||||
| -rw-r--r-- | src/Fl_cocoa.mm | 174 | ||||
| -rw-r--r-- | src/Fl_win32.cxx | 153 | ||||
| -rw-r--r-- | src/Fl_x.cxx | 93 | ||||
| -rw-r--r-- | src/fl_cursor.cxx | 378 | ||||
| -rw-r--r-- | src/fl_cursor_help.xpm | 39 | ||||
| -rw-r--r-- | src/fl_cursor_nesw.xpm | 46 | ||||
| -rw-r--r-- | src/fl_cursor_none.xpm | 19 | ||||
| -rw-r--r-- | src/fl_cursor_nwse.xpm | 46 | ||||
| -rw-r--r-- | src/fl_cursor_wait.xpm | 46 |
11 files changed, 678 insertions, 324 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9948a6b52..52ce00b87 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -247,6 +247,10 @@ if(HAVE_XFIXES) target_link_libraries(fltk ${X11_Xfixes_LIB}) endif(HAVE_XFIXES) +if(HAVE_XCURSOR) + target_link_libraries(fltk ${X11_Xcursor_LIB}) +endif(HAVE_XCURSOR) + if(USE_XFT) target_link_libraries(fltk ${X11_Xft_LIB}) endif(USE_XFT) diff --git a/src/Fl_Window.cxx b/src/Fl_Window.cxx index 2bc536556..058de009c 100644 --- a/src/Fl_Window.cxx +++ b/src/Fl_Window.cxx @@ -62,8 +62,6 @@ void Fl_Window::_Fl_Window() { Fl_Window::Fl_Window(int X,int Y,int W, int H, const char *l) : Fl_Group(X, Y, W, H, l) { cursor_default = FL_CURSOR_DEFAULT; - cursor_fg = FL_BLACK; - cursor_bg = FL_WHITE; _Fl_Window(); set_flag(FORCE_POSITION); @@ -73,8 +71,6 @@ Fl_Window::Fl_Window(int W, int H, const char *l) // fix common user error of a missing end() with current(0): : Fl_Group((Fl_Group::current(0),0), 0, W, H, l) { cursor_default = FL_CURSOR_DEFAULT; - cursor_fg = FL_BLACK; - cursor_bg = FL_WHITE; _Fl_Window(); clear_visible(); diff --git a/src/Fl_cocoa.mm b/src/Fl_cocoa.mm index c9fe2db44..722ece9cd 100644 --- a/src/Fl_cocoa.mm +++ b/src/Fl_cocoa.mm @@ -97,7 +97,6 @@ Fl_Display_Device *Fl_Display_Device::_display = new Fl_Display_Device(new Fl_Qu // public variables CGContextRef fl_gc = 0; -NSCursor *fl_default_cursor; void *fl_capture = 0; // (NSWindow*) we need this to compensate for a missing(?) mouse capture bool fl_show_iconic; // true if called from iconize() - shows the next created window in collapsed state //int fl_disable_transient_for; // secret method of removing TRANSIENT_FOR @@ -1413,8 +1412,6 @@ void fl_open_display() { dequeue:YES]; while (ign_event); - fl_default_cursor = [NSCursor arrowCursor]; - // bring the application into foreground without a 'CARB' resource Boolean same_psn; ProcessSerialNumber cur_psn, front_psn; @@ -1832,6 +1829,7 @@ static void cocoaKeyboardHandler(NSEvent *theEvent) - (void)drawRect:(NSRect)rect; - (BOOL)acceptsFirstResponder; - (BOOL)acceptsFirstMouse:(NSEvent*)theEvent; +- (void)resetCursorRects; - (BOOL)performKeyEquivalent:(NSEvent*)theEvent; - (void)mouseUp:(NSEvent *)theEvent; - (void)rightMouseUp:(NSEvent *)theEvent; @@ -1921,6 +1919,16 @@ static void cocoaKeyboardHandler(NSEvent *theEvent) Fl_Window *first = Fl::first_window(); return (first == w || !first->modal()); } +- (void)resetCursorRects { + Fl_Window *w = [(FLWindow*)[self window] getFl_Window]; + Fl_X *i = Fl_X::i(w); + // We have to have at least one cursor rect for invalidateCursorRectsForView + // to work, hence the "else" clause. + if (i->cursor) + [self addCursorRect:[self visibleRect] cursor:(NSCursor*)i->cursor]; + else + [self addCursorRect:[self visibleRect] cursor:[NSCursor arrowCursor]]; +} - (void)mouseUp:(NSEvent *)theEvent { cocoaMouseHandler(theEvent); } @@ -2367,7 +2375,7 @@ void Fl_X::make(Fl_Window* w) x->other_xid = 0; x->region = 0; x->subRegion = 0; - x->cursor = fl_default_cursor; + x->cursor = NULL; x->gc = 0; // stay 0 for Quickdraw; fill with CGContext for Quartz w->set_visible(); Fl_Window *win = w->window(); @@ -2464,7 +2472,7 @@ void Fl_X::make(Fl_Window* w) x->other_xid = 0; // room for doublebuffering image map. On OS X this is only used by overlay windows x->region = 0; x->subRegion = 0; - x->cursor = fl_default_cursor; + x->cursor = NULL; x->xidChildren = 0; x->xidNext = 0; x->gc = 0; @@ -3148,6 +3156,10 @@ void Fl_X::map() { Fl_X::relink(w, w->window() ); w->redraw(); } + if (cursor) { + [(NSCursor*)cursor release]; + cursor = NULL; + } } void Fl_X::unmap() { @@ -3282,68 +3294,106 @@ CFDataRef Fl_X::CGBitmapContextToTIFF(CGContextRef c) return (CFDataRef)tiff; } -static NSCursor *PrepareCursor(NSCursor *cursor, CGContextRef (*f)() ) +int Fl_X::set_cursor(Fl_Cursor c) { - if (cursor == nil) { - CGContextRef c = f(); - NSImage *image = CGBitmapContextToNSImage(c); - fl_delete_offscreen( (Fl_Offscreen)c ); - NSPoint pt = {[image size].width/2, [image size].height/2}; - cursor = [[NSCursor alloc] initWithImage:image hotSpot:pt]; + if (cursor) { + [(NSCursor*)cursor release]; + cursor = NULL; } - return cursor; -} -void Fl_X::set_cursor(Fl_Cursor c) -{ - NSCursor *icrsr; switch (c) { - case FL_CURSOR_CROSS: icrsr = [NSCursor crosshairCursor]; break; - case FL_CURSOR_WAIT: - static NSCursor *watch = nil; - watch = PrepareCursor(watch, &Fl_X::watch_cursor_image); - icrsr = watch; - break; - case FL_CURSOR_INSERT: icrsr = [NSCursor IBeamCursor]; break; - case FL_CURSOR_N: icrsr = [NSCursor resizeUpCursor]; break; - case FL_CURSOR_S: icrsr = [NSCursor resizeDownCursor]; break; - case FL_CURSOR_NS: icrsr = [NSCursor resizeUpDownCursor]; break; - case FL_CURSOR_HELP: - static NSCursor *help = nil; - help = PrepareCursor(help, &Fl_X::help_cursor_image); - icrsr = help; - break; - case FL_CURSOR_HAND: icrsr = [NSCursor pointingHandCursor]; break; - case FL_CURSOR_MOVE: icrsr = [NSCursor openHandCursor]; break; - case FL_CURSOR_NE: - case FL_CURSOR_SW: - case FL_CURSOR_NESW: - static NSCursor *nesw = nil; - nesw = PrepareCursor(nesw, &Fl_X::nesw_cursor_image); - icrsr = nesw; - break; - case FL_CURSOR_E: icrsr = [NSCursor resizeRightCursor]; break; - case FL_CURSOR_W: icrsr = [NSCursor resizeLeftCursor]; break; - case FL_CURSOR_WE: icrsr = [NSCursor resizeLeftRightCursor]; break; - case FL_CURSOR_SE: - case FL_CURSOR_NW: - case FL_CURSOR_NWSE: - static NSCursor *nwse = nil; - nwse = PrepareCursor(nwse, &Fl_X::nwse_cursor_image); - icrsr = nwse; - break; - case FL_CURSOR_NONE: - static NSCursor *none = nil; - none = PrepareCursor(none, &Fl_X::none_cursor_image); - icrsr = none; - break; - case FL_CURSOR_ARROW: - case FL_CURSOR_DEFAULT: - default: icrsr = [NSCursor arrowCursor]; - break; + case FL_CURSOR_ARROW: cursor = [NSCursor arrowCursor]; break; + case FL_CURSOR_CROSS: cursor = [NSCursor crosshairCursor]; break; + case FL_CURSOR_INSERT: cursor = [NSCursor IBeamCursor]; break; + case FL_CURSOR_HAND: cursor = [NSCursor pointingHandCursor]; break; + case FL_CURSOR_MOVE: cursor = [NSCursor openHandCursor]; break; + case FL_CURSOR_NS: cursor = [NSCursor resizeUpDownCursor]; break; + case FL_CURSOR_WE: cursor = [NSCursor resizeLeftRightCursor]; break; + case FL_CURSOR_N: cursor = [NSCursor resizeUpCursor]; break; + case FL_CURSOR_E: cursor = [NSCursor resizeRightCursor]; break; + case FL_CURSOR_W: cursor = [NSCursor resizeLeftCursor]; break; + case FL_CURSOR_S: cursor = [NSCursor resizeDownCursor]; break; + default: + return 0; + } + + [(NSCursor*)cursor retain]; + + [(NSWindow*)xid invalidateCursorRectsForView:[(NSWindow*)xid contentView]]; + + return 1; +} + +int Fl_X::set_cursor(const Fl_RGB_Image *image, int hotx, int hoty) { + if (cursor) { + [(NSCursor*)cursor release]; + cursor = NULL; + } + + if ((hotx < 0) || (hotx >= image->w())) + return 0; + if ((hoty < 0) || (hoty >= image->h())) + return 0; + + // OS X >= 10.6 can create a NSImage from a CGImage, but we need to + // support older versions, hence this pesky handling. + + NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] + initWithBitmapDataPlanes:NULL + pixelsWide:image->w() + pixelsHigh:image->h() + bitsPerSample:8 + samplesPerPixel:image->d() + hasAlpha:!(image->d() & 1) + isPlanar:NO + colorSpaceName:(image->d()<=2) ? NSDeviceWhiteColorSpace : NSDeviceRGBColorSpace + bytesPerRow:(image->w() * image->d()) + bitsPerPixel:(image->d()*8)]; + + // Alpha needs to be premultiplied for this format + + const uchar *i = (const uchar*)*image->data(); + unsigned char *o = [bitmap bitmapData]; + for (int y = 0;y < image->h();y++) { + if (image->d() & 1) { + for (int x = 0;x < image->w();x++) { + unsigned int alpha; + if (image->d() == 4) { + alpha = i[3]; + *o++ = (unsigned char)((unsigned int)*i++ * alpha / 255); + *o++ = (unsigned char)((unsigned int)*i++ * alpha / 255); + } + + alpha = i[1]; + *o++ = (unsigned char)((unsigned int)*i++ * alpha / 255); + *o++ = alpha; + i++; + } + } else { + // No alpha, so we can just copy everything directly. + int len = image->w() * image->d(); + memcpy(o, i, len); + o += len; + i += len; + } + i += image->ld(); } - [icrsr set]; - cursor = icrsr; + + NSImage *nsimage = [[NSImage alloc] + initWithSize:NSMakeSize(image->w(), image->h())]; + + [nsimage addRepresentation:bitmap]; + + cursor = [[NSCursor alloc] + initWithImage:nsimage + hotSpot:NSMakePoint(hotx, hoty)]; + + [(NSWindow*)xid invalidateCursorRectsForView:[(NSWindow*)xid contentView]]; + + [bitmap release]; + [nsimage release]; + + return 1; } @interface FLaboutItemTarget : NSObject diff --git a/src/Fl_win32.cxx b/src/Fl_win32.cxx index 8ca3a70ec..094edb39d 100644 --- a/src/Fl_win32.cxx +++ b/src/Fl_win32.cxx @@ -1653,7 +1653,6 @@ void fl_fix_focus(); // in Fl.cxx char fl_show_iconic; // hack for Fl_Window::iconic() // int fl_background_pixel = -1; // color to use for background -HCURSOR fl_default_cursor; UINT fl_wake_msg = 0; int fl_disable_transient_for; // secret method of removing TRANSIENT_FOR @@ -1702,7 +1701,7 @@ Fl_X* Fl_X::make(Fl_Window* w) { if (!w->icon()) w->icon((void *)LoadIcon(NULL, IDI_APPLICATION)); wcw.hIcon = wcw.hIconSm = (HICON)w->icon(); - wcw.hCursor = fl_default_cursor = LoadCursor(NULL, IDC_ARROW); + wcw.hCursor = LoadCursor(NULL, IDC_ARROW); //uchar r,g,b; Fl::get_color(FL_GRAY,r,g,b); //wc.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(r,g,b)); wcw.hbrBackground = NULL; @@ -1794,7 +1793,8 @@ Fl_X* Fl_X::make(Fl_Window* w) { x->setwindow(w); x->region = 0; x->private_dc = 0; - x->cursor = fl_default_cursor; + x->cursor = LoadCursor(NULL, IDC_ARROW); + x->custom_cursor = 0; if (!fl_codepage) fl_get_codepage(); WCHAR *lab = NULL; @@ -2052,6 +2052,153 @@ void Fl_Window::label(const char *name,const char *iname) { } //////////////////////////////////////////////////////////////// + +#ifndef IDC_HAND +# define IDC_HAND MAKEINTRESOURCE(32649) +#endif // !IDC_HAND + +int Fl_X::set_cursor(Fl_Cursor c) { + LPSTR n; + HCURSOR new_cursor; + + if (c == FL_CURSOR_NONE) + new_cursor = NULL; + else { + switch (c) { + case FL_CURSOR_ARROW: n = IDC_ARROW; break; + case FL_CURSOR_CROSS: n = IDC_CROSS; break; + case FL_CURSOR_WAIT: n = IDC_WAIT; break; + case FL_CURSOR_INSERT: n = IDC_IBEAM; break; + case FL_CURSOR_HAND: n = IDC_HAND; break; + case FL_CURSOR_HELP: n = IDC_HELP; break; + case FL_CURSOR_MOVE: n = IDC_SIZEALL; break; + case FL_CURSOR_N: + case FL_CURSOR_S: + // FIXME: Should probably have fallbacks for these instead + case FL_CURSOR_NS: n = IDC_SIZENS; break; + case FL_CURSOR_NE: + case FL_CURSOR_SW: + // FIXME: Dito. + case FL_CURSOR_NESW: n = IDC_SIZENESW; break; + case FL_CURSOR_E: + case FL_CURSOR_W: + // FIXME: Dito. + case FL_CURSOR_WE: n = IDC_SIZEWE; break; + case FL_CURSOR_SE: + case FL_CURSOR_NW: + // FIXME: Dito. + case FL_CURSOR_NWSE: n = IDC_SIZENWSE; break; + default: + return 0; + } + + new_cursor = LoadCursor(NULL, n); + if (new_cursor == NULL) + return 0; + } + + if ((cursor != NULL) && custom_cursor) + DestroyIcon(cursor); + + cursor = new_cursor; + custom_cursor = 0; + + SetCursor(cursor); + + return 1; +} + +int Fl_X::set_cursor(const Fl_RGB_Image *image, int hotx, int hoty) { + BITMAPV5HEADER bi; + HBITMAP bitmap, mask; + DWORD *bits; + HCURSOR new_cursor; + + if ((hotx < 0) || (hotx >= image->w())) + return 0; + if ((hoty < 0) || (hoty >= image->h())) + return 0; + + memset(&bi, 0, sizeof(BITMAPV5HEADER)); + + bi.bV5Size = sizeof(BITMAPV5HEADER); + bi.bV5Width = image->w(); + bi.bV5Height = -image->h(); // Negative for top-down + bi.bV5Planes = 1; + bi.bV5BitCount = 32; + bi.bV5Compression = BI_BITFIELDS; + bi.bV5RedMask = 0x00FF0000; + bi.bV5GreenMask = 0x0000FF00; + bi.bV5BlueMask = 0x000000FF; + bi.bV5AlphaMask = 0xFF000000; + + HDC hdc; + + hdc = GetDC(NULL); + bitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bi, DIB_RGB_COLORS, (void**)&bits, NULL, 0); + ReleaseDC(NULL, hdc); + + if (bits == NULL) + return 0; + + const uchar *i = (const uchar*)*image->data(); + for (int y = 0;y < image->h();y++) { + for (int x = 0;x < image->w();x++) { + switch (image->d()) { + case 1: + *bits = (0xff<<24) | (i[0]<<16) | (i[0]<<8) | i[0]; + break; + case 2: + *bits = (i[1]<<24) | (i[0]<<16) | (i[0]<<8) | i[0]; + break; + case 3: + *bits = (0xff<<24) | (i[0]<<16) | (i[1]<<8) | i[2]; + break; + case 4: + *bits = (i[3]<<24) | (i[0]<<16) | (i[1]<<8) | i[2]; + break; + } + i += image->d(); + bits++; + } + i += image->ld(); + } + + // A mask bitmap is still needed even though it isn't used + mask = CreateBitmap(image->w(),image->h(),1,1,NULL); + if (mask == NULL) { + DeleteObject(bitmap); + return 0; + } + + ICONINFO ii; + + ii.fIcon = FALSE; + ii.xHotspot = hotx; + ii.yHotspot = hoty; + ii.hbmMask = mask; + ii.hbmColor = bitmap; + + new_cursor = CreateIconIndirect(&ii); + + DeleteObject(bitmap); + DeleteObject(mask); + + if (new_cursor == NULL) + return 0; + + if ((cursor != NULL) && custom_cursor) + DestroyIcon(cursor); + + cursor = new_cursor; + custom_cursor = 1; + + SetCursor(cursor); + + return 1; +} + +//////////////////////////////////////////////////////////////// // Implement the virtual functions for the base Fl_Window class: // If the box is a filled rectangle, we can make the redisplay *look* diff --git a/src/Fl_x.cxx b/src/Fl_x.cxx index 3896777cd..b2b0f705a 100644 --- a/src/Fl_x.cxx +++ b/src/Fl_x.cxx @@ -62,6 +62,11 @@ static int xfixes_event_base = 0; static bool have_xfixes = false; # endif +# include <X11/cursorfont.h> + +# if HAVE_XCURSOR +# include <X11/Xcursor/Xcursor.h> +# endif static Fl_Xlib_Graphics_Driver fl_xlib_driver; static Fl_Display_Device fl_xlib_display(&fl_xlib_driver); Fl_Display_Device *Fl_Display_Device::_display = &fl_xlib_display;// the platform display @@ -2600,6 +2605,94 @@ void Fl_Window::size_range_() { //////////////////////////////////////////////////////////////// +int Fl_X::set_cursor(Fl_Cursor c) { + unsigned int shape; + Cursor xc; + + switch (c) { + case FL_CURSOR_ARROW: shape = XC_left_ptr; break; + case FL_CURSOR_CROSS: shape = XC_tcross; break; + case FL_CURSOR_WAIT: shape = XC_watch; break; + case FL_CURSOR_INSERT: shape = XC_xterm; break; + case FL_CURSOR_HAND: shape = XC_hand2; break; + case FL_CURSOR_HELP: shape = XC_question_arrow; break; + case FL_CURSOR_MOVE: shape = XC_fleur; break; + case FL_CURSOR_NS: shape = XC_sb_v_double_arrow; break; + case FL_CURSOR_WE: shape = XC_sb_h_double_arrow; break; + case FL_CURSOR_NE: shape = XC_top_right_corner; break; + case FL_CURSOR_N: shape = XC_top_side; break; + case FL_CURSOR_NW: shape = XC_top_left_corner; break; + case FL_CURSOR_E: shape = XC_right_side; break; + case FL_CURSOR_W: shape = XC_left_side; break; + case FL_CURSOR_SE: shape = XC_bottom_right_corner; break; + case FL_CURSOR_S: shape = XC_bottom_side; break; + case FL_CURSOR_SW: shape = XC_bottom_left_corner; break; + default: + return 0; + } + + xc = XCreateFontCursor(fl_display, shape); + XDefineCursor(fl_display, xid, xc); + XFreeCursor(fl_display, xc); + + return 1; +} + +int Fl_X::set_cursor(const Fl_RGB_Image *image, int hotx, int hoty) { +#if ! HAVE_XCURSOR + return 0; +#else + XcursorImage *cursor; + Cursor xc; + + if ((hotx < 0) || (hotx >= image->w())) + return 0; + if ((hoty < 0) || (hoty >= image->h())) + return 0; + + cursor = XcursorImageCreate(image->w(), image->h()); + if (!cursor) + return 0; + + const uchar *i = (const uchar*)*image->data(); + XcursorPixel *o = cursor->pixels; + for (int y = 0;y < image->h();y++) { + for (int x = 0;x < image->w();x++) { + switch (image->d()) { + case 1: + *o = (0xff<<24) | (i[0]<<16) | (i[0]<<8) | i[0]; + break; + case 2: + *o = (i[1]<<24) | (i[0]<<16) | (i[0]<<8) | i[0]; + break; + case 3: + *o = (0xff<<24) | (i[0]<<16) | (i[1]<<8) | i[2]; + break; + case 4: + *o = (i[3]<<24) | (i[0]<<16) | (i[1]<<8) | i[2]; + break; + } + i += image->d(); + o++; + } + i += image->ld(); + } + + cursor->xhot = hotx; + cursor->yhot = hoty; + + xc = XcursorImageLoadCursor(fl_display, cursor); + XDefineCursor(fl_display, xid, xc); + XFreeCursor(fl_display, xc); + + XcursorImageDestroy(cursor); + + return 1; +#endif +} + +//////////////////////////////////////////////////////////////// + // returns pointer to the filename, or null if name ends with '/' const char *fl_filename_name(const char *name) { const char *p,*q; diff --git a/src/fl_cursor.cxx b/src/fl_cursor.cxx index c9b372915..c958c9668 100644 --- a/src/fl_cursor.cxx +++ b/src/fl_cursor.cxx @@ -24,297 +24,165 @@ #include <FL/Fl.H> #include <FL/Fl_Window.H> +#include <FL/Fl_Pixmap.H> +#include <FL/Fl_RGB_Image.H> #include <FL/x.H> -#if !defined(WIN32) && !defined(__APPLE__) -# include <X11/cursorfont.h> -#endif #include <FL/fl_draw.H> +#include "fl_cursor_wait.xpm" +#include "fl_cursor_help.xpm" +#include "fl_cursor_nwse.xpm" +#include "fl_cursor_nesw.xpm" +#include "fl_cursor_none.xpm" + /** Sets the cursor for the current window to the specified shape and colors. The cursors are defined in the <FL/Enumerations.H> header file. */ +void fl_cursor(Fl_Cursor c) { + if (Fl::first_window()) Fl::first_window()->cursor(c); +} + +/* For back compatibility only. */ void fl_cursor(Fl_Cursor c, Fl_Color fg, Fl_Color bg) { - if (Fl::first_window()) Fl::first_window()->cursor(c,fg,bg); + fl_cursor(c); } + + /** - Sets the default window cursor as well as its color. + Sets the default window cursor. This is the cursor that will be used + after the mouse pointer leaves a widget with a custom cursor set. - For back compatibility only. + \see cursor(const Fl_RGB_Image*, int, int), default_cursor() */ -void Fl_Window::default_cursor(Fl_Cursor c, Fl_Color fg, Fl_Color bg) { -// if (c == FL_CURSOR_DEFAULT) c = FL_CURSOR_ARROW; - +void Fl_Window::default_cursor(Fl_Cursor c) { cursor_default = c; - cursor_fg = fg; - cursor_bg = bg; + cursor(c); +} + + +static void fallback_cursor(Fl_Window *w, Fl_Cursor c) { + const char **xpm; + int hotx, hoty; + + // The standard arrow is our final fallback, so something is broken + // if we get called back here with that as an argument. + if (c == FL_CURSOR_ARROW) + return; + + switch (c) { + case FL_CURSOR_WAIT: + xpm = (const char**)fl_cursor_wait_xpm; + hotx = 8; + hoty = 15; + break; + case FL_CURSOR_HELP: + xpm = (const char**)fl_cursor_help_xpm; + hotx = 1; + hoty = 3; + break; + case FL_CURSOR_NWSE: + xpm = (const char**)fl_cursor_nwse_xpm; + hotx = 7; + hoty = 7; + break; + case FL_CURSOR_NESW: + xpm = (const char**)fl_cursor_nesw_xpm; + hotx = 7; + hoty = 7; + break; + case FL_CURSOR_NONE: + xpm = (const char**)fl_cursor_none_xpm; + hotx = 0; + hoty = 0; + break; + default: + w->cursor(FL_CURSOR_ARROW); + return; + } - cursor(c, fg, bg); + Fl_Pixmap pxm(xpm); + Fl_RGB_Image image(&pxm); + + w->cursor(&image, hotx, hoty); } -#ifdef WIN32 -# ifndef IDC_HAND -# define IDC_HAND MAKEINTRESOURCE(32649) -# endif // !IDC_HAND +void Fl_Window::cursor(Fl_Cursor c) { + int ret; -void Fl_Window::cursor(Fl_Cursor c, Fl_Color c1, Fl_Color c2) { - if (!shown()) return; // the cursor must be set for the top level window, not for subwindows Fl_Window *w = window(), *toplevel = this; - while (w) { toplevel = w; w = w->window(); } - if (toplevel != this) { toplevel->cursor(c, c1, c2); return; } - // now set the actual cursor - if (c == FL_CURSOR_DEFAULT) { - c = cursor_default; + + while (w) { + toplevel = w; + w = w->window(); } - if (c > FL_CURSOR_NESW) { - i->cursor = 0; - } else if (c == FL_CURSOR_DEFAULT) { - i->cursor = fl_default_cursor; - } else { - LPSTR n; - switch (c) { - case FL_CURSOR_ARROW: n = IDC_ARROW; break; - case FL_CURSOR_CROSS: n = IDC_CROSS; break; - case FL_CURSOR_WAIT: n = IDC_WAIT; break; - case FL_CURSOR_INSERT: n = IDC_IBEAM; break; - case FL_CURSOR_HELP: n = IDC_HELP; break; - case FL_CURSOR_HAND: { - OSVERSIONINFO osvi; - - // Get the OS version: Windows 98 and 2000 have a standard - // hand cursor. - memset(&osvi, 0, sizeof(OSVERSIONINFO)); - osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); - GetVersionEx(&osvi); - - if (osvi.dwMajorVersion > 4 || - (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion > 0 && - osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)) n = IDC_HAND; - else n = IDC_UPARROW; - } break; - case FL_CURSOR_MOVE: n = IDC_SIZEALL; break; - case FL_CURSOR_N: - case FL_CURSOR_S: - case FL_CURSOR_NS: n = IDC_SIZENS; break; - case FL_CURSOR_NE: - case FL_CURSOR_SW: - case FL_CURSOR_NESW: n = IDC_SIZENESW; break; - case FL_CURSOR_E: - case FL_CURSOR_W: - case FL_CURSOR_WE: n = IDC_SIZEWE; break; - case FL_CURSOR_SE: - case FL_CURSOR_NW: - case FL_CURSOR_NWSE: n = IDC_SIZENWSE; break; - default: n = IDC_NO; break; - } - i->cursor = LoadCursor(NULL, n); + + if (toplevel != this) { + toplevel->cursor(c); + return; } - SetCursor(i->cursor); -} -#elif defined(__APPLE__) - -#ifdef __BIG_ENDIAN__ -# define E(x) x -#elif defined __LITTLE_ENDIAN__ -// Don't worry. This will be resolved at compile time -# define E(x) (x>>8)|((x<<8)&0xff00) -#else -# error "Either __LITTLE_ENDIAN__ or __BIG_ENDIAN__ must be defined" -#endif - -CGContextRef Fl_X::help_cursor_image(void) -{ - int w = 20, h = 20; - Fl_Offscreen off = Fl_Quartz_Graphics_Driver::create_offscreen_with_alpha(w, h); - fl_begin_offscreen(off); - CGContextSetRGBFillColor( (CGContextRef)off, 0,0,0,0); - fl_rectf(0,0,w,h); - fl_color(FL_BLACK); - fl_font(FL_COURIER_BOLD, 20); - fl_draw("?", 1, h-1); - fl_end_offscreen(); - return (CGContextRef)off; -} + if (c == FL_CURSOR_DEFAULT) + c = cursor_default; -CGContextRef Fl_X::none_cursor_image(void) -{ - int w = 20, h = 20; - Fl_Offscreen off = Fl_Quartz_Graphics_Driver::create_offscreen_with_alpha(w, h); - fl_begin_offscreen(off); - CGContextSetRGBFillColor( (CGContextRef)off, 0,0,0,0); - fl_rectf(0,0,w,h); - fl_end_offscreen(); - return (CGContextRef)off; -} + if (!i) + return; -CGContextRef Fl_X::watch_cursor_image(void) -{ - int w, h, r = 5; - w = 2*r+6; - h = 4*r; - Fl_Offscreen off = Fl_Quartz_Graphics_Driver::create_offscreen_with_alpha(w, h); - fl_begin_offscreen(off); - CGContextSetRGBFillColor( (CGContextRef)off, 0,0,0,0); - fl_rectf(0,0,w,h); - CGContextTranslateCTM( (CGContextRef)off, w/2, h/2); - fl_color(FL_WHITE); - fl_circle(0, 0, r+1); - fl_color(FL_BLACK); - fl_rectf(int(-r*0.7), int(-r*1.7), int(1.4*r), int(3.4*r)); - fl_rectf(r-1, -1, 3, 3); - fl_color(FL_WHITE); - fl_pie(-r, -r, 2*r, 2*r, 0, 360); - fl_color(FL_BLACK); - fl_circle(0,0,r); - fl_xyline(0, 0, int(-r*.7)); - fl_xyline(0, 0, 0, int(-r*.7)); - fl_end_offscreen(); - return (CGContextRef)off; -} + ret = i->set_cursor(c); + if (ret) + return; -CGContextRef Fl_X::nesw_cursor_image(void) -{ - int c = 7, r = 2*c; - int w = r, h = r; - Fl_Offscreen off = Fl_Quartz_Graphics_Driver::create_offscreen_with_alpha(w, h); - fl_begin_offscreen(off); - CGContextSetRGBFillColor( (CGContextRef)off, 0,0,0,0); - fl_rectf(0,0,w,h); - CGContextTranslateCTM( (CGContextRef)off, 0, h); - CGContextScaleCTM( (CGContextRef)off, 1, -1); - fl_color(FL_BLACK); - fl_polygon(0, 0, c, 0, 0, c); - fl_polygon(r, r, r, r-c, r-c, r); - fl_line_style(FL_SOLID, 2, 0); - fl_line(0,1, r,r+1); - fl_line_style(FL_SOLID, 0, 0); - fl_end_offscreen(); - return (CGContextRef)off; + fallback_cursor(this, c); } -CGContextRef Fl_X::nwse_cursor_image(void) -{ - int c = 7, r = 2*c; - int w = r, h = r; - Fl_Offscreen off = Fl_Quartz_Graphics_Driver::create_offscreen_with_alpha(w, h); - fl_begin_offscreen(off); - CGContextSetRGBFillColor( (CGContextRef)off, 0,0,0,0); - fl_rectf(0,0,w,h); - CGContextTranslateCTM( (CGContextRef)off, 0, h); - CGContextScaleCTM( (CGContextRef)off, 1, -1); - fl_color(FL_BLACK); - fl_polygon(r-1, 0, r-1, c, r-1-c, 0); - fl_polygon(-1, r, c-1, r, -1, r-c); - fl_line_style(FL_SOLID, 2, 0); - fl_line(r-1,1, -1,r+1); - fl_line_style(FL_SOLID, 0, 0); - fl_end_offscreen(); - return (CGContextRef)off; -} +/** + Changes the cursor for this window. This always calls the system, if + you are changing the cursor a lot you may want to keep track of how + you set it in a static variable and call this only if the new cursor + is different. -void Fl_Window::cursor(Fl_Cursor c, Fl_Color, Fl_Color) { - if (c == FL_CURSOR_DEFAULT) { - c = cursor_default; - } - if (i) i->set_cursor(c); -} + The default cursor will be used if the provided image cannot be used + as a cursor. -#else - -// I like the MSWindows resize cursors, so I duplicate them here: - -#define CURSORSIZE 16 -#define HOTXY 7 -static struct TableEntry { - uchar bits[CURSORSIZE*CURSORSIZE/8]; - uchar mask[CURSORSIZE*CURSORSIZE/8]; - Cursor cursor; -} table[] = { - {{ // FL_CURSOR_NS - 0x00, 0x00, 0x80, 0x01, 0xc0, 0x03, 0xe0, 0x07, 0x80, 0x01, 0x80, 0x01, - 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, - 0xe0, 0x07, 0xc0, 0x03, 0x80, 0x01, 0x00, 0x00}, - { - 0x80, 0x01, 0xc0, 0x03, 0xe0, 0x07, 0xf0, 0x0f, 0xf0, 0x0f, 0xc0, 0x03, - 0xc0, 0x03, 0xc0, 0x03, 0xc0, 0x03, 0xc0, 0x03, 0xc0, 0x03, 0xf0, 0x0f, - 0xf0, 0x0f, 0xe0, 0x07, 0xc0, 0x03, 0x80, 0x01}}, - {{ // FL_CURSOR_EW - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, - 0x0c, 0x30, 0xfe, 0x7f, 0xfe, 0x7f, 0x0c, 0x30, 0x08, 0x10, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, - { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x1c, 0x38, - 0xfe, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x7f, 0x1c, 0x38, 0x18, 0x18, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}, - {{ // FL_CURSOR_NWSE - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x38, 0x00, 0x78, 0x00, - 0xe8, 0x00, 0xc0, 0x01, 0x80, 0x03, 0x00, 0x17, 0x00, 0x1e, 0x00, 0x1c, - 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, - { - 0x00, 0x00, 0x00, 0x00, 0xfc, 0x00, 0xfc, 0x00, 0x7c, 0x00, 0xfc, 0x00, - 0xfc, 0x01, 0xec, 0x03, 0xc0, 0x37, 0x80, 0x3f, 0x00, 0x3f, 0x00, 0x3e, - 0x00, 0x3f, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00}}, - {{ // FL_CURSOR_NESW - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x1c, 0x00, 0x1e, - 0x00, 0x17, 0x80, 0x03, 0xc0, 0x01, 0xe8, 0x00, 0x78, 0x00, 0x38, 0x00, - 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, - { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x3f, 0x00, 0x3e, 0x00, 0x3f, - 0x80, 0x3f, 0xc0, 0x37, 0xec, 0x03, 0xfc, 0x01, 0xfc, 0x00, 0x7c, 0x00, - 0xfc, 0x00, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00}}, - {{0}, {0}} // FL_CURSOR_NONE & unknown -}; + \see cursor(Fl_Cursor), default_cursor() +*/ +void Fl_Window::cursor(const Fl_RGB_Image *image, int hotx, int hoty) { + int ret; -void Fl_Window::cursor(Fl_Cursor c, Fl_Color fg, Fl_Color bg) { - if (!shown()) return; - Cursor xc; - int deleteit = 0; - if (c == FL_CURSOR_DEFAULT) { - c = cursor_default; - fg = cursor_fg; - bg = cursor_bg; + // the cursor must be set for the top level window, not for subwindows + Fl_Window *w = window(), *toplevel = this; + + while (w) { + toplevel = w; + w = w->window(); } - if (!c) { - xc = None; - } else { - if (c >= FL_CURSOR_NS) { - TableEntry *q = (c > FL_CURSOR_NESW) ? table+4 : table+(c-FL_CURSOR_NS); - if (!(q->cursor)) { - XColor dummy = { 0 }; - Pixmap p = XCreateBitmapFromData(fl_display, - RootWindow(fl_display, fl_screen), (const char*)(q->bits), - CURSORSIZE, CURSORSIZE); - Pixmap m = XCreateBitmapFromData(fl_display, - RootWindow(fl_display, fl_screen), (const char*)(q->mask), - CURSORSIZE, CURSORSIZE); - q->cursor = XCreatePixmapCursor(fl_display, p,m,&dummy, &dummy, - HOTXY, HOTXY); - XFreePixmap(fl_display, m); - XFreePixmap(fl_display, p); - } - xc = q->cursor; - } else { - xc = XCreateFontCursor(fl_display, (c-1)*2); - deleteit = 1; - } - XColor fgc; - uchar r,g,b; - Fl::get_color(fg,r,g,b); - fgc.red = r<<8; fgc.green = g<<8; fgc.blue = b<<8; - XColor bgc; - Fl::get_color(bg,r,g,b); - bgc.red = r<<8; bgc.green = g<<8; bgc.blue = b<<8; - XRecolorCursor(fl_display, xc, &fgc, &bgc); + if (toplevel != this) { + toplevel->cursor(image, hotx, hoty); + return; } - XDefineCursor(fl_display, fl_xid(this), xc); - if (deleteit) XFreeCursor(fl_display, xc); + + if (!i) + return; + + ret = i->set_cursor(image, hotx, hoty); + if (ret) + return; + + cursor(FL_CURSOR_DEFAULT); } -#endif +/* For back compatibility only. */ +void Fl_Window::cursor(Fl_Cursor c, Fl_Color, Fl_Color) { + cursor(c); +}; + +void Fl_Window::default_cursor(Fl_Cursor c, Fl_Color, Fl_Color) { + default_cursor(c); +}; + // // End of "$Id$". diff --git a/src/fl_cursor_help.xpm b/src/fl_cursor_help.xpm new file mode 100644 index 000000000..d9d8be0fd --- /dev/null +++ b/src/fl_cursor_help.xpm @@ -0,0 +1,39 @@ +/* XPM */ +static char * fl_cursor_help_xpm[] = { +"16 27 9 1", +" c None", +". c #FFFFFF", +"+ c #DBDBDB", +"@ c #242424", +"# c #000000", +"$ c #494949", +"% c #6D6D6D", +"& c #929292", +"* c #B6B6B6", +" ", +". ", +".+ ", +".@+ ", +".#@+ ", +".##@. ", +".###@. ", +".####@. ", +".#####$. ", +".######$. ", +".#######$. ", +".#####@@@%. ", +".#####+..... ", +".##$%#%. ", +".#$..@#. ", +".$. .&#%. ", +".. .##. .... ", +". .&#.+%$%&. ", +" ...*#@##%.", +" ++.*#@.", +" .%#$.", +" .%#@. ", +" .##+ ", +" .++. ", +" +##. ", +" +##. ", +" .... "}; diff --git a/src/fl_cursor_nesw.xpm b/src/fl_cursor_nesw.xpm new file mode 100644 index 000000000..5a7113a9e --- /dev/null +++ b/src/fl_cursor_nesw.xpm @@ -0,0 +1,46 @@ +/* XPM */ +static const char * fl_cursor_nesw_xpm[] = { +"15 15 28 1", +" c None", +". c #FFFFFF", +"+ c #767676", +"@ c #000000", +"# c #4E4E4E", +"$ c #0C0C0C", +"% c #494949", +"& c #4D4D4D", +"* c #1B1B1B", +"= c #515151", +"- c #646464", +"; c #363636", +"> c #6A6A6A", +", c #545454", +"' c #585858", +") c #242424", +"! c #797979", +"~ c #2E2E2E", +"{ c #444444", +"] c #3B3B3B", +"^ c #0A0A0A", +"/ c #595959", +"( c #F7F7F7", +"_ c #080808", +": c #6B6B6B", +"< c #FDFDFD", +"[ c #FCFCFC", +"} c #FEFEFE", +" ..........", +" .+@@@@@@.", +" .#@@@@@.", +" .$@@@@.", +" .%@@@@@.", +". .&@@@*@@.", +".. .=@@@-.;@.", +".>. .,@@@'. .).", +".@!.'@@@#. ..", +".@@~@@@{. .", +".@@@@@]. ", +".@@@@^. ", +".@@@@@/( ", +".______:( ", +"<[[[[[[[[} "}; diff --git a/src/fl_cursor_none.xpm b/src/fl_cursor_none.xpm new file mode 100644 index 000000000..054e461d2 --- /dev/null +++ b/src/fl_cursor_none.xpm @@ -0,0 +1,19 @@ +/* XPM */ +static const char * fl_cursor_none_xpm[] = { +"15 15 1 1", +" c None", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" ", +" "}; diff --git a/src/fl_cursor_nwse.xpm b/src/fl_cursor_nwse.xpm new file mode 100644 index 000000000..d495eb4ff --- /dev/null +++ b/src/fl_cursor_nwse.xpm @@ -0,0 +1,46 @@ +/* XPM */ +static const char * fl_cursor_nwse_xpm[] = { +"15 15 28 1", +" c None", +". c #FFFFFF", +"+ c #000000", +"@ c #767676", +"# c #4E4E4E", +"$ c #0C0C0C", +"% c #494949", +"& c #1B1B1B", +"* c #4D4D4D", +"= c #363636", +"- c #646464", +"; c #515151", +"> c #242424", +", c #585858", +"' c #545454", +") c #6A6A6A", +"! c #797979", +"~ c #444444", +"{ c #2E2E2E", +"] c #3B3B3B", +"^ c #0A0A0A", +"/ c #F7F7F7", +"( c #595959", +"_ c #6B6B6B", +": c #080808", +"< c #FEFEFE", +"[ c #FCFCFC", +"} c #FDFDFD", +".......... ", +".++++++@. ", +".+++++#. ", +".++++$. ", +".+++++%. ", +".++&+++*. .", +".+=.-+++;. ..", +".>. .,+++'. .).", +".. .#+++,.!+.", +". .~+++{++.", +" .]+++++.", +" .^++++.", +" /(+++++.", +" /_::::::.", +" <[[[[[[[[}"}; diff --git a/src/fl_cursor_wait.xpm b/src/fl_cursor_wait.xpm new file mode 100644 index 000000000..3fb2ce32a --- /dev/null +++ b/src/fl_cursor_wait.xpm @@ -0,0 +1,46 @@ +/* XPM */ +static char * fl_cursor_wait_xpm[] = { +"17 32 11 1", +" c None", +". c #FFFFFF", +"+ c #2E2E2E", +"@ c #202020", +"# c #242424", +"$ c #000000", +"% c #494949", +"& c #929292", +"* c #B6B6B6", +"= c #DBDBDB", +"- c #6D6D6D", +".................", +".+@@@@@@@@@@@@@+.", +".................", +" .#$$$$$$$$$$$#. ", +" .%$$$$$$$$$$$%. ", +" .&$$$$$$$$$$$&. ", +" *$$$$$$$$$$$* ", +" =$$$$$$$$$$$= ", +" .#$$$$$$$$$#. ", +" .-$$$$$$$$$-. ", +" *$$$$$$$$$* ", +" .$=.....=$. ", +" .%-.....-%. ", +" =#=...=#= ", +" .-%...%-. ", +" .##.##. ", +" .#$.$#. ", +" .-$$.$$-. ", +" =$$$.$$$= ", +" .%$$$.$$$%. ", +" .$$$$.$$$$. ", +" *$$$$.$$$$* ", +" .-$$$$.$$$$-. ", +" .#$$$$.$$$$#. ", +" =$$#&*.*&#$$= ", +" *$%.......%$* ", +" .&#.........#&. ", +" .%-.........-%. ", +" .#$$$$$$$$$$$#. ", +".................", +".+@@@@@@@@@@@@@+.", +"................."}; |
