1
/****************************************************************************
3
GLUI User Interface Toolkit
4
---------------------------
6
glui_listbox - GLUI_ListBox control class
9
--------------------------------------------------
11
Copyright (c) 1998 Paul Rademacher
13
This program is freely distributable without licensing fees and is
14
provided without guarantee or warrantee expressed or implied. This
15
program is -not- in the public domain.
17
*****************************************************************************/
22
/****************************** GLUI_Listbox::mouse_down_handler() **********/
24
int GLUI_Listbox::mouse_down_handler( int local_x, int local_y )
30
/****************************** GLUI_Listbox::mouse_up_handler() **********/
32
int GLUI_Listbox::mouse_up_handler( int local_x, int local_y, int inside )
39
/****************************** GLUI_Listbox::mouse_held_down_handler() ******/
41
int GLUI_Listbox::mouse_held_down_handler( int local_x, int local_y,
49
/****************************** GLUI_Listbox::key_handler() **********/
51
int GLUI_Listbox::key_handler( unsigned char key,int modifiers )
57
/****************************** GLUI_Listbox::draw() **********/
59
void GLUI_Listbox::draw( int x, int y )
66
orig = set_to_glut_window();
68
/* draw_active_area(); */
70
name_x = MAX(text_x_offset - string_width(this->name) - 3,0);
71
draw_name( name_x , 13);
72
draw_box_inwards_outline( text_x_offset, w,
77
draw_box( text_x_offset+3, w-2, 2, h-2, 1.0, 1.0, 1.0 );
79
glColor3b( 32, 32, 32 );
81
glColor3f( 0.0, 0.0, 0.0 );
82
glRasterPos2i( text_x_offset+5, 13 );
83
draw_string( curr_text.string );
86
draw_box( text_x_offset+3, w-2, 2, h-2, .0, .0, .6 );
87
glColor3f( 1.0, 1.0, 1.0 );
88
glRasterPos2i( text_x_offset+5, 13 );
89
draw_string( curr_text.string );
95
draw(GLUI_STDBITMAP_LISTBOX_UP,
96
w-glui->std_bitmaps.bitmaps[GLUI_STDBITMAP_LISTBOX_UP].w-1,
101
draw(GLUI_STDBITMAP_LISTBOX_UP_DIS,
102
w-glui->std_bitmaps.bitmaps[GLUI_STDBITMAP_LISTBOX_UP].w-1,
106
restore_window(orig);
110
/************************************ GLUI_Listbox::update_si() **********/
112
void GLUI_Listbox::update_size( void )
114
int text_size, delta;
120
text_size = string_width( name );
122
/*** Find the longest item string ***/
126
if ( text_x_offset < text_size +2 )
127
delta = text_size+2-text_x_offset;
129
text_x_offset += delta;
130
if ( w < text_x_offset+MAX(GLUI_EDITTEXT_MIN_TEXT_WIDTH,item_text_size)+20)
131
w = text_x_offset + MAX( GLUI_EDITTEXT_MIN_TEXT_WIDTH,item_text_size)+20;
136
/********************************* GLUI_Listbox::set_int_val() **************/
138
void GLUI_Listbox::set_int_val( int new_val )
140
/* int_val = new_val; */
142
do_selection( new_val );
144
/*** Update the variable we're (possibly) pointing to, and update the main gfx ***/
149
/**************************** GLUI_Listbox::draw_active_area() **************/
151
void GLUI_Listbox::draw_active_area( void )
155
if ( NOT can_draw() )
158
orig = set_to_glut_window();
160
restore_window(orig);
164
/**************************************** GLUI_Listbox::add_item() **********/
166
int GLUI_Listbox::add_item( int id, char *new_text )
168
GLUI_Listbox_Item *new_node = new GLUI_Listbox_Item;
169
GLUI_Listbox_Item *head;
171
strcpy( new_node->text, new_text );
174
head = (GLUI_Listbox_Item*) items_list.first_child();
175
new_node->link_this_to_parent_last( &items_list );
177
if ( head == NULL ) {
178
/*** This is first item added ***/
180
int_val = id+1; /** Different than id **/
185
glui->post_update_main_gfx();
188
/*** Check if we need to increase control size ***/
189
if ( w < text_x_offset + MAX( GLUI_EDITTEXT_MIN_TEXT_WIDTH, string_width( new_text ) ) + 20 ) {
190
w = text_x_offset + MAX( GLUI_EDITTEXT_MIN_TEXT_WIDTH, string_width( new_text ) ) + 20;
195
/* printf( "%s\n", new_text ); */
203
/************************************** GLUI_Listbox::delete_item() **********/
205
int GLUI_Listbox::delete_item( char *text )
207
GLUI_Listbox_Item *node = get_item_ptr( text );
220
/************************************** GLUI_Listbox::delete_item() **********/
222
int GLUI_Listbox::delete_item( int id )
224
GLUI_Listbox_Item *node = get_item_ptr( id );
237
/************************************** GLUI_Listbox::sort_items() **********/
239
int GLUI_Listbox::sort_items( void )
245
/********************************************* GLUI_Listbox::dump() **********/
247
void GLUI_Listbox::dump( FILE *output )
249
GLUI_Listbox_Item *item;
251
/* printf( "%p\n", (char*) name ); */
253
fprintf( output, "Listbox: %s\n", (char*) name );
255
item = (GLUI_Listbox_Item *) items_list.first_child();
257
fprintf( output, " %3d : %s\n", item->id, (char*) item->text );
259
item = (GLUI_Listbox_Item *) item->next();
264
/************************************ GLUI_Listbox::get_item_ptr() **********/
266
GLUI_Listbox_Item *GLUI_Listbox::get_item_ptr( char *text )
268
GLUI_Listbox_Item *item;
270
item = (GLUI_Listbox_Item *) items_list.first_child();
272
if ( NOT strcmp( item->text, text ))
275
item = (GLUI_Listbox_Item *) item->next();
282
/************************************ GLUI_Listbox::get_item_ptr() **********/
284
GLUI_Listbox_Item *GLUI_Listbox::get_item_ptr( int id )
286
GLUI_Listbox_Item *item;
288
item = (GLUI_Listbox_Item *) items_list.first_child();
290
if ( item->id == id )
293
item = (GLUI_Listbox_Item *) item->next();
300
/************************************ GLUI_Listbox::mouse_over() **********/
302
static void listbox_callback( int i )
306
if ( NOT GLUI_Master.curr_left_button_glut_menu OR
307
GLUI_Master.curr_left_button_glut_menu->type != GLUI_CONTROL_LISTBOX )
310
old_val = ((GLUI_Listbox*)GLUI_Master.curr_left_button_glut_menu)->int_val;
311
((GLUI_Listbox*)GLUI_Master.curr_left_button_glut_menu)->set_int_val(i);
313
/**** If value changed, execute callback ****/
315
((GLUI_Listbox*)GLUI_Master.curr_left_button_glut_menu)->int_val ) {
316
((GLUI_Listbox*)GLUI_Master.curr_left_button_glut_menu)->execute_callback();
321
/*************************************** GLUI_Listbox::mouse_over() **********/
323
int GLUI_Listbox::mouse_over( int state, int x, int y )
325
GLUI_Listbox_Item *item;
327
/* printf( "x/y: %d/%d\n", x, y ); */
329
if ( state AND enabled AND x > x_abs + text_x_offset) {
330
/**** Build a GLUT menu for this listbox ***/
332
/* printf( "%d %d\n", x, y ); */
334
glut_menu_id = glutCreateMenu(listbox_callback);
336
item = (GLUI_Listbox_Item *) items_list.first_child();
338
glutAddMenuEntry( item->text, item->id );
339
item = (GLUI_Listbox_Item *) item->next();
342
glutAttachMenu( GLUT_LEFT_BUTTON);
344
GLUI_Master.set_left_button_glut_menu_control( this );
346
else if ( glut_menu_id != -1 ) {
347
/* printf( "OUT\n" ); */
348
glutDetachMenu( GLUT_LEFT_BUTTON );
349
glutDestroyMenu( glut_menu_id );
357
/************************************ GLUI_Listbox::do_selection() **********/
359
int GLUI_Listbox::do_selection( int item_num )
361
GLUI_Listbox_Item *item, *sel_item;
363
/*** Is this item already selected? ***/
364
if ( item_num == int_val )
368
item = (GLUI_Listbox_Item *) items_list.first_child();
370
if ( item->id == item_num ) {
375
item = (GLUI_Listbox_Item *) item->next();
381
/* printf( "-> %s\n", (char*) sel_item->text ); */
384
strcpy( curr_text.string, sel_item->text.string );
386
translate_and_draw_front();
392
/*********************************** GLUI_Listbox::~GLUI_Listbox() **********/
394
GLUI_Listbox::~GLUI_Listbox( )
396
GLUI_Listbox_Item *item, *tmp_item;
398
item = (GLUI_Listbox_Item *) items_list.first_child();
404
item = (GLUI_Listbox_Item *) tmp_item->next();
409
/****************************** GLUI_Listbox::special_handler() **********/
411
int GLUI_Listbox::special_handler( int key,int modifiers )
413
GLUI_Listbox_Item *node, *new_node;
415
node = get_item_ptr( int_val );
418
if ( key == GLUT_KEY_DOWN ) {
419
new_node = (GLUI_Listbox_Item*) node->next();
421
else if ( key == GLUT_KEY_UP ) {
422
new_node = (GLUI_Listbox_Item*) node->prev();
424
else if ( key == GLUT_KEY_HOME ) {
425
new_node = (GLUI_Listbox_Item*) items_list.first_child();
427
else if ( key == GLUT_KEY_END ) {
428
new_node = (GLUI_Listbox_Item*) items_list.last_child();
431
if ( new_node != NULL AND new_node != node ) {
433
set_int_val( node->id );
443
/************************* GLUI_Listbox::increase_width( void ) ***********/
445
void GLUI_Listbox::increase_width( void )