4 void puInput::normalize_cursors ( void )
6 char val [ PUSTRING_MAX ] ;
8 int sl = strlen ( val ) ;
10 /* Clamp the positions to the limits of the text. */
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 ;
19 /* Swap the ends of the select window if they get crossed over */
21 if ( select_end_position < select_start_position )
23 int tmp = select_end_position ;
24 select_end_position = select_start_position ;
25 select_start_position = tmp ;
29 void puInput::draw ( int dx, int dy )
31 normalize_cursors () ;
33 if ( !visible ) return ;
35 /* 3D Input boxes look nicest if they are always in inverse style. */
37 abox . draw ( dx, dy, (style==PUSTYLE_SMALL_BEVELLED) ? -style :
38 (accepting ? -style : style ), colour, FALSE ) ;
40 int xx = puGetStringWidth ( legendFont, " " ) ;
41 int yy = ( abox.max[1] - abox.min[1] - puGetStringHeight(legendFont) ) / 2 ;
45 char val [ PUSTRING_MAX ] ;
48 /* Highlight the select area */
50 if ( select_end_position > 0 &&
51 select_end_position != select_start_position )
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] ;
58 glColor3f ( 1.0, 1.0, 0.7 ) ;
59 glRecti ( cpos1, dy + abox.min[1] + 6 ,
60 cpos2, dy + abox.max[1] - 6 ) ;
67 /* If greyed out then halve the opacity when drawing the label and legend */
70 glColor4fv ( colour [ PUCOL_LEGEND ] ) ;
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 */
77 char val [ PUSTRING_MAX ] ;
80 puDrawString ( legendFont, val,
81 dx + abox.min[0] + xx,
82 dy + abox.min[1] + yy ) ;
84 draw_label ( dx, dy ) ;
89 char val [ PUSTRING_MAX ] ;
92 /* Draw the 'I' bar cursor. */
94 if ( cursor_position >= 0 )
96 val [ cursor_position ] = '\0' ;
98 int cpos = puGetStringWidth ( legendFont, val ) + xx + dx + abox.min[0] ;
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 ) ;
116 void puInput::doHit ( int button, int updown, int x, int /* y */ )
118 if ( button == PU_LEFT_BUTTON )
120 /* Most GUI's activate a button on button-UP not button-DOWN. */
122 if ( updown == active_mouse_edge || active_mouse_edge == PU_UP_AND_DOWN )
127 getValue ( & strval ) ;
128 char *tmpval = new char [ strlen(strval) + 1 ] ;
129 strcpy ( tmpval, strval ) ;
131 int i = strlen ( tmpval ) ;
133 while ( x <= puGetStringWidth ( legendFont, tmpval ) + abox.min[0] &&
138 cursor_position = i ;
139 normalize_cursors () ;
149 int puInput::checkKey ( int key, int updown )
153 if ( ! isAcceptingInput() || ! isActive () || ! isVisible () )
156 normalize_cursors () ;
162 case PU_KEY_PAGE_UP :
163 case PU_KEY_PAGE_DOWN :
164 case PU_KEY_INSERT : return FALSE ;
168 case 0x1B /* ESC */ :
171 case '\n' : /* Carriage return/Line Feed/TAB -- End of input */
173 normalize_cursors () ;
177 case '\b' : /* Backspace */
178 if ( cursor_position > 0 )
179 for ( p = & string [ --cursor_position ] ; *p != '\0' ; p++ )
183 case 0x7F : /* DEL */
184 if ( select_start_position != select_end_position )
186 char *p1 = & string [ select_start_position ] ;
187 char *p2 = & string [ select_end_position ] ;
189 while ( *p1 != '\0' )
192 select_end_position = select_start_position ;
195 for ( p = & string [ cursor_position ] ; *p != '\0' ; p++ )
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 ;
206 if ( key < ' ' || key > 127 ) return FALSE ;
208 if ( strlen ( string ) >= PUSTRING_MAX )
211 for ( p = & string [ strlen(string) ] ;
212 p != &string[cursor_position] ; p-- )
220 setValue ( string ) ;
221 normalize_cursors () ;