Next: User Defined Dialogs Up: Dialogs Previous: The prompt.c program

Selection and FileSelection Dialogs

Both these widget allow the user to select from a list displayed within the Dialog.

We will study the FileSelectionDialog as it is more complex.

The FileSelectionDialog (Fig. 6.7) allows the selection of files from a directory which has use in many applications (texteditors, graphics programs etc.)

The SelectionDialog allows more general selection.

Fig. 6.7 file_select.c output

To create a FileSelectionDialog with the XmCreateFileSelectionDialog() function. You must include the <Xm\FileSB.h> header file.

A FileSelectionDialog has many resources you can set to control the search of files: (All are XmString types except where noted).

XmNdirectory
- specify the directory where files are stored. Default current working directory.
XmNdirListLabelString
- the text displayed above directory listing in Dialog.
XmNdirMask
- the mask used to filter out certain files. E.g. in file_select.c this mask is set to *.c so as only to list C source files in the dialog.
XmNdirSpec
- the name of the file (to be) chosen.

XmNfileListLabelString
- text displayed above file list.
XmNfileTypeMask
- the type of file to be listed (char type -XmFILE_REGULAR, XmFILE_DIRECTORY, XmFILE_TYPE_ANY predefined).
XmNfilterLabelString
- text displayed above filter list.

The search directory, directory mask and others can be altered in the Dialog window.

The program file_select.c simply looks for C source files in a directory.

If a file is selected it's listing is printed to standard output.


#include <stdio.h>

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

void ScrubDial(Widget, int);

Widget top_wid;


main(int argc, char *argv[])
{
    XtAppContext app;
    Widget main_w, menu_bar, file_select, quit;
    void  quit_pop_up(), select_pop_up(); /* callbacks */
    
    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 */
        
    file_select = XtVaCreateManagedWidget( "Select",
        xmCascadeButtonWidgetClass, menu_bar,
        XmNmnemonic, 'S',
        NULL);

               
    /* Callback has data passed to */
    XtAddCallback(file_select, XmNactivateCallback, 
                  select_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?");  

    XtRealizeWidget(top_wid);
    XtAppMainLoop(app);
}


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

{   Widget dialog, remove;
    void select_activate(), cancel();
    XmString mask;
    Arg args[1];
       
    /* Create the FileSelectionDialog */     
    mask  = XmStringCreateSimple("*.c");
    XtSetArg(args[0], XmNdirMask, mask);

 
    dialog = XmCreateFileSelectionDialog(cascade_button, 
           "select", args, 1);        
    XtAddCallback(dialog, XmNokCallback, select_activate, 
                  NULL);
    XtAddCallback(dialog, XmNcancelCallback, cancel, NULL);

    remove = XmSelectionBoxGetChild(dialog, 
           XmDIALOG_HELP_BUTTON);
   
    XtUnmanageChild(remove); /* delete HELP BUTTON */
    

    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[2];

    /* 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, 2);
    
    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 select_activate(Widget widget, caddr_t client_data, 
XmFileSelectionBoxCallbackStruct *selection)

{   /* function opens file (text) and prints to stdout */

    FILE *fp, *fopen();
    char *filename, line[200];
    void error();
    
    XmStringGetLtoR(selection->value, 
 XmSTRING_DEFAULT_CHARSET, &filename);

   
    if ( (fp = fopen(filename,"r")) == NULL) 
      error("CANNOT OPEN FILE", filename);
  else
    {  while ( !feof(fp) )
          { fgets(line,200,fp);
            printf("%s\n",line);
          }           
       fclose(fp);         
    }          
}


void cancel(Widget widget, caddr_t client_data, 
XmFileSelectionBoxCallbackStruct *selection)

{ XtUnmanageChild(widget);  /* undisplay widget */   
}

void error(char *s1, char *s2)

{  /* prints error to stdout */
/* EXERCISE: 
      REWRITE THIS ROUTINE TO PUT OUT AN ERROR DIALOG. */
   
   printf("%s: %s\n", s1, s2);
   exit(-1);
   
}


/* 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