proper goto using a popup

This commit is contained in:
Lephenixnoir 2022-06-20 01:51:06 +01:00
parent 7a0978071e
commit 8eb962a364
Signed by untrusted user: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
1 changed files with 35 additions and 21 deletions

View File

@ -12,6 +12,7 @@
#include <justui/jfileselect.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void update_status(jlabel *status, jlabel *title, source_t *s)
@ -62,18 +63,13 @@ void hex_view(void)
jwidget *tab = jwidget_create(NULL);
heditor *mem = heditor_create(tab); /* previously 321x153 */
jlabel *status = jlabel_create("", tab);
jinput *input = jinput_create("Go to: ", 12, tab);
jwidget_set_margin(mem, 3, 0, 3, 0);
jwidget_set_stretch(mem, 1, 1, false);
jwidget_set_stretch(status, 1, 0, false);
jwidget_set_margin(input, 0, 0, 0, 4);
jwidget_set_stretch(input, 1, 0, false);
jwidget_set_visible(input, false);
jlayout_set_vbox(tab)->spacing = 4;
jlayout_set_vbox(tab)->spacing = 3;
jwidget_add_child(stack, tab);
jwidget_set_stretch(tab, 1, 1, false);
@ -86,12 +82,38 @@ void hex_view(void)
jfileselect_set_show_file_size(fileselect, true);
jwidget_set_stretch(fileselect, 1, 1, false);
// Goto popup //
jwidget *popup_goto = jwidget_create(scene);
jwidget_set_floating(popup_goto, true);
jwidget_set_border(popup_goto, J_BORDER_SOLID, 2, C_BLACK);
jwidget_set_padding(popup_goto, 4, 4, 4, 4);
jwidget_set_background(popup_goto, C_WHITE);
jwidget_set_visible(popup_goto, false);
jwidget_set_fixed_size(popup_goto, DWIDTH * 3 / 4, DHEIGHT / 2);
popup_goto->x = DWIDTH / 8;
popup_goto->y = DHEIGHT / 4;
jlayout_set_vbox(popup_goto)->spacing = 3;
jinput *goto_input = jinput_create("Go to: ", 12, popup_goto);
jwidget_set_stretch(goto_input, 1, 0, false);
jlabel *goto_help = jlabel_create(
"In hexadecimal. Use [ALPHA] to type letters.\n"
"Prefix with [+] or [-] to move relative to the current position.",
popup_goto);
jlabel_set_wrap_mode(goto_help, J_WRAP_WORD);
jlabel_set_line_spacing(goto_help, 2);
jlabel_set_block_alignment(goto_help, J_ALIGN_LEFT, J_ALIGN_TOP);
jwidget_set_stretch(goto_help, 1, 1, false);
// Event handling //
int key = 0;
while(key != KEY_EXIT)
{
bool input_focus = (jscene_focused_widget(scene) == input);
bool input_focus = (jscene_focused_widget(scene) == goto_input);
jevent e = jscene_run(scene);
if(e.type == JSCENE_PAINT) {
@ -101,19 +123,12 @@ void hex_view(void)
}
if(e.type == JINPUT_VALIDATED) {
/* Parse string into hexa */
char const *str = jinput_value(input);
uint32_t target = 0;
for(int k = 0; k < str[k]; k++)
{
target <<= 4;
if(str[k] <= '9') target += (str[k] - '0');
else target += ((str[k]|0x20) - 'a' + 10);
}
char const *str = jinput_value(goto_input);
uint32_t target = strtoul(str, NULL, 16);
heditor_move_to(mem, target);
}
if(e.type == JINPUT_VALIDATED || e.type == JINPUT_CANCELED) {
jwidget_set_visible(input, false);
jwidget_set_visible(fkeys, true);
jwidget_set_visible(popup_goto, false);
jscene_show_and_focus(scene, mem);
}
if(e.type == JFILESELECT_LOADED) {
@ -145,10 +160,9 @@ void hex_view(void)
key = e.key.key;
if(key == KEY_F1 && stack->layout_stack.active == 0 && !input_focus) {
jinput_clear(input);
jwidget_set_visible(input, true);
jwidget_set_visible(fkeys, false);
jscene_set_focused_widget(scene, input);
jinput_clear(goto_input);
jwidget_set_visible(popup_goto, true);
jscene_set_focused_widget(scene, goto_input);
}
if(key == KEY_F2 && !input_focus) {
jfileselect_browse(fileselect, "/");