From 62695e02314f2f5d4767a6db7699860fc30946fd Mon Sep 17 00:00:00 2001 From: cnmcdee Date: Mon, 30 Nov 2026 06:23:38 +0000 Subject: [PATCH] Phase 1 R4: Rust generator mirrors the C Phase 1 output byte-for-byte Anchors/effective_anchors, lightgui_pick_font + conditional , apply_autosize helper, per-widget font into emit_widget_styles, Entry password/show-password, Listbox selection-mode 3 -> set_multi_select, Menubutton direction, Scrollbar lower/page-increment, Text cursor-visible, enabled/visible via the IWidget virtuals, the model-only NOTE comment, the apply_anchors resize lambda (with (void) guards for unused sizes) called from ConfigureNotify, and an apply_autosize call per autosize widget. New props emit only when non-default. Fixtures regenerated from the C generator in the next commit. --- crates/lightgui-gen/src/gen.rs | 413 ++++++++++++++++++++++++++++++++- 1 file changed, 411 insertions(+), 2 deletions(-) diff --git a/crates/lightgui-gen/src/gen.rs b/crates/lightgui-gen/src/gen.rs index c2fc338..29033e1 100644 --- a/crates/lightgui-gen/src/gen.rs +++ b/crates/lightgui-gen/src/gen.rs @@ -667,6 +667,9 @@ struct StyleSpec { text_b: i32, radius: i32, linewidth: i32, + // Font argument for add_style_ex: the default XLFD literal, or a + // lightgui_pick_font(...) call when the widget has a custom font. + font_expr: String, } fn style_for(w: &Widget, m: &XFrameMap) -> StyleSpec { @@ -683,6 +686,7 @@ fn style_for(w: &Widget, m: &XFrameMap) -> StyleSpec { text_b: 20, radius: 6, linewidth: 1, + font_expr: "\"-misc-fixed-medium-r-normal--13-*\"".to_string(), }; let has_bg = w.props.contains_key("background") && parse_color(&prop_or(w, "background", "")).is_some(); @@ -692,14 +696,17 @@ fn style_for(w: &Widget, m: &XFrameMap) -> StyleSpec { let br = prop_int(w, "border-radius", 0); let has_bc = w.props.contains_key("border-color") && parse_color(&prop_or(w, "border-color", "")).is_some(); + let font_custom = widget_font_custom(w); s.needed = (has_bg && m.fill.is_empty()) || (has_fg && m.text.is_empty()) || bw > 0 || br > 0 - || has_bc; + || has_bc + || font_custom; if !s.needed { return s; } + s.font_expr = widget_font_expr(w); if has_bg && m.fill.is_empty() { if let Some((r, g, b)) = parse_color(&prop_or(w, "background", "")) { s.fill_r = r; @@ -741,7 +748,7 @@ fn emit_widget_styles(o: &mut String, widgets: &[Widget]) { } let var = &r.var; o.push_str(&format!( - " styles.add_style_ex(\"{var}_style\", Srgb{{{fill_r}, {fill_g}, {fill_b}}}, Srgb{{120, 120, 120}}, Srgb{{{line_r}, {line_g}, {line_b}}}, Srgb{{{text_r}, {text_g}, {text_b}}}, {radius}, {linewidth}, \"-misc-fixed-medium-r-normal--13-*\");\n", + " styles.add_style_ex(\"{var}_style\", Srgb{{{fill_r}, {fill_g}, {fill_b}}}, Srgb{{120, 120, 120}}, Srgb{{{line_r}, {line_g}, {line_b}}}, Srgb{{{text_r}, {text_g}, {text_b}}}, {radius}, {linewidth}, {font_expr});\n", fill_r = s.fill_r, fill_g = s.fill_g, fill_b = s.fill_b, @@ -753,6 +760,7 @@ fn emit_widget_styles(o: &mut String, widgets: &[Widget]) { text_b = s.text_b, radius = s.radius, linewidth = s.linewidth, + font_expr = s.font_expr, )); } } @@ -877,6 +885,19 @@ fn emit_widget_props(o: &mut String, w: &Widget, m: &XFrameMap, var: &str, kind: } )); } + // Password: the new prop wins when present; a legacy + // visibility=false means the same thing. + let pw = if w.props.contains_key("password") { + prop_bool(w, "password", false) + } else { + w.props.contains_key("visibility") && !prop_bool(w, "visibility", true) + }; + if pw { + o.push_str(&format!(" {var}.w.set_password(true);\n")); + } + if prop_bool(w, "show-password", false) { + o.push_str(&format!(" {var}.w.set_show_password(true);\n")); + } } else if t == "Checkbutton" { if w.props.contains_key("active") { o.push_str(&format!( @@ -927,6 +948,16 @@ fn emit_widget_props(o: &mut String, w: &Widget, m: &XFrameMap, var: &str, kind: o.push_str(&format!(" {var}.w.add_row(\"{}\");\n", esc_c(&item))); } } + if w.props.contains_key("selection-mode") && prop_int(w, "selection-mode", 1) == 3 { + o.push_str(&format!(" {var}.w.set_multi_select(true);\n")); + } + } else if t == "Menubutton" { + if w.props.contains_key("direction") && prop_int(w, "direction", 1) != 1 { + o.push_str(&format!( + " {var}.w.set_direction({});\n", + prop_int(w, "direction", 1) + )); + } } else if t == "Scale" { if w.props.contains_key("value") { o.push_str(&format!( @@ -945,6 +976,14 @@ fn emit_widget_props(o: &mut String, w: &Widget, m: &XFrameMap, var: &str, kind: )); } } else if t == "Scrollbar" { + if w.props.contains_key("lower") + && prop_or(w, "lower", "0").parse::().unwrap_or(0.0) != 0.0 + { + o.push_str(&format!( + " {var}.w.set_lower({});\n", + fmt_double(&prop_or(w, "lower", "0")) + )); + } if w.props.contains_key("upper") { o.push_str(&format!( " {var}.w.set_upper({});\n", @@ -963,6 +1002,14 @@ fn emit_widget_props(o: &mut String, w: &Widget, m: &XFrameMap, var: &str, kind: fmt_double(&prop_or(w, "step-increment", "1")) )); } + if w.props.contains_key("page-increment") + && prop_or(w, "page-increment", "10").parse::().unwrap_or(10.0) != 10.0 + { + o.push_str(&format!( + " {var}.w.set_page_increment({});\n", + fmt_double(&prop_or(w, "page-increment", "10")) + )); + } if w.props.contains_key("value") { o.push_str(&format!( " {var}.w.set_value({});\n", @@ -990,6 +1037,9 @@ fn emit_widget_props(o: &mut String, w: &Widget, m: &XFrameMap, var: &str, kind: } )); } + if w.props.contains_key("cursor-visible") && !prop_bool(w, "cursor-visible", true) { + o.push_str(&format!(" {var}.w.set_cursor_visible(false);\n")); + } } else if t == "Memo" { if w.props.contains_key("wrap-mode") { o.push_str(&format!( @@ -1335,6 +1385,59 @@ fn emit_widget_props(o: &mut String, w: &Widget, m: &XFrameMap, var: &str, kind: if style_for(w, m).needed { o.push_str(&format!(" {var}.w.set_style_name(\"{var}_style\");\n")); } + // Phase 1 generic props: enabled/visible go through the IWidget + // virtuals, so they compile for every class (a class without the + // setter is a no-op). Only emitted when false (the default). + if w.props.contains_key("enabled") && !prop_bool(w, "enabled", true) { + o.push_str(&format!(" {var}.set_enabled(false);\n")); + } + if w.props.contains_key("visible") && !prop_bool(w, "visible", true) { + o.push_str(&format!(" {var}.set_visible(false);\n")); + } + // Model-only props (stored in the place file, no XFrame setter): + // recorded here, not silently dropped. + let mut notes: Vec = Vec::new(); + if has("hint") { + notes.push(format!("hint=\"{}\"", esc_c(&prop_or(w, "hint", "")))); + } + if has("tooltip-text") { + notes.push(format!( + "tooltip-text=\"{}\"", + esc_c(&prop_or(w, "tooltip-text", "")) + )); + } + if prop_int(w, "tag", 0) != 0 { + notes.push(format!("tag={}", prop_int(w, "tag", 0))); + } + if has("cursor") { + notes.push(format!("cursor=\"{}\"", esc_c(&prop_or(w, "cursor", "")))); + } + if prop_int(w, "tab-order", 0) != 0 { + notes.push(format!("tab-order={}", prop_int(w, "tab-order", 0))); + } + if w.props.contains_key("tab-stop") && !prop_bool(w, "tab-stop", true) { + notes.push("tab-stop=false".to_string()); + } + for mg in ["margin-left", "margin-top", "margin-right", "margin-bottom", "margin-around"] { + if prop_int(w, mg, 0) != 0 { + notes.push(format!("{mg}={}", prop_int(w, mg, 0))); + } + } + if prop_bool(w, "show-hint", false) { + notes.push("show-hint=true".to_string()); + } + if prop_bool(w, "font-underline", false) { + notes.push("font-underline=true".to_string()); + } + if prop_bool(w, "font-strikeout", false) { + notes.push("font-strikeout=true".to_string()); + } + if !notes.is_empty() { + o.push_str(&format!( + " // NOTE (model-only, no XFrame setter): {}\n", + notes.join(", ") + )); + } } // ------------------------------------------------------- event handlers @@ -1490,6 +1593,167 @@ fn emit_event_wiring(o: &mut String, w: &Widget, var: &str, kind: &str) { } } +// ------------------------------------------------------- Phase 1 helpers +// +// Effective anchors for a widget (Flag 1): align maps to an anchor +// set (Top -> top+left+right, Bottom -> bottom+left+right, Left -> +// left+top+bottom, Right -> right+top+bottom, Client -> all four); +// align None (0) leaves the four anchor bools in charge. The +// Lazarus default is [akTop, akLeft]. +struct Anchors { + top: bool, + left: bool, + right: bool, + bottom: bool, +} + +fn effective_anchors(w: &Widget) -> Anchors { + match prop_int(w, "align", 0) { + 1 => Anchors { top: true, left: true, right: true, bottom: false }, + 2 => Anchors { top: false, left: true, right: true, bottom: true }, + 3 => Anchors { top: true, left: true, right: false, bottom: true }, + 4 => Anchors { top: true, left: false, right: true, bottom: true }, + 5 => Anchors { top: true, left: true, right: true, bottom: true }, + _ => Anchors { + top: prop_bool(w, "anchor-top", true), + left: prop_bool(w, "anchor-left", true), + right: prop_bool(w, "anchor-right", false), + bottom: prop_bool(w, "anchor-bottom", false), + }, + } +} + +fn anchor_label(a: &Anchors) -> String { + let mut parts: Vec<&str> = Vec::new(); + if a.top { + parts.push("top"); + } + if a.left { + parts.push("left"); + } + if a.right { + parts.push("right"); + } + if a.bottom { + parts.push("bottom"); + } + if parts.is_empty() { + "none".to_string() + } else { + parts.join(", ") + } +} + +// True when the widget has a per-widget font that differs from the +// default (fixed, 13, regular): the style then needs the font picker. +fn widget_font_custom(w: &Widget) -> bool { + if !prop_or(w, "font-name", "").is_empty() { + return true; + } + if w.props.contains_key("font-size") && prop_int(w, "font-size", 13) != 13 { + return true; + } + if prop_bool(w, "font-bold", false) { + return true; + } + if prop_bool(w, "font-italic", false) { + return true; + } + false +} + +// The font argument for a per-widget add_style_ex: the default XLFD +// literal, or a lightgui_pick_font(...) call when the widget has a +// custom font (Phase 1, Flag 2). Underline / strikeout have no X +// core-font rendering and do not make the font custom. +fn widget_font_expr(w: &Widget) -> String { + if !widget_font_custom(w) { + return "\"-misc-fixed-medium-r-normal--13-*\"".to_string(); + } + format!( + "lightgui_pick_font(display, \"{}\", {}, {}, {})", + esc_c(&prop_or(w, "font-name", "")), + prop_int(w, "font-size", 13), + if prop_bool(w, "font-bold", false) { "true" } else { "false" }, + if prop_bool(w, "font-italic", false) { "true" } else { "false" }, + ) +} + +// Per-widget font picker, emitted into generated code when a widget +// has a custom font (Phase 1, Flag 2). Returns the first loadable +// XLFD for family/size/bold/italic (the same candidate chain as +// font_xlfd_for in the canvas core). +const LIGHTGUI_PICK_FONT: &str = r##"// Pick a loadable XLFD for a per-widget font (Phase 1, Flag 2): +// family/size/bold/italic, then oblique for italic, then plain, then +// fixed at the requested size, then the fixed 13 default. +static std::string lightgui_pick_font(Display* display, + const std::string& family, int size, + bool bold, bool italic) { + if (size < 6) size = 6; + if (size > 72) size = 72; + std::string fam = family; + for (char& c : fam) c = (char)std::tolower((unsigned char)c); + if (fam.empty()) fam = "fixed"; + std::string weight = bold ? "bold" : "medium"; + std::string slant = italic ? "i" : "r"; + std::string sz = std::to_string(size); + std::vector cands; + if (!fam.empty() && fam[0] == '-') { + cands.push_back(fam); + } else { + cands.push_back("-*-" + fam + "-" + weight + "-" + slant + "-normal--" + sz + "-*"); + if (italic) cands.push_back("-*-" + fam + "-" + weight + "-o-normal--" + sz + "-*"); + cands.push_back("-*-" + fam + "-medium-r-normal--" + sz + "-*"); + } + cands.push_back("-*-fixed-" + weight + "-" + slant + "-normal--" + sz + "-*"); + if (italic) cands.push_back("-*-fixed-" + weight + "-o-normal--" + sz + "-*"); + cands.push_back("-*-fixed-medium-r-normal--" + sz + "-*"); + cands.push_back("-misc-fixed-medium-r-normal--13-*"); + for (const std::string& pat : cands) { + int count = 0; + char** list = XListFonts(display, pat.c_str(), 1, &count); + if (list) { + XFreeFontNames(list); + if (count > 0) return pat; + } + } + return "-misc-fixed-medium-r-normal--13-*"; +} +"##; + +// AutoSize helper, emitted into generated code when a widget has +// autosize=true (Phase 1, Flag 11): resize to fit the caption/text, +// measured with the widget's resolved style font. +const APPLY_AUTOSIZE: &str = r##"// AutoSize (Phase 1, Flag 11): resize a widget to fit its caption/text. +static void apply_autosize(lg::IWidget* w, SStyle_Manager& styles, + const std::string& style_name, int x, int y, + int minw, int minh, int maxw, int maxh) { + std::string text = w->get_text(); + if (text.empty()) return; + SStyle st = styles.resolve(style_name); + XFontStruct* f = st.font_; + if (!f) return; + size_t best_start = 0, best_len = 0, cur_start = 0; + int lines = 1; + for (size_t i = 0; i <= text.size(); ++i) { + if (i == text.size() || text[i] == '\n') { + size_t len = i - cur_start; + if (len > best_len) { best_len = len; best_start = cur_start; } + if (i != text.size()) { lines++; cur_start = i + 1; } + } + } + int ww = XTextWidth(f, text.c_str() + best_start, (int)best_len) + 16; + int hh = (f->ascent + f->descent) * lines + 8; + if (minw > 0 && ww < minw) ww = minw; + if (minh > 0 && hh < minh) hh = minh; + if (maxw > 0 && ww > maxw) ww = maxw; + if (maxh > 0 && hh > maxh) hh = maxh; + if (ww < 24) ww = 24; + if (hh < 16) hh = 16; + w->set_bounds(x, y, ww, hh); +} +"##; + // ------------------------------------------------------- main.cpp emitter fn emit_main_cpp(project: &Project, widgets: &[Widget]) -> String { @@ -1517,6 +1781,25 @@ fn emit_main_cpp(project: &Project, widgets: &[Widget]) -> String { } } + // Phase 1: which generated-code helpers are needed. Computed over + // the resolved widgets only, so an emitted helper is always used + // (an unused static function would warn under -Wall -Wextra). + let mut need_font_helper = false; + let mut need_autosize_helper = false; + let mut need_anchors = !menus.is_empty(); // the menubar spans the width + for r in &resolved { + if widget_font_custom(r.w) { + need_font_helper = true; + } + if prop_bool(r.w, "autosize", false) { + need_autosize_helper = true; + } + let a = effective_anchors(r.w); + if a.right || a.bottom { + need_anchors = true; + } + } + let mut o = String::new(); o.push_str("// Generated by LightGUI (C++/XFrame) — step 4, unified canvas.\n"); o.push_str("// A plain X11/XFrame program: no GTK. Build with the generated\n"); @@ -1541,6 +1824,11 @@ fn emit_main_cpp(project: &Project, widgets: &[Widget]) -> String { o.push_str("#include \n\n"); o.push_str("#include \n"); o.push_str("#include \n"); + // lightgui_pick_font uses std::tolower (only when a widget has a + // custom font; otherwise the include would be unused). + if need_font_helper { + o.push_str("#include \n"); + } o.push_str("#include \n"); o.push_str("#include \n"); o.push_str("#include \n\n"); @@ -1576,6 +1864,15 @@ fn emit_main_cpp(project: &Project, widgets: &[Widget]) -> String { } } + if need_font_helper { + o.push_str(LIGHTGUI_PICK_FONT); + o.push_str("\n"); + } + if need_autosize_helper { + o.push_str(APPLY_AUTOSIZE); + o.push_str("\n"); + } + o.push_str("int main() {\n"); o.push_str(" Display* display = XOpenDisplay(nullptr);\n"); o.push_str( @@ -1662,6 +1959,24 @@ fn emit_main_cpp(project: &Project, widgets: &[Widget]) -> String { )); emit_widget_props(&mut o, w, &r.map, &r.var, &r.kind); emit_event_wiring(&mut o, w, &r.var, &r.kind); + if prop_bool(w, "autosize", false) { + let sty = if style_for(w, &r.map).needed { + format!("{}_style", r.var) + } else { + String::new() + }; + o.push_str(&format!( + " apply_autosize(&{}, styles, \"{}\", {}, {}, {}, {}, {}, {});\n", + r.var, + sty, + w.x, + w.y + menubar_h, + prop_int(w, "min-width", 0), + prop_int(w, "min-height", 0), + prop_int(w, "max-width", 0), + prop_int(w, "max-height", 0) + )); + } } o.push_str(" std::vector widgets = {"); @@ -1678,6 +1993,97 @@ fn emit_main_cpp(project: &Project, widgets: &[Widget]) -> String { o.push_str(&format!(" &{}", r.var)); } o.push_str(" };\n"); + if need_anchors { + o.push_str(" // Anchors (Phase 1, Flag 1): minimal anchor layout. Each anchored widget is repositioned/resized from its design geometry when the window resizes; top+left alone is static and needs no code.\n"); + o.push_str(" auto apply_anchors = [&](int new_w, int new_h) {\n"); + // Silence -Wunused-parameter when the lambda body uses only + // one of the two sizes (menubar-only designs use new_w alone; + // bottom-only anchor sets use new_h alone). + let mut uses_w = !menus.is_empty(); + let mut uses_h = false; + for r in &resolved { + let a = effective_anchors(r.w); + if a.right { + uses_w = true; + } + if a.bottom { + uses_h = true; + } + } + if !uses_w { + o.push_str(" (void)new_w;\n"); + } + if !uses_h { + o.push_str(" (void)new_h;\n"); + } + if !menus.is_empty() { + o.push_str(&format!( + " menubar.set_bounds(0, 0, new_w, {});\n", + menubar_h + )); + } + for r in &resolved { + let w = r.w; + let a = effective_anchors(w); + if !a.right && !a.bottom { + continue; + } + let x = w.x; + let y = w.y + menubar_h; + let mut xs = x.to_string(); + let mut ys = y.to_string(); + let mut ws = w.width.to_string(); + let mut hs = w.height.to_string(); + if a.right && a.left { + ws = format!("{ws} + (new_w - {win_w})"); + } else if a.right { + xs = format!("{xs} + (new_w - {win_w})"); + } + if a.bottom && a.top { + hs = format!("{hs} + (new_h - {win_h})"); + } else if a.bottom { + ys = format!("{ys} + (new_h - {win_h})"); + } + o.push_str(&format!(" {{ // {}: anchors {}\n", r.var, anchor_label(&a))); + o.push_str(&format!(" int _x = {xs};\n")); + o.push_str(&format!(" int _y = {ys};\n")); + o.push_str(&format!(" int _w = {ws};\n")); + o.push_str(&format!(" int _h = {hs};\n")); + if prop_int(w, "min-width", 0) > 0 { + o.push_str(&format!( + " if (_w < {}) _w = {};\n", + prop_int(w, "min-width", 0), + prop_int(w, "min-width", 0) + )); + } + if prop_int(w, "max-width", 0) > 0 { + o.push_str(&format!( + " if (_w > {}) _w = {};\n", + prop_int(w, "max-width", 0), + prop_int(w, "max-width", 0) + )); + } + if prop_int(w, "min-height", 0) > 0 { + o.push_str(&format!( + " if (_h < {}) _h = {};\n", + prop_int(w, "min-height", 0), + prop_int(w, "min-height", 0) + )); + } + if prop_int(w, "max-height", 0) > 0 { + o.push_str(&format!( + " if (_h > {}) _h = {};\n", + prop_int(w, "max-height", 0), + prop_int(w, "max-height", 0) + )); + } + o.push_str(" if (_w < 24) _w = 24;\n"); + o.push_str(" if (_h < 16) _h = 16;\n"); + o.push_str(&format!(" {}.set_bounds(_x, _y, _w, _h);\n", r.var)); + o.push_str(" }\n"); + } + o.push_str(" };\n"); + } o.push_str(" auto redraw_all = [&]() {\n"); o.push_str(" XClearWindow(display, window);\n"); o.push_str(" for (lg::IWidget* wd : widgets) wd->draw();\n"); @@ -1720,6 +2126,9 @@ fn emit_main_cpp(project: &Project, widgets: &[Widget]) -> String { o.push_str(" break;\n"); o.push_str(" }\n"); o.push_str(" case ConfigureNotify:\n"); + if need_anchors { + o.push_str(" apply_anchors(event.xconfigure.width, event.xconfigure.height);\n"); + } o.push_str(" handled = true;\n"); o.push_str(" break;\n"); o.push_str(" case ClientMessage:\n");