Photon Tutorial: Radio Buttons
This is not the most exciting demonstration, but it does at least
illustrate how to create radio buttons in about as concise a way as
is possible. Radio buttons are buttons that are mutually exclusive
in that setting one resets any others that are associated with the
button just set. Radio buttons are not a standalone widget in
Photon; instead, you have to use a toggle button, set it to work
'exclusively', then group together the buttons that are to work in
mutual exclusion.
- Run Photon Application Builder.
- Click on the File menu, and click New.
- PhAB presents a list of window styles to chose from, the
default “Plain” is fine, so just click
“Done”.
- Name and save this project by clicking on the File menu, then
clicking “Save As...”. Enter demo_radbut
in the “Name” box, then click
“Save”.
- In the PhAB control panel, click on the
“Resources” tab.
- Click in the name window at the top of the resources control
panel (this will contain the text “base” to begin
with). Enter a name for the window by typing
wndBase.
- Click on “Window Title” in the resource list, and
enter the text Radio Button Demo then click
“Done”.
- Click on the “Widgets” control panel tab.
- Click on “Toggle Button”, then click on the
window you just created at the position where you want the button
to appear.
- Click on the control panel's “Resources”
tab.
- Click on the name box in the control panel, which should
currently contain
“PtToggleButton” and type
btnRadioSW.
- Click on “Toggle Text” in the resources list,
type in Short Wave, and click “Done”.
Below is a screenshot of how my screen appears at this stage.

- Click on “Indicator Type” in the resource list,
select “Pt_TOGGLE_RADIO”, then click
“Done”.
- Right-click on the radio button you just created, and click
“Copy”.
- Right-click on the wndBase window, and click
“Paste”. The mouse pointer will change and you should
now click on the position where you want the next radio button to
appear.
- With this button still selected, click in the name box in the
Resources control panel, and type btnRadioMW.
- Click on “Toggle Text”, type Medium
Wave, and click “Done”.
- Create a third button by right-clicking in the window again,
click “Paste”, then click where you want this button
to appear.
- With this button still selected, click in the name box in the
Resources control panel, type btnRadioLW.
- Click on “Toggle Text”, type Long
Wave, then click “Done”.
- To align the buttons, first move the mouse above and to the
left of the buttons then click–and–drag to form a box
around the buttons. Release the mouse button and a single set of
resize markers will appear around the three buttons.
- Click on “Align” in the toolbar on the left of
PhAB's window and click “Align left”.
- Click the
“Group” button in the toolbar under PhAB's main
menu; this creates a new PtGroup widget, which will
automatically contain our selected radio buttons. Below is a
screenshot of how my system appears at this point.

- Click in the name box of the Resources control panel (which
should currently contain “PtGroup”, and type
grpWaveband
- Click on “Group Flags” in the resources list,
select “Pt_GROUP_EXCLUSIVE”, then click
“Done”.
- Click “Application” in PhAB's main menu and
select “Startup Info/Modules...”.
- In the “Global header” box (the cursor should
already be in this box), type globals and click the
edit icon on the right.
- Using the editor, enter the code shown below, then save the
file and quit the editor.
#ifdef DEFINE_GLOBALS
#define GLOBAL
#else
#define GLOBAL extern
#endif
#define BAND_LONGWAVE 1
#define BAND_MEDIUMWAVE 2
#define BAND_SHORTWAVE 3
GLOBAL int waveband;
- In the editor, open the file
indHfiles, and append the string
../globals.h
to the single line (which should start
with MYHDR =
). This tells PhAB to ensure that our
header file is included in the Makefile, so that any files that
#include it are recompiled when it changes.
- You should now be back in the Startup Info dialog box. In the
Initialisation function box, type init and click the
edit icon.
- Using the editor, enter the code
#define
DEFINE_GLOBALS
outside the init()
function
definition. Also, add the code waveband =
BAND_SHORTWAVE
within the init()
function
definition. This piece of code merely sets the default
(start–up) value for the waveband variable. This value will
be used later to initialise the state of the radio buttons on
program start–up. Note that waveband
is a
global variable shared by more than one file. Therefore, we have
to define it once (i.e. create its storage space, which
done in the init.c file) and then in each file that uses it, we
have to declare it (merely report that we may use it, in
this case, we use the extern
keyword). This is
achieved using the
DEFINE_GLOBALS
coding technique.
- Save the file and quit the editor.
- Back in the Startup Info dialog box, click
“Done”.
- Our group of radio buttons should still be selected (the
resize markers are visible around them). Select the
“Callbacks” tab in the control panel then click on
“Realized”.
- In the “Function” text box, type
initRadioButtons and click on the “Apply”
button at the bottom edge of the dialog box.
- Click the create icon next to the “Function” text
box. In the editor, add
switch ( waveband )
{
case BAND_LONGWAVE:
PtSetResource( ABW_btnRadioLW, Pt_ARG_FLAGS, Pt_TRUE, Pt_SET);
break;
case BAND_MEDIUMWAVE:
PtSetResource( ABW_btnRadioMW, Pt_ARG_FLAGS, Pt_TRUE, Pt_SET);
break;
case BAND_SHORTWAVE:
PtSetResource( ABW_btnRadioSW, Pt_ARG_FLAGS, Pt_TRUE, Pt_SET);
break;
}
- Save the file and quit the editor.
- You should now be back in the Callback dialog box. Click the
“Done” button in the bottom right hand corner of the
dialog box.
- Click on the control panel's “Module Tree” tab.
You should see a list of the main widgets in your application.
“baseWindow” and “grpWaveband” should be
visible, with “grpWaveband” already selected.
- There should be a small triangle next to
“grpWaveband” that points to the right. Click this
triangle and the three radio button widgets should be
listed.
- Select “btnBandSW” then click on the control
panel's tab and select “Callbacks”.
- From the list of events, click “Activate” and in
the function text box type setBandSW. Click Apply (at
the bottom edge of the dialog) and then click on the create
icon.
- Using the editor, add the code
waveband =
BAND_SHORTWAVE;
within the body of the function.
- Save the file and quit the editor.
- You should now be back in the Callbacks dialog box. Click
“Done” in the bottom right.
- Press the F10 key to change to the next button in
the tab order, and you should see the “btnRadioMW”
become selected.
- Click on “Activate” in the event list and type
setBandMW in the “Function” text box.
- Click “Apply”, then click the create button.
Enter the code
waveband = BAND_MEDIUMWAVE;
in the
editor
- Save the file and close the editor.
- Press F10 again to select “btnBandLW”
and repeat the steps, using LW or LONGWAVE in the obvious places
in the function name and its contents. Save the file and quit the
editor.
- Once back in the Callbacks dialog box, click
“Done”.
- Save your work so far, by clicking on “File” in
the menu, then clicking “Save”.
- Click “Application” in PhAB's main menu, then
select “Build & Run”.
- Click “Generate” and a dialog box will appear
where you select which target platform you want to compile
for.
- Click “Generate...” in the new dialog box. When
the generation has been completed, click “Done”.
- Click “Make” to compile the code. Once this is
complete, click “Done”.
- The program is now ready to run, so click
“Run”.
Now, when you click any of the radio buttons, if it is not
already set, it will become set, and the other button that was set
will become unset. The use of the global variable allows the
multiple files to keep track of the state that the variable (and
thus the program) is in. Here's the finished product:
Content last updated: 2004-03-22