Next: Scale Widgets Up: List Widgets Previous: List Callbacks

list.c program

An example of a List in action is given in list.c.

We create a simple list that shows a selection of colours. Selection of these colours changes the background colour of the List widget just as the menu_pull.c program did.

#include <Xm/Xm.h>
#include <Xm/List.h>

String colours[] = { "Black",  "Red", "Green", 
                     "Blue", "Grey"};

Display *display; /* xlib id of display */
Colormap cmap;

main(argc, argv)
char *argv[];
    Widget           top_wid, list;
    XtAppContext     app;
    int              i, n = XtNumber(colours);
    XColor           back, fore, spare;
    XmStringTable    str_list;
    Arg              args[4];
    void             list_cbk();

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

    str_list = 
      (XmStringTable) XtMalloc(n * sizeof (XmString *));

    for (i = 0; i < n; i++)
        str_list[i] = XmStringCreateSimple(colours[i]);

    list = XtVaCreateManagedWidget("List",
        xmListWidgetClass,     top_wid,
        XmNvisibleItemCount,   n,
        XmNitemCount,          n,
        XmNitems,              str_list,
        XmNwidth,               300,
        XmNheight,              300,

    for (i = 0; i < n; i++)
    /* background pixel to black foreground to white */
    cmap = DefaultColormapOfScreen(XtScreen(list));
    display = XtDisplay(list);
    XAllocNamedColor(display, cmap, colours[0], &back, 
    XAllocNamedColor(display, cmap, "white", &fore, &spare);

    n = 0;
    XtSetArg(args[n],XmNbackground, back.pixel);      
    XtSetArg(args[n],XmNforeground, fore.pixel);      
    XtSetValues(list, args, n);
    XtAddCallback(list, XmNdefaultActionCallback, list_cbk, 


/* called from any of the "Colour" list items.  
   Change the color of the list widget. 
   Note: we have to use dynamic setting with setargs()..
void list_cbk(Widget w, XtPointer data, 
         XmListCallbackStruct *list_cbs)

{   int n =0;
    Arg args[1];
    String selection;
    XColor xcolour, spare; /* xlib color struct */
    /* list->cbs holds XmString of selected list item */
    /* map this to "ordinary" string */

    XmStringGetLtoR(list_cbs->item, XmSTRING_DEFAULT_CHARSET, 
    if (XAllocNamedColor(display, cmap, selection, 
                         &xcolour, &spare) == 0)
    XtSetArg(args[n],XmNbackground, xcolour.pixel);      
    /* w id of list widget passed in */
    XtSetValues(w, args, n); 
Tue May 24 16:52:56 BST 1994