Anonymous user
Simple windowed application: Difference between revisions
obj-c with "*Step" fw; tested only on GNUstep, as usual (but it should work with Cocoa too)
No edit summary |
(obj-c with "*Step" fw; tested only on GNUstep, as usual (but it should work with Cocoa too)) |
||
Line 535:
)
createDialog buttonClick
=={{header|Objective-C}}==
{{works with|GNUstep}}
<lang objc>#include <Foundation/Foundation.h>
#include <AppKit/AppKit.h>
@interface ClickMe : NSWindow
{
NSButton *button;
NSTextField *text;
int counter;
}
- (void)applicationDidFinishLaunching: (NSNotification *)notification;
- (BOOL)applicationShouldTerminateAfterLastWindowClosed: (NSNotification *)notification;
- (void)advanceCounter: (id)sender;
@end</lang>
<lang objc>@implementation ClickMe : NSWindow
-(id) init
{
NSRect buttonRect;
int totalWindowHeight;
counter = 0;
button = [[NSButton alloc] init];
[button setButtonType: NSToggleButton];
[button setTitle: @"Click Me"];
[button sizeToFit];
[button setTarget: self];
[button setAction: @selector(advanceCounter:)];
buttonRect = [button frame];
text = [[NSTextField alloc]
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 ) ];
totalWindowHeight = buttonRect.size.height + [text frame].size.height;
[self
initWithContentRect: NSMakeRect(100, 100,
[text frame].size.width, totalWindowHeight)
styleMask: (NSTitledWindowMask | NSClosableWindowMask)
backing: NSBackingStoreBuffered
defer: NO];
[[self contentView] addSubview: text]; [text release];
[[self contentView] addSubview: button]; [button release];
[self setTitle: @"Click Me!"];
[self center];
return self;
}
-(void) dealloc
{
[super dealloc];
}
- (void)applicationDidFinishLaunching: (NSNotification *)notification
{
[self orderFront: self];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed: (NSNotification *)notification
{
return YES;
}
- (void)advanceCounter: (id)sender
{
counter++;
[text setIntValue: counter];
}
@end
int main()
{
ClickMe *clickme;
NSAutoreleasePool *pool;
NSApplication *app;
pool = [[NSAutoreleasePool alloc] init];
app = [NSApplication sharedApplication];
clickme = [[ClickMe alloc] init];
[app setDelegate: clickme];
[app run];
return 0;
}</lang>
=={{header|Perl}}==
|