Event handlers on all widgets: signal table, Events pane, generated stubs, tests

- src/events.h (new): natural GTK signal per widget type for all 36
  types (27 with signals, 9 with an explicit no-events note); kinds
  cover simple, switch-page, row-activated, row-selected, text-buffer,
  delete-event, draw, per-item toolbar/menu, and spreadsheet cells.
- Generator emits a stub handler + g_signal_connect for every
  connected signal (Widget.signals); Shape/Plot/StockChart draw stays
  wired exactly once. Stubs print an EVENT trace line under
  FDESIGNER_EVENT_TRACE (test harness only).
- Designer: Events section in the property pane — one check button
  per signal, builtin draw shown as always-connected, no-event types
  show the reason. Connections persist in .fdj as "signals".
- Self-test Phase 8: UI check-button toggle, all-signal generation
  checks, .fdj round-trip.
- tests/event_handler_tests.sh/.cpp + event_handler_runtime.cpp:
  36/36 generation markers, warning-free generated build, 41/41
  runtime triggers under Xvfb.
This commit is contained in:
Muse committed 2026-11-25 11:54:50 -05:00
1 parent 7828ced3d6
commit 0492137214
8 files changed
+1252 -14

No files matched your search

+1 -1
View File
@@ -3,7 +3,7 @@ CXXFLAGS ?= -O2 -Wall -Wextra -std=c++17
GTKFLAGS := $(shell pkg-config --cflags gtk+-3.0)
GTKLIBS := $(shell pkg-config --libs gtk+-3.0)
fdesigner: src/main.cpp src/generator.cpp src/project_io.cpp src/debugger.cpp src/debugger_ui.cpp src/model.h src/props.h src/generator.h src/project_io.h src/debugger.h src/debugger_ui.h src/indicators.h src/indicators_embedded.h src/drawings.h src/drawings_embedded.h ui/designer.ui
fdesigner: src/main.cpp src/generator.cpp src/project_io.cpp src/debugger.cpp src/debugger_ui.cpp src/model.h src/props.h src/events.h src/generator.h src/project_io.h src/debugger.h src/debugger_ui.h src/indicators.h src/indicators_embedded.h src/drawings.h src/drawings_embedded.h ui/designer.ui
$(CXX) $(CXXFLAGS) -o $@ src/main.cpp src/generator.cpp src/project_io.cpp src/debugger.cpp src/debugger_ui.cpp $(GTKFLAGS) $(GTKLIBS)
src/indicators_embedded.h: src/indicators.h tools/embed_indicators.sh
+227
View File
@@ -0,0 +1,227 @@
// FDesigner event table — the natural GTK signal(s) for every widget type.
//
// Every palette type is listed here exactly once: either with the signals
// a user can connect (each gets a generated stub handler, connected via
// g_signal_connect in the generated widget class) or with an explicit
// "no events" note saying why it has none. NONE is left unexamined.
// The table drives three things: the designer's Events pane (main.cpp),
// the code generator (generator.cpp), and the event tests.
//
// Signal kinds (how the generator wires each one):
// Simple — void on_x(); trampoline (GtkWidget*, gpointer)
// SwitchPage — void on_x(guint page); notebook "switch-page"
// RowActivated — void on_x(GtkTreePath* path); tree "row-activated"
// RowSelected — void on_x(); trampoline (GtkListBox*, GtkListBoxRow*, gpointer)
// BufferChanged — void on_x(); connected to the TextView's GtkTextBuffer
// DeleteEvent — bool on_x(); trampoline (GtkWidget*, GdkEvent*, gpointer)
// Draw — static gboolean on_draw(GtkWidget*, cairo_t*, gpointer)
// ItemClicked — ToolBar: one void on_<item>_clicked() per tool button
// ItemActivate — MenuBar: one void on_<item>_activate() per menu item
// CellChanged — Spreadsheet: void on_x() on every cell entry
// A builtin event is already wired by the generator for other reasons
// (Shape/Plot/StockChart draw); the table documents it and the generator
// must not connect it a second time.
#pragma once
#include <algorithm>
#include <string>
#include <vector>
#include "model.h"
enum class EventKind {
Simple,
SwitchPage,
RowActivated,
RowSelected,
BufferChanged,
DeleteEvent,
Draw,
ItemClicked,
ItemActivate,
CellChanged,
};
struct EventDef {
const char* signal; // GTK signal name, e.g. "clicked"
const char* handler; // member handler name, e.g. "on_clicked"
const char* label; // short human label for the Events pane
EventKind kind;
bool builtin = false; // already wired; document only, do not reconnect
};
inline const std::vector<EventDef>& events_for(const std::string& type) {
using K = EventKind;
static const std::vector<EventDef> none;
static const std::vector<EventDef> button = {
{"clicked", "on_clicked", "Clicked", K::Simple},
};
static const std::vector<EventDef> entry = {
{"changed", "on_changed", "Text changed", K::Simple},
{"activate", "on_activate", "Enter pressed (activate)", K::Simple},
};
static const std::vector<EventDef> checkbutton = {
{"toggled", "on_toggled", "Toggled", K::Simple},
};
static const std::vector<EventDef> radiobutton = {
{"toggled", "on_toggled", "Toggled", K::Simple},
};
static const std::vector<EventDef> combobox = {
{"changed", "on_changed", "Selection changed", K::Simple},
};
static const std::vector<EventDef> listbox = {
{"row-selected", "on_row_selected", "Row selected", K::RowSelected},
};
static const std::vector<EventDef> menubutton = {
{"clicked", "on_clicked", "Clicked", K::Simple},
};
static const std::vector<EventDef> canvas = {
{"draw", "on_draw", "Draw (redraw)", K::Draw},
};
static const std::vector<EventDef> scale = {
{"value-changed", "on_value_changed", "Value changed", K::Simple},
};
static const std::vector<EventDef> scrollbar = {
{"value-changed", "on_value_changed", "Value changed", K::Simple},
};
static const std::vector<EventDef> text = {
{"changed", "on_changed", "Text changed", K::BufferChanged},
};
static const std::vector<EventDef> toplevel = {
{"delete-event", "on_delete_event", "Window close (delete-event)",
K::DeleteEvent},
};
static const std::vector<EventDef> spreadsheet = {
{"changed", "on_cell_changed", "Cell text changed", K::CellChanged},
};
static const std::vector<EventDef> stockchart = {
// Draw, wheel zoom, drag pan and the drawing tools are already
// wired by the generator (on_draw, on_chart_scroll, on_chart_press,
// on_chart_motion, on_chart_release, on_chart_key).
{"draw", "on_draw", "Draw (redraw)", K::Draw, true},
};
static const std::vector<EventDef> memo = {
{"changed", "on_changed", "Text changed", K::BufferChanged},
};
static const std::vector<EventDef> shape = {
{"draw", "on_draw", "Draw (redraw)", K::Draw, true},
};
static const std::vector<EventDef> notebook = {
{"switch-page", "on_switch_page", "Page switched", K::SwitchPage},
};
static const std::vector<EventDef> toolbar = {
{"clicked", "on_clicked", "Tool button clicked", K::ItemClicked},
};
static const std::vector<EventDef> menubar = {
{"activate", "on_activate", "Menu item activated", K::ItemActivate},
};
static const std::vector<EventDef> spinedit = {
{"value-changed", "on_value_changed", "Value changed", K::Simple},
};
static const std::vector<EventDef> trackbar = {
{"value-changed", "on_value_changed", "Value changed", K::Simple},
};
static const std::vector<EventDef> datetimepicker = {
{"day-selected", "on_day_selected", "Day selected", K::Simple},
{"month-changed", "on_month_changed", "Month changed", K::Simple},
};
static const std::vector<EventDef> colorbox = {
{"color-set", "on_color_set", "Colour chosen", K::Simple},
};
static const std::vector<EventDef> listview = {
{"row-activated", "on_row_activated", "Row activated (double-click)",
K::RowActivated},
{"cursor-changed", "on_cursor_changed", "Cursor moved", K::Simple},
};
static const std::vector<EventDef> treeview = {
{"row-activated", "on_row_activated", "Row activated (double-click)",
K::RowActivated},
{"cursor-changed", "on_cursor_changed", "Cursor moved", K::Simple},
};
static const std::vector<EventDef> stringgrid = {
{"row-activated", "on_row_activated", "Row activated (double-click)",
K::RowActivated},
{"cursor-changed", "on_cursor_changed", "Cursor moved", K::Simple},
};
static const std::vector<EventDef> plot = {
{"draw", "on_draw", "Draw (redraw)", K::Draw, true},
};
if (type == "Button") return button;
if (type == "Entry") return entry;
if (type == "Checkbutton") return checkbutton;
if (type == "Radiobutton") return radiobutton;
if (type == "Combobox") return combobox;
if (type == "Listbox") return listbox;
if (type == "Menubutton") return menubutton;
if (type == "Canvas") return canvas;
if (type == "Scale") return scale;
if (type == "Scrollbar") return scrollbar;
if (type == "Text") return text;
if (type == "Toplevel") return toplevel;
if (type == "Spreadsheet") return spreadsheet;
if (type == "StockChart") return stockchart;
if (type == "Memo") return memo;
if (type == "Shape") return shape;
if (type == "Notebook") return notebook;
if (type == "ToolBar") return toolbar;
if (type == "MenuBar") return menubar;
if (type == "SpinEdit") return spinedit;
if (type == "TrackBar") return trackbar;
if (type == "DateTimePicker") return datetimepicker;
if (type == "ColorBox") return colorbox;
if (type == "ListView") return listview;
if (type == "TreeView") return treeview;
if (type == "StringGrid") return stringgrid;
if (type == "Plot") return plot;
return none; // Label, Frame, GroupBox, Panel, Image, ScrollBox,
// Splitter, StatusBar, ProgressBar — see no_events_note()
}
// Why a type with no events has none (shown in the Events pane so the
// absence is explicit, not an omission).
inline std::string no_events_note(const std::string& type) {
if (type == "Label")
return "Display-only: no widget-specific signal "
"(mouse handlers still apply).";
if (type == "Frame" || type == "GroupBox" || type == "Panel")
return "Container: no widget-specific signal — connect the "
"signals of the widgets placed inside it.";
if (type == "Image")
return "Display-only: no widget-specific signal "
"(mouse handlers still apply).";
if (type == "ScrollBox")
return "Container: no widget-specific signal — connect the "
"signals of the widgets placed inside it.";
if (type == "Splitter")
return "GTK 3 GtkPaned exposes no position signal: no "
"widget-specific signal.";
if (type == "StatusBar")
return "Display-only: no widget-specific signal.";
if (type == "ProgressBar")
return "Display-only: no widget-specific signal — set its "
"fraction from another widget's handler.";
return "No widget-specific signal.";
}
// Find one event definition for a type by GTK signal name.
inline const EventDef* find_event(const std::string& type,
const std::string& signal) {
for (const EventDef& d : events_for(type))
if (signal == d.signal) return &d;
return nullptr;
}
// Connect/disconnect one signal on a widget model; false if the signal
// is not one of the type's events.
inline bool set_widget_signal(Widget& w, const std::string& signal,
bool connect) {
if (!find_event(w.type, signal)) return false;
auto it = std::find(w.signals.begin(), w.signals.end(), signal);
if (connect) {
if (it == w.signals.end()) w.signals.push_back(signal);
} else if (it != w.signals.end()) {
w.signals.erase(it);
}
return true;
}
+301 -12
View File
@@ -3,6 +3,7 @@
#include "generator.h"
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
@@ -13,6 +14,7 @@
#include <sstream>
#include "drawings_embedded.h"
#include "events.h"
#include "indicators_embedded.h"
#include "props.h"
@@ -295,6 +297,247 @@ static std::string connect_mouse(const std::string& cls,
return o.str();
}
// ------------------------------------------------------- event handlers
static std::vector<std::string> split_on(char sep,
const std::string& spec);
// The connected signals of one widget (Widget.signals, validated against
// the event table, de-duplicated). Builtin events (Shape/Plot/StockChart
// draw) are already wired by the generator and are left out here so they
// are never connected twice.
static std::vector<const EventDef*> connected_events(const Widget& w) {
std::vector<const EventDef*> out;
std::set<std::string> seen;
for (const std::string& s : w.signals) {
const EventDef* d = find_event(w.type, s);
if (!d || d->builtin) continue;
if (!seen.insert(s).second) continue;
out.push_back(d);
}
return out;
}
// Identifier fragments for per-item handlers (ToolBar tool buttons,
// MenuBar top-level items) from the widget's items/menus property.
static std::vector<std::string> event_item_ids(const Widget& w) {
std::vector<std::string> out;
std::string spec;
if (w.type == "ToolBar")
spec = prop_or(w, "items", "New|Open|Save");
else if (w.type == "MenuBar")
spec = prop_or(w, "menus", "File|Edit|Help");
else
return out;
for (const std::string& name : split_on('|', spec))
out.push_back(menu_id_part(name));
return out;
}
static bool event_connected(const Widget& w, const char* signal) {
for (const std::string& s : w.signals)
if (s == signal) return true;
return false;
}
// One g_signal_connect line (4-space indent) for a plain event.
static std::string event_connect_line(const Widget& w, const EventDef& d,
const std::string& target) {
std::ostringstream o;
std::string cb = std::string(d.handler) + "_cb";
switch (d.kind) {
case EventKind::BufferChanged:
o << " g_signal_connect(gtk_text_view_get_buffer(GTK_TEXT_VIEW("
<< target << ")), \"" << d.signal << "\", G_CALLBACK(&"
<< w.name << "::" << cb << "), this);\n";
break;
case EventKind::Draw:
o << " g_signal_connect(" << target << ", \"" << d.signal
<< "\", G_CALLBACK(&" << w.name << "::" << d.handler
<< "), this);\n";
break;
default:
o << " g_signal_connect(" << target << ", \"" << d.signal
<< "\", G_CALLBACK(&" << w.name << "::" << cb << "), this);\n";
break;
}
return o.str();
}
// All g_signal_connect lines for a widget's connected events (the
// per-item and per-cell kinds are emitted where the items/cells are
// created instead).
static std::string event_connect_lines(const Widget& w,
const std::string& target) {
std::ostringstream o;
for (const EventDef* d : connected_events(w)) {
if (d->kind == EventKind::ItemClicked ||
d->kind == EventKind::ItemActivate ||
d->kind == EventKind::CellChanged)
continue;
o << event_connect_line(w, *d, target);
}
return o.str();
}
// Handler declarations for the widget class header.
static void event_handler_decls(std::ostringstream& o, const Widget& w) {
std::vector<const EventDef*> evs = connected_events(w);
if (evs.empty()) return;
o << "\n // Event handlers (connected signals — see events.h)\n";
for (const EventDef* d : evs) {
switch (d->kind) {
case EventKind::Simple:
case EventKind::BufferChanged:
case EventKind::CellChanged:
o << " void " << d->handler << "();\n"
<< " static void " << d->handler
<< "_cb(GtkWidget*, gpointer);\n";
break;
case EventKind::SwitchPage:
o << " void " << d->handler << "(guint page);\n"
<< " static void " << d->handler
<< "_cb(GtkNotebook*, GtkWidget*, guint, gpointer);\n";
break;
case EventKind::RowActivated:
o << " void " << d->handler << "(GtkTreePath* path);\n"
<< " static void " << d->handler
<< "_cb(GtkTreeView*, GtkTreePath*, GtkTreeViewColumn*, "
"gpointer);\n";
break;
case EventKind::RowSelected:
o << " void " << d->handler << "();\n"
<< " static void " << d->handler
<< "_cb(GtkListBox*, GtkListBoxRow*, gpointer);\n";
break;
case EventKind::DeleteEvent:
o << " bool " << d->handler << "();\n"
<< " static gboolean " << d->handler
<< "_cb(GtkWidget*, GdkEvent*, gpointer);\n";
break;
case EventKind::Draw:
o << " static gboolean " << d->handler
<< "(GtkWidget*, cairo_t*, gpointer);\n";
break;
case EventKind::ItemClicked:
for (const std::string& id : event_item_ids(w))
o << " void " << d->handler << "_" << id << "();\n"
<< " static void " << d->handler << "_" << id
<< "_cb(GtkToolButton*, gpointer);\n";
break;
case EventKind::ItemActivate:
for (const std::string& id : event_item_ids(w))
o << " void " << d->handler << "_" << id << "();\n"
<< " static void " << d->handler << "_" << id
<< "_cb(GtkMenuItem*, gpointer);\n";
break;
}
}
}
// The trace line a stub prints under FDESIGNER_EVENT_TRACE (compiled
// into generated apps only when the test harness defines the macro).
static std::string event_trace(const Widget& w, const EventDef& d,
const std::string& detail) {
std::string line = "#ifdef FDESIGNER_EVENT_TRACE\n"
" std::fprintf(stderr, \"EVENT " +
w.name + " " + d.signal + detail + "\\n\");\n#endif\n";
return line;
}
// Handler definitions for the widget class .cpp: the member stub the
// user fills in, plus the static trampoline GTK calls.
static void event_handler_defs(std::ostringstream& o, const Widget& w) {
for (const EventDef* d : connected_events(w)) {
std::string cb = std::string(d->handler) + "_cb";
switch (d->kind) {
case EventKind::Simple:
case EventKind::BufferChanged:
case EventKind::CellChanged:
o << "\nvoid " << w.name << "::" << d->handler << "() {\n"
<< " // Code block here\n"
<< event_trace(w, *d, "") << "}\n"
<< "\nvoid " << w.name << "::" << cb
<< "(GtkWidget*, gpointer self) {\n"
<< " auto* me = static_cast<" << w.name << "*>(self);\n"
<< " me->" << d->handler << "();\n}\n";
break;
case EventKind::SwitchPage:
o << "\nvoid " << w.name << "::" << d->handler
<< "(guint page) {\n"
<< " // Code block here\n"
<< event_trace(w, *d, "") << " (void)page;\n}\n"
<< "\nvoid " << w.name << "::" << cb
<< "(GtkNotebook*, GtkWidget*, guint page, gpointer self) {\n"
<< " auto* me = static_cast<" << w.name << "*>(self);\n"
<< " me->" << d->handler << "(page);\n}\n";
break;
case EventKind::RowActivated:
o << "\nvoid " << w.name << "::" << d->handler
<< "(GtkTreePath* path) {\n"
<< " // Code block here\n"
<< event_trace(w, *d, "") << " (void)path;\n}\n"
<< "\nvoid " << w.name << "::" << cb
<< "(GtkTreeView*, GtkTreePath* path, GtkTreeViewColumn*, "
"gpointer self) {\n"
<< " auto* me = static_cast<" << w.name << "*>(self);\n"
<< " me->" << d->handler << "(path);\n}\n";
break;
case EventKind::RowSelected:
o << "\nvoid " << w.name << "::" << d->handler << "() {\n"
<< " // Code block here\n"
<< event_trace(w, *d, "") << "}\n"
<< "\nvoid " << w.name << "::" << cb
<< "(GtkListBox*, GtkListBoxRow*, gpointer self) {\n"
<< " auto* me = static_cast<" << w.name << "*>(self);\n"
<< " me->" << d->handler << "();\n}\n";
break;
case EventKind::DeleteEvent:
o << "\nbool " << w.name << "::" << d->handler << "() {\n"
<< " // Code block here — return true to stop the "
"window closing.\n"
<< event_trace(w, *d, "") << " return false;\n}\n"
<< "\n"
"gboolean "
<< w.name << "::" << cb
<< "(GtkWidget*, GdkEvent*, gpointer self) {\n"
<< " auto* me = static_cast<" << w.name << "*>(self);\n"
<< " return me->" << d->handler << "() ? TRUE : FALSE;\n}\n";
break;
case EventKind::Draw:
o << "\ngboolean " << w.name << "::" << d->handler
<< "(GtkWidget*, cairo_t*, gpointer) {\n"
<< " // Code block here — draw with cairo.\n"
<< event_trace(w, *d, "") << " return FALSE;\n}\n";
break;
case EventKind::ItemClicked:
for (const std::string& id : event_item_ids(w)) {
std::string h = std::string(d->handler) + "_" + id;
o << "\nvoid " << w.name << "::" << h << "() {\n"
<< " // Code block here\n"
<< event_trace(w, *d, " " + id) << "}\n"
<< "\nvoid " << w.name << "::" << h
<< "_cb(GtkToolButton*, gpointer self) {\n"
<< " auto* me = static_cast<" << w.name
<< "*>(self);\n"
<< " me->" << h << "();\n}\n";
}
break;
case EventKind::ItemActivate:
for (const std::string& id : event_item_ids(w)) {
std::string h = std::string(d->handler) + "_" + id;
o << "\nvoid " << w.name << "::" << h << "() {\n"
<< " // Code block here\n"
<< event_trace(w, *d, " " + id) << "}\n"
<< "\nvoid " << w.name << "::" << h
<< "_cb(GtkMenuItem*, gpointer self) {\n"
<< " auto* me = static_cast<" << w.name
<< "*>(self);\n"
<< " me->" << h << "();\n}\n";
}
break;
}
}
}
// g_object_set lines for a widget's stored properties, one per property
// (one steppable line each). Skips colours (CSS) and per-type properties
// consumed at creation time.
@@ -384,25 +627,57 @@ static std::string emit_notebook_pages(const Widget& w, const std::string& M) {
return o.str();
}
// Append one tool button per "New|Open|Save" entry.
// Append one tool button per "New|Open|Save" entry. When the ToolBar's
// "clicked" event is connected, each button is kept in a local and wired
// to its own on_<item>_clicked handler.
static std::string emit_toolbar_items(const Widget& w, const std::string& M) {
std::ostringstream o;
bool connect = event_connected(w, "clicked");
for (const std::string& name :
split_on('|', prop_or(w, "items", "New|Open|Save")))
o << " gtk_toolbar_insert(GTK_TOOLBAR(" << M
<< "), gtk_tool_button_new(nullptr, \"" << esc_c(name)
<< "\"), -1);\n";
split_on('|', prop_or(w, "items", "New|Open|Save"))) {
if (connect) {
std::string id = menu_id_part(name);
o << " GtkToolItem* tb_" << id
<< " = gtk_tool_button_new(nullptr, \"" << esc_c(name)
<< "\");\n"
<< " gtk_toolbar_insert(GTK_TOOLBAR(" << M << "), tb_" << id
<< ", -1);\n"
<< " g_signal_connect(tb_" << id
<< ", \"clicked\", G_CALLBACK(&" << w.name << "::on_clicked_"
<< id << "_cb), this);\n";
} else {
o << " gtk_toolbar_insert(GTK_TOOLBAR(" << M
<< "), gtk_tool_button_new(nullptr, \"" << esc_c(name)
<< "\"), -1);\n";
}
}
return o.str();
}
// Append one top-level item per "File|Edit|Help" entry.
// Append one top-level item per "File|Edit|Help" entry. When the MenuBar's
// "activate" event is connected, each item is kept in a local and wired
// to its own on_<item>_activate handler.
static std::string emit_menubar_items(const Widget& w, const std::string& M) {
std::ostringstream o;
bool connect = event_connected(w, "activate");
for (const std::string& name :
split_on('|', prop_or(w, "menus", "File|Edit|Help")))
o << " gtk_menu_shell_append(GTK_MENU_SHELL(" << M
<< "), gtk_menu_item_new_with_label(\"" << esc_c(name)
<< "\"));\n";
split_on('|', prop_or(w, "menus", "File|Edit|Help"))) {
if (connect) {
std::string id = menu_id_part(name);
o << " GtkWidget* mb_" << id
<< " = gtk_menu_item_new_with_label(\"" << esc_c(name)
<< "\");\n"
<< " gtk_menu_shell_append(GTK_MENU_SHELL(" << M << "), mb_"
<< id << ");\n"
<< " g_signal_connect(mb_" << id
<< ", \"activate\", G_CALLBACK(&" << w.name
<< "::on_activate_" << id << "_cb), this);\n";
} else {
o << " gtk_menu_shell_append(GTK_MENU_SHELL(" << M
<< "), gtk_menu_item_new_with_label(\"" << esc_c(name)
<< "\"));\n";
}
}
return o.str();
}
@@ -516,6 +791,9 @@ static std::string shape_plot_draw_methods(const Widget& w) {
o << "\ngboolean " << w.name << "::on_draw(GtkWidget* w, cairo_t* cr,\n"
<< " gpointer self) {\n"
<< " auto* me = static_cast<" << w.name << "*>(self);\n"
<< "#ifdef FDESIGNER_EVENT_TRACE\n"
<< " std::fprintf(stderr, \"EVENT " << w.name << " draw\\n\");\n"
<< "#endif\n"
<< " GtkAllocation a;\n"
<< " gtk_widget_get_allocation(w, &a);\n";
if (w.type == "Shape") {
@@ -725,6 +1003,7 @@ static std::string emit_widget_header(const Widget& w) {
o << " void util_error_print(const char* what);\n\n"
<< " // Mouse handlers\n";
stub_decls(o, kWidgetStubs);
event_handler_decls(o, w);
o << "};\n";
return o.str();
}
@@ -1015,6 +1294,9 @@ fdw::Map %CLASS%::current_map(const Layout& L) const {
gboolean %CLASS%::on_draw(GtkWidget* drawing, cairo_t* cr,
gpointer self) {
auto* me = static_cast<%CLASS%*>(self);
#ifdef FDESIGNER_EVENT_TRACE
std::fprintf(stderr, "EVENT %CLASS% draw\n");
#endif
int width = gtk_widget_get_allocated_width(drawing);
int height = gtk_widget_get_allocated_height(drawing);
cairo_set_source_rgb(cr, 1, 1, 1);
@@ -1536,6 +1818,7 @@ static std::string emit_widget_cpp(const Widget& w) {
"inner_fixed);\n"
<< colour_lines(w, M)
<< connect_mouse(w.name, M, kWidgetStubs, 4)
<< event_connect_lines(w, M)
<< " // #endregion\n"
<< "}\n";
} else {
@@ -1770,8 +2053,12 @@ static std::string emit_widget_cpp(const Widget& w) {
<< " gtk_entry_set_width_chars(GTK_ENTRY(cell), "
"8);\n"
<< " gtk_grid_attach(GTK_GRID(" << M
<< "), cell, c, r, 1, 1);\n"
<< " }\n";
<< "), cell, c, r, 1, 1);\n";
if (event_connected(w, "changed"))
o << " g_signal_connect(cell, \"changed\", "
"G_CALLBACK(&"
<< w.name << "::on_cell_changed_cb), this);\n";
o << " }\n";
} else if (t == "Memo") {
o << " gtk_text_buffer_set_text(gtk_text_view_get_buffer("
"GTK_TEXT_VIEW("
@@ -1879,6 +2166,7 @@ static std::string emit_widget_cpp(const Widget& w) {
<< prop_lines(w, M, skip)
<< colour_lines(w, M)
<< connect_mouse(w.name, M, kWidgetStubs, 4)
<< event_connect_lines(w, M)
<< " // #endregion\n"
<< "}\n";
}
@@ -1896,6 +2184,7 @@ static std::string emit_widget_cpp(const Widget& w) {
<< " std::fprintf(stderr, \"%s\\n\", what);\n"
<< "}\n";
stub_defs(o, w.name, kWidgetStubs);
event_handler_defs(o, w);
return o.str();
}
+184
View File
@@ -31,6 +31,7 @@
#include "model.h"
#include "props.h"
#include "events.h"
#include "drawings.h"
#include "generator.h"
#include "indicators.h"
@@ -210,8 +211,16 @@ struct PropBinding {
struct PickBinding {
GtkEntry* entry;
};
// One Events-pane check button: which widget and which GTK signal it
// connects. (The signal string is copied — the EventDef table is static,
// but the binding owns its copy for safety.)
struct EventBinding {
Item* item;
std::string signal;
};
static std::vector<PropBinding*> g_prop_bindings;
static std::vector<PickBinding*> g_pick_bindings;
static std::vector<EventBinding*> g_event_bindings;
// Bulk-parameter inputs
static GtkWidget* g_bulk_param = nullptr; // editable combo
@@ -1572,6 +1581,7 @@ static void on_spin_changed(GtkSpinButton*, gpointer which);
static void on_delete_clicked(GtkButton*, gpointer);
static void on_prop_entry_changed(GtkEntry*, gpointer binding);
static void on_prop_bool_toggled(GtkToggleButton*, gpointer binding);
static void on_event_toggled(GtkToggleButton*, gpointer binding);
static void on_prop_spin_changed(GtkSpinButton*, gpointer binding);
static void on_prop_enum_changed(GtkComboBoxText*, gpointer binding);
static void on_prop_pick_clicked(GtkButton*, gpointer binding);
@@ -1587,8 +1597,10 @@ static void clear_props() {
g_list_free(kids);
for (PropBinding* b : g_prop_bindings) delete b;
for (PickBinding* b : g_pick_bindings) delete b;
for (EventBinding* b : g_event_bindings) delete b;
g_prop_bindings.clear();
g_pick_bindings.clear();
g_event_bindings.clear();
g_spin_x = g_spin_y = g_spin_w = g_spin_h = nullptr;
}
@@ -1765,6 +1777,41 @@ static void refresh_props() {
for (const PropDef* d : defs)
add_row(row++, d->name, make_prop_editor(g_selected, *d));
// Events: one check button per natural GTK signal of this type.
// Checked = connected: the generator emits a stub handler and a
// g_signal_connect for it. Types with no natural signal get an
// explicit note instead (see events.h — none left unexamined).
GtkWidget* ev_hdr = gtk_label_new("<b>Events</b>");
gtk_label_set_use_markup(GTK_LABEL(ev_hdr), TRUE);
gtk_widget_set_halign(ev_hdr, GTK_ALIGN_START);
gtk_grid_attach(GTK_GRID(g_props_grid), ev_hdr, 0, row++, 2, 1);
const std::vector<EventDef>& evs = events_for(m.type);
if (evs.empty()) {
GtkWidget* note = gtk_label_new(no_events_note(m.type).c_str());
gtk_label_set_line_wrap(GTK_LABEL(note), TRUE);
gtk_widget_set_halign(note, GTK_ALIGN_START);
gtk_grid_attach(GTK_GRID(g_props_grid), note, 0, row++, 2, 1);
}
for (const EventDef& d : evs) {
std::string text = std::string(d.signal) + " → " + d.handler +
"()";
if (d.builtin) text += " (always connected)";
GtkWidget* c = gtk_check_button_new_with_label(text.c_str());
bool on = d.builtin ||
std::find(m.signals.begin(), m.signals.end(),
d.signal) != m.signals.end();
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(c), on);
if (d.builtin) {
gtk_widget_set_sensitive(c, FALSE);
} else {
EventBinding* b = new EventBinding{g_selected, d.signal};
g_event_bindings.push_back(b);
g_signal_connect(c, "toggled", G_CALLBACK(on_event_toggled), b);
}
gtk_widget_set_halign(c, GTK_ALIGN_START);
gtk_grid_attach(GTK_GRID(g_props_grid), c, 0, row++, 2, 1);
}
GtkWidget* del = gtk_button_new_with_label("Delete widget");
g_signal_connect(del, "clicked", G_CALLBACK(on_delete_clicked), nullptr);
gtk_grid_attach(GTK_GRID(g_props_grid), del, 0, row, 2, 1);
@@ -2054,6 +2101,18 @@ static void on_prop_bool_toggled(GtkToggleButton* t, gpointer binding) {
gtk_toggle_button_get_active(t) ? "true" : "false", err);
}
// Events pane: connect/disconnect one GTK signal on the selected widget.
// The generator emits a stub handler + g_signal_connect for each entry in
// Widget.signals, so this checkbox is the whole UI for event wiring.
static void on_event_toggled(GtkToggleButton* t, gpointer binding) {
if (g_syncing_props) return;
EventBinding* b = (EventBinding*)binding;
bool on = gtk_toggle_button_get_active(t) != 0;
if (set_widget_signal(b->item->model, b->signal, on))
status(b->item->model.name + ": signal \"" + b->signal + "\" " +
(on ? "connected" : "disconnected"));
}
static void on_prop_spin_changed(GtkSpinButton* s, gpointer binding) {
if (g_syncing_props) return;
PropBinding* b = (PropBinding*)binding;
@@ -2987,6 +3046,131 @@ static gboolean selftest_step(gpointer) {
std::printf("GENERATED7 /tmp/fdesigner_gen_test7\n");
}
// Phase 8: event handlers — connect every natural signal on every
// widget on the canvas, generate, and check the emitted code carries
// a stub handler + g_signal_connect for each; types with no natural
// signal get no event stubs; save/load keeps the connections.
{
auto read_file8 = [](const std::string& path) {
gchar* t = nullptr;
if (!g_file_get_contents(path.c_str(), &t, nullptr, nullptr) ||
!t)
return std::string();
std::string s = t;
g_free(t);
return s;
};
// The Events pane itself: ticking Button_001's "clicked" check
// button connects the signal through the real UI callback.
{
Item* b = nullptr;
for (Item* it : g_items)
if (it->model.name == "Button_001") b = it;
check(b != nullptr);
select_item(b); // rebuilds the property pane
GtkWidget* clicked_btn = nullptr;
GList* kids =
gtk_container_get_children(GTK_CONTAINER(g_props_grid));
for (GList* p = kids; p; p = p->next) {
GtkWidget* ch = GTK_WIDGET(p->data);
if (GTK_IS_CHECK_BUTTON(ch)) {
const char* lab = gtk_button_get_label(GTK_BUTTON(ch));
if (lab &&
std::string(lab).find("clicked") !=
std::string::npos)
clicked_btn = ch;
}
}
check(clicked_btn != nullptr);
if (clicked_btn) {
check(!gtk_toggle_button_get_active(
GTK_TOGGLE_BUTTON(clicked_btn)));
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(clicked_btn),
TRUE);
check(std::find(b->model.signals.begin(),
b->model.signals.end(),
"clicked") != b->model.signals.end());
}
g_list_free(kids);
}
for (Item* it : g_items)
for (const EventDef& d : events_for(it->model.type))
if (!d.builtin)
check(set_widget_signal(it->model, d.signal, true));
check(!set_widget_signal(g_items.front()->model, "bogus-signal",
true));
{
Widget scratch = g_items.front()->model;
check(set_widget_signal(scratch, "clicked", false));
check(std::find(scratch.signals.begin(), scratch.signals.end(),
"clicked") == scratch.signals.end());
}
Project snap8 = snapshot_project();
check(snap8.widgets.size() == 29);
GenResult gr8 = generate_project(snap8, snap8.widgets,
"/tmp/fdesigner_gen_test8");
check(gr8.ok);
for (const Widget& w : snap8.widgets) {
std::string cpp =
read_file8("/tmp/fdesigner_gen_test8/" + w.name + ".cpp");
std::string hdr =
read_file8("/tmp/fdesigner_gen_test8/" + w.name + ".h");
check(!cpp.empty() && !hdr.empty());
const std::vector<EventDef>& evs = events_for(w.type);
if (evs.empty()) {
// No natural signal: no event stubs at all (the EVENT
// trace marker only appears in event handler stubs).
check(cpp.find("EVENT ") == std::string::npos);
continue;
}
for (const EventDef& d : evs) {
if (d.builtin) {
check(cpp.find("\"draw\"") != std::string::npos);
check(cpp.find("g_signal_connect") !=
std::string::npos);
continue;
}
check(cpp.find(std::string("\"") + d.signal + "\"") !=
std::string::npos);
check(cpp.find("g_signal_connect") != std::string::npos);
check(cpp.find("EVENT " + w.name + " " + d.signal) !=
std::string::npos);
if (d.kind == EventKind::ItemClicked) {
check(cpp.find("on_clicked_") != std::string::npos);
check(cpp.find("tb_cut") != std::string::npos);
check(hdr.find("on_clicked_") != std::string::npos);
} else if (d.kind == EventKind::ItemActivate) {
check(cpp.find("on_activate_") != std::string::npos);
check(cpp.find("mb_file") != std::string::npos);
check(hdr.find("on_activate_") != std::string::npos);
} else {
check(cpp.find(w.name + "::" + d.handler) !=
std::string::npos);
check(hdr.find(d.handler) != std::string::npos);
}
}
}
// Save/load round-trip keeps the connections.
check(save_project_file("/tmp/fdesigner_test8.fdj", snap8, err));
clear_canvas();
check(g_items.empty());
Project loaded8;
check(load_project_file("/tmp/fdesigner_test8.fdj", loaded8, err));
load_into_canvas(loaded8);
check(g_items.size() == 29);
for (Item* it : g_items) {
if (it->model.name == "Button_001")
check(std::find(it->model.signals.begin(),
it->model.signals.end(),
"clicked") != it->model.signals.end());
if (it->model.name == "Entry_001")
check(it->model.signals.size() == 2);
if (it->model.type == "Label")
check(it->model.signals.empty());
}
std::printf("GENERATED8 /tmp/fdesigner_gen_test8\n");
}
// Debugger backend: settings round-trip, breakpoint-location
// parsing, MI value parsing, staged breakpoints (no gdb needed).
{
+11 -1
View File
@@ -65,7 +65,12 @@ bool save_project_file(const std::string& path, const Project& p,
<< "\": \"" << json_escape(v) << "\"";
first = false;
}
o << (w.props.empty() ? "" : "\n ") << "}\n"
o << (w.props.empty() ? "" : "\n ") << "},\n"
<< " \"signals\": [";
for (size_t j = 0; j < w.signals.size(); ++j)
o << (j ? ", " : "") << "\"" << json_escape(w.signals[j])
<< "\"";
o << "]\n"
<< " }";
}
o << (p.widgets.empty() ? "" : "\n ") << "]\n}\n";
@@ -350,6 +355,11 @@ bool load_project_file(const std::string& path, Project& out,
for (const auto& [k, v] : props->obj)
if (v.type == Json::Str) w.props[k] = v.str;
}
if (const Json* sigs = jw.find("signals");
sigs && sigs->type == Json::Arr) {
for (const Json& s : sigs->arr)
if (s.type == Json::Str) w.signals.push_back(s.str);
}
p.widgets.push_back(w);
}
}
+273
View File
@@ -0,0 +1,273 @@
// Runtime event harness: instantiate every generated widget class,
// trigger each connected signal through the GTK API, and check the EVENT
// trace lines the stubs print to stderr. The shell script runs this under
// Xvfb with stderr redirected to argv[1]; the harness reads that file
// back and prints a per-widget pass/fail table to stdout.
// Usage: event_handler_runtime <event-log-path>
// (Compile with -DFDESIGNER_EVENT_TRACE and -I <generated dir>, linking
// the generated widget .cpp files except main.cpp.)
#include <gtk/gtk.h>
#include <cstdio>
#include <fstream>
#include <string>
#include <vector>
#include "Button_001.h"
#include "Label_001.h"
#include "Entry_001.h"
#include "Checkbutton_001.h"
#include "Radiobutton_001.h"
#include "Frame_001.h"
#include "Combobox_001.h"
#include "Listbox_001.h"
#include "Menubutton_001.h"
#include "Canvas_001.h"
#include "Scale_001.h"
#include "Scrollbar_001.h"
#include "Text_001.h"
#include "Toplevel_001.h"
#include "Spreadsheet_001.h"
#include "StockChart_001.h"
#include "Memo_001.h"
#include "GroupBox_001.h"
#include "Panel_001.h"
#include "Image_001.h"
#include "Shape_001.h"
#include "Notebook_001.h"
#include "ScrollBox_001.h"
#include "Splitter_001.h"
#include "StatusBar_001.h"
#include "ToolBar_001.h"
#include "MenuBar_001.h"
#include "SpinEdit_001.h"
#include "TrackBar_001.h"
#include "ProgressBar_001.h"
#include "DateTimePicker_001.h"
#include "ColorBox_001.h"
#include "ListView_001.h"
#include "TreeView_001.h"
#include "StringGrid_001.h"
#include "Plot_001.h"
static void pump() {
while (gtk_events_pending()) gtk_main_iteration();
}
struct Expect {
const char* type;
const char* signal; // "" = no-events type (pass by design)
const char* line; // substring to find in the trace log
};
static const Expect kExpect[] = {
{"Button", "clicked", "EVENT Button_001 clicked"},
{"Entry", "changed", "EVENT Entry_001 changed"},
{"Entry", "activate", "EVENT Entry_001 activate"},
{"Checkbutton", "toggled", "EVENT Checkbutton_001 toggled"},
{"Radiobutton", "toggled", "EVENT Radiobutton_001 toggled"},
{"Combobox", "changed", "EVENT Combobox_001 changed"},
{"Listbox", "row-selected", "EVENT Listbox_001 row-selected"},
{"Menubutton", "clicked", "EVENT Menubutton_001 clicked"},
{"Canvas", "draw", "EVENT Canvas_001 draw"},
{"Scale", "value-changed", "EVENT Scale_001 value-changed"},
{"Scrollbar", "value-changed", "EVENT Scrollbar_001 value-changed"},
{"Text", "changed", "EVENT Text_001 changed"},
{"Toplevel", "delete-event", "EVENT Toplevel_001 delete-event"},
{"Spreadsheet", "changed", "EVENT Spreadsheet_001 changed"},
{"StockChart", "draw", "EVENT StockChart_001 draw"},
{"Memo", "changed", "EVENT Memo_001 changed"},
{"Shape", "draw", "EVENT Shape_001 draw"},
{"Notebook", "switch-page", "EVENT Notebook_001 switch-page"},
{"ToolBar", "clicked", "EVENT ToolBar_001 clicked"},
{"MenuBar", "activate", "EVENT MenuBar_001 activate"},
{"SpinEdit", "value-changed", "EVENT SpinEdit_001 value-changed"},
{"TrackBar", "value-changed", "EVENT TrackBar_001 value-changed"},
{"DateTimePicker", "day-selected",
"EVENT DateTimePicker_001 day-selected"},
{"DateTimePicker", "month-changed",
"EVENT DateTimePicker_001 month-changed"},
{"ColorBox", "color-set", "EVENT ColorBox_001 color-set"},
{"ListView", "row-activated", "EVENT ListView_001 row-activated"},
{"ListView", "cursor-changed", "EVENT ListView_001 cursor-changed"},
{"TreeView", "row-activated", "EVENT TreeView_001 row-activated"},
{"TreeView", "cursor-changed", "EVENT TreeView_001 cursor-changed"},
{"StringGrid", "row-activated", "EVENT StringGrid_001 row-activated"},
{"StringGrid", "cursor-changed", "EVENT StringGrid_001 cursor-changed"},
{"Plot", "draw", "EVENT Plot_001 draw"},
// No natural signal — pass by design, with the explicit note.
{"Label", "", ""},
{"Frame", "", ""},
{"GroupBox", "", ""},
{"Panel", "", ""},
{"Image", "", ""},
{"ScrollBox", "", ""},
{"Splitter", "", ""},
{"StatusBar", "", ""},
{"ProgressBar", "", ""},
};
int main(int argc, char** argv) {
gtk_init(&argc, &argv);
std::string log_path = argc > 1 ? argv[1] : "/tmp/event_trace.log";
GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GtkWidget* fixed = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), fixed);
Button_001 btn(fixed);
Label_001 lbl(fixed);
Entry_001 ent(fixed);
Checkbutton_001 chk(fixed);
Radiobutton_001 radio(fixed);
Frame_001 frm(fixed);
Combobox_001 combo(fixed);
Listbox_001 lbox(fixed);
Menubutton_001 mbtn(fixed);
Canvas_001 canvas(fixed);
Scale_001 scale(fixed);
Scrollbar_001 sbar(fixed);
Text_001 text(fixed);
Toplevel_001 top; // own window; not shown
Spreadsheet_001 sheet(fixed);
StockChart_001 chart(fixed);
Memo_001 memo(fixed);
GroupBox_001 gbox(fixed);
Panel_001 panel(fixed);
Image_001 img(fixed);
Shape_001 shape(fixed);
Notebook_001 nb(fixed);
ScrollBox_001 sbox(fixed);
Splitter_001 split(fixed);
StatusBar_001 status(fixed);
ToolBar_001 tbar(fixed);
MenuBar_001 mbar(fixed);
SpinEdit_001 spin(fixed);
TrackBar_001 track(fixed);
ProgressBar_001 prog(fixed);
DateTimePicker_001 dtp(fixed);
ColorBox_001 cbox(fixed);
ListView_001 lview(fixed);
TreeView_001 tview(fixed);
StringGrid_001 grid(fixed);
Plot_001 plot(fixed);
gtk_widget_show_all(window);
pump();
// No-events widgets are constructed (they must build + run) but have
// no signal to trigger; keep the build warning-free.
(void)lbl.widget();
(void)frm.widget();
(void)gbox.widget();
(void)panel.widget();
(void)img.widget();
(void)sbox.widget();
(void)split.widget();
(void)status.widget();
(void)prog.widget();
// --- trigger every signal through the GTK API ---
gtk_button_clicked(GTK_BUTTON(btn.widget()));
gtk_entry_set_text(GTK_ENTRY(ent.widget()), "hello");
g_signal_emit_by_name(ent.widget(), "activate");
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chk.widget()), TRUE);
// A lone radio button can never leave its group active: GTK keeps
// the sole member on (set_active(FALSE) and clicking are both
// no-ops). Toggle it by activating a second button in its group.
{
GtkWidget* radio2 = gtk_radio_button_new_with_label_from_widget(
GTK_RADIO_BUTTON(radio.widget()), "Radiobutton_002");
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio2), TRUE);
}
gtk_combo_box_set_active(GTK_COMBO_BOX(combo.widget()), 1);
{
GtkListBoxRow* row = gtk_list_box_get_row_at_index(
GTK_LIST_BOX(lbox.widget()), 0);
gtk_list_box_select_row(GTK_LIST_BOX(lbox.widget()), row);
}
gtk_button_clicked(GTK_BUTTON(mbtn.widget()));
gtk_range_set_value(GTK_RANGE(scale.widget()), 42.0);
gtk_range_set_value(GTK_RANGE(sbar.widget()), 55.0);
gtk_text_buffer_set_text(
gtk_text_view_get_buffer(GTK_TEXT_VIEW(text.widget())), "hi", -1);
{
GdkEvent* ev = gdk_event_new(GDK_DELETE);
gboolean ret = FALSE;
g_signal_emit_by_name(top.widget(), "delete-event", ev, &ret);
gdk_event_free(ev);
}
{
GList* kids =
gtk_container_get_children(GTK_CONTAINER(sheet.widget()));
if (kids) {
gtk_entry_set_text(GTK_ENTRY(kids->data), "9");
g_list_free(kids);
}
}
gtk_text_buffer_set_text(
gtk_text_view_get_buffer(GTK_TEXT_VIEW(memo.widget())), "memo", -1);
gtk_notebook_set_current_page(GTK_NOTEBOOK(nb.widget()), 1);
{
GList* kids =
gtk_container_get_children(GTK_CONTAINER(tbar.widget()));
if (kids) {
g_signal_emit_by_name(kids->data, "clicked");
g_list_free(kids);
}
}
{
GList* kids =
gtk_container_get_children(GTK_CONTAINER(mbar.widget()));
if (kids) {
g_signal_emit_by_name(kids->data, "activate");
g_list_free(kids);
}
}
gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin.widget()), 7.0);
gtk_range_set_value(GTK_RANGE(track.widget()), 33.0);
gtk_calendar_select_day(GTK_CALENDAR(dtp.widget()), 15);
gtk_calendar_select_month(GTK_CALENDAR(dtp.widget()), 5, 2026);
g_signal_emit_by_name(cbox.widget(), "color-set");
auto trigger_tree = [](GtkWidget* tv) {
GtkTreePath* path = gtk_tree_path_new_from_string("0");
gtk_tree_view_set_cursor(GTK_TREE_VIEW(tv), path, nullptr, FALSE);
GtkTreeViewColumn* col = gtk_tree_view_get_column(
GTK_TREE_VIEW(tv), 0);
g_signal_emit_by_name(tv, "row-activated", path, col);
gtk_tree_path_free(path);
};
trigger_tree(lview.widget());
trigger_tree(tview.widget());
trigger_tree(grid.widget());
// Draw events need a main-loop pass after the expose is queued.
gtk_widget_queue_draw(canvas.widget());
gtk_widget_queue_draw(chart.widget());
gtk_widget_queue_draw(shape.widget());
gtk_widget_queue_draw(plot.widget());
pump();
// --- read the trace log back and score it ---
std::ifstream in(log_path);
std::string log((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
int pass = 0, fail = 0;
for (const Expect& e : kExpect) {
if (e.signal[0] == '\0') {
std::printf("PASS %-16s (no events — by design)\n", e.type);
++pass;
continue;
}
if (log.find(e.line) != std::string::npos) {
std::printf("PASS %-16s %s\n", e.type, e.signal);
++pass;
} else {
std::printf("FAIL %-16s %s: no \"%s\" in trace log\n", e.type,
e.signal, e.line);
++fail;
}
}
std::printf("== event_handler_runtime: %d passed, %d failed ==\n", pass,
fail);
return fail ? 1 : 0;
}
+160
View File
@@ -0,0 +1,160 @@
// Event handler tests: one project holding every widget type with every
// natural signal connected; generate it and check per-widget markers in
// the emitted code — a handler declaration in <Name>.h, a g_signal_connect
// for the signal in <Name>.cpp, and the stub definition (with its EVENT
// trace line). Types with no natural signal must emit no event stubs.
// The companion shell script (tests/event_handler_tests.sh) compiles the
// generated app and runs the runtime harness, which triggers each signal
// for real. Usage: event_handler_tests [out_dir]
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
#include "../src/events.h"
#include "../src/generator.h"
#include "../src/model.h"
#include "../src/props.h"
static const char* kEventTypes[] = {
"Button", "Label", "Entry", "Checkbutton", "Radiobutton", "Frame",
"Combobox", "Listbox", "Menubutton", "Canvas", "Scale", "Scrollbar",
"Text", "Toplevel", "Spreadsheet", "StockChart",
"Memo", "GroupBox", "Panel", "Image", "Shape", "Notebook",
"ScrollBox", "Splitter", "StatusBar", "ToolBar", "MenuBar",
"SpinEdit", "TrackBar", "ProgressBar", "DateTimePicker", "ColorBox",
"ListView", "TreeView", "StringGrid", "Plot",
};
static std::string read_file(const std::string& path) {
std::ifstream f(path);
if (!f) return "";
return std::string(std::istreambuf_iterator<char>(f),
std::istreambuf_iterator<char>());
}
// Count non-overlapping occurrences of needle in haystack.
static int count_of(const std::string& hay, const std::string& needle) {
int n = 0;
size_t pos = 0;
while ((pos = hay.find(needle, pos)) != std::string::npos) {
++n;
pos += needle.size();
}
return n;
}
// First item id of a per-item event ("New" -> "new"), "" if not per-item.
static std::string first_item_id(const Widget& w, const EventDef& d) {
if (d.kind != EventKind::ItemClicked &&
d.kind != EventKind::ItemActivate)
return "";
std::string spec = w.type == "ToolBar"
? "New|Open|Save"
: "File|Edit|Help";
auto bar = spec.find('|');
std::string first = spec.substr(0, bar);
std::string out;
for (unsigned char c : first) {
if (std::isalnum(c))
out += (char)std::tolower(c);
else if (!out.empty() && out.back() != '_')
out += '_';
}
while (!out.empty() && out.back() == '_') out.pop_back();
return out.empty() ? "item" : out;
}
int main(int argc, char** argv) {
std::string out = argc > 1 ? argv[1] : "/tmp/fdesigner_events_gen";
Project p;
p.title = "Event Test";
p.class_name = "MyWindow";
int i = 0;
for (const char* type : kEventTypes) {
Widget w;
w.name = std::string(type) + "_001";
w.type = type;
const TypeInfo* t = find_type(type);
w.width = t ? t->def_width : 100;
w.height = t ? t->def_height : 30;
w.x = (i % 6) * 140;
w.y = (i / 6) * 110;
if (w.type == "ToolBar") w.props["items"] = "New|Open|Save";
if (w.type == "MenuBar") w.props["menus"] = "File|Edit|Help";
if (w.type == "DateTimePicker") w.props["date"] = "2026-11-25";
// Connect every natural signal (builtin draw included: the
// generator must wire it exactly once, not twice).
for (const EventDef& d : events_for(w.type))
w.signals.push_back(d.signal);
p.widgets.push_back(w);
++i;
}
GenResult gr = generate_project(p, p.widgets, out);
if (!gr.ok) {
std::printf("FAIL generate: %s\n", gr.error.c_str());
return 1;
}
int pass = 0, fail = 0;
for (const Widget& w : p.widgets) {
std::string cpp = read_file(out + "/" + w.name + ".cpp");
std::string hdr = read_file(out + "/" + w.name + ".h");
std::string why;
if (cpp.empty()) why = "missing " + w.name + ".cpp";
if (hdr.empty()) why = "missing " + w.name + ".h";
const std::vector<EventDef>& evs = events_for(w.type);
if (why.empty() && evs.empty()) {
// No natural signal: no event stubs at all.
if (cpp.find("EVENT ") != std::string::npos)
why = "unexpected event stub in " + w.name + ".cpp";
}
for (const EventDef& d : evs) {
if (!why.empty()) break;
std::string handler = d.handler + first_item_id(w, d);
if (d.kind == EventKind::ItemClicked ||
d.kind == EventKind::ItemActivate)
handler = std::string(d.handler) + "_" + first_item_id(w, d);
std::string sig = "\"" + std::string(d.signal) + "\"";
if (hdr.find(handler) == std::string::npos) {
why = "no decl " + handler + " in " + w.name + ".h";
} else if (cpp.find(sig) == std::string::npos) {
why = "no signal " + sig + " in " + w.name + ".cpp";
} else if (cpp.find("g_signal_connect") == std::string::npos) {
why = "no g_signal_connect in " + w.name + ".cpp";
} else if (cpp.find(w.name + "::" + handler) ==
std::string::npos) {
why = "no stub " + w.name + "::" + handler;
} else if (cpp.find("EVENT " + w.name + " " + d.signal) ==
std::string::npos) {
why = std::string("no EVENT trace for ") + d.signal;
}
if (d.builtin && why.empty()) {
// Builtin draw must be wired exactly once.
int n = count_of(cpp, "g_signal_connect");
int draws = count_of(cpp, "\"draw\"");
if (draws != 1)
why = "draw connected " + std::to_string(draws) +
" times (want 1)";
(void)n;
}
}
if (why.empty()) {
std::printf("PASS %-16s %-18s (%zu event%s)\n", w.type.c_str(),
w.name.c_str(), evs.size(),
evs.size() == 1 ? "" : "s");
++pass;
} else {
std::printf("FAIL %-16s %-18s: %s\n", w.type.c_str(),
w.name.c_str(), why.c_str());
++fail;
}
}
std::printf("== event_handler_tests: %d passed, %d failed ==\n", pass,
fail);
return fail ? 1 : 0;
}
+95
View File
@@ -0,0 +1,95 @@
#!/usr/bin/env bash
# Event handler tests: designer self-test (Phase 8 connects every signal),
# generator-API checks for all 36 widget types (per-widget markers), a
# -Wall -Wextra compile of the generated app, and a runtime harness that
# triggers every signal through the GTK API under Xvfb and checks the
# handler ran (EVENT trace lines). Draw handlers are verified via queued
# exposes; ColorBox "color-set" and TreeView "row-activated" have no
# programmatic GTK trigger, so the harness emits them by name.
set -u
cd "$(dirname "$0")/.."
pass=0; fail=0; failed=""
ok() { echo "PASS $1"; pass=$((pass+1)); }
bad() { echo "FAIL $1"; fail=$((fail+1)); failed="$failed $1"; }
echo "== building fdesigner =="
if make > /tmp/events_make.log 2>&1; then ok make; else
bad make; cat /tmp/events_make.log; exit 1
fi
echo "== designer self-test (Phase 8: event handlers) =="
if FDESIGNER_SELFTEST=1 timeout 60 xvfb-run -a ./fdesigner \
> /tmp/events_selftest.log 2>&1; then
if grep -q "SELFTEST PASS" /tmp/events_selftest.log; then ok selftest;
else bad "selftest(no PASS line)"; tail -20 /tmp/events_selftest.log; fi
else
bad selftest; tail -20 /tmp/events_selftest.log
fi
grep -E "GENERATED8" /tmp/events_selftest.log || true
echo "== event generator test (all 36 widget types) =="
if g++ -O2 -Wall -Wextra -std=c++17 -o /tmp/event_handler_tests \
tests/event_handler_tests.cpp src/generator.cpp src/project_io.cpp; then
rm -rf /tmp/fdesigner_events_gen
if /tmp/event_handler_tests /tmp/fdesigner_events_gen; then
ok event-markers
else
bad event-markers
fi
else
bad event-tests-build
fi
echo "== compile generated project (all widgets + handlers) =="
if (cd /tmp/fdesigner_events_gen && \
make CXXFLAGS="-O2 -Wall -Wextra -std=c++17" >build.log 2>&1); then
if grep -q "warning" /tmp/fdesigner_events_gen/build.log; then
bad "generated-compile(warnings)"
grep "warning" /tmp/fdesigner_events_gen/build.log | head
else ok generated-compile; fi
else
bad generated-compile; cat /tmp/fdesigner_events_gen/build.log
fi
echo "== run generated project briefly =="
timeout 3 xvfb-run -a /tmp/fdesigner_events_gen/app \
> /tmp/fdesigner_events_gen/run.log 2>&1
rc=$?
if [ $rc -ne 124 ]; then
bad "generated-run(exit $rc)"; cat /tmp/fdesigner_events_gen/run.log
else
badlog=$(grep -E "CRITICAL|WARNING \*\*" \
/tmp/fdesigner_events_gen/run.log | grep -v "Could not load a pixbuf" || true)
if [ -n "$badlog" ]; then
bad "generated-run(runtime warnings)"; echo "$badlog"
else ok generated-run; fi
fi
echo "== runtime harness: trigger every signal, check handlers ran =="
GEN_SRCS=$(cd /tmp/fdesigner_events_gen && ls *.cpp | grep -v '^main.cpp$')
# shellcheck disable=SC2086
if g++ -O2 -Wall -Wextra -std=c++17 -DFDESIGNER_EVENT_TRACE \
-I/tmp/fdesigner_events_gen -o /tmp/event_handler_runtime \
tests/event_handler_runtime.cpp \
$(for s in $GEN_SRCS; do echo "/tmp/fdesigner_events_gen/$s"; done) \
$(pkg-config --cflags --libs gtk+-3.0) \
> /tmp/event_runtime_build.log 2>&1; then
if grep -q "warning" /tmp/event_runtime_build.log; then
echo "note: harness build warnings (not fatal):"
grep "warning" /tmp/event_runtime_build.log | head
fi
rm -f /tmp/event_trace.log
if timeout 30 xvfb-run -a /tmp/event_handler_runtime /tmp/event_trace.log \
> /tmp/event_runtime.log 2> /tmp/event_trace.log; then
ok event-runtime
cat /tmp/event_runtime.log
else
bad event-runtime; cat /tmp/event_runtime.log
fi
else
bad event-runtime-build; cat /tmp/event_runtime_build.log
fi
echo "== event_handler_tests: $pass passed, $fail failed ==$failed"
[ $fail -eq 0 ]