Important: The information in this document is obsolete and should not be used for new development.
Dialog Boxes and Controls
A dialog box is a special type of window used in Macintosh applications to display information and receive input from the user. A control is a user interface item, such as a radio button or checkbox, that commonly appears in a dialog box.In MacApp, a dialog box is just another type of window, containing its own hierarchy of views. MacApp supplies the
TDialogViewclass for building a dialog box, as well as a number of view classes to represent Macintosh Control Manager controls, such as buttons and checkboxes. TheTDialogViewclass automatically creates aTDialogBehaviorobject to supply dialog-box behavior. The dialog behavior object works with the dialog view to validate view data when the dialog box is closed.MacApp also provides a behavior class that supports tabbing between fields. This behavior works in any window but is most commonly used in dialog boxes. The tabbing support allows a data-entry view to validate its data when a user tabs to another field. Tabbing is described in "Tabbing Between Views," beginning on page 243.
As with other windows in MacApp, you can use a view resource to define a window and view hierarchy for a dialog box. You can then create the window with
NewTemplateWindowand display it as either a modal or modeless dialog box.Modal Versus Modeless Dialog Boxes
A modal dialog box is one that, once opened, does not allow a user to perform any other program task until the dialog has been dismissed. A modeless dialog box is one that does not interfere with other program operations--for example, it may be open in the background while a user works in a different window. Modal and modeless dialog boxes may be similar in appearance--for example, both may contain buttons and text-entry fields, have a title bar, and be movable.A modal dialog box usually has one or more buttons that serve as dismissers, such as an OK button and a Cancel button. The user clicks the OK button to dismiss the dialog and accept its action. The user clicks the cancel button to dismiss the dialog without accepting any changes. If the user types Command-period or presses the Escape key, the result is the same as clicking the Cancel button--that is, the dialog is dismissed without any changes.
A modeless dialog box may also include buttons, but is commonly dismissed by clicking its close box.
The default button is indicated by a 3-pixel black border. Pressing the Return key specifies the same action as clicking the default button and normally dismisses the dialog box. Depending on the function performed by the dialog, the default button may be the OK button, the Cancel button, or another button.
Creating a Dialog Box
MacApp uses theTDialogViewclass for both modal and modeless dialog boxes.TDialogViewis not a large or complicated class. Its main contributions are to supply fields that identify the default and cancel views in a dialog box and to call theSetDialogItemsmethod of its window from itsDoPostCreatemethod. TheSetDialogItemsmethod creates aTDialogBehaviorobject and adds it to the window's behavior list. When you construct a view hierarchy for a dialog box, you use aTDialogViewobject as the superview for the dialog box's control-item views.You typically define a subclass of
TDialogViewto
- override the
DoEventorDoKeyEventmethods to provide special event handling- override the
DoPostCreatemethod to take care of initial settings that can't be handled in a view resource
- IMPORTANT
- If you override the
DoPostCreatemethod in a subclass ofTDialogView, your version should callInherited, which calls the window'sSetDialogItemsmethod to create a dialog behavior for the view. If yourDoPostCreatemethod doesn't callInherited, it should either call its window'sSetDialogItemsmethod, or supply its window with a dialog behavior directly.
Displaying a Modal Dialog
TheTDialogBehaviorclass handles certain tasks that are most commonly required of a modal dialog, including
The
- supplying a method,
PoseModally, to display a dialog modally- handling a Command-period or Escape keypress as a click in the cancel view (typically the Cancel button), which dismisses the dialog box without accepting any changes
- handling an Enter or Return keypress as a click in the default view (typically the OK button), which dismisses the dialog box and accepts any changes
PoseModallymethod sets the behavior'sfModalfield toTRUEand processes events in its own event loop until the dialog box is dismissed. You can use a view-editing program to define any control item in the dialog window as a dismisser. WhenPoseModallyprocesses an event from a dismisser control, it terminates its event loop and returns control to the caller. The return value ofPoseModallyis the ID of the item that caused the dismissal.
- Note
- When you call
PoseModallyto display a dialog window modally, you must call the window'sClosemethod after the dialog box is dismissed.
Adding Controls to Dialog Boxes
Most of the items in a dialog box are controls such as buttons, checkboxes, and text-entry fields. MacApp defines theTControlclass, a descendant ofTView, for creating these kinds of items. Each control object has these properties:
- It can determine whether it contains a mouse click.
- It can track the mouse without creating a command object.
- It can be dimmed, highlighted, and flashed.
- It can be adorned with one of the standard adornments, such as a rectangular frame.
- It can have an associated text style for displaying a label, if it has one.
Control View Classes
MacApp defines subclasses ofTControlto support standard control items:
You can read more about these classes in the MacApp Class and Method Reference.
TButton- Implements a Control Manager button
TCheckBox- Implements a Control Manager checkbox
TRadio- Implements a Control Manager radio button
TScrollBar- Implements a Control Manager scroll bar
TScrollerScrollBar- Subclass of
TScrollBarthat is associated with one or moreTScrollerview objects
TCluster- Provides a label for a group of controls; understands an
mRadioHitmessage from a subview and therefore can respond to a mouse click on a radio button by turning off all other radio buttons in the cluster
TIcon- Displays an icon; can operate as a button, if desired
TPicture- Displays a picture; can operate as a button, if desired
TPopup- Implements a pop-up menu
TStaticText- Implements a static text item that can serve as a basic form of button if enabled, but whose text cannot be edited
TEditText- Implements a text item that can be edited
TNumberText- Implements a view to accept and validate numeric data
Control Object Events
When a user clicks in a control view, MacApp calls the control view object'sDoEventmethod. The interface to this method is defined as follows:
virtual void DoEvent(EventNumber eventNumber, TEventHandler* source, TEvent* event );TheeventNumberparameter is the ID of the event. Constants for control object event numbers are defined in the UMacAppGlobals unit. Some examples are
const EventNumber mOKHit = 1; // Click OK button. const EventNumber mCancelHit = 2;// Click Cancel button. const EventNumber mButtonHit = 3;// Click any button. const EventNumber mCheckBoxHit = 4;// Click checkbox.Table 19-1 contains a more complete listing of these event constants, and Chapter 19, "Working With Dialog Boxes and Controls," provides recipes for processing the specified events.Tabbing Between Views
MacApp provides theTMultiWindowTabberbehavior class to support tabbing between views. It supports tabbing within any view, not just views based onTDialogView. TheTMultiWindowTabberclass is called multiwindow because it operates correctly in an environment with multiple windows open, including any number of floating windows. Multiwindow does not refer to support for tabbing between views in multiple windows, because that is not a standard feature of the Macintosh user interface.The
IApplicationmethod creates an instance ofTMultiWindowTabberand adds it to the application object's behavior list. You don't need to create your own tabber behavior objects--the application's tabber object will support correct tabbing in your dialog views or other views.Tabbing is accomplished by iterating over a view's list of subviews, looking for the current target and the next subview that wants to become the target object. The order of tabbing depends on the order of the subviews in the list.
The
TMultiWindowTabberclass descends from theTTabberclass.TTabberis an abstract behavior class that intercepts the Tab key and calls its ownTabmethod. TheTabmethod callsFindTargets, which does nothing inTTabberbut is overridden inTMultiWindowTabber. If you need specialized tab handling, you can define your own subclass of eitherTTabberorTMultiWindowTabber.MacApp also provides the
TLetterTabberclass for tabbing between the mailer view and the main content view of a document with an attached mailer.TLetterTabberis described in "PowerTalk Mailers," beginning on page 192.Validating Dialog-Box Data
If your application uses a dialog box for data entry, you may need to validate data when the user attempts to move from one entry field to another or when the user attempts to close the dialog box.Validating Data When a User Changes Fields in a Dialog Box
When a user attempts to change the field in a dialog box by tabbing or by clicking the mouse, MacApp callsBecomeTarget, a method ofTEventHandler, to set the new target object. Data validation occurs as part of the process of setting a new target object.The
BecomeTargetmethod calls theResignTargetmethod of the current target object. For aTEditTextview or aTNumberTextview, this results in a call to the view'sGetValidationErrormethod, which validates the current data.If the data in the field is invalid,
GetValidationErrorreturns a nonzero value. For aTEditTextview or aTNumberTextview, this results in a call to the view'sValidationFailedmethod, which notifies the user of the invalid data.
When you define a subclass of MacApp's
- Note
- To clarify the previous discussion, some method calls were omitted.

TEditTextorTNumberTextclasses, you can overrideGetValidationErrorto do special checking and overrideValidationFailedto put up the desired error alert.Validating Data When a User Accepts Changes to a Dialog Box
When a user accepts changes to a modal data-entry dialog box, usually by clicking the OK button, theTDialogBehaviorobject'sDismissmethod is called. This method calls the dialog view'sIsHierarchyValidmethod.The
IsHierarchyValidmethod calls theIsValidmethod for each subview in the hierarchy. TheIsValidmethod in turn callsGetValidationError, which is described in the previous section. Thus each control view in the dialog's view hierarchy has a chance to validate its data. IfGetValidationErrorreturns a nonzero value,IsValidcallsValidationFailed, which is also described in the previous section.Good program design calls for a minimum of modal dialogs, and your application may often use a modeless dialog for data entry. To validate data when a user closes a modeless dialog, you can subclass
TDialogViewand override theClosemethod to callIsHierarchyValiddirectly, as shown in this code fragment from the DemoDialogs sample application:
void TMonthlyDialog::Close()// Override. { // Give text-entry fields a chance to validate their data. if (this->IsHierarchyValid()) { // Store validated data. (Not shown.) . . . // Perform other closing tasks. Inherited::Close(); } else // Silent failure--don't close dialog box. Failure(noErr, 0); }WhenIsHierarchyValidreturnsFALSE, the dialog field with the invalid data has already displayed an error message, if appropriate, telling the user why the data is invalid. The call toFailureensures that the modeless dialog box is not closed. MacApp's failure handling is described in Chapter 3, "Core Technologies."
 
  
  
 