From cf1a6ed2998eb22b112f233d65975c27fa5ced5b Mon Sep 17 00:00:00 2001
From: Marvin Borner
Date: Sun, 23 May 2021 23:19:26 +0200
Subject: Added title to app bar

---
 libs/libgui/gfx.c | 42 +++++++++++++++++++++---------------------
 libs/libgui/gfx.h | 51 ++++++++++++++++++++++++++++++++++-----------------
 libs/libgui/gui.c | 24 +++++++++++-------------
 libs/libgui/gui.h |  4 ++--
 libs/libgui/msg.h |  3 ++-
 libs/libgui/psf.c |  4 ++--
 libs/libgui/psf.h |  2 +-
 7 files changed, 73 insertions(+), 57 deletions(-)

(limited to 'libs/libgui')

diff --git a/libs/libgui/gfx.c b/libs/libgui/gfx.c
index 12c0e44..8bdea87 100644
--- a/libs/libgui/gfx.c
+++ b/libs/libgui/gfx.c
@@ -24,7 +24,7 @@
 #define FONT_32_PATH "/font/spleen-16x32.psfu"
 #define FONT_64_PATH "/font/spleen-32x64.psfu"
 
-struct font *fonts[FONT_COUNT] = { 0 };
+struct gfx_font *fonts[FONT_COUNT] = { 0 };
 
 static void load_font(enum font_type font_type)
 {
@@ -70,7 +70,7 @@ static void free_fonts(void)
 	}
 }
 
-static void write_char(struct context *ctx, vec2 pos, struct font *font, u32 c, char ch)
+static void write_char(struct gfx_context *ctx, vec2 pos, struct gfx_font *font, u32 c, char ch)
 {
 	u8 bypp = ctx->bpp >> 3;
 
@@ -92,7 +92,7 @@ static void write_char(struct context *ctx, vec2 pos, struct font *font, u32 c,
 	}
 }
 
-struct context *gfx_new_ctx(struct context *ctx, vec2 size, u8 bpp)
+struct gfx_context *gfx_new_ctx(struct gfx_context *ctx, vec2 size, u8 bpp)
 {
 	ctx->size = size;
 	ctx->bpp = bpp;
@@ -104,7 +104,7 @@ struct context *gfx_new_ctx(struct context *ctx, vec2 size, u8 bpp)
 
 // On-demand font loading
 static u8 fonts_loaded = 0;
-struct font *gfx_resolve_font(enum font_type font_type)
+struct gfx_font *gfx_resolve_font(enum font_type font_type)
 {
 	if (!fonts_loaded) {
 		fonts_loaded = 1;
@@ -116,15 +116,15 @@ struct font *gfx_resolve_font(enum font_type font_type)
 	return fonts[font_type];
 }
 
-void gfx_write_char(struct context *ctx, vec2 pos, enum font_type font_type, u32 c, char ch)
+void gfx_write_char(struct gfx_context *ctx, vec2 pos, enum font_type font_type, u32 c, char ch)
 {
-	struct font *font = gfx_resolve_font(font_type);
+	struct gfx_font *font = gfx_resolve_font(font_type);
 	write_char(ctx, pos, font, c, ch);
 }
 
-void gfx_write(struct context *ctx, vec2 pos, enum font_type font_type, u32 c, const char *text)
+void gfx_write(struct gfx_context *ctx, vec2 pos, enum font_type font_type, u32 c, const char *text)
 {
-	struct font *font = gfx_resolve_font(font_type);
+	struct gfx_font *font = gfx_resolve_font(font_type);
 	u32 cnt = 0;
 	for (u32 i = 0; i < strlen(text); i++) {
 		// TODO: Should this be here?
@@ -204,7 +204,7 @@ static void gfx_image_cache_save(const char *path, struct bmp *bmp)
 	list_add(gfx_image_cache_list, cache);
 }
 
-void gfx_load_image_filter(struct context *ctx, vec2 pos, enum gfx_filter filter, const char *path)
+void gfx_load_image_filter(struct gfx_context *ctx, vec2 pos, enum gfx_filter filter, const char *path)
 {
 	// TODO: Detect image type
 
@@ -252,17 +252,17 @@ void gfx_load_image_filter(struct context *ctx, vec2 pos, enum gfx_filter filter
 	}
 }
 
-void gfx_load_image(struct context *ctx, vec2 pos, const char *path)
+void gfx_load_image(struct gfx_context *ctx, vec2 pos, const char *path)
 {
 	gfx_load_image_filter(ctx, pos, GFX_FILTER_NONE, path);
 }
 
-void gfx_load_wallpaper(struct context *ctx, const char *path)
+void gfx_load_wallpaper(struct gfx_context *ctx, const char *path)
 {
 	gfx_load_image(ctx, vec2(0, 0), path);
 }
 
-void gfx_draw_pixel(struct context *ctx, vec2 pos1, u32 c)
+void gfx_draw_pixel(struct gfx_context *ctx, vec2 pos1, u32 c)
 {
 	u8 bypp = ctx->bpp >> 3;
 	u8 *draw = &ctx->fb[pos1.x * bypp + pos1.y * ctx->pitch];
@@ -272,7 +272,7 @@ void gfx_draw_pixel(struct context *ctx, vec2 pos1, u32 c)
 	draw[3] = GET_ALPHA(c);
 }
 
-void gfx_draw_rectangle(struct context *ctx, vec2 pos1, vec2 pos2, u32 c)
+void gfx_draw_rectangle(struct gfx_context *ctx, vec2 pos1, vec2 pos2, u32 c)
 {
 	assert(pos1.x <= pos2.x && pos1.y <= pos2.y);
 	u8 bypp = ctx->bpp >> 3;
@@ -288,7 +288,7 @@ void gfx_draw_rectangle(struct context *ctx, vec2 pos1, vec2 pos2, u32 c)
 	}
 }
 
-void gfx_draw_border(struct context *ctx, u32 width, u32 c)
+void gfx_draw_border(struct gfx_context *ctx, u32 width, u32 c)
 {
 	if (width <= 0)
 		return;
@@ -311,7 +311,7 @@ void gfx_draw_border(struct context *ctx, u32 width, u32 c)
 
 // Using Bresenham's algorithm
 // TODO: Better line scaling
-void gfx_draw_line(struct context *ctx, vec2 pos1, vec2 pos2, u32 scale, u32 c)
+void gfx_draw_line(struct gfx_context *ctx, vec2 pos1, vec2 pos2, u32 scale, u32 c)
 {
 	int dx = ABS(pos2.x - pos1.x), sx = pos1.x < pos2.x ? 1 : -1;
 	int dy = ABS(pos2.y - pos1.y), sy = pos1.y < pos2.y ? 1 : -1;
@@ -333,7 +333,7 @@ void gfx_draw_line(struct context *ctx, vec2 pos1, vec2 pos2, u32 scale, u32 c)
 	}
 }
 
-void gfx_copy(struct context *dest, struct context *src, vec2 pos, vec2 size)
+void gfx_copy(struct gfx_context *dest, struct gfx_context *src, vec2 pos, vec2 size)
 {
 	u8 bypp = dest->bpp >> 3;
 	u8 *srcfb = &src->fb[pos.x * bypp + pos.y * src->pitch];
@@ -347,7 +347,7 @@ void gfx_copy(struct context *dest, struct context *src, vec2 pos, vec2 size)
 
 // TODO: Support alpha values other than 0x0 and 0xff (blending)
 // TODO: Optimize!
-HOT void gfx_ctx_on_ctx(struct context *dest, struct context *src, vec2 pos, u8 alpha)
+HOT void gfx_ctx_on_ctx(struct gfx_context *dest, struct gfx_context *src, vec2 pos, u8 alpha)
 {
 	// TODO: Some kind of alpha-acknowledging memcpy?
 	if (!alpha && src->size.x == dest->size.x && src->size.y == dest->size.y) {
@@ -377,24 +377,24 @@ HOT void gfx_ctx_on_ctx(struct context *dest, struct context *src, vec2 pos, u8
 	}
 }
 
-void gfx_clear(struct context *ctx)
+void gfx_clear(struct gfx_context *ctx)
 {
 	memset(ctx->fb, 0, ctx->bytes);
 }
 
-void gfx_fill(struct context *ctx, u32 c)
+void gfx_fill(struct gfx_context *ctx, u32 c)
 {
 	gfx_draw_rectangle(ctx, vec2(0, 0), vec2(ctx->size.x, ctx->size.y), c);
 }
 
 int gfx_font_height(enum font_type font_type)
 {
-	struct font *font = gfx_resolve_font(font_type);
+	struct gfx_font *font = gfx_resolve_font(font_type);
 	return font->size.y;
 }
 
 int gfx_font_width(enum font_type font_type)
 {
-	struct font *font = gfx_resolve_font(font_type);
+	struct gfx_font *font = gfx_resolve_font(font_type);
 	return font->size.x;
 }
diff --git a/libs/libgui/gfx.h b/libs/libgui/gfx.h
index 7c069f4..855e060 100644
--- a/libs/libgui/gfx.h
+++ b/libs/libgui/gfx.h
@@ -48,7 +48,19 @@
 #define GFX_NON_ALPHA 0
 #define GFX_ALPHA 1
 
+/**
+ * Useful macros
+ */
+
 #define GFX_CENTER_IN(a, b) (ABS((a) - (b)) / 2)
+#define GFX_RECT(pos, size) ((struct gfx_rect){ .pos = (pos), .size = (size) })
+#define GFX_IN_RECT(rect, p)                                                                       \
+	((p).x >= (rect).pos.x && (p).x < (rect).pos.x + (rect).size.x && (p).y >= (rect).pos.y && \
+	 (p).y < (rect).pos.y + (rect).size.y)
+
+/**
+ * Structures
+ */
 
 enum font_type { FONT_8, FONT_12, FONT_16, FONT_24, FONT_32, FONT_64 };
 enum gfx_filter {
@@ -57,14 +69,14 @@ enum gfx_filter {
 };
 
 // Generalized font struct
-struct font {
+struct gfx_font {
 	void *raw;
 	char *chars;
 	vec2 size;
 	int char_size;
 };
 
-struct context {
+struct gfx_context {
 	vec2 size;
 	u8 *fb;
 	u32 bpp;
@@ -72,16 +84,21 @@ struct context {
 	u32 bytes;
 };
 
-struct context *gfx_new_ctx(struct context *ctx, vec2 size, u8 bpp) NONNULL;
+struct gfx_rect {
+	vec2 pos; // Upper left
+	vec2 size;
+};
+
+struct gfx_context *gfx_new_ctx(struct gfx_context *ctx, vec2 size, u8 bpp) NONNULL;
 
 /**
  * Text stuff
  */
 
-struct font *gfx_resolve_font(enum font_type font_type);
-void gfx_write_char(struct context *ctx, vec2 pos, enum font_type font_type, u32 c,
+struct gfx_font *gfx_resolve_font(enum font_type font_type);
+void gfx_write_char(struct gfx_context *ctx, vec2 pos, enum font_type font_type, u32 c,
 		    char ch) NONNULL;
-void gfx_write(struct context *ctx, vec2 pos, enum font_type font_type, u32 c,
+void gfx_write(struct gfx_context *ctx, vec2 pos, enum font_type font_type, u32 c,
 	       const char *text) NONNULL;
 
 int gfx_font_height(enum font_type);
@@ -91,28 +108,28 @@ int gfx_font_width(enum font_type);
  * Image loading
  */
 
-void gfx_load_image(struct context *ctx, vec2 pos, const char *path) NONNULL;
-void gfx_load_image_filter(struct context *ctx, vec2 pos, enum gfx_filter filter,
+void gfx_load_image(struct gfx_context *ctx, vec2 pos, const char *path) NONNULL;
+void gfx_load_image_filter(struct gfx_context *ctx, vec2 pos, enum gfx_filter filter,
 			   const char *path) NONNULL;
-void gfx_load_wallpaper(struct context *ctx, const char *path) NONNULL;
+void gfx_load_wallpaper(struct gfx_context *ctx, const char *path) NONNULL;
 
 /**
  * Context copying
  */
 
-void gfx_copy(struct context *dest, struct context *src, vec2 pos, vec2 size) NONNULL;
-void gfx_ctx_on_ctx(struct context *dest, struct context *src, vec2 pos, u8 alpha) NONNULL;
+void gfx_copy(struct gfx_context *dest, struct gfx_context *src, vec2 pos, vec2 size) NONNULL;
+void gfx_ctx_on_ctx(struct gfx_context *dest, struct gfx_context *src, vec2 pos, u8 alpha) NONNULL;
 
 /**
  * Drawing functions
  */
 
-void gfx_draw_pixel(struct context *ctx, vec2 pos1, u32 c);
-void gfx_draw_rectangle(struct context *ctx, vec2 pos1, vec2 pos2, u32 c) NONNULL;
-void gfx_draw_line(struct context *ctx, vec2 pos1, vec2 pos2, u32 scale, u32 c);
+void gfx_draw_pixel(struct gfx_context *ctx, vec2 pos1, u32 c);
+void gfx_draw_rectangle(struct gfx_context *ctx, vec2 pos1, vec2 pos2, u32 c) NONNULL;
+void gfx_draw_line(struct gfx_context *ctx, vec2 pos1, vec2 pos2, u32 scale, u32 c);
 
-void gfx_clear(struct context *ctx);
-void gfx_fill(struct context *ctx, u32 c) NONNULL;
-void gfx_draw_border(struct context *ctx, u32 width, u32 c) NONNULL;
+void gfx_clear(struct gfx_context *ctx);
+void gfx_fill(struct gfx_context *ctx, u32 c) NONNULL;
+void gfx_draw_border(struct gfx_context *ctx, u32 width, u32 c) NONNULL;
 
 #endif
diff --git a/libs/libgui/gui.c b/libs/libgui/gui.c
index b53c366..cc45bfb 100644
--- a/libs/libgui/gui.c
+++ b/libs/libgui/gui.c
@@ -11,9 +11,8 @@
 struct gui_widget {
 	u32 id;
 	vec2 pos;
-	/* struct context ctx; */
-	struct context fg;
-	struct context bg;
+	struct gfx_context fg;
+	struct gfx_context bg;
 	struct list *children;
 
 	struct {
@@ -26,7 +25,7 @@ struct gui_window {
 	u32 id;
 	vec2 off; // fb offset
 	vec2 pos;
-	struct context ctx;
+	struct gfx_context ctx;
 	struct list *widgets;
 };
 
@@ -392,8 +391,8 @@ static vec2 gui_offset_widget(struct gui_widget *parent, struct gui_widget *chil
 static struct gui_widget *gui_new_plain_widget(vec2 pos, vec2 size, u8 bpp)
 {
 	struct gui_widget *widget = zalloc(sizeof(*widget));
-	struct context bg;
-	struct context fg;
+	struct gfx_context bg;
+	struct gfx_context fg;
 
 	static u32 id = 0;
 	widget->id = id++;
@@ -490,8 +489,7 @@ void gui_popup(const char *text)
 {
 	vec2 pos = vec2(200, 200);
 
-	u32 popup;
-	gui_new_custom_window(&popup, pos, vec2(POPUP_WIDTH, POPUP_HEIGHT));
+	u32 popup = gui_new_custom_window("Popup", pos, vec2(POPUP_WIDTH, POPUP_HEIGHT));
 	struct gui_window *win = gui_window_by_id(popup);
 	gui_fill(popup, gui_main_widget_id(win), GUI_LAYER_BG, COLOR_WHITE);
 
@@ -518,7 +516,7 @@ vec2 gui_window_size(u32 win_id)
  * Window manager interfaces
  */
 
-void gui_new_custom_window(u32 *id, vec2 pos, vec2 size)
+u32 gui_new_custom_window(const char *name, vec2 pos, vec2 size)
 {
 	if (!windows)
 		windows = list_new();
@@ -533,6 +531,7 @@ void gui_new_custom_window(u32 *id, vec2 pos, vec2 size)
 	struct message_new_window msg = { .header.state = MSG_NEED_ANSWER,
 					  .pos = pos,
 					  .size = size };
+	strlcpy(msg.name, name, sizeof(msg.name));
 
 	gui_connect_wm();
 	if (msg_send(GUI_NEW_WINDOW, &msg, sizeof(msg)) > 0 && msg_receive(&msg, sizeof(msg)) > 0 &&
@@ -558,16 +557,15 @@ void gui_new_custom_window(u32 *id, vec2 pos, vec2 size)
 		list_add(win->widgets,
 			 gui_new_plain_widget(vec2(0, 0), win->ctx.size, win->ctx.bpp));
 
-		*id = win->id;
-		return;
+		return win->id;
 	}
 
 	gui_error(EINVAL);
 }
 
-INLINE void gui_new_window(u32 *id)
+u32 gui_new_window(const char *name)
 {
-	gui_new_custom_window(id, vec2(0, 0), vec2(0, 0));
+	return gui_new_custom_window(name, vec2(0, 0), vec2(0, 0));
 }
 
 void gui_redraw_window_only(u32 id)
diff --git a/libs/libgui/gui.h b/libs/libgui/gui.h
index d9087f1..c3e1d10 100644
--- a/libs/libgui/gui.h
+++ b/libs/libgui/gui.h
@@ -36,8 +36,8 @@ struct gui_event_mouse {
  * Window operations
  */
 
-void gui_new_custom_window(u32 *id, vec2 pos, vec2 size);
-void gui_new_window(u32 *id);
+u32 gui_new_custom_window(const char *name, vec2 pos, vec2 size);
+u32 gui_new_window(const char *name);
 void gui_redraw_window(u32 id);
 void gui_redraw_window_only(u32 id); // Without widgets
 
diff --git a/libs/libgui/msg.h b/libs/libgui/msg.h
index 71491c4..2684ab3 100644
--- a/libs/libgui/msg.h
+++ b/libs/libgui/msg.h
@@ -34,12 +34,13 @@ struct message_ping_window {
 
 struct message_new_window {
 	struct message_header header;
+	char name[64];
 	u32 id;
 	u32 shid;
 	vec2 off;
 	vec2 size;
 	vec2 pos;
-	struct context ctx;
+	struct gfx_context ctx;
 };
 
 struct message_redraw_window {
diff --git a/libs/libgui/psf.c b/libs/libgui/psf.c
index 95678ed..5a6b1bb 100644
--- a/libs/libgui/psf.c
+++ b/libs/libgui/psf.c
@@ -23,7 +23,7 @@ static int psf_verify(char *data)
 		return 0;
 }
 
-struct font *psf_parse(char *data)
+struct gfx_font *psf_parse(char *data)
 {
 	int version = psf_verify(data);
 
@@ -47,7 +47,7 @@ struct font *psf_parse(char *data)
 		return NULL;
 	}
 
-	struct font *font = malloc(sizeof(*font));
+	struct gfx_font *font = malloc(sizeof(*font));
 	font->raw = data;
 	font->chars = chars;
 	font->size.x = width;
diff --git a/libs/libgui/psf.h b/libs/libgui/psf.h
index 4d63118..8298441 100644
--- a/libs/libgui/psf.h
+++ b/libs/libgui/psf.h
@@ -43,6 +43,6 @@ struct psf2_header {
 	u32 width;
 };
 
-struct font *psf_parse(char *data) NONNULL;
+struct gfx_font *psf_parse(char *data) NONNULL;
 
 #endif
-- 
cgit v1.2.3