2015-12-30 06:41:46 +00:00
|
|
|
#if defined(Hiro_Canvas)
|
|
|
|
|
2013-03-15 13:11:33 +00:00
|
|
|
@implementation CocoaCanvas : NSImageView
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
-(id) initWith:(hiro::mCanvas&)canvasReference {
|
2013-03-15 13:11:33 +00:00
|
|
|
if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
|
|
|
|
canvas = &canvasReference;
|
|
|
|
[self setEditable:NO]; //disable image drag-and-drop functionality
|
2013-11-28 10:29:01 +00:00
|
|
|
NSTrackingArea* area = [[[NSTrackingArea alloc] initWithRect:[self frame]
|
2013-03-15 13:11:33 +00:00
|
|
|
options:NSTrackingMouseMoved | NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow | NSTrackingInVisibleRect
|
|
|
|
owner:self userInfo:nil
|
|
|
|
] autorelease];
|
|
|
|
[self addTrackingArea:area];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2013-07-29 09:42:45 +00:00
|
|
|
-(NSDragOperation) draggingEntered:(id<NSDraggingInfo>)sender {
|
|
|
|
return DropPathsOperation(sender);
|
|
|
|
}
|
|
|
|
|
|
|
|
-(BOOL) performDragOperation:(id<NSDraggingInfo>)sender {
|
|
|
|
lstring paths = DropPaths(sender);
|
2016-05-04 10:07:13 +00:00
|
|
|
if(!paths) return NO;
|
2015-12-30 06:41:46 +00:00
|
|
|
canvas->doDrop(paths);
|
2013-07-29 09:42:45 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2013-03-21 12:59:01 +00:00
|
|
|
-(void) mouseButton:(NSEvent*)event down:(BOOL)isDown {
|
2016-01-08 09:23:46 +00:00
|
|
|
if(isDown) {
|
2013-03-15 13:11:33 +00:00
|
|
|
switch([event buttonNumber]) {
|
2016-01-08 09:23:46 +00:00
|
|
|
case 0: return canvas->doMousePress(hiro::Mouse::Button::Left);
|
|
|
|
case 1: return canvas->doMousePress(hiro::Mouse::Button::Right);
|
|
|
|
case 2: return canvas->doMousePress(hiro::Mouse::Button::Middle);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch([event buttonNumber]) {
|
|
|
|
case 0: return canvas->doMouseRelease(hiro::Mouse::Button::Left);
|
|
|
|
case 1: return canvas->doMouseRelease(hiro::Mouse::Button::Right);
|
|
|
|
case 2: return canvas->doMouseRelease(hiro::Mouse::Button::Middle);
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 12:59:01 +00:00
|
|
|
-(void) mouseExited:(NSEvent*)event {
|
2015-12-30 06:41:46 +00:00
|
|
|
canvas->doMouseLeave();
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 12:59:01 +00:00
|
|
|
-(void) mouseMove:(NSEvent*)event {
|
2013-03-15 13:11:33 +00:00
|
|
|
if([event window] == nil) return;
|
|
|
|
NSPoint location = [self convertPoint:[event locationInWindow] fromView:nil];
|
2015-12-30 06:41:46 +00:00
|
|
|
canvas->doMouseMove({(int)location.x, (int)([self frame].size.height - 1 - location.y)});
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 12:59:01 +00:00
|
|
|
-(void) mouseDown:(NSEvent*)event {
|
2013-03-15 13:11:33 +00:00
|
|
|
[self mouseButton:event down:YES];
|
|
|
|
}
|
|
|
|
|
2013-03-21 12:59:01 +00:00
|
|
|
-(void) mouseUp:(NSEvent*)event {
|
2013-03-15 13:11:33 +00:00
|
|
|
[self mouseButton:event down:NO];
|
|
|
|
}
|
|
|
|
|
2013-03-21 12:59:01 +00:00
|
|
|
-(void) mouseDragged:(NSEvent*)event {
|
2013-03-15 13:11:33 +00:00
|
|
|
[self mouseMove:event];
|
|
|
|
}
|
|
|
|
|
2013-03-21 12:59:01 +00:00
|
|
|
-(void) rightMouseDown:(NSEvent*)event {
|
2013-03-15 13:11:33 +00:00
|
|
|
[self mouseButton:event down:YES];
|
|
|
|
}
|
|
|
|
|
2013-03-21 12:59:01 +00:00
|
|
|
-(void) rightMouseUp:(NSEvent*)event {
|
2013-03-15 13:11:33 +00:00
|
|
|
[self mouseButton:event down:NO];
|
|
|
|
}
|
|
|
|
|
2013-03-21 12:59:01 +00:00
|
|
|
-(void) rightMouseDragged:(NSEvent*)event {
|
2013-03-15 13:11:33 +00:00
|
|
|
[self mouseMove:event];
|
|
|
|
}
|
|
|
|
|
2013-03-21 12:59:01 +00:00
|
|
|
-(void) otherMouseDown:(NSEvent*)event {
|
2013-03-15 13:11:33 +00:00
|
|
|
[self mouseButton:event down:YES];
|
|
|
|
}
|
|
|
|
|
2013-03-21 12:59:01 +00:00
|
|
|
-(void) otherMouseUp:(NSEvent*)event {
|
2013-03-15 13:11:33 +00:00
|
|
|
[self mouseButton:event down:NO];
|
|
|
|
}
|
|
|
|
|
2013-03-21 12:59:01 +00:00
|
|
|
-(void) otherMouseDragged:(NSEvent*)event {
|
2013-03-15 13:11:33 +00:00
|
|
|
[self mouseMove:event];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
namespace hiro {
|
|
|
|
|
|
|
|
auto pCanvas::construct() -> void {
|
|
|
|
@autoreleasepool {
|
|
|
|
cocoaView = cocoaCanvas = [[CocoaCanvas alloc] initWith:self()];
|
|
|
|
pWidget::construct();
|
|
|
|
|
|
|
|
setDroppable(state().droppable);
|
|
|
|
}
|
|
|
|
}
|
2013-03-15 13:11:33 +00:00
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pCanvas::destruct() -> void {
|
|
|
|
@autoreleasepool {
|
2016-01-07 08:14:33 +00:00
|
|
|
[cocoaView removeFromSuperview];
|
2015-12-30 06:41:46 +00:00
|
|
|
[cocoaView release];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pCanvas::minimumSize() const -> Size {
|
2016-01-08 09:23:46 +00:00
|
|
|
if(auto& icon = state().icon) return {(int)icon.width(), (int)icon.height()};
|
2015-12-30 06:41:46 +00:00
|
|
|
return {0, 0};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pCanvas::setColor(Color color) -> void {
|
2016-01-08 09:23:46 +00:00
|
|
|
update();
|
2015-12-30 06:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto pCanvas::setDroppable(bool droppable) -> void {
|
2013-07-29 09:42:45 +00:00
|
|
|
@autoreleasepool {
|
|
|
|
if(droppable) {
|
|
|
|
[cocoaCanvas registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
|
|
|
|
} else {
|
|
|
|
[cocoaCanvas unregisterDraggedTypes];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pCanvas::setGeometry(Geometry geometry) -> void {
|
2013-11-28 10:29:01 +00:00
|
|
|
pWidget::setGeometry(geometry);
|
2016-01-08 09:23:46 +00:00
|
|
|
update();
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pCanvas::setGradient(Gradient gradient) -> void {
|
2016-01-08 09:23:46 +00:00
|
|
|
update();
|
2013-11-28 10:29:01 +00:00
|
|
|
}
|
2013-03-15 13:11:33 +00:00
|
|
|
|
2016-01-07 08:14:33 +00:00
|
|
|
auto pCanvas::setIcon(const image& icon) -> void {
|
2016-01-08 09:23:46 +00:00
|
|
|
update();
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pCanvas::update() -> void {
|
2016-01-08 09:23:46 +00:00
|
|
|
_rasterize();
|
|
|
|
@autoreleasepool {
|
|
|
|
[cocoaView setNeedsDisplay:YES];
|
|
|
|
}
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pCanvas::_rasterize() -> void {
|
2013-11-28 10:29:01 +00:00
|
|
|
@autoreleasepool {
|
2016-01-08 09:23:46 +00:00
|
|
|
int width = 0;
|
|
|
|
int height = 0;
|
|
|
|
|
|
|
|
if(auto& icon = state().icon) {
|
|
|
|
width = icon.width();
|
|
|
|
height = icon.height();
|
|
|
|
} else {
|
|
|
|
width = pSizable::state().geometry.width();
|
|
|
|
height = pSizable::state().geometry.height();
|
|
|
|
}
|
|
|
|
if(width <= 0 || height <= 0) return;
|
2013-11-28 10:29:01 +00:00
|
|
|
|
|
|
|
if(width != surfaceWidth || height != surfaceHeight) {
|
2016-01-08 09:23:46 +00:00
|
|
|
[cocoaView setImage:nil];
|
|
|
|
[surface release];
|
|
|
|
surface = nullptr;
|
|
|
|
bitmap = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
surfaceWidth = width;
|
|
|
|
surfaceHeight = height;
|
|
|
|
|
|
|
|
if(!surface) {
|
|
|
|
surface = [[[NSImage alloc] initWithSize:NSMakeSize(width, height)] autorelease];
|
|
|
|
bitmap = [[[NSBitmapImageRep alloc]
|
2013-11-28 10:29:01 +00:00
|
|
|
initWithBitmapDataPlanes:nil
|
|
|
|
pixelsWide:width pixelsHigh:height
|
|
|
|
bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES
|
|
|
|
isPlanar:NO colorSpaceName:NSCalibratedRGBColorSpace
|
|
|
|
bitmapFormat:NSAlphaNonpremultipliedBitmapFormat
|
2016-01-08 09:23:46 +00:00
|
|
|
bytesPerRow:(width * 4) bitsPerPixel:32
|
2013-11-28 10:29:01 +00:00
|
|
|
] autorelease];
|
2016-01-08 09:23:46 +00:00
|
|
|
[surface addRepresentation:bitmap];
|
|
|
|
[cocoaView setImage:surface];
|
2013-11-28 10:29:01 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 09:23:46 +00:00
|
|
|
auto target = (uint32*)[bitmap bitmapData];
|
|
|
|
|
|
|
|
if(auto icon = state().icon) {
|
|
|
|
icon.transform();
|
|
|
|
memory::copy(target, icon.data(), icon.size());
|
|
|
|
} else if(auto& gradient = state().gradient) {
|
|
|
|
auto& colors = gradient.state.colors;
|
|
|
|
image fill;
|
|
|
|
fill.allocate(width, height);
|
|
|
|
fill.gradient(colors[0].value(), colors[1].value(), colors[2].value(), colors[3].value());
|
|
|
|
memory::copy(target, fill.data(), fill.size());
|
|
|
|
} else {
|
|
|
|
uint32 color = state().color.value();
|
|
|
|
for(auto n : range(width * height)) target[n] = color;
|
2013-11-28 10:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
2015-12-30 06:41:46 +00:00
|
|
|
|
|
|
|
#endif
|