Task 4: route ButtonRelease, MotionNotify and KeyPress to the right widget in generated apps

Generated event loop now tracks pressed_widget (set on a handled ButtonPress) and focused_widget (click-to-focus). ButtonRelease goes to the pressed widget even if the pointer moved off it; MotionNotify goes to the pressed widget while a press is held (drag), otherwise to the topmost widget under the pointer; KeyPress goes to the focused widget only. ButtonPress hit-testing, Expose, ConfigureNotify and ClientMessage unchanged. Verified live under Xvfb with a generated 4-widget app (2 buttons + 2 entries) and an instrumented event log: keys go only to the focused entry, hover only to the widget under the pointer, drag motion and release only to the pressed button. The designer canvas does NOT share this loop (it already targets release/motion/keys by selection/hit-test), so it is unchanged.
This commit is contained in:
cnmcdee committed 2026-12-02 05:30:16 +00:00
1 parent 56a34bb1cf
commit 541fd842dc
1 file changed
+26 -6
+26 -6
View File
@@ -2318,6 +2318,11 @@ static std::string emit_main_cpp(const Project& project,
<< " };\n"
<< " redraw_all();\n"
<< " bool running = true;\n"
<< " // Task 4: event routing. pressed_widget is the widget that\n"
<< " // received the ButtonPress (release + drag motion go there);\n"
<< " // focused_widget is the last widget clicked (keys go there).\n"
<< " lg::IWidget* pressed_widget = nullptr;\n"
<< " lg::IWidget* focused_widget = nullptr;\n"
<< " while (running) {\n"
<< " XEvent event;\n"
<< " XNextEvent(display, &event);\n"
@@ -2331,25 +2336,40 @@ static std::string emit_main_cpp(const Project& project,
<< " for (auto it = widgets.rbegin(); it != widgets.rend(); ++it) {\n"
<< " if ((*it)->contains(event.xbutton.x, event.xbutton.y)) {\n"
<< " handled = (*it)->on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button);\n"
<< " if (handled) { pressed_widget = *it; focused_widget = *it; }\n"
<< " break;\n"
<< " }\n"
<< " }\n"
<< " break;\n"
<< " case ButtonRelease:\n"
<< " for (lg::IWidget* wd : widgets)\n"
<< " handled = wd->on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button) || handled;\n"
<< " // Release goes to the widget that got the press,\n"
<< " // even if the pointer moved off it.\n"
<< " if (pressed_widget) {\n"
<< " handled = pressed_widget->on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button);\n"
<< " pressed_widget = nullptr;\n"
<< " }\n"
<< " break;\n"
<< " case MotionNotify:\n"
<< " for (lg::IWidget* wd : widgets)\n"
<< " handled = wd->on_motion(event.xmotion.x, event.xmotion.y) || handled;\n"
<< " if (pressed_widget) {\n"
<< " // Drag: motion goes to the pressed widget.\n"
<< " handled = pressed_widget->on_motion(event.xmotion.x, event.xmotion.y);\n"
<< " } else {\n"
<< " // Hover: topmost widget under the pointer.\n"
<< " for (auto it = widgets.rbegin(); it != widgets.rend(); ++it) {\n"
<< " if ((*it)->contains(event.xmotion.x, event.xmotion.y)) {\n"
<< " handled = (*it)->on_motion(event.xmotion.x, event.xmotion.y);\n"
<< " break;\n"
<< " }\n"
<< " }\n"
<< " }\n"
<< " break;\n"
<< " case KeyPress: {\n"
<< " char buffer[32];\n"
<< " KeySym key = 0;\n"
<< " int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr);\n"
<< " buffer[n] = 0;\n"
<< " for (lg::IWidget* wd : widgets)\n"
<< " handled = wd->on_key(key, buffer) || handled;\n"
<< " if (focused_widget)\n"
<< " handled = focused_widget->on_key(key, buffer);\n"
<< " break;\n"
<< " }\n"
<< " case ConfigureNotify:\n";