dialogue: count dimensions correctly
This commit is contained in:
@@ -7,9 +7,9 @@
|
||||
#include "../widget.h"
|
||||
#include "button.h"
|
||||
|
||||
button_t *init_button(WINDOW **win, uint32_t parent_id, char *label) {
|
||||
button_t *init_button(WINDOW **win, widget_t *w_parent, char *label) {
|
||||
button_t *btn = malloc(sizeof(button_t));
|
||||
init_widget(&(btn->w), win, label, parent_id);
|
||||
init_widget(&(btn->w), w_parent, win, label);
|
||||
btn->is_disabled = 0;
|
||||
btn->is_focused = 0;
|
||||
btn->is_hovered = 0;
|
||||
@@ -23,8 +23,8 @@ button_t *init_button(WINDOW **win, uint32_t parent_id, char *label) {
|
||||
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;
|
||||
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) {
|
||||
sprintf(title, "[< %s >]", btn->w.title);
|
||||
@@ -38,7 +38,7 @@ int32_t draw_button(button_t *btn) {
|
||||
wattrset(win, COLOR_PAIR(0) | A_REVERSE);
|
||||
}
|
||||
|
||||
mvwprintw(win, cur_y, cur_x, "%s", title);
|
||||
mvwprintw(win, margin_y, margin_x, "%s", title);
|
||||
|
||||
wattroff(win, A_BOLD | A_REVERSE);
|
||||
return strlen(title);
|
||||
|
||||
@@ -14,7 +14,7 @@ typedef struct {
|
||||
callback_t *callback;
|
||||
} button_t;
|
||||
|
||||
button_t *init_button(WINDOW **win, uint32_t parent_id, char *label);
|
||||
button_t *init_button(WINDOW **win, widget_t *w_parent, char *label);
|
||||
int32_t draw_button(button_t *btn);
|
||||
void destroy_button(button_t *btn);
|
||||
#endif
|
||||
+42
-15
@@ -1,30 +1,58 @@
|
||||
#include "dialogue.h"
|
||||
#include "../app.h"
|
||||
#include "group.h"
|
||||
#include <ncurses.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
dialogue_t *init_dialogue(const char title[], const char text[],
|
||||
const uint32_t width, const uint32_t height) {
|
||||
#include "../app.h"
|
||||
#include "dialogue.h"
|
||||
#include "group.h"
|
||||
|
||||
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 = width;
|
||||
dialogue->w.y = height;
|
||||
dialogue->w.x = 0;
|
||||
dialogue->w.y = 0;
|
||||
strcpy(dialogue->w.title, title);
|
||||
strcpy(dialogue->text, text);
|
||||
return dialogue;
|
||||
}
|
||||
|
||||
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;
|
||||
int32_t draw_dialogue(void *_app, dialogue_t *d) {
|
||||
app_t *app = (app_t *)_app;
|
||||
|
||||
d->win = newwin(d->w.y, d->w.x, d->w.m_y, d->w.m_x);
|
||||
/* count dimensions */
|
||||
uint32_t x = 1; /* when uses box */
|
||||
uint32_t y = 1; /* when uses box */
|
||||
|
||||
uint32_t line_count = 0;
|
||||
uint32_t line_max_len = 0;
|
||||
line_max_len = get_max_line_len(d->text, &line_count);
|
||||
y += line_count;
|
||||
y += 2; /* margin for text */
|
||||
|
||||
if (d->ch_group) {
|
||||
d->ch_group->w.m_y = y;
|
||||
if (line_max_len < d->ch_group->w.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 += 1; /* when uses box */
|
||||
y += 1; /* when uses box */
|
||||
d->w.x = x;
|
||||
d->w.y = y;
|
||||
d->w.m_x = (app->cur_x - x) / 2;
|
||||
d->w.m_y = (app->cur_y - y) / 2;
|
||||
|
||||
/* render window */
|
||||
d->win = newwin(y, x, d->w.m_y, d->w.m_x);
|
||||
wattrset(d->win, COLOR_PAIR(0) | A_BOLD | A_REVERSE);
|
||||
|
||||
/* background */
|
||||
@@ -42,10 +70,9 @@ int32_t draw_dialogue(void *app, dialogue_t *d, uint32_t cur_x, uint32_t cur_y)
|
||||
mvwprintw(d->win, 0, title_mx_pos + strlen(d->w.title), " ");
|
||||
|
||||
/* text */
|
||||
print_multiline_text(d->win, d->text, d->w.x, 1, 1, PMT_ALIGN_CENTER);
|
||||
wattroff(d->win, A_BOLD);
|
||||
print_multiline_text(d->win, d->text, d->w.x, 2, 1, PMT_ALIGN_CENTER);
|
||||
wattroff(d->win, A_REVERSE);
|
||||
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);
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
#ifndef DIALOGUE_H
|
||||
#define DIALOGUE_H
|
||||
|
||||
#include <ncurses.h>
|
||||
#include "../widget.h"
|
||||
#include "button.h"
|
||||
#include "group.h"
|
||||
#include <ncurses.h>
|
||||
|
||||
typedef struct {
|
||||
WINDOW *win;
|
||||
@@ -13,8 +13,8 @@ typedef struct {
|
||||
char text[DIALOGUE_TEXT];
|
||||
} dialogue_t;
|
||||
|
||||
dialogue_t *init_dialogue(const char title[], const char text[], const uint32_t w, const uint32_t h);
|
||||
int32_t draw_dialogue(void *app, dialogue_t *d, const uint32_t cur_x, const uint32_t cur_y);
|
||||
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);
|
||||
|
||||
#endif
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
group_t *init_group(WINDOW **win, uint32_t par_id, group_el_init_t *children,
|
||||
group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
|
||||
enum g_direction direction) {
|
||||
group_t *group = malloc(sizeof(group_t));
|
||||
|
||||
@@ -12,8 +12,8 @@ group_t *init_group(WINDOW **win, uint32_t par_id, group_el_init_t *children,
|
||||
uint32_t count = 0;
|
||||
for (; children[count].type != w_end; count++)
|
||||
;
|
||||
count++; /* TODO: improve counting */
|
||||
group->elements = calloc(count, sizeof(group_el_t));
|
||||
group->w.w_parent = w_parent;
|
||||
group_el_t *elements = group->elements;
|
||||
for (int i = 0; i < count; i++) {
|
||||
elements[i].type = children[i].type;
|
||||
@@ -23,7 +23,7 @@ group_t *init_group(WINDOW **win, uint32_t par_id, group_el_init_t *children,
|
||||
for (int i = 0; elements[i].type != w_end; i++) {
|
||||
switch (elements[i].type) {
|
||||
case w_button:
|
||||
elements[i].element = init_button(win, par_id, children[i].label);
|
||||
elements[i].element = init_button(win, &(group->w), children[i].label);
|
||||
if (direction == horizontal) {
|
||||
button_t *b = (button_t *)elements[i].element;
|
||||
b->w.m_x = group->w.x;
|
||||
@@ -40,7 +40,7 @@ group_t *init_group(WINDOW **win, uint32_t par_id, group_el_init_t *children,
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return group;
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ typedef struct {
|
||||
enum g_direction direction;
|
||||
} group_t;
|
||||
|
||||
group_t *init_group(WINDOW **win, uint32_t par_id, group_el_init_t *children,
|
||||
group_t *init_group(WINDOW **win, widget_t *w_parent, group_el_init_t *children,
|
||||
enum g_direction dir);
|
||||
void draw_group(WINDOW *win, group_t *group);
|
||||
void destroy_group(group_t *group);
|
||||
|
||||
Reference in New Issue
Block a user