~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/bullet/Extras/glui/glui_listbox.cpp

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
  
 
3
  GLUI User Interface Toolkit
 
4
  ---------------------------
 
5
 
 
6
     glui_listbox - GLUI_ListBox control class
 
7
 
 
8
 
 
9
          --------------------------------------------------
 
10
 
 
11
  Copyright (c) 1998 Paul Rademacher
 
12
 
 
13
  WWW:    http://sourceforge.net/projects/glui/
 
14
  Forums: http://sourceforge.net/forum/?group_id=92496
 
15
 
 
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.
 
20
 
 
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.
 
25
 
 
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
 
29
 
 
30
*****************************************************************************/
 
31
 
 
32
#include "glui_internal_control.h"
 
33
 
 
34
/****************************** GLUI_Listbox::GLUI_Listbox() **********/
 
35
GLUI_Listbox::GLUI_Listbox( GLUI_Node *parent,
 
36
                            const char *name, int *value_ptr,
 
37
                            int id, 
 
38
                            GLUI_CB cb)
 
39
{
 
40
  common_init();
 
41
  set_ptr_val( value_ptr );
 
42
  user_id    = id;
 
43
  set_name( name );
 
44
  callback    = cb;
 
45
 
 
46
  parent->add_control( this );
 
47
 
 
48
  init_live();
 
49
}
 
50
 
 
51
 
 
52
/****************************** GLUI_Listbox::mouse_down_handler() **********/
 
53
 
 
54
int    GLUI_Listbox::mouse_down_handler( int local_x, int local_y )
 
55
{
 
56
  return false;
 
57
}
 
58
 
 
59
 
 
60
/****************************** GLUI_Listbox::mouse_up_handler() **********/
 
61
 
 
62
int    GLUI_Listbox::mouse_up_handler( int local_x, int local_y, bool inside )
 
63
{
 
64
 
 
65
  return false;
 
66
}
 
67
 
 
68
 
 
69
/****************************** GLUI_Listbox::mouse_held_down_handler() ******/
 
70
 
 
71
int    GLUI_Listbox::mouse_held_down_handler( int local_x, int local_y,
 
72
                                              bool inside)
 
73
{
 
74
  
 
75
  return false;
 
76
}
 
77
 
 
78
 
 
79
/****************************** GLUI_Listbox::key_handler() **********/
 
80
 
 
81
int    GLUI_Listbox::key_handler( unsigned char key,int modifiers )
 
82
{
 
83
  return false;
 
84
}
 
85
 
 
86
 
 
87
/****************************** GLUI_Listbox::draw() **********/
 
88
 
 
89
void    GLUI_Listbox::draw( int x, int y )
 
90
{
 
91
  GLUI_DRAWINGSENTINAL_IDIOM
 
92
  int name_x;
 
93
 
 
94
  /*  draw_active_area();              */
 
95
 
 
96
  name_x = MAX(text_x_offset - string_width(this->name) - 3,0);
 
97
  draw_name( name_x , 13);
 
98
  draw_box_inwards_outline( text_x_offset, w,
 
99
                            0, h );
 
100
 
 
101
  if ( NOT active ) {
 
102
    draw_box( text_x_offset+3, w-2, 2, h-2, 1.0, 1.0, 1.0 );
 
103
    if ( NOT enabled )
 
104
      glColor3b( 32, 32, 32 );
 
105
    else
 
106
      glColor3f( 0.0, 0.0, 0.0 );
 
107
    glRasterPos2i( text_x_offset+5, 13 );
 
108
    draw_string( curr_text );
 
109
  }
 
110
  else {
 
111
    draw_box( text_x_offset+3, w-2, 2, h-2, .0, .0, .6 );
 
112
    glColor3f( 1.0, 1.0, 1.0 );
 
113
    glRasterPos2i( text_x_offset+5, 13 );
 
114
    draw_string( curr_text );
 
115
  }
 
116
 
 
117
 
 
118
  if ( enabled ) {
 
119
    glui->std_bitmaps.
 
120
      draw(GLUI_STDBITMAP_LISTBOX_UP,
 
121
           w-glui->std_bitmaps.width(GLUI_STDBITMAP_LISTBOX_UP)-1,
 
122
           2 );
 
123
  }
 
124
  else {
 
125
    glui->std_bitmaps.
 
126
      draw(GLUI_STDBITMAP_LISTBOX_UP_DIS,
 
127
           w-glui->std_bitmaps.width(GLUI_STDBITMAP_LISTBOX_UP)-1,
 
128
           2 );
 
129
  }
 
130
}
 
131
 
 
132
 
 
133
/************************************ GLUI_Listbox::update_si() **********/
 
134
void   GLUI_Listbox::update_size( void )
 
135
{
 
136
  recalculate_item_width();
 
137
}
 
138
 
 
139
/********************************* GLUI_Listbox::set_int_val() **************/
 
140
 
 
141
void    GLUI_Listbox::set_int_val( int new_val )
 
142
{
 
143
  /*  int_val = new_val;              */
 
144
 
 
145
  do_selection( new_val );
 
146
 
 
147
  /*** Update the variable we're (possibly) pointing to, and update the main gfx ***/
 
148
  output_live(true);
 
149
}
 
150
 
 
151
/**************************************** GLUI_Listbox::add_item() **********/
 
152
 
 
153
int  GLUI_Listbox::add_item( int id, const char *new_text )
 
154
{
 
155
  GLUI_Listbox_Item *new_node = new GLUI_Listbox_Item;
 
156
  GLUI_Listbox_Item *head;
 
157
 
 
158
  new_node->text = new_text;
 
159
  new_node->id = id;
 
160
 
 
161
  head = (GLUI_Listbox_Item*) items_list.first_child();
 
162
  new_node->link_this_to_parent_last( &items_list );
 
163
 
 
164
  if ( head == NULL ) {
 
165
    /***   This is first item added   ***/
 
166
 
 
167
    int_val       = id+1;  /** Different than id **/
 
168
    do_selection( id );
 
169
    last_live_int = id;
 
170
 
 
171
    if( glui )
 
172
      glui->post_update_main_gfx();
 
173
  }
 
174
  if (recalculate_item_width()) glui->refresh();
 
175
 
 
176
  return true;
 
177
}
 
178
 
 
179
 
 
180
/************************************** GLUI_Listbox::delete_item() **********/
 
181
 
 
182
int  GLUI_Listbox::delete_item( const char *text )
 
183
{
 
184
  GLUI_Listbox_Item *node = get_item_ptr(text);
 
185
 
 
186
  if (node) 
 
187
  {
 
188
    node->unlink();
 
189
    delete node;
 
190
    return true;
 
191
  }
 
192
  if (recalculate_item_width()) glui->refresh();
 
193
 
 
194
  return false;
 
195
}
 
196
 
 
197
 
 
198
/************************************** GLUI_Listbox::delete_item() **********/
 
199
 
 
200
int  GLUI_Listbox::delete_item(int id)
 
201
{
 
202
  GLUI_Listbox_Item *node = get_item_ptr(id);
 
203
 
 
204
  if (node) 
 
205
  {
 
206
    node->unlink();
 
207
    delete node;
 
208
    return true;
 
209
  }
 
210
  if (recalculate_item_width()) glui->refresh();
 
211
  
 
212
  return false;
 
213
}
 
214
 
 
215
 
 
216
/************************************** GLUI_Listbox::sort_items() **********/
 
217
 
 
218
int  GLUI_Listbox::sort_items( void )
 
219
{
 
220
  return false;
 
221
}
 
222
 
 
223
 
 
224
/********************************************* GLUI_Listbox::dump() **********/
 
225
 
 
226
void     GLUI_Listbox::dump( FILE *output )
 
227
{
 
228
  GLUI_Listbox_Item *item;
 
229
 
 
230
  /*  printf( "%p\n", (char*) name );              */
 
231
 
 
232
  fprintf( output, "Listbox: %s\n", name.c_str() );
 
233
 
 
234
  item = (GLUI_Listbox_Item *) items_list.first_child();
 
235
  while( item ) {
 
236
    fprintf( output, "         %3d : %s\n", item->id, item->text.c_str() );
 
237
    
 
238
    item = (GLUI_Listbox_Item *) item->next();
 
239
  }
 
240
}
 
241
 
 
242
 
 
243
/************************************ GLUI_Listbox::get_item_ptr() **********/
 
244
 
 
245
GLUI_Listbox_Item *GLUI_Listbox::get_item_ptr( const char *text )
 
246
{
 
247
  GLUI_Listbox_Item *item;
 
248
 
 
249
  item = (GLUI_Listbox_Item *) items_list.first_child();
 
250
  while( item ) {
 
251
    if ( item->text == text )
 
252
      return item;
 
253
    
 
254
    item = (GLUI_Listbox_Item *) item->next();
 
255
  }
 
256
 
 
257
  return NULL;
 
258
}
 
259
 
 
260
 
 
261
/************************************ GLUI_Listbox::get_item_ptr() **********/
 
262
 
 
263
GLUI_Listbox_Item *GLUI_Listbox::get_item_ptr( int id )
 
264
{
 
265
  GLUI_Listbox_Item *item;
 
266
 
 
267
  item = (GLUI_Listbox_Item *) items_list.first_child();
 
268
  while( item ) {
 
269
    if ( item->id == id )
 
270
      return item;
 
271
    
 
272
    item = (GLUI_Listbox_Item *) item->next();
 
273
  }
 
274
 
 
275
  return NULL;
 
276
}
 
277
 
 
278
 
 
279
/************************************ GLUI_Listbox::mouse_over() **********/
 
280
 
 
281
static void listbox_callback( int i )
 
282
{
 
283
  int old_val;
 
284
 
 
285
  if ( NOT GLUI_Master.curr_left_button_glut_menu OR 
 
286
          !GLUI_Master.curr_left_button_glut_menu->dynamicCastGLUI_Listbox() ) 
 
287
    return;
 
288
 
 
289
  old_val = ((GLUI_Listbox*)GLUI_Master.curr_left_button_glut_menu)->int_val;
 
290
  ((GLUI_Listbox*)GLUI_Master.curr_left_button_glut_menu)->set_int_val(i);
 
291
 
 
292
  /****   If value changed, execute callback   ****/
 
293
  if ( old_val != 
 
294
       ((GLUI_Listbox*)GLUI_Master.curr_left_button_glut_menu)->int_val ) {
 
295
    ((GLUI_Listbox*)GLUI_Master.curr_left_button_glut_menu)->execute_callback();
 
296
  }
 
297
}
 
298
 
 
299
 
 
300
/*************************************** GLUI_Listbox::mouse_over() **********/
 
301
 
 
302
int     GLUI_Listbox::mouse_over( int state, int x, int y )
 
303
{
 
304
  GLUI_Listbox_Item *item;
 
305
 
 
306
  /*  printf( "x/y:   %d/%d\n", x, y );              */
 
307
 
 
308
  if ( state AND enabled AND x > x_abs + text_x_offset) {
 
309
    /****  Build a GLUT menu for this listbox   ***/
 
310
    
 
311
    /*  printf( "%d %d\n", x, y );              */
 
312
 
 
313
    glut_menu_id = glutCreateMenu(listbox_callback);
 
314
 
 
315
    item = (GLUI_Listbox_Item *) items_list.first_child();
 
316
    while( item ) {
 
317
      glutAddMenuEntry( item->text.c_str(), item->id );
 
318
      item = (GLUI_Listbox_Item *) item->next();
 
319
    }
 
320
 
 
321
    glutAttachMenu( GLUT_LEFT_BUTTON);
 
322
    
 
323
    GLUI_Master.set_left_button_glut_menu_control( this );
 
324
  }
 
325
  else if ( glut_menu_id != -1 ) {
 
326
    /*    printf( "OUT\n" );              */
 
327
    glutDetachMenu( GLUT_LEFT_BUTTON );
 
328
    glutDestroyMenu( glut_menu_id );
 
329
    glut_menu_id = -1;
 
330
  }
 
331
 
 
332
  return true;
 
333
}
 
334
 
 
335
 
 
336
/************************************ GLUI_Listbox::do_selection() **********/
 
337
 
 
338
int    GLUI_Listbox::do_selection( int item_num )
 
339
{
 
340
  GLUI_Listbox_Item *item, *sel_item;
 
341
 
 
342
  /***  Is this item already selected?  ***/
 
343
  if ( item_num == int_val )
 
344
    return false;
 
345
 
 
346
  sel_item = NULL;
 
347
  item     = (GLUI_Listbox_Item *) items_list.first_child();
 
348
  while( item ) {
 
349
    if ( item->id == item_num ) {
 
350
      sel_item = item;
 
351
      break;
 
352
    }
 
353
    
 
354
    item = (GLUI_Listbox_Item *) item->next();
 
355
  }
 
356
 
 
357
  if ( NOT sel_item )
 
358
    return false;
 
359
 
 
360
  /*  printf( "-> %s\n", (char*) sel_item->text );              */
 
361
 
 
362
  int_val = item_num;
 
363
  curr_text = sel_item->text;
 
364
  redraw();
 
365
 
 
366
  return true;
 
367
}
 
368
 
 
369
 
 
370
/*********************************** GLUI_Listbox::~GLUI_Listbox() **********/
 
371
 
 
372
GLUI_Listbox::~GLUI_Listbox()
 
373
{
 
374
  GLUI_Listbox_Item *item = (GLUI_Listbox_Item *) items_list.first_child();
 
375
 
 
376
  while (item) 
 
377
  {
 
378
    GLUI_Listbox_Item *tmp = item;
 
379
    item = (GLUI_Listbox_Item *) item->next();
 
380
    delete tmp;
 
381
  }
 
382
}
 
383
 
 
384
/****************************** GLUI_Listbox::special_handler() **********/
 
385
 
 
386
int    GLUI_Listbox::special_handler( int key,int modifiers )
 
387
{
 
388
  GLUI_Listbox_Item *node, *new_node;
 
389
 
 
390
  node     = get_item_ptr( int_val );
 
391
  new_node = NULL;
 
392
 
 
393
  if ( key == GLUT_KEY_DOWN ) {
 
394
    new_node = (GLUI_Listbox_Item*) node->next();
 
395
  }
 
396
  else if ( key == GLUT_KEY_UP ) {
 
397
    new_node = (GLUI_Listbox_Item*) node->prev();
 
398
  }
 
399
  else if ( key == GLUT_KEY_HOME ) {
 
400
    new_node = (GLUI_Listbox_Item*) items_list.first_child();
 
401
  }
 
402
  else if ( key == GLUT_KEY_END ) {
 
403
    new_node = (GLUI_Listbox_Item*) items_list.last_child();
 
404
  }
 
405
 
 
406
  if ( new_node != NULL AND new_node != node ) {
 
407
    node = new_node;
 
408
    set_int_val( node->id );
 
409
    execute_callback();
 
410
    return true;
 
411
  }
 
412
  else {
 
413
    return false;
 
414
  }
 
415
}
 
416
 
 
417
 
 
418
/************************* GLUI_Listbox::recalculate_item_width( void ) ***********/
 
419
/** Change w and return true if we need to be widened to fit the current items. */
 
420
bool    GLUI_Listbox::recalculate_item_width( void )
 
421
{
 
422
  int item_text_size;
 
423
 
 
424
  if ( NOT glui )
 
425
    return false;
 
426
 
 
427
  /* Find the title size */
 
428
  text_x_offset = string_width( name );
 
429
 
 
430
  /* Find the longest item string ***/
 
431
  item_text_size = 0;   
 
432
 
 
433
  GLUI_Listbox_Item *item = (GLUI_Listbox_Item *) items_list.first_child();
 
434
  while( item ) {
 
435
    item_text_size = MAX(item_text_size,string_width(item->text));
 
436
    item = (GLUI_Listbox_Item *) item->next();
 
437
  }
 
438
  
 
439
  /* Sum up our layout: name, item, and drop-down marker */
 
440
  int new_wid=text_x_offset+MAX(GLUI_EDITTEXT_MIN_TEXT_WIDTH,item_text_size)+20;
 
441
  if ( w != new_wid) {
 
442
    w = new_wid;
 
443
    return true; /* we gotta be shortened or widened */
 
444
  }
 
445
  else {
 
446
    return false; /* our current width is OK */
 
447
  }
 
448
}