updates widget positions
This commit is contained in:
+18
-9
@@ -1,19 +1,30 @@
|
||||
#include "button.h"
|
||||
#include <ncurses.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
button_t *init_button() {
|
||||
#include "../app.h"
|
||||
#include "../widget.h"
|
||||
#include "button.h"
|
||||
|
||||
button_t *init_button(WINDOW **win, uint32_t parent_id, char *label) {
|
||||
button_t *btn = malloc(sizeof(button_t));
|
||||
init_widget(&(btn->w), win, label, parent_id);
|
||||
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.y = 1; // TODO: detect new line
|
||||
if (btn->is_focused) btn->w.x += 2; // + "<>"
|
||||
return btn;
|
||||
}
|
||||
|
||||
int32_t draw_button(WINDOW *win, button_t *btn, uint32_t cur_x, uint32_t cur_y) {
|
||||
int32_t draw_button(button_t *btn) {
|
||||
char title[DIALOGUE_TITLE + 6];
|
||||
WINDOW *win = *(btn->w.parent_win);
|
||||
uint32_t cur_y = btn->w.m_y;
|
||||
uint32_t cur_x = btn->w.m_x;
|
||||
|
||||
if (btn->is_focused) {
|
||||
sprintf(title, "[< %s >]", btn->w.title);
|
||||
@@ -22,17 +33,15 @@ int32_t draw_button(WINDOW *win, button_t *btn, uint32_t cur_x, uint32_t cur_y)
|
||||
}
|
||||
|
||||
if (btn->is_hovered) {
|
||||
wattrset(win, COLOR_PAIR(1) | A_BOLD | A_REVERSE);
|
||||
wattrset(win, COLOR_PAIR(MODAL_COLOR_PAIR) | A_BOLD | A_REVERSE);
|
||||
} else {
|
||||
wattrset(win, COLOR_PAIR(1) | A_BOLD);
|
||||
wattrset(win, COLOR_PAIR(0) | A_REVERSE);
|
||||
}
|
||||
|
||||
mvwprintw(win, cur_y, cur_x, "%s", title);
|
||||
|
||||
wattroff(win, A_REVERSE | A_BOLD);
|
||||
wattroff(win, A_BOLD | A_REVERSE);
|
||||
return strlen(title);
|
||||
}
|
||||
|
||||
void destroy_button(button_t *btn) {
|
||||
free(btn);
|
||||
}
|
||||
void destroy_button(button_t *btn) { free(btn); }
|
||||
@@ -14,7 +14,7 @@ typedef struct {
|
||||
callback_t *callback;
|
||||
} button_t;
|
||||
|
||||
button_t *init_button();
|
||||
int32_t draw_button(WINDOW *win, button_t *btn, uint32_t cur_x, uint32_t cur_y);
|
||||
button_t *init_button(WINDOW **win, uint32_t parent_id, char *label);
|
||||
int32_t draw_button(button_t *btn);
|
||||
void destroy_button(button_t *btn);
|
||||
#endif
|
||||
@@ -18,13 +18,14 @@ dialogue_t *init_dialogue(const char title[], const char text[],
|
||||
return dialogue;
|
||||
}
|
||||
|
||||
int32_t draw_dialogue(dialogue_t *d, uint32_t cur_x, uint32_t cur_y) {
|
||||
int32_t draw_dialogue(void *app, dialogue_t *d, uint32_t cur_x, uint32_t cur_y) {
|
||||
app_t *_app = (app_t *) app;
|
||||
d->w.m_x = (cur_x - d->w.x) / 2;
|
||||
d->w.m_y = (cur_y - d->w.y) / 2;
|
||||
|
||||
d->win = newwin(d->w.y, d->w.x, d->w.m_y, d->w.m_x);
|
||||
|
||||
wattrset(d->win, COLOR_PAIR(MODAL_COLOR_PAIR) | A_BOLD | A_REVERSE);
|
||||
wattrset(d->win, COLOR_PAIR(0) | A_BOLD | A_REVERSE);
|
||||
|
||||
/* background */
|
||||
for (int xp = 1; xp < d->w.x; xp++) {
|
||||
@@ -43,14 +44,18 @@ int32_t draw_dialogue(dialogue_t *d, uint32_t cur_x, uint32_t cur_y) {
|
||||
/* text */
|
||||
print_multiline_text(d->win, d->text, d->w.x, 1, 1, PMT_ALIGN_CENTER);
|
||||
wattroff(d->win, A_REVERSE);
|
||||
// temp!!!
|
||||
draw_group(d->win, d->ch_group, 1, 4);
|
||||
d->ch_group->w.m_x = (d->w.x - d->ch_group->w.x) / 2;
|
||||
d->ch_group->w.m_y = 4;
|
||||
draw_group(d->win, d->ch_group);
|
||||
wrefresh(d->win);
|
||||
|
||||
return 0;
|
||||
};
|
||||
|
||||
void destroy_active_dialogue(dialogue_t *d) {
|
||||
if (d->ch_group) {
|
||||
destroy_group(d->ch_group);
|
||||
}
|
||||
delwin(d->win);
|
||||
free(d->win);
|
||||
d->win = NULL;
|
||||
|
||||
@@ -14,7 +14,7 @@ typedef struct {
|
||||
} dialogue_t;
|
||||
|
||||
dialogue_t *init_dialogue(const char title[], const char text[], const uint32_t w, const uint32_t h);
|
||||
int32_t draw_dialogue(dialogue_t *d, const uint32_t cur_x, const uint32_t cur_y);
|
||||
int32_t draw_dialogue(void *app, dialogue_t *d, const uint32_t cur_x, const uint32_t cur_y);
|
||||
void destroy_active_dialogue(dialogue_t *d);
|
||||
|
||||
#endif
|
||||
+23
-13
@@ -2,45 +2,55 @@
|
||||
#include "button.h"
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
group_t *init_group(group_el_t *children) {
|
||||
group_t *init_group(WINDOW **win, uint32_t par_id, group_el_init_t *children,
|
||||
enum g_direction direction) {
|
||||
group_t *group = malloc(sizeof(group_t));
|
||||
|
||||
/* count elements */
|
||||
uint32_t count = 0;
|
||||
for (count = 0; children[count].type != w_end; count++)
|
||||
for (; children[count].type != w_end; count++)
|
||||
;
|
||||
count++; /* TODO: improve counting */
|
||||
group->elements = malloc(sizeof(group_el_t) * count);
|
||||
memcpy(group->elements, children, sizeof(group_el_t) * count);
|
||||
group->elements = calloc(count, sizeof(group_el_t));
|
||||
group_el_t *elements = group->elements;
|
||||
for (int i = 0; i < count; i++) {
|
||||
elements[i].type = children[i].type;
|
||||
}
|
||||
|
||||
/* init child elements */
|
||||
for (int i = 0; group->elements[i].type != w_end; i++) {
|
||||
group_el_t *el = &(group->elements)[i];
|
||||
switch (el->type) {
|
||||
for (int i = 0; elements[i].type != w_end; i++) {
|
||||
switch (elements[i].type) {
|
||||
case w_button:
|
||||
el->element = init_button();
|
||||
elements[i].element = init_button(win, par_id, children[i].label);
|
||||
if (direction == horizontal) {
|
||||
button_t *b = (button_t *)elements[i].element;
|
||||
b->w.m_x = group->w.x;
|
||||
b->w.m_y = group->w.m_y;
|
||||
group->w.x += b->w.x + 1;
|
||||
if (group->w.y < b->w.y)
|
||||
group->w.y = b->w.y;
|
||||
}
|
||||
break;
|
||||
case w_box:
|
||||
case w_group:
|
||||
case w_end:
|
||||
group->w.x--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
group->direction = horizontal; /* horizontal by default */
|
||||
|
||||
return group;
|
||||
}
|
||||
|
||||
void draw_group(WINDOW *win, group_t *group, uint32_t cur_x, uint32_t cur_y) {
|
||||
void draw_group(WINDOW *win, group_t *group) {
|
||||
group_el_t *children = group->elements;
|
||||
int32_t x_used = 0; /* used space */
|
||||
for (int i = 0; children[i].type != w_end; i++) {
|
||||
group_el_t *el = &children[i];
|
||||
switch (el->type) {
|
||||
case w_button:
|
||||
x_used += draw_button(win, (button_t *)el->element, cur_x + x_used + 1 * i, cur_y);
|
||||
draw_button((button_t *)el->element);
|
||||
break;
|
||||
case w_box:
|
||||
case w_group:
|
||||
|
||||
@@ -13,6 +13,11 @@ typedef struct {
|
||||
enum w_type type;
|
||||
} group_el_t;
|
||||
|
||||
typedef struct {
|
||||
enum w_type type;
|
||||
char label[DIALOGUE_TITLE];
|
||||
} group_el_init_t;
|
||||
|
||||
typedef struct {
|
||||
group_el_t *elements;
|
||||
widget_t w;
|
||||
@@ -20,8 +25,9 @@ typedef struct {
|
||||
enum g_direction direction;
|
||||
} group_t;
|
||||
|
||||
group_t *init_group(group_el_t *children);
|
||||
void draw_group(WINDOW *win, group_t *group, uint32_t cur_x, uint32_t cur_y);
|
||||
group_t *init_group(WINDOW **win, uint32_t par_id, group_el_init_t *children,
|
||||
enum g_direction dir);
|
||||
void draw_group(WINDOW *win, group_t *group);
|
||||
void destroy_group(group_t *group);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user