From 0d8fd57da29ba9249800354b8337cb2c21b7f4c0 Mon Sep 17 00:00:00 2001 From: stal Date: Sun, 10 May 2015 20:03:36 -0700 Subject: [PATCH] cursors Signed-off-by: stal --- cocoa/cursor.h | 88 +++++++++++++++++++++++++++++++++++++++++++++ cocoa/interaction.m | 21 ++++++++++- cocoa/main.m | 1 + cocoa/objc_main.h | 1 + 4 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 cocoa/cursor.h diff --git a/cocoa/cursor.h b/cocoa/cursor.h new file mode 100644 index 00000000..af6ad7ee --- /dev/null +++ b/cocoa/cursor.h @@ -0,0 +1,88 @@ +#ifndef SCA_CURSOR_H +#define SCA_CURSOR_H + +#import + +static inline CGImageRef create_zoom_cursor(CGFloat scale, BOOL plus) { + CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB(); + CGColorRef black = CGColorCreateGenericRGB(0, 0, 0, 1.0); + CGColorRef white = CGColorCreateGenericRGB(1, 1, 1, 1.0); + + CGContextRef draw = CGBitmapContextCreate(NULL, 16 * scale, 16 * scale, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaPremultipliedLast); + CGColorSpaceRelease(cs); + + CGContextBeginPath(draw); + CGContextMoveToPoint(draw, 2., 14.); + CGContextAddLineToPoint(draw, 15., 1.); + //CGContextCloseSubpath(draw); + + CGContextSetStrokeColorWithColor(draw, black); + CGContextSetLineWidth(draw, 3.0); + CGContextDrawPath(draw, kCGPathStroke); + + CGContextBeginPath(draw); + CGContextAddEllipseInRect(draw, CGRectInset((CGRect){0, 4, 12, 12}, 0.5, 0.5)); + + CGContextSetLineWidth(draw, 1.0); + CGContextSetFillColorWithColor(draw, white); + CGContextDrawPath(draw, kCGPathFillStroke); + + CGContextBeginPath(draw); + CGContextAddRect(draw, (CGRect){3, 9, 6, 2}); + + if (plus) + CGContextAddRect(draw, (CGRect){5, 7, 2, 6}); + + CGContextSetFillColorWithColor(draw, black); + CGContextDrawPath(draw, kCGPathFill); + + CGImageRef glass = CGBitmapContextCreateImage(draw); + CGContextRelease(draw); + CGColorRelease(white); + CGColorRelease(black); + return glass; +} + +static inline NSCursor *create_zoom_in_cursor(void) { + CGImageRef base = create_zoom_cursor(1.0, YES); + CGImageRef base2x = create_zoom_cursor(2.0, YES); + + NSBitmapImageRep *b = [[NSBitmapImageRep alloc] initWithCGImage:base]; + NSBitmapImageRep *b2x = [[NSBitmapImageRep alloc] initWithCGImage:base2x]; + NSImage *cur = [[NSImage alloc] initWithSize:(CGSize){16, 16}]; + [cur addRepresentation:b]; + [cur addRepresentation:b2x]; + + NSCursor *ret = [[NSCursor alloc] initWithImage:cur hotSpot:(CGPoint){6, 6}]; + + [b release]; + [b2x release]; + [cur release]; + CGImageRelease(base); + CGImageRelease(base2x); + + return ret; +} + +static inline NSCursor *create_zoom_out_cursor(void) { + CGImageRef base = create_zoom_cursor(1.0, NO); + CGImageRef base2x = create_zoom_cursor(2.0, NO); + + NSBitmapImageRep *b = [[NSBitmapImageRep alloc] initWithCGImage:base]; + NSBitmapImageRep *b2x = [[NSBitmapImageRep alloc] initWithCGImage:base2x]; + NSImage *cur = [[NSImage alloc] initWithSize:(CGSize){16, 16}]; + [cur addRepresentation:b]; + [cur addRepresentation:b2x]; + + NSCursor *ret = [[NSCursor alloc] initWithImage:cur hotSpot:(CGPoint){6, 6}]; + + [b release]; + [b2x release]; + [cur release]; + CGImageRelease(base); + CGImageRelease(base2x); + + return ret; +} + +#endif diff --git a/cocoa/interaction.m b/cocoa/interaction.m index fcf201fd..c1744683 100644 --- a/cocoa/interaction.m +++ b/cocoa/interaction.m @@ -1,4 +1,16 @@ #include "../main.h" +#include "cursor.h" + +NSCursor *cursors[8]; + +void setup_cursors(void) { + cursors[CURSOR_NONE] = [NSCursor arrowCursor]; + cursors[CURSOR_HAND] = [NSCursor pointingHandCursor]; + cursors[CURSOR_SELECT] = [NSCursor crosshairCursor]; + cursors[CURSOR_TEXT] = [NSCursor IBeamCursor]; + cursors[CURSOR_ZOOM_IN] = create_zoom_in_cursor(); + cursors[CURSOR_ZOOM_OUT] = create_zoom_out_cursor(); +} int getbuf(char_t *ptr, size_t len, int value); @@ -132,23 +144,30 @@ - (void)rightMouseUp:(NSEvent *)theEvent { } - (void)mouseMoved:(NSEvent *)theEvent { + cursor = 0; panel_mmove(&panel_main, 0, 0, utox_window_width, utox_window_height, theEvent.locationInWindow.x, self.frame.size.height - theEvent.locationInWindow.y, theEvent.deltaX, theEvent.deltaY); + [cursors[cursor] set]; } - (void)mouseDragged:(NSEvent *)theEvent { + cursor = 0; panel_mmove(&panel_main, 0, 0, utox_window_width, utox_window_height, theEvent.locationInWindow.x, self.frame.size.height - theEvent.locationInWindow.y, theEvent.deltaX, theEvent.deltaY); + [cursors[cursor] set]; } - (void)rightMouseDragged:(NSEvent *)theEvent { + cursor = 0; panel_mmove(&panel_main, 0, 0, utox_window_width, utox_window_height, theEvent.locationInWindow.x, self.frame.size.height - theEvent.locationInWindow.y, theEvent.deltaX, theEvent.deltaY); + [cursors[cursor] set]; } - (void)mouseEntered:(NSEvent *)theEvent { - + [cursors[0] push]; } - (void)mouseExited:(NSEvent *)theEvent { panel_mleave(&panel_main); + [NSCursor pop]; } - (void)scrollWheel:(NSEvent *)theEvent { diff --git a/cocoa/main.m b/cocoa/main.m index 3e20b7fa..c2375043 100644 --- a/cocoa/main.m +++ b/cocoa/main.m @@ -303,6 +303,7 @@ @implementation uToxAppDelegate #undef SUB_TITLE #define SUB_TITLE "(-cocoa alpha)" - (void)applicationDidFinishLaunching:(NSNotification *)notification { + setup_cursors(); ironclad = [[NSMutableDictionary alloc] init]; // hold COMMAND to start utox in portable mode diff --git a/cocoa/objc_main.h b/cocoa/objc_main.h index 6e5b9f44..4dd65f0c 100644 --- a/cocoa/objc_main.h +++ b/cocoa/objc_main.h @@ -8,6 +8,7 @@ extern CGDirectDisplayID desktop_capture_from; extern CGRect desktop_capture_rect; extern CGFloat desktop_capture_scale; +void setup_cursors(void); #define RELEASE_CHK(func, obj) if ((obj)) \ func((obj));