Next: Selection Box Dialogs Up: Working with dialogs Previous: Message Dialog boxes

Prompt Dialog

Motif also supports a dialog box for getting strings from the user. It is called a prompt dialog. It's use is almost identical to that of a message dialog (including OK, Cancel and Help buttons that are accessed the same way), but you have to jump through a hoop to get the string out once the dialog's OK callback is activated. Here's some example code:


        #include <X11/Intrinsic.h>
        #include <Xm/Xm.h>
        #include <Xm/PushB.h>
        #include <Xm/SelectioB.h> /* a prompt dialog is made from 
                                     a stripped-down selection box. */

        #define OK      1
        #define CANCEL  2

        void dialogCB(Widget w,
                   int client_data,
                   XmAnyCallbackStruct *call_data)
        {
          char *s;
          XmSelectionBoxCallbackStruct *selection;

          switch (client_data)
          {
                case OK:
                        selection=(XmSelectionBoxCallbackStruct *) call_data;
                        XmStringGetLtoR(selection->value, 
                                XmSTRING_DEFAULT_CHARSET, &s);
                        printf("string='%s'\n",s);
                        XtFree(selection);
                        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], XmNselectionLabelString, XmStringCreateLtoR
                ("Type in a string. ", XmSTRING_DEFAULT_CHARSET));  ac++;
          dialog = XmCreatePromptDialog(toplevel, "dialog", al, ac);
          XtAddCallback (dialog, XmNokCallback, dialogCB, OK);
          XtAddCallback (dialog, XmNcancelCallback, dialogCB, CANCEL);
          XtUnmanageChild(XmSelectionBoxGetChild(dialog,
                XmDIALOG_HELP_BUTTON));

          XtRealizeWidget(toplevel);
          XtMainLoop();
        }

The code in the "Case OK:" for the dialogCB routine extracts the string. It gets it from the event record generated for the dialog, passed in call_data. Besides this special string-extraction code, and the use of a different convenience function to create a different dialog box, the rest of the program is identical to the code used to demonstrate the message dialog.


morbe@enstb.enst-bretagne.fr