aboutsummaryrefslogtreecommitdiff
path: root/libgui/gui.c
diff options
context:
space:
mode:
Diffstat (limited to 'libgui/gui.c')
-rw-r--r--libgui/gui.c59
1 files changed, 56 insertions, 3 deletions
diff --git a/libgui/gui.c b/libgui/gui.c
index 8ed79af..7ea341c 100644
--- a/libgui/gui.c
+++ b/libgui/gui.c
@@ -4,6 +4,7 @@
#include <def.h>
#include <gfx.h>
#include <gui.h>
+#include <list.h>
#include <mem.h>
#define MAX_WINDOWS 10
@@ -23,8 +24,10 @@ struct window *new_window(const char *title, int x, int y, u32 width, u32 height
win->ctx->width = width > 0 ? width : 600;
win->ctx->height = height > 0 ? height : 400;
win->ctx->flags = flags;
+ win->id = window_count + 1;
win->title = title;
- gfx_new_ctx(windows[window_count + 1].ctx);
+ win->childs = list_new();
+ gfx_new_ctx(win->ctx);
if (!win->ctx->fb)
return NULL;
@@ -34,7 +37,49 @@ struct window *new_window(const char *title, int x, int y, u32 width, u32 height
return win;
}
-struct window *gui_init(const char *title, u32 width, u32 height)
+void merge_elements(struct element *container)
+{
+ if (!container->childs || !container->childs->head)
+ return;
+
+ struct node *iterator = container->childs->head;
+ while (iterator != NULL) {
+ struct element *elem = iterator->data;
+ struct context *ctx = elem->ctx;
+ merge_elements(elem);
+ gfx_ctx_on_ctx(container->ctx, ctx, ctx->x, ctx->y);
+ iterator = iterator->next;
+ }
+}
+
+struct element *gui_add_button(struct element *container, int x, int y, u32 width, u32 height,
+ const char *text, u32 color)
+{
+ if (!container || !container->childs)
+ return NULL;
+
+ struct element *button = malloc(sizeof(*button));
+ button->type = GUI_TYPE_BUTTON;
+ button->window_id = container->window_id;
+ button->ctx = malloc(sizeof(*button->ctx));
+ button->ctx->x = x;
+ button->ctx->y = y;
+ button->ctx->width = width;
+ button->ctx->height = height;
+ button->ctx->flags = WF_RELATIVE;
+ button->childs = list_new();
+ button->data = malloc(sizeof(struct element_button));
+ ((struct element_button *)button->data)->text = text;
+ ((struct element_button *)button->data)->color = color;
+ gfx_new_ctx(button->ctx);
+ gfx_fill(button->ctx, color);
+ list_add(container->childs, button);
+ merge_elements(container);
+
+ return button;
+}
+
+struct element *gui_init(const char *title, u32 width, u32 height)
{
if (window_count != 0)
return NULL;
@@ -46,5 +91,13 @@ struct window *gui_init(const char *title, u32 width, u32 height)
gfx_fill(win->ctx, COLOR_BG);
gfx_init("/font/spleen-12x24.psfu");
- return win;
+ struct element *container = malloc(sizeof(*container));
+ container->type = GUI_TYPE_CONTAINER;
+ container->window_id = win->id;
+ container->ctx = win->ctx;
+ container->childs = list_new();
+ container->data = NULL;
+ list_add(win->childs, container);
+
+ return container;
}