suwako / SuwaUI アーカイブ

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

色々追加した

諏訪子 <suwako@076.moe>、2026-01-19 22:17
コミット 2b9281e44fce7b525b3498adb6e8a20906f47817 ・親 be0a10f ・ファイルを見る

1個のファイル、+242 -37

変更 suwaui.h
@@ -21,25 +21,81 @@
2121 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
2222 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2323 **************************************************************************************/
24-#pragma once
24+#ifndef SUWAUI_H
25+#define SUWAUI_H
2526
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__)
2636 #include <X11/Xlib.h>
2737 #include <X11/Xft/Xft.h>
2838 #include <X11/Xatom.h>
39+#define SUWAUI_UNIX
40+#endif
2941
3042 #include <stdio.h>
3143
32-#define FGCOL 0xfcfcfc
44+#define COLOR_WHITE 0xfcfcfc
3345 #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
3854 #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
4399 #endif
44100
45101 #if defined(__cplusplus)
@@ -55,6 +111,13 @@
55111 int isrunning;
56112 const char *name;
57113 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)
58121 const char *res_name;
59122 const char *class_name;
60123 Display *display;
@@ -68,18 +131,91 @@
68131 Colormap colormap;
69132 Pixmap backbuf;
70133 XEvent event;
134+#endif
71135 } SuwaWindow;
72136
73137 __attribute__((unused)) static void suwaui_destroy_window(SuwaWindow *w);
74138
75139 __attribute__((unused)) static inline float get_scale(SuwaWindow *w) {
140+#if defined(SUWAUI_WINDOWS)
141+#elif defined(SUWAUI_UNIX)
76142 XWindowAttributes attr;
77143 XGetWindowAttributes(w->display, w->xwindow, &attr);
78144 float sx = (float)attr.width / w->start_width;
79145 float sy = (float)attr.height / w->start_height;
146+#endif
80147 return sx < sy ? sx : sy;
81148 }
82149
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+
83219 __attribute__((unused)) static SuwaWindow suwaui_create_window(int x, int y, int width, int height,
84220 const char *software_name, const char *software_version,
85221 const char *res_name, const char *class_name,
@@ -88,23 +224,46 @@
88224 (void)cannot_grow;
89225
90226 SuwaWindow w = {0};
227+ w.isrunning = 1;
91228 w.width = width;
92229 w.height = height;
93230 w.start_width = width;
94231 w.start_height = height;
95232 w.name = software_name;
96233 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)
97260 w.res_name = res_name;
98261 w.class_name = class_name;
99- w.isrunning = 1;
100262
101263 XGCValues values;
102264
103265 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};
108267
109268 w.screen = DefaultScreen(w.display);
110269
@@ -122,10 +281,9 @@
122281 RootWindow(w.display, w.screen),
123282 w.x, w.y,
124283 w.width, w.height,
125- 1, BTCOL, BGCOL);
284+ 1, COLOR_ND, COLOR_BD);
126285 if (!w.xwindow) {
127286 suwaui_destroy_window(&w);
128- fprintf(stderr, "window create fail\n");
129287 return (SuwaWindow){0};
130288 }
131289
@@ -167,22 +325,21 @@
167325 XFree(classHint);
168326 }
169327
170- XSetWindowBackground(w.display, w.xwindow, BGCOL);
328+ XSetWindowBackground(w.display, w.xwindow, COLOR_BD);
171329
172330 XSelectInput(w.display, w.xwindow,
173331 ExposureMask
174332 | ButtonPressMask
175333 | ButtonReleaseMask
176334 | KeyPressMask
177- // | PointerMotionMask
178- // | ButtonMotionMask
335+ | PointerMotionMask
336+ | ButtonMotionMask
179337 // | StructureNotifyMask
180338 );
181339
182340 w.gc = XCreateGC(w.display, w.xwindow, 0, &values);
183341 if (!w.gc) {
184342 suwaui_destroy_window(&w);
185- fprintf(stderr, "gc create fail\n");
186343 return (SuwaWindow){0};
187344 }
188345
@@ -192,28 +349,31 @@
192349 &w.visual, AllocNone);
193350 if (w.colormap == None) {
194351 suwaui_destroy_window(&w);
195- fprintf(stderr, "calor map create fail\n");
196352 return (SuwaWindow){0};
197353 }
198354
199355 w.font = XftFontOpenName(w.display, w.screen, font);
200356 if (!w.font) {
201357 suwaui_destroy_window(&w);
202- fprintf(stderr, "font create fail\n");
203358 return (SuwaWindow){0};
204359 }
205360
206361 if (!XftColorAllocName(w.display, &w.visual,
207362 w.colormap, fg_color, &w.color)) {
208363 suwaui_destroy_window(&w);
209- fprintf(stderr, "color estimate fail\n");
210364 return (SuwaWindow){0};
211365 }
366+#endif
212367
213368 return w;
214369 }
215370
216371 __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)
217377 if (w->font) XftFontClose(w->display, w->font);
218378
219379 if (w->gc) XFreeGC(w->display, w->gc);
@@ -223,27 +383,22 @@
223383 }
224384 if (w->xwindow) XDestroyWindow(w->display, w->xwindow);
225385 if (w->display) XCloseDisplay(w->display);
386+#endif
387+
388+ *w = (SuwaWindow){0};
226389 }
227390 #endif // SUWAUI_IMPLEMENTS_SUWAWINDOW
228391
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-
239392 #if defined(SUWAUI_IMPLEMENTS_SUWABUTTON)
240393 typedef struct {
241394 int x, y, width, height;
395+ int row, col;
242396 const char *text;
243397 XftFont *font;
244- int bg_color;
398+ unsigned long bg_color;
245399 XftColor fg_color;
246400 int pressed; // 0 = 普通、1 = 押している
401+ int hovered;
247402 } SuwaButton;
248403
249404 // __attribute__((unused)) static void suwaui_del_button(SuwaWindow *window, SuwaButton *button);
@@ -259,7 +414,10 @@
259414 }
260415
261416 __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;
263421 XSetForeground(window->display, window->gc, curbg);
264422 XFillRectangle(window->display, window->target, window->gc, btn->x, btn->y,
265423 btn->width, btn->height);
@@ -284,7 +442,7 @@
284442 }
285443
286444 __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) {
288446 SuwaButton button = {
289447 .x = x,
290448 .y = y,
@@ -365,3 +523,50 @@
365523 #if defined(__cplusplus)
366524 }
367525 #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