Next: Selection and FileSelection Dialogs Up: The PromptDialog Widget Previous: The PromptDialog Widget

The prompt.c program

This program, an extension of dialog2.c, creates a PromptDialog into which the user can enter text.

The PromptDialog first displayed by this program is shown in Fig. 15

The text is echoed in an InformationDialog which is created in a Prompt Callback function, prompt_activate().

Fig. 15 PromptDialog Widget

A PromptDialog Callback has the following structure


void prompt_callback(Widget widget, XtPointer client_data,
XmSelectionBoxCallbackStruct *selection)

Normally we will only be interested in obtaining the string entered to the PromptDialog.

An element of the XmSelectionBoxCallbackStruct, value (XmString data type) holds the string value.

In prompt.c, the callback for the prompt dialog (activated with Ok button) - prompt_activate().

This function use the selection->value to set up the InformationDialog messageString.

Note: That since a PromptDialog is a SelectionBox widget type we must use XmSelectionBoxGetChild() to find any buttons we may wish to remove from a the PromptDialog.

In prompt.c we remove the Help button in this way.


#include <Xm/Xm.h>
#include <Xm/MainW.h>
#include <Xm/CascadeB.h>
#include <Xm/MessageB.h>
#include <Xm/PushB.h>
#include <Xm/SelectioB.h>

void ScrubDial(Widget, int);

Widget top_wid;

main(int argc, char *argv[])
{   XtAppContext app;
    Widget main_w, menu_bar, info, prompt, quit;
    void info_pop_up(), quit_pop_up(), prompt_pop_up();

    
    top_wid = XtVaAppInitialize(&app, "Demos", NULL, 0,
        &argc, argv, NULL, NULL);

    main_w = XtVaCreateManagedWidget("main_window",
        xmMainWindowWidgetClass,   top_wid, 
        XmNheight, 300,
        XmNwidth,300,
        NULL);

        
    menu_bar = XmCreateMenuBar(main_w, "main_list", 
        NULL, 0);        
    XtManageChild(menu_bar);
       
    /* create prompt widget + callback */       
    prompt = XtVaCreateManagedWidget( "Prompt",
        xmCascadeButtonWidgetClass, menu_bar,
        XmNmnemonic, 'P',
        NULL);
                
    /* Callback has data passed to */   
    XtAddCallback(prompt, XmNactivateCallback, 
                  prompt_pop_up, NULL);

    
    
    /* create quit widget + callback */
        
    quit = XtVaCreateManagedWidget( "Quit",
        xmCascadeButtonWidgetClass, menu_bar,
        XmNmnemonic, 'Q',
        NULL);
        
        
    /* Callback has data passed to */
    
    XtAddCallback(quit, XmNactivateCallback, quit_pop_up, 
                  "Are you sure you want to quit?");


    
    /* create help widget + callback */
        
    info = XtVaCreateManagedWidget( "Info",
        xmCascadeButtonWidgetClass, menu_bar,
        XmNmnemonic, 'I',
        NULL);    

    XtAddCallback(info, XmNactivateCallback, info_pop_up, 
            "Select Prompt Option To Get Program Going."); 
    
    XtRealizeWidget(top_wid);
    XtAppMainLoop(app);
}


void
prompt_pop_up(Widget cascade_button, char *text, 
XmPushButtonCallbackStruct *cbs)

{   Widget dialog, remove;
    XmString xm_string1, xm_string2;
    void prompt_activate();
    Arg args[3];

    /* label the dialog */
    xm_string1 = XmStringCreateSimple("Enter Text Here:");
    XtSetArg(args[0], XmNselectionLabelString, xm_string1);
    /* default text string  */
    xm_string2 = XmStringCreateSimple("Default String");

   
    XtSetArg(args[1], XmNtextString, xm_string2);
   /* set up default button for cancel callback */
    XtSetArg(args[2], XmNdefaultButtonType, 
                    XmDIALOG_CANCEL_BUTTON);

    /* Create the WarningDialog */
    dialog = XmCreatePromptDialog(cascade_button, "prompt", 
                                  args, 3);
    
    XmStringFree(xm_string1);
    XmStringFree(xm_string2);

   
    XtAddCallback(dialog, XmNokCallback, prompt_activate, 
                  NULL);
   /* Scrub Prompt Help Button */
    remove = XmSelectionBoxGetChild(dialog, 
                      XmDIALOG_HELP_BUTTON);
   
    XtUnmanageChild(remove);  /* scrub HELP button */ 
    
    XtManageChild(dialog);
    XtPopup(XtParent(dialog), XtGrabNone);
}


void
info_pop_up(Widget cascade_button, char *text, 
XmPushButtonCallbackStruct *cbs)

{   Widget dialog;
    XmString xm_string;
    extern void info_activate();
    Arg args[2];

    /* label the dialog */
    xm_string = XmStringCreateSimple(text);
    XtSetArg(args[0], XmNmessageString, xm_string);
    /* set up default button for OK callback */
    XtSetArg(args[1], XmNdefaultButtonType, 
             XmDIALOG_OK_BUTTON);

   
    /* Create the InformationDialog as child of 
       cascade_button passed in */
    dialog = XmCreateInformationDialog(cascade_button, 
                                       "info", args, 2);
    
    ScrubDial(dialog, XmDIALOG_CANCEL_BUTTON);
    ScrubDial(dialog, XmDIALOG_HELP_BUTTON);

    XmStringFree(xm_string);

    XtManageChild(dialog);
    XtPopup(XtParent(dialog), XtGrabNone);
}


void quit_pop_up(Widget cascade_button, char *text, 
XmPushButtonCallbackStruct *cbs)
{   Widget dialog;
    XmString xm_string;
    void quit_activate();
    Arg args[1];

    /* label the dialog */
    xm_string = XmStringCreateSimple(text);
    XtSetArg(args[0], XmNmessageString, xm_string);
    /* set up default button for cancel callback */
    XtSetArg(args[1], XmNdefaultButtonType, 
                   XmDIALOG_CANCEL_BUTTON);


    /* Create the WarningDialog */
    dialog = XmCreateWarningDialog(cascade_button, "quit", 
                                   args, 1);
    
    ScrubDial(dialog, XmDIALOG_HELP_BUTTON);

    XmStringFree(xm_string);

    XtAddCallback(dialog, XmNokCallback, quit_activate, 
                  NULL);

    XtManageChild(dialog);
    XtPopup(XtParent(dialog), XtGrabNone);
}


/* routine to remove a DialButton from a Dialog */

void ScrubDial(Widget wid, int dial)

{  Widget remove;

   remove = XmMessageBoxGetChild(wid, dial);
   
   XtUnmanageChild(remove);
}


/* callback function for Prompt activate */

void prompt_activate(Widget widget, XtPointer client_data,
XmSelectionBoxCallbackStruct *selection)

{   Widget dialog;
    Arg args[2];
    XmString xm_string;
    
    /* compose InformationDialog output string */
    /* selection->value holds XmString entered to prompt */
    
    xm_string = XmStringCreateSimple("You typed: ");
    xm_string = XmStringConcat(xm_string,selection->value);

 
    XtSetArg(args[0], XmNmessageString, xm_string);
    /* set up default button for OK callback */
    XtSetArg(args[1], XmNdefaultButtonType, 
             XmDIALOG_OK_BUTTON);
  
    /* Create the InformationDialog to echo 
       string grabbed from prompt */
    dialog = XmCreateInformationDialog(top_wid, 
           "prompt_message", args, 2);
    
    ScrubDial(dialog, XmDIALOG_CANCEL_BUTTON);
    ScrubDial(dialog, XmDIALOG_HELP_BUTTON);

    XtManageChild(dialog);
    XtPopup(XtParent(dialog), XtGrabNone);
}



/* callback routines for quit ok dialog */


void
quit_activate(Widget dialog)
{
    printf("Quit Ok was pressed.\n");
    exit(0);
}


Dave.Marshall@cm.cf.ac.uk
Tue May 24 16:52:56 BST 1994