Modified border logic to support user-defined border structures

This commit is contained in:
Kai S. K. Engelbart 2020-01-09 09:24:13 +01:00
parent 90878a7d9e
commit 62ec021810
3 changed files with 60 additions and 23 deletions

3
.gitignore vendored
View File

@ -1,6 +1,7 @@
################################################################################ ################################################################################
# Diese .gitignore-Datei wurde von Microsoft(R) Visual Studio automatisch erstellt. # Diese .gitignore-Datei wurde von Microsoft(R) Visual Studio automatisch erstellt.
################################################################################ ################################################################################
/.vs /.vs
/x64 /x64
/Debug

57
menu.c
View File

@ -2,6 +2,22 @@
#include "framework.h" #include "framework.h"
#include "menu.h" #include "menu.h"
const struct MenuBorder DEFAULT = {
'*', '*', '*', '*', '*', '*', '[', ']'
};
const struct MenuBorder MODERN = {
'|', '-', '-', '-', '-', '-', '-', '-'
};
const struct MenuBorder NO_BORDER = {
' ', ' ', ' ', ' ', ' ' ,' ' ,' ' ,' '
};
const struct MenuBorder SOLID = {
186, 205, 201, 187, 200, 188, '[', ']'
};
// Checks if a line index should display a menu item // Checks if a line index should display a menu item
bool is_item_line(const int line, const int itemc, int* item_index) bool is_item_line(const int line, const int itemc, int* item_index)
{ {
@ -24,7 +40,7 @@ void get_console_dimensions(int* width, int* height)
*height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; *height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
} }
void show_menu(const int itemc, struct MenuItem itemv[], const char title[], bool loopback, bool pause, enum MenuStyle style) void show_menu(const int itemc, const struct MenuItem itemv[], const char title[], const bool loopback, const bool pause, const struct MenuBorder *border)
{ {
unsigned width, height, item_index; unsigned width, height, item_index;
char key; char key;
@ -42,26 +58,31 @@ void show_menu(const int itemc, struct MenuItem itemv[], const char title[], boo
// Top line with title // Top line with title
if (i == 0) if (i == 0)
{ {
for (unsigned j = 0; j < width - strlen(title) - 1; ++j) // Upper left corner
putchar(border->corner_upper_left);
for (unsigned j = 1; j < width - strlen(title) - 2; ++j)
{ {
if (j == ((width - strlen(title)) - 2)/ 2) if (j == ((width - strlen(title)) - 2)/ 2)
{ {
style == DEFAULT ? putchar('*') : style == MODERN ? putchar('[') : putchar(' '); putchar(border->title_left);
fputs(title, stdout); fputs(title, stdout);
style == DEFAULT ? putchar('*') : style == MODERN ? putchar('[') : putchar(' '); putchar(border->title_right);
} }
else else
{ {
style == DEFAULT ? putchar('*') : style == MODERN ? putchar('-') : putchar(' '); putchar(border->line_horizontal);
} }
} }
// Upper right corner
putchar(border->corner_upper_right);
} }
// Line with menu item // Line with menu item
else if (is_item_line(i, itemc, &item_index)) else if (is_item_line(i, itemc, &item_index))
{ {
// Print item text // Print item text
putchar(border->line_vertical);
style == DEFAULT ? putchar('*') : style == MODERN ? putchar('|') : putchar(' ');
if (strcmp(itemv[item_index].text, "BLANK") == 0) { if (strcmp(itemv[item_index].text, "BLANK") == 0) {
putchar('\t'); putchar('\t');
@ -73,9 +94,9 @@ void show_menu(const int itemc, struct MenuItem itemv[], const char title[], boo
putchar(' '); putchar(' ');
} }
} }
else { else
{
printf("\t%c) %s", itemv[item_index].key, itemv[item_index].text); printf("\t%c) %s", itemv[item_index].key, itemv[item_index].text);
} }
// Print right side of frame // Print right side of frame
@ -84,25 +105,31 @@ void show_menu(const int itemc, struct MenuItem itemv[], const char title[], boo
putchar(' '); putchar(' ');
} }
style == DEFAULT ? putchar('*') : style == MODERN ? putchar('|') : putchar(' '); putchar(border->line_vertical);
} }
// Line above bottom line // Line above bottom line
else if (i == height - 3) else if (i == height - 3)
{ {
for (unsigned j = 0; j < width; ++j) // Lower left corner
putchar(border->corner_lower_left);
for (unsigned j = 1; j < width - 1; ++j)
{ {
style == DEFAULT ? putchar('*') : style == MODERN ? putchar('-') : putchar(' '); putchar(border->line_horizontal);
} }
// Lower right corner
putchar(border->corner_lower_right);
} }
// Blank line // Blank line
else else
{ {
i != height - 2 ? style == DEFAULT ? putchar('*') : style == MODERN ? putchar('|') : putchar(' ') : putchar(' '); i != height - 2 ? putchar(border->line_vertical) : putchar(' ');
for (unsigned j = 0; j < width - 2; ++j) for (unsigned j = 0; j < width - 2; ++j)
{ {
putchar(' '); putchar(' ');
} }
i != height - 2 ? style == DEFAULT ? putchar('*') : style == MODERN ? putchar('|') : putchar(' ') : putchar(' '); i != height - 2 ? putchar(border->line_vertical) : putchar(' ');
} }
} }
@ -134,6 +161,6 @@ void show_menu(const int itemc, struct MenuItem itemv[], const char title[], boo
// Show menu again if requested // Show menu again if requested
if (loopback) if (loopback)
{ {
show_menu(itemc, itemv, title, loopback, pause, style); show_menu(itemc, itemv, title, loopback, pause, border);
} }
} }

23
menu.h
View File

@ -10,15 +10,24 @@ struct MenuItem {
}; };
struct MenuBorder {
char line_vertical;
char line_horizontal;
char corner_upper_left;
char corner_upper_right;
char corner_lower_left;
char corner_lower_right;
char title_left;
char title_right;
};
const extern struct MenuBorder DEFAULT, MODERN, NO_BORDER, SOLID;
/// <summary>Is used for style-switching</summary> /// <summary>Is used for style-switching</summary>
/// <item name="DEFAULT">Default style with asterisks around the terminal</item> /// <item name="DEFAULT">Default style with asterisks around the terminal</item>
/// <item name="SOLID">Solid border with ASCII border characters</item>
/// <item name="MODERN">Minimalistic design with bar instead of start>/item> /// <item name="MODERN">Minimalistic design with bar instead of start>/item>
/// <item name="NO_BORDER">No border, just plain text</item> /// <item name="NO_BORDER">No border, just plain text</item>
enum MenuStyle {
DEFAULT,
MODERN,
NO_BORDER
};
/// <summary>Displaces a CUI menu to the user and lets them choose an option, then calls the corresponding function.</summary> /// <summary>Displaces a CUI menu to the user and lets them choose an option, then calls the corresponding function.</summary>
@ -27,5 +36,5 @@ enum MenuStyle {
/// <param name="title">The title of the menu.</param> /// <param name="title">The title of the menu.</param>
/// <param name="loopback">If this parameter is set to <c>true</c>, the menu will be displayed again after an action is executed.</param> /// <param name="loopback">If this parameter is set to <c>true</c>, the menu will be displayed again after an action is executed.</param>
/// <param name="pause">If this parameter is set to <c>true</c>, a <c>pause</c>command will be run after an action is executed.</param> /// <param name="pause">If this parameter is set to <c>true</c>, a <c>pause</c>command will be run after an action is executed.</param>
/// <param name="style">Specifies the style in which the menu is displayed.</param> /// <param name="border">Specifies the border in which the menu is displayed.</param>
void show_menu(const int itemc, struct MenuItem itemv[], const char title[], bool loopback, bool pause, enum MenuStyle style); void show_menu(const int itemc, const struct MenuItem itemv[], const char title[], const bool loopback, const bool pause, const struct MenuBorder *border);