1
/****************************************************************************
3
GLUI User Interface Toolkit
4
---------------------------
6
glui_radio.cpp - GLUI_RadioGroup and GLUI_RadioButton control classes
9
--------------------------------------------------
11
Copyright (c) 1998 Paul Rademacher
13
WWW: http://sourceforge.net/projects/glui/
14
Forums: http://sourceforge.net/forum/?group_id=92496
16
This library is free software; you can redistribute it and/or
17
modify it under the terms of the GNU Lesser General Public
18
License as published by the Free Software Foundation; either
19
version 2.1 of the License, or (at your option) any later version.
21
This library is distributed in the hope that it will be useful,
22
but WITHOUT ANY WARRANTY; without even the implied warranty of
23
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24
Lesser General Public License for more details.
26
You should have received a copy of the GNU Lesser General Public
27
License along with this library; if not, write to the Free Software
28
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30
*****************************************************************************/
32
#include "glui_internal_control.h"
35
/****************************** GLUI_RadioGroup::GLUI_RadioGroup() **********/
37
GLUI_RadioGroup::GLUI_RadioGroup(GLUI_Node *parent,
44
set_ptr_val( value_ptr );
46
int_val = *value_ptr; /** Can't call set_int_val(), b/c that
47
function will try to call the
49
/** Actually, maybe not **/
50
last_live_int = *value_ptr;
54
glui_format_str( buf, "RadioGroup: %p", this );
55
set_name( buf.c_str() );
58
parent->add_control( this );
64
/****************************** GLUI_RadioGroup::draw() **********/
66
void GLUI_RadioGroup::draw( int x, int y )
75
/********************* GLUI_RadioGroup::draw_group(int translate) **********/
77
void GLUI_RadioGroup::draw_group( int translate )
79
GLUI_DRAWINGSENTINAL_IDIOM
80
GLUI_RadioButton *button;
81
this->int_val = int_val;
83
glMatrixMode(GL_MODELVIEW );
85
button = (GLUI_RadioButton*) first_child();
86
while( button != NULL ) {
89
button->translate_to_origin();
92
glTranslatef(button->x_abs-x_abs,
93
button->y_abs-y_abs,0.0);
96
if ( button->int_val )
97
button->draw_checked();
99
button->draw_unchecked();
103
button = (GLUI_RadioButton*) button->next();
108
/****************************** GLUI_RadioGroup::set_name() **********/
110
void GLUI_RadioGroup::set_name( const char *text )
119
/********************************* GLUI_RadioGroup::set_selected() **********/
121
void GLUI_RadioGroup::set_selected( int int_val )
123
GLUI_RadioButton *button;
125
this->int_val = int_val;
127
button = (GLUI_RadioButton*) first_child();
128
while( button != NULL ) {
129
if ( int_val == -1 ) { /*** All buttons in group are deselected ***/
130
button->set_int_val(0);
132
else if ( int_val == button->user_id ) { /*** This is selected button ***/
133
button->set_int_val(1);
135
else { /*** This is NOT selected button ***/
136
button->set_int_val(0);
139
button = (GLUI_RadioButton*) button->next();
145
/************************ GLUI_RadioButton::GLUI_RadioButton() **********/
147
GLUI_RadioButton::GLUI_RadioButton( GLUI_RadioGroup *grp, const char *name )
153
/** A radio button's user id is always its ordinal number (zero-indexed)
155
user_id = grp->num_buttons;
159
group->num_buttons++; /* Increments radiogroup's button count */
160
group->add_control( this );
162
/*** Now update button states ***/
163
group->set_int_val( group->int_val ); /* This tells the group to
165
current value, thereby
166
updating all its buttons */
170
/************************ GLUI_RadioButton::mouse_down_handler() **********/
172
int GLUI_RadioButton::mouse_down_handler( int local_x, int local_y )
177
orig_value = group->int_val;
179
currently_inside = true;
181
group->set_selected( this->user_id );
187
/********************** GLUI_RadioButton::mouse_held_down_handler() ******/
189
int GLUI_RadioButton::mouse_held_down_handler( int local_x, int local_y,
192
if (inside != currently_inside) {
193
if (inside) group->set_selected( this->user_id );
194
else group->set_selected( orig_value );
195
currently_inside = inside;
203
/*************************** GLUI_RadioButton::mouse_up_handler() **********/
205
int GLUI_RadioButton::mouse_up_handler( int local_x, int local_y,
212
group->set_selected( orig_value );
216
/** Now we update the radio button group. We tell the group
217
handler to set the currently-selected item to this button, which
218
is reference by its user_id/ordinal number within group **/
220
group->set_selected( this->user_id );
223
/*** Now update the linked variable, and call the callback,
224
but ONLY if the value of the radio group actually changed ***/
225
if ( group->int_val != orig_value ) {
226
group->output_live(true); /** Output live and update gfx ***/
228
group->execute_callback();
235
/****************************** GLUI_RadioButton::draw() **********/
237
void GLUI_RadioButton::draw( int x, int y )
239
GLUI_DRAWINGSENTINAL_IDIOM
241
if ( NOT group OR NOT can_draw() )
244
/*** See if we're the currently-selected button. If so, draw ***/
245
if ( group->int_val == this->user_id ) {
247
glui->std_bitmaps.draw( GLUI_STDBITMAP_RADIOBUTTON_ON, 0, 0 );
249
glui->std_bitmaps.draw( GLUI_STDBITMAP_RADIOBUTTON_ON_DIS, 0, 0 );
253
glui->std_bitmaps.draw( GLUI_STDBITMAP_RADIOBUTTON_OFF, 0, 0 );
255
glui->std_bitmaps.draw( GLUI_STDBITMAP_RADIOBUTTON_OFF_DIS, 0, 0 );
260
draw_name( text_x_offset, 10 );
264
/************************************ GLUI_RadioButton::draw_checked() ******/
266
void GLUI_RadioButton::draw_checked( void )
268
GLUI_DRAWINGSENTINAL_IDIOM
270
glui->std_bitmaps.draw( GLUI_STDBITMAP_RADIOBUTTON_ON, 0, 0 );
272
glui->std_bitmaps.draw( GLUI_STDBITMAP_RADIOBUTTON_ON_DIS, 0, 0 );
277
/*********************************** GLUI_RadioButton::draw_unchecked() ******/
279
void GLUI_RadioButton::draw_unchecked( void )
281
GLUI_DRAWINGSENTINAL_IDIOM
284
glui->std_bitmaps.draw( GLUI_STDBITMAP_RADIOBUTTON_OFF, 0, 0 );
286
glui->std_bitmaps.draw( GLUI_STDBITMAP_RADIOBUTTON_OFF_DIS, 0, 0 );
291
/**************************************** GLUI_RadioButton::draw_O() ********/
293
void GLUI_RadioButton::draw_O( void )
295
GLUI_DRAWINGSENTINAL_IDIOM
298
glBegin( GL_POINTS );
299
for(i=3; i<=GLUI_RADIOBUTTON_SIZE-3; i++ )
300
for(j=3; j<=GLUI_RADIOBUTTON_SIZE-3; j++ )
306
/******************************** GLUI_RadioButton::update_size() **********/
308
void GLUI_RadioButton::update_size( void )
315
text_size = _glutBitmapWidthString( glui->font, name.c_str() );
317
/* if ( w < text_x_offset + text_size + 6 ) */
318
w = text_x_offset + text_size + 6 ;
322
/************************* GLUI_RadioButton::draw_active_area() **************/
324
void GLUI_RadioButton::draw_active_area( void )
326
GLUI_DRAWINGSENTINAL_IDIOM
327
int text_width, left, right;
329
text_width = _glutBitmapWidthString( glui->font, name.c_str() );
330
left = text_x_offset-3;
331
right = left + 7 + text_width;
334
glEnable( GL_LINE_STIPPLE );
335
glLineStipple( 1, 0x5555 );
336
glColor3f( 0., 0., 0. );
338
glColor3ub( glui->bkgd_color.r, glui->bkgd_color.g, glui->bkgd_color.b );
341
glBegin( GL_LINE_LOOP );
342
glVertex2i(left,0); glVertex2i( right,0);
343
glVertex2i(right,h+1); glVertex2i( left,h+1);
346
glDisable( GL_LINE_STIPPLE );
350
/********************************* GLUI_RadioGroup::set_int_val() **********/
352
void GLUI_RadioGroup::set_int_val( int new_val )
354
if ( new_val == int_val )
357
set_selected( new_val );