Compare commits

...

2 commits

Author SHA1 Message Date
04f1a94cb5 cocoa: attempted drawing 2026-01-14 17:13:12 -07:00
a710967115 cocoa: move interfaces to header (guarded by ifdef __OBJC__) 2026-01-14 13:47:49 -07:00
3 changed files with 199 additions and 78 deletions

View file

@ -6,6 +6,7 @@
#ifndef __MW_LOWLEVEL_H__
#define __MW_LOWLEVEL_H__
#include "Mw/BaseTypes.h"
#include <Mw/MachDep.h>
typedef struct _MwLLHandler* MwLLHandler;
@ -40,9 +41,9 @@ struct _MwLLCommon {
};
struct _MwLLCommonColor {
int red;
int green;
int blue;
MwU8 red;
MwU8 green;
MwU8 blue;
};
struct _MwLLCommonPixmap {

View file

@ -10,11 +10,100 @@
#include <Mw/MachDep.h>
#include <Mw/TypeDefs.h>
#ifdef __OBJC__
#import <Foundation/NSGeometry.h>
#import <AppKit/AppKit.h>
#import <Foundation/Foundation.h>
#ifdef __APPLE__
#import <CoreServices/CoreServices.h>
#else
#import <CoreGraphics/CoreGraphics.h>
#endif
@interface MilskoCocoaPixmap : NSObject {
int width;
int height;
NSData* data;
NSImage* image;
}
+ (MilskoCocoaPixmap*)newWithWidth:(int)width height:(int)height;
- (void)updateWithData:(void*)data;
- (void)destroy;
/* using @property to create instance variables fucks up 10.4 gcc for some reason? */
- (NSImage*)image;
@end
@interface MilskoCocoaView : NSView {
CGContextRef cg;
CGColorSpaceRef space;
CGDataProviderRef provider;
MwU32* buf;
float width;
float height;
}
- (CGContextRef)context;
@end
@interface MilskoCocoa : NSObject {
NSApplication* application;
NSWindow* window;
NSRect rect;
MilskoCocoaView* view;
}
+ (MilskoCocoa*)newWithParent:(MwLL)parent
x:(int)x
y:(int)y
width:(int)width
height:(int)height;
- (void)polygonWithPoints:(MwPoint*)points
points_count:(int)points_count
color:(MwLLColor)color;
- (void)lineWithPoints:(MwPoint*)points color:(MwLLColor)color;
- (void)getX:(int*)x Y:(int*)y W:(unsigned int*)w H:(unsigned int*)h;
- (void)setX:(int)x Y:(int)y;
- (void)setW:(int)w H:(int)h;
- (int)pending;
- (void)getNextEvent;
- (void)setTitle:(const char*)title;
- (void)drawPixmap:(MwLLPixmap)pixmap rect:(MwRect*)rect;
- (void)setIcon:(MwLLPixmap)pixmap;
- (void)forceRender;
- (void)setCursor:(MwCursor*)image mask:(MwCursor*)mask;
- (void)detachWithPoint:(MwPoint*)point;
- (void)show:(int)show;
- (void)makePopupWithParent:(MwLL)parent;
- (void)setSizeHintsWithMinX:(int)minx
MinY:(int)miny
MaxX:(int)maxx
MaxY:(int)maxy;
- (void)makeBorderless:(int)toggle;
- (void)focus;
- (void)grabPointer:(int)toggle;
- (void)setClipboard:(const char*)text;
- (void)makeToolWindow;
- (void)getCursorCoord:(MwPoint*)point;
- (void)getScreenSize:(MwRect*)rect;
- (void)destroy;
@end
#define OBJC(x) x
#else
#define OBJC(x) void*
#endif
MWDECL int MwLLCocoaCallInit(void);
struct _MwLLCocoa {
struct _MwLLCommon common;
void* real;
OBJC(MilskoCocoa*)
real;
};
struct _MwLLCocoaColor {
@ -23,7 +112,8 @@ struct _MwLLCocoaColor {
struct _MwLLCocoaPixmap {
struct _MwLLCommonPixmap common;
void* real;
OBJC(MilskoCocoaPixmap*)
real;
};
#endif

View file

@ -1,86 +1,41 @@
#ifndef USE_COCOA
#define USE_COCOA
#include <Foundation/NSGeometry.h>
#include <Foundation/NSObjCRuntime.h>
#endif
#import <AppKit/AppKit.h>
#import <AppKit/NSEvent.h>
#import <Foundation/Foundation.h>
#include <AppKit/NSGraphicsContext.h>
#include <Mw/Milsko.h>
#include <assert.h>
#include <limits.h>
#include <stdint.h>
#include "../../external/stb_ds.h"
@interface MilskoCocoaPixmap : NSObject {
NSImage* image;
}
+ (MilskoCocoaPixmap*)newWithWidth:(int)width height:(int)height;
- (void)updateWithData:(void*)data;
- (void)destroy;
@end
#include "Mw/BaseTypes.h"
@implementation MilskoCocoaPixmap
+ (MilskoCocoaPixmap*)newWithWidth:(int)width height:(int)height {
MilskoCocoaPixmap* p = [MilskoCocoaPixmap alloc];
NSSize sz;
sz.width = width;
sz.height = height;
p->image = [[NSImage alloc] initWithSize:sz];
p->width = width;
p->height = height;
p->data = NULL;
p->image = NULL;
return p;
}
- (void)updateWithData:(void*)data {
- (void)updateWithData:(void*)_data {
[self destroy];
self->data = [NSData dataWithBytes:_data length:self->width * self->height * 4];
self->image = [[NSImage alloc] initWithData:self->data];
}
- (void)destroy {
if(self->data != NULL) {
[self->data dealloc];
}
if(self->image != NULL) {
[self->image dealloc];
}
@end
@interface MilskoCocoa : NSObject {
NSApplication* application;
NSWindow* window;
NSRect rect;
}
+ (MilskoCocoa*)newWithParent:(MwLL)parent
x:(int)x
y:(int)y
width:(int)width
height:(int)height;
- (void)polygonWithPoints:(MwPoint*)points
points_count:(int)points_count
color:(MwLLColor)color;
- (void)lineWithPoints:(MwPoint*)points color:(MwLLColor)color;
- (void)getX:(int*)x Y:(int*)y W:(unsigned int*)w H:(unsigned int*)h;
- (void)setX:(int)x Y:(int)y;
- (void)setW:(int)w H:(int)h;
- (int)pending;
- (void)getNextEvent;
- (void)setTitle:(const char*)title;
- (void)drawPixmap:(MwLLPixmap)pixmap rect:(MwRect*)rect;
- (void)setIcon:(MwLLPixmap)pixmap;
- (void)forceRender;
- (void)setCursor:(MwCursor*)image mask:(MwCursor*)mask;
- (void)detachWithPoint:(MwPoint*)point;
- (void)show:(int)show;
- (void)makePopupWithParent:(MwLL)parent;
- (void)setSizeHintsWithMinX:(int)minx
MinY:(int)miny
MaxX:(int)maxx
MaxY:(int)maxy;
- (void)makeBorderless:(int)toggle;
- (void)focus;
- (void)grabPointer:(int)toggle;
- (void)setClipboard:(const char*)text;
- (void)makeToolWindow;
- (void)getCursorCoord:(MwPoint*)point;
- (void)getScreenSize:(MwRect*)rect;
- (void)destroy;
- (NSImage*)image {
return self->image;
}
@end
@implementation MilskoCocoa
@ -119,13 +74,32 @@
if(parent != NULL) {
MilskoCocoa* p = parent->cocoa.real;
[p->window addChildWindow:c->window ordered:NSWindowAbove];
} else {
[c->application activateIgnoringOtherApps:true];
}
c->view = [[MilskoCocoaView alloc] initWithFrame:c->rect];
[c->window setContentView:c->view];
return c;
}
- (void)polygonWithPoints:(MwPoint*)points
points_count:(int)points_count
color:(MwLLColor)color {
int i;
CGContextRef cg = [self->view context];
CGContextSetRGBFillColor(cg, color->common.red / 255., color->common.blue / 255., color->common.green / 255., 1);
CGContextBeginPath(cg);
for(i = 0; i < points_count; i++) {
CGContextMoveToPoint(cg, points[i].x, points[i].y);
if(i < points_count - 1) {
CGContextAddLineToPoint(cg, points[i + 1].x, points[i + 1].y);
}
}
CGContextFillPath(cg);
// [self->view setNeedsDisplay:true];
};
- (void)lineWithPoints:(MwPoint*)points color:(MwLLColor)color {
};
@ -157,6 +131,7 @@
};
- (void)getNextEvent {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSEvent* event = [self->application nextEventMatchingMask:NSAnyEventMask
untilDate:nil
inMode:NSDefaultRunLoopMode
@ -165,6 +140,12 @@
if(event != nil) {
printf("got event: %p\n", event);
}
[self->application sendEvent:event];
/* this should be in the draw functions but it's here for now for testing */
[self->view setNeedsDisplay:true];
[pool release];
};
- (void)setTitle:(const char*)title {
@ -173,7 +154,9 @@
setTitleWithRepresentedFilename:[NSString stringWithUTF8String:title]];
[pool release];
};
- (void)drawPixmap:(MwLLPixmap)pixmap rect:(MwRect*)rect {
- (void)drawPixmap:(MwLLPixmap)pixmap rect:(MwRect*)_rect {
MilskoCocoaPixmap* p = pixmap->cocoa.real;
[[p image] drawAtPoint:NSMakePoint(_rect->x, _rect->y) fromRect:NSMakeRect(_rect->x, _rect->y, _rect->width, _rect->height) operation:NSCompositeClear fraction:1.0];
};
- (void)setIcon:(MwLLPixmap)pixmap {
};
@ -234,6 +217,53 @@
- (void)destroy {
[self->window dealloc];
}
@end
@implementation MilskoCocoaView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
self->width = frame.size.width;
self->height = frame.size.height;
self->buf = malloc(self->width * self->height * 4);
self->space = CGColorSpaceCreateDeviceRGB();
self->cg = CGBitmapContextCreate(self->buf,
self->width,
self->height,
CHAR_BIT,
self->width * sizeof(uint32_t),
self->space, kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedLast);
assert(self->cg);
printf("%p\n", self->cg);
return self;
}
- (CGContextRef)context {
return self->cg;
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
if(self) {
CGImageRef img = CGBitmapContextCreateImage(self->cg);
if(!img) {
return;
}
printf("printed image\n");
CGContextDrawImage([[NSGraphicsContext currentContext] graphicsPort], CGRectMake(0, 0, self->width, self->height), img);
CGImageRelease(img);
}
// printf("%0.2f %0.2f %0.2f %0.2f\n", dirtyRect.origin.x, dirtyRect.origin.y, dirtyRect.size.width, dirtyRect.size.height);
}
- (void)destroy {
free(self->buf);
CGContextRelease(self->cg);
CGColorSpaceRelease(self->space);
}
@end
static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) {