diff --git a/examples/gtkaboutdialog.py b/examples/gtkaboutdialog.py new file mode 100644 index 0000000..38f7a8e --- /dev/null +++ b/examples/gtkaboutdialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkaboutdialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkAboutDialog example' (552x292) 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_GtkAboutDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkAboutDialog_001', 'OnClick'): on_GtkAboutDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkAboutDialog_001': (True, True, False, False, 16, 16, 520, 260), +} + + +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('GtkAboutDialog', 'GtkAboutDialog_001', 16, 16, 520, 260) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (552, 292) +_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='GtkAboutDialog example') + window.set_default_size(552, 292) + 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/gtkaccellabel.py b/examples/gtkaccellabel.py new file mode 100644 index 0000000..b3ea05c --- /dev/null +++ b/examples/gtkaccellabel.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkaccellabel.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkAccelLabel 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_GtkAccelLabel_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkAccelLabel_001', 'OnClick'): on_GtkAccelLabel_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkAccelLabel_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('GtkAccelLabel', 'GtkAccelLabel_001', 16, 16, 180, 30) + widget.set_prop('label', 'GtkAccelLabel_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='GtkAccelLabel 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/gtkactionbar.py b/examples/gtkactionbar.py new file mode 100644 index 0000000..1297c7c --- /dev/null +++ b/examples/gtkactionbar.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkactionbar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkActionBar 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_GtkActionBar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkActionBar_001', 'OnClick'): on_GtkActionBar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkActionBar_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('GtkActionBar', 'GtkActionBar_001', 16, 16, 200, 90) + 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='GtkActionBar 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/gtkappchooserbutton.py b/examples/gtkappchooserbutton.py new file mode 100644 index 0000000..d9e045f --- /dev/null +++ b/examples/gtkappchooserbutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkappchooserbutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkAppChooserButton 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_GtkAppChooserButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkAppChooserButton_001', 'OnClick'): on_GtkAppChooserButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkAppChooserButton_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('GtkAppChooserButton', 'GtkAppChooserButton_001', 16, 16, 150, 36) + widget.set_prop('label', 'GtkAppChooserButton_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='GtkAppChooserButton 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/gtkappchooserdialog.py b/examples/gtkappchooserdialog.py new file mode 100644 index 0000000..f1bed6b --- /dev/null +++ b/examples/gtkappchooserdialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkappchooserdialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkAppChooserDialog example' (332x252) 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_GtkAppChooserDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkAppChooserDialog_001', 'OnClick'): on_GtkAppChooserDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkAppChooserDialog_001': (True, True, False, False, 16, 16, 300, 220), +} + + +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('GtkAppChooserDialog', 'GtkAppChooserDialog_001', 16, 16, 300, 220) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (332, 252) +_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='GtkAppChooserDialog example') + window.set_default_size(332, 252) + 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/gtkappchooserwidget.py b/examples/gtkappchooserwidget.py new file mode 100644 index 0000000..fe520f2 --- /dev/null +++ b/examples/gtkappchooserwidget.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkappchooserwidget.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkAppChooserWidget example' (352x272) 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_GtkAppChooserWidget_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkAppChooserWidget_001', 'OnClick'): on_GtkAppChooserWidget_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkAppChooserWidget_001': (True, True, False, False, 16, 16, 320, 240), +} + + +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('GtkAppChooserWidget', 'GtkAppChooserWidget_001', 16, 16, 320, 240) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (352, 272) +_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='GtkAppChooserWidget example') + window.set_default_size(352, 272) + 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/gtkapplicationwindow.py b/examples/gtkapplicationwindow.py new file mode 100644 index 0000000..9d2eac9 --- /dev/null +++ b/examples/gtkapplicationwindow.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkapplicationwindow.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkApplicationWindow example' (332x252) 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_GtkApplicationWindow_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkApplicationWindow_001', 'OnClick'): on_GtkApplicationWindow_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkApplicationWindow_001': (True, True, False, False, 16, 16, 300, 220), +} + + +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('GtkApplicationWindow', 'GtkApplicationWindow_001', 16, 16, 300, 220) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (332, 252) +_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='GtkApplicationWindow example') + window.set_default_size(332, 252) + 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/gtkaspectframe.py b/examples/gtkaspectframe.py new file mode 100644 index 0000000..a9f531f --- /dev/null +++ b/examples/gtkaspectframe.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkaspectframe.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkAspectFrame 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_GtkAspectFrame_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkAspectFrame_001', 'OnClick'): on_GtkAspectFrame_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkAspectFrame_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('GtkAspectFrame', 'GtkAspectFrame_001', 16, 16, 200, 90) + widget.set_prop('label', 'GtkAspectFrame_001') + 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='GtkAspectFrame 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/gtkassistant.py b/examples/gtkassistant.py new file mode 100644 index 0000000..e20a835 --- /dev/null +++ b/examples/gtkassistant.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkassistant.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkAssistant example' (352x312) 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_GtkAssistant_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkAssistant_001', 'OnClick'): on_GtkAssistant_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkAssistant_001': (True, True, False, False, 16, 16, 320, 280), +} + + +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('GtkAssistant', 'GtkAssistant_001', 16, 16, 320, 280) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (352, 312) +_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='GtkAssistant example') + window.set_default_size(352, 312) + 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/gtkbox.py b/examples/gtkbox.py new file mode 100644 index 0000000..600c7b7 --- /dev/null +++ b/examples/gtkbox.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkbox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkBox 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_GtkBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkBox_001', 'OnClick'): on_GtkBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkBox_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('GtkBox', 'GtkBox_001', 16, 16, 200, 90) + 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='GtkBox 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/gtkbutton.py b/examples/gtkbutton.py index 778386d..97cecc5 100644 --- a/examples/gtkbutton.py +++ b/examples/gtkbutton.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # Generated by LightGUI (Python/XFrame). # A plain Python/GTK program: no CMake. Run with: -# python3 Example.py +# python3 gtkbutton.py # with the LightGUI Python package on PYTHONPATH # (the designer sets it when it runs this script). # diff --git a/examples/gtkbuttonbox.py b/examples/gtkbuttonbox.py new file mode 100644 index 0000000..b54dfa6 --- /dev/null +++ b/examples/gtkbuttonbox.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkbuttonbox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkButtonBox 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_GtkButtonBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkButtonBox_001', 'OnClick'): on_GtkButtonBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkButtonBox_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('GtkButtonBox', 'GtkButtonBox_001', 16, 16, 150, 36) + 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='GtkButtonBox 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 index 339ef88..326b012 100644 --- a/examples/gtkcalendar.py +++ b/examples/gtkcalendar.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # Generated by LightGUI (Python/XFrame). # A plain Python/GTK program: no CMake. Run with: -# python3 Example.py +# python3 gtkcalendar.py # with the LightGUI Python package on PYTHONPATH # (the designer sets it when it runs this script). # diff --git a/examples/gtkcellview.py b/examples/gtkcellview.py new file mode 100644 index 0000000..604a317 --- /dev/null +++ b/examples/gtkcellview.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkcellview.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkCellView 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_GtkCellView_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkCellView_001', 'OnClick'): on_GtkCellView_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkCellView_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('GtkCellView', 'GtkCellView_001', 16, 16, 200, 90) + 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='GtkCellView 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/gtkcenterbox.py b/examples/gtkcenterbox.py new file mode 100644 index 0000000..4ad0e9c --- /dev/null +++ b/examples/gtkcenterbox.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkcenterbox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkCenterBox 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_GtkCenterBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkCenterBox_001', 'OnClick'): on_GtkCenterBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkCenterBox_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('GtkCenterBox', 'GtkCenterBox_001', 16, 16, 200, 90) + 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='GtkCenterBox 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/gtkcheckbutton.py b/examples/gtkcheckbutton.py new file mode 100644 index 0000000..724e944 --- /dev/null +++ b/examples/gtkcheckbutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkcheckbutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkCheckButton 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_GtkCheckButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkCheckButton_001', 'OnClick'): on_GtkCheckButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkCheckButton_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('GtkCheckButton', 'GtkCheckButton_001', 16, 16, 150, 36) + widget.set_prop('label', 'GtkCheckButton_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='GtkCheckButton 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/gtkcheckmenuitem.py b/examples/gtkcheckmenuitem.py new file mode 100644 index 0000000..5d6dcfc --- /dev/null +++ b/examples/gtkcheckmenuitem.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkcheckmenuitem.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkCheckMenuItem 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_GtkCheckMenuItem_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkCheckMenuItem_001', 'OnClick'): on_GtkCheckMenuItem_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkCheckMenuItem_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('GtkCheckMenuItem', 'GtkCheckMenuItem_001', 16, 16, 150, 36) + widget.set_prop('label', 'GtkCheckMenuItem_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='GtkCheckMenuItem 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/gtkcolorbutton.py b/examples/gtkcolorbutton.py new file mode 100644 index 0000000..d244cb0 --- /dev/null +++ b/examples/gtkcolorbutton.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkcolorbutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkColorButton 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_GtkColorButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkColorButton_001', 'OnClick'): on_GtkColorButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkColorButton_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('GtkColorButton', 'GtkColorButton_001', 16, 16, 150, 36) + 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='GtkColorButton 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/gtkcolorchooserdialog.py b/examples/gtkcolorchooserdialog.py new file mode 100644 index 0000000..9699108 --- /dev/null +++ b/examples/gtkcolorchooserdialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkcolorchooserdialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkColorChooserDialog example' (372x272) 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_GtkColorChooserDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkColorChooserDialog_001', 'OnClick'): on_GtkColorChooserDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkColorChooserDialog_001': (True, True, False, False, 16, 16, 340, 240), +} + + +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('GtkColorChooserDialog', 'GtkColorChooserDialog_001', 16, 16, 340, 240) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (372, 272) +_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='GtkColorChooserDialog example') + window.set_default_size(372, 272) + 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/gtkcolorchooserwidget.py b/examples/gtkcolorchooserwidget.py new file mode 100644 index 0000000..4d967a2 --- /dev/null +++ b/examples/gtkcolorchooserwidget.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkcolorchooserwidget.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkColorChooserWidget example' (372x292) 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_GtkColorChooserWidget_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkColorChooserWidget_001', 'OnClick'): on_GtkColorChooserWidget_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkColorChooserWidget_001': (True, True, False, False, 16, 16, 340, 260), +} + + +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('GtkColorChooserWidget', 'GtkColorChooserWidget_001', 16, 16, 340, 260) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (372, 292) +_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='GtkColorChooserWidget example') + window.set_default_size(372, 292) + 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/gtkcolordialogbutton.py b/examples/gtkcolordialogbutton.py new file mode 100644 index 0000000..fad9516 --- /dev/null +++ b/examples/gtkcolordialogbutton.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkcolordialogbutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkColorDialogButton example' (332x252) 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_GtkColorDialogButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkColorDialogButton_001', 'OnClick'): on_GtkColorDialogButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkColorDialogButton_001': (True, True, False, False, 16, 16, 300, 220), +} + + +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('GtkColorDialogButton', 'GtkColorDialogButton_001', 16, 16, 300, 220) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (332, 252) +_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='GtkColorDialogButton example') + window.set_default_size(332, 252) + 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/gtkcolumnview.py b/examples/gtkcolumnview.py new file mode 100644 index 0000000..4f3979e --- /dev/null +++ b/examples/gtkcolumnview.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkcolumnview.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkColumnView example' (292x212) 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_GtkColumnView_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkColumnView_001', 'OnClick'): on_GtkColumnView_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkColumnView_001': (True, True, False, False, 16, 16, 260, 180), +} + + +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('GtkColumnView', 'GtkColumnView_001', 16, 16, 260, 180) + widget.set_prop('items', 'Alpha|Beta|Gamma') + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (292, 212) +_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='GtkColumnView example') + window.set_default_size(292, 212) + 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/gtkcombobox.py b/examples/gtkcombobox.py new file mode 100644 index 0000000..a0548f6 --- /dev/null +++ b/examples/gtkcombobox.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkcombobox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkComboBox 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_GtkComboBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkComboBox_001', 'OnClick'): on_GtkComboBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkComboBox_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('GtkComboBox', 'GtkComboBox_001', 16, 16, 180, 30) + widget.set_prop('items', 'Alpha|Beta|Gamma') + widget.set_prop('text', 'GtkComboBox_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='GtkComboBox 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/gtkcomboboxtext.py b/examples/gtkcomboboxtext.py new file mode 100644 index 0000000..0aa40de --- /dev/null +++ b/examples/gtkcomboboxtext.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkcomboboxtext.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkComboBoxText 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_GtkComboBoxText_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkComboBoxText_001', 'OnClick'): on_GtkComboBoxText_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkComboBoxText_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('GtkComboBoxText', 'GtkComboBoxText_001', 16, 16, 180, 30) + widget.set_prop('items', 'Alpha|Beta|Gamma') + widget.set_prop('text', 'GtkComboBoxText_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='GtkComboBoxText 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/gtkdialog.py b/examples/gtkdialog.py new file mode 100644 index 0000000..5d5cfa6 --- /dev/null +++ b/examples/gtkdialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkdialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkDialog example' (332x252) 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_GtkDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkDialog_001', 'OnClick'): on_GtkDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkDialog_001': (True, True, False, False, 16, 16, 300, 220), +} + + +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('GtkDialog', 'GtkDialog_001', 16, 16, 300, 220) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (332, 252) +_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='GtkDialog example') + window.set_default_size(332, 252) + 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/gtkdrawingarea.py b/examples/gtkdrawingarea.py new file mode 100644 index 0000000..d68c26f --- /dev/null +++ b/examples/gtkdrawingarea.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkdrawingarea.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkDrawingArea 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_GtkDrawingArea_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkDrawingArea_001', 'OnClick'): on_GtkDrawingArea_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkDrawingArea_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('GtkDrawingArea', 'GtkDrawingArea_001', 16, 16, 200, 90) + 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='GtkDrawingArea 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/gtkdropdown.py b/examples/gtkdropdown.py new file mode 100644 index 0000000..f2d3d41 --- /dev/null +++ b/examples/gtkdropdown.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkdropdown.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkDropDown 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_GtkDropDown_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkDropDown_001', 'OnClick'): on_GtkDropDown_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkDropDown_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('GtkDropDown', 'GtkDropDown_001', 16, 16, 200, 90) + widget.set_prop('items', 'Alpha|Beta|Gamma') + widget.set_prop('text', 'GtkDropDown_001') + 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='GtkDropDown 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/gtkeditablelabel.py b/examples/gtkeditablelabel.py new file mode 100644 index 0000000..5058e89 --- /dev/null +++ b/examples/gtkeditablelabel.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkeditablelabel.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkEditableLabel 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_GtkEditableLabel_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkEditableLabel_001', 'OnClick'): on_GtkEditableLabel_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkEditableLabel_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('GtkEditableLabel', 'GtkEditableLabel_001', 16, 16, 180, 30) + widget.set_prop('text', 'GtkEditableLabel_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='GtkEditableLabel 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/gtkentry.py b/examples/gtkentry.py index 4f13b54..75b80a2 100644 --- a/examples/gtkentry.py +++ b/examples/gtkentry.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # Generated by LightGUI (Python/XFrame). # A plain Python/GTK program: no CMake. Run with: -# python3 Example.py +# python3 gtkentry.py # with the LightGUI Python package on PYTHONPATH # (the designer sets it when it runs this script). # diff --git a/examples/gtkentrycompletion.py b/examples/gtkentrycompletion.py new file mode 100644 index 0000000..5db916f --- /dev/null +++ b/examples/gtkentrycompletion.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkentrycompletion.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkEntryCompletion 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_GtkEntryCompletion_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkEntryCompletion_001', 'OnClick'): on_GtkEntryCompletion_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkEntryCompletion_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('GtkEntryCompletion', 'GtkEntryCompletion_001', 16, 16, 180, 30) + widget.set_prop('items', 'Alpha|Beta|Gamma') + widget.set_prop('text', 'GtkEntryCompletion_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='GtkEntryCompletion 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/gtkeventbox.py b/examples/gtkeventbox.py new file mode 100644 index 0000000..5d75833 --- /dev/null +++ b/examples/gtkeventbox.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkeventbox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkEventBox 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_GtkEventBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkEventBox_001', 'OnClick'): on_GtkEventBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkEventBox_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('GtkEventBox', 'GtkEventBox_001', 16, 16, 200, 90) + 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='GtkEventBox 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/gtkexpander.py b/examples/gtkexpander.py new file mode 100644 index 0000000..b69d4ff --- /dev/null +++ b/examples/gtkexpander.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkexpander.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkExpander 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_GtkExpander_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkExpander_001', 'OnClick'): on_GtkExpander_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkExpander_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('GtkExpander', 'GtkExpander_001', 16, 16, 200, 90) + 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='GtkExpander 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/gtkfilechooserbutton.py b/examples/gtkfilechooserbutton.py new file mode 100644 index 0000000..ad31eb6 --- /dev/null +++ b/examples/gtkfilechooserbutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkfilechooserbutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkFileChooserButton 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_GtkFileChooserButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkFileChooserButton_001', 'OnClick'): on_GtkFileChooserButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkFileChooserButton_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('GtkFileChooserButton', 'GtkFileChooserButton_001', 16, 16, 150, 36) + widget.set_prop('label', 'GtkFileChooserButton_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='GtkFileChooserButton 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/gtkfilechooserdialog.py b/examples/gtkfilechooserdialog.py new file mode 100644 index 0000000..284553c --- /dev/null +++ b/examples/gtkfilechooserdialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkfilechooserdialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkFileChooserDialog example' (392x272) 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_GtkFileChooserDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkFileChooserDialog_001', 'OnClick'): on_GtkFileChooserDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkFileChooserDialog_001': (True, True, False, False, 16, 16, 360, 240), +} + + +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('GtkFileChooserDialog', 'GtkFileChooserDialog_001', 16, 16, 360, 240) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (392, 272) +_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='GtkFileChooserDialog example') + window.set_default_size(392, 272) + 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/gtkfilechooserwidget.py b/examples/gtkfilechooserwidget.py new file mode 100644 index 0000000..a89602d --- /dev/null +++ b/examples/gtkfilechooserwidget.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkfilechooserwidget.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkFileChooserWidget example' (372x292) 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_GtkFileChooserWidget_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkFileChooserWidget_001', 'OnClick'): on_GtkFileChooserWidget_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkFileChooserWidget_001': (True, True, False, False, 16, 16, 340, 260), +} + + +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('GtkFileChooserWidget', 'GtkFileChooserWidget_001', 16, 16, 340, 260) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (372, 292) +_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='GtkFileChooserWidget example') + window.set_default_size(372, 292) + 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/gtkfixed.py b/examples/gtkfixed.py new file mode 100644 index 0000000..3ce591f --- /dev/null +++ b/examples/gtkfixed.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkfixed.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkFixed 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_GtkFixed_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkFixed_001', 'OnClick'): on_GtkFixed_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkFixed_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('GtkFixed', 'GtkFixed_001', 16, 16, 200, 90) + 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='GtkFixed 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/gtkflowbox.py b/examples/gtkflowbox.py new file mode 100644 index 0000000..0b79eff --- /dev/null +++ b/examples/gtkflowbox.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkflowbox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkFlowBox 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_GtkFlowBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkFlowBox_001', 'OnClick'): on_GtkFlowBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkFlowBox_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('GtkFlowBox', 'GtkFlowBox_001', 16, 16, 200, 90) + widget.set_prop('items', 'Alpha|Beta|Gamma') + 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='GtkFlowBox 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/gtkfontbutton.py b/examples/gtkfontbutton.py new file mode 100644 index 0000000..bfc71b1 --- /dev/null +++ b/examples/gtkfontbutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkfontbutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkFontButton 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_GtkFontButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkFontButton_001', 'OnClick'): on_GtkFontButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkFontButton_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('GtkFontButton', 'GtkFontButton_001', 16, 16, 150, 36) + widget.set_prop('label', 'GtkFontButton_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='GtkFontButton 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/gtkfontchooserdialog.py b/examples/gtkfontchooserdialog.py new file mode 100644 index 0000000..7bbe3d8 --- /dev/null +++ b/examples/gtkfontchooserdialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkfontchooserdialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkFontChooserDialog example' (372x272) 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_GtkFontChooserDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkFontChooserDialog_001', 'OnClick'): on_GtkFontChooserDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkFontChooserDialog_001': (True, True, False, False, 16, 16, 340, 240), +} + + +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('GtkFontChooserDialog', 'GtkFontChooserDialog_001', 16, 16, 340, 240) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (372, 272) +_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='GtkFontChooserDialog example') + window.set_default_size(372, 272) + 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/gtkfontchooserwidget.py b/examples/gtkfontchooserwidget.py new file mode 100644 index 0000000..c37c293 --- /dev/null +++ b/examples/gtkfontchooserwidget.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkfontchooserwidget.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkFontChooserWidget example' (372x292) 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_GtkFontChooserWidget_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkFontChooserWidget_001', 'OnClick'): on_GtkFontChooserWidget_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkFontChooserWidget_001': (True, True, False, False, 16, 16, 340, 260), +} + + +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('GtkFontChooserWidget', 'GtkFontChooserWidget_001', 16, 16, 340, 260) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (372, 292) +_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='GtkFontChooserWidget example') + window.set_default_size(372, 292) + 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/gtkframe.py b/examples/gtkframe.py new file mode 100644 index 0000000..50d8c93 --- /dev/null +++ b/examples/gtkframe.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkframe.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkFrame 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_GtkFrame_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkFrame_001', 'OnClick'): on_GtkFrame_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkFrame_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('GtkFrame', 'GtkFrame_001', 16, 16, 200, 90) + widget.set_prop('label', 'GtkFrame_001') + 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='GtkFrame 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/gtkglarea.py b/examples/gtkglarea.py new file mode 100644 index 0000000..3113e35 --- /dev/null +++ b/examples/gtkglarea.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkglarea.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkGLArea 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_GtkGLArea_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkGLArea_001', 'OnClick'): on_GtkGLArea_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkGLArea_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('GtkGLArea', 'GtkGLArea_001', 16, 16, 200, 90) + 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='GtkGLArea 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/gtkgrid.py b/examples/gtkgrid.py new file mode 100644 index 0000000..9768f0b --- /dev/null +++ b/examples/gtkgrid.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkgrid.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkGrid example' (292x212) 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_GtkGrid_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkGrid_001', 'OnClick'): on_GtkGrid_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkGrid_001': (True, True, False, False, 16, 16, 260, 180), +} + + +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('GtkGrid', 'GtkGrid_001', 16, 16, 260, 180) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (292, 212) +_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='GtkGrid example') + window.set_default_size(292, 212) + 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/gtkgridview.py b/examples/gtkgridview.py new file mode 100644 index 0000000..14d2214 --- /dev/null +++ b/examples/gtkgridview.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkgridview.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkGridView example' (292x212) 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_GtkGridView_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkGridView_001', 'OnClick'): on_GtkGridView_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkGridView_001': (True, True, False, False, 16, 16, 260, 180), +} + + +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('GtkGridView', 'GtkGridView_001', 16, 16, 260, 180) + widget.set_prop('items', 'Alpha|Beta|Gamma') + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (292, 212) +_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='GtkGridView example') + window.set_default_size(292, 212) + 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/gtkheaderbar.py b/examples/gtkheaderbar.py new file mode 100644 index 0000000..c2868c9 --- /dev/null +++ b/examples/gtkheaderbar.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkheaderbar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkHeaderBar 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_GtkHeaderBar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkHeaderBar_001', 'OnClick'): on_GtkHeaderBar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkHeaderBar_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('GtkHeaderBar', 'GtkHeaderBar_001', 16, 16, 200, 90) + 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='GtkHeaderBar 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/gtkiconview.py b/examples/gtkiconview.py new file mode 100644 index 0000000..ee55e77 --- /dev/null +++ b/examples/gtkiconview.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkiconview.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkIconView 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_GtkIconView_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkIconView_001', 'OnClick'): on_GtkIconView_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkIconView_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('GtkIconView', 'GtkIconView_001', 16, 16, 200, 90) + widget.set_prop('items', 'Alpha|Beta|Gamma') + 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='GtkIconView 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/gtkimage.py b/examples/gtkimage.py new file mode 100644 index 0000000..7bb4aef --- /dev/null +++ b/examples/gtkimage.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkimage.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkImage 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_GtkImage_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkImage_001', 'OnClick'): on_GtkImage_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkImage_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('GtkImage', 'GtkImage_001', 16, 16, 200, 90) + 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='GtkImage 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/gtkinfobar.py b/examples/gtkinfobar.py new file mode 100644 index 0000000..58f9010 --- /dev/null +++ b/examples/gtkinfobar.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkinfobar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkInfoBar 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_GtkInfoBar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkInfoBar_001', 'OnClick'): on_GtkInfoBar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkInfoBar_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('GtkInfoBar', 'GtkInfoBar_001', 16, 16, 200, 90) + 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='GtkInfoBar 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/gtkinscription.py b/examples/gtkinscription.py new file mode 100644 index 0000000..4900bf3 --- /dev/null +++ b/examples/gtkinscription.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkinscription.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkInscription 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_GtkInscription_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkInscription_001', 'OnClick'): on_GtkInscription_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkInscription_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('GtkInscription', 'GtkInscription_001', 16, 16, 200, 90) + widget.set_prop('label', 'GtkInscription_001') + 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='GtkInscription 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/gtkinvisible.py b/examples/gtkinvisible.py new file mode 100644 index 0000000..fe52513 --- /dev/null +++ b/examples/gtkinvisible.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkinvisible.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkInvisible 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_GtkInvisible_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkInvisible_001', 'OnClick'): on_GtkInvisible_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkInvisible_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('GtkInvisible', 'GtkInvisible_001', 16, 16, 200, 90) + 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='GtkInvisible 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/gtklabel.py b/examples/gtklabel.py new file mode 100644 index 0000000..190046d --- /dev/null +++ b/examples/gtklabel.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtklabel.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkLabel 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_GtkLabel_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkLabel_001', 'OnClick'): on_GtkLabel_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkLabel_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('GtkLabel', 'GtkLabel_001', 16, 16, 180, 30) + widget.set_prop('label', 'GtkLabel_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='GtkLabel 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/gtklayout.py b/examples/gtklayout.py new file mode 100644 index 0000000..62ae90a --- /dev/null +++ b/examples/gtklayout.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtklayout.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkLayout 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_GtkLayout_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkLayout_001', 'OnClick'): on_GtkLayout_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkLayout_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('GtkLayout', 'GtkLayout_001', 16, 16, 200, 90) + 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='GtkLayout 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/gtklevelbar.py b/examples/gtklevelbar.py new file mode 100644 index 0000000..d0e29a0 --- /dev/null +++ b/examples/gtklevelbar.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtklevelbar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkLevelBar 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_GtkLevelBar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkLevelBar_001', 'OnClick'): on_GtkLevelBar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkLevelBar_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('GtkLevelBar', 'GtkLevelBar_001', 16, 16, 180, 30) + widget.set_prop('value', '65') + 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='GtkLevelBar 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/gtklinkbutton.py b/examples/gtklinkbutton.py new file mode 100644 index 0000000..0ce7b20 --- /dev/null +++ b/examples/gtklinkbutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtklinkbutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkLinkButton 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_GtkLinkButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkLinkButton_001', 'OnClick'): on_GtkLinkButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkLinkButton_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('GtkLinkButton', 'GtkLinkButton_001', 16, 16, 150, 36) + widget.set_prop('label', 'GtkLinkButton_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='GtkLinkButton 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/gtklistbox.py b/examples/gtklistbox.py new file mode 100644 index 0000000..d9f51d1 --- /dev/null +++ b/examples/gtklistbox.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtklistbox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkListBox 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_GtkListBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkListBox_001', 'OnClick'): on_GtkListBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkListBox_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('GtkListBox', 'GtkListBox_001', 16, 16, 200, 90) + widget.set_prop('items', 'Alpha|Beta|Gamma') + 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='GtkListBox 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/gtklistview.py b/examples/gtklistview.py new file mode 100644 index 0000000..67149c7 --- /dev/null +++ b/examples/gtklistview.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtklistview.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkListView example' (292x212) 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_GtkListView_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkListView_001', 'OnClick'): on_GtkListView_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkListView_001': (True, True, False, False, 16, 16, 260, 180), +} + + +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('GtkListView', 'GtkListView_001', 16, 16, 260, 180) + widget.set_prop('items', 'Alpha|Beta|Gamma') + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (292, 212) +_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='GtkListView example') + window.set_default_size(292, 212) + 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/gtklockbutton.py b/examples/gtklockbutton.py new file mode 100644 index 0000000..3d52307 --- /dev/null +++ b/examples/gtklockbutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtklockbutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkLockButton 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_GtkLockButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkLockButton_001', 'OnClick'): on_GtkLockButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkLockButton_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('GtkLockButton', 'GtkLockButton_001', 16, 16, 150, 36) + widget.set_prop('label', 'GtkLockButton_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='GtkLockButton 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/gtkmediacontrols.py b/examples/gtkmediacontrols.py new file mode 100644 index 0000000..acb7eaf --- /dev/null +++ b/examples/gtkmediacontrols.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkmediacontrols.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkMediaControls 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_GtkMediaControls_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkMediaControls_001', 'OnClick'): on_GtkMediaControls_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkMediaControls_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('GtkMediaControls', 'GtkMediaControls_001', 16, 16, 200, 90) + 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='GtkMediaControls 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/gtkmenu.py b/examples/gtkmenu.py new file mode 100644 index 0000000..5df975b --- /dev/null +++ b/examples/gtkmenu.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkmenu.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkMenu 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_GtkMenu_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkMenu_001', 'OnClick'): on_GtkMenu_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkMenu_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('GtkMenu', 'GtkMenu_001', 16, 16, 200, 90) + 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='GtkMenu 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/gtkmenubar.py b/examples/gtkmenubar.py new file mode 100644 index 0000000..e0ccd56 --- /dev/null +++ b/examples/gtkmenubar.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkmenubar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkMenuBar 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_GtkMenuBar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkMenuBar_001', 'OnClick'): on_GtkMenuBar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkMenuBar_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('GtkMenuBar', 'GtkMenuBar_001', 16, 16, 200, 90) + 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='GtkMenuBar 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/gtkmenubutton.py b/examples/gtkmenubutton.py new file mode 100644 index 0000000..202df43 --- /dev/null +++ b/examples/gtkmenubutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkmenubutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkMenuButton 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_GtkMenuButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkMenuButton_001', 'OnClick'): on_GtkMenuButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkMenuButton_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('GtkMenuButton', 'GtkMenuButton_001', 16, 16, 150, 36) + widget.set_prop('label', 'GtkMenuButton_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='GtkMenuButton 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/gtkmenuitem.py b/examples/gtkmenuitem.py new file mode 100644 index 0000000..5cd56ac --- /dev/null +++ b/examples/gtkmenuitem.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkmenuitem.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkMenuItem 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_GtkMenuItem_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkMenuItem_001', 'OnClick'): on_GtkMenuItem_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkMenuItem_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('GtkMenuItem', 'GtkMenuItem_001', 16, 16, 200, 90) + 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='GtkMenuItem 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/gtkmessagedialog.py b/examples/gtkmessagedialog.py new file mode 100644 index 0000000..86b88f2 --- /dev/null +++ b/examples/gtkmessagedialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkmessagedialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkMessageDialog example' (332x252) 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_GtkMessageDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkMessageDialog_001', 'OnClick'): on_GtkMessageDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkMessageDialog_001': (True, True, False, False, 16, 16, 300, 220), +} + + +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('GtkMessageDialog', 'GtkMessageDialog_001', 16, 16, 300, 220) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (332, 252) +_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='GtkMessageDialog example') + window.set_default_size(332, 252) + 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/gtkmodelbutton.py b/examples/gtkmodelbutton.py new file mode 100644 index 0000000..110f479 --- /dev/null +++ b/examples/gtkmodelbutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkmodelbutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkModelButton 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_GtkModelButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkModelButton_001', 'OnClick'): on_GtkModelButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkModelButton_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('GtkModelButton', 'GtkModelButton_001', 16, 16, 150, 36) + widget.set_prop('label', 'GtkModelButton_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='GtkModelButton 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/gtknotebook.py b/examples/gtknotebook.py new file mode 100644 index 0000000..ebb3e8c --- /dev/null +++ b/examples/gtknotebook.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtknotebook.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkNotebook example' (292x212) 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_GtkNotebook_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkNotebook_001', 'OnClick'): on_GtkNotebook_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkNotebook_001': (True, True, False, False, 16, 16, 260, 180), +} + + +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('GtkNotebook', 'GtkNotebook_001', 16, 16, 260, 180) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (292, 212) +_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='GtkNotebook example') + window.set_default_size(292, 212) + 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/gtkoverlay.py b/examples/gtkoverlay.py new file mode 100644 index 0000000..57f1fff --- /dev/null +++ b/examples/gtkoverlay.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkoverlay.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkOverlay 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_GtkOverlay_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkOverlay_001', 'OnClick'): on_GtkOverlay_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkOverlay_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('GtkOverlay', 'GtkOverlay_001', 16, 16, 200, 90) + 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='GtkOverlay 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/gtkpaned.py b/examples/gtkpaned.py new file mode 100644 index 0000000..cef402e --- /dev/null +++ b/examples/gtkpaned.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkpaned.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkPaned 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_GtkPaned_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkPaned_001', 'OnClick'): on_GtkPaned_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkPaned_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('GtkPaned', 'GtkPaned_001', 16, 16, 200, 90) + 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='GtkPaned 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/gtkpasswordentry.py b/examples/gtkpasswordentry.py new file mode 100644 index 0000000..c0848d3 --- /dev/null +++ b/examples/gtkpasswordentry.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkpasswordentry.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkPasswordEntry 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_GtkPasswordEntry_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkPasswordEntry_001', 'OnClick'): on_GtkPasswordEntry_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkPasswordEntry_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('GtkPasswordEntry', 'GtkPasswordEntry_001', 16, 16, 180, 30) + widget.set_prop('text', 'GtkPasswordEntry_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='GtkPasswordEntry 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/gtkpicture.py b/examples/gtkpicture.py new file mode 100644 index 0000000..3f5071f --- /dev/null +++ b/examples/gtkpicture.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkpicture.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkPicture 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_GtkPicture_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkPicture_001', 'OnClick'): on_GtkPicture_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkPicture_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('GtkPicture', 'GtkPicture_001', 16, 16, 200, 90) + 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='GtkPicture 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/gtkplacessidebar.py b/examples/gtkplacessidebar.py new file mode 100644 index 0000000..2eb6cbf --- /dev/null +++ b/examples/gtkplacessidebar.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkplacessidebar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkPlacesSidebar 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_GtkPlacesSidebar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkPlacesSidebar_001', 'OnClick'): on_GtkPlacesSidebar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkPlacesSidebar_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('GtkPlacesSidebar', 'GtkPlacesSidebar_001', 16, 16, 200, 90) + widget.set_prop('items', 'Alpha|Beta|Gamma') + 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='GtkPlacesSidebar 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/gtkpopover.py b/examples/gtkpopover.py new file mode 100644 index 0000000..8b67a5e --- /dev/null +++ b/examples/gtkpopover.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkpopover.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkPopover 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_GtkPopover_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkPopover_001', 'OnClick'): on_GtkPopover_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkPopover_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('GtkPopover', 'GtkPopover_001', 16, 16, 200, 90) + 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='GtkPopover 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/gtkprintunixdialog.py b/examples/gtkprintunixdialog.py new file mode 100644 index 0000000..d37f2d2 --- /dev/null +++ b/examples/gtkprintunixdialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkprintunixdialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkPrintUnixDialog example' (392x332) 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_GtkPrintUnixDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkPrintUnixDialog_001', 'OnClick'): on_GtkPrintUnixDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkPrintUnixDialog_001': (True, True, False, False, 16, 16, 360, 300), +} + + +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('GtkPrintUnixDialog', 'GtkPrintUnixDialog_001', 16, 16, 360, 300) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (392, 332) +_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='GtkPrintUnixDialog example') + window.set_default_size(392, 332) + 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/gtkprogressbar.py b/examples/gtkprogressbar.py new file mode 100644 index 0000000..490849f --- /dev/null +++ b/examples/gtkprogressbar.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkprogressbar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkProgressBar 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_GtkProgressBar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkProgressBar_001', 'OnClick'): on_GtkProgressBar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkProgressBar_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('GtkProgressBar', 'GtkProgressBar_001', 16, 16, 180, 30) + widget.set_prop('text', 'GtkProgressBar_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='GtkProgressBar 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/gtkradiobutton.py b/examples/gtkradiobutton.py new file mode 100644 index 0000000..d4fa54c --- /dev/null +++ b/examples/gtkradiobutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkradiobutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkRadioButton 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_GtkRadioButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkRadioButton_001', 'OnClick'): on_GtkRadioButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkRadioButton_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('GtkRadioButton', 'GtkRadioButton_001', 16, 16, 150, 36) + widget.set_prop('label', 'GtkRadioButton_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='GtkRadioButton 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/gtkradiomenuitem.py b/examples/gtkradiomenuitem.py new file mode 100644 index 0000000..c74024a --- /dev/null +++ b/examples/gtkradiomenuitem.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkradiomenuitem.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkRadioMenuItem 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_GtkRadioMenuItem_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkRadioMenuItem_001', 'OnClick'): on_GtkRadioMenuItem_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkRadioMenuItem_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('GtkRadioMenuItem', 'GtkRadioMenuItem_001', 16, 16, 150, 36) + widget.set_prop('label', 'GtkRadioMenuItem_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='GtkRadioMenuItem 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/gtkrevealer.py b/examples/gtkrevealer.py new file mode 100644 index 0000000..f87dab8 --- /dev/null +++ b/examples/gtkrevealer.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkrevealer.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkRevealer 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_GtkRevealer_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkRevealer_001', 'OnClick'): on_GtkRevealer_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkRevealer_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('GtkRevealer', 'GtkRevealer_001', 16, 16, 200, 90) + 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='GtkRevealer 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/gtkscale.py b/examples/gtkscale.py new file mode 100644 index 0000000..acd2dfa --- /dev/null +++ b/examples/gtkscale.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkscale.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkScale 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_GtkScale_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkScale_001', 'OnClick'): on_GtkScale_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkScale_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('GtkScale', 'GtkScale_001', 16, 16, 180, 30) + widget.set_prop('value', '65') + 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='GtkScale 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/gtkscalebutton.py b/examples/gtkscalebutton.py new file mode 100644 index 0000000..7a8a97e --- /dev/null +++ b/examples/gtkscalebutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkscalebutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkScaleButton 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_GtkScaleButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkScaleButton_001', 'OnClick'): on_GtkScaleButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkScaleButton_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('GtkScaleButton', 'GtkScaleButton_001', 16, 16, 150, 36) + widget.set_prop('value', '65') + 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='GtkScaleButton 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/gtkscrollbar.py b/examples/gtkscrollbar.py new file mode 100644 index 0000000..d9094ac --- /dev/null +++ b/examples/gtkscrollbar.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkscrollbar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkScrollbar 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_GtkScrollbar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkScrollbar_001', 'OnClick'): on_GtkScrollbar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkScrollbar_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('GtkScrollbar', 'GtkScrollbar_001', 16, 16, 180, 30) + widget.set_prop('value', '65') + 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='GtkScrollbar 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/gtkscrolledwindow.py b/examples/gtkscrolledwindow.py new file mode 100644 index 0000000..3789cb7 --- /dev/null +++ b/examples/gtkscrolledwindow.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkscrolledwindow.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkScrolledWindow example' (332x252) 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_GtkScrolledWindow_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkScrolledWindow_001', 'OnClick'): on_GtkScrolledWindow_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkScrolledWindow_001': (True, True, False, False, 16, 16, 300, 220), +} + + +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('GtkScrolledWindow', 'GtkScrolledWindow_001', 16, 16, 300, 220) + widget.set_prop('label', 'GtkScrolledWindow_001') + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (332, 252) +_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='GtkScrolledWindow example') + window.set_default_size(332, 252) + 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/gtksearchbar.py b/examples/gtksearchbar.py new file mode 100644 index 0000000..f38d29a --- /dev/null +++ b/examples/gtksearchbar.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtksearchbar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkSearchBar 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_GtkSearchBar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkSearchBar_001', 'OnClick'): on_GtkSearchBar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkSearchBar_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('GtkSearchBar', 'GtkSearchBar_001', 16, 16, 180, 30) + 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='GtkSearchBar 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/gtksearchentry.py b/examples/gtksearchentry.py new file mode 100644 index 0000000..ed417c8 --- /dev/null +++ b/examples/gtksearchentry.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtksearchentry.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkSearchEntry 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_GtkSearchEntry_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkSearchEntry_001', 'OnClick'): on_GtkSearchEntry_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkSearchEntry_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('GtkSearchEntry', 'GtkSearchEntry_001', 16, 16, 180, 30) + widget.set_prop('text', 'GtkSearchEntry_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='GtkSearchEntry 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/gtkseparator.py b/examples/gtkseparator.py new file mode 100644 index 0000000..90c59b3 --- /dev/null +++ b/examples/gtkseparator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkseparator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkSeparator 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_GtkSeparator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkSeparator_001', 'OnClick'): on_GtkSeparator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkSeparator_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('GtkSeparator', 'GtkSeparator_001', 16, 16, 180, 30) + 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='GtkSeparator 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/gtkseparatormenuitem.py b/examples/gtkseparatormenuitem.py new file mode 100644 index 0000000..c1feb02 --- /dev/null +++ b/examples/gtkseparatormenuitem.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkseparatormenuitem.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkSeparatorMenuItem 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_GtkSeparatorMenuItem_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkSeparatorMenuItem_001', 'OnClick'): on_GtkSeparatorMenuItem_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkSeparatorMenuItem_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('GtkSeparatorMenuItem', 'GtkSeparatorMenuItem_001', 16, 16, 180, 30) + 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='GtkSeparatorMenuItem 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/gtkshortcutswindow.py b/examples/gtkshortcutswindow.py new file mode 100644 index 0000000..a253ead --- /dev/null +++ b/examples/gtkshortcutswindow.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkshortcutswindow.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkShortcutsWindow example' (352x332) 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_GtkShortcutsWindow_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkShortcutsWindow_001', 'OnClick'): on_GtkShortcutsWindow_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkShortcutsWindow_001': (True, True, False, False, 16, 16, 320, 300), +} + + +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('GtkShortcutsWindow', 'GtkShortcutsWindow_001', 16, 16, 320, 300) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (352, 332) +_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='GtkShortcutsWindow example') + window.set_default_size(352, 332) + 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/gtkspinbutton.py b/examples/gtkspinbutton.py new file mode 100644 index 0000000..da45abf --- /dev/null +++ b/examples/gtkspinbutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkspinbutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkSpinButton 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_GtkSpinButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkSpinButton_001', 'OnClick'): on_GtkSpinButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkSpinButton_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('GtkSpinButton', 'GtkSpinButton_001', 16, 16, 150, 36) + widget.set_prop('value', '65') + 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='GtkSpinButton 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/gtkspinner.py b/examples/gtkspinner.py new file mode 100644 index 0000000..fb21f25 --- /dev/null +++ b/examples/gtkspinner.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkspinner.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkSpinner 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_GtkSpinner_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkSpinner_001', 'OnClick'): on_GtkSpinner_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkSpinner_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('GtkSpinner', 'GtkSpinner_001', 16, 16, 150, 36) + 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='GtkSpinner 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/gtkstack.py b/examples/gtkstack.py new file mode 100644 index 0000000..e67882e --- /dev/null +++ b/examples/gtkstack.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkstack.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkStack example' (292x212) 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_GtkStack_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkStack_001', 'OnClick'): on_GtkStack_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkStack_001': (True, True, False, False, 16, 16, 260, 180), +} + + +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('GtkStack', 'GtkStack_001', 16, 16, 260, 180) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (292, 212) +_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='GtkStack example') + window.set_default_size(292, 212) + 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/gtkstacksidebar.py b/examples/gtkstacksidebar.py new file mode 100644 index 0000000..57ef200 --- /dev/null +++ b/examples/gtkstacksidebar.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkstacksidebar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkStackSidebar example' (292x212) 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_GtkStackSidebar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkStackSidebar_001', 'OnClick'): on_GtkStackSidebar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkStackSidebar_001': (True, True, False, False, 16, 16, 260, 180), +} + + +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('GtkStackSidebar', 'GtkStackSidebar_001', 16, 16, 260, 180) + widget.set_prop('items', 'Alpha|Beta|Gamma') + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (292, 212) +_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='GtkStackSidebar example') + window.set_default_size(292, 212) + 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/gtkstackswitcher.py b/examples/gtkstackswitcher.py new file mode 100644 index 0000000..703f98a --- /dev/null +++ b/examples/gtkstackswitcher.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkstackswitcher.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkStackSwitcher example' (292x212) 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_GtkStackSwitcher_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkStackSwitcher_001', 'OnClick'): on_GtkStackSwitcher_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkStackSwitcher_001': (True, True, False, False, 16, 16, 260, 180), +} + + +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('GtkStackSwitcher', 'GtkStackSwitcher_001', 16, 16, 260, 180) + widget.set_prop('items', 'Alpha|Beta|Gamma') + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (292, 212) +_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='GtkStackSwitcher example') + window.set_default_size(292, 212) + 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/gtkstatusbar.py b/examples/gtkstatusbar.py new file mode 100644 index 0000000..a4ce1e1 --- /dev/null +++ b/examples/gtkstatusbar.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkstatusbar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkStatusbar 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_GtkStatusbar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkStatusbar_001', 'OnClick'): on_GtkStatusbar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkStatusbar_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('GtkStatusbar', 'GtkStatusbar_001', 16, 16, 180, 30) + widget.set_prop('text', 'GtkStatusbar_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='GtkStatusbar 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/gtkswitch.py b/examples/gtkswitch.py new file mode 100644 index 0000000..43a68c1 --- /dev/null +++ b/examples/gtkswitch.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkswitch.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkSwitch 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_GtkSwitch_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkSwitch_001', 'OnClick'): on_GtkSwitch_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkSwitch_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('GtkSwitch', 'GtkSwitch_001', 16, 16, 150, 36) + widget.set_prop('text', 'GtkSwitch_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='GtkSwitch 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/gtktextview.py b/examples/gtktextview.py new file mode 100644 index 0000000..b17a53d --- /dev/null +++ b/examples/gtktextview.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtktextview.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkTextView example' (292x212) 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_GtkTextView_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkTextView_001', 'OnClick'): on_GtkTextView_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkTextView_001': (True, True, False, False, 16, 16, 260, 180), +} + + +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('GtkTextView', 'GtkTextView_001', 16, 16, 260, 180) + widget.set_prop('text', 'GtkTextView_001') + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (292, 212) +_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='GtkTextView example') + window.set_default_size(292, 212) + 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/gtktogglebutton.py b/examples/gtktogglebutton.py new file mode 100644 index 0000000..bec1d88 --- /dev/null +++ b/examples/gtktogglebutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtktogglebutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkToggleButton 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_GtkToggleButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkToggleButton_001', 'OnClick'): on_GtkToggleButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkToggleButton_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('GtkToggleButton', 'GtkToggleButton_001', 16, 16, 150, 36) + widget.set_prop('text', 'GtkToggleButton_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='GtkToggleButton 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/gtktoggletoolbutton.py b/examples/gtktoggletoolbutton.py new file mode 100644 index 0000000..8cb6d16 --- /dev/null +++ b/examples/gtktoggletoolbutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtktoggletoolbutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkToggleToolButton 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_GtkToggleToolButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkToggleToolButton_001', 'OnClick'): on_GtkToggleToolButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkToggleToolButton_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('GtkToggleToolButton', 'GtkToggleToolButton_001', 16, 16, 150, 36) + widget.set_prop('label', 'GtkToggleToolButton_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='GtkToggleToolButton 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/gtktoolbar.py b/examples/gtktoolbar.py new file mode 100644 index 0000000..3770644 --- /dev/null +++ b/examples/gtktoolbar.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtktoolbar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkToolbar 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_GtkToolbar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkToolbar_001', 'OnClick'): on_GtkToolbar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkToolbar_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('GtkToolbar', 'GtkToolbar_001', 16, 16, 200, 90) + widget.set_prop('items', 'Alpha|Beta|Gamma') + 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='GtkToolbar 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/gtktoolbutton.py b/examples/gtktoolbutton.py new file mode 100644 index 0000000..a6a9738 --- /dev/null +++ b/examples/gtktoolbutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtktoolbutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkToolButton 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_GtkToolButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkToolButton_001', 'OnClick'): on_GtkToolButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkToolButton_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('GtkToolButton', 'GtkToolButton_001', 16, 16, 150, 36) + widget.set_prop('label', 'GtkToolButton_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='GtkToolButton 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/gtktreeview.py b/examples/gtktreeview.py new file mode 100644 index 0000000..c9f8e70 --- /dev/null +++ b/examples/gtktreeview.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtktreeview.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkTreeView example' (352x232) 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_GtkTreeView_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkTreeView_001', 'OnClick'): on_GtkTreeView_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkTreeView_001': (True, True, False, False, 16, 16, 320, 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('GtkTreeView', 'GtkTreeView_001', 16, 16, 320, 200) + widget.set_prop('items', 'Alpha|Beta|Gamma') + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (352, 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='GtkTreeView example') + window.set_default_size(352, 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/gtkvideo.py b/examples/gtkvideo.py new file mode 100644 index 0000000..11591a6 --- /dev/null +++ b/examples/gtkvideo.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkvideo.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkVideo 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_GtkVideo_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkVideo_001', 'OnClick'): on_GtkVideo_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkVideo_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('GtkVideo', 'GtkVideo_001', 16, 16, 200, 90) + 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='GtkVideo 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/gtkviewport.py b/examples/gtkviewport.py new file mode 100644 index 0000000..dca116a --- /dev/null +++ b/examples/gtkviewport.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkviewport.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkViewport 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_GtkViewport_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkViewport_001', 'OnClick'): on_GtkViewport_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkViewport_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('GtkViewport', 'GtkViewport_001', 16, 16, 200, 90) + 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='GtkViewport 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/gtkvolumebutton.py b/examples/gtkvolumebutton.py new file mode 100644 index 0000000..12a9d3b --- /dev/null +++ b/examples/gtkvolumebutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkvolumebutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkVolumeButton 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_GtkVolumeButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkVolumeButton_001', 'OnClick'): on_GtkVolumeButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkVolumeButton_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('GtkVolumeButton', 'GtkVolumeButton_001', 16, 16, 150, 36) + widget.set_prop('label', 'GtkVolumeButton_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='GtkVolumeButton 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/gtkwindow.py b/examples/gtkwindow.py new file mode 100644 index 0000000..bd30923 --- /dev/null +++ b/examples/gtkwindow.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 gtkwindow.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'GtkWindow example' (332x252) 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_GtkWindow_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('GtkWindow_001', 'OnClick'): on_GtkWindow_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'GtkWindow_001': (True, True, False, False, 16, 16, 300, 220), +} + + +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('GtkWindow', 'GtkWindow_001', 16, 16, 300, 220) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (332, 252) +_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='GtkWindow example') + window.set_default_size(332, 252) + 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/indacceleratoroscillator.py b/examples/indacceleratoroscillator.py new file mode 100644 index 0000000..b416d00 --- /dev/null +++ b/examples/indacceleratoroscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indacceleratoroscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndAcceleratorOscillator 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_IndAcceleratorOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndAcceleratorOscillator_001', 'OnClick'): on_IndAcceleratorOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndAcceleratorOscillator_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('IndAcceleratorOscillator', 'IndAcceleratorOscillator_001', 16, 16, 200, 90) + 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='IndAcceleratorOscillator 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/indaccumulationdistributionline.py b/examples/indaccumulationdistributionline.py new file mode 100644 index 0000000..b368d87 --- /dev/null +++ b/examples/indaccumulationdistributionline.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indaccumulationdistributionline.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndAccumulationDistributionLine 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_IndAccumulationDistributionLine_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndAccumulationDistributionLine_001', 'OnClick'): on_IndAccumulationDistributionLine_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndAccumulationDistributionLine_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('IndAccumulationDistributionLine', 'IndAccumulationDistributionLine_001', 16, 16, 200, 90) + 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='IndAccumulationDistributionLine 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/indaccumulativeswingindex.py b/examples/indaccumulativeswingindex.py new file mode 100644 index 0000000..91562c0 --- /dev/null +++ b/examples/indaccumulativeswingindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indaccumulativeswingindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndAccumulativeSwingIndex 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_IndAccumulativeSwingIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndAccumulativeSwingIndex_001', 'OnClick'): on_IndAccumulativeSwingIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndAccumulativeSwingIndex_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('IndAccumulativeSwingIndex', 'IndAccumulativeSwingIndex_001', 16, 16, 200, 90) + 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='IndAccumulativeSwingIndex 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/indadaptivecybercycle.py b/examples/indadaptivecybercycle.py new file mode 100644 index 0000000..feb88c2 --- /dev/null +++ b/examples/indadaptivecybercycle.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indadaptivecybercycle.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndAdaptiveCyberCycle 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_IndAdaptiveCyberCycle_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndAdaptiveCyberCycle_001', 'OnClick'): on_IndAdaptiveCyberCycle_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndAdaptiveCyberCycle_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('IndAdaptiveCyberCycle', 'IndAdaptiveCyberCycle_001', 16, 16, 200, 90) + 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='IndAdaptiveCyberCycle 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/indadaptiversi.py b/examples/indadaptiversi.py new file mode 100644 index 0000000..8703c05 --- /dev/null +++ b/examples/indadaptiversi.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indadaptiversi.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndAdaptiveRSI 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_IndAdaptiveRSI_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndAdaptiveRSI_001', 'OnClick'): on_IndAdaptiveRSI_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndAdaptiveRSI_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('IndAdaptiveRSI', 'IndAdaptiveRSI_001', 16, 16, 200, 90) + 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='IndAdaptiveRSI 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/indadx.py b/examples/indadx.py new file mode 100644 index 0000000..6d50f76 --- /dev/null +++ b/examples/indadx.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indadx.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndADX 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_IndADX_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndADX_001', 'OnClick'): on_IndADX_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndADX_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('IndADX', 'IndADX_001', 16, 16, 200, 90) + 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='IndADX 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/indadxr.py b/examples/indadxr.py new file mode 100644 index 0000000..91f7863 --- /dev/null +++ b/examples/indadxr.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indadxr.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndADXR 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_IndADXR_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndADXR_001', 'OnClick'): on_IndADXR_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndADXR_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('IndADXR', 'IndADXR_001', 16, 16, 200, 90) + 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='IndADXR 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/indalligatorjaw.py b/examples/indalligatorjaw.py new file mode 100644 index 0000000..5b73ae4 --- /dev/null +++ b/examples/indalligatorjaw.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indalligatorjaw.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndAlligatorJaw 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_IndAlligatorJaw_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndAlligatorJaw_001', 'OnClick'): on_IndAlligatorJaw_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndAlligatorJaw_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('IndAlligatorJaw', 'IndAlligatorJaw_001', 16, 16, 200, 90) + 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='IndAlligatorJaw 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/indalligatorlips.py b/examples/indalligatorlips.py new file mode 100644 index 0000000..10cd10a --- /dev/null +++ b/examples/indalligatorlips.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indalligatorlips.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndAlligatorLips 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_IndAlligatorLips_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndAlligatorLips_001', 'OnClick'): on_IndAlligatorLips_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndAlligatorLips_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('IndAlligatorLips', 'IndAlligatorLips_001', 16, 16, 200, 90) + 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='IndAlligatorLips 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/indalligatorteeth.py b/examples/indalligatorteeth.py new file mode 100644 index 0000000..eabebd8 --- /dev/null +++ b/examples/indalligatorteeth.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indalligatorteeth.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndAlligatorTeeth 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_IndAlligatorTeeth_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndAlligatorTeeth_001', 'OnClick'): on_IndAlligatorTeeth_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndAlligatorTeeth_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('IndAlligatorTeeth', 'IndAlligatorTeeth_001', 16, 16, 200, 90) + 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='IndAlligatorTeeth 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/indalma.py b/examples/indalma.py new file mode 100644 index 0000000..07f8e8a --- /dev/null +++ b/examples/indalma.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indalma.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndALMA 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_IndALMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndALMA_001', 'OnClick'): on_IndALMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndALMA_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('IndALMA', 'IndALMA_001', 16, 16, 200, 90) + 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='IndALMA 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/indanchoredvwap.py b/examples/indanchoredvwap.py new file mode 100644 index 0000000..9fdb33e --- /dev/null +++ b/examples/indanchoredvwap.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indanchoredvwap.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndAnchoredVWAP 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_IndAnchoredVWAP_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndAnchoredVWAP_001', 'OnClick'): on_IndAnchoredVWAP_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndAnchoredVWAP_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('IndAnchoredVWAP', 'IndAnchoredVWAP_001', 16, 16, 200, 90) + 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='IndAnchoredVWAP 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/indapo.py b/examples/indapo.py new file mode 100644 index 0000000..aa81d25 --- /dev/null +++ b/examples/indapo.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indapo.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndAPO 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_IndAPO_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndAPO_001', 'OnClick'): on_IndAPO_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndAPO_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('IndAPO', 'IndAPO_001', 16, 16, 200, 90) + 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='IndAPO 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/indaroondown.py b/examples/indaroondown.py new file mode 100644 index 0000000..7547ab2 --- /dev/null +++ b/examples/indaroondown.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indaroondown.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndAroonDown 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_IndAroonDown_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndAroonDown_001', 'OnClick'): on_IndAroonDown_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndAroonDown_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('IndAroonDown', 'IndAroonDown_001', 16, 16, 200, 90) + 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='IndAroonDown 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/indaroonoscillator.py b/examples/indaroonoscillator.py new file mode 100644 index 0000000..4d5bb20 --- /dev/null +++ b/examples/indaroonoscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indaroonoscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndAroonOscillator 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_IndAroonOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndAroonOscillator_001', 'OnClick'): on_IndAroonOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndAroonOscillator_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('IndAroonOscillator', 'IndAroonOscillator_001', 16, 16, 200, 90) + 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='IndAroonOscillator 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/indaroonup.py b/examples/indaroonup.py new file mode 100644 index 0000000..e9b7d9e --- /dev/null +++ b/examples/indaroonup.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indaroonup.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndAroonUp 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_IndAroonUp_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndAroonUp_001', 'OnClick'): on_IndAroonUp_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndAroonUp_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('IndAroonUp', 'IndAroonUp_001', 16, 16, 200, 90) + 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='IndAroonUp 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/indatr.py b/examples/indatr.py new file mode 100644 index 0000000..b044c7e --- /dev/null +++ b/examples/indatr.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indatr.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndATR 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_IndATR_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndATR_001', 'OnClick'): on_IndATR_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndATR_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('IndATR', 'IndATR_001', 16, 16, 200, 90) + 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='IndATR 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/indautocorrelation.py b/examples/indautocorrelation.py new file mode 100644 index 0000000..673770d --- /dev/null +++ b/examples/indautocorrelation.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indautocorrelation.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndAutocorrelation 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_IndAutocorrelation_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndAutocorrelation_001', 'OnClick'): on_IndAutocorrelation_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndAutocorrelation_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('IndAutocorrelation', 'IndAutocorrelation_001', 16, 16, 200, 90) + 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='IndAutocorrelation 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/indawesomeoscillator.py b/examples/indawesomeoscillator.py new file mode 100644 index 0000000..8c6f221 --- /dev/null +++ b/examples/indawesomeoscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indawesomeoscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndAwesomeOscillator 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_IndAwesomeOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndAwesomeOscillator_001', 'OnClick'): on_IndAwesomeOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndAwesomeOscillator_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('IndAwesomeOscillator', 'IndAwesomeOscillator_001', 16, 16, 200, 90) + 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='IndAwesomeOscillator 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/indbalanceofpower.py b/examples/indbalanceofpower.py new file mode 100644 index 0000000..0b73201 --- /dev/null +++ b/examples/indbalanceofpower.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indbalanceofpower.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndBalanceOfPower 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_IndBalanceOfPower_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndBalanceOfPower_001', 'OnClick'): on_IndBalanceOfPower_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndBalanceOfPower_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('IndBalanceOfPower', 'IndBalanceOfPower_001', 16, 16, 200, 90) + 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='IndBalanceOfPower 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/indbearpower.py b/examples/indbearpower.py new file mode 100644 index 0000000..553dcea --- /dev/null +++ b/examples/indbearpower.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indbearpower.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndBearPower 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_IndBearPower_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndBearPower_001', 'OnClick'): on_IndBearPower_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndBearPower_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('IndBearPower', 'IndBearPower_001', 16, 16, 200, 90) + 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='IndBearPower 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/indbollingerbands.py b/examples/indbollingerbands.py new file mode 100644 index 0000000..2c53364 --- /dev/null +++ b/examples/indbollingerbands.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indbollingerbands.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndBollingerBands 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_IndBollingerBands_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndBollingerBands_001', 'OnClick'): on_IndBollingerBands_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndBollingerBands_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('IndBollingerBands', 'IndBollingerBands_001', 16, 16, 200, 90) + 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='IndBollingerBands 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/indbollingerbandwidth.py b/examples/indbollingerbandwidth.py new file mode 100644 index 0000000..7e513e3 --- /dev/null +++ b/examples/indbollingerbandwidth.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indbollingerbandwidth.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndBollingerBandwidth 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_IndBollingerBandwidth_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndBollingerBandwidth_001', 'OnClick'): on_IndBollingerBandwidth_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndBollingerBandwidth_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('IndBollingerBandwidth', 'IndBollingerBandwidth_001', 16, 16, 200, 90) + 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='IndBollingerBandwidth 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/indbollingerpercentb.py b/examples/indbollingerpercentb.py new file mode 100644 index 0000000..341045d --- /dev/null +++ b/examples/indbollingerpercentb.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indbollingerpercentb.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndBollingerPercentB 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_IndBollingerPercentB_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndBollingerPercentB_001', 'OnClick'): on_IndBollingerPercentB_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndBollingerPercentB_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('IndBollingerPercentB', 'IndBollingerPercentB_001', 16, 16, 200, 90) + 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='IndBollingerPercentB 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/indbullpower.py b/examples/indbullpower.py new file mode 100644 index 0000000..f47911d --- /dev/null +++ b/examples/indbullpower.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indbullpower.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndBullPower 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_IndBullPower_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndBullPower_001', 'OnClick'): on_IndBullPower_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndBullPower_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('IndBullPower', 'IndBullPower_001', 16, 16, 200, 90) + 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='IndBullPower 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/indcamarillapivotpoints.py b/examples/indcamarillapivotpoints.py new file mode 100644 index 0000000..9d33395 --- /dev/null +++ b/examples/indcamarillapivotpoints.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indcamarillapivotpoints.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndCamarillaPivotPoints 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_IndCamarillaPivotPoints_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndCamarillaPivotPoints_001', 'OnClick'): on_IndCamarillaPivotPoints_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndCamarillaPivotPoints_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('IndCamarillaPivotPoints', 'IndCamarillaPivotPoints_001', 16, 16, 200, 90) + 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='IndCamarillaPivotPoints 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/indcci.py b/examples/indcci.py new file mode 100644 index 0000000..25f1e5a --- /dev/null +++ b/examples/indcci.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indcci.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndCCI 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_IndCCI_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndCCI_001', 'OnClick'): on_IndCCI_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndCCI_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('IndCCI', 'IndCCI_001', 16, 16, 200, 90) + 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='IndCCI 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/indcenterofgravity.py b/examples/indcenterofgravity.py new file mode 100644 index 0000000..cb6bc3c --- /dev/null +++ b/examples/indcenterofgravity.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indcenterofgravity.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndCenterOfGravity 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_IndCenterOfGravity_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndCenterOfGravity_001', 'OnClick'): on_IndCenterOfGravity_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndCenterOfGravity_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('IndCenterOfGravity', 'IndCenterOfGravity_001', 16, 16, 200, 90) + 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='IndCenterOfGravity 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/indchaikinmoneyflow.py b/examples/indchaikinmoneyflow.py new file mode 100644 index 0000000..a41e39c --- /dev/null +++ b/examples/indchaikinmoneyflow.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indchaikinmoneyflow.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndChaikinMoneyFlow 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_IndChaikinMoneyFlow_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndChaikinMoneyFlow_001', 'OnClick'): on_IndChaikinMoneyFlow_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndChaikinMoneyFlow_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('IndChaikinMoneyFlow', 'IndChaikinMoneyFlow_001', 16, 16, 200, 90) + 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='IndChaikinMoneyFlow 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/indchaikinoscillator.py b/examples/indchaikinoscillator.py new file mode 100644 index 0000000..f8d0218 --- /dev/null +++ b/examples/indchaikinoscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indchaikinoscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndChaikinOscillator 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_IndChaikinOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndChaikinOscillator_001', 'OnClick'): on_IndChaikinOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndChaikinOscillator_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('IndChaikinOscillator', 'IndChaikinOscillator_001', 16, 16, 200, 90) + 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='IndChaikinOscillator 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/indchaikinvolatility.py b/examples/indchaikinvolatility.py new file mode 100644 index 0000000..2521818 --- /dev/null +++ b/examples/indchaikinvolatility.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indchaikinvolatility.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndChaikinVolatility 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_IndChaikinVolatility_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndChaikinVolatility_001', 'OnClick'): on_IndChaikinVolatility_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndChaikinVolatility_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('IndChaikinVolatility', 'IndChaikinVolatility_001', 16, 16, 200, 90) + 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='IndChaikinVolatility 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/indchandeforecastoscillator.py b/examples/indchandeforecastoscillator.py new file mode 100644 index 0000000..463d6f4 --- /dev/null +++ b/examples/indchandeforecastoscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indchandeforecastoscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndChandeForecastOscillator 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_IndChandeForecastOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndChandeForecastOscillator_001', 'OnClick'): on_IndChandeForecastOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndChandeForecastOscillator_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('IndChandeForecastOscillator', 'IndChandeForecastOscillator_001', 16, 16, 200, 90) + 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='IndChandeForecastOscillator 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/indchandekrollstop.py b/examples/indchandekrollstop.py new file mode 100644 index 0000000..034069f --- /dev/null +++ b/examples/indchandekrollstop.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indchandekrollstop.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndChandeKrollStop 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_IndChandeKrollStop_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndChandeKrollStop_001', 'OnClick'): on_IndChandeKrollStop_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndChandeKrollStop_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('IndChandeKrollStop', 'IndChandeKrollStop_001', 16, 16, 200, 90) + 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='IndChandeKrollStop 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/indchandelierexit.py b/examples/indchandelierexit.py new file mode 100644 index 0000000..8b04e15 --- /dev/null +++ b/examples/indchandelierexit.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indchandelierexit.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndChandelierExit 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_IndChandelierExit_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndChandelierExit_001', 'OnClick'): on_IndChandelierExit_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndChandelierExit_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('IndChandelierExit', 'IndChandelierExit_001', 16, 16, 200, 90) + 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='IndChandelierExit 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/indchandelierexitlong.py b/examples/indchandelierexitlong.py new file mode 100644 index 0000000..a88cdcd --- /dev/null +++ b/examples/indchandelierexitlong.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indchandelierexitlong.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndChandelierExitLong 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_IndChandelierExitLong_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndChandelierExitLong_001', 'OnClick'): on_IndChandelierExitLong_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndChandelierExitLong_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('IndChandelierExitLong', 'IndChandelierExitLong_001', 16, 16, 200, 90) + 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='IndChandelierExitLong 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/indchandelierexitshort.py b/examples/indchandelierexitshort.py new file mode 100644 index 0000000..412e020 --- /dev/null +++ b/examples/indchandelierexitshort.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indchandelierexitshort.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndChandelierExitShort 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_IndChandelierExitShort_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndChandelierExitShort_001', 'OnClick'): on_IndChandelierExitShort_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndChandelierExitShort_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('IndChandelierExitShort', 'IndChandelierExitShort_001', 16, 16, 200, 90) + 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='IndChandelierExitShort 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/indchandemomentumoscillator.py b/examples/indchandemomentumoscillator.py new file mode 100644 index 0000000..a07cfbb --- /dev/null +++ b/examples/indchandemomentumoscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indchandemomentumoscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndChandeMomentumOscillator 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_IndChandeMomentumOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndChandeMomentumOscillator_001', 'OnClick'): on_IndChandeMomentumOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndChandeMomentumOscillator_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('IndChandeMomentumOscillator', 'IndChandeMomentumOscillator_001', 16, 16, 200, 90) + 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='IndChandeMomentumOscillator 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/indchikouspan.py b/examples/indchikouspan.py new file mode 100644 index 0000000..b2bd54d --- /dev/null +++ b/examples/indchikouspan.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indchikouspan.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndChikouSpan 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_IndChikouSpan_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndChikouSpan_001', 'OnClick'): on_IndChikouSpan_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndChikouSpan_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('IndChikouSpan', 'IndChikouSpan_001', 16, 16, 200, 90) + 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='IndChikouSpan 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/indchoppinessindex.py b/examples/indchoppinessindex.py new file mode 100644 index 0000000..72fe306 --- /dev/null +++ b/examples/indchoppinessindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indchoppinessindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndChoppinessIndex 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_IndChoppinessIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndChoppinessIndex_001', 'OnClick'): on_IndChoppinessIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndChoppinessIndex_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('IndChoppinessIndex', 'IndChoppinessIndex_001', 16, 16, 200, 90) + 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='IndChoppinessIndex 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/indclosevolatility.py b/examples/indclosevolatility.py new file mode 100644 index 0000000..eced2c7 --- /dev/null +++ b/examples/indclosevolatility.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indclosevolatility.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndCloseVolatility 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_IndCloseVolatility_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndCloseVolatility_001', 'OnClick'): on_IndCloseVolatility_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndCloseVolatility_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('IndCloseVolatility', 'IndCloseVolatility_001', 16, 16, 200, 90) + 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='IndCloseVolatility 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/indconnorsrsi.py b/examples/indconnorsrsi.py new file mode 100644 index 0000000..bad90c4 --- /dev/null +++ b/examples/indconnorsrsi.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indconnorsrsi.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndConnorsRSI 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_IndConnorsRSI_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndConnorsRSI_001', 'OnClick'): on_IndConnorsRSI_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndConnorsRSI_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('IndConnorsRSI', 'IndConnorsRSI_001', 16, 16, 200, 90) + 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='IndConnorsRSI 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/indcoppockcurve.py b/examples/indcoppockcurve.py new file mode 100644 index 0000000..1951b95 --- /dev/null +++ b/examples/indcoppockcurve.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indcoppockcurve.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndCoppockCurve 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_IndCoppockCurve_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndCoppockCurve_001', 'OnClick'): on_IndCoppockCurve_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndCoppockCurve_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('IndCoppockCurve', 'IndCoppockCurve_001', 16, 16, 200, 90) + 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='IndCoppockCurve 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/indcorrelation.py b/examples/indcorrelation.py new file mode 100644 index 0000000..cb13f1e --- /dev/null +++ b/examples/indcorrelation.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indcorrelation.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndCorrelation 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_IndCorrelation_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndCorrelation_001', 'OnClick'): on_IndCorrelation_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndCorrelation_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('IndCorrelation', 'IndCorrelation_001', 16, 16, 200, 90) + 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='IndCorrelation 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/indcorrelationcoefficient.py b/examples/indcorrelationcoefficient.py new file mode 100644 index 0000000..373ee7d --- /dev/null +++ b/examples/indcorrelationcoefficient.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indcorrelationcoefficient.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndCorrelationCoefficient 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_IndCorrelationCoefficient_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndCorrelationCoefficient_001', 'OnClick'): on_IndCorrelationCoefficient_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndCorrelationCoefficient_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('IndCorrelationCoefficient', 'IndCorrelationCoefficient_001', 16, 16, 200, 90) + 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='IndCorrelationCoefficient 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/indcovariance.py b/examples/indcovariance.py new file mode 100644 index 0000000..dcda0af --- /dev/null +++ b/examples/indcovariance.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indcovariance.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndCovariance 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_IndCovariance_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndCovariance_001', 'OnClick'): on_IndCovariance_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndCovariance_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('IndCovariance', 'IndCovariance_001', 16, 16, 200, 90) + 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='IndCovariance 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/indcumulativevolumedelta.py b/examples/indcumulativevolumedelta.py new file mode 100644 index 0000000..8150bc5 --- /dev/null +++ b/examples/indcumulativevolumedelta.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indcumulativevolumedelta.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndCumulativeVolumeDelta 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_IndCumulativeVolumeDelta_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndCumulativeVolumeDelta_001', 'OnClick'): on_IndCumulativeVolumeDelta_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndCumulativeVolumeDelta_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('IndCumulativeVolumeDelta', 'IndCumulativeVolumeDelta_001', 16, 16, 200, 90) + 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='IndCumulativeVolumeDelta 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/indcutlerrsi.py b/examples/indcutlerrsi.py new file mode 100644 index 0000000..9e668a8 --- /dev/null +++ b/examples/indcutlerrsi.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indcutlerrsi.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndCutlerRSI 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_IndCutlerRSI_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndCutlerRSI_001', 'OnClick'): on_IndCutlerRSI_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndCutlerRSI_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('IndCutlerRSI', 'IndCutlerRSI_001', 16, 16, 200, 90) + 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='IndCutlerRSI 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/indcybercycle.py b/examples/indcybercycle.py new file mode 100644 index 0000000..c2e9b56 --- /dev/null +++ b/examples/indcybercycle.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indcybercycle.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndCyberCycle 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_IndCyberCycle_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndCyberCycle_001', 'OnClick'): on_IndCyberCycle_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndCyberCycle_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('IndCyberCycle', 'IndCyberCycle_001', 16, 16, 200, 90) + 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='IndCyberCycle 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/inddecycler.py b/examples/inddecycler.py new file mode 100644 index 0000000..7bd3890 --- /dev/null +++ b/examples/inddecycler.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 inddecycler.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndDecycler 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_IndDecycler_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndDecycler_001', 'OnClick'): on_IndDecycler_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndDecycler_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('IndDecycler', 'IndDecycler_001', 16, 16, 200, 90) + 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='IndDecycler 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/inddema.py b/examples/inddema.py new file mode 100644 index 0000000..ab7da1c --- /dev/null +++ b/examples/inddema.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 inddema.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndDEMA 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_IndDEMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndDEMA_001', 'OnClick'): on_IndDEMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndDEMA_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('IndDEMA', 'IndDEMA_001', 16, 16, 200, 90) + 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='IndDEMA 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/inddemandindex.py b/examples/inddemandindex.py new file mode 100644 index 0000000..147d1bf --- /dev/null +++ b/examples/inddemandindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 inddemandindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndDemandIndex 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_IndDemandIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndDemandIndex_001', 'OnClick'): on_IndDemandIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndDemandIndex_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('IndDemandIndex', 'IndDemandIndex_001', 16, 16, 200, 90) + 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='IndDemandIndex 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/inddemarker.py b/examples/inddemarker.py new file mode 100644 index 0000000..4f46cfe --- /dev/null +++ b/examples/inddemarker.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 inddemarker.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndDeMarker 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_IndDeMarker_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndDeMarker_001', 'OnClick'): on_IndDeMarker_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndDeMarker_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('IndDeMarker', 'IndDeMarker_001', 16, 16, 200, 90) + 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='IndDeMarker 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/inddetrendedpriceoscillator.py b/examples/inddetrendedpriceoscillator.py new file mode 100644 index 0000000..ece6b23 --- /dev/null +++ b/examples/inddetrendedpriceoscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 inddetrendedpriceoscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndDetrendedPriceOscillator 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_IndDetrendedPriceOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndDetrendedPriceOscillator_001', 'OnClick'): on_IndDetrendedPriceOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndDetrendedPriceOscillator_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('IndDetrendedPriceOscillator', 'IndDetrendedPriceOscillator_001', 16, 16, 200, 90) + 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='IndDetrendedPriceOscillator 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/inddisparityindex.py b/examples/inddisparityindex.py new file mode 100644 index 0000000..78dd7cc --- /dev/null +++ b/examples/inddisparityindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 inddisparityindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndDisparityIndex 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_IndDisparityIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndDisparityIndex_001', 'OnClick'): on_IndDisparityIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndDisparityIndex_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('IndDisparityIndex', 'IndDisparityIndex_001', 16, 16, 200, 90) + 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='IndDisparityIndex 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/inddominantcycle.py b/examples/inddominantcycle.py new file mode 100644 index 0000000..998083c --- /dev/null +++ b/examples/inddominantcycle.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 inddominantcycle.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndDominantCycle 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_IndDominantCycle_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndDominantCycle_001', 'OnClick'): on_IndDominantCycle_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndDominantCycle_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('IndDominantCycle', 'IndDominantCycle_001', 16, 16, 200, 90) + 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='IndDominantCycle 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/inddominantcycleperiod.py b/examples/inddominantcycleperiod.py new file mode 100644 index 0000000..2fd9bae --- /dev/null +++ b/examples/inddominantcycleperiod.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 inddominantcycleperiod.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndDominantCyclePeriod 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_IndDominantCyclePeriod_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndDominantCyclePeriod_001', 'OnClick'): on_IndDominantCyclePeriod_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndDominantCyclePeriod_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('IndDominantCyclePeriod', 'IndDominantCyclePeriod_001', 16, 16, 200, 90) + 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='IndDominantCyclePeriod 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/inddonchianchannels.py b/examples/inddonchianchannels.py new file mode 100644 index 0000000..0f52dbe --- /dev/null +++ b/examples/inddonchianchannels.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 inddonchianchannels.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndDonchianChannels 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_IndDonchianChannels_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndDonchianChannels_001', 'OnClick'): on_IndDonchianChannels_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndDonchianChannels_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('IndDonchianChannels', 'IndDonchianChannels_001', 16, 16, 200, 90) + 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='IndDonchianChannels 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/inddonchianlower.py b/examples/inddonchianlower.py new file mode 100644 index 0000000..044e979 --- /dev/null +++ b/examples/inddonchianlower.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 inddonchianlower.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndDonchianLower 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_IndDonchianLower_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndDonchianLower_001', 'OnClick'): on_IndDonchianLower_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndDonchianLower_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('IndDonchianLower', 'IndDonchianLower_001', 16, 16, 200, 90) + 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='IndDonchianLower 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/inddonchianmiddle.py b/examples/inddonchianmiddle.py new file mode 100644 index 0000000..e569f3b --- /dev/null +++ b/examples/inddonchianmiddle.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 inddonchianmiddle.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndDonchianMiddle 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_IndDonchianMiddle_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndDonchianMiddle_001', 'OnClick'): on_IndDonchianMiddle_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndDonchianMiddle_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('IndDonchianMiddle', 'IndDonchianMiddle_001', 16, 16, 200, 90) + 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='IndDonchianMiddle 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/inddonchianupper.py b/examples/inddonchianupper.py new file mode 100644 index 0000000..e05df35 --- /dev/null +++ b/examples/inddonchianupper.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 inddonchianupper.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndDonchianUpper 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_IndDonchianUpper_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndDonchianUpper_001', 'OnClick'): on_IndDonchianUpper_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndDonchianUpper_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('IndDonchianUpper', 'IndDonchianUpper_001', 16, 16, 200, 90) + 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='IndDonchianUpper 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/inddoublestochastic.py b/examples/inddoublestochastic.py new file mode 100644 index 0000000..f6c52b8 --- /dev/null +++ b/examples/inddoublestochastic.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 inddoublestochastic.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndDoubleStochastic 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_IndDoubleStochastic_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndDoubleStochastic_001', 'OnClick'): on_IndDoubleStochastic_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndDoubleStochastic_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('IndDoubleStochastic', 'IndDoubleStochastic_001', 16, 16, 200, 90) + 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='IndDoubleStochastic 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/inddpo.py b/examples/inddpo.py new file mode 100644 index 0000000..35f7d21 --- /dev/null +++ b/examples/inddpo.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 inddpo.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndDPO 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_IndDPO_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndDPO_001', 'OnClick'): on_IndDPO_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndDPO_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('IndDPO', 'IndDPO_001', 16, 16, 200, 90) + 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='IndDPO 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/inddss.py b/examples/inddss.py new file mode 100644 index 0000000..7329959 --- /dev/null +++ b/examples/inddss.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 inddss.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndDSS 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_IndDSS_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndDSS_001', 'OnClick'): on_IndDSS_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndDSS_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('IndDSS', 'IndDSS_001', 16, 16, 200, 90) + 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='IndDSS 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/inddx.py b/examples/inddx.py new file mode 100644 index 0000000..db82144 --- /dev/null +++ b/examples/inddx.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 inddx.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndDX 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_IndDX_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndDX_001', 'OnClick'): on_IndDX_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndDX_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('IndDX', 'IndDX_001', 16, 16, 200, 90) + 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='IndDX 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/inddynamicmomentumindex.py b/examples/inddynamicmomentumindex.py new file mode 100644 index 0000000..51ab8eb --- /dev/null +++ b/examples/inddynamicmomentumindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 inddynamicmomentumindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndDynamicMomentumIndex 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_IndDynamicMomentumIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndDynamicMomentumIndex_001', 'OnClick'): on_IndDynamicMomentumIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndDynamicMomentumIndex_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('IndDynamicMomentumIndex', 'IndDynamicMomentumIndex_001', 16, 16, 200, 90) + 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='IndDynamicMomentumIndex 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/indeaseofmovement.py b/examples/indeaseofmovement.py new file mode 100644 index 0000000..f1e0a90 --- /dev/null +++ b/examples/indeaseofmovement.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indeaseofmovement.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndEaseOfMovement 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_IndEaseOfMovement_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndEaseOfMovement_001', 'OnClick'): on_IndEaseOfMovement_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndEaseOfMovement_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('IndEaseOfMovement', 'IndEaseOfMovement_001', 16, 16, 200, 90) + 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='IndEaseOfMovement 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/indefficiencyratio.py b/examples/indefficiencyratio.py new file mode 100644 index 0000000..6bc2fe5 --- /dev/null +++ b/examples/indefficiencyratio.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indefficiencyratio.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndEfficiencyRatio 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_IndEfficiencyRatio_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndEfficiencyRatio_001', 'OnClick'): on_IndEfficiencyRatio_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndEfficiencyRatio_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('IndEfficiencyRatio', 'IndEfficiencyRatio_001', 16, 16, 200, 90) + 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='IndEfficiencyRatio 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/indehlersdecycler.py b/examples/indehlersdecycler.py new file mode 100644 index 0000000..8edf00b --- /dev/null +++ b/examples/indehlersdecycler.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indehlersdecycler.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndEhlersDecycler 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_IndEhlersDecycler_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndEhlersDecycler_001', 'OnClick'): on_IndEhlersDecycler_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndEhlersDecycler_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('IndEhlersDecycler', 'IndEhlersDecycler_001', 16, 16, 200, 90) + 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='IndEhlersDecycler 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/indehlerssmoothedrsi.py b/examples/indehlerssmoothedrsi.py new file mode 100644 index 0000000..f5237f1 --- /dev/null +++ b/examples/indehlerssmoothedrsi.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indehlerssmoothedrsi.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndEhlersSmoothedRSI 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_IndEhlersSmoothedRSI_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndEhlersSmoothedRSI_001', 'OnClick'): on_IndEhlersSmoothedRSI_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndEhlersSmoothedRSI_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('IndEhlersSmoothedRSI', 'IndEhlersSmoothedRSI_001', 16, 16, 200, 90) + 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='IndEhlersSmoothedRSI 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/indehlerssupersmoother.py b/examples/indehlerssupersmoother.py new file mode 100644 index 0000000..e98c810 --- /dev/null +++ b/examples/indehlerssupersmoother.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indehlerssupersmoother.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndEhlersSuperSmoother 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_IndEhlersSuperSmoother_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndEhlersSuperSmoother_001', 'OnClick'): on_IndEhlersSuperSmoother_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndEhlersSuperSmoother_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('IndEhlersSuperSmoother', 'IndEhlersSuperSmoother_001', 16, 16, 200, 90) + 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='IndEhlersSuperSmoother 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/indehlerstrendline.py b/examples/indehlerstrendline.py new file mode 100644 index 0000000..c30fd8b --- /dev/null +++ b/examples/indehlerstrendline.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indehlerstrendline.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndEhlersTrendline 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_IndEhlersTrendline_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndEhlersTrendline_001', 'OnClick'): on_IndEhlersTrendline_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndEhlersTrendline_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('IndEhlersTrendline', 'IndEhlersTrendline_001', 16, 16, 200, 90) + 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='IndEhlersTrendline 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/indelderforceindex.py b/examples/indelderforceindex.py new file mode 100644 index 0000000..89df2af --- /dev/null +++ b/examples/indelderforceindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indelderforceindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndElderForceIndex 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_IndElderForceIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndElderForceIndex_001', 'OnClick'): on_IndElderForceIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndElderForceIndex_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('IndElderForceIndex', 'IndElderForceIndex_001', 16, 16, 200, 90) + 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='IndElderForceIndex 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/indelderimpulse.py b/examples/indelderimpulse.py new file mode 100644 index 0000000..d1eff1d --- /dev/null +++ b/examples/indelderimpulse.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indelderimpulse.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndElderImpulse 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_IndElderImpulse_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndElderImpulse_001', 'OnClick'): on_IndElderImpulse_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndElderImpulse_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('IndElderImpulse', 'IndElderImpulse_001', 16, 16, 200, 90) + 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='IndElderImpulse 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/indelderraybear.py b/examples/indelderraybear.py new file mode 100644 index 0000000..18fd30f --- /dev/null +++ b/examples/indelderraybear.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indelderraybear.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndElderRayBear 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_IndElderRayBear_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndElderRayBear_001', 'OnClick'): on_IndElderRayBear_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndElderRayBear_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('IndElderRayBear', 'IndElderRayBear_001', 16, 16, 200, 90) + 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='IndElderRayBear 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/indelderraybull.py b/examples/indelderraybull.py new file mode 100644 index 0000000..e92de8d --- /dev/null +++ b/examples/indelderraybull.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indelderraybull.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndElderRayBull 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_IndElderRayBull_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndElderRayBull_001', 'OnClick'): on_IndElderRayBull_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndElderRayBull_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('IndElderRayBull', 'IndElderRayBull_001', 16, 16, 200, 90) + 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='IndElderRayBull 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/indeldersafezone.py b/examples/indeldersafezone.py new file mode 100644 index 0000000..240959a --- /dev/null +++ b/examples/indeldersafezone.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indeldersafezone.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndElderSafeZone 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_IndElderSafeZone_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndElderSafeZone_001', 'OnClick'): on_IndElderSafeZone_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndElderSafeZone_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('IndElderSafeZone', 'IndElderSafeZone_001', 16, 16, 200, 90) + 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='IndElderSafeZone 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/indema.py b/examples/indema.py new file mode 100644 index 0000000..02cf822 --- /dev/null +++ b/examples/indema.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indema.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndEMA 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_IndEMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndEMA_001', 'OnClick'): on_IndEMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndEMA_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('IndEMA', 'IndEMA_001', 16, 16, 200, 90) + 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='IndEMA 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/indergodicoscillator.py b/examples/indergodicoscillator.py new file mode 100644 index 0000000..d9e71e8 --- /dev/null +++ b/examples/indergodicoscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indergodicoscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndErgodicOscillator 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_IndErgodicOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndErgodicOscillator_001', 'OnClick'): on_IndErgodicOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndErgodicOscillator_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('IndErgodicOscillator', 'IndErgodicOscillator_001', 16, 16, 200, 90) + 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='IndErgodicOscillator 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/indevenbettersinewave.py b/examples/indevenbettersinewave.py new file mode 100644 index 0000000..a6abdd4 --- /dev/null +++ b/examples/indevenbettersinewave.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indevenbettersinewave.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndEvenBetterSineWave 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_IndEvenBetterSineWave_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndEvenBetterSineWave_001', 'OnClick'): on_IndEvenBetterSineWave_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndEvenBetterSineWave_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('IndEvenBetterSineWave', 'IndEvenBetterSineWave_001', 16, 16, 200, 90) + 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='IndEvenBetterSineWave 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/indfama.py b/examples/indfama.py new file mode 100644 index 0000000..c3d80c5 --- /dev/null +++ b/examples/indfama.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indfama.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndFAMA 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_IndFAMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndFAMA_001', 'OnClick'): on_IndFAMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndFAMA_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('IndFAMA', 'IndFAMA_001', 16, 16, 200, 90) + 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='IndFAMA 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/indfibonaccipivotpoints.py b/examples/indfibonaccipivotpoints.py new file mode 100644 index 0000000..9d580ad --- /dev/null +++ b/examples/indfibonaccipivotpoints.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indfibonaccipivotpoints.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndFibonacciPivotPoints 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_IndFibonacciPivotPoints_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndFibonacciPivotPoints_001', 'OnClick'): on_IndFibonacciPivotPoints_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndFibonacciPivotPoints_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('IndFibonacciPivotPoints', 'IndFibonacciPivotPoints_001', 16, 16, 200, 90) + 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='IndFibonacciPivotPoints 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/indfishertransform.py b/examples/indfishertransform.py new file mode 100644 index 0000000..fe4bb23 --- /dev/null +++ b/examples/indfishertransform.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indfishertransform.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndFisherTransform example' (332x252) 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_IndFisherTransform_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndFisherTransform_001', 'OnClick'): on_IndFisherTransform_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndFisherTransform_001': (True, True, False, False, 16, 16, 300, 220), +} + + +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('IndFisherTransform', 'IndFisherTransform_001', 16, 16, 300, 220) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (332, 252) +_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='IndFisherTransform example') + window.set_default_size(332, 252) + 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/indfractaldimension.py b/examples/indfractaldimension.py new file mode 100644 index 0000000..7f35d85 --- /dev/null +++ b/examples/indfractaldimension.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indfractaldimension.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndFractalDimension 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_IndFractalDimension_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndFractalDimension_001', 'OnClick'): on_IndFractalDimension_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndFractalDimension_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('IndFractalDimension', 'IndFractalDimension_001', 16, 16, 200, 90) + 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='IndFractalDimension 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/indfractaldimensionindex.py b/examples/indfractaldimensionindex.py new file mode 100644 index 0000000..275b1b7 --- /dev/null +++ b/examples/indfractaldimensionindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indfractaldimensionindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndFractalDimensionIndex 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_IndFractalDimensionIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndFractalDimensionIndex_001', 'OnClick'): on_IndFractalDimensionIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndFractalDimensionIndex_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('IndFractalDimensionIndex', 'IndFractalDimensionIndex_001', 16, 16, 200, 90) + 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='IndFractalDimensionIndex 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/indfractaldown.py b/examples/indfractaldown.py new file mode 100644 index 0000000..3afa4d1 --- /dev/null +++ b/examples/indfractaldown.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indfractaldown.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndFractalDown 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_IndFractalDown_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndFractalDown_001', 'OnClick'): on_IndFractalDown_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndFractalDown_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('IndFractalDown', 'IndFractalDown_001', 16, 16, 200, 90) + 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='IndFractalDown 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/indfractalup.py b/examples/indfractalup.py new file mode 100644 index 0000000..b791530 --- /dev/null +++ b/examples/indfractalup.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indfractalup.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndFractalUp 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_IndFractalUp_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndFractalUp_001', 'OnClick'): on_IndFractalUp_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndFractalUp_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('IndFractalUp', 'IndFractalUp_001', 16, 16, 200, 90) + 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='IndFractalUp 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/indframa.py b/examples/indframa.py new file mode 100644 index 0000000..c7ba61d --- /dev/null +++ b/examples/indframa.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indframa.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndFRAMA 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_IndFRAMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndFRAMA_001', 'OnClick'): on_IndFRAMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndFRAMA_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('IndFRAMA', 'IndFRAMA_001', 16, 16, 200, 90) + 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='IndFRAMA 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/indgannhiloactivator.py b/examples/indgannhiloactivator.py new file mode 100644 index 0000000..84c1a5f --- /dev/null +++ b/examples/indgannhiloactivator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indgannhiloactivator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndGannHiLoActivator 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_IndGannHiLoActivator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndGannHiLoActivator_001', 'OnClick'): on_IndGannHiLoActivator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndGannHiLoActivator_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('IndGannHiLoActivator', 'IndGannHiLoActivator_001', 16, 16, 200, 90) + 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='IndGannHiLoActivator 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/indgannswingoscillator.py b/examples/indgannswingoscillator.py new file mode 100644 index 0000000..c3ecbdb --- /dev/null +++ b/examples/indgannswingoscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indgannswingoscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndGannSwingOscillator 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_IndGannSwingOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndGannSwingOscillator_001', 'OnClick'): on_IndGannSwingOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndGannSwingOscillator_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('IndGannSwingOscillator', 'IndGannSwingOscillator_001', 16, 16, 200, 90) + 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='IndGannSwingOscillator 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/indgarmanklassvolatility.py b/examples/indgarmanklassvolatility.py new file mode 100644 index 0000000..afb3190 --- /dev/null +++ b/examples/indgarmanklassvolatility.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indgarmanklassvolatility.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndGarmanKlassVolatility 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_IndGarmanKlassVolatility_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndGarmanKlassVolatility_001', 'OnClick'): on_IndGarmanKlassVolatility_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndGarmanKlassVolatility_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('IndGarmanKlassVolatility', 'IndGarmanKlassVolatility_001', 16, 16, 200, 90) + 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='IndGarmanKlassVolatility 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/indgatoroscillator.py b/examples/indgatoroscillator.py new file mode 100644 index 0000000..cda4ebc --- /dev/null +++ b/examples/indgatoroscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indgatoroscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndGatorOscillator 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_IndGatorOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndGatorOscillator_001', 'OnClick'): on_IndGatorOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndGatorOscillator_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('IndGatorOscillator', 'IndGatorOscillator_001', 16, 16, 200, 90) + 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='IndGatorOscillator 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/indgaussianma.py b/examples/indgaussianma.py new file mode 100644 index 0000000..03a4f62 --- /dev/null +++ b/examples/indgaussianma.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indgaussianma.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndGaussianMA 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_IndGaussianMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndGaussianMA_001', 'OnClick'): on_IndGaussianMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndGaussianMA_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('IndGaussianMA', 'IndGaussianMA_001', 16, 16, 200, 90) + 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='IndGaussianMA 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/indgopalakrishnanrangeindex.py b/examples/indgopalakrishnanrangeindex.py new file mode 100644 index 0000000..d52cf03 --- /dev/null +++ b/examples/indgopalakrishnanrangeindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indgopalakrishnanrangeindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndGopalakrishnanRangeIndex 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_IndGopalakrishnanRangeIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndGopalakrishnanRangeIndex_001', 'OnClick'): on_IndGopalakrishnanRangeIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndGopalakrishnanRangeIndex_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('IndGopalakrishnanRangeIndex', 'IndGopalakrishnanRangeIndex_001', 16, 16, 200, 90) + 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='IndGopalakrishnanRangeIndex 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/indguppymma.py b/examples/indguppymma.py new file mode 100644 index 0000000..f1abe1a --- /dev/null +++ b/examples/indguppymma.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indguppymma.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndGuppyMMA 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_IndGuppyMMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndGuppyMMA_001', 'OnClick'): on_IndGuppyMMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndGuppyMMA_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('IndGuppyMMA', 'IndGuppyMMA_001', 16, 16, 200, 90) + 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='IndGuppyMMA 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/indhalftrend.py b/examples/indhalftrend.py new file mode 100644 index 0000000..497a0ab --- /dev/null +++ b/examples/indhalftrend.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indhalftrend.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndHalfTrend 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_IndHalfTrend_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndHalfTrend_001', 'OnClick'): on_IndHalfTrend_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndHalfTrend_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('IndHalfTrend', 'IndHalfTrend_001', 16, 16, 200, 90) + 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='IndHalfTrend 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/indhighlowrange.py b/examples/indhighlowrange.py new file mode 100644 index 0000000..d1cf944 --- /dev/null +++ b/examples/indhighlowrange.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indhighlowrange.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndHighLowRange 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_IndHighLowRange_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndHighLowRange_001', 'OnClick'): on_IndHighLowRange_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndHighLowRange_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('IndHighLowRange', 'IndHighLowRange_001', 16, 16, 200, 90) + 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='IndHighLowRange 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/indhistoricalvolatility.py b/examples/indhistoricalvolatility.py new file mode 100644 index 0000000..f40083d --- /dev/null +++ b/examples/indhistoricalvolatility.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indhistoricalvolatility.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndHistoricalVolatility 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_IndHistoricalVolatility_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndHistoricalVolatility_001', 'OnClick'): on_IndHistoricalVolatility_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndHistoricalVolatility_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('IndHistoricalVolatility', 'IndHistoricalVolatility_001', 16, 16, 200, 90) + 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='IndHistoricalVolatility 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/indhma.py b/examples/indhma.py new file mode 100644 index 0000000..2d4f0be --- /dev/null +++ b/examples/indhma.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indhma.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndHMA 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_IndHMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndHMA_001', 'OnClick'): on_IndHMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndHMA_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('IndHMA', 'IndHMA_001', 16, 16, 200, 90) + 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='IndHMA 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/indhurstexponent.py b/examples/indhurstexponent.py new file mode 100644 index 0000000..85ce48d --- /dev/null +++ b/examples/indhurstexponent.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indhurstexponent.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndHurstExponent 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_IndHurstExponent_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndHurstExponent_001', 'OnClick'): on_IndHurstExponent_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndHurstExponent_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('IndHurstExponent', 'IndHurstExponent_001', 16, 16, 200, 90) + 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='IndHurstExponent 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/indinertia.py b/examples/indinertia.py new file mode 100644 index 0000000..bf9a67a --- /dev/null +++ b/examples/indinertia.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indinertia.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndInertia 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_IndInertia_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndInertia_001', 'OnClick'): on_IndInertia_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndInertia_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('IndInertia', 'IndInertia_001', 16, 16, 200, 90) + 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='IndInertia 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/indintradayintensityindex.py b/examples/indintradayintensityindex.py new file mode 100644 index 0000000..df8055b --- /dev/null +++ b/examples/indintradayintensityindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indintradayintensityindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndIntradayIntensityIndex 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_IndIntradayIntensityIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndIntradayIntensityIndex_001', 'OnClick'): on_IndIntradayIntensityIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndIntradayIntensityIndex_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('IndIntradayIntensityIndex', 'IndIntradayIntensityIndex_001', 16, 16, 200, 90) + 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='IndIntradayIntensityIndex 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/indintradaymomentumindex.py b/examples/indintradaymomentumindex.py new file mode 100644 index 0000000..8ace1b6 --- /dev/null +++ b/examples/indintradaymomentumindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indintradaymomentumindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndIntradayMomentumIndex 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_IndIntradayMomentumIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndIntradayMomentumIndex_001', 'OnClick'): on_IndIntradayMomentumIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndIntradayMomentumIndex_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('IndIntradayMomentumIndex', 'IndIntradayMomentumIndex_001', 16, 16, 200, 90) + 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='IndIntradayMomentumIndex 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/indinversefishertransform.py b/examples/indinversefishertransform.py new file mode 100644 index 0000000..3a89e0b --- /dev/null +++ b/examples/indinversefishertransform.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indinversefishertransform.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndInverseFisherTransform example' (332x252) 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_IndInverseFisherTransform_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndInverseFisherTransform_001', 'OnClick'): on_IndInverseFisherTransform_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndInverseFisherTransform_001': (True, True, False, False, 16, 16, 300, 220), +} + + +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('IndInverseFisherTransform', 'IndInverseFisherTransform_001', 16, 16, 300, 220) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (332, 252) +_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='IndInverseFisherTransform example') + window.set_default_size(332, 252) + 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/indjurikmovingaverage.py b/examples/indjurikmovingaverage.py new file mode 100644 index 0000000..307bdc3 --- /dev/null +++ b/examples/indjurikmovingaverage.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indjurikmovingaverage.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndJurikMovingAverage 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_IndJurikMovingAverage_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndJurikMovingAverage_001', 'OnClick'): on_IndJurikMovingAverage_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndJurikMovingAverage_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('IndJurikMovingAverage', 'IndJurikMovingAverage_001', 16, 16, 200, 90) + 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='IndJurikMovingAverage 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/indjurikvelocity.py b/examples/indjurikvelocity.py new file mode 100644 index 0000000..15fbfe3 --- /dev/null +++ b/examples/indjurikvelocity.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indjurikvelocity.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndJurikVelocity 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_IndJurikVelocity_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndJurikVelocity_001', 'OnClick'): on_IndJurikVelocity_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndJurikVelocity_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('IndJurikVelocity', 'IndJurikVelocity_001', 16, 16, 200, 90) + 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='IndJurikVelocity 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/indkairi.py b/examples/indkairi.py new file mode 100644 index 0000000..acd4bb6 --- /dev/null +++ b/examples/indkairi.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indkairi.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndKairi 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_IndKairi_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndKairi_001', 'OnClick'): on_IndKairi_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndKairi_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('IndKairi', 'IndKairi_001', 16, 16, 200, 90) + 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='IndKairi 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/indkama.py b/examples/indkama.py new file mode 100644 index 0000000..17fcd8c --- /dev/null +++ b/examples/indkama.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indkama.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndKAMA 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_IndKAMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndKAMA_001', 'OnClick'): on_IndKAMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndKAMA_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('IndKAMA', 'IndKAMA_001', 16, 16, 200, 90) + 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='IndKAMA 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/indkdj.py b/examples/indkdj.py new file mode 100644 index 0000000..bc9383f --- /dev/null +++ b/examples/indkdj.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indkdj.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndKDJ 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_IndKDJ_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndKDJ_001', 'OnClick'): on_IndKDJ_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndKDJ_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('IndKDJ', 'IndKDJ_001', 16, 16, 200, 90) + 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='IndKDJ 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/indkeltnerbandwidth.py b/examples/indkeltnerbandwidth.py new file mode 100644 index 0000000..2b1baef --- /dev/null +++ b/examples/indkeltnerbandwidth.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indkeltnerbandwidth.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndKeltnerBandwidth 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_IndKeltnerBandwidth_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndKeltnerBandwidth_001', 'OnClick'): on_IndKeltnerBandwidth_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndKeltnerBandwidth_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('IndKeltnerBandwidth', 'IndKeltnerBandwidth_001', 16, 16, 200, 90) + 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='IndKeltnerBandwidth 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/indkeltnerchannels.py b/examples/indkeltnerchannels.py new file mode 100644 index 0000000..e0db4bf --- /dev/null +++ b/examples/indkeltnerchannels.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indkeltnerchannels.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndKeltnerChannels 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_IndKeltnerChannels_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndKeltnerChannels_001', 'OnClick'): on_IndKeltnerChannels_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndKeltnerChannels_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('IndKeltnerChannels', 'IndKeltnerChannels_001', 16, 16, 200, 90) + 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='IndKeltnerChannels 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/indkijunsen.py b/examples/indkijunsen.py new file mode 100644 index 0000000..44c177f --- /dev/null +++ b/examples/indkijunsen.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indkijunsen.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndKijunSen 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_IndKijunSen_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndKijunSen_001', 'OnClick'): on_IndKijunSen_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndKijunSen_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('IndKijunSen', 'IndKijunSen_001', 16, 16, 200, 90) + 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='IndKijunSen 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/indklingervolumeoscillator.py b/examples/indklingervolumeoscillator.py new file mode 100644 index 0000000..b06eba5 --- /dev/null +++ b/examples/indklingervolumeoscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indklingervolumeoscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndKlingerVolumeOscillator 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_IndKlingerVolumeOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndKlingerVolumeOscillator_001', 'OnClick'): on_IndKlingerVolumeOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndKlingerVolumeOscillator_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('IndKlingerVolumeOscillator', 'IndKlingerVolumeOscillator_001', 16, 16, 200, 90) + 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='IndKlingerVolumeOscillator 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/indkst.py b/examples/indkst.py new file mode 100644 index 0000000..ccf129d --- /dev/null +++ b/examples/indkst.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indkst.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndKST 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_IndKST_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndKST_001', 'OnClick'): on_IndKST_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndKST_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('IndKST', 'IndKST_001', 16, 16, 200, 90) + 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='IndKST 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/indkurtosis.py b/examples/indkurtosis.py new file mode 100644 index 0000000..8a65f7b --- /dev/null +++ b/examples/indkurtosis.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indkurtosis.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndKurtosis 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_IndKurtosis_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndKurtosis_001', 'OnClick'): on_IndKurtosis_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndKurtosis_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('IndKurtosis', 'IndKurtosis_001', 16, 16, 200, 90) + 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='IndKurtosis 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/indlaguerrefilter.py b/examples/indlaguerrefilter.py new file mode 100644 index 0000000..9f7e29e --- /dev/null +++ b/examples/indlaguerrefilter.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indlaguerrefilter.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndLaguerreFilter 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_IndLaguerreFilter_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndLaguerreFilter_001', 'OnClick'): on_IndLaguerreFilter_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndLaguerreFilter_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('IndLaguerreFilter', 'IndLaguerreFilter_001', 16, 16, 200, 90) + 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='IndLaguerreFilter 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/indlaguerrersi.py b/examples/indlaguerrersi.py new file mode 100644 index 0000000..506e4ff --- /dev/null +++ b/examples/indlaguerrersi.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indlaguerrersi.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndLaguerreRSI 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_IndLaguerreRSI_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndLaguerreRSI_001', 'OnClick'): on_IndLaguerreRSI_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndLaguerreRSI_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('IndLaguerreRSI', 'IndLaguerreRSI_001', 16, 16, 200, 90) + 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='IndLaguerreRSI 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/indlinearregressionforecast.py b/examples/indlinearregressionforecast.py new file mode 100644 index 0000000..4209919 --- /dev/null +++ b/examples/indlinearregressionforecast.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indlinearregressionforecast.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndLinearRegressionForecast 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_IndLinearRegressionForecast_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndLinearRegressionForecast_001', 'OnClick'): on_IndLinearRegressionForecast_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndLinearRegressionForecast_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('IndLinearRegressionForecast', 'IndLinearRegressionForecast_001', 16, 16, 200, 90) + 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='IndLinearRegressionForecast 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/indlinearregressionintercept.py b/examples/indlinearregressionintercept.py new file mode 100644 index 0000000..d0165dd --- /dev/null +++ b/examples/indlinearregressionintercept.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indlinearregressionintercept.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndLinearRegressionIntercept 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_IndLinearRegressionIntercept_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndLinearRegressionIntercept_001', 'OnClick'): on_IndLinearRegressionIntercept_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndLinearRegressionIntercept_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('IndLinearRegressionIntercept', 'IndLinearRegressionIntercept_001', 16, 16, 200, 90) + 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='IndLinearRegressionIntercept 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/indlinearregressionoscillator.py b/examples/indlinearregressionoscillator.py new file mode 100644 index 0000000..ff50b5d --- /dev/null +++ b/examples/indlinearregressionoscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indlinearregressionoscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndLinearRegressionOscillator 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_IndLinearRegressionOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndLinearRegressionOscillator_001', 'OnClick'): on_IndLinearRegressionOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndLinearRegressionOscillator_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('IndLinearRegressionOscillator', 'IndLinearRegressionOscillator_001', 16, 16, 200, 90) + 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='IndLinearRegressionOscillator 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/indlinearregressionslope.py b/examples/indlinearregressionslope.py new file mode 100644 index 0000000..1d3278b --- /dev/null +++ b/examples/indlinearregressionslope.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indlinearregressionslope.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndLinearRegressionSlope 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_IndLinearRegressionSlope_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndLinearRegressionSlope_001', 'OnClick'): on_IndLinearRegressionSlope_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndLinearRegressionSlope_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('IndLinearRegressionSlope', 'IndLinearRegressionSlope_001', 16, 16, 200, 90) + 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='IndLinearRegressionSlope 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/indlinreg.py b/examples/indlinreg.py new file mode 100644 index 0000000..102d474 --- /dev/null +++ b/examples/indlinreg.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indlinreg.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndLinReg 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_IndLinReg_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndLinReg_001', 'OnClick'): on_IndLinReg_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndLinReg_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('IndLinReg', 'IndLinReg_001', 16, 16, 200, 90) + 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='IndLinReg 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/indlinregangle.py b/examples/indlinregangle.py new file mode 100644 index 0000000..982b224 --- /dev/null +++ b/examples/indlinregangle.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indlinregangle.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndLinRegAngle 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_IndLinRegAngle_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndLinRegAngle_001', 'OnClick'): on_IndLinRegAngle_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndLinRegAngle_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('IndLinRegAngle', 'IndLinRegAngle_001', 16, 16, 200, 90) + 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='IndLinRegAngle 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/indlinregintercept.py b/examples/indlinregintercept.py new file mode 100644 index 0000000..b86ec7d --- /dev/null +++ b/examples/indlinregintercept.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indlinregintercept.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndLinRegIntercept 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_IndLinRegIntercept_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndLinRegIntercept_001', 'OnClick'): on_IndLinRegIntercept_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndLinRegIntercept_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('IndLinRegIntercept', 'IndLinRegIntercept_001', 16, 16, 200, 90) + 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='IndLinRegIntercept 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/indlinregslope.py b/examples/indlinregslope.py new file mode 100644 index 0000000..becbdb8 --- /dev/null +++ b/examples/indlinregslope.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indlinregslope.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndLinRegSlope 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_IndLinRegSlope_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndLinRegSlope_001', 'OnClick'): on_IndLinRegSlope_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndLinRegSlope_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('IndLinRegSlope', 'IndLinRegSlope_001', 16, 16, 200, 90) + 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='IndLinRegSlope 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/indlsma.py b/examples/indlsma.py new file mode 100644 index 0000000..9f92547 --- /dev/null +++ b/examples/indlsma.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indlsma.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndLSMA 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_IndLSMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndLSMA_001', 'OnClick'): on_IndLSMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndLSMA_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('IndLSMA', 'IndLSMA_001', 16, 16, 200, 90) + 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='IndLSMA 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/indmacd.py b/examples/indmacd.py new file mode 100644 index 0000000..8708777 --- /dev/null +++ b/examples/indmacd.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indmacd.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndMACD 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_IndMACD_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndMACD_001', 'OnClick'): on_IndMACD_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndMACD_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('IndMACD', 'IndMACD_001', 16, 16, 200, 90) + 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='IndMACD 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/indmacdhistogram.py b/examples/indmacdhistogram.py new file mode 100644 index 0000000..3c8fef0 --- /dev/null +++ b/examples/indmacdhistogram.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indmacdhistogram.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndMACDHistogram 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_IndMACDHistogram_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndMACDHistogram_001', 'OnClick'): on_IndMACDHistogram_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndMACDHistogram_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('IndMACDHistogram', 'IndMACDHistogram_001', 16, 16, 200, 90) + 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='IndMACDHistogram 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/indmacdofrsi.py b/examples/indmacdofrsi.py new file mode 100644 index 0000000..888915e --- /dev/null +++ b/examples/indmacdofrsi.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indmacdofrsi.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndMACDofRSI 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_IndMACDofRSI_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndMACDofRSI_001', 'OnClick'): on_IndMACDofRSI_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndMACDofRSI_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('IndMACDofRSI', 'IndMACDofRSI_001', 16, 16, 200, 90) + 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='IndMACDofRSI 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/indmaenvelopelower.py b/examples/indmaenvelopelower.py new file mode 100644 index 0000000..1cdb8cf --- /dev/null +++ b/examples/indmaenvelopelower.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indmaenvelopelower.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndMAEnvelopeLower 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_IndMAEnvelopeLower_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndMAEnvelopeLower_001', 'OnClick'): on_IndMAEnvelopeLower_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndMAEnvelopeLower_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('IndMAEnvelopeLower', 'IndMAEnvelopeLower_001', 16, 16, 200, 90) + 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='IndMAEnvelopeLower 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/indmaenvelopeupper.py b/examples/indmaenvelopeupper.py new file mode 100644 index 0000000..02cfcdf --- /dev/null +++ b/examples/indmaenvelopeupper.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indmaenvelopeupper.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndMAEnvelopeUpper 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_IndMAEnvelopeUpper_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndMAEnvelopeUpper_001', 'OnClick'): on_IndMAEnvelopeUpper_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndMAEnvelopeUpper_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('IndMAEnvelopeUpper', 'IndMAEnvelopeUpper_001', 16, 16, 200, 90) + 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='IndMAEnvelopeUpper 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/indmama.py b/examples/indmama.py new file mode 100644 index 0000000..3e278aa --- /dev/null +++ b/examples/indmama.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indmama.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndMAMA 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_IndMAMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndMAMA_001', 'OnClick'): on_IndMAMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndMAMA_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('IndMAMA', 'IndMAMA_001', 16, 16, 200, 90) + 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='IndMAMA 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/indmassindex.py b/examples/indmassindex.py new file mode 100644 index 0000000..35a2e91 --- /dev/null +++ b/examples/indmassindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indmassindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndMassIndex 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_IndMassIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndMassIndex_001', 'OnClick'): on_IndMassIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndMassIndex_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('IndMassIndex', 'IndMassIndex_001', 16, 16, 200, 90) + 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='IndMassIndex 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/indmcginley.py b/examples/indmcginley.py new file mode 100644 index 0000000..41b522b --- /dev/null +++ b/examples/indmcginley.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indmcginley.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndMcGinley 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_IndMcGinley_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndMcGinley_001', 'OnClick'): on_IndMcGinley_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndMcGinley_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('IndMcGinley', 'IndMcGinley_001', 16, 16, 200, 90) + 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='IndMcGinley 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/indmedianma.py b/examples/indmedianma.py new file mode 100644 index 0000000..a59212a --- /dev/null +++ b/examples/indmedianma.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indmedianma.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndMedianMA 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_IndMedianMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndMedianMA_001', 'OnClick'): on_IndMedianMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndMedianMA_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('IndMedianMA', 'IndMedianMA_001', 16, 16, 200, 90) + 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='IndMedianMA 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/indmedianprice.py b/examples/indmedianprice.py new file mode 100644 index 0000000..47f317f --- /dev/null +++ b/examples/indmedianprice.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indmedianprice.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndMedianPrice 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_IndMedianPrice_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndMedianPrice_001', 'OnClick'): on_IndMedianPrice_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndMedianPrice_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('IndMedianPrice', 'IndMedianPrice_001', 16, 16, 200, 90) + 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='IndMedianPrice 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/indminusdi.py b/examples/indminusdi.py new file mode 100644 index 0000000..97a951b --- /dev/null +++ b/examples/indminusdi.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indminusdi.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndMinusDI 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_IndMinusDI_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndMinusDI_001', 'OnClick'): on_IndMinusDI_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndMinusDI_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('IndMinusDI', 'IndMinusDI_001', 16, 16, 200, 90) + 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='IndMinusDI 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/indmomentum.py b/examples/indmomentum.py new file mode 100644 index 0000000..effb09e --- /dev/null +++ b/examples/indmomentum.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indmomentum.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndMomentum 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_IndMomentum_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndMomentum_001', 'OnClick'): on_IndMomentum_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndMomentum_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('IndMomentum', 'IndMomentum_001', 16, 16, 200, 90) + 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='IndMomentum 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/indmoneyflowindex.py b/examples/indmoneyflowindex.py new file mode 100644 index 0000000..e89803e --- /dev/null +++ b/examples/indmoneyflowindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indmoneyflowindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndMoneyFlowIndex 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_IndMoneyFlowIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndMoneyFlowIndex_001', 'OnClick'): on_IndMoneyFlowIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndMoneyFlowIndex_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('IndMoneyFlowIndex', 'IndMoneyFlowIndex_001', 16, 16, 200, 90) + 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='IndMoneyFlowIndex 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/indmoneyflowvolume.py b/examples/indmoneyflowvolume.py new file mode 100644 index 0000000..697f7bf --- /dev/null +++ b/examples/indmoneyflowvolume.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indmoneyflowvolume.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndMoneyFlowVolume 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_IndMoneyFlowVolume_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndMoneyFlowVolume_001', 'OnClick'): on_IndMoneyFlowVolume_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndMoneyFlowVolume_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('IndMoneyFlowVolume', 'IndMoneyFlowVolume_001', 16, 16, 200, 90) + 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='IndMoneyFlowVolume 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/indmovingaverageribbon.py b/examples/indmovingaverageribbon.py new file mode 100644 index 0000000..7e45a5b --- /dev/null +++ b/examples/indmovingaverageribbon.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indmovingaverageribbon.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndMovingAverageRibbon 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_IndMovingAverageRibbon_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndMovingAverageRibbon_001', 'OnClick'): on_IndMovingAverageRibbon_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndMovingAverageRibbon_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('IndMovingAverageRibbon', 'IndMovingAverageRibbon_001', 16, 16, 200, 90) + 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='IndMovingAverageRibbon 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/indnatr.py b/examples/indnatr.py new file mode 100644 index 0000000..fa9cf8f --- /dev/null +++ b/examples/indnatr.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indnatr.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndNATR 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_IndNATR_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndNATR_001', 'OnClick'): on_IndNATR_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndNATR_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('IndNATR', 'IndNATR_001', 16, 16, 200, 90) + 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='IndNATR 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/indnegativevolumeindex.py b/examples/indnegativevolumeindex.py new file mode 100644 index 0000000..2aded39 --- /dev/null +++ b/examples/indnegativevolumeindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indnegativevolumeindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndNegativeVolumeIndex 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_IndNegativeVolumeIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndNegativeVolumeIndex_001', 'OnClick'): on_IndNegativeVolumeIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndNegativeVolumeIndex_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('IndNegativeVolumeIndex', 'IndNegativeVolumeIndex_001', 16, 16, 200, 90) + 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='IndNegativeVolumeIndex 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/indnormalizedroc.py b/examples/indnormalizedroc.py new file mode 100644 index 0000000..1f0bf69 --- /dev/null +++ b/examples/indnormalizedroc.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indnormalizedroc.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndNormalizedROC 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_IndNormalizedROC_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndNormalizedROC_001', 'OnClick'): on_IndNormalizedROC_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndNormalizedROC_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('IndNormalizedROC', 'IndNormalizedROC_001', 16, 16, 200, 90) + 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='IndNormalizedROC 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/indobv.py b/examples/indobv.py new file mode 100644 index 0000000..0ccb2db --- /dev/null +++ b/examples/indobv.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indobv.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndOBV 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_IndOBV_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndOBV_001', 'OnClick'): on_IndOBV_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndOBV_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('IndOBV', 'IndOBV_001', 16, 16, 200, 90) + 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='IndOBV 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/indparkinsonvolatility.py b/examples/indparkinsonvolatility.py new file mode 100644 index 0000000..e268977 --- /dev/null +++ b/examples/indparkinsonvolatility.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indparkinsonvolatility.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndParkinsonVolatility 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_IndParkinsonVolatility_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndParkinsonVolatility_001', 'OnClick'): on_IndParkinsonVolatility_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndParkinsonVolatility_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('IndParkinsonVolatility', 'IndParkinsonVolatility_001', 16, 16, 200, 90) + 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='IndParkinsonVolatility 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/indpercentagevolumeoscillator.py b/examples/indpercentagevolumeoscillator.py new file mode 100644 index 0000000..c0b3109 --- /dev/null +++ b/examples/indpercentagevolumeoscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indpercentagevolumeoscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndPercentageVolumeOscillator 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_IndPercentageVolumeOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndPercentageVolumeOscillator_001', 'OnClick'): on_IndPercentageVolumeOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndPercentageVolumeOscillator_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('IndPercentageVolumeOscillator', 'IndPercentageVolumeOscillator_001', 16, 16, 200, 90) + 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='IndPercentageVolumeOscillator 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/indpercentilerank.py b/examples/indpercentilerank.py new file mode 100644 index 0000000..2d0edf2 --- /dev/null +++ b/examples/indpercentilerank.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indpercentilerank.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndPercentileRank 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_IndPercentileRank_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndPercentileRank_001', 'OnClick'): on_IndPercentileRank_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndPercentileRank_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('IndPercentileRank', 'IndPercentileRank_001', 16, 16, 200, 90) + 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='IndPercentileRank 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/indpercentrank.py b/examples/indpercentrank.py new file mode 100644 index 0000000..11a2af5 --- /dev/null +++ b/examples/indpercentrank.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indpercentrank.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndPercentRank 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_IndPercentRank_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndPercentRank_001', 'OnClick'): on_IndPercentRank_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndPercentRank_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('IndPercentRank', 'IndPercentRank_001', 16, 16, 200, 90) + 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='IndPercentRank 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/indpivotpoint.py b/examples/indpivotpoint.py new file mode 100644 index 0000000..027b8a4 --- /dev/null +++ b/examples/indpivotpoint.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indpivotpoint.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndPivotPoint 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_IndPivotPoint_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndPivotPoint_001', 'OnClick'): on_IndPivotPoint_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndPivotPoint_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('IndPivotPoint', 'IndPivotPoint_001', 16, 16, 200, 90) + 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='IndPivotPoint 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/indplusdi.py b/examples/indplusdi.py new file mode 100644 index 0000000..224b0dd --- /dev/null +++ b/examples/indplusdi.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indplusdi.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndPlusDI 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_IndPlusDI_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndPlusDI_001', 'OnClick'): on_IndPlusDI_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndPlusDI_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('IndPlusDI', 'IndPlusDI_001', 16, 16, 200, 90) + 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='IndPlusDI 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/indpositivevolumeindex.py b/examples/indpositivevolumeindex.py new file mode 100644 index 0000000..0a803b1 --- /dev/null +++ b/examples/indpositivevolumeindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indpositivevolumeindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndPositiveVolumeIndex 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_IndPositiveVolumeIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndPositiveVolumeIndex_001', 'OnClick'): on_IndPositiveVolumeIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndPositiveVolumeIndex_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('IndPositiveVolumeIndex', 'IndPositiveVolumeIndex_001', 16, 16, 200, 90) + 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='IndPositiveVolumeIndex 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/indppo.py b/examples/indppo.py new file mode 100644 index 0000000..19f0dbd --- /dev/null +++ b/examples/indppo.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indppo.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndPPO 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_IndPPO_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndPPO_001', 'OnClick'): on_IndPPO_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndPPO_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('IndPPO', 'IndPPO_001', 16, 16, 200, 90) + 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='IndPPO 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/indprettygoodoscillator.py b/examples/indprettygoodoscillator.py new file mode 100644 index 0000000..b6e59a6 --- /dev/null +++ b/examples/indprettygoodoscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indprettygoodoscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndPrettyGoodOscillator 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_IndPrettyGoodOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndPrettyGoodOscillator_001', 'OnClick'): on_IndPrettyGoodOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndPrettyGoodOscillator_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('IndPrettyGoodOscillator', 'IndPrettyGoodOscillator_001', 16, 16, 200, 90) + 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='IndPrettyGoodOscillator 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/indpricemomentumoscillator.py b/examples/indpricemomentumoscillator.py new file mode 100644 index 0000000..5245781 --- /dev/null +++ b/examples/indpricemomentumoscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indpricemomentumoscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndPriceMomentumOscillator 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_IndPriceMomentumOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndPriceMomentumOscillator_001', 'OnClick'): on_IndPriceMomentumOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndPriceMomentumOscillator_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('IndPriceMomentumOscillator', 'IndPriceMomentumOscillator_001', 16, 16, 200, 90) + 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='IndPriceMomentumOscillator 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/indpsar.py b/examples/indpsar.py new file mode 100644 index 0000000..e7cce22 --- /dev/null +++ b/examples/indpsar.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indpsar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndPSAR 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_IndPSAR_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndPSAR_001', 'OnClick'): on_IndPSAR_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndPSAR_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('IndPSAR', 'IndPSAR_001', 16, 16, 200, 90) + 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='IndPSAR 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/indqstick.py b/examples/indqstick.py new file mode 100644 index 0000000..41d96ff --- /dev/null +++ b/examples/indqstick.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indqstick.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndQStick 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_IndQStick_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndQStick_001', 'OnClick'): on_IndQStick_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndQStick_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('IndQStick', 'IndQStick_001', 16, 16, 200, 90) + 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='IndQStick 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/indrainbowoscillator.py b/examples/indrainbowoscillator.py new file mode 100644 index 0000000..415e48c --- /dev/null +++ b/examples/indrainbowoscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indrainbowoscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndRainbowOscillator 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_IndRainbowOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndRainbowOscillator_001', 'OnClick'): on_IndRainbowOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndRainbowOscillator_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('IndRainbowOscillator', 'IndRainbowOscillator_001', 16, 16, 200, 90) + 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='IndRainbowOscillator 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/indrangeexpansionindex.py b/examples/indrangeexpansionindex.py new file mode 100644 index 0000000..f60076f --- /dev/null +++ b/examples/indrangeexpansionindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indrangeexpansionindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndRangeExpansionIndex 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_IndRangeExpansionIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndRangeExpansionIndex_001', 'OnClick'): on_IndRangeExpansionIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndRangeExpansionIndex_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('IndRangeExpansionIndex', 'IndRangeExpansionIndex_001', 16, 16, 200, 90) + 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='IndRangeExpansionIndex 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/indreflex.py b/examples/indreflex.py new file mode 100644 index 0000000..9a5e2e5 --- /dev/null +++ b/examples/indreflex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indreflex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndReflex 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_IndReflex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndReflex_001', 'OnClick'): on_IndReflex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndReflex_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('IndReflex', 'IndReflex_001', 16, 16, 200, 90) + 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='IndReflex 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/indregressionchannel.py b/examples/indregressionchannel.py new file mode 100644 index 0000000..7241af2 --- /dev/null +++ b/examples/indregressionchannel.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indregressionchannel.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndRegressionChannel 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_IndRegressionChannel_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndRegressionChannel_001', 'OnClick'): on_IndRegressionChannel_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndRegressionChannel_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('IndRegressionChannel', 'IndRegressionChannel_001', 16, 16, 200, 90) + 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='IndRegressionChannel 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/indrelativemomentumindex.py b/examples/indrelativemomentumindex.py new file mode 100644 index 0000000..6bdbca3 --- /dev/null +++ b/examples/indrelativemomentumindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indrelativemomentumindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndRelativeMomentumIndex 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_IndRelativeMomentumIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndRelativeMomentumIndex_001', 'OnClick'): on_IndRelativeMomentumIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndRelativeMomentumIndex_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('IndRelativeMomentumIndex', 'IndRelativeMomentumIndex_001', 16, 16, 200, 90) + 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='IndRelativeMomentumIndex 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/indrelativevigorindex.py b/examples/indrelativevigorindex.py new file mode 100644 index 0000000..fb310e3 --- /dev/null +++ b/examples/indrelativevigorindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indrelativevigorindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndRelativeVigorIndex 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_IndRelativeVigorIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndRelativeVigorIndex_001', 'OnClick'): on_IndRelativeVigorIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndRelativeVigorIndex_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('IndRelativeVigorIndex', 'IndRelativeVigorIndex_001', 16, 16, 200, 90) + 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='IndRelativeVigorIndex 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/indrelativevolatilityindex.py b/examples/indrelativevolatilityindex.py new file mode 100644 index 0000000..d1b6ca8 --- /dev/null +++ b/examples/indrelativevolatilityindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indrelativevolatilityindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndRelativeVolatilityIndex 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_IndRelativeVolatilityIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndRelativeVolatilityIndex_001', 'OnClick'): on_IndRelativeVolatilityIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndRelativeVolatilityIndex_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('IndRelativeVolatilityIndex', 'IndRelativeVolatilityIndex_001', 16, 16, 200, 90) + 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='IndRelativeVolatilityIndex 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/indrelativevolume.py b/examples/indrelativevolume.py new file mode 100644 index 0000000..769fb72 --- /dev/null +++ b/examples/indrelativevolume.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indrelativevolume.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndRelativeVolume 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_IndRelativeVolume_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndRelativeVolume_001', 'OnClick'): on_IndRelativeVolume_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndRelativeVolume_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('IndRelativeVolume', 'IndRelativeVolume_001', 16, 16, 200, 90) + 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='IndRelativeVolume 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/indrma.py b/examples/indrma.py new file mode 100644 index 0000000..dd0803d --- /dev/null +++ b/examples/indrma.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indrma.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndRMA 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_IndRMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndRMA_001', 'OnClick'): on_IndRMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndRMA_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('IndRMA', 'IndRMA_001', 16, 16, 200, 90) + 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='IndRMA 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/indroc.py b/examples/indroc.py new file mode 100644 index 0000000..7082c5f --- /dev/null +++ b/examples/indroc.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indroc.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndROC 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_IndROC_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndROC_001', 'OnClick'): on_IndROC_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndROC_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('IndROC', 'IndROC_001', 16, 16, 200, 90) + 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='IndROC 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/indrocr.py b/examples/indrocr.py new file mode 100644 index 0000000..8c832c2 --- /dev/null +++ b/examples/indrocr.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indrocr.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndROCR 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_IndROCR_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndROCR_001', 'OnClick'): on_IndROCR_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndROCR_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('IndROCR', 'IndROCR_001', 16, 16, 200, 90) + 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='IndROCR 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/indrogerssatchellvolatility.py b/examples/indrogerssatchellvolatility.py new file mode 100644 index 0000000..4b7795f --- /dev/null +++ b/examples/indrogerssatchellvolatility.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indrogerssatchellvolatility.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndRogersSatchellVolatility 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_IndRogersSatchellVolatility_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndRogersSatchellVolatility_001', 'OnClick'): on_IndRogersSatchellVolatility_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndRogersSatchellVolatility_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('IndRogersSatchellVolatility', 'IndRogersSatchellVolatility_001', 16, 16, 200, 90) + 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='IndRogersSatchellVolatility 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/indroofingfilter.py b/examples/indroofingfilter.py new file mode 100644 index 0000000..9b810ff --- /dev/null +++ b/examples/indroofingfilter.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indroofingfilter.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndRoofingFilter 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_IndRoofingFilter_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndRoofingFilter_001', 'OnClick'): on_IndRoofingFilter_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndRoofingFilter_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('IndRoofingFilter', 'IndRoofingFilter_001', 16, 16, 200, 90) + 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='IndRoofingFilter 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/indrsi.py b/examples/indrsi.py new file mode 100644 index 0000000..31406b0 --- /dev/null +++ b/examples/indrsi.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indrsi.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndRSI 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_IndRSI_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndRSI_001', 'OnClick'): on_IndRSI_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndRSI_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('IndRSI', 'IndRSI_001', 16, 16, 200, 90) + 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='IndRSI 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/indrsquared.py b/examples/indrsquared.py new file mode 100644 index 0000000..94c85bc --- /dev/null +++ b/examples/indrsquared.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indrsquared.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndRSquared 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_IndRSquared_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndRSquared_001', 'OnClick'): on_IndRSquared_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndRSquared_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('IndRSquared', 'IndRSquared_001', 16, 16, 200, 90) + 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='IndRSquared 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/indrsx.py b/examples/indrsx.py new file mode 100644 index 0000000..26c590e --- /dev/null +++ b/examples/indrsx.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indrsx.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndRSX 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_IndRSX_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndRSX_001', 'OnClick'): on_IndRSX_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndRSX_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('IndRSX', 'IndRSX_001', 16, 16, 200, 90) + 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='IndRSX 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/indrwihigh.py b/examples/indrwihigh.py new file mode 100644 index 0000000..dae820a --- /dev/null +++ b/examples/indrwihigh.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indrwihigh.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndRWIHigh 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_IndRWIHigh_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndRWIHigh_001', 'OnClick'): on_IndRWIHigh_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndRWIHigh_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('IndRWIHigh', 'IndRWIHigh_001', 16, 16, 200, 90) + 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='IndRWIHigh 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/indrwilow.py b/examples/indrwilow.py new file mode 100644 index 0000000..35b118e --- /dev/null +++ b/examples/indrwilow.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indrwilow.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndRWILow 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_IndRWILow_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndRWILow_001', 'OnClick'): on_IndRWILow_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndRWILow_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('IndRWILow', 'IndRWILow_001', 16, 16, 200, 90) + 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='IndRWILow 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/indschafftrendcycle.py b/examples/indschafftrendcycle.py new file mode 100644 index 0000000..9777b42 --- /dev/null +++ b/examples/indschafftrendcycle.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indschafftrendcycle.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndSchaffTrendCycle 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_IndSchaffTrendCycle_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndSchaffTrendCycle_001', 'OnClick'): on_IndSchaffTrendCycle_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndSchaffTrendCycle_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('IndSchaffTrendCycle', 'IndSchaffTrendCycle_001', 16, 16, 200, 90) + 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='IndSchaffTrendCycle 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/indsenkouspana.py b/examples/indsenkouspana.py new file mode 100644 index 0000000..87dc785 --- /dev/null +++ b/examples/indsenkouspana.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indsenkouspana.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndSenkouSpanA 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_IndSenkouSpanA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndSenkouSpanA_001', 'OnClick'): on_IndSenkouSpanA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndSenkouSpanA_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('IndSenkouSpanA', 'IndSenkouSpanA_001', 16, 16, 200, 90) + 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='IndSenkouSpanA 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/indsenkouspanb.py b/examples/indsenkouspanb.py new file mode 100644 index 0000000..f9559e1 --- /dev/null +++ b/examples/indsenkouspanb.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indsenkouspanb.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndSenkouSpanB 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_IndSenkouSpanB_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndSenkouSpanB_001', 'OnClick'): on_IndSenkouSpanB_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndSenkouSpanB_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('IndSenkouSpanB', 'IndSenkouSpanB_001', 16, 16, 200, 90) + 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='IndSenkouSpanB 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/indshannonentropy.py b/examples/indshannonentropy.py new file mode 100644 index 0000000..fc169a0 --- /dev/null +++ b/examples/indshannonentropy.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indshannonentropy.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndShannonEntropy 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_IndShannonEntropy_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndShannonEntropy_001', 'OnClick'): on_IndShannonEntropy_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndShannonEntropy_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('IndShannonEntropy', 'IndShannonEntropy_001', 16, 16, 200, 90) + 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='IndShannonEntropy 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/indsinewave.py b/examples/indsinewave.py new file mode 100644 index 0000000..162113a --- /dev/null +++ b/examples/indsinewave.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indsinewave.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndSineWave 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_IndSineWave_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndSineWave_001', 'OnClick'): on_IndSineWave_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndSineWave_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('IndSineWave', 'IndSineWave_001', 16, 16, 200, 90) + 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='IndSineWave 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/indsinewma.py b/examples/indsinewma.py new file mode 100644 index 0000000..31d39cb --- /dev/null +++ b/examples/indsinewma.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indsinewma.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndSineWMA 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_IndSineWMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndSineWMA_001', 'OnClick'): on_IndSineWMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndSineWMA_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('IndSineWMA', 'IndSineWMA_001', 16, 16, 200, 90) + 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='IndSineWMA 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/indskewness.py b/examples/indskewness.py new file mode 100644 index 0000000..f8712e9 --- /dev/null +++ b/examples/indskewness.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indskewness.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndSkewness 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_IndSkewness_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndSkewness_001', 'OnClick'): on_IndSkewness_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndSkewness_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('IndSkewness', 'IndSkewness_001', 16, 16, 200, 90) + 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='IndSkewness 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/indsma.py b/examples/indsma.py new file mode 100644 index 0000000..dfbdf5b --- /dev/null +++ b/examples/indsma.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indsma.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndSMA 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_IndSMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndSMA_001', 'OnClick'): on_IndSMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndSMA_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('IndSMA', 'IndSMA_001', 16, 16, 200, 90) + 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='IndSMA 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/indsmi.py b/examples/indsmi.py new file mode 100644 index 0000000..d6d5823 --- /dev/null +++ b/examples/indsmi.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indsmi.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndSMI 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_IndSMI_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndSMI_001', 'OnClick'): on_IndSMI_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndSMI_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('IndSMI', 'IndSMI_001', 16, 16, 200, 90) + 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='IndSMI 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/indsmma.py b/examples/indsmma.py new file mode 100644 index 0000000..657f199 --- /dev/null +++ b/examples/indsmma.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indsmma.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndSMMA 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_IndSMMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndSMMA_001', 'OnClick'): on_IndSMMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndSMMA_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('IndSMMA', 'IndSMMA_001', 16, 16, 200, 90) + 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='IndSMMA 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/indsmoothedroc.py b/examples/indsmoothedroc.py new file mode 100644 index 0000000..45f472a --- /dev/null +++ b/examples/indsmoothedroc.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indsmoothedroc.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndSmoothedROC 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_IndSmoothedROC_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndSmoothedROC_001', 'OnClick'): on_IndSmoothedROC_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndSmoothedROC_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('IndSmoothedROC', 'IndSmoothedROC_001', 16, 16, 200, 90) + 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='IndSmoothedROC 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/indspearmanrank.py b/examples/indspearmanrank.py new file mode 100644 index 0000000..dca8bad --- /dev/null +++ b/examples/indspearmanrank.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indspearmanrank.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndSpearmanRank 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_IndSpearmanRank_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndSpearmanRank_001', 'OnClick'): on_IndSpearmanRank_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndSpearmanRank_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('IndSpearmanRank', 'IndSpearmanRank_001', 16, 16, 200, 90) + 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='IndSpearmanRank 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/indspecialk.py b/examples/indspecialk.py new file mode 100644 index 0000000..5971b74 --- /dev/null +++ b/examples/indspecialk.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indspecialk.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndSpecialK 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_IndSpecialK_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndSpecialK_001', 'OnClick'): on_IndSpecialK_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndSpecialK_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('IndSpecialK', 'IndSpecialK_001', 16, 16, 200, 90) + 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='IndSpecialK 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/indsslchannel.py b/examples/indsslchannel.py new file mode 100644 index 0000000..b712fd1 --- /dev/null +++ b/examples/indsslchannel.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indsslchannel.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndSSLChannel 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_IndSSLChannel_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndSSLChannel_001', 'OnClick'): on_IndSSLChannel_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndSSLChannel_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('IndSSLChannel', 'IndSSLChannel_001', 16, 16, 200, 90) + 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='IndSSLChannel 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/indstandarderror.py b/examples/indstandarderror.py new file mode 100644 index 0000000..afe0f23 --- /dev/null +++ b/examples/indstandarderror.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indstandarderror.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndStandardError 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_IndStandardError_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndStandardError_001', 'OnClick'): on_IndStandardError_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndStandardError_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('IndStandardError', 'IndStandardError_001', 16, 16, 200, 90) + 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='IndStandardError 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/indstandardpivotpoints.py b/examples/indstandardpivotpoints.py new file mode 100644 index 0000000..6d4732f --- /dev/null +++ b/examples/indstandardpivotpoints.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indstandardpivotpoints.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndStandardPivotPoints 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_IndStandardPivotPoints_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndStandardPivotPoints_001', 'OnClick'): on_IndStandardPivotPoints_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndStandardPivotPoints_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('IndStandardPivotPoints', 'IndStandardPivotPoints_001', 16, 16, 200, 90) + 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='IndStandardPivotPoints 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/indstddev.py b/examples/indstddev.py new file mode 100644 index 0000000..885e5e4 --- /dev/null +++ b/examples/indstddev.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indstddev.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndStdDev 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_IndStdDev_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndStdDev_001', 'OnClick'): on_IndStdDev_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndStdDev_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('IndStdDev', 'IndStdDev_001', 16, 16, 200, 90) + 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='IndStdDev 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/indstochasticfast.py b/examples/indstochasticfast.py new file mode 100644 index 0000000..f8a7e39 --- /dev/null +++ b/examples/indstochasticfast.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indstochasticfast.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndStochasticFast 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_IndStochasticFast_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndStochasticFast_001', 'OnClick'): on_IndStochasticFast_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndStochasticFast_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('IndStochasticFast', 'IndStochasticFast_001', 16, 16, 200, 90) + 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='IndStochasticFast 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/indstochasticfull.py b/examples/indstochasticfull.py new file mode 100644 index 0000000..42e7d2b --- /dev/null +++ b/examples/indstochasticfull.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indstochasticfull.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndStochasticFull 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_IndStochasticFull_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndStochasticFull_001', 'OnClick'): on_IndStochasticFull_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndStochasticFull_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('IndStochasticFull', 'IndStochasticFull_001', 16, 16, 200, 90) + 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='IndStochasticFull 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/indstochasticpop.py b/examples/indstochasticpop.py new file mode 100644 index 0000000..0f3e6c9 --- /dev/null +++ b/examples/indstochasticpop.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indstochasticpop.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndStochasticPop 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_IndStochasticPop_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndStochasticPop_001', 'OnClick'): on_IndStochasticPop_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndStochasticPop_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('IndStochasticPop', 'IndStochasticPop_001', 16, 16, 200, 90) + 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='IndStochasticPop 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/indstochasticslow.py b/examples/indstochasticslow.py new file mode 100644 index 0000000..b108402 --- /dev/null +++ b/examples/indstochasticslow.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indstochasticslow.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndStochasticSlow 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_IndStochasticSlow_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndStochasticSlow_001', 'OnClick'): on_IndStochasticSlow_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndStochasticSlow_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('IndStochasticSlow', 'IndStochasticSlow_001', 16, 16, 200, 90) + 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='IndStochasticSlow 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/indstochrsi.py b/examples/indstochrsi.py new file mode 100644 index 0000000..a157022 --- /dev/null +++ b/examples/indstochrsi.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indstochrsi.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndStochRSI 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_IndStochRSI_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndStochRSI_001', 'OnClick'): on_IndStochRSI_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndStochRSI_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('IndStochRSI', 'IndStochRSI_001', 16, 16, 200, 90) + 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='IndStochRSI 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/indsupersmoother.py b/examples/indsupersmoother.py new file mode 100644 index 0000000..f47a18c --- /dev/null +++ b/examples/indsupersmoother.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indsupersmoother.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndSuperSmoother 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_IndSuperSmoother_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndSuperSmoother_001', 'OnClick'): on_IndSuperSmoother_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndSuperSmoother_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('IndSuperSmoother', 'IndSuperSmoother_001', 16, 16, 200, 90) + 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='IndSuperSmoother 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/indsupertrend.py b/examples/indsupertrend.py new file mode 100644 index 0000000..70260ec --- /dev/null +++ b/examples/indsupertrend.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indsupertrend.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndSupertrend 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_IndSupertrend_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndSupertrend_001', 'OnClick'): on_IndSupertrend_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndSupertrend_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('IndSupertrend', 'IndSupertrend_001', 16, 16, 200, 90) + 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='IndSupertrend 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/indswenlintradingoscillator.py b/examples/indswenlintradingoscillator.py new file mode 100644 index 0000000..c4d858a --- /dev/null +++ b/examples/indswenlintradingoscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indswenlintradingoscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndSwenlinTradingOscillator 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_IndSwenlinTradingOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndSwenlinTradingOscillator_001', 'OnClick'): on_IndSwenlinTradingOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndSwenlinTradingOscillator_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('IndSwenlinTradingOscillator', 'IndSwenlinTradingOscillator_001', 16, 16, 200, 90) + 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='IndSwenlinTradingOscillator 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/indswingindex.py b/examples/indswingindex.py new file mode 100644 index 0000000..735da44 --- /dev/null +++ b/examples/indswingindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indswingindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndSwingIndex 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_IndSwingIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndSwingIndex_001', 'OnClick'): on_IndSwingIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndSwingIndex_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('IndSwingIndex', 'IndSwingIndex_001', 16, 16, 200, 90) + 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='IndSwingIndex 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/indt3.py b/examples/indt3.py new file mode 100644 index 0000000..64e689b --- /dev/null +++ b/examples/indt3.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indt3.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndT3 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_IndT3_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndT3_001', 'OnClick'): on_IndT3_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndT3_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('IndT3', 'IndT3_001', 16, 16, 200, 90) + 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='IndT3 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/indtdsequential.py b/examples/indtdsequential.py new file mode 100644 index 0000000..f5e5270 --- /dev/null +++ b/examples/indtdsequential.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indtdsequential.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndTDSequential 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_IndTDSequential_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndTDSequential_001', 'OnClick'): on_IndTDSequential_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndTDSequential_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('IndTDSequential', 'IndTDSequential_001', 16, 16, 200, 90) + 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='IndTDSequential 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/indtema.py b/examples/indtema.py new file mode 100644 index 0000000..d0f2c4f --- /dev/null +++ b/examples/indtema.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indtema.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndTEMA 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_IndTEMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndTEMA_001', 'OnClick'): on_IndTEMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndTEMA_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('IndTEMA', 'IndTEMA_001', 16, 16, 200, 90) + 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='IndTEMA 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/indtenkansen.py b/examples/indtenkansen.py new file mode 100644 index 0000000..4794fbf --- /dev/null +++ b/examples/indtenkansen.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indtenkansen.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndTenkanSen 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_IndTenkanSen_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndTenkanSen_001', 'OnClick'): on_IndTenkanSen_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndTenkanSen_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('IndTenkanSen', 'IndTenkanSen_001', 16, 16, 200, 90) + 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='IndTenkanSen 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/indtrendflex.py b/examples/indtrendflex.py new file mode 100644 index 0000000..0de6291 --- /dev/null +++ b/examples/indtrendflex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indtrendflex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndTrendFlex 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_IndTrendFlex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndTrendFlex_001', 'OnClick'): on_IndTrendFlex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndTrendFlex_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('IndTrendFlex', 'IndTrendFlex_001', 16, 16, 200, 90) + 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='IndTrendFlex 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/indtrendintensityindex.py b/examples/indtrendintensityindex.py new file mode 100644 index 0000000..98dbc94 --- /dev/null +++ b/examples/indtrendintensityindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indtrendintensityindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndTrendIntensityIndex 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_IndTrendIntensityIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndTrendIntensityIndex_001', 'OnClick'): on_IndTrendIntensityIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndTrendIntensityIndex_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('IndTrendIntensityIndex', 'IndTrendIntensityIndex_001', 16, 16, 200, 90) + 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='IndTrendIntensityIndex 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/indtrima.py b/examples/indtrima.py new file mode 100644 index 0000000..ee27365 --- /dev/null +++ b/examples/indtrima.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indtrima.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndTRIMA 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_IndTRIMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndTRIMA_001', 'OnClick'): on_IndTRIMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndTRIMA_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('IndTRIMA', 'IndTRIMA_001', 16, 16, 200, 90) + 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='IndTRIMA 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/indtrix.py b/examples/indtrix.py new file mode 100644 index 0000000..3c111a6 --- /dev/null +++ b/examples/indtrix.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indtrix.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndTRIX 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_IndTRIX_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndTRIX_001', 'OnClick'): on_IndTRIX_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndTRIX_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('IndTRIX', 'IndTRIX_001', 16, 16, 200, 90) + 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='IndTRIX 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/indtruerange.py b/examples/indtruerange.py new file mode 100644 index 0000000..944250e --- /dev/null +++ b/examples/indtruerange.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indtruerange.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndTrueRange 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_IndTrueRange_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndTrueRange_001', 'OnClick'): on_IndTrueRange_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndTrueRange_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('IndTrueRange', 'IndTrueRange_001', 16, 16, 200, 90) + 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='IndTrueRange 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/indtsf.py b/examples/indtsf.py new file mode 100644 index 0000000..8698beb --- /dev/null +++ b/examples/indtsf.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indtsf.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndTSF 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_IndTSF_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndTSF_001', 'OnClick'): on_IndTSF_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndTSF_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('IndTSF', 'IndTSF_001', 16, 16, 200, 90) + 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='IndTSF 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/indtsi.py b/examples/indtsi.py new file mode 100644 index 0000000..7dc700d --- /dev/null +++ b/examples/indtsi.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indtsi.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndTSI 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_IndTSI_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndTSI_001', 'OnClick'): on_IndTSI_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndTSI_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('IndTSI', 'IndTSI_001', 16, 16, 200, 90) + 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='IndTSI 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/indtsma.py b/examples/indtsma.py new file mode 100644 index 0000000..a90d4d0 --- /dev/null +++ b/examples/indtsma.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indtsma.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndTSMA 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_IndTSMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndTSMA_001', 'OnClick'): on_IndTSMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndTSMA_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('IndTSMA', 'IndTSMA_001', 16, 16, 200, 90) + 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='IndTSMA 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/indttmsqueeze.py b/examples/indttmsqueeze.py new file mode 100644 index 0000000..b83271a --- /dev/null +++ b/examples/indttmsqueeze.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indttmsqueeze.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndTTMSqueeze 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_IndTTMSqueeze_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndTTMSqueeze_001', 'OnClick'): on_IndTTMSqueeze_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndTTMSqueeze_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('IndTTMSqueeze', 'IndTTMSqueeze_001', 16, 16, 200, 90) + 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='IndTTMSqueeze 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/indtwiggsmoneyflow.py b/examples/indtwiggsmoneyflow.py new file mode 100644 index 0000000..2463def --- /dev/null +++ b/examples/indtwiggsmoneyflow.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indtwiggsmoneyflow.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndTwiggsMoneyFlow 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_IndTwiggsMoneyFlow_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndTwiggsMoneyFlow_001', 'OnClick'): on_IndTwiggsMoneyFlow_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndTwiggsMoneyFlow_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('IndTwiggsMoneyFlow', 'IndTwiggsMoneyFlow_001', 16, 16, 200, 90) + 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='IndTwiggsMoneyFlow 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/indtypicalprice.py b/examples/indtypicalprice.py new file mode 100644 index 0000000..faab369 --- /dev/null +++ b/examples/indtypicalprice.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indtypicalprice.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndTypicalPrice 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_IndTypicalPrice_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndTypicalPrice_001', 'OnClick'): on_IndTypicalPrice_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndTypicalPrice_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('IndTypicalPrice', 'IndTypicalPrice_001', 16, 16, 200, 90) + 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='IndTypicalPrice 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/indulcerindex.py b/examples/indulcerindex.py new file mode 100644 index 0000000..16689c2 --- /dev/null +++ b/examples/indulcerindex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indulcerindex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndUlcerIndex 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_IndUlcerIndex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndUlcerIndex_001', 'OnClick'): on_IndUlcerIndex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndUlcerIndex_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('IndUlcerIndex', 'IndUlcerIndex_001', 16, 16, 200, 90) + 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='IndUlcerIndex 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/indultimateoscillator.py b/examples/indultimateoscillator.py new file mode 100644 index 0000000..be92af7 --- /dev/null +++ b/examples/indultimateoscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indultimateoscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndUltimateOscillator 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_IndUltimateOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndUltimateOscillator_001', 'OnClick'): on_IndUltimateOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndUltimateOscillator_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('IndUltimateOscillator', 'IndUltimateOscillator_001', 16, 16, 200, 90) + 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='IndUltimateOscillator 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/indupdownratio.py b/examples/indupdownratio.py new file mode 100644 index 0000000..0ea65ac --- /dev/null +++ b/examples/indupdownratio.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indupdownratio.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndUpDownRatio 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_IndUpDownRatio_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndUpDownRatio_001', 'OnClick'): on_IndUpDownRatio_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndUpDownRatio_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('IndUpDownRatio', 'IndUpDownRatio_001', 16, 16, 150, 36) + 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='IndUpDownRatio 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/indupdownvolumeratio.py b/examples/indupdownvolumeratio.py new file mode 100644 index 0000000..abc1e10 --- /dev/null +++ b/examples/indupdownvolumeratio.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indupdownvolumeratio.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndUpDownVolumeRatio 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_IndUpDownVolumeRatio_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndUpDownVolumeRatio_001', 'OnClick'): on_IndUpDownVolumeRatio_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndUpDownVolumeRatio_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('IndUpDownVolumeRatio', 'IndUpDownVolumeRatio_001', 16, 16, 150, 36) + 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='IndUpDownVolumeRatio 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/indutbot.py b/examples/indutbot.py new file mode 100644 index 0000000..6193940 --- /dev/null +++ b/examples/indutbot.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indutbot.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndUTBot 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_IndUTBot_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndUTBot_001', 'OnClick'): on_IndUTBot_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndUTBot_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('IndUTBot', 'IndUTBot_001', 16, 16, 200, 90) + 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='IndUTBot 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/indvariance.py b/examples/indvariance.py new file mode 100644 index 0000000..aa8805f --- /dev/null +++ b/examples/indvariance.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indvariance.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndVariance 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_IndVariance_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndVariance_001', 'OnClick'): on_IndVariance_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndVariance_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('IndVariance', 'IndVariance_001', 16, 16, 200, 90) + 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='IndVariance 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/indvhf.py b/examples/indvhf.py new file mode 100644 index 0000000..acfaaf2 --- /dev/null +++ b/examples/indvhf.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indvhf.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndVHF 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_IndVHF_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndVHF_001', 'OnClick'): on_IndVHF_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndVHF_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('IndVHF', 'IndVHF_001', 16, 16, 200, 90) + 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='IndVHF 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/indvidya.py b/examples/indvidya.py new file mode 100644 index 0000000..f66bab8 --- /dev/null +++ b/examples/indvidya.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indvidya.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndVIDYA 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_IndVIDYA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndVIDYA_001', 'OnClick'): on_IndVIDYA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndVIDYA_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('IndVIDYA', 'IndVIDYA_001', 16, 16, 200, 90) + 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='IndVIDYA 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/indvolatilityratio.py b/examples/indvolatilityratio.py new file mode 100644 index 0000000..a00e1f8 --- /dev/null +++ b/examples/indvolatilityratio.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indvolatilityratio.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndVolatilityRatio 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_IndVolatilityRatio_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndVolatilityRatio_001', 'OnClick'): on_IndVolatilityRatio_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndVolatilityRatio_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('IndVolatilityRatio', 'IndVolatilityRatio_001', 16, 16, 200, 90) + 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='IndVolatilityRatio 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/indvolatilitystop.py b/examples/indvolatilitystop.py new file mode 100644 index 0000000..ac93734 --- /dev/null +++ b/examples/indvolatilitystop.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indvolatilitystop.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndVolatilityStop 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_IndVolatilityStop_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndVolatilityStop_001', 'OnClick'): on_IndVolatilityStop_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndVolatilityStop_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('IndVolatilityStop', 'IndVolatilityStop_001', 16, 16, 200, 90) + 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='IndVolatilityStop 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/indvolumeaverage.py b/examples/indvolumeaverage.py new file mode 100644 index 0000000..b634444 --- /dev/null +++ b/examples/indvolumeaverage.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indvolumeaverage.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndVolumeAverage 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_IndVolumeAverage_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndVolumeAverage_001', 'OnClick'): on_IndVolumeAverage_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndVolumeAverage_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('IndVolumeAverage', 'IndVolumeAverage_001', 16, 16, 200, 90) + 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='IndVolumeAverage 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/indvolumedelta.py b/examples/indvolumedelta.py new file mode 100644 index 0000000..9173b40 --- /dev/null +++ b/examples/indvolumedelta.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indvolumedelta.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndVolumeDelta 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_IndVolumeDelta_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndVolumeDelta_001', 'OnClick'): on_IndVolumeDelta_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndVolumeDelta_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('IndVolumeDelta', 'IndVolumeDelta_001', 16, 16, 200, 90) + 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='IndVolumeDelta 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/indvolumeoscillator.py b/examples/indvolumeoscillator.py new file mode 100644 index 0000000..cd63a1a --- /dev/null +++ b/examples/indvolumeoscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indvolumeoscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndVolumeOscillator 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_IndVolumeOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndVolumeOscillator_001', 'OnClick'): on_IndVolumeOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndVolumeOscillator_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('IndVolumeOscillator', 'IndVolumeOscillator_001', 16, 16, 200, 90) + 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='IndVolumeOscillator 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/indvolumepricetrend.py b/examples/indvolumepricetrend.py new file mode 100644 index 0000000..d6b72f9 --- /dev/null +++ b/examples/indvolumepricetrend.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indvolumepricetrend.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndVolumePriceTrend 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_IndVolumePriceTrend_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndVolumePriceTrend_001', 'OnClick'): on_IndVolumePriceTrend_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndVolumePriceTrend_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('IndVolumePriceTrend', 'IndVolumePriceTrend_001', 16, 16, 200, 90) + 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='IndVolumePriceTrend 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/indvolumerateofchange.py b/examples/indvolumerateofchange.py new file mode 100644 index 0000000..85f8fd9 --- /dev/null +++ b/examples/indvolumerateofchange.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indvolumerateofchange.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndVolumeRateOfChange 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_IndVolumeRateOfChange_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndVolumeRateOfChange_001', 'OnClick'): on_IndVolumeRateOfChange_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndVolumeRateOfChange_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('IndVolumeRateOfChange', 'IndVolumeRateOfChange_001', 16, 16, 200, 90) + 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='IndVolumeRateOfChange 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/indvolumeweightedmovingaverage.py b/examples/indvolumeweightedmovingaverage.py new file mode 100644 index 0000000..a9d19c1 --- /dev/null +++ b/examples/indvolumeweightedmovingaverage.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indvolumeweightedmovingaverage.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndVolumeWeightedMovingAverage 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_IndVolumeWeightedMovingAverage_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndVolumeWeightedMovingAverage_001', 'OnClick'): on_IndVolumeWeightedMovingAverage_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndVolumeWeightedMovingAverage_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('IndVolumeWeightedMovingAverage', 'IndVolumeWeightedMovingAverage_001', 16, 16, 200, 90) + 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='IndVolumeWeightedMovingAverage 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/indvolumezoneoscillator.py b/examples/indvolumezoneoscillator.py new file mode 100644 index 0000000..e648a86 --- /dev/null +++ b/examples/indvolumezoneoscillator.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indvolumezoneoscillator.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndVolumeZoneOscillator 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_IndVolumeZoneOscillator_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndVolumeZoneOscillator_001', 'OnClick'): on_IndVolumeZoneOscillator_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndVolumeZoneOscillator_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('IndVolumeZoneOscillator', 'IndVolumeZoneOscillator_001', 16, 16, 200, 90) + 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='IndVolumeZoneOscillator 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/indvortex.py b/examples/indvortex.py new file mode 100644 index 0000000..8e274db --- /dev/null +++ b/examples/indvortex.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indvortex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndVortex 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_IndVortex_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndVortex_001', 'OnClick'): on_IndVortex_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndVortex_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('IndVortex', 'IndVortex_001', 16, 16, 200, 90) + 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='IndVortex 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/indvwap.py b/examples/indvwap.py new file mode 100644 index 0000000..7f7e09e --- /dev/null +++ b/examples/indvwap.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indvwap.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndVWAP 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_IndVWAP_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndVWAP_001', 'OnClick'): on_IndVWAP_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndVWAP_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('IndVWAP', 'IndVWAP_001', 16, 16, 200, 90) + 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='IndVWAP 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/indvwapbands.py b/examples/indvwapbands.py new file mode 100644 index 0000000..c52fea1 --- /dev/null +++ b/examples/indvwapbands.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indvwapbands.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndVWAPBands 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_IndVWAPBands_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndVWAPBands_001', 'OnClick'): on_IndVWAPBands_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndVWAPBands_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('IndVWAPBands', 'IndVWAPBands_001', 16, 16, 200, 90) + 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='IndVWAPBands 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/indvwema.py b/examples/indvwema.py new file mode 100644 index 0000000..94ddf3c --- /dev/null +++ b/examples/indvwema.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indvwema.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndVWEMA 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_IndVWEMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndVWEMA_001', 'OnClick'): on_IndVWEMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndVWEMA_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('IndVWEMA', 'IndVWEMA_001', 16, 16, 200, 90) + 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='IndVWEMA 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/indvwma.py b/examples/indvwma.py new file mode 100644 index 0000000..38c95ae --- /dev/null +++ b/examples/indvwma.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indvwma.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndVWMA 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_IndVWMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndVWMA_001', 'OnClick'): on_IndVWMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndVWMA_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('IndVWMA', 'IndVWMA_001', 16, 16, 200, 90) + 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='IndVWMA 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/indwavetrend.py b/examples/indwavetrend.py new file mode 100644 index 0000000..1ea3a89 --- /dev/null +++ b/examples/indwavetrend.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indwavetrend.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndWaveTrend 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_IndWaveTrend_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndWaveTrend_001', 'OnClick'): on_IndWaveTrend_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndWaveTrend_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('IndWaveTrend', 'IndWaveTrend_001', 16, 16, 200, 90) + 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='IndWaveTrend 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/indweightedclose.py b/examples/indweightedclose.py new file mode 100644 index 0000000..b3d1292 --- /dev/null +++ b/examples/indweightedclose.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indweightedclose.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndWeightedClose 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_IndWeightedClose_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndWeightedClose_001', 'OnClick'): on_IndWeightedClose_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndWeightedClose_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('IndWeightedClose', 'IndWeightedClose_001', 16, 16, 200, 90) + 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='IndWeightedClose 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/indwildersma.py b/examples/indwildersma.py new file mode 100644 index 0000000..a59ab42 --- /dev/null +++ b/examples/indwildersma.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indwildersma.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndWildersMA 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_IndWildersMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndWildersMA_001', 'OnClick'): on_IndWildersMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndWildersMA_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('IndWildersMA', 'IndWildersMA_001', 16, 16, 200, 90) + 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='IndWildersMA 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/indwildervolatilitysystem.py b/examples/indwildervolatilitysystem.py new file mode 100644 index 0000000..57a5412 --- /dev/null +++ b/examples/indwildervolatilitysystem.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indwildervolatilitysystem.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndWilderVolatilitySystem 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_IndWilderVolatilitySystem_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndWilderVolatilitySystem_001', 'OnClick'): on_IndWilderVolatilitySystem_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndWilderVolatilitySystem_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('IndWilderVolatilitySystem', 'IndWilderVolatilitySystem_001', 16, 16, 200, 90) + 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='IndWilderVolatilitySystem 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/indwilliamsaccumulationdistribution.py b/examples/indwilliamsaccumulationdistribution.py new file mode 100644 index 0000000..98f69eb --- /dev/null +++ b/examples/indwilliamsaccumulationdistribution.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indwilliamsaccumulationdistribution.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndWilliamsAccumulationDistribution 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_IndWilliamsAccumulationDistribution_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndWilliamsAccumulationDistribution_001', 'OnClick'): on_IndWilliamsAccumulationDistribution_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndWilliamsAccumulationDistribution_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('IndWilliamsAccumulationDistribution', 'IndWilliamsAccumulationDistribution_001', 16, 16, 200, 90) + 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='IndWilliamsAccumulationDistribution 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/indwilliamsr.py b/examples/indwilliamsr.py new file mode 100644 index 0000000..6527bb9 --- /dev/null +++ b/examples/indwilliamsr.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indwilliamsr.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndWilliamsR 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_IndWilliamsR_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndWilliamsR_001', 'OnClick'): on_IndWilliamsR_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndWilliamsR_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('IndWilliamsR', 'IndWilliamsR_001', 16, 16, 200, 90) + 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='IndWilliamsR 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/indwma.py b/examples/indwma.py new file mode 100644 index 0000000..7f4b5a9 --- /dev/null +++ b/examples/indwma.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indwma.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndWMA 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_IndWMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndWMA_001', 'OnClick'): on_IndWMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndWMA_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('IndWMA', 'IndWMA_001', 16, 16, 200, 90) + 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='IndWMA 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/indwoodiepivotpoints.py b/examples/indwoodiepivotpoints.py new file mode 100644 index 0000000..d18eee4 --- /dev/null +++ b/examples/indwoodiepivotpoints.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indwoodiepivotpoints.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndWoodiePivotPoints 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_IndWoodiePivotPoints_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndWoodiePivotPoints_001', 'OnClick'): on_IndWoodiePivotPoints_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndWoodiePivotPoints_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('IndWoodiePivotPoints', 'IndWoodiePivotPoints_001', 16, 16, 200, 90) + 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='IndWoodiePivotPoints 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/indyangzhangvolatility.py b/examples/indyangzhangvolatility.py new file mode 100644 index 0000000..32b0bea --- /dev/null +++ b/examples/indyangzhangvolatility.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indyangzhangvolatility.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndYangZhangVolatility 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_IndYangZhangVolatility_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndYangZhangVolatility_001', 'OnClick'): on_IndYangZhangVolatility_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndYangZhangVolatility_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('IndYangZhangVolatility', 'IndYangZhangVolatility_001', 16, 16, 200, 90) + 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='IndYangZhangVolatility 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/indzerolagmacd.py b/examples/indzerolagmacd.py new file mode 100644 index 0000000..94b7ec9 --- /dev/null +++ b/examples/indzerolagmacd.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indzerolagmacd.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndZeroLagMACD 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_IndZeroLagMACD_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndZeroLagMACD_001', 'OnClick'): on_IndZeroLagMACD_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndZeroLagMACD_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('IndZeroLagMACD', 'IndZeroLagMACD_001', 16, 16, 200, 90) + 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='IndZeroLagMACD 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/indzigzag.py b/examples/indzigzag.py new file mode 100644 index 0000000..e5a63bc --- /dev/null +++ b/examples/indzigzag.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indzigzag.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndZigZag 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_IndZigZag_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndZigZag_001', 'OnClick'): on_IndZigZag_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndZigZag_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('IndZigZag', 'IndZigZag_001', 16, 16, 200, 90) + 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='IndZigZag 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/indzlema.py b/examples/indzlema.py new file mode 100644 index 0000000..c467ad6 --- /dev/null +++ b/examples/indzlema.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indzlema.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndZLEMA 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_IndZLEMA_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndZLEMA_001', 'OnClick'): on_IndZLEMA_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndZLEMA_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('IndZLEMA', 'IndZLEMA_001', 16, 16, 200, 90) + 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='IndZLEMA 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/indzscore.py b/examples/indzscore.py new file mode 100644 index 0000000..d408235 --- /dev/null +++ b/examples/indzscore.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 indzscore.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'IndZScore 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_IndZScore_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('IndZScore_001', 'OnClick'): on_IndZScore_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'IndZScore_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('IndZScore', 'IndZScore_001', 16, 16, 200, 90) + 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='IndZScore 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/instanaloggauge.py b/examples/instanaloggauge.py index 2910320..b44c563 100644 --- a/examples/instanaloggauge.py +++ b/examples/instanaloggauge.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # Generated by LightGUI (Python/XFrame). # A plain Python/GTK program: no CMake. Run with: -# python3 Example.py +# python3 instanaloggauge.py # with the LightGUI Python package on PYTHONPATH # (the designer sets it when it runs this script). # diff --git a/examples/instbarmeter.py b/examples/instbarmeter.py new file mode 100644 index 0000000..8893f1e --- /dev/null +++ b/examples/instbarmeter.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instbarmeter.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstBarMeter 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_InstBarMeter_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstBarMeter_001', 'OnClick'): on_InstBarMeter_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstBarMeter_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('InstBarMeter', 'InstBarMeter_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstBarMeter_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='InstBarMeter 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/instbeacon.py b/examples/instbeacon.py new file mode 100644 index 0000000..41e2326 --- /dev/null +++ b/examples/instbeacon.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instbeacon.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstBeacon 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_InstBeacon_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstBeacon_001', 'OnClick'): on_InstBeacon_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstBeacon_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('InstBeacon', 'InstBeacon_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstBeacon_001') + 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='InstBeacon 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/instbezel.py b/examples/instbezel.py new file mode 100644 index 0000000..35f0396 --- /dev/null +++ b/examples/instbezel.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instbezel.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstBezel 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_InstBezel_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstBezel_001', 'OnClick'): on_InstBezel_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstBezel_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('InstBezel', 'InstBezel_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstBezel_001') + 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='InstBezel 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/instcircuitbreaker.py b/examples/instcircuitbreaker.py new file mode 100644 index 0000000..e03cd0c --- /dev/null +++ b/examples/instcircuitbreaker.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instcircuitbreaker.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstCircuitBreaker 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_InstCircuitBreaker_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstCircuitBreaker_001', 'OnClick'): on_InstCircuitBreaker_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstCircuitBreaker_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('InstCircuitBreaker', 'InstCircuitBreaker_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstCircuitBreaker_001') + 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='InstCircuitBreaker 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/instcontrolpanel.py b/examples/instcontrolpanel.py new file mode 100644 index 0000000..c9d1e0d --- /dev/null +++ b/examples/instcontrolpanel.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instcontrolpanel.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstControlPanel 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_InstControlPanel_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstControlPanel_001', 'OnClick'): on_InstControlPanel_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstControlPanel_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('InstControlPanel', 'InstControlPanel_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstControlPanel_001') + 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='InstControlPanel 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/instdigitalreadout.py b/examples/instdigitalreadout.py new file mode 100644 index 0000000..5e9ea39 --- /dev/null +++ b/examples/instdigitalreadout.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instdigitalreadout.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstDigitalReadout 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_InstDigitalReadout_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstDigitalReadout_001', 'OnClick'): on_InstDigitalReadout_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstDigitalReadout_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('InstDigitalReadout', 'InstDigitalReadout_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstDigitalReadout_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='InstDigitalReadout 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/instemergencystop.py b/examples/instemergencystop.py new file mode 100644 index 0000000..2a405eb --- /dev/null +++ b/examples/instemergencystop.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instemergencystop.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstEmergencyStop 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_InstEmergencyStop_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstEmergencyStop_001', 'OnClick'): on_InstEmergencyStop_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstEmergencyStop_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('InstEmergencyStop', 'InstEmergencyStop_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstEmergencyStop_001') + 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='InstEmergencyStop 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/instflowmeter.py b/examples/instflowmeter.py new file mode 100644 index 0000000..e91b392 --- /dev/null +++ b/examples/instflowmeter.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instflowmeter.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstFlowMeter 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_InstFlowMeter_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstFlowMeter_001', 'OnClick'): on_InstFlowMeter_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstFlowMeter_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('InstFlowMeter', 'InstFlowMeter_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstFlowMeter_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='InstFlowMeter 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/insthorizontaltanklevel.py b/examples/insthorizontaltanklevel.py new file mode 100644 index 0000000..7982bf5 --- /dev/null +++ b/examples/insthorizontaltanklevel.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 insthorizontaltanklevel.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstHorizontalTankLevel 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_InstHorizontalTankLevel_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstHorizontalTankLevel_001', 'OnClick'): on_InstHorizontalTankLevel_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstHorizontalTankLevel_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('InstHorizontalTankLevel', 'InstHorizontalTankLevel_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstHorizontalTankLevel_001') + 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='InstHorizontalTankLevel 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/instkeyswitch.py b/examples/instkeyswitch.py new file mode 100644 index 0000000..dff5bf1 --- /dev/null +++ b/examples/instkeyswitch.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instkeyswitch.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstKeySwitch 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_InstKeySwitch_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstKeySwitch_001', 'OnClick'): on_InstKeySwitch_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstKeySwitch_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('InstKeySwitch', 'InstKeySwitch_001', 16, 16, 150, 36) + widget.set_prop('text', 'InstKeySwitch_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='InstKeySwitch 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/instlamptestbutton.py b/examples/instlamptestbutton.py new file mode 100644 index 0000000..6f89de7 --- /dev/null +++ b/examples/instlamptestbutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instlamptestbutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstLampTestButton 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_InstLampTestButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstLampTestButton_001', 'OnClick'): on_InstLampTestButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstLampTestButton_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('InstLampTestButton', 'InstLampTestButton_001', 16, 16, 150, 36) + widget.set_prop('text', 'InstLampTestButton_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='InstLampTestButton 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/instledblue.py b/examples/instledblue.py new file mode 100644 index 0000000..f6a56e8 --- /dev/null +++ b/examples/instledblue.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instledblue.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstLedBlue 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_InstLedBlue_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstLedBlue_001', 'OnClick'): on_InstLedBlue_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstLedBlue_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('InstLedBlue', 'InstLedBlue_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstLedBlue_001') + 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='InstLedBlue 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/instleddigits1.py b/examples/instleddigits1.py new file mode 100644 index 0000000..df53467 --- /dev/null +++ b/examples/instleddigits1.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instleddigits1.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstLedDigits1 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_InstLedDigits1_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstLedDigits1_001', 'OnClick'): on_InstLedDigits1_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstLedDigits1_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('InstLedDigits1', 'InstLedDigits1_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstLedDigits1_001') + widget.set_prop('value', '5.9') + 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='InstLedDigits1 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/instleddigits2.py b/examples/instleddigits2.py new file mode 100644 index 0000000..29fcb3a --- /dev/null +++ b/examples/instleddigits2.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instleddigits2.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstLedDigits2 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_InstLedDigits2_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstLedDigits2_001', 'OnClick'): on_InstLedDigits2_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstLedDigits2_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('InstLedDigits2', 'InstLedDigits2_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstLedDigits2_001') + widget.set_prop('value', '6.4') + 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='InstLedDigits2 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/instleddigits3.py b/examples/instleddigits3.py new file mode 100644 index 0000000..009bf72 --- /dev/null +++ b/examples/instleddigits3.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instleddigits3.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstLedDigits3 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_InstLedDigits3_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstLedDigits3_001', 'OnClick'): on_InstLedDigits3_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstLedDigits3_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('InstLedDigits3', 'InstLedDigits3_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstLedDigits3_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='InstLedDigits3 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/instleddigits4.py b/examples/instleddigits4.py new file mode 100644 index 0000000..a7d2b6e --- /dev/null +++ b/examples/instleddigits4.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instleddigits4.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstLedDigits4 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_InstLedDigits4_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstLedDigits4_001', 'OnClick'): on_InstLedDigits4_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstLedDigits4_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('InstLedDigits4', 'InstLedDigits4_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstLedDigits4_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='InstLedDigits4 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/instleddigits5.py b/examples/instleddigits5.py new file mode 100644 index 0000000..29f4ca8 --- /dev/null +++ b/examples/instleddigits5.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instleddigits5.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstLedDigits5 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_InstLedDigits5_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstLedDigits5_001', 'OnClick'): on_InstLedDigits5_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstLedDigits5_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('InstLedDigits5', 'InstLedDigits5_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstLedDigits5_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='InstLedDigits5 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/instleddigits6.py b/examples/instleddigits6.py new file mode 100644 index 0000000..e262016 --- /dev/null +++ b/examples/instleddigits6.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instleddigits6.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstLedDigits6 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_InstLedDigits6_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstLedDigits6_001', 'OnClick'): on_InstLedDigits6_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstLedDigits6_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('InstLedDigits6', 'InstLedDigits6_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstLedDigits6_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='InstLedDigits6 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/instledgreen.py b/examples/instledgreen.py new file mode 100644 index 0000000..20ace02 --- /dev/null +++ b/examples/instledgreen.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instledgreen.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstLedGreen 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_InstLedGreen_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstLedGreen_001', 'OnClick'): on_InstLedGreen_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstLedGreen_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('InstLedGreen', 'InstLedGreen_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstLedGreen_001') + 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='InstLedGreen 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/instledred.py b/examples/instledred.py new file mode 100644 index 0000000..35d9f43 --- /dev/null +++ b/examples/instledred.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instledred.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstLedRed 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_InstLedRed_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstLedRed_001', 'OnClick'): on_InstLedRed_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstLedRed_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('InstLedRed', 'InstLedRed_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstLedRed_001') + 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='InstLedRed 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/instlevelcolumngauge.py b/examples/instlevelcolumngauge.py new file mode 100644 index 0000000..9f51e0d --- /dev/null +++ b/examples/instlevelcolumngauge.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instlevelcolumngauge.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstLevelColumnGauge 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_InstLevelColumnGauge_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstLevelColumnGauge_001', 'OnClick'): on_InstLevelColumnGauge_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstLevelColumnGauge_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('InstLevelColumnGauge', 'InstLevelColumnGauge_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstLevelColumnGauge_001') + 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='InstLevelColumnGauge 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/instleveltransmitter.py b/examples/instleveltransmitter.py new file mode 100644 index 0000000..646cd88 --- /dev/null +++ b/examples/instleveltransmitter.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instleveltransmitter.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstLevelTransmitter 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_InstLevelTransmitter_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstLevelTransmitter_001', 'OnClick'): on_InstLevelTransmitter_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstLevelTransmitter_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('InstLevelTransmitter', 'InstLevelTransmitter_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstLevelTransmitter_001') + 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='InstLevelTransmitter 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/instmanometer.py b/examples/instmanometer.py new file mode 100644 index 0000000..5784ccb --- /dev/null +++ b/examples/instmanometer.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instmanometer.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstManometer 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_InstManometer_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstManometer_001', 'OnClick'): on_InstManometer_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstManometer_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('InstManometer', 'InstManometer_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstManometer_001') + widget.set_prop('value', '15') + 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='InstManometer 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/instnameplate.py b/examples/instnameplate.py new file mode 100644 index 0000000..d804381 --- /dev/null +++ b/examples/instnameplate.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instnameplate.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstNameplate 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_InstNameplate_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstNameplate_001', 'OnClick'): on_InstNameplate_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstNameplate_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('InstNameplate', 'InstNameplate_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstNameplate_001') + 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='InstNameplate 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/instneedleedgewise.py b/examples/instneedleedgewise.py new file mode 100644 index 0000000..c3388f8 --- /dev/null +++ b/examples/instneedleedgewise.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instneedleedgewise.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstNeedleEdgewise 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_InstNeedleEdgewise_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstNeedleEdgewise_001', 'OnClick'): on_InstNeedleEdgewise_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstNeedleEdgewise_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('InstNeedleEdgewise', 'InstNeedleEdgewise_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstNeedleEdgewise_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='InstNeedleEdgewise 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/instneedleknifeedge.py b/examples/instneedleknifeedge.py new file mode 100644 index 0000000..f390dd2 --- /dev/null +++ b/examples/instneedleknifeedge.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instneedleknifeedge.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstNeedleKnifeEdge 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_InstNeedleKnifeEdge_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstNeedleKnifeEdge_001', 'OnClick'): on_InstNeedleKnifeEdge_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstNeedleKnifeEdge_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('InstNeedleKnifeEdge', 'InstNeedleKnifeEdge_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstNeedleKnifeEdge_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='InstNeedleKnifeEdge 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/instneedletapered.py b/examples/instneedletapered.py new file mode 100644 index 0000000..a743b60 --- /dev/null +++ b/examples/instneedletapered.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instneedletapered.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstNeedleTapered 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_InstNeedleTapered_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstNeedleTapered_001', 'OnClick'): on_InstNeedleTapered_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstNeedleTapered_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('InstNeedleTapered', 'InstNeedleTapered_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstNeedleTapered_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='InstNeedleTapered 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/instneedlevu.py b/examples/instneedlevu.py new file mode 100644 index 0000000..2eb8c53 --- /dev/null +++ b/examples/instneedlevu.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instneedlevu.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstNeedleVu 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_InstNeedleVu_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstNeedleVu_001', 'OnClick'): on_InstNeedleVu_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstNeedleVu_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('InstNeedleVu', 'InstNeedleVu_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstNeedleVu_001') + widget.set_prop('value', '-5') + 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='InstNeedleVu 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/instopenchannellevel.py b/examples/instopenchannellevel.py new file mode 100644 index 0000000..3ba04e0 --- /dev/null +++ b/examples/instopenchannellevel.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instopenchannellevel.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstOpenChannelLevel 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_InstOpenChannelLevel_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstOpenChannelLevel_001', 'OnClick'): on_InstOpenChannelLevel_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstOpenChannelLevel_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('InstOpenChannelLevel', 'InstOpenChannelLevel_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstOpenChannelLevel_001') + 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='InstOpenChannelLevel 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/instpanellampamber.py b/examples/instpanellampamber.py new file mode 100644 index 0000000..7e5bc1b --- /dev/null +++ b/examples/instpanellampamber.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instpanellampamber.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstPanelLampAmber 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_InstPanelLampAmber_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstPanelLampAmber_001', 'OnClick'): on_InstPanelLampAmber_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstPanelLampAmber_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('InstPanelLampAmber', 'InstPanelLampAmber_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstPanelLampAmber_001') + 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='InstPanelLampAmber 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/instpanellampblue.py b/examples/instpanellampblue.py new file mode 100644 index 0000000..6bdbb55 --- /dev/null +++ b/examples/instpanellampblue.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instpanellampblue.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstPanelLampBlue 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_InstPanelLampBlue_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstPanelLampBlue_001', 'OnClick'): on_InstPanelLampBlue_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstPanelLampBlue_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('InstPanelLampBlue', 'InstPanelLampBlue_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstPanelLampBlue_001') + 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='InstPanelLampBlue 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/instpanellampgreen.py b/examples/instpanellampgreen.py new file mode 100644 index 0000000..1ae2627 --- /dev/null +++ b/examples/instpanellampgreen.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instpanellampgreen.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstPanelLampGreen 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_InstPanelLampGreen_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstPanelLampGreen_001', 'OnClick'): on_InstPanelLampGreen_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstPanelLampGreen_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('InstPanelLampGreen', 'InstPanelLampGreen_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstPanelLampGreen_001') + 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='InstPanelLampGreen 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/instpanellampred.py b/examples/instpanellampred.py new file mode 100644 index 0000000..6f5a994 --- /dev/null +++ b/examples/instpanellampred.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instpanellampred.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstPanelLampRed 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_InstPanelLampRed_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstPanelLampRed_001', 'OnClick'): on_InstPanelLampRed_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstPanelLampRed_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('InstPanelLampRed', 'InstPanelLampRed_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstPanelLampRed_001') + 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='InstPanelLampRed 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/instpanellampwhite.py b/examples/instpanellampwhite.py new file mode 100644 index 0000000..b17cf60 --- /dev/null +++ b/examples/instpanellampwhite.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instpanellampwhite.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstPanelLampWhite 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_InstPanelLampWhite_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstPanelLampWhite_001', 'OnClick'): on_InstPanelLampWhite_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstPanelLampWhite_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('InstPanelLampWhite', 'InstPanelLampWhite_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstPanelLampWhite_001') + 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='InstPanelLampWhite 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/instpanelplate.py b/examples/instpanelplate.py new file mode 100644 index 0000000..12c7540 --- /dev/null +++ b/examples/instpanelplate.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instpanelplate.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstPanelPlate 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_InstPanelPlate_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstPanelPlate_001', 'OnClick'): on_InstPanelPlate_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstPanelPlate_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('InstPanelPlate', 'InstPanelPlate_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstPanelPlate_001') + 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='InstPanelPlate 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/instpressuregauge.py b/examples/instpressuregauge.py new file mode 100644 index 0000000..183ddcc --- /dev/null +++ b/examples/instpressuregauge.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instpressuregauge.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstPressureGauge 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_InstPressureGauge_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstPressureGauge_001', 'OnClick'): on_InstPressureGauge_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstPressureGauge_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('InstPressureGauge', 'InstPressureGauge_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstPressureGauge_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='InstPressureGauge 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/instpushbutton.py b/examples/instpushbutton.py new file mode 100644 index 0000000..1a08e96 --- /dev/null +++ b/examples/instpushbutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instpushbutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstPushButton 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_InstPushButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstPushButton_001', 'OnClick'): on_InstPushButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstPushButton_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('InstPushButton', 'InstPushButton_001', 16, 16, 150, 36) + widget.set_prop('text', 'InstPushButton_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='InstPushButton 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/instpushbuttonlatching.py b/examples/instpushbuttonlatching.py new file mode 100644 index 0000000..9861f47 --- /dev/null +++ b/examples/instpushbuttonlatching.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instpushbuttonlatching.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstPushButtonLatching 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_InstPushButtonLatching_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstPushButtonLatching_001', 'OnClick'): on_InstPushButtonLatching_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstPushButtonLatching_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('InstPushButtonLatching', 'InstPushButtonLatching_001', 16, 16, 150, 36) + widget.set_prop('text', 'InstPushButtonLatching_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='InstPushButtonLatching 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/instrgbstatusled.py b/examples/instrgbstatusled.py new file mode 100644 index 0000000..d172fb9 --- /dev/null +++ b/examples/instrgbstatusled.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instrgbstatusled.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstRgbStatusLed 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_InstRgbStatusLed_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstRgbStatusLed_001', 'OnClick'): on_InstRgbStatusLed_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstRgbStatusLed_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('InstRgbStatusLed', 'InstRgbStatusLed_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstRgbStatusLed_001') + 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='InstRgbStatusLed 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/instrockerswitch.py b/examples/instrockerswitch.py new file mode 100644 index 0000000..fed774a --- /dev/null +++ b/examples/instrockerswitch.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instrockerswitch.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstRockerSwitch 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_InstRockerSwitch_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstRockerSwitch_001', 'OnClick'): on_InstRockerSwitch_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstRockerSwitch_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('InstRockerSwitch', 'InstRockerSwitch_001', 16, 16, 150, 36) + widget.set_prop('text', 'InstRockerSwitch_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='InstRockerSwitch 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/instrockerswitchdouble.py b/examples/instrockerswitchdouble.py new file mode 100644 index 0000000..0d5f629 --- /dev/null +++ b/examples/instrockerswitchdouble.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instrockerswitchdouble.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstRockerSwitchDouble 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_InstRockerSwitchDouble_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstRockerSwitchDouble_001', 'OnClick'): on_InstRockerSwitchDouble_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstRockerSwitchDouble_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('InstRockerSwitchDouble', 'InstRockerSwitchDouble_001', 16, 16, 150, 36) + widget.set_prop('text', 'InstRockerSwitchDouble_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='InstRockerSwitchDouble 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/instrockerswitchilluminated.py b/examples/instrockerswitchilluminated.py new file mode 100644 index 0000000..49db1c4 --- /dev/null +++ b/examples/instrockerswitchilluminated.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instrockerswitchilluminated.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstRockerSwitchIlluminated 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_InstRockerSwitchIlluminated_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstRockerSwitchIlluminated_001', 'OnClick'): on_InstRockerSwitchIlluminated_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstRockerSwitchIlluminated_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('InstRockerSwitchIlluminated', 'InstRockerSwitchIlluminated_001', 16, 16, 150, 36) + widget.set_prop('text', 'InstRockerSwitchIlluminated_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='InstRockerSwitchIlluminated 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/instrotaryselector.py b/examples/instrotaryselector.py new file mode 100644 index 0000000..81fa24d --- /dev/null +++ b/examples/instrotaryselector.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instrotaryselector.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstRotarySelector 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_InstRotarySelector_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstRotarySelector_001', 'OnClick'): on_InstRotarySelector_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstRotarySelector_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('InstRotarySelector', 'InstRotarySelector_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstRotarySelector_001') + 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='InstRotarySelector 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/instseg14display1.py b/examples/instseg14display1.py new file mode 100644 index 0000000..6b66ee2 --- /dev/null +++ b/examples/instseg14display1.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instseg14display1.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstSeg14Display1 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_InstSeg14Display1_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstSeg14Display1_001', 'OnClick'): on_InstSeg14Display1_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstSeg14Display1_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('InstSeg14Display1', 'InstSeg14Display1_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstSeg14Display1_001') + 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='InstSeg14Display1 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/instseg14display2.py b/examples/instseg14display2.py new file mode 100644 index 0000000..79735ff --- /dev/null +++ b/examples/instseg14display2.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instseg14display2.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstSeg14Display2 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_InstSeg14Display2_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstSeg14Display2_001', 'OnClick'): on_InstSeg14Display2_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstSeg14Display2_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('InstSeg14Display2', 'InstSeg14Display2_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstSeg14Display2_001') + 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='InstSeg14Display2 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/instseg14display3.py b/examples/instseg14display3.py new file mode 100644 index 0000000..cc16c1a --- /dev/null +++ b/examples/instseg14display3.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instseg14display3.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstSeg14Display3 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_InstSeg14Display3_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstSeg14Display3_001', 'OnClick'): on_InstSeg14Display3_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstSeg14Display3_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('InstSeg14Display3', 'InstSeg14Display3_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstSeg14Display3_001') + 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='InstSeg14Display3 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/instseg14display4.py b/examples/instseg14display4.py new file mode 100644 index 0000000..c514521 --- /dev/null +++ b/examples/instseg14display4.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instseg14display4.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstSeg14Display4 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_InstSeg14Display4_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstSeg14Display4_001', 'OnClick'): on_InstSeg14Display4_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstSeg14Display4_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('InstSeg14Display4', 'InstSeg14Display4_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstSeg14Display4_001') + 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='InstSeg14Display4 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/instseg14display5.py b/examples/instseg14display5.py new file mode 100644 index 0000000..c92bab6 --- /dev/null +++ b/examples/instseg14display5.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instseg14display5.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstSeg14Display5 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_InstSeg14Display5_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstSeg14Display5_001', 'OnClick'): on_InstSeg14Display5_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstSeg14Display5_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('InstSeg14Display5', 'InstSeg14Display5_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstSeg14Display5_001') + 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='InstSeg14Display5 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/instseg14display6.py b/examples/instseg14display6.py new file mode 100644 index 0000000..f6dddbc --- /dev/null +++ b/examples/instseg14display6.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instseg14display6.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstSeg14Display6 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_InstSeg14Display6_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstSeg14Display6_001', 'OnClick'): on_InstSeg14Display6_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstSeg14Display6_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('InstSeg14Display6', 'InstSeg14Display6_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstSeg14Display6_001') + 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='InstSeg14Display6 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/instseg16display1.py b/examples/instseg16display1.py new file mode 100644 index 0000000..94d4b6f --- /dev/null +++ b/examples/instseg16display1.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instseg16display1.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstSeg16Display1 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_InstSeg16Display1_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstSeg16Display1_001', 'OnClick'): on_InstSeg16Display1_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstSeg16Display1_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('InstSeg16Display1', 'InstSeg16Display1_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstSeg16Display1_001') + 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='InstSeg16Display1 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/instseg16display2.py b/examples/instseg16display2.py new file mode 100644 index 0000000..a1f6bba --- /dev/null +++ b/examples/instseg16display2.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instseg16display2.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstSeg16Display2 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_InstSeg16Display2_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstSeg16Display2_001', 'OnClick'): on_InstSeg16Display2_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstSeg16Display2_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('InstSeg16Display2', 'InstSeg16Display2_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstSeg16Display2_001') + 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='InstSeg16Display2 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/instseg16display3.py b/examples/instseg16display3.py new file mode 100644 index 0000000..ffe0669 --- /dev/null +++ b/examples/instseg16display3.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instseg16display3.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstSeg16Display3 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_InstSeg16Display3_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstSeg16Display3_001', 'OnClick'): on_InstSeg16Display3_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstSeg16Display3_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('InstSeg16Display3', 'InstSeg16Display3_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstSeg16Display3_001') + 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='InstSeg16Display3 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/instseg16display4.py b/examples/instseg16display4.py new file mode 100644 index 0000000..d0b73ad --- /dev/null +++ b/examples/instseg16display4.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instseg16display4.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstSeg16Display4 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_InstSeg16Display4_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstSeg16Display4_001', 'OnClick'): on_InstSeg16Display4_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstSeg16Display4_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('InstSeg16Display4', 'InstSeg16Display4_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstSeg16Display4_001') + 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='InstSeg16Display4 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/instseg16display5.py b/examples/instseg16display5.py new file mode 100644 index 0000000..468dbc7 --- /dev/null +++ b/examples/instseg16display5.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instseg16display5.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstSeg16Display5 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_InstSeg16Display5_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstSeg16Display5_001', 'OnClick'): on_InstSeg16Display5_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstSeg16Display5_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('InstSeg16Display5', 'InstSeg16Display5_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstSeg16Display5_001') + 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='InstSeg16Display5 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/instseg16display6.py b/examples/instseg16display6.py new file mode 100644 index 0000000..a4b4335 --- /dev/null +++ b/examples/instseg16display6.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instseg16display6.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstSeg16Display6 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_InstSeg16Display6_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstSeg16Display6_001', 'OnClick'): on_InstSeg16Display6_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstSeg16Display6_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('InstSeg16Display6', 'InstSeg16Display6_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstSeg16Display6_001') + 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='InstSeg16Display6 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/instsightglasslevel.py b/examples/instsightglasslevel.py new file mode 100644 index 0000000..b92595c --- /dev/null +++ b/examples/instsightglasslevel.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instsightglasslevel.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstSightGlassLevel 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_InstSightGlassLevel_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstSightGlassLevel_001', 'OnClick'): on_InstSightGlassLevel_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstSightGlassLevel_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('InstSightGlassLevel', 'InstSightGlassLevel_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstSightGlassLevel_001') + 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='InstSightGlassLevel 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/instslideswitch.py b/examples/instslideswitch.py new file mode 100644 index 0000000..08dcc76 --- /dev/null +++ b/examples/instslideswitch.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instslideswitch.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstSlideSwitch 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_InstSlideSwitch_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstSlideSwitch_001', 'OnClick'): on_InstSlideSwitch_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstSlideSwitch_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('InstSlideSwitch', 'InstSlideSwitch_001', 16, 16, 150, 36) + widget.set_prop('text', 'InstSlideSwitch_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='InstSlideSwitch 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/instsphericaltanklevel.py b/examples/instsphericaltanklevel.py new file mode 100644 index 0000000..265d53d --- /dev/null +++ b/examples/instsphericaltanklevel.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instsphericaltanklevel.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstSphericalTankLevel 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_InstSphericalTankLevel_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstSphericalTankLevel_001', 'OnClick'): on_InstSphericalTankLevel_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstSphericalTankLevel_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('InstSphericalTankLevel', 'InstSphericalTankLevel_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstSphericalTankLevel_001') + 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='InstSphericalTankLevel 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/inststacklight.py b/examples/inststacklight.py new file mode 100644 index 0000000..0d269f0 --- /dev/null +++ b/examples/inststacklight.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 inststacklight.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstStackLight example' (292x212) 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_InstStackLight_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstStackLight_001', 'OnClick'): on_InstStackLight_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstStackLight_001': (True, True, False, False, 16, 16, 260, 180), +} + + +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('InstStackLight', 'InstStackLight_001', 16, 16, 260, 180) + widget.set_prop('text', 'InstStackLight_001') + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (292, 212) +_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='InstStackLight example') + window.set_default_size(292, 212) + 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/insttanklevelalarms.py b/examples/insttanklevelalarms.py new file mode 100644 index 0000000..9e0d427 --- /dev/null +++ b/examples/insttanklevelalarms.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 insttanklevelalarms.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstTankLevelAlarms 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_InstTankLevelAlarms_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstTankLevelAlarms_001', 'OnClick'): on_InstTankLevelAlarms_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstTankLevelAlarms_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('InstTankLevelAlarms', 'InstTankLevelAlarms_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstTankLevelAlarms_001') + 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='InstTankLevelAlarms 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/insttemperaturegauge.py b/examples/insttemperaturegauge.py new file mode 100644 index 0000000..a0f4dd7 --- /dev/null +++ b/examples/insttemperaturegauge.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 insttemperaturegauge.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstTemperatureGauge 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_InstTemperatureGauge_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstTemperatureGauge_001', 'OnClick'): on_InstTemperatureGauge_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstTemperatureGauge_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('InstTemperatureGauge', 'InstTemperatureGauge_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstTemperatureGauge_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='InstTemperatureGauge 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/insttoggleswitch.py b/examples/insttoggleswitch.py new file mode 100644 index 0000000..d4b43d3 --- /dev/null +++ b/examples/insttoggleswitch.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 insttoggleswitch.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstToggleSwitch 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_InstToggleSwitch_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstToggleSwitch_001', 'OnClick'): on_InstToggleSwitch_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstToggleSwitch_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('InstToggleSwitch', 'InstToggleSwitch_001', 16, 16, 150, 36) + widget.set_prop('text', 'InstToggleSwitch_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='InstToggleSwitch 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/insttoggleswitchilluminated.py b/examples/insttoggleswitchilluminated.py new file mode 100644 index 0000000..d4cadf4 --- /dev/null +++ b/examples/insttoggleswitchilluminated.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 insttoggleswitchilluminated.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstToggleSwitchIlluminated 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_InstToggleSwitchIlluminated_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstToggleSwitchIlluminated_001', 'OnClick'): on_InstToggleSwitchIlluminated_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstToggleSwitchIlluminated_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('InstToggleSwitchIlluminated', 'InstToggleSwitchIlluminated_001', 16, 16, 150, 36) + widget.set_prop('text', 'InstToggleSwitchIlluminated_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='InstToggleSwitchIlluminated 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/instverticaltanklevel.py b/examples/instverticaltanklevel.py new file mode 100644 index 0000000..246fded --- /dev/null +++ b/examples/instverticaltanklevel.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instverticaltanklevel.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstVerticalTankLevel 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_InstVerticalTankLevel_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstVerticalTankLevel_001', 'OnClick'): on_InstVerticalTankLevel_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstVerticalTankLevel_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('InstVerticalTankLevel', 'InstVerticalTankLevel_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstVerticalTankLevel_001') + 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='InstVerticalTankLevel 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/instvumeter.py b/examples/instvumeter.py new file mode 100644 index 0000000..d9ad241 --- /dev/null +++ b/examples/instvumeter.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 instvumeter.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'InstVuMeter 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_InstVuMeter_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('InstVuMeter_001', 'OnClick'): on_InstVuMeter_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'InstVuMeter_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('InstVuMeter', 'InstVuMeter_001', 16, 16, 200, 90) + widget.set_prop('text', 'InstVuMeter_001') + widget.set_prop('value', '-5') + 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='InstVuMeter 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/stockaccumulationdistribution.py b/examples/stockaccumulationdistribution.py new file mode 100644 index 0000000..e3fe654 --- /dev/null +++ b/examples/stockaccumulationdistribution.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockaccumulationdistribution.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockAccumulationDistribution 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_StockAccumulationDistribution_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockAccumulationDistribution_001', 'OnClick'): on_StockAccumulationDistribution_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockAccumulationDistribution_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('StockAccumulationDistribution', 'StockAccumulationDistribution_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockAccumulationDistribution_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='StockAccumulationDistribution 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/stockareachart.py b/examples/stockareachart.py new file mode 100644 index 0000000..fe16f3a --- /dev/null +++ b/examples/stockareachart.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockareachart.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockAreaChart 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_StockAreaChart_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockAreaChart_001', 'OnClick'): on_StockAreaChart_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockAreaChart_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('StockAreaChart', 'StockAreaChart_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockAreaChart_001') + 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='StockAreaChart 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/stockarrow.py b/examples/stockarrow.py new file mode 100644 index 0000000..a78ac10 --- /dev/null +++ b/examples/stockarrow.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockarrow.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockArrow 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_StockArrow_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockArrow_001', 'OnClick'): on_StockArrow_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockArrow_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('StockArrow', 'StockArrow_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockArrow_001') + 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='StockArrow 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/stockbaselinechart.py b/examples/stockbaselinechart.py new file mode 100644 index 0000000..dfbf91a --- /dev/null +++ b/examples/stockbaselinechart.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockbaselinechart.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockBaselineChart 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_StockBaselineChart_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockBaselineChart_001', 'OnClick'): on_StockBaselineChart_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockBaselineChart_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('StockBaselineChart', 'StockBaselineChart_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockBaselineChart_001') + 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='StockBaselineChart 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/stockbrush.py b/examples/stockbrush.py new file mode 100644 index 0000000..45ff373 --- /dev/null +++ b/examples/stockbrush.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockbrush.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockBrush 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_StockBrush_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockBrush_001', 'OnClick'): on_StockBrush_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockBrush_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('StockBrush', 'StockBrush_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockBrush_001') + 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='StockBrush 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/stockcandlestickchart.py b/examples/stockcandlestickchart.py new file mode 100644 index 0000000..eba7160 --- /dev/null +++ b/examples/stockcandlestickchart.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockcandlestickchart.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockCandlestickChart 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_StockCandlestickChart_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockCandlestickChart_001', 'OnClick'): on_StockCandlestickChart_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockCandlestickChart_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('StockCandlestickChart', 'StockCandlestickChart_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockCandlestickChart_001') + 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='StockCandlestickChart 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/stockcorrelationview.py b/examples/stockcorrelationview.py new file mode 100644 index 0000000..8069a26 --- /dev/null +++ b/examples/stockcorrelationview.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockcorrelationview.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockCorrelationView 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_StockCorrelationView_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockCorrelationView_001', 'OnClick'): on_StockCorrelationView_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockCorrelationView_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('StockCorrelationView', 'StockCorrelationView_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockCorrelationView_001') + 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='StockCorrelationView 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/stockellipse.py b/examples/stockellipse.py new file mode 100644 index 0000000..52fb200 --- /dev/null +++ b/examples/stockellipse.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockellipse.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockEllipse 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_StockEllipse_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockEllipse_001', 'OnClick'): on_StockEllipse_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockEllipse_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('StockEllipse', 'StockEllipse_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockEllipse_001') + 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='StockEllipse 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/stockfibextension.py b/examples/stockfibextension.py new file mode 100644 index 0000000..f1eb780 --- /dev/null +++ b/examples/stockfibextension.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockfibextension.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockFibExtension 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_StockFibExtension_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockFibExtension_001', 'OnClick'): on_StockFibExtension_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockFibExtension_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('StockFibExtension', 'StockFibExtension_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockFibExtension_001') + 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='StockFibExtension 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/stockfibretracement.py b/examples/stockfibretracement.py new file mode 100644 index 0000000..8d0b41c --- /dev/null +++ b/examples/stockfibretracement.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockfibretracement.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockFibRetracement 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_StockFibRetracement_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockFibRetracement_001', 'OnClick'): on_StockFibRetracement_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockFibRetracement_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('StockFibRetracement', 'StockFibRetracement_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockFibRetracement_001') + 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='StockFibRetracement 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/stockheikinashi.py b/examples/stockheikinashi.py new file mode 100644 index 0000000..df83a3f --- /dev/null +++ b/examples/stockheikinashi.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockheikinashi.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockHeikinAshi 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_StockHeikinAshi_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockHeikinAshi_001', 'OnClick'): on_StockHeikinAshi_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockHeikinAshi_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('StockHeikinAshi', 'StockHeikinAshi_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockHeikinAshi_001') + 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='StockHeikinAshi 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/stockhollowcandles.py b/examples/stockhollowcandles.py new file mode 100644 index 0000000..8eaa76d --- /dev/null +++ b/examples/stockhollowcandles.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockhollowcandles.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockHollowCandles 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_StockHollowCandles_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockHollowCandles_001', 'OnClick'): on_StockHollowCandles_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockHollowCandles_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('StockHollowCandles', 'StockHollowCandles_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockHollowCandles_001') + 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='StockHollowCandles 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/stockhorizontalline.py b/examples/stockhorizontalline.py new file mode 100644 index 0000000..231827a --- /dev/null +++ b/examples/stockhorizontalline.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockhorizontalline.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockHorizontalLine 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_StockHorizontalLine_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockHorizontalLine_001', 'OnClick'): on_StockHorizontalLine_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockHorizontalLine_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('StockHorizontalLine', 'StockHorizontalLine_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockHorizontalLine_001') + 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='StockHorizontalLine 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/stockindexedcomparison.py b/examples/stockindexedcomparison.py new file mode 100644 index 0000000..7a30530 --- /dev/null +++ b/examples/stockindexedcomparison.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockindexedcomparison.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockIndexedComparison 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_StockIndexedComparison_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockIndexedComparison_001', 'OnClick'): on_StockIndexedComparison_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockIndexedComparison_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('StockIndexedComparison', 'StockIndexedComparison_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockIndexedComparison_001') + 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='StockIndexedComparison 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/stocklinechart.py b/examples/stocklinechart.py new file mode 100644 index 0000000..666ed24 --- /dev/null +++ b/examples/stocklinechart.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stocklinechart.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockLineChart 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_StockLineChart_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockLineChart_001', 'OnClick'): on_StockLineChart_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockLineChart_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('StockLineChart', 'StockLineChart_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockLineChart_001') + 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='StockLineChart 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/stockmeasuretool.py b/examples/stockmeasuretool.py new file mode 100644 index 0000000..9ff2204 --- /dev/null +++ b/examples/stockmeasuretool.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockmeasuretool.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockMeasureTool 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_StockMeasureTool_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockMeasureTool_001', 'OnClick'): on_StockMeasureTool_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockMeasureTool_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('StockMeasureTool', 'StockMeasureTool_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockMeasureTool_001') + 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='StockMeasureTool 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/stockmoneyflowchart.py b/examples/stockmoneyflowchart.py new file mode 100644 index 0000000..cad2e82 --- /dev/null +++ b/examples/stockmoneyflowchart.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockmoneyflowchart.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockMoneyFlowChart 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_StockMoneyFlowChart_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockMoneyFlowChart_001', 'OnClick'): on_StockMoneyFlowChart_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockMoneyFlowChart_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('StockMoneyFlowChart', 'StockMoneyFlowChart_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockMoneyFlowChart_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='StockMoneyFlowChart 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/stockmultisymbolchart.py b/examples/stockmultisymbolchart.py new file mode 100644 index 0000000..7bfc6f4 --- /dev/null +++ b/examples/stockmultisymbolchart.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockmultisymbolchart.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockMultiSymbolChart 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_StockMultiSymbolChart_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockMultiSymbolChart_001', 'OnClick'): on_StockMultiSymbolChart_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockMultiSymbolChart_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('StockMultiSymbolChart', 'StockMultiSymbolChart_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockMultiSymbolChart_001') + 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='StockMultiSymbolChart 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/stockobvchart.py b/examples/stockobvchart.py new file mode 100644 index 0000000..85c9cce --- /dev/null +++ b/examples/stockobvchart.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockobvchart.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockOBVChart 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_StockOBVChart_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockOBVChart_001', 'OnClick'): on_StockOBVChart_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockOBVChart_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('StockOBVChart', 'StockOBVChart_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockOBVChart_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='StockOBVChart 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/stockohlcbarchart.py b/examples/stockohlcbarchart.py new file mode 100644 index 0000000..88b0964 --- /dev/null +++ b/examples/stockohlcbarchart.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockohlcbarchart.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockOHLCBarChart 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_StockOHLCBarChart_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockOHLCBarChart_001', 'OnClick'): on_StockOHLCBarChart_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockOHLCBarChart_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('StockOHLCBarChart', 'StockOHLCBarChart_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockOHLCBarChart_001') + 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='StockOHLCBarChart 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/stockoverlaychart.py b/examples/stockoverlaychart.py new file mode 100644 index 0000000..07031fd --- /dev/null +++ b/examples/stockoverlaychart.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockoverlaychart.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockOverlayChart 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_StockOverlayChart_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockOverlayChart_001', 'OnClick'): on_StockOverlayChart_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockOverlayChart_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('StockOverlayChart', 'StockOverlayChart_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockOverlayChart_001') + 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='StockOverlayChart 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/stockparallelchannel.py b/examples/stockparallelchannel.py new file mode 100644 index 0000000..dfd2ef4 --- /dev/null +++ b/examples/stockparallelchannel.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockparallelchannel.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockParallelChannel 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_StockParallelChannel_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockParallelChannel_001', 'OnClick'): on_StockParallelChannel_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockParallelChannel_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('StockParallelChannel', 'StockParallelChannel_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockParallelChannel_001') + 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='StockParallelChannel 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/stockpitchfork.py b/examples/stockpitchfork.py new file mode 100644 index 0000000..9455faa --- /dev/null +++ b/examples/stockpitchfork.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockpitchfork.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockPitchfork 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_StockPitchfork_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockPitchfork_001', 'OnClick'): on_StockPitchfork_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockPitchfork_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('StockPitchfork', 'StockPitchfork_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockPitchfork_001') + 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='StockPitchfork 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/stockpricelabel.py b/examples/stockpricelabel.py new file mode 100644 index 0000000..acc973a --- /dev/null +++ b/examples/stockpricelabel.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockpricelabel.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockPriceLabel 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_StockPriceLabel_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockPriceLabel_001', 'OnClick'): on_StockPriceLabel_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockPriceLabel_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('StockPriceLabel', 'StockPriceLabel_001', 16, 16, 180, 30) + widget.set_prop('text', 'StockPriceLabel_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='StockPriceLabel 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/stockratiochart.py b/examples/stockratiochart.py new file mode 100644 index 0000000..6f2cffc --- /dev/null +++ b/examples/stockratiochart.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockratiochart.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockRatioChart 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_StockRatioChart_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockRatioChart_001', 'OnClick'): on_StockRatioChart_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockRatioChart_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('StockRatioChart', 'StockRatioChart_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockRatioChart_001') + 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='StockRatioChart 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/stockrectangle.py b/examples/stockrectangle.py new file mode 100644 index 0000000..1ca987f --- /dev/null +++ b/examples/stockrectangle.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockrectangle.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockRectangle 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_StockRectangle_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockRectangle_001', 'OnClick'): on_StockRectangle_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockRectangle_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('StockRectangle', 'StockRectangle_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockRectangle_001') + 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='StockRectangle 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/stockrelativeperformance.py b/examples/stockrelativeperformance.py new file mode 100644 index 0000000..80162e7 --- /dev/null +++ b/examples/stockrelativeperformance.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockrelativeperformance.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockRelativePerformance 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_StockRelativePerformance_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockRelativePerformance_001', 'OnClick'): on_StockRelativePerformance_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockRelativePerformance_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('StockRelativePerformance', 'StockRelativePerformance_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockRelativePerformance_001') + 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='StockRelativePerformance 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/stockrenko.py b/examples/stockrenko.py new file mode 100644 index 0000000..df17768 --- /dev/null +++ b/examples/stockrenko.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockrenko.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockRenko 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_StockRenko_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockRenko_001', 'OnClick'): on_StockRenko_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockRenko_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('StockRenko', 'StockRenko_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockRenko_001') + 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='StockRenko 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/stocksectorcomparison.py b/examples/stocksectorcomparison.py new file mode 100644 index 0000000..1f2127d --- /dev/null +++ b/examples/stocksectorcomparison.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stocksectorcomparison.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockSectorComparison 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_StockSectorComparison_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockSectorComparison_001', 'OnClick'): on_StockSectorComparison_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockSectorComparison_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('StockSectorComparison', 'StockSectorComparison_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockSectorComparison_001') + 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='StockSectorComparison 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/stockspreadchart.py b/examples/stockspreadchart.py new file mode 100644 index 0000000..087b26f --- /dev/null +++ b/examples/stockspreadchart.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockspreadchart.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockSpreadChart 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_StockSpreadChart_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockSpreadChart_001', 'OnClick'): on_StockSpreadChart_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockSpreadChart_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('StockSpreadChart', 'StockSpreadChart_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockSpreadChart_001') + 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='StockSpreadChart 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/stocktextannotation.py b/examples/stocktextannotation.py new file mode 100644 index 0000000..6b6ccf4 --- /dev/null +++ b/examples/stocktextannotation.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stocktextannotation.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockTextAnnotation 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_StockTextAnnotation_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockTextAnnotation_001', 'OnClick'): on_StockTextAnnotation_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockTextAnnotation_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('StockTextAnnotation', 'StockTextAnnotation_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockTextAnnotation_001') + 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='StockTextAnnotation 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/stocktrendline.py b/examples/stocktrendline.py new file mode 100644 index 0000000..662b9a6 --- /dev/null +++ b/examples/stocktrendline.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stocktrendline.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockTrendline 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_StockTrendline_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockTrendline_001', 'OnClick'): on_StockTrendline_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockTrendline_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('StockTrendline', 'StockTrendline_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockTrendline_001') + 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='StockTrendline 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/stockverticalline.py b/examples/stockverticalline.py new file mode 100644 index 0000000..3233bbf --- /dev/null +++ b/examples/stockverticalline.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockverticalline.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockVerticalLine 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_StockVerticalLine_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockVerticalLine_001', 'OnClick'): on_StockVerticalLine_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockVerticalLine_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('StockVerticalLine', 'StockVerticalLine_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockVerticalLine_001') + 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='StockVerticalLine 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/stockvolumebars.py b/examples/stockvolumebars.py new file mode 100644 index 0000000..07656cf --- /dev/null +++ b/examples/stockvolumebars.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockvolumebars.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockVolumeBars 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_StockVolumeBars_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockVolumeBars_001', 'OnClick'): on_StockVolumeBars_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockVolumeBars_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('StockVolumeBars', 'StockVolumeBars_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockVolumeBars_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='StockVolumeBars 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/stockvolumecandles.py b/examples/stockvolumecandles.py new file mode 100644 index 0000000..7a3a29f --- /dev/null +++ b/examples/stockvolumecandles.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockvolumecandles.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockVolumeCandles 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_StockVolumeCandles_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockVolumeCandles_001', 'OnClick'): on_StockVolumeCandles_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockVolumeCandles_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('StockVolumeCandles', 'StockVolumeCandles_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockVolumeCandles_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='StockVolumeCandles 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/stockvolumeprofile.py b/examples/stockvolumeprofile.py new file mode 100644 index 0000000..40f7579 --- /dev/null +++ b/examples/stockvolumeprofile.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 stockvolumeprofile.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'StockVolumeProfile 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_StockVolumeProfile_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('StockVolumeProfile_001', 'OnClick'): on_StockVolumeProfile_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'StockVolumeProfile_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('StockVolumeProfile', 'StockVolumeProfile_001', 16, 16, 200, 90) + widget.set_prop('text', 'StockVolumeProfile_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='StockVolumeProfile 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 index d2c25a0..36c7c16 100644 --- a/examples/stockvwap.py +++ b/examples/stockvwap.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # Generated by LightGUI (Python/XFrame). # A plain Python/GTK program: no CMake. Run with: -# python3 Example.py +# python3 stockvwap.py # with the LightGUI Python package on PYTHONPATH # (the designer sets it when it runs this script). # diff --git a/examples/tactionlist.py b/examples/tactionlist.py new file mode 100644 index 0000000..491c946 --- /dev/null +++ b/examples/tactionlist.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tactionlist.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TActionList 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_TActionList_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TActionList_001', 'OnClick'): on_TActionList_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TActionList_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('TActionList', 'TActionList_001', 16, 16, 200, 90) + 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='TActionList 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/tapplicationproperties.py b/examples/tapplicationproperties.py new file mode 100644 index 0000000..41b16c9 --- /dev/null +++ b/examples/tapplicationproperties.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tapplicationproperties.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TApplicationProperties 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_TApplicationProperties_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TApplicationProperties_001', 'OnClick'): on_TApplicationProperties_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TApplicationProperties_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('TApplicationProperties', 'TApplicationProperties_001', 16, 16, 200, 90) + 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='TApplicationProperties 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/tarrow.py b/examples/tarrow.py new file mode 100644 index 0000000..86b6e34 --- /dev/null +++ b/examples/tarrow.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tarrow.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TArrow 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_TArrow_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TArrow_001', 'OnClick'): on_TArrow_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TArrow_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('TArrow', 'TArrow_001', 16, 16, 200, 90) + 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='TArrow 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/tbevel.py b/examples/tbevel.py new file mode 100644 index 0000000..a9e38fe --- /dev/null +++ b/examples/tbevel.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tbevel.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TBevel 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_TBevel_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TBevel_001', 'OnClick'): on_TBevel_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TBevel_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('TBevel', 'TBevel_001', 16, 16, 200, 90) + 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='TBevel 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/tbitbtn.py b/examples/tbitbtn.py new file mode 100644 index 0000000..810d94b --- /dev/null +++ b/examples/tbitbtn.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tbitbtn.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TBitBtn 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_TBitBtn_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TBitBtn_001', 'OnClick'): on_TBitBtn_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TBitBtn_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('TBitBtn', 'TBitBtn_001', 16, 16, 200, 90) + widget.set_prop('label', 'TBitBtn_001') + 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='TBitBtn 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/tbutton.py b/examples/tbutton.py new file mode 100644 index 0000000..ad3e986 --- /dev/null +++ b/examples/tbutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tbutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TButton 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_TButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TButton_001', 'OnClick'): on_TButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TButton_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('TButton', 'TButton_001', 16, 16, 150, 36) + widget.set_prop('label', 'TButton_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='TButton 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/tbuttonpanel.py b/examples/tbuttonpanel.py new file mode 100644 index 0000000..a70246f --- /dev/null +++ b/examples/tbuttonpanel.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tbuttonpanel.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TButtonPanel 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_TButtonPanel_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TButtonPanel_001', 'OnClick'): on_TButtonPanel_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TButtonPanel_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('TButtonPanel', 'TButtonPanel_001', 16, 16, 150, 36) + widget.set_prop('label', 'TButtonPanel_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='TButtonPanel 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/tcalcedit.py b/examples/tcalcedit.py new file mode 100644 index 0000000..093eb58 --- /dev/null +++ b/examples/tcalcedit.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tcalcedit.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TCalcEdit 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_TCalcEdit_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TCalcEdit_001', 'OnClick'): on_TCalcEdit_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TCalcEdit_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('TCalcEdit', 'TCalcEdit_001', 16, 16, 180, 30) + widget.set_prop('text', 'TCalcEdit_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='TCalcEdit 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/tcalculatordialog.py b/examples/tcalculatordialog.py new file mode 100644 index 0000000..389168d --- /dev/null +++ b/examples/tcalculatordialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tcalculatordialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TCalculatorDialog example' (332x252) 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_TCalculatorDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TCalculatorDialog_001', 'OnClick'): on_TCalculatorDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TCalculatorDialog_001': (True, True, False, False, 16, 16, 300, 220), +} + + +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('TCalculatorDialog', 'TCalculatorDialog_001', 16, 16, 300, 220) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (332, 252) +_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='TCalculatorDialog example') + window.set_default_size(332, 252) + 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/tcalendar.py b/examples/tcalendar.py new file mode 100644 index 0000000..ff7d957 --- /dev/null +++ b/examples/tcalendar.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tcalendar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TCalendar 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_TCalendar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TCalendar_001', 'OnClick'): on_TCalendar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TCalendar_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('TCalendar', 'TCalendar_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='TCalendar 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/tcalendardialog.py b/examples/tcalendardialog.py new file mode 100644 index 0000000..f439c02 --- /dev/null +++ b/examples/tcalendardialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tcalendardialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TCalendarDialog example' (352x312) 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_TCalendarDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TCalendarDialog_001', 'OnClick'): on_TCalendarDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TCalendarDialog_001': (True, True, False, False, 16, 16, 320, 280), +} + + +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('TCalendarDialog', 'TCalendarDialog_001', 16, 16, 320, 280) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (352, 312) +_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='TCalendarDialog example') + window.set_default_size(352, 312) + 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/tcheckbox.py b/examples/tcheckbox.py new file mode 100644 index 0000000..7b28088 --- /dev/null +++ b/examples/tcheckbox.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tcheckbox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TCheckBox 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_TCheckBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TCheckBox_001', 'OnClick'): on_TCheckBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TCheckBox_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('TCheckBox', 'TCheckBox_001', 16, 16, 150, 36) + widget.set_prop('label', 'TCheckBox_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='TCheckBox 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/tcheckcombobox.py b/examples/tcheckcombobox.py new file mode 100644 index 0000000..89af331 --- /dev/null +++ b/examples/tcheckcombobox.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tcheckcombobox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TCheckComboBox 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_TCheckComboBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TCheckComboBox_001', 'OnClick'): on_TCheckComboBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TCheckComboBox_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('TCheckComboBox', 'TCheckComboBox_001', 16, 16, 150, 36) + widget.set_prop('items', 'Alpha|Beta|Gamma') + widget.set_prop('text', 'TCheckComboBox_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='TCheckComboBox 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/tcheckgroup.py b/examples/tcheckgroup.py new file mode 100644 index 0000000..809451b --- /dev/null +++ b/examples/tcheckgroup.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tcheckgroup.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TCheckGroup 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_TCheckGroup_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TCheckGroup_001', 'OnClick'): on_TCheckGroup_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TCheckGroup_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('TCheckGroup', 'TCheckGroup_001', 16, 16, 150, 36) + widget.set_prop('items', 'Alpha|Beta|Gamma') + 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='TCheckGroup 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/tchecklistbox.py b/examples/tchecklistbox.py new file mode 100644 index 0000000..7fd46a4 --- /dev/null +++ b/examples/tchecklistbox.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tchecklistbox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TCheckListBox 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_TCheckListBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TCheckListBox_001', 'OnClick'): on_TCheckListBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TCheckListBox_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('TCheckListBox', 'TCheckListBox_001', 16, 16, 150, 36) + widget.set_prop('items', 'Alpha|Beta|Gamma') + 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='TCheckListBox 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/tcolorbox.py b/examples/tcolorbox.py new file mode 100644 index 0000000..022fa8d --- /dev/null +++ b/examples/tcolorbox.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tcolorbox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TColorBox 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_TColorBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TColorBox_001', 'OnClick'): on_TColorBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TColorBox_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('TColorBox', 'TColorBox_001', 16, 16, 200, 90) + 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='TColorBox 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/tcolorbutton.py b/examples/tcolorbutton.py new file mode 100644 index 0000000..3cdc329 --- /dev/null +++ b/examples/tcolorbutton.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tcolorbutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TColorButton 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_TColorButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TColorButton_001', 'OnClick'): on_TColorButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TColorButton_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('TColorButton', 'TColorButton_001', 16, 16, 150, 36) + 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='TColorButton 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/tcolordialog.py b/examples/tcolordialog.py new file mode 100644 index 0000000..15dcceb --- /dev/null +++ b/examples/tcolordialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tcolordialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TColorDialog example' (552x332) 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_TColorDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TColorDialog_001', 'OnClick'): on_TColorDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TColorDialog_001': (True, True, False, False, 16, 16, 520, 300), +} + + +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('TColorDialog', 'TColorDialog_001', 16, 16, 520, 300) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (552, 332) +_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='TColorDialog example') + window.set_default_size(552, 332) + 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/tcolorlistbox.py b/examples/tcolorlistbox.py new file mode 100644 index 0000000..f703c0c --- /dev/null +++ b/examples/tcolorlistbox.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tcolorlistbox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TColorListBox 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_TColorListBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TColorListBox_001', 'OnClick'): on_TColorListBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TColorListBox_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('TColorListBox', 'TColorListBox_001', 16, 16, 200, 90) + 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='TColorListBox 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/tcombobox.py b/examples/tcombobox.py new file mode 100644 index 0000000..05f29eb --- /dev/null +++ b/examples/tcombobox.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tcombobox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TComboBox 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_TComboBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TComboBox_001', 'OnClick'): on_TComboBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TComboBox_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('TComboBox', 'TComboBox_001', 16, 16, 180, 30) + widget.set_prop('items', 'Alpha|Beta|Gamma') + widget.set_prop('text', 'TComboBox_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='TComboBox 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/tcomboboxex.py b/examples/tcomboboxex.py new file mode 100644 index 0000000..5a3d749 --- /dev/null +++ b/examples/tcomboboxex.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tcomboboxex.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TComboBoxEx 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_TComboBoxEx_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TComboBoxEx_001', 'OnClick'): on_TComboBoxEx_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TComboBoxEx_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('TComboBoxEx', 'TComboBoxEx_001', 16, 16, 180, 30) + widget.set_prop('items', 'Alpha|Beta|Gamma') + widget.set_prop('text', 'TComboBoxEx_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='TComboBoxEx 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/tcontrolbar.py b/examples/tcontrolbar.py new file mode 100644 index 0000000..6e2e10a --- /dev/null +++ b/examples/tcontrolbar.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tcontrolbar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TControlBar 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_TControlBar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TControlBar_001', 'OnClick'): on_TControlBar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TControlBar_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('TControlBar', 'TControlBar_001', 16, 16, 200, 90) + widget.set_prop('items', 'Alpha|Beta|Gamma') + 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='TControlBar 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/tcoolbar.py b/examples/tcoolbar.py new file mode 100644 index 0000000..d45708a --- /dev/null +++ b/examples/tcoolbar.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tcoolbar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TCoolBar 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_TCoolBar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TCoolBar_001', 'OnClick'): on_TCoolBar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TCoolBar_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('TCoolBar', 'TCoolBar_001', 16, 16, 200, 90) + widget.set_prop('items', 'Alpha|Beta|Gamma') + 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='TCoolBar 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/tdateedit.py b/examples/tdateedit.py new file mode 100644 index 0000000..9acb9a6 --- /dev/null +++ b/examples/tdateedit.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tdateedit.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TDateEdit 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_TDateEdit_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TDateEdit_001', 'OnClick'): on_TDateEdit_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TDateEdit_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('TDateEdit', 'TDateEdit_001', 16, 16, 180, 30) + widget.set_prop('text', 'TDateEdit_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='TDateEdit 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/tdatetimepicker.py b/examples/tdatetimepicker.py new file mode 100644 index 0000000..b7ca8d2 --- /dev/null +++ b/examples/tdatetimepicker.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tdatetimepicker.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TDateTimePicker 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_TDateTimePicker_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TDateTimePicker_001', 'OnClick'): on_TDateTimePicker_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TDateTimePicker_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('TDateTimePicker', 'TDateTimePicker_001', 16, 16, 200, 90) + 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='TDateTimePicker 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/tdirectoryedit.py b/examples/tdirectoryedit.py new file mode 100644 index 0000000..c8c78f1 --- /dev/null +++ b/examples/tdirectoryedit.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tdirectoryedit.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TDirectoryEdit 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_TDirectoryEdit_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TDirectoryEdit_001', 'OnClick'): on_TDirectoryEdit_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TDirectoryEdit_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('TDirectoryEdit', 'TDirectoryEdit_001', 16, 16, 180, 30) + widget.set_prop('text', 'TDirectoryEdit_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='TDirectoryEdit 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/tdrawgrid.py b/examples/tdrawgrid.py new file mode 100644 index 0000000..5c3bff2 --- /dev/null +++ b/examples/tdrawgrid.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tdrawgrid.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TDrawGrid example' (292x212) 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_TDrawGrid_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TDrawGrid_001', 'OnClick'): on_TDrawGrid_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TDrawGrid_001': (True, True, False, False, 16, 16, 260, 180), +} + + +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('TDrawGrid', 'TDrawGrid_001', 16, 16, 260, 180) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (292, 212) +_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='TDrawGrid example') + window.set_default_size(292, 212) + 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/tedit.py b/examples/tedit.py new file mode 100644 index 0000000..4d99682 --- /dev/null +++ b/examples/tedit.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tedit.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TEdit 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_TEdit_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TEdit_001', 'OnClick'): on_TEdit_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TEdit_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('TEdit', 'TEdit_001', 16, 16, 180, 30) + widget.set_prop('text', 'TEdit_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='TEdit 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/teditbutton.py b/examples/teditbutton.py new file mode 100644 index 0000000..06845cc --- /dev/null +++ b/examples/teditbutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 teditbutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TEditButton 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_TEditButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TEditButton_001', 'OnClick'): on_TEditButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TEditButton_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('TEditButton', 'TEditButton_001', 16, 16, 150, 36) + widget.set_prop('label', 'TEditButton_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='TEditButton 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/tfilelistbox.py b/examples/tfilelistbox.py new file mode 100644 index 0000000..e10d406 --- /dev/null +++ b/examples/tfilelistbox.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tfilelistbox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TFileListBox 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_TFileListBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TFileListBox_001', 'OnClick'): on_TFileListBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TFileListBox_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('TFileListBox', 'TFileListBox_001', 16, 16, 200, 90) + widget.set_prop('items', 'Alpha|Beta|Gamma') + 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='TFileListBox 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/tfilenameedit.py b/examples/tfilenameedit.py new file mode 100644 index 0000000..b960747 --- /dev/null +++ b/examples/tfilenameedit.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tfilenameedit.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TFileNameEdit 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_TFileNameEdit_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TFileNameEdit_001', 'OnClick'): on_TFileNameEdit_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TFileNameEdit_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('TFileNameEdit', 'TFileNameEdit_001', 16, 16, 180, 30) + widget.set_prop('text', 'TFileNameEdit_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='TFileNameEdit 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/tfiltercombobox.py b/examples/tfiltercombobox.py new file mode 100644 index 0000000..799ba73 --- /dev/null +++ b/examples/tfiltercombobox.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tfiltercombobox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TFilterComboBox 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_TFilterComboBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TFilterComboBox_001', 'OnClick'): on_TFilterComboBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TFilterComboBox_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('TFilterComboBox', 'TFilterComboBox_001', 16, 16, 180, 30) + widget.set_prop('items', 'Alpha|Beta|Gamma') + widget.set_prop('text', 'TFilterComboBox_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='TFilterComboBox 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/tfinddialog.py b/examples/tfinddialog.py new file mode 100644 index 0000000..d71865b --- /dev/null +++ b/examples/tfinddialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tfinddialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TFindDialog example' (412x292) 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_TFindDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TFindDialog_001', 'OnClick'): on_TFindDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TFindDialog_001': (True, True, False, False, 16, 16, 380, 260), +} + + +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('TFindDialog', 'TFindDialog_001', 16, 16, 380, 260) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (412, 292) +_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='TFindDialog example') + window.set_default_size(412, 292) + 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/tfloatspinedit.py b/examples/tfloatspinedit.py new file mode 100644 index 0000000..93b774f --- /dev/null +++ b/examples/tfloatspinedit.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tfloatspinedit.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TFloatSpinEdit 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_TFloatSpinEdit_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TFloatSpinEdit_001', 'OnClick'): on_TFloatSpinEdit_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TFloatSpinEdit_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('TFloatSpinEdit', 'TFloatSpinEdit_001', 16, 16, 150, 36) + widget.set_prop('value', '65') + 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='TFloatSpinEdit 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/tflowpanel.py b/examples/tflowpanel.py new file mode 100644 index 0000000..fa08369 --- /dev/null +++ b/examples/tflowpanel.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tflowpanel.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TFlowPanel 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_TFlowPanel_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TFlowPanel_001', 'OnClick'): on_TFlowPanel_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TFlowPanel_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('TFlowPanel', 'TFlowPanel_001', 16, 16, 200, 90) + 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='TFlowPanel 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/tfontdialog.py b/examples/tfontdialog.py new file mode 100644 index 0000000..0b9e9d6 --- /dev/null +++ b/examples/tfontdialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tfontdialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TFontDialog example' (352x452) 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_TFontDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TFontDialog_001', 'OnClick'): on_TFontDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TFontDialog_001': (True, True, False, False, 16, 16, 320, 420), +} + + +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('TFontDialog', 'TFontDialog_001', 16, 16, 320, 420) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (352, 452) +_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='TFontDialog example') + window.set_default_size(352, 452) + 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/tform.py b/examples/tform.py new file mode 100644 index 0000000..b0e9b15 --- /dev/null +++ b/examples/tform.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tform.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TForm example' (332x252) 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_TForm_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TForm_001', 'OnClick'): on_TForm_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TForm_001': (True, True, False, False, 16, 16, 300, 220), +} + + +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('TForm', 'TForm_001', 16, 16, 300, 220) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (332, 252) +_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='TForm example') + window.set_default_size(332, 252) + 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/tframe.py b/examples/tframe.py new file mode 100644 index 0000000..b14b98d --- /dev/null +++ b/examples/tframe.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tframe.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TFrame 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_TFrame_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TFrame_001', 'OnClick'): on_TFrame_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TFrame_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('TFrame', 'TFrame_001', 16, 16, 200, 90) + widget.set_prop('label', 'TFrame_001') + 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='TFrame 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/tgroupbox.py b/examples/tgroupbox.py new file mode 100644 index 0000000..71812d0 --- /dev/null +++ b/examples/tgroupbox.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tgroupbox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TGroupBox 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_TGroupBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TGroupBox_001', 'OnClick'): on_TGroupBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TGroupBox_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('TGroupBox', 'TGroupBox_001', 16, 16, 200, 90) + widget.set_prop('label', 'TGroupBox_001') + 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='TGroupBox 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/theadercontrol.py b/examples/theadercontrol.py new file mode 100644 index 0000000..91a17e2 --- /dev/null +++ b/examples/theadercontrol.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 theadercontrol.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'THeaderControl 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_THeaderControl_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('THeaderControl_001', 'OnClick'): on_THeaderControl_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'THeaderControl_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('THeaderControl', 'THeaderControl_001', 16, 16, 200, 90) + widget.set_prop('items', 'Alpha|Beta|Gamma') + 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='THeaderControl 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/timage.py b/examples/timage.py new file mode 100644 index 0000000..7a58ed1 --- /dev/null +++ b/examples/timage.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 timage.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TImage 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_TImage_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TImage_001', 'OnClick'): on_TImage_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TImage_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('TImage', 'TImage_001', 16, 16, 200, 90) + 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='TImage 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/timagelist.py b/examples/timagelist.py new file mode 100644 index 0000000..8d327ab --- /dev/null +++ b/examples/timagelist.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 timagelist.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TImageList 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_TImageList_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TImageList_001', 'OnClick'): on_TImageList_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TImageList_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('TImageList', 'TImageList_001', 16, 16, 200, 90) + 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='TImageList 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/tlabel.py b/examples/tlabel.py new file mode 100644 index 0000000..bb13c45 --- /dev/null +++ b/examples/tlabel.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tlabel.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TLabel 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_TLabel_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TLabel_001', 'OnClick'): on_TLabel_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TLabel_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('TLabel', 'TLabel_001', 16, 16, 180, 30) + widget.set_prop('label', 'TLabel_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='TLabel 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/tlabelededit.py b/examples/tlabelededit.py new file mode 100644 index 0000000..7e56eef --- /dev/null +++ b/examples/tlabelededit.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tlabelededit.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TLabeledEdit 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_TLabeledEdit_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TLabeledEdit_001', 'OnClick'): on_TLabeledEdit_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TLabeledEdit_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('TLabeledEdit', 'TLabeledEdit_001', 16, 16, 180, 30) + widget.set_prop('text', 'TLabeledEdit_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='TLabeledEdit 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/tlistbox.py b/examples/tlistbox.py new file mode 100644 index 0000000..3e684f7 --- /dev/null +++ b/examples/tlistbox.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tlistbox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TListBox 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_TListBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TListBox_001', 'OnClick'): on_TListBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TListBox_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('TListBox', 'TListBox_001', 16, 16, 200, 90) + widget.set_prop('items', 'Alpha|Beta|Gamma') + 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='TListBox 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/tlistview.py b/examples/tlistview.py new file mode 100644 index 0000000..4839987 --- /dev/null +++ b/examples/tlistview.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tlistview.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TListView example' (352x232) 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_TListView_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TListView_001', 'OnClick'): on_TListView_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TListView_001': (True, True, False, False, 16, 16, 320, 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('TListView', 'TListView_001', 16, 16, 320, 200) + widget.set_prop('items', 'Alpha|Beta|Gamma') + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (352, 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='TListView example') + window.set_default_size(352, 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/tmainmenu.py b/examples/tmainmenu.py new file mode 100644 index 0000000..01d0f29 --- /dev/null +++ b/examples/tmainmenu.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tmainmenu.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TMainMenu 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_TMainMenu_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TMainMenu_001', 'OnClick'): on_TMainMenu_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TMainMenu_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('TMainMenu', 'TMainMenu_001', 16, 16, 200, 90) + 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='TMainMenu 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/tmaskedit.py b/examples/tmaskedit.py new file mode 100644 index 0000000..be0e03b --- /dev/null +++ b/examples/tmaskedit.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tmaskedit.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TMaskEdit 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_TMaskEdit_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TMaskEdit_001', 'OnClick'): on_TMaskEdit_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TMaskEdit_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('TMaskEdit', 'TMaskEdit_001', 16, 16, 180, 30) + widget.set_prop('text', 'TMaskEdit_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='TMaskEdit 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/tmemo.py b/examples/tmemo.py new file mode 100644 index 0000000..b04efa1 --- /dev/null +++ b/examples/tmemo.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tmemo.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TMemo example' (292x212) 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_TMemo_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TMemo_001', 'OnClick'): on_TMemo_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TMemo_001': (True, True, False, False, 16, 16, 260, 180), +} + + +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('TMemo', 'TMemo_001', 16, 16, 260, 180) + widget.set_prop('text', 'TMemo_001') + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (292, 212) +_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='TMemo example') + window.set_default_size(292, 212) + 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/tnotebook.py b/examples/tnotebook.py new file mode 100644 index 0000000..6b5b0f0 --- /dev/null +++ b/examples/tnotebook.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tnotebook.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TNotebook example' (292x212) 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_TNotebook_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TNotebook_001', 'OnClick'): on_TNotebook_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TNotebook_001': (True, True, False, False, 16, 16, 260, 180), +} + + +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('TNotebook', 'TNotebook_001', 16, 16, 260, 180) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (292, 212) +_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='TNotebook example') + window.set_default_size(292, 212) + 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/topendialog.py b/examples/topendialog.py new file mode 100644 index 0000000..55c3830 --- /dev/null +++ b/examples/topendialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 topendialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TOpenDialog example' (332x252) 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_TOpenDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TOpenDialog_001', 'OnClick'): on_TOpenDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TOpenDialog_001': (True, True, False, False, 16, 16, 300, 220), +} + + +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('TOpenDialog', 'TOpenDialog_001', 16, 16, 300, 220) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (332, 252) +_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='TOpenDialog example') + window.set_default_size(332, 252) + 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/topenpicturedialog.py b/examples/topenpicturedialog.py new file mode 100644 index 0000000..9cbde80 --- /dev/null +++ b/examples/topenpicturedialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 topenpicturedialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TOpenPictureDialog example' (512x272) 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_TOpenPictureDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TOpenPictureDialog_001', 'OnClick'): on_TOpenPictureDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TOpenPictureDialog_001': (True, True, False, False, 16, 16, 480, 240), +} + + +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('TOpenPictureDialog', 'TOpenPictureDialog_001', 16, 16, 480, 240) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (512, 272) +_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='TOpenPictureDialog example') + window.set_default_size(512, 272) + 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/tpagecontrol.py b/examples/tpagecontrol.py new file mode 100644 index 0000000..5c29d8e --- /dev/null +++ b/examples/tpagecontrol.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tpagecontrol.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TPageControl example' (292x212) 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_TPageControl_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TPageControl_001', 'OnClick'): on_TPageControl_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TPageControl_001': (True, True, False, False, 16, 16, 260, 180), +} + + +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('TPageControl', 'TPageControl_001', 16, 16, 260, 180) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (292, 212) +_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='TPageControl example') + window.set_default_size(292, 212) + 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/tpagesetupdialog.py b/examples/tpagesetupdialog.py new file mode 100644 index 0000000..49f6fc3 --- /dev/null +++ b/examples/tpagesetupdialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tpagesetupdialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TPageSetupDialog example' (352x432) 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_TPageSetupDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TPageSetupDialog_001', 'OnClick'): on_TPageSetupDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TPageSetupDialog_001': (True, True, False, False, 16, 16, 320, 400), +} + + +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('TPageSetupDialog', 'TPageSetupDialog_001', 16, 16, 320, 400) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (352, 432) +_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='TPageSetupDialog example') + window.set_default_size(352, 432) + 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/tpaintbox.py b/examples/tpaintbox.py new file mode 100644 index 0000000..6429824 --- /dev/null +++ b/examples/tpaintbox.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tpaintbox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TPaintBox 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_TPaintBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TPaintBox_001', 'OnClick'): on_TPaintBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TPaintBox_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('TPaintBox', 'TPaintBox_001', 16, 16, 200, 90) + 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='TPaintBox 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/tpairsplitter.py b/examples/tpairsplitter.py new file mode 100644 index 0000000..a1b40fa --- /dev/null +++ b/examples/tpairsplitter.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tpairsplitter.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TPairSplitter 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_TPairSplitter_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TPairSplitter_001', 'OnClick'): on_TPairSplitter_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TPairSplitter_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('TPairSplitter', 'TPairSplitter_001', 16, 16, 200, 90) + 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='TPairSplitter 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/tpanel.py b/examples/tpanel.py new file mode 100644 index 0000000..39bb7c9 --- /dev/null +++ b/examples/tpanel.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tpanel.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TPanel 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_TPanel_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TPanel_001', 'OnClick'): on_TPanel_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TPanel_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('TPanel', 'TPanel_001', 16, 16, 200, 90) + widget.set_prop('label', 'TPanel_001') + 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='TPanel 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/tpopupmenu.py b/examples/tpopupmenu.py new file mode 100644 index 0000000..78d2199 --- /dev/null +++ b/examples/tpopupmenu.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tpopupmenu.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TPopupMenu 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_TPopupMenu_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TPopupMenu_001', 'OnClick'): on_TPopupMenu_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TPopupMenu_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('TPopupMenu', 'TPopupMenu_001', 16, 16, 200, 90) + 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='TPopupMenu 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/tpopupnotifier.py b/examples/tpopupnotifier.py new file mode 100644 index 0000000..688f164 --- /dev/null +++ b/examples/tpopupnotifier.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tpopupnotifier.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TPopupNotifier 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_TPopupNotifier_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TPopupNotifier_001', 'OnClick'): on_TPopupNotifier_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TPopupNotifier_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('TPopupNotifier', 'TPopupNotifier_001', 16, 16, 200, 90) + 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='TPopupNotifier 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/tprintdialog.py b/examples/tprintdialog.py new file mode 100644 index 0000000..72bc1c2 --- /dev/null +++ b/examples/tprintdialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tprintdialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TPrintDialog example' (352x332) 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_TPrintDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TPrintDialog_001', 'OnClick'): on_TPrintDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TPrintDialog_001': (True, True, False, False, 16, 16, 320, 300), +} + + +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('TPrintDialog', 'TPrintDialog_001', 16, 16, 320, 300) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (352, 332) +_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='TPrintDialog example') + window.set_default_size(352, 332) + 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/tprintersetupdialog.py b/examples/tprintersetupdialog.py new file mode 100644 index 0000000..4e64795 --- /dev/null +++ b/examples/tprintersetupdialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tprintersetupdialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TPrinterSetupDialog example' (332x252) 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_TPrinterSetupDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TPrinterSetupDialog_001', 'OnClick'): on_TPrinterSetupDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TPrinterSetupDialog_001': (True, True, False, False, 16, 16, 300, 220), +} + + +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('TPrinterSetupDialog', 'TPrinterSetupDialog_001', 16, 16, 300, 220) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (332, 252) +_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='TPrinterSetupDialog example') + window.set_default_size(332, 252) + 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/tprogressbar.py b/examples/tprogressbar.py new file mode 100644 index 0000000..f0c6544 --- /dev/null +++ b/examples/tprogressbar.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tprogressbar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TProgressBar 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_TProgressBar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TProgressBar_001', 'OnClick'): on_TProgressBar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TProgressBar_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('TProgressBar', 'TProgressBar_001', 16, 16, 180, 30) + widget.set_prop('text', 'TProgressBar_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='TProgressBar 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/tradiobutton.py b/examples/tradiobutton.py new file mode 100644 index 0000000..a953db2 --- /dev/null +++ b/examples/tradiobutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tradiobutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TRadioButton 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_TRadioButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TRadioButton_001', 'OnClick'): on_TRadioButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TRadioButton_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('TRadioButton', 'TRadioButton_001', 16, 16, 150, 36) + widget.set_prop('label', 'TRadioButton_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='TRadioButton 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/tradiogroup.py b/examples/tradiogroup.py new file mode 100644 index 0000000..a9be6d1 --- /dev/null +++ b/examples/tradiogroup.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tradiogroup.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TRadioGroup 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_TRadioGroup_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TRadioGroup_001', 'OnClick'): on_TRadioGroup_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TRadioGroup_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('TRadioGroup', 'TRadioGroup_001', 16, 16, 150, 36) + widget.set_prop('items', 'Alpha|Beta|Gamma') + 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='TRadioGroup 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/treplacedialog.py b/examples/treplacedialog.py new file mode 100644 index 0000000..d5f5631 --- /dev/null +++ b/examples/treplacedialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 treplacedialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TReplaceDialog example' (392x292) 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_TReplaceDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TReplaceDialog_001', 'OnClick'): on_TReplaceDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TReplaceDialog_001': (True, True, False, False, 16, 16, 360, 260), +} + + +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('TReplaceDialog', 'TReplaceDialog_001', 16, 16, 360, 260) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (392, 292) +_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='TReplaceDialog example') + window.set_default_size(392, 292) + 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/tsavedialog.py b/examples/tsavedialog.py new file mode 100644 index 0000000..0cfa9d9 --- /dev/null +++ b/examples/tsavedialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tsavedialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TSaveDialog example' (332x252) 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_TSaveDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TSaveDialog_001', 'OnClick'): on_TSaveDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TSaveDialog_001': (True, True, False, False, 16, 16, 300, 220), +} + + +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('TSaveDialog', 'TSaveDialog_001', 16, 16, 300, 220) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (332, 252) +_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='TSaveDialog example') + window.set_default_size(332, 252) + 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/tsavepicturedialog.py b/examples/tsavepicturedialog.py new file mode 100644 index 0000000..42791c7 --- /dev/null +++ b/examples/tsavepicturedialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tsavepicturedialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TSavePictureDialog example' (512x272) 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_TSavePictureDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TSavePictureDialog_001', 'OnClick'): on_TSavePictureDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TSavePictureDialog_001': (True, True, False, False, 16, 16, 480, 240), +} + + +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('TSavePictureDialog', 'TSavePictureDialog_001', 16, 16, 480, 240) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (512, 272) +_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='TSavePictureDialog example') + window.set_default_size(512, 272) + 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/tscrollbar.py b/examples/tscrollbar.py new file mode 100644 index 0000000..f49a243 --- /dev/null +++ b/examples/tscrollbar.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tscrollbar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TScrollBar 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_TScrollBar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TScrollBar_001', 'OnClick'): on_TScrollBar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TScrollBar_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('TScrollBar', 'TScrollBar_001', 16, 16, 180, 30) + widget.set_prop('value', '65') + 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='TScrollBar 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/tscrollbox.py b/examples/tscrollbox.py new file mode 100644 index 0000000..80d793e --- /dev/null +++ b/examples/tscrollbox.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tscrollbox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TScrollBox 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_TScrollBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TScrollBox_001', 'OnClick'): on_TScrollBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TScrollBox_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('TScrollBox', 'TScrollBox_001', 16, 16, 200, 90) + widget.set_prop('label', 'TScrollBox_001') + 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='TScrollBox 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/tselectdirectorydialog.py b/examples/tselectdirectorydialog.py new file mode 100644 index 0000000..228f8b7 --- /dev/null +++ b/examples/tselectdirectorydialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tselectdirectorydialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TSelectDirectoryDialog example' (392x292) 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_TSelectDirectoryDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TSelectDirectoryDialog_001', 'OnClick'): on_TSelectDirectoryDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TSelectDirectoryDialog_001': (True, True, False, False, 16, 16, 360, 260), +} + + +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('TSelectDirectoryDialog', 'TSelectDirectoryDialog_001', 16, 16, 360, 260) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (392, 292) +_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='TSelectDirectoryDialog example') + window.set_default_size(392, 292) + 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/tshape.py b/examples/tshape.py new file mode 100644 index 0000000..a29dddf --- /dev/null +++ b/examples/tshape.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tshape.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TShape 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_TShape_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TShape_001', 'OnClick'): on_TShape_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TShape_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('TShape', 'TShape_001', 16, 16, 200, 90) + 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='TShape 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/tshelllistview.py b/examples/tshelllistview.py new file mode 100644 index 0000000..3e1717e --- /dev/null +++ b/examples/tshelllistview.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tshelllistview.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TShellListView example' (292x212) 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_TShellListView_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TShellListView_001', 'OnClick'): on_TShellListView_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TShellListView_001': (True, True, False, False, 16, 16, 260, 180), +} + + +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('TShellListView', 'TShellListView_001', 16, 16, 260, 180) + widget.set_prop('items', 'Alpha|Beta|Gamma') + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (292, 212) +_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='TShellListView example') + window.set_default_size(292, 212) + 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/tshelltreeview.py b/examples/tshelltreeview.py new file mode 100644 index 0000000..c00a0a4 --- /dev/null +++ b/examples/tshelltreeview.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tshelltreeview.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TShellTreeView example' (292x212) 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_TShellTreeView_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TShellTreeView_001', 'OnClick'): on_TShellTreeView_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TShellTreeView_001': (True, True, False, False, 16, 16, 260, 180), +} + + +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('TShellTreeView', 'TShellTreeView_001', 16, 16, 260, 180) + widget.set_prop('items', 'Alpha|Beta|Gamma') + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (292, 212) +_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='TShellTreeView example') + window.set_default_size(292, 212) + 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/tspeedbutton.py b/examples/tspeedbutton.py new file mode 100644 index 0000000..7860629 --- /dev/null +++ b/examples/tspeedbutton.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tspeedbutton.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TSpeedButton 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_TSpeedButton_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TSpeedButton_001', 'OnClick'): on_TSpeedButton_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TSpeedButton_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('TSpeedButton', 'TSpeedButton_001', 16, 16, 150, 36) + widget.set_prop('text', 'TSpeedButton_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='TSpeedButton 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/tspinedit.py b/examples/tspinedit.py new file mode 100644 index 0000000..44d9271 --- /dev/null +++ b/examples/tspinedit.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tspinedit.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TSpinEdit 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_TSpinEdit_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TSpinEdit_001', 'OnClick'): on_TSpinEdit_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TSpinEdit_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('TSpinEdit', 'TSpinEdit_001', 16, 16, 150, 36) + widget.set_prop('value', '65') + 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='TSpinEdit 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/tsplitter.py b/examples/tsplitter.py new file mode 100644 index 0000000..f48d394 --- /dev/null +++ b/examples/tsplitter.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tsplitter.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TSplitter 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_TSplitter_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TSplitter_001', 'OnClick'): on_TSplitter_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TSplitter_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('TSplitter', 'TSplitter_001', 16, 16, 200, 90) + 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='TSplitter 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/tstatictext.py b/examples/tstatictext.py new file mode 100644 index 0000000..23ce9bf --- /dev/null +++ b/examples/tstatictext.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tstatictext.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TStaticText 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_TStaticText_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TStaticText_001', 'OnClick'): on_TStaticText_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TStaticText_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('TStaticText', 'TStaticText_001', 16, 16, 200, 90) + widget.set_prop('label', 'TStaticText_001') + 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='TStaticText 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/tstatusbar.py b/examples/tstatusbar.py new file mode 100644 index 0000000..0ebc518 --- /dev/null +++ b/examples/tstatusbar.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tstatusbar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TStatusBar 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_TStatusBar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TStatusBar_001', 'OnClick'): on_TStatusBar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TStatusBar_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('TStatusBar', 'TStatusBar_001', 16, 16, 180, 30) + widget.set_prop('text', 'TStatusBar_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='TStatusBar 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/tstringgrid.py b/examples/tstringgrid.py new file mode 100644 index 0000000..76b011b --- /dev/null +++ b/examples/tstringgrid.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tstringgrid.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TStringGrid example' (392x232) 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_TStringGrid_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TStringGrid_001', 'OnClick'): on_TStringGrid_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TStringGrid_001': (True, True, False, False, 16, 16, 360, 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('TStringGrid', 'TStringGrid_001', 16, 16, 360, 200) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (392, 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='TStringGrid example') + window.set_default_size(392, 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/ttabcontrol.py b/examples/ttabcontrol.py new file mode 100644 index 0000000..318ea99 --- /dev/null +++ b/examples/ttabcontrol.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 ttabcontrol.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TTabControl example' (292x212) 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_TTabControl_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TTabControl_001', 'OnClick'): on_TTabControl_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TTabControl_001': (True, True, False, False, 16, 16, 260, 180), +} + + +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('TTabControl', 'TTabControl_001', 16, 16, 260, 180) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (292, 212) +_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='TTabControl example') + window.set_default_size(292, 212) + 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/ttaskdialog.py b/examples/ttaskdialog.py new file mode 100644 index 0000000..02ae706 --- /dev/null +++ b/examples/ttaskdialog.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 ttaskdialog.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TTaskDialog example' (372x312) 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_TTaskDialog_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TTaskDialog_001', 'OnClick'): on_TTaskDialog_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TTaskDialog_001': (True, True, False, False, 16, 16, 340, 280), +} + + +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('TTaskDialog', 'TTaskDialog_001', 16, 16, 340, 280) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (372, 312) +_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='TTaskDialog example') + window.set_default_size(372, 312) + 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/ttimeedit.py b/examples/ttimeedit.py new file mode 100644 index 0000000..620ad44 --- /dev/null +++ b/examples/ttimeedit.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 ttimeedit.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TTimeEdit 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_TTimeEdit_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TTimeEdit_001', 'OnClick'): on_TTimeEdit_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TTimeEdit_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('TTimeEdit', 'TTimeEdit_001', 16, 16, 180, 30) + widget.set_prop('text', 'TTimeEdit_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='TTimeEdit 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/ttimer.py b/examples/ttimer.py new file mode 100644 index 0000000..9839b30 --- /dev/null +++ b/examples/ttimer.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 ttimer.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TTimer 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_TTimer_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TTimer_001', 'OnClick'): on_TTimer_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TTimer_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('TTimer', 'TTimer_001', 16, 16, 200, 90) + 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='TTimer 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/ttogglebox.py b/examples/ttogglebox.py new file mode 100644 index 0000000..93eb5b6 --- /dev/null +++ b/examples/ttogglebox.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 ttogglebox.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TToggleBox 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_TToggleBox_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TToggleBox_001', 'OnClick'): on_TToggleBox_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TToggleBox_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('TToggleBox', 'TToggleBox_001', 16, 16, 150, 36) + widget.set_prop('text', 'TToggleBox_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='TToggleBox 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/ttoolbar.py b/examples/ttoolbar.py new file mode 100644 index 0000000..c77d72c --- /dev/null +++ b/examples/ttoolbar.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 ttoolbar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TToolBar 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_TToolBar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TToolBar_001', 'OnClick'): on_TToolBar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TToolBar_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('TToolBar', 'TToolBar_001', 16, 16, 200, 90) + widget.set_prop('items', 'Alpha|Beta|Gamma') + 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='TToolBar 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/ttrackbar.py b/examples/ttrackbar.py new file mode 100644 index 0000000..62c2920 --- /dev/null +++ b/examples/ttrackbar.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 ttrackbar.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TTrackBar 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_TTrackBar_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TTrackBar_001', 'OnClick'): on_TTrackBar_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TTrackBar_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('TTrackBar', 'TTrackBar_001', 16, 16, 200, 90) + 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='TTrackBar 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/ttrayicon.py b/examples/ttrayicon.py new file mode 100644 index 0000000..eab44af --- /dev/null +++ b/examples/ttrayicon.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 ttrayicon.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TTrayIcon 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_TTrayIcon_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TTrayIcon_001', 'OnClick'): on_TTrayIcon_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TTrayIcon_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('TTrayIcon', 'TTrayIcon_001', 16, 16, 200, 90) + 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='TTrayIcon 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/ttreeview.py b/examples/ttreeview.py new file mode 100644 index 0000000..a5e69fd --- /dev/null +++ b/examples/ttreeview.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 ttreeview.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TTreeView example' (292x212) 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_TTreeView_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TTreeView_001', 'OnClick'): on_TTreeView_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TTreeView_001': (True, True, False, False, 16, 16, 260, 180), +} + + +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('TTreeView', 'TTreeView_001', 16, 16, 260, 180) + widget.set_prop('items', 'Alpha|Beta|Gamma') + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (292, 212) +_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='TTreeView example') + window.set_default_size(292, 212) + 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/tupdown.py b/examples/tupdown.py new file mode 100644 index 0000000..cf95608 --- /dev/null +++ b/examples/tupdown.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tupdown.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TUpDown 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_TUpDown_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TUpDown_001', 'OnClick'): on_TUpDown_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TUpDown_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('TUpDown', 'TUpDown_001', 16, 16, 150, 36) + widget.set_prop('value', '65') + 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='TUpDown 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/tvaluelisteditor.py b/examples/tvaluelisteditor.py new file mode 100644 index 0000000..79f0f8b --- /dev/null +++ b/examples/tvaluelisteditor.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 tvaluelisteditor.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'TValueListEditor 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_TValueListEditor_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('TValueListEditor_001', 'OnClick'): on_TValueListEditor_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'TValueListEditor_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('TValueListEditor', 'TValueListEditor_001', 16, 16, 180, 30) + 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='TValueListEditor 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/xgraph.py b/examples/xgraph.py new file mode 100644 index 0000000..a1786ac --- /dev/null +++ b/examples/xgraph.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 xgraph.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'XGraph example' (352x232) 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_XGraph_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('XGraph_001', 'OnClick'): on_XGraph_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'XGraph_001': (True, True, False, False, 16, 16, 320, 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('XGraph', 'XGraph_001', 16, 16, 320, 200) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (352, 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='XGraph example') + window.set_default_size(352, 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/xspreadsheet.py b/examples/xspreadsheet.py new file mode 100644 index 0000000..0dddd75 --- /dev/null +++ b/examples/xspreadsheet.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# Generated by LightGUI (Python/XFrame). +# A plain Python/GTK program: no CMake. Run with: +# python3 xspreadsheet.py +# with the LightGUI Python package on PYTHONPATH +# (the designer sets it when it runs this script). +# +# The window is 'XSpreadsheet example' (352x232) 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_XSpreadsheet_001_OnClick(widget): + print("EVENT %s %s" % (widget.name, 'OnClick')) + +# Event handlers, wired to the widgets in main(): +_HANDLERS = { + ('XSpreadsheet_001', 'OnClick'): on_XSpreadsheet_001_OnClick, +} + + +# Design-time geometry + effective anchors per widget: +# name -> (top, left, right, bottom, x, y, width, height) +_ANCHORS = { + 'XSpreadsheet_001': (True, True, False, False, 16, 16, 320, 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('XSpreadsheet', 'XSpreadsheet_001', 16, 16, 320, 200) + widgets.append(widget) + return widgets + + +_DESIGN_SIZE = (352, 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='XSpreadsheet example') + window.set_default_size(352, 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())