The Switch Object

Synopsis

Class Name: Switch
Valid Parents: Menu, Popup, Dialog, Bulletin
Valid Children: Popup

Description

A small button that can be toggled between selected and unselected states. A label is displayed to the right of the switch.

  • When the switch is pressed, its state changes automatically and a callback is invoked with the switch in its changed state.
  • You can use the functions GuiSwitchGetState and GuiSwitchSetState to get and set the switch state.

Inherited Resources

New Resources

Name Type Options
XuNguiSwitchState float (boolean) true or false
  • The state is false when the switch is not selected, and true when it is.
  • This resource can be retrieved or set. The functions GuiSwitchGetState and GuiSwitchSetState can also be used.
Name Type Options
XuNguiSwitchType string "check" or "radio"
  • Sets the type of the switch button.
  • A check button is used when multiple switches can be set, a radio button when only one of many can be set.
  • Only the type of button is affected, the behavior of the button is controlled by the application.
Name Type Default
XuNguiSpacing float 4
XuNguiRadioGroup float unset
  • XuNguiSpacing defines the distance in pixels between the button and the label. This resource only applies when running under Motif.
  • XuNguiRadioGroup is a convinience resource to be used by the callback function to group radioboxes.

Callback Information

Reason Event
XuCR_VALUE_CHANGED MB1 clicked on switch button or label..
  • A callback occurs any time the switch changes state. Query the XuNguiSwitchState resource to determine if the new state is true or false.

Example Code

function GuiRadioButtonCB(string o, string d, float r)
  string child, parent;
  child = strrchr(o,".");
  parent = substr(o,1,strlen(o)-strlen(child));
  for child in parent+"."+childrenof($parent)
    if typeof($child)<>"Switch" continue;
    if $child.XuNguiSwitchType<>"radio" continue;
    GuiSwitchSetState(child, o=child, false);
  endfor
endfunction

MENUBAR = all("Menubar");
MENUBAR = MENUBAR[1];

create Menu $(MENUBAR).User
 ( XuNguiMnemonic = "U"
 );
for i = 1 to 4
  PTR = MENUBAR+".User.Option"+i;
  create Switch $(PTR)
   ( XuNguiLabel = "Option "+i,
     XuNguiCallback = "GuiRadioButtonCB",
     XuNguiSwitchType = "radio"
   );
endfor
GuiSwitchSetState(MENUBAR+".User.Option1", true, true);

Return to Gsharp Applications Reference Manual