Next: ErrorWorking and Question Dialogs Up: Dialogs Previous: The InformationDialog Widget

The dialog1.c program

This program use Warning and Information Dialogs. These Dialogs are based on the MessageBox widget and so we must include <Xm/MessageB.h> header file.


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

main(int argc, char *argv[])
{
    XtAppContext app;
    Widget top_wid, main_w, menu_bar, info, quit;


    /* callback for the pushbuttons.  pops up dialog */
    void info_pop_up(), quit_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 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, 
"Dialog widgets added to give info and check quit choice"); 
    
    XtRealizeWidget(top_wid);
    XtAppMainLoop(app);
}


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

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

    /* label the dialog */
    xm_string = XmStringCreateSimple(text);
    XtSetArg(args[0], XmNmessageString, xm_string);


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

    XmStringFree(xm_string);

    XtAddCallback(dialog, XmNokCallback, info_activate, 
                  NULL);

    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);


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

    XmStringFree(xm_string);

    XtAddCallback(dialog, XmNokCallback, quit_activate, 
                  NULL);

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


/* callback routines for dialogs */

void
info_activate(Widget dialog)
{
    printf("Info Ok was pressed.\n");
}


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