~ubuntu-branches/ubuntu/edgy/glui/edgy

« back to all changes in this revision

Viewing changes to glui_listbox.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Marcelo E. Magallon
  • Date: 2001-09-23 12:57:28 UTC
  • Revision ID: james.westby@ubuntu.com-20010923125728-qbts7xit2eg1ogo2
Tags: upstream-2.1.0.beta
ImportĀ upstreamĀ versionĀ 2.1.0.beta

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