suwako / UnixCalc アーカイブ

このリポジトリはアーカイブされています。読むだけで、プッシュやイシューの変更は出来ません。

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

諏訪子 <suwako@076.moe>、2026-01-18 01:01
コミット cf4a24cc74ca9469aece77d4c20f2fa236261686 ・親 78a1c06 ・ファイルを見る

4個のファイル、+79 -72

変更 main.c
@@ -41,6 +41,15 @@
4141 exit(1);
4242 }
4343
44+ XSizeHints *sizeHint = XAllocSizeHints();
45+ if (sizeHint) {
46+ sizeHint->flags = PMinSize;
47+ sizeHint->min_width = 382;
48+ sizeHint->min_height = 534;
49+ XSetWMNormalHints(ui.display, ui.xwindow, sizeHint);
50+ XFree(sizeHint);
51+ }
52+
4453 ui.backbuf = XCreatePixmap(ui.display, ui.xwindow,
4554 ui.window.width, ui.window.height,
4655 DefaultDepth(ui.display, screen));
変更 src/control.c
@@ -5,6 +5,17 @@
55 #include "control.h"
66 #include "display.h"
77
8+#define NUM_ROWS 6
9+#define NUM_COLS 4
10+
11+#define DESIGN_WIDTH 382
12+#define DESIGN_HEIGHT 534
13+
14+#define DESIGN_BUTTON_AREA_Y 224
15+#define DESIGN_BUTTON_WIDTH 93
16+#define DESIGN_BUTTON_HEIGHT 60
17+#define DESIGN_PADDING 2
18+
819 char curinput[64] = {0};
920 int input_pos = 0;
1021 int initialized = 0;
@@ -19,21 +30,30 @@
1930 { "0", ".", "=", "<" },
2031 };
2132
22-#define NUM_ROWS 6
23-#define NUM_COLS 4
33+static inline float get_scale(UiSystem *ui) {
34+ XWindowAttributes attr;
35+ XGetWindowAttributes(ui->display, ui->xwindow, &attr);
36+ float sx = (float)attr.width / DESIGN_WIDTH;
37+ float sy = (float)attr.height / DESIGN_HEIGHT;
38+ return sx < sy ? sx : sy;
39+}
2440
25-static SuwaButton *find_button_at(int mx, int my) {
41+static SuwaButton *find_button_at(UiSystem *ui, int mx, int my) {
2642 static SuwaButton found = {0};
2743
28- int btn_w = 93;
29- int btn_h = 60;
30- int padding = 2;
44+ float scale = get_scale(ui);
45+ if (scale <= 0) scale = 1.0f;
3146
47+ int btn_w = (int)(DESIGN_BUTTON_WIDTH * scale + .5f);
48+ int btn_h = (int)(DESIGN_BUTTON_HEIGHT * scale + .5f);
49+ int padding = (int)(DESIGN_PADDING * scale + .5f);;
50+ int start_y = (int)(DESIGN_BUTTON_AREA_Y * scale + .5f);;
51+
3252 int row = -1;
3353 int col = -1;
3454
3555 for (int r = 1; r < NUM_ROWS; ++r) {
36- int y_start = 224 + (r - 1) * btn_h + padding;
56+ int y_start = start_y + (r - 1) * btn_h + padding;
3757 if (my >= y_start && my < y_start + btn_h) {
3858 row = r;
3959 break;
@@ -52,11 +72,11 @@
5272
5373 if (col < 0) return NULL;
5474
55- found.x = padding + col * (btn_w + padding);
56- found.y = 224 + (row - 1) * (btn_h + padding);
57- found.width = btn_w;
58- found.height = btn_h;
59- found.text = btn_labels[row][col];
75+ found.x = padding + col * (btn_w + padding);
76+ found.y = start_y + (row - 1) * (btn_h + padding);
77+ found.width = btn_w;
78+ found.height = btn_h;
79+ found.text = btn_labels[row][col];
6080 found.pressed = 1;
6181
6282 return &found;
@@ -132,19 +152,11 @@
132152 int w = attr.width;
133153 int h = attr.height;
134154
135- if (ui->backbuf != None) {
136- XFreePixmap(ui->display, ui->backbuf);
137- ui->backbuf = None;
138- }
139-
155+ if (ui->backbuf != None) XFreePixmap(ui->display, ui->backbuf);
140156 ui->backbuf = XCreatePixmap(ui->display, ui->xwindow, w, h,
141157 DefaultDepth(ui->display, DefaultScreen(ui->display)));
142158
143- if (ui->backbuf == None) {
144- fprintf(stderr, "バックバッファ作成失敗!\n");
145- ui->backbuf = ui->xwindow;
146- }
147-
159+ if (ui->backbuf == None) ui->backbuf = ui->xwindow;
148160 ui->target = ui->backbuf;
149161
150162 XftDraw *backdraw = XftDrawCreate(ui->display, ui->backbuf,
@@ -160,6 +172,11 @@
160172 XSetForeground(ui->display, ui->gc, BGCOL);
161173 XFillRectangle(ui->display, ui->backbuf, ui->gc, 0, 0, w, h);
162174
175+ float scale = get_scale(ui);
176+ if (scale <= 0) scale = 1.f;
177+
178+ int label_padding = (int)(20 * scale + .5f);
179+
163180 // 出力
164181 {
165182 XGlyphInfo extents;
@@ -167,12 +184,12 @@
167184 (const FcChar8 *)ui->resLabel.text,
168185 strlen(ui->resLabel.text), &extents);
169186
170- int tx = w - 20 - extents.xOff;
187+ int tx = w - label_padding - extents.xOff;
188+ int ty = (int)(180 * scale + .5f);
171189
172190 XftDrawStringUtf8(
173191 backdraw, &ui->resLabel.fg_color, ui->resLabel.font,
174- tx, ui->resLabel.y,
175- (const FcChar8 *)ui->resLabel.text, 32);
192+ tx, ty, (const FcChar8 *)ui->resLabel.text, 64);
176193 }
177194
178195 {
@@ -181,19 +198,18 @@
181198 (const FcChar8 *)ui->problemLabel.text,
182199 strlen(ui->problemLabel.text), &extents);
183200
184- int tx = w - 20 - extents.xOff;
201+ int tx = w - label_padding - extents.xOff;
202+ int ty = (int)(80 * scale + .5f);
185203
186204 XftDrawStringUtf8(
187- backdraw, &ui->problemLabel.fg_color,
188- ui->problemLabel.font, tx, ui->problemLabel.y,
189- (const FcChar8 *)ui->problemLabel.text, 32);
205+ backdraw, &ui->problemLabel.fg_color, ui->problemLabel.font,
206+ tx, ty, (const FcChar8 *)ui->problemLabel.text, 64);
190207 }
191208
192- for (int i = 0; i < 64; ++i) ui->buttons[i] = NULL;
193- int width = 93;
194- int height = 60;
195- int padding = 2;
196- printf("ウィンドウ: (%dx%d)\n", attr.width, attr.height);
209+ int width = (int)(DESIGN_BUTTON_WIDTH * scale + .5f);
210+ int height = (int)(DESIGN_BUTTON_HEIGHT * scale + .5f);
211+ int padding = (int)(DESIGN_PADDING * scale + .5f);
212+ int start_y = (int)(DESIGN_BUTTON_AREA_Y * scale + .5f);
197213
198214 if (initialized == 0) {
199215 XftColorAllocName(ui->display,
@@ -204,26 +220,20 @@
204220 initialized = 1;
205221
206222 for (int row = 1; row < NUM_ROWS; ++row) {
207- int y = 162 + row * (height + padding);
208-
209223 for (int col = 0; col < NUM_COLS; ++col) {
210- const char *label = btn_labels[row][col];
211- if (!label) continue;
224+ SuwaButton btn = {
225+ .x = padding + col * (width + padding),
226+ .y = start_y + (row - 1) * (height + padding),
227+ .width = width,
228+ .height = height,
229+ .text = btn_labels[row][col],
230+ .font = ui->font,
231+ .bg_color = BTCOL,
232+ .fg_color = buttonColor,
233+ .pressed = 0
234+ };
212235
213- int x = padding + col * (width + padding);
214-
215- SuwaButton *btn = malloc(sizeof(SuwaButton));
216- btn->x = x;
217- btn->y = y;
218- btn->width = width;
219- btn->height = height;
220- btn->text = label;
221- btn->bg_color = BTCOL;
222- btn->fg_color = buttonColor;
223- btn->pressed = 0;
224- // ui->buttons[0] = btn;
225-
226- drawbuttons(ui, btn, backdraw);
236+ drawbuttons(ui, &btn, backdraw);
227237 }
228238 }
229239
@@ -233,7 +243,7 @@
233243 }
234244
235245 void handle_button_press(UiSystem *ui, int mx, int my) {
236- SuwaButton *btn = find_button_at(mx, my);
246+ SuwaButton *btn = find_button_at(ui, mx, my);
237247 if (!btn) return;
238248
239249 btn->pressed = 1;
@@ -252,7 +262,7 @@
252262 }
253263
254264 void handle_button_release(UiSystem *ui, int mx, int my) {
255- SuwaButton *btn = find_button_at(mx, my);
265+ SuwaButton *btn = find_button_at(ui, mx, my);
256266 if (!btn) return;
257267
258268 btn->pressed = 0;
@@ -283,16 +293,8 @@
283293 strcpy(ui->resLabel.text, curinput[0] ? curinput : "0");
284294 } else if (strcmp(label, "=") == 0) {
285295 double res = evaluate_simple(ui, curinput);
286- if (isnan(res)) {
287- strncpy(ui->resLabel.text, "Error", 5);
288- } else {
289- snprintf(ui->resLabel.text, sizeof(ui->resLabel.text), "%.8g", res);
290- strncpy(curinput, ui->resLabel.text, strlen(ui->resLabel.text));
291- input_pos = strlen(curinput);
292- strcpy(curinput, "");
293- curinput[0] = '\0';
294- input_pos = 0;
295- }
296+ if (isnan(res)) strncpy(ui->resLabel.text, "Error", 5);
297+ else snprintf(ui->resLabel.text, sizeof(ui->resLabel.text), "%.8g", res);
296298 } else if (strlen(label) == 1) {
297299 append_to_input(ui, label[0]);
298300 }
@@ -350,11 +352,8 @@
350352
351353 if (keysym == XK_Return || keysym == XK_KP_Enter) {
352354 double res = evaluate_simple(ui, curinput);
353- if (isnan(res)) {
354- strncpy(ui->resLabel.text, "Error", 5);
355- } else {
356- snprintf(ui->resLabel.text, sizeof(ui->resLabel.text), "%.8g", res);
357- }
355+ if (isnan(res)) strncpy(ui->resLabel.text, "Error", 5);
356+ else snprintf(ui->resLabel.text, sizeof(ui->resLabel.text), "%.8g", res);
358357 goto redraw;
359358 }
360359
変更 src/display.c
@@ -22,7 +22,7 @@
2222 int tx = btn->x + (btn->width - text_w) / 2;
2323 int ty = btn->y + (btn->height - text_h) / 2 + ui->font->ascent;
2424
25- XftDrawStringUtf8(xftdraw, &ui->color, ui->font, tx, ty,
25+ XftDrawStringUtf8(xftdraw, &btn->fg_color, ui->font, tx, ty,
2626 (FcChar8 *)btn->text, len);
2727 }
2828 }
変更 src/ui.h
@@ -24,7 +24,7 @@
2424
2525 typedef struct {
2626 int x, y, width, height;
27- char text[32];
27+ char text[64];
2828 XftFont *font;
2929 XftColor fg_color;
3030 } SuwaLabel;
@@ -43,5 +43,4 @@
4343 SuwaWindow window;
4444 SuwaLabel resLabel;
4545 SuwaLabel problemLabel;
46- SuwaButton *buttons[32];
4746 } UiSystem;