textscreen: Change most strings to UTF-8.

If we are to accept arbitrary labels, window names etc., then it makes
sense for these to be in UTF-8 format rather than strings in the code
page extended ASCII format. This should make the API more interoperable
with other data sources.
This commit is contained in:
Simon Howard 2017-01-21 16:21:27 +00:00
parent 8f5a4af761
commit 06dd832185
21 changed files with 69 additions and 55 deletions

View file

@ -21,13 +21,14 @@
#include "txt_gui.h" #include "txt_gui.h"
#include "txt_io.h" #include "txt_io.h"
#include "txt_main.h" #include "txt_main.h"
#include "txt_utf8.h"
#include "txt_window.h" #include "txt_window.h"
static void TXT_ButtonSizeCalc(TXT_UNCAST_ARG(button)) static void TXT_ButtonSizeCalc(TXT_UNCAST_ARG(button))
{ {
TXT_CAST_ARG(txt_button_t, button); TXT_CAST_ARG(txt_button_t, button);
button->widget.w = strlen(button->label); button->widget.w = TXT_UTF8_Strlen(button->label);
button->widget.h = 1; button->widget.h = 1;
} }
@ -41,9 +42,9 @@ static void TXT_ButtonDrawer(TXT_UNCAST_ARG(button))
TXT_SetWidgetBG(button); TXT_SetWidgetBG(button);
TXT_DrawString(button->label); TXT_DrawUTF8String(button->label);
for (i=strlen(button->label); i < w; ++i) for (i = TXT_UTF8_Strlen(button->label); i < w; ++i)
{ {
TXT_DrawString(" "); TXT_DrawString(" ");
} }

View file

@ -41,7 +41,7 @@ struct txt_button_s
/** /**
* Create a new button widget. * Create a new button widget.
* *
* @param label The label to use on the new button. * @param label The label to use on the new button (UTF-8 format).
* @return Pointer to the new button widget. * @return Pointer to the new button widget.
*/ */
@ -51,7 +51,7 @@ txt_button_t *TXT_NewButton(char *label);
* Create a new button widget, binding the "pressed" signal to a * Create a new button widget, binding the "pressed" signal to a
* specified callback function. * specified callback function.
* *
* @param label The label to use on the new button. * @param label The label to use on the new button (UTF-8 format).
* @param func The callback function to invoke. * @param func The callback function to invoke.
* @param user_data User-specified pointer to pass to the callback. * @param user_data User-specified pointer to pass to the callback.
* @return Pointer to the new button widget. * @return Pointer to the new button widget.
@ -64,7 +64,7 @@ txt_button_t *TXT_NewButton2(char *label, TxtWidgetSignalFunc func,
* Change the label used on a button. * Change the label used on a button.
* *
* @param button The button. * @param button The button.
* @param label The new label. * @param label The new label (UTF-8 format).
*/ */
void TXT_SetButtonLabel(txt_button_t *button, char *label); void TXT_SetButtonLabel(txt_button_t *button, char *label);

View file

@ -21,6 +21,7 @@
#include "txt_gui.h" #include "txt_gui.h"
#include "txt_io.h" #include "txt_io.h"
#include "txt_main.h" #include "txt_main.h"
#include "txt_utf8.h"
#include "txt_window.h" #include "txt_window.h"
static void TXT_CheckBoxSizeCalc(TXT_UNCAST_ARG(checkbox)) static void TXT_CheckBoxSizeCalc(TXT_UNCAST_ARG(checkbox))
@ -29,7 +30,7 @@ static void TXT_CheckBoxSizeCalc(TXT_UNCAST_ARG(checkbox))
// Minimum width is the string length + right-side space for padding // Minimum width is the string length + right-side space for padding
checkbox->widget.w = strlen(checkbox->label) + 5; checkbox->widget.w = TXT_UTF8_Strlen(checkbox->label) + 5;
checkbox->widget.h = 1; checkbox->widget.h = 1;
} }
@ -63,9 +64,9 @@ static void TXT_CheckBoxDrawer(TXT_UNCAST_ARG(checkbox))
TXT_RestoreColors(&colors); TXT_RestoreColors(&colors);
TXT_SetWidgetBG(checkbox); TXT_SetWidgetBG(checkbox);
TXT_DrawString(checkbox->label); TXT_DrawUTF8String(checkbox->label);
for (i=strlen(checkbox->label); i < w-4; ++i) for (i = TXT_UTF8_Strlen(checkbox->label); i < w-4; ++i)
{ {
TXT_DrawString(" "); TXT_DrawString(" ");
} }

View file

@ -49,7 +49,7 @@ struct txt_checkbox_s
/** /**
* Create a new checkbox. * Create a new checkbox.
* *
* @param label The label for the new checkbox. * @param label The label for the new checkbox (UTF-8 format).
* @param variable Pointer to the variable containing this checkbox's * @param variable Pointer to the variable containing this checkbox's
* value. * value.
* @return Pointer to the new checkbox. * @return Pointer to the new checkbox.
@ -63,7 +63,7 @@ txt_checkbox_t *TXT_NewCheckBox(char *label, int *variable);
* An inverted checkbox displays the opposite of a normal checkbox; * An inverted checkbox displays the opposite of a normal checkbox;
* where it would be checked, it appears unchecked, and vice-versa. * where it would be checked, it appears unchecked, and vice-versa.
* *
* @param label The label for the new checkbox. * @param label The label for the new checkbox (UTF-8 format).
* @param variable Pointer to the variable containing this checkbox's * @param variable Pointer to the variable containing this checkbox's
* value. * value.
* @return Pointer to the new checkbox. * @return Pointer to the new checkbox.

View file

@ -36,7 +36,7 @@ int TXT_WindowKeyPress(txt_window_t *window, int c);
/** /**
* Set the title displayed at the top of the screen. * Set the title displayed at the top of the screen.
* *
* @param title The title to display. * @param title The title to display (UTF-8 format).
*/ */
void TXT_SetDesktopTitle(char *title); void TXT_SetDesktopTitle(char *title);

View file

@ -22,6 +22,7 @@
#include "txt_gui.h" #include "txt_gui.h"
#include "txt_io.h" #include "txt_io.h"
#include "txt_main.h" #include "txt_main.h"
#include "txt_utf8.h"
#include "txt_window.h" #include "txt_window.h"
typedef struct typedef struct
@ -196,7 +197,7 @@ static int DropdownListWidth(txt_dropdown_list_t *list)
for (i=0; i<list->num_values; ++i) for (i=0; i<list->num_values; ++i)
{ {
int w = strlen(list->values[i]); int w = TXT_UTF8_Strlen(list->values[i]);
if (w > result) if (w > result)
{ {
result = w; result = w;
@ -238,9 +239,9 @@ static void TXT_DropdownListDrawer(TXT_UNCAST_ARG(list))
// Draw the string and fill to the end with spaces // Draw the string and fill to the end with spaces
TXT_DrawString(str); TXT_DrawUTF8String(str);
for (i=strlen(str); i<list->widget.w; ++i) for (i = TXT_UTF8_Strlen(str); i < list->widget.w; ++i)
{ {
TXT_DrawString(" "); TXT_DrawString(" ");
} }

View file

@ -57,7 +57,7 @@ struct txt_dropdown_list_s
* @param variable Pointer to the variable containing the * @param variable Pointer to the variable containing the
* list's value. * list's value.
* @param values Pointer to an array of strings containing * @param values Pointer to an array of strings containing
* the labels to use for the list. * the labels to use for the list (UTF-8 format).
* @param num_values The number of variables in the list. * @param num_values The number of variables in the list.
*/ */

View file

@ -56,7 +56,7 @@ char *TXT_SelectFile(char *prompt, char **extensions);
* Create a new txt_fileselect_t widget. * Create a new txt_fileselect_t widget.
* *
* @param variable Pointer to a char * variable in which the selected * @param variable Pointer to a char * variable in which the selected
* file should be stored. * file should be stored (UTF-8 format).
* @param size Width of the file selector widget in characters. * @param size Width of the file selector widget in characters.
* @param prompt Pointer to a string containing a prompt to display * @param prompt Pointer to a string containing a prompt to display
* in the file selection window. * in the file selection window.

View file

@ -92,7 +92,7 @@ void TXT_DrawDesktopBackground(const char *title)
TXT_BGColor(TXT_COLOR_GREY, 0); TXT_BGColor(TXT_COLOR_GREY, 0);
TXT_DrawString(" "); TXT_DrawString(" ");
TXT_DrawString(title); TXT_DrawUTF8String(title);
} }
void TXT_DrawShadow(int x, int y, int w, int h) void TXT_DrawShadow(int x, int y, int w, int h)
@ -167,8 +167,8 @@ void TXT_DrawWindowFrame(const char *title, int x, int y, int w, int h)
TXT_DrawString(" "); TXT_DrawString(" ");
} }
TXT_GotoXY(x + (w - strlen(title)) / 2, y + 1); TXT_GotoXY(x + (w - TXT_UTF8_Strlen(title)) / 2, y + 1);
TXT_DrawString(title); TXT_DrawUTF8String(title);
} }
// Draw the window's shadow. // Draw the window's shadow.

View file

@ -52,7 +52,8 @@ struct txt_inputbox_s
* input box. The value should be allocated * input box. The value should be allocated
* dynamically; when the string is changed it * dynamically; when the string is changed it
* will be freed and the variable set to point * will be freed and the variable set to point
* to the new string value. * to the new string value. String will be in
* UTF-8 format.
* @param size Width of the input box, in characters. * @param size Width of the input box, in characters.
* @return Pointer to the new input box widget. * @return Pointer to the new input box widget.
*/ */

View file

@ -36,7 +36,7 @@ static void TXT_LabelDrawer(TXT_UNCAST_ARG(label))
unsigned int x, y; unsigned int x, y;
int origin_x, origin_y; int origin_x, origin_y;
unsigned int align_indent = 0; unsigned int align_indent = 0;
unsigned int w; unsigned int w, sw;
w = label->widget.w; w = label->widget.w;
@ -55,17 +55,17 @@ static void TXT_LabelDrawer(TXT_UNCAST_ARG(label))
{ {
// Calculate the amount to indent this line due to the align // Calculate the amount to indent this line due to the align
// setting // setting
sw = TXT_UTF8_Strlen(label->lines[y]);
switch (label->widget.align) switch (label->widget.align)
{ {
case TXT_HORIZ_LEFT: case TXT_HORIZ_LEFT:
align_indent = 0; align_indent = 0;
break; break;
case TXT_HORIZ_CENTER: case TXT_HORIZ_CENTER:
align_indent = (label->w - strlen(label->lines[y])) / 2; align_indent = (label->w - sw) / 2;
break; break;
case TXT_HORIZ_RIGHT: case TXT_HORIZ_RIGHT:
align_indent = label->w - strlen(label->lines[y]); align_indent = label->w - sw;
break; break;
} }
@ -83,7 +83,7 @@ static void TXT_LabelDrawer(TXT_UNCAST_ARG(label))
// The string itself // The string itself
TXT_DrawUTF8String(label->lines[y]); TXT_DrawUTF8String(label->lines[y]);
x += TXT_UTF8_Strlen(label->lines[y]); x += sw;
// Gap at the end // Gap at the end

View file

@ -45,7 +45,7 @@ struct txt_label_s
/** /**
* Create a new label widget. * Create a new label widget.
* *
* @param label String to display in the widget. * @param label String to display in the widget (UTF-8 format).
* @return Pointer to the new label widget. * @return Pointer to the new label widget.
*/ */
@ -55,7 +55,7 @@ txt_label_t *TXT_NewLabel(char *label);
* Set the string displayed in a label widget. * Set the string displayed in a label widget.
* *
* @param label The widget. * @param label The widget.
* @param value The string to display. * @param value The string to display (UTF-8 format).
*/ */
void TXT_SetLabel(txt_label_t *label, char *value); void TXT_SetLabel(txt_label_t *label, char *value);

View file

@ -154,7 +154,7 @@ int TXT_GetModifierState(txt_modifier_t mod);
// keyboard (like that returned by TXT_INPUT_RAW), and the resulting string // keyboard (like that returned by TXT_INPUT_RAW), and the resulting string
// takes keyboard layout into consideration. For example, // takes keyboard layout into consideration. For example,
// TXT_GetKeyDescription('q') on a French keyboard returns "A". // TXT_GetKeyDescription('q') on a French keyboard returns "A".
// The contents of the filled buffer will be in UTF8 format, but will never // The contents of the filled buffer will be in UTF-8 format, but will never
// contain characters which can't be shown on the screen. // contain characters which can't be shown on the screen.
void TXT_GetKeyDescription(int key, char *buf, size_t buf_len); void TXT_GetKeyDescription(int key, char *buf, size_t buf_len);

View file

@ -21,6 +21,7 @@
#include "txt_gui.h" #include "txt_gui.h"
#include "txt_io.h" #include "txt_io.h"
#include "txt_main.h" #include "txt_main.h"
#include "txt_utf8.h"
#include "txt_window.h" #include "txt_window.h"
static void TXT_RadioButtonSizeCalc(TXT_UNCAST_ARG(radiobutton)) static void TXT_RadioButtonSizeCalc(TXT_UNCAST_ARG(radiobutton))
@ -29,7 +30,7 @@ static void TXT_RadioButtonSizeCalc(TXT_UNCAST_ARG(radiobutton))
// Minimum width is the string length + right-side spaces for padding // Minimum width is the string length + right-side spaces for padding
radiobutton->widget.w = strlen(radiobutton->label) + 5; radiobutton->widget.w = TXT_UTF8_Strlen(radiobutton->label) + 5;
radiobutton->widget.h = 1; radiobutton->widget.h = 1;
} }
@ -64,9 +65,9 @@ static void TXT_RadioButtonDrawer(TXT_UNCAST_ARG(radiobutton))
TXT_RestoreColors(&colors); TXT_RestoreColors(&colors);
TXT_SetWidgetBG(radiobutton); TXT_SetWidgetBG(radiobutton);
TXT_DrawString(radiobutton->label); TXT_DrawUTF8String(radiobutton->label);
for (i=strlen(radiobutton->label); i < w-5; ++i) for (i=TXT_UTF8_Strlen(radiobutton->label); i < w-5; ++i)
{ {
TXT_DrawString(" "); TXT_DrawString(" ");
} }

View file

@ -54,7 +54,8 @@ struct txt_radiobutton_s
/** /**
* Create a new radio button widget. * Create a new radio button widget.
* *
* @param label The label to display next to the radio button. * @param label The label to display next to the radio button
* (UTF-8 format).
* @param variable Pointer to the variable tracking whether this * @param variable Pointer to the variable tracking whether this
* radio button is selected. * radio button is selected.
* @param value If the variable is equal to this value, the * @param value If the variable is equal to this value, the
@ -68,7 +69,7 @@ txt_radiobutton_t *TXT_NewRadioButton(char *label, int *variable, int value);
* Set the label on a radio button. * Set the label on a radio button.
* *
* @param radiobutton The radio button. * @param radiobutton The radio button.
* @param value The new label. * @param value The new label (UTF-8 format).
*/ */
void TXT_SetRadioButtonLabel(txt_radiobutton_t *radiobutton, char *value); void TXT_SetRadioButtonLabel(txt_radiobutton_t *radiobutton, char *value);

View file

@ -19,6 +19,7 @@
#include "txt_gui.h" #include "txt_gui.h"
#include "txt_io.h" #include "txt_io.h"
#include "txt_main.h" #include "txt_main.h"
#include "txt_utf8.h"
#include "txt_window.h" #include "txt_window.h"
static void TXT_SeparatorSizeCalc(TXT_UNCAST_ARG(separator)) static void TXT_SeparatorSizeCalc(TXT_UNCAST_ARG(separator))
@ -29,7 +30,7 @@ static void TXT_SeparatorSizeCalc(TXT_UNCAST_ARG(separator))
{ {
// Minimum width is the string length + two spaces for padding // Minimum width is the string length + two spaces for padding
separator->widget.w = strlen(separator->label) + 2; separator->widget.w = TXT_UTF8_Strlen(separator->label) + 2;
} }
else else
{ {
@ -60,7 +61,7 @@ static void TXT_SeparatorDrawer(TXT_UNCAST_ARG(separator))
TXT_FGColor(TXT_COLOR_BRIGHT_GREEN); TXT_FGColor(TXT_COLOR_BRIGHT_GREEN);
TXT_DrawString(" "); TXT_DrawString(" ");
TXT_DrawString(separator->label); TXT_DrawUTF8String(separator->label);
TXT_DrawString(" "); TXT_DrawString(" ");
} }
} }

View file

@ -45,8 +45,8 @@ extern txt_widget_class_t txt_separator_class;
/** /**
* Create a new horizontal separator widget. * Create a new horizontal separator widget.
* *
* @param label Label to display on the separator. If this is * @param label Label to display on the separator (UTF-8 format).
* set to NULL, no label is displayed. * If this is set to NULL, no label is displayed.
* @return The new separator widget. * @return The new separator widget.
*/ */
@ -56,7 +56,7 @@ txt_separator_t *TXT_NewSeparator(char *label);
* Change the label on a separator. * Change the label on a separator.
* *
* @param separator The separator. * @param separator The separator.
* @param label The new label. * @param label The new label (UTF-8 format).
*/ */
void TXT_SetSeparatorLabel(txt_separator_t *separator, char *label); void TXT_SetSeparatorLabel(txt_separator_t *separator, char *label);

View file

@ -24,6 +24,7 @@
#include "txt_gui.h" #include "txt_gui.h"
#include "txt_io.h" #include "txt_io.h"
#include "txt_main.h" #include "txt_main.h"
#include "txt_utf8.h"
#include "txt_window.h" #include "txt_window.h"
// Generate the format string to be used for displaying floats // Generate the format string to be used for displaying floats
@ -143,6 +144,7 @@ static void TXT_SpinControlDrawer(TXT_UNCAST_ARG(spincontrol))
unsigned int i; unsigned int i;
unsigned int padding; unsigned int padding;
txt_saved_colors_t colors; txt_saved_colors_t colors;
int bw;
int focused; int focused;
focused = spincontrol->widget.focused; focused = spincontrol->widget.focused;
@ -172,7 +174,8 @@ static void TXT_SpinControlDrawer(TXT_UNCAST_ARG(spincontrol))
i = 0; i = 0;
padding = spincontrol->widget.w - strlen(spincontrol->buffer) - 4; bw = TXT_UTF8_Strlen(spincontrol->buffer);
padding = spincontrol->widget.w - bw - 4;
while (i < padding) while (i < padding)
{ {
@ -180,8 +183,8 @@ static void TXT_SpinControlDrawer(TXT_UNCAST_ARG(spincontrol))
++i; ++i;
} }
TXT_DrawString(spincontrol->buffer); TXT_DrawUTF8String(spincontrol->buffer);
i += strlen(spincontrol->buffer); i += bw;
while (i < spincontrol->widget.w - 4) while (i < spincontrol->widget.w - 4)
{ {
@ -203,7 +206,8 @@ static void TXT_SpinControlDestructor(TXT_UNCAST_ARG(spincontrol))
static void AddCharacter(txt_spincontrol_t *spincontrol, int key) static void AddCharacter(txt_spincontrol_t *spincontrol, int key)
{ {
if (strlen(spincontrol->buffer) < SpinControlWidth(spincontrol)) if (TXT_UTF8_Strlen(spincontrol->buffer) < SpinControlWidth(spincontrol)
&& strlen(spincontrol->buffer) < spincontrol->buffer_len - 2)
{ {
spincontrol->buffer[strlen(spincontrol->buffer) + 1] = '\0'; spincontrol->buffer[strlen(spincontrol->buffer) + 1] = '\0';
spincontrol->buffer[strlen(spincontrol->buffer)] = key; spincontrol->buffer[strlen(spincontrol->buffer)] = key;
@ -212,7 +216,7 @@ static void AddCharacter(txt_spincontrol_t *spincontrol, int key)
static void Backspace(txt_spincontrol_t *spincontrol) static void Backspace(txt_spincontrol_t *spincontrol)
{ {
if (strlen(spincontrol->buffer) > 0) if (TXT_UTF8_Strlen(spincontrol->buffer) > 0)
{ {
spincontrol->buffer[strlen(spincontrol->buffer) - 1] = '\0'; spincontrol->buffer[strlen(spincontrol->buffer) - 1] = '\0';
} }

View file

@ -94,7 +94,8 @@ struct txt_window_s
/** /**
* Open a new window. * Open a new window.
* *
* @param title Title to display in the titlebar of the new window. * @param title Title to display in the titlebar of the new window
* (UTF-8 format).
* @return Pointer to a new @ref txt_window_t structure * @return Pointer to a new @ref txt_window_t structure
* representing the new window. * representing the new window.
*/ */
@ -191,8 +192,8 @@ void TXT_SetMouseListener(txt_window_t *window,
/** /**
* Open a window displaying a message. * Open a window displaying a message.
* *
* @param title Title of the window. * @param title Title of the window (UTF-8 format).
* @param message The message to display in the window. * @param message The message to display in the window (UTF-8 format).
* @return The new window. * @return The new window.
*/ */

View file

@ -22,6 +22,7 @@
#include "txt_gui.h" #include "txt_gui.h"
#include "txt_io.h" #include "txt_io.h"
#include "txt_main.h" #include "txt_main.h"
#include "txt_utf8.h"
#include "txt_window.h" #include "txt_window.h"
static void TXT_WindowActionSizeCalc(TXT_UNCAST_ARG(action)) static void TXT_WindowActionSizeCalc(TXT_UNCAST_ARG(action))
@ -34,7 +35,8 @@ static void TXT_WindowActionSizeCalc(TXT_UNCAST_ARG(action))
// Width is label length, plus key description length, plus '=' // Width is label length, plus key description length, plus '='
// and two surrounding spaces. // and two surrounding spaces.
action->widget.w = strlen(action->label) + strlen(buf) + 3; action->widget.w = TXT_UTF8_Strlen(action->label)
+ TXT_UTF8_Strlen(buf) + 3;
action->widget.h = 1; action->widget.h = 1;
} }
@ -52,12 +54,12 @@ static void TXT_WindowActionDrawer(TXT_UNCAST_ARG(action))
TXT_DrawString(" "); TXT_DrawString(" ");
TXT_FGColor(TXT_COLOR_BRIGHT_GREEN); TXT_FGColor(TXT_COLOR_BRIGHT_GREEN);
TXT_DrawString(buf); TXT_DrawUTF8String(buf);
TXT_FGColor(TXT_COLOR_BRIGHT_CYAN); TXT_FGColor(TXT_COLOR_BRIGHT_CYAN);
TXT_DrawString("="); TXT_DrawString("=");
TXT_FGColor(TXT_COLOR_BRIGHT_WHITE); TXT_FGColor(TXT_COLOR_BRIGHT_WHITE);
TXT_DrawString(action->label); TXT_DrawUTF8String(action->label);
TXT_DrawString(" "); TXT_DrawString(" ");
} }

View file

@ -48,7 +48,7 @@ struct txt_window_action_s
* *
* @param key The keyboard key that triggers this action. * @param key The keyboard key that triggers this action.
* @param label Label to display for this action in the tray * @param label Label to display for this action in the tray
* at the bottom of the window. * at the bottom of the window (UTF-8 format).
* @return Pointer to the new window action widget. * @return Pointer to the new window action widget.
*/ */