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 ||
38 style==PUSTYLE_SMALL_SHADED) ) ? -style :
39 (accepting ? -style : style ), colour, FALSE ) ;
41 int xx = puGetStringWidth ( legendFont, " " ) ;
42 int yy = ( abox.max[1] - abox.min[1] - puGetStringHeight(legendFont) ) / 2 ;
46 char val [ PUSTRING_MAX ] ;
49 /* Highlight the select area */
51 if ( select_end_position > 0 &&
52 select_end_position != select_start_position )
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] ;
59 glColor3f ( 1.0f, 1.0f, 0.7f ) ;
60 glRecti ( cpos1, dy + abox.min[1] + 6 ,
61 cpos2, dy + abox.max[1] - 6 ) ;
68 /* If greyed out then halve the opacity when drawing the label and legend */
71 glColor4fv ( colour [ PUCOL_LEGEND ] ) ;
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 */
78 char val [ PUSTRING_MAX ] ;
81 puDrawString ( legendFont, val,
82 dx + abox.min[0] + xx,
83 dy + abox.min[1] + yy ) ;
85 draw_label ( dx, dy ) ;
90 char val [ PUSTRING_MAX ] ;
93 /* Draw the 'I' bar cursor. */
95 if ( cursor_position >= 0 )
97 val [ cursor_position ] = '\0' ;
99 int cpos = puGetStringWidth ( legendFont, val ) + xx + dx + abox.min[0] ;
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 ) ;
117 void puInput::doHit ( int button, int updown, int x, int /* y */ )
119 if ( button == PU_LEFT_BUTTON )
121 /* Most GUI's activate a button on button-UP not button-DOWN. */
123 if ( updown == active_mouse_edge || active_mouse_edge == PU_UP_AND_DOWN )
128 getValue ( & strval ) ;
129 char *tmpval = new char [ strlen(strval) + 1 ] ;
130 strcpy ( tmpval, strval ) ;
132 int i = strlen ( tmpval ) ;
134 while ( x <= puGetStringWidth ( legendFont, tmpval ) + abox.min[0] &&
139 cursor_position = i ;
140 normalize_cursors () ;
150 int puInput::checkKey ( int key, int updown )
154 if ( ! isAcceptingInput() || ! isActive () || ! isVisible () )
157 normalize_cursors () ;
163 case PU_KEY_PAGE_UP :
164 case PU_KEY_PAGE_DOWN :
165 case PU_KEY_INSERT : return FALSE ;
169 case 0x1B /* ESC */ :
172 case '\n' : /* Carriage return/Line Feed/TAB -- End of input */
174 normalize_cursors () ;
178 case '\b' : /* Backspace */
179 if ( cursor_position > 0 )
180 for ( p = & string [ --cursor_position ] ; *p != '\0' ; p++ )
184 case 0x7F : /* DEL */
185 if ( select_start_position != select_end_position )
187 char *p1 = & string [ select_start_position ] ;
188 char *p2 = & string [ select_end_position ] ;
190 while ( *p1 != '\0' )
193 select_end_position = select_start_position ;
196 for ( p = & string [ cursor_position ] ; *p != '\0' ; p++ )
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 ;
207 if ( key < ' ' || key > 127 ) return FALSE ;
209 if ( strlen ( string ) >= PUSTRING_MAX )
212 for ( p = & string [ strlen(string) ] ;
213 p != &string[cursor_position] ; p-- )
221 setValue ( string ) ;
222 normalize_cursors () ;