hex-editor/src/util.c

98 lines
2.3 KiB
C

#include "util.h"
#include <gint/display.h>
#include <gint/keyboard.h>
#include <stdio.h>
void popup(int w, int h, int *x_ptr, int *y_ptr)
{
int x = DWIDTH / 2;
int y = DHEIGHT / 2;
int mx = 14, bx = 8;
int my = 12, by = 6;
drect(x - w/2 - mx, y - h/2 - my, x + w/2 + mx, y + h/2 + my, C_WHITE);
drect_border(x - w/2 - bx, y - h/2 - by, x + w/2 + bx, y + h/2 + by,
C_WHITE, 1, C_BLACK);
*x_ptr = x;
*y_ptr = y;
}
void please_wait(void)
{
int w, h, x, y;
char const *message = "Please wait...";
dsize(message, NULL, &w, &h);
popup(w, h, &x, &y);
dtext_opt(x, y, C_BLACK, C_NONE, DTEXT_CENTER, DTEXT_MIDDLE, message, -1);
dupdate();
}
bool confirm_discard(void)
{
int w=210, h=42, x, y;
popup(w, h, &x, &y);
dtext_opt(x-w/2, y-15, C_BLACK, C_NONE, DTEXT_LEFT, DTEXT_MIDDLE,
"There are unsaved changes.", -1);
dtext_opt(x-w/2, y, C_BLACK, C_NONE, DTEXT_LEFT, DTEXT_MIDDLE,
"[EXE]: Discard them", -1);
dtext_opt(x-w/2, y+15, C_BLACK, C_NONE, DTEXT_LEFT, DTEXT_MIDDLE,
"[EXIT]: Go back to editor", -1);
dupdate();
while(1) {
key_event_t ev = getkey();
if(ev.type != KEYEV_DOWN)
continue;
if(ev.key == KEY_EXE)
return true;
if(ev.key == KEY_ACON || ev.key == KEY_EXIT)
return false;
}
}
char const *human_size(int size, char *str)
{
static char default_buffer[64];
if(str == NULL)
str = default_buffer;
char digits[16];
sprintf(digits, "%d", size);
if(size < 1000) /* 1 kB */
sprintf(str, "%d B", size);
else if(size < 10000) /* 10 kB */
sprintf(str, "%d.%d kB", size / 1000, (size / 100) % 10);
else if(size < 1000000) /* 1 MB */
sprintf(str, "%d kB", size / 1000);
else
sprintf(str, "%d.%d MB", size / 1000000, (size / 100000) % 10);
return str;
}
char const *human_isize(int size, char *str)
{
static char default_buffer[64];
if(str == NULL)
str = default_buffer;
char digits[16];
sprintf(digits, "%d", size);
if(size < (1 << 10)) /* 1 kiB */
sprintf(str, "%d B", size);
else if(size < (1 << 20)) /* 1 MiB */
sprintf(str, "%d kiB", size >> 10);
else
sprintf(str, "%d MiB", size >> 20);
return str;
}