From 4488d527a52acab7c1cb2c8bf0abd544cc7e648b Mon Sep 17 00:00:00 2001 From: Muse Date: Sat, 5 Dec 2026 16:49:02 +0000 Subject: [PATCH] XFrame monitor widgets: XUsageBar, XStackBar, XVerticalBar, XTachometer, XArcGauge, XDonutGauge Gradient usage bar (green->yellow->red, absolute scale), stacked multi-segment bar with legend, vertical bar meter with scale + peak-hold marker (click resets peak), round tachometer (needle, redline zone, digital readout, peak hold), semicircular arc gauge with coloured zones + big %, circular progress ring with centred %. Header-only C++, demo mains, examples, Xvfb screenshots. --- examples/XArcGauge.cpp | 73 ++ examples/XDonutGauge.cpp | 73 ++ examples/XStackBar.cpp | 73 ++ examples/XTachometer.cpp | 73 ++ examples/XUsageBar.cpp | 73 ++ examples/XVerticalBar.cpp | 73 ++ widgets/components/XArcGauge/CMakeLists.txt | 13 + widgets/components/XArcGauge/XArcGauge.h | 188 ++++ .../components/XArcGauge/core/xframe_core.h | 988 ++++++++++++++++++ .../components/XArcGauge/main_XArcGauge.cpp | 69 ++ widgets/components/XDonutGauge/CMakeLists.txt | 13 + widgets/components/XDonutGauge/XDonutGauge.h | 164 +++ .../components/XDonutGauge/core/xframe_core.h | 988 ++++++++++++++++++ .../XDonutGauge/main_XDonutGauge.cpp | 69 ++ widgets/components/XStackBar/CMakeLists.txt | 13 + widgets/components/XStackBar/XStackBar.h | 209 ++++ .../components/XStackBar/core/xframe_core.h | 988 ++++++++++++++++++ .../components/XStackBar/main_XStackBar.cpp | 69 ++ widgets/components/XTachometer/CMakeLists.txt | 13 + widgets/components/XTachometer/XTachometer.h | 244 +++++ .../components/XTachometer/core/xframe_core.h | 988 ++++++++++++++++++ .../XTachometer/main_XTachometer.cpp | 69 ++ widgets/components/XUsageBar/CMakeLists.txt | 13 + widgets/components/XUsageBar/XUsageBar.h | 173 +++ .../components/XUsageBar/core/xframe_core.h | 988 ++++++++++++++++++ .../components/XUsageBar/main_XUsageBar.cpp | 69 ++ .../components/XVerticalBar/CMakeLists.txt | 13 + .../components/XVerticalBar/XVerticalBar.h | 201 ++++ .../XVerticalBar/core/xframe_core.h | 988 ++++++++++++++++++ .../XVerticalBar/main_XVerticalBar.cpp | 69 ++ widgets/screenshots/XArcGauge.png | Bin 0 -> 2656 bytes widgets/screenshots/XDonutGauge.png | Bin 0 -> 3179 bytes widgets/screenshots/XStackBar.png | Bin 0 -> 1924 bytes widgets/screenshots/XTachometer.png | Bin 0 -> 3336 bytes widgets/screenshots/XUsageBar.png | Bin 0 -> 2512 bytes widgets/screenshots/XVerticalBar.png | Bin 0 -> 1448 bytes 36 files changed, 8037 insertions(+) create mode 100644 examples/XArcGauge.cpp create mode 100644 examples/XDonutGauge.cpp create mode 100644 examples/XStackBar.cpp create mode 100644 examples/XTachometer.cpp create mode 100644 examples/XUsageBar.cpp create mode 100644 examples/XVerticalBar.cpp create mode 100644 widgets/components/XArcGauge/CMakeLists.txt create mode 100644 widgets/components/XArcGauge/XArcGauge.h create mode 100644 widgets/components/XArcGauge/core/xframe_core.h create mode 100644 widgets/components/XArcGauge/main_XArcGauge.cpp create mode 100644 widgets/components/XDonutGauge/CMakeLists.txt create mode 100644 widgets/components/XDonutGauge/XDonutGauge.h create mode 100644 widgets/components/XDonutGauge/core/xframe_core.h create mode 100644 widgets/components/XDonutGauge/main_XDonutGauge.cpp create mode 100644 widgets/components/XStackBar/CMakeLists.txt create mode 100644 widgets/components/XStackBar/XStackBar.h create mode 100644 widgets/components/XStackBar/core/xframe_core.h create mode 100644 widgets/components/XStackBar/main_XStackBar.cpp create mode 100644 widgets/components/XTachometer/CMakeLists.txt create mode 100644 widgets/components/XTachometer/XTachometer.h create mode 100644 widgets/components/XTachometer/core/xframe_core.h create mode 100644 widgets/components/XTachometer/main_XTachometer.cpp create mode 100644 widgets/components/XUsageBar/CMakeLists.txt create mode 100644 widgets/components/XUsageBar/XUsageBar.h create mode 100644 widgets/components/XUsageBar/core/xframe_core.h create mode 100644 widgets/components/XUsageBar/main_XUsageBar.cpp create mode 100644 widgets/components/XVerticalBar/CMakeLists.txt create mode 100644 widgets/components/XVerticalBar/XVerticalBar.h create mode 100644 widgets/components/XVerticalBar/core/xframe_core.h create mode 100644 widgets/components/XVerticalBar/main_XVerticalBar.cpp create mode 100644 widgets/screenshots/XArcGauge.png create mode 100644 widgets/screenshots/XDonutGauge.png create mode 100644 widgets/screenshots/XStackBar.png create mode 100644 widgets/screenshots/XTachometer.png create mode 100644 widgets/screenshots/XUsageBar.png create mode 100644 widgets/screenshots/XVerticalBar.png diff --git a/examples/XArcGauge.cpp b/examples/XArcGauge.cpp new file mode 100644 index 0000000..73bfeed --- /dev/null +++ b/examples/XArcGauge.cpp @@ -0,0 +1,73 @@ +// XArcGauge -- minimal XFrame example (C++). +// Build: g++ -std=c++17 -I/root/lg-review/lightgui/widgets/components -I/root/lg-review/lightgui/widgets XArcGauge.cpp -lX11 -o XArcGauge +// Run under Xvfb (or a real X server); close the window to quit. +// Setup mirrors the widget's own demo in widgets/components/XArcGauge/main_XArcGauge.cpp. +#include "XArcGauge/XArcGauge.h" + +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XArcGauge - XFrame demo"); + XSelectInput(display, window, ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask); + Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False); + XSetWMProtocols(display, window, &deleteWindowMessage, 1); + XMapWindow(display, window); + GC gc = DefaultGC(display, screen); + SStyle_Manager styles(display, window, screen, gc); + styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120}, Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22}, Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.set_active_style_byName("default"); + XArcGauge widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + while (running) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.on_motion(event.xmotion.x, event.xmotion.y); + break; + case KeyPress: + { + char buffer[32]; + KeySym key = 0; + int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr); + buffer[n] = 0; + handled = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + } + + XCloseDisplay(display); + return 0; +} diff --git a/examples/XDonutGauge.cpp b/examples/XDonutGauge.cpp new file mode 100644 index 0000000..4805565 --- /dev/null +++ b/examples/XDonutGauge.cpp @@ -0,0 +1,73 @@ +// XDonutGauge -- minimal XFrame example (C++). +// Build: g++ -std=c++17 -I/root/lg-review/lightgui/widgets/components -I/root/lg-review/lightgui/widgets XDonutGauge.cpp -lX11 -o XDonutGauge +// Run under Xvfb (or a real X server); close the window to quit. +// Setup mirrors the widget's own demo in widgets/components/XDonutGauge/main_XDonutGauge.cpp. +#include "XDonutGauge/XDonutGauge.h" + +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XDonutGauge - XFrame demo"); + XSelectInput(display, window, ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask); + Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False); + XSetWMProtocols(display, window, &deleteWindowMessage, 1); + XMapWindow(display, window); + GC gc = DefaultGC(display, screen); + SStyle_Manager styles(display, window, screen, gc); + styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120}, Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22}, Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.set_active_style_byName("default"); + XDonutGauge widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + while (running) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.on_motion(event.xmotion.x, event.xmotion.y); + break; + case KeyPress: + { + char buffer[32]; + KeySym key = 0; + int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr); + buffer[n] = 0; + handled = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + } + + XCloseDisplay(display); + return 0; +} diff --git a/examples/XStackBar.cpp b/examples/XStackBar.cpp new file mode 100644 index 0000000..df3def7 --- /dev/null +++ b/examples/XStackBar.cpp @@ -0,0 +1,73 @@ +// XStackBar -- minimal XFrame example (C++). +// Build: g++ -std=c++17 -I/root/lg-review/lightgui/widgets/components -I/root/lg-review/lightgui/widgets XStackBar.cpp -lX11 -o XStackBar +// Run under Xvfb (or a real X server); close the window to quit. +// Setup mirrors the widget's own demo in widgets/components/XStackBar/main_XStackBar.cpp. +#include "XStackBar/XStackBar.h" + +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XStackBar - XFrame demo"); + XSelectInput(display, window, ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask); + Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False); + XSetWMProtocols(display, window, &deleteWindowMessage, 1); + XMapWindow(display, window); + GC gc = DefaultGC(display, screen); + SStyle_Manager styles(display, window, screen, gc); + styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120}, Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22}, Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.set_active_style_byName("default"); + XStackBar widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + while (running) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.on_motion(event.xmotion.x, event.xmotion.y); + break; + case KeyPress: + { + char buffer[32]; + KeySym key = 0; + int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr); + buffer[n] = 0; + handled = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + } + + XCloseDisplay(display); + return 0; +} diff --git a/examples/XTachometer.cpp b/examples/XTachometer.cpp new file mode 100644 index 0000000..f634844 --- /dev/null +++ b/examples/XTachometer.cpp @@ -0,0 +1,73 @@ +// XTachometer -- minimal XFrame example (C++). +// Build: g++ -std=c++17 -I/root/lg-review/lightgui/widgets/components -I/root/lg-review/lightgui/widgets XTachometer.cpp -lX11 -o XTachometer +// Run under Xvfb (or a real X server); close the window to quit. +// Setup mirrors the widget's own demo in widgets/components/XTachometer/main_XTachometer.cpp. +#include "XTachometer/XTachometer.h" + +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XTachometer - XFrame demo"); + XSelectInput(display, window, ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask); + Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False); + XSetWMProtocols(display, window, &deleteWindowMessage, 1); + XMapWindow(display, window); + GC gc = DefaultGC(display, screen); + SStyle_Manager styles(display, window, screen, gc); + styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120}, Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22}, Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.set_active_style_byName("default"); + XTachometer widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + while (running) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.on_motion(event.xmotion.x, event.xmotion.y); + break; + case KeyPress: + { + char buffer[32]; + KeySym key = 0; + int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr); + buffer[n] = 0; + handled = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + } + + XCloseDisplay(display); + return 0; +} diff --git a/examples/XUsageBar.cpp b/examples/XUsageBar.cpp new file mode 100644 index 0000000..08f883c --- /dev/null +++ b/examples/XUsageBar.cpp @@ -0,0 +1,73 @@ +// XUsageBar -- minimal XFrame example (C++). +// Build: g++ -std=c++17 -I/root/lg-review/lightgui/widgets/components -I/root/lg-review/lightgui/widgets XUsageBar.cpp -lX11 -o XUsageBar +// Run under Xvfb (or a real X server); close the window to quit. +// Setup mirrors the widget's own demo in widgets/components/XUsageBar/main_XUsageBar.cpp. +#include "XUsageBar/XUsageBar.h" + +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XUsageBar - XFrame demo"); + XSelectInput(display, window, ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask); + Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False); + XSetWMProtocols(display, window, &deleteWindowMessage, 1); + XMapWindow(display, window); + GC gc = DefaultGC(display, screen); + SStyle_Manager styles(display, window, screen, gc); + styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120}, Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22}, Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.set_active_style_byName("default"); + XUsageBar widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + while (running) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.on_motion(event.xmotion.x, event.xmotion.y); + break; + case KeyPress: + { + char buffer[32]; + KeySym key = 0; + int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr); + buffer[n] = 0; + handled = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + } + + XCloseDisplay(display); + return 0; +} diff --git a/examples/XVerticalBar.cpp b/examples/XVerticalBar.cpp new file mode 100644 index 0000000..2b66a5e --- /dev/null +++ b/examples/XVerticalBar.cpp @@ -0,0 +1,73 @@ +// XVerticalBar -- minimal XFrame example (C++). +// Build: g++ -std=c++17 -I/root/lg-review/lightgui/widgets/components -I/root/lg-review/lightgui/widgets XVerticalBar.cpp -lX11 -o XVerticalBar +// Run under Xvfb (or a real X server); close the window to quit. +// Setup mirrors the widget's own demo in widgets/components/XVerticalBar/main_XVerticalBar.cpp. +#include "XVerticalBar/XVerticalBar.h" + +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XVerticalBar - XFrame demo"); + XSelectInput(display, window, ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask); + Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False); + XSetWMProtocols(display, window, &deleteWindowMessage, 1); + XMapWindow(display, window); + GC gc = DefaultGC(display, screen); + SStyle_Manager styles(display, window, screen, gc); + styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120}, Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22}, Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.set_active_style_byName("default"); + XVerticalBar widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + while (running) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.on_motion(event.xmotion.x, event.xmotion.y); + break; + case KeyPress: + { + char buffer[32]; + KeySym key = 0; + int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr); + buffer[n] = 0; + handled = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + } + + XCloseDisplay(display); + return 0; +} diff --git a/widgets/components/XArcGauge/CMakeLists.txt b/widgets/components/XArcGauge/CMakeLists.txt new file mode 100644 index 0000000..eb5de81 --- /dev/null +++ b/widgets/components/XArcGauge/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.20) + +project(XArcGauge LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) + +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(X11 REQUIRED) + +add_executable(XArcGauge main_XArcGauge.cpp) + +target_link_libraries(XArcGauge PRIVATE X11::X11) diff --git a/widgets/components/XArcGauge/XArcGauge.h b/widgets/components/XArcGauge/XArcGauge.h new file mode 100644 index 0000000..ac4c8ab --- /dev/null +++ b/widgets/components/XArcGauge/XArcGauge.h @@ -0,0 +1,188 @@ +#pragma once + +#include "core/xframe_core.h" + +#include +#include +#include + +// region XArcGauge +// +// A semicircular arc gauge: a 180-degree arc with green / yellow / red +// zones and a big % in the middle. Raw X11, same as the rest of the +// XFrame set. +// +class XArcGauge +{ +private: + Display* display_; + int screen_; + Window window_; + GC gc_; + SStyle_Manager* styles_; + int x1_, y1_, x2_, y2_; + std::string name_; + std::string text_; + std::string style_name_; + bool visible_ = true; + bool enabled_ = true; + bool focused_ = false; + + double value_ = 0.0; // 0..100 + double warn_ = 70.0; // yellow starts here + double crit_ = 90.0; // red starts here + + Srgb white_color_{255, 255, 255}; + Srgb axis_color_{150, 150, 150}; + Srgb track_color_{230, 230, 230}; + Srgb ok_color_{0, 170, 70}; + Srgb warn_color_{220, 180, 0}; + Srgb crit_color_{210, 40, 30}; + + unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); } + + void draw_text_color(int x, int baseline_y, const std::string& s, Srgb color) + { + SStyle st = style(); + if (!st.font_ || s.empty()) return; + XSetFont(display_, gc_, st.font_->fid); + XSetForeground(display_, gc_, col(color)); + XDrawString(display_, window_, gc_, x, baseline_y, s.c_str(), (int)s.size()); + } + + // Draw a thick arc slice of the upper semicircle: frac0..frac1 in 0..1. + void arc_slice(int cx, int cy, int r, double frac0, double frac1, Srgb color, int width) + { + // X angles: 0 deg = 3 o'clock, counter-clockwise. Upper semicircle + // runs 0..180 deg; value 0 is at 180 deg (9 o'clock), 1 at 0 deg. + int a1 = (int)((1.0 - frac1) * 180.0 * 64); + int a0 = (int)((1.0 - frac0) * 180.0 * 64); + XSetForeground(display_, gc_, col(color)); + xf::set_line_width(display_, gc_, width); + XDrawArc(display_, window_, gc_, cx - r, cy - r, 2 * r, 2 * r, a1, a0 - a1); + xf::set_line_width(display_, gc_, 1); + } + +public: + XArcGauge(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height) + { + display_ = display; + screen_ = screen; + window_ = window; + gc_ = gc; + styles_ = styles; + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + name_ = "XArcGauge"; + text_ = "CPU"; + value_ = 63.0; + } + + ~XArcGauge() {} + + // region Standard plumbing + void set_bounds(int x, int y, int width, int height) + { + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + } + int width() { return x2_ - x1_; } + int height() { return y2_ - y1_; } + bool contains(int x, int y) + { + return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_; + } + void set_visible(bool v) { visible_ = v; } + bool get_visible() { return visible_; } + void set_enabled(bool e) { enabled_ = e; } + bool get_enabled() { return enabled_; } + void set_style_name(std::string s) { style_name_ = s; } + void set_text(std::string t) { text_ = t; } + std::string get_text() { return text_; } + SStyle style() { return styles_->resolve(style_name_); } + // endregion + + // region Gauge API + void set_value(double v) { value_ = std::max(0.0, std::min(100.0, v)); } + double get_value() { return value_; } + void set_zones(double warn, double crit) + { + warn_ = std::max(0.0, std::min(100.0, warn)); + crit_ = std::max(warn_, std::min(100.0, crit)); + } + // endregion + + void draw() + { + if (!visible_) return; + SStyle st = style(); + int w = width(); + int h = height(); + XSetForeground(display_, gc_, col(white_color_)); + XFillRectangle(display_, window_, gc_, x1_, y1_, w, h); + + const int title_h = 20; + int r = std::max(20, std::min(w / 2 - 20, h - title_h - 40)); + int cx = x1_ + w / 2; + int cy = y1_ + title_h + r + 8; + double frac = value_ / 100.0; + double wfrac = warn_ / 100.0; + double cfrac = crit_ / 100.0; + + xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_); + + // Track + coloured zones. + arc_slice(cx, cy, r, 0.0, 1.0, track_color_, 14); + arc_slice(cx, cy, r, 0.0, std::min(frac, wfrac), ok_color_, 14); + if (frac > wfrac) arc_slice(cx, cy, r, wfrac, std::min(frac, cfrac), warn_color_, 14); + if (frac > cfrac) arc_slice(cx, cy, r, cfrac, frac, crit_color_, 14); + + // End labels 0 / 100. + draw_text_color(cx - r - 4, cy + 16, "0", Srgb{100, 100, 100}); + draw_text_color(cx + r - 12, cy + 16, "100", Srgb{100, 100, 100}); + + // Big % in the middle. + char pct[16]; + std::snprintf(pct, sizeof(pct), "%.0f%%", value_); + Srgb pct_color = value_ >= crit_ ? crit_color_ : (value_ >= warn_ ? warn_color_ : ok_color_); + int pw = xf::text_width(st, pct); + // Fake a larger readout by drawing the text twice offset by 1 px. + draw_text_color(cx - pw / 2, cy - 8, pct, pct_color); + draw_text_color(cx - pw / 2 + 1, cy - 8, pct, pct_color); + + XSetForeground(display_, gc_, st.lineColor_); + XDrawRectangle(display_, window_, gc_, x1_, y1_, w, h); + XFlush(display_); + } + + bool on_button_press(int x, int y, unsigned int button) + { + (void)x; + (void)y; + (void)button; + return false; + } + bool on_button_release(int x, int y, unsigned int button) + { + (void)x; + (void)y; + (void)button; + return false; + } + bool on_motion(int x, int y) + { + (void)x; + (void)y; + return false; + } + bool on_key(KeySym key, const char* str) + { + (void)key; + (void)str; + return false; + } +}; diff --git a/widgets/components/XArcGauge/core/xframe_core.h b/widgets/components/XArcGauge/core/xframe_core.h new file mode 100644 index 0000000..098187d --- /dev/null +++ b/widgets/components/XArcGauge/core/xframe_core.h @@ -0,0 +1,988 @@ +#pragma once +#ifndef XFRAME_CORE_H +#define XFRAME_CORE_H + +// +// xframe_core.h - shared core for the XFrame widget library. +// +// Everything in here follows the format of the "Writing Your Own +// X-11 Framework Basics" articles (hotconfig.com): +// +// Part 1 - Display / Screen / Window boilerplate, the GC as a +// "pen", the drawing primitives, and the Canvas class. +// Part 2 - widgets are standalone classes that record display_, +// screen_, window_ and gc_ at construction time. +// Part 3 - SStyle_Manager tracks named styles in a vector. +// Part 4 - a polished widget is built from primitives: two fill +// rectangles + four fill arcs make a rounded body, four +// lines + four draw arcs make its outline, and text is +// centred with XTextWidth + font ascent / descent. +// +// Two small completions versus the articles, needed by Part 4's own +// draw code: SStyle gains lineColor_ and text_color_, and the style +// manager gains the delete_style() function Part 3 left as a reader +// challenge, plus bounds-safe getters. +// + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +//region SStyle + +struct Srgb + +{ + + int r; + + int g; + + int b; + +}; + +struct SStyle + +{ + + unsigned long fillColor_ = 0; + + unsigned long outlineColor_ = 0; + + unsigned long lineColor_ = 0; + + unsigned long text_color_ = 0; + + unsigned long radius_ = 0; + + unsigned long lineWidth_ = 1; + + XFontStruct *font_ = nullptr; + + std::string style_name_; + +}; + +//endregion + +//region SStyle_Manager + +class SStyle_Manager + +{ + +private: + + std::vector styles_; + + Display *display_; + + Window window_; + + int screen_; + + GC gc_; + + int active_style = 0; // just set the default to first passed. + +public: + + SStyle_Manager(Display *display, Window window, int screen, GC gc) + + { + + display_ = display; + + window_ = window; + + screen_ = screen; + + gc_ = gc; + + } + + // NOTE (test agent, Nov 26 2026): do NOT XFreeFont here. Every demo + // main calls XCloseDisplay() before this destructor runs (stack + // unwinding after main returns), so XFreeFont would operate on a + // closed Display and corrupt the heap (SIGSEGV/SIGABRT inside + // XFreeFont on every clean exit). The fonts are process-lifetime + // resources; the X server reclaims them when the client disconnects. + // Use delete_style() to free a single style while the display is + // still open. + ~SStyle_Manager() + + { + + styles_.clear(); + + } + + int add_style(std::string styleName, Srgb fillColor, Srgb outlineColor, int radius, int linewidth, std::string fontName) + + { + + return add_style_ex(styleName, fillColor, outlineColor, outlineColor, Srgb{0, 0, 0}, radius, linewidth, fontName); + + } + + int add_style_ex(std::string styleName, Srgb fillColor, Srgb outlineColor, Srgb lineColor, Srgb textColor, int radius, int linewidth, std::string fontName) + + { + + SStyle nstyle; + + nstyle.style_name_ = std::move(styleName); + + nstyle.fillColor_ = get_color_Srgb(fillColor); + + nstyle.outlineColor_ = get_color_Srgb(outlineColor); + + nstyle.lineColor_ = get_color_Srgb(lineColor); + + nstyle.text_color_ = get_color_Srgb(textColor); + + nstyle.radius_ = radius; + + nstyle.lineWidth_ = linewidth; + + // Define fallback fonts to try if the requested font fails + + const char* fallbackFonts[] = { "fixed", "helvetica" }; + + size_t numFallbacks = sizeof(fallbackFonts) / sizeof(*fallbackFonts); + + nstyle.font_ = nullptr; + + // Attempt to load the primary font (if provided and not empty) + + if (!fontName.empty()) { + + nstyle.font_ = XLoadQueryFont(display_, fontName.c_str()); + + } + + // If the primary failed, iterate through fallback fonts until one succeeds or all are exhausted + + if (!nstyle.font_) { + + for (size_t i = 0; i < numFallbacks && !nstyle.font_; ++i) { + + nstyle.font_ = XLoadQueryFont(display_, fallbackFonts[i]); + + } + + } + + // Final validation: if no font loaded, report which fonts were attempted and exit + + if (!nstyle.font_) + + { + + printf("Failed to load any font (tried: "); + + if (!fontName.empty()) printf("%s", fontName.c_str()); + + for (size_t i = 0; i < numFallbacks; ++i) { + + if (!fontName.empty() || i > 0) printf(", "); + + printf("%s", fallbackFonts[i]); + + } + + printf(")\n"); + + return -1; + + } + + styles_.push_back(nstyle); + + return 0; + + } + + // The delete function Part 3 left as a challenge: remove a style + // by name, freeing its font, and keep active_style in range. + + int delete_style(std::string styleName) + + { + + for (size_t i = 0; i < styles_.size(); ++i) + + { + + if (styles_[i].style_name_ == styleName) + + { + + if (styles_[i].font_) + + { + + XFreeFont(display_, styles_[i].font_); + + } + + styles_.erase(styles_.begin() + static_cast(i)); + + if (active_style >= (int)styles_.size()) + + { + + active_style = styles_.empty() ? 0 : (int)styles_.size() - 1; + + } + + return 0; + + } + + } + + return -1; // Not found + + } + + int style_count() + + { + + return (int)styles_.size(); + + } + + unsigned long get_color_Srgb(Srgb color) + + { + + XColor xcolor; + + xcolor.red = color.r << 8; + + xcolor.green = color.g << 8; + + xcolor.blue = color.b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", color.r, color.g, color.b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + unsigned long get_color_rgb(unsigned char r, unsigned char g, unsigned char b) + + { + + XColor xcolor; + + xcolor.red = r << 8; + + xcolor.green = g << 8; + + xcolor.blue = b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", r, g, b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + int set_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + active_style = static_cast(std::distance(styles_.begin(), it)); + + return active_style; + + } + + return -1; // Not found + + } + + SStyle get_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + return *it; + + } + + return get_active_style(); + + } + + SStyle get_active_style() + + { + + if (styles_.empty()) + + { + + return SStyle{}; + + } + + if (active_style < 0 || active_style >= (int)styles_.size()) + + { + + active_style = 0; + + } + + return styles_[active_style]; + + } + + SStyle get_index_style(int index) + + { + + if (index >= 0 && index < (int)styles_.size()) + + { + + return styles_[index]; + + } + + return get_active_style(); + + } + + // Resolve the style a widget should draw with: its own named + // style when set, otherwise the manager's active style. + + SStyle resolve(const std::string& styleName) + + { + + if (!styleName.empty()) + + { + + return get_active_style_byName(styleName); + + } + + return get_active_style(); + + } + +}; + +//endregion + +//region Drawing helpers (Part 4 construction, shared by all widgets) + +namespace xf + +{ + + inline void set_line_width(Display* display, GC gc, int lineWidth) + + { + + unsigned long valuemask = GCLineWidth; + + XGCValues gc_values; + + gc_values.line_width = lineWidth; + + XChangeGC(display, gc, valuemask, &gc_values); + + } + + // The Part 4 rounded body: Rectangle A + Rectangle B + 4 fill arcs. + + inline void fill_rounded_rect(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + int w = x2 - x1; + + int h = y2 - y1; + + if (r <= 0 || w <= 2 * r || h <= 2 * r) + + { + + XFillRectangle(display, window, gc, x1, y1, w, h); + + return; + + } + + XSetForeground(display, gc, style.fillColor_); + + //Rectangle A + + XFillRectangle(display, window, gc, x1 + r, y1, w - 2 * r, h); + + //Rectangle B + + XFillRectangle(display, window, gc, x1, y1 + r, w, h - 2 * r); + + //region Arc C Top Left + + XFillArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XFillArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XFillArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XFillArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // The Part 4 outline: 4 line bars + 4 draw arcs, in lineColor_. + + inline void draw_rounded_outline(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + XSetForeground(display, gc, style.lineColor_); + + set_line_width(display, gc, (int)style.lineWidth_); + + if (r <= 0) + + { + + XDrawRectangle(display, window, gc, x1, y1, x2 - x1, y2 - y1); + + return; + + } + + // 4 Line Bars + + XDrawLine(display, window, gc, x1 + r, y1, x2 - r, y1); + + XDrawLine(display, window, gc, x2, y1 + r, x2, y2 - r); + + XDrawLine(display, window, gc, x1, y1 + r, x1, y2 - r); + + XDrawLine(display, window, gc, x1 + r, y2, x2 - r, y2); + + // 4 Arcs + + XDrawArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XDrawArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XDrawArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XDrawArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // Convenience: body + outline in one call, the polished look. + + inline void draw_polished(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + fill_rounded_rect(display, window, gc, style, x1, y1, x2, y2); + + draw_rounded_outline(display, window, gc, style, x1, y1, x2, y2); + + } + + inline int text_width(const SStyle& style, const std::string& text) + + { + + if (!style.font_) + + { + + return (int)text.size() * 8; + + } + + return XTextWidth(style.font_, text.c_str(), (int)text.length()); + + } + + // Part 4 centred text: XTextWidth + ascent / descent. + + inline void draw_text_centered(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int tw = XTextWidth(style.font_, text.c_str(), (int)text.length()); + + int text_x = x1 + ((x2 - x1) - tw) / 2; + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, text_x, text_y, text.c_str(), (int)text.length()); + + } + + // Left-aligned text, vertically centred in the given band. + + inline void draw_text_left(Display* display, Window window, GC gc, const SStyle& style, int x, int y1, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, x, text_y, text.c_str(), (int)text.length()); + + } + + // Baseline text at an explicit position (labels, grid cells...). + + inline void draw_text_at(Display* display, Window window, GC gc, const SStyle& style, int x, int baseline_y, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + XDrawString(display, window, gc, x, baseline_y, text.c_str(), (int)text.length()); + + } + + inline void draw_checkmark(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + set_line_width(display, gc, 2); + + int s = size / 2; + + XDrawLine(display, window, gc, cx - s, cy, cx - s / 3, cy + s); + + XDrawLine(display, window, gc, cx - s / 3, cy + s, cx + s, cy - s); + + set_line_width(display, gc, 1); + + } + + inline void draw_triangle_down(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy + i, cx + (size - i), cy + i); + + } + + } + + inline void draw_triangle_up(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy - i, cx + (size - i), cy - i); + + } + + } + + inline void draw_triangle_right(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx + i, cy - (size - i), cx + i, cy + (size - i)); + + } + + } + +} + +//endregion + +//region Region Canvas (from Part 1) + +class Canvas + +{ + +private: + + Display* display = nullptr; + + Window window = 0; + + GC gc = nullptr; + + XImage* image = nullptr; + + int width = 0; + + int height = 0; + + std::vector pixels; + +public: + + Canvas(Display* display, Window window, GC graphicsContext, int width, int height) : display(display), window(window), gc(graphicsContext) + + { + + resize(width, height); + + } + + ~Canvas() { destroyImage(); } + + void resize(int newWidth, int newHeight) + + { + + width = std::max(1, newWidth); + + height = std::max(1, newHeight); + + pixels.resize(static_cast(width) * height); + + clear(0x202020); + + destroyImage(); + + Visual* visual = DefaultVisual(display, DefaultScreen(display)); + + int depth = DefaultDepth(display, DefaultScreen(display)); + + image = XCreateImage(display, visual, depth, ZPixmap, 0, nullptr, width, height, 32, 0); + + if (!image) + + { + + std::exit(1); + + } + + image->data = static_cast(std::malloc(image->bytes_per_line * height)); + + if (!image->data) + + { + + std::exit(1); + + } + + } + + int getWidth() const + + { + + return width; + + } + + int getHeight() const + + { + + return height; + + } + + // Direct access to the raw framebuffer. + + uint32_t* data() + + { + + return pixels.data(); + + } + + const uint32_t* data() const + + { + + return pixels.data(); + + } + + void clear(uint32_t rgb) + + { + + std::fill(pixels.begin(), pixels.end(), rgb); + + } + + void setPixel(int x, int y, uint32_t rgb) + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return; + + } + + pixels[static_cast(y) * width + x] = rgb; + + } + + uint32_t getPixel(int x, int y) const + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return 0; + + } + + return pixels[static_cast(y) * width + x]; + + } + + void fillRect(int x, int y, int w, int h, uint32_t rgb) + + { + + int x0 = std::max(0, x); + + int y0 = std::max(0, y); + + int x1 = std::min(width, x + w); + + int y1 = std::min(height, y + h); + + for (int py = y0; py < y1; ++py) + + { + + for (int px = x0; px < x1; ++px) + + { + + setPixel(px, py, rgb); + + } + + } + + } + + void drawGradient() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint8_t red = static_cast(255.0 * x / std::max(1, width - 1)); + + uint8_t green = static_cast(255.0 * y / std::max(1, height - 1)); + + uint8_t blue = 100; + + uint32_t color = (static_cast(red) << 16) | (static_cast(green) << 8) | blue; + + setPixel(x, y, color); + + } + + } + + } + + void present() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint32_t rgb = getPixel(x, y); + + unsigned long x11Pixel = ((rgb >> 16) & 0xff) << 16 | ((rgb >> 8) & 0xff) << 8 | (rgb & 0xff); + + XPutPixel(image, x, y, x11Pixel); + + } + + } + + XPutImage(display, window, gc, image, 0, 0, 0, 0, width, height); + + XFlush(display); + + } + +private: + + void destroyImage() + + { + + if (image) + + { + + XDestroyImage(image); + + image = nullptr; + + } + + } + +}; + +//endregion +#endif // XFRAME_CORE_H diff --git a/widgets/components/XArcGauge/main_XArcGauge.cpp b/widgets/components/XArcGauge/main_XArcGauge.cpp new file mode 100644 index 0000000..ba35573 --- /dev/null +++ b/widgets/components/XArcGauge/main_XArcGauge.cpp @@ -0,0 +1,69 @@ +#include "XArcGauge.h" + +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XArcGauge - XFrame demo"); + XSelectInput(display, window, ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask); + Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False); + XSetWMProtocols(display, window, &deleteWindowMessage, 1); + XMapWindow(display, window); + GC gc = DefaultGC(display, screen); + SStyle_Manager styles(display, window, screen, gc); + styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120}, Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22}, Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.set_active_style_byName("default"); + XArcGauge widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + while (running) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.on_motion(event.xmotion.x, event.xmotion.y); + break; + case KeyPress: + { + char buffer[32]; + KeySym key = 0; + int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr); + buffer[n] = 0; + handled = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + } + + XCloseDisplay(display); + return 0; +} diff --git a/widgets/components/XDonutGauge/CMakeLists.txt b/widgets/components/XDonutGauge/CMakeLists.txt new file mode 100644 index 0000000..0e07bac --- /dev/null +++ b/widgets/components/XDonutGauge/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.20) + +project(XDonutGauge LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) + +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(X11 REQUIRED) + +add_executable(XDonutGauge main_XDonutGauge.cpp) + +target_link_libraries(XDonutGauge PRIVATE X11::X11) diff --git a/widgets/components/XDonutGauge/XDonutGauge.h b/widgets/components/XDonutGauge/XDonutGauge.h new file mode 100644 index 0000000..164f3e2 --- /dev/null +++ b/widgets/components/XDonutGauge/XDonutGauge.h @@ -0,0 +1,164 @@ +#pragma once + +#include "core/xframe_core.h" + +#include +#include +#include + +// region XDonutGauge +// +// A circular progress ring: a thick arc proportional to the value with +// the % centred inside. Raw X11, same as the rest of the XFrame set. +// +class XDonutGauge +{ +private: + Display* display_; + int screen_; + Window window_; + GC gc_; + SStyle_Manager* styles_; + int x1_, y1_, x2_, y2_; + std::string name_; + std::string text_; + std::string style_name_; + bool visible_ = true; + bool enabled_ = true; + bool focused_ = false; + + double value_ = 0.0; // 0..100 + Srgb ring_color_{0, 150, 200}; + + Srgb white_color_{255, 255, 255}; + Srgb track_color_{230, 230, 230}; + + unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); } + + void draw_text_color(int x, int baseline_y, const std::string& s, Srgb color) + { + SStyle st = style(); + if (!st.font_ || s.empty()) return; + XSetFont(display_, gc_, st.font_->fid); + XSetForeground(display_, gc_, col(color)); + XDrawString(display_, window_, gc_, x, baseline_y, s.c_str(), (int)s.size()); + } + +public: + XDonutGauge(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height) + { + display_ = display; + screen_ = screen; + window_ = window; + gc_ = gc; + styles_ = styles; + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + name_ = "XDonutGauge"; + text_ = "Disk"; + value_ = 47.0; + } + + ~XDonutGauge() {} + + // region Standard plumbing + void set_bounds(int x, int y, int width, int height) + { + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + } + int width() { return x2_ - x1_; } + int height() { return y2_ - y1_; } + bool contains(int x, int y) + { + return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_; + } + void set_visible(bool v) { visible_ = v; } + bool get_visible() { return visible_; } + void set_enabled(bool e) { enabled_ = e; } + bool get_enabled() { return enabled_; } + void set_style_name(std::string s) { style_name_ = s; } + void set_text(std::string t) { text_ = t; } + std::string get_text() { return text_; } + SStyle style() { return styles_->resolve(style_name_); } + // endregion + + // region Gauge API + void set_value(double v) { value_ = std::max(0.0, std::min(100.0, v)); } + double get_value() { return value_; } + void set_ring_color(Srgb c) { ring_color_ = c; } + void set_ring_color_rgb(int r, int g, int b) { ring_color_ = Srgb{r, g, b}; } + // endregion + + void draw() + { + if (!visible_) return; + SStyle st = style(); + int w = width(); + int h = height(); + XSetForeground(display_, gc_, col(white_color_)); + XFillRectangle(display_, window_, gc_, x1_, y1_, w, h); + + const int title_h = 20; + int cx = x1_ + w / 2; + int cy = y1_ + title_h + (h - title_h) / 2; + int r = std::max(16, std::min(w, h - title_h - 8) / 2 - 10); + const int thick = 16; + + xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_); + + // Track ring (full circle) + value arc from the top, clockwise. + XSetForeground(display_, gc_, col(track_color_)); + xf::set_line_width(display_, gc_, thick); + XDrawArc(display_, window_, gc_, cx - r, cy - r, 2 * r, 2 * r, 0, 360 * 64); + XSetForeground(display_, gc_, col(ring_color_)); + int sweep = (int)(value_ / 100.0 * 360.0 * 64); + if (sweep > 0) + XDrawArc(display_, window_, gc_, cx - r, cy - r, 2 * r, 2 * r, 90 * 64, -sweep); + xf::set_line_width(display_, gc_, 1); + + // % centred, label under it. + char pct[16]; + std::snprintf(pct, sizeof(pct), "%.0f%%", value_); + int pw = xf::text_width(st, pct); + draw_text_color(cx - pw / 2, cy + 4, pct, ring_color_); + draw_text_color(cx - pw / 2 + 1, cy + 4, pct, ring_color_); + int tw = xf::text_width(st, text_); + xf::draw_text_at(display_, window_, gc_, st, cx - tw / 2, cy + 22, text_); + + XSetForeground(display_, gc_, st.lineColor_); + XDrawRectangle(display_, window_, gc_, x1_, y1_, w, h); + XFlush(display_); + } + + bool on_button_press(int x, int y, unsigned int button) + { + (void)x; + (void)y; + (void)button; + return false; + } + bool on_button_release(int x, int y, unsigned int button) + { + (void)x; + (void)y; + (void)button; + return false; + } + bool on_motion(int x, int y) + { + (void)x; + (void)y; + return false; + } + bool on_key(KeySym key, const char* str) + { + (void)key; + (void)str; + return false; + } +}; diff --git a/widgets/components/XDonutGauge/core/xframe_core.h b/widgets/components/XDonutGauge/core/xframe_core.h new file mode 100644 index 0000000..098187d --- /dev/null +++ b/widgets/components/XDonutGauge/core/xframe_core.h @@ -0,0 +1,988 @@ +#pragma once +#ifndef XFRAME_CORE_H +#define XFRAME_CORE_H + +// +// xframe_core.h - shared core for the XFrame widget library. +// +// Everything in here follows the format of the "Writing Your Own +// X-11 Framework Basics" articles (hotconfig.com): +// +// Part 1 - Display / Screen / Window boilerplate, the GC as a +// "pen", the drawing primitives, and the Canvas class. +// Part 2 - widgets are standalone classes that record display_, +// screen_, window_ and gc_ at construction time. +// Part 3 - SStyle_Manager tracks named styles in a vector. +// Part 4 - a polished widget is built from primitives: two fill +// rectangles + four fill arcs make a rounded body, four +// lines + four draw arcs make its outline, and text is +// centred with XTextWidth + font ascent / descent. +// +// Two small completions versus the articles, needed by Part 4's own +// draw code: SStyle gains lineColor_ and text_color_, and the style +// manager gains the delete_style() function Part 3 left as a reader +// challenge, plus bounds-safe getters. +// + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +//region SStyle + +struct Srgb + +{ + + int r; + + int g; + + int b; + +}; + +struct SStyle + +{ + + unsigned long fillColor_ = 0; + + unsigned long outlineColor_ = 0; + + unsigned long lineColor_ = 0; + + unsigned long text_color_ = 0; + + unsigned long radius_ = 0; + + unsigned long lineWidth_ = 1; + + XFontStruct *font_ = nullptr; + + std::string style_name_; + +}; + +//endregion + +//region SStyle_Manager + +class SStyle_Manager + +{ + +private: + + std::vector styles_; + + Display *display_; + + Window window_; + + int screen_; + + GC gc_; + + int active_style = 0; // just set the default to first passed. + +public: + + SStyle_Manager(Display *display, Window window, int screen, GC gc) + + { + + display_ = display; + + window_ = window; + + screen_ = screen; + + gc_ = gc; + + } + + // NOTE (test agent, Nov 26 2026): do NOT XFreeFont here. Every demo + // main calls XCloseDisplay() before this destructor runs (stack + // unwinding after main returns), so XFreeFont would operate on a + // closed Display and corrupt the heap (SIGSEGV/SIGABRT inside + // XFreeFont on every clean exit). The fonts are process-lifetime + // resources; the X server reclaims them when the client disconnects. + // Use delete_style() to free a single style while the display is + // still open. + ~SStyle_Manager() + + { + + styles_.clear(); + + } + + int add_style(std::string styleName, Srgb fillColor, Srgb outlineColor, int radius, int linewidth, std::string fontName) + + { + + return add_style_ex(styleName, fillColor, outlineColor, outlineColor, Srgb{0, 0, 0}, radius, linewidth, fontName); + + } + + int add_style_ex(std::string styleName, Srgb fillColor, Srgb outlineColor, Srgb lineColor, Srgb textColor, int radius, int linewidth, std::string fontName) + + { + + SStyle nstyle; + + nstyle.style_name_ = std::move(styleName); + + nstyle.fillColor_ = get_color_Srgb(fillColor); + + nstyle.outlineColor_ = get_color_Srgb(outlineColor); + + nstyle.lineColor_ = get_color_Srgb(lineColor); + + nstyle.text_color_ = get_color_Srgb(textColor); + + nstyle.radius_ = radius; + + nstyle.lineWidth_ = linewidth; + + // Define fallback fonts to try if the requested font fails + + const char* fallbackFonts[] = { "fixed", "helvetica" }; + + size_t numFallbacks = sizeof(fallbackFonts) / sizeof(*fallbackFonts); + + nstyle.font_ = nullptr; + + // Attempt to load the primary font (if provided and not empty) + + if (!fontName.empty()) { + + nstyle.font_ = XLoadQueryFont(display_, fontName.c_str()); + + } + + // If the primary failed, iterate through fallback fonts until one succeeds or all are exhausted + + if (!nstyle.font_) { + + for (size_t i = 0; i < numFallbacks && !nstyle.font_; ++i) { + + nstyle.font_ = XLoadQueryFont(display_, fallbackFonts[i]); + + } + + } + + // Final validation: if no font loaded, report which fonts were attempted and exit + + if (!nstyle.font_) + + { + + printf("Failed to load any font (tried: "); + + if (!fontName.empty()) printf("%s", fontName.c_str()); + + for (size_t i = 0; i < numFallbacks; ++i) { + + if (!fontName.empty() || i > 0) printf(", "); + + printf("%s", fallbackFonts[i]); + + } + + printf(")\n"); + + return -1; + + } + + styles_.push_back(nstyle); + + return 0; + + } + + // The delete function Part 3 left as a challenge: remove a style + // by name, freeing its font, and keep active_style in range. + + int delete_style(std::string styleName) + + { + + for (size_t i = 0; i < styles_.size(); ++i) + + { + + if (styles_[i].style_name_ == styleName) + + { + + if (styles_[i].font_) + + { + + XFreeFont(display_, styles_[i].font_); + + } + + styles_.erase(styles_.begin() + static_cast(i)); + + if (active_style >= (int)styles_.size()) + + { + + active_style = styles_.empty() ? 0 : (int)styles_.size() - 1; + + } + + return 0; + + } + + } + + return -1; // Not found + + } + + int style_count() + + { + + return (int)styles_.size(); + + } + + unsigned long get_color_Srgb(Srgb color) + + { + + XColor xcolor; + + xcolor.red = color.r << 8; + + xcolor.green = color.g << 8; + + xcolor.blue = color.b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", color.r, color.g, color.b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + unsigned long get_color_rgb(unsigned char r, unsigned char g, unsigned char b) + + { + + XColor xcolor; + + xcolor.red = r << 8; + + xcolor.green = g << 8; + + xcolor.blue = b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", r, g, b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + int set_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + active_style = static_cast(std::distance(styles_.begin(), it)); + + return active_style; + + } + + return -1; // Not found + + } + + SStyle get_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + return *it; + + } + + return get_active_style(); + + } + + SStyle get_active_style() + + { + + if (styles_.empty()) + + { + + return SStyle{}; + + } + + if (active_style < 0 || active_style >= (int)styles_.size()) + + { + + active_style = 0; + + } + + return styles_[active_style]; + + } + + SStyle get_index_style(int index) + + { + + if (index >= 0 && index < (int)styles_.size()) + + { + + return styles_[index]; + + } + + return get_active_style(); + + } + + // Resolve the style a widget should draw with: its own named + // style when set, otherwise the manager's active style. + + SStyle resolve(const std::string& styleName) + + { + + if (!styleName.empty()) + + { + + return get_active_style_byName(styleName); + + } + + return get_active_style(); + + } + +}; + +//endregion + +//region Drawing helpers (Part 4 construction, shared by all widgets) + +namespace xf + +{ + + inline void set_line_width(Display* display, GC gc, int lineWidth) + + { + + unsigned long valuemask = GCLineWidth; + + XGCValues gc_values; + + gc_values.line_width = lineWidth; + + XChangeGC(display, gc, valuemask, &gc_values); + + } + + // The Part 4 rounded body: Rectangle A + Rectangle B + 4 fill arcs. + + inline void fill_rounded_rect(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + int w = x2 - x1; + + int h = y2 - y1; + + if (r <= 0 || w <= 2 * r || h <= 2 * r) + + { + + XFillRectangle(display, window, gc, x1, y1, w, h); + + return; + + } + + XSetForeground(display, gc, style.fillColor_); + + //Rectangle A + + XFillRectangle(display, window, gc, x1 + r, y1, w - 2 * r, h); + + //Rectangle B + + XFillRectangle(display, window, gc, x1, y1 + r, w, h - 2 * r); + + //region Arc C Top Left + + XFillArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XFillArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XFillArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XFillArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // The Part 4 outline: 4 line bars + 4 draw arcs, in lineColor_. + + inline void draw_rounded_outline(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + XSetForeground(display, gc, style.lineColor_); + + set_line_width(display, gc, (int)style.lineWidth_); + + if (r <= 0) + + { + + XDrawRectangle(display, window, gc, x1, y1, x2 - x1, y2 - y1); + + return; + + } + + // 4 Line Bars + + XDrawLine(display, window, gc, x1 + r, y1, x2 - r, y1); + + XDrawLine(display, window, gc, x2, y1 + r, x2, y2 - r); + + XDrawLine(display, window, gc, x1, y1 + r, x1, y2 - r); + + XDrawLine(display, window, gc, x1 + r, y2, x2 - r, y2); + + // 4 Arcs + + XDrawArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XDrawArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XDrawArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XDrawArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // Convenience: body + outline in one call, the polished look. + + inline void draw_polished(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + fill_rounded_rect(display, window, gc, style, x1, y1, x2, y2); + + draw_rounded_outline(display, window, gc, style, x1, y1, x2, y2); + + } + + inline int text_width(const SStyle& style, const std::string& text) + + { + + if (!style.font_) + + { + + return (int)text.size() * 8; + + } + + return XTextWidth(style.font_, text.c_str(), (int)text.length()); + + } + + // Part 4 centred text: XTextWidth + ascent / descent. + + inline void draw_text_centered(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int tw = XTextWidth(style.font_, text.c_str(), (int)text.length()); + + int text_x = x1 + ((x2 - x1) - tw) / 2; + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, text_x, text_y, text.c_str(), (int)text.length()); + + } + + // Left-aligned text, vertically centred in the given band. + + inline void draw_text_left(Display* display, Window window, GC gc, const SStyle& style, int x, int y1, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, x, text_y, text.c_str(), (int)text.length()); + + } + + // Baseline text at an explicit position (labels, grid cells...). + + inline void draw_text_at(Display* display, Window window, GC gc, const SStyle& style, int x, int baseline_y, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + XDrawString(display, window, gc, x, baseline_y, text.c_str(), (int)text.length()); + + } + + inline void draw_checkmark(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + set_line_width(display, gc, 2); + + int s = size / 2; + + XDrawLine(display, window, gc, cx - s, cy, cx - s / 3, cy + s); + + XDrawLine(display, window, gc, cx - s / 3, cy + s, cx + s, cy - s); + + set_line_width(display, gc, 1); + + } + + inline void draw_triangle_down(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy + i, cx + (size - i), cy + i); + + } + + } + + inline void draw_triangle_up(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy - i, cx + (size - i), cy - i); + + } + + } + + inline void draw_triangle_right(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx + i, cy - (size - i), cx + i, cy + (size - i)); + + } + + } + +} + +//endregion + +//region Region Canvas (from Part 1) + +class Canvas + +{ + +private: + + Display* display = nullptr; + + Window window = 0; + + GC gc = nullptr; + + XImage* image = nullptr; + + int width = 0; + + int height = 0; + + std::vector pixels; + +public: + + Canvas(Display* display, Window window, GC graphicsContext, int width, int height) : display(display), window(window), gc(graphicsContext) + + { + + resize(width, height); + + } + + ~Canvas() { destroyImage(); } + + void resize(int newWidth, int newHeight) + + { + + width = std::max(1, newWidth); + + height = std::max(1, newHeight); + + pixels.resize(static_cast(width) * height); + + clear(0x202020); + + destroyImage(); + + Visual* visual = DefaultVisual(display, DefaultScreen(display)); + + int depth = DefaultDepth(display, DefaultScreen(display)); + + image = XCreateImage(display, visual, depth, ZPixmap, 0, nullptr, width, height, 32, 0); + + if (!image) + + { + + std::exit(1); + + } + + image->data = static_cast(std::malloc(image->bytes_per_line * height)); + + if (!image->data) + + { + + std::exit(1); + + } + + } + + int getWidth() const + + { + + return width; + + } + + int getHeight() const + + { + + return height; + + } + + // Direct access to the raw framebuffer. + + uint32_t* data() + + { + + return pixels.data(); + + } + + const uint32_t* data() const + + { + + return pixels.data(); + + } + + void clear(uint32_t rgb) + + { + + std::fill(pixels.begin(), pixels.end(), rgb); + + } + + void setPixel(int x, int y, uint32_t rgb) + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return; + + } + + pixels[static_cast(y) * width + x] = rgb; + + } + + uint32_t getPixel(int x, int y) const + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return 0; + + } + + return pixels[static_cast(y) * width + x]; + + } + + void fillRect(int x, int y, int w, int h, uint32_t rgb) + + { + + int x0 = std::max(0, x); + + int y0 = std::max(0, y); + + int x1 = std::min(width, x + w); + + int y1 = std::min(height, y + h); + + for (int py = y0; py < y1; ++py) + + { + + for (int px = x0; px < x1; ++px) + + { + + setPixel(px, py, rgb); + + } + + } + + } + + void drawGradient() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint8_t red = static_cast(255.0 * x / std::max(1, width - 1)); + + uint8_t green = static_cast(255.0 * y / std::max(1, height - 1)); + + uint8_t blue = 100; + + uint32_t color = (static_cast(red) << 16) | (static_cast(green) << 8) | blue; + + setPixel(x, y, color); + + } + + } + + } + + void present() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint32_t rgb = getPixel(x, y); + + unsigned long x11Pixel = ((rgb >> 16) & 0xff) << 16 | ((rgb >> 8) & 0xff) << 8 | (rgb & 0xff); + + XPutPixel(image, x, y, x11Pixel); + + } + + } + + XPutImage(display, window, gc, image, 0, 0, 0, 0, width, height); + + XFlush(display); + + } + +private: + + void destroyImage() + + { + + if (image) + + { + + XDestroyImage(image); + + image = nullptr; + + } + + } + +}; + +//endregion +#endif // XFRAME_CORE_H diff --git a/widgets/components/XDonutGauge/main_XDonutGauge.cpp b/widgets/components/XDonutGauge/main_XDonutGauge.cpp new file mode 100644 index 0000000..7e7e13d --- /dev/null +++ b/widgets/components/XDonutGauge/main_XDonutGauge.cpp @@ -0,0 +1,69 @@ +#include "XDonutGauge.h" + +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XDonutGauge - XFrame demo"); + XSelectInput(display, window, ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask); + Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False); + XSetWMProtocols(display, window, &deleteWindowMessage, 1); + XMapWindow(display, window); + GC gc = DefaultGC(display, screen); + SStyle_Manager styles(display, window, screen, gc); + styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120}, Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22}, Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.set_active_style_byName("default"); + XDonutGauge widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + while (running) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.on_motion(event.xmotion.x, event.xmotion.y); + break; + case KeyPress: + { + char buffer[32]; + KeySym key = 0; + int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr); + buffer[n] = 0; + handled = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + } + + XCloseDisplay(display); + return 0; +} diff --git a/widgets/components/XStackBar/CMakeLists.txt b/widgets/components/XStackBar/CMakeLists.txt new file mode 100644 index 0000000..e83b48f --- /dev/null +++ b/widgets/components/XStackBar/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.20) + +project(XStackBar LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) + +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(X11 REQUIRED) + +add_executable(XStackBar main_XStackBar.cpp) + +target_link_libraries(XStackBar PRIVATE X11::X11) diff --git a/widgets/components/XStackBar/XStackBar.h b/widgets/components/XStackBar/XStackBar.h new file mode 100644 index 0000000..883dcd8 --- /dev/null +++ b/widgets/components/XStackBar/XStackBar.h @@ -0,0 +1,209 @@ +#pragma once + +#include "core/xframe_core.h" + +#include +#include +#include +#include + +// region XStackBar +// +// A stacked multi-segment horizontal bar (used / cached / buffers / free) +// with a legend underneath: each segment's label, value and share. +// Raw X11, same as the rest of the XFrame set. +// +class XStackBar +{ +private: + Display* display_; + int screen_; + Window window_; + GC gc_; + SStyle_Manager* styles_; + int x1_, y1_, x2_, y2_; + std::string name_; + std::string text_; + std::string style_name_; + bool visible_ = true; + bool enabled_ = true; + bool focused_ = false; + + struct Segment + { + std::string name; + double value = 0.0; + Srgb color{0, 150, 60}; + }; + + std::vector segments_; + + Srgb white_color_{255, 255, 255}; + Srgb axis_color_{150, 150, 150}; + + unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); } + + static std::string fmt1(double v) + { + char buf[32]; + std::snprintf(buf, sizeof(buf), "%.1f", v); + return buf; + } + +public: + XStackBar(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height) + { + display_ = display; + screen_ = screen; + window_ = window; + gc_ = gc; + styles_ = styles; + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + name_ = "XStackBar"; + text_ = "Memory"; + + // Preload a demo: a 30 GiB box split used / cached / buffers / free. + add_segment("Used", 11.7, Srgb{0, 150, 60}); + add_segment("Cached", 15.5, Srgb{220, 180, 0}); + add_segment("Buffers", 1.2, Srgb{0, 120, 200}); + add_segment("Free", 1.9, Srgb{200, 200, 200}); + } + + ~XStackBar() {} + + // region Standard plumbing + void set_bounds(int x, int y, int width, int height) + { + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + } + int width() { return x2_ - x1_; } + int height() { return y2_ - y1_; } + bool contains(int x, int y) + { + return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_; + } + void set_visible(bool v) { visible_ = v; } + bool get_visible() { return visible_; } + void set_enabled(bool e) { enabled_ = e; } + bool get_enabled() { return enabled_; } + void set_style_name(std::string s) { style_name_ = s; } + void set_text(std::string t) { text_ = t; } + std::string get_text() { return text_; } + SStyle style() { return styles_->resolve(style_name_); } + // endregion + + // region Segment API + void add_segment(const std::string& name, double value, Srgb color) + { + Segment s; + s.name = name; + s.value = std::max(0.0, value); + s.color = color; + segments_.push_back(s); + } + void clear_segments() { segments_.clear(); } + void set_segment(int index, double value) + { + if (index >= 0 && index < (int)segments_.size()) + segments_[index].value = std::max(0.0, value); + } + int segment_count() { return (int)segments_.size(); } + double total() + { + double t = 0.0; + for (auto& s : segments_) t += s.value; + return t; + } + // endregion + + void draw() + { + if (!visible_) return; + SStyle st = style(); + int w = width(); + int h = height(); + XSetForeground(display_, gc_, col(white_color_)); + XFillRectangle(display_, window_, gc_, x1_, y1_, w, h); + + const int title_h = 20; + const int bar_h = 26; + double tot = total(); + + xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_); + if (tot > 0.0) + { + std::string tot_s = "total " + fmt1(tot) + " GiB"; + xf::draw_text_at(display_, window_, gc_, st, x2_ - 8 - xf::text_width(st, tot_s), y1_ + 15, tot_s); + } + + // Stacked bar. + int bx = x1_ + 8; + int by = y1_ + title_h + 4; + int bw = std::max(10, w - 16); + int x = bx; + for (auto& s : segments_) + { + int sw = tot > 0.0 ? (int)(bw * s.value / tot) : 0; + if (sw <= 0) continue; + XSetForeground(display_, gc_, col(s.color)); + XFillRectangle(display_, window_, gc_, x, by, sw, bar_h); + x += sw; + } + XSetForeground(display_, gc_, col(axis_color_)); + XDrawRectangle(display_, window_, gc_, bx, by, bw, bar_h); + + // Legend: one row per segment — swatch, name, value, share. + int ly = by + bar_h + 18; + for (auto& s : segments_) + { + XSetForeground(display_, gc_, col(s.color)); + XFillRectangle(display_, window_, gc_, x1_ + 8, ly - 10, 12, 12); + std::string line = s.name + " " + fmt1(s.value) + " GiB"; + if (tot > 0.0) + { + char share[16]; + std::snprintf(share, sizeof(share), " %.0f%%", s.value / tot * 100.0); + line += share; + } + xf::draw_text_at(display_, window_, gc_, st, x1_ + 28, ly, line); + ly += 18; + } + + XSetForeground(display_, gc_, st.lineColor_); + XDrawRectangle(display_, window_, gc_, x1_, y1_, w, h); + XFlush(display_); + } + + bool on_button_press(int x, int y, unsigned int button) + { + (void)x; + (void)y; + (void)button; + return false; + } + bool on_button_release(int x, int y, unsigned int button) + { + (void)x; + (void)y; + (void)button; + return false; + } + bool on_motion(int x, int y) + { + (void)x; + (void)y; + return false; + } + bool on_key(KeySym key, const char* str) + { + (void)key; + (void)str; + return false; + } +}; diff --git a/widgets/components/XStackBar/core/xframe_core.h b/widgets/components/XStackBar/core/xframe_core.h new file mode 100644 index 0000000..098187d --- /dev/null +++ b/widgets/components/XStackBar/core/xframe_core.h @@ -0,0 +1,988 @@ +#pragma once +#ifndef XFRAME_CORE_H +#define XFRAME_CORE_H + +// +// xframe_core.h - shared core for the XFrame widget library. +// +// Everything in here follows the format of the "Writing Your Own +// X-11 Framework Basics" articles (hotconfig.com): +// +// Part 1 - Display / Screen / Window boilerplate, the GC as a +// "pen", the drawing primitives, and the Canvas class. +// Part 2 - widgets are standalone classes that record display_, +// screen_, window_ and gc_ at construction time. +// Part 3 - SStyle_Manager tracks named styles in a vector. +// Part 4 - a polished widget is built from primitives: two fill +// rectangles + four fill arcs make a rounded body, four +// lines + four draw arcs make its outline, and text is +// centred with XTextWidth + font ascent / descent. +// +// Two small completions versus the articles, needed by Part 4's own +// draw code: SStyle gains lineColor_ and text_color_, and the style +// manager gains the delete_style() function Part 3 left as a reader +// challenge, plus bounds-safe getters. +// + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +//region SStyle + +struct Srgb + +{ + + int r; + + int g; + + int b; + +}; + +struct SStyle + +{ + + unsigned long fillColor_ = 0; + + unsigned long outlineColor_ = 0; + + unsigned long lineColor_ = 0; + + unsigned long text_color_ = 0; + + unsigned long radius_ = 0; + + unsigned long lineWidth_ = 1; + + XFontStruct *font_ = nullptr; + + std::string style_name_; + +}; + +//endregion + +//region SStyle_Manager + +class SStyle_Manager + +{ + +private: + + std::vector styles_; + + Display *display_; + + Window window_; + + int screen_; + + GC gc_; + + int active_style = 0; // just set the default to first passed. + +public: + + SStyle_Manager(Display *display, Window window, int screen, GC gc) + + { + + display_ = display; + + window_ = window; + + screen_ = screen; + + gc_ = gc; + + } + + // NOTE (test agent, Nov 26 2026): do NOT XFreeFont here. Every demo + // main calls XCloseDisplay() before this destructor runs (stack + // unwinding after main returns), so XFreeFont would operate on a + // closed Display and corrupt the heap (SIGSEGV/SIGABRT inside + // XFreeFont on every clean exit). The fonts are process-lifetime + // resources; the X server reclaims them when the client disconnects. + // Use delete_style() to free a single style while the display is + // still open. + ~SStyle_Manager() + + { + + styles_.clear(); + + } + + int add_style(std::string styleName, Srgb fillColor, Srgb outlineColor, int radius, int linewidth, std::string fontName) + + { + + return add_style_ex(styleName, fillColor, outlineColor, outlineColor, Srgb{0, 0, 0}, radius, linewidth, fontName); + + } + + int add_style_ex(std::string styleName, Srgb fillColor, Srgb outlineColor, Srgb lineColor, Srgb textColor, int radius, int linewidth, std::string fontName) + + { + + SStyle nstyle; + + nstyle.style_name_ = std::move(styleName); + + nstyle.fillColor_ = get_color_Srgb(fillColor); + + nstyle.outlineColor_ = get_color_Srgb(outlineColor); + + nstyle.lineColor_ = get_color_Srgb(lineColor); + + nstyle.text_color_ = get_color_Srgb(textColor); + + nstyle.radius_ = radius; + + nstyle.lineWidth_ = linewidth; + + // Define fallback fonts to try if the requested font fails + + const char* fallbackFonts[] = { "fixed", "helvetica" }; + + size_t numFallbacks = sizeof(fallbackFonts) / sizeof(*fallbackFonts); + + nstyle.font_ = nullptr; + + // Attempt to load the primary font (if provided and not empty) + + if (!fontName.empty()) { + + nstyle.font_ = XLoadQueryFont(display_, fontName.c_str()); + + } + + // If the primary failed, iterate through fallback fonts until one succeeds or all are exhausted + + if (!nstyle.font_) { + + for (size_t i = 0; i < numFallbacks && !nstyle.font_; ++i) { + + nstyle.font_ = XLoadQueryFont(display_, fallbackFonts[i]); + + } + + } + + // Final validation: if no font loaded, report which fonts were attempted and exit + + if (!nstyle.font_) + + { + + printf("Failed to load any font (tried: "); + + if (!fontName.empty()) printf("%s", fontName.c_str()); + + for (size_t i = 0; i < numFallbacks; ++i) { + + if (!fontName.empty() || i > 0) printf(", "); + + printf("%s", fallbackFonts[i]); + + } + + printf(")\n"); + + return -1; + + } + + styles_.push_back(nstyle); + + return 0; + + } + + // The delete function Part 3 left as a challenge: remove a style + // by name, freeing its font, and keep active_style in range. + + int delete_style(std::string styleName) + + { + + for (size_t i = 0; i < styles_.size(); ++i) + + { + + if (styles_[i].style_name_ == styleName) + + { + + if (styles_[i].font_) + + { + + XFreeFont(display_, styles_[i].font_); + + } + + styles_.erase(styles_.begin() + static_cast(i)); + + if (active_style >= (int)styles_.size()) + + { + + active_style = styles_.empty() ? 0 : (int)styles_.size() - 1; + + } + + return 0; + + } + + } + + return -1; // Not found + + } + + int style_count() + + { + + return (int)styles_.size(); + + } + + unsigned long get_color_Srgb(Srgb color) + + { + + XColor xcolor; + + xcolor.red = color.r << 8; + + xcolor.green = color.g << 8; + + xcolor.blue = color.b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", color.r, color.g, color.b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + unsigned long get_color_rgb(unsigned char r, unsigned char g, unsigned char b) + + { + + XColor xcolor; + + xcolor.red = r << 8; + + xcolor.green = g << 8; + + xcolor.blue = b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", r, g, b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + int set_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + active_style = static_cast(std::distance(styles_.begin(), it)); + + return active_style; + + } + + return -1; // Not found + + } + + SStyle get_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + return *it; + + } + + return get_active_style(); + + } + + SStyle get_active_style() + + { + + if (styles_.empty()) + + { + + return SStyle{}; + + } + + if (active_style < 0 || active_style >= (int)styles_.size()) + + { + + active_style = 0; + + } + + return styles_[active_style]; + + } + + SStyle get_index_style(int index) + + { + + if (index >= 0 && index < (int)styles_.size()) + + { + + return styles_[index]; + + } + + return get_active_style(); + + } + + // Resolve the style a widget should draw with: its own named + // style when set, otherwise the manager's active style. + + SStyle resolve(const std::string& styleName) + + { + + if (!styleName.empty()) + + { + + return get_active_style_byName(styleName); + + } + + return get_active_style(); + + } + +}; + +//endregion + +//region Drawing helpers (Part 4 construction, shared by all widgets) + +namespace xf + +{ + + inline void set_line_width(Display* display, GC gc, int lineWidth) + + { + + unsigned long valuemask = GCLineWidth; + + XGCValues gc_values; + + gc_values.line_width = lineWidth; + + XChangeGC(display, gc, valuemask, &gc_values); + + } + + // The Part 4 rounded body: Rectangle A + Rectangle B + 4 fill arcs. + + inline void fill_rounded_rect(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + int w = x2 - x1; + + int h = y2 - y1; + + if (r <= 0 || w <= 2 * r || h <= 2 * r) + + { + + XFillRectangle(display, window, gc, x1, y1, w, h); + + return; + + } + + XSetForeground(display, gc, style.fillColor_); + + //Rectangle A + + XFillRectangle(display, window, gc, x1 + r, y1, w - 2 * r, h); + + //Rectangle B + + XFillRectangle(display, window, gc, x1, y1 + r, w, h - 2 * r); + + //region Arc C Top Left + + XFillArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XFillArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XFillArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XFillArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // The Part 4 outline: 4 line bars + 4 draw arcs, in lineColor_. + + inline void draw_rounded_outline(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + XSetForeground(display, gc, style.lineColor_); + + set_line_width(display, gc, (int)style.lineWidth_); + + if (r <= 0) + + { + + XDrawRectangle(display, window, gc, x1, y1, x2 - x1, y2 - y1); + + return; + + } + + // 4 Line Bars + + XDrawLine(display, window, gc, x1 + r, y1, x2 - r, y1); + + XDrawLine(display, window, gc, x2, y1 + r, x2, y2 - r); + + XDrawLine(display, window, gc, x1, y1 + r, x1, y2 - r); + + XDrawLine(display, window, gc, x1 + r, y2, x2 - r, y2); + + // 4 Arcs + + XDrawArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XDrawArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XDrawArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XDrawArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // Convenience: body + outline in one call, the polished look. + + inline void draw_polished(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + fill_rounded_rect(display, window, gc, style, x1, y1, x2, y2); + + draw_rounded_outline(display, window, gc, style, x1, y1, x2, y2); + + } + + inline int text_width(const SStyle& style, const std::string& text) + + { + + if (!style.font_) + + { + + return (int)text.size() * 8; + + } + + return XTextWidth(style.font_, text.c_str(), (int)text.length()); + + } + + // Part 4 centred text: XTextWidth + ascent / descent. + + inline void draw_text_centered(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int tw = XTextWidth(style.font_, text.c_str(), (int)text.length()); + + int text_x = x1 + ((x2 - x1) - tw) / 2; + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, text_x, text_y, text.c_str(), (int)text.length()); + + } + + // Left-aligned text, vertically centred in the given band. + + inline void draw_text_left(Display* display, Window window, GC gc, const SStyle& style, int x, int y1, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, x, text_y, text.c_str(), (int)text.length()); + + } + + // Baseline text at an explicit position (labels, grid cells...). + + inline void draw_text_at(Display* display, Window window, GC gc, const SStyle& style, int x, int baseline_y, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + XDrawString(display, window, gc, x, baseline_y, text.c_str(), (int)text.length()); + + } + + inline void draw_checkmark(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + set_line_width(display, gc, 2); + + int s = size / 2; + + XDrawLine(display, window, gc, cx - s, cy, cx - s / 3, cy + s); + + XDrawLine(display, window, gc, cx - s / 3, cy + s, cx + s, cy - s); + + set_line_width(display, gc, 1); + + } + + inline void draw_triangle_down(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy + i, cx + (size - i), cy + i); + + } + + } + + inline void draw_triangle_up(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy - i, cx + (size - i), cy - i); + + } + + } + + inline void draw_triangle_right(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx + i, cy - (size - i), cx + i, cy + (size - i)); + + } + + } + +} + +//endregion + +//region Region Canvas (from Part 1) + +class Canvas + +{ + +private: + + Display* display = nullptr; + + Window window = 0; + + GC gc = nullptr; + + XImage* image = nullptr; + + int width = 0; + + int height = 0; + + std::vector pixels; + +public: + + Canvas(Display* display, Window window, GC graphicsContext, int width, int height) : display(display), window(window), gc(graphicsContext) + + { + + resize(width, height); + + } + + ~Canvas() { destroyImage(); } + + void resize(int newWidth, int newHeight) + + { + + width = std::max(1, newWidth); + + height = std::max(1, newHeight); + + pixels.resize(static_cast(width) * height); + + clear(0x202020); + + destroyImage(); + + Visual* visual = DefaultVisual(display, DefaultScreen(display)); + + int depth = DefaultDepth(display, DefaultScreen(display)); + + image = XCreateImage(display, visual, depth, ZPixmap, 0, nullptr, width, height, 32, 0); + + if (!image) + + { + + std::exit(1); + + } + + image->data = static_cast(std::malloc(image->bytes_per_line * height)); + + if (!image->data) + + { + + std::exit(1); + + } + + } + + int getWidth() const + + { + + return width; + + } + + int getHeight() const + + { + + return height; + + } + + // Direct access to the raw framebuffer. + + uint32_t* data() + + { + + return pixels.data(); + + } + + const uint32_t* data() const + + { + + return pixels.data(); + + } + + void clear(uint32_t rgb) + + { + + std::fill(pixels.begin(), pixels.end(), rgb); + + } + + void setPixel(int x, int y, uint32_t rgb) + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return; + + } + + pixels[static_cast(y) * width + x] = rgb; + + } + + uint32_t getPixel(int x, int y) const + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return 0; + + } + + return pixels[static_cast(y) * width + x]; + + } + + void fillRect(int x, int y, int w, int h, uint32_t rgb) + + { + + int x0 = std::max(0, x); + + int y0 = std::max(0, y); + + int x1 = std::min(width, x + w); + + int y1 = std::min(height, y + h); + + for (int py = y0; py < y1; ++py) + + { + + for (int px = x0; px < x1; ++px) + + { + + setPixel(px, py, rgb); + + } + + } + + } + + void drawGradient() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint8_t red = static_cast(255.0 * x / std::max(1, width - 1)); + + uint8_t green = static_cast(255.0 * y / std::max(1, height - 1)); + + uint8_t blue = 100; + + uint32_t color = (static_cast(red) << 16) | (static_cast(green) << 8) | blue; + + setPixel(x, y, color); + + } + + } + + } + + void present() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint32_t rgb = getPixel(x, y); + + unsigned long x11Pixel = ((rgb >> 16) & 0xff) << 16 | ((rgb >> 8) & 0xff) << 8 | (rgb & 0xff); + + XPutPixel(image, x, y, x11Pixel); + + } + + } + + XPutImage(display, window, gc, image, 0, 0, 0, 0, width, height); + + XFlush(display); + + } + +private: + + void destroyImage() + + { + + if (image) + + { + + XDestroyImage(image); + + image = nullptr; + + } + + } + +}; + +//endregion +#endif // XFRAME_CORE_H diff --git a/widgets/components/XStackBar/main_XStackBar.cpp b/widgets/components/XStackBar/main_XStackBar.cpp new file mode 100644 index 0000000..85f0dc5 --- /dev/null +++ b/widgets/components/XStackBar/main_XStackBar.cpp @@ -0,0 +1,69 @@ +#include "XStackBar.h" + +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XStackBar - XFrame demo"); + XSelectInput(display, window, ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask); + Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False); + XSetWMProtocols(display, window, &deleteWindowMessage, 1); + XMapWindow(display, window); + GC gc = DefaultGC(display, screen); + SStyle_Manager styles(display, window, screen, gc); + styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120}, Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22}, Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.set_active_style_byName("default"); + XStackBar widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + while (running) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.on_motion(event.xmotion.x, event.xmotion.y); + break; + case KeyPress: + { + char buffer[32]; + KeySym key = 0; + int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr); + buffer[n] = 0; + handled = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + } + + XCloseDisplay(display); + return 0; +} diff --git a/widgets/components/XTachometer/CMakeLists.txt b/widgets/components/XTachometer/CMakeLists.txt new file mode 100644 index 0000000..85d8743 --- /dev/null +++ b/widgets/components/XTachometer/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.20) + +project(XTachometer LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) + +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(X11 REQUIRED) + +add_executable(XTachometer main_XTachometer.cpp) + +target_link_libraries(XTachometer PRIVATE X11::X11) diff --git a/widgets/components/XTachometer/XTachometer.h b/widgets/components/XTachometer/XTachometer.h new file mode 100644 index 0000000..de65cc5 --- /dev/null +++ b/widgets/components/XTachometer/XTachometer.h @@ -0,0 +1,244 @@ +#pragma once + +#include "core/xframe_core.h" + +#include +#include +#include +#include + +// region XTachometer +// +// A round tachometer: needle over a 240-degree sweep, a redline zone at +// the top of the scale, a digital readout and a peak-hold tick. +// Raw X11, same as the rest of the XFrame set. +// +class XTachometer +{ +private: + Display* display_; + int screen_; + Window window_; + GC gc_; + SStyle_Manager* styles_; + int x1_, y1_, x2_, y2_; + std::string name_; + std::string text_; + std::string style_name_; + bool visible_ = true; + bool enabled_ = true; + bool focused_ = false; + + double value_ = 0.0; + double max_ = 8000.0; + double redline_ = 6500.0; + double peak_ = 0.0; + bool peak_hold_ = true; + + Srgb white_color_{255, 255, 255}; + Srgb axis_color_{150, 150, 150}; + Srgb dial_color_{240, 240, 240}; + Srgb redline_color_{210, 40, 30}; + Srgb needle_color_{30, 30, 30}; + + unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); } + + static std::string fmt0(double v) + { + char buf[32]; + std::snprintf(buf, sizeof(buf), "%.0f", v); + return buf; + } + + // Sweep: 240 degrees clockwise from 150 deg (7:30 position) to 30 deg. + static double angle_deg(double frac) + { + return 150.0 - frac * 240.0; + } + +public: + XTachometer(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height) + { + display_ = display; + screen_ = screen; + window_ = window; + gc_ = gc; + styles_ = styles; + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + name_ = "XTachometer"; + text_ = "RPM"; + value_ = 4200.0; + peak_ = 6100.0; + } + + ~XTachometer() {} + + // region Standard plumbing + void set_bounds(int x, int y, int width, int height) + { + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + } + int width() { return x2_ - x1_; } + int height() { return y2_ - y1_; } + bool contains(int x, int y) + { + return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_; + } + void set_visible(bool v) { visible_ = v; } + bool get_visible() { return visible_; } + void set_enabled(bool e) { enabled_ = e; } + bool get_enabled() { return enabled_; } + void set_style_name(std::string s) { style_name_ = s; } + void set_text(std::string t) { text_ = t; } + std::string get_text() { return text_; } + SStyle style() { return styles_->resolve(style_name_); } + // endregion + + // region Gauge API + void set_value(double v) + { + value_ = std::max(0.0, std::min(max_, v)); + if (peak_hold_) peak_ = std::max(peak_, value_); + } + double get_value() { return value_; } + void set_max(double m) + { + max_ = m > 0.0 ? m : 1.0; + value_ = std::min(value_, max_); + peak_ = std::min(peak_, max_); + redline_ = std::min(redline_, max_); + } + double get_max() { return max_; } + void set_redline(double r) { redline_ = std::max(0.0, std::min(max_, r)); } + double get_redline() { return redline_; } + double peak() { return peak_; } + void reset_peak() { peak_ = value_; } + // endregion + + void draw() + { + if (!visible_) return; + SStyle st = style(); + int w = width(); + int h = height(); + XSetForeground(display_, gc_, col(white_color_)); + XFillRectangle(display_, window_, gc_, x1_, y1_, w, h); + + const int title_h = 20; + int cx = x1_ + w / 2; + int cy = y1_ + title_h + (h - title_h) / 2 + 8; + int r = std::max(20, std::min(w, h - title_h - 44) / 2 - 6); + double frac = max_ > 0.0 ? value_ / max_ : 0.0; + double red_frac = max_ > 0.0 ? redline_ / max_ : 1.0; + double peak_frac = max_ > 0.0 ? peak_ / max_ : 0.0; + + xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_); + + // Dial face. + XSetForeground(display_, gc_, col(dial_color_)); + XFillArc(display_, window_, gc_, cx - r, cy - r, 2 * r, 2 * r, 0, 360 * 64); + XSetForeground(display_, gc_, col(axis_color_)); + XDrawArc(display_, window_, gc_, cx - r, cy - r, 2 * r, 2 * r, 0, 360 * 64); + + // Redline zone: thick red arc from the redline to the end of scale. + { + int a0 = (int)(angle_deg(red_frac) * 64); + int sweep = (int)((1.0 - red_frac) * 240.0 * 64); + XSetForeground(display_, gc_, col(redline_color_)); + xf::set_line_width(display_, gc_, 8); + // X angles run counter-clockwise; our sweep is clockwise. + XDrawArc(display_, window_, gc_, cx - r + 4, cy - r + 4, 2 * (r - 4), 2 * (r - 4), + a0 - sweep, sweep); + xf::set_line_width(display_, gc_, 1); + } + + // Tick marks every 1/8 of the sweep, labels every 1/4. + for (int i = 0; i <= 8; ++i) + { + double f = (double)i / 8.0; + double a = angle_deg(f) * M_PI / 180.0; + int x0 = cx + (int)((r - 10) * std::cos(a)); + int y0 = cy - (int)((r - 10) * std::sin(a)); + int x1 = cx + (int)((r - 2) * std::cos(a)); + int y1 = cy - (int)((r - 2) * std::sin(a)); + XSetForeground(display_, gc_, col(axis_color_)); + XDrawLine(display_, window_, gc_, x0, y0, x1, y1); + if (i % 2 == 0) + { + std::string lab = fmt0(max_ * f / 1000.0); + int lx = cx + (int)((r - 26) * std::cos(a)); + int ly = cy - (int)((r - 26) * std::sin(a)); + xf::draw_text_at(display_, window_, gc_, st, lx - xf::text_width(st, lab) / 2, ly + 4, lab); + } + } + + // Peak-hold tick. + { + double a = angle_deg(peak_frac) * M_PI / 180.0; + int x0 = cx + (int)((r - 16) * std::cos(a)); + int y0 = cy - (int)((r - 16) * std::sin(a)); + int x1 = cx + (int)((r - 4) * std::cos(a)); + int y1 = cy - (int)((r - 4) * std::sin(a)); + XSetForeground(display_, gc_, col(redline_color_)); + xf::set_line_width(display_, gc_, 3); + XDrawLine(display_, window_, gc_, x0, y0, x1, y1); + xf::set_line_width(display_, gc_, 1); + } + + // Needle. + { + double a = angle_deg(frac) * M_PI / 180.0; + int nx = cx + (int)((r - 14) * std::cos(a)); + int ny = cy - (int)((r - 14) * std::sin(a)); + XSetForeground(display_, gc_, col(needle_color_)); + xf::set_line_width(display_, gc_, 3); + XDrawLine(display_, window_, gc_, cx, cy, nx, ny); + xf::set_line_width(display_, gc_, 1); + XFillArc(display_, window_, gc_, cx - 6, cy - 6, 12, 12, 0, 360 * 64); + } + + // Digital readout under the dial. + std::string readout = fmt0(value_) + " (peak " + fmt0(peak_) + ")"; + int rw = xf::text_width(st, readout); + xf::draw_text_at(display_, window_, gc_, st, cx - rw / 2, y2_ - 10, readout); + + XSetForeground(display_, gc_, st.lineColor_); + XDrawRectangle(display_, window_, gc_, x1_, y1_, w, h); + XFlush(display_); + } + + bool on_button_press(int x, int y, unsigned int button) + { + if (button == 1 && contains(x, y)) + { + reset_peak(); + return true; + } + return false; + } + bool on_button_release(int x, int y, unsigned int button) + { + (void)x; + (void)y; + (void)button; + return false; + } + bool on_motion(int x, int y) + { + (void)x; + (void)y; + return false; + } + bool on_key(KeySym key, const char* str) + { + (void)key; + (void)str; + return false; + } +}; diff --git a/widgets/components/XTachometer/core/xframe_core.h b/widgets/components/XTachometer/core/xframe_core.h new file mode 100644 index 0000000..098187d --- /dev/null +++ b/widgets/components/XTachometer/core/xframe_core.h @@ -0,0 +1,988 @@ +#pragma once +#ifndef XFRAME_CORE_H +#define XFRAME_CORE_H + +// +// xframe_core.h - shared core for the XFrame widget library. +// +// Everything in here follows the format of the "Writing Your Own +// X-11 Framework Basics" articles (hotconfig.com): +// +// Part 1 - Display / Screen / Window boilerplate, the GC as a +// "pen", the drawing primitives, and the Canvas class. +// Part 2 - widgets are standalone classes that record display_, +// screen_, window_ and gc_ at construction time. +// Part 3 - SStyle_Manager tracks named styles in a vector. +// Part 4 - a polished widget is built from primitives: two fill +// rectangles + four fill arcs make a rounded body, four +// lines + four draw arcs make its outline, and text is +// centred with XTextWidth + font ascent / descent. +// +// Two small completions versus the articles, needed by Part 4's own +// draw code: SStyle gains lineColor_ and text_color_, and the style +// manager gains the delete_style() function Part 3 left as a reader +// challenge, plus bounds-safe getters. +// + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +//region SStyle + +struct Srgb + +{ + + int r; + + int g; + + int b; + +}; + +struct SStyle + +{ + + unsigned long fillColor_ = 0; + + unsigned long outlineColor_ = 0; + + unsigned long lineColor_ = 0; + + unsigned long text_color_ = 0; + + unsigned long radius_ = 0; + + unsigned long lineWidth_ = 1; + + XFontStruct *font_ = nullptr; + + std::string style_name_; + +}; + +//endregion + +//region SStyle_Manager + +class SStyle_Manager + +{ + +private: + + std::vector styles_; + + Display *display_; + + Window window_; + + int screen_; + + GC gc_; + + int active_style = 0; // just set the default to first passed. + +public: + + SStyle_Manager(Display *display, Window window, int screen, GC gc) + + { + + display_ = display; + + window_ = window; + + screen_ = screen; + + gc_ = gc; + + } + + // NOTE (test agent, Nov 26 2026): do NOT XFreeFont here. Every demo + // main calls XCloseDisplay() before this destructor runs (stack + // unwinding after main returns), so XFreeFont would operate on a + // closed Display and corrupt the heap (SIGSEGV/SIGABRT inside + // XFreeFont on every clean exit). The fonts are process-lifetime + // resources; the X server reclaims them when the client disconnects. + // Use delete_style() to free a single style while the display is + // still open. + ~SStyle_Manager() + + { + + styles_.clear(); + + } + + int add_style(std::string styleName, Srgb fillColor, Srgb outlineColor, int radius, int linewidth, std::string fontName) + + { + + return add_style_ex(styleName, fillColor, outlineColor, outlineColor, Srgb{0, 0, 0}, radius, linewidth, fontName); + + } + + int add_style_ex(std::string styleName, Srgb fillColor, Srgb outlineColor, Srgb lineColor, Srgb textColor, int radius, int linewidth, std::string fontName) + + { + + SStyle nstyle; + + nstyle.style_name_ = std::move(styleName); + + nstyle.fillColor_ = get_color_Srgb(fillColor); + + nstyle.outlineColor_ = get_color_Srgb(outlineColor); + + nstyle.lineColor_ = get_color_Srgb(lineColor); + + nstyle.text_color_ = get_color_Srgb(textColor); + + nstyle.radius_ = radius; + + nstyle.lineWidth_ = linewidth; + + // Define fallback fonts to try if the requested font fails + + const char* fallbackFonts[] = { "fixed", "helvetica" }; + + size_t numFallbacks = sizeof(fallbackFonts) / sizeof(*fallbackFonts); + + nstyle.font_ = nullptr; + + // Attempt to load the primary font (if provided and not empty) + + if (!fontName.empty()) { + + nstyle.font_ = XLoadQueryFont(display_, fontName.c_str()); + + } + + // If the primary failed, iterate through fallback fonts until one succeeds or all are exhausted + + if (!nstyle.font_) { + + for (size_t i = 0; i < numFallbacks && !nstyle.font_; ++i) { + + nstyle.font_ = XLoadQueryFont(display_, fallbackFonts[i]); + + } + + } + + // Final validation: if no font loaded, report which fonts were attempted and exit + + if (!nstyle.font_) + + { + + printf("Failed to load any font (tried: "); + + if (!fontName.empty()) printf("%s", fontName.c_str()); + + for (size_t i = 0; i < numFallbacks; ++i) { + + if (!fontName.empty() || i > 0) printf(", "); + + printf("%s", fallbackFonts[i]); + + } + + printf(")\n"); + + return -1; + + } + + styles_.push_back(nstyle); + + return 0; + + } + + // The delete function Part 3 left as a challenge: remove a style + // by name, freeing its font, and keep active_style in range. + + int delete_style(std::string styleName) + + { + + for (size_t i = 0; i < styles_.size(); ++i) + + { + + if (styles_[i].style_name_ == styleName) + + { + + if (styles_[i].font_) + + { + + XFreeFont(display_, styles_[i].font_); + + } + + styles_.erase(styles_.begin() + static_cast(i)); + + if (active_style >= (int)styles_.size()) + + { + + active_style = styles_.empty() ? 0 : (int)styles_.size() - 1; + + } + + return 0; + + } + + } + + return -1; // Not found + + } + + int style_count() + + { + + return (int)styles_.size(); + + } + + unsigned long get_color_Srgb(Srgb color) + + { + + XColor xcolor; + + xcolor.red = color.r << 8; + + xcolor.green = color.g << 8; + + xcolor.blue = color.b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", color.r, color.g, color.b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + unsigned long get_color_rgb(unsigned char r, unsigned char g, unsigned char b) + + { + + XColor xcolor; + + xcolor.red = r << 8; + + xcolor.green = g << 8; + + xcolor.blue = b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", r, g, b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + int set_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + active_style = static_cast(std::distance(styles_.begin(), it)); + + return active_style; + + } + + return -1; // Not found + + } + + SStyle get_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + return *it; + + } + + return get_active_style(); + + } + + SStyle get_active_style() + + { + + if (styles_.empty()) + + { + + return SStyle{}; + + } + + if (active_style < 0 || active_style >= (int)styles_.size()) + + { + + active_style = 0; + + } + + return styles_[active_style]; + + } + + SStyle get_index_style(int index) + + { + + if (index >= 0 && index < (int)styles_.size()) + + { + + return styles_[index]; + + } + + return get_active_style(); + + } + + // Resolve the style a widget should draw with: its own named + // style when set, otherwise the manager's active style. + + SStyle resolve(const std::string& styleName) + + { + + if (!styleName.empty()) + + { + + return get_active_style_byName(styleName); + + } + + return get_active_style(); + + } + +}; + +//endregion + +//region Drawing helpers (Part 4 construction, shared by all widgets) + +namespace xf + +{ + + inline void set_line_width(Display* display, GC gc, int lineWidth) + + { + + unsigned long valuemask = GCLineWidth; + + XGCValues gc_values; + + gc_values.line_width = lineWidth; + + XChangeGC(display, gc, valuemask, &gc_values); + + } + + // The Part 4 rounded body: Rectangle A + Rectangle B + 4 fill arcs. + + inline void fill_rounded_rect(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + int w = x2 - x1; + + int h = y2 - y1; + + if (r <= 0 || w <= 2 * r || h <= 2 * r) + + { + + XFillRectangle(display, window, gc, x1, y1, w, h); + + return; + + } + + XSetForeground(display, gc, style.fillColor_); + + //Rectangle A + + XFillRectangle(display, window, gc, x1 + r, y1, w - 2 * r, h); + + //Rectangle B + + XFillRectangle(display, window, gc, x1, y1 + r, w, h - 2 * r); + + //region Arc C Top Left + + XFillArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XFillArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XFillArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XFillArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // The Part 4 outline: 4 line bars + 4 draw arcs, in lineColor_. + + inline void draw_rounded_outline(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + XSetForeground(display, gc, style.lineColor_); + + set_line_width(display, gc, (int)style.lineWidth_); + + if (r <= 0) + + { + + XDrawRectangle(display, window, gc, x1, y1, x2 - x1, y2 - y1); + + return; + + } + + // 4 Line Bars + + XDrawLine(display, window, gc, x1 + r, y1, x2 - r, y1); + + XDrawLine(display, window, gc, x2, y1 + r, x2, y2 - r); + + XDrawLine(display, window, gc, x1, y1 + r, x1, y2 - r); + + XDrawLine(display, window, gc, x1 + r, y2, x2 - r, y2); + + // 4 Arcs + + XDrawArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XDrawArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XDrawArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XDrawArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // Convenience: body + outline in one call, the polished look. + + inline void draw_polished(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + fill_rounded_rect(display, window, gc, style, x1, y1, x2, y2); + + draw_rounded_outline(display, window, gc, style, x1, y1, x2, y2); + + } + + inline int text_width(const SStyle& style, const std::string& text) + + { + + if (!style.font_) + + { + + return (int)text.size() * 8; + + } + + return XTextWidth(style.font_, text.c_str(), (int)text.length()); + + } + + // Part 4 centred text: XTextWidth + ascent / descent. + + inline void draw_text_centered(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int tw = XTextWidth(style.font_, text.c_str(), (int)text.length()); + + int text_x = x1 + ((x2 - x1) - tw) / 2; + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, text_x, text_y, text.c_str(), (int)text.length()); + + } + + // Left-aligned text, vertically centred in the given band. + + inline void draw_text_left(Display* display, Window window, GC gc, const SStyle& style, int x, int y1, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, x, text_y, text.c_str(), (int)text.length()); + + } + + // Baseline text at an explicit position (labels, grid cells...). + + inline void draw_text_at(Display* display, Window window, GC gc, const SStyle& style, int x, int baseline_y, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + XDrawString(display, window, gc, x, baseline_y, text.c_str(), (int)text.length()); + + } + + inline void draw_checkmark(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + set_line_width(display, gc, 2); + + int s = size / 2; + + XDrawLine(display, window, gc, cx - s, cy, cx - s / 3, cy + s); + + XDrawLine(display, window, gc, cx - s / 3, cy + s, cx + s, cy - s); + + set_line_width(display, gc, 1); + + } + + inline void draw_triangle_down(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy + i, cx + (size - i), cy + i); + + } + + } + + inline void draw_triangle_up(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy - i, cx + (size - i), cy - i); + + } + + } + + inline void draw_triangle_right(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx + i, cy - (size - i), cx + i, cy + (size - i)); + + } + + } + +} + +//endregion + +//region Region Canvas (from Part 1) + +class Canvas + +{ + +private: + + Display* display = nullptr; + + Window window = 0; + + GC gc = nullptr; + + XImage* image = nullptr; + + int width = 0; + + int height = 0; + + std::vector pixels; + +public: + + Canvas(Display* display, Window window, GC graphicsContext, int width, int height) : display(display), window(window), gc(graphicsContext) + + { + + resize(width, height); + + } + + ~Canvas() { destroyImage(); } + + void resize(int newWidth, int newHeight) + + { + + width = std::max(1, newWidth); + + height = std::max(1, newHeight); + + pixels.resize(static_cast(width) * height); + + clear(0x202020); + + destroyImage(); + + Visual* visual = DefaultVisual(display, DefaultScreen(display)); + + int depth = DefaultDepth(display, DefaultScreen(display)); + + image = XCreateImage(display, visual, depth, ZPixmap, 0, nullptr, width, height, 32, 0); + + if (!image) + + { + + std::exit(1); + + } + + image->data = static_cast(std::malloc(image->bytes_per_line * height)); + + if (!image->data) + + { + + std::exit(1); + + } + + } + + int getWidth() const + + { + + return width; + + } + + int getHeight() const + + { + + return height; + + } + + // Direct access to the raw framebuffer. + + uint32_t* data() + + { + + return pixels.data(); + + } + + const uint32_t* data() const + + { + + return pixels.data(); + + } + + void clear(uint32_t rgb) + + { + + std::fill(pixels.begin(), pixels.end(), rgb); + + } + + void setPixel(int x, int y, uint32_t rgb) + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return; + + } + + pixels[static_cast(y) * width + x] = rgb; + + } + + uint32_t getPixel(int x, int y) const + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return 0; + + } + + return pixels[static_cast(y) * width + x]; + + } + + void fillRect(int x, int y, int w, int h, uint32_t rgb) + + { + + int x0 = std::max(0, x); + + int y0 = std::max(0, y); + + int x1 = std::min(width, x + w); + + int y1 = std::min(height, y + h); + + for (int py = y0; py < y1; ++py) + + { + + for (int px = x0; px < x1; ++px) + + { + + setPixel(px, py, rgb); + + } + + } + + } + + void drawGradient() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint8_t red = static_cast(255.0 * x / std::max(1, width - 1)); + + uint8_t green = static_cast(255.0 * y / std::max(1, height - 1)); + + uint8_t blue = 100; + + uint32_t color = (static_cast(red) << 16) | (static_cast(green) << 8) | blue; + + setPixel(x, y, color); + + } + + } + + } + + void present() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint32_t rgb = getPixel(x, y); + + unsigned long x11Pixel = ((rgb >> 16) & 0xff) << 16 | ((rgb >> 8) & 0xff) << 8 | (rgb & 0xff); + + XPutPixel(image, x, y, x11Pixel); + + } + + } + + XPutImage(display, window, gc, image, 0, 0, 0, 0, width, height); + + XFlush(display); + + } + +private: + + void destroyImage() + + { + + if (image) + + { + + XDestroyImage(image); + + image = nullptr; + + } + + } + +}; + +//endregion +#endif // XFRAME_CORE_H diff --git a/widgets/components/XTachometer/main_XTachometer.cpp b/widgets/components/XTachometer/main_XTachometer.cpp new file mode 100644 index 0000000..de90261 --- /dev/null +++ b/widgets/components/XTachometer/main_XTachometer.cpp @@ -0,0 +1,69 @@ +#include "XTachometer.h" + +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XTachometer - XFrame demo"); + XSelectInput(display, window, ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask); + Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False); + XSetWMProtocols(display, window, &deleteWindowMessage, 1); + XMapWindow(display, window); + GC gc = DefaultGC(display, screen); + SStyle_Manager styles(display, window, screen, gc); + styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120}, Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22}, Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.set_active_style_byName("default"); + XTachometer widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + while (running) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.on_motion(event.xmotion.x, event.xmotion.y); + break; + case KeyPress: + { + char buffer[32]; + KeySym key = 0; + int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr); + buffer[n] = 0; + handled = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + } + + XCloseDisplay(display); + return 0; +} diff --git a/widgets/components/XUsageBar/CMakeLists.txt b/widgets/components/XUsageBar/CMakeLists.txt new file mode 100644 index 0000000..b9d8e28 --- /dev/null +++ b/widgets/components/XUsageBar/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.20) + +project(XUsageBar LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) + +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(X11 REQUIRED) + +add_executable(XUsageBar main_XUsageBar.cpp) + +target_link_libraries(XUsageBar PRIVATE X11::X11) diff --git a/widgets/components/XUsageBar/XUsageBar.h b/widgets/components/XUsageBar/XUsageBar.h new file mode 100644 index 0000000..2466c65 --- /dev/null +++ b/widgets/components/XUsageBar/XUsageBar.h @@ -0,0 +1,173 @@ +#pragma once + +#include "core/xframe_core.h" + +#include +#include +#include + +// region XUsageBar +// +// A horizontal gradient usage bar (btop memory/disk style): label on the +// left, a green -> yellow -> red gradient fill, % text on the right. +// Raw X11, same as the rest of the XFrame set. +// +class XUsageBar +{ +private: + Display* display_; + int screen_; + Window window_; + GC gc_; + SStyle_Manager* styles_; + int x1_, y1_, x2_, y2_; + std::string name_; + std::string text_; + std::string style_name_; + bool visible_ = true; + bool enabled_ = true; + bool focused_ = false; + + double value_ = 0.0; // 0..100 + double max_ = 100.0; + + Srgb white_color_{255, 255, 255}; + Srgb axis_color_{150, 150, 150}; + Srgb track_color_{230, 230, 230}; + + unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); } + + // Gradient stop: green (0%) -> yellow (60%) -> red (100%). + static Srgb gradient(double frac) + { + frac = std::max(0.0, std::min(1.0, frac)); + if (frac < 0.6) + { + double t = frac / 0.6; + return Srgb{(int)(0 + t * 210), (int)(180 - t * 10), 40}; + } + double t = (frac - 0.6) / 0.4; + return Srgb{(int)(210 + t * 10), (int)(170 - t * 130), (int)(40 - t * 10)}; + } + +public: + XUsageBar(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height) + { + display_ = display; + screen_ = screen; + window_ = window; + gc_ = gc; + styles_ = styles; + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + name_ = "XUsageBar"; + text_ = "CPU"; + value_ = 63.0; + } + + ~XUsageBar() {} + + // region Standard plumbing + void set_bounds(int x, int y, int width, int height) + { + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + } + int width() { return x2_ - x1_; } + int height() { return y2_ - y1_; } + bool contains(int x, int y) + { + return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_; + } + void set_visible(bool v) { visible_ = v; } + bool get_visible() { return visible_; } + void set_enabled(bool e) { enabled_ = e; } + bool get_enabled() { return enabled_; } + void set_style_name(std::string s) { style_name_ = s; } + void set_text(std::string t) { text_ = t; } + std::string get_text() { return text_; } + SStyle style() { return styles_->resolve(style_name_); } + // endregion + + // region Bar API + void set_value(double v) { value_ = std::max(0.0, std::min(max_, v)); } + double get_value() { return value_; } + void set_max(double m) { max_ = m > 0.0 ? m : 100.0; } + double get_max() { return max_; } + double fraction() { return max_ > 0.0 ? value_ / max_ : 0.0; } + // endregion + + void draw() + { + if (!visible_) return; + SStyle st = style(); + int w = width(); + int h = height(); + XSetForeground(display_, gc_, col(white_color_)); + XFillRectangle(display_, window_, gc_, x1_, y1_, w, h); + + // Label left, % right. + xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + (h + 12) / 2 - 4, text_); + char pct[16]; + std::snprintf(pct, sizeof(pct), "%.0f%%", fraction() * 100.0); + int pct_w = xf::text_width(st, pct); + xf::draw_text_at(display_, window_, gc_, st, x2_ - 8 - pct_w, y1_ + (h + 12) / 2 - 4, pct); + + // Bar track between label and %. + int label_w = xf::text_width(st, text_) + 16; + int bx = x1_ + label_w; + int bw = std::max(10, x2_ - 16 - pct_w - bx); + int bh = std::max(8, h - 16); + int by = y1_ + (h - bh) / 2; + XSetForeground(display_, gc_, col(track_color_)); + XFillRectangle(display_, window_, gc_, bx, by, bw, bh); + // Gradient fill: 48 slices across the filled fraction. + int filled = (int)(bw * fraction()); + const int slices = 48; + for (int i = 0; i < slices; ++i) + { + int sx = bx + filled * i / slices; + int sw = filled * (i + 1) / slices - filled * i / slices; + if (sw <= 0) continue; + XSetForeground(display_, gc_, col(gradient((double)i / slices * fraction()))); + XFillRectangle(display_, window_, gc_, sx, by, sw, bh); + } + XSetForeground(display_, gc_, col(axis_color_)); + XDrawRectangle(display_, window_, gc_, bx, by, bw, bh); + + XSetForeground(display_, gc_, st.lineColor_); + XDrawRectangle(display_, window_, gc_, x1_, y1_, w, h); + XFlush(display_); + } + + bool on_button_press(int x, int y, unsigned int button) + { + (void)x; + (void)y; + (void)button; + return false; + } + bool on_button_release(int x, int y, unsigned int button) + { + (void)x; + (void)y; + (void)button; + return false; + } + bool on_motion(int x, int y) + { + (void)x; + (void)y; + return false; + } + bool on_key(KeySym key, const char* str) + { + (void)key; + (void)str; + return false; + } +}; diff --git a/widgets/components/XUsageBar/core/xframe_core.h b/widgets/components/XUsageBar/core/xframe_core.h new file mode 100644 index 0000000..098187d --- /dev/null +++ b/widgets/components/XUsageBar/core/xframe_core.h @@ -0,0 +1,988 @@ +#pragma once +#ifndef XFRAME_CORE_H +#define XFRAME_CORE_H + +// +// xframe_core.h - shared core for the XFrame widget library. +// +// Everything in here follows the format of the "Writing Your Own +// X-11 Framework Basics" articles (hotconfig.com): +// +// Part 1 - Display / Screen / Window boilerplate, the GC as a +// "pen", the drawing primitives, and the Canvas class. +// Part 2 - widgets are standalone classes that record display_, +// screen_, window_ and gc_ at construction time. +// Part 3 - SStyle_Manager tracks named styles in a vector. +// Part 4 - a polished widget is built from primitives: two fill +// rectangles + four fill arcs make a rounded body, four +// lines + four draw arcs make its outline, and text is +// centred with XTextWidth + font ascent / descent. +// +// Two small completions versus the articles, needed by Part 4's own +// draw code: SStyle gains lineColor_ and text_color_, and the style +// manager gains the delete_style() function Part 3 left as a reader +// challenge, plus bounds-safe getters. +// + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +//region SStyle + +struct Srgb + +{ + + int r; + + int g; + + int b; + +}; + +struct SStyle + +{ + + unsigned long fillColor_ = 0; + + unsigned long outlineColor_ = 0; + + unsigned long lineColor_ = 0; + + unsigned long text_color_ = 0; + + unsigned long radius_ = 0; + + unsigned long lineWidth_ = 1; + + XFontStruct *font_ = nullptr; + + std::string style_name_; + +}; + +//endregion + +//region SStyle_Manager + +class SStyle_Manager + +{ + +private: + + std::vector styles_; + + Display *display_; + + Window window_; + + int screen_; + + GC gc_; + + int active_style = 0; // just set the default to first passed. + +public: + + SStyle_Manager(Display *display, Window window, int screen, GC gc) + + { + + display_ = display; + + window_ = window; + + screen_ = screen; + + gc_ = gc; + + } + + // NOTE (test agent, Nov 26 2026): do NOT XFreeFont here. Every demo + // main calls XCloseDisplay() before this destructor runs (stack + // unwinding after main returns), so XFreeFont would operate on a + // closed Display and corrupt the heap (SIGSEGV/SIGABRT inside + // XFreeFont on every clean exit). The fonts are process-lifetime + // resources; the X server reclaims them when the client disconnects. + // Use delete_style() to free a single style while the display is + // still open. + ~SStyle_Manager() + + { + + styles_.clear(); + + } + + int add_style(std::string styleName, Srgb fillColor, Srgb outlineColor, int radius, int linewidth, std::string fontName) + + { + + return add_style_ex(styleName, fillColor, outlineColor, outlineColor, Srgb{0, 0, 0}, radius, linewidth, fontName); + + } + + int add_style_ex(std::string styleName, Srgb fillColor, Srgb outlineColor, Srgb lineColor, Srgb textColor, int radius, int linewidth, std::string fontName) + + { + + SStyle nstyle; + + nstyle.style_name_ = std::move(styleName); + + nstyle.fillColor_ = get_color_Srgb(fillColor); + + nstyle.outlineColor_ = get_color_Srgb(outlineColor); + + nstyle.lineColor_ = get_color_Srgb(lineColor); + + nstyle.text_color_ = get_color_Srgb(textColor); + + nstyle.radius_ = radius; + + nstyle.lineWidth_ = linewidth; + + // Define fallback fonts to try if the requested font fails + + const char* fallbackFonts[] = { "fixed", "helvetica" }; + + size_t numFallbacks = sizeof(fallbackFonts) / sizeof(*fallbackFonts); + + nstyle.font_ = nullptr; + + // Attempt to load the primary font (if provided and not empty) + + if (!fontName.empty()) { + + nstyle.font_ = XLoadQueryFont(display_, fontName.c_str()); + + } + + // If the primary failed, iterate through fallback fonts until one succeeds or all are exhausted + + if (!nstyle.font_) { + + for (size_t i = 0; i < numFallbacks && !nstyle.font_; ++i) { + + nstyle.font_ = XLoadQueryFont(display_, fallbackFonts[i]); + + } + + } + + // Final validation: if no font loaded, report which fonts were attempted and exit + + if (!nstyle.font_) + + { + + printf("Failed to load any font (tried: "); + + if (!fontName.empty()) printf("%s", fontName.c_str()); + + for (size_t i = 0; i < numFallbacks; ++i) { + + if (!fontName.empty() || i > 0) printf(", "); + + printf("%s", fallbackFonts[i]); + + } + + printf(")\n"); + + return -1; + + } + + styles_.push_back(nstyle); + + return 0; + + } + + // The delete function Part 3 left as a challenge: remove a style + // by name, freeing its font, and keep active_style in range. + + int delete_style(std::string styleName) + + { + + for (size_t i = 0; i < styles_.size(); ++i) + + { + + if (styles_[i].style_name_ == styleName) + + { + + if (styles_[i].font_) + + { + + XFreeFont(display_, styles_[i].font_); + + } + + styles_.erase(styles_.begin() + static_cast(i)); + + if (active_style >= (int)styles_.size()) + + { + + active_style = styles_.empty() ? 0 : (int)styles_.size() - 1; + + } + + return 0; + + } + + } + + return -1; // Not found + + } + + int style_count() + + { + + return (int)styles_.size(); + + } + + unsigned long get_color_Srgb(Srgb color) + + { + + XColor xcolor; + + xcolor.red = color.r << 8; + + xcolor.green = color.g << 8; + + xcolor.blue = color.b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", color.r, color.g, color.b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + unsigned long get_color_rgb(unsigned char r, unsigned char g, unsigned char b) + + { + + XColor xcolor; + + xcolor.red = r << 8; + + xcolor.green = g << 8; + + xcolor.blue = b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", r, g, b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + int set_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + active_style = static_cast(std::distance(styles_.begin(), it)); + + return active_style; + + } + + return -1; // Not found + + } + + SStyle get_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + return *it; + + } + + return get_active_style(); + + } + + SStyle get_active_style() + + { + + if (styles_.empty()) + + { + + return SStyle{}; + + } + + if (active_style < 0 || active_style >= (int)styles_.size()) + + { + + active_style = 0; + + } + + return styles_[active_style]; + + } + + SStyle get_index_style(int index) + + { + + if (index >= 0 && index < (int)styles_.size()) + + { + + return styles_[index]; + + } + + return get_active_style(); + + } + + // Resolve the style a widget should draw with: its own named + // style when set, otherwise the manager's active style. + + SStyle resolve(const std::string& styleName) + + { + + if (!styleName.empty()) + + { + + return get_active_style_byName(styleName); + + } + + return get_active_style(); + + } + +}; + +//endregion + +//region Drawing helpers (Part 4 construction, shared by all widgets) + +namespace xf + +{ + + inline void set_line_width(Display* display, GC gc, int lineWidth) + + { + + unsigned long valuemask = GCLineWidth; + + XGCValues gc_values; + + gc_values.line_width = lineWidth; + + XChangeGC(display, gc, valuemask, &gc_values); + + } + + // The Part 4 rounded body: Rectangle A + Rectangle B + 4 fill arcs. + + inline void fill_rounded_rect(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + int w = x2 - x1; + + int h = y2 - y1; + + if (r <= 0 || w <= 2 * r || h <= 2 * r) + + { + + XFillRectangle(display, window, gc, x1, y1, w, h); + + return; + + } + + XSetForeground(display, gc, style.fillColor_); + + //Rectangle A + + XFillRectangle(display, window, gc, x1 + r, y1, w - 2 * r, h); + + //Rectangle B + + XFillRectangle(display, window, gc, x1, y1 + r, w, h - 2 * r); + + //region Arc C Top Left + + XFillArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XFillArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XFillArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XFillArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // The Part 4 outline: 4 line bars + 4 draw arcs, in lineColor_. + + inline void draw_rounded_outline(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + XSetForeground(display, gc, style.lineColor_); + + set_line_width(display, gc, (int)style.lineWidth_); + + if (r <= 0) + + { + + XDrawRectangle(display, window, gc, x1, y1, x2 - x1, y2 - y1); + + return; + + } + + // 4 Line Bars + + XDrawLine(display, window, gc, x1 + r, y1, x2 - r, y1); + + XDrawLine(display, window, gc, x2, y1 + r, x2, y2 - r); + + XDrawLine(display, window, gc, x1, y1 + r, x1, y2 - r); + + XDrawLine(display, window, gc, x1 + r, y2, x2 - r, y2); + + // 4 Arcs + + XDrawArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XDrawArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XDrawArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XDrawArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // Convenience: body + outline in one call, the polished look. + + inline void draw_polished(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + fill_rounded_rect(display, window, gc, style, x1, y1, x2, y2); + + draw_rounded_outline(display, window, gc, style, x1, y1, x2, y2); + + } + + inline int text_width(const SStyle& style, const std::string& text) + + { + + if (!style.font_) + + { + + return (int)text.size() * 8; + + } + + return XTextWidth(style.font_, text.c_str(), (int)text.length()); + + } + + // Part 4 centred text: XTextWidth + ascent / descent. + + inline void draw_text_centered(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int tw = XTextWidth(style.font_, text.c_str(), (int)text.length()); + + int text_x = x1 + ((x2 - x1) - tw) / 2; + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, text_x, text_y, text.c_str(), (int)text.length()); + + } + + // Left-aligned text, vertically centred in the given band. + + inline void draw_text_left(Display* display, Window window, GC gc, const SStyle& style, int x, int y1, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, x, text_y, text.c_str(), (int)text.length()); + + } + + // Baseline text at an explicit position (labels, grid cells...). + + inline void draw_text_at(Display* display, Window window, GC gc, const SStyle& style, int x, int baseline_y, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + XDrawString(display, window, gc, x, baseline_y, text.c_str(), (int)text.length()); + + } + + inline void draw_checkmark(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + set_line_width(display, gc, 2); + + int s = size / 2; + + XDrawLine(display, window, gc, cx - s, cy, cx - s / 3, cy + s); + + XDrawLine(display, window, gc, cx - s / 3, cy + s, cx + s, cy - s); + + set_line_width(display, gc, 1); + + } + + inline void draw_triangle_down(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy + i, cx + (size - i), cy + i); + + } + + } + + inline void draw_triangle_up(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy - i, cx + (size - i), cy - i); + + } + + } + + inline void draw_triangle_right(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx + i, cy - (size - i), cx + i, cy + (size - i)); + + } + + } + +} + +//endregion + +//region Region Canvas (from Part 1) + +class Canvas + +{ + +private: + + Display* display = nullptr; + + Window window = 0; + + GC gc = nullptr; + + XImage* image = nullptr; + + int width = 0; + + int height = 0; + + std::vector pixels; + +public: + + Canvas(Display* display, Window window, GC graphicsContext, int width, int height) : display(display), window(window), gc(graphicsContext) + + { + + resize(width, height); + + } + + ~Canvas() { destroyImage(); } + + void resize(int newWidth, int newHeight) + + { + + width = std::max(1, newWidth); + + height = std::max(1, newHeight); + + pixels.resize(static_cast(width) * height); + + clear(0x202020); + + destroyImage(); + + Visual* visual = DefaultVisual(display, DefaultScreen(display)); + + int depth = DefaultDepth(display, DefaultScreen(display)); + + image = XCreateImage(display, visual, depth, ZPixmap, 0, nullptr, width, height, 32, 0); + + if (!image) + + { + + std::exit(1); + + } + + image->data = static_cast(std::malloc(image->bytes_per_line * height)); + + if (!image->data) + + { + + std::exit(1); + + } + + } + + int getWidth() const + + { + + return width; + + } + + int getHeight() const + + { + + return height; + + } + + // Direct access to the raw framebuffer. + + uint32_t* data() + + { + + return pixels.data(); + + } + + const uint32_t* data() const + + { + + return pixels.data(); + + } + + void clear(uint32_t rgb) + + { + + std::fill(pixels.begin(), pixels.end(), rgb); + + } + + void setPixel(int x, int y, uint32_t rgb) + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return; + + } + + pixels[static_cast(y) * width + x] = rgb; + + } + + uint32_t getPixel(int x, int y) const + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return 0; + + } + + return pixels[static_cast(y) * width + x]; + + } + + void fillRect(int x, int y, int w, int h, uint32_t rgb) + + { + + int x0 = std::max(0, x); + + int y0 = std::max(0, y); + + int x1 = std::min(width, x + w); + + int y1 = std::min(height, y + h); + + for (int py = y0; py < y1; ++py) + + { + + for (int px = x0; px < x1; ++px) + + { + + setPixel(px, py, rgb); + + } + + } + + } + + void drawGradient() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint8_t red = static_cast(255.0 * x / std::max(1, width - 1)); + + uint8_t green = static_cast(255.0 * y / std::max(1, height - 1)); + + uint8_t blue = 100; + + uint32_t color = (static_cast(red) << 16) | (static_cast(green) << 8) | blue; + + setPixel(x, y, color); + + } + + } + + } + + void present() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint32_t rgb = getPixel(x, y); + + unsigned long x11Pixel = ((rgb >> 16) & 0xff) << 16 | ((rgb >> 8) & 0xff) << 8 | (rgb & 0xff); + + XPutPixel(image, x, y, x11Pixel); + + } + + } + + XPutImage(display, window, gc, image, 0, 0, 0, 0, width, height); + + XFlush(display); + + } + +private: + + void destroyImage() + + { + + if (image) + + { + + XDestroyImage(image); + + image = nullptr; + + } + + } + +}; + +//endregion +#endif // XFRAME_CORE_H diff --git a/widgets/components/XUsageBar/main_XUsageBar.cpp b/widgets/components/XUsageBar/main_XUsageBar.cpp new file mode 100644 index 0000000..249efb7 --- /dev/null +++ b/widgets/components/XUsageBar/main_XUsageBar.cpp @@ -0,0 +1,69 @@ +#include "XUsageBar.h" + +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XUsageBar - XFrame demo"); + XSelectInput(display, window, ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask); + Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False); + XSetWMProtocols(display, window, &deleteWindowMessage, 1); + XMapWindow(display, window); + GC gc = DefaultGC(display, screen); + SStyle_Manager styles(display, window, screen, gc); + styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120}, Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22}, Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.set_active_style_byName("default"); + XUsageBar widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + while (running) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.on_motion(event.xmotion.x, event.xmotion.y); + break; + case KeyPress: + { + char buffer[32]; + KeySym key = 0; + int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr); + buffer[n] = 0; + handled = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + } + + XCloseDisplay(display); + return 0; +} diff --git a/widgets/components/XVerticalBar/CMakeLists.txt b/widgets/components/XVerticalBar/CMakeLists.txt new file mode 100644 index 0000000..fa64782 --- /dev/null +++ b/widgets/components/XVerticalBar/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.20) + +project(XVerticalBar LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) + +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(X11 REQUIRED) + +add_executable(XVerticalBar main_XVerticalBar.cpp) + +target_link_libraries(XVerticalBar PRIVATE X11::X11) diff --git a/widgets/components/XVerticalBar/XVerticalBar.h b/widgets/components/XVerticalBar/XVerticalBar.h new file mode 100644 index 0000000..4e9d4cc --- /dev/null +++ b/widgets/components/XVerticalBar/XVerticalBar.h @@ -0,0 +1,201 @@ +#pragma once + +#include "core/xframe_core.h" + +#include +#include +#include + +// region XVerticalBar +// +// A vertical bar meter with a scale on the left and a peak-hold marker: +// the thin line (and peak value) stays at the highest recent value. +// Raw X11, same as the rest of the XFrame set. +// +class XVerticalBar +{ +private: + Display* display_; + int screen_; + Window window_; + GC gc_; + SStyle_Manager* styles_; + int x1_, y1_, x2_, y2_; + std::string name_; + std::string text_; + std::string style_name_; + bool visible_ = true; + bool enabled_ = true; + bool focused_ = false; + + double value_ = 0.0; + double max_ = 100.0; + double peak_ = 0.0; + bool peak_hold_ = true; + + Srgb white_color_{255, 255, 255}; + Srgb grid_color_{225, 225, 225}; + Srgb axis_color_{150, 150, 150}; + Srgb fill_color_{0, 170, 70}; + Srgb peak_color_{210, 40, 30}; + + unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); } + + static std::string fmt0(double v) + { + char buf[32]; + std::snprintf(buf, sizeof(buf), "%.0f", v); + return buf; + } + +public: + XVerticalBar(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height) + { + display_ = display; + screen_ = screen; + window_ = window; + gc_ = gc; + styles_ = styles; + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + name_ = "XVerticalBar"; + text_ = "Load"; + value_ = 72.0; + peak_ = 91.0; + } + + ~XVerticalBar() {} + + // region Standard plumbing + void set_bounds(int x, int y, int width, int height) + { + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + } + int width() { return x2_ - x1_; } + int height() { return y2_ - y1_; } + bool contains(int x, int y) + { + return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_; + } + void set_visible(bool v) { visible_ = v; } + bool get_visible() { return visible_; } + void set_enabled(bool e) { enabled_ = e; } + bool get_enabled() { return enabled_; } + void set_style_name(std::string s) { style_name_ = s; } + void set_text(std::string t) { text_ = t; } + std::string get_text() { return text_; } + SStyle style() { return styles_->resolve(style_name_); } + // endregion + + // region Bar API + void set_value(double v) + { + value_ = std::max(0.0, std::min(max_, v)); + if (peak_hold_) peak_ = std::max(peak_, value_); + } + double get_value() { return value_; } + void set_max(double m) + { + max_ = m > 0.0 ? m : 100.0; + value_ = std::min(value_, max_); + peak_ = std::min(peak_, max_); + } + double get_max() { return max_; } + double peak() { return peak_; } + void reset_peak() { peak_ = value_; } + void set_peak_hold(bool on) { peak_hold_ = on; } + // endregion + + void draw() + { + if (!visible_) return; + SStyle st = style(); + int w = width(); + int h = height(); + XSetForeground(display_, gc_, col(white_color_)); + XFillRectangle(display_, window_, gc_, x1_, y1_, w, h); + + const int title_h = 20; + const int scale_w = 36; + const int bottom_h = 18; + int bx = x1_ + scale_w + 8; + int by = y1_ + title_h + 16; + int bw = std::max(10, x2_ - 8 - bx); + int bh = std::max(10, y2_ - bottom_h - by); + + xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_); + + // Scale ticks + labels (0 / 25 / 50 / 75 / 100 % of max). + for (int g = 0; g <= 4; ++g) + { + int gy = by + bh * g / 4; + XSetForeground(display_, gc_, col(grid_color_)); + XDrawLine(display_, window_, gc_, bx, gy, bx + bw, gy); + XSetForeground(display_, gc_, col(axis_color_)); + XDrawLine(display_, window_, gc_, bx - 4, gy, bx, gy); + std::string lab = fmt0(max_ * (4 - g) / 4.0); + xf::draw_text_at(display_, window_, gc_, st, bx - 8 - xf::text_width(st, lab), gy + 4, lab); + } + + // Fill from the bottom. + int filled = (int)(bh * value_ / max_); + XSetForeground(display_, gc_, col(fill_color_)); + XFillRectangle(display_, window_, gc_, bx, by + bh - filled, bw, filled); + XSetForeground(display_, gc_, col(axis_color_)); + XDrawRectangle(display_, window_, gc_, bx, by, bw, bh); + + // Peak-hold marker: red line across the bar + value. + if (peak_hold_) + { + int py = by + bh - (int)(bh * peak_ / max_); + XSetForeground(display_, gc_, col(peak_color_)); + xf::set_line_width(display_, gc_, 2); + XDrawLine(display_, window_, gc_, bx - 4, py, bx + bw + 4, py); + xf::set_line_width(display_, gc_, 1); + std::string pk = "peak " + fmt0(peak_); + xf::draw_text_at(display_, window_, gc_, st, bx + bw - xf::text_width(st, pk) - 4, py - 4, pk); + } + + // Current value under the bar. + std::string cur = fmt0(value_) + " / " + fmt0(max_); + xf::draw_text_at(display_, window_, gc_, st, bx, y2_ - 4, cur); + + XSetForeground(display_, gc_, st.lineColor_); + XDrawRectangle(display_, window_, gc_, x1_, y1_, w, h); + XFlush(display_); + } + + bool on_button_press(int x, int y, unsigned int button) + { + if (button == 1 && contains(x, y)) + { + reset_peak(); + return true; + } + return false; + } + bool on_button_release(int x, int y, unsigned int button) + { + (void)x; + (void)y; + (void)button; + return false; + } + bool on_motion(int x, int y) + { + (void)x; + (void)y; + return false; + } + bool on_key(KeySym key, const char* str) + { + (void)key; + (void)str; + return false; + } +}; diff --git a/widgets/components/XVerticalBar/core/xframe_core.h b/widgets/components/XVerticalBar/core/xframe_core.h new file mode 100644 index 0000000..098187d --- /dev/null +++ b/widgets/components/XVerticalBar/core/xframe_core.h @@ -0,0 +1,988 @@ +#pragma once +#ifndef XFRAME_CORE_H +#define XFRAME_CORE_H + +// +// xframe_core.h - shared core for the XFrame widget library. +// +// Everything in here follows the format of the "Writing Your Own +// X-11 Framework Basics" articles (hotconfig.com): +// +// Part 1 - Display / Screen / Window boilerplate, the GC as a +// "pen", the drawing primitives, and the Canvas class. +// Part 2 - widgets are standalone classes that record display_, +// screen_, window_ and gc_ at construction time. +// Part 3 - SStyle_Manager tracks named styles in a vector. +// Part 4 - a polished widget is built from primitives: two fill +// rectangles + four fill arcs make a rounded body, four +// lines + four draw arcs make its outline, and text is +// centred with XTextWidth + font ascent / descent. +// +// Two small completions versus the articles, needed by Part 4's own +// draw code: SStyle gains lineColor_ and text_color_, and the style +// manager gains the delete_style() function Part 3 left as a reader +// challenge, plus bounds-safe getters. +// + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +//region SStyle + +struct Srgb + +{ + + int r; + + int g; + + int b; + +}; + +struct SStyle + +{ + + unsigned long fillColor_ = 0; + + unsigned long outlineColor_ = 0; + + unsigned long lineColor_ = 0; + + unsigned long text_color_ = 0; + + unsigned long radius_ = 0; + + unsigned long lineWidth_ = 1; + + XFontStruct *font_ = nullptr; + + std::string style_name_; + +}; + +//endregion + +//region SStyle_Manager + +class SStyle_Manager + +{ + +private: + + std::vector styles_; + + Display *display_; + + Window window_; + + int screen_; + + GC gc_; + + int active_style = 0; // just set the default to first passed. + +public: + + SStyle_Manager(Display *display, Window window, int screen, GC gc) + + { + + display_ = display; + + window_ = window; + + screen_ = screen; + + gc_ = gc; + + } + + // NOTE (test agent, Nov 26 2026): do NOT XFreeFont here. Every demo + // main calls XCloseDisplay() before this destructor runs (stack + // unwinding after main returns), so XFreeFont would operate on a + // closed Display and corrupt the heap (SIGSEGV/SIGABRT inside + // XFreeFont on every clean exit). The fonts are process-lifetime + // resources; the X server reclaims them when the client disconnects. + // Use delete_style() to free a single style while the display is + // still open. + ~SStyle_Manager() + + { + + styles_.clear(); + + } + + int add_style(std::string styleName, Srgb fillColor, Srgb outlineColor, int radius, int linewidth, std::string fontName) + + { + + return add_style_ex(styleName, fillColor, outlineColor, outlineColor, Srgb{0, 0, 0}, radius, linewidth, fontName); + + } + + int add_style_ex(std::string styleName, Srgb fillColor, Srgb outlineColor, Srgb lineColor, Srgb textColor, int radius, int linewidth, std::string fontName) + + { + + SStyle nstyle; + + nstyle.style_name_ = std::move(styleName); + + nstyle.fillColor_ = get_color_Srgb(fillColor); + + nstyle.outlineColor_ = get_color_Srgb(outlineColor); + + nstyle.lineColor_ = get_color_Srgb(lineColor); + + nstyle.text_color_ = get_color_Srgb(textColor); + + nstyle.radius_ = radius; + + nstyle.lineWidth_ = linewidth; + + // Define fallback fonts to try if the requested font fails + + const char* fallbackFonts[] = { "fixed", "helvetica" }; + + size_t numFallbacks = sizeof(fallbackFonts) / sizeof(*fallbackFonts); + + nstyle.font_ = nullptr; + + // Attempt to load the primary font (if provided and not empty) + + if (!fontName.empty()) { + + nstyle.font_ = XLoadQueryFont(display_, fontName.c_str()); + + } + + // If the primary failed, iterate through fallback fonts until one succeeds or all are exhausted + + if (!nstyle.font_) { + + for (size_t i = 0; i < numFallbacks && !nstyle.font_; ++i) { + + nstyle.font_ = XLoadQueryFont(display_, fallbackFonts[i]); + + } + + } + + // Final validation: if no font loaded, report which fonts were attempted and exit + + if (!nstyle.font_) + + { + + printf("Failed to load any font (tried: "); + + if (!fontName.empty()) printf("%s", fontName.c_str()); + + for (size_t i = 0; i < numFallbacks; ++i) { + + if (!fontName.empty() || i > 0) printf(", "); + + printf("%s", fallbackFonts[i]); + + } + + printf(")\n"); + + return -1; + + } + + styles_.push_back(nstyle); + + return 0; + + } + + // The delete function Part 3 left as a challenge: remove a style + // by name, freeing its font, and keep active_style in range. + + int delete_style(std::string styleName) + + { + + for (size_t i = 0; i < styles_.size(); ++i) + + { + + if (styles_[i].style_name_ == styleName) + + { + + if (styles_[i].font_) + + { + + XFreeFont(display_, styles_[i].font_); + + } + + styles_.erase(styles_.begin() + static_cast(i)); + + if (active_style >= (int)styles_.size()) + + { + + active_style = styles_.empty() ? 0 : (int)styles_.size() - 1; + + } + + return 0; + + } + + } + + return -1; // Not found + + } + + int style_count() + + { + + return (int)styles_.size(); + + } + + unsigned long get_color_Srgb(Srgb color) + + { + + XColor xcolor; + + xcolor.red = color.r << 8; + + xcolor.green = color.g << 8; + + xcolor.blue = color.b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", color.r, color.g, color.b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + unsigned long get_color_rgb(unsigned char r, unsigned char g, unsigned char b) + + { + + XColor xcolor; + + xcolor.red = r << 8; + + xcolor.green = g << 8; + + xcolor.blue = b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", r, g, b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + int set_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + active_style = static_cast(std::distance(styles_.begin(), it)); + + return active_style; + + } + + return -1; // Not found + + } + + SStyle get_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + return *it; + + } + + return get_active_style(); + + } + + SStyle get_active_style() + + { + + if (styles_.empty()) + + { + + return SStyle{}; + + } + + if (active_style < 0 || active_style >= (int)styles_.size()) + + { + + active_style = 0; + + } + + return styles_[active_style]; + + } + + SStyle get_index_style(int index) + + { + + if (index >= 0 && index < (int)styles_.size()) + + { + + return styles_[index]; + + } + + return get_active_style(); + + } + + // Resolve the style a widget should draw with: its own named + // style when set, otherwise the manager's active style. + + SStyle resolve(const std::string& styleName) + + { + + if (!styleName.empty()) + + { + + return get_active_style_byName(styleName); + + } + + return get_active_style(); + + } + +}; + +//endregion + +//region Drawing helpers (Part 4 construction, shared by all widgets) + +namespace xf + +{ + + inline void set_line_width(Display* display, GC gc, int lineWidth) + + { + + unsigned long valuemask = GCLineWidth; + + XGCValues gc_values; + + gc_values.line_width = lineWidth; + + XChangeGC(display, gc, valuemask, &gc_values); + + } + + // The Part 4 rounded body: Rectangle A + Rectangle B + 4 fill arcs. + + inline void fill_rounded_rect(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + int w = x2 - x1; + + int h = y2 - y1; + + if (r <= 0 || w <= 2 * r || h <= 2 * r) + + { + + XFillRectangle(display, window, gc, x1, y1, w, h); + + return; + + } + + XSetForeground(display, gc, style.fillColor_); + + //Rectangle A + + XFillRectangle(display, window, gc, x1 + r, y1, w - 2 * r, h); + + //Rectangle B + + XFillRectangle(display, window, gc, x1, y1 + r, w, h - 2 * r); + + //region Arc C Top Left + + XFillArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XFillArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XFillArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XFillArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // The Part 4 outline: 4 line bars + 4 draw arcs, in lineColor_. + + inline void draw_rounded_outline(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + XSetForeground(display, gc, style.lineColor_); + + set_line_width(display, gc, (int)style.lineWidth_); + + if (r <= 0) + + { + + XDrawRectangle(display, window, gc, x1, y1, x2 - x1, y2 - y1); + + return; + + } + + // 4 Line Bars + + XDrawLine(display, window, gc, x1 + r, y1, x2 - r, y1); + + XDrawLine(display, window, gc, x2, y1 + r, x2, y2 - r); + + XDrawLine(display, window, gc, x1, y1 + r, x1, y2 - r); + + XDrawLine(display, window, gc, x1 + r, y2, x2 - r, y2); + + // 4 Arcs + + XDrawArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XDrawArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XDrawArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XDrawArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // Convenience: body + outline in one call, the polished look. + + inline void draw_polished(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + fill_rounded_rect(display, window, gc, style, x1, y1, x2, y2); + + draw_rounded_outline(display, window, gc, style, x1, y1, x2, y2); + + } + + inline int text_width(const SStyle& style, const std::string& text) + + { + + if (!style.font_) + + { + + return (int)text.size() * 8; + + } + + return XTextWidth(style.font_, text.c_str(), (int)text.length()); + + } + + // Part 4 centred text: XTextWidth + ascent / descent. + + inline void draw_text_centered(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int tw = XTextWidth(style.font_, text.c_str(), (int)text.length()); + + int text_x = x1 + ((x2 - x1) - tw) / 2; + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, text_x, text_y, text.c_str(), (int)text.length()); + + } + + // Left-aligned text, vertically centred in the given band. + + inline void draw_text_left(Display* display, Window window, GC gc, const SStyle& style, int x, int y1, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, x, text_y, text.c_str(), (int)text.length()); + + } + + // Baseline text at an explicit position (labels, grid cells...). + + inline void draw_text_at(Display* display, Window window, GC gc, const SStyle& style, int x, int baseline_y, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + XDrawString(display, window, gc, x, baseline_y, text.c_str(), (int)text.length()); + + } + + inline void draw_checkmark(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + set_line_width(display, gc, 2); + + int s = size / 2; + + XDrawLine(display, window, gc, cx - s, cy, cx - s / 3, cy + s); + + XDrawLine(display, window, gc, cx - s / 3, cy + s, cx + s, cy - s); + + set_line_width(display, gc, 1); + + } + + inline void draw_triangle_down(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy + i, cx + (size - i), cy + i); + + } + + } + + inline void draw_triangle_up(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy - i, cx + (size - i), cy - i); + + } + + } + + inline void draw_triangle_right(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx + i, cy - (size - i), cx + i, cy + (size - i)); + + } + + } + +} + +//endregion + +//region Region Canvas (from Part 1) + +class Canvas + +{ + +private: + + Display* display = nullptr; + + Window window = 0; + + GC gc = nullptr; + + XImage* image = nullptr; + + int width = 0; + + int height = 0; + + std::vector pixels; + +public: + + Canvas(Display* display, Window window, GC graphicsContext, int width, int height) : display(display), window(window), gc(graphicsContext) + + { + + resize(width, height); + + } + + ~Canvas() { destroyImage(); } + + void resize(int newWidth, int newHeight) + + { + + width = std::max(1, newWidth); + + height = std::max(1, newHeight); + + pixels.resize(static_cast(width) * height); + + clear(0x202020); + + destroyImage(); + + Visual* visual = DefaultVisual(display, DefaultScreen(display)); + + int depth = DefaultDepth(display, DefaultScreen(display)); + + image = XCreateImage(display, visual, depth, ZPixmap, 0, nullptr, width, height, 32, 0); + + if (!image) + + { + + std::exit(1); + + } + + image->data = static_cast(std::malloc(image->bytes_per_line * height)); + + if (!image->data) + + { + + std::exit(1); + + } + + } + + int getWidth() const + + { + + return width; + + } + + int getHeight() const + + { + + return height; + + } + + // Direct access to the raw framebuffer. + + uint32_t* data() + + { + + return pixels.data(); + + } + + const uint32_t* data() const + + { + + return pixels.data(); + + } + + void clear(uint32_t rgb) + + { + + std::fill(pixels.begin(), pixels.end(), rgb); + + } + + void setPixel(int x, int y, uint32_t rgb) + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return; + + } + + pixels[static_cast(y) * width + x] = rgb; + + } + + uint32_t getPixel(int x, int y) const + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return 0; + + } + + return pixels[static_cast(y) * width + x]; + + } + + void fillRect(int x, int y, int w, int h, uint32_t rgb) + + { + + int x0 = std::max(0, x); + + int y0 = std::max(0, y); + + int x1 = std::min(width, x + w); + + int y1 = std::min(height, y + h); + + for (int py = y0; py < y1; ++py) + + { + + for (int px = x0; px < x1; ++px) + + { + + setPixel(px, py, rgb); + + } + + } + + } + + void drawGradient() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint8_t red = static_cast(255.0 * x / std::max(1, width - 1)); + + uint8_t green = static_cast(255.0 * y / std::max(1, height - 1)); + + uint8_t blue = 100; + + uint32_t color = (static_cast(red) << 16) | (static_cast(green) << 8) | blue; + + setPixel(x, y, color); + + } + + } + + } + + void present() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint32_t rgb = getPixel(x, y); + + unsigned long x11Pixel = ((rgb >> 16) & 0xff) << 16 | ((rgb >> 8) & 0xff) << 8 | (rgb & 0xff); + + XPutPixel(image, x, y, x11Pixel); + + } + + } + + XPutImage(display, window, gc, image, 0, 0, 0, 0, width, height); + + XFlush(display); + + } + +private: + + void destroyImage() + + { + + if (image) + + { + + XDestroyImage(image); + + image = nullptr; + + } + + } + +}; + +//endregion +#endif // XFRAME_CORE_H diff --git a/widgets/components/XVerticalBar/main_XVerticalBar.cpp b/widgets/components/XVerticalBar/main_XVerticalBar.cpp new file mode 100644 index 0000000..d0d4fea --- /dev/null +++ b/widgets/components/XVerticalBar/main_XVerticalBar.cpp @@ -0,0 +1,69 @@ +#include "XVerticalBar.h" + +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XVerticalBar - XFrame demo"); + XSelectInput(display, window, ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask); + Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False); + XSetWMProtocols(display, window, &deleteWindowMessage, 1); + XMapWindow(display, window); + GC gc = DefaultGC(display, screen); + SStyle_Manager styles(display, window, screen, gc); + styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120}, Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22}, Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1, "-misc-fixed-medium-r-normal--13-*"); + styles.set_active_style_byName("default"); + XVerticalBar widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + while (running) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.on_motion(event.xmotion.x, event.xmotion.y); + break; + case KeyPress: + { + char buffer[32]; + KeySym key = 0; + int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr); + buffer[n] = 0; + handled = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + } + + XCloseDisplay(display); + return 0; +} diff --git a/widgets/screenshots/XArcGauge.png b/widgets/screenshots/XArcGauge.png new file mode 100644 index 0000000000000000000000000000000000000000..d7a4591ef81e1e3a8222b5a23eea54bb281749e7 GIT binary patch literal 2656 zcma)8dr%YC8sAMq5?&z?1O*?52tg4=C=TT%8x69Gh)o3%K`dkxXu)CuB^1fxgJ2Oh z;8mj{w>4ISiWN|)6}-h%76ojBq9_#Qr6MTdLIld=9@O^Unfp&?cIJ2Hd;Gq~IXmB% zvwmHGsgaElf*_`=agi87=m0@zVGKHy#D*2GgaBf2aS(z?1qdSBhah86Bzu7%DN7Ng zcPoN;9YYZ7FHc_o#0M%Y*00^*KQ%Swgnl`NF*yz&RM8MqXPp(h}>Wf zjv$7=t`-S5?0($;SeR(*G4sG@b{q8lrdcPqBEv}eF}kGikEk=Dt?oy@sGG0w%U-$M z#V9$%{b^_Z;numF0{hi}elH6rL;o=uZald0rr+m51`N3j`D6y^(ta~zbaG;#f8dv& zm!X4v-3py4?hRM~leHS?4!B?APM>I_G?fGS@3-W`o1T>djKN@Y0gWcYIN63&Pf_Ak zUeO**8yAGx)I2g+!vbd=kIURuzg5(g3fC#G#Fj;o$xi0vs@uQyt<@XubPE5}KAqt? zZ!L&jLC}bhNNMa$?fVzT0Z_*-_)2+9_O28sV@3ue1F&Wd?}s|)WnYZ zH&}0uJFDRM$l|E$OtnC3gU5HjeY&F<8XqFuJ9V48%y0+ARZJW>bxSBsj|t($8L1ZL zKlE!9Z-(8y?bDd~&&(r_`KE-bXVRtS)-xkj7QBkZ(%GEA1jRP&@z6OJtq~sIH#*XE zxfEXn8l*RP%4{U=(djbW76hf=pAkn>CFPg!7t2)5zQ5-TnU>M>5AvVv?W;L`iqeBW z1}d$TKN+-q6R=ouQK(JyDfJ&?v=j!+Q6vb(g*St5MDEYd*i06sMdZ}C5q*uPv#28= zE_3cieg@I^N;nlcbgSBqTH;$O>dC{j%OQ@TE#36N@E2#U%%b-AF7fYKhw1ywoyyFq z#ChaHTCw5~rteQ$C{E=+k@fvi<7J&1mq05>eUr4XES;>13VZ(Dl8cKm=>VJ8f~c~M zUq#fPxq{-3bFKN^0`WXmK9o>rLgv_~bV85KWa5$wU!gp;skRAQArQ|}x#Y7IcR*xh z*z8}Y=aqzeFg&qnoFkz;8Br1`aEG46PYMNUO29-?Qrl#{E-U;SM zggwu55QtI9VdAh7Hy}S@insA~pwmP}i8_S0XW@#ReS34=NeiAKYgQa^5LlSHHDg9= z?QJ9%2+L#mQYw~SSNtb9Q@iPPr|=K3fRfYeitrC$PaUJzoy0%D4DvE_a2r2?PzhOs zsr(?q#GAa3qakwNgNYA$GG`x=`yNbu$=ndH3NDwPwuf&Hm<6upfG(@N zJo!rLy$I1@I|UX)t=$nU;c-2h6UY>vAx(*mw;utC7t-Nt z(qLtdVoS|Y@+WX!U_qxnOQ>ytUX!ApJAf(P%E!EzkhGWZJ`d!?;bUxakFYiD+UCsd z2C|PQpYYU%9k;{L!O@}+j^DmwXryWFAbrB3*Un&KTJ#pC z#nmKTD#jPcZC@pkKhmhK%8eUP4R4S^iBZjGmtM{^>o81XN;oX#Y<)?M(8n&AaDE2z zq>;6;++gY}@YPbP>)b{w6KG{2F=QtF`nmV*tlcY487Nl@1+1#;>!DQ>2wWhK+0yQ6 z0f~L6*=#q@9+nY9w@)H-CIDwtk%t{XlVW@yR0S~bVDS5pGwEQWG52eBHPSq+@gY6g zlARhero;qtmA9pdWXX(i1)luN@L)T;B3?A zQKPsuJkm;)KH z#~hW8JmUuH`MS^@i6-Ioqga)^gTzbc+p-JS8;7!%pmim#= zkKUxqqv;>2MPc^S#la5A5cBs{{v8$U$~*fHo%|mL+BD&Zwn0wxFSh@8fm%NN1II5q ojsAS}zl+sh8`j@C*xblOE$fWKJl=?^;G&1D_FpF|U9mOmKk9Z|82|tP literal 0 HcmV?d00001 diff --git a/widgets/screenshots/XDonutGauge.png b/widgets/screenshots/XDonutGauge.png new file mode 100644 index 0000000000000000000000000000000000000000..73e910f6cfce2dae0945df237a931c1c61f304c3 GIT binary patch literal 3179 zcmbtWYg7~07M?tLV-g8ejByx(3JIVTMQD%-PRc}(3MfR2FN}zaD2O5tO9)s8@X912 zicoB0v_-@kKmm)WEiQ&4SHYqpC`*hF6a<0-Ld84SyY9XA@BMT3xA#70)}DRN`OcTU zA~?Xx+}<1j04o`agaQB!1OTd>P9q~R@{>zR18zb?mjgh`8~{*e0l+vJQoaCyJ@WzJ z&U&**(Cc0vAC~F}7%w|ioOG8Ti`lN{JbguNldm4Kz zw0#pt-SREV-_#-N)4|W+*Q4>PR{dEzk-FSP3HSq)tQuuqUEh+)oy}W2Y=X^*FUnq7NZk-LE6hi_z@C=aftYe8V4%a{0gr=*?1~gN{3BB zB|tT$&{*%YbNNu22kW12~)RQOlV^Qsp``9Pud!A?m- zbs0d|H~L^6vJ`yJ;Q=^NrpZie9P-6nWO4X!k14E{;gT7KUHdE%jli8q+{Z(9Rkx{l z)=Oc-GL4PG=MXhSC=13r#c}jjj?64!u_Z%&qC5+{Q_erQw+ve$GiLnxBEJ70YwXpD z;GLfr#4V*5>eDh8bPqloKPZ?%?0;BOKGz9&1=_W<>-LI zLofB=92oL#Ksd^v=-sF1X{SXE#P{Psg}q6h5K1LeS;!xfIkQPe+10aPX1%)=JDwGg z=T7FLd_57JWX_KhLte`_ROL(Rd<96pq|3J+>!Yl8$8LSGqm|prFdM?`S{Xz8uZ;%{ z#x$vdB>ZEL5=o`!{-kQ);iFYVD~C}?^>vd1*|W|$Wt=)~#_5Z9s7O!2?_XcT@miw+ zAM@>?5^e~+P;0VXwo*!$2*kf4bP1igU3NRBHv_W~oP|dXn+npD-R4 zEQxZ_GCj?V5tby)f;}yI*0LQ~t*XTlh>0Tf?c(Xl5Uch2nl_#gqCt_Y zdpU&C)UM$oE=EgKH7f-?L6}7BLZ0;JO;vJ!5!Xsn23m7lDYD1$ zp=vEyYumiBQYEQyEqEW@q!bLz?>@HyPh@zP67?Wjf5W3&#Kk@NcZ;|dnhPLl%r)`) zb6$aTf%@Q3^Z}JlgbhVgsKix6r07YD254G^AIY(y$u7$;6>&{9d-SnhffT{lk{~Z3 zOVA%x7*{|=ee!O#oTY0(xikfrH{uU3D`uT?LanZDun*9A^+mGHp@`dlH#~r{l&4pRa>Uc)AB+ z15*fa-gD=m-ze4C1wJiFnL<>wylBaItsM&n*woTd55*e`HVl-$#TSkr_>0Rxq4Svk z{x^YdKf`M|1e2UNRTw}K*z0R2${P4#`_W9P7&Wn_5+nV6X$pBqu1A`PoorW z&xrl^ijT|wHW*gJ?+gj$`&^lD_KU`oUio?u_?$&LNADp(pe3OpbEH10@zeW6ov`nM1Vq)xPSI`{n zWJH-@wx(9C)Q;DSI*&cPn@Puu>?=|y^bhOZ?LZFLsJWjCBnXKjQ>zfFcd)IOVGC>c z>eCB#*2xpe;+G@brit%(W9#Pr&##K76OrbEp`=UCQm@kf(*U_@aNx5X<)Hh1>c^OI zJ`riEqfv?Zc#b>r?UJ`s;T-S)ztSF59-@&QR`fNKT`k?){spMCq?e9%eKB%|Mn&~y zj{u57fYv^Lbp5L~gl0(3{I&b!)o#HB1IifwhrNC%0v74iiA(HEn@Ua#gDJwP!yHhDE~`vmL^-C58P zMPB!A-6K)ujZi=)SBJ9&!_ghC1$3g=T6c^tOGEl>1F3?C(HBpE$zIL7^!}2zxPpxm zPgt`fyv$s)r&44`Ugu!V?#dv$K!zYpnz9xzF4}xqejS|Q)x1})fQ>745zMBV)t7R~ z>%Oz{kX@jOAW)jJ1HaX>EL0s`Cw~lfENV{CXTi^Gz$-pzMT^J!bNlc}?gDqRfsu3W zM0dCs0O*;V`*sK*2$Ay4wG8b#^&}5WUevr%Zv(f*7Boi5l!{U;K+*u~gl#2ELDTBd z#k~;d?9vm)vche+L(b&G5i5NVmJu}0?Ol@VMAdAlEH>9tq4c}o1z)zr(xi3fB;T)N zSF7f!=7sD0W?>^ocJBUKG>g@nGsZ#IDA-y zaj#6-S&^=+Dibhk=9+YI8J)OcTP@*IKtZ zFhc0;QZz_nBk@F3Vu8fwOJU3fL~>@A+IAIdIX;|olLGpl1+`$<1$3Gan}-g@+^{MT z5$|HWbE|5uw=uGL<=04KW@GmDHly}4S3fo1TmCy3c5}v7J%i26W@nGAV3Kh=wXInG z06g*waUcMe=p7O;KMH%Y!}7ygi>No~ZWk_b?{&6gX&^~lZF=$%2_q?YF@ zRJ-#@r6E;_NfBZVtru&VLK>Ed-Wwu-1gXO9m73ZhM}BuSA@2RQB<@muNnTB6iHYF^ zaa@zQAWsC|LzwxxreqzZ*2NejyIc~$$IGB;M|I9A0&1;WLb~j-E?^G+3QE!>hUBdT zg-{ZnxH>Nq+zwq8^#00kg2Sb8egox!sS4V=FAZl-@T`0Iz3}<8wXg1$)Lngg;vjMG z*ffKQnwx*ii!+Lv8Y_uv_9$U$?02ZOP`JO*pQb141V8`&7PT-ciLy%p;rPUsSF84& z56$fQS`+a!MaQhI{Ft{+n4Rqzuoq9=8<|FX$g=5DGH|D}ilV=VZAeQa>%$xqeu-^BF)?HZbf{p6Lg Vm+Basx013SkV%7)^ZpxD{{rj-;zj@f literal 0 HcmV?d00001 diff --git a/widgets/screenshots/XStackBar.png b/widgets/screenshots/XStackBar.png new file mode 100644 index 0000000000000000000000000000000000000000..81a89e1450331238f8bbebe07664e657777f2e2e GIT binary patch literal 1924 zcmd5-Yc$kp7=O*hU|dEi)Ql#aT5^t>){e`#4@N{$qC;{U(q)DcX=$+Q(siOUs$q0? zr%_Anx>SQuBDb>EbsLi%#%)I0NG>z7V^DqBvp(+s{h#x`=Xu`C@45V*Cu5(7i<-(N z6##&mD~ad@02B)V#1{sYNg{mn9Ay(WgygjsfcR|y(0%}5UM8ZA0uZwk0MStZEN=p! zP0gv;Pmn2e_PHN$T3lQt5C~dYS_}pQFbI8R0K_%QJ|hjEW3oL8!EP=@kPgR?08pH8 zCE6c|d;OL#xXyIep#=>W)K2`h*|^c&%VB98CP_A$AdwE?xe%NOE4HCKmtDDq0P!#> ztnkP*bY1-u@{8l$0$K36$^L`~0mfK67%cq_Bs0J_*t92mQBr*gU45~-ExPRkf5d)% zEL(IxIHseIynt)r`+ai5+QJNfCb`d;nCBsyY$~FzvC9-!ShXQro38o z*%x#}XTvmyI7n>2pJQ{hG4cY$??^-Yqk^U#5<1Tm^O3llgb=#YgBGnR6^y%#!P5Ec znXML|N{ZmhSWg;JqAkLZW=!pGn+a2qLMT0~LvMp&eoe`6fNR3I6Fn;b2*tSMqquau z{dAnbIicqIAUQ0)u<9P=+LI49p_IF@Ns(wOV@N7pf1cv5KG7oZ)Iwn)5SS85VUBB` z*mZwl(vRMsSVO!Zotv8O=|8zR-mRdpq{rIPz<(_0TT}=7o?Xi2-kX9#h;}q3CDa0y zK3U!BP>F4fv}`R5B;q|^mg8$e4smEXoZ|L5Y#rh3pMBh0D!<%O>ko&VgOdEZTbfI9 z`+xgBfJ5JN=@XS6)ZxuZz8$#0k1Q&CE4?~YM8=t7l)Ze?BEsi*@Qf})k1Q;GcQ-w1 z3+BAl^syw$aO??I7DM_Ti@)ygX&kHZ$J>Y%Qm(|tUhu%y#@s1()k}=42Vd^iY zJ**q>x^>nYj#}4*EO5(STB3z@Rr98bKgA}=(91L66)*~SIvk-5%fRXPmcN6Dt?M+i zaKW>JT0xvn{rSceyUB^t7e3H5aohGv+XnMUezh4j4o#Xt84l=$#ycQ!S1%eS4caw7 ztxwNQs=H}JE1W}@bkzo_GfU2Nibu^t2~w7oi@;#yf!8BEwbwIkGzndq8tTX4?xxN@ zMbveTu-n`$wL&fH+1zuga&yp1xKnIBHFSg2<>XlngnCD%*BonPU zdJ^PAbnnG9gm#GeWw;PVN2L9J8j364&@z}x(NHwfrZ5cNx6}%%o-=t8&AOdB(ch9R zTCD=~y_~=E$yO%^O!D8P2SF@i)O25onua$`>#`^I%vZig^(3&AGx^_>+cp`BBTE!? z;TdSPbx2YtQ;}`4`J0JUYMR)lW58CGo#(7tS>aubpEBM_u*u^-#_U4d7j8u)6SClm zyHL66`b){EiNab!L+1~`12ow0W*U_9{Ysj7Q}=qbho!%ckvySI{1Gj7VUNM{;b*VL z{SVIi(8mUn{R6P;3R)bm@K^|3p#S3&RmP-1dwo3iq3`yj8{$?m-0C=eG~3LOx8rr% zls7HsVXC|1kL?rXJl^G(LLlhO>#&br$P7$55i8=9B_c@Ud)G6Ej25iZ3slY{hKO14E{CT|CQfIdxxY4DNV@0)u!{g`>b&>cHs@4+j<*5a_1>k P)&_yAlLxWL;b`(dWZS>U literal 0 HcmV?d00001 diff --git a/widgets/screenshots/XTachometer.png b/widgets/screenshots/XTachometer.png new file mode 100644 index 0000000000000000000000000000000000000000..2456cd21a074cf310b566cb9fe06403317a6a63b GIT binary patch literal 3336 zcmbuCeLPh89>>qjoOwWvnPH}+reiwFv&ch)ozdV-q-7&oB4$O|w$yBH&GaB?F6h|>UIUY8QT006;e z02qk|0QWoqSasyfh9D1Jf&PBLZl9&4B@Yh|V`Jl-oScb?2_}=-X~Eoo;@UkOfQj+< z;{r1LIs^b#2zXr1?&P1xp6x&UJk02`{h(=HFQ%pj@V`+59I2H~q+VVed}lw<%tpM$ z(f0+TKQ)cdQ3WjbM{fpp_r?|5Fw!XAB(uMpIkTl~cy2fxO`SSj!NXJ7BuI?HP)Oar z(5{%Ku%usLHkn403+k?JJt78@R`DYpYMj`8CrFUhO1Fw*ISnh+G>R=M0V(FFIkW=; zDQp0f24G1Tkb))s58?8F>RnoMXQ+?yEH`mODIZ_cP!nIZ8{n09h(7i`@lFU{h+yA7 z^b~Nr_>=hZFE0*XX_~oP;ld+BC$|13mR(R0>syYl;$DQ^)ws`z&>gs%6WV$vc9Fsl z8y9Q#Wu^Nq+(&ZY&FZq3B4tH1@40lLhFNB=XW)1){Sxv%+Mi|JRZTbIcYUh$@rPk# zOG*0U$8wTmZF&yEM!(7T{BWHj86q%uum;!x9%9ZLG2EECQn)%zSaaCqSohWf@>`LQ>?H(ufI9JsQvExTqggLHoA*o2ni)C&cs4lt*}`*cpDo3B8SNlaLh83#WQg?n>d5C@z`%{QMnLx+Q~K3%#@|Cb0IR z$LT@1AX}tHE|VaI+QLp_J(jTTRAIOv18TtYoNKSM`Fl<_)K%h(BTz0~fmPTc#d1VK zs+#5>iqz+xin*c;-vK>0vToG`V}tCGY&l6nin{WBiKPLzZ^K;;4clRl$dy-cv`dhs zmF^XnYzmbs&2hA1NY9FeV~L&&VuN$BwAh&5b{jeh8MTPwO2wcuY<7!39)6&5EDC2q z`#0vyX|N#r*r?@XT!I+9rCn5f1EOr^y)FJYIGB@pfB}iYqA*>Eg*6||n_O;NcX{W; zwhhDnPlm`fHA{l1W3yj77wASE<(_pMCMSKlC#dhz!Sn64eD)0oc3@S0BS%^=&5ZIC z1KH%u?-q8u+7}b@?TfnZiW?KToU#~p=$@+Wt@1>dj5r3=K6GU*>Vj4OdNBQQs|_0v zZmvGgDH}0w)zEMqo-GaYCx#3<_j!KOTCliMl^9nlN#j*QnDP9_Z8~@^JNlE7RBWjh z%xLh|9?Rf_u%1dg_O07=Df4S?Fk}+SV}N9IL)Cy6_zlDrDZgUAP{}q$jmE z$7KMH&v29yfvA1MHk4#NIP2~j!4e0&2Dq9H+bA>J!c#>Po9pw3^jNnat^abL$zcIo zG=P!i`Nct<+fFZKrr!-E4i?`3kcc=LvQ-|AxS(e*QwwLWJsuH1ol2hnQ5-J>SidUh zvcbUqEbV4pIzree5UXaE53r`x6)YZ*rvIp$ap8X2_XJrE5sG1o^D49nOcRMk0e|P- z;gUp*VoFEjC>I1?x?K&j`WH^KuX{LR6KW^&mKtxyqwmH&0cFp>DsWr^pRv3Kpgda8 zK7diDFtsTq5$sT9vPNVU468GlVohz^0WH91WZfiFOi-aGK-B_+uSZit4cVeG2klLe z+k&GwWF@lq<_d1RzPaHHUGVI4XY<+R9?K|y#NBlY;1{R&x)OgU4cWefV~uULo`6a3 zie>>>eQQNm@|M>CKY%kk%M@pV)w2M9t?V5izlJSEW_fzm~1@a@;dO~U+May06XrR&+PuhWUG;l!%fbA zwq@T<=zg^+{Ew<}ZP%ecdL!75k4LoQ$Nz|S^o}ey1d|aX(Pw-DP4unltK|}x0WXAB zVK5+?&6e6z&(W9Hc_9U(SUP<5nY7#ab*&V5n&uF0ru#@*X{F0(CmU-|&0j|AHRWq4 zY??T&DYQRMpZ00S>Iw?GQ1guy2tS|+dDp!GVGR`N06D~UZ|u^?4D!&?b=mevrm{|0 z|FiChz75~5Mk!lH*QgR>FjM@pGHX0MIJP`9TLLx~eB@dUVBlHJ!OTqzDE7M!20%L) zw~}?%$N;ru)t>>kO@3^sjI5L3|sTNJKmmJhEX6(cEJFW zsV%RSy;KEbGL9AsXbJj2M$+>qhUc2>FtECApz>y6m^jne5)VJTdoq%MuQUT+sI7KBA$&yX%?Put* zQtJ9_c7Rl!7Av8eL!PZWdT@#gB+^P2?-NYt8gj3*1ocq%f!j}{9JoCt-=`jmq4!>~ zm9b=@KA*pLD!&qvL&0>n#aAESks?xykjv{ua@+kJn3HCeWP$XT*vK}@0(}Tsi{r(| zDN6ZT&-pbJh5S|LDy}j2GK-84!Yi)I{5ZQgu+{S*N;pL#zh%s2a4)g!@E!Q0sZoQ2 zlTjlj6$Z~2x(5=XXG)9eHp`Crq!4nPWTX4WBV8I?WW)RVg>>#3RW<7{UO||AGTQXW zcMoy$=TQ;Pf1N5Jer~0WfKt@dw~fJFr;3dil5I|-)7uXXyIq^+bQ;?BNaz4O^Dt?a z1Z002?`Bv|g7oYSpPb0*8~dj(z`N;tvEcO&a!ht&{Ku!pC3GCDM3zQ=qvGI>ha>G! z-S7wJF^)S2M{+ygJ9_7B&$A8sYGcLDo~F!3VD9b67?r@kv&MwjX<##rKt^0&$<<^P zMhQ8_q^pxbtVxrvuhZbO=2H?C*2H?sLx%cUpbFNmGZsQ!!gA^}ldn)bj-HJdsOPIAF;r!mb8Ws0Qdcuu|P^Y6Wx4jj=EBW9_0 z`%5JrA6|a3e(SvNNQMu7uJ!T!ov*HY#heMC&pj}!^OBwkF!}IZi0WR*5wys^)9WWn zx!-YgJMZUV_Pi;z+%H+@Gg~Wq7De!=0;ArZ-E(BEo7e*quDq`xsw0=fdhrT)>=<<3 z$)v^=VL&qnJE%|gwbD4W~k=m*;a-+uH?q9XGiDh3{kwzcftGwc?6`W0#f z9tiJ-x~?_$?CCLUp7gSt89HV9c~MA2`c|JhKn!^Qaj%BNI=MC5vTit)mpyT3)8B9N i|8=?lze7upC8F76Zkx61tr6Yd2*C3Ri28yg*QRD=q6p}rHd>I(3)EF2VS{N99 z0fib~Fff!FFfhDIU|_JC!N4G1FlSew4Nzi!fKQ0)|NsAOY;2}Yo5rw3gJrV@=VlGw z%^E_RHN-Y)NN>_m*rcJdNkem!hVDiUqm3Ho8#SyqYB+4raM_^Yxk1BkgGSH>jqvpv z(d#u5)@!7$*T`I_k+)8xXq`s+I*poj8VzeTTGwiHuGQ#Ut1)@4#`HBBbJl1qSfjCY zjmFB=8tYbTY+kLgeYM7()fxv@X&hapacY&uxm6mMR;XWEqWc3!;`=h}F3G(E+N!5!o|Ge=&*ktxu z*UvYfXDCMZ>`U>!ShP)Oty}iZ>o20W$<>}WTXZ%@`_{R#MR#-bZ}szSI{ZSzy00$9 zesTQG_b>dvnOk-GZQlGMzDzDQW%|X?GTFN++b_nJ{XfUVBH+NlI7*C$!Du?5Nj8}2 zr*Jm;@9WE#b~AG*gfdC?WSz=^ zp|F%k^47Wri)Mx(+X?r2rp4@Hz4S{Ntls3UV=hoF!=<_-QaoAH(hp!k;$Xhp~#}WmhGdetbjwD?fC|fk)yuR^x@LnDCmK8+ua8r>)WXt% ufM4VqJuXkNb;8yVz}h;Xn&7If>^Hn`&&W@W^Z~YI7(8A5T-G@yGywo${@0QK literal 0 HcmV?d00001 diff --git a/widgets/screenshots/XVerticalBar.png b/widgets/screenshots/XVerticalBar.png new file mode 100644 index 0000000000000000000000000000000000000000..28102a0ea0145e98749ce9394a1555640b14d590 GIT binary patch literal 1448 zcmeAS@N?(olHy`uVBq!ia0y~yU{+vYV2WU228v`v%y$M-3dtTpz6=aiY77hwEes65 zfItx7?Zr+T^Kr8Wjz=eSVcWu978H@y}cXjcRN6!;o|B@#y1Hj+76p;iI#BR zb^5-)<(E2Vpf;D1+Pp(QRfG;P^V><@kC3S5@tmXrLtni5cX{*YvsV7t^L}d>Bcqan zLy7o=J^vhURX8*-Wby}@E-DIP%48I}o!rB4iA`nIA-&at9(uw|ON2dcrfy|eS?L-t z{&mBO?F@nf3Jwh%90Fi7SXej&6quM?9QLtz^aUQ4=egZ*M#yk+%8fExhoWNzlbn?& zFtTt=FmLj?wRQPz^(QBztB&8}F8Zp;ed3|Q);YiGEcqCV{GwylLHC>+UnZWZX2TO2VP4QDws7E{oWoJFZ<`5#uj| z2`m?0^0?+|tU`Ok8R`1nvogZs{_>nyD7+~)-@L(uq4UI7S zwO4w{szuwZdl+9DDhjGC5%ploWEFb7_kKeOljYqeeh=;mHnu*Ox6i+|ezv2JM?Yuh z>9f&|C5(~p*!Y%kpOD}0gc43ySfBjXe=Cj>fUvj%1t!85P&N}w2ROjDDM92w;SP>j zh-#p3!O~z$NRzsF*Kt*r3sWZZric`Vmn=fk+!N#!raD+Pp3&x*;HU7I$H>%2bq=3KtoWS0rVgwFv0NwPvy`Q54C!uC`v#8eT9fUpm7kFfinupQo!iEc)S4^ zIjoZymk6IYGZ7TOpW+*VSyWcdrMmp_a-C8}PUcJ8F1El({AacI#OLIh3m&Vf%)9>m zYv#0$pVrxD0u?G5Z*mkJ+YxrD*71>%l==k5Cj}F_StdMo{Isd=IKw4o74`{c3WdCd zb>B2!M@(3?hbc17lFyO{lCz(Dl`EaT=c6jDbb=RCL^AeO>}Q;%Q`;rnVC)7gEg3vr L{an^LB{Ts5^oXPX literal 0 HcmV?d00001