From 1138de4fb695a3ca5e4d06d75891fadce5cf7679 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Thu, 2 Feb 2012 21:10:20 +0000 Subject: Fix scroll bar behavior (thanks Alexandre Xavier). Subversion-branch: /trunk/chocolate-doom Subversion-revision: 2484 --- textscreen/txt_gui.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'textscreen') diff --git a/textscreen/txt_gui.c b/textscreen/txt_gui.c index d00e3a65..af1bb0a4 100644 --- a/textscreen/txt_gui.c +++ b/textscreen/txt_gui.c @@ -269,9 +269,9 @@ void TXT_DrawHorizScrollbar(int x, int y, int w, int cursor, int range) cursor_x = x + 1; - if (range > 1) + if (range > 0) { - cursor_x += (cursor * (w - 3)) / (range - 1); + cursor_x += (cursor * (w - 3)) / range; } if (cursor_x > x + w - 2) @@ -320,9 +320,9 @@ void TXT_DrawVertScrollbar(int x, int y, int h, int cursor, int range) cursor_y = y + h - 2; } - if (range > 1) + if (range > 0) { - cursor_y += (cursor * (h - 3)) / (range - 1); + cursor_y += (cursor * (h - 3)) / range; } for (y1=y+1; y1 +#include + +#include "txt_utf8.h" + +// Encode a Unicode character as UTF-8, storing it in the buffer 'p' +// and returning the new, incremented position. + +char *TXT_EncodeUTF8(char *p, unsigned int c) +{ + if (c < 0x80) // 1 character (ASCII): + { + p[0] = c; + return p + 1; + } + else if (c < 0x800) // 2 character: + { + p[0] = 0xc0 | (c >> 6); + p[1] = 0x80 | (c & 0x3f); + return p + 2; + } + else if (c < 0x10000) // 3 chacater: + { + p[0] = 0xe0 | (c >> 12); + p[1] = 0x80 | ((c >> 6) & 0x3f); + p[2] = 0x80 | (c & 0x3f); + return p + 3; + } + else if (c < 0x200000) // 4 character: + { + p[0] = 0xf0 | (c >> 18); + p[1] = 0x80 | ((c >> 12) & 0x3f); + p[2] = 0x80 | ((c >> 6) & 0x3f); + p[3] = 0x80 | (c & 0x3f); + return p + 4; + } + else + { + // Too big! + + return p; + } +} + +// Decode UTF-8 character, incrementing *ptr over the decoded bytes. + +unsigned int TXT_DecodeUTF8(const char **ptr) +{ + const char *p = *ptr; + unsigned int c; + + // UTF-8 decode. + + if ((*p & 0x80) == 0) // 1 character (ASCII): + { + c = *p; + *ptr += 1; + } + else if ((p[0] & 0xe0) == 0xc0 // 2 character: + && (p[1] & 0xc0) == 0x80) + { + c = ((p[0] & 0x1f) << 6) + | (p[1] & 0x3f); + *ptr += 2; + } + else if ((p[0] & 0xf0) == 0xe0 // 3 character: + && (p[1] & 0xc0) == 0x80 + && (p[2] & 0xc0) == 0x80) + { + c = ((p[0] & 0x0f) << 12) + | ((p[1] & 0x3f) << 6) + | (p[2] & 0x3f); + *ptr += 3; + } + else if ((p[0] & 0xf8) == 0xf0 // 4 character: + && (p[1] & 0xc0) == 0x80 + && (p[2] & 0xc0) == 0x80 + && (p[3] & 0xc0) == 0x80) + { + c = ((p[0] & 0x07) << 18) + | ((p[1] & 0x3f) << 12) + | ((p[2] & 0x3f) << 6) + | (p[3] & 0x3f); + *ptr += 4; + } + else + { + // Decode failure. + // Don't bother with 5/6 byte sequences. + + c = 0; + } + + return c; +} + +// Count the number of characters in a UTF-8 string. + +unsigned int TXT_UTF8_Strlen(const char *s) +{ + const char *p; + unsigned int result = 0; + unsigned int c; + + for (p = s; *p != '\0';) + { + c = TXT_DecodeUTF8(&p); + + if (c == 0) + { + break; + } + + ++result; + } + + return result; +} + +// Skip past the first n characters in a UTF-8 string. + +char *TXT_UTF8_SkipChars(const char *s, unsigned int n) +{ + unsigned int i; + const char *p; + + p = s; + + for (i = 0; i < n; ++i) + { + if (TXT_DecodeUTF8(&p) == 0) + { + break; + } + } + + return (char *) p; +} + diff --git a/textscreen/txt_utf8.h b/textscreen/txt_utf8.h new file mode 100644 index 00000000..dc519032 --- /dev/null +++ b/textscreen/txt_utf8.h @@ -0,0 +1,31 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// Copyright(C) 2012 Simon Howard +// +// This program 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 2 +// of the License, or (at your option) any later version. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// + +#ifndef TXT_UTF8_H +#define TXT_UTF8_H + +char *TXT_EncodeUTF8(char *p, unsigned int c); +unsigned int TXT_DecodeUTF8(const char **ptr); +unsigned int TXT_UTF8_Strlen(const char *s); +char *TXT_UTF8_SkipChars(const char *s, unsigned int n); + +#endif /* #ifndef TXT_UTF8_H */ + -- cgit v1.2.3 From d745a6409c84a9ffb85e5d2029132642435c5879 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Fri, 3 Feb 2012 21:09:57 +0000 Subject: Support Unicode input by mapping typed Unicode characters >= 128 up into a higher range to avoid conflicts with Doom's key constants. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 2490 --- textscreen/txt_main.h | 30 +++++++++++++++++++++++++++++- textscreen/txt_sdl.c | 12 +++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) (limited to 'textscreen') diff --git a/textscreen/txt_main.h b/textscreen/txt_main.h index a415ee1b..a6dcc954 100644 --- a/textscreen/txt_main.h +++ b/textscreen/txt_main.h @@ -32,9 +32,21 @@ #include "txt_sdl.h" +// textscreen key values: +// Key values are difficult because we have to support multiple conflicting +// address spaces. +// First, Doom's key constants use 0-127 as ASCII and extra values from +// 128-255 to represent special keys. Second, mouse buttons are represented +// as buttons. Finally, we want to be able to support Unicode. +// +// So we define different ranges: +// 0-255: Doom key constants, including ASCII. +// 256-511: Mouse buttons and other reserved. +// >=512: Unicode values greater than 127 are offset up into this range. + // Special keypress values that correspond to mouse button clicks -#define TXT_MOUSE_BASE 0x10000 +#define TXT_MOUSE_BASE 256 #define TXT_MOUSE_LEFT (TXT_MOUSE_BASE + 0) #define TXT_MOUSE_RIGHT (TXT_MOUSE_BASE + 1) #define TXT_MOUSE_MIDDLE (TXT_MOUSE_BASE + 2) @@ -42,6 +54,22 @@ #define TXT_MOUSE_SCROLLDOWN (TXT_MOUSE_BASE + 4) #define TXT_MAX_MOUSE_BUTTONS 16 +#define TXT_KEY_TO_MOUSE_BUTTON(x) \ + ( (x) >= TXT_MOUSE_BASE \ + && (x) < TXT_MOUSE_BASE + TXT_MAX_MOUSE_BUTTONS ? \ + (x) - TXT_MOUSE_BASE : -1 ) + +// Unicode offset. Unicode values from 128 onwards are offset up into +// this range, so TXT_UNICODE_BASE = Unicode character #128, and so on. + +#define TXT_UNICODE_BASE 512 + +// Convert a key value to a Unicode character: + +#define TXT_KEY_TO_UNICODE(x) \ + ( (x) < 128 ? (x) : \ + (x) >= TXT_UNICODE_BASE ? ((x) - TXT_UNICODE_BASE + 128) : 0 ) + // Screen size #define TXT_SCREEN_W 80 diff --git a/textscreen/txt_sdl.c b/textscreen/txt_sdl.c index a2781ac1..eeb3732f 100644 --- a/textscreen/txt_sdl.c +++ b/textscreen/txt_sdl.c @@ -431,7 +431,17 @@ static int TranslateKey(SDL_keysym *sym) if (key_mapping) { - return sym->unicode; + // Unicode characters beyond the ASCII range need to be + // mapped up into textscreen's Unicode range. + + if (sym->unicode < 128) + { + return sym->unicode; + } + else + { + return sym->unicode - 128 + TXT_UNICODE_BASE; + } } else { -- cgit v1.2.3 From 85b5a7487d5b44b94e6d2fc74c997ec69e352b1a Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Fri, 3 Feb 2012 21:10:36 +0000 Subject: Upgrade the input box and label widgets to use UTF-8 strings. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 2491 --- textscreen/examples/guitest.c | 4 ++++ textscreen/txt_inputbox.c | 44 +++++++++++++++++++++++++++++-------------- textscreen/txt_label.c | 19 ++++++++++++------- 3 files changed, 46 insertions(+), 21 deletions(-) (limited to 'textscreen') diff --git a/textscreen/examples/guitest.c b/textscreen/examples/guitest.c index df79be2d..a5aad93f 100644 --- a/textscreen/examples/guitest.c +++ b/textscreen/examples/guitest.c @@ -179,6 +179,10 @@ void Window2(void) NULL); TXT_AddWidget(unselectable_table, TXT_NewLabel("* Unselectable table *")); + TXT_AddWidget(unselectable_table, TXT_NewLabel( + "This is a UTF-8 string:\n" + "\xc3\x80 bient\xc3\xb4t na\xc3\xaet " + "\xc3\xa9v\xc3\xaaque \xc3\xa0 l'\xc5\x93uvre p\xc3\xa8re.")); TXT_AddWidget(window, TXT_NewSeparator("Input boxes")); table = TXT_NewTable(2); diff --git a/textscreen/txt_inputbox.c b/textscreen/txt_inputbox.c index 2ba08ac6..32704284 100644 --- a/textscreen/txt_inputbox.c +++ b/textscreen/txt_inputbox.c @@ -30,6 +30,7 @@ #include "txt_gui.h" #include "txt_io.h" #include "txt_main.h" +#include "txt_utf8.h" #include "txt_window.h" extern txt_widget_class_t txt_inputbox_class; @@ -140,9 +141,9 @@ static void TXT_InputBoxDrawer(TXT_UNCAST_ARG(inputbox)) SetBufferFromValue(inputbox); } - TXT_DrawString(inputbox->buffer); + TXT_DrawUTF8String(inputbox->buffer); - chars = strlen(inputbox->buffer); + chars = TXT_UTF8_Strlen(inputbox->buffer); if (chars < w && inputbox->editing && focused) { @@ -166,26 +167,36 @@ static void TXT_InputBoxDestructor(TXT_UNCAST_ARG(inputbox)) static void Backspace(txt_inputbox_t *inputbox) { - if (strlen(inputbox->buffer) > 0) + unsigned int len; + char *p; + + len = TXT_UTF8_Strlen(inputbox->buffer); + + if (len > 0) { - inputbox->buffer[strlen(inputbox->buffer) - 1] = '\0'; + p = TXT_UTF8_SkipChars(inputbox->buffer, len - 1); + *p = '\0'; } } static void AddCharacter(txt_inputbox_t *inputbox, int key) { - if (strlen(inputbox->buffer) < inputbox->size) + char *end, *p; + + if (TXT_UTF8_Strlen(inputbox->buffer) < inputbox->size) { // Add character to the buffer - inputbox->buffer[strlen(inputbox->buffer) + 1] = '\0'; - inputbox->buffer[strlen(inputbox->buffer)] = key; + end = inputbox->buffer + strlen(inputbox->buffer); + p = TXT_EncodeUTF8(end, key); + *p = '\0'; } } static int TXT_InputBoxKeyPress(TXT_UNCAST_ARG(inputbox), int key) { TXT_CAST_ARG(txt_inputbox_t, inputbox); + unsigned int c; if (!inputbox->editing) { @@ -208,16 +219,18 @@ static int TXT_InputBoxKeyPress(TXT_UNCAST_ARG(inputbox), int key) inputbox->editing = 0; } - if (isprint(key)) + if (key == KEY_BACKSPACE) { - // Add character to the buffer - - AddCharacter(inputbox, key); + Backspace(inputbox); } - if (key == KEY_BACKSPACE) + c = TXT_KEY_TO_UNICODE(key); + + if (c >= 128 || isprint(c)) { - Backspace(inputbox); + // Add character to the buffer + + AddCharacter(inputbox, c); } return 1; @@ -286,7 +299,10 @@ txt_inputbox_t *TXT_NewInputBox(char **value, int size) TXT_InitWidget(inputbox, &txt_inputbox_class); inputbox->value = value; inputbox->size = size; - inputbox->buffer = malloc(size + 1); + // 'size' is the maximum number of characters that can be entered, + // but for a UTF-8 string, each character can take up to four + // characters. + inputbox->buffer = malloc(size * 4 + 1); inputbox->editing = 0; return inputbox; diff --git a/textscreen/txt_label.c b/textscreen/txt_label.c index a2afc13b..c4238bf3 100644 --- a/textscreen/txt_label.c +++ b/textscreen/txt_label.c @@ -26,6 +26,7 @@ #include "txt_gui.h" #include "txt_io.h" #include "txt_main.h" +#include "txt_utf8.h" #include "txt_window.h" static void TXT_LabelSizeCalc(TXT_UNCAST_ARG(label)) @@ -68,7 +69,7 @@ static void TXT_LabelDrawer(TXT_UNCAST_ARG(label)) align_indent = label->w - strlen(label->lines[y]); break; } - + // Draw this line TXT_GotoXY(origin_x, origin_y + y); @@ -82,8 +83,8 @@ static void TXT_LabelDrawer(TXT_UNCAST_ARG(label)) // The string itself - TXT_DrawString(label->lines[y]); - x += strlen(label->lines[y]); + TXT_DrawUTF8String(label->lines[y]); + x += TXT_UTF8_Strlen(label->lines[y]); // Gap at the end @@ -123,7 +124,7 @@ void TXT_SetLabel(txt_label_t *label, char *value) free(label->label); free(label->lines); - // Set the new value + // Set the new value label->label = strdup(value); @@ -144,7 +145,7 @@ void TXT_SetLabel(txt_label_t *label, char *value) label->lines = malloc(sizeof(char *) * label->h); label->lines[0] = label->label; y = 1; - + for (p = label->label; *p != '\0'; ++p) { if (*p == '\n') @@ -159,8 +160,12 @@ void TXT_SetLabel(txt_label_t *label, char *value) for (y=0; yh; ++y) { - if (strlen(label->lines[y]) > label->w) - label->w = strlen(label->lines[y]); + unsigned int line_len; + + line_len = TXT_UTF8_Strlen(label->lines[y]); + + if (line_len > label->w) + label->w = line_len; } } -- cgit v1.2.3 From 20f405ec5416e5c543425fadc1068e5983cdd136 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Fri, 3 Feb 2012 21:38:06 +0000 Subject: Fix CP437-Unicode mapping of cedilla character. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 2492 --- textscreen/txt_gui.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'textscreen') diff --git a/textscreen/txt_gui.c b/textscreen/txt_gui.c index bc869255..30d43808 100644 --- a/textscreen/txt_gui.c +++ b/textscreen/txt_gui.c @@ -34,7 +34,7 @@ typedef struct txt_cliparea_s txt_cliparea_t; static const uint16_t cp437_unicode[] = { 0x00c7, 0x00fc, 0x00e9, 0x00e2, // 80-8f - 0x00e4, 0x00e0, 0x00e5, 0x00e6, + 0x00e4, 0x00e0, 0x00e5, 0x00e7, 0x00ea, 0x00eb, 0x00e8, 0x00ef, 0x00ee, 0x00ec, 0x00c4, 0x00c5, -- cgit v1.2.3 From c6b8f1163708db85251daac24b2ffa6bea24a72a Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Fri, 3 Feb 2012 22:05:49 +0000 Subject: Fix crash when typing lots of Unicode characters into a number input box. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 2494 --- textscreen/txt_inputbox.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'textscreen') diff --git a/textscreen/txt_inputbox.c b/textscreen/txt_inputbox.c index 32704284..d5ad440a 100644 --- a/textscreen/txt_inputbox.c +++ b/textscreen/txt_inputbox.c @@ -290,13 +290,14 @@ txt_widget_class_t txt_int_inputbox_class = TXT_InputBoxFocused, }; -txt_inputbox_t *TXT_NewInputBox(char **value, int size) +static txt_inputbox_t *NewInputBox(txt_widget_class_t *widget_class, + void *value, int size) { txt_inputbox_t *inputbox; inputbox = malloc(sizeof(txt_inputbox_t)); - TXT_InitWidget(inputbox, &txt_inputbox_class); + TXT_InitWidget(inputbox, widget_class); inputbox->value = value; inputbox->size = size; // 'size' is the maximum number of characters that can be entered, @@ -308,17 +309,13 @@ txt_inputbox_t *TXT_NewInputBox(char **value, int size) return inputbox; } -txt_inputbox_t *TXT_NewIntInputBox(int *value, int size) +txt_inputbox_t *TXT_NewInputBox(char **value, int size) { - txt_inputbox_t *inputbox; - - inputbox = malloc(sizeof(txt_inputbox_t)); + return NewInputBox(&txt_inputbox_class, value, size); +} - TXT_InitWidget(inputbox, &txt_int_inputbox_class); - inputbox->value = value; - inputbox->size = size; - inputbox->buffer = malloc(15); - inputbox->editing = 0; - return inputbox; +txt_inputbox_t *TXT_NewIntInputBox(int *value, int size) +{ + return NewInputBox(&txt_int_inputbox_class, value, size); } -- cgit v1.2.3 From 9ffd1cc4d4bbd2b2799cbd0bf927fc4f30aed63e Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Thu, 1 Mar 2012 20:26:56 +0000 Subject: Rework the way that window background colors are set, and change the background color of inactive windows to black, to give better contrast when viewing many layered windows. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 2507 --- textscreen/txt_checkbox.c | 1 - textscreen/txt_gui.c | 16 ++++++++++++++-- textscreen/txt_gui.h | 6 +++--- textscreen/txt_io.c | 12 ++++++++++++ textscreen/txt_io.h | 8 ++++++++ textscreen/txt_label.c | 14 ++++++++++---- textscreen/txt_label.h | 4 ++-- textscreen/txt_radiobutton.c | 1 - textscreen/txt_separator.c | 1 - textscreen/txt_spinctrl.c | 9 +++++---- textscreen/txt_widget.c | 12 ++++++++++-- textscreen/txt_window.c | 14 +++++++++++++- textscreen/txt_window_action.c | 4 ---- 13 files changed, 77 insertions(+), 25 deletions(-) (limited to 'textscreen') diff --git a/textscreen/txt_checkbox.c b/textscreen/txt_checkbox.c index 8c3271cf..f8fb00bb 100644 --- a/textscreen/txt_checkbox.c +++ b/textscreen/txt_checkbox.c @@ -48,7 +48,6 @@ static void TXT_CheckBoxDrawer(TXT_UNCAST_ARG(checkbox)) w = checkbox->widget.w; - TXT_BGColor(TXT_WINDOW_BACKGROUND, 0); TXT_FGColor(TXT_COLOR_BRIGHT_CYAN); TXT_DrawString("("); diff --git a/textscreen/txt_gui.c b/textscreen/txt_gui.c index 30d43808..09ba87ef 100644 --- a/textscreen/txt_gui.c +++ b/textscreen/txt_gui.c @@ -173,11 +173,12 @@ void TXT_DrawShadow(int x, int y, int w, int h) void TXT_DrawWindowFrame(const char *title, int x, int y, int w, int h) { + txt_saved_colors_t colors; int x1, y1; int bx, by; + TXT_SaveColors(&colors); TXT_FGColor(TXT_COLOR_BRIGHT_CYAN); - TXT_BGColor(TXT_WINDOW_BACKGROUND, 0); for (y1=y; y1bgcolor = bgcolor; + save->fgcolor = fgcolor; +} + +void TXT_RestoreColors(txt_saved_colors_t *save) +{ + bgcolor = save->bgcolor; + fgcolor = save->fgcolor; +} + void TXT_ClearScreen(void) { unsigned char *screen; diff --git a/textscreen/txt_io.h b/textscreen/txt_io.h index dc25aa93..004fa562 100644 --- a/textscreen/txt_io.h +++ b/textscreen/txt_io.h @@ -29,12 +29,20 @@ #include "txt_main.h" +typedef struct +{ + int bgcolor; + int fgcolor; +} txt_saved_colors_t; + void TXT_PutChar(int c); void TXT_Puts(const char *s); void TXT_GotoXY(int x, int y); void TXT_GetXY(int *x, int *y); void TXT_FGColor(txt_color_t color); void TXT_BGColor(int color, int blinking); +void TXT_SaveColors(txt_saved_colors_t *save); +void TXT_RestoreColors(txt_saved_colors_t *save); void TXT_ClearScreen(void); #endif /* #ifndef TXT_IO_H */ diff --git a/textscreen/txt_label.c b/textscreen/txt_label.c index c4238bf3..39ea0e16 100644 --- a/textscreen/txt_label.c +++ b/textscreen/txt_label.c @@ -47,8 +47,14 @@ static void TXT_LabelDrawer(TXT_UNCAST_ARG(label)) w = label->widget.w; - TXT_BGColor(label->bgcolor, 0); - TXT_FGColor(label->fgcolor); + if (label->bgcolor >= 0) + { + TXT_BGColor(label->bgcolor, 0); + } + if (label->fgcolor >= 0) + { + TXT_FGColor(label->fgcolor); + } TXT_GetXY(&origin_x, &origin_y); @@ -181,8 +187,8 @@ txt_label_t *TXT_NewLabel(char *text) // Default colors - label->bgcolor = TXT_WINDOW_BACKGROUND; - label->fgcolor = TXT_COLOR_BRIGHT_WHITE; + label->bgcolor = -1; + label->fgcolor = -1; TXT_SetLabel(label, text); diff --git a/textscreen/txt_label.h b/textscreen/txt_label.h index 16395c93..c0a20bf2 100644 --- a/textscreen/txt_label.h +++ b/textscreen/txt_label.h @@ -45,8 +45,8 @@ struct txt_label_s char *label; char **lines; unsigned int w, h; - txt_color_t fgcolor; - txt_color_t bgcolor; + int fgcolor; + int bgcolor; }; /** diff --git a/textscreen/txt_radiobutton.c b/textscreen/txt_radiobutton.c index 10f94ad3..3c3bb5dd 100644 --- a/textscreen/txt_radiobutton.c +++ b/textscreen/txt_radiobutton.c @@ -48,7 +48,6 @@ static void TXT_RadioButtonDrawer(TXT_UNCAST_ARG(radiobutton)) w = radiobutton->widget.w; - TXT_BGColor(TXT_WINDOW_BACKGROUND, 0); TXT_FGColor(TXT_COLOR_BRIGHT_CYAN); TXT_DrawString("("); diff --git a/textscreen/txt_separator.c b/textscreen/txt_separator.c index ce6e29ec..25d6f7bc 100644 --- a/textscreen/txt_separator.c +++ b/textscreen/txt_separator.c @@ -65,7 +65,6 @@ static void TXT_SeparatorDrawer(TXT_UNCAST_ARG(separator)) { TXT_GotoXY(x, y); - TXT_BGColor(TXT_WINDOW_BACKGROUND, 0); TXT_FGColor(TXT_COLOR_BRIGHT_GREEN); TXT_DrawString(" "); TXT_DrawString(separator->label); diff --git a/textscreen/txt_spinctrl.c b/textscreen/txt_spinctrl.c index a4d20343..0b77805f 100644 --- a/textscreen/txt_spinctrl.c +++ b/textscreen/txt_spinctrl.c @@ -147,15 +147,17 @@ static void TXT_SpinControlDrawer(TXT_UNCAST_ARG(spincontrol)) TXT_CAST_ARG(txt_spincontrol_t, spincontrol); unsigned int i; unsigned int padding; + txt_saved_colors_t colors; int focused; focused = spincontrol->widget.focused; TXT_FGColor(TXT_COLOR_BRIGHT_CYAN); - TXT_BGColor(TXT_WINDOW_BACKGROUND, 0); TXT_DrawString("\x1b "); - + + TXT_SaveColors(&colors); + TXT_FGColor(TXT_COLOR_BRIGHT_WHITE); // Choose background color @@ -193,8 +195,7 @@ static void TXT_SpinControlDrawer(TXT_UNCAST_ARG(spincontrol)) ++i; } - TXT_FGColor(TXT_COLOR_BRIGHT_CYAN); - TXT_BGColor(TXT_WINDOW_BACKGROUND, 0); + TXT_RestoreColors(&colors); TXT_DrawString(" \x1a"); } diff --git a/textscreen/txt_widget.c b/textscreen/txt_widget.c index 7d31ad68..e0303531 100644 --- a/textscreen/txt_widget.c +++ b/textscreen/txt_widget.c @@ -162,14 +162,22 @@ void TXT_CalcWidgetSize(TXT_UNCAST_ARG(widget)) void TXT_DrawWidget(TXT_UNCAST_ARG(widget)) { TXT_CAST_ARG(txt_widget_t, widget); + txt_saved_colors_t colors; + + // The drawing function might change the fg/bg colors, + // so make sure we restore them after it's done. + + TXT_SaveColors(&colors); // For convenience... TXT_GotoXY(widget->x, widget->y); // Call drawer method - + widget->widget_class->drawer(widget); + + TXT_RestoreColors(&colors); } void TXT_DestroyWidget(TXT_UNCAST_ARG(widget)) @@ -319,7 +327,7 @@ void TXT_SetWidgetBG(TXT_UNCAST_ARG(widget)) } else { - TXT_BGColor(TXT_WINDOW_BACKGROUND, 0); + // Use normal window background. } } diff --git a/textscreen/txt_window.c b/textscreen/txt_window.c index d33dd75b..b08ac658 100644 --- a/textscreen/txt_window.c +++ b/textscreen/txt_window.c @@ -26,6 +26,7 @@ #include "txt_desktop.h" #include "txt_gui.h" +#include "txt_io.h" #include "txt_main.h" #include "txt_separator.h" #include "txt_window.h" @@ -319,7 +320,18 @@ void TXT_DrawWindow(txt_window_t *window) txt_widget_t *widgets; TXT_LayoutWindow(window); - + + if (window->table.widget.focused) + { + TXT_BGColor(TXT_ACTIVE_WINDOW_BACKGROUND, 0); + } + else + { + TXT_BGColor(TXT_INACTIVE_WINDOW_BACKGROUND, 0); + } + + TXT_FGColor(TXT_COLOR_BRIGHT_WHITE); + // Draw the window TXT_DrawWindowFrame(window->title, diff --git a/textscreen/txt_window_action.c b/textscreen/txt_window_action.c index cf5ac4a7..f195f06f 100644 --- a/textscreen/txt_window_action.c +++ b/textscreen/txt_window_action.c @@ -55,10 +55,6 @@ static void TXT_WindowActionDrawer(TXT_UNCAST_ARG(action)) { TXT_BGColor(TXT_COLOR_BLACK, 0); } - else - { - TXT_BGColor(TXT_WINDOW_BACKGROUND, 0); - } TXT_DrawString(" "); TXT_FGColor(TXT_COLOR_BRIGHT_GREEN); -- cgit v1.2.3 From 003c82ce37610a66bc61b9380fda6fd74223473b Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 4 Mar 2012 12:06:29 +0000 Subject: Remove some calls to TXT_FGColor by using the new TXT_SaveColors system instead. Remove the unused "embedded color code" system from TXT_Puts. Subversion-branch: /trunk/chocolate-doom Subversion-revision: 2508 --- textscreen/txt_button.c | 2 - textscreen/txt_checkbox.c | 7 ++-- textscreen/txt_dropdown.c | 1 - textscreen/txt_inputbox.c | 2 - textscreen/txt_io.c | 94 +------------------------------------------- textscreen/txt_radiobutton.c | 6 ++- textscreen/txt_spinctrl.c | 10 ++--- 7 files changed, 15 insertions(+), 107 deletions(-) (limited to 'textscreen') diff --git a/textscreen/txt_button.c b/textscreen/txt_button.c index 989bd4fc..c8e3fc77 100644 --- a/textscreen/txt_button.c +++ b/textscreen/txt_button.c @@ -46,8 +46,6 @@ static void TXT_ButtonDrawer(TXT_UNCAST_ARG(button)) w = button->widget.w; - TXT_FGColor(TXT_COLOR_BRIGHT_WHITE); - TXT_SetWidgetBG(button); TXT_DrawString(button->label); diff --git a/textscreen/txt_checkbox.c b/textscreen/txt_checkbox.c index f8fb00bb..3bd0a054 100644 --- a/textscreen/txt_checkbox.c +++ b/textscreen/txt_checkbox.c @@ -43,11 +43,13 @@ static void TXT_CheckBoxSizeCalc(TXT_UNCAST_ARG(checkbox)) static void TXT_CheckBoxDrawer(TXT_UNCAST_ARG(checkbox)) { TXT_CAST_ARG(txt_checkbox_t, checkbox); + txt_saved_colors_t colors; int i; int w; w = checkbox->widget.w; + TXT_SaveColors(&colors); TXT_FGColor(TXT_COLOR_BRIGHT_CYAN); TXT_DrawString("("); @@ -66,11 +68,10 @@ static void TXT_CheckBoxDrawer(TXT_UNCAST_ARG(checkbox)) TXT_DrawString(") "); + TXT_RestoreColors(&colors); TXT_SetWidgetBG(checkbox); - TXT_FGColor(TXT_COLOR_BRIGHT_WHITE); - TXT_DrawString(checkbox->label); - + for (i=strlen(checkbox->label); i < w-5; ++i) { TXT_DrawString(" "); diff --git a/textscreen/txt_dropdown.c b/textscreen/txt_dropdown.c index 0856fb4e..d88716b5 100644 --- a/textscreen/txt_dropdown.c +++ b/textscreen/txt_dropdown.c @@ -215,7 +215,6 @@ static void TXT_DropdownListDrawer(TXT_UNCAST_ARG(list)) // Set bg/fg text colors. TXT_SetWidgetBG(list); - TXT_FGColor(TXT_COLOR_BRIGHT_WHITE); // Select a string to draw from the list, if the current value is // in range. Otherwise fall back to a default. diff --git a/textscreen/txt_inputbox.c b/textscreen/txt_inputbox.c index d5ad440a..0f2986cc 100644 --- a/textscreen/txt_inputbox.c +++ b/textscreen/txt_inputbox.c @@ -132,8 +132,6 @@ static void TXT_InputBoxDrawer(TXT_UNCAST_ARG(inputbox)) TXT_SetWidgetBG(inputbox); } - TXT_FGColor(TXT_COLOR_BRIGHT_WHITE); - if (!inputbox->editing) { // If not editing, use the current value from inputbox->value. diff --git a/textscreen/txt_io.c b/textscreen/txt_io.c index 08bb5ac9..f7c9a7a4 100644 --- a/textscreen/txt_io.c +++ b/textscreen/txt_io.c @@ -30,48 +30,10 @@ #include "txt_io.h" #include "txt_main.h" -static struct -{ - txt_color_t color; - const char *name; -} colors[] = { - {TXT_COLOR_BLACK, "black"}, - {TXT_COLOR_BLUE, "blue"}, - {TXT_COLOR_GREEN, "green"}, - {TXT_COLOR_CYAN, "cyan"}, - {TXT_COLOR_RED, "red"}, - {TXT_COLOR_MAGENTA, "magenta"}, - {TXT_COLOR_BROWN, "brown"}, - {TXT_COLOR_GREY, "grey"}, - {TXT_COLOR_DARK_GREY, "darkgrey"}, - {TXT_COLOR_BRIGHT_BLUE, "brightblue"}, - {TXT_COLOR_BRIGHT_GREEN, "brightgreen"}, - {TXT_COLOR_BRIGHT_CYAN, "brightcyan"}, - {TXT_COLOR_BRIGHT_RED, "brightred"}, - {TXT_COLOR_BRIGHT_MAGENTA, "brightmagenta"}, - {TXT_COLOR_YELLOW, "yellow"}, - {TXT_COLOR_BRIGHT_WHITE, "brightwhite"}, -}; - static int cur_x = 0, cur_y = 0; static txt_color_t fgcolor = TXT_COLOR_GREY; static txt_color_t bgcolor = TXT_COLOR_BLACK; -static int GetColorForName(char *s) -{ - size_t i; - - for (i=0; i'); - - if (ending == NULL) - { - return; - } - - strncpy(colorname_buf, p, 19); - colorname_buf[ending-p] = '\0'; - - if (!strcmp(colorname_buf, "/")) - { - // End of color block - - col = previous_color; - } - else - { - col = GetColorForName(colorname_buf); - - if (col < 0) - { - return; - } - - // Save the color for the ending marker - - previous_color = fgcolor; - } - - TXT_FGColor(col); - - p = ending; - } - } - else - { - PutChar(screen, *p); - } + PutChar(screen, *p); } PutChar(screen, '\n'); diff --git a/textscreen/txt_radiobutton.c b/textscreen/txt_radiobutton.c index 3c3bb5dd..fa722c13 100644 --- a/textscreen/txt_radiobutton.c +++ b/textscreen/txt_radiobutton.c @@ -43,11 +43,13 @@ static void TXT_RadioButtonSizeCalc(TXT_UNCAST_ARG(radiobutton)) static void TXT_RadioButtonDrawer(TXT_UNCAST_ARG(radiobutton)) { TXT_CAST_ARG(txt_radiobutton_t, radiobutton); + txt_saved_colors_t colors; int i; int w; w = radiobutton->widget.w; + TXT_SaveColors(&colors); TXT_FGColor(TXT_COLOR_BRIGHT_CYAN); TXT_DrawString("("); @@ -66,11 +68,11 @@ static void TXT_RadioButtonDrawer(TXT_UNCAST_ARG(radiobutton)) TXT_DrawString(") "); + TXT_RestoreColors(&colors); TXT_SetWidgetBG(radiobutton); - TXT_FGColor(TXT_COLOR_BRIGHT_WHITE); TXT_DrawString(radiobutton->label); - + for (i=strlen(radiobutton->label); i < w-5; ++i) { TXT_DrawString(" "); diff --git a/textscreen/txt_spinctrl.c b/textscreen/txt_spinctrl.c index 0b77805f..1015ece5 100644 --- a/textscreen/txt_spinctrl.c +++ b/textscreen/txt_spinctrl.c @@ -152,13 +152,12 @@ static void TXT_SpinControlDrawer(TXT_UNCAST_ARG(spincontrol)) focused = spincontrol->widget.focused; - TXT_FGColor(TXT_COLOR_BRIGHT_CYAN); + TXT_SaveColors(&colors); + TXT_FGColor(TXT_COLOR_BRIGHT_CYAN); TXT_DrawString("\x1b "); - TXT_SaveColors(&colors); - - TXT_FGColor(TXT_COLOR_BRIGHT_WHITE); + TXT_RestoreColors(&colors); // Choose background color @@ -175,7 +174,7 @@ static void TXT_SpinControlDrawer(TXT_UNCAST_ARG(spincontrol)) { SetBuffer(spincontrol); } - + i = 0; padding = spincontrol->widget.w - strlen(spincontrol->buffer) - 4; @@ -196,6 +195,7 @@ static void TXT_SpinControlDrawer(TXT_UNCAST_ARG(spincontrol)) } TXT_RestoreColors(&colors); + TXT_FGColor(TXT_COLOR_BRIGHT_CYAN); TXT_DrawString(" \x1a"); } -- cgit v1.2.3