From 1d091637d8a958b648fe77dd1ce5a9760737dfc1 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Wed, 18 Feb 2015 23:04:14 -0500 Subject: Migrated the Mac OS X Table implementation. Untested due to VM issues. --- image_darwin.m | 31 +++++++++++++++++++++++++++++++ imagelist_darwin.m | 30 ------------------------------ objc_darwin.h | 4 ++-- table_darwin.go | 11 ++++++----- table_darwin.m | 1 + 5 files changed, 40 insertions(+), 37 deletions(-) create mode 100644 image_darwin.m delete mode 100644 imagelist_darwin.m diff --git a/image_darwin.m b/image_darwin.m new file mode 100644 index 0000000..1b77e22 --- /dev/null +++ b/image_darwin.m @@ -0,0 +1,31 @@ +// 16 august 2014 + +#import "objc_darwin.h" +#import + +#define toNSInteger(x) ((NSInteger) (x)) + +id toTableImage(void *pixels, intptr_t width, intptr_t height, intptr_t stride) +{ + NSBitmapImageRep *bitmap; + NSImage *image; + + // we can't just hand it pixels; we need to make a copy + bitmap = [[NSBitmapImageRep alloc] + initWithBitmapDataPlanes:NULL + pixelsWide:toNSInteger(width) + pixelsHigh:toNSInteger(height) + bitsPerSample:8 + samplesPerPixel:4 + hasAlpha:YES + isPlanar:NO + colorSpaceName:NSDeviceRGBColorSpace + bitmapFormat:0 + bytesPerRow:toNSInteger(stride) + bitsPerPixel:32]; + memcpy((void *) [bitmap bitmapData], pixels, [bitmap bytesPerPlane]); + image = [[NSImage alloc] initWithSize:NSMakeSize((CGFloat) width, (CGFloat) height)]; + [image addRepresentation:bitmap]; + // TODO release bitmap? + return (id) image; +} diff --git a/imagelist_darwin.m b/imagelist_darwin.m deleted file mode 100644 index 7433b12..0000000 --- a/imagelist_darwin.m +++ /dev/null @@ -1,30 +0,0 @@ -// 16 august 2014 - -#import "objc_darwin.h" -#import - -#define toNSInteger(x) ((NSInteger) (x)) - -id toImageListImage(void *pixels, intptr_t width, intptr_t height, intptr_t stride) -{ - NSBitmapImageRep *bitmap; - NSImage *image; - - // we can't just hand it pixels; we need to make a copy - bitmap = [[NSBitmapImageRep alloc] - initWithBitmapDataPlanes:NULL - pixelsWide:toNSInteger(width) - pixelsHigh:toNSInteger(height) - bitsPerSample:8 - samplesPerPixel:4 - hasAlpha:YES - isPlanar:NO - colorSpaceName:NSDeviceRGBColorSpace - bitmapFormat:0 - bytesPerRow:toNSInteger(stride) - bitsPerPixel:32]; - memcpy((void *) [bitmap bitmapData], pixels, [bitmap bytesPerPlane]); - image = [[NSImage alloc] initWithSize:NSMakeSize((CGFloat) width, (CGFloat) height)]; - [image addRepresentation:bitmap]; - return (id) image; -} diff --git a/objc_darwin.h b/objc_darwin.h index e8b7752..388f43f 100644 --- a/objc_darwin.h +++ b/objc_darwin.h @@ -146,8 +146,8 @@ extern void areaEndTextFieldEditing(id, id); /* common_darwin.m */ extern void disableAutocorrect(id); -/* imagerep_darwin.m */ -extern id toImageListImage(void *, intptr_t, intptr_t, intptr_t); +/* image_darwin.m */ +extern id toTableImage(void *, intptr_t, intptr_t, intptr_t); /* dialog_darwin.m */ extern void openFile(id, void *); diff --git a/table_darwin.go b/table_darwin.go index 60a3614..23482fb 100644 --- a/table_darwin.go +++ b/table_darwin.go @@ -6,6 +6,7 @@ import ( "fmt" "reflect" "unsafe" + "image" ) // #include "objc_darwin.h" @@ -16,7 +17,6 @@ type table struct { *scroller - images []C.id selected *event } @@ -35,7 +35,7 @@ func finishNewTable(b *tablebase, ty reflect.Type) Table { coltype := C.colTypeText editable := false switch { - case ty.Field(i).Type == reflect.TypeOf(ImageIndex(0)): + case ty.Field(i).Type == reflect.TypeOf((*image.RGBA)(nil)): coltype = C.colTypeImage case ty.Field(i).Type.Kind() == reflect.Bool: coltype = C.colTypeCheckbox @@ -88,10 +88,11 @@ func goTableDataSource_getValue(data unsafe.Pointer, row C.intptr_t, col C.intpt d := reflect.Indirect(reflect.ValueOf(t.data)) datum := d.Index(int(row)).Field(int(col)) switch { - case datum.Type() == reflect.TypeOf(ImageIndex(0)): + case datum.Type() == reflect.TypeOf((*image.RGBA)(nil)): *outtype = C.colTypeImage - d := datum.Interface().(ImageIndex) - return unsafe.Pointer(t.images[d]) + d := datum.Interface().(*image.RGBA) + img := C.toTableImage(unsafe.Pointer(pixelData(img)), C.intptr_t(d.Rect.Dx()), C.intptr_t(d.Rect.Dy()), C.intptr_t(d.Stride)) + return unsafe.Pointer(img) case datum.Kind() == reflect.Bool: *outtype = C.colTypeCheckbox if datum.Bool() == true { diff --git a/table_darwin.m b/table_darwin.m index 5e7a9e6..f04aae8 100644 --- a/table_darwin.m +++ b/table_darwin.m @@ -42,6 +42,7 @@ ret = goTableDataSource_getValue(self->gotable, (intptr_t) row, colnum, &type); switch (type) { case colTypeImage: + // TODO free the returned image when done somehow return (id) ret; case colTypeCheckbox: if (ret == NULL) -- cgit v1.2.3