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

« back to all changes in this revision

Viewing changes to glui_button.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_button.cpp - GLUI_Button 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_Button::mouse_down_handler() **********/
 
23
 
 
24
int    GLUI_Button::mouse_down_handler( int local_x, int local_y )
 
25
{
 
26
  int_val = 1;   /** A button always in unpressed before here, so
 
27
                   now we invariably set it to 'depressed' **/
 
28
 
 
29
  currently_inside = true;
 
30
 
 
31
  draw_pressed();
 
32
 
 
33
  return false;
 
34
}
 
35
 
 
36
 
 
37
/****************************** GLUI_Button::mouse_up_handler() **********/
 
38
 
 
39
int    GLUI_Button::mouse_up_handler( int local_x, int local_y, int inside )
 
40
{
 
41
  set_int_val( 0 );   /** A button always turns off after you press it **/
 
42
 
 
43
  draw_unpressed();
 
44
 
 
45
  if ( NOT inside ) {    
 
46
  }
 
47
  else {
 
48
    /*** Invoke the callback ***/
 
49
    execute_callback();
 
50
 
 
51
    /** Tell the main gfx window to update itself **/
 
52
    if( glui )
 
53
      glui->post_update_main_gfx();
 
54
  }
 
55
 
 
56
  return false;
 
57
}
 
58
 
 
59
 
 
60
/****************************** GLUI_Button::mouse_held_down_handler() ******/
 
61
 
 
62
int    GLUI_Button::mouse_held_down_handler( int local_x, int local_y,
 
63
                                             int new_inside)
 
64
{
 
65
  if ( NOT new_inside AND currently_inside == true ) {
 
66
    draw_unpressed();
 
67
  } 
 
68
  else if ( new_inside AND currently_inside == false ) {
 
69
    draw_pressed();
 
70
  }
 
71
 
 
72
  currently_inside = new_inside;
 
73
  
 
74
  return false;
 
75
}
 
76
 
 
77
 
 
78
/****************************** GLUI_Button::key_handler() **********/
 
79
 
 
80
int    GLUI_Button::key_handler( unsigned char key,int modifiers )
 
81
{
 
82
  return false;
 
83
}
 
84
 
 
85
 
 
86
/************************************** GLUI_Button::draw_pressed() ******/
 
87
 
 
88
void   GLUI_Button::draw_pressed( void )
 
89
{
 
90
  int state, orig;
 
91
 
 
92
  if ( NOT can_draw() )
 
93
    return;
 
94
 
 
95
  orig  = set_to_glut_window();
 
96
  state = glui->set_front_draw_buffer();
 
97
  
 
98
  glColor3f( 0.0, 0.0, 0.0 );
 
99
  glPushMatrix();
 
100
  translate_to_origin();
 
101
 
 
102
  draw_text( 1 );
 
103
 
 
104
  glBegin( GL_LINE_LOOP );
 
105
  glVertex2i( 0, 0 );         glVertex2i( w, 0 );
 
106
  glVertex2i( w, h );         glVertex2i( 0, h );
 
107
  glEnd();
 
108
 
 
109
  glBegin( GL_LINE_LOOP );
 
110
  glVertex2i( 1, 1 );         glVertex2i( w-1, 1 );
 
111
  glVertex2i( w-1, h-1 );     glVertex2i( 1, h-1 );
 
112
  glEnd();
 
113
 
 
114
  glPopMatrix();
 
115
 
 
116
  glui->restore_draw_buffer(state);
 
117
  restore_window(orig);
 
118
}
 
119
 
 
120
 
 
121
/************************************** GLUI_Button::draw_unpressed() ******/
 
122
 
 
123
void   GLUI_Button::draw_unpressed( void )
 
124
{
 
125
  if ( NOT can_draw() )
 
126
    return;
 
127
 
 
128
  translate_and_draw_front();
 
129
}
 
130
 
 
131
 
 
132
 
 
133
 
 
134
/********************************************** GLUI_Button::draw() **********/
 
135
 
 
136
void    GLUI_Button::draw( int x, int y )
 
137
{
 
138
  if ( NOT can_draw() )
 
139
    return;
 
140
 
 
141
  if ( glui )
 
142
    glui->draw_raised_box( 0, 0, w, h );
 
143
 
 
144
  draw_text( 0 );
 
145
}
 
146
 
 
147
 
 
148
/**************************************** GLUI_Button::draw_text() **********/
 
149
 
 
150
void     GLUI_Button::draw_text( int sunken )
 
151
{
 
152
  int string_width, orig;
 
153
 
 
154
  if ( NOT can_draw() )
 
155
    return;
 
156
 
 
157
  orig = set_to_glut_window();
 
158
 
 
159
  glColor3ub( glui->bkgd_color.r, glui->bkgd_color.g, glui->bkgd_color.b );
 
160
  glDisable( GL_CULL_FACE );
 
161
  glBegin( GL_QUADS );
 
162
  glVertex2i( 2, 2 );         glVertex2i( w-2, 2 );
 
163
  glVertex2i( w-2, h-2 );     glVertex2i( 2, h-2 );
 
164
  glEnd();
 
165
 
 
166
  glColor3ub( 0,0,0 );
 
167
  
 
168
  string_width = _glutBitmapWidthString( glui->font,
 
169
                                         this->name );
 
170
  if ( NOT sunken ) {
 
171
    draw_name( MAX((w-string_width),0)/2, 13);
 
172
  }
 
173
  else {
 
174
    draw_name( MAX((w-string_width),0)/2 + 1, 13 + 1);
 
175
  }
 
176
 
 
177
  if ( active ) {
 
178
    glEnable( GL_LINE_STIPPLE );
 
179
    glLineStipple( 1, 0x5555 );
 
180
    
 
181
    glColor3f( 0., 0., 0. );
 
182
    
 
183
    glBegin( GL_LINE_LOOP );
 
184
    glVertex2i( 3, 3 );         glVertex2i( w-3, 3 );
 
185
    glVertex2i( w-3, h-3 );     glVertex2i( 3, h-3 );
 
186
    glEnd();
 
187
    
 
188
    glDisable( GL_LINE_STIPPLE );
 
189
  }
 
190
 
 
191
  restore_window(orig);
 
192
}
 
193
 
 
194
 
 
195
/************************************** GLUI_Button::update_size() **********/
 
196
 
 
197
void   GLUI_Button::update_size( void )
 
198
{
 
199
  int text_size;
 
200
 
 
201
  if ( NOT glui )
 
202
    return;
 
203
 
 
204
  text_size = string_width( name );
 
205
 
 
206
  if ( w < text_size + 16 )
 
207
    w = text_size + 16 ;
 
208
}