CS551 - Self-Study Exercises
Exercise 6: Toggles and Radio Groups
So-called "toggle" buttons and "radio lights" are among the most common
components of graphical user interfaces. This exercise shows how they
differ from other forms of buttons and labels, and provides more practice
with resource settings.
You should have read all of Chapter 2 as well as Chapter
5.1 in Nye and O'Reilly, Vol. 4, before starting this exercise.
- Make a copy of the labels application you used in Exercise 1,
calling it toggles (and making all changes to resources and
filenames needed to support that application name).
- Now modify the program so that the short labels ("label_N") are
Toggles instead of Labels (see vol. 5 of the O'Reilly series, or the
man pages on the widgets, for information on Toggles). Compile and
run your program.
How, if at all, does this change the behavior of the
application?
- One feature of the Toggle widget is that it can be grouped with
other Toggles to form a "radio group," where just one of the Toggles
can be selected at a time. Change your code so that the Toggles
inside each box form a radio group. To do this, you will need to
create a variable to store the first Toggle's widget identifier
(rather than discarding it). Then, change the loop that creates the
remaining Toggles so that it also sets the XtNradioGroup
resource to that widget identifier. Compile and run the program.
Observe the effects of the radio group.
- It is also possible to "pre-select" one of the Toggles in a radio
group.
Which resource setting accomplishes this?
Change the code to pre-select the first toggle.
- Change fill_box() so that you no longer discard the widget
identifier from the Toggles created inside the loop. Now add a
callback immediately after each Toggle is created; note that all
Toggles should share a single callback function. At first, just
have the callback print a message indicating that it was invoked.
Now make the callback receive a value from the application. Try
passing the "number" of the toggle (the same N that is printed
in "label_N") so that the callback can print the number of the Toggle
which was selected. Don't forget to cast the int to
XtPointer and then re-cast it before printing.
How many times is the callback actually invoked? What does this
tell you about how radio groups work?