綺麗に

This commit is contained in:
2026-01-17 18:35:27 +09:00
parent b20fa6cc3a
commit fa83f224e2
9 changed files with 164 additions and 151 deletions

View File

@@ -120,78 +120,85 @@ double evaluate_simple(const char *expr) {
return res;
}
void control_expose(Display *dpy, Window wnd, GC gc, XEvent *e, XftColor *color, XftFont *f, XftFont *df, XftFont *pf) {
(void)color;
void control_expose(UiSystem *ui, XEvent *e) {
if (e->type != Expose && e->type != ConfigureNotify) return;
XWindowAttributes attr;
XGetWindowAttributes(dpy, wnd, &attr);
XGetWindowAttributes(ui->display, ui->window, &attr);
int w = attr.width;
int h = attr.height;
if (backbuf != None) {
XFreePixmap(dpy, backbuf);
if (ui->backbuf != None) {
XFreePixmap(ui->display, ui->backbuf);
ui->backbuf = None;
}
backbuf = XCreatePixmap(dpy, wnd, w, h, DefaultDepth(dpy, DefaultScreen(dpy)));
ui->backbuf = XCreatePixmap(ui->display, ui->window, w, h,
DefaultDepth(ui->display, DefaultScreen(ui->display)));
if (backbuf == None) {
backbuf = wnd;
if (ui->backbuf == None) {
fprintf(stderr, "バックバッファ作成失敗!\n");
ui->backbuf = ui->window;
}
XftDraw *backdraw = XftDrawCreate(dpy, backbuf,
DefaultVisual(dpy, DefaultScreen(dpy)), DefaultColormap(dpy, DefaultScreen(dpy)));
ui->target = ui->backbuf;
XftDraw *backdraw = XftDrawCreate(ui->display, ui->backbuf,
DefaultVisual(ui->display, DefaultScreen(ui->display)),
DefaultColormap(ui->display, DefaultScreen(ui->display)));
if (!backdraw) {
fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n");
XFreePixmap(dpy, backbuf);
backbuf = None;
XFreePixmap(ui->display, ui->backbuf);
ui->backbuf = None;
return;
}
XSetForeground(dpy, gc, BGCOL);
XFillRectangle(dpy, backbuf, gc, 0, 0, w, h);
XSetForeground(ui->display, ui->gc, BGCOL);
XFillRectangle(ui->display, ui->backbuf, ui->gc, 0, 0, w, h);
// 出力
if (displaytxt[0] != '\0' && df) {
if (displaytxt[0] != '\0' && ui->disfont) {
const FcChar8 *text = (const FcChar8 *)displaytxt;
int len = strlen((const char *)text);
XGlyphInfo extents;
XftTextExtentsUtf8(dpy, df, text, len, &extents);
XftTextExtentsUtf8(ui->display, ui->disfont, text, len, &extents);
int tx = w - 20 - extents.xOff;
int ty = 160;
XftColor discol;
XftColorAllocName(dpy, DefaultVisual(dpy, DefaultScreen(dpy)),
DefaultColormap(dpy, DefaultScreen(dpy)),
XftColorAllocName(ui->display, DefaultVisual(ui->display, DefaultScreen(ui->display)),
DefaultColormap(ui->display, DefaultScreen(ui->display)),
"#ee4030", &discol);
XftDrawStringUtf8(backdraw, &discol, df, tx, ty, text, len);
XftDrawStringUtf8(backdraw, &discol, ui->disfont, tx, ty, text, len);
XftColorFree(dpy, DefaultVisual(dpy, DefaultScreen(dpy)),
DefaultColormap(dpy, DefaultScreen(dpy)), &discol);
XftColorFree(ui->display, DefaultVisual(ui->display, DefaultScreen(ui->display)),
DefaultColormap(ui->display, DefaultScreen(ui->display)), &discol);
}
if (displayprb[0] != '\0' && pf) {
if (displayprb[0] != '\0' && ui->prbfont) {
const FcChar8 *text = (const FcChar8 *)displayprb;
int len = strlen((const char *)text);
XGlyphInfo extents;
XftTextExtentsUtf8(dpy, pf, text, len, &extents);
XftTextExtentsUtf8(ui->display, ui->prbfont, text, len, &extents);
int tx = w - 20 - extents.xOff;
int ty = 80;
XftColor discol;
XftColorAllocName(dpy, DefaultVisual(dpy, DefaultScreen(dpy)),
DefaultColormap(dpy, DefaultScreen(dpy)),
"#b61729", &discol);
XftColorAllocName(ui->display,
DefaultVisual(ui->display, DefaultScreen(ui->display)),
DefaultColormap(ui->display, DefaultScreen(ui->display)),
"#b61729", &discol);
XftDrawStringUtf8(backdraw, &discol, pf, tx, ty, text, len);
XftDrawStringUtf8(backdraw, &discol, ui->prbfont, tx, ty, text, len);
XftColorFree(dpy, DefaultVisual(dpy, DefaultScreen(dpy)),
DefaultColormap(dpy, DefaultScreen(dpy)), &discol);
XftColorFree(ui->display,
DefaultVisual(ui->display, DefaultScreen(ui->display)),
DefaultColormap(ui->display, DefaultScreen(ui->display)), &discol);
}
int width = 93;
@@ -219,50 +226,54 @@ void control_expose(Display *dpy, Window wnd, GC gc, XEvent *e, XftColor *color,
btn.label = label;
btn.pressed = 0;
drawbuttons(dpy, backbuf, gc, &btn, backdraw, color, f);
drawbuttons(ui, &btn, backdraw);
}
}
XCopyArea(dpy, backbuf, wnd, gc, 0, 0, w, h, 0, 0);
XCopyArea(ui->display, ui->backbuf, ui->window, ui->gc, 0, 0, w, h, 0, 0);
XftDrawDestroy(backdraw);
XFlush(dpy);
XFlush(ui->display);
}
void handle_button_press(Display *dpy, GC gc, int mx, int my, XftColor *col, XftFont *f) {
void handle_button_press(UiSystem *ui, int mx, int my) {
SuwaButton *btn = find_button_at(mx, my);
if (!btn) return;
btn->pressed = 1;
XftDraw *backdraw = XftDrawCreate(dpy, backbuf,
DefaultVisual(dpy, DefaultScreen(dpy)), DefaultColormap(dpy, DefaultScreen(dpy)));
XftDraw *backdraw = XftDrawCreate(ui->display, ui->backbuf,
DefaultVisual(ui->display, DefaultScreen(ui->display)),
DefaultColormap(ui->display, DefaultScreen(ui->display)));
if (!backdraw) {
fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n");
XFreePixmap(dpy, backbuf);
backbuf = None;
XFreePixmap(ui->display, ui->backbuf);
ui->backbuf = None;
return;
}
drawbuttons(dpy, backbuf, gc, btn, backdraw, col, f);
XFlush(dpy);
drawbuttons(ui, btn, backdraw);
XFlush(ui->display);
}
void handle_button_release(Display *dpy, Window wnd, GC gc, int mx, int my, XftColor *col, XftFont *f, XftFont *df, XftFont *pf) {
void handle_button_release(UiSystem *ui, int mx, int my) {
SuwaButton *btn = find_button_at(mx, my);
if (!btn) return;
btn->pressed = 0;
XftDraw *backdraw = XftDrawCreate(dpy, backbuf,
DefaultVisual(dpy, DefaultScreen(dpy)), DefaultColormap(dpy, DefaultScreen(dpy)));
XftDraw *backdraw = XftDrawCreate(ui->display, ui->backbuf,
DefaultVisual(ui->display, DefaultScreen(ui->display)),
DefaultColormap(ui->display, DefaultScreen(ui->display)));
if (!backdraw) {
fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n");
XFreePixmap(dpy, backbuf);
backbuf = None;
XFreePixmap(ui->display, ui->backbuf);
ui->backbuf = None;
return;
}
drawbuttons(dpy, backbuf, gc, btn, backdraw, col, f);
ui->target = ui->backbuf;
drawbuttons(ui, btn, backdraw);
XftDrawDestroy(backdraw);
XFlush(dpy);
XFlush(ui->display);
const char *label = btn->label;
if (strcmp(label, "C") == 0) {
@@ -283,10 +294,10 @@ void handle_button_release(Display *dpy, Window wnd, GC gc, int mx, int my, XftC
append_to_input(label[0]);
}
control_expose(dpy, wnd, gc, &(XEvent){.type = Expose}, col, f, df, pf);
control_expose(ui, &(XEvent){.type = Expose});
}
void handle_key_press(Display *dpy, Window wnd, GC gc, XEvent *event, XftColor *col, XftFont *f, XftFont *df, XftFont *pf) {
void handle_key_press(UiSystem *ui, XEvent *event) {
KeySym keysym;
char buf[32];
int len;
@@ -369,21 +380,21 @@ void handle_key_press(Display *dpy, Window wnd, GC gc, XEvent *event, XftColor *
return;
redraw:
control_expose(dpy, wnd, gc, &(XEvent){.type = Expose}, col, f, df, pf);
control_expose(ui, &(XEvent){.type = Expose});
}
// void handle_mouse_hover(Display *dpy, Window wnd, GC gc, XEvent *event, XftColor *col, XftFont *f) {
// void handle_mouse_hover(UiSystem *ui, XEvent *event) {
// SuwaButton *hover = find_button_at(event->xmotion.x, event->xmotion.y);
// if (hover != hovered_btn) {
// if (hovered_btn) {
// hovered_btn->pressed = 0;
// redraw_single_button(dpy, wnd, gc, col, f, hovered_btn);
// redraw_single_button(&ui, hovered_btn);
// }
// hovered_btn = hover;
// if (hovered_btn) {
// redraw_single_button(dpy, wnd, gc, col, f, hovered_btn);
// redraw_single_button(&ui, hovered_btn);
// }
// }
// }

View File

@@ -1,10 +1,9 @@
#pragma once
#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>
#include "ui.h"
void control_expose(Display *dpy, Window wnd, GC gc, XEvent *event, XftColor *color, XftFont *f, XftFont *df, XftFont *pf);
void handle_button_press(Display *dpy, GC gc, int mx, int my, XftColor *col, XftFont *f);
void handle_button_release(Display *dpy, Window wnd, GC gc, int mx, int my, XftColor *col, XftFont *f, XftFont *df, XftFont *pf);
void handle_key_press(Display *dpy, Window wnd, GC gc, XEvent *event, XftColor *col, XftFont *f, XftFont *df, XftFont *pf);
void handle_mouse_hover(Display *dpy, Window wnd, GC gc, XEvent *event, XftColor *col, XftFont *f);
void control_expose(UiSystem *ui, XEvent *event);
void handle_button_press(UiSystem *ui, int mx, int my);
void handle_button_release(UiSystem *ui, int mx, int my);
void handle_key_press(UiSystem *ui, XEvent *event);
void handle_mouse_hover(UiSystem *ui, XEvent *event);

View File

@@ -3,28 +3,28 @@
#include <string.h>
void drawbuttons(Display *dpy, Drawable target, GC gc, SuwaButton *btn,
XftDraw *xftdraw, XftColor *textcolor, XftFont *font) {
void drawbuttons(UiSystem *ui, SuwaButton *btn, XftDraw *xftdraw) {
unsigned long curbg = btn->pressed ? BTSEL : BTCOL;
XSetForeground(dpy, gc, curbg);
XFillRectangle(dpy, target, gc, btn->x, btn->y, btn->width, btn->height);
XSetForeground(ui->display, ui->gc, curbg);
XFillRectangle(ui->display, ui->target, ui->gc, btn->x, btn->y, btn->width, btn->height);
// 文字の中央に
if (btn->label && font && xftdraw) {
if (btn->label && ui->font && xftdraw) {
const FcChar8 *str = (const FcChar8 *)btn->label;
int len = strlen((const char *)str);
XGlyphInfo extents;
XftTextExtentsUtf8(dpy, font, str, len, &extents);
XftTextExtentsUtf8(ui->display, ui->font, str, len, &extents);
int text_w = extents.xOff;
int text_h = font->ascent + font->descent;
int text_h = ui->font->ascent + ui->font->descent;
int tx = btn->x + (btn->width - text_w) / 2;
int ty = btn->y + (btn->height - text_h) / 2 + font->ascent;
int ty = btn->y + (btn->height - text_h) / 2 + ui->font->ascent;
// tx -= extents.x;
XftDrawStringUtf8(xftdraw, textcolor, font, tx, ty, (FcChar8 *)btn->label, len);
XftDrawStringUtf8(xftdraw, &ui->textcolor, ui->font, tx, ty,
(FcChar8 *)btn->label, len);
}
}

View File

@@ -1,6 +1,6 @@
#pragma once
#include "program.h"
#include "ui.h"
void drawbuttons(Display *dpy, Drawable target, GC gc, SuwaButton *btn,
XftDraw *xftdraw, XftColor *textcolor, XftFont *font);
void drawbuttons(UiSystem *ui, SuwaButton *btn, XftDraw *xftdraw);

View File

@@ -1,7 +1,5 @@
#pragma once
#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>
#include "ui.h"
#define FGCOL 0xfcfcfc
@@ -17,6 +15,5 @@ extern char displayprb[128];
extern char curinput[256];
extern char displaytxt[64];
extern int input_pos;
extern Pixmap backbuf;
extern SuwaButton *hovered_btn;
extern SuwaButton *pressed_btn;

View File

@@ -1,5 +1,21 @@
#pragma once
#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>
typedef struct {
Display *display;
Window window;
Drawable target;
GC gc;
Visual visual;
XftDraw *xftdraw;
XftColor color, btncolor, textcolor;
XftFont *font, *prbfont, *disfont;
Colormap colormap;
Pixmap backbuf;
} UiSystem;
typedef struct {
int x, y, width, height;
const char *label;

View File

@@ -1,19 +1,18 @@
#include "utils.h"
void cleanup(Display *display, Window window, GC gc,
XftColor *color, XftColor *btncolor, XftFont *font,
XftFont *disfont, XftFont *prbfont, Colormap colormap, Visual *visual,
Pixmap backbuf) {
if (btncolor) XftColorFree(display, visual, colormap, btncolor);
if (color) XftColorFree(display, visual, colormap, color);
if (disfont) XftFontClose(display, disfont);
if (prbfont) XftFontClose(display, prbfont);
if (font) XftFontClose(display, font);
if (gc) XFreeGC(display, gc);
if (backbuf) {
XFreePixmap(display, backbuf);
backbuf = None;
void cleanup(UiSystem *ui) {
if (ui->btncolor.pixel != 0)
XftColorFree(ui->display, &ui->visual, ui->colormap, &ui->btncolor);
if (ui->color.pixel != 0)
XftColorFree(ui->display, &ui->visual, ui->colormap, &ui->color);
if (ui->disfont) XftFontClose(ui->display, ui->disfont);
if (ui->prbfont) XftFontClose(ui->display, ui->prbfont);
if (ui->font) XftFontClose(ui->display, ui->font);
if (ui->gc) XFreeGC(ui->display, ui->gc);
if (ui->backbuf) {
XFreePixmap(ui->display, ui->backbuf);
ui->backbuf = None;
}
if (window) XDestroyWindow(display, window);
if (display) XCloseDisplay(display);
if (ui->window) XDestroyWindow(ui->display, ui->window);
if (ui->display) XCloseDisplay(ui->display);
}

View File

@@ -2,7 +2,4 @@
#include "program.h"
void cleanup(Display *display, Window window, GC gc,
XftColor *color, XftColor *btncolor, XftFont *font,
XftFont *disfont, XftFont *prbfont, Colormap colormap, Visual *visual,
Pixmap backbuf);
void cleanup(UiSystem *ui);