suwako / UnixCalc アーカイブ

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

マウスオーバー効果

諏訪子 <suwako@076.moe>、2026-01-21 23:50
コミット dd3be3a555c5aae19acf95c528046b620fb54363 ・親 01666a7 ・ファイルを見る

4個のファイル、+24 -21

変更 control.c
@@ -54,7 +54,7 @@
5454 char curinput[64] = {0};
5555 int input_pos = 0;
5656 int initialized = 0;
57-SuwaButton buttons[NUM_ROWS][NUM_COLS];
57+SuwaButton *lasthover = NULL;
5858
5959 static const char *btn_labels[][10] = {
6060 { NULL }, // 数字表示
@@ -195,7 +195,6 @@
195195 }
196196 }
197197 suwaui_draw_button(window, &btn, backdraw);
198- buttons[row][col] = btn;
199198 }
200199 }
201200 }
@@ -385,23 +384,14 @@
385384 control_expose(window, labels, &(SuwaButton){0});
386385 }
387386
388-void handle_mouse_hover(SuwaWindow *window, CtrlLabels *labels) {
389- int x = window->event.xmotion.x;
390- int y = window->event.xmotion.y;
391- SuwaButton *btn = find_button_at(window, x, y);
387+void handle_mouse_hover(SuwaWindow *window, CtrlLabels *labels, int mx, int my) {
388+ SuwaButton *btn = find_button_at(window, mx, my);
392389 if (!btn) return;
393390
394- btn->hovered = 1;
395- XftDraw *backdraw = XftDrawCreate(window->display, window->backbuf,
396- DefaultVisual(window->display, DefaultScreen(window->display)),
397- DefaultColormap(window->display, DefaultScreen(window->display)));
398- if (!backdraw) {
399- fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n");
400- XFreePixmap(window->display, window->backbuf);
401- window->backbuf = None;
402- return;
403- }
391+ if (lasthover && lasthover != btn) lasthover->hovered = 0;
392+ if (btn) btn->hovered = 1;
393+ lasthover = btn;
404394
405395 window->event = (XEvent){.type = Expose};
406- control_expose(window, labels, btn);
396+ control_expose(window, labels, btn ? btn : &(SuwaButton){0});
407397 }
変更 control.h
@@ -47,4 +47,4 @@
4747 void handle_button_press(SuwaWindow *window, CtrlLabels *labels, int mx, int my);
4848 void handle_button_release(SuwaWindow *window, CtrlLabels *labels, int mx, int my);
4949 void handle_key_press(SuwaWindow *window, CtrlLabels *labels);
50-void handle_mouse_hover(SuwaWindow *window, CtrlLabels *labels);
50+void handle_mouse_hover(SuwaWindow *window, CtrlLabels *labels, int mx, int my);
@@ -332,8 +332,8 @@
332332 | ButtonPressMask
333333 | ButtonReleaseMask
334334 | KeyPressMask
335- // | PointerMotionMask
336- // | ButtonMotionMask
335+ | PointerMotionMask
336+ | Button1MotionMask
337337 // | StructureNotifyMask
338338 );
339339
変更 main.c
@@ -43,6 +43,8 @@
4343 const char *version = "0.0.0";
4444 const char *disname = "Unix Calc";
4545
46+static Time lasthovertime = 0;
47+
4648 int main() {
4749 #if defined(__OpenBSD__)
4850 SuwaWindow window = suwaui_create_window(
@@ -109,7 +111,18 @@
109111 handle_key_press(&window, &lbl);
110112 break;
111113 case MotionNotify:
112- handle_mouse_hover(&window, &lbl);
114+ Time now = window.event.xmotion.time;
115+ if (now - lasthovertime < 40) break;
116+ lasthovertime = now;
117+ Window root, child;
118+ int root_x, root_y,
119+ win_x, win_y;
120+ unsigned int mask;
121+
122+ XQueryPointer(window.display, window.xwindow, &root, &child,
123+ &root_x, &root_y, &win_x, &win_y, &mask);
124+ if (window.event.xmotion.state == 0) break;
125+ handle_mouse_hover(&window, &lbl, win_x, win_y);
113126 break;
114127 case ClientMessage:
115128 // WM_DELETE_WINDOW