Fixed paging freeze, simplified show_menu function
This commit is contained in:
parent
0138aa5e5f
commit
1c9050484e
86
menu.c
86
menu.c
@ -1,5 +1,6 @@
|
|||||||
#include "menu.h"
|
#include "menu.h"
|
||||||
|
|
||||||
|
#include <conio.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
|
|
||||||
@ -19,66 +20,67 @@ const struct MenuBorder SOLID = {
|
|||||||
186, 205, 201, 187, 200, 188, '[', ']'
|
186, 205, 201, 187, 200, 188, '[', ']'
|
||||||
};
|
};
|
||||||
|
|
||||||
void print_content(const int itemc, const struct MenuItem itemv[], const char title[], const struct MenuBorder* border);
|
void print_content(const size_t itemc, const struct MenuItem itemv[], const char title[], const struct MenuBorder* border);
|
||||||
|
|
||||||
void show_menu(const struct MenuPage* pages, const size_t page_count, const int infinite_loop)
|
void show_menu(const struct MenuPage* pages, const size_t page_count, const int infinite_loop)
|
||||||
{
|
{
|
||||||
unsigned int page_index = 0;
|
unsigned page_index = 0;
|
||||||
int action_performed, loop, page_changed = 1;
|
int action_performed, page_changed, i;
|
||||||
char page_key, itemKey;
|
char key;
|
||||||
int i;
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
print_content(pages[page_index].item_count, pages[page_index].items, pages[page_index].title, pages[page_index].border);
|
||||||
|
|
||||||
|
// Wait for user selection
|
||||||
|
putchar('>');
|
||||||
|
putchar(' ');
|
||||||
|
|
||||||
do {
|
do {
|
||||||
print_content(pages[page_index].item_count, pages[page_index].items, pages[page_index].title, pages[page_index].border);
|
page_changed = 0;
|
||||||
|
action_performed = 0;
|
||||||
|
|
||||||
// Wait for user selection
|
// Get user selection
|
||||||
putchar('>');
|
key = _getch();
|
||||||
putchar(' ');
|
|
||||||
|
|
||||||
loop = pages[page_index].loopback;
|
|
||||||
page_key = _getch();
|
|
||||||
|
|
||||||
// Forward paging
|
// Forward paging
|
||||||
if (page_key == 'm' && page_index < page_count - 1)
|
if (key == 'm' && page_index < page_count - 1)
|
||||||
|
{
|
||||||
++page_index;
|
++page_index;
|
||||||
|
page_changed = 1;
|
||||||
|
}
|
||||||
// Backward paging
|
// Backward paging
|
||||||
else if (page_key == 'n' && page_index > 0)
|
else if (key == 'n' && page_index > 0)
|
||||||
|
{
|
||||||
--page_index;
|
--page_index;
|
||||||
|
page_changed = 1;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (page_key == 'n' || page_key == 'm')
|
// Search for action with a matching key
|
||||||
break;
|
for (i = 0; i < pages[page_index].item_count; ++i)
|
||||||
|
{
|
||||||
page_changed = 0;
|
if (pages[page_index].items[i].key == key)
|
||||||
itemKey = page_key;
|
|
||||||
do {
|
|
||||||
action_performed = 0;
|
|
||||||
|
|
||||||
itemKey != page_key ? itemKey = _getch() : page_key;
|
|
||||||
page_key = 0;
|
|
||||||
for (i = 0; i < pages[page_index].item_count; ++i)
|
|
||||||
{
|
{
|
||||||
if (pages[page_index].items[i].key == itemKey)
|
// Perform action
|
||||||
{
|
system("cls");
|
||||||
// Perform action
|
pages[page_index].items[i].action(pages[page_index].items[i].param);
|
||||||
system("cls");
|
action_performed = 1;
|
||||||
pages[page_index].items[i].action(pages[page_index].items[i].param);
|
|
||||||
action_performed = 1;
|
|
||||||
|
|
||||||
// Pause if requested
|
// Pause if requested
|
||||||
if (pages[page_index].pause)
|
if (pages[page_index].pause)
|
||||||
{
|
{
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
system("pause");
|
system("pause");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (!action_performed);
|
}
|
||||||
}
|
}
|
||||||
} while (loop || page_changed);
|
} while (!action_performed && !page_changed);
|
||||||
|
|
||||||
page_index = 0;
|
if (!pages[page_index].loopback && !page_changed)
|
||||||
|
{
|
||||||
|
page_index = 0;
|
||||||
|
}
|
||||||
} while (infinite_loop);
|
} while (infinite_loop);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,7 +107,7 @@ void get_console_dimensions(int* width, int* height)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Draws a menu to the console
|
// Draws a menu to the console
|
||||||
void print_content(const int itemc, const struct MenuItem itemv[], const char title[], const struct MenuBorder* border)
|
void print_content(const size_t itemc, const struct MenuItem itemv[], const char title[], const struct MenuBorder* border)
|
||||||
{
|
{
|
||||||
unsigned width, height, item_index;
|
unsigned width, height, item_index;
|
||||||
|
|
||||||
@ -142,7 +144,7 @@ void print_content(const int itemc, const struct MenuItem itemv[], const char ti
|
|||||||
putchar(border->corner_upper_right);
|
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, (int)itemc, &item_index))
|
||||||
{
|
{
|
||||||
// Print item text
|
// Print item text
|
||||||
putchar(border->line_vertical);
|
putchar(border->line_vertical);
|
||||||
|
Reference in New Issue
Block a user