doom3-bfg/neo/framework/EditField.cpp

709 lines
14 KiB
C++
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#include "../idlib/precompiled.h"
#pragma hdrstop
static autoComplete_t globalAutoComplete;
/*
===============
FindMatches
===============
*/
static void FindMatches( const char* s )
{
2012-11-26 18:58:24 +00:00
int i;
if( idStr::Icmpn( s, globalAutoComplete.completionString, strlen( globalAutoComplete.completionString ) ) != 0 )
{
2012-11-26 18:58:24 +00:00
return;
}
globalAutoComplete.matchCount++;
if( globalAutoComplete.matchCount == 1 )
{
2012-11-26 18:58:24 +00:00
idStr::Copynz( globalAutoComplete.currentMatch, s, sizeof( globalAutoComplete.currentMatch ) );
return;
}
2012-11-26 18:58:24 +00:00
// cut currentMatch to the amount common with s
for( i = 0; s[i]; i++ )
{
if( tolower( globalAutoComplete.currentMatch[i] ) != tolower( s[i] ) )
{
2012-11-26 18:58:24 +00:00
globalAutoComplete.currentMatch[i] = 0;
break;
}
}
globalAutoComplete.currentMatch[i] = 0;
}
/*
===============
FindIndexMatch
===============
*/
static void FindIndexMatch( const char* s )
{
2012-11-26 18:58:24 +00:00
if( idStr::Icmpn( s, globalAutoComplete.completionString, strlen( globalAutoComplete.completionString ) ) != 0 )
{
2012-11-26 18:58:24 +00:00
return;
}
if( globalAutoComplete.findMatchIndex == globalAutoComplete.matchIndex )
{
2012-11-26 18:58:24 +00:00
idStr::Copynz( globalAutoComplete.currentMatch, s, sizeof( globalAutoComplete.currentMatch ) );
}
2012-11-26 18:58:24 +00:00
globalAutoComplete.findMatchIndex++;
}
/*
===============
PrintMatches
===============
*/
static void PrintMatches( const char* s )
{
if( idStr::Icmpn( s, globalAutoComplete.currentMatch, strlen( globalAutoComplete.currentMatch ) ) == 0 )
{
2012-11-26 18:58:24 +00:00
common->Printf( " %s\n", s );
}
}
/*
===============
PrintCvarMatches
===============
*/
static void PrintCvarMatches( const char* s )
{
if( idStr::Icmpn( s, globalAutoComplete.currentMatch, strlen( globalAutoComplete.currentMatch ) ) == 0 )
{
2012-11-26 18:58:24 +00:00
common->Printf( " %s" S_COLOR_WHITE " = \"%s\"\n", s, cvarSystem->GetCVarString( s ) );
}
}
/*
===============
idEditField::idEditField
===============
*/
idEditField::idEditField()
{
2012-11-26 18:58:24 +00:00
widthInChars = 0;
Clear();
}
/*
===============
idEditField::~idEditField
===============
*/
idEditField::~idEditField()
{
2012-11-26 18:58:24 +00:00
}
/*
===============
idEditField::Clear
===============
*/
void idEditField::Clear()
{
2012-11-26 18:58:24 +00:00
buffer[0] = 0;
cursor = 0;
scroll = 0;
autoComplete.length = 0;
autoComplete.valid = false;
}
/*
===============
idEditField::SetWidthInChars
===============
*/
void idEditField::SetWidthInChars( int w )
{
2012-11-26 18:58:24 +00:00
assert( w <= MAX_EDIT_LINE );
widthInChars = w;
}
/*
===============
idEditField::SetCursor
===============
*/
void idEditField::SetCursor( int c )
{
2012-11-26 18:58:24 +00:00
assert( c <= MAX_EDIT_LINE );
cursor = c;
}
/*
===============
idEditField::GetCursor
===============
*/
int idEditField::GetCursor() const
{
2012-11-26 18:58:24 +00:00
return cursor;
}
/*
===============
idEditField::ClearAutoComplete
===============
*/
void idEditField::ClearAutoComplete()
{
if( autoComplete.length > 0 && autoComplete.length <= ( int ) strlen( buffer ) )
{
2012-11-26 18:58:24 +00:00
buffer[autoComplete.length] = '\0';
if( cursor > autoComplete.length )
{
2012-11-26 18:58:24 +00:00
cursor = autoComplete.length;
}
}
autoComplete.length = 0;
autoComplete.valid = false;
}
/*
===============
idEditField::GetAutoCompleteLength
===============
*/
int idEditField::GetAutoCompleteLength() const
{
2012-11-26 18:58:24 +00:00
return autoComplete.length;
}
/*
===============
idEditField::AutoComplete
===============
*/
void idEditField::AutoComplete()
{
2012-11-26 18:58:24 +00:00
char completionArgString[MAX_EDIT_LINE];
idCmdArgs args;
if( !autoComplete.valid )
{
2012-11-26 18:58:24 +00:00
args.TokenizeString( buffer, false );
idStr::Copynz( autoComplete.completionString, args.Argv( 0 ), sizeof( autoComplete.completionString ) );
idStr::Copynz( completionArgString, args.Args(), sizeof( completionArgString ) );
autoComplete.matchCount = 0;
autoComplete.matchIndex = 0;
autoComplete.currentMatch[0] = 0;
if( strlen( autoComplete.completionString ) == 0 )
{
2012-11-26 18:58:24 +00:00
return;
}
2012-11-26 18:58:24 +00:00
globalAutoComplete = autoComplete;
2012-11-26 18:58:24 +00:00
cmdSystem->CommandCompletion( FindMatches );
cvarSystem->CommandCompletion( FindMatches );
2012-11-26 18:58:24 +00:00
autoComplete = globalAutoComplete;
if( autoComplete.matchCount == 0 )
{
2012-11-26 18:58:24 +00:00
return; // no matches
}
2012-11-26 18:58:24 +00:00
// when there's only one match or there's an argument
if( autoComplete.matchCount == 1 || completionArgString[0] != '\0' )
{
2012-11-26 18:58:24 +00:00
/// try completing arguments
idStr::Append( autoComplete.completionString, sizeof( autoComplete.completionString ), " " );
idStr::Append( autoComplete.completionString, sizeof( autoComplete.completionString ), completionArgString );
autoComplete.matchCount = 0;
2012-11-26 18:58:24 +00:00
globalAutoComplete = autoComplete;
2012-11-26 18:58:24 +00:00
cmdSystem->ArgCompletion( autoComplete.completionString, FindMatches );
cvarSystem->ArgCompletion( autoComplete.completionString, FindMatches );
2012-11-26 18:58:24 +00:00
autoComplete = globalAutoComplete;
2012-11-26 18:58:24 +00:00
idStr::snPrintf( buffer, sizeof( buffer ), "%s", autoComplete.currentMatch );
if( autoComplete.matchCount == 0 )
{
2012-11-26 18:58:24 +00:00
// no argument matches
idStr::Append( buffer, sizeof( buffer ), " " );
idStr::Append( buffer, sizeof( buffer ), completionArgString );
SetCursor( strlen( buffer ) );
return;
}
}
else
{
2012-11-26 18:58:24 +00:00
// multiple matches, complete to shortest
idStr::snPrintf( buffer, sizeof( buffer ), "%s", autoComplete.currentMatch );
if( strlen( completionArgString ) )
{
2012-11-26 18:58:24 +00:00
idStr::Append( buffer, sizeof( buffer ), " " );
idStr::Append( buffer, sizeof( buffer ), completionArgString );
}
}
2012-11-26 18:58:24 +00:00
autoComplete.length = strlen( buffer );
autoComplete.valid = ( autoComplete.matchCount != 1 );
SetCursor( autoComplete.length );
2012-11-26 18:58:24 +00:00
common->Printf( "]%s\n", buffer );
2012-11-26 18:58:24 +00:00
// run through again, printing matches
globalAutoComplete = autoComplete;
2012-11-26 18:58:24 +00:00
cmdSystem->CommandCompletion( PrintMatches );
cmdSystem->ArgCompletion( autoComplete.completionString, PrintMatches );
cvarSystem->CommandCompletion( PrintCvarMatches );
cvarSystem->ArgCompletion( autoComplete.completionString, PrintMatches );
}
else if( autoComplete.matchCount != 1 )
{
2012-11-26 18:58:24 +00:00
// get the next match and show instead
autoComplete.matchIndex++;
if( autoComplete.matchIndex == autoComplete.matchCount )
{
2012-11-26 18:58:24 +00:00
autoComplete.matchIndex = 0;
}
autoComplete.findMatchIndex = 0;
2012-11-26 18:58:24 +00:00
globalAutoComplete = autoComplete;
2012-11-26 18:58:24 +00:00
cmdSystem->CommandCompletion( FindIndexMatch );
cmdSystem->ArgCompletion( autoComplete.completionString, FindIndexMatch );
cvarSystem->CommandCompletion( FindIndexMatch );
cvarSystem->ArgCompletion( autoComplete.completionString, FindIndexMatch );
2012-11-26 18:58:24 +00:00
autoComplete = globalAutoComplete;
2012-11-26 18:58:24 +00:00
// and print it
idStr::snPrintf( buffer, sizeof( buffer ), autoComplete.currentMatch );
if( autoComplete.length > ( int )strlen( buffer ) )
{
2012-11-26 18:58:24 +00:00
autoComplete.length = strlen( buffer );
}
SetCursor( autoComplete.length );
}
}
/*
===============
idEditField::CharEvent
===============
*/
void idEditField::CharEvent( int ch )
{
2012-11-26 18:58:24 +00:00
int len;
if( ch == 'v' - 'a' + 1 ) // ctrl-v is paste
{
2012-11-26 18:58:24 +00:00
Paste();
return;
}
if( ch == 'c' - 'a' + 1 ) // ctrl-c clears the field
{
2012-11-26 18:58:24 +00:00
Clear();
return;
}
2012-11-26 18:58:24 +00:00
len = strlen( buffer );
if( ch == 'h' - 'a' + 1 || ch == K_BACKSPACE ) // ctrl-h is backspace
{
if( cursor > 0 )
{
2012-11-26 18:58:24 +00:00
memmove( buffer + cursor - 1, buffer + cursor, len + 1 - cursor );
cursor--;
if( cursor < scroll )
{
2012-11-26 18:58:24 +00:00
scroll--;
}
}
return;
}
if( ch == 'a' - 'a' + 1 ) // ctrl-a is home
{
2012-11-26 18:58:24 +00:00
cursor = 0;
scroll = 0;
return;
}
if( ch == 'e' - 'a' + 1 ) // ctrl-e is end
{
2012-11-26 18:58:24 +00:00
cursor = len;
scroll = cursor - widthInChars;
return;
}
2012-11-26 18:58:24 +00:00
//
// ignore any other non printable chars
//
if( ch < 32 )
{
2012-11-26 18:58:24 +00:00
return;
}
if( idKeyInput::GetOverstrikeMode() )
{
if( cursor == MAX_EDIT_LINE - 1 )
{
2012-11-26 18:58:24 +00:00
return;
}
buffer[cursor] = ch;
cursor++;
}
else // insert mode
{
if( len == MAX_EDIT_LINE - 1 )
{
2012-11-26 18:58:24 +00:00
return; // all full
}
memmove( buffer + cursor + 1, buffer + cursor, len + 1 - cursor );
buffer[cursor] = ch;
cursor++;
}
if( cursor >= widthInChars )
{
2012-11-26 18:58:24 +00:00
scroll++;
}
if( cursor == len + 1 )
{
2012-11-26 18:58:24 +00:00
buffer[cursor] = 0;
}
}
/*
===============
idEditField::KeyDownEvent
===============
*/
void idEditField::KeyDownEvent( int key )
{
2012-11-26 18:58:24 +00:00
int len;
2012-11-26 18:58:24 +00:00
// shift-insert is paste
if( ( ( key == K_INS ) || ( key == K_KP_0 ) ) && ( idKeyInput::IsDown( K_LSHIFT ) || idKeyInput::IsDown( K_RSHIFT ) ) )
{
2012-11-26 18:58:24 +00:00
ClearAutoComplete();
Paste();
return;
}
2012-11-26 18:58:24 +00:00
len = strlen( buffer );
if( key == K_DEL )
{
if( autoComplete.length )
{
2012-11-26 18:58:24 +00:00
ClearAutoComplete();
}
else if( cursor < len )
{
2012-11-26 18:58:24 +00:00
memmove( buffer + cursor, buffer + cursor + 1, len - cursor );
}
return;
}
if( key == K_RIGHTARROW )
{
if( idKeyInput::IsDown( K_LCTRL ) || idKeyInput::IsDown( K_RCTRL ) )
{
2012-11-26 18:58:24 +00:00
// skip to next word
while( ( cursor < len ) && ( buffer[ cursor ] != ' ' ) )
{
2012-11-26 18:58:24 +00:00
cursor++;
}
while( ( cursor < len ) && ( buffer[ cursor ] == ' ' ) )
{
2012-11-26 18:58:24 +00:00
cursor++;
}
}
else
{
2012-11-26 18:58:24 +00:00
cursor++;
}
if( cursor > len )
{
2012-11-26 18:58:24 +00:00
cursor = len;
}
if( cursor >= scroll + widthInChars )
{
2012-11-26 18:58:24 +00:00
scroll = cursor - widthInChars + 1;
}
if( autoComplete.length > 0 )
{
2012-11-26 18:58:24 +00:00
autoComplete.length = cursor;
}
return;
}
if( key == K_LEFTARROW )
{
if( idKeyInput::IsDown( K_LCTRL ) || idKeyInput::IsDown( K_RCTRL ) )
{
2012-11-26 18:58:24 +00:00
// skip to previous word
while( ( cursor > 0 ) && ( buffer[ cursor - 1 ] == ' ' ) )
{
2012-11-26 18:58:24 +00:00
cursor--;
}
while( ( cursor > 0 ) && ( buffer[ cursor - 1 ] != ' ' ) )
{
2012-11-26 18:58:24 +00:00
cursor--;
}
}
else
{
2012-11-26 18:58:24 +00:00
cursor--;
}
if( cursor < 0 )
{
2012-11-26 18:58:24 +00:00
cursor = 0;
}
if( cursor < scroll )
{
2012-11-26 18:58:24 +00:00
scroll = cursor;
}
if( autoComplete.length )
{
2012-11-26 18:58:24 +00:00
autoComplete.length = cursor;
}
return;
}
if( key == K_HOME || ( key == K_A && ( idKeyInput::IsDown( K_LCTRL ) || idKeyInput::IsDown( K_RCTRL ) ) ) )
{
2012-11-26 18:58:24 +00:00
cursor = 0;
scroll = 0;
if( autoComplete.length )
{
2012-11-26 18:58:24 +00:00
autoComplete.length = cursor;
autoComplete.valid = false;
}
return;
}
if( key == K_END || ( key == K_E && ( idKeyInput::IsDown( K_LCTRL ) || idKeyInput::IsDown( K_RCTRL ) ) ) )
{
2012-11-26 18:58:24 +00:00
cursor = len;
if( cursor >= scroll + widthInChars )
{
2012-11-26 18:58:24 +00:00
scroll = cursor - widthInChars + 1;
}
if( autoComplete.length )
{
2012-11-26 18:58:24 +00:00
autoComplete.length = cursor;
autoComplete.valid = false;
}
return;
}
if( key == K_INS )
{
2012-11-26 18:58:24 +00:00
idKeyInput::SetOverstrikeMode( !idKeyInput::GetOverstrikeMode() );
return;
}
2012-11-26 18:58:24 +00:00
// clear autocompletion buffer on normal key input
if( key != K_CAPSLOCK && key != K_LALT && key != K_LCTRL && key != K_LSHIFT && key != K_RALT && key != K_RCTRL && key != K_RSHIFT )
{
2012-11-26 18:58:24 +00:00
ClearAutoComplete();
}
}
/*
===============
idEditField::Paste
===============
*/
void idEditField::Paste()
{
char* cbd;
2012-11-26 18:58:24 +00:00
int pasteLen, i;
2012-11-26 18:58:24 +00:00
cbd = Sys_GetClipboardData();
if( !cbd )
{
2012-11-26 18:58:24 +00:00
return;
}
2012-11-26 18:58:24 +00:00
// send as if typed, so insert / overstrike works properly
pasteLen = strlen( cbd );
for( i = 0; i < pasteLen; i++ )
{
2012-11-26 18:58:24 +00:00
CharEvent( cbd[i] );
}
2012-11-26 18:58:24 +00:00
Mem_Free( cbd );
}
/*
===============
idEditField::GetBuffer
===============
*/
char* idEditField::GetBuffer()
{
2012-11-26 18:58:24 +00:00
return buffer;
}
/*
===============
idEditField::SetBuffer
===============
*/
void idEditField::SetBuffer( const char* buf )
{
2012-11-26 18:58:24 +00:00
Clear();
idStr::Copynz( buffer, buf, sizeof( buffer ) );
SetCursor( strlen( buffer ) );
}
/*
===============
idEditField::Draw
===============
*/
void idEditField::Draw( int x, int y, int width, bool showCursor )
{
2012-11-26 18:58:24 +00:00
int len;
int drawLen;
int prestep;
int cursorChar;
char str[MAX_EDIT_LINE];
int size;
2012-11-26 18:58:24 +00:00
size = SMALLCHAR_WIDTH;
2012-11-26 18:58:24 +00:00
drawLen = widthInChars;
len = strlen( buffer ) + 1;
2012-11-26 18:58:24 +00:00
// guarantee that cursor will be visible
if( len <= drawLen )
{
2012-11-26 18:58:24 +00:00
prestep = 0;
}
else
{
if( scroll + drawLen > len )
{
2012-11-26 18:58:24 +00:00
scroll = len - drawLen;
if( scroll < 0 )
{
2012-11-26 18:58:24 +00:00
scroll = 0;
}
}
prestep = scroll;
2012-11-26 18:58:24 +00:00
// Skip color code
if( idStr::IsColor( buffer + prestep ) )
{
2012-11-26 18:58:24 +00:00
prestep += 2;
}
if( prestep > 0 && idStr::IsColor( buffer + prestep - 1 ) )
{
2012-11-26 18:58:24 +00:00
prestep++;
}
}
if( prestep + drawLen > len )
{
2012-11-26 18:58:24 +00:00
drawLen = len - prestep;
}
2012-11-26 18:58:24 +00:00
// extract <drawLen> characters from the field at <prestep>
if( drawLen >= MAX_EDIT_LINE )
{
2012-11-26 18:58:24 +00:00
common->Error( "drawLen >= MAX_EDIT_LINE" );
}
2012-11-26 18:58:24 +00:00
memcpy( str, buffer + prestep, drawLen );
str[ drawLen ] = 0;
2012-11-26 18:58:24 +00:00
// draw it
renderSystem->DrawSmallStringExt( x, y, str, colorWhite, false );
2012-11-26 18:58:24 +00:00
// draw the cursor
if( !showCursor )
{
2012-11-26 18:58:24 +00:00
return;
}
if( ( int )( idLib::frameNumber >> 4 ) & 1 )
{
2012-11-26 18:58:24 +00:00
return; // off blink
}
if( idKeyInput::GetOverstrikeMode() )
{
2012-11-26 18:58:24 +00:00
cursorChar = 11;
}
else
{
2012-11-26 18:58:24 +00:00
cursorChar = 10;
}
2012-11-26 18:58:24 +00:00
// Move the cursor back to account for color codes
for( int i = 0; i < cursor; i++ )
{
if( idStr::IsColor( &str[i] ) )
{
2012-11-26 18:58:24 +00:00
i++;
prestep += 2;
}
}
2012-11-26 18:58:24 +00:00
renderSystem->DrawSmallChar( x + ( cursor - prestep ) * size, y, cursorChar );
}