diff --git a/crates/xframe-widgets/src/archetypes.rs b/crates/xframe-widgets/src/archetypes.rs index 9081b7c..6f155c3 100644 --- a/crates/xframe-widgets/src/archetypes.rs +++ b/crates/xframe-widgets/src/archetypes.rs @@ -75,6 +75,18 @@ pub struct ButtonWidget { pub disabled_text_color: Srgb, /// Latched/on state (C++ `InstEmergencyStop` / `InstPushButton` `set_on`). pub on: bool, + /// Default button (draws an extra outer ring, C++ `default_`). + pub default_button: bool, + /// Cancel button (Esc closes the dialog; no visual effect). + pub cancel: bool, + /// Dialog result (Lazarus ModalResult; set by Kind). + pub modal_result: i32, + /// Button kind (0=None, 1=OK, 2=Cancel, 3=Yes, 4=No, 5=Close, + /// 6=Help, 7=Retry, 8=Ignore, 9=Abort). + pub kind_val: i32, + /// Glyph/text layout (0=Left, 1=Right, 2=Top, 3=Bottom; no visual + /// effect until glyph support exists). + pub layout: i32, /// MenuButton popup direction (C++ `GtkMenuButton::direction_`, /// kArrowValues: 0=Up 1=Down 2=Left 3=Right 4=None; default Down). pub direction: i32, @@ -90,6 +102,11 @@ impl ButtonWidget { disabled_text_color: Srgb::new(140, 140, 140), on: false, direction: 1, + default_button: false, + cancel: false, + modal_result: 0, + kind_val: 0, + layout: 0, } } @@ -107,6 +124,42 @@ impl ButtonWidget { pub fn set_on(&mut self, on: bool) { self.on = on; } + pub fn get_default(&self) -> bool { + self.default_button + } + pub fn set_default(&mut self, v: bool) { + self.default_button = v; + } + pub fn get_cancel(&self) -> bool { + self.cancel + } + pub fn set_cancel(&mut self, v: bool) { + self.cancel = v; + } + pub fn get_modal_result(&self) -> i32 { + self.modal_result + } + pub fn set_modal_result(&mut self, v: i32) { + self.modal_result = v; + } + pub fn get_kind(&self) -> i32 { + self.kind_val + } + pub fn set_kind(&mut self, v: i32) { + self.kind_val = v; + const TEXTS: [&str; 10] = ["", "OK", "Cancel", "Yes", "No", "Close", "Help", "Retry", "Ignore", "Abort"]; + const RESULTS: [i32; 10] = [0, 1, 2, 6, 7, 8, 9, 4, 5, 3]; + if (1..=9).contains(&v) { + self.state.text = TEXTS[v as usize].to_string(); + self.modal_result = RESULTS[v as usize]; + } + } + pub fn get_layout(&self) -> i32 { + self.layout + } + pub fn set_layout(&mut self, v: i32) { + self.layout = v; + } /// MenuButton direction (0=Up 1=Down 2=Left 3=Right 4=None). pub fn get_direction(&self) -> i32 { self.direction @@ -123,12 +176,32 @@ archetype_impl!(ButtonWidget, Archetype::Button, fn set_direction(&mut self, d: i32) { self.direction = d.clamp(0, 4); } + fn set_default(&mut self, v: bool) { + self.default_button = v; + } + fn set_cancel(&mut self, v: bool) { + self.cancel = v; + } + fn set_modal_result(&mut self, v: i32) { + self.modal_result = v; + } + fn set_kind(&mut self, v: i32) { + self.set_kind(v); + } + fn set_layout(&mut self, v: i32) { + self.layout = v; + } fn draw(&self, target: &mut dyn DrawTarget, styles: &mut StyleManager) { if !self.state.visible { return; } let style = resolve_style(styles, &self.state.style_name); draw_button_body(target, &style, &self.state, self.disabled_text_color.to_pixel()); + if self.default_button { + let b = self.state.bounds; + target.set_foreground(style.line_color); + target.draw_rect(b.x + 1, b.y + 1, (b.w - 2).max(0), (b.h - 2).max(0)); + } // MenuButton: arrow triangle on the side the popup opens toward // (Left on the left, otherwise on the right; None hides it). if self.kind == "MenuButton" { @@ -173,6 +246,19 @@ pub struct ToggleWidget { pub click_count: u32, pub on_click_cb: Option>, pub checked: bool, + /// Greyed (mixed) state; only settable when `allow_grayed`. + pub inconsistent: bool, + /// Allow the greyed state (C++ `allow_grayed_`). + pub allow_grayed: bool, + /// Box position (0 = left, 1 = right). + pub alignment: i32, + /// Stored values for the checked / unchecked states (Lazarus + /// ValueChecked / ValueUnchecked; model only). + pub value_checked: String, + pub value_unchecked: String, + /// Radio group id (stored; the generator wires same-id buttons + /// into one shared group vector). + pub group_id: i32, /// Disabled text colour (was hardcoded (140,140,140) in `draw_button_body`). pub disabled_text_color: Srgb, } @@ -185,6 +271,12 @@ impl ToggleWidget { click_count: 0, on_click_cb: None, checked: false, + inconsistent: false, + allow_grayed: false, + alignment: 0, + value_checked: String::new(), + value_unchecked: String::new(), + group_id: 0, disabled_text_color: Srgb::new(140, 140, 140), } } @@ -199,6 +291,45 @@ impl ToggleWidget { pub fn new_widget(name: &str, kind: &str, x: i32, y: i32, w: i32, h: i32) -> ToggleWidget { ToggleWidget::new(name, kind, x, y, w, h) } + pub fn get_inconsistent(&self) -> bool { + self.inconsistent + } + pub fn set_inconsistent(&mut self, v: bool) { + if v && !self.allow_grayed { + return; + } + self.inconsistent = v; + } + pub fn get_allow_grayed(&self) -> bool { + self.allow_grayed + } + pub fn set_allow_grayed(&mut self, v: bool) { + self.allow_grayed = v; + } + pub fn get_alignment(&self) -> i32 { + self.alignment + } + pub fn set_alignment(&mut self, v: i32) { + self.alignment = v; + } + pub fn get_value_checked(&self) -> &str { + &self.value_checked + } + pub fn set_value_checked(&mut self, v: &str) { + self.value_checked = v.to_string(); + } + pub fn get_value_unchecked(&self) -> &str { + &self.value_unchecked + } + pub fn set_value_unchecked(&mut self, v: &str) { + self.value_unchecked = v.to_string(); + } + pub fn get_group_id(&self) -> i32 { + self.group_id + } + pub fn set_group_id(&mut self, v: i32) { + self.group_id = v; + } } archetype_impl!(ToggleWidget, Archetype::Toggle, @@ -208,16 +339,42 @@ archetype_impl!(ToggleWidget, Archetype::Toggle, fn set_checked(&mut self, v: bool) { self.checked = v; } + fn set_inconsistent(&mut self, v: bool) { + self.set_inconsistent(v); + } + fn set_allow_grayed(&mut self, v: bool) { + self.allow_grayed = v; + } + fn set_alignment(&mut self, v: i32) { + self.alignment = v; + } + fn set_value_checked(&mut self, v: &str) { + self.value_checked = v.to_string(); + } + fn set_value_unchecked(&mut self, v: &str) { + self.value_unchecked = v.to_string(); + } + fn set_group_id(&mut self, v: i32) { + self.group_id = v; + } fn draw(&self, target: &mut dyn DrawTarget, styles: &mut StyleManager) { if !self.state.visible { return; } let style = resolve_style(styles, &self.state.style_name); draw_button_body(target, &style, &self.state, self.disabled_text_color.to_pixel()); - if self.checked { - let b = self.state.bounds; - let s = (b.h - 10).max(6); - draw_checkmark(target, style.line_color, b.x + 6 + s / 2, b.y + b.h / 2, s); + let b = self.state.bounds; + let s = (b.h - 10).max(6); + let cx = if self.alignment == 1 { + b.x + b.w - 6 - s / 2 + } else { + b.x + 6 + s / 2 + }; + if self.inconsistent { + target.set_foreground(style.line_color); + target.draw_line(cx - s / 2 + 2, b.y + b.h / 2, cx + s / 2 - 2, b.y + b.h / 2); + } else if self.checked { + draw_checkmark(target, style.line_color, cx, b.y + b.h / 2, s); } } fn on_button_press(&mut self, x: i32, y: i32, button: u32) -> bool { @@ -247,6 +404,16 @@ archetype_impl!(ToggleWidget, Archetype::Toggle, /// Static text (optionally with an icon from `assets/icons/`). pub struct LabelWidget { + /// Horizontal alignment (0 = left, 0.5 = centre, 1 = right). + pub xalign: f64, + /// Vertical alignment (0 = top, 0.5 = middle, 1 = bottom). + pub yalign: f64, + /// Transparent background (labels never fill; stored for the model). + pub transparent: bool, + /// Strip single '&' accelerator markers from the displayed text. + pub show_accel_char: bool, + /// Focus target name (Lazarus FocusControl; model only). + pub focus_control: String, pub state: WidgetState, pub kind: String, pub click_count: u32, @@ -260,20 +427,103 @@ impl LabelWidget { kind: kind.to_string(), click_count: 0, on_click_cb: None, + xalign: 0.0, + yalign: 0.5, + transparent: false, + show_accel_char: true, + focus_control: String::new(), } } pub fn new_widget(name: &str, kind: &str, x: i32, y: i32, w: i32, h: i32) -> LabelWidget { LabelWidget::new(name, kind, x, y, w, h) } + /// Display text: single '&' accelerator markers stripped when + /// `show_accel_char` (Lazarus: '&' marks the accelerator, '&&' is + /// a literal '&'). + pub fn display_text(&self) -> String { + if !self.show_accel_char { + return self.state.text.clone(); + } + let mut out = String::new(); + let mut chars = self.state.text.chars().peekable(); + while let Some(c) = chars.next() { + if c == '&' { + if chars.peek() == Some(&'&') { + out.push('&'); + chars.next(); + } + } else { + out.push(c); + } + } + out + } + pub fn get_xalign(&self) -> f64 { + self.xalign + } + pub fn set_xalign(&mut self, v: f64) { + self.xalign = v.clamp(0.0, 1.0); + } + pub fn get_yalign(&self) -> f64 { + self.yalign + } + pub fn set_yalign(&mut self, v: f64) { + self.yalign = v.clamp(0.0, 1.0); + } + pub fn get_transparent(&self) -> bool { + self.transparent + } + pub fn set_transparent(&mut self, v: bool) { + self.transparent = v; + } + pub fn get_show_accel_char(&self) -> bool { + self.show_accel_char + } + pub fn set_show_accel_char(&mut self, v: bool) { + self.show_accel_char = v; + } + pub fn get_focus_control(&self) -> &str { + &self.focus_control + } + pub fn set_focus_control(&mut self, v: &str) { + self.focus_control = v.to_string(); + } } archetype_impl!(LabelWidget, Archetype::Label, + fn set_xalign(&mut self, v: f64) { + self.xalign = v.clamp(0.0, 1.0); + } + fn set_yalign(&mut self, v: f64) { + self.yalign = v.clamp(0.0, 1.0); + } + fn set_transparent(&mut self, v: bool) { + self.transparent = v; + } + fn set_show_accel_char(&mut self, v: bool) { + self.show_accel_char = v; + } + fn set_focus_control(&mut self, v: &str) { + self.focus_control = v.to_string(); + } fn draw(&self, target: &mut dyn DrawTarget, styles: &mut StyleManager) { if !self.state.visible { return; } let style = resolve_style(styles, &self.state.style_name); let b = self.state.bounds; + let text = self.display_text(); + let tw = text_width(&style, &text); + // Vertical window for draw_text_left: the text block placed + // by yalign (default 0.5 = centred, as before). + let (ty1, ty2) = match style.font_metrics { + Some(m) => { + let th = m.ascent + m.descent; + let top = b.y + ((b.h - th) as f64 * self.yalign) as i32; + (top, top + th) + } + None => (b.y, b.y + b.h), + }; // Icon on the left (16x16) when the widget has one, text after it. if let Some(ico) = load_icon(&self.state.name) { if ico.width == 16 && ico.height == 16 { @@ -282,11 +532,13 @@ archetype_impl!(LabelWidget, Archetype::Label, rgba.push(((px[0] as u32) << 16) | ((px[1] as u32) << 8) | (px[2] as u32)); } target.put_image(b.x + 2, b.y + (b.h - 16) / 2, 16, 16, &rgba); - draw_text_left(target, &style, b.x + 22, b.y, b.y + b.h, &self.state.text); + let tx = b.x + 22 + (((b.w - 22 - tw).max(0)) as f64 * self.xalign) as i32; + draw_text_left(target, &style, tx, ty1, ty2, &text); return; } } - draw_text_left(target, &style, b.x, b.y, b.y + b.h, &self.state.text); + let tx = b.x + (((b.w - tw).max(0)) as f64 * self.xalign) as i32; + draw_text_left(target, &style, tx, ty1, ty2, &text); } fn on_button_press(&mut self, x: i32, y: i32, button: u32) -> bool { button_press(&mut self.state, x, y, button) @@ -323,6 +575,19 @@ pub struct EntryWidget { pub password: bool, pub show_password: bool, pub cursor_visible: bool, + /// Text alignment (0 = left, 1 = center, 2 = right). + pub alignment: i32, + /// Character case (0 = normal, 1 = upper, 2 = lower). + pub char_case: i32, + /// Only digits may be typed / set. + pub numbers_only: bool, + /// Keep the selection highlighted when unfocused (no selection is + /// drawn by this renderer; stored for the model). + pub hide_selection: bool, + /// Select all text when the widget gains focus by click. + pub auto_select: bool, + /// Maximum text length (0 = unlimited). + pub max_length: i32, pub multiline: bool, } @@ -340,12 +605,70 @@ impl EntryWidget { show_password: false, cursor_visible: true, multiline: false, + alignment: 0, + char_case: 0, + numbers_only: false, + hide_selection: false, + auto_select: false, + max_length: 0, } } pub fn new_widget(name: &str, kind: &str, x: i32, y: i32, w: i32, h: i32) -> EntryWidget { EntryWidget::new(name, kind, x, y, w, h) } + /// Apply char-case / numbers-only filtering to a candidate text. + pub fn filter_text(&self, t: &str) -> String { + t.chars() + .filter_map(|c| { + if self.numbers_only && !c.is_ascii_digit() { + return None; + } + Some(match self.char_case { + 1 => c.to_ascii_uppercase(), + 2 => c.to_ascii_lowercase(), + _ => c, + }) + }) + .collect() + } + pub fn get_alignment(&self) -> i32 { + self.alignment + } + pub fn set_alignment(&mut self, v: i32) { + self.alignment = v; + } + pub fn get_char_case(&self) -> i32 { + self.char_case + } + pub fn set_char_case(&mut self, v: i32) { + self.char_case = v; + } + pub fn get_numbers_only(&self) -> bool { + self.numbers_only + } + pub fn set_numbers_only(&mut self, v: bool) { + self.numbers_only = v; + } + pub fn get_hide_selection(&self) -> bool { + self.hide_selection + } + pub fn set_hide_selection(&mut self, v: bool) { + self.hide_selection = v; + } + pub fn get_auto_select(&self) -> bool { + self.auto_select + } + pub fn set_auto_select(&mut self, v: bool) { + self.auto_select = v; + } + pub fn get_max_length(&self) -> i32 { + self.max_length + } + pub fn set_max_length(&mut self, v: i32) { + self.max_length = v.max(0); + } + pub fn get_editable(&self) -> bool { self.editable } @@ -379,6 +702,35 @@ impl EntryWidget { } archetype_impl!(EntryWidget, Archetype::Entry, + fn set_text(&mut self, t: &str) { + let filtered = self.filter_text(t); + let text: String = if self.max_length > 0 { + filtered.chars().take(self.max_length as usize).collect() + } else { + filtered + }; + self.state.text = text; + self.cursor = self.state.text.len(); + self.sel_start = None; + } + fn set_alignment(&mut self, v: i32) { + self.alignment = v; + } + fn set_char_case(&mut self, v: i32) { + self.char_case = v; + } + fn set_numbers_only(&mut self, v: bool) { + self.numbers_only = v; + } + fn set_hide_selection(&mut self, v: bool) { + self.hide_selection = v; + } + fn set_auto_select(&mut self, v: bool) { + self.auto_select = v; + } + fn set_max_length(&mut self, v: i32) { + self.max_length = v.max(0); + } fn set_password(&mut self, v: bool) { self.password = v; } @@ -400,11 +752,13 @@ archetype_impl!(EntryWidget, Archetype::Entry, } else { self.state.text.clone() }; - draw_text_left(target, &style, b.x + 4, b.y, b.y + b.h, &shown); + let tw = style.text_width(&shown); + let tx = b.x + 4 + (((b.w - 8 - tw).max(0)) as f64 * self.alignment as f64 / 2.0) as i32; + draw_text_left(target, &style, tx, b.y, b.y + b.h, &shown); if self.state.focused && self.cursor_visible { let tw = style.text_width(&shown[..self.cursor.min(shown.len())]); target.set_foreground(style.text_color); - target.draw_line(b.x + 4 + tw, b.y + 3, b.x + 4 + tw, b.y + b.h - 3); + target.draw_line(tx + tw, b.y + 3, tx + tw, b.y + b.h - 3); } } fn on_button_press(&mut self, x: i32, y: i32, button: u32) -> bool { @@ -417,6 +771,10 @@ archetype_impl!(EntryWidget, Archetype::Entry, let rel = (x - self.state.bounds.x - 4).max(0); self.cursor = ((rel / 8) as usize).min(self.state.text.len()); self.sel_start = None; + if self.auto_select { + self.sel_start = Some(0); + self.cursor = self.state.text.len(); + } true } fn on_button_release(&mut self, x: i32, y: i32, button: u32) -> bool { @@ -464,6 +822,17 @@ archetype_impl!(EntryWidget, Archetype::Entry, _ => { if let Some(c) = text { if (32..127).contains(&(c as u32)) || (self.multiline && c == '\n') { + let c = match self.char_case { + 1 => c.to_ascii_uppercase(), + 2 => c.to_ascii_lowercase(), + _ => c, + }; + if self.numbers_only && !c.is_ascii_digit() && c != '\n' { + return false; + } + if self.max_length > 0 && self.state.text.len() >= self.max_length as usize { + return true; + } self.state.text.insert(self.cursor, c); self.cursor += 1; return true; @@ -490,6 +859,16 @@ pub struct RangeWidget { pub dragging: bool, /// Scrollbar page size (C++ `GtkScrollbar::page_size_`, default 25). pub page_size: f64, + /// Scale / TrackBar page step (C++ `GtkScale::page_step_`, + /// default 10): the PgUp/PgDn step for scales. + pub page_step: f64, + /// Tick-mark positions (C++ `GtkScale::marks_`), set from the + /// tick-marks / frequency props. + pub marks: Vec, + /// Show the selection range between `sel_start` and `sel_end`. + pub show_sel_range: bool, + pub sel_start: f64, + pub sel_end: f64, /// Scrollbar page increment (C++ `GtkScrollbar::page_increment_`, /// default 10): the PgUp/PgDn step. pub page_increment: f64, @@ -508,6 +887,11 @@ impl RangeWidget { dragging: false, page_size: 25.0, page_increment: 10.0, + page_step: 10.0, + marks: Vec::new(), + show_sel_range: false, + sel_start: 0.0, + sel_end: 0.0, } } pub fn new_widget(name: &str, kind: &str, x: i32, y: i32, w: i32, h: i32) -> RangeWidget { @@ -541,6 +925,36 @@ impl RangeWidget { pub fn get_page_increment(&self) -> f64 { self.page_increment } + pub fn get_page_step(&self) -> f64 { + self.page_step + } + pub fn set_page_step(&mut self, v: f64) { + self.page_step = v; + } + pub fn get_marks(&self) -> &[f64] { + &self.marks + } + pub fn set_marks(&mut self, v: &[f64]) { + self.marks = v.to_vec(); + } + pub fn get_show_sel_range(&self) -> bool { + self.show_sel_range + } + pub fn set_show_sel_range(&mut self, v: bool) { + self.show_sel_range = v; + } + pub fn get_sel_start(&self) -> f64 { + self.sel_start + } + pub fn set_sel_start(&mut self, v: f64) { + self.sel_start = v; + } + pub fn get_sel_end(&self) -> f64 { + self.sel_end + } + pub fn set_sel_end(&mut self, v: f64) { + self.sel_end = v; + } pub fn set_page_increment(&mut self, v: f64) { self.page_increment = v; } @@ -580,6 +994,21 @@ archetype_impl!(RangeWidget, Archetype::Range, fn set_page_increment(&mut self, v: f64) { self.page_increment = v; } + fn set_page_step(&mut self, v: f64) { + self.page_step = v; + } + fn set_marks(&mut self, v: &[f64]) { + self.marks = v.to_vec(); + } + fn set_show_sel_range(&mut self, v: bool) { + self.show_sel_range = v; + } + fn set_sel_start(&mut self, v: f64) { + self.sel_start = v; + } + fn set_sel_end(&mut self, v: f64) { + self.sel_end = v; + } fn draw(&self, target: &mut dyn DrawTarget, styles: &mut StyleManager) { if !self.state.visible { return; @@ -588,6 +1017,38 @@ archetype_impl!(RangeWidget, Archetype::Range, let b = self.state.bounds; draw_polished(target, &style, b.x, b.y, b.x + b.w, b.y + b.h); let f = self.frac(); + // Selection range (under the handle) and tick marks. + if self.show_sel_range { + let (mn, mx) = (self.state.min, self.state.max); + let frac = |v: f64| if mx > mn { (v - mn) / (mx - mn) } else { 0.0 }; + target.set_foreground(style.line_color); + if self.orientation == "vertical" { + let y1 = b.y + ((1.0 - frac(self.sel_end)) * b.h as f64) as i32; + let y2 = b.y + ((1.0 - frac(self.sel_start)) * b.h as f64) as i32; + target.fill_rect(b.x + 2, y1, b.w - 4, (y2 - y1).max(1)); + } else { + let x1 = b.x + (frac(self.sel_start) * b.w as f64) as i32; + let x2 = b.x + (frac(self.sel_end) * b.w as f64) as i32; + target.fill_rect(x1, b.y + 2, (x2 - x1).max(1), b.h - 4); + } + } + if !self.marks.is_empty() { + target.set_foreground(style.line_color); + for &m in &self.marks { + let mf = if self.state.max > self.state.min { + (m - self.state.min) / (self.state.max - self.state.min) + } else { + 0.0 + }; + if self.orientation == "vertical" { + let my = b.y + b.h - (mf * b.h as f64) as i32; + target.draw_line(b.x + b.w - 8, my, b.x + b.w - 3, my); + } else { + let mx = b.x + (mf * b.w as f64) as i32; + target.draw_line(mx, b.y + b.h - 8, mx, b.y + b.h - 3); + } + } + } if self.orientation == "vertical" { let th = 12.max(b.h / 8); let ty = b.y + ((1.0 - f) * (b.h - th) as f64) as i32; @@ -684,6 +1145,22 @@ pub struct ListSelWidget { pub multi: bool, pub selected_set: Vec, pub row_h: i32, + /// Per-item checked state (CheckListBox only, parallel to `items`). + pub checked: Vec, + /// Sort items on set (ComboBox / ListBox / CheckListBox). + pub sorted: bool, + /// ComboBox style (0 = DropDown, 1 = Simple, 2 = DropDownList). + pub style_val: i32, + /// ComboBox drop-down row count (default 8; the canvas shows only + /// the selected row, so this is stored for the generator). + pub drop_down_count: i32, + /// ComboBox drop-down width in pixels (0 = widget width; stored). + pub drop_down_width: i32, + /// Stored ComboBox props (the widget has no text editing). + pub read_only: bool, + pub max_length: i32, + pub auto_complete: bool, + pub char_case: i32, pub open: bool, /// Selected-row fill (was hardcoded 0xc6dcf5 in `draw`). pub selected_fill: Srgb, @@ -703,6 +1180,15 @@ impl ListSelWidget { row_h: 20, open: false, selected_fill: Srgb::new(198, 220, 245), + checked: Vec::new(), + sorted: false, + style_val: 0, + drop_down_count: 8, + drop_down_width: 0, + read_only: false, + max_length: 0, + auto_complete: false, + char_case: 0, } } pub fn new_widget(name: &str, kind: &str, x: i32, y: i32, w: i32, h: i32) -> ListSelWidget { @@ -730,13 +1216,45 @@ impl ListSelWidget { } fn is_combo(&self) -> bool { + self.kind.contains("Combo") || self.kind.contains("combo") } + fn is_checklist(&self) -> bool { + self.kind == "CheckListBox" + } + + fn sort_items(&mut self) { + let mut pairs: Vec<(String, bool)> = self + .items + .iter() + .cloned() + .zip(self.checked.iter().copied().chain(std::iter::repeat(false))) + .collect(); + pairs.sort_by(|a, b| a.0.cmp(&b.0)); + self.items = pairs.iter().map(|p| p.0.clone()).collect(); + self.checked = pairs.iter().map(|p| p.1).collect(); + self.checked.resize(self.items.len(), false); + } + + /// Checked-item indices as a comma-separated string (model form). + pub fn checked_items_csv(&self) -> String { + self.checked + .iter() + .enumerate() + .filter(|(_, c)| **c) + .map(|(i, _)| i.to_string()) + .collect::>() + .join(",") + } } archetype_impl!(ListSelWidget, Archetype::ListSel, fn set_items(&mut self, items: &[String]) { self.items = items.to_vec(); + self.checked.resize(self.items.len(), false); + if self.sorted { + self.sort_items(); + } if let Some(sel) = self.selected { if sel >= self.items.len() { self.selected = None; @@ -747,6 +1265,57 @@ archetype_impl!(ListSelWidget, Archetype::ListSel, fn set_multi_select(&mut self, v: bool) { self.multi = v; } + fn set_sorted(&mut self, v: bool) { + self.sorted = v; + if v { + self.sort_items(); + self.selected = None; + self.selected_set.clear(); + } + } + fn set_checked_items(&mut self, csv: &str) { + for c in self.checked.iter_mut() { + *c = false; + } + for part in csv.split(',') { + if let Ok(i) = part.trim().parse::() { + if i < self.checked.len() { + self.checked[i] = true; + } + } + } + } + fn set_selected_row(&mut self, v: i32) { + if v < 0 { + self.selected = None; + } else if (v as usize) < self.items.len() { + self.select(v as usize); + } + } + fn set_row_height(&mut self, v: i32) { + self.row_h = v.max(1); + } + fn set_style(&mut self, v: i32) { + self.style_val = v; + } + fn set_drop_down_count(&mut self, v: i32) { + self.drop_down_count = v.max(1); + } + fn set_drop_down_width(&mut self, v: i32) { + self.drop_down_width = v.max(0); + } + fn set_read_only(&mut self, v: bool) { + self.read_only = v; + } + fn set_max_length(&mut self, v: i32) { + self.max_length = v.max(0); + } + fn set_auto_complete(&mut self, v: bool) { + self.auto_complete = v; + } + fn set_char_case(&mut self, v: i32) { + self.char_case = v; + } fn items(&self) -> &[String] { &self.items } @@ -788,7 +1357,18 @@ archetype_impl!(ListSelWidget, Archetype::ListSel, target.set_foreground(self.selected_fill.to_pixel()); target.fill_rect(b.x + 1, ry, b.w - 2, self.row_h); } - draw_text_left(target, &style, b.x + 4, ry, ry + self.row_h, item); + let mut tx = b.x + 4; + if self.is_checklist() { + let bs = 12; + let by = ry + (self.row_h - bs) / 2; + target.set_foreground(style.outline_color); + target.draw_rect(b.x + 2, by, bs, bs); + if self.checked.get(i).copied().unwrap_or(false) { + draw_checkmark(target, style.line_color, b.x + 2 + bs / 2, by + bs / 2, bs - 2); + } + tx = b.x + 18; + } + draw_text_left(target, &style, tx, ry, ry + self.row_h, item); } if self.is_combo() { draw_triangle_down(target, style.text_color, b.x + b.w - 12, b.y + b.h / 2, 8); @@ -802,6 +1382,17 @@ archetype_impl!(ListSelWidget, Archetype::ListSel, let rel = y - self.state.bounds.y; let i = (rel / self.row_h.max(1)) as usize; if i < self.items.len() { + if self.is_checklist() && x - self.state.bounds.x < 18 { + // Click on the checkbox square toggles the check. + let cur = self.checked.get(i).copied().unwrap_or(false); + if self.checked.len() <= i { + self.checked.resize(i + 1, false); + } + self.checked[i] = !cur; + self.select(i); + self.on_click(); + return true; + } self.select(i); self.on_click(); true diff --git a/crates/xframe-widgets/src/widget.rs b/crates/xframe-widgets/src/widget.rs index 9553c45..fd5369d 100644 --- a/crates/xframe-widgets/src/widget.rs +++ b/crates/xframe-widgets/src/widget.rs @@ -190,6 +190,7 @@ pub trait Widget { false } fn set_checked(&mut self, _checked: bool) {} + fn set_inconsistent(&mut self, _v: bool) {} // -- list / selection widgets (default: no items) -- fn items(&self) -> &[String] { @@ -210,6 +211,45 @@ pub trait Widget { fn set_page_size(&mut self, _v: f64) {} fn set_multi_select(&mut self, _v: bool) {} fn set_items(&mut self, _items: &[String]) {} + // -- Phase 2 per-widget setters (proposal §2.1–§2.10 + CheckListBox; + // default: no-op; archetypes override the ones their class supports) -- + fn set_default(&mut self, _v: bool) {} + fn set_cancel(&mut self, _v: bool) {} + fn set_modal_result(&mut self, _v: i32) {} + fn set_kind(&mut self, _v: i32) {} + fn set_layout(&mut self, _v: i32) {} + fn set_alignment(&mut self, _v: i32) {} + fn set_char_case(&mut self, _v: i32) {} + fn set_numbers_only(&mut self, _v: bool) {} + fn set_hide_selection(&mut self, _v: bool) {} + fn set_auto_select(&mut self, _v: bool) {} + fn set_transparent(&mut self, _v: bool) {} + fn set_show_accel_char(&mut self, _v: bool) {} + fn set_focus_control(&mut self, _v: &str) {} + fn set_allow_grayed(&mut self, _v: bool) {} + fn set_value_checked(&mut self, _v: &str) {} + fn set_value_unchecked(&mut self, _v: &str) {} + fn set_group_id(&mut self, _v: i32) {} + fn set_style(&mut self, _v: i32) {} + fn set_drop_down_count(&mut self, _v: i32) {} + fn set_drop_down_width(&mut self, _v: i32) {} + fn set_sorted(&mut self, _v: bool) {} + fn set_read_only(&mut self, _v: bool) {} + fn set_auto_complete(&mut self, _v: bool) {} + fn set_selected_row(&mut self, _v: i32) {} + fn set_row_height(&mut self, _v: i32) {} + fn set_page_step(&mut self, _v: f64) {} + fn set_marks(&mut self, _v: &[f64]) {} + fn set_show_sel_range(&mut self, _v: bool) {} + fn set_sel_start(&mut self, _v: f64) {} + fn set_sel_end(&mut self, _v: f64) {} + fn set_scroll_bars(&mut self, _v: i32) {} + fn set_want_tabs(&mut self, _v: bool) {} + fn set_want_returns(&mut self, _v: bool) {} + fn set_yalign(&mut self, _v: f64) {} + fn set_checked_items(&mut self, _csv: &str) {} + fn set_max_length(&mut self, _v: i32) {} + fn set_xalign(&mut self, _v: f64) {} // -- containers (default: no children) -- fn children_count(&self) -> usize {