1
/****************************************************************************
3
GLUI User Interface Toolkit
4
---------------------------
6
glui_list.cpp - GLUI_List control class
9
--------------------------------------------------
11
Copyright (c) 2004 John Kew
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
*****************************************************************************/
19
#include "glui_internal_control.h"
21
#include <sys/timeb.h>
23
/****************************** GLUI_List::GLUI_List() **********/
25
GLUI_List::GLUI_List( GLUI_Node *parent, bool scroll,
26
int id, GLUI_CB callback
27
/*,GLUI_Control *object
28
GLUI_InterObject_CB obj_cb*/)
30
common_construct(parent, NULL, scroll, id, callback/*, object, obj_cb*/);
33
/****************************** GLUI_List::GLUI_List() **********/
35
GLUI_List::GLUI_List( GLUI_Node *parent,
36
GLUI_String& live_var, bool scroll,
39
/* ,GLUI_Control *object
40
,GLUI_InterObject_CB obj_cb*/ )
42
common_construct(parent, &live_var, scroll, id, callback/*, object, obj_cb*/);
45
/****************************** GLUI_List::common_construct() **********/
47
void GLUI_List::common_construct(
49
GLUI_String* data, bool scroll,
52
/*,GLUI_Control *object
53
, GLUI_InterObject_CB obj_cb*/)
56
GLUI_Node *list_panel = parent;
59
GLUI_Panel *p = new GLUI_Panel(parent,"",GLUI_PANEL_NONE);
65
this->live_type = GLUI_LIVE_STRING;
68
this->callback = callback;
70
list_panel->add_control( this );
73
new GLUI_Column(list_panel, false);
75
new GLUI_Scrollbar(list_panel,
79
scrollbar->set_object_callback(GLUI_List::scrollbar_callback, this);
80
scrollbar->set_alignment(GLUI_ALIGN_LEFT);
81
// scrollbar->can_activate = false; //kills ability to mouse drag too
86
/****************************** GLUI_List::mouse_down_handler() **********/
87
int GLUI_List::mouse_down_handler( int local_x, int local_y )
93
ms = time.millitm + (time.time)*1000;
95
tmp_line = find_line( local_x-x_abs, local_y-y_abs-5 );
96
if ( tmp_line == -1 ) {
98
glui->deactivate_current_control( );
102
if (tmp_line < num_lines) {
103
curr_line = tmp_line;
105
scrollbar->set_int_val(curr_line);
106
this->execute_callback();
107
if (associated_object != NULL)
108
if (cb_click_type == GLUI_SINGLE_CLICK) {
110
// obj_cb(associated_object, user_id);
114
if (last_line == curr_line && (ms - last_click_time) < 300) {
115
//obj_cb(associated_object, user_id);
118
last_click_time = ms;
119
last_line = curr_line;
123
update_and_draw_text();
132
/******************************** GLUI_List::mouse_up_handler() **********/
134
int GLUI_List::mouse_up_handler( int local_x, int local_y, bool inside )
140
/***************************** GLUI_List::mouse_held_down_handler() ******/
142
int GLUI_List::mouse_held_down_handler( int local_x, int local_y,
149
/****************************** GLUI_List::key_handler() **********/
151
int GLUI_List::key_handler( unsigned char key,int modifiers )
155
draw_text_only = false; /** Well, hack is not yet working **/
156
update_and_draw_text();
157
draw_text_only = false;
163
/****************************** GLUI_List::activate() **********/
165
void GLUI_List::activate( int how )
168
// dump( stdout, "-> ACTIVATE" );
171
if ( how == GLUI_ACTIVATE_MOUSE )
172
return; /* Don't select everything if activated with mouse */
177
/****************************** GLUI_List::deactivate() **********/
179
void GLUI_List::deactivate( void )
185
/****************************** GLUI_List::draw() **********/
187
void GLUI_List::draw( int x, int y )
191
GLUI_List_Item *item;
193
GLUI_DRAWINGSENTINAL_IDIOM
195
/* Bevelled Border */
197
glColor3f( .5, .5, .5 );
198
glVertex2i( 0, 0 ); glVertex2i( w, 0 );
199
glVertex2i( 0, 0 ); glVertex2i( 0, h );
201
glColor3f( 1., 1., 1. );
202
glVertex2i( 0, h ); glVertex2i( w, h );
203
glVertex2i( w, h ); glVertex2i( w, 0 );
206
glColor3f( 0., 0., 0. );
208
glColor3f( .25, .25, .25 );
209
glVertex2i( 1, 1 ); glVertex2i( w-1, 1 );
210
glVertex2i( 1, 1 ); glVertex2i( 1, h-1 );
212
glColor3f( .75, .75, .75 );
213
glVertex2i( 1, h-1 ); glVertex2i( w-1, h-1 );
214
glVertex2i( w-1, h-1 ); glVertex2i( w-1, 1 );
217
/* Draw Background if enabled*/
219
glColor3f( 1., 1., 1. );
220
glDisable( GL_CULL_FACE );
222
glVertex2i( 2, 2 ); glVertex2i( w-2, 2 );
223
glVertex2i( w-2, h-2 ); glVertex2i(2, h-2 );
226
glColor3f( .8, .8, .8 );
227
glDisable( GL_CULL_FACE );
229
glVertex2i( 2, 2 ); glVertex2i( w-2, 2 );
230
glVertex2i( w-2, h-2 ); glVertex2i(2, h-2 );
234
/* Figure out how wide the box is */
235
box_width = get_box_width();
237
/* Figure out which lines are visible*/
239
visible_lines = (int)(h-20)/15;
241
item = (GLUI_List_Item *) items_list.first_child();
245
if (line < start_line) {
247
item = (GLUI_List_Item *) item->next();
250
if (line >= start_line && line <= (start_line+visible_lines)) {
251
if (curr_line == line)
252
draw_text(item->text.c_str(),1,0,(line - start_line)*15);
254
draw_text(item->text.c_str(),0,0,(line - start_line)*15);
257
item = (GLUI_List_Item *) item->next();
261
scrollbar->set_int_limits(MAX(0,num_lines-visible_lines), 0);
263
glTranslatef(scrollbar->x_abs-x_abs, scrollbar->y_abs-y_abs,0.0);
264
scrollbar->draw_scroll();
269
/********************************* GLUI_List::draw_text() ****************/
271
void GLUI_List::draw_text(const char *t, int selected, int x, int y )
273
int text_x, i, x_pos;
276
GLUI_DRAWINGSENTINAL_IDIOM
278
/** Find where to draw the text **/
280
text_x = 2 + GLUI_LIST_BOXINNERMARGINX;
282
/** Draw selection area dark **/
283
if ( enabled && selected ) {
284
glColor3f( 0.0f, 0.0f, .6f );
286
glVertex2i(text_x, y+5 ); glVertex2i( w-text_x, y+5 );
287
glVertex2i(w-text_x, y+19 ); glVertex2i(text_x, y+19 );
290
box_width = get_box_width();
292
if ( !selected || !enabled ) { /* No current selection */
293
x_pos = text_x; /* or control disabled */
295
glColor3b( 0, 0, 0 );
297
glColor3b( 32, 32, 32 );
299
glRasterPos2i( text_x, y+15);
301
while( t[i] != '\0' && substring_width(t,0,i) < box_width) {
302
glutBitmapCharacter( get_font(), t[i] );
303
x_pos += char_width( t[i] );
307
else { /* There is a selection */
310
glColor3f( 1., 1., 1. );
311
glRasterPos2i( text_x, y+15);
312
while( t[i] != '\0' && substring_width(t,0,i) < box_width) {
313
glutBitmapCharacter( get_font(), t[i] );
314
x_pos += char_width( t[i] );
321
int GLUI_List::find_line(int x, int y) {
322
return start_line + ((int)(y/15));
325
int GLUI_List::get_box_width() {
327
- 6 /* 2 * the two-line box border */
328
- 2 * GLUI_LIST_BOXINNERMARGINX, 0 );
332
/******************************** GLUI_List::substring_width() *********/
333
int GLUI_List::substring_width( const char *t, int start, int end )
339
for( i=start; i<=end; i++ )
340
width += char_width( t[i] );
346
/***************************** GLUI_List::update_and_draw_text() ********/
348
void GLUI_List::update_and_draw_text( void )
350
if ( NOT can_draw() )
353
//update_substring_bounds();
354
/* printf( "ss: %d/%d\n", substring_start, substring_end ); */
360
/********************************* GLUI_List::special_handler() **********/
362
int GLUI_List::special_handler( int key,int modifiers )
367
if ( key == GLUT_KEY_DOWN ) {
368
if (curr_line < num_lines) {
370
if (curr_line > start_line+visible_lines)
373
} else if ( key == GLUT_KEY_UP ) {
376
if (curr_line < start_line)
382
scrollbar->set_int_val(curr_line);
388
/************************************ GLUI_List::update_size() **********/
390
void GLUI_List::update_size( void )
395
if ( w < GLUI_LIST_MIN_TEXT_WIDTH )
396
w = GLUI_LIST_MIN_TEXT_WIDTH;
399
/**************************************** GLUI_Listbox::add_item() **********/
401
int GLUI_List::add_item( int id, const char *new_text )
403
GLUI_List_Item *new_node = new GLUI_List_Item;
404
GLUI_List_Item *head;
406
new_node->text = new_text;
409
head = (GLUI_List_Item*) items_list.first_child();
410
new_node->link_this_to_parent_last( &items_list );
412
if ( head == NULL ) {
413
/*** This is first item added ***/
415
int_val = id+1; /** Different than id **/
416
// do_selection( id );
420
glui->post_update_main_gfx();
424
scrollbar->set_int_limits(MAX(num_lines-visible_lines,0), 0);
429
/************************************** GLUI_Listbox::delete_() **********/
431
int GLUI_List::delete_all()
433
GLUI_List_Item *item;
435
item = (GLUI_List_Item *) items_list.first_child();
439
item = (GLUI_List_Item *) items_list.first_child();
449
/************************************** GLUI_Listbox::delete_item() **********/
451
int GLUI_List::delete_item( const char *text )
453
GLUI_List_Item *node = get_item_ptr( text );
467
/************************************** GLUI_Listbox::delete_item() **********/
469
int GLUI_List::delete_item( int id )
471
GLUI_List_Item *node = get_item_ptr( id );
485
/************************************ GLUI_Listbox::get_item_ptr() **********/
487
GLUI_List_Item *GLUI_List::get_item_ptr( const char *text )
489
GLUI_List_Item *item;
491
item = (GLUI_List_Item *) items_list.first_child();
493
if ( item->text == text )
496
item = (GLUI_List_Item *) item->next();
503
/************************************ GLUI_Listbox::get_item_ptr() **********/
505
GLUI_List_Item *GLUI_List::get_item_ptr( int id )
507
GLUI_List_Item *item;
509
item = (GLUI_List_Item *) items_list.first_child();
511
if ( item->id == id )
514
item = (GLUI_List_Item *) item->next();
520
/**************************************** GLUI_List::mouse_over() ********/
522
int GLUI_List::mouse_over( int state, int x, int y )
524
glutSetCursor( GLUT_CURSOR_LEFT_ARROW );
529
void GLUI_List::scrollbar_callback(GLUI_Control *my_scrollbar) {
530
GLUI_Scrollbar *sb = dynamic_cast<GLUI_Scrollbar*>(my_scrollbar);
532
GLUI_List* me = (GLUI_List*) sb->associated_object;
533
if (me->scrollbar == NULL)
535
int new_start_line = sb->get_int_val(); // TODO!!
536
me->start_line = new_start_line;
538
if ( me->can_draw() )
539
me->update_and_draw_text();