summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sources/main.c140
1 files changed, 94 insertions, 46 deletions
diff --git a/sources/main.c b/sources/main.c
index 4049a30..e1dc077 100644
--- a/sources/main.c
+++ b/sources/main.c
@@ -36,6 +36,9 @@
#include "meta/exec.c"
+#define IUI_TXT_CENTER bit(0)
+#define IUI_CLIP_CLEAR bit(0)
+
typedef struct
{
enum
@@ -45,12 +48,14 @@ typedef struct
IUI_FILL,
IUI_LINE,
IUI_TEXT,
+ IUI_CLIP,
} type;
u32 color;
u16 x, y, w, h;
u16 size;
+ u16 flags;
char* text;
} iui_cmd;
@@ -71,6 +76,8 @@ typedef struct
u8 pbtns;
u8 btns;
+ /* TODO: event pump */
+
usize ncmds;
iui_cmd cmds[IUI_CMDBUF_LEN]; /* TODO: arena? */
@@ -140,9 +147,10 @@ int iui_button(iui* ui, u16 x, u16 y, int w, int h, char* label)
cmd.color = x11->cols.as.fg;
ui->cmds[ui->ncmds++] = cmd;
- cmd.type = IUI_TEXT;
- cmd.size = 14;
- cmd.text = label;
+ cmd.type = IUI_TEXT;
+ cmd.size = 14;
+ cmd.text = label;
+ cmd.flags = IUI_TXT_CENTER;
if(imactive) cmd.color = x11->cols.as.bg;
else cmd.color = x11->cols.as.fg;
@@ -173,59 +181,80 @@ int iui_textbox(iui* ui, u16 x, u16 y, int w, int h, char* buf, usize buflen)
imactive = ui->active == id;
{
- XGCValues gcvals = {0};
-
- if(imhot) gcvals.line_width = 2;
- else gcvals.line_width = 1;
-
- gcvals.foreground = x11->cols.as.bg;
+ iui_cmd cmd = {0};
+ cmd.type = IUI_FILL;
- XChangeGC(x11->disp, x11->gc, GCForeground | GCLineWidth, &gcvals);
- XFillRectangle(x11->disp, x11->wnd, x11->gc, x, y, w, h);
+ cmd.x = x;
+ cmd.y = y;
+ cmd.w = w;
+ cmd.h = h;
- XSetForeground(x11->disp, x11->gc, x11->cols.as.fg);
- XDrawLine(x11->disp, x11->wnd, x11->gc, x, y, x, y + h);
- XDrawLine(x11->disp, x11->wnd, x11->gc, x + w, y, x, y);
- XDrawLine(x11->disp, x11->wnd, x11->gc, x, y + h, x + w, y + h);
- XDrawLine(x11->disp, x11->wnd, x11->gc, x + w, y + h, x + w, y);
- }
+ if(imhot) cmd.size = 2;
+ else cmd.size = 1;
- if(imactive) ui->kbdfocus = id;
- if(ui->kbdfocus == id)
- {
+ cmd.color = x11->cols.as.bg;
+ ui->cmds[ui->ncmds++] = cmd;
- XGCValues gcvals = {0};
- gcvals.foreground = x11->cols.as.fg;
+ cmd.type = IUI_BOX;
+ cmd.color = x11->cols.as.fg;
+ ui->cmds[ui->ncmds++] = cmd;
- XChangeGC(x11->disp, x11->gc, GCForeground, &gcvals);
- XFillRectangle(x11->disp, x11->wnd, x11->gc, x + 6 + (6 * len), y + 6, 10, h - 12);
- }
- else
- {
- XGCValues gcvals = {0};
- gcvals.foreground = x11->cols.as.fg;
- gcvals.line_width = 1;
+ {
+ iui_cmd clip = cmd;
+ clip.type = IUI_CLIP;
+ clip.flags = 0;
+ clip.x++;
+ clip.y++;
+ clip.w--;
+ clip.h--;
+
+ ui->cmds[ui->ncmds++] = clip;
+ }
- XChangeGC(x11->disp, x11->gc, GCForeground | GCLineWidth, &gcvals);
- XDrawRectangle(x11->disp, x11->wnd, x11->gc, x + 6 + (6 * len), y + 6, 9, h - 13);
- }
+ if(imactive) ui->kbdfocus = id;
+ if(ui->kbdfocus == id)
+ {
+ cmd.type = IUI_FILL;
+ cmd.x = x + 6 + 6 * len;
+ cmd.y = y + 6;
+ cmd.w = 10;
+ cmd.h = h - 12;
+ cmd.color = x11->cols.as.fg;
+
+ ui->cmds[ui->ncmds++] = cmd;
+ }
+ else
+ {
+ cmd.type = IUI_BOX;
+ cmd.x = x + 6 + 6 * len;
+ cmd.y = y + 6;
+ cmd.w = 9;
+ cmd.h = h - 13;
+ cmd.color = x11->cols.as.fg;
+ cmd.size = 1;
+
+ ui->cmds[ui->ncmds++] = cmd;
+ }
- {
- int xx, yy;
- int di, fa, fd;
- int width, height;
- XCharStruct chs = {0};
+ {
+ cmd.type = IUI_TEXT;
+ cmd.size = 14;
+ cmd.text = buf;
+ cmd.flags = 0;
- XTextExtents(x11->xfs, buf, buflen, &di, &fa, &fd, &chs);
- height = chs.ascent + chs.descent;
+ cmd.x = x + 6;
+ cmd.y = y;
+ cmd.w = w;
+ cmd.h = h;
- xx = x + 6;
- yy = y + h / 2 + height / 2;
+ cmd.color = x11->cols.as.fg;
+ ui->cmds[ui->ncmds++] = cmd;
+ }
- if(imactive) XSetForeground(x11->disp, x11->gc, x11->cols.as.bg);
- else XSetForeground(x11->disp, x11->gc, x11->cols.as.fg);
- XDrawString(x11->disp, x11->wnd, x11->gc, xx, yy, buf, len);
+ cmd.type = IUI_CLIP;
+ cmd.flags = IUI_CLIP_CLEAR;
+ ui->cmds[ui->ncmds++] = cmd;
}
return imhot && imactive && !(ui->btns & bit(1));
@@ -283,7 +312,10 @@ void iui_draw(iui* ui)
width = chs.rbearing - chs.lbearing;
height = chs.ascent + chs.descent;
- xx = cmd->x + cmd->w / 2 - width / 2;
+ if(cmd->flags & IUI_TXT_CENTER)
+ xx = cmd->x + cmd->w / 2 - width / 2;
+ else
+ xx = cmd->x;
yy = cmd->y + cmd->h / 2 + height / 2;
XSetForeground(x11->disp, x11->gc, cmd->color);
@@ -291,6 +323,22 @@ void iui_draw(iui* ui)
}
break;
+ case IUI_CLIP:
+ {
+ if(cmd->flags & IUI_CLIP_CLEAR) XSetClipMask(x11->disp, x11->gc, None);
+ else
+ {
+ XRectangle clip_region = {0};
+ clip_region.x = cmd->x;
+ clip_region.y = cmd->y;
+ clip_region.width = cmd->w;
+ clip_region.height = cmd->h;
+
+ XSetClipRectangles(x11->disp, x11->gc, 0, 0, &clip_region, 1, Unsorted);
+ }
+ }
+ break;
+
default:
report(WARN, "IUI: Unknown draw command %u\n", cmd->type);
break;