Subsections

Toggle Widgets

   

A Toggle Widget basically provides a simple switch type of selection. The Toggle is either a square or circle shape indicator which if pressed can be turned on and if pressed again turned off. Text and pictorial items (pixmaps) can be used to label a Toggle .

Several Toggle widgets can be grouped together to allow greater control of selection. Motif provides two methods of grouping Toggles together.

RadioBox Widget
    -- Only one of the group can be on at any given time. Toggles are diamond (Motif 1.2) or circular (CDE 1.0 and Motif 2.0) in this widget (Fig 14.1).  

Fig. 14.1 A RadioBox set of Toggle Widgets

CheckBox Widget
    -- More than one Toggle can be on at any time. Toggles are square. (Fig. 14.2).

 

Fig. 14.2 A CheckBox set of Toggle Widgets

Toggle Basics

 

To create a single Toggle use XtVaCreateManagedWidget()  with a
xmToggleButtonWidgetClass  pointer or use XmCreateToggleButton() .

The header file <Xm/ToggleB.h> holds definitions etc for this widget.

Several Toggle Resources  are relevant to the programmer:

XmNindicatorType
  -- Defines the mode of operation of the Toggle. Set this resource to XmN_OF_MANY for a CheckBox    or XmONE_OF_MANY for a RadioBox   .
XmNindicatorOn
  -- Set this resource to True to turn indicator on. The default is False (off).
XmNindicatorSize
  -- Changes indicator size. Size is specified in Pixel unit size.
XmNlabelType
  -- Either set to XmSTRING or XmPIXMAP. Defines the type of label associated with the Toggle.
XmNlabelString
  -- The XmString label of Toggle.
XmNlabelPixmap
  -- The Pixmap of an unselected Toggle.
XmNselectPixmap
  -- The Pixmap of a selected Toggle.
XmNselectColour
  -- The colour of a selected Toggle. (Pixel data type).

The Pixmap is a standard X data type. You can use XmGetPixmap() to load in a Pixmap from a file. (See Chapter 15 on Graphics and Xlib for further details.)

Toggle Callbacks

 

Toggle Callbacks have the usual callback function format and are prototyped by:

void toggle_cbk(Widget, XtPointer, 
                XmToggleCallbackStruct *).

Toggle Callbacks are activated upon an XmNvalueChangedCallback. The XmToggleCallbackStruct has a boolean element set that is True if the Toggle is on. The toggle.c (Section 14.3 below) illustrates the use of Toggle callbacks.

The toggle.c program

   This program creates two CheckBox Toggles in a RowColumn Widget. When their Callbacks are activated, the state of the Toggle is interrogated and a message is printed to standard output.

#include <Xm/Xm.h>
#include <Xm/ToggleB.h>
#include <Xm/RowColumn.h>

/* Prototype callback */
void toggle1_cbk(Widget , XtPointer ,
XmToggleButtonCallbackStruct *), 
void toggle2_cbk(Widget , XtPointer ,
XmToggleButtonCallbackStruct *);
 
main(int argc, char **argv)

{
    Widget toplevel, rowcol, toggle1, toggle2;
    XtAppContext app;
    

    toplevel = XtVaAppInitialize(&app, "Toggle", NULL, 0,
        &argc, argv, NULL, NULL);

    rowcol = XtVaCreateWidget("rowcol",
        xmRowColumnWidgetClass, toplevel, 
        XmNwidth, 300,
        XmNheight, 200,
        NULL);

    toggle1 = XtVaCreateManagedWidget("Dolby ON/OFF",
        xmToggleButtonWidgetClass, rowcol, NULL);
        
    XtAddCallback(toggle1, XmNvalueChangedCallback, 
                  toggle1_cbk, NULL);
    
    toggle2 = XtVaCreateManagedWidget("Dolby B/C",
            xmToggleButtonWidgetClass, rowcol, NULL);
        
    XtAddCallback(toggle2, XmNvalueChangedCallback, 
                  toggle2_cbk, NULL);
    XtManageChild(rowcol);
    XtRealizeWidget(toplevel);
    XtAppMainLoop(app);
}
 
void toggle1_cbk(Widget widget, XtPointer client_data,
XmToggleButtonCallbackStruct *state)

{  printf("%s: %s\n", XtName(widget), 
            state->set? "on" : "off");
}


void
toggle2_cbk(Widget widget, XtPointer client_data, 
XmToggleButtonCallbackStruct *state)

{ printf("%s: %s\n", XtName(widget), state->set ? "B" : "C");
}

Grouping Toggles

 

You can, if you wish, group RadioBoxes or CheckBox Toggles in RowColumn, or Forms yourself (as has been done in the above toggle.c program).

However, Motif provides a few convenience functions, XmCreateSimpleRadioBox()     and XmCreateSimpleCheckBox()     are common examples.

Basically, these are RowColumn Widgets with Toggle children created automatically. Appropriate resources can be set, e.g. XmNindicatorType or XmNradioBehaviour (in this enhanced RowColumn Widget) .



Dave Marshall
1/5/1999