サイズ変更が可能に+ボタンで計算の修正
諏訪子 <suwako@076.moe>、2026-01-18 01:01
コミット cf4a24cc74ca9469aece77d4c20f2fa236261686
・親 78a1c06
・ファイルを見る
4個のファイル、+79 -72
変更
main.c
| @@ -41,6 +41,15 @@ | ||
| 41 | 41 | exit(1); |
| 42 | 42 | } |
| 43 | 43 | |
| 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 | + | |
| 44 | 53 | ui.backbuf = XCreatePixmap(ui.display, ui.xwindow, |
| 45 | 54 | ui.window.width, ui.window.height, |
| 46 | 55 | DefaultDepth(ui.display, screen)); |
| @@ -5,6 +5,17 @@ | ||
| 5 | 5 | #include "control.h" |
| 6 | 6 | #include "display.h" |
| 7 | 7 | |
| 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 | + | |
| 8 | 19 | char curinput[64] = {0}; |
| 9 | 20 | int input_pos = 0; |
| 10 | 21 | int initialized = 0; |
| @@ -19,21 +30,30 @@ | ||
| 19 | 30 | { "0", ".", "=", "<" }, |
| 20 | 31 | }; |
| 21 | 32 | |
| 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 | +} | |
| 24 | 40 | |
| 25 | -static SuwaButton *find_button_at(int mx, int my) { | |
| 41 | +static SuwaButton *find_button_at(UiSystem *ui, int mx, int my) { | |
| 26 | 42 | static SuwaButton found = {0}; |
| 27 | 43 | |
| 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; | |
| 31 | 46 | |
| 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 | + | |
| 32 | 52 | int row = -1; |
| 33 | 53 | int col = -1; |
| 34 | 54 | |
| 35 | 55 | 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; | |
| 37 | 57 | if (my >= y_start && my < y_start + btn_h) { |
| 38 | 58 | row = r; |
| 39 | 59 | break; |
| @@ -52,11 +72,11 @@ | ||
| 52 | 72 | |
| 53 | 73 | if (col < 0) return NULL; |
| 54 | 74 | |
| 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]; | |
| 60 | 80 | found.pressed = 1; |
| 61 | 81 | |
| 62 | 82 | return &found; |
| @@ -132,19 +152,11 @@ | ||
| 132 | 152 | int w = attr.width; |
| 133 | 153 | int h = attr.height; |
| 134 | 154 | |
| 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); | |
| 140 | 156 | ui->backbuf = XCreatePixmap(ui->display, ui->xwindow, w, h, |
| 141 | 157 | DefaultDepth(ui->display, DefaultScreen(ui->display))); |
| 142 | 158 | |
| 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; | |
| 148 | 160 | ui->target = ui->backbuf; |
| 149 | 161 | |
| 150 | 162 | XftDraw *backdraw = XftDrawCreate(ui->display, ui->backbuf, |
| @@ -160,6 +172,11 @@ | ||
| 160 | 172 | XSetForeground(ui->display, ui->gc, BGCOL); |
| 161 | 173 | XFillRectangle(ui->display, ui->backbuf, ui->gc, 0, 0, w, h); |
| 162 | 174 | |
| 175 | + float scale = get_scale(ui); | |
| 176 | + if (scale <= 0) scale = 1.f; | |
| 177 | + | |
| 178 | + int label_padding = (int)(20 * scale + .5f); | |
| 179 | + | |
| 163 | 180 | // 出力 |
| 164 | 181 | { |
| 165 | 182 | XGlyphInfo extents; |
| @@ -167,12 +184,12 @@ | ||
| 167 | 184 | (const FcChar8 *)ui->resLabel.text, |
| 168 | 185 | strlen(ui->resLabel.text), &extents); |
| 169 | 186 | |
| 170 | - int tx = w - 20 - extents.xOff; | |
| 187 | + int tx = w - label_padding - extents.xOff; | |
| 188 | + int ty = (int)(180 * scale + .5f); | |
| 171 | 189 | |
| 172 | 190 | XftDrawStringUtf8( |
| 173 | 191 | 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); | |
| 176 | 193 | } |
| 177 | 194 | |
| 178 | 195 | { |
| @@ -181,19 +198,18 @@ | ||
| 181 | 198 | (const FcChar8 *)ui->problemLabel.text, |
| 182 | 199 | strlen(ui->problemLabel.text), &extents); |
| 183 | 200 | |
| 184 | - int tx = w - 20 - extents.xOff; | |
| 201 | + int tx = w - label_padding - extents.xOff; | |
| 202 | + int ty = (int)(80 * scale + .5f); | |
| 185 | 203 | |
| 186 | 204 | 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); | |
| 190 | 207 | } |
| 191 | 208 | |
| 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); | |
| 197 | 213 | |
| 198 | 214 | if (initialized == 0) { |
| 199 | 215 | XftColorAllocName(ui->display, |
| @@ -204,26 +220,20 @@ | ||
| 204 | 220 | initialized = 1; |
| 205 | 221 | |
| 206 | 222 | for (int row = 1; row < NUM_ROWS; ++row) { |
| 207 | - int y = 162 + row * (height + padding); | |
| 208 | - | |
| 209 | 223 | 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 | + }; | |
| 212 | 235 | |
| 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); | |
| 227 | 237 | } |
| 228 | 238 | } |
| 229 | 239 | |
| @@ -233,7 +243,7 @@ | ||
| 233 | 243 | } |
| 234 | 244 | |
| 235 | 245 | 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); | |
| 237 | 247 | if (!btn) return; |
| 238 | 248 | |
| 239 | 249 | btn->pressed = 1; |
| @@ -252,7 +262,7 @@ | ||
| 252 | 262 | } |
| 253 | 263 | |
| 254 | 264 | 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); | |
| 256 | 266 | if (!btn) return; |
| 257 | 267 | |
| 258 | 268 | btn->pressed = 0; |
| @@ -283,16 +293,8 @@ | ||
| 283 | 293 | strcpy(ui->resLabel.text, curinput[0] ? curinput : "0"); |
| 284 | 294 | } else if (strcmp(label, "=") == 0) { |
| 285 | 295 | 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); | |
| 296 | 298 | } else if (strlen(label) == 1) { |
| 297 | 299 | append_to_input(ui, label[0]); |
| 298 | 300 | } |
| @@ -350,11 +352,8 @@ | ||
| 350 | 352 | |
| 351 | 353 | if (keysym == XK_Return || keysym == XK_KP_Enter) { |
| 352 | 354 | 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); | |
| 358 | 357 | goto redraw; |
| 359 | 358 | } |
| 360 | 359 |
| @@ -22,7 +22,7 @@ | ||
| 22 | 22 | int tx = btn->x + (btn->width - text_w) / 2; |
| 23 | 23 | int ty = btn->y + (btn->height - text_h) / 2 + ui->font->ascent; |
| 24 | 24 | |
| 25 | - XftDrawStringUtf8(xftdraw, &ui->color, ui->font, tx, ty, | |
| 25 | + XftDrawStringUtf8(xftdraw, &btn->fg_color, ui->font, tx, ty, | |
| 26 | 26 | (FcChar8 *)btn->text, len); |
| 27 | 27 | } |
| 28 | 28 | } |
変更
src/ui.h
| @@ -24,7 +24,7 @@ | ||
| 24 | 24 | |
| 25 | 25 | typedef struct { |
| 26 | 26 | int x, y, width, height; |
| 27 | - char text[32]; | |
| 27 | + char text[64]; | |
| 28 | 28 | XftFont *font; |
| 29 | 29 | XftColor fg_color; |
| 30 | 30 | } SuwaLabel; |
| @@ -43,5 +43,4 @@ | ||
| 43 | 43 | SuwaWindow window; |
| 44 | 44 | SuwaLabel resLabel; |
| 45 | 45 | SuwaLabel problemLabel; |
| 46 | - SuwaButton *buttons[32]; | |
| 47 | 46 | } UiSystem; |