initial callback support
This commit is contained in:
+4
-12
@@ -11,30 +11,22 @@ button_t *init_button(WINDOW **win, widget_t *w_parent, char *label) {
|
||||
button_t *btn = malloc(sizeof(button_t));
|
||||
init_widget(&(btn->w), w_parent, win, label);
|
||||
btn->is_disabled = 0;
|
||||
btn->is_focused = 0;
|
||||
btn->is_hovered = 0;
|
||||
btn->callback = NULL;
|
||||
btn->w.x = strlen(btn->w.title) + 4; // + "[ ]"
|
||||
btn->w.x = strlen(btn->w.title) + 6; // + "[ ]"
|
||||
btn->w.y = 1; // TODO: detect new line
|
||||
if (btn->is_focused) btn->w.x += 2; // + "<>"
|
||||
return btn;
|
||||
}
|
||||
|
||||
int32_t draw_button(button_t *btn) {
|
||||
int32_t draw_button(button_t *btn, uint32_t active_id) {
|
||||
char title[DIALOGUE_TITLE + 6];
|
||||
WINDOW *win = *(btn->w.parent_win);
|
||||
uint32_t margin_y = btn->w.m_y + btn->w.w_parent->m_y;
|
||||
uint32_t margin_x = btn->w.m_x + btn->w.w_parent->m_x;
|
||||
|
||||
if (btn->is_focused) {
|
||||
if (btn->w.id == active_id) {
|
||||
sprintf(title, "[< %s >]", btn->w.title);
|
||||
} else {
|
||||
sprintf(title, "[ %s ]", btn->w.title);
|
||||
}
|
||||
|
||||
if (btn->is_hovered) {
|
||||
wattrset(win, COLOR_PAIR(MODAL_COLOR_PAIR) | A_BOLD | A_REVERSE);
|
||||
} else {
|
||||
sprintf(title, "[ %s ]", btn->w.title);
|
||||
wattrset(win, COLOR_PAIR(0) | A_REVERSE);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,13 +8,10 @@
|
||||
typedef struct {
|
||||
widget_t w;
|
||||
char hotkey;
|
||||
uint32_t is_focused : 1;
|
||||
uint32_t is_disabled : 1;
|
||||
uint32_t is_hovered : 1;
|
||||
callback_t *callback;
|
||||
} button_t;
|
||||
|
||||
button_t *init_button(WINDOW **win, widget_t *w_parent, char *label);
|
||||
int32_t draw_button(button_t *btn);
|
||||
int32_t draw_button(button_t *btn, uint32_t active_id);
|
||||
void destroy_button(button_t *btn);
|
||||
#endif
|
||||
@@ -8,12 +8,28 @@
|
||||
#include "dialogue.h"
|
||||
#include "group.h"
|
||||
|
||||
void default_callback(WINDOW *win, void *widget, void *data) {
|
||||
dialogue_t *d = (void *)widget;
|
||||
char key = *((char *)data);
|
||||
switch (key) {
|
||||
case '\t':
|
||||
if (d->ch_group->w_active_id != d->ch_group->last_id) {
|
||||
d->ch_group->w_active_id = d->ch_group->w_active_id + 1;
|
||||
} else {
|
||||
d->ch_group->w_active_id = d->ch_group->first_id;
|
||||
}
|
||||
draw_group(win, d->ch_group);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
dialogue_t *init_dialogue(const char title[], const char text[]) {
|
||||
dialogue_t *dialogue = malloc(sizeof(dialogue_t));
|
||||
dialogue->win = 0;
|
||||
dialogue->ch_group = NULL;
|
||||
dialogue->w.x = 0;
|
||||
dialogue->w.y = 0;
|
||||
dialogue->w.callback = default_callback;
|
||||
strcpy(dialogue->w.title, title);
|
||||
strcpy(dialogue->text, text);
|
||||
return dialogue;
|
||||
@@ -52,7 +68,9 @@ int32_t draw_dialogue(void *_app, dialogue_t *d) {
|
||||
d->w.m_y = (app->cur_y - y) / 2;
|
||||
|
||||
/* render window */
|
||||
d->win = newwin(y, x, d->w.m_y, d->w.m_x);
|
||||
if (d->win == NULL) {
|
||||
d->win = newwin(y, x, d->w.m_y, d->w.m_x);
|
||||
}
|
||||
wattrset(d->win, COLOR_PAIR(0) | A_BOLD | A_REVERSE);
|
||||
|
||||
/* background */
|
||||
|
||||
@@ -16,5 +16,6 @@ typedef struct {
|
||||
dialogue_t *init_dialogue(const char title[], const char text[]);
|
||||
int32_t draw_dialogue(void *app, dialogue_t *d);
|
||||
void destroy_active_dialogue(dialogue_t *d);
|
||||
void default_callback(WINDOW *win, void *widget, void *data);
|
||||
|
||||
#endif
|
||||
+17
-10
@@ -9,23 +9,25 @@ group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
|
||||
group_t *group = malloc(sizeof(group_t));
|
||||
|
||||
/* count elements */
|
||||
uint32_t count = 0;
|
||||
for (; children[count].type != w_end; count++)
|
||||
group->count = 0;
|
||||
for (; children[group->count].type != w_end; group->count++)
|
||||
;
|
||||
group->elements = calloc(count, sizeof(group_el_t));
|
||||
group->elements = calloc(group->count, sizeof(group_el_t));
|
||||
group->w.w_parent = w_parent;
|
||||
group_el_t *elements = group->elements;
|
||||
for (int i = 0; i < count; i++) {
|
||||
for (int i = 0; i < group->count; i++) {
|
||||
elements[i].type = children[i].type;
|
||||
}
|
||||
|
||||
/* init child elements */
|
||||
for (int i = 0; elements[i].type != w_end; i++) {
|
||||
uint32_t el_id = 0;
|
||||
for (int i = 0; i < group->count; i++) {
|
||||
switch (elements[i].type) {
|
||||
case w_button:
|
||||
elements[i].element = init_button(win, &(group->w), children[i].label);
|
||||
if (direction == horizontal) {
|
||||
button_t *b = (button_t *)elements[i].element;
|
||||
el_id = b->w.id;
|
||||
b->w.m_x = group->w.x;
|
||||
b->w.m_y = group->w.m_y;
|
||||
group->w.x += b->w.x + 1;
|
||||
@@ -36,21 +38,26 @@ group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
|
||||
case w_box:
|
||||
case w_group:
|
||||
case w_end:
|
||||
group->w.x--;
|
||||
break;
|
||||
}
|
||||
if (i == 0) {
|
||||
group->first_id = el_id;
|
||||
group->w_active_id = el_id;
|
||||
} else {
|
||||
group->last_id = el_id;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return group;
|
||||
}
|
||||
|
||||
void draw_group(WINDOW *win, group_t *group) {
|
||||
group_el_t *children = group->elements;
|
||||
for (int i = 0; children[i].type != w_end; i++) {
|
||||
for (int i = 0; i < group->count; i++) {
|
||||
group_el_t *el = &children[i];
|
||||
switch (el->type) {
|
||||
case w_button:
|
||||
draw_button((button_t *)el->element);
|
||||
draw_button((button_t *)el->element, group->w_active_id);
|
||||
break;
|
||||
case w_box:
|
||||
case w_group:
|
||||
@@ -62,7 +69,7 @@ void draw_group(WINDOW *win, group_t *group) {
|
||||
|
||||
void destroy_group(group_t *group) {
|
||||
group_el_t *children = group->elements;
|
||||
for (int i = 0; children[i].type != w_end; i++) {
|
||||
for (int i = 0; i < group->count; i++) {
|
||||
group_el_t *el = &children[i];
|
||||
switch (el->type) {
|
||||
case w_button:
|
||||
|
||||
@@ -23,6 +23,8 @@ typedef struct {
|
||||
widget_t w;
|
||||
uint32_t w_active_id;
|
||||
enum g_direction direction;
|
||||
uint32_t count;
|
||||
uint32_t first_id, last_id;
|
||||
} group_t;
|
||||
|
||||
group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
|
||||
|
||||
Reference in New Issue
Block a user