From 4f9f1bff32a52e2c09236f478ca9ea070238bd0d Mon Sep 17 00:00:00 2001 From: Muse Date: Wed, 2 Dec 2026 05:54:31 +0000 Subject: [PATCH] docs(pythondoc): runnable example programs for 5 pilot widgets Minimal complete programs from the designer codegen (one window, one widget, OnClick wired), plus examples/README.md. Screenshots taken under Xvfb prove each runs. Full 547-widget set follows. --- examples/README.md | 41 +++++++++ examples/gtkbutton.py | 168 +++++++++++++++++++++++++++++++++++ examples/gtkcalendar.py | 167 +++++++++++++++++++++++++++++++++++ examples/gtkentry.py | 168 +++++++++++++++++++++++++++++++++++ examples/instanaloggauge.py | 169 ++++++++++++++++++++++++++++++++++++ examples/stockvwap.py | 169 ++++++++++++++++++++++++++++++++++++ 6 files changed, 882 insertions(+) create mode 100644 examples/README.md create mode 100644 examples/gtkbutton.py create mode 100644 examples/gtkcalendar.py create mode 100644 examples/gtkentry.py create mode 100644 examples/instanaloggauge.py create mode 100644 examples/stockvwap.py diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..f0a2e66 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,41 @@ +# LightGUI Python widget examples + +One minimal complete program per widget in the LightGUI Python set +(547 widgets: 194 XFrame + 287 Stock + 66 Instrumentation). + +Each file is the LightGUI designer's own Python codegen output for a +project holding just that widget: one window, the widget placed at its +default size, `OnClick` wired to a handler that prints +`EVENT OnClick`. Some widgets get a seeded demo prop so the +screenshot shows something (a gauge gets `value` 65, a combo box gets +three items, a stock chart gets symbol `AAPL`); charts and indicators +render the built-in deterministic demo price series. + +## Run one + +```sh +PYTHONPATH=/opt/lightgui_python python3 examples/gtkbutton.py +``` + +From a checkout of this repo, `PYTHONPATH` is the repo root (the +directory holding the `lightgui` package). You need a display; under +Xvfb: + +```sh +DISPLAY=:99 python3 examples/gtkbutton.py +``` + +## How they were made + +Generated by `/opt/lightgui-docs/pythondoc/drive.py` on the docs server: +`drive.py gen all` writes these files from `lightgui.model.Project` + +`lightgui.generator.generate`, and `drive.py shot all` runs each one +under Xvfb and screenshots it. The screenshots appear on the widget +pages at https://pythondoc.lightgui.com/. + +## Regenerate + +```sh +python3 /opt/lightgui-docs/pythondoc/drive.py gen all +python3 /opt/lightgui-docs/pythondoc/drive.py shot all --workers=6 +``` diff --git a/examples/gtkbutton.py b/examples/gtkbutton.py new file mode 100644 index 0000000..778386d --- /dev/null +++ b/examples/gtkbutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 Example.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkButton example' (220x120) with 1 widget(s). + +import sys + +import gi + +gi.require_version("Gtk", "3.0") +gi.require_version("Gdk", "3.0") + +from gi.repository import Gdk, Gtk # noqa: E402 +from lightgui.xframe.core import create, parse_color # noqa: E402 + + +def on_GtkButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkButton_001', 'OnClick'): on_GtkButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkButton_001': (True, True, False, False, 16, 16, 150, 36), +} + + +def _apply_anchors(widgets, width, height): + """Reposition anchored widgets for a drawing area of + *width* x *height* (Flag 1: minimal anchor layout). + Computed from the design geometry every time, so a + resize is idempotent — no drift across resizes.""" + design_w, design_h = _DESIGN_SIZE + dx = width - design_w + dy = height - design_h + for widget in widgets: + top, left, right, bottom, ox, oy, ow, oh = \ + _ANCHORS[widget.name] + x, y, w, h = ox, oy, ow, oh + if right and not left: + x = ox + dx + elif right and left: + w = ow + dx + if bottom and not top: + y = oy + dy + elif bottom and top: + h = oh + dy + widget.move_to(x, y) + widget.resize_to(w, h) + +def _build_widgets(): + """Create every placed widget with its props.""" + widgets = [] + widget = create('GtkButton', 'GtkButton_001', 16, 16, 150, 36) + widget.set_prop('label', 'GtkButton_001') + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (220, 120) +_BACKGROUND = '' +_WIDGETS = [] +_LAST_SIZE = [None] +_PRESSED = [None] # Task 4: widget that got the ButtonPress + + +def _hit_widget(widgets, x, y): + """Topmost visible widget containing (*x*, *y*).""" + for widget in reversed(widgets): + if widget.get_prop("visible", "true") in ("false", "0"): + continue + if widget.contains(x, y): + return widget + return None + + +def _dispatch_to(widget, event): + """Call the *event* handler of *widget* (already chosen).""" + if widget is None: + return + if widget.get_prop("enabled", "true") in ("false", "0"): + return + handler = _HANDLERS.get((widget.name, event)) + if handler is not None: + handler(widget) + + +def _dispatch(widgets, x, y, event): + """Call the *event* handler of the widget at (*x*, *y*), + if it has one wired.""" + _dispatch_to(_hit_widget(widgets, x, y), event) + + +def _on_draw(area, cr): + bg = parse_color(_BACKGROUND, None) + if bg is None: + bg = (1.0, 1.0, 1.0) + cr.set_source_rgb(*bg) + cr.paint() + for widget in _WIDGETS: + widget.draw(cr) + return False + + +def _on_button_press(area, event): + if event.type == Gdk.EventType.DOUBLE_BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnDblClick") + elif event.type == Gdk.EventType.BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnMouseDown") + _dispatch(_WIDGETS, event.x, event.y, "OnClick") + # Task 4: remember the pressed widget; release goes there. + _PRESSED[0] = _hit_widget(_WIDGETS, event.x, event.y) + return False + + +def _on_button_release(area, event): + # Task 4: release goes to the widget that got the press, + # even if the pointer moved off it. + widget = _PRESSED[0] + _PRESSED[0] = None + _dispatch_to(widget, "OnMouseUp") + return False + + +def _on_area_resize(area, allocation): + size = (allocation.width, allocation.height) + if size == _LAST_SIZE[0]: + return False + _LAST_SIZE[0] = size + _apply_anchors(_WIDGETS, size[0], size[1]) + area.queue_draw() + return False + + +def main(): + window = Gtk.Window(title='GtkButton example') + window.set_default_size(220, 120) + window.connect("destroy", Gtk.main_quit) + vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, + spacing=0) + window.add(vbox) + area = Gtk.DrawingArea() + area.set_hexpand(True) + area.set_vexpand(True) + area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | + Gdk.EventMask.BUTTON_RELEASE_MASK) + area.connect("draw", _on_draw) + area.connect("button-press-event", _on_button_press) + area.connect("button-release-event", _on_button_release) + area.connect("size-allocate", _on_area_resize) + vbox.pack_start(area, True, True, 0) + _WIDGETS.extend(_build_widgets()) + window.show_all() + Gtk.main() + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/examples/gtkcalendar.py b/examples/gtkcalendar.py new file mode 100644 index 0000000..339ef88 --- /dev/null +++ b/examples/gtkcalendar.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 Example.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkCalendar example' (292x232) with 1 widget(s). + +import sys + +import gi + +gi.require_version("Gtk", "3.0") +gi.require_version("Gdk", "3.0") + +from gi.repository import Gdk, Gtk # noqa: E402 +from lightgui.xframe.core import create, parse_color # noqa: E402 + + +def on_GtkCalendar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkCalendar_001', 'OnClick'): on_GtkCalendar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkCalendar_001': (True, True, False, False, 16, 16, 260, 200), +} + + +def _apply_anchors(widgets, width, height): + """Reposition anchored widgets for a drawing area of + *width* x *height* (Flag 1: minimal anchor layout). + Computed from the design geometry every time, so a + resize is idempotent — no drift across resizes.""" + design_w, design_h = _DESIGN_SIZE + dx = width - design_w + dy = height - design_h + for widget in widgets: + top, left, right, bottom, ox, oy, ow, oh = \ + _ANCHORS[widget.name] + x, y, w, h = ox, oy, ow, oh + if right and not left: + x = ox + dx + elif right and left: + w = ow + dx + if bottom and not top: + y = oy + dy + elif bottom and top: + h = oh + dy + widget.move_to(x, y) + widget.resize_to(w, h) + +def _build_widgets(): + """Create every placed widget with its props.""" + widgets = [] + widget = create('GtkCalendar', 'GtkCalendar_001', 16, 16, 260, 200) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (292, 232) +_BACKGROUND = '' +_WIDGETS = [] +_LAST_SIZE = [None] +_PRESSED = [None] # Task 4: widget that got the ButtonPress + + +def _hit_widget(widgets, x, y): + """Topmost visible widget containing (*x*, *y*).""" + for widget in reversed(widgets): + if widget.get_prop("visible", "true") in ("false", "0"): + continue + if widget.contains(x, y): + return widget + return None + + +def _dispatch_to(widget, event): + """Call the *event* handler of *widget* (already chosen).""" + if widget is None: + return + if widget.get_prop("enabled", "true") in ("false", "0"): + return + handler = _HANDLERS.get((widget.name, event)) + if handler is not None: + handler(widget) + + +def _dispatch(widgets, x, y, event): + """Call the *event* handler of the widget at (*x*, *y*), + if it has one wired.""" + _dispatch_to(_hit_widget(widgets, x, y), event) + + +def _on_draw(area, cr): + bg = parse_color(_BACKGROUND, None) + if bg is None: + bg = (1.0, 1.0, 1.0) + cr.set_source_rgb(*bg) + cr.paint() + for widget in _WIDGETS: + widget.draw(cr) + return False + + +def _on_button_press(area, event): + if event.type == Gdk.EventType.DOUBLE_BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnDblClick") + elif event.type == Gdk.EventType.BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnMouseDown") + _dispatch(_WIDGETS, event.x, event.y, "OnClick") + # Task 4: remember the pressed widget; release goes there. + _PRESSED[0] = _hit_widget(_WIDGETS, event.x, event.y) + return False + + +def _on_button_release(area, event): + # Task 4: release goes to the widget that got the press, + # even if the pointer moved off it. + widget = _PRESSED[0] + _PRESSED[0] = None + _dispatch_to(widget, "OnMouseUp") + return False + + +def _on_area_resize(area, allocation): + size = (allocation.width, allocation.height) + if size == _LAST_SIZE[0]: + return False + _LAST_SIZE[0] = size + _apply_anchors(_WIDGETS, size[0], size[1]) + area.queue_draw() + return False + + +def main(): + window = Gtk.Window(title='GtkCalendar example') + window.set_default_size(292, 232) + window.connect("destroy", Gtk.main_quit) + vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, + spacing=0) + window.add(vbox) + area = Gtk.DrawingArea() + area.set_hexpand(True) + area.set_vexpand(True) + area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | + Gdk.EventMask.BUTTON_RELEASE_MASK) + area.connect("draw", _on_draw) + area.connect("button-press-event", _on_button_press) + area.connect("button-release-event", _on_button_release) + area.connect("size-allocate", _on_area_resize) + vbox.pack_start(area, True, True, 0) + _WIDGETS.extend(_build_widgets()) + window.show_all() + Gtk.main() + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/examples/gtkentry.py b/examples/gtkentry.py new file mode 100644 index 0000000..4f13b54 --- /dev/null +++ b/examples/gtkentry.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 Example.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkEntry example' (220x120) with 1 widget(s). + +import sys + +import gi + +gi.require_version("Gtk", "3.0") +gi.require_version("Gdk", "3.0") + +from gi.repository import Gdk, Gtk # noqa: E402 +from lightgui.xframe.core import create, parse_color # noqa: E402 + + +def on_GtkEntry_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkEntry_001', 'OnClick'): on_GtkEntry_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkEntry_001': (True, True, False, False, 16, 16, 180, 30), +} + + +def _apply_anchors(widgets, width, height): + """Reposition anchored widgets for a drawing area of + *width* x *height* (Flag 1: minimal anchor layout). + Computed from the design geometry every time, so a + resize is idempotent — no drift across resizes.""" + design_w, design_h = _DESIGN_SIZE + dx = width - design_w + dy = height - design_h + for widget in widgets: + top, left, right, bottom, ox, oy, ow, oh = \ + _ANCHORS[widget.name] + x, y, w, h = ox, oy, ow, oh + if right and not left: + x = ox + dx + elif right and left: + w = ow + dx + if bottom and not top: + y = oy + dy + elif bottom and top: + h = oh + dy + widget.move_to(x, y) + widget.resize_to(w, h) + +def _build_widgets(): + """Create every placed widget with its props.""" + widgets = [] + widget = create('GtkEntry', 'GtkEntry_001', 16, 16, 180, 30) + widget.set_prop('text', 'GtkEntry_001') + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (220, 120) +_BACKGROUND = '' +_WIDGETS = [] +_LAST_SIZE = [None] +_PRESSED = [None] # Task 4: widget that got the ButtonPress + + +def _hit_widget(widgets, x, y): + """Topmost visible widget containing (*x*, *y*).""" + for widget in reversed(widgets): + if widget.get_prop("visible", "true") in ("false", "0"): + continue + if widget.contains(x, y): + return widget + return None + + +def _dispatch_to(widget, event): + """Call the *event* handler of *widget* (already chosen).""" + if widget is None: + return + if widget.get_prop("enabled", "true") in ("false", "0"): + return + handler = _HANDLERS.get((widget.name, event)) + if handler is not None: + handler(widget) + + +def _dispatch(widgets, x, y, event): + """Call the *event* handler of the widget at (*x*, *y*), + if it has one wired.""" + _dispatch_to(_hit_widget(widgets, x, y), event) + + +def _on_draw(area, cr): + bg = parse_color(_BACKGROUND, None) + if bg is None: + bg = (1.0, 1.0, 1.0) + cr.set_source_rgb(*bg) + cr.paint() + for widget in _WIDGETS: + widget.draw(cr) + return False + + +def _on_button_press(area, event): + if event.type == Gdk.EventType.DOUBLE_BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnDblClick") + elif event.type == Gdk.EventType.BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnMouseDown") + _dispatch(_WIDGETS, event.x, event.y, "OnClick") + # Task 4: remember the pressed widget; release goes there. + _PRESSED[0] = _hit_widget(_WIDGETS, event.x, event.y) + return False + + +def _on_button_release(area, event): + # Task 4: release goes to the widget that got the press, + # even if the pointer moved off it. + widget = _PRESSED[0] + _PRESSED[0] = None + _dispatch_to(widget, "OnMouseUp") + return False + + +def _on_area_resize(area, allocation): + size = (allocation.width, allocation.height) + if size == _LAST_SIZE[0]: + return False + _LAST_SIZE[0] = size + _apply_anchors(_WIDGETS, size[0], size[1]) + area.queue_draw() + return False + + +def main(): + window = Gtk.Window(title='GtkEntry example') + window.set_default_size(220, 120) + window.connect("destroy", Gtk.main_quit) + vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, + spacing=0) + window.add(vbox) + area = Gtk.DrawingArea() + area.set_hexpand(True) + area.set_vexpand(True) + area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | + Gdk.EventMask.BUTTON_RELEASE_MASK) + area.connect("draw", _on_draw) + area.connect("button-press-event", _on_button_press) + area.connect("button-release-event", _on_button_release) + area.connect("size-allocate", _on_area_resize) + vbox.pack_start(area, True, True, 0) + _WIDGETS.extend(_build_widgets()) + window.show_all() + Gtk.main() + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/examples/instanaloggauge.py b/examples/instanaloggauge.py new file mode 100644 index 0000000..2910320 --- /dev/null +++ b/examples/instanaloggauge.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 Example.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstAnalogGauge example' (232x122) with 1 widget(s). + +import sys + +import gi + +gi.require_version("Gtk", "3.0") +gi.require_version("Gdk", "3.0") + +from gi.repository import Gdk, Gtk # noqa: E402 +from lightgui.xframe.core import create, parse_color # noqa: E402 + + +def on_InstAnalogGauge_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstAnalogGauge_001', 'OnClick'): on_InstAnalogGauge_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstAnalogGauge_001': (True, True, False, False, 16, 16, 200, 90), +} + + +def _apply_anchors(widgets, width, height): + """Reposition anchored widgets for a drawing area of + *width* x *height* (Flag 1: minimal anchor layout). + Computed from the design geometry every time, so a + resize is idempotent — no drift across resizes.""" + design_w, design_h = _DESIGN_SIZE + dx = width - design_w + dy = height - design_h + for widget in widgets: + top, left, right, bottom, ox, oy, ow, oh = \ + _ANCHORS[widget.name] + x, y, w, h = ox, oy, ow, oh + if right and not left: + x = ox + dx + elif right and left: + w = ow + dx + if bottom and not top: + y = oy + dy + elif bottom and top: + h = oh + dy + widget.move_to(x, y) + widget.resize_to(w, h) + +def _build_widgets(): + """Create every placed widget with its props.""" + widgets = [] + widget = create('InstAnalogGauge', 'InstAnalogGauge_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstAnalogGauge_001') + widget.set_prop('value', '65') + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (232, 122) +_BACKGROUND = '' +_WIDGETS = [] +_LAST_SIZE = [None] +_PRESSED = [None] # Task 4: widget that got the ButtonPress + + +def _hit_widget(widgets, x, y): + """Topmost visible widget containing (*x*, *y*).""" + for widget in reversed(widgets): + if widget.get_prop("visible", "true") in ("false", "0"): + continue + if widget.contains(x, y): + return widget + return None + + +def _dispatch_to(widget, event): + """Call the *event* handler of *widget* (already chosen).""" + if widget is None: + return + if widget.get_prop("enabled", "true") in ("false", "0"): + return + handler = _HANDLERS.get((widget.name, event)) + if handler is not None: + handler(widget) + + +def _dispatch(widgets, x, y, event): + """Call the *event* handler of the widget at (*x*, *y*), + if it has one wired.""" + _dispatch_to(_hit_widget(widgets, x, y), event) + + +def _on_draw(area, cr): + bg = parse_color(_BACKGROUND, None) + if bg is None: + bg = (1.0, 1.0, 1.0) + cr.set_source_rgb(*bg) + cr.paint() + for widget in _WIDGETS: + widget.draw(cr) + return False + + +def _on_button_press(area, event): + if event.type == Gdk.EventType.DOUBLE_BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnDblClick") + elif event.type == Gdk.EventType.BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnMouseDown") + _dispatch(_WIDGETS, event.x, event.y, "OnClick") + # Task 4: remember the pressed widget; release goes there. + _PRESSED[0] = _hit_widget(_WIDGETS, event.x, event.y) + return False + + +def _on_button_release(area, event): + # Task 4: release goes to the widget that got the press, + # even if the pointer moved off it. + widget = _PRESSED[0] + _PRESSED[0] = None + _dispatch_to(widget, "OnMouseUp") + return False + + +def _on_area_resize(area, allocation): + size = (allocation.width, allocation.height) + if size == _LAST_SIZE[0]: + return False + _LAST_SIZE[0] = size + _apply_anchors(_WIDGETS, size[0], size[1]) + area.queue_draw() + return False + + +def main(): + window = Gtk.Window(title='InstAnalogGauge example') + window.set_default_size(232, 122) + window.connect("destroy", Gtk.main_quit) + vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, + spacing=0) + window.add(vbox) + area = Gtk.DrawingArea() + area.set_hexpand(True) + area.set_vexpand(True) + area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | + Gdk.EventMask.BUTTON_RELEASE_MASK) + area.connect("draw", _on_draw) + area.connect("button-press-event", _on_button_press) + area.connect("button-release-event", _on_button_release) + area.connect("size-allocate", _on_area_resize) + vbox.pack_start(area, True, True, 0) + _WIDGETS.extend(_build_widgets()) + window.show_all() + Gtk.main() + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/examples/stockvwap.py b/examples/stockvwap.py new file mode 100644 index 0000000..d2c25a0 --- /dev/null +++ b/examples/stockvwap.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 Example.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockVWAP example' (232x122) with 1 widget(s). + +import sys + +import gi + +gi.require_version("Gtk", "3.0") +gi.require_version("Gdk", "3.0") + +from gi.repository import Gdk, Gtk # noqa: E402 +from lightgui.xframe.core import create, parse_color # noqa: E402 + + +def on_StockVWAP_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockVWAP_001', 'OnClick'): on_StockVWAP_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockVWAP_001': (True, True, False, False, 16, 16, 200, 90), +} + + +def _apply_anchors(widgets, width, height): + """Reposition anchored widgets for a drawing area of + *width* x *height* (Flag 1: minimal anchor layout). + Computed from the design geometry every time, so a + resize is idempotent — no drift across resizes.""" + design_w, design_h = _DESIGN_SIZE + dx = width - design_w + dy = height - design_h + for widget in widgets: + top, left, right, bottom, ox, oy, ow, oh = \ + _ANCHORS[widget.name] + x, y, w, h = ox, oy, ow, oh + if right and not left: + x = ox + dx + elif right and left: + w = ow + dx + if bottom and not top: + y = oy + dy + elif bottom and top: + h = oh + dy + widget.move_to(x, y) + widget.resize_to(w, h) + +def _build_widgets(): + """Create every placed widget with its props.""" + widgets = [] + widget = create('StockVWAP', 'StockVWAP_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockVWAP_001') + # NOTE (model-only, no XFrame setter): symbol='AAPL' + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (232, 122) +_BACKGROUND = '' +_WIDGETS = [] +_LAST_SIZE = [None] +_PRESSED = [None] # Task 4: widget that got the ButtonPress + + +def _hit_widget(widgets, x, y): + """Topmost visible widget containing (*x*, *y*).""" + for widget in reversed(widgets): + if widget.get_prop("visible", "true") in ("false", "0"): + continue + if widget.contains(x, y): + return widget + return None + + +def _dispatch_to(widget, event): + """Call the *event* handler of *widget* (already chosen).""" + if widget is None: + return + if widget.get_prop("enabled", "true") in ("false", "0"): + return + handler = _HANDLERS.get((widget.name, event)) + if handler is not None: + handler(widget) + + +def _dispatch(widgets, x, y, event): + """Call the *event* handler of the widget at (*x*, *y*), + if it has one wired.""" + _dispatch_to(_hit_widget(widgets, x, y), event) + + +def _on_draw(area, cr): + bg = parse_color(_BACKGROUND, None) + if bg is None: + bg = (1.0, 1.0, 1.0) + cr.set_source_rgb(*bg) + cr.paint() + for widget in _WIDGETS: + widget.draw(cr) + return False + + +def _on_button_press(area, event): + if event.type == Gdk.EventType.DOUBLE_BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnDblClick") + elif event.type == Gdk.EventType.BUTTON_PRESS: + _dispatch(_WIDGETS, event.x, event.y, "OnMouseDown") + _dispatch(_WIDGETS, event.x, event.y, "OnClick") + # Task 4: remember the pressed widget; release goes there. + _PRESSED[0] = _hit_widget(_WIDGETS, event.x, event.y) + return False + + +def _on_button_release(area, event): + # Task 4: release goes to the widget that got the press, + # even if the pointer moved off it. + widget = _PRESSED[0] + _PRESSED[0] = None + _dispatch_to(widget, "OnMouseUp") + return False + + +def _on_area_resize(area, allocation): + size = (allocation.width, allocation.height) + if size == _LAST_SIZE[0]: + return False + _LAST_SIZE[0] = size + _apply_anchors(_WIDGETS, size[0], size[1]) + area.queue_draw() + return False + + +def main(): + window = Gtk.Window(title='StockVWAP example') + window.set_default_size(232, 122) + window.connect("destroy", Gtk.main_quit) + vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, + spacing=0) + window.add(vbox) + area = Gtk.DrawingArea() + area.set_hexpand(True) + area.set_vexpand(True) + area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | + Gdk.EventMask.BUTTON_RELEASE_MASK) + area.connect("draw", _on_draw) + area.connect("button-press-event", _on_button_press) + area.connect("button-release-event", _on_button_release) + area.connect("size-allocate", _on_area_resize) + vbox.pack_start(area, True, True, 0) + _WIDGETS.extend(_build_widgets()) + window.show_all() + Gtk.main() + return 0 + + +if __name__ == "__main__": + sys.exit(main())