Removed the Area drawing code from bleh_darwin.m and rewrote it in Objective-C in area_darwin.m.

This commit is contained in:
Pietro Gagliardi 2014-05-15 22:15:54 -04:00
parent 31fc77a99d
commit 524867574b
5 changed files with 32 additions and 45 deletions

View File

@ -39,8 +39,8 @@ func areaView_drawRect(self C.id, rect C.struct_xrect) {
}
i := s.handler.Paint(cliprect)
C.drawImage(
unsafe.Pointer(pixelData(i)), C.int64_t(i.Rect.Dx()), C.int64_t(i.Rect.Dy()), C.int64_t(i.Stride),
C.int64_t(cliprect.Min.X), C.int64_t(cliprect.Min.Y))
unsafe.Pointer(pixelData(i)), C.intptr_t(i.Rect.Dx()), C.intptr_t(i.Rect.Dy()), C.intptr_t(i.Stride),
C.intptr_t(cliprect.Min.X), C.intptr_t(cliprect.Min.Y))
}
func parseModifiers(e C.id) (m Modifiers) {

View File

@ -1,6 +1,7 @@
/* 13 may 2014 */
extern id makeArea(void);
extern void drawImage(void *, intptr_t, intptr_t, intptr_t, intptr_t, intptr_t);
extern uintptr_t modifierFlags(id);
extern struct xpoint getTranslatedEventPoint(id, id);
extern intptr_t buttonNumber(id);

View File

@ -7,6 +7,7 @@
#include <AppKit/NSTrackingArea.h>
#include <Foundation/NSGeometry.h>
#include <AppKit/NSEvent.h>
#include <AppKit/NSBitmapImageRep.h>
#define to(T, x) ((T *) (x))
#define toNSEvent(x) to(NSEvent, (x))
@ -106,6 +107,34 @@ id makeArea(void)
initWithFrame:NSMakeRect(0, 0, 100, 100)];
}
void drawImage(void *pixels, intptr_t width, intptr_t height, intptr_t stride, intptr_t xdest, intptr_t ydest)
{
unsigned char *planes[1]; // NSBitmapImageRep wants an array of planes; we have one plane
NSBitmapImageRep *bitmap;
planes[0] = (unsigned char *) pixels;
bitmap = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:planes
pixelsWide:toNSInteger(width)
pixelsHigh:toNSInteger(height)
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace // TODO NSDeviceRGBColorSpace?
bitmapFormat:0 // this is where the flag for placing alpha first would go if alpha came first; the default is alpha last, which is how we're doing things (otherwise the docs say "Color planes are arranged in the standard order—for example, red before green before blue for RGB color."); this is also where the flag for non-premultiplied colors would go if we used it (the default is alpha-premultiplied)
bytesPerRow:toNSInteger(stride)
bitsPerPixel:32];
// TODO this CAN fali; check error
[bitmap drawInRect:NSMakeRect((CGFloat) xdest, (CGFloat) ydest, (CGFloat) width, (CGFloat) height)
fromRect:NSZeroRect // draw whole image
operation:NSCompositeSourceOver
fraction:1.0
respectFlipped:YES
hints:nil];
[bitmap release];
}
uintptr_t modifierFlags(id e)
{
return fromNSUInteger([toNSEvent(e) modifierFlags]);

View File

@ -109,45 +109,3 @@ because @encode() is NOT A LITERAL, we're going to just stick it all the way bac
see also: http://stackoverflow.com/questions/6812035/adding-methods-dynamically
*/
char *encodedNSRect = @encode(NSRect);
/*
the NSBitmapImageRep constructor is complex; put it here
the only way to draw a NSBitmapImageRep in a flipped NSView is to use the most complex drawing method; put it here too
*/
/*
hey guys you know what's fun? 32-bit ABI changes!
*/
static BOOL (*objc_msgSend_drawInRect)(id, SEL, NSRect, NSRect, NSCompositingOperation, CGFloat, BOOL, id) =
(BOOL (*)(id, SEL, NSRect, NSRect, NSCompositingOperation, CGFloat, BOOL, id)) objc_msgSend;
void drawImage(void *pixels, int64_t width, int64_t height, int64_t stride, int64_t xdest, int64_t ydest)
{
unsigned char *planes[1]; /* NSBitmapImageRep wants an array of planes; we have one plane */
id bitmap;
bitmap = objc_msgSend(c_NSBitmapImageRep, s_alloc);
planes[0] = (unsigned char *) pixels;
bitmap = objc_msgSend(bitmap, s_initWithBitmapDataPlanes,
planes, /* initWithBitmapDataPlanes: */
(NSInteger) width, /* pixelsWide: */
(NSInteger) height, /* pixelsHigh: */
(NSInteger) 8, /* bitsPerSample: */
(NSInteger) 4, /* samplesPerPixel: */
(BOOL) YES, /* hasAlpha: */
(BOOL) NO, /* isPlanar: */
NSCalibratedRGBColorSpace, /* colorSpaceName: | TODO NSDeviceRGBColorSpace? */
(NSBitmapFormat) 0, /* bitmapFormat: | this is where the flag for placing alpha first would go if alpha came first; the default is alpha last, which is how we're doing things (otherwise the docs say "Color planes are arranged in the standard order—for example, red before green before blue for RGB color."); this is also where the flag for non-premultiplied colors would go if we used it (the default is alpha-premultiplied) */
(NSInteger) stride, /* bytesPerRow: */
(NSInteger) 32); /* bitsPerPixel: */
/* TODO this CAN fail; check error */
objc_msgSend_drawInRect(bitmap, s_drawInRect,
NSMakeRect((CGFloat) xdest, (CGFloat) ydest,
(CGFloat) width, (CGFloat) height), /* drawInRect: */
NSZeroRect, /* fromRect: | draw whole image */
(NSCompositingOperation) NSCompositeSourceOver, /* op: */
(CGFloat) 1.0, /* fraction: */
(BOOL) YES, /* respectFlipped: */
nil); /* hints: */
objc_msgSend(bitmap, s_release);
}

View File

@ -56,7 +56,6 @@ extern id makeDummyEvent();
/* for area_darwin.go */
/* TODO apparently ISO C forbids casting a function pointer to a non-function pointer; this will need to change???? */
extern void *_areaView_drawRect;
extern void drawImage(void *, int64_t, int64_t, int64_t, int64_t, int64_t);
/* for objc_darwin.go */
extern char *encodedNSRect;