/**
 * @brief edit the current cvar with a char
 */
static void UI_TextEntryNodeEdit (uiNode_t* node, unsigned int unicode)
{
	char buffer[MAX_CVAR_EDITING_LENGTH];

	/* copy the cvar */
	Q_strncpyz(buffer, editedCvar->string, sizeof(buffer));

	/* compute result */
	if (unicode == K_BACKSPACE) {
		if (EXTRADATA(node).cursorPosition > 0){
			UTF8_delete_char_at(buffer, EXTRADATA(node).cursorPosition - 1);
			EXTRADATA(node).cursorPosition--;
		}
	} else if (unicode == K_DEL) {
		if (EXTRADATA(node).cursorPosition < UTF8_strlen(editedCvar->string)){
			UTF8_delete_char_at(buffer, EXTRADATA(node).cursorPosition);
		}
	} else {
		int length = strlen(buffer);
		int charLength = UTF8_encoded_len(unicode);

		/* is buffer full? */
		if (length + charLength >= sizeof(buffer))
			return;

		int insertedLength = UTF8_insert_char_at(buffer, sizeof(buffer), EXTRADATA(node).cursorPosition, unicode);
		if (insertedLength > 0)
			EXTRADATA(node).cursorPosition++;
	}

	/* update the cvar */
	Cvar_ForceSet(editedCvar->name, buffer);
}
Exemple #2
0
/**
 * @brief Insert a (possibly multibyte) UTF-8 character into a string.
 * @param[in] s Start of the string
 * @param[in] n Buffer size of the string
 * @param[in] pos UTF-8 char offset from the start (not the byte offset)
 * @param[in] c Unicode code as 32-bit integer
 * @return Number of bytes added
 */
int UTF8_insert_char_at (char* s, int n, int pos, int c)
{
	/* Convert the UTF-8 char offset to byte offset */
	pos = UTF8_char_offset_to_byte_offset(s, pos);

	const int utf8len = UTF8_encoded_len(c);
	const int tail = strlen(&s[pos]) + 1;

	if (utf8len == 0)
		return 0;

	if (pos + tail + utf8len > n)
		return 0;

	/* Insertion: move up rest of string. Also moves string terminator. */
	memmove(&s[pos + utf8len], &s[pos], tail);

	if (c <= 0x7f) {
		s[pos] = c;
	} else if (c <= 0x7ff) { 				/* c has 11 bits */
		s[pos] = 0xc0 | (c >> 6);	  			/* high 5 bits */
		s[pos + 1] = 0x80 | (c & 0x3f); 		/* low 6 bits */
	} else if (c <= 0xffff) { 				/* c has 16 bits */
Exemple #3
0
/**
 * @brief edit the current cvar with a char
 */
static void UI_TextEntryNodeEdit (uiNode_t *node, unsigned int key)
{
    char buffer[MAX_CVAR_EDITING_LENGTH];
    int length;

    /* copy the cvar */
    Q_strncpyz(buffer, editedCvar->string, sizeof(buffer));
    length = strlen(buffer);

    /* compute result */
    if (key == K_BACKSPACE) {
        length = UTF8_delete_char(buffer, length - 1);
    } else {
        int charLength = UTF8_encoded_len(key);
        /* is buffer full? */
        if (length + charLength >= sizeof(buffer))
            return;

        length += UTF8_insert_char(buffer, sizeof(buffer), length, key);
    }

    /* update the cvar */
    Cvar_ForceSet(editedCvar->name, buffer);
}