色々追加した
諏訪子 <suwako@076.moe>、2026-01-19 22:17
コミット 2b9281e44fce7b525b3498adb6e8a20906f47817
・親 be0a10f
・ファイルを見る
1個のファイル、+242 -37
変更
suwaui.h
| @@ -21,25 +21,81 @@ | ||
| 21 | 21 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 22 | 22 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | 23 | **************************************************************************************/ |
| 24 | -#pragma once | |
| 24 | +#ifndef SUWAUI_H | |
| 25 | +#define SUWAUI_H | |
| 25 | 26 | |
| 27 | +#if defined(_WIN32) || defined(_WIN64) | |
| 28 | +#if defined(_WIN32) | |
| 29 | +#define WIN32_LEAN_AND_MEAN | |
| 30 | +#else | |
| 31 | +#define WIN64_LEAN_AND_MEAN | |
| 32 | +#endif | |
| 33 | +#include <windows.h> | |
| 34 | +#define SUWAUI_WINDOWS | |
| 35 | +#elif defined(__unix__) || defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__sunos) || defined(__apple__) | |
| 26 | 36 | #include <X11/Xlib.h> |
| 27 | 37 | #include <X11/Xft/Xft.h> |
| 28 | 38 | #include <X11/Xatom.h> |
| 39 | +#define SUWAUI_UNIX | |
| 40 | +#endif | |
| 29 | 41 | |
| 30 | 42 | #include <stdio.h> |
| 31 | 43 | |
| 32 | -#define FGCOL 0xfcfcfc | |
| 44 | +#define COLOR_WHITE 0xfcfcfc | |
| 33 | 45 | #if defined(__OpenBSD__) |
| 34 | -#define BGCOL 0x232320 | |
| 35 | -#define BTSEL 0xb8b515 | |
| 36 | -#define BTCOL 0xf1ed25 | |
| 37 | -#define BTHVR 0xecea71 | |
| 46 | +#define COLOR_BD 0x232320 // color black decimal | |
| 47 | +#define COLOR_BS "#232320" // color black string | |
| 48 | +#define COLOR_DD 0xb8b515 // color dark decimal | |
| 49 | +#define COLOR_DS "#b8b515" // color dark string | |
| 50 | +#define COLOR_ND 0xf1ed25 // color normal decimal | |
| 51 | +#define COLOR_NS "#f1ed25" // color normal string | |
| 52 | +#define COLOR_LD 0xecea71 // color light decimal | |
| 53 | +#define COLOR_LS "#ecea71" // color light string | |
| 38 | 54 | #elif defined(__FreeBSD__) |
| 39 | -#define BGCOL 0x232020 | |
| 40 | -#define BTSEL 0xb61729 | |
| 41 | -#define BTCOL 0xee4030 | |
| 42 | -#define BTHVR 0xf35869 | |
| 55 | +#define COLOR_BD 0x232020 // color black decimal | |
| 56 | +#define COLOR_BS "#232020" // color black string | |
| 57 | +#define COLOR_DD 0xb61729 // color dark decimal | |
| 58 | +#define COLOR_DS "#b61729" // color dark string | |
| 59 | +#define COLOR_ND 0xee4030 // color normal decimal | |
| 60 | +#define COLOR_NS "#ee4030" // color normal string | |
| 61 | +#define COLOR_LD 0xf35869 // color light decimal | |
| 62 | +#define COLOR_LS "#f35869" // color light string | |
| 63 | +#elif defined(__NetBSD__) | |
| 64 | +#define COLOR_BD 0x232018 // color black decimal | |
| 65 | +#define COLOR_BS "#232018" // color black string | |
| 66 | +#define COLOR_DD 0xac7718 // color dark decimal | |
| 67 | +#define COLOR_DS "#ac7718" // color dark string | |
| 68 | +#define COLOR_ND 0xf7a717 // color normal decimal | |
| 69 | +#define COLOR_NS "#f7a717" // color normal string | |
| 70 | +#define COLOR_LD 0xf8c56a // color light decimal | |
| 71 | +#define COLOR_LS "#f8c56a" // color light string | |
| 72 | +#elif defined(SUWAUI_WINDOWS) | |
| 73 | +#define COLOR_BD 0x202023 // color black decimal | |
| 74 | +#define COLOR_BS "#202023" // color black string | |
| 75 | +#define COLOR_DD 0x1a6ecf // color dark decimal | |
| 76 | +#define COLOR_DS "#1a6ecf" // color dark string | |
| 77 | +#define COLOR_ND 0x2687f7 // color normal decimal | |
| 78 | +#define COLOR_NS "#2687f7" // color normal string | |
| 79 | +#define COLOR_LD 0x6aa6eb // color light decimal | |
| 80 | +#define COLOR_LS "#6aa6eb" // color light string | |
| 81 | +#elif defined(__apple__) | |
| 82 | +#define COLOR_BD 0x232323 // color black decimal | |
| 83 | +#define COLOR_BS "#232323" // color black string | |
| 84 | +#define COLOR_DD 0x746c74 // color dark decimal | |
| 85 | +#define COLOR_DS "#746c74" // color dark string | |
| 86 | +#define COLOR_ND 0x988f98 // color normal decimal | |
| 87 | +#define COLOR_NS "#988f98" // color normal string | |
| 88 | +#define COLOR_LD 0xbdb4bd // color light decimal | |
| 89 | +#define COLOR_LS "#bdb4bd" // color light string | |
| 90 | +#elif defined(__sunos) | |
| 91 | +#define COLOR_BD 0x182023 // color black decimal | |
| 92 | +#define COLOR_BS "#182023" // color black string | |
| 93 | +#define COLOR_DD 0x1cbcd0 // color dark decimal | |
| 94 | +#define COLOR_DS "#1cbcd0" // color dark string | |
| 95 | +#define COLOR_ND 0x29d3ff // color normal decimal | |
| 96 | +#define COLOR_NS "#29d3ff" // color normal string | |
| 97 | +#define COLOR_LD 0x8ae5ff // color light decimal | |
| 98 | +#define COLOR_LS "#8ae5ff" // color light string | |
| 43 | 99 | #endif |
| 44 | 100 | |
| 45 | 101 | #if defined(__cplusplus) |
| @@ -55,6 +111,13 @@ | ||
| 55 | 111 | int isrunning; |
| 56 | 112 | const char *name; |
| 57 | 113 | const char *version; |
| 114 | +#if defined(SUWAUI_WINDOWS) | |
| 115 | + HWND hwnd; | |
| 116 | + HDC hdc_mem; | |
| 117 | + HBITMAP backbitmap; | |
| 118 | + HFONT hfont; | |
| 119 | + COLORREF fg_color; | |
| 120 | +#elif defined(SUWAUI_UNIX) | |
| 58 | 121 | const char *res_name; |
| 59 | 122 | const char *class_name; |
| 60 | 123 | Display *display; |
| @@ -68,18 +131,91 @@ | ||
| 68 | 131 | Colormap colormap; |
| 69 | 132 | Pixmap backbuf; |
| 70 | 133 | XEvent event; |
| 134 | +#endif | |
| 71 | 135 | } SuwaWindow; |
| 72 | 136 | |
| 73 | 137 | __attribute__((unused)) static void suwaui_destroy_window(SuwaWindow *w); |
| 74 | 138 | |
| 75 | 139 | __attribute__((unused)) static inline float get_scale(SuwaWindow *w) { |
| 140 | +#if defined(SUWAUI_WINDOWS) | |
| 141 | +#elif defined(SUWAUI_UNIX) | |
| 76 | 142 | XWindowAttributes attr; |
| 77 | 143 | XGetWindowAttributes(w->display, w->xwindow, &attr); |
| 78 | 144 | float sx = (float)attr.width / w->start_width; |
| 79 | 145 | float sy = (float)attr.height / w->start_height; |
| 146 | +#endif | |
| 80 | 147 | return sx < sy ? sx : sy; |
| 81 | 148 | } |
| 82 | 149 | |
| 150 | +#if defined(SUWAUI_WINDOWS) | |
| 151 | +LRESULT CALLBACK SuwaWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { | |
| 152 | + SuwaWindow *w = (SuwaWindow *)GetWindowLongPtr(hwnd, GWLP_USERDATA); | |
| 153 | + | |
| 154 | + switch (msg) { | |
| 155 | + case WM_CREATE: | |
| 156 | + LPCREATESTRUCT cs = (LPCREATESTRUCT)lParam; | |
| 157 | + w = (SuwaWindow *)cs->lpCreateParams; | |
| 158 | + SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)w); | |
| 159 | + return 0 | |
| 160 | + case WM_SIZE: | |
| 161 | + if (w == NULL) break; | |
| 162 | + | |
| 163 | + int new_w = LOWORD(lParam); | |
| 164 | + int new_h = HIWORD(lParam); | |
| 165 | + | |
| 166 | + if (w->back_bitmap != NULL) { | |
| 167 | + DeleteObject(w->back_bitmap); | |
| 168 | + w->back_bitmap = NULL; | |
| 169 | + } | |
| 170 | + | |
| 171 | + HDC hdc = GetDC(hwnd); | |
| 172 | + if (hdc) { | |
| 173 | + w->back_bitmap = CreateCompatibleBitmap(hdc, new_w, new_h); | |
| 174 | + if (w->back_bitmap) { | |
| 175 | + SelectObject(w->hdc_memm, w->back_bitmap); | |
| 176 | + } | |
| 177 | + ReleaseDC(hwnd, hdc); | |
| 178 | + } | |
| 179 | + | |
| 180 | + w->width = new_w; | |
| 181 | + w->height = new_h; | |
| 182 | + | |
| 183 | + InvalidateRect(hwnd, NULL, FALSE); | |
| 184 | + return 0; | |
| 185 | + case WM_PAINT: | |
| 186 | + PAINTSTRUCT ps; | |
| 187 | + HDC hdc = BeginPaint(hwnd, &ps); | |
| 188 | + | |
| 189 | + if (w && w->hdc_mem) { | |
| 190 | + RECT client; | |
| 191 | + GetClientRect(hwnd, &client); | |
| 192 | + FillRect(w->hdc_mem, &client, (HBRUSH)GetStockObject(COLOR_BD)); | |
| 193 | + | |
| 194 | + // suwaui_on_draw(w); | |
| 195 | + | |
| 196 | + BitBlt(hdc, 0, 0, w->width, w->height, w->hdc_mem, 0, 0, SRCCOPY); | |
| 197 | + } | |
| 198 | + | |
| 199 | + EndPaint(hwnd, &ps); | |
| 200 | + return 0; | |
| 201 | + // case WM_ERASEBKGND: | |
| 202 | + // return 1; | |
| 203 | + // case WM_LBUTTONDOWN: | |
| 204 | + // case WM_LBUTTONUP: | |
| 205 | + // case WM_MOUSEMOVE: | |
| 206 | + // case WM_KEYDOWN: | |
| 207 | + // case WM_KEYUP: | |
| 208 | + case WM_DESTROY: | |
| 209 | + PostQuitMessage(0); | |
| 210 | + return 0; | |
| 211 | + default: | |
| 212 | + return DefWindowProc(hwnd, msg, wParam, lParam); | |
| 213 | + } | |
| 214 | + | |
| 215 | + return DefWindowProc(hwnd, msg, wParam, lParam); | |
| 216 | +} | |
| 217 | +#endif | |
| 218 | + | |
| 83 | 219 | __attribute__((unused)) static SuwaWindow suwaui_create_window(int x, int y, int width, int height, |
| 84 | 220 | const char *software_name, const char *software_version, |
| 85 | 221 | const char *res_name, const char *class_name, |
| @@ -88,23 +224,46 @@ | ||
| 88 | 224 | (void)cannot_grow; |
| 89 | 225 | |
| 90 | 226 | SuwaWindow w = {0}; |
| 227 | + w.isrunning = 1; | |
| 91 | 228 | w.width = width; |
| 92 | 229 | w.height = height; |
| 93 | 230 | w.start_width = width; |
| 94 | 231 | w.start_height = height; |
| 95 | 232 | w.name = software_name; |
| 96 | 233 | w.version = software_version; |
| 234 | +#if defined(SUWAUI_WINDOWS) | |
| 235 | + WNDCLASSEX wc = { sizeof(WNDCLASSEX) }; | |
| 236 | + wc.style = CS_HREDRAW | CS_VREDRAW; | |
| 237 | + wc.lpfnWndProc = SuwaWindowsProc; | |
| 238 | + wc.hInstance = GetModuleHandle(NULL); | |
| 239 | + wc.hCursor = LoadCursor(NULL, IDCARROW); | |
| 240 | + wc.hbrBackground = NULL; | |
| 241 | + wc.lpszClassName = TEXT("SuwaWindowClass"); | |
| 242 | + | |
| 243 | + RegisterClassEx(&wc); | |
| 244 | + | |
| 245 | + DWORD style = WS_OVERLAPPEDWINDOW; | |
| 246 | + | |
| 247 | + w.hwnd = CreateWindowEx(0, wc.lpszClassName, name, style, | |
| 248 | + x, y, width, height, NULL, NULL, wc.hInstance, NULL); | |
| 249 | + if (!w.hwnd) return (SUwaWindow){0}; | |
| 250 | + | |
| 251 | + HDC hdc = GetDC(w.hwnd); | |
| 252 | + w.hdc_mem = CreateCompatibleDC(hdc); | |
| 253 | + w.back_bitmap = CreateCompatibleBitmap(hdc, width, height); | |
| 254 | + SelectObject(w.hdc_mem, w.back_bitmap); | |
| 255 | + ReleaseDC(w.hwnd, hdc); | |
| 256 | + | |
| 257 | + ShowWindow(w.hwnd, hdc); | |
| 258 | + UpdateWindow(w.hwnd); | |
| 259 | +#elif defined(SUWAUI_UNIX) | |
| 97 | 260 | w.res_name = res_name; |
| 98 | 261 | w.class_name = class_name; |
| 99 | - w.isrunning = 1; | |
| 100 | 262 | |
| 101 | 263 | XGCValues values; |
| 102 | 264 | |
| 103 | 265 | w.display = XOpenDisplay(NULL); |
| 104 | - if (w.display == NULL) { | |
| 105 | - fprintf(stderr, "screen open fail\n"); | |
| 106 | - return (SuwaWindow){0}; | |
| 107 | - } | |
| 266 | + if (w.display == NULL) return (SuwaWindow){0}; | |
| 108 | 267 | |
| 109 | 268 | w.screen = DefaultScreen(w.display); |
| 110 | 269 | |
| @@ -122,10 +281,9 @@ | ||
| 122 | 281 | RootWindow(w.display, w.screen), |
| 123 | 282 | w.x, w.y, |
| 124 | 283 | w.width, w.height, |
| 125 | - 1, BTCOL, BGCOL); | |
| 284 | + 1, COLOR_ND, COLOR_BD); | |
| 126 | 285 | if (!w.xwindow) { |
| 127 | 286 | suwaui_destroy_window(&w); |
| 128 | - fprintf(stderr, "window create fail\n"); | |
| 129 | 287 | return (SuwaWindow){0}; |
| 130 | 288 | } |
| 131 | 289 | |
| @@ -167,22 +325,21 @@ | ||
| 167 | 325 | XFree(classHint); |
| 168 | 326 | } |
| 169 | 327 | |
| 170 | - XSetWindowBackground(w.display, w.xwindow, BGCOL); | |
| 328 | + XSetWindowBackground(w.display, w.xwindow, COLOR_BD); | |
| 171 | 329 | |
| 172 | 330 | XSelectInput(w.display, w.xwindow, |
| 173 | 331 | ExposureMask |
| 174 | 332 | | ButtonPressMask |
| 175 | 333 | | ButtonReleaseMask |
| 176 | 334 | | KeyPressMask |
| 177 | - // | PointerMotionMask | |
| 178 | - // | ButtonMotionMask | |
| 335 | + | PointerMotionMask | |
| 336 | + | ButtonMotionMask | |
| 179 | 337 | // | StructureNotifyMask |
| 180 | 338 | ); |
| 181 | 339 | |
| 182 | 340 | w.gc = XCreateGC(w.display, w.xwindow, 0, &values); |
| 183 | 341 | if (!w.gc) { |
| 184 | 342 | suwaui_destroy_window(&w); |
| 185 | - fprintf(stderr, "gc create fail\n"); | |
| 186 | 343 | return (SuwaWindow){0}; |
| 187 | 344 | } |
| 188 | 345 | |
| @@ -192,28 +349,31 @@ | ||
| 192 | 349 | &w.visual, AllocNone); |
| 193 | 350 | if (w.colormap == None) { |
| 194 | 351 | suwaui_destroy_window(&w); |
| 195 | - fprintf(stderr, "calor map create fail\n"); | |
| 196 | 352 | return (SuwaWindow){0}; |
| 197 | 353 | } |
| 198 | 354 | |
| 199 | 355 | w.font = XftFontOpenName(w.display, w.screen, font); |
| 200 | 356 | if (!w.font) { |
| 201 | 357 | suwaui_destroy_window(&w); |
| 202 | - fprintf(stderr, "font create fail\n"); | |
| 203 | 358 | return (SuwaWindow){0}; |
| 204 | 359 | } |
| 205 | 360 | |
| 206 | 361 | if (!XftColorAllocName(w.display, &w.visual, |
| 207 | 362 | w.colormap, fg_color, &w.color)) { |
| 208 | 363 | suwaui_destroy_window(&w); |
| 209 | - fprintf(stderr, "color estimate fail\n"); | |
| 210 | 364 | return (SuwaWindow){0}; |
| 211 | 365 | } |
| 366 | +#endif | |
| 212 | 367 | |
| 213 | 368 | return w; |
| 214 | 369 | } |
| 215 | 370 | |
| 216 | 371 | __attribute__((unused)) static void suwaui_destroy_window(SuwaWindow *w) { |
| 372 | +#if defined(SUWAUI_WINDOWS) | |
| 373 | + if (w->back_bitmap) DeleteObject(w->back_bitmap); | |
| 374 | + if (w->hdc_mem) DeleteDC(w->hdc_mem); | |
| 375 | + if (w->hwnd) DestroyWindow(w->hwnd); | |
| 376 | +#elif defined(SUWAUI_UNIX) | |
| 217 | 377 | if (w->font) XftFontClose(w->display, w->font); |
| 218 | 378 | |
| 219 | 379 | if (w->gc) XFreeGC(w->display, w->gc); |
| @@ -223,27 +383,22 @@ | ||
| 223 | 383 | } |
| 224 | 384 | if (w->xwindow) XDestroyWindow(w->display, w->xwindow); |
| 225 | 385 | if (w->display) XCloseDisplay(w->display); |
| 386 | +#endif | |
| 387 | + | |
| 388 | + *w = (SuwaWindow){0}; | |
| 226 | 389 | } |
| 227 | 390 | #endif // SUWAUI_IMPLEMENTS_SUWAWINDOW |
| 228 | 391 | |
| 229 | -#if defined(SUWAUI_IMPLEMENTS_SUWATABS) | |
| 230 | -typedef struct { | |
| 231 | - int x, y, width, height; | |
| 232 | -} SuwaTab; | |
| 233 | - | |
| 234 | -typedef struct { | |
| 235 | - int x, y, width, height; | |
| 236 | -} SuwaTabs; | |
| 237 | -#endif // SUWAUI_IMPLEMENTS_SUWATABS | |
| 238 | - | |
| 239 | 392 | #if defined(SUWAUI_IMPLEMENTS_SUWABUTTON) |
| 240 | 393 | typedef struct { |
| 241 | 394 | int x, y, width, height; |
| 395 | + int row, col; | |
| 242 | 396 | const char *text; |
| 243 | 397 | XftFont *font; |
| 244 | - int bg_color; | |
| 398 | + unsigned long bg_color; | |
| 245 | 399 | XftColor fg_color; |
| 246 | 400 | int pressed; // 0 = 普通、1 = 押している |
| 401 | + int hovered; | |
| 247 | 402 | } SuwaButton; |
| 248 | 403 | |
| 249 | 404 | // __attribute__((unused)) static void suwaui_del_button(SuwaWindow *window, SuwaButton *button); |
| @@ -259,7 +414,10 @@ | ||
| 259 | 414 | } |
| 260 | 415 | |
| 261 | 416 | __attribute__((unused)) static void suwaui_draw_button(SuwaWindow *window, SuwaButton *btn, XftDraw *xftdraw) { |
| 262 | - unsigned long curbg = btn->pressed ? BTSEL : btn->bg_color; | |
| 417 | + unsigned long curbg; | |
| 418 | + if (btn->pressed) curbg = COLOR_DD; | |
| 419 | + else if (btn->hovered) curbg = COLOR_LD; | |
| 420 | + else curbg = btn->bg_color; | |
| 263 | 421 | XSetForeground(window->display, window->gc, curbg); |
| 264 | 422 | XFillRectangle(window->display, window->target, window->gc, btn->x, btn->y, |
| 265 | 423 | btn->width, btn->height); |
| @@ -284,7 +442,7 @@ | ||
| 284 | 442 | } |
| 285 | 443 | |
| 286 | 444 | __attribute__((unused)) static SuwaButton suwaui_add_button(int x, int y, int width, int height, |
| 287 | - const char *text, XftFont *font, int bg_color, XftColor fg_color) { | |
| 445 | + const char *text, XftFont *font, unsigned long bg_color, XftColor fg_color) { | |
| 288 | 446 | SuwaButton button = { |
| 289 | 447 | .x = x, |
| 290 | 448 | .y = y, |
| @@ -365,3 +523,50 @@ | ||
| 365 | 523 | #if defined(__cplusplus) |
| 366 | 524 | } |
| 367 | 525 | #endif |
| 526 | + | |
| 527 | +#if defined(SUWAUI_IMPLEMENTS_SUWATABS) | |
| 528 | +typedef struct { | |
| 529 | + int x, y, width, height; | |
| 530 | +} SuwaTab; | |
| 531 | + | |
| 532 | +typedef struct { | |
| 533 | + int x, y, width, height; | |
| 534 | +} SuwaTabs; | |
| 535 | +#endif // SUWAUI_IMPLEMENTS_SUWATABS | |
| 536 | + | |
| 537 | +#if defined(SUWAUI_IMPLEMENTS_SUWASIDE) | |
| 538 | +typedef struct { | |
| 539 | + int x, y, width, height; | |
| 540 | +} SuwaSide; | |
| 541 | +#endif // SUWAUI_IMPLEMENTS_SUWASIDE | |
| 542 | + | |
| 543 | +#if defined(SUWAUI_IMPLEMENTS_SUWATEXTBOX) | |
| 544 | +typedef struct { | |
| 545 | + int x, y, width, height; | |
| 546 | +} SuwaTextbox; | |
| 547 | +#endif // SUWAUI_IMPLEMENTS_SUWATEXTBOX | |
| 548 | + | |
| 549 | +#if defined(SUWAUI_IMPLEMENTS_SUWATEXTEDIT) | |
| 550 | +typedef struct { | |
| 551 | + int x, y, width, height; | |
| 552 | +} SuwaTextEdit; | |
| 553 | +#endif // SUWAUI_IMPLEMENTS_SUWATEXTEDIT | |
| 554 | + | |
| 555 | +#if defined(SUWAUI_IMPLEMENTS_SUWAMENUBAR) | |
| 556 | +typedef struct { | |
| 557 | + int x, y, width, height; | |
| 558 | +} SuwaMenubar; | |
| 559 | +#endif // SUWAUI_IMPLEMENTS_SUWAMENUBAR | |
| 560 | + | |
| 561 | +#if defined(SUWAUI_IMPLEMENTS_SUWADIALOG) | |
| 562 | +typedef struct { | |
| 563 | + int x, y, width, height; | |
| 564 | +} SuwaDialog; | |
| 565 | +#endif // SUWAUI_IMPLEMENTS_SUWADIALOG | |
| 566 | + | |
| 567 | +#if defined(SUWAUI_IMPLEMENTS_SUWAIO) | |
| 568 | +typedef struct { | |
| 569 | + int x, y, width, height; | |
| 570 | +} SuwaIO; | |
| 571 | +#endif // SUWAUI_IMPLEMENTS_SUWAIO | |
| 572 | +#endif // SUWAUI_H |