Next: Prompt Dialog Up: Working with dialogs Previous: Menu Bars

Message Dialog boxes

A programmer frequently wants to ask the user questions for which a yes/no response is appropriate. Motif provides a dialog box (called a message dialog) that makes this easy to do. The following code demonstrates the process. When this code is executed, a pushbuttom appears in the application window. When it is pushed, the dialog box appears. The result of the user's interaction with the dialog box is shown in stdout.


        #include <X11/Intrinsic.h>
        #include <Xm/Xm.h>
        #include <Xm/PushB.h>
        #include <Xm/MessageB.h>

        #define OK      1
        #define CANCEL  2

        void dialogCB(Widget w,
                   int client_data,
                   XmAnyCallbackStruct *call_data)
        {
          switch (client_data)
          {
                case OK:
                        printf("OK selected\n");
                        break;
                case CANCEL:
                        printf("CANCEL selected\n");
                        break;
          }
          XtUnmanageChild(w);
        }

        void buttonCB(Widget w,
                   Widget *b,
                   XmAnyCallbackStruct *call_data)
        {
          XtManageChild(*b);
        }

        main(int argc, char *argv[])
        {
          Widget toplevel, button, dialog;
          Arg al[10];
          int ac;

          toplevel=XtInitialize(argv[0],"",NULL,0,&argc,argv);

          ac=0;
          XtSetArg(al[ac],XmNlabelString, XmStringCreate("Push Me",
                XmSTRING_DEFAULT_CHARSET)); ac++;
          button=XtCreateManagedWidget("label",xmPushButtonWidgetClass,
                toplevel,al,ac);
          XtAddCallback (button, XmNactivateCallback, buttonCB, &dialog);

          ac=0;
          XtSetArg(al[ac], XmNmessageString, 
                XmStringCreateLtoR("Is everything OK?", 
                XmSTRING_DEFAULT_CHARSET));  ac++;
          dialog = XmCreateMessageDialog(toplevel,"ok_dialog", al, ac);
          XtAddCallback (dialog, XmNokCallback, dialogCB, OK);
          XtAddCallback (dialog, XmNcancelCallback, dialogCB, CANCEL);
          XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));

          XtRealizeWidget(toplevel);
          XtMainLoop();
        }

The code starts by setting up a pushbutton. The client_dataparameter of the pushbutton callback receives a pointer to the message dialog widget variable. The dialog box is then created using a convenience function (and note again that it is NOT managed, just created). It's OK and Cancel callbacks are directed into the dialogCB procedure, and constants are passed through the client_dataparameter so that the procedure can distinguish which button was pressed. Notice that the dialog box has a third button (the help button) which is disabled (and also made invisible) by a call to unmanage it. If you want to provide help, add a callback for the button. Or you can change the name used to display the help button and use it for something else. To do this you would change the helpLabelString and then add a helpCallback. To make a message that has only an OK button, unmanage the cancel button as well.

When the pushbutton is pushed, the buttonCB procedure is called. This procedure "manages" the messageDialog. It is the act of managing the dialog that causes it to appear on the screen (when it is unmanaged, it will dissappear). Once the user has made a selection it is unmanaged to make it go away again. It continues to exist, but it must be managed to make it visible and active again.



Next: Prompt Dialog Up: Working with dialogs Previous: Menu Bars


morbe@enstb.enst-bretagne.fr