Skip to content

Commit

Permalink
cursors
Browse files Browse the repository at this point in the history
Signed-off-by: stal <[email protected]>
  • Loading branch information
stal888 committed May 22, 2015
1 parent 75fc9ce commit 0d8fd57
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
88 changes: 88 additions & 0 deletions cocoa/cursor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#ifndef SCA_CURSOR_H
#define SCA_CURSOR_H

#import <AppKit/AppKit.h>

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
21 changes: 20 additions & 1 deletion cocoa/interaction.m
Original file line number Diff line number Diff line change
@@ -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);

Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions cocoa/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cocoa/objc_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 0d8fd57

Please sign in to comment.