suwako / UnixCalc アーカイブ

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

master
ブランチ master
UnixCalc / include / suwaui.h
16.7 KB、574行 生 Blame 履歴
1/**************************************************************************************
2# 076 Free License
3
4Copyright (c) 2026 テクニカル諏訪子
5
6Permission is hereby granted to any person obtaining a copy of the software
7SuwaUI (the "Software") to use, modify, merge, copy, publish, distribute,
8sublicense, and/or sell copies of the Software, subject to the following conditions:
9
10 1. **Origin Attribution**:
11 - You must not misrepresent the origin of the Software; you must not claim
12 you created the original Software.
13 2. **Notice Preservation**:
14 - This license and the above copyright notice must remain intact in all copies
15 of the source code.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
19PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
22OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23**************************************************************************************/
24#ifndef SUWAUI_H
25#define SUWAUI_H
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__)
36#include <X11/Xlib.h>
37#include <X11/Xft/Xft.h>
38#include <X11/Xatom.h>
39#define SUWAUI_UNIX
40#endif
41
42#include <stdio.h>
43
44#define COLOR_WHITE 0xfcfcfc
45#if defined(__OpenBSD__)
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
54#elif defined(__FreeBSD__)
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
99#endif
100
101#if defined(__cplusplus)
102extern "C" {
103#endif
104
105#if defined(SUWAUI_IMPLEMENTS_SUWAWINDOW)
106#include <string.h>
107
108typedef struct {
109 int x, y, width, height;
110 int start_width, start_height;
111 int isrunning;
112 const char *name;
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)
121 const char *res_name;
122 const char *class_name;
123 Display *display;
124 Window xwindow;
125 int screen;
126 Drawable target;
127 GC gc;
128 Visual visual;
129 XftColor color;
130 XftFont *font;
131 Colormap colormap;
132 Pixmap backbuf;
133 XEvent event;
134#endif
135} SuwaWindow;
136
137__attribute__((unused)) static void suwaui_destroy_window(SuwaWindow *w);
138
139__attribute__((unused)) static inline float get_scale(SuwaWindow *w) {
140#if defined(SUWAUI_WINDOWS)
141#elif defined(SUWAUI_UNIX)
142 XWindowAttributes attr;
143 XGetWindowAttributes(w->display, w->xwindow, &attr);
144 float sx = (float)attr.width / w->start_width;
145 float sy = (float)attr.height / w->start_height;
146#endif
147 return sx < sy ? sx : sy;
148}
149
150#if defined(SUWAUI_WINDOWS)
151LRESULT 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
219__attribute__((unused)) static SuwaWindow suwaui_create_window(int x, int y, int width, int height,
220 const char *software_name, const char *software_version,
221 const char *res_name, const char *class_name,
222 const char *font, const char *fg_color,
223 int is_centered, int cannot_shrink, int cannot_grow) {
224 (void)cannot_grow;
225
226 SuwaWindow w = {0};
227 w.isrunning = 1;
228 w.width = width;
229 w.height = height;
230 w.start_width = width;
231 w.start_height = height;
232 w.name = software_name;
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)
260 w.res_name = res_name;
261 w.class_name = class_name;
262
263 XGCValues values;
264
265 w.display = XOpenDisplay(NULL);
266 if (w.display == NULL) return (SuwaWindow){0};
267
268 w.screen = DefaultScreen(w.display);
269
270 if (is_centered > 0) {
271 int sw = DisplayWidth(w.display, w.screen);
272 int sh = DisplayHeight(w.display, w.screen);
273 w.x = (sw - w.width) / 2;
274 w.y = (sh - w.height) / 2;
275 } else {
276 w.x = x;
277 w.y = y;
278 }
279
280 w.xwindow = XCreateSimpleWindow(w.display,
281 RootWindow(w.display, w.screen),
282 w.x, w.y,
283 w.width, w.height,
284 1, COLOR_ND, COLOR_BD);
285 if (!w.xwindow) {
286 suwaui_destroy_window(&w);
287 return (SuwaWindow){0};
288 }
289
290 if (cannot_shrink) {
291 XSizeHints *sizeHint = XAllocSizeHints();
292 if (sizeHint) {
293 sizeHint->flags = PMinSize;
294 sizeHint->min_width = width;
295 sizeHint->min_height = height;
296 XSetWMNormalHints(w.display, w.xwindow, sizeHint);
297 XFree(sizeHint);
298 }
299 }
300
301 w.backbuf = XCreatePixmap(w.display, w.xwindow,
302 w.width, w.height,
303 DefaultDepth(w.display, w.screen));
304 w.target = w.backbuf;
305
306 Atom net_wm_window_type = XInternAtom(w.display, "_NET_WM_WINDOW_TYPE", False);
307 Atom dialog = XInternAtom(w.display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
308
309 XChangeProperty(w.display, w.xwindow, net_wm_window_type, XA_ATOM, 32,
310 PropModeReplace, (unsigned char *)&dialog, 1);
311
312 XStoreName(w.display, w.xwindow, w.name);
313 Atom net_wm_name = XInternAtom(w.display, "_NET_WM_NAME", False);
314 char displayname[16];
315 snprintf(displayname, 16, "%s %s", w.name, w.version);
316 XChangeProperty(w.display, w.xwindow, net_wm_name,
317 XInternAtom(w.display, "UTF8_STRING", False), 8,
318 PropModeReplace, (unsigned char *)displayname, strlen(displayname));
319
320 XClassHint *classHint = XAllocClassHint();
321 if (classHint) {
322 classHint->res_name = strdup(w.res_name);
323 classHint->res_class = strdup(w.class_name);
324 XSetClassHint(w.display, w.xwindow, classHint);
325 XFree(classHint);
326 }
327
328 XSetWindowBackground(w.display, w.xwindow, COLOR_BD);
329
330 XSelectInput(w.display, w.xwindow,
331 ExposureMask
332 | ButtonPressMask
333 | ButtonReleaseMask
334 | KeyPressMask
335 | PointerMotionMask
336 | Button1MotionMask
337 | EnterWindowMask
338 | LeaveWindowMask
339 // | StructureNotifyMask
340 );
341
342 w.gc = XCreateGC(w.display, w.xwindow, 0, &values);
343 if (!w.gc) {
344 suwaui_destroy_window(&w);
345 return (SuwaWindow){0};
346 }
347
348 w.visual = *DefaultVisual(w.display, w.screen);
349
350 w.colormap = XCreateColormap(w.display, w.xwindow,
351 &w.visual, AllocNone);
352 if (w.colormap == None) {
353 suwaui_destroy_window(&w);
354 return (SuwaWindow){0};
355 }
356
357 w.font = XftFontOpenName(w.display, w.screen, font);
358 if (!w.font) {
359 suwaui_destroy_window(&w);
360 return (SuwaWindow){0};
361 }
362
363 if (!XftColorAllocName(w.display, &w.visual,
364 w.colormap, fg_color, &w.color)) {
365 suwaui_destroy_window(&w);
366 return (SuwaWindow){0};
367 }
368#endif
369
370 return w;
371}
372
373__attribute__((unused)) static void suwaui_destroy_window(SuwaWindow *w) {
374#if defined(SUWAUI_WINDOWS)
375 if (w->back_bitmap) DeleteObject(w->back_bitmap);
376 if (w->hdc_mem) DeleteDC(w->hdc_mem);
377 if (w->hwnd) DestroyWindow(w->hwnd);
378#elif defined(SUWAUI_UNIX)
379 if (w->font) XftFontClose(w->display, w->font);
380
381 if (w->gc) XFreeGC(w->display, w->gc);
382 if (w->backbuf) {
383 XFreePixmap(w->display, w->backbuf);
384 w->backbuf = None;
385 }
386 if (w->xwindow) XDestroyWindow(w->display, w->xwindow);
387 if (w->display) XCloseDisplay(w->display);
388#endif
389
390 *w = (SuwaWindow){0};
391}
392#endif // SUWAUI_IMPLEMENTS_SUWAWINDOW
393
394#if defined(SUWAUI_IMPLEMENTS_SUWABUTTON)
395typedef struct {
396 int x, y, width, height;
397 int row, col;
398 const char *text;
399 XftFont *font;
400 unsigned long bg_color;
401 XftColor fg_color;
402 int pressed; // 0 = 普通、1 = 押している
403 int hovered;
404} SuwaButton;
405
406// __attribute__((unused)) static void suwaui_del_button(SuwaWindow *window, SuwaButton *button);
407
408__attribute__((unused)) static XftColor suwaui_set_button_fgcolor(SuwaWindow *window, const char *color) {
409 XftColor xcolor;
410 XftColorAllocName(window->display,
411 DefaultVisual(window->display, DefaultScreen(window->display)),
412 DefaultColormap(window->display, DefaultScreen(window->display)),
413 color, &xcolor);
414
415 return xcolor;
416}
417
418__attribute__((unused)) static void suwaui_draw_button(SuwaWindow *window, SuwaButton *btn, XftDraw *xftdraw) {
419 unsigned long curbg;
420 if (btn->pressed) curbg = COLOR_DD;
421 else if (btn->hovered) curbg = COLOR_LD;
422 else curbg = btn->bg_color;
423 XSetForeground(window->display, window->gc, curbg);
424 XFillRectangle(window->display, window->target, window->gc, btn->x, btn->y,
425 btn->width, btn->height);
426
427 // 文字の中央に
428 if (btn->text && window->font && xftdraw) {
429 const FcChar8 *str = (const FcChar8 *)btn->text;
430 int len = strlen((const char *)str);
431
432 XGlyphInfo extents;
433 XftTextExtentsUtf8(window->display, window->font, str, len, &extents);
434
435 int text_w = extents.xOff;
436 int text_h = window->font->ascent + window->font->descent;
437
438 int tx = btn->x + (btn->width - text_w) / 2;
439 int ty = btn->y + (btn->height - text_h) / 2 + window->font->ascent;
440
441 XftDrawStringUtf8(xftdraw, &btn->fg_color, window->font, tx, ty,
442 (FcChar8 *)btn->text, len);
443 }
444}
445
446__attribute__((unused)) static SuwaButton suwaui_add_button(int x, int y, int width, int height,
447 const char *text, XftFont *font, unsigned long bg_color, XftColor fg_color) {
448 SuwaButton button = {
449 .x = x,
450 .y = y,
451 .width = width,
452 .height = height,
453 .text = text,
454 .font = font,
455 .bg_color = bg_color,
456 .fg_color = fg_color,
457 .pressed = 0
458 };
459
460 return button;
461}
462#endif // SUWAUI_IMPLEMENTS_SUWABUTTON
463
464#if defined(SUWAUI_IMPLEMENTS_SUWALABEL)
465typedef struct {
466 int x, y, width, height;
467 char text[64];
468 XftFont *font;
469 XftColor fg_color;
470} SuwaLabel;
471
472__attribute__((unused)) static void suwaui_del_label(SuwaWindow *window, SuwaLabel *label);
473
474__attribute__((unused)) static SuwaLabel suwaui_add_label(SuwaWindow *window,
475 int x, int y, int width, int height, char text[64], const char *font,
476 const char *fg_color, int rtl) {
477 (void)width;
478 (void)height;
479 SuwaLabel label = {
480 .x = x,
481 .y = y,
482 .width = width,
483 .height = height
484 };
485
486 label.font = XftFontOpenName(window->display, window->screen, font);
487 if (!label.font) {
488 fprintf(stderr, "font create fail\n");
489 return (SuwaLabel){0};
490 }
491
492 strncpy(label.text, text ? text : "", sizeof(label.text) - 1);
493 label.text[sizeof(label.text)-1] = '\0';
494
495 const FcChar8 *txt = (const FcChar8 *)label.text;
496 int len = strlen((const char *)txt);
497
498 XGlyphInfo extents;
499 XftTextExtentsUtf8(window->display, label.font, txt, len, &extents);
500
501 if (rtl > 0) {
502 XWindowAttributes attr;
503 XGetWindowAttributes(window->display, window->xwindow, &attr);
504 int w = attr.width;
505 label.x = w - label.x - extents.xOff;
506 }
507
508 if (!XftColorAllocName(window->display,
509 DefaultVisual(window->display, DefaultScreen(window->display)),
510 DefaultColormap(window->display, DefaultScreen(window->display)),
511 fg_color, &label.fg_color)) {
512 suwaui_del_label(window, &label);
513 fprintf(stderr, "color estimate fail\n");
514 return (SuwaLabel){0};
515 }
516
517 return label;
518}
519
520__attribute__((unused)) static void suwaui_del_label(SuwaWindow *window, SuwaLabel *label) {
521 if (label->font) XftFontClose(window->display, label->font);
522}
523#endif // SUWAUI_IMPLEMENTS_SUWALABEL
524
525#if defined(__cplusplus)
526}
527#endif
528
529#if defined(SUWAUI_IMPLEMENTS_SUWATABS)
530typedef struct {
531 int x, y, width, height;
532} SuwaTab;
533
534typedef struct {
535 int x, y, width, height;
536} SuwaTabs;
537#endif // SUWAUI_IMPLEMENTS_SUWATABS
538
539#if defined(SUWAUI_IMPLEMENTS_SUWASIDE)
540typedef struct {
541 int x, y, width, height;
542} SuwaSide;
543#endif // SUWAUI_IMPLEMENTS_SUWASIDE
544
545#if defined(SUWAUI_IMPLEMENTS_SUWATEXTBOX)
546typedef struct {
547 int x, y, width, height;
548} SuwaTextbox;
549#endif // SUWAUI_IMPLEMENTS_SUWATEXTBOX
550
551#if defined(SUWAUI_IMPLEMENTS_SUWATEXTEDIT)
552typedef struct {
553 int x, y, width, height;
554} SuwaTextEdit;
555#endif // SUWAUI_IMPLEMENTS_SUWATEXTEDIT
556
557#if defined(SUWAUI_IMPLEMENTS_SUWAMENUBAR)
558typedef struct {
559 int x, y, width, height;
560} SuwaMenubar;
561#endif // SUWAUI_IMPLEMENTS_SUWAMENUBAR
562
563#if defined(SUWAUI_IMPLEMENTS_SUWADIALOG)
564typedef struct {
565 int x, y, width, height;
566} SuwaDialog;
567#endif // SUWAUI_IMPLEMENTS_SUWADIALOG
568
569#if defined(SUWAUI_IMPLEMENTS_SUWAIO)
570typedef struct {
571 int x, y, width, height;
572} SuwaIO;
573#endif // SUWAUI_IMPLEMENTS_SUWAIO
574#endif // SUWAUI_H