forked from libglui/glui
-
Notifications
You must be signed in to change notification settings - Fork 2
/
glui_radio.cpp
375 lines (273 loc) · 9.2 KB
/
glui_radio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/****************************************************************************
GLUI User Interface Toolkit
---------------------------
glui_radio.cpp - GLUI_RadioGroup and GLUI_RadioButton control classes
--------------------------------------------------
Copyright (c) 1998 Paul Rademacher
WWW: https://github.com/libglui/glui
Issues: https://github.com/libglui/glui/issues
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*****************************************************************************/
#include "glui_internal_control.h"
#include "tinyformat.h"
#include <cassert>
/****************************** GLUI_RadioGroup::GLUI_RadioGroup() **********/
GLUI_RadioGroup::GLUI_RadioGroup(GLUI_Node *parent,
int *value_ptr,
int id, GLUI_CB cb)
{
common_init();
set_ptr_val( value_ptr );
if ( value_ptr ) {
int_val = *value_ptr; /** Can't call set_int_val(), b/c that
function will try to call the
callback, etc */
/** Actually, maybe not **/
last_live_int = *value_ptr;
}
user_id = id;
GLUI_String buf = tfm::format("RadioGroup: %p", this );
set_name( buf.c_str() );
callback = cb;
parent->add_control( this );
init_live();
}
/****************************** GLUI_RadioGroup::draw() **********/
void GLUI_RadioGroup::draw( int x, int y )
{
if ( NOT can_draw() )
return;
draw_group(false);
}
/********************* GLUI_RadioGroup::draw_group(int translate) **********/
void GLUI_RadioGroup::draw_group( int translate )
{
GLUI_DRAWINGSENTINAL_IDIOM
GLUI_RadioButton *button;
this->int_val = int_val;
glMatrixMode(GL_MODELVIEW );
button = (GLUI_RadioButton*) first_child();
while( button != NULL ) {
glPushMatrix();
if (translate) {
button->translate_to_origin();
}
else {
glTranslatef(button->x_abs-x_abs,
button->y_abs-y_abs,0.0);
}
if ( button->int_val )
button->draw_checked();
else
button->draw_unchecked();
glPopMatrix();
button = (GLUI_RadioButton*) button->next();
}
}
/****************************** GLUI_RadioGroup::set_name() **********/
void GLUI_RadioGroup::set_name( const GLUI_String &text )
{
name = text;
if ( glui )
glui->refresh();
}
/********************************* GLUI_RadioGroup::set_selected() **********/
void GLUI_RadioGroup::set_selected( int int_val )
{
GLUI_RadioButton *button;
this->int_val = int_val;
button = (GLUI_RadioButton*) first_child();
while( button != NULL ) {
if ( int_val == -1 ) { /*** All buttons in group are deselected ***/
button->set_int_val(0);
}
else if ( int_val == button->user_id ) { /*** This is selected button ***/
button->set_int_val(1);
}
else { /*** This is NOT selected button ***/
button->set_int_val(0);
}
button = (GLUI_RadioButton*) button->next();
}
redraw();
}
/************************ GLUI_RadioButton::GLUI_RadioButton() **********/
GLUI_RadioButton::GLUI_RadioButton( GLUI_RadioGroup *grp, const GLUI_String &name )
{
common_init();
set_int_val( 0 );
/** A radio button's user id is always its ordinal number (zero-indexed)
within the group */
user_id = grp->num_buttons;
set_name( name );
group = grp;
group->num_buttons++; /* Increments radiogroup's button count */
group->add_control( this );
/*** Now update button states ***/
group->set_int_val( group->int_val ); /* This tells the group to
reset itself to its
current value, thereby
updating all its buttons */
}
void GLUI_RadioButton::common_init()
{
name = tfm::format("RadioButton: %p", this );
h = GLUI_RADIOBUTTON_SIZE;
group = NULL;
orig_value = -1;
text_x_offset = 18;
can_activate = true;
}
/************************ GLUI_RadioButton::mouse_down_handler() **********/
int GLUI_RadioButton::mouse_down_handler( int local_x, int local_y )
{
if ( NOT group )
return false;
orig_value = group->int_val;
currently_inside = true;
group->set_selected( this->user_id );
redraw();
return false;
}
/********************** GLUI_RadioButton::mouse_held_down_handler() ******/
int GLUI_RadioButton::mouse_held_down_handler( int local_x, int local_y,
bool inside)
{
if (inside != currently_inside) {
if (inside) group->set_selected( this->user_id );
else group->set_selected( orig_value );
currently_inside = inside;
redraw();
}
return false;
}
/*************************** GLUI_RadioButton::mouse_up_handler() **********/
int GLUI_RadioButton::mouse_up_handler( int local_x, int local_y,
bool inside )
{
if ( NOT group )
return false;
if ( NOT inside ) {
group->set_selected( orig_value );
redraw();
}
else {
/** Now we update the radio button group. We tell the group
handler to set the currently-selected item to this button, which
is reference by its user_id/ordinal number within group **/
group->set_selected( this->user_id );
redraw();
/*** Now update the linked variable, and call the callback,
but ONLY if the value of the radio group actually changed ***/
if ( group->int_val != orig_value ) {
group->output_live(true); /** Output live and update gfx ***/
group->execute_callback();
}
}
return false;
}
/****************************** GLUI_RadioButton::draw() **********/
void GLUI_RadioButton::draw( int x, int y )
{
GLUI_DRAWINGSENTINAL_IDIOM
if ( NOT group OR NOT can_draw() )
return;
/*** See if we're the currently-selected button. If so, draw ***/
if ( group->int_val == this->user_id ) {
if ( enabled )
glui->std_bitmaps.draw( GLUI_STDBITMAP_RADIOBUTTON_ON, 0, 0 );
else
glui->std_bitmaps.draw( GLUI_STDBITMAP_RADIOBUTTON_ON_DIS, 0, 0 );
}
else {
if ( enabled )
glui->std_bitmaps.draw( GLUI_STDBITMAP_RADIOBUTTON_OFF, 0, 0 );
else
glui->std_bitmaps.draw( GLUI_STDBITMAP_RADIOBUTTON_OFF_DIS, 0, 0 );
}
draw_active_area();
draw_name( text_x_offset, 10 );
}
/************************************ GLUI_RadioButton::draw_checked() ******/
void GLUI_RadioButton::draw_checked()
{
GLUI_DRAWINGSENTINAL_IDIOM
if ( enabled )
glui->std_bitmaps.draw( GLUI_STDBITMAP_RADIOBUTTON_ON, 0, 0 );
else
glui->std_bitmaps.draw( GLUI_STDBITMAP_RADIOBUTTON_ON_DIS, 0, 0 );
draw_active_area();
}
/*********************************** GLUI_RadioButton::draw_unchecked() ******/
void GLUI_RadioButton::draw_unchecked()
{
GLUI_DRAWINGSENTINAL_IDIOM
if ( enabled )
glui->std_bitmaps.draw( GLUI_STDBITMAP_RADIOBUTTON_OFF, 0, 0 );
else
glui->std_bitmaps.draw( GLUI_STDBITMAP_RADIOBUTTON_OFF_DIS, 0, 0 );
draw_active_area();
}
/**************************************** GLUI_RadioButton::draw_O() ********/
void GLUI_RadioButton::draw_O()
{
GLUI_DRAWINGSENTINAL_IDIOM
int i, j;
glBegin( GL_POINTS );
for(i=3; i<=GLUI_RADIOBUTTON_SIZE-3; i++ )
for(j=3; j<=GLUI_RADIOBUTTON_SIZE-3; j++ )
glVertex2i(i,j);
glEnd();
}
/******************************** GLUI_RadioButton::update_size() **********/
void GLUI_RadioButton::update_size()
{
int text_size;
if ( NOT glui )
return;
text_size = _glutBitmapWidthString( glui->font, name.c_str() );
/* if ( w < text_x_offset + text_size + 6 ) */
w = text_x_offset + text_size + 6 ;
}
/************************* GLUI_RadioButton::draw_active_area() **************/
void GLUI_RadioButton::draw_active_area()
{
GLUI_DRAWINGSENTINAL_IDIOM
int text_width, left, right;
text_width = _glutBitmapWidthString( glui->font, name.c_str() );
left = text_x_offset-3;
right = left + 7 + text_width;
if ( active ) {
glEnable( GL_LINE_STIPPLE );
glLineStipple( 1, 0x5555 );
glColor3f( 0., 0., 0. );
} else {
glColor3ubv( glui->bkgd_color );
}
glBegin( GL_LINE_LOOP );
glVertex2i(left,0); glVertex2i( right,0);
glVertex2i(right,h+1); glVertex2i( left,h+1);
glEnd();
glDisable( GL_LINE_STIPPLE );
}
/********************************* GLUI_RadioGroup::set_int_val() **********/
void GLUI_RadioGroup::set_int_val( int new_val )
{
if ( new_val == int_val )
return;
set_selected( new_val );
redraw();
output_live(true);
}