Skip to content

Instantly share code, notes, and snippets.

@jonsterling
Created May 31, 2010 04:47
Show Gist options
  • Select an option

  • Save jonsterling/419547 to your computer and use it in GitHub Desktop.

Select an option

Save jonsterling/419547 to your computer and use it in GitHub Desktop.
NSString *WindowControllerPath = [[NSBundle mainBundle] pathForResource:@"WindowController"
ofType:@"st"];
NSString *WindowControllerCode = [NSString stringWithFormat:@"[%@]",
[NSString stringWithContentsOfFile:FileWindowControllerPath
encoding:NSUTF8StringEncoding
error:nil]];
FSBlock *WindowControllerBlock = [WindowControllerCode asBlock];
[WindowConrollerBlock value];
Class WindowController = NSClassFromString(@"WindowController");
id windowController = [[WindowController alloc] init];
[windowController loadWindow];
#define LoadSmalltalk(File)\
NSString *File##Path = [[NSBundle mainBundle] pathForResource:@#File ofType:@"st"];\
NSString *File##Code = [NSString stringWithFormat:@"[%@]",[NSString stringWithContentsOfFile:File##Path encoding:NSUTF8StringEncoding error:nil]];\
FSBlock *File##Block = [File##Code asBlock];\
[File##Block value];\
Class File = NSClassFromString(@#File);
// now you can do this:
LoadSmalltalk(WindowController);
id windowController = [[WindowController alloc] init];
[windowController loadWindow];
[foo setBar:@"what-e-ver"];
[foo setBaz:@"howdydoody"];
[foo setQux:YES];
foo setBar:'what-e-ver'; setBaz:'howdydoody'; setQux:YES.
"or, more readably"
foo setBar:'what-e-ver';
setBaz:'howdydoody';
setQux:YES.
WindowController : NSObject {
"instance variables"
window drawer toggleButtonTitleClosed toggleButtonTitleOpened
"initializer"
- init {
self := super init.
self ~~ nil ifTrue:[
toggleButtonTitleClosed := 'open drawer'.
toggleButtonTitleOpened := 'close drawer'.
].
^ self
}
"build and present window"
- loadWindow {
window := NSWindow alloc initWithContentRect:(400<>200 extent:400<>300)
styleMask:(NSTitledWindowMask + NSClosableWindowMask + NSMiniaturizableWindowMask + NSResizableWindowMask )
backing:NSBackingStoreBuffered
defer:NO.
window setTitle:'Howdy! I’m from Smalltalk!';
setMinSize:(NSValue sizeWithWidth:300 height:200).
window setContentBorderThickness:32 forEdge:NSMinYEdge.
window orderFront:nil.
self windowDidLoad.
}
"fill window with controls"
- windowDidLoad {
| toggleButton toggleButtonWidth toggleButtonHeight toggleButtonCenterX toggleButtonCenterY
drawerDefaultSize drawerMinSize drawerView
switchButton switchButtonWidth switchButtonHeight switchButtonCenterX switchButtonCenterY |
"prepare toggleButton dimensions"
toggleButtonWidth := 150. toggleButtonHeight:= 70.
toggleButtonCenterX := window contentView bounds extent x / 2 - (toggleButtonWidth / 2).
toggleButtonCenterY := window contentView bounds extent y / 2 - (toggleButtonHeight / 2).
"create toggleButton"
toggleButton := NSButton alloc initWithFrame:(toggleButtonCenterX<>toggleButtonCenterY extent:toggleButtonWidth<>toggleButtonHeight).
toggleButton setBezelStyle:NSSmallSquareBezelStyle;
setAutoresizingMask:(NSViewMinXMargin + NSViewMaxXMargin + NSViewMinYMargin + NSViewMaxYMargin);
setTitle:(toggleButtonTitleClosed);
setTarget:self;
setAction:#toggleDrawer:.
window contentView addSubview:toggleButton.
"prepare drawer dimensions"
drawerDefaultSize := NSValue sizeWithWidth:200 height:(window contentView bounds extent y).
drawerMinSize := NSValue sizeWithWidth:150 height:(window contentView bounds extent y).
"create drawer and drawer contentView"
drawerView := NSView alloc init.
drawer := NSDrawer alloc initWithContentSize:drawerDefaultSize
preferredEdge:NSMinXEdge.
drawer setMinContentSize:drawerMinSize;
setMaxContentSize:drawerDefaultSize;
setContentView:drawerView;
setParentWindow:window.
"prepare switchButton dimensions"
switchButtonWidth := 100. switchButtonHeight := 100.
switchButtonCenterX := drawer contentView bounds extent x / 2- (switchButtonWidth / 2).
switchButtonCenterY := drawer contentView bounds extent y / 2 - (switchButtonHeight / 2).
"create switchButton"
switchButton := NSButton alloc initWithFrame:(switchButtonCenterX<>switchButtonCenterY extent: switchButtonWidth<>switchButtonHeight).
switchButton setBezelStyle:NSRoundedBezelStyle;
setAutoresizingMask:(NSViewMinXMargin + NSViewMaxXMargin + NSViewMinYMargin + NSViewMaxYMargin);
setTitle:'other side';
setTarget:self;
setAction:#pushDrawerToOtherSide:.
drawerView addSubview:switchButton.
}
"make the drawer open and close"
- toggleDrawer:sender {
drawer toggle:self.
sender title = toggleButtonTitleOpened ifTrue:[
sender setTitle:toggleButtonTitleClosed.
] ifFalse:[
sender setTitle:toggleButtonTitleOpened.
].
}
"make the drawer close and open on the opposite side"
- pushDrawerToOtherSide:sender {
drawer edge = NSMaxXEdge ifTrue:[
drawer openOnEdge:NSMinXEdge.
]. drawer edge = NSMinXEdge ifTrue:[
drawer openOnEdge:NSMaxXEdge.
].
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment