File: glui_checkbox.cpp

package info (click to toggle)
glui 2.1.0.beta-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,068 kB
  • ctags: 1,302
  • sloc: cpp: 9,043; ansic: 857; makefile: 235
file content (329 lines) | stat: -rw-r--r-- 7,363 bytes parent folder | download
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
/****************************************************************************
  
  GLUI User Interface Toolkit
  ---------------------------

     glui_checkbox - GLUI_Checkbox control class


          --------------------------------------------------

  Copyright (c) 1998 Paul Rademacher

  This program is freely distributable without licensing fees and is
  provided without guarantee or warrantee expressed or implied. This
  program is -not- in the public domain.

*****************************************************************************/

#include "glui.h"
#include "stdinc.h"

/****************************** GLUI_Checkbox::mouse_down_handler() **********/

int    GLUI_Checkbox::mouse_down_handler( int local_x, int local_y )
{
  orig_value = int_val;
  
  (void)local_x;
  (void)local_y;

  TOGGLE_BOOL( int_val );

  currently_inside = true;

  if ( int_val )
    draw_checked();
  else
    draw_unchecked();

  return false;
}


/****************************** GLUI_Checkbox::mouse_up_handler() **********/

int    GLUI_Checkbox::mouse_up_handler( int local_x, int local_y, int inside )
{
  (void)local_x;
  (void)local_y;

  if ( NOT inside ) {
    int_val = orig_value;    
  }
  else {
    set_int_val( int_val );

    /*** Invoke the callback ***/
    execute_callback();
  }

  if ( int_val )
    draw_checked();
  else
    draw_unchecked();

  return false;
}


/****************************** GLUI_Checkbox::mouse_held_down_handler() ******/

int    GLUI_Checkbox::mouse_held_down_handler( int local_x, int local_y,
					       int inside)
{
  (void)local_x;
  (void)local_y;
  (void)inside;

  /********** Toggle checked and unchecked bitmap if we're entering or
    leaving the checkbox area **********/

  /** oops, this was wrong!
    if ( NOT inside AND currently_inside == true )
    draw_unchecked();
  else if ( inside AND currently_inside == false ) 
    draw_checked();*/

  if ( inside != currently_inside )
    TOGGLE_BOOL( int_val );

  currently_inside = inside;

  if ( int_val )
    draw_checked();
  else
    draw_unchecked();
  
  return false;
}


/****************************** GLUI_Checkbox::key_handler() **********/

int    GLUI_Checkbox::key_handler( unsigned char key,int modifiers )
{
  (void)key;
  (void)modifiers;

  return false;
}


/****************************** GLUI_Checkbox::draw() **********/

void    GLUI_Checkbox::draw( int x, int y )
{
  int orig;

  (void)x;
  (void)y;

  if ( NOT can_draw() )
    return;

  orig = set_to_glut_window();

  if ( int_val != 0 ) {
    if ( enabled ) 
      glui->std_bitmaps.draw( GLUI_STDBITMAP_CHECKBOX_ON, 0, 0 );
    else
      glui->std_bitmaps.draw( GLUI_STDBITMAP_CHECKBOX_ON_DIS, 0, 0 );
  }
  else {
    if ( enabled )
      glui->std_bitmaps.draw( GLUI_STDBITMAP_CHECKBOX_OFF, 0, 0 );
    else
      glui->std_bitmaps.draw( GLUI_STDBITMAP_CHECKBOX_OFF_DIS, 0, 0 );      
  }

  draw_active_area();

  draw_name( text_x_offset, 10);

  restore_window(orig);
}


/************************************** GLUI_Checkbox::draw_checked() ******/

void   GLUI_Checkbox::draw_checked( void )
{
  int state, orig;

  if ( NOT can_draw() )
    return;

  orig = set_to_glut_window();
  state = glui->set_front_draw_buffer();
  
  glColor3f( 0.0, 0.0, 0.0 );
  glPushMatrix();
  translate_to_origin();
  if ( enabled )
    glui->std_bitmaps.draw( GLUI_STDBITMAP_CHECKBOX_ON, 0, 0 );
  else
    glui->std_bitmaps.draw( GLUI_STDBITMAP_CHECKBOX_ON_DIS, 0, 0 );
  draw_active_area();
  glPopMatrix();

  glui->restore_draw_buffer(state);
  restore_window(orig);
}


/************************************** GLUI_Checkbox::draw_unchecked() ******/

void   GLUI_Checkbox::draw_unchecked( void )
{
  int state, orig;

  if ( NOT can_draw() )
    return;

  orig = set_to_glut_window();
  state = glui->set_front_draw_buffer();

  glColor3f( 1.0, 1.0, 1.0 );
  glPushMatrix();
  translate_to_origin();
  if ( enabled )
    glui->std_bitmaps.draw( GLUI_STDBITMAP_CHECKBOX_OFF, 0, 0 );
  else
    glui->std_bitmaps.draw( GLUI_STDBITMAP_CHECKBOX_OFF_DIS, 0, 0 );
  draw_active_area();
  glPopMatrix();

  glui->restore_draw_buffer(state);
  restore_window(orig);  
}


/**************************************** GLUI_Checkbox::draw_X() ************/

void   GLUI_Checkbox::draw_X( void )
{
  int orig;

  orig = set_to_glut_window();

  /*  glPointSize( 1.0 );
      glBegin( GL_POINTS );
      for( i=2; i<=GLUI_CHECKBOX_SIZE-2; i++ ) {
      glVertex2i( i,i );
      glVertex2i( i,GLUI_CHECKBOX_SIZE-i );
      }
      glEnd();
      */

  /*  glColor3f( 0.0, 0.0, 0.0 );              */

  /*
    glBegin( GL_LINES );
    glVertex2i( 0,                    0 );
    glVertex2i( GLUI_CHECKBOX_SIZE-0, GLUI_CHECKBOX_SIZE-0 );
    glVertex2i( GLUI_CHECKBOX_SIZE-0, 0  );
    glVertex2i( 0,                    GLUI_CHECKBOX_SIZE-0 );
    glEnd();*/

  restore_window(orig);
}


/********************************** GLUI_Checkbox::draw_empty_box() **********/

void     GLUI_Checkbox::draw_empty_box( void )
{
  int orig;

  if ( NOT can_draw())
    return;
  
  orig = set_to_glut_window();

  glColor3f( 1.0, 1.0, 1.0 );
  glBegin( GL_QUADS );
  glVertex2i( 0, 0 );
  glVertex2i( GLUI_CHECKBOX_SIZE, 0 );
  glVertex2i( GLUI_CHECKBOX_SIZE, GLUI_CHECKBOX_SIZE );
  glVertex2i( 0,                  GLUI_CHECKBOX_SIZE );
  glEnd();

  glColor3f( 0.0, 0.0, 0.0 );
  glBegin( GL_LINE_LOOP );
  glVertex2i( 0, 0 );
  glVertex2i( GLUI_CHECKBOX_SIZE, 0 );
  glVertex2i( GLUI_CHECKBOX_SIZE, GLUI_CHECKBOX_SIZE );
  glVertex2i( 0,                  GLUI_CHECKBOX_SIZE );
  glEnd();

  restore_window(orig);
}


/************************************ GLUI_Checkbox::update_size() **********/

void   GLUI_Checkbox::update_size( void )
{
  int text_size;

  if ( NOT glui )
    return;

  text_size = _glutBitmapWidthString( glui->font, name );

  /*  if ( w < text_x_offset + text_size + 6 )              */
  w = text_x_offset + text_size + 6 ;
}


/**************************** GLUI_Checkbox::draw_active_area() **************/

void    GLUI_Checkbox::draw_active_area( void )
{
  int text_width, left, right, orig;

  if ( NOT can_draw())
    return;

  orig = set_to_glut_window();

  text_width = _glutBitmapWidthString( glui->font, name );
  left       = text_x_offset-3;
  right      = left + 7 + text_width;

  if ( active ) {
    glEnable( GL_LINE_STIPPLE );
    glLineStipple( 1, 0x5555 );
    glColor3f( 0., 0., 0. );
  } else {
    glColor3ub( glui->bkgd_color.r, glui->bkgd_color.g, glui->bkgd_color.b );
  }

  glBegin( GL_LINE_LOOP );
  glVertex2i(left,0);     glVertex2i( right,0);
  glVertex2i(right,h+1);   glVertex2i( left,h+1);
  glEnd();
  
  glDisable( GL_LINE_STIPPLE );

  restore_window(orig);
}


/********************************* GLUI_Checkbox::set_int_val() **************/

void    GLUI_Checkbox::set_int_val( int new_val )
{
  int_val = new_val;

  /*** Update the variable we're (possibly) pointing to ***/
  output_live(true);

  if ( can_draw() ) {
    if ( int_val )
      draw_checked();
    else
      draw_unchecked();
  }
}