Go to the first, previous, next, last section, table of contents.

RadioButton in action

To create a RadioButton, use radioButton function giving the label picture to use:


radioButton :: Picture -> Component (RadioButton, DisplayHandle)

main =
 wopen ["*name: RBtns"] 
 (\dc ->
   radioButton (text "option-1") dc >>= \ (_,dh1) ->
   radioButton (text "option-2") dc >>= \ (_,dh2) ->
   radioButton (text "option-3") dc >>= \ (_,dh3) ->
   return ((), vbox [dh1,dh2,dh3])) >>
 return ()

A pair of radiobuttons

The interaction behaviour and looks is based on the radio item for the Tcl/Tk toolkit, which again is based on the radio item in the Motif toolkit.

The RadioButton is just an instance of a Toggle, so to catch clicks on the radio button, waitToggleClick is your friend:


type RadioButton = Toggle Bool

waitToggleClick :: RadioButton -> IO Bool

monitorClicks :: RadioButton -> MVar (Maybe RadioButton) -> IO ()
monitorClicks rb currentSelection =
  {-
    wait for changes in radiobutton's on/off state.
    This may originate from the user or other processes
    signalling that the radiobutton is not the current
    choice anymore.
   -}
 waitToggleClick rb >>= \v ->
 let
  str =
   "Turned " ++
   if v then 
      "on"
   else 
      "off"
 in
 setToggleLabel rb v (text str)  >>
 if v then
   swapMVar currentSelection (Just rb) >>= \old ->
   case old of
     Nothing    -> monitorClicks rb currentSelection
     Just oldRb ->
       {- Turn off previously selected item -}
       setToggle oldRb False >>
       monitorClicks rb currentSelection
 else
   monitorClicks rb currentSelection

main =
 newMVar Nothing >>= \selected ->
 wopen [] 
 (\dc ->
    {-
      Create three RadioButtons that will form an exclusive
      choice group. Each RadioButton has a process attached to it,
      monitoring changes to the on/off state of the button.
      
      To track the current choice of the group, a variable
      is used. Once a radiobutton is selected, the monitoring
      process for it will tell the previous selected item to
      turn itself off. A simple-minded implementation of the
      functionality provided by RadioGroups.
     -}
   radioButton (text "Turned off") dc >>= \ (btn1,dh1) ->
     forkIO (monitorClicks btn1 selected)    >>

   radioButton (text "Turned off") dc >>= \ (btn2,dh2) ->
     forkIO (monitorClicks btn2 selected)    >>

   radioButton (text "Turned off") dc >>= \ (btn3,dh3) ->
     forkIO (monitorClicks btn3 selected)    >>
   return ((), vbox [dh1,dh2,dh3] dc))  >>
 return ()

Sample radiogroup


Go to the first, previous, next, last section, table of contents.