サイズ変更が可能に+ボタンで計算の修正

This commit is contained in:
2026-01-18 01:01:47 +09:00
parent 78a1c061a7
commit cf4a24cc74
4 changed files with 79 additions and 72 deletions

9
main.c
View File

@@ -41,6 +41,15 @@ int main() {
exit(1); exit(1);
} }
XSizeHints *sizeHint = XAllocSizeHints();
if (sizeHint) {
sizeHint->flags = PMinSize;
sizeHint->min_width = 382;
sizeHint->min_height = 534;
XSetWMNormalHints(ui.display, ui.xwindow, sizeHint);
XFree(sizeHint);
}
ui.backbuf = XCreatePixmap(ui.display, ui.xwindow, ui.backbuf = XCreatePixmap(ui.display, ui.xwindow,
ui.window.width, ui.window.height, ui.window.width, ui.window.height,
DefaultDepth(ui.display, screen)); DefaultDepth(ui.display, screen));

View File

@@ -5,6 +5,17 @@
#include "control.h" #include "control.h"
#include "display.h" #include "display.h"
#define NUM_ROWS 6
#define NUM_COLS 4
#define DESIGN_WIDTH 382
#define DESIGN_HEIGHT 534
#define DESIGN_BUTTON_AREA_Y 224
#define DESIGN_BUTTON_WIDTH 93
#define DESIGN_BUTTON_HEIGHT 60
#define DESIGN_PADDING 2
char curinput[64] = {0}; char curinput[64] = {0};
int input_pos = 0; int input_pos = 0;
int initialized = 0; int initialized = 0;
@@ -19,21 +30,30 @@ static const char *btn_labels[][10] = {
{ "0", ".", "=", "<" }, { "0", ".", "=", "<" },
}; };
#define NUM_ROWS 6 static inline float get_scale(UiSystem *ui) {
#define NUM_COLS 4 XWindowAttributes attr;
XGetWindowAttributes(ui->display, ui->xwindow, &attr);
float sx = (float)attr.width / DESIGN_WIDTH;
float sy = (float)attr.height / DESIGN_HEIGHT;
return sx < sy ? sx : sy;
}
static SuwaButton *find_button_at(int mx, int my) { static SuwaButton *find_button_at(UiSystem *ui, int mx, int my) {
static SuwaButton found = {0}; static SuwaButton found = {0};
int btn_w = 93; float scale = get_scale(ui);
int btn_h = 60; if (scale <= 0) scale = 1.0f;
int padding = 2;
int btn_w = (int)(DESIGN_BUTTON_WIDTH * scale + .5f);
int btn_h = (int)(DESIGN_BUTTON_HEIGHT * scale + .5f);
int padding = (int)(DESIGN_PADDING * scale + .5f);;
int start_y = (int)(DESIGN_BUTTON_AREA_Y * scale + .5f);;
int row = -1; int row = -1;
int col = -1; int col = -1;
for (int r = 1; r < NUM_ROWS; ++r) { for (int r = 1; r < NUM_ROWS; ++r) {
int y_start = 224 + (r - 1) * btn_h + padding; int y_start = start_y + (r - 1) * btn_h + padding;
if (my >= y_start && my < y_start + btn_h) { if (my >= y_start && my < y_start + btn_h) {
row = r; row = r;
break; break;
@@ -52,11 +72,11 @@ static SuwaButton *find_button_at(int mx, int my) {
if (col < 0) return NULL; if (col < 0) return NULL;
found.x = padding + col * (btn_w + padding); found.x = padding + col * (btn_w + padding);
found.y = 224 + (row - 1) * (btn_h + padding); found.y = start_y + (row - 1) * (btn_h + padding);
found.width = btn_w; found.width = btn_w;
found.height = btn_h; found.height = btn_h;
found.text = btn_labels[row][col]; found.text = btn_labels[row][col];
found.pressed = 1; found.pressed = 1;
return &found; return &found;
@@ -132,19 +152,11 @@ void control_expose(UiSystem *ui, XEvent *e) {
int w = attr.width; int w = attr.width;
int h = attr.height; int h = attr.height;
if (ui->backbuf != None) { if (ui->backbuf != None) XFreePixmap(ui->display, ui->backbuf);
XFreePixmap(ui->display, ui->backbuf);
ui->backbuf = None;
}
ui->backbuf = XCreatePixmap(ui->display, ui->xwindow, w, h, ui->backbuf = XCreatePixmap(ui->display, ui->xwindow, w, h,
DefaultDepth(ui->display, DefaultScreen(ui->display))); DefaultDepth(ui->display, DefaultScreen(ui->display)));
if (ui->backbuf == None) { if (ui->backbuf == None) ui->backbuf = ui->xwindow;
fprintf(stderr, "バックバッファ作成失敗!\n");
ui->backbuf = ui->xwindow;
}
ui->target = ui->backbuf; ui->target = ui->backbuf;
XftDraw *backdraw = XftDrawCreate(ui->display, ui->backbuf, XftDraw *backdraw = XftDrawCreate(ui->display, ui->backbuf,
@@ -160,6 +172,11 @@ void control_expose(UiSystem *ui, XEvent *e) {
XSetForeground(ui->display, ui->gc, BGCOL); XSetForeground(ui->display, ui->gc, BGCOL);
XFillRectangle(ui->display, ui->backbuf, ui->gc, 0, 0, w, h); XFillRectangle(ui->display, ui->backbuf, ui->gc, 0, 0, w, h);
float scale = get_scale(ui);
if (scale <= 0) scale = 1.f;
int label_padding = (int)(20 * scale + .5f);
// 出力 // 出力
{ {
XGlyphInfo extents; XGlyphInfo extents;
@@ -167,12 +184,12 @@ void control_expose(UiSystem *ui, XEvent *e) {
(const FcChar8 *)ui->resLabel.text, (const FcChar8 *)ui->resLabel.text,
strlen(ui->resLabel.text), &extents); strlen(ui->resLabel.text), &extents);
int tx = w - 20 - extents.xOff; int tx = w - label_padding - extents.xOff;
int ty = (int)(180 * scale + .5f);
XftDrawStringUtf8( XftDrawStringUtf8(
backdraw, &ui->resLabel.fg_color, ui->resLabel.font, backdraw, &ui->resLabel.fg_color, ui->resLabel.font,
tx, ui->resLabel.y, tx, ty, (const FcChar8 *)ui->resLabel.text, 64);
(const FcChar8 *)ui->resLabel.text, 32);
} }
{ {
@@ -181,19 +198,18 @@ void control_expose(UiSystem *ui, XEvent *e) {
(const FcChar8 *)ui->problemLabel.text, (const FcChar8 *)ui->problemLabel.text,
strlen(ui->problemLabel.text), &extents); strlen(ui->problemLabel.text), &extents);
int tx = w - 20 - extents.xOff; int tx = w - label_padding - extents.xOff;
int ty = (int)(80 * scale + .5f);
XftDrawStringUtf8( XftDrawStringUtf8(
backdraw, &ui->problemLabel.fg_color, backdraw, &ui->problemLabel.fg_color, ui->problemLabel.font,
ui->problemLabel.font, tx, ui->problemLabel.y, tx, ty, (const FcChar8 *)ui->problemLabel.text, 64);
(const FcChar8 *)ui->problemLabel.text, 32);
} }
for (int i = 0; i < 64; ++i) ui->buttons[i] = NULL; int width = (int)(DESIGN_BUTTON_WIDTH * scale + .5f);
int width = 93; int height = (int)(DESIGN_BUTTON_HEIGHT * scale + .5f);
int height = 60; int padding = (int)(DESIGN_PADDING * scale + .5f);
int padding = 2; int start_y = (int)(DESIGN_BUTTON_AREA_Y * scale + .5f);
printf("ウィンドウ: (%dx%d)\n", attr.width, attr.height);
if (initialized == 0) { if (initialized == 0) {
XftColorAllocName(ui->display, XftColorAllocName(ui->display,
@@ -204,26 +220,20 @@ void control_expose(UiSystem *ui, XEvent *e) {
initialized = 1; initialized = 1;
for (int row = 1; row < NUM_ROWS; ++row) { for (int row = 1; row < NUM_ROWS; ++row) {
int y = 162 + row * (height + padding);
for (int col = 0; col < NUM_COLS; ++col) { for (int col = 0; col < NUM_COLS; ++col) {
const char *label = btn_labels[row][col]; SuwaButton btn = {
if (!label) continue; .x = padding + col * (width + padding),
.y = start_y + (row - 1) * (height + padding),
.width = width,
.height = height,
.text = btn_labels[row][col],
.font = ui->font,
.bg_color = BTCOL,
.fg_color = buttonColor,
.pressed = 0
};
int x = padding + col * (width + padding); drawbuttons(ui, &btn, backdraw);
SuwaButton *btn = malloc(sizeof(SuwaButton));
btn->x = x;
btn->y = y;
btn->width = width;
btn->height = height;
btn->text = label;
btn->bg_color = BTCOL;
btn->fg_color = buttonColor;
btn->pressed = 0;
// ui->buttons[0] = btn;
drawbuttons(ui, btn, backdraw);
} }
} }
@@ -233,7 +243,7 @@ void control_expose(UiSystem *ui, XEvent *e) {
} }
void handle_button_press(UiSystem *ui, int mx, int my) { void handle_button_press(UiSystem *ui, int mx, int my) {
SuwaButton *btn = find_button_at(mx, my); SuwaButton *btn = find_button_at(ui, mx, my);
if (!btn) return; if (!btn) return;
btn->pressed = 1; btn->pressed = 1;
@@ -252,7 +262,7 @@ void handle_button_press(UiSystem *ui, int mx, int my) {
} }
void handle_button_release(UiSystem *ui, int mx, int my) { void handle_button_release(UiSystem *ui, int mx, int my) {
SuwaButton *btn = find_button_at(mx, my); SuwaButton *btn = find_button_at(ui, mx, my);
if (!btn) return; if (!btn) return;
btn->pressed = 0; btn->pressed = 0;
@@ -283,16 +293,8 @@ void handle_button_release(UiSystem *ui, int mx, int my) {
strcpy(ui->resLabel.text, curinput[0] ? curinput : "0"); strcpy(ui->resLabel.text, curinput[0] ? curinput : "0");
} else if (strcmp(label, "=") == 0) { } else if (strcmp(label, "=") == 0) {
double res = evaluate_simple(ui, curinput); double res = evaluate_simple(ui, curinput);
if (isnan(res)) { if (isnan(res)) strncpy(ui->resLabel.text, "Error", 5);
strncpy(ui->resLabel.text, "Error", 5); else snprintf(ui->resLabel.text, sizeof(ui->resLabel.text), "%.8g", res);
} else {
snprintf(ui->resLabel.text, sizeof(ui->resLabel.text), "%.8g", res);
strncpy(curinput, ui->resLabel.text, strlen(ui->resLabel.text));
input_pos = strlen(curinput);
strcpy(curinput, "");
curinput[0] = '\0';
input_pos = 0;
}
} else if (strlen(label) == 1) { } else if (strlen(label) == 1) {
append_to_input(ui, label[0]); append_to_input(ui, label[0]);
} }
@@ -350,11 +352,8 @@ void handle_key_press(UiSystem *ui, XEvent *event) {
if (keysym == XK_Return || keysym == XK_KP_Enter) { if (keysym == XK_Return || keysym == XK_KP_Enter) {
double res = evaluate_simple(ui, curinput); double res = evaluate_simple(ui, curinput);
if (isnan(res)) { if (isnan(res)) strncpy(ui->resLabel.text, "Error", 5);
strncpy(ui->resLabel.text, "Error", 5); else snprintf(ui->resLabel.text, sizeof(ui->resLabel.text), "%.8g", res);
} else {
snprintf(ui->resLabel.text, sizeof(ui->resLabel.text), "%.8g", res);
}
goto redraw; goto redraw;
} }

View File

@@ -22,7 +22,7 @@ void drawbuttons(UiSystem *ui, SuwaButton *btn, XftDraw *xftdraw) {
int tx = btn->x + (btn->width - text_w) / 2; int tx = btn->x + (btn->width - text_w) / 2;
int ty = btn->y + (btn->height - text_h) / 2 + ui->font->ascent; int ty = btn->y + (btn->height - text_h) / 2 + ui->font->ascent;
XftDrawStringUtf8(xftdraw, &ui->color, ui->font, tx, ty, XftDrawStringUtf8(xftdraw, &btn->fg_color, ui->font, tx, ty,
(FcChar8 *)btn->text, len); (FcChar8 *)btn->text, len);
} }
} }

View File

@@ -24,7 +24,7 @@ typedef struct {
typedef struct { typedef struct {
int x, y, width, height; int x, y, width, height;
char text[32]; char text[64];
XftFont *font; XftFont *font;
XftColor fg_color; XftColor fg_color;
} SuwaLabel; } SuwaLabel;
@@ -43,5 +43,4 @@ typedef struct {
SuwaWindow window; SuwaWindow window;
SuwaLabel resLabel; SuwaLabel resLabel;
SuwaLabel problemLabel; SuwaLabel problemLabel;
SuwaButton *buttons[32];
} UiSystem; } UiSystem;