* and the message to display.
*/
Widget
PostDialog(parent, dialog_type, msg)
Widget parent;
int dialog_type;
char *msg;
{
Widget dialog;
XmString text;
dialog = XmCreateMessageDialog (parent, "dialog", NULL, 0);
text = XmStringCreateLocalized (msg);
XtVaSetValues (dialog,
XmNdialogType, dialog_type,
XmNmessageString, text,
NULL);
XmStringFree (text);
XtManageChild (dialog);
XtPopup (XtParent (dialog), XtGrabNone);
return dialog;
}
This routine allows the programmer to specify several parameters: the parent widget, the type of dialog that is to be
used, and the message that is to be displayed. The function returns the new dialog widget, so that the calling routine
can modify it, unmanage it, or keep a handle to it. You may have additional requirements that this simplified example
does not satisfy. For instance, the routine does not allow you to specify callback functions for the buttons in the action
area and it does not handle the destruction of the widget when it is no longer needed. You could extend the routine to
handle these issues, or you could control them outside the context of the function. You may also want to extend the
routine so that it reuses the same dialog each time it is called and so that it allows you to disable the different action
area buttons. All of these issues are discussed again in Chapter 6, Selection Dialogs, and in Chapter 21, Advanced
Dialog Programming.
6.4 Dialog Resources
The following sections discuss resources that are specific to Motif dialogs. In most cases, these resources are
BulletinBoard widget resources, since all Motif dialogs are subclassed from this class. However, they are not intended
to be used by generic BulletinBoard widgets. The resources only apply when the widget is an immediate child of a
DialogShell widget; they are really intended to be used exclusively by the predefined Motif dialog classes. Remember
that the resources must be set on the dialog widget, not the DialogShell. See Chapter 8, Manager Widgets, for details
on the generic BulletinBoard resources.
6.4.1 The Default Button
All predefined Motif dialogs have a default button in their action area. The default button is activated when the user
presses the RETURN key in the dialog. The OK button is normally the default button, but once the dialog is
displayed, the user can change the default button by using the arrow keys to traverse the action buttons. The action
button with the keyboard focus is always the default button. Since the default button can be changed by the user, the
button that is the default is only important when the dialog is initially popped up. The importance of the default button
lies in its ability to influence the user's default response to the dialog.
6 Introduction to Dialogs 6.4 Dialog Resources
120
You can change the default button for a MessageDialog by setting the XmNdefaultButtonType resource on the
dialog widget. This resource is specific to MessageDialogs; it cannot be set for the various types of selection dialogs.
The resource can have one of the following values:
XmDIALOG_OK_BUTTON
This value specifies that the default button is the furthest button on the left of the dialog. By default, this
button is the OK button, although its label may have been changed to another string.
XmDIALOG_CANCEL_BUTTON
This value specifies that the Cancel button is the default button. This value is appropriate in situations where
the action of the dialog is destructive, such as for a WarningDialog that is posted in order to warn the user of a
possibly dangerous action.
XmDIALOG_HELP_BUTTON
This value specifies the Help button, which is always the furthest button on the right of a Motif dialog. This
button is rarely set as the default button.
XmDIALOG_NONE
This value specifies that there is no default button.
The values for XmNdefaultButtonType come up again later, when we discuss
XmMessageBoxGetChild() and again in Chapter 6, Selection Dialogs, for
XmSelectionBoxGetChild(). An example of how the default button type can be used is shown in the
source code XmStringCreateLocalized() is only available in Motif 1.2;
XmStringCreateSimple() is the corresponding function in Motif 1.1.
/*
* WarningMsg() −− Inform the user that she is about to embark on a
* dangerous mission and give her the opportunity to back out.
*/
void
WarningMsg(parent, client_data, call_data)
Widget parent;
XtPointer client_data;
XtPointer call_data;
{
static Widget dialog;
XmString text, ok_str, cancel_str;
char *msg = (char *) client_data;
if (!dialog)
dialog = XmCreateWarningDialog (parent, "warning", NULL, 0);
text = XmStringCreateLtoR (msg, XmFONTLIST_DEFAULT_TAG);
ok_str = XmStringCreateLocalized ("Yes");
cancel_str = XmStringCreateLocalized ("No");
XtVaSetValues (dialog,
XmNmessageString, text,
XmNokLabelString, ok_str,
XmNcancelLabelString, cancel_str,
XmNdefaultButtonType, XmDIALOG_CANCEL_BUTTON,
NULL);
XmStringFree (text);
XmStringFree (ok_str);
XmStringFree (cancel_str);
XtManageChild (dialog);
6 Introduction to Dialogs 6.4 Dialog Resources
121
Get Volume 6A: Motif Programming Manual now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.