improvement of detection of active elements (not finished)
This commit is contained in:
+1
-1
@@ -110,7 +110,7 @@ void app_draw_modal(app_t *app) {
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
draw_dialogue(NULL, &(app->modal.dialogue));
|
draw_dialogue(&(app->modal.dialogue));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,17 +30,26 @@ void asa_modal_group_callback(callback_args_t *args) {
|
|||||||
dialogue_t *init_asa_modal(app_t *app) {
|
dialogue_t *init_asa_modal(app_t *app) {
|
||||||
if (app == NULL)
|
if (app == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
group_el_init_t children[] = {
|
group_el_init_t content[] = {
|
||||||
{.type = w_input, .label = "Address", .length = 15},
|
{.type = w_input, .label = "Address", .length = 15},
|
||||||
{.type = w_input, .label = "Port", .length = 5},
|
{.type = w_input, .label = "Port", .length = 5},
|
||||||
{.type = w_end}};
|
{.type = w_end}};
|
||||||
|
|
||||||
|
group_el_init_t actions[] = {{.type = w_button, .label = "Connect"},
|
||||||
|
{.type = w_button, .label = "Cancel"},
|
||||||
|
{.type = w_end}};
|
||||||
|
|
||||||
init_dialogue(&(app->modal.dialogue), "Connect to server",
|
init_dialogue(&(app->modal.dialogue), "Connect to server",
|
||||||
"There is information needed", app->cur_y, app->cur_x);
|
"There is information needed", app->cur_y, app->cur_x);
|
||||||
app->modal.dialogue.ch_group =
|
dialogue_t *d = &(app->modal.dialogue);
|
||||||
init_group(&(app->modal.dialogue.win), &(app->modal.dialogue.w), children,
|
|
||||||
horizontal);
|
d->g_content = init_group(&(d->win), &(d->w), content, horizontal);
|
||||||
app->modal.dialogue.ch_group->w.callback = asa_modal_group_callback;
|
d->g_content->w.callback = asa_modal_group_callback;
|
||||||
|
|
||||||
|
d->g_action = init_group(&(d->win), &(d->w), actions, horizontal);
|
||||||
|
d->g_action->w.callback = asa_modal_group_callback;
|
||||||
|
|
||||||
|
dialogue_init_active_id(d);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -40,10 +40,10 @@ dialogue_t *init_login_modal(app_t *app) {
|
|||||||
"Select your login "
|
"Select your login "
|
||||||
"option",
|
"option",
|
||||||
app->cur_y, app->cur_x);
|
app->cur_y, app->cur_x);
|
||||||
app->modal.dialogue.ch_group =
|
app->modal.dialogue.g_content =
|
||||||
init_group(&(app->modal.dialogue.win), &(app->modal.dialogue.w), children,
|
init_group(&(app->modal.dialogue.win), &(app->modal.dialogue.w), children,
|
||||||
horizontal);
|
horizontal);
|
||||||
app->modal.dialogue.ch_group->w.callback = login_modal_group_callback;
|
app->modal.dialogue.g_content->w.callback = login_modal_group_callback;
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -38,6 +38,10 @@ typedef struct widget_t {
|
|||||||
uint32_t y;
|
uint32_t y;
|
||||||
uint32_t m_x;
|
uint32_t m_x;
|
||||||
uint32_t m_y;
|
uint32_t m_y;
|
||||||
|
struct {
|
||||||
|
uint32_t y;
|
||||||
|
uint32_t x;
|
||||||
|
} cur;
|
||||||
WINDOW *const *parent_win;
|
WINDOW *const *parent_win;
|
||||||
struct widget_t *w_parent;
|
struct widget_t *w_parent;
|
||||||
enum rsize rsize;
|
enum rsize rsize;
|
||||||
|
|||||||
+84
-23
@@ -3,9 +3,9 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include "../app.h"
|
||||||
#include "dialogue.h"
|
#include "dialogue.h"
|
||||||
#include "group.h"
|
#include "group.h"
|
||||||
#include "../app.h"
|
|
||||||
|
|
||||||
void dialogue_default_callback(callback_args_t *args) {
|
void dialogue_default_callback(callback_args_t *args) {
|
||||||
dialogue_t *d = (void *)args->widget;
|
dialogue_t *d = (void *)args->widget;
|
||||||
@@ -14,25 +14,43 @@ void dialogue_default_callback(callback_args_t *args) {
|
|||||||
callback_args_t new_args;
|
callback_args_t new_args;
|
||||||
group_resp_t response;
|
group_resp_t response;
|
||||||
memcpy(&new_args, args, sizeof(callback_args_t));
|
memcpy(&new_args, args, sizeof(callback_args_t));
|
||||||
new_args.widget = d->ch_group;
|
new_args.widget = d->g_content;
|
||||||
new_args.resp_data = &response;
|
new_args.resp_data = &response;
|
||||||
switch (key) {
|
switch (key) {
|
||||||
|
case '\t':
|
||||||
|
// TODO: active task
|
||||||
|
if (d->active.type == g_content) {
|
||||||
|
if (d->active.id != d->g_content->last_id) {
|
||||||
|
d->active.id = d->active.id + 1;
|
||||||
|
} else {
|
||||||
|
d->active.id = d->g_content->first_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (d->active.type == g_action) {
|
||||||
|
if (d->active.id != d->g_action->last_id) {
|
||||||
|
d->active.id = d->active.id + 1;
|
||||||
|
} else {
|
||||||
|
d->active.id = d->g_action->first_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
draw_dialogue(d);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
d->ch_group->w.callback(&new_args);
|
d->g_content->w.callback(&new_args);
|
||||||
if (response.value > -1) {
|
if (response.value > -1) {
|
||||||
d->is_initiated = 0;
|
d->is_initiated = 0;
|
||||||
destroy_dialogue(d);
|
destroy_dialogue(d);
|
||||||
app->modal.type = none;
|
app->modal.type = none;
|
||||||
} else {
|
} else {
|
||||||
draw_dialogue(NULL, d);
|
draw_dialogue(d);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_dialogue(dialogue_t *dialogue, const char title[], const char text[], uint32_t cur_y,
|
void init_dialogue(dialogue_t *dialogue, const char title[], const char text[],
|
||||||
uint32_t cur_x) {
|
uint32_t cur_y, uint32_t cur_x) {
|
||||||
dialogue->win = 0;
|
dialogue->win = 0;
|
||||||
dialogue->ch_group = NULL;
|
dialogue->g_content = NULL;
|
||||||
dialogue->w.x = 0;
|
dialogue->w.x = 0;
|
||||||
dialogue->w.y = 0;
|
dialogue->w.y = 0;
|
||||||
dialogue->cur_y = cur_y;
|
dialogue->cur_y = cur_y;
|
||||||
@@ -43,32 +61,70 @@ void init_dialogue(dialogue_t *dialogue, const char title[], const char text[],
|
|||||||
strcpy(dialogue->text, text);
|
strcpy(dialogue->text, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t draw_dialogue(void *e, dialogue_t *d) {
|
void dialogue_init_active_id(dialogue_t *dialogue) {
|
||||||
if (!d->is_initiated) return -1;
|
enum w_type widget_type;
|
||||||
|
|
||||||
|
if (dialogue->g_content != NULL) {
|
||||||
|
for (int i = 0; i < dialogue->g_content->count; i++) {
|
||||||
|
widget_type = dialogue->g_content->elements[i].type;
|
||||||
|
if (widget_type == w_button || widget_type == w_input) {
|
||||||
|
dialogue->active.type = g_content;
|
||||||
|
widget_t *w = (widget_t *)dialogue->g_content->elements[i].element;
|
||||||
|
dialogue->active.id = w->id;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (dialogue->g_action != NULL) {
|
||||||
|
for (int i = 0; i < dialogue->g_action->count; i++) {
|
||||||
|
widget_type = dialogue->g_action->elements[i].type;
|
||||||
|
if (widget_type == w_button || widget_type == w_input) {
|
||||||
|
dialogue->active.type = g_action;
|
||||||
|
widget_t *w = (widget_t *)dialogue->g_action->elements[i].element;
|
||||||
|
dialogue->active.id = w->id;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#define DETECT_GROUP_SIZE(group, line_max_len, y, x) \
|
||||||
|
if (group) { \
|
||||||
|
group->w.m_y = y; \
|
||||||
|
if (line_max_len < group->w.x) { \
|
||||||
|
line_max_len = group->w.x; \
|
||||||
|
group->w.m_x = 1; /* when uses box */ \
|
||||||
|
} else { \
|
||||||
|
/* TODO: centering */ \
|
||||||
|
} \
|
||||||
|
y += group->w.y; \
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t draw_dialogue(dialogue_t *d) {
|
||||||
|
if (!d->is_initiated)
|
||||||
|
return -1;
|
||||||
/* count dimensions */
|
/* count dimensions */
|
||||||
uint32_t x = 1; /* when uses box */
|
uint32_t x = 1; /* when uses box */
|
||||||
uint32_t y = 1; /* when uses box */
|
uint32_t y = 1; /* when uses box */
|
||||||
|
|
||||||
|
/* analyze text content */
|
||||||
uint32_t line_count = 0;
|
uint32_t line_count = 0;
|
||||||
uint32_t line_max_len = 0;
|
uint32_t line_max_len = 0;
|
||||||
line_max_len = get_max_line_len(d->text, &line_count);
|
line_max_len = get_max_line_len(d->text, &line_count);
|
||||||
y += line_count;
|
y += line_count;
|
||||||
y += 2; /* margin for text */
|
y += 2; /* margin for text */
|
||||||
|
|
||||||
if (d->ch_group) {
|
/* analyze groups */
|
||||||
d->ch_group->w.m_y = y;
|
DETECT_GROUP_SIZE(d->g_content, line_max_len, y, x);
|
||||||
if (line_max_len < d->ch_group->w.x) {
|
DETECT_GROUP_SIZE(d->g_action, line_max_len, y, x);
|
||||||
line_max_len = d->ch_group->w.x;
|
|
||||||
d->ch_group->w.m_x = 1; /* when uses box */
|
|
||||||
} else {
|
|
||||||
/* TODO: centering */
|
|
||||||
}
|
|
||||||
y += d->ch_group->w.y;
|
|
||||||
}
|
|
||||||
x += line_max_len;
|
x += line_max_len;
|
||||||
|
|
||||||
x += 1; /* when uses box */
|
x += 1; /* when uses box */
|
||||||
y += 1; /* when uses box */
|
y += 1; /* when uses box */
|
||||||
|
|
||||||
|
/* space for action block */
|
||||||
|
|
||||||
|
y += 2;
|
||||||
|
|
||||||
d->w.x = x;
|
d->w.x = x;
|
||||||
d->w.y = y;
|
d->w.y = y;
|
||||||
d->w.m_y = (d->cur_y - y) / 2;
|
d->w.m_y = (d->cur_y - y) / 2;
|
||||||
@@ -96,16 +152,21 @@ int32_t draw_dialogue(void *e, dialogue_t *d) {
|
|||||||
|
|
||||||
/* text */
|
/* text */
|
||||||
wattroff(d->win, A_BOLD);
|
wattroff(d->win, A_BOLD);
|
||||||
|
/* mvwhline(d->win, d->w.y - 3, 1, 0, d->w.x - 2); */
|
||||||
print_multiline_text(d->win, d->text, d->w.x, 2, 1, PMT_ALIGN_CENTER);
|
print_multiline_text(d->win, d->text, d->w.x, 2, 1, PMT_ALIGN_CENTER);
|
||||||
wattroff(d->win, A_REVERSE);
|
wattroff(d->win, A_REVERSE);
|
||||||
draw_group(d->win, d->ch_group);
|
if (d->g_content != NULL) {
|
||||||
|
draw_group(d->win, d->g_content, d->active.id);
|
||||||
|
}
|
||||||
|
if (d->g_action != NULL) {
|
||||||
|
draw_group(d->win, d->g_action, d->active.id);
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
void destroy_dialogue(dialogue_t *d) {
|
void destroy_dialogue(dialogue_t *d) {
|
||||||
if (d->ch_group) {
|
if (d->g_content) {
|
||||||
destroy_group(d->ch_group);
|
destroy_group(d->g_content);
|
||||||
}
|
}
|
||||||
delwin(d->win);
|
delwin(d->win);
|
||||||
d->win = NULL;
|
d->win = NULL;
|
||||||
|
|||||||
@@ -5,21 +5,31 @@
|
|||||||
#include "button.h"
|
#include "button.h"
|
||||||
#include "group.h"
|
#include "group.h"
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
enum g_type { g_content, g_action };
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
WINDOW *win;
|
|
||||||
widget_t w;
|
widget_t w;
|
||||||
group_t *ch_group;
|
WINDOW *win;
|
||||||
|
group_t *g_content;
|
||||||
|
group_t *g_action;
|
||||||
|
struct {
|
||||||
|
int32_t id;
|
||||||
|
enum g_type type;
|
||||||
|
uint32_t y, x;
|
||||||
|
} active;
|
||||||
uint32_t cur_y;
|
uint32_t cur_y;
|
||||||
uint32_t cur_x;
|
uint32_t cur_x;
|
||||||
char text[DIALOGUE_TEXT];
|
char text[DIALOGUE_TEXT];
|
||||||
uint32_t is_initiated : 1;
|
uint32_t is_initiated : 1;
|
||||||
} dialogue_t;
|
} dialogue_t;
|
||||||
|
|
||||||
void init_dialogue(dialogue_t *d, const char title[], const char text[], uint32_t cur_x,
|
void init_dialogue(dialogue_t *d, const char title[], const char text[],
|
||||||
uint32_t cur_y);
|
uint32_t cur_x, uint32_t cur_y);
|
||||||
int32_t draw_dialogue(void *, dialogue_t *d);
|
int32_t draw_dialogue(dialogue_t *d);
|
||||||
void destroy_dialogue(dialogue_t *d);
|
void destroy_dialogue(dialogue_t *d);
|
||||||
void dialogue_default_callback(callback_args_t *args);
|
void dialogue_default_callback(callback_args_t *args);
|
||||||
|
void dialogue_init_active_id(dialogue_t *dialogue);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+47
-53
@@ -5,67 +5,59 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
// TODO: refactor
|
||||||
|
|
||||||
|
#define MAKE_RESPONSE_M1(args, resp_data, response) \
|
||||||
|
if (args->resp_data != NULL) { \
|
||||||
|
response->active_id = g->active_id; \
|
||||||
|
response->value = -1; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define FIND_ELEMENT(args, element_ptr, element_idx) \
|
||||||
|
if (args->resp_data != NULL) { \
|
||||||
|
for (int i = 0; i < g->count; i++) { \
|
||||||
|
if (g->elements[i].id == g->active_id) { \
|
||||||
|
element_ptr = &(g->elements[i]); \
|
||||||
|
element_idx = i; \
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
void group_default_callback(callback_args_t *args) {
|
void group_default_callback(callback_args_t *args) {
|
||||||
group_t *g = (group_t *)args->widget;
|
group_t *g = (group_t *)args->widget;
|
||||||
char key = *((char *)args->data);
|
char key = *((char *)args->data);
|
||||||
group_resp_t *response = (group_resp_t *)args->resp_data;
|
group_resp_t *response = (group_resp_t *)args->resp_data;
|
||||||
input_t *inpt;
|
input_t *input;
|
||||||
|
group_el_t *element_ptr;
|
||||||
|
int32_t element_idx = -1;
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case '\t':
|
case '\n': /* Enter */
|
||||||
if (g->active_id != g->last_id) {
|
FIND_ELEMENT(args, element_ptr, element_idx);
|
||||||
g->active_id = g->active_id + 1;
|
response->active_id = g->active_id;
|
||||||
} else {
|
response->value = element_idx;
|
||||||
g->active_id = g->first_id;
|
|
||||||
}
|
|
||||||
if (args->resp_data != NULL) {
|
|
||||||
response->active_id = g->active_id;
|
|
||||||
response->value = -1;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case '\n':
|
case '\177': /* DEL */
|
||||||
if (args->resp_data != NULL) {
|
FIND_ELEMENT(args, element_ptr, element_idx);
|
||||||
for (int i = 0; i < g->count; i++) {
|
if (element_ptr->type == w_input) {
|
||||||
if (g->elements[i].id == g->active_id) {
|
input = (input_t *)element_ptr->element;
|
||||||
response->active_id = g->active_id;
|
if (input->value_len) {
|
||||||
response->value = i;
|
input->value[--input->value_len] = '\0';
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
MAKE_RESPONSE_M1(args, resp_data, response);
|
||||||
case '\177':
|
|
||||||
for (int i = 0; i < g->count; i++) {
|
|
||||||
if (g->elements[i].id == g->active_id) {
|
|
||||||
if (g->elements[i].type == w_input) {
|
|
||||||
inpt = (input_t *)g->elements[i].element;
|
|
||||||
if (inpt->value_len) {
|
|
||||||
inpt->value[--inpt->value_len] = '\0';
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (args->resp_data != NULL) {
|
|
||||||
response->active_id = g->active_id;
|
|
||||||
response->value = -1;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
for (int i = 0; i < g->count; i++) {
|
FIND_ELEMENT(args, element_ptr, element_idx);
|
||||||
if (g->elements[i].id == g->active_id) {
|
if (element_ptr->type == w_input) {
|
||||||
if (g->elements[i].type == w_input) {
|
input = (input_t *)element_ptr->element;
|
||||||
inpt = (input_t *)g->elements[i].element;
|
if (input->max_len > input->value_len) {
|
||||||
if (inpt->max_len > inpt->value_len) {
|
input->value[input->value_len++] = key;
|
||||||
inpt->value[inpt->value_len++] = key;
|
input->value[input->value_len] = '\0';
|
||||||
inpt->value[inpt->value_len] = '\0';
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (args->resp_data != NULL) {
|
MAKE_RESPONSE_M1(args, resp_data, response);
|
||||||
response->active_id = g->active_id;
|
break;
|
||||||
response->value = -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,20 +118,20 @@ group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
|
|||||||
return group;
|
return group;
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_group(WINDOW *win, group_t *group) {
|
void draw_group(WINDOW *win, group_t *group, int32_t active_id) {
|
||||||
group_el_t *children = group->elements;
|
group_el_t *children = group->elements;
|
||||||
for (int i = 0; i < group->count; i++) {
|
for (int i = 0; i < group->count; i++) {
|
||||||
group_el_t *el = &children[i];
|
group_el_t *el = &children[i];
|
||||||
switch (el->type) {
|
switch (el->type) {
|
||||||
case w_button:
|
case w_button:
|
||||||
draw_button((button_t *)el->element, group->active_id);
|
draw_button((button_t *)el->element, active_id);
|
||||||
break;
|
break;
|
||||||
case w_box:
|
case w_box:
|
||||||
case w_group:
|
case w_group:
|
||||||
case w_end:
|
case w_end:
|
||||||
break;
|
break;
|
||||||
case w_input:
|
case w_input:
|
||||||
draw_input((input_t *)el->element, group->active_id);
|
draw_input((input_t *)el->element, active_id);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -152,7 +144,9 @@ void draw_group(WINDOW *win, group_t *group) {
|
|||||||
if (input->max_len == input->value_len) {
|
if (input->max_len == input->value_len) {
|
||||||
wmove(win, margin_y + 1, margin_x + input->value_len);
|
wmove(win, margin_y + 1, margin_x + input->value_len);
|
||||||
} else {
|
} else {
|
||||||
wmove(win, margin_y + 1, margin_x + input->value_len + 1); // x: +1: margin left from the border
|
wmove(win, margin_y + 1,
|
||||||
|
margin_x + input->value_len +
|
||||||
|
1); // x: +1: margin left from the border
|
||||||
}
|
}
|
||||||
curs_set(1);
|
curs_set(1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,13 +22,12 @@ typedef struct {
|
|||||||
} group_el_init_t;
|
} group_el_init_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
group_el_t *elements;
|
|
||||||
widget_t w;
|
widget_t w;
|
||||||
uint32_t active_id;
|
group_el_t *elements;
|
||||||
|
uint32_t active_id; // TODO: needs to be removed
|
||||||
enum g_direction direction;
|
enum g_direction direction;
|
||||||
uint32_t count;
|
uint32_t count;
|
||||||
uint32_t first_id, last_id;
|
uint32_t first_id, last_id;
|
||||||
void *action_args;
|
|
||||||
} group_t;
|
} group_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -38,7 +37,7 @@ typedef struct {
|
|||||||
|
|
||||||
group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
|
group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
|
||||||
enum g_direction dir);
|
enum g_direction dir);
|
||||||
void draw_group(WINDOW *win, group_t *group);
|
void draw_group(WINDOW *win, group_t *group, int32_t active_id);
|
||||||
void destroy_group(group_t *group);
|
void destroy_group(group_t *group);
|
||||||
void group_default_callback(callback_args_t *args);
|
void group_default_callback(callback_args_t *args);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user