How Windows Work

The NSWindow class defines objects that manage and coordinate the windows an application displays on the screen. A single NSWindow object corresponds to at most one onscreen window. The two principal functions of an NSWindow object are to provide an area in which NSView objects can be placed and to accept and distribute, to the appropriate views, events the user instigates through actions with the mouse and keyboard. Note that the term window sometimes refers to the Application Kit object and sometimes to the window server’s display device; which meaning is intended is made clear in context. AppKit also defines an abstract subclass of NSWindowNSPanel—that adds behavior more appropriate for auxiliary windows.

An NSWindow object is defined by a frame rectangle that encloses the entire window, including its title bar, border, and other peripheral elements (such as the resize control), and by a content rectangle that encloses just its content area. Both rectangles are specified in the screen coordinate system and are restricted to integer values. The frame rectangle establishes the window’s base coordinate system. This coordinate system is always aligned with and measured in the same increments as the screen coordinate system (in other words, the base coordinate system can’t be rotated or scaled). The origin of the base coordinate system is the bottom-left corner of the window’s frame rectangle.

Typically, you create windows using Interface Builder, which allows you to position them, set many of their attributes, and lay out their views. The programmatic work you do with windows more often involves bringing them on and off the screen; changing dynamic attributes such as the window’s title; running modal windows to restrict user input; and assigning a delegate that can monitor certain of the window’s actions, such as closing, zooming, and resizing.

You can also create a window programmatically with one of its initializers by specifying, among other attributes, the size and location of its content rectangle. The frame rectangle is derived from the dimensions of the content rectangle.

When it’s created, a window automatically creates two views: an opaque frame view that fills the frame rectangle and draws the border, title bar, other peripheral elements, and background, and a transparent content view that fills the content rectangle. The frame view and its peripheral elements are private objects that your application can’t access directly. The content view is the “highest” accessible view in the window; you can replace the default content view with a view of your own creation using the setContentView: method. The window determines the placement of the content view; you can’t position it using the NSView methods that begin with setFrame; you must use the NSWindow class’s placement methods, as described in “Opening and Closing Windows.”

You add other views to the window as subviews of the content view or as subviews of any of the content view’s subviews, and so on, via the addSubview: method of NSView. This tree of views is called the window’s view hierarchy. When a window is told to display itself, it does so by sending display... messages to the top-level view in its view hierarchy. Because displaying is carried out in a determined order, the content view (which is drawn first) may be wholly or partially obscured by its subviews, and these subviews may be obscured by their subviews (and so on).