2024-01-07 16:20:37 -05:00
|
|
|
//
|
|
|
|
// Created by william on 1/7/24.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "dialog.h"
|
|
|
|
|
2024-01-09 14:46:20 -05:00
|
|
|
#define DIALOG_KEY_CONFIRM 10
|
|
|
|
#define DIALOG_KEY_EXIT 27
|
|
|
|
#define DIALOG_KEY_DELETE 127
|
|
|
|
#define DIALOG_KEY_DIGIT_MIN 48
|
|
|
|
#define DIALOG_KEY_DIGIT_MAX 57
|
|
|
|
#define DIALOG_KEY_ALPHA_MIN 97
|
|
|
|
#define DIALOG_KEY_ALPHA_MAX 102
|
|
|
|
|
|
|
|
Dialog dialog_create(char *message) {
|
2024-01-07 16:20:37 -05:00
|
|
|
Dialog dialog;
|
2024-01-09 14:46:20 -05:00
|
|
|
|
|
|
|
int termHeight = getmaxy(stdscr);
|
|
|
|
int termWidth = getmaxx(stdscr);
|
|
|
|
|
|
|
|
int height = 3;
|
2024-01-07 16:20:37 -05:00
|
|
|
int width = (int) strlen(message) + 2;
|
2024-01-09 14:46:20 -05:00
|
|
|
if (width % 2 == 1) {
|
|
|
|
width += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int y = termHeight / 2 - (height / 2);
|
|
|
|
int x = termWidth / 2 - (width / 2);
|
2024-01-07 16:20:37 -05:00
|
|
|
|
2024-01-09 14:46:20 -05:00
|
|
|
WINDOW *window = newwin(height, width, y, x);
|
2024-01-07 16:20:37 -05:00
|
|
|
box(window, 0, 0);
|
|
|
|
|
|
|
|
mvwprintw(window, 0, 1, "%s", message);
|
2024-01-09 14:46:20 -05:00
|
|
|
wmove(window, 1, 1);
|
2024-01-07 16:20:37 -05:00
|
|
|
|
|
|
|
dialog.panel = new_panel(window);
|
|
|
|
|
|
|
|
return dialog;
|
|
|
|
}
|
2024-01-09 14:46:20 -05:00
|
|
|
|
|
|
|
address dialog_get_address(Dialog *dialog, bool *cancelled) {
|
|
|
|
int input_length = 0;
|
|
|
|
address out = 0;
|
|
|
|
|
|
|
|
int keycode;
|
|
|
|
while ((keycode = wgetch(dialog->panel->win)) != DIALOG_KEY_CONFIRM) {
|
|
|
|
if (keycode == DIALOG_KEY_EXIT) {
|
|
|
|
*cancelled = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input_length > 0 && keycode == DIALOG_KEY_DELETE) {
|
|
|
|
int offset = 16 - (4 * input_length);
|
|
|
|
address mask = 0xf << offset;
|
|
|
|
out &= ~mask;
|
|
|
|
|
|
|
|
input_length--;
|
|
|
|
mvwprintw(dialog->panel->win, 1, 1 + input_length, " ");
|
|
|
|
wmove(dialog->panel->win, 1, 1 + input_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input_length < 4 && keycode >= DIALOG_KEY_DIGIT_MIN && keycode <= DIALOG_KEY_ALPHA_MAX) {
|
|
|
|
int digit = keycode - DIALOG_KEY_DIGIT_MIN; // 0-9
|
|
|
|
if (digit > 9) {
|
|
|
|
if (keycode >= DIALOG_KEY_ALPHA_MIN) { // A-F
|
|
|
|
digit -= DIALOG_KEY_ALPHA_MIN - DIALOG_KEY_DIGIT_MAX - 1;
|
|
|
|
} else {
|
|
|
|
// Anything else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int offset = 12 - (4 * input_length);
|
|
|
|
out += digit << offset;
|
|
|
|
|
|
|
|
mvwprintw(dialog->panel->win, 1, 1 + input_length, "%c", keycode);
|
|
|
|
|
|
|
|
input_length++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dialog_remove(Dialog *dialog) {
|
|
|
|
del_panel(dialog->panel);
|
|
|
|
}
|