summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--main.c6
-rw-r--r--ui.c56
-rw-r--r--ui.h2
4 files changed, 44 insertions, 24 deletions
diff --git a/Makefile b/Makefile
index 6fbfe1f..149aba0 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,8 @@
EXE = xmenu
OBJ != find . -name '*.c' | sed -e 's/\.c$$/.o/' -e 's|^\./||'
-CFLAGS += -Wall -Wpedantic -I/usr/X11R7/include
-LDFLAGS += -s -lX11 -lxcb -lXt -lXau -lXdmcp -L/usr/X11R7/lib -static
+CFLAGS += -Wall -Wpedantic -I/usr/X11R7/include -g
+LDFLAGS += -lX11 -lxcb -lXt -lXau -lXdmcp -L/usr/X11R7/lib -static
PREFIX ?= ${HOME}
BINDIR ?= ${PREFIX}/bin
diff --git a/main.c b/main.c
index 0368e32..c2b9946 100644
--- a/main.c
+++ b/main.c
@@ -64,8 +64,6 @@ read_all(FILE *f, DynStr *out, Arena *a)
int
main(int argc, char **argv)
{
- ui_init(argc, argv);
-
Arena a = { 0 };
DynStr input = { 0 };
int inpi = 0;
@@ -80,7 +78,9 @@ main(int argc, char **argv)
src = c.tail;
}
- if (!opt.n) goto done;
+ if (!opt.n) return 0;
+
+ ui_init(argc, argv, opt);
for (UiEvent ev; ui_wait_event(&ev); ) {
switch (ev.type) {
diff --git a/ui.c b/ui.c
index 80d6267..f3f3943 100644
--- a/ui.c
+++ b/ui.c
@@ -18,18 +18,21 @@ static XSizeHints szhint;
static GC gc;
static Atom wm_delete_window;
-static Font freg, fital;
+static XFontStruct *freg, *fital;
/* TODO: center menu on screen */
void
-ui_init(int argc, char **argv)
+ui_init(int argc, char **argv, UiOpts opt)
{
dsp = XOpenDisplay(NULL);
scr = DefaultScreen(dsp);
fg = BlackPixel(dsp, scr);
bg = WhitePixel(dsp, scr);
+ int scr_width = XWidthOfScreen(ScreenOfDisplay(dsp, scr));
+ int scr_height = XHeightOfScreen(ScreenOfDisplay(dsp, scr));
+
szhint = (XSizeHints) { 0 };
szhint.width = 350;
szhint.height = 250;
@@ -52,15 +55,32 @@ ui_init(int argc, char **argv)
XSetBackground(dsp, gc, bg);
XSetForeground(dsp, gc, fg);
- /* TODO: quit events, how do they work? */
XSelectInput(dsp, win, KeyPressMask | ExposureMask);
XMapRaised(dsp, win);
wm_delete_window = XInternAtom(dsp, "WM_DELETE_WINDOW", False);
XSetWMProtocols(dsp, win, &wm_delete_window, 1);
- freg = XLoadFont(dsp, "-*-new century schoolbook-medium-r-*-*-24-*-*-*-*-*-*-*");
- fital = XLoadFont(dsp, "-*-new century schoolbook-medium-i-*-*-24-*-*-*-*-*-*-*");
+ /* TODO: error checking */
+ freg = XLoadQueryFont(dsp, "-*-new century schoolbook-medium-r-*-*-24-*-*-*-*-*-*-*");
+ fital = XLoadQueryFont(dsp, "-*-new century schoolbook-medium-i-*-*-24-*-*-*-*-*-*-*");
+
+ int w = 0;
+ for (int i = 0; i < opt.n; i++) {
+ int ow = XTextWidth(freg, opt.v[i].s, opt.v[i].n);
+ if (ow > w) w = ow;
+ }
+
+ int h = 24 * (opt.n + 1);
+ int margin = 64;
+ if (w > scr_width - 2*margin) w = scr_width - 2*margin;
+ if (h > scr_height - 2*margin) h = scr_height - 2*margin;
+
+ XMoveResizeWindow(dsp, win,
+ (scr_width - w - 32) / 2,
+ (scr_height - h - 32) / 2,
+ w + 32, h + 32
+ );
}
UiKey
@@ -82,6 +102,8 @@ xksym_to_uik(KeySym sym)
}
}
+/* TODO: allow selecting options with mouse */
+
int
ui_wait_event(UiEvent *e)
{
@@ -124,23 +146,21 @@ ui_draw(Str input, int inpi, int seli, UiOpts o)
/* draw input */
- XSetFont(dsp, gc, fital);
- XDrawString(dsp, win, gc, 16, 24, input.s, input.n);
- XFontStruct *f = XQueryFont(dsp, XGContextFromGC(gc));
- int w = XTextWidth(f, input.s, inpi);
- XDrawLine(dsp, win, gc, 16 + w, 24 - f->ascent, 16 + w, 24 + f->descent);
+ XSetFont(dsp, gc, fital->fid);
+ XDrawString(dsp, win, gc, 16, 32, input.s, input.n);
+ int w = XTextWidth(fital, input.s, inpi);
+ XDrawLine(dsp, win, gc,
+ 16 + w, 32 - fital->ascent,
+ 16 + w, 32 + fital->descent);
/* draw options */
- XSetFont(dsp, gc, freg);
+ XSetFont(dsp, gc, freg->fid);
for (int i = 0; i < o.n; i++) {
if (i == seli) {
- int w = XTextWidth(XQueryFont(dsp,
- XGContextFromGC(gc)),
- o.v[i].s, o.v[i].n);
+ int w = XTextWidth(freg, o.v[i].s, o.v[i].n);
XFillRectangle(dsp, win, gc,
- 16, 28 + i * 24 + 8,
- w, 24);
+ 16, 28 + i * 24 + 8, w, 24);
XSetForeground(dsp, gc, bg);
XDrawString(dsp, win, gc,
16, 48 + i * 24 + 8,
@@ -157,8 +177,8 @@ ui_draw(Str input, int inpi, int seli, UiOpts o)
void
ui_fini(void)
{
- XUnloadFont(dsp, freg);
- XUnloadFont(dsp, fital);
+ XFreeFont(dsp, freg);
+ XFreeFont(dsp, fital);
XFreeGC(dsp, gc);
XDestroyWindow(dsp, win);
XCloseDisplay(dsp);
diff --git a/ui.h b/ui.h
index 015b1aa..9a80a35 100644
--- a/ui.h
+++ b/ui.h
@@ -41,7 +41,7 @@ typedef struct {
typedef DYNARR(Str) UiOpts;
-void ui_init(int argc, char **argv);
+void ui_init(int argc, char **argv, UiOpts o);
void ui_fini(void);
void ui_draw(Str input, int inpi, int seli, UiOpts o);
int ui_wait_event(UiEvent *e);