Created
December 29, 2022 02:36
-
-
Save cellularmitosis/15383aae4e95b633aff04ffc217f28fd to your computer and use it in GitHub Desktop.
A trivial inches-to-mm converter application for OS X Tiger.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.m | |
// Project1 | |
// | |
// Created by Mac User on 12/22/22. | |
// Copyright __MyCompanyName__ 2022. All rights reserved. | |
// | |
#import <Cocoa/Cocoa.h> | |
#define NSViewLeftMargin (NSViewMinXMargin) | |
#define NSViewRightMargin (NSViewMaxXMargin) | |
#define NSViewTopMargin (NSViewMaxYMargin) | |
#define NSViewBottomMargin (NSViewMinYMargin) | |
@class MainView; | |
// MARK: - MainController | |
#define INCHES_FIELD (1) | |
#define MM_FIELD (2) | |
@interface MainController: NSObject { | |
MainView* _mainView; | |
int _lastEdited; | |
} | |
@end | |
@implementation MainController | |
- (id)initWithView:(MainView*)mainView { | |
self = [super init]; | |
if (self != nil) { | |
_mainView = mainView; | |
[[_mainView inchesField] setDelegate:self]; | |
[[_mainView mmField] setDelegate:self]; | |
[[_mainView convertButton] setTarget:self]; | |
[[_mainView convertButton] setAction:@selector(convertDidGetClicked:)]; | |
_lastEdited = INCHES_FIELD; | |
} | |
return self; | |
} | |
- (void)convertDidGetClicked:(id)sender { | |
NSLog(@"Convert did get clicked!"); | |
if (_lastEdited == INCHES_FIELD) { | |
float inches = [[_mainView inchesField] floatValue]; | |
float mm = inches * 25.4; | |
[[_mainView mmField] setFloatValue:mm]; | |
} else if (_lastEdited == MM_FIELD) { | |
float mm = [[_mainView mmField] floatValue]; | |
float inches = mm / 25.4; | |
[[_mainView inchesField] setFloatValue:inches]; | |
} | |
} | |
- (void)controlTextDidBeginEditing:(NSNotification *)aNotification { | |
if ([[aNotification object] isEqual:[_mainView inchesField]]) { | |
NSLog(@"textDidBeginEditing (inchesField)"); | |
_lastEdited = INCHES_FIELD; | |
} else if ([[aNotification object] isEqual:[_mainView mmField]]) { | |
NSLog(@"textDidBeginEditing (mmField)"); | |
_lastEdited = MM_FIELD; | |
} | |
} | |
@end | |
// MARK: - MainView | |
@interface MainView: NSView { | |
NSButton* _convertButton; | |
NSTextField* _inchesField; | |
NSTextField* _inchesLabel; | |
NSTextField* _mmField; | |
NSTextField* _mmLabel; | |
} | |
- (NSButton*)convertButton; | |
- (NSTextField*)inchesField; | |
- (NSTextField*)mmField; | |
@end | |
@implementation MainView | |
- (NSButton*)convertButton { | |
return _convertButton; | |
} | |
- (NSTextField*)inchesField { | |
return _inchesField; | |
} | |
- (NSTextField*)mmField { | |
return _mmField; | |
} | |
- (id) initWithFrame:(NSRect)frame { | |
self = [super initWithFrame:frame]; | |
NSLog(@"MainView.initWithFrame: %fx%f, %fx%f", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height); | |
if (self != nil) { | |
[self _setupSubviews]; | |
[self setAutoresizesSubviews:true]; | |
} | |
return self; | |
} | |
- (void)setBounds:(NSRect)boundsRect { | |
NSLog(@"MainView.setBounds: %@", boundsRect); | |
[self _layoutSubviews]; | |
[super setBounds:boundsRect]; | |
} | |
- (void)setFrame:(NSRect)frameRect { | |
NSLog(@"MainView.setFrame: %fx%f, %fx%f", frameRect.origin.x, frameRect.origin.y, frameRect.size.width, frameRect.size.height); | |
[self _layoutSubviews]; | |
[super setFrame:frameRect]; | |
} | |
- (void)_setupSubviews { | |
NSSize size = [self bounds].size; | |
NSLog(@"MainView._setupSubviews, size: %fx%f", size.width, size.height); | |
_inchesField = [[NSTextField alloc] init]; | |
[_inchesField setAutoresizingMask:NSViewBottomMargin]; | |
[self addSubview:_inchesField]; | |
_inchesLabel = [[NSTextField alloc] init]; | |
[_inchesLabel setAutoresizingMask:NSViewBottomMargin]; | |
[self addSubview:_inchesLabel]; | |
[_inchesLabel setBordered:false]; | |
[_inchesLabel setBezeled:false]; | |
[_inchesLabel setDrawsBackground:false]; | |
[_inchesLabel setStringValue:@"inches"]; | |
_mmField = [[NSTextField alloc] init]; | |
[_mmField setAutoresizingMask:NSViewBottomMargin]; | |
[self addSubview:_mmField]; | |
_mmLabel = [[NSTextField alloc] init]; | |
[_mmLabel setAutoresizingMask:NSViewBottomMargin]; | |
[self addSubview:_mmLabel]; | |
[_mmLabel setBordered:false]; | |
[_mmLabel setBezeled:false]; | |
[_mmLabel setDrawsBackground:false]; | |
[_mmLabel setStringValue:@"mm"]; | |
_convertButton = [[NSButton alloc] init]; | |
[_convertButton setButtonType:NSMomentaryPushInButton]; | |
[_convertButton setBezelStyle:NSRoundedBezelStyle]; | |
[_convertButton setTitle:@"Convert"]; | |
[_convertButton setAutoresizingMask:NSViewRightMargin|NSViewBottomMargin]; | |
[self addSubview:_convertButton]; | |
} | |
- (void)_layoutSubviews { | |
NSSize size = [self bounds].size; | |
NSLog(@"MainView._layoutSubviews, size: %fx%f", size.width, size.height); | |
[_inchesField setFrameSize:NSMakeSize(96,22)]; | |
[_inchesField setFrameOrigin:NSMakePoint(16, size.height-16-22)]; | |
[_inchesLabel sizeToFit]; | |
[_inchesLabel setFrameOrigin:NSMakePoint(16+96+4, size.height-16-22)]; | |
[_mmField setFrameSize:NSMakeSize(96,22)]; | |
[_mmField setFrameOrigin:NSMakePoint(16, size.height-16-22-16-22)]; | |
[_mmLabel sizeToFit]; | |
[_mmLabel setFrameOrigin:NSMakePoint(16+96+4, size.height-16-22-16-22)]; | |
[_convertButton sizeToFit]; | |
NSSize bsize = [_convertButton bounds].size; | |
[_convertButton setFrameOrigin:NSMakePoint(16+96+64, size.height-bsize.height-31)]; | |
} | |
@end | |
// MARK: - AppDelegate | |
@interface AppDelegate: NSObject { | |
MainController* _mainController; | |
} | |
- (void)applicationWillFinishLaunching:(NSNotification *)aNotification; | |
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification; | |
@end | |
@implementation AppDelegate | |
- (void) applicationWillFinishLaunching:(NSNotification *)aNotification { | |
NSLog(@"AppDelegate.applicationDidFinishLaunching"); | |
NSWindow* window = [[[NSApplication sharedApplication] windows] objectAtIndex:0]; | |
NSRect wframe = [window frame]; | |
[window setFrame:NSMakeRect(wframe.origin.x,wframe.origin.y,260,116) display:true]; | |
NSView* mainView = [[MainView alloc] initWithFrame:NSMakeRect(0,0,640,480)]; | |
_mainController = [[MainController alloc] initWithView:mainView]; | |
[window setContentView:mainView]; | |
} | |
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification { | |
NSLog(@"AppDelegate.applicationWillFinishLaunching"); | |
} | |
@end | |
// MARK: - main | |
int main(int argc, char *argv[]) { | |
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; | |
AppDelegate* appDelegate = [[AppDelegate alloc] init]; | |
[[NSApplication sharedApplication] setDelegate:appDelegate]; | |
int ret = NSApplicationMain(argc, (const char **) argv); | |
[pool release]; | |
NSLog(@"main exiting!"); | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment