mac: convert child windows to subviews

This commit is contained in:
IoI_xD 2026-03-16 16:08:25 -07:00
commit c64d7778cd
2 changed files with 947 additions and 966 deletions

View file

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

View file

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