element groups: initial support, improves login dialog init
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
#include "button.h"
|
||||
#include <ncurses.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
button_t *init_button() {
|
||||
button_t *btn = malloc(sizeof(button_t));
|
||||
btn->is_disabled = 0;
|
||||
btn->is_focused = 0;
|
||||
btn->is_hovered = 0;
|
||||
btn->callback = NULL;
|
||||
return btn;
|
||||
}
|
||||
|
||||
int32_t draw_button(WINDOW *win, button_t *btn, uint32_t cur_x, uint32_t cur_y) {
|
||||
char title[DIALOGUE_TITLE + 6];
|
||||
|
||||
if (btn->is_focused) {
|
||||
sprintf(title, "[< %s >]", btn->w.title);
|
||||
} else {
|
||||
sprintf(title, "[ %s ]", btn->w.title);
|
||||
}
|
||||
|
||||
if (btn->is_hovered) {
|
||||
wattrset(win, COLOR_PAIR(1) | A_BOLD | A_REVERSE);
|
||||
} else {
|
||||
wattrset(win, COLOR_PAIR(1) | A_BOLD);
|
||||
}
|
||||
|
||||
mvwprintw(win, cur_y, cur_x, "%s", title);
|
||||
|
||||
wattroff(win, A_REVERSE | A_BOLD);
|
||||
return strlen(title);
|
||||
}
|
||||
|
||||
void destroy_button(button_t *btn) {
|
||||
free(btn);
|
||||
}
|
||||
@@ -6,10 +6,15 @@
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
widget_t widget;
|
||||
char value;
|
||||
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();
|
||||
int32_t draw_button(WINDOW *win, button_t *btn, uint32_t cur_x, uint32_t cur_y);
|
||||
void destroy_button(button_t *btn);
|
||||
#endif
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "dialogue.h"
|
||||
#include "../app.h"
|
||||
#include "group.h"
|
||||
#include <ncurses.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
@@ -9,7 +10,7 @@ dialogue_t *init_dialogue(const char title[], const char text[],
|
||||
const uint32_t width, const uint32_t height) {
|
||||
dialogue_t *dialogue = malloc(sizeof(dialogue_t));
|
||||
dialogue->win = 0;
|
||||
dialogue->buttons = NULL;
|
||||
dialogue->ch_group = NULL;
|
||||
dialogue->w.x = width;
|
||||
dialogue->w.y = height;
|
||||
strcpy(dialogue->w.title, title);
|
||||
@@ -23,7 +24,6 @@ int32_t draw_dialogue(dialogue_t *d, uint32_t cur_x, uint32_t cur_y) {
|
||||
|
||||
d->win = newwin(d->w.y, d->w.x, d->w.m_y, d->w.m_x);
|
||||
|
||||
color_set(MODAL_COLOR_PAIR, NULL);
|
||||
wattrset(d->win, COLOR_PAIR(MODAL_COLOR_PAIR) | A_BOLD | A_REVERSE);
|
||||
|
||||
/* background */
|
||||
@@ -43,6 +43,8 @@ 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);
|
||||
wrefresh(d->win);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -4,11 +4,12 @@
|
||||
#include <ncurses.h>
|
||||
#include "../widget.h"
|
||||
#include "button.h"
|
||||
#include "group.h"
|
||||
|
||||
typedef struct {
|
||||
WINDOW *win;
|
||||
widget_t w;
|
||||
button_t *buttons; // ! should be NULL-terminated
|
||||
group_t *ch_group;
|
||||
char text[DIALOGUE_TEXT];
|
||||
} dialogue_t;
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "group.h"
|
||||
#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 *group = malloc(sizeof(group_t));
|
||||
|
||||
/* count elements */
|
||||
uint32_t count = 0;
|
||||
for (count = 0; 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);
|
||||
|
||||
/* init child elements */
|
||||
for (int i = 0; group->elements[i].type != w_end; i++) {
|
||||
group_el_t *el = &(group->elements)[i];
|
||||
switch (el->type) {
|
||||
case w_button:
|
||||
el->element = init_button();
|
||||
break;
|
||||
case w_box:
|
||||
case w_group:
|
||||
case w_end:
|
||||
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) {
|
||||
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);
|
||||
break;
|
||||
case w_box:
|
||||
case w_group:
|
||||
case w_end:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void destroy_group(group_t *group) {
|
||||
group_el_t *children = group->elements;
|
||||
for (int i = 0; children[i].type != w_end; i++) {
|
||||
group_el_t *el = &children[i];
|
||||
switch (el->type) {
|
||||
case w_button:
|
||||
destroy_button(el->element);
|
||||
break;
|
||||
case w_box:
|
||||
case w_group:
|
||||
case w_end:
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(group->elements);
|
||||
free(group);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef GROUP_H
|
||||
#define GROUP_H
|
||||
|
||||
#include "../widget.h"
|
||||
|
||||
enum g_direction {
|
||||
horizontal,
|
||||
vertical,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
void *element;
|
||||
enum w_type type;
|
||||
} group_el_t;
|
||||
|
||||
typedef struct {
|
||||
group_el_t *elements;
|
||||
widget_t w;
|
||||
uint32_t w_active_id;
|
||||
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);
|
||||
void destroy_group(group_t *group);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user