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