background image
Windows and Menus in Cocoa
Windows have numerous characteristics. They can be onscreen or offscreen. Onscreen windows are "layered"
on the screen in tiers managed by the window server. A typical Cocoa window has a title bar, a content area,
and several control objects.
Key and Main Windows
Onscreen windows can carry a status: key or main. Key windows respond to keypresses for an application
and are the primary recipient of messages from menus and panels. Usually, a window is made key when the
user clicks it. Each application can have only one key window.
An application has one main window, which can often have key status as well. The main window is the
principal focus of user actions for an application. Often user actions in a key window (typically a panel such
as the Font window or an Info window) have a direct effect on the main window.
NSWindow and the Window Server
Many user interface objects other than the standard window are windows. Menus, pop-up lists, and pull-down
lists are primarily windows, as are all varieties of utility windows and dialogs: attention dialogs, Info windows,
drawers, utility windows, and tool palettes, to name a few. In fact, anything drawn on the screen must appear
in a window. Users, however, may not recognize or refer to them as windows.
Two interacting systems create and manage Cocoa windows. A window is created by the window server.
The window server is a process that uses the internal window management portion of Quartz (the low-level
drawing system) to draw, resize, hide, and move windows using Quartz graphics routines. The window server
also detects user events (such as mouse clicks) and forwards them to applications.
The window that the window server creates is paired with an object supplied by the Application Kit framework.
The object supplied is an instance of the
NSWindow
class. Each physical window in a Cocoa program is
managed by an instance of
NSWindow
or a subclass of it. For information on the Application Kit, see What Is
Cocoa? in Cocoa Fundamentals Guide.
When you create an
NSWindow
object, the window server creates the physical window that the
NSWindow
object manages. The
NSWindow
class offers a number of instance methods through which you customize
the operation of its onscreen window.
Application, Window, View
In a running Cocoa application,
NSWindow
objects occupy a middle position between an instance of
NSApplication
and the views of the application. (A view is an object that can draw itself and detect user
events.) The
NSApplication
object keeps a list of its windows and tracks the current status of each. Each
NSWindow
object, on the other hand, manages a hierarchy of views in addition to its window.
At the top of this hierarchy is the content view, which fits just within the window's content rectangle. The
content view encloses all other views (its subviews), which come below it in the hierarchy. The
NSWindow
object distributes events to views in the hierarchy and regulates coordinate transformations among them.
26
User Interface Elements and Interface Builder
2007-10-31 | © 2007 Apple Inc. All Rights Reserved.
CHAPTER 4
Defining the View: Building the User Interface