toggle.c with Explicitly-loaded Pixmaps

This code is adapted from The X Window System: Programming and Applications with Xt, Second OSF/Motif Edition by Douglas A. Young. The original code appears in Chapter 4, p.99.

This version uses explicit loading of the pixmaps into variables using XmGetPixmap(), rather than ataching the pixmaps to widget resources through a resource file.

Original code by Douglas Young. Revisions by Ian Soboroff.

/******************************************************
 * toggle.c: Demonstrate a Motif toggle button widget
 ******************************************************/
#include 
#include 
#include 

Pixmap on, off;

static void ValueChangedCallback ( Widget    w, 
				   XtPointer clientData, 
                                   XtPointer callData );

void main ( int argc, char **argv )
{
    Widget       shell, toggle;
    XtAppContext app;
    Pixel bg, fg;

   /*
    * Initialize Xt
    */
    
    shell = XtAppInitialize ( &app, "Toggle", NULL, 0, 
                              &argc, argv, NULL, NULL, 0 );
    
   /*
    * Create an XmToggleButton widget
    */

    XtVaGetValues(shell,
		  XmNforeground, &fg,
		  XmNbackground, &bg,
		  NULL);
    on = XmGetPixmap(XtScreen(shell), "thumbs_up", fg, bg);
    off = XmGetPixmap(XtScreen(shell), "thumbs_down", fg, bg);
    if (on == XmUNSPECIFIED_PIXMAP || off == XmUNSPECIFIED_PIXMAP)
    {
	fprintf(stderr, "Couldn't load pixmaps!\n");
	exit(1);
    }
    
    toggle = XtVaCreateManagedWidget ( "toggle",
				       xmToggleButtonWidgetClass, shell, 
 				       XmNlabelType, XmPIXMAP,
 				       XmNlabelPixmap, off,
 				       XmNselectPixmap, on,
 				       XmNindicatorOn, False,
				       NULL);

    XtAddCallback ( toggle, XmNvalueChangedCallback, 
                    ValueChangedCallback, NULL );

   /*
    * Realize the shell and enter the event loop.
    */
    
    XtRealizeWidget ( shell );
    XtAppMainLoop ( app );
}

static void ValueChangedCallback ( Widget    w,
                                   XtPointer clientData, 
                                   XtPointer callData )
{
    XmToggleButtonCallbackStruct *cbs = 
                          ( XmToggleButtonCallbackStruct * ) callData;
   /*
    * Report the new state of the toggle button for which this 
    * callback function was called.
    */

    if ( cbs->set )
        printf ( "button set\n" );
    else
        printf ( "button unset\n" );
}

Ian Soboroff -- ian@umbc.edu