Learn Objective-C on the Mac - Mark Dalrymple [99]
TIP: The Human Interface Guidelines (often shortened to “HIG”) are a set of recommendations that Apple provides for application developers. The HIG acts as a sort of style guide that you can refer to when you’re concerned that your application looks a little “off.” It’s not a strict set of rules, and no one is going to stop you from violating the HIG. In fact, many applications, including Apple’s own, stray from the guidelines in all kinds of ways. However, it gives you a good baseline, describing how the various components are meant to be used.
We mentioned the HIG in Chapter 2 already, but it’s worth pointing out again. You can find the HIG online at http://developer.apple.com/documentation/UserExperience/Conceptual/AppleHIGuidelines.
Figure 10-1 gives you a glimpse of the main kinds of windows that are prevalent in most Cocoa applications today. There are two included drawing styles for NSWindow, the “normal” appearance and the “textured” appearance, which somewhat resembles a shiny piece of metal. NSPanels can be configured to be in utility mode (in which case they have a smaller title bar, cast a smaller shadow, and float above the application’s other windows) or to look like a normal window, and in either case the choice of normal vs. textured also applies, just like NSWindow. One additional option for NSPanel is to run in HUD (short for heads-up display) mode, in which case the panel’s color scheme is inverted, its title bar and bottom edge are modified, and the whole window is made slightly transparent. This mode (which has no textured option) is intended to allow the user to see through a part of the interface to what’s behind it, and is put to good use in applications like iPhoto, where you can bring up a HUD panel containing adjustable color settings, through which the photo you’re looking at can still be seen.
Figure 10-1. A sampling of the main types of windows that are readily available in Interface Builder
Unlike some other GUI toolkits, where one of the first things you do when creating a new application is to subclass some sort of Application class and some sort of Window class, Cocoa allows for true separation of the model, view, and controller portions of your application. The window only needs to know how to display itself and provide a graphics context for its views, so that’s all it does. Any code that deals with what happens to a window while the application is running (e.g. it gets loaded from a nib file, or gets dragged across the screen, or gets closed by the user) can typically be dealt with by the window’s delegate object (i.e. its controller).
Handling Input
In addition to providing a frame for views to display their stuff, NSWindow also handles user input from the mouse and keyboard. Any mousing action (clicking, dragging, moving, releasing, and so on) in an NSWindow will trigger a method in NSWindow that finds the appropriate view object in its contents and calls the same method in the view. This symmetry works because NSWindow and NSView both inherit from NSResponder, where the methods for handling these events are defined. Likewise, when the user presses or releases a key on the keyboard, the application calls a method in the application’s “key window” (i.e. the window that currently has keyboard focus; usually the last window the user clicked in), which in turn determines which view currently has keyboard focus, and passes off responsibility, calling the same method in the focused view.
To Use a Panel, or Not to Use a Panel
Assuming you don’t need to modify the basic appearance of windows, you’ll be using either NSWindow or its subclass NSPanel to display the controls and other views in your application. The core elements of your application will usually be in an NSWindow, while NSPanel is used for auxiliary windows, such as the Inspector in Interface Builder. From a user’s standpoint, there are just a few critical differences between NSWindow and NSPanel:
• NSPanel instances normally become invisible when another application becomes the active application,