Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / glui / glui_button.cpp
1 /****************************************************************************
2   
3   GLUI User Interface Toolkit
4   ---------------------------
5
6      glui_button.cpp - GLUI_Button 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 #include "glui_internal_control.h"
32
33 /****************************** GLUI_Button::GLUI_Button() **********/
34
35 GLUI_Button::GLUI_Button( GLUI_Node *parent, const char *name,
36                           int id, GLUI_CB cb )
37 {
38   common_init();
39   user_id     = id;
40   callback    = cb;
41   set_name( name );
42   currently_inside = false; 
43   
44   parent->add_control( this );
45 }
46
47
48 /****************************** GLUI_Button::mouse_down_handler() **********/
49
50 int    GLUI_Button::mouse_down_handler( int local_x, int local_y )
51 {
52   int_val = 1;   /** A button always in unpressed before here, so
53                    now we invariably set it to 'depressed' **/
54
55   currently_inside = true;
56   redraw();
57
58   return false;
59 }
60
61
62 /****************************** GLUI_Button::mouse_up_handler() **********/
63
64 int    GLUI_Button::mouse_up_handler( int local_x, int local_y, bool inside )
65 {
66   set_int_val( 0 );   /** A button always turns off after you press it **/
67
68   currently_inside = false; 
69   redraw();
70
71   if ( inside ) { 
72     /*** Invoke the user's callback ***/
73     execute_callback();
74   }
75
76   return false;
77 }
78
79
80 /****************************** GLUI_Button::mouse_held_down_handler() ******/
81
82 int    GLUI_Button::mouse_held_down_handler( int local_x, int local_y,
83                                              bool new_inside)
84 {
85   if (new_inside != currently_inside) {
86     currently_inside = new_inside;
87     redraw();
88   }
89   
90   return false;
91 }
92
93
94 /****************************** GLUI_Button::key_handler() **********/
95
96 int    GLUI_Button::key_handler( unsigned char key,int modifiers )
97 {
98   return false;
99 }
100
101 /********************************************** GLUI_Button::draw() **********/
102
103 void    GLUI_Button::draw( int x, int y )
104 {
105   if (currently_inside) draw_pressed();
106   else {
107     glui->draw_raised_box( 0, 0, w, h );
108     draw_text( 0 );
109   }
110 }
111
112
113 /************************************** GLUI_Button::draw_pressed() ******/
114
115 void   GLUI_Button::draw_pressed( void )
116 {
117   glColor3f( 0.0, 0.0, 0.0 );
118
119   draw_text( 1 );
120
121   glBegin( GL_LINE_LOOP );
122   glVertex2i( 0, 0 );         glVertex2i( w, 0 );
123   glVertex2i( w, h );         glVertex2i( 0, h );
124   glEnd();
125
126   glBegin( GL_LINE_LOOP );
127   glVertex2i( 1, 1 );         glVertex2i( w-1, 1 );
128   glVertex2i( w-1, h-1 );     glVertex2i( 1, h-1 );
129   glEnd();
130 }
131
132
133 /**************************************** GLUI_Button::draw_text() **********/
134
135 void     GLUI_Button::draw_text( int sunken )
136 {
137   int string_width;
138
139   glColor3ub( glui->bkgd_color.r, glui->bkgd_color.g, glui->bkgd_color.b );
140   glDisable( GL_CULL_FACE );
141   glBegin( GL_QUADS );
142   glVertex2i( 2, 2 );         glVertex2i( w-2, 2 );
143   glVertex2i( w-2, h-2 );     glVertex2i( 2, h-2 );
144   glEnd();
145
146   glColor3ub( 0,0,0 );
147   
148   string_width = _glutBitmapWidthString( glui->font,
149                                          this->name.c_str() );
150   if ( NOT sunken ) {
151     draw_name( MAX((w-string_width),0)/2, 13);
152   }
153   else {
154     draw_name( MAX((w-string_width),0)/2 + 1, 13 + 1);
155   }
156
157   if ( active ) {
158     glEnable( GL_LINE_STIPPLE );
159     glLineStipple( 1, 0x5555 );
160     
161     glColor3f( 0., 0., 0. );
162     
163     glBegin( GL_LINE_LOOP );
164     glVertex2i( 3, 3 );         glVertex2i( w-3, 3 );
165     glVertex2i( w-3, h-3 );     glVertex2i( 3, h-3 );
166     glEnd();
167     
168     glDisable( GL_LINE_STIPPLE );
169   }
170 }
171
172
173 /************************************** GLUI_Button::update_size() **********/
174
175 void   GLUI_Button::update_size( void )
176 {
177   int text_size;
178
179   if ( NOT glui )
180     return;
181
182   text_size = string_width( name );
183
184   if ( w < text_size + 16 )
185     w = text_size + 16 ;
186 }