Round-9 UI items #5, #6, #7 and the #9 algorithm-cycle follow-on. #6 KPI boxes: switch the header from a wrap sizer to a horizontal box sizer with every card at proportion 1 + EXPAND, so the cards are always equal width and grow to span the full window; a per-card min size (116x62) floors them at their largest content. Fix the filter-alignment drift by re-running each card's sizer in refresh_kpis after set_label (the centring spacers now recompute around the new value width). The active-filter highlight now paints the card panel and both its texts, so the whole box tints instead of only the strip behind the text. Bind the card click on the value and label static texts too (a click on the number or caption registers the quick-filter, not just the card gap). #5 error indicator: add a second, colour-tinted status-bar field pinned to the bottom-right corner (wxbr_status_indicator_init/set in the widget bridge; a wxStaticText repositioned into the right field on resize). Scan and apply outcomes route here without overwriting the left scan-status text: a total apply failure and a scan failure show red, a partial apply failure amber, full success clears it; a new scan/action clears any stale message. #7 algorithm filter: the Filter dialog now offers every column (via a new ViewState::filter_columns) rather than only the visible ones, so Algorithm -- and any other hidden metric -- can be filtered without first un-hiding its column. Filters already match on the plain model cell, independent of visibility. #9 hashrate cycling: compute per-algorithm hashrate totals in compute_kpis (Kpis::hashrate_by_algo, ordered by total). The hashrate KPI card shows the selected algorithm's total under a "<Algo> Hashrate" caption and cycles through the running algorithms when clicked (a no-op with a single algorithm). SHA256 renders as "SHA-256". Bridge provenance patches 0002/0003 regenerated to match the -patched tree. Compile-checked (release + tests); 74 offline tests pass; headless self-test PASS; screenshot-verified equal-width cards, full-box highlight, click-through-text, and centred values after filtering. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Y8h447BPyJR4fioYZvQ9bh
1134 lines
41 KiB
C++
1134 lines
41 KiB
C++
// widgets_bridge.cpp — alkane-toolkit extension to wx-rs
|
|
// Adds the widget/event surface the toolkit UI needs on top of the upstream
|
|
// render-callback bridge (which is kept untouched).
|
|
// See thirdparty/wx-rs/patches/ for the patch queue that carries this file.
|
|
|
|
#include <wx/wx.h>
|
|
#include <wx/dataview.h>
|
|
#include <wx/checklst.h>
|
|
#include <wx/sizer.h>
|
|
#include <wx/stattext.h>
|
|
#include <wx/textctrl.h>
|
|
#include <wx/button.h>
|
|
#include <wx/checkbox.h>
|
|
#include <wx/dialog.h>
|
|
#include <wx/timer.h>
|
|
#include <wx/vector.h>
|
|
#include <wx/gauge.h>
|
|
#include <wx/settings.h>
|
|
#include <wx/utils.h>
|
|
#include <wx/app.h>
|
|
#include <wx/rearrangectrl.h>
|
|
#include <wx/menu.h>
|
|
#include <wx/choice.h>
|
|
#include <wx/bmpbuttn.h>
|
|
#include <wx/bitmap.h>
|
|
#include <wx/image.h>
|
|
#include <wx/dcmemory.h>
|
|
#include <wx/dcgraph.h>
|
|
#include <wx/graphics.h>
|
|
#include <wx/wrapsizer.h>
|
|
#include <wx/filedlg.h>
|
|
#include <wx/statusbr.h>
|
|
#include <cstring>
|
|
#include <cstdio>
|
|
#include <map>
|
|
#include <vector>
|
|
#ifdef __WXGTK__
|
|
// For native GtkTreeView rubber-band (click-drag) selection, which wx does not
|
|
// enable and which cannot be done via wx mouse events on GTK (the native widget
|
|
// grabs the pointer during a drag). Guarded so non-GTK builds are unaffected.
|
|
#include <gtk/gtk.h>
|
|
#endif
|
|
|
|
typedef void (*WxWidgetEventFn)(int kind, long p1, long p2);
|
|
static WxWidgetEventFn g_widget_event = nullptr;
|
|
|
|
#ifndef __WXGTK__
|
|
// Anchor row per data-view control for the wx-mouse-event rubber-band fallback
|
|
// (non-GTK platforms; GTK uses native GtkTreeView rubber-banding instead).
|
|
static std::map<wxDataViewCtrl*, int> g_drag_anchor;
|
|
#endif
|
|
|
|
// Event kinds (must match Rust WxEventKind in src/widgets.rs)
|
|
enum WxWidgetEventKind {
|
|
WxEV_BUTTON_CLICKED = 1,
|
|
WxEV_CLB_TOGGLED = 3,
|
|
WxEV_DVLC_VALUE_CHANGED = 4,
|
|
WxEV_DVLC_HEADER_CLICK = 5,
|
|
WxEV_DVLC_REORDERED = 6,
|
|
WxEV_TIMER_TICK = 7,
|
|
WxEV_DVLC_ACTIVATED = 8,
|
|
WxEV_DVLC_SELECTION_CHANGED = 9,
|
|
WxEV_DVLC_CONTEXT_MENU = 10,
|
|
WxEV_MENU_ITEM = 11,
|
|
WxEV_CHOICE_CHANGED = 12,
|
|
WxEV_SYS_COLOUR = 13,
|
|
WxEV_DEFERRED = 14,
|
|
WxEV_DVLC_HEADER_RIGHT_CLICK = 15,
|
|
};
|
|
|
|
// A list store that carries an optional per-row foreground colour plus an
|
|
// alternating-row (zebra) background, surfaced to the native control via
|
|
// GetAttrByRow. The zebra background is set on odd rows only; because it is a
|
|
// BACKGROUND attr (not a foreground one), it does not affect the SegmentRenderer
|
|
// "no explicit colour -> default text colour" path, and the native selection
|
|
// highlight still paints over it for selected rows.
|
|
class AttrListStore : public wxDataViewListStore {
|
|
public:
|
|
std::vector<wxColour> row_fg;
|
|
wxColour zebra; // odd-row background; unset (=off) unless the app sets it
|
|
// Per-cell hover tooltip text, keyed by (row, model column). Used for the
|
|
// Errors column's full-message tooltip (see the GTK query-tooltip handler).
|
|
std::map<std::pair<unsigned, unsigned>, wxString> cell_tip;
|
|
bool GetAttrByRow(unsigned int row, unsigned int col,
|
|
wxDataViewItemAttr& attr) const override {
|
|
(void)col;
|
|
bool set = false;
|
|
if (row < row_fg.size() && row_fg[row].IsOk()) {
|
|
attr.SetColour(row_fg[row]);
|
|
set = true;
|
|
}
|
|
if (zebra.IsOk() && (row % 2 == 1)) {
|
|
attr.SetBackgroundColour(zebra);
|
|
set = true;
|
|
}
|
|
return set;
|
|
}
|
|
};
|
|
|
|
// A wxDataViewCustomRenderer that draws a cell value made of coloured text
|
|
// runs. The encoding matches alkane_gui::model::encode_segments: segments are
|
|
// joined by U+001F (ASCII unit separator); each segment is an optional
|
|
// 6-hex-digit RRGGBB colour followed by U+001E (ASCII record separator) and
|
|
// then its plain text. A value containing no U+001E at all -- e.g. any
|
|
// ordinary string -- is drawn as a single run in the default text colour, so
|
|
// this renderer is safe to use in place of a plain text column.
|
|
namespace {
|
|
|
|
struct ColorSegment {
|
|
bool has_colour = false;
|
|
wxColour colour;
|
|
wxString text;
|
|
};
|
|
|
|
std::vector<ColorSegment> ParseColorSegments(const wxString& value) {
|
|
std::vector<ColorSegment> segs;
|
|
if (value.Find(wxUniChar(0x1e)) == wxNOT_FOUND) {
|
|
ColorSegment s;
|
|
s.text = value;
|
|
segs.push_back(s);
|
|
return segs;
|
|
}
|
|
wxString rest = value;
|
|
for (;;) {
|
|
int sep = rest.Find(wxUniChar(0x1f));
|
|
wxString piece = (sep == wxNOT_FOUND) ? rest : rest.Left(sep);
|
|
ColorSegment s;
|
|
int csep = piece.Find(wxUniChar(0x1e));
|
|
if (csep == wxNOT_FOUND) {
|
|
s.text = piece;
|
|
} else {
|
|
wxString hex = piece.Left(csep);
|
|
s.text = piece.Mid(csep + 1);
|
|
unsigned long v;
|
|
if (hex.length() == 6 && hex.ToULong(&v, 16)) {
|
|
s.colour = wxColour((unsigned char)((v >> 16) & 0xff),
|
|
(unsigned char)((v >> 8) & 0xff),
|
|
(unsigned char)(v & 0xff));
|
|
s.has_colour = true;
|
|
}
|
|
}
|
|
segs.push_back(s);
|
|
if (sep == wxNOT_FOUND) break;
|
|
rest = rest.Mid(sep + 1);
|
|
}
|
|
return segs;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
class SegmentRenderer : public wxDataViewCustomRenderer {
|
|
public:
|
|
SegmentRenderer()
|
|
: wxDataViewCustomRenderer(wxASCII_STR("string"), wxDATAVIEW_CELL_INERT) {}
|
|
|
|
bool SetValue(const wxVariant& value) override {
|
|
m_value = value.GetString();
|
|
return true;
|
|
}
|
|
|
|
bool GetValue(wxVariant& value) const override {
|
|
value = m_value;
|
|
return true;
|
|
}
|
|
|
|
bool Render(wxRect cell, wxDC* dc, int state) override {
|
|
bool selected = (state & wxDATAVIEW_CELL_SELECTED) != 0;
|
|
wxColour highlight = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
|
|
wxColour plain = GetAttr().HasColour()
|
|
? GetAttr().GetColour()
|
|
: wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
|
|
wxCoord x = cell.x;
|
|
for (const ColorSegment& seg : ParseColorSegments(m_value)) {
|
|
if (seg.text.IsEmpty()) continue;
|
|
dc->SetTextForeground(selected ? highlight : (seg.has_colour ? seg.colour : plain));
|
|
wxSize extent = dc->GetTextExtent(seg.text);
|
|
wxCoord y = cell.y + (cell.height - extent.GetHeight()) / 2;
|
|
dc->DrawText(seg.text, x, y);
|
|
x += extent.GetWidth();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
wxSize GetSize() const override {
|
|
wxString plain;
|
|
for (const ColorSegment& seg : ParseColorSegments(m_value)) plain += seg.text;
|
|
wxSize sz = GetTextExtent(plain);
|
|
if (sz.GetWidth() < 1) sz.SetWidth(1);
|
|
return sz;
|
|
}
|
|
|
|
private:
|
|
wxString m_value;
|
|
};
|
|
|
|
extern "C" {
|
|
|
|
void wxbr_set_widget_event_fn(WxWidgetEventFn f) { g_widget_event = f; }
|
|
|
|
// ---- window tree / sizers -------------------------------------------------
|
|
|
|
void* wxbr_main_frame() { return (void*)wxTheApp->GetTopWindow(); }
|
|
|
|
void* wxbr_panel(void* parent) {
|
|
return (void*)new wxPanel((wxWindow*)parent, wxID_ANY);
|
|
}
|
|
|
|
void* wxbr_box_sizer(int vertical) {
|
|
return (void*)new wxBoxSizer(vertical ? wxVERTICAL : wxHORIZONTAL);
|
|
}
|
|
|
|
// A box sizer that wraps its items onto additional lines instead of shrinking
|
|
// them when the available space runs out. wxWrapSizer is a wxBoxSizer
|
|
// subclass, so it accepts the same add/add_sizer/add_spacer/add_stretch and
|
|
// set_sizer_on calls as wxbr_box_sizer's result.
|
|
void* wxbr_wrap_sizer(int vertical) {
|
|
// wxREMOVE_LEADING_SPACES only: drop the default wxEXTEND_LAST_ON_EACH_LINE so
|
|
// the last item on a line is NOT stretched to fill the row (keeps fixed-size
|
|
// items -- e.g. the uniform KPI cards -- the same size).
|
|
return (void*)new wxWrapSizer(vertical ? wxVERTICAL : wxHORIZONTAL,
|
|
wxREMOVE_LEADING_SPACES);
|
|
}
|
|
|
|
// flag bits (must match Rust sizer_flags):
|
|
// 1 = EXPAND, 2 = ALIGN_CENTRE_VERTICAL, 4 = wxALL, 8 = wxTOP,
|
|
// 16 = wxBOTTOM, 32 = wxLEFT, 64 = wxRIGHT
|
|
void wxbr_sizer_add(void* sizer, void* win, int proportion, int flags, int border) {
|
|
int f = 0;
|
|
if (flags & 1) f |= wxEXPAND;
|
|
if (flags & 2) f |= wxALIGN_CENTRE_VERTICAL;
|
|
if (flags & 4) f |= wxALL;
|
|
if (flags & 8) f |= wxTOP;
|
|
if (flags & 16) f |= wxBOTTOM;
|
|
if (flags & 32) f |= wxLEFT;
|
|
if (flags & 64) f |= wxRIGHT;
|
|
((wxSizer*)sizer)->Add((wxWindow*)win, proportion, f, border);
|
|
}
|
|
|
|
void wxbr_sizer_add_sizer(void* sizer, void* subsizer, int proportion, int flags, int border) {
|
|
int f = 0;
|
|
if (flags & 1) f |= wxEXPAND;
|
|
if (flags & 2) f |= wxALIGN_CENTRE_VERTICAL;
|
|
if (flags & 4) f |= wxALL;
|
|
if (flags & 8) f |= wxTOP;
|
|
if (flags & 16) f |= wxBOTTOM;
|
|
if (flags & 32) f |= wxLEFT;
|
|
if (flags & 64) f |= wxRIGHT;
|
|
((wxSizer*)sizer)->Add((wxSizer*)subsizer, proportion, f, border);
|
|
}
|
|
|
|
void wxbr_sizer_add_spacer(void* sizer, int px) {
|
|
((wxSizer*)sizer)->AddSpacer(px);
|
|
}
|
|
|
|
void wxbr_sizer_add_stretch(void* sizer, int proportion) {
|
|
((wxSizer*)sizer)->AddStretchSpacer(proportion);
|
|
}
|
|
|
|
void wxbr_set_sizer(void* win, void* sizer) {
|
|
((wxWindow*)win)->SetSizer((wxSizer*)sizer);
|
|
}
|
|
|
|
// Re-run this window's sizer to (re)position its children. Needed after a
|
|
// child window is added to an already-installed sizer (e.g. the table, which
|
|
// is created after the frame's sizer is set and rebuilt on column changes).
|
|
void wxbr_layout(void* win) { ((wxWindow*)win)->Layout(); }
|
|
|
|
// Remove a child window from a sizer without destroying the window. Call this
|
|
// before destroying a sizer-managed window so the sizer keeps no dangling item.
|
|
void wxbr_sizer_detach(void* sizer, void* win) {
|
|
((wxSizer*)sizer)->Detach((wxWindow*)win);
|
|
}
|
|
|
|
void wxbr_fit(void* win) { ((wxWindow*)win)->Fit(); }
|
|
|
|
void wxbr_set_min_size(void* win, int w, int h) {
|
|
((wxWindow*)win)->SetMinSize(wxSize(w, h));
|
|
}
|
|
|
|
// ---- static text / fonts --------------------------------------------------
|
|
|
|
void* wxbr_static_text(void* parent, const char* label) {
|
|
return (void*)new wxStaticText((wxWindow*)parent, wxID_ANY,
|
|
wxString::FromUTF8(label));
|
|
}
|
|
|
|
void wxbr_set_label(void* win, const char* label) {
|
|
wxStaticText* st = dynamic_cast<wxStaticText*>((wxWindow*)win);
|
|
if (st) st->SetLabel(wxString::FromUTF8(label));
|
|
else ((wxWindow*)win)->SetLabel(wxString::FromUTF8(label));
|
|
}
|
|
|
|
// Read a control's current label text (used by the headless self-test to read
|
|
// back what a KPI static text actually displays).
|
|
int wxbr_get_label(void* win, char* buf, int cap) {
|
|
wxWindow* w = (wxWindow*)win;
|
|
wxStaticText* st = dynamic_cast<wxStaticText*>(w);
|
|
wxString s = st ? st->GetLabel() : w->GetLabel();
|
|
wxCharBuffer b = s.ToUTF8();
|
|
int n = (int)strlen(b.data());
|
|
if (n >= cap) n = cap - 1;
|
|
memcpy(buf, b.data(), n);
|
|
buf[n] = 0;
|
|
return n + 1;
|
|
}
|
|
|
|
void wxbr_set_bold(void* win) {
|
|
wxWindow* w = (wxWindow*)win;
|
|
wxFont f = w->GetFont();
|
|
wxFontInfo info(f.GetPointSize());
|
|
info.Bold();
|
|
w->SetFont(wxFont(info));
|
|
}
|
|
|
|
void wxbr_set_font_size(void* win, int pts) {
|
|
wxWindow* w = (wxWindow*)win;
|
|
wxFontInfo info(pts);
|
|
if (w->GetFont().GetWeight() == wxFONTWEIGHT_BOLD) info.Bold();
|
|
w->SetFont(wxFont(info));
|
|
}
|
|
|
|
// ---- button ---------------------------------------------------------------
|
|
|
|
void* wxbr_button(void* parent, const char* label, int id) {
|
|
wxButton* b = new wxButton((wxWindow*)parent, id, wxString::FromUTF8(label));
|
|
b->Bind(wxEVT_BUTTON, [id](wxCommandEvent&) {
|
|
if (g_widget_event) g_widget_event(WxEV_BUTTON_CLICKED, id, 0);
|
|
});
|
|
return (void*)b;
|
|
}
|
|
|
|
// Make an arbitrary window (e.g. a KPI card panel) clickable: a left mouse
|
|
// down on it emits the same WxEV_BUTTON_CLICKED event a real button would,
|
|
// carrying `id`. Bind this on the panel itself; a child wxStaticText label
|
|
// swallows its own mouse events, so the caller binds each child that should
|
|
// also act as part of the click target rather than relying on propagation.
|
|
void wxbr_bind_click(void* win, int id) {
|
|
((wxWindow*)win)->Bind(wxEVT_LEFT_DOWN, [id](wxMouseEvent&) {
|
|
if (g_widget_event) g_widget_event(WxEV_BUTTON_CLICKED, id, 0);
|
|
});
|
|
}
|
|
|
|
// ---- text ctrl ------------------------------------------------------------
|
|
|
|
void* wxbr_text_ctrl(void* parent, int multiline) {
|
|
long style = multiline ? (wxTE_MULTILINE | wxTE_DONTWRAP) : 0;
|
|
return (void*)new wxTextCtrl((wxWindow*)parent, wxID_ANY, wxT(""),
|
|
wxDefaultPosition, wxSize(-1, -1), style);
|
|
}
|
|
|
|
int wxbr_text_ctrl_get(void* win, char* buf, int cap) {
|
|
wxTextCtrl* t = (wxTextCtrl*)win;
|
|
wxString v = t->GetValue();
|
|
wxCharBuffer b = v.ToUTF8();
|
|
int n = (int)strlen(b.data());
|
|
if (n >= cap) n = cap - 1;
|
|
memcpy(buf, b.data(), n);
|
|
buf[n] = 0;
|
|
return n + 1; // total length needed if truncated
|
|
}
|
|
|
|
void wxbr_text_ctrl_set(void* win, const char* value) {
|
|
((wxTextCtrl*)win)->SetValue(wxString::FromUTF8(value));
|
|
}
|
|
|
|
// ---- checkbox -------------------------------------------------------------
|
|
|
|
void* wxbr_check_box(void* parent, const char* label) {
|
|
return (void*)new wxCheckBox((wxWindow*)parent, wxID_ANY,
|
|
wxString::FromUTF8(label));
|
|
}
|
|
|
|
int wxbr_check_box_get(void* win) {
|
|
return ((wxCheckBox*)win)->GetValue() ? 1 : 0;
|
|
}
|
|
|
|
void wxbr_check_box_set(void* win, int v) {
|
|
((wxCheckBox*)win)->SetValue(v != 0);
|
|
}
|
|
|
|
// ---- check list box ---------------------------------------------------------
|
|
|
|
void* wxbr_check_list_box(void* parent) {
|
|
wxCheckListBox* clb = new wxCheckListBox((wxWindow*)parent, wxID_ANY,
|
|
wxDefaultPosition, wxSize(-1, -1),
|
|
0, NULL, wxLB_SINGLE | wxLB_NEEDED_SB);
|
|
clb->Bind(wxEVT_CHECKLISTBOX, [clb](wxCommandEvent& e) {
|
|
if (g_widget_event)
|
|
g_widget_event(WxEV_CLB_TOGGLED, (long)(intptr_t)clb, e.GetInt());
|
|
});
|
|
return (void*)clb;
|
|
}
|
|
|
|
void wxbr_clb_append(void* win, const char* label) {
|
|
((wxCheckListBox*)win)->Append(wxString::FromUTF8(label));
|
|
}
|
|
|
|
void wxbr_clb_clear(void* win) { ((wxCheckListBox*)win)->Clear(); }
|
|
|
|
int wxbr_clb_count(void* win) { return (int)((wxCheckListBox*)win)->GetCount(); }
|
|
|
|
int wxbr_clb_checked(void* win, int i) {
|
|
return ((wxCheckListBox*)win)->IsChecked(i) ? 1 : 0;
|
|
}
|
|
|
|
void wxbr_clb_set_checked(void* win, int i, int v) {
|
|
wxCheckListBox* clb = (wxCheckListBox*)win;
|
|
if (v)
|
|
clb->Check(i);
|
|
else
|
|
clb->Check(i, false);
|
|
}
|
|
|
|
int wxbr_clb_string(void* win, int i, char* buf, int cap) {
|
|
wxCheckListBox* clb = (wxCheckListBox*)win;
|
|
if (i < 0 || i >= (int)clb->GetCount()) return 0;
|
|
wxCharBuffer b = clb->GetString(i).ToUTF8();
|
|
int n = (int)strlen(b.data());
|
|
if (n >= cap) n = cap - 1;
|
|
memcpy(buf, b.data(), n);
|
|
buf[n] = 0;
|
|
return n + 1;
|
|
}
|
|
|
|
// ---- dialog -----------------------------------------------------------------
|
|
|
|
void* wxbr_dialog(void* parent, const char* title) {
|
|
return (void*)new wxDialog((wxWindow*)parent, wxID_ANY,
|
|
wxString::FromUTF8(title), wxDefaultPosition,
|
|
wxSize(360, 480), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
|
|
}
|
|
|
|
int wxbr_dialog_show_modal(void* win) {
|
|
return ((wxDialog*)win)->ShowModal();
|
|
}
|
|
|
|
void wxbr_dialog_close(void* win, int code) {
|
|
((wxDialog*)win)->EndModal(code);
|
|
}
|
|
|
|
// ---- file dialog -------------------------------------------------------------
|
|
|
|
// Show a native "open file, must exist" dialog. On OK, copies the chosen
|
|
// path (UTF-8) into buf (capped at cap) and returns its length + 1, like the
|
|
// other buffer-filling bridge functions; on Cancel returns 0 and leaves buf
|
|
// untouched.
|
|
int wxbr_file_dialog(void* parent, const char* title, char* buf, int cap) {
|
|
wxFileDialog dlg((wxWindow*)parent, wxString::FromUTF8(title), wxEmptyString,
|
|
wxEmptyString, wxFileSelectorDefaultWildcardStr,
|
|
wxFD_OPEN | wxFD_FILE_MUST_EXIST);
|
|
if (dlg.ShowModal() != wxID_OK) return 0;
|
|
wxCharBuffer b = dlg.GetPath().ToUTF8();
|
|
int n = (int)strlen(b.data());
|
|
if (n >= cap) n = cap - 1;
|
|
memcpy(buf, b.data(), n);
|
|
buf[n] = 0;
|
|
return n + 1;
|
|
}
|
|
|
|
// ---- data view list ctrl ------------------------------------------------------
|
|
|
|
static AttrListStore* dvlc_store(void* win) {
|
|
return static_cast<AttrListStore*>(((wxDataViewCtrl*)win)->GetModel());
|
|
}
|
|
|
|
#ifdef __WXGTK__
|
|
// GtkTreeView "query-tooltip" handler: show the per-cell tooltip stored for the
|
|
// (row, model column) under the pointer (used by the Errors column). wx does
|
|
// not surface per-cell tooltips on wxDataViewCtrl, and mouse-motion events are
|
|
// not delivered to wx handlers on the native GTK control, so this hooks the
|
|
// native signal directly.
|
|
static gboolean dvlc_query_tooltip(GtkWidget* tv, gint x, gint y, gboolean keyboard,
|
|
GtkTooltip* tooltip, gpointer user_data) {
|
|
(void)keyboard;
|
|
wxDataViewCtrl* dv = static_cast<wxDataViewCtrl*>(user_data);
|
|
gint bx = 0, by = 0;
|
|
gtk_tree_view_convert_widget_to_bin_window_coords(GTK_TREE_VIEW(tv), x, y, &bx, &by);
|
|
GtkTreePath* path = nullptr;
|
|
GtkTreeViewColumn* gcol = nullptr;
|
|
if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(tv), bx, by, &path, &gcol, nullptr, nullptr))
|
|
return FALSE;
|
|
gint* idx = gtk_tree_path_get_indices(path);
|
|
int row = idx ? idx[0] : -1;
|
|
gtk_tree_path_free(path);
|
|
if (row < 0 || !gcol) return FALSE;
|
|
wxDataViewColumn* wxcol = dv->GTKColumnToWX(gcol);
|
|
if (!wxcol) return FALSE;
|
|
AttrListStore* store = static_cast<AttrListStore*>(dv->GetModel());
|
|
auto it = store->cell_tip.find({(unsigned)row, wxcol->GetModelColumn()});
|
|
if (it == store->cell_tip.end() || it->second.IsEmpty()) return FALSE;
|
|
gtk_tooltip_set_text(tooltip, it->second.utf8_str());
|
|
return TRUE;
|
|
}
|
|
#endif
|
|
|
|
void* wxbr_dvlc(void* parent) {
|
|
// Multi-select (ctrl/shift-click) + row lines. Selection replaces the old
|
|
// checkbox column. A custom AttrListStore backs it so rows can be coloured by
|
|
// status while the native selection highlight still shows through.
|
|
wxDataViewCtrl* dv =
|
|
new wxDataViewCtrl((wxWindow*)parent, wxID_ANY, wxDefaultPosition,
|
|
wxSize(-1, -1),
|
|
wxDV_ROW_LINES | wxDV_VERT_RULES | wxDV_MULTIPLE);
|
|
AttrListStore* store = new AttrListStore();
|
|
dv->AssociateModel(store);
|
|
store->DecRef(); // the control owns a reference now
|
|
dv->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, [](wxDataViewEvent&) {
|
|
if (g_widget_event) g_widget_event(WxEV_DVLC_SELECTION_CHANGED, 0, 0);
|
|
});
|
|
dv->Bind(wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, [](wxDataViewEvent&) {
|
|
if (g_widget_event) g_widget_event(WxEV_DVLC_CONTEXT_MENU, 0, 0);
|
|
});
|
|
dv->Bind(wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, [](wxDataViewEvent& e) {
|
|
if (g_widget_event) {
|
|
wxDataViewCtrl* dv = (wxDataViewCtrl*)e.GetEventObject();
|
|
if (!dv) return;
|
|
wxDataViewItem item = e.GetItem();
|
|
if (!item.IsOk()) return;
|
|
int row = ((wxDataViewIndexListModel*)dv->GetModel())->GetRow(item);
|
|
g_widget_event(WxEV_DVLC_VALUE_CHANGED, row, (long)e.GetColumn());
|
|
}
|
|
});
|
|
dv->Bind(wxEVT_DATAVIEW_COLUMN_HEADER_CLICK, [](wxDataViewEvent& e) {
|
|
if (g_widget_event) g_widget_event(WxEV_DVLC_HEADER_CLICK, (long)e.GetColumn(), 0);
|
|
});
|
|
// Right-click on a column header: Excel-style per-column menu (hide/sort/...).
|
|
dv->Bind(wxEVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, [](wxDataViewEvent& e) {
|
|
if (g_widget_event) g_widget_event(WxEV_DVLC_HEADER_RIGHT_CLICK, (long)e.GetColumn(), 0);
|
|
});
|
|
dv->Bind(wxEVT_DATAVIEW_COLUMN_REORDERED, [](wxDataViewEvent&) {
|
|
if (g_widget_event) g_widget_event(WxEV_DVLC_REORDERED, 0, 0);
|
|
});
|
|
// Rubber-band (click-drag) range selection. wxDataViewCtrl has none natively.
|
|
#ifdef __WXGTK__
|
|
// On GTK the control wraps a GtkTreeView, which supports native rubber-band
|
|
// selection -- just not enabled by wx. Enable it directly. (wx delivers no
|
|
// MOTION events during the native drag, so the mouse-event fallback below is
|
|
// inapplicable on GTK.)
|
|
{
|
|
GtkWidget* tv = dv->GtkGetTreeView();
|
|
if (tv) {
|
|
gtk_tree_view_set_rubber_banding(GTK_TREE_VIEW(tv), TRUE);
|
|
// Per-cell hover tooltips (Errors column) via the native signal.
|
|
gtk_widget_set_has_tooltip(tv, TRUE);
|
|
g_signal_connect(tv, "query-tooltip", G_CALLBACK(dvlc_query_tooltip), dv);
|
|
}
|
|
}
|
|
#else
|
|
// Non-GTK fallback: track a drag anchor and extend the selection to the row
|
|
// under the cursor. LEFT_DOWN records the anchor (and lets the native handler
|
|
// select it); dragging with the button down selects the inclusive range.
|
|
dv->Bind(wxEVT_LEFT_DOWN, [](wxMouseEvent& e) {
|
|
wxDataViewCtrl* dv = (wxDataViewCtrl*)e.GetEventObject();
|
|
if (!dv) { e.Skip(); return; }
|
|
// Leave modifier-clicks to the native ctrl/shift multi-select behaviour.
|
|
if (e.ControlDown() || e.ShiftDown()) { g_drag_anchor.erase(dv); e.Skip(); return; }
|
|
wxDataViewItem item;
|
|
wxDataViewColumn* col = nullptr;
|
|
dv->HitTest(e.GetPosition(), item, col);
|
|
if (item.IsOk()) {
|
|
g_drag_anchor[dv] =
|
|
(int)((wxDataViewIndexListModel*)dv->GetModel())->GetRow(item);
|
|
} else {
|
|
g_drag_anchor.erase(dv);
|
|
}
|
|
e.Skip(); // let the native handler select the anchor row
|
|
});
|
|
dv->Bind(wxEVT_MOTION, [](wxMouseEvent& e) {
|
|
wxDataViewCtrl* dv = (wxDataViewCtrl*)e.GetEventObject();
|
|
if (!dv || !e.LeftIsDown()) { e.Skip(); return; }
|
|
auto it = g_drag_anchor.find(dv);
|
|
if (it == g_drag_anchor.end()) { e.Skip(); return; }
|
|
wxDataViewItem item;
|
|
wxDataViewColumn* col = nullptr;
|
|
dv->HitTest(e.GetPosition(), item, col);
|
|
if (!item.IsOk()) { e.Skip(); return; }
|
|
wxDataViewIndexListModel* model = (wxDataViewIndexListModel*)dv->GetModel();
|
|
int cur = (int)model->GetRow(item);
|
|
int lo = it->second < cur ? it->second : cur;
|
|
int hi = it->second < cur ? cur : it->second;
|
|
dv->UnselectAll();
|
|
for (int r = lo; r <= hi; ++r) dv->Select(model->GetItem((unsigned)r));
|
|
// Selecting programmatically does not emit SELECTION_CHANGED, so notify.
|
|
if (g_widget_event) g_widget_event(WxEV_DVLC_SELECTION_CHANGED, 0, 0);
|
|
e.Skip();
|
|
});
|
|
dv->Bind(wxEVT_LEFT_UP, [](wxMouseEvent& e) {
|
|
wxDataViewCtrl* dv = (wxDataViewCtrl*)e.GetEventObject();
|
|
if (dv) g_drag_anchor.erase(dv);
|
|
e.Skip();
|
|
});
|
|
#endif
|
|
// Row activation (double-click / Enter): report the row + column so the app
|
|
// can, e.g., open the miner's IP in a browser.
|
|
dv->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, [](wxDataViewEvent& e) {
|
|
if (g_widget_event) {
|
|
wxDataViewCtrl* dv = (wxDataViewCtrl*)e.GetEventObject();
|
|
if (!dv) return;
|
|
wxDataViewItem item = e.GetItem();
|
|
if (!item.IsOk()) return;
|
|
int row = ((wxDataViewIndexListModel*)dv->GetModel())->GetRow(item);
|
|
g_widget_event(WxEV_DVLC_ACTIVATED, row, (long)e.GetColumn());
|
|
}
|
|
});
|
|
return (void*)dv;
|
|
}
|
|
|
|
void wxbr_dvlc_append_text_col(void* win, const char* title, int width, int reorderable) {
|
|
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
|
int flags = wxDATAVIEW_COL_RESIZABLE;
|
|
if (reorderable) flags |= wxDATAVIEW_COL_REORDERABLE;
|
|
unsigned col = dv->GetColumnCount(); // next model column index
|
|
dvlc_store(win)->AppendColumn("string");
|
|
dv->AppendTextColumn(wxString::FromUTF8(title), col, wxDATAVIEW_CELL_INERT,
|
|
width, wxALIGN_NOT, flags);
|
|
}
|
|
|
|
// Same bookkeeping as wxbr_dvlc_append_text_col, but the column is drawn by a
|
|
// SegmentRenderer so its cell values can carry the '\x1f'/'\x1e' colour-run
|
|
// encoding produced by alkane_gui::model::encode_segments. An ordinary string
|
|
// with no encoding in it still renders fine (single default-coloured run).
|
|
void* wxbr_dvlc_append_colored_col(void* win, const char* title, int width, int reorderable) {
|
|
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
|
int flags = wxDATAVIEW_COL_RESIZABLE;
|
|
if (reorderable) flags |= wxDATAVIEW_COL_REORDERABLE;
|
|
unsigned col = dv->GetColumnCount(); // next model column index
|
|
dvlc_store(win)->AppendColumn("string");
|
|
SegmentRenderer* renderer = new SegmentRenderer();
|
|
wxDataViewColumn* c = new wxDataViewColumn(wxString::FromUTF8(title), renderer, col,
|
|
width, wxALIGN_NOT, flags);
|
|
dv->AppendColumn(c);
|
|
return (void*)c;
|
|
}
|
|
|
|
void wxbr_dvlc_append_toggle_col(void* win, int width) {
|
|
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
|
unsigned col = dv->GetColumnCount();
|
|
dvlc_store(win)->AppendColumn("bool");
|
|
dv->AppendToggleColumn(wxT(""), col, wxDATAVIEW_CELL_ACTIVATABLE, width,
|
|
wxALIGN_CENTRE, 0);
|
|
}
|
|
|
|
void wxbr_dvlc_append_row(void* win, int nvals, const char** vals) {
|
|
// All columns are text now (row selection replaced the checkbox column).
|
|
wxVector<wxVariant> values;
|
|
for (int i = 0; i < nvals; i++) {
|
|
values.push_back(wxVariant(vals[i] ? wxString::FromUTF8(vals[i]) : wxString()));
|
|
}
|
|
AttrListStore* store = dvlc_store(win);
|
|
store->AppendItem(values);
|
|
store->row_fg.push_back(wxColour()); // no per-row colour until set
|
|
}
|
|
|
|
void wxbr_dvlc_delete_row(void* win, unsigned row) {
|
|
AttrListStore* store = dvlc_store(win);
|
|
store->DeleteItem(row);
|
|
if (row < store->row_fg.size()) store->row_fg.erase(store->row_fg.begin() + row);
|
|
}
|
|
|
|
void wxbr_dvlc_clear(void* win) {
|
|
AttrListStore* store = dvlc_store(win);
|
|
store->DeleteAllItems();
|
|
store->row_fg.clear();
|
|
store->cell_tip.clear();
|
|
}
|
|
|
|
// Set (or clear, with an empty string) the hover tooltip for one cell,
|
|
// addressed by (row, model column). Used for the Errors column's full text.
|
|
void wxbr_dvlc_set_cell_tooltip(void* win, unsigned row, unsigned col, const char* text) {
|
|
AttrListStore* store = dvlc_store(win);
|
|
if (text && text[0]) {
|
|
store->cell_tip[{row, col}] = wxString::FromUTF8(text);
|
|
} else {
|
|
store->cell_tip.erase({row, col});
|
|
}
|
|
}
|
|
|
|
// Set (or clear, when r<0) a row's text colour; used to paint rows by status.
|
|
void wxbr_dvlc_set_row_color(void* win, unsigned row, int r, int g, int b) {
|
|
AttrListStore* store = dvlc_store(win);
|
|
if (row >= store->row_fg.size()) return;
|
|
store->row_fg[row] = (r < 0) ? wxColour() : wxColour(r, g, b);
|
|
store->RowChanged(row);
|
|
}
|
|
|
|
// Set (or clear, when r<0) the alternating-row (zebra) background colour. The
|
|
// app passes a theme-aware colour; odd rows get it (see AttrListStore).
|
|
void wxbr_dvlc_set_zebra(void* win, int r, int g, int b) {
|
|
dvlc_store(win)->zebra = (r < 0) ? wxColour() : wxColour(r, g, b);
|
|
}
|
|
|
|
// The model-row index of the first (top) visible row, or -1 if none. Used to
|
|
// hold the scroll position across a full clear+refill during a live scan.
|
|
int wxbr_dvlc_get_top_row(void* win) {
|
|
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
|
wxDataViewItem item = dv->GetTopItem();
|
|
if (!item.IsOk()) return -1;
|
|
return (int)((wxDataViewIndexListModel*)dv->GetModel())->GetRow(item);
|
|
}
|
|
|
|
// Scroll so `row` is visible (used with wxbr_dvlc_get_top_row to restore the
|
|
// scroll position after a refill). Out-of-range rows are ignored -- GetItem()
|
|
// itself asserts on row >= count, so the bound MUST be checked before calling.
|
|
void wxbr_dvlc_ensure_visible(void* win, unsigned row) {
|
|
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
|
if (row >= (unsigned)dvlc_store(win)->GetItemCount()) return;
|
|
wxDataViewItem item = ((wxDataViewIndexListModel*)dv->GetModel())->GetItem(row);
|
|
if (item.IsOk()) dv->EnsureVisible(item);
|
|
}
|
|
|
|
int wxbr_dvlc_row_count(void* win) {
|
|
return (int)dvlc_store(win)->GetItemCount();
|
|
}
|
|
|
|
void wxbr_dvlc_set_cell(void* win, unsigned row, unsigned col, const char* value) {
|
|
AttrListStore* store = dvlc_store(win);
|
|
store->SetValueByRow(wxVariant(wxString::FromUTF8(value)), row, col);
|
|
store->RowValueChanged(row, col);
|
|
}
|
|
|
|
int wxbr_dvlc_get_cell(void* win, unsigned row, unsigned col, char* buf, int cap) {
|
|
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
|
AttrListStore* store = dvlc_store(win);
|
|
if (row >= store->GetItemCount() || (int)col >= (int)dv->GetColumnCount())
|
|
return 0;
|
|
wxVariant v;
|
|
store->GetValueByRow(v, row, col);
|
|
wxString s = v.GetString();
|
|
wxCharBuffer b = s.ToUTF8();
|
|
int n = (int)strlen(b.data());
|
|
if (n >= cap) n = cap - 1;
|
|
memcpy(buf, b.data(), n);
|
|
buf[n] = 0;
|
|
return n + 1;
|
|
}
|
|
|
|
int wxbr_dvlc_col_count(void* win) {
|
|
return (int)((wxDataViewCtrl*)win)->GetColumnCount();
|
|
}
|
|
|
|
// Auto-size a column (by display position) to fit its content + header.
|
|
void wxbr_dvlc_autosize_col(void* win, int pos) {
|
|
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
|
if (pos < 0 || (unsigned)pos >= dv->GetColumnCount()) return;
|
|
wxDataViewColumn* c = dv->GetColumn((unsigned)pos);
|
|
if (c) c->SetWidth(wxCOL_WIDTH_AUTOSIZE);
|
|
}
|
|
|
|
// Update a column's header text in place (creation index) without recreating
|
|
// the control -- used to refresh the ASCII sort marker on a sort click.
|
|
void wxbr_dvlc_set_col_title(void* win, int col, const char* title) {
|
|
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
|
if (col < 0 || (unsigned)col >= dv->GetColumnCount()) return;
|
|
wxDataViewColumn* c = dv->GetColumn(col);
|
|
if (c) c->SetTitle(wxString::FromUTF8(title));
|
|
}
|
|
|
|
// Show/clear the native sort arrow on a column (creation index). The app does
|
|
// its own sorting; this only draws the indicator so the header shows which
|
|
// column is the sort key and in which direction.
|
|
void wxbr_dvlc_set_sort_indicator(void* win, int col, int active, int ascending) {
|
|
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
|
if (col < 0 || (unsigned)col >= dv->GetColumnCount()) return;
|
|
wxDataViewColumn* c = dv->GetColumn(col);
|
|
if (!c) return;
|
|
if (active) {
|
|
c->SetSortOrder(ascending != 0);
|
|
} else if (c->IsSortKey()) {
|
|
c->UnsetAsSortKey();
|
|
}
|
|
}
|
|
|
|
// model column index (creation order) currently shown at display position pos
|
|
int wxbr_dvlc_col_at_pos(void* win, int pos) {
|
|
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
|
unsigned n = dv->GetColumnCount();
|
|
if (pos < 0 || (unsigned)pos >= n) return -1;
|
|
#ifdef __WXGTK__
|
|
// wxGTK's GetColumnPosition() does NOT reflect a native GtkTreeView column
|
|
// drag, so after the user reorders columns it reports the creation order and
|
|
// the reorder is silently lost. Read the real display order from the
|
|
// GtkTreeView instead, then map the column back to its wx creation index.
|
|
GtkWidget* tvw = dv->GtkGetTreeView();
|
|
if (tvw) {
|
|
GList* cols = gtk_tree_view_get_columns(GTK_TREE_VIEW(tvw)); // display order
|
|
GtkTreeViewColumn* gcol = (GtkTreeViewColumn*)g_list_nth_data(cols, (guint)pos);
|
|
g_list_free(cols);
|
|
if (gcol) {
|
|
wxDataViewColumn* wxcol = dv->GTKColumnToWX(gcol);
|
|
for (unsigned i = 0; i < n; i++) {
|
|
if (dv->GetColumn(i) == wxcol) return (int)i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
#endif
|
|
for (unsigned i = 0; i < n; i++) {
|
|
wxDataViewColumn* c = dv->GetColumn(i);
|
|
if (c && dv->GetColumnPosition(c) == (unsigned)pos) return (int)i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
long wxbr_dvlc_selected_row(void* win) {
|
|
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
|
wxDataViewItem it = dv->GetSelection();
|
|
if (!it.IsOk()) return -1;
|
|
return (long)((wxDataViewIndexListModel*)dv->GetModel())->GetRow(it);
|
|
}
|
|
|
|
// Fill `buf` with the selected row indices (multi-select); returns the count.
|
|
int wxbr_dvlc_get_selections(void* win, int* buf, int cap) {
|
|
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
|
wxDataViewIndexListModel* m = (wxDataViewIndexListModel*)dv->GetModel();
|
|
wxDataViewItemArray items;
|
|
int n = (int)dv->GetSelections(items);
|
|
int out = 0;
|
|
for (int i = 0; i < n && out < cap; i++) {
|
|
int row = m->GetRow(items[i]);
|
|
if (row >= 0) buf[out++] = row;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
void wxbr_dvlc_select_rows(void* win, const int* rows, int n) {
|
|
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
|
wxDataViewIndexListModel* m = (wxDataViewIndexListModel*)dv->GetModel();
|
|
int count = (int)dvlc_store(win)->GetItemCount();
|
|
dv->UnselectAll();
|
|
for (int i = 0; i < n; i++) {
|
|
// GetItem() asserts on an out-of-range row, so bound both ends.
|
|
if (rows[i] >= 0 && rows[i] < count) dv->Select(m->GetItem((unsigned)rows[i]));
|
|
}
|
|
}
|
|
|
|
// Retained for ABI; per-row colour is handled by the store (GetAttrByRow) and
|
|
// the native selection highlight, so these row-background hacks are no-ops now
|
|
// (they fought the selection highlight on GTK 3.3).
|
|
void wxbr_dvlc_set_alt_row_color(void* win, int r, int g, int b) {
|
|
(void)win; (void)r; (void)g; (void)b;
|
|
}
|
|
|
|
void wxbr_apply_zebra_css(int r, int g, int b) {
|
|
(void)r; (void)g; (void)b;
|
|
}
|
|
|
|
void wxbr_dvlc_select_row(void* win, long row) {
|
|
if (row < 0) return;
|
|
wxDataViewCtrl* dv = (wxDataViewCtrl*)win;
|
|
wxDataViewIndexListModel* m = (wxDataViewIndexListModel*)dv->GetModel();
|
|
if (row >= (long)dvlc_store(win)->GetItemCount()) return;
|
|
dv->Select(m->GetItem((unsigned)row));
|
|
}
|
|
|
|
// ---- timer -----------------------------------------------------------------
|
|
|
|
void* wxbr_timer(int id, int ms) {
|
|
wxTimer* t = new wxTimer;
|
|
t->Bind(wxEVT_TIMER, [id](wxTimerEvent&) {
|
|
if (g_widget_event) g_widget_event(WxEV_TIMER_TICK, id, 0);
|
|
});
|
|
t->Start(ms);
|
|
return (void*)t;
|
|
}
|
|
|
|
// ---- gauge (progress bar) ---------------------------------------------------
|
|
|
|
void* wxbr_gauge(void* parent, int range) {
|
|
wxGauge* g = new wxGauge((wxWindow*)parent, wxID_ANY, range < 1 ? 1 : range,
|
|
wxDefaultPosition, wxSize(-1, 6),
|
|
wxGA_HORIZONTAL | wxGA_SMOOTH);
|
|
return (void*)g;
|
|
}
|
|
|
|
void wxbr_gauge_set_range(void* win, int range) {
|
|
((wxGauge*)win)->SetRange(range < 1 ? 1 : range);
|
|
}
|
|
|
|
void wxbr_gauge_set_value(void* win, int value) {
|
|
((wxGauge*)win)->SetValue(value);
|
|
}
|
|
|
|
void wxbr_gauge_pulse(void* win) { ((wxGauge*)win)->Pulse(); }
|
|
|
|
// ---- show / hide any window -------------------------------------------------
|
|
|
|
void wxbr_show(void* win, int shown) { ((wxWindow*)win)->Show(shown != 0); }
|
|
|
|
// ---- open a URL in the default browser --------------------------------------
|
|
|
|
int wxbr_launch_browser(const char* url) {
|
|
return wxLaunchDefaultBrowser(wxString::FromUTF8(url)) ? 1 : 0;
|
|
}
|
|
|
|
// ---- appearance / dark mode -------------------------------------------------
|
|
|
|
void wxbr_set_appearance_system() {
|
|
#if wxCHECK_VERSION(3, 3, 0)
|
|
if (wxTheApp) wxTheApp->SetAppearance(wxApp::Appearance::System);
|
|
#endif
|
|
}
|
|
|
|
int wxbr_is_dark() {
|
|
return wxSystemSettings::GetAppearance().IsDark() ? 1 : 0;
|
|
}
|
|
|
|
// Fetch a theme colour so custom widgets follow light/dark. which:
|
|
// 0=window bg, 1=window text, 2=highlight, 3=button face, 4=grey text.
|
|
void wxbr_system_color(int which, unsigned char* r, unsigned char* g,
|
|
unsigned char* b) {
|
|
wxSystemColour idx = wxSYS_COLOUR_WINDOW;
|
|
switch (which) {
|
|
case 0: idx = wxSYS_COLOUR_WINDOW; break;
|
|
case 1: idx = wxSYS_COLOUR_WINDOWTEXT; break;
|
|
case 2: idx = wxSYS_COLOUR_HIGHLIGHT; break;
|
|
case 3: idx = wxSYS_COLOUR_BTNFACE; break;
|
|
case 4: idx = wxSYS_COLOUR_GRAYTEXT; break;
|
|
default: idx = wxSYS_COLOUR_WINDOW; break;
|
|
}
|
|
wxColour c = wxSystemSettings::GetColour(idx);
|
|
*r = c.Red();
|
|
*g = c.Green();
|
|
*b = c.Blue();
|
|
}
|
|
|
|
// ---- widget colours (for the KPI cards) -------------------------------------
|
|
|
|
void wxbr_set_bg(void* win, int r, int g, int b) {
|
|
((wxWindow*)win)->SetBackgroundColour(wxColour(r, g, b));
|
|
}
|
|
|
|
void wxbr_set_fg(void* win, int r, int g, int b) {
|
|
((wxWindow*)win)->SetForegroundColour(wxColour(r, g, b));
|
|
}
|
|
|
|
// ---- rearrangeable check-list (Choose columns, drag/reorder + check) ---------
|
|
|
|
// order[i] encodes the item at display position i: value v >= 0 means original
|
|
// item v, checked; a negative value ~v means original item v, unchecked.
|
|
void* wxbr_rearrange_list(void* parent, int n, const int* order, const char** items) {
|
|
wxArrayInt aorder;
|
|
wxArrayString aitems;
|
|
for (int i = 0; i < n; i++) {
|
|
aorder.Add(order[i]);
|
|
aitems.Add(wxString::FromUTF8(items[i]));
|
|
}
|
|
wxRearrangeCtrl* ctrl =
|
|
new wxRearrangeCtrl((wxWindow*)parent, wxID_ANY, wxDefaultPosition,
|
|
wxSize(-1, -1), aorder, aitems);
|
|
return (void*)ctrl;
|
|
}
|
|
|
|
// Read back the current order (same encoding as above) into buf; returns count.
|
|
int wxbr_rearrange_get_order(void* win, int* buf, int cap) {
|
|
wxRearrangeCtrl* ctrl = (wxRearrangeCtrl*)win;
|
|
const wxArrayInt& order = ctrl->GetList()->GetCurrentOrder();
|
|
int n = (int)order.GetCount();
|
|
for (int i = 0; i < n && i < cap; i++) buf[i] = order[i];
|
|
return n < cap ? n : cap;
|
|
}
|
|
|
|
// ---- popup menu (right-click context menu) ----------------------------------
|
|
|
|
void* wxbr_menu_new() { return (void*)new wxMenu(); }
|
|
|
|
void wxbr_menu_append(void* menu, int id, const char* label) {
|
|
((wxMenu*)menu)->Append(id, wxString::FromUTF8(label));
|
|
}
|
|
|
|
void wxbr_menu_append_separator(void* menu) {
|
|
((wxMenu*)menu)->AppendSeparator();
|
|
}
|
|
|
|
// Show the menu at the current mouse position on `win`; blocks until dismissed.
|
|
// The chosen item id is delivered via WxEV_MENU_ITEM before this returns.
|
|
void wxbr_popup_menu(void* win, void* menu) {
|
|
wxWindow* w = (wxWindow*)win;
|
|
wxMenu* m = (wxMenu*)menu;
|
|
m->Bind(wxEVT_MENU, [](wxCommandEvent& e) {
|
|
if (g_widget_event) g_widget_event(WxEV_MENU_ITEM, e.GetId(), 0);
|
|
});
|
|
w->PopupMenu(m);
|
|
delete m;
|
|
}
|
|
|
|
// ---- choice (dropdown) ------------------------------------------------------
|
|
|
|
void* wxbr_choice(void* parent, int id) {
|
|
wxChoice* c = new wxChoice((wxWindow*)parent, id);
|
|
c->Bind(wxEVT_CHOICE, [id](wxCommandEvent& e) {
|
|
if (g_widget_event)
|
|
g_widget_event(WxEV_CHOICE_CHANGED, id, (long)e.GetSelection());
|
|
});
|
|
return (void*)c;
|
|
}
|
|
|
|
void wxbr_choice_append(void* win, const char* label) {
|
|
((wxChoice*)win)->Append(wxString::FromUTF8(label));
|
|
}
|
|
|
|
void wxbr_choice_clear(void* win) { ((wxChoice*)win)->Clear(); }
|
|
|
|
int wxbr_choice_get_selection(void* win) {
|
|
return ((wxChoice*)win)->GetSelection();
|
|
}
|
|
|
|
void wxbr_choice_set_selection(void* win, int i) {
|
|
((wxChoice*)win)->SetSelection(i);
|
|
}
|
|
|
|
// ---- media (icon) button: play / pause / stop -------------------------------
|
|
|
|
// kind: 0=play (triangle), 1=pause (two bars), 2=stop (square). Drawn in the
|
|
// given colour on a transparent bitmap so it reads as a coloured glyph on the
|
|
// native button. Emits WxEV_BUTTON_CLICKED with `id`, like a normal button.
|
|
void* wxbr_media_button(void* parent, int id, int kind, int r, int g, int b) {
|
|
const int sz = 20;
|
|
wxImage img(sz, sz);
|
|
img.InitAlpha();
|
|
unsigned char* a = img.GetAlpha();
|
|
if (a) memset(a, 0, (size_t)sz * sz); // fully transparent
|
|
wxBitmap bmp(img);
|
|
{
|
|
wxMemoryDC mdc(bmp);
|
|
wxGraphicsContext* gc = wxGraphicsContext::Create(mdc);
|
|
if (gc) {
|
|
gc->SetAntialiasMode(wxANTIALIAS_DEFAULT);
|
|
wxColour col(r, g, b);
|
|
gc->SetBrush(wxBrush(col));
|
|
gc->SetPen(wxPen(col, 1.0));
|
|
if (kind == 0) {
|
|
wxGraphicsPath p = gc->CreatePath();
|
|
p.MoveToPoint(5, 4);
|
|
p.AddLineToPoint(16, 10);
|
|
p.AddLineToPoint(5, 16);
|
|
p.CloseSubpath();
|
|
gc->FillPath(p);
|
|
} else if (kind == 1) {
|
|
gc->DrawRectangle(5, 4, 3.5, 12);
|
|
gc->DrawRectangle(11.5, 4, 3.5, 12);
|
|
} else {
|
|
gc->DrawRectangle(5, 5, 10, 10);
|
|
}
|
|
delete gc;
|
|
}
|
|
}
|
|
wxBitmapButton* btn = new wxBitmapButton((wxWindow*)parent, id, bmp);
|
|
btn->Bind(wxEVT_BUTTON, [id](wxCommandEvent&) {
|
|
if (g_widget_event) g_widget_event(WxEV_BUTTON_CLICKED, id, 0);
|
|
});
|
|
return (void*)btn;
|
|
}
|
|
|
|
// ---- live theme changes -----------------------------------------------------
|
|
|
|
// Emit WxEV_SYS_COLOUR when the OS light/dark theme changes so the app can
|
|
// repaint custom-coloured widgets (KPI cards, table rows).
|
|
void wxbr_bind_sys_colour(void* win) {
|
|
((wxWindow*)win)->Bind(wxEVT_SYS_COLOUR_CHANGED, [](wxSysColourChangedEvent& e) {
|
|
e.Skip();
|
|
if (g_widget_event) g_widget_event(WxEV_SYS_COLOUR, 0, 0);
|
|
});
|
|
}
|
|
|
|
// Run a deferred action after the current event finishes unwinding: emits
|
|
// WxEV_DEFERRED with `id` from the main event loop. Used to rebuild a control
|
|
// from outside its own event handler (destroying a widget mid-event is unsafe).
|
|
void wxbr_call_after(int id) {
|
|
if (wxTheApp) {
|
|
wxTheApp->CallAfter([id]() {
|
|
if (g_widget_event) g_widget_event(WxEV_DEFERRED, id, 0);
|
|
});
|
|
}
|
|
}
|
|
|
|
// ---- status-bar error/warning indicator -------------------------------------
|
|
// A colour-tinted static text pinned to the RIGHT-hand field of the frame's
|
|
// status bar, so scan/apply errors surface in the bottom-right corner without
|
|
// overwriting the main (left) scan-status text. The status bar is reconfigured
|
|
// to two fields: field 0 (stretch) carries the scan status set via the upstream
|
|
// SetStatusText path; field 1 (fixed) hosts this coloured label, which we
|
|
// reposition into that field's rect whenever the bar is resized.
|
|
static wxStaticText* g_status_indicator = nullptr;
|
|
|
|
static void reposition_status_indicator() {
|
|
if (!g_status_indicator) return;
|
|
wxStatusBar* sb = wxDynamicCast(g_status_indicator->GetParent(), wxStatusBar);
|
|
if (!sb) return;
|
|
wxRect r;
|
|
if (!sb->GetFieldRect(1, r)) return;
|
|
wxSize best = g_status_indicator->GetBestSize();
|
|
int h = best.GetHeight();
|
|
int y = r.y + (r.height - h) / 2;
|
|
g_status_indicator->SetSize(r.x + 4, y, r.width - 8, h);
|
|
}
|
|
|
|
void wxbr_status_indicator_init(void* frame_ptr) {
|
|
wxFrame* frame = (wxFrame*)frame_ptr;
|
|
wxStatusBar* sb = frame->GetStatusBar();
|
|
if (!sb) sb = frame->CreateStatusBar();
|
|
int widths[2] = { -1, 340 };
|
|
sb->SetFieldsCount(2);
|
|
sb->SetStatusWidths(2, widths);
|
|
if (!g_status_indicator) {
|
|
g_status_indicator = new wxStaticText(sb, wxID_ANY, wxEmptyString,
|
|
wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT | wxST_NO_AUTORESIZE);
|
|
sb->Bind(wxEVT_SIZE, [](wxSizeEvent& ev) {
|
|
reposition_status_indicator();
|
|
ev.Skip();
|
|
});
|
|
}
|
|
reposition_status_indicator();
|
|
}
|
|
|
|
// Set the bottom-right indicator text and its colour. An empty string clears it.
|
|
void wxbr_status_indicator_set(const char* text, int r, int g, int b) {
|
|
if (!g_status_indicator) return;
|
|
g_status_indicator->SetLabel(wxString::FromUTF8(text ? text : ""));
|
|
g_status_indicator->SetForegroundColour(
|
|
wxColour((unsigned char)r, (unsigned char)g, (unsigned char)b));
|
|
reposition_status_indicator();
|
|
g_status_indicator->Refresh();
|
|
}
|
|
|
|
// ---- deferred destroy (safe from event handlers) ----------------------------
|
|
|
|
void wxbr_destroy(void* win) { ((wxWindow*)win)->Destroy(); }
|
|
|
|
// Destroy a window's children immediately (used to rebuild a panel's dynamic
|
|
// contents in place, e.g. the filter dialog's per-column controls).
|
|
void wxbr_destroy_children(void* win) { ((wxWindow*)win)->DestroyChildren(); }
|
|
|
|
} // extern "C"
|