Why does pressing a toggle button that is already selected
cause the callback to be called twice?
|
4-Jan-02 14:00 GMT
|
Question:
I have an application containing an XmRadioBox with two XmToggleButtons.
If I press a toggle button that is already selected my value changed
callback gets called twice.
The behavior, confusing as it looks, is actually correct.
The value changed callbacks are called twice as follows:
- once for the previous toggle going off
- once for the new toggle going on
Now if you press a toggle that is already on in a radio box, the old and
the new toggle are the same, so the callback goes off for the same object.
Fortunately you can distinguish what is going on by looking at the callback
data.
For the old toggle, the "event" element of the XmToggleButtonCallbackStruct
will be NULL. The "set" element will also be zero, except if the same
toggle has been pressed twice. For the new toggle, the "event" element will
be properly set to point to some XButtonPressedEvent structure, and the
"set" element will be true.
Thus:
- event is NULL, set is TRUE
- hold on, another callback is coming for this toggle - it has been
pressed again
- event is NULL, set is FALSE
- this was the previous selection - user has chosen a different toggle
- event is not null, set is TRUE
- this is the new toggle
- event is not null, set is FALSE
- only possible in a radio box if XmNradioAlwaysOne is FALSE. In
which case, there are now no toggles set as the user has turned off the
only selection.
It is also important to note that Motif 2.1 introduces an additional toggle
state called indeterminate. When coding for Motif 2.1 this fact needs to be
taken into consideration in the callback.
Hence if you want to process things only once, when a toggle is turned on,
then this is the correct form of the callback:
void toggle_changed_callback(Widget w, XtPointer client, XtPointer call)
{
XmToggleButtonCallbackStruct *tp = (XmToggleButtonCallbackStruct *) call ;
if ((tp->event != (XEvent *) 0)) {
// do application action
#if (XmVERSION > 1)
/* Motif 2.1 */
if (tp->set == XmSET) {
// toggle is ON
...
}
else if (tp->set == XmINDETERMINATE) {
// toggle is in third indeterminate state
...
}
else if (tp->set == XmUNSET) {
// toggle is OFF
...
}
#else /* (XmVERSION > 1) */
/* Motif 1.2 */
if (tp->set) {
// toggle is ON
...
}
else {
// toggle is OFF
...
}
#endif /* (XmVERSION > 1) */
}
}
Sponsored
by X-Designer - The Leading X/Motif GUI Builder
- Click to download a FREE evaluation
Goto top of page
|