aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-11-22 12:51:10 +0100
committerMarvin Borner2020-11-22 12:51:10 +0100
commit11568b08115cd0a7fe17608599abdc8a80a4ec50 (patch)
tree82df8168992fe1f938ff41dcbc661981b398ff6a
parentc0decce503f945ac5883b811443ddca44a7525b9 (diff)
Added percentage-based GUI system
-rw-r--r--apps/browser.c23
-rw-r--r--apps/exec.c6
-rw-r--r--apps/window.c7
-rw-r--r--libgui/Makefile2
-rw-r--r--libgui/gui.c33
-rw-r--r--libgui/inc/gui.h6
6 files changed, 41 insertions, 36 deletions
diff --git a/apps/browser.c b/apps/browser.c
index c51b0a4..bdc7b14 100644
--- a/apps/browser.c
+++ b/apps/browser.c
@@ -14,24 +14,11 @@
#define HEIGHT 400
#define FONT_HEIGHT 24
#define LABEL_WIDTH 36 // Thx Lars
-#define BORDER 2
static struct element *root;
static struct element *code_label;
static struct element *output;
-// Temporary: Will be moved to libnet
-char **dns_split(char *url, char **buf)
-{
- strchr(url, '.')[0] = '\0';
- char *first = url;
- char *second = url + strlen(url) + 1;
- buf[0] = first;
- buf[1] = second;
-
- return buf;
-}
-
u32 status_color(char *http_code)
{
u32 c = 0;
@@ -99,13 +86,11 @@ void on_submit(void *event, struct element *box)
int main()
{
// TODO: Dynamic element positioning
- root = gui_init("browser", WIDTH + 2 * BORDER, HEIGHT + 2 * BORDER, COLOR_BG);
- code_label = gui_add_label(root, BORDER, BORDER, FONT_24, "000", COLOR_BLACK, COLOR_WHITE);
+ root = gui_init("browser", WIDTH, HEIGHT, COLOR_BG);
+ code_label = gui_add_label(root, 0, 0, FONT_24, "000", COLOR_BLACK, COLOR_WHITE);
struct element *text_input =
- gui_add_text_input(root, LABEL_WIDTH + 2 * BORDER, BORDER,
- WIDTH - LABEL_WIDTH - BORDER, FONT_24, COLOR_WHITE, COLOR_BLACK);
- output = gui_add_text_box(root, BORDER, FONT_HEIGHT + 2 * BORDER, WIDTH,
- HEIGHT - FONT_HEIGHT - BORDER, FONT_16,
+ gui_add_text_input(root, LABEL_WIDTH, 0, 100, FONT_24, COLOR_WHITE, COLOR_BLACK);
+ output = gui_add_text_box(root, 0, FONT_HEIGHT + 2, 100, 100, FONT_16,
"Enter URL and press Enter :)", COLOR_WHITE, COLOR_BLACK);
text_input->event.on_submit = on_submit;
diff --git a/apps/exec.c b/apps/exec.c
index 68f9e63..51aef91 100644
--- a/apps/exec.c
+++ b/apps/exec.c
@@ -10,7 +10,6 @@
#define HEIGHT 32
#define WIDTH 300
-#define BORDER 2
void on_submit(struct gui_event_keyboard *event, struct element *elem)
{
@@ -37,10 +36,9 @@ void on_submit(struct gui_event_keyboard *event, struct element *elem)
int main()
{
- struct element *root =
- gui_init("Exec", WIDTH + BORDER * 2, HEIGHT + BORDER * 2, COLOR_BLACK);
+ struct element *root = gui_init("Exec", WIDTH, HEIGHT, COLOR_BLACK);
struct element *input =
- gui_add_text_input(root, BORDER, BORDER, WIDTH, FONT_32, COLOR_WHITE, COLOR_BLACK);
+ gui_add_text_input(root, 0, 0, 100, FONT_32, COLOR_WHITE, COLOR_BLACK);
input->event.on_submit = on_submit;
diff --git a/apps/window.c b/apps/window.c
index 7e1b4be..835a978 100644
--- a/apps/window.c
+++ b/apps/window.c
@@ -15,15 +15,12 @@ void on_click()
int main()
{
- /* print("[test context loaded]\n"); */
-
struct element *root = gui_init("test", 600, 400, COLOR_BG);
- struct element *container =
- gui_add_container(root, 0, 0, root->ctx->width / 2, root->ctx->height, COLOR_RED);
+ struct element *container = gui_add_container(root, 0, 0, 50, 100, COLOR_RED);
struct element *button = gui_add_button(container, 10, 10, FONT_24, strdup("Button"),
COLOR_WHITE, COLOR_BLACK);
struct element *text_input =
- gui_add_text_input(container, 10, 50, 200, FONT_24, COLOR_WHITE, COLOR_BLACK);
+ gui_add_text_input(container, 10, 50, 70, FONT_24, COLOR_WHITE, COLOR_BLACK);
(void)text_input;
button->event.on_click = on_click;
diff --git a/libgui/Makefile b/libgui/Makefile
index 7996c29..dc254d3 100644
--- a/libgui/Makefile
+++ b/libgui/Makefile
@@ -10,7 +10,7 @@ LD = ccache ../cross/opt/bin/i686-elf-ld
AR = ccache ../cross/opt/bin/i686-elf-ar
WARNINGS = -Wall -Wextra -pedantic-errors -Wshadow -Wpointer-arith -Wwrite-strings -Wredundant-decls -Wnested-externs -Wno-long-long
-CFLAGS = $(WARNINGS) -nostdlib -nostdinc -fno-builtin -mgeneral-regs-only -std=c99 -m32 -Iinc/ -I../libc/inc/ -fPIE -Duserspace -Ofast
+CFLAGS = $(WARNINGS) -nostdlib -nostdinc -fno-builtin -std=c99 -m32 -Iinc/ -I../libc/inc/ -fPIE -Duserspace -Ofast
all: libgui
diff --git a/libgui/gui.c b/libgui/gui.c
index 50c81c5..943c5d7 100644
--- a/libgui/gui.c
+++ b/libgui/gui.c
@@ -15,6 +15,8 @@
// TODO: Use list (and add index-based access)
#define MAX_WINDOWS 10
+#define BORDER 2
+#define PERCENTAGE(c, e) ((u32)((double)(c) * (double)(e) / 100.0))
u32 window_count = 0;
static struct window windows[MAX_WINDOWS] = { 0 };
@@ -208,6 +210,7 @@ struct element *gui_add_button(struct element *container, int x, int y, enum fon
button->ctx->width = strlen(text) * gfx_font_width(font_type);
button->ctx->height = gfx_font_height(font_type);
button->ctx->flags = WF_RELATIVE;
+ button->parent = container;
button->childs = list_new();
button->data = malloc(sizeof(struct element_button));
((struct element_button *)button->data)->text = strdup(text);
@@ -237,6 +240,7 @@ struct element *gui_add_label(struct element *container, int x, int y, enum font
label->ctx->width = strlen(text) * gfx_font_width(font_type);
label->ctx->height = gfx_font_height(font_type);
label->ctx->flags = WF_RELATIVE;
+ label->parent = container;
label->childs = list_new();
label->data = malloc(sizeof(struct element_label));
((struct element_label *)label->data)->text = strdup(text);
@@ -264,11 +268,14 @@ struct element *gui_add_text_box(struct element *container, int x, int y, u32 wi
text_box->ctx = malloc(sizeof(*text_box->ctx));
text_box->ctx->x = x;
text_box->ctx->y = y;
- text_box->ctx->width = width;
- text_box->ctx->height = height;
+ text_box->ctx->width = PERCENTAGE(container->ctx->width, width);
+ text_box->ctx->height = PERCENTAGE(container->ctx->height, height);
text_box->ctx->flags = WF_RELATIVE;
+ text_box->parent = container;
text_box->childs = list_new();
text_box->data = malloc(sizeof(struct element_text_box));
+ ((struct element_text_box *)text_box->data)->width = width;
+ ((struct element_text_box *)text_box->data)->height = height;
((struct element_text_box *)text_box->data)->text = strdup(text);
((struct element_text_box *)text_box->data)->color_fg = color_fg;
((struct element_text_box *)text_box->data)->color_bg = color_bg;
@@ -293,11 +300,13 @@ struct element *gui_add_text_input(struct element *container, int x, int y, u32
text_input->ctx = malloc(sizeof(*text_input->ctx));
text_input->ctx->x = x;
text_input->ctx->y = y;
- text_input->ctx->width = width;
+ text_input->ctx->width = PERCENTAGE(container->ctx->width, width);
text_input->ctx->height = gfx_font_height(font_type);
text_input->ctx->flags = WF_RELATIVE;
+ text_input->parent = container;
text_input->childs = list_new();
text_input->data = malloc(sizeof(struct element_text_input));
+ ((struct element_text_input *)text_input->data)->width = width;
((struct element_text_input *)text_input->data)->color_fg = color_fg;
((struct element_text_input *)text_input->data)->color_bg = color_bg;
((struct element_text_input *)text_input->data)->font_type = font_type;
@@ -321,11 +330,14 @@ struct element *gui_add_container(struct element *container, int x, int y, u32 w
new_container->ctx = malloc(sizeof(*new_container->ctx));
new_container->ctx->x = x;
new_container->ctx->y = y;
- new_container->ctx->width = width;
- new_container->ctx->height = height;
+ new_container->ctx->width = PERCENTAGE(container->ctx->width, width);
+ new_container->ctx->height = PERCENTAGE(container->ctx->height, height);
new_container->ctx->flags = WF_RELATIVE;
+ new_container->parent = container;
new_container->childs = list_new();
new_container->data = malloc(sizeof(struct element_container));
+ ((struct element_container *)new_container->data)->width = width;
+ ((struct element_container *)new_container->data)->height = height;
((struct element_container *)new_container->data)->color_bg = color_bg;
((struct element_container *)new_container->data)->flags = 0;
@@ -431,7 +443,8 @@ struct element *gui_init(const char *title, u32 width, u32 height, u32 color_bg)
return NULL;
// TODO: Add center flag
- struct window *win = new_window(title, 30, 30, width, height, WF_DEFAULT);
+ struct window *win =
+ new_window(title, 30, 30, width + BORDER * 2, height + BORDER * 2, WF_DEFAULT);
if (!win)
return NULL;
@@ -445,5 +458,11 @@ struct element *gui_init(const char *title, u32 width, u32 height, u32 color_bg)
container->data = NULL;
list_add(win->childs, container);
- return container;
+ struct element *root = gui_add_container(container, BORDER, BORDER, 100, 100, COLOR_BLACK);
+ if (!root)
+ return NULL;
+ root->ctx->width -= BORDER * 2;
+ root->ctx->height -= BORDER * 2;
+
+ return root;
}
diff --git a/libgui/inc/gui.h b/libgui/inc/gui.h
index ec5da95..8e925d6 100644
--- a/libgui/inc/gui.h
+++ b/libgui/inc/gui.h
@@ -32,6 +32,8 @@ struct element_event {
};
struct element_container {
+ u32 width;
+ u32 height;
u32 color_bg;
enum container_flags flags;
};
@@ -52,6 +54,8 @@ struct element_label {
struct element_text_box {
char *text;
+ u32 width;
+ u32 height;
u32 color_fg;
u32 color_bg;
enum font_type font_type;
@@ -59,6 +63,7 @@ struct element_text_box {
struct element_text_input {
char text[MAX_INPUT_LENGTH];
+ u32 width;
u32 color_fg;
u32 color_bg;
enum font_type font_type;
@@ -70,6 +75,7 @@ struct element {
struct context *ctx; // Coordinates are relative to container
struct element_event event;
void *attributes;
+ struct element *parent;
struct list *childs;
void *data; // Who needs static types anyways :)
};