summaryrefslogtreecommitdiff
path: root/source/q_field.c
diff options
context:
space:
mode:
authorAndrey Nazarov <skuller@skuller.net>2008-09-25 13:13:05 +0000
committerAndrey Nazarov <skuller@skuller.net>2008-09-25 13:13:05 +0000
commit95f993680be144ec40fa7f08002faf27a2e229a7 (patch)
treed1bf8e3174ffbe3e6bb332682b70c694b898da67 /source/q_field.c
parentea8394f51c74208866a891def93c55bd6cd3529e (diff)
Strip trailing whitespace as gi.args() expects.
Properly set maxChars value for input fields on Unix.
Diffstat (limited to 'source/q_field.c')
-rw-r--r--source/q_field.c267
1 files changed, 145 insertions, 122 deletions
diff --git a/source/q_field.c b/source/q_field.c
index 1846a63..bd92caf 100644
--- a/source/q_field.c
+++ b/source/q_field.c
@@ -33,15 +33,18 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
IF_Init
================
*/
-void IF_Init( inputField_t *field, size_t visibleChars, size_t maxChars, const char *text ) {
- memset( field, 0, sizeof( *field ) );
+void IF_Init( inputField_t *field, size_t visibleChars, size_t maxChars ) {
+ memset( field, 0, sizeof( *field ) );
- field->maxChars = clamp( maxChars, 1, sizeof( field->text ) );
- field->visibleChars = clamp( visibleChars, 1, maxChars );
- if( text ) {
- size_t len = Q_strlcpy( field->text, text, maxChars );
- field->cursorPos = len >= maxChars ? maxChars - 1 : len;
+ if( maxChars > sizeof( field->text ) - 1 ) {
+ maxChars = sizeof( field->text ) - 1;
}
+ if( visibleChars > maxChars ) {
+ visibleChars = maxChars;
+ }
+
+ field->maxChars = maxChars;
+ field->visibleChars = visibleChars;
}
/*
@@ -50,8 +53,8 @@ IF_Clear
================
*/
void IF_Clear( inputField_t *field ) {
- memset( field->text, 0, sizeof( field->text ) );
- field->cursorPos = 0;
+ memset( field->text, 0, sizeof( field->text ) );
+ field->cursorPos = 0;
}
/*
@@ -60,9 +63,12 @@ IF_Replace
================
*/
void IF_Replace( inputField_t *field, const char *text ) {
- if( text ) {
- size_t len = Q_strlcpy( field->text, text, field->maxChars );
- field->cursorPos = len >= field->maxChars ? field->maxChars - 1 : len;
+ if( field->maxChars && text ) {
+ size_t len = Q_strlcpy( field->text, text, field->maxChars + 1 );
+ field->cursorPos = len >= field->maxChars ? field->maxChars - 1 : len;
+ } else {
+ field->text[0] = 0;
+ field->cursorPos = 0;
}
}
@@ -74,44 +80,51 @@ IF_KeyEvent
================
*/
qboolean IF_KeyEvent( inputField_t *field, int key ) {
- if( key == K_DEL ) {
- if( field->text[field->cursorPos] ) {
- memmove( field->text + field->cursorPos,
+ if( !field->maxChars ) {
+ return qfalse;
+ }
+ if( field->cursorPos >= field->maxChars ) {
+ Com_Error( ERR_FATAL, "%s: bad cursorPos", __func__ );
+ }
+
+ if( key == K_DEL ) {
+ if( field->text[field->cursorPos] ) {
+ memmove( field->text + field->cursorPos,
field->text + field->cursorPos + 1,
sizeof( field->text ) - field->cursorPos );
- }
- return qtrue;
- }
-
- if( key == K_BACKSPACE || ( key == 'h' && Key_IsDown( K_CTRL ) ) ) {
- if( field->cursorPos > 0 ) {
- memmove( field->text + field->cursorPos - 1,
+ }
+ return qtrue;
+ }
+
+ if( key == K_BACKSPACE || ( key == 'h' && Key_IsDown( K_CTRL ) ) ) {
+ if( field->cursorPos > 0 ) {
+ memmove( field->text + field->cursorPos - 1,
field->text + field->cursorPos,
sizeof( field->text ) - field->cursorPos );
- field->cursorPos--;
- }
- return qtrue;
- }
+ field->cursorPos--;
+ }
+ return qtrue;
+ }
if( key == 'w' && Key_IsDown( K_CTRL ) ) {
size_t oldpos = field->cursorPos;
// kill trailing whitespace
- while( field->cursorPos > 0 && field->text[field->cursorPos] <= 32 ) {
- field->cursorPos--;
+ while( field->cursorPos > 0 && field->text[field->cursorPos] <= 32 ) {
+ field->cursorPos--;
}
// kill this word
- while( field->cursorPos > 0 && field->text[ field->cursorPos - 1 ] > 32 ) {
- field->cursorPos--;
+ while( field->cursorPos > 0 && field->text[ field->cursorPos - 1 ] > 32 ) {
+ field->cursorPos--;
}
- memmove( field->text + field->cursorPos, field->text + oldpos,
+ memmove( field->text + field->cursorPos, field->text + oldpos,
sizeof( field->text ) - oldpos );
return qtrue;
}
if( key == 'u' && Key_IsDown( K_CTRL ) ) {
- memmove( field->text, field->text + field->cursorPos,
+ memmove( field->text, field->text + field->cursorPos,
sizeof( field->text ) - field->cursorPos );
field->cursorPos = 0;
return qtrue;
@@ -126,60 +139,60 @@ qboolean IF_KeyEvent( inputField_t *field, int key ) {
VID_SetClipboardData( field->text );
return qtrue;
}
-
- if( key == K_LEFTARROW || ( key == 'b' && Key_IsDown( K_CTRL ) ) ) {
- if( field->cursorPos > 0 ) {
- field->cursorPos--;
- }
- return qtrue;
- }
-
- if( key == K_RIGHTARROW || ( key == 'f' && Key_IsDown( K_CTRL ) ) ) {
- if( field->text[field->cursorPos] ) {
- field->cursorPos++;
- }
- return qtrue;
- }
-
- if( key == 'b' && Key_IsDown( K_ALT ) ) {
- if( field->cursorPos > 0 && field->text[field->cursorPos - 1] <= 32 ) {
- field->cursorPos--;
- }
- while( field->cursorPos > 0 && field->text[field->cursorPos] <= 32 ) {
- field->cursorPos--;
- }
- while( field->cursorPos > 0 && field->text[field->cursorPos - 1] > 32 ) {
- field->cursorPos--;
- }
- return qtrue;
- }
-
- if( key == 'f' && Key_IsDown( K_ALT ) ) {
- while( field->text[field->cursorPos] && field->text[field->cursorPos] <= 32 ) {
- field->cursorPos++;
- }
- while( field->text[field->cursorPos] > 32 ) {
- field->cursorPos++;
- }
- return qtrue;
- }
-
- if( key == K_HOME || ( key == 'a' && Key_IsDown( K_CTRL ) ) ) {
- field->cursorPos = 0;
- return qtrue;
- }
-
- if( key == K_END || ( key == 'e' && Key_IsDown( K_CTRL ) ) ) {
- field->cursorPos = strlen( field->text );
- return qtrue;
- }
-
- if( key == K_INS ) {
- Key_SetOverstrikeMode( Key_GetOverstrikeMode() ^ 1 );
- return qtrue;
- }
-
- return qfalse;
+
+ if( key == K_LEFTARROW || ( key == 'b' && Key_IsDown( K_CTRL ) ) ) {
+ if( field->cursorPos > 0 ) {
+ field->cursorPos--;
+ }
+ return qtrue;
+ }
+
+ if( key == K_RIGHTARROW || ( key == 'f' && Key_IsDown( K_CTRL ) ) ) {
+ if( field->text[field->cursorPos] ) {
+ field->cursorPos++;
+ }
+ return qtrue;
+ }
+
+ if( key == 'b' && Key_IsDown( K_ALT ) ) {
+ if( field->cursorPos > 0 && field->text[field->cursorPos - 1] <= 32 ) {
+ field->cursorPos--;
+ }
+ while( field->cursorPos > 0 && field->text[field->cursorPos] <= 32 ) {
+ field->cursorPos--;
+ }
+ while( field->cursorPos > 0 && field->text[field->cursorPos - 1] > 32 ) {
+ field->cursorPos--;
+ }
+ return qtrue;
+ }
+
+ if( key == 'f' && Key_IsDown( K_ALT ) ) {
+ while( field->text[field->cursorPos] && field->text[field->cursorPos] <= 32 ) {
+ field->cursorPos++;
+ }
+ while( field->text[field->cursorPos] > 32 ) {
+ field->cursorPos++;
+ }
+ return qtrue;
+ }
+
+ if( key == K_HOME || ( key == 'a' && Key_IsDown( K_CTRL ) ) ) {
+ field->cursorPos = 0;
+ return qtrue;
+ }
+
+ if( key == K_END || ( key == 'e' && Key_IsDown( K_CTRL ) ) ) {
+ field->cursorPos = strlen( field->text );
+ return qtrue;
+ }
+
+ if( key == K_INS ) {
+ Key_SetOverstrikeMode( Key_GetOverstrikeMode() ^ 1 );
+ return qtrue;
+ }
+
+ return qfalse;
}
/*
@@ -188,30 +201,36 @@ IF_CharEvent
================
*/
qboolean IF_CharEvent( inputField_t *field, int key ) {
- if( key < 32 || key > 127 ) {
- return qfalse; // non printable
- }
-
- if( field->cursorPos >= field->maxChars - 1 ) {
- // buffer limit was reached, just replace the last character
- field->text[field->cursorPos] = key;
- return qtrue;
- }
-
- if( Key_GetOverstrikeMode() ) {
- // replace the character at cursor and advance
- field->text[field->cursorPos] = key;
- field->cursorPos++;
- return qtrue;
- }
-
- // insert new character at cursor position
- memmove( field->text + field->cursorPos + 1, field->text + field->cursorPos,
+ if( !field->maxChars ) {
+ return qfalse;
+ }
+ if( key < 32 || key > 127 ) {
+ return qfalse; // non printable
+ }
+
+ if( field->cursorPos >= field->maxChars ) {
+ Com_Error( ERR_FATAL, "%s: bad cursorPos", __func__ );
+ }
+
+ if( field->cursorPos == field->maxChars - 1 ) {
+ // buffer limit was reached, just replace the last character
+ field->text[field->cursorPos] = key;
+ return qtrue;
+ }
+
+ if( Key_GetOverstrikeMode() ) {
+ // replace the character at cursor and advance
+ field->text[field->cursorPos++] = key;
+ return qtrue;
+ }
+
+ // insert new character at cursor position
+ memmove( field->text + field->cursorPos + 1,
+ field->text + field->cursorPos,
sizeof( field->text ) - field->cursorPos - 1 );
- field->text[field->cursorPos] = key;
- field->cursorPos++;
+ field->text[field->cursorPos++] = key;
- return qtrue;
+ return qtrue;
}
/*
@@ -223,25 +242,29 @@ Returns x offset of the rightmost character drawn.
================
*/
int IF_Draw( inputField_t *field, int x, int y, int flags, qhandle_t font ) {
- char *text = field->text;
- size_t cursorPos = field->cursorPos;
+ char *text = field->text;
+ size_t cursorPos = field->cursorPos;
size_t offset = 0;
- int ret;
+ int ret;
- if( cursorPos >= sizeof( field->text ) ) {
- Com_Error( ERR_FATAL, "IF_Draw: bad field->cursorPos" );
- }
+ if( !field->visibleChars ) {
+ return 0;
+ }
- // scroll horizontally
- if( cursorPos > field->visibleChars - 1 ) {
- cursorPos = field->visibleChars - 1;
- offset = field->cursorPos - cursorPos;
- }
+ if( cursorPos >= field->maxChars ) {
+ Com_Error( ERR_FATAL, "%s: bad cursorPos", __func__ );
+ }
+
+ // scroll horizontally
+ if( cursorPos >= field->visibleChars ) {
+ cursorPos = field->visibleChars - 1;
+ offset = field->cursorPos - cursorPos;
+ }
- // draw text
- ret = R_DrawString( x, y, flags, field->visibleChars, text + offset, font );
+ // draw text
+ ret = R_DrawString( x, y, flags, field->visibleChars, text + offset, font );
- if( flags & UI_DRAWCURSOR ) {
+ if( flags & UI_DRAWCURSOR ) {
// draw blinking cursor
if( ( com_localTime >> 8 ) & 1 ) {
int c = Key_GetOverstrikeMode() ? 11 : '_';