Compare commits

..

No commits in common. "98eb529c6fe300cc82ceebd3071e3bd7da98935a" and "3260643ada9e7c5ca0c3455c8e265d3b68da0c55" have entirely different histories.

2 changed files with 966 additions and 947 deletions

View file

@ -113,7 +113,6 @@
MwBool pointerLocked; MwBool pointerLocked;
MwBool mouseMoved; MwBool mouseMoved;
} }
+ (MilskoCocoa *)newWithParent:(MwLL)parent + (MilskoCocoa *)newWithParent:(MwLL)parent

View file

@ -1,4 +1,3 @@
#include "Mw/BaseTypes.h"
#include <Mw/Milsko.h> #include <Mw/Milsko.h>
#ifdef __clang__ #ifdef __clang__
@ -14,6 +13,14 @@ static NSRect rectFlip(NSRect originFrame) {
double destinationY = screenHeight - (originY + frameHeight); double destinationY = screenHeight - (originY + frameHeight);
NSRect destinationFrame = originFrame; NSRect destinationFrame = originFrame;
destinationFrame.origin.y = destinationY; destinationFrame.origin.y = destinationY;
if (destinationFrame.origin.x < 0)
destinationFrame.origin.x = 0;
if (destinationFrame.origin.y < 0)
destinationFrame.origin.y = 0;
if (destinationFrame.size.width < 0)
destinationFrame.size.width = 0;
if (destinationFrame.size.height < 0)
destinationFrame.size.height = 0;
return destinationFrame; return destinationFrame;
} }
@ -21,16 +28,6 @@ static NSPoint pointFlip(NSPoint point) {
return rectFlip(NSMakeRect(point.x, point.y, 0, 0)).origin; return rectFlip(NSMakeRect(point.x, point.y, 0, 0)).origin;
} }
static NSRect localRectFlip(NSRect originFrame, NSView* view) {
float viewHeight = [view bounds].size.height;
NSRect destinationFrame = NSMakeRect(
originFrame.origin.x,
[view bounds].size.height - (originFrame.origin.y + originFrame.size.height),
originFrame.size.width,
originFrame.size.height);
return destinationFrame;
}
@implementation MilskoCocoaPixmap @implementation MilskoCocoaPixmap
+ (MilskoCocoaPixmap *)newWithWidth:(int)width height:(int)height { + (MilskoCocoaPixmap *)newWithWidth:(int)width height:(int)height {
@ -102,74 +99,78 @@ static NSRect localRectFlip(NSRect originFrame, NSView* view) {
MilskoCocoa *c = [MilskoCocoa alloc]; MilskoCocoa *c = [MilskoCocoa alloc];
[c retain]; [c retain];
/*
* MacOS doesn't really have a "default" window position, you're actually meant to center the window yourself,
* so if the user passes MwDEFAULT respond appropriately. Also for child windows just have it be 0.
*/
if (x == MwDEFAULT) { if (x == MwDEFAULT) {
x = r ? ([[NSScreen mainScreen] frame].size.width / 2.) - (width / 2.) : 0; x = ([[NSScreen mainScreen] frame].size.width / 2.) - (width / 2.);
} }
if (y == MwDEFAULT) { if (y == MwDEFAULT) {
y = r ? ([[NSScreen mainScreen] frame].size.height / 2.) - (height / 2.) : 0; y = ([[NSScreen mainScreen] frame].size.height / 2.) - (height / 2.);
} }
c->application = [NSApplication sharedApplication]; c->application = [NSApplication sharedApplication];
c->rect = NSMakeRect(x, y, width, height); c->rect = rectFlip(NSMakeRect(x, y, width, height));
if (parent == NULL) { if (parent == NULL) {
c->window = [[MilskoCocoaWindow alloc] c->window = [[MilskoCocoaWindow alloc]
initWithContentRect:rectFlip(c->rect) initWithContentRect:c->rect
styleMask:(NSTitledWindowMask | styleMask:(NSTitledWindowMask |
NSTexturedBackgroundWindowMask | NSTexturedBackgroundWindowMask |
NSClosableWindowMask | NSMiniaturizableWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask |
NSResizableWindowMask) NSResizableWindowMask)
backing:NSBackingStoreBuffered backing:NSBackingStoreBuffered
defer:NO]; defer:NO];
} else {
double offset = 0;
MilskoCocoaWindow *parentWindow = parent->cocoa.real->window;
offset =
[parentWindow frameRectForContentRect:[parentWindow frame]]
.size.height -
[parentWindow contentRectForFrameRect:[parentWindow frame]].size.height;
c->rect.origin.x += [parentWindow frame].origin.x;
c->rect.origin.y -= [parentWindow frame].origin.y - offset;
c->rect.origin.y -= offset;
c->window =
[[MilskoCocoaWindow alloc] initWithContentRect:c->rect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
}
[c->window [c->window
setDelegate:[[MilskoCocoaWindowDelegate alloc] initWithWin:c->window]]; setDelegate:[[MilskoCocoaWindowDelegate alloc] initWithWin:c->window]];
[c->window retain];
[c->window makeKeyAndOrderFront:c->application]; [c->window makeKeyAndOrderFront:c->application];
[c->window makeFirstResponder:c->view]; [c->window retain];
[c->window setAcceptsMouseMovedEvents:MwTRUE];
c->view = [[MilskoCocoaView alloc] initWithFrame:c->rect];
[c->view retain];
[c->window setContentView:c->view];
if (parent) {
MilskoCocoa *p = parent->cocoa.real;
double offset = 0;
offset = [p->window frameRectForContentRect:[p->window frame]].size.height -
[p->window contentRectForFrameRect:[p->window frame]].size.height;
[p->window addChildWindow:c->window ordered:NSWindowAbove];
[c->window setHasShadow:MwFALSE];
[c->window setParentWindow:p->window];
} else {
if ([c->application respondsToSelector:@selector(setActivationPolicy:)]) { if ([c->application respondsToSelector:@selector(setActivationPolicy:)]) {
[c->application [c->application
setActivationPolicy:0 /* NSApplicationActivationPolicyRegular */]; setActivationPolicy:0 /* NSApplicationActivationPolicyRegular */];
} }
[c->window makeKeyAndOrderFront:nil];
[c->application activateIgnoringOtherApps:true]; [c->application activateIgnoringOtherApps:true];
[c->window makeFirstResponder:c->view];
}
[c->application setDelegate:[[MilskoCocoaApplicationDelegate alloc] [c->application setDelegate:[[MilskoCocoaApplicationDelegate alloc]
initWithAppl:c->application]]; initWithAppl:c->application]];
[c->application retain]; [c->application retain];
} else {
MilskoCocoa* p = parent->cocoa.real;
c->window = p->window;
if(parent) {
MilskoCocoa* topmost = p;
NSRect rect = localRectFlip(c->rect, p->view);
printf("%0.2f %0.2f %0.2f %0.2f\n",c->rect.origin.x, c->rect.origin.y, c->rect.size.width, c->rect.size.height);
c->view = [[MilskoCocoaView alloc] initWithFrame:rect];
[c->view setBounds:c->rect];
} else {
c->view = [[MilskoCocoaView alloc] initWithFrame:c->rect]; c->view = [[MilskoCocoaView alloc] initWithFrame:c->rect];
}
[c->view retain]; [c->view retain];
[c->view setNeedsDisplay:TRUE];
[p->view addSubview:c->view];
[p->window setContentView:p->view];
}
c->handle = [[MilskoFakePointer alloc] initWithFrame:NSMakeRect(0, 0, 1, 1)]; c->handle = [[MilskoFakePointer alloc] initWithFrame:NSMakeRect(0, 0, 1, 1)];
[c->handle retain]; [c->handle retain];
[c->handle setPointer:r]; [c->handle setPointer:r];
[c->view addSubview:c->handle]; [c->view addSubview:c->handle];
c->modalSession = [c->application beginModalSessionForWindow:c->window]; [c->window setContentView:c->view];
c->parent = parent; c->parent = parent;
c->_forceRender = MwTRUE; c->_forceRender = MwTRUE;
@ -180,7 +181,7 @@ static NSRect localRectFlip(NSRect originFrame, NSView* view) {
[c->application finishLaunching]; [c->application finishLaunching];
c->pointerLocked = MwFALSE; c->pointerLocked = MwFALSE;
c->modalSession = [c->application beginModalSessionForWindow:c->window];
return c; return c;
} }
@ -193,9 +194,6 @@ static NSRect localRectFlip(NSRect originFrame, NSView* view) {
if (ctx) { if (ctx) {
int i; int i;
NSBezierPath *path = [NSBezierPath bezierPath]; NSBezierPath *path = [NSBezierPath bezierPath];
/* whatever rect we use for coordinate flipping */
NSRect _rect = parent ? [self->view bounds] : [self->window frame];
NSColor *nscolor = NSColor *nscolor =
[NSColor colorWithCalibratedRed:color->common.red / 255. [NSColor colorWithCalibratedRed:color->common.red / 255.
green:color->common.green / 255. green:color->common.green / 255.
@ -206,13 +204,14 @@ static NSRect localRectFlip(NSRect originFrame, NSView* view) {
[NSGraphicsContext setCurrentContext:ctx]; [NSGraphicsContext setCurrentContext:ctx];
[nscolor setFill]; [nscolor setFill];
NSRectFill(self->view.bounds);
for (i = 0; i < points_count; i++) { for (i = 0; i < points_count; i++) {
if (i == 0) { if (i == 0) {
[path moveToPoint:NSMakePoint(points[i].x, _rect.size.height - [path moveToPoint:NSMakePoint(points[i].x,
[self->window frame].size.height -
points[i].y)]; points[i].y)];
} else { } else {
[path lineToPoint:NSMakePoint(points[i].x, _rect.size.height - [path lineToPoint:NSMakePoint(points[i].x,
[self->window frame].size.height -
points[i].y)]; points[i].y)];
} }
} }
@ -231,14 +230,14 @@ static NSRect localRectFlip(NSRect originFrame, NSView* view) {
(void)color; (void)color;
}; };
- (void)getX:(int *)x Y:(int *)y W:(unsigned int *)w H:(unsigned int *)h { - (void)getX:(int *)x Y:(int *)y W:(unsigned int *)w H:(unsigned int *)h {
NSRect frame; NSRect frame = rectFlip([self->window frame]);
if(parent) {
frame = [self->view frame];
} else {
frame = [self->window frame];
}
frame = rectFlip(frame); frame = rectFlip(frame);
if (parent) {
frame.origin.x -= [parent->cocoa.real->window frame].origin.x;
frame.origin.y += [parent->cocoa.real->window frame].origin.y;
}
*x = frame.origin.x; *x = frame.origin.x;
*y = frame.origin.y; *y = frame.origin.y;
@ -246,23 +245,31 @@ static NSRect localRectFlip(NSRect originFrame, NSView* view) {
*h = frame.size.height; *h = frame.size.height;
}; };
- (void)setX:(int)x Y:(int)y { - (void)setX:(int)x Y:(int)y {
NSRect frame; NSRect frame = [self->window frame];
if(parent) {
frame = [self->view frame];
} else {
frame = [self->window frame];
}
frame.origin.x = x; frame.origin.x = x;
frame.origin.y = y; frame.origin.y = y;
if(!parent) {
frame = rectFlip(frame); frame = rectFlip(frame);
[self->window setFrame:frame display:TRUE animate:TRUE];
} else { if (parent) {
frame = localRectFlip(frame, parent->cocoa.real->view); double offset = 0;
[self->view setBounds:frame]; MilskoCocoaWindow *parentWindow = parent->cocoa.real->window;
offset =
[parentWindow frameRectForContentRect:[parentWindow frame]]
.size.height -
[parentWindow contentRectForFrameRect:[parentWindow frame]].size.height;
frame.origin.x += [parentWindow frame].origin.x;
frame.origin.y -= [parentWindow frame].origin.y - offset;
frame.origin.y -= offset;
} }
[self->view setFrameSize:frame.size];
[self->window setFrameOrigin:frame.origin];
[self forceRender];
}; };
- (void)setW:(int)w H:(int)h { - (void)setW:(int)w H:(int)h {
NSRect frame = [self->window frame]; NSRect frame = [self->window frame];
@ -272,26 +279,63 @@ static NSRect localRectFlip(NSRect originFrame, NSView* view) {
self->rect = frame; self->rect = frame;
[self->view setFrameSize:frame.size]; [self->view setFrameSize:frame.size];
if(self->parent) {
[self->view setFrame:frame];
} else {
[self->window setFrame:frame display:YES animate:false]; [self->window setFrame:frame display:YES animate:false];
} [self forceRender];
}; };
- (int)pending { - (int)pending {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[NSApp runModalSession:self->modalSession]; MwBool isPending = MwFALSE;
if (_forceRender) {
_forceRender = MwFALSE;
[pool release]; [pool release];
return 1; return 1;
}
self->lastEvent = [self->window nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast]
inMode:NSDefaultRunLoopMode
dequeue:YES];
[self->lastEvent retain];
if (!self->lastEvent) {
[NSApp runModalSession:self->modalSession];
}
isPending = self->lastEvent != NULL;
[pool release];
return isPending;
}; };
+ (void)eventCanceller:(MilskoCocoa *)this {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSEvent *event = [NSEvent otherEventWithType:NSApplicationDefined
location:NSMakePoint(0, 0)
modifierFlags:0
timestamp:0
windowNumber:0
context:nil
subtype:0
data1:0
data2:0];
this->lastEvent = [this->window currentEvent];
[this eventProcess:this->lastEvent];
[NSApp postEvent:event atStart:YES];
[pool release];
[[NSApplication sharedApplication] stop:nil];
usleep(1000);
}
- (void)getNextEvent { - (void)getNextEvent {
NSEvent *event;
[self eventProcess:self->lastEvent]; [self eventProcess:self->lastEvent];
if (_forceRender) { if (_forceRender) {
MwLL h = [self->handle pointer]; MwLL h = [self->handle pointer];
MwLLDispatch(h, draw, NULL); MwLLDispatch(h, draw, NULL);
[self->view setNeedsDisplay:true];
_forceRender = MwFALSE; _forceRender = MwFALSE;
} }
[self sendClipboardEvent]; [self sendClipboardEvent];
@ -327,7 +371,6 @@ static NSRect localRectFlip(NSRect originFrame, NSView* view) {
objectAtIndex:0]) pointer]; objectAtIndex:0]) pointer];
} }
switch ([ev type]) { switch ([ev type]) {
case NSLeftMouseDragged: case NSLeftMouseDragged:
case NSRightMouseDragged: case NSRightMouseDragged:
@ -338,7 +381,7 @@ static NSRect localRectFlip(NSRect originFrame, NSView* view) {
pos.x = pos_translated.x; pos.x = pos_translated.x;
pos.y = pos_translated.y; pos.y = pos_translated.y;
MwLLDispatch(h, move, &pos); MwLLDispatch(h, move, &pos);
break; // break;
} }
case NSLeftMouseDown: case NSLeftMouseDown:
case NSRightMouseDown: case NSRightMouseDown:
@ -410,8 +453,6 @@ static NSRect localRectFlip(NSRect originFrame, NSView* view) {
mouse.point.x = mousePoint.x; mouse.point.x = mousePoint.x;
mouse.point.y = mousePoint.y; mouse.point.y = mousePoint.y;
printf("%s at %d %d\n", isDown ? "down" : "up", mouse.point.x, mouse.point.y);
if (isDown) { if (isDown) {
MwLLDispatch(ll, down, &mouse); MwLLDispatch(ll, down, &mouse);
} else { } else {
@ -644,8 +685,7 @@ static NSRect localRectFlip(NSRect originFrame, NSView* view) {
[self->cursorPixmap destroy]; [self->cursorPixmap destroy];
} }
self->cursorPixmap = self->cursorPixmap =
[MilskoCocoaPixmap newWithWidth:image->width [MilskoCocoaPixmap newWithWidth:image->width height:image->height];
height:image->height];
xs = -mask->x + image->x; xs = -mask->x + image->x;
ys = MwCursorDataHeight + mask->y; ys = MwCursorDataHeight + mask->y;
@ -808,9 +848,6 @@ static NSRect localRectFlip(NSRect originFrame, NSView* view) {
- (void)drawRect:(NSRect)dirtyRect { - (void)drawRect:(NSRect)dirtyRect {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSSize sz = [self->rep size]; NSSize sz = [self->rep size];
MwLL ll = [((MilskoFakePointer *)[[[[self window] contentView] subviews]
objectAtIndex:0]) pointer];
[super drawRect:dirtyRect]; [super drawRect:dirtyRect];
if (!self->rep) { if (!self->rep) {
if (dirtyRect.size.width && dirtyRect.size.height) { if (dirtyRect.size.width && dirtyRect.size.height) {
@ -824,17 +861,9 @@ static NSRect localRectFlip(NSRect originFrame, NSView* view) {
} }
} }
// MwLLDispatch(ll, draw, NULL); [self->rep drawInRect:NSMakeRect(0, 0, sz.width, sz.height)];
[self->rep drawInRect:dirtyRect];
for(NSView* subview in [self subviews]) {
NSRect bounds = [subview bounds];
[subview drawRect:bounds];
}
[self->rep bitmapData]; [self->rep bitmapData];
[pool release]; [pool release];
} }
@ -913,7 +942,7 @@ static NSRect localRectFlip(NSRect originFrame, NSView* view) {
MilskoFakePointer *ptr = [[[win contentView] subviews] objectAtIndex:0]; MilskoFakePointer *ptr = [[[win contentView] subviews] objectAtIndex:0];
MwLL h = [ptr pointer]; MwLL h = [ptr pointer];
MwLLDispatch(h, resize, NULL); // MwLLDispatch(h, resize, NULL);
MwLLDispatch(h, draw, NULL); MwLLDispatch(h, draw, NULL);
} }
return frameSize; return frameSize;
@ -978,6 +1007,11 @@ static NSRect localRectFlip(NSRect originFrame, NSView* view) {
static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) { static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) {
MwLL r; MwLL r;
(void)x;
(void)y;
(void)width;
(void)height;
r = malloc(sizeof(*r)); r = malloc(sizeof(*r));
MwLLCreateCommon(r); MwLLCreateCommon(r);
@ -1003,13 +1037,9 @@ static void MwLLDestroyImpl(MwLL handle) {
free(handle); free(handle);
} }
static void MwLLBeginDrawImpl(MwLL handle) { static void MwLLBeginDrawImpl(MwLL handle) { (void)handle; }
(void)handle;
}
static void MwLLEndDrawImpl(MwLL handle) { static void MwLLEndDrawImpl(MwLL handle) { (void)handle; }
(void)handle;
}
static void MwLLPolygonImpl(MwLL handle, MwPoint *points, int points_count, static void MwLLPolygonImpl(MwLL handle, MwPoint *points, int points_count,
MwLLColor color) { MwLLColor color) {
@ -1052,9 +1082,7 @@ static void MwLLSetWHImpl(MwLL handle, int w, int height) {
[h setW:w H:height]; [h setW:w H:height];
} }
static void MwLLFreeColorImpl(MwLLColor color) { static void MwLLFreeColorImpl(MwLLColor color) { free(color); }
free(color);
}
static int MwLLPendingImpl(MwLL handle) { static int MwLLPendingImpl(MwLL handle) {
int p = [handle->cocoa.real pending]; int p = [handle->cocoa.real pending];
@ -1167,9 +1195,7 @@ static void MwLLSetClipboardImpl(MwLL handle, const char* text) {
[h setClipboard:text]; [h setClipboard:text];
} }
static void MwLLGetClipboardImpl(MwLL handle) { static void MwLLGetClipboardImpl(MwLL handle) { (void)handle; }
(void)handle;
}
static void MwLLMakeToolWindowImpl(MwLL handle) { static void MwLLMakeToolWindowImpl(MwLL handle) {
@ -1187,17 +1213,11 @@ static void MwLLGetScreenSizeImpl(MwLL handle, MwRect* rect) {
[h getScreenSize:rect]; [h getScreenSize:rect];
} }
static void MwLLBeginStateChangeImpl(MwLL handle) { static void MwLLBeginStateChangeImpl(MwLL handle) { MwLLShow(handle, 0); }
MwLLShow(handle, 0);
}
static void MwLLEndStateChangeImpl(MwLL handle) { static void MwLLEndStateChangeImpl(MwLL handle) { MwLLShow(handle, 1); }
MwLLShow(handle, 1);
}
static int MwLLCocoaCallInitImpl(void) { static int MwLLCocoaCallInitImpl(void) { return 0; }
return 0;
}
#include "call.c" #include "call.c"
CALL(Cocoa); CALL(Cocoa);