Mac OS X Programming
Sample Cocoa Code
The following are some small projects that provide sample code
illustrating various aspects of programming for Mac OS X using the Cocoa API.
-
TestKeyDownEvent:
displays info on the raw KeyDown events that are received when you type in
a text area.
- use of a subclass of NSTextView that overrides the 'keyDown' method
- use of the 'scrollRangetoVisible' method to keep the recently added
text visible in the NSTextView
-
ViewHierarchy:
provides a runtime inspector that allows you to examine
the hierarchy of NSView objects in the windows of your application.
It is intended as a debugging tool, or just as an aid to understanding.
- use of NSOutlineView data sources
- use of a notification to update an NSPopupButton menu on the fly
- use of tags to identify items in an NSPopupButton
- drawing on an NSView outside of the 'drawRect' method
- use of 'respondsToSelector' to determine the appropriate method to use
for an object of unknown type
-
TestLines:
provides a test of line-drawing speed.
It draws a specified number of lines each update.
- separate Model, View, and Controller classes
- use of bindings
- drawing lines via NSBezierPath in an NSView subclass
- use of applicationShouldTerminateAfterLastWindowClosed
- use of NSTimer (to get regular updates)
-
IsingCocoa:
provides an interface for running simulations of the
Ising model of magnetically interacting spins on a 2-dimensional
lattice. It is intended as a demonstration of the possibilities for
interactive exploration of scientific models - the computing power
available in modern machines is sufficient to perform sophisticated
calculations on the fly as the parameters are changed.
- NSDocument architecture
- separate NSWindowController's for each type of window or sheet
- auxiliary information displayed in an NSDrawer
- the use of sheets for getting parameters from the user
- a toolbar with state-dependent icons
- interactive controls (e.g. a slider) in the toolbar
- calculation in a separate thread
- synchronizing use of shared variables via NSLock
- image drawn via NSBitmapImageRep in a subclass of NSView
- display updates done via NSTimer
- using NSEventTrackingRunLoopMode to get events while mouse is down
- maintaining aspect ratio of a view (as opposed to a window) via tricky code
in the window resizing methods, and the use of performSelector:withObject:afterDelay
to wrap up after the resizing has finished
- adding resizing methods to NSWindow via an Objective-C category
- the use of an Objective-C protocol in keeping multiple views in sync
The calculation is done extremely efficiently
(thanks to analysis from Apple's "Shark" utility) and uses a few interesting techniques:
- multi-spin coding technique to achieve a 32 times parallelism (32 = # of bits in an int)
- faster random numbers via inlining of BSD code for random()
- use of gcc's "-fast" flag on a per-file basis
(in gcc 4.0 it is not supported for Objective-C code, so it was only used for the .c files)
-
VMTester:
provides a test platform for exploring the behaviour of OS X's virtual memory
system as applications allocate and de-allocate memory.
- use of NSTableView data sources & notifications
- use of an NSMutableArray to create a "ring buffer"
- use of applicationShouldTerminateAfterLastWindowClosed
- using a "sheet" for error notification and for simple dialogs
- use of NSTimer (to get regular updates)
- use of NSTask (to run a shell command)
-
TestSignals:
provides a test platform for exploring UNIX signals
- using NSPopupButton to implemement a pull-down menu
that uses 'tags' to identify which menu item was chosen
- using 'scrollRangeToVisible' to keep the latest information visible
in a scrolled NSTextView
- use of 'applicationShouldTerminateAfterLastWindowClosed'
to quit the application when the window is closed
- updating a counter via NSTimer
-
ImageCalc:
displays an image which is being continuously updated
- calculation in a separate thread
- synchronizing use of shared variables via NSLock
- image drawn via NSBitmapImageRep in a subclass of NSView
- regular display updates via NSTimer
- using NSEventTrackingRunLoopMode to get timer events while mouse is down
- handling window resizing via inLiveResize
- handling model size constraints when resizing
- auxiliary information displayed in an NSDrawer
-
TestRainbowCursor:
shows the dreaded "spinning rainbow pizza" (aka "spinning beachball of death")
cursor for those people who haven't seen enough of it. It also shows the two
styles of progress indicator that are available in Jaguar.
- (trivial) use of Carbon functions in a Cocoa application
- use of applicationShouldTerminateAfterLastWindowClosed
- use of two styles of NSProgressIndicator
- use of NSTimer (to simulate progress)
-
TestEvent:
shows how to do event handling in a Cocoa program without calling
NSApplicationMain and how to handle socket activity in a Foundation program
with CFRunLoop.
- adding a socket as an event source in a CFRunLoop-based program
- explicit event handling in a Cocoa program
-
TestButtonDown:
shows that an NSTimer can be effective even while a button is held down
- regular display updates via NSTimer
- using NSEventTrackingRunLoopMode to get timer events while mouse is down
Misc