# gCheckbox

Definition: | include xpGUI.e gdx id = gCheckbox( [nullable_string title=NULL, ] [rtn value_changed=NULL, ] string attributes="", dword_seq args={})
Description: | Creates a checkbox interface element, also known as a toggle. It is a two-state (on/off) button. title: Text to be shown, can be NULL. Sets the TITLE attribute. value_changed: an (optional) procedure invoked when toggled, see VALUE_CHANGED below. For more information on the attributes and args parameters see gSetAttributes(). This is a paranormalised function (see technicalia). Returns: the identifier of the created element.
pwa/p2js: | Supported.
See Also: | gImage, gButton, gLabel
Example: |
    -- demo\xpGUI\gCheckbox.exw
    include xpGUI.e

    procedure value_changed(gdx chk, bool bState)
        gdx dlg = gGetDialog(chk)
        gSetAttribute(dlg,"TITLE","gCheckbox:%t",{bState})
    end procedure

    gdx chk = gCheckbox("Is this checked?",value_changed),
        hbx = gHbox({chk},"MARGIN=9x18"),
        dlg = gDialog(hbx,"gCheckbox","SIZE=240x80")
    gShow(dlg)
    gMainLoop()
Notes: | Checkboxes are activated/toggled using the Space key, or by being clicked. Since GTK offers no apparent facility for checkboxes on the right of the text, the native support for such in WinAPI is not exposed, and all I can suggest is emulating that with something perhaps like gHbox({gLabel(txt),gCheckbox("")}).
Radio Groups: | gRadio(gdx ids) specifies a set of mutually exclusive toggles, where ids is a (flat) sequence of toggles which have already been added to the dialog hierarchy by some other means. Note that radio groups (and checkboxes) are handled quite differently on menus. Should a dialog contain more than one radio group, be aware the first element of ids and the tab order of the dialog determine radio groups under WinAPI, whereas they are more explicitly managed under GTK and JavaScript. A toggle cannot be part of more than one radio group. Use gdx selected = gRadioItem(gdx id), where id is any element of ids, to obtain the currently selected radio button from that group, 0 if none, or -1 if it is not part of any radio group. Note that within a VALUE_CHANGED handler, where it is not really needed anyway, under WinAPI it returns the previous (off) selection, whereas under GTK and JavaScript [DEV testme] it gets the id of the latest (on). Also note that results from gRadioItem() before the dialog has been mapped/displayed are officially undefined/unsupported. You would normally set a default explicitly, otherwise different backends may default differently, and otherwise the VALUE attribute may be used as normal, except it is illegal to set a radio group toggle off, and that any multiple pre-mapping ons would be applied in mapping rather than chronological order.
    -- demo\xpGUI\gRadio.exw
    include xpGUI.e

    procedure radio_changed(gdx toggle, bool bState)
        string title = gGetAttribute(toggle,"TITLE")
        printf(1,"toggle(%s), state:%t\n",{title,bState})
    end procedure

    gdx y = gCheckbox(`You`,radio_changed),
        g = gCheckbox(`get`,radio_changed,`VALUE=ON`),
        i = gCheckbox(`in`,radio_changed),
        t = gCheckbox(`the`,radio_changed),
        b = gCheckbox(`bowl`,radio_changed),
    ygitb = {y,g,i,t,b},
     hbox = gHbox(ygitb,"MARGIN=9x8"),
      dlg = gDialog(hbox,"gRadio","SIZE=240x80")
    gRadio(ygitb)
    gSetAttribute(y,"FONT","Arial, italic 9")
    gShow(dlg)
    gMainLoop()
Attributes: |
VALUE | Toggle’s state. Can be true/"ON", false/"OFF" or "TOGGLE" (inverts the current state). Default: false. A toggle in a radio group cannot be set to false/off, setting it on automatically clears the "previously on" toggle.
also | ACTIVE, CANFOCUS, FONT, EXPAND, TIP, TITLE SIZE, VISIBLE.
Handlers: |
VALUE_CHANGED | Called after the value was interactively changed by the user. procedure value_changed(gdx id[, bool bChecked]) id: identifier of the element that activated the event. bChecked: true(1) if the toggle’s state is on; false(0) if it is off. Note that in a radio group, the raw WinAPI has a tendency to send "off" signals to all other toggles, whereas GTK more sensibly only signals the one off toggle that actually changed, hence xpGUI deliberately only forwards the "on" signal for toggles that are part of a radio group. As mentioned above, using gRadioItem() within a VALUE_CHANGED handler (for an item of that very same radio group) is rather foolish, and thankfully completely pointless, since you have already been (/only get) passed the "on" id anyway. For that reason, bChecked can be omitted, however a fatal error occurs should a handler without it ever be triggered on a non-radio checkbox.
also | KEY: All common handlers are supported.
Technicalia | As this is a paranormalised function, and with no expectation that you should attempt to memorise this lot, except maybe "traa", you can assume that all of the following are potentially perfectly valid, with the fairly obvious restrictions that any parameters must be in strict traa-order, and no args without attributes:
                            gCheckbox()
                            gCheckbox(value_changed)
                            gCheckbox(value_changed, attributes)
                            gCheckbox(value_changed, attributes, args)
                            gCheckbox(attributes, args)
                            gCheckbox(attributes)                    -- (see 2nd note)
                            gCheckbox(title)
                            gCheckbox(title, value_changed)
                            gCheckbox(title, attributes)
                            gCheckbox(title, attributes, args)
                            gCheckbox(title, value_changed, attributes)
                            gCheckbox(title, value_changed, attributes, args)  -- (the full set)
Note that in practice title and value_changed are actually defined as object, and attributes as a sequence rather than a string, but they are all manually and thoroughly verified to be of the documented types, after being repositioned via xpGUI.e/paranormalise_traa(). Note that at creation gCheckbox(attributes) is distinguished from gCheckbox(title) by the presence of an '=' character, see here. Tri-state checkboxes, which I have never personally found much use for, are left as an exercise for the reader.