|
| 1 | +/** |
| 2 | + * clang++ main.mm -framework Cocoa -o test && ./test |
| 3 | + **/ |
| 4 | + |
| 5 | +#import <Cocoa/Cocoa.h> |
| 6 | + |
| 7 | +@interface TestView: NSView |
| 8 | +{ |
| 9 | +} |
| 10 | + |
| 11 | +@end |
| 12 | + |
| 13 | +@implementation TestView |
| 14 | + |
| 15 | +- (id)initWithFrame:(NSRect)aFrame |
| 16 | +{ |
| 17 | + if (self = [super initWithFrame:aFrame]) { |
| 18 | + } |
| 19 | + return self; |
| 20 | +} |
| 21 | +extern "C" { |
| 22 | +typedef enum { |
| 23 | + kCGContextTypeUnknown, |
| 24 | + kCGContextTypePDF, |
| 25 | + kCGContextTypePostScript, |
| 26 | + kCGContextTypeWindow, |
| 27 | + kCGContextTypeBitmap, |
| 28 | + kCGContextTypeGL, |
| 29 | + kCGContextTypeDisplayList, |
| 30 | + kCGContextTypeKSeparation, |
| 31 | + kCGContextTypeIOSurface, |
| 32 | + kCGContextTypeCount |
| 33 | +} CGContextType; |
| 34 | + |
| 35 | + |
| 36 | +CGContextType CGContextGetType(CGContextRef); |
| 37 | +} |
| 38 | +- (void)drawRect:(NSRect)aRect |
| 39 | +{ |
| 40 | + CGContextRef cg = NSGraphicsContext.currentContext.CGContext; |
| 41 | + printf("CGContextType: %d\n", CGContextGetType(cg)); |
| 42 | + CGColorSpaceRef color = CGBitmapContextGetColorSpace(cg); |
| 43 | + CFShow(color); |
| 44 | + CFShow(cg); |
| 45 | + exit(1); |
| 46 | + for (int y = 0; y<20; y++) { |
| 47 | + for (int x = 0; x<20; x++) { |
| 48 | + CGContextSetRGBFillColor(cg, 0.2, 0.6, 1.0, 0.9); |
| 49 | + CGContextFillEllipseInRect(cg, CGRectMake(50+x*50, 30+30*y, 200, 130)); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +@end |
| 55 | + |
| 56 | +@interface TerminateOnClose : NSObject<NSWindowDelegate> |
| 57 | +@end |
| 58 | + |
| 59 | +@implementation TerminateOnClose |
| 60 | +- (void)windowWillClose:(NSNotification*)notification |
| 61 | +{ |
| 62 | + [NSApp terminate:self]; |
| 63 | +} |
| 64 | +@end |
| 65 | + |
| 66 | +int |
| 67 | +main (int argc, char **argv) |
| 68 | +{ |
| 69 | + NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; |
| 70 | + |
| 71 | + [NSApplication sharedApplication]; |
| 72 | + [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; |
| 73 | + |
| 74 | + int style = |
| 75 | + NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable | NSWindowStyleMaskMiniaturizable; |
| 76 | + NSRect contentRect = NSMakeRect(400, 300, 300, 200); |
| 77 | + NSWindow* window = [[NSWindow alloc] initWithContentRect:contentRect |
| 78 | + styleMask:style |
| 79 | + backing:NSBackingStoreBuffered |
| 80 | + defer:NO]; |
| 81 | + |
| 82 | + NSView* view = [[TestView alloc] initWithFrame:NSMakeRect(0, 0, contentRect.size.width, contentRect.size.height)]; |
| 83 | + |
| 84 | + [window setContentView:view]; |
| 85 | + [window setDelegate:[[TerminateOnClose alloc] autorelease]]; |
| 86 | + [NSApp activateIgnoringOtherApps:YES]; |
| 87 | + [window makeKeyAndOrderFront:window]; |
| 88 | + |
| 89 | + [NSApp run]; |
| 90 | + |
| 91 | + [pool release]; |
| 92 | + |
| 93 | + return 0; |
| 94 | +} |
0 commit comments