1 
2 /****************************************************************************
3 
4   GLUI User Interface Toolkit
5   ---------------------------
6 
7      glui_checkbox - GLUI_Checkbox control class
8 
9 
10           --------------------------------------------------
11 
12   Copyright (c) 1998 Paul Rademacher
13 
14   WWW:    http://sourceforge.net/projects/glui/
15   Forums: http://sourceforge.net/forum/?group_id=92496
16 
17   This software is provided 'as-is', without any express or implied
18   warranty. In no event will the authors be held liable for any damages
19   arising from the use of this software.
20 
21   Permission is granted to anyone to use this software for any purpose,
22   including commercial applications, and to alter it and redistribute it
23   freely, subject to the following restrictions:
24 
25   1. The origin of this software must not be misrepresented; you must not
26   claim that you wrote the original software. If you use this software
27   in a product, an acknowledgment in the product documentation would be
28   appreciated but is not required.
29   2. Altered source versions must be plainly marked as such, and must not be
30   misrepresented as being the original software.
31   3. This notice may not be removed or altered from any source distribution.
32 
33 *****************************************************************************/
34 
35 #include "glui_internal_control.h"
36 
37 /****************************** GLUI_Checkbox::GLUI_Checkbox() **********/
38 
GLUI_Checkbox(GLUI_Node * parent,const char * name,int * value_ptr,int id,GLUI_CB cb)39 GLUI_Checkbox::GLUI_Checkbox( GLUI_Node *parent,
40                               const char *name, int *value_ptr,
41                               int id,
42                               GLUI_CB cb )
43 {
44   common_init();
45 
46   set_ptr_val( value_ptr );
47   set_name( name );
48   user_id    = id;
49   callback   = cb;
50 
51   parent->add_control( this );
52 
53   init_live();
54 }
55 
56 /****************************** GLUI_Checkbox::mouse_down_handler() **********/
57 
mouse_down_handler(int local_x,int local_y)58 int    GLUI_Checkbox::mouse_down_handler( int local_x, int local_y )
59 {
60   orig_value = int_val;
61   int_val = !int_val;
62 
63   currently_inside = true;
64   redraw();
65 
66   return false;
67 }
68 
69 
70 /****************************** GLUI_Checkbox::mouse_up_handler() **********/
71 
mouse_up_handler(int local_x,int local_y,bool inside)72 int    GLUI_Checkbox::mouse_up_handler( int local_x, int local_y, bool inside )
73 {
74   if ( NOT inside ) { /* undo effect on value */
75     int_val = orig_value;
76   }
77   else {
78     set_int_val( int_val );
79 
80     /*** Invoke the callback ***/
81     execute_callback();
82   }
83 
84   return false;
85 }
86 
87 
88 /****************************** GLUI_Checkbox::mouse_held_down_handler() ******/
89 
mouse_held_down_handler(int local_x,int local_y,bool inside)90 int    GLUI_Checkbox::mouse_held_down_handler( int local_x, int local_y,
91 					       bool inside)
92 {
93   /********** Toggle checked and unchecked bitmap if we're entering or
94     leaving the checkbox area **********/
95   if ( inside != currently_inside ) {
96      int_val = !int_val;
97      currently_inside = inside;
98      redraw();
99   }
100 
101   return false;
102 }
103 
104 
105 /****************************** GLUI_Checkbox::key_handler() **********/
106 
key_handler(unsigned char key,int modifiers)107 int    GLUI_Checkbox::key_handler( unsigned char key,int modifiers )
108 {
109   return false;
110 }
111 
112 
113 /****************************** GLUI_Checkbox::draw() **********/
114 
draw(int x,int y)115 void    GLUI_Checkbox::draw( int x, int y )
116 {
117   GLUI_DRAWINGSENTINAL_IDIOM
118 
119   if ( int_val != 0 ) {
120     if ( enabled )
121       glui->std_bitmaps.draw( GLUI_STDBITMAP_CHECKBOX_ON, 0, 0 );
122     else
123       glui->std_bitmaps.draw( GLUI_STDBITMAP_CHECKBOX_ON_DIS, 0, 0 );
124   }
125   else {
126     if ( enabled )
127       glui->std_bitmaps.draw( GLUI_STDBITMAP_CHECKBOX_OFF, 0, 0 );
128     else
129       glui->std_bitmaps.draw( GLUI_STDBITMAP_CHECKBOX_OFF_DIS, 0, 0 );
130   }
131 
132   draw_active_area();
133 
134   draw_name( text_x_offset, 10);
135 }
136 
137 /**************************** GLUI_Checkbox::draw_active_area() **************/
138 
draw_active_area(void)139 void    GLUI_Checkbox::draw_active_area( void )
140 {
141   GLUI_DRAWINGSENTINAL_IDIOM
142   int text_width, left, right;
143 
144   text_width = _glutBitmapWidthString( glui->font, name.c_str() );
145   left       = text_x_offset-3;
146   right      = left + 7 + text_width;
147 
148   if ( active ) {
149     glEnable( GL_LINE_STIPPLE );
150     glLineStipple( 1, 0x5555 );
151     glColor3f( 0., 0., 0. );
152   } else {
153     glColor3ubv( glui->bkgd_color );
154   }
155 
156   glBegin( GL_LINE_LOOP );
157   glVertex2i(left,0);     glVertex2i( right,0);
158   glVertex2i(right,h+1);   glVertex2i( left,h+1);
159   glEnd();
160 
161   glDisable( GL_LINE_STIPPLE );
162 }
163 
164 
165 /************************************ GLUI_Checkbox::update_size() **********/
166 
update_size(void)167 void   GLUI_Checkbox::update_size( void )
168 {
169   int text_size;
170 
171   if ( NOT glui )
172     return;
173 
174   text_size = _glutBitmapWidthString( glui->font, name.c_str() );
175 
176   /*  if ( w < text_x_offset + text_size + 6 )              */
177   w = text_x_offset + text_size + 6 ;
178 }
179 
180 
181 /********************************* GLUI_Checkbox::set_int_val() **************/
182 
set_int_val(int new_val)183 void    GLUI_Checkbox::set_int_val( int new_val )
184 {
185   int_val = new_val;
186 
187   /*** Update the variable we're (possibly) pointing to ***/
188   output_live(true);
189   redraw();
190 }
191