]> git.mxchange.org Git - flightgear.git/blob - Lib/PUI/puInput.cxx
Merge FG_Lib as subdirectory
[flightgear.git] / Lib / PUI / puInput.cxx
1
2 #include "puLocal.h"
3
4 void puInput::normalize_cursors ( void )
5 {
6   char val [ PUSTRING_MAX ] ;
7   getValue ( val ) ;
8   int sl = strlen ( val ) ;
9
10   /* Clamp the positions to the limits of the text.  */
11
12   if ( cursor_position       <  0 ) cursor_position       =  0 ;
13   if ( select_start_position <  0 ) select_start_position =  0 ;
14   if ( select_end_position   <  0 ) select_end_position   =  0 ;
15   if ( cursor_position       > sl ) cursor_position       = sl ;
16   if ( select_start_position > sl ) select_start_position = sl ;
17   if ( select_end_position   > sl ) select_end_position   = sl ;
18
19   /* Swap the ends of the select window if they get crossed over */
20
21   if ( select_end_position < select_start_position )
22   {
23     int tmp = select_end_position ;     
24     select_end_position = select_start_position ;     
25     select_start_position = tmp ;
26   }
27 }
28
29 void puInput::draw ( int dx, int dy )
30 {
31   normalize_cursors () ;
32
33   if ( !visible ) return ;
34
35   /* 3D Input boxes look nicest if they are always in inverse style. */
36
37   abox . draw ( dx, dy, ( (style==PUSTYLE_SMALL_BEVELLED ||
38                            style==PUSTYLE_SMALL_SHADED) ) ? -style :
39                         (accepting ? -style : style ), colour, FALSE ) ;
40
41   int xx = puGetStringWidth ( legendFont, " " ) ;
42   int yy = ( abox.max[1] - abox.min[1] - puGetStringHeight(legendFont) ) / 2 ;
43
44   if ( accepting )
45   {
46     char val [ PUSTRING_MAX ] ;
47     getValue ( val ) ;
48
49     /* Highlight the select area */
50
51     if ( select_end_position > 0 &&
52          select_end_position != select_start_position )    
53     {
54       val [ select_end_position ] = '\0' ;
55       int cpos2 = puGetStringWidth ( legendFont, val ) + xx + dx + abox.min[0] ;
56       val [ select_start_position ] = '\0' ;
57       int cpos1 = puGetStringWidth ( legendFont, val ) + xx + dx + abox.min[0] ;
58
59       glColor3f ( 1.0f, 1.0f, 0.7f ) ;
60       glRecti ( cpos1, dy + abox.min[1] + 6 ,
61                 cpos2, dy + abox.max[1] - 6 ) ;
62     }
63   }
64
65   /* Draw the text */
66
67   {
68     /* If greyed out then halve the opacity when drawing the label and legend */
69
70     if ( active )
71       glColor4fv ( colour [ PUCOL_LEGEND ] ) ;
72     else
73       glColor4f ( colour [ PUCOL_LEGEND ][0],
74                   colour [ PUCOL_LEGEND ][1],
75                   colour [ PUCOL_LEGEND ][2],
76                   colour [ PUCOL_LEGEND ][3] / 2.0f ) ; /* 50% more transparent */
77
78     char val [ PUSTRING_MAX ] ;
79     getValue ( val ) ;
80
81     puDrawString ( legendFont, val,
82                   dx + abox.min[0] + xx,
83                   dy + abox.min[1] + yy ) ;
84
85     draw_label ( dx, dy ) ;
86   }
87
88   if ( accepting )
89   { 
90     char val [ PUSTRING_MAX ] ;
91     getValue ( val ) ;
92
93     /* Draw the 'I' bar cursor. */
94
95     if ( cursor_position >= 0 )
96     {
97       val [ cursor_position ] = '\0' ;
98
99       int cpos = puGetStringWidth ( legendFont, val ) + xx + dx + abox.min[0] ;
100
101       glColor3f ( 0.1f, 0.1f, 1.0f ) ;
102       glBegin   ( GL_LINES ) ;
103       glVertex2i ( cpos    , dy + abox.min[1] + 7 ) ;
104       glVertex2i ( cpos    , dy + abox.max[1] - 7 ) ;
105       glVertex2i ( cpos - 1, dy + abox.min[1] + 7 ) ;
106       glVertex2i ( cpos - 1, dy + abox.max[1] - 7 ) ;
107       glVertex2i ( cpos - 4, dy + abox.min[1] + 7 ) ;
108       glVertex2i ( cpos + 3, dy + abox.min[1] + 7 ) ;
109       glVertex2i ( cpos - 4, dy + abox.max[1] - 7 ) ;
110       glVertex2i ( cpos + 3, dy + abox.max[1] - 7 ) ;
111       glEnd      () ;
112     }
113   }
114 }
115
116
117 void puInput::doHit ( int button, int updown, int x, int /* y */ )
118 {
119   if ( button == PU_LEFT_BUTTON )
120   {
121     /* Most GUI's activate a button on button-UP not button-DOWN. */
122
123     if ( updown == active_mouse_edge || active_mouse_edge == PU_UP_AND_DOWN )
124     {
125       lowlight () ;
126
127       char *strval ;
128       getValue ( & strval ) ;
129       char *tmpval = new char [ strlen(strval) + 1 ] ;
130       strcpy ( tmpval, strval ) ;
131
132       int i = strlen ( tmpval ) ;
133
134       while ( x <= puGetStringWidth ( legendFont, tmpval ) + abox.min[0] &&
135               i >= 0 )
136         tmpval[--i] = '\0' ;
137     
138       accepting = TRUE ;
139       cursor_position = i ;
140       normalize_cursors () ;
141       invokeCallback () ;
142     }
143     else
144       highlight () ;
145   }
146   else
147     lowlight () ;
148 }
149
150 int puInput::checkKey ( int key, int updown )
151 {
152   (updown,updown);
153
154   if ( ! isAcceptingInput() || ! isActive () || ! isVisible () )
155     return FALSE ;
156
157   normalize_cursors () ;
158
159   char *p ;
160
161   switch ( key )
162   {
163     case PU_KEY_PAGE_UP   :
164     case PU_KEY_PAGE_DOWN :
165     case PU_KEY_INSERT    : return FALSE ; 
166
167     case PU_KEY_UP   :
168     case PU_KEY_DOWN :
169     case 0x1B /* ESC */ :
170     case '\t' :
171     case '\r' :
172     case '\n' : /* Carriage return/Line Feed/TAB  -- End of input */
173       rejectInput () ;
174       normalize_cursors () ;
175       invokeCallback () ;
176       break ;
177
178     case '\b' : /* Backspace */
179       if ( cursor_position > 0 ) 
180         for ( p = & string [ --cursor_position ] ; *p != '\0' ; p++ )
181           *p = *(p+1) ;
182       break ;
183
184     case 0x7F : /* DEL */
185       if ( select_start_position != select_end_position )
186       {
187         char *p1 = & string [ select_start_position ] ;
188         char *p2 = & string [ select_end_position   ] ;
189
190         while ( *p1 != '\0' )
191           *p1++ = *p2++ ;
192
193         select_end_position = select_start_position ;
194       }
195       else
196         for ( p = & string [ cursor_position ] ; *p != '\0' ; p++ )
197           *p = *(p+1) ;
198       break ;
199
200     case 0x15 /* ^U */ : string [ 0 ] = '\0' ; break ;
201     case PU_KEY_HOME   : cursor_position = 0 ; break ;
202     case PU_KEY_END    : cursor_position = PUSTRING_MAX ; break ;
203     case PU_KEY_LEFT   : cursor_position-- ; break ;
204     case PU_KEY_RIGHT  : cursor_position++ ; break ;
205
206     default:
207       if ( key < ' ' || key > 127 ) return FALSE ;
208
209       if ( strlen ( string ) >= PUSTRING_MAX )
210         return FALSE ;
211
212       for ( p = & string [ strlen(string) ] ;
213                p != &string[cursor_position] ; p-- )
214         *(p+1) = *p ;
215
216       *p = key ;
217       cursor_position++ ;
218       break ;
219   }
220
221   setValue ( string ) ;
222   normalize_cursors () ;
223   return TRUE ;
224 }
225
226