to the DialogShell. The resource only affects the presence of resize handles in the window manager frame; it does not
deal with other window manager controls. See Chapter 16, Interacting With the Window Manager, for details on how
to specify the window manager controls for a DialogShell, or any shell widget, directly.
6.4.6 Dialog Fonts
The BulletinBoard widget provides resources that enable you to specify the fonts that are used for all of the button,
Label, and Text widget descendants of the BulletinBoard. Since Motif dialog widgets are subclassed from the
BulletinBoard, you can use these resources to make sure that the fonts that are used within a dialog are consistent. The
XmNbuttonFontList resource specifies the font list that is used for all of the button descendants of the dialog.
The resource is set on the dialog widget itself, not on its individual children. Similarly, the XmNlabelFontList
resource is used to set the font list for all of the Label descendants of the dialog and XmNtextFontList is used for
all of the Text and TextField descendants.
If one of these resources is not set, the toolkit determines the font list by searching up the widget hierarchy for an
ancestor that is a subclass of BulletinBoard, VendorShell, or MenuShell. If an ancestor is found, the font list resource
is set to the value of that font list resource in the ancestor widget. See Chapter 19, Compound Strings, for more
information on font lists.
You can override the XmNbuttonFontList, XmNlabelFontList, and XmNtextFontList resources on a
per−widget basis by setting the XmNfontList resource directly on individual widgets. Of course, you must break
the dialog abstraction and retrieve the widgets internal to the dialog itself to set this resource. While we describe how
to do this in the following section, we do not recommend configuring dialogs down to this level of detail.
6.5 Dialog Callback Routines
As mentioned earlier, the predefined Motif dialogs have their own resources to reference the labels and callback
routines for the action area PushButtons. Instead of accessing the PushButton widgets in the action area to install
callbacks, you use the resources XmNokCallback, XmNcancelCallback, and XmNhelpCallback on the
dialog widget itself. These callbacks correspond to each of the three buttons, OK, Cancel, and Help.
Installing callbacks for a dialog is no different than installing them for any other type of Motif widget; it may just
seem different because the dialog widgets contain so many subwidgets. The following code fragment demonstrates the
installation of simple callback for all of the buttons in a MessageDialog:
...
dialog = XmCreateMessageDialog (w, "notice", NULL, 0);
...
XtAddCallback (dialog, XmNokCallback, ok_pushed, "Hi");
XtAddCallback (dialog, XmNcancelCallback, cancel_pushed, "Foo");
XtAddCallback (dialog, XmNhelpCallback, help_pushed, NULL);
XtManageChild (dialog);
...
/* ok_pushed() −−the OK button was selected. */
void
ok_pushed(widget, client_data, call_data)
Widget widget;
XtPointer client_data;
XtPointer call_data; { char *message = (char *) client_data; printf ("OK was selected: %s0, message); } /*
cancel_pushed() −−the Cancel button was selected. */ void cancel_pushed(widget, client_data, call_data) Widget
6 Introduction to Dialogs 6.4.6 Dialog Fonts
124
widget; XtPointer client_data;
XtPointer call_data; { char *message = (char *) client_data; printf ("Cancel was selected: %s0, message); } /*
help_pushed() −−the Help button was selected. */ void help_pushed(widget, client_data, call_data) Widget widget;
XtPointer client_data;
XtPointer call_data; { printf ("Help was selected0); } In this example, a dialog is created and callback routines for
each of the three responses are added using XtAddCallback(). We also provide simple client data to demonstrate
how the data is passed to the callback routines. These callback routines simply print the fact that they have been
activated; the messages they print are taken from the client data.
All of the dialog callback routines take three parameters, just like any standard callback routine. The widget
parameter is the dialog widget that contains the button that was selected; it is not the DialogShell widget or the
PushButton that the user selected from the action area. The second parameter is the client_data, which is
supplied to XtAddCallback(), and the third is the call_data, which is provided by the internals of the widget
that invoked the callback.
The client_data parameter is of type XtPointer, which means that you can pass arbitrary values to the
function, depending on what is necessary. However, you cannot pass a float or a double value or an actual data
structure. If you need to pass such values, you must pass the address of the variable or a pointer to the data structure.
In keeping with the philosophy of abstracting and generalizing code, you should use the client_data parameter as
much as possible because it eliminates the need for some global variables and it keeps the structure of an application
modular.
For the predefined Motif dialogs, the call_data parameter is a pointer to a data structure that is filled in by the
dialog box when the callback is invoked. The data structure contains a callback reason and the event that invoked the
callback. The structure is of type XmAnyCallbackStruct, which is declared as follows:
typedef struct {
int reason;
XEvent *event;
} XmAnyCallbackStruct;
The value of the reason field is an integer value that can be any one of XmCR_HELP, XmCR_OK, or
XmCR_CANCEL. The value specifies the button that the user pressed in the dialog box. The values for the reason
field remain the same, no matter how you change the button labels for a dialog. For example, you can change the label
for the OK button to say Help, using the resource XmNokLabelString, but the reason parameter will still be
XmCR_OK when the button is activated.
Because the reason field provides information about the user's response to the dialog in terms of the button that was
pushed, we can simplify the previous code fragment and use one callback function for all of the possible actions. The
callback function can determine which button was selected by examining reason. the source code demonstrates this
simplification. XtSetLanguageProc() is only available in X11R5; there is no corresponding function in X11R4.
XmStringCreateLocalized() is only available in Motif 1.2; XmStringCreateSimple() is the
corresponding function in Motif 1.1.
/* reason.c −− examine the reason field of the callback structure
* passed as the call_data of the callback function. This field
* indicates which action area button in the dialog was pressed.
*/
#include <Xm/RowColumn.h>
#include <Xm/MessageB.h>
#include <Xm/PushB.h>
6 Introduction to Dialogs 6.4.6 Dialog Fonts
125
/* main() −−create a pushbutton whose callback pops up a dialog box */
main(argc, argv)
char *argv[];
{
XtAppContext app;
Widget toplevel, rc, pb;
extern void pushed();
XtSetLanguageProc (NULL, NULL, NULL);
toplevel = XtVaAppInitialize (&app, "Demos", NULL, 0,
&argc, argv, NULL, NULL);
rc = XtVaCreateWidget ("rowcol", xmRowColumnWidgetClass, toplevel, NULL);
pb = XtVaCreateManagedWidget ("Hello",
xmPushButtonWidgetClass, rc, NULL);
XtAddCallback (pb, XmNactivateCallback, pushed, "Hello World");
pb = XtVaCreateManagedWidget ("Goodbye",
xmPushButtonWidgetClass, rc, NULL);
XtAddCallback (pb, XmNactivateCallback, pushed, "Goodbye World");
XtManageChild (rc);
XtRealizeWidget (toplevel);
XtAppMainLoop (app);
}
/* pushed() −−the callback routine for the main app's pushbuttons.
* Create and popup a dialog box that has callback functions for
* the OK, Cancel and Help buttons.
*/
void
pushed(widget, client_data, call_data)
Widget widget;
XtPointer client_data;
XtPointer call_data;
{
static Widget dialog;
char *message = (char *) client_data;
XmString t = XmStringCreateLocalized (message);
/* See if we've already created this dialog −− if so,
* we don't need to create it again. Just set the message
* and manage it (repop it up).
*/
if (!dialog) {
extern void callback();
Arg args[5];
int n = 0;
XtSetArg (args[n], XmNautoUnmanage, False); n++;
dialog = XmCreateMessageDialog (widget, "notice", args, n);
XtAddCallback (dialog, XmNokCallback, callback, "Hi");
XtAddCallback (dialog, XmNcancelCallback, callback, "Foo");
XtAddCallback (dialog, XmNhelpCallback, callback, "Bar");
}
XtVaSetValues (dialog, XmNmessageString, t, NULL);
XmStringFree (t);
XtManageChild (dialog);
6 Introduction to Dialogs 6.4.6 Dialog Fonts
126

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.