RosettaCodeData/Task/Simple-windowed-application/Objective-C/simple-windowed-application-2.m

72 lines
1.9 KiB
Mathematica
Raw Permalink Normal View History

2013-04-11 01:07:29 -07:00
@implementation ClickMe : NSWindow
2014-04-02 16:56:35 +00:00
-(instancetype) init
2013-04-11 01:07:29 -07:00
{
2014-04-02 16:56:35 +00:00
NSButton *button = [[NSButton alloc] init];
2013-04-11 01:07:29 -07:00
[button setButtonType: NSToggleButton];
[button setTitle: @"Click Me"];
[button sizeToFit];
[button setTarget: self];
[button setAction: @selector(advanceCounter:)];
2014-04-02 16:56:35 +00:00
NSRect buttonRect = [button frame];
2013-04-11 01:07:29 -07:00
2014-04-02 16:56:35 +00:00
NSTextField *text = [[NSTextField alloc]
2013-04-11 01:07:29 -07:00
initWithFrame: NSMakeRect(buttonRect.origin.x, buttonRect.size.height,
buttonRect.size.width, buttonRect.size.height)];
[text setAlignment: NSCenterTextAlignment];
[text setEditable: NO];
[text setStringValue: @"There have been no clicks yet"];
[text sizeToFit];
// reset size of button according to size of (larger...?) text
[button
setFrameSize: NSMakeSize( [text frame].size.width, buttonRect.size.height ) ];
2014-04-02 16:56:35 +00:00
int totalWindowHeight = buttonRect.size.height + [text frame].size.height;
2013-04-11 01:07:29 -07:00
2014-04-02 16:56:35 +00:00
if ((self = [super initWithContentRect: NSMakeRect(100, 100,
2013-04-11 01:07:29 -07:00
[text frame].size.width, totalWindowHeight)
2014-04-02 16:56:35 +00:00
styleMask: (NSTitledWindowMask | NSClosableWindowMask)
backing: NSBackingStoreBuffered
defer: NO])) {
_counter = 0;
_button = button;
_text = text;
[[self contentView] addSubview: _text];
[[self contentView] addSubview: _button];
[self setTitle: @"Click Me!"];
[self center];
}
2013-04-11 01:07:29 -07:00
return self;
}
- (void)applicationDidFinishLaunching: (NSNotification *)notification
{
[self orderFront: self];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed: (NSNotification *)notification
{
return YES;
}
- (void)advanceCounter: (id)sender
{
2014-04-02 16:56:35 +00:00
[_text setStringValue: [NSString stringWithFormat: @"Clicked %d times", ++_counter]];
2013-04-11 01:07:29 -07:00
}
@end
int main()
{
2014-04-02 16:56:35 +00:00
@autoreleasepool {
NSApplication *app = [NSApplication sharedApplication];
ClickMe *clickme = [[ClickMe alloc] init];
[app setDelegate: clickme];
[app run];
}
2013-04-11 01:07:29 -07:00
return 0;
}