Designer: poll shared XFrame selection, editable ComboBox/Listbox items, Scrollbar adjustment, Text text, Memo preview, border props
- Placement hole: the designer only set g_xframe_selected from its own combined palette, so a widget chosen in the separate XFrame palette never placed (on_canvas_press saw an empty selection). Poll the shared selection file (same path rule as xframe_selection_path) on the 200ms timer and at the start of on_canvas_press, so either palette works. Verified under openbox: separate-palette flow now places (GtkButton 0->1), combined-palette flow still places (1->2). - ComboBox/Listbox: new editable items prop (pipe-separated, default Item 1|Item 2|Item 3); preview rebuilds from it, generator emits one append/insert per item. active / selection-mode unchanged. - Scrollbar: editable lower/upper/step-increment/page-increment/ page-size; preview reconfigures the adjustment, generator emits them. value/inverted unchanged; defaults preserve 0-100/1/10/10. - Text: new text prop (same as Memo); preview and generator use it, falling back to the widget name when empty. - Memo: preview now uses the text prop (was hardcoded to the name); added cursor-visible (same as Text). - Border (GTK): border-width/border-color/border-radius in the common prop set; preview and generator emit border + border-radius CSS. Defaults (0/empty/0) preserve the current appearance. - Tests: SELFTEST 35 items, widget_tests 58 passed, widget_tests.sh 5 passed. Border CSS verified in generated code; XFrame border verified in the layout (1072 red pixels, state border=3 #ff0000 10).
This commit is contained in:
1 parent
75470266f3
commit
fbfab38e41
4 files changed
+399
-28
No files matched your search
+46
-13
@@ -546,7 +546,11 @@ static std::string prop_lines(const Widget& w, const std::string& target,
|
||||
const std::set<std::string>& skip) {
|
||||
std::ostringstream o;
|
||||
for (const auto& [name, value] : w.props) {
|
||||
if (name == "background" || name == "foreground") continue;
|
||||
// background/foreground/border are consumed by colour_lines
|
||||
// (CSS); they are not real GObject properties.
|
||||
if (name == "background" || name == "foreground" ||
|
||||
name == "border-width" || name == "border-color" ||
|
||||
name == "border-radius") continue;
|
||||
if (skip.count(name)) continue;
|
||||
const PropDef* def = find_prop(w.type, name);
|
||||
if (!def) continue;
|
||||
@@ -579,10 +583,25 @@ static std::string prop_lines(const Widget& w, const std::string& target,
|
||||
static std::string colour_lines(const Widget& w, const std::string& target) {
|
||||
auto bg = w.props.find("background");
|
||||
auto fg = w.props.find("foreground");
|
||||
if (bg == w.props.end() && fg == w.props.end()) return "";
|
||||
auto bw = w.props.find("border-width");
|
||||
auto bc = w.props.find("border-color");
|
||||
auto br = w.props.find("border-radius");
|
||||
int bwidth = 0, bradius = 0;
|
||||
std::string bcolor;
|
||||
if (bw != w.props.end()) bwidth = std::atoi(bw->second.c_str());
|
||||
if (br != w.props.end()) bradius = std::atoi(br->second.c_str());
|
||||
if (bc != w.props.end()) bcolor = bc->second;
|
||||
// A border line needs both a width and a colour; a radius alone
|
||||
// still rounds the corners. Width/radius 0 = no border (default).
|
||||
bool has_border = (bwidth > 0 && !bcolor.empty()) || bradius > 0;
|
||||
if (bg == w.props.end() && fg == w.props.end() && !has_border) return "";
|
||||
std::string body;
|
||||
if (bg != w.props.end()) body += "background: " + bg->second + "; ";
|
||||
if (fg != w.props.end()) body += "color: " + fg->second + "; ";
|
||||
if (bwidth > 0 && !bcolor.empty())
|
||||
body += "border: " + std::to_string(bwidth) + "px solid " + bcolor + "; ";
|
||||
if (bradius > 0)
|
||||
body += "border-radius: " + std::to_string(bradius) + "px; ";
|
||||
std::ostringstream o;
|
||||
o << " GtkCssProvider* css = gtk_css_provider_new();\n"
|
||||
<< " gtk_css_provider_load_from_data(css, \"* { " << body
|
||||
@@ -1849,7 +1868,7 @@ static std::string emit_widget_cpp(const Widget& w) {
|
||||
skip = {"label"};
|
||||
} else if (t == "Combobox") {
|
||||
creation = " " + M + " = gtk_combo_box_text_new();\n";
|
||||
skip = {"active"};
|
||||
skip = {"active", "items"};
|
||||
} else if (t == "Menubutton") {
|
||||
creation = " " + M + " = gtk_menu_button_new();\n";
|
||||
skip = {"label"};
|
||||
@@ -1925,9 +1944,14 @@ static std::string emit_widget_cpp(const Widget& w) {
|
||||
creation = M +
|
||||
" = gtk_scrollbar_new(GTK_ORIENTATION_HORIZONTAL,\n"
|
||||
" gtk_adjustment_new(" +
|
||||
fmt_double(prop_or(w, "value", "0")) +
|
||||
", 0, 100, 1, 10, 10));\n";
|
||||
skip = {"value"};
|
||||
fmt_double(prop_or(w, "value", "0")) + ", " +
|
||||
fmt_double(prop_or(w, "lower", "0")) + ", " +
|
||||
fmt_double(prop_or(w, "upper", "100")) + ", " +
|
||||
fmt_double(prop_or(w, "step-increment", "1")) + ", " +
|
||||
fmt_double(prop_or(w, "page-increment", "10")) + ", " +
|
||||
fmt_double(prop_or(w, "page-size", "10")) + "));\n";
|
||||
skip = {"value", "lower", "upper", "step-increment",
|
||||
"page-increment", "page-size"};
|
||||
} else if (t == "Shape" || t == "Plot") {
|
||||
creation =
|
||||
" GtkWidget* frame = gtk_frame_new(nullptr);\n"
|
||||
@@ -1991,8 +2015,13 @@ static std::string emit_widget_cpp(const Widget& w) {
|
||||
"max-candles", "indicators", "scale-mode",
|
||||
"compare-files", "drawings"};
|
||||
}
|
||||
else if (t == "Listbox") inner = "gtk_list_box_new()";
|
||||
else if (t == "Text") inner = "gtk_text_view_new()";
|
||||
else if (t == "Listbox") {
|
||||
inner = "gtk_list_box_new()";
|
||||
skip = {"items"};
|
||||
} else if (t == "Text") {
|
||||
inner = "gtk_text_view_new()";
|
||||
skip = {"text"};
|
||||
}
|
||||
else if (t == "Spreadsheet") {
|
||||
inner = "gtk_grid_new()";
|
||||
skip = {"rows", "columns"};
|
||||
@@ -2025,22 +2054,26 @@ static std::string emit_widget_cpp(const Widget& w) {
|
||||
|
||||
// Creation-time extras that need the live widget.
|
||||
if (t == "Combobox") {
|
||||
for (int i = 1; i <= 3; ++i)
|
||||
for (const std::string& name :
|
||||
split_on('|', prop_or(w, "items", "Item 1|Item 2|Item 3")))
|
||||
o << " gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT("
|
||||
<< M << "), \"Item " << i << "\");\n";
|
||||
<< M << "), \"" << esc_c(name) << "\");\n";
|
||||
o << " gtk_combo_box_set_active(GTK_COMBO_BOX(" << M << "), "
|
||||
<< prop_or(w, "active", "0") << ");\n";
|
||||
} else if (t == "Menubutton") {
|
||||
o << " gtk_button_set_label(GTK_BUTTON(" << M << "), \""
|
||||
<< esc_c(label) << "\");\n";
|
||||
} else if (t == "Listbox") {
|
||||
for (int i = 1; i <= 3; ++i)
|
||||
for (const std::string& name :
|
||||
split_on('|', prop_or(w, "items", "Item 1|Item 2|Item 3")))
|
||||
o << " gtk_list_box_insert(GTK_LIST_BOX(" << M
|
||||
<< "), gtk_label_new(\"Item " << i << "\"), -1);\n";
|
||||
<< "), gtk_label_new(\"" << esc_c(name) << "\"), -1);\n";
|
||||
} else if (t == "Text") {
|
||||
std::string txt = prop_or(w, "text", "");
|
||||
if (txt.empty()) txt = w.name;
|
||||
o << " gtk_text_buffer_set_text(gtk_text_view_get_buffer("
|
||||
"GTK_TEXT_VIEW("
|
||||
<< M << ")), \"" << esc_c(w.name) << "\", -1);\n";
|
||||
<< M << ")), \"" << esc_c(txt) << "\", -1);\n";
|
||||
} else if (t == "Spreadsheet") {
|
||||
int rows = std::atoi(prop_or(w, "rows", "5").c_str());
|
||||
int cols = std::atoi(prop_or(w, "columns", "4").c_str());
|
||||
|
||||
+317
-10
@@ -20,6 +20,7 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <sys/stat.h>
|
||||
#include <ctime>
|
||||
#include <fstream>
|
||||
#include <limits>
|
||||
@@ -192,6 +193,12 @@ static std::vector<GtkWidget*> g_palette_fd_buttons; // 36 LightGUI buttons, kT
|
||||
static std::vector<GtkWidget*> g_xframe_buttons; // one button per XFrame widget, section order
|
||||
static int g_xframe_icons_attached = 0; // XFrame buttons given their .ico
|
||||
static std::string g_xframe_selected; // last XFrame entry selected
|
||||
static long g_xframe_sel_mtime = -1; // mtime of the selection file last seen
|
||||
// Border for XFrame widgets placed from here, set in the Inspector's
|
||||
// Window view. 0/""/0 = use the layout's style default (no change).
|
||||
static int g_xframe_border_width = 0;
|
||||
static std::string g_xframe_border_color;
|
||||
static int g_xframe_border_radius = 0;
|
||||
static int g_palette_selftest_rc = 1; // set to 0 by the palette self-test
|
||||
static GtkWidget* g_palette_menu_item = nullptr; // View -> Widget Palette
|
||||
static GtkWidget* g_inspector_menu_item = nullptr; // View -> Object Inspector
|
||||
@@ -369,10 +376,26 @@ static void refresh_item_css(Item* it) {
|
||||
std::string css;
|
||||
auto bg = it->model.props.find("background");
|
||||
auto fg = it->model.props.find("foreground");
|
||||
if (bg != it->model.props.end() || fg != it->model.props.end()) {
|
||||
auto bw = it->model.props.find("border-width");
|
||||
auto bc = it->model.props.find("border-color");
|
||||
auto br = it->model.props.find("border-radius");
|
||||
int bwidth = 0, bradius = 0;
|
||||
std::string bcolor;
|
||||
if (bw != it->model.props.end()) bwidth = std::atoi(bw->second.c_str());
|
||||
if (br != it->model.props.end()) bradius = std::atoi(br->second.c_str());
|
||||
if (bc != it->model.props.end()) bcolor = bc->second;
|
||||
// A border line needs both a width and a colour; a radius alone
|
||||
// still rounds the corners. Width/radius 0 = no border (default).
|
||||
bool has_border = (bwidth > 0 && !bcolor.empty()) || bradius > 0;
|
||||
if (bg != it->model.props.end() || fg != it->model.props.end() ||
|
||||
has_border) {
|
||||
css = "* { ";
|
||||
if (bg != it->model.props.end()) css += "background: " + bg->second + "; ";
|
||||
if (fg != it->model.props.end()) css += "color: " + fg->second + "; ";
|
||||
if (bwidth > 0 && !bcolor.empty())
|
||||
css += "border: " + std::to_string(bwidth) + "px solid " + bcolor + "; ";
|
||||
if (bradius > 0)
|
||||
css += "border-radius: " + std::to_string(bradius) + "px; ";
|
||||
css += "}";
|
||||
}
|
||||
gtk_css_provider_load_from_data(it->css, css.c_str(), -1, nullptr);
|
||||
@@ -493,6 +516,48 @@ static void rebuild_menubar(Item* it) {
|
||||
gtk_widget_show_all(it->inner);
|
||||
}
|
||||
|
||||
// Rebuild a ComboBox's items from its "items" prop ("Item 1|Item 2|Item 3").
|
||||
static void rebuild_combobox(Item* it) {
|
||||
GtkComboBoxText* c = GTK_COMBO_BOX_TEXT(it->target);
|
||||
gtk_combo_box_text_remove_all(c);
|
||||
std::vector<std::string> items =
|
||||
split_spec(prop_or_str(it, "items", "Item 1|Item 2|Item 3"), '|');
|
||||
for (const std::string& name : items)
|
||||
gtk_combo_box_text_append_text(c, name.c_str());
|
||||
int active = prop_int(it, "active", 0);
|
||||
if (items.empty()) active = -1;
|
||||
else if (active < -1) active = -1;
|
||||
else if (active >= (int)items.size()) active = (int)items.size() - 1;
|
||||
gtk_combo_box_set_active(GTK_COMBO_BOX(c), active);
|
||||
gtk_widget_show_all(it->inner);
|
||||
}
|
||||
|
||||
// Rebuild a Listbox's rows from its "items" prop ("Item 1|Item 2|Item 3").
|
||||
static void rebuild_listbox(Item* it) {
|
||||
GtkListBox* lb = GTK_LIST_BOX(it->target);
|
||||
GList* kids = gtk_container_get_children(GTK_CONTAINER(lb));
|
||||
for (GList* l = kids; l; l = l->next)
|
||||
gtk_widget_destroy(GTK_WIDGET(l->data));
|
||||
g_list_free(kids);
|
||||
for (const std::string& name :
|
||||
split_spec(prop_or_str(it, "items", "Item 1|Item 2|Item 3"), '|'))
|
||||
gtk_list_box_insert(lb, gtk_label_new(name.c_str()), -1);
|
||||
gtk_widget_show_all(it->inner);
|
||||
}
|
||||
|
||||
// Reconfigure a Scrollbar's adjustment from its props (value, lower,
|
||||
// upper, step-increment, page-increment, page-size).
|
||||
static void rebuild_scrollbar(Item* it) {
|
||||
GtkAdjustment* adj = gtk_range_get_adjustment(GTK_RANGE(it->target));
|
||||
double value = std::atof(prop_or_str(it, "value", "0").c_str());
|
||||
double lower = std::atof(prop_or_str(it, "lower", "0").c_str());
|
||||
double upper = std::atof(prop_or_str(it, "upper", "100").c_str());
|
||||
double step = std::atof(prop_or_str(it, "step-increment", "1").c_str());
|
||||
double page = std::atof(prop_or_str(it, "page-increment", "10").c_str());
|
||||
double psize = std::atof(prop_or_str(it, "page-size", "10").c_str());
|
||||
gtk_adjustment_configure(adj, value, lower, upper, step, page, psize);
|
||||
}
|
||||
|
||||
// Drop every column of a TreeView (helper for the ListView/TreeView/
|
||||
// StringGrid rebuilds below).
|
||||
static void drop_tree_columns(GtkTreeView* tv) {
|
||||
@@ -626,7 +691,10 @@ static GtkWidget* datetime_entry(Item* it) {
|
||||
static void apply_prop(Item* it, const PropDef& def, const std::string& value) {
|
||||
const char* name = def.name;
|
||||
if (std::strcmp(name, "background") == 0 ||
|
||||
std::strcmp(name, "foreground") == 0) {
|
||||
std::strcmp(name, "foreground") == 0 ||
|
||||
std::strcmp(name, "border-width") == 0 ||
|
||||
std::strcmp(name, "border-color") == 0 ||
|
||||
std::strcmp(name, "border-radius") == 0) {
|
||||
refresh_item_css(it);
|
||||
return;
|
||||
}
|
||||
@@ -702,7 +770,13 @@ static void apply_prop(Item* it, const PropDef& def, const std::string& value) {
|
||||
if (ty == "Memo" && std::strcmp(name, "text") == 0) {
|
||||
gtk_text_buffer_set_text(
|
||||
gtk_text_view_get_buffer(GTK_TEXT_VIEW(it->target)),
|
||||
value.c_str(), -1);
|
||||
value.empty() ? it->model.name.c_str() : value.c_str(), -1);
|
||||
return;
|
||||
}
|
||||
if (ty == "Text" && std::strcmp(name, "text") == 0) {
|
||||
gtk_text_buffer_set_text(
|
||||
gtk_text_view_get_buffer(GTK_TEXT_VIEW(it->target)),
|
||||
value.empty() ? it->model.name.c_str() : value.c_str(), -1);
|
||||
return;
|
||||
}
|
||||
if (ty == "Image" && std::strcmp(name, "file") == 0) {
|
||||
@@ -748,6 +822,25 @@ static void apply_prop(Item* it, const PropDef& def, const std::string& value) {
|
||||
rebuild_menubar(it);
|
||||
return;
|
||||
}
|
||||
if (ty == "Combobox" &&
|
||||
(std::strcmp(name, "items") == 0 ||
|
||||
std::strcmp(name, "active") == 0)) {
|
||||
rebuild_combobox(it);
|
||||
return;
|
||||
}
|
||||
if (ty == "Listbox" && std::strcmp(name, "items") == 0) {
|
||||
rebuild_listbox(it);
|
||||
return;
|
||||
}
|
||||
if (ty == "Scrollbar" &&
|
||||
(std::strcmp(name, "lower") == 0 ||
|
||||
std::strcmp(name, "upper") == 0 ||
|
||||
std::strcmp(name, "step-increment") == 0 ||
|
||||
std::strcmp(name, "page-increment") == 0 ||
|
||||
std::strcmp(name, "page-size") == 0)) {
|
||||
rebuild_scrollbar(it);
|
||||
return;
|
||||
}
|
||||
if (ty == "SpinEdit" &&
|
||||
(std::strcmp(name, "minimum") == 0 ||
|
||||
std::strcmp(name, "maximum") == 0 ||
|
||||
@@ -1778,6 +1871,39 @@ static void refresh_props() {
|
||||
g_signal_connect(menus, "clicked",
|
||||
G_CALLBACK(on_edit_menus_clicked), nullptr);
|
||||
add_row(row++, "menu bar", menus);
|
||||
// Border for XFrame widgets placed from here (the layout
|
||||
// window). Applies to widgets placed after it is set; the
|
||||
// designer widgets' own border is on each widget's properties.
|
||||
GtkWidget* xf_hdr = gtk_label_new("<b>XFrame placement border</b>");
|
||||
gtk_label_set_use_markup(GTK_LABEL(xf_hdr), TRUE);
|
||||
gtk_widget_set_halign(xf_hdr, GTK_ALIGN_START);
|
||||
gtk_grid_attach(GTK_GRID(g_props_grid), xf_hdr, 0, row++, 2, 1);
|
||||
GtkWidget* xbw = gtk_spin_button_new_with_range(0, 100, 1);
|
||||
gtk_spin_button_set_value(GTK_SPIN_BUTTON(xbw), g_xframe_border_width);
|
||||
g_signal_connect(xbw, "value-changed",
|
||||
G_CALLBACK(on_win_spin_changed),
|
||||
(gpointer) "xframe-border-width");
|
||||
add_row(row++, "border-width", xbw);
|
||||
GtkWidget* xb_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
|
||||
GtkWidget* xbc = gtk_entry_new();
|
||||
gtk_entry_set_text(GTK_ENTRY(xbc), g_xframe_border_color.c_str());
|
||||
g_signal_connect(xbc, "changed",
|
||||
G_CALLBACK(on_win_entry_changed),
|
||||
(gpointer) "xframe-border-color");
|
||||
GtkWidget* xbp = gtk_button_new_with_label("Pick…");
|
||||
PickBinding* xpb = new PickBinding{GTK_ENTRY(xbc)};
|
||||
g_pick_bindings.push_back(xpb);
|
||||
g_signal_connect(xbp, "clicked",
|
||||
G_CALLBACK(on_win_bg_pick_clicked), xpb);
|
||||
gtk_box_pack_start(GTK_BOX(xb_box), xbc, TRUE, TRUE, 0);
|
||||
gtk_box_pack_start(GTK_BOX(xb_box), xbp, FALSE, FALSE, 0);
|
||||
add_row(row++, "border-color", xb_box);
|
||||
GtkWidget* xbr = gtk_spin_button_new_with_range(0, 1000, 1);
|
||||
gtk_spin_button_set_value(GTK_SPIN_BUTTON(xbr), g_xframe_border_radius);
|
||||
g_signal_connect(xbr, "value-changed",
|
||||
G_CALLBACK(on_win_spin_changed),
|
||||
(gpointer) "xframe-border-radius");
|
||||
add_row(row++, "border-radius", xbr);
|
||||
gtk_widget_show_all(g_props_grid);
|
||||
g_syncing_props = false;
|
||||
return;
|
||||
@@ -2197,15 +2323,30 @@ static void on_win_entry_changed(GtkEntry* e, gpointer which) {
|
||||
if (std::strcmp(w, "title") == 0) set_project_title(text);
|
||||
if (std::strcmp(w, "class") == 0) set_project_class(text);
|
||||
if (std::strcmp(w, "background") == 0) set_project_background(text);
|
||||
if (std::strcmp(w, "xframe-border-color") == 0) {
|
||||
GdkRGBA rgba;
|
||||
if (gdk_rgba_parse(&rgba, text)) {
|
||||
gchar* s = gdk_rgba_to_string(&rgba);
|
||||
g_xframe_border_color = s;
|
||||
g_free(s);
|
||||
} else {
|
||||
g_xframe_border_color = text;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void on_win_spin_changed(GtkSpinButton* s, gpointer which) {
|
||||
if (g_syncing_props) return;
|
||||
int v = gtk_spin_button_get_value_as_int(s);
|
||||
if (std::strcmp((const char*)which, "w") == 0)
|
||||
const char* w = (const char*)which;
|
||||
if (std::strcmp(w, "w") == 0)
|
||||
set_project_size(v, g_project.win_height);
|
||||
else
|
||||
else if (std::strcmp(w, "h") == 0)
|
||||
set_project_size(g_project.win_width, v);
|
||||
else if (std::strcmp(w, "xframe-border-width") == 0)
|
||||
g_xframe_border_width = v;
|
||||
else if (std::strcmp(w, "xframe-border-radius") == 0)
|
||||
g_xframe_border_radius = v;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------- bulk UI callbacks
|
||||
@@ -2527,6 +2668,53 @@ static std::string xframe_place_path() {
|
||||
return "/tmp/lightgui-place.txt";
|
||||
}
|
||||
|
||||
// Convert a canonical colour string ("rgb(r,g,b)" / "rgba(...)" /
|
||||
// "#rrggbb" / named) to "#rrggbb" for the place file; "" when unset
|
||||
// or unparseable.
|
||||
static std::string colour_to_hex(const std::string& s) {
|
||||
if (s.empty()) return "";
|
||||
GdkRGBA rgba;
|
||||
if (!gdk_rgba_parse(&rgba, s.c_str())) return "";
|
||||
char buf[16];
|
||||
std::snprintf(buf, sizeof buf, "#%02x%02x%02x",
|
||||
(int)(rgba.red * 255 + 0.5),
|
||||
(int)(rgba.green * 255 + 0.5),
|
||||
(int)(rgba.blue * 255 + 0.5));
|
||||
return buf;
|
||||
}
|
||||
|
||||
// mtime of the palette -> layout selection file (same file the layout
|
||||
// polls). -1 when the file does not exist.
|
||||
static long xframe_selection_mtime() {
|
||||
struct stat st;
|
||||
if (::stat(xframe_selection_path().c_str(), &st) != 0) return -1;
|
||||
return (long)st.st_mtime * 1000000000L + (long)st.st_mtim.tv_nsec;
|
||||
}
|
||||
|
||||
// Pick up a selection made in the SEPARATE XFrame palette process.
|
||||
// That process writes only the shared selection file; it cannot set
|
||||
// this process's in-memory g_xframe_selected. So poll the file's mtime
|
||||
// (same pattern the layout uses) and adopt a new selection when the
|
||||
// file changes. Called from the 200 ms code-window poll and from
|
||||
// on_canvas_press, so a click right after selecting still works.
|
||||
static void poll_xframe_selection() {
|
||||
long mt = xframe_selection_mtime();
|
||||
if (mt < 0 || mt == g_xframe_sel_mtime) return;
|
||||
g_xframe_sel_mtime = mt;
|
||||
char name[256] = "";
|
||||
FILE* f = std::fopen(xframe_selection_path().c_str(), "r");
|
||||
if (f) {
|
||||
if (std::fgets(name, sizeof name, f))
|
||||
name[std::strcspn(name, "\r\n")] = 0;
|
||||
std::fclose(f);
|
||||
}
|
||||
if (name[0] && g_xframe_selected != name) {
|
||||
g_xframe_selected = name;
|
||||
status(std::string("XFrame selected: ") + name +
|
||||
" \u2014 click in the layout window to place it");
|
||||
}
|
||||
}
|
||||
|
||||
static void on_xframe_palette_clicked(GtkButton*, gpointer data) {
|
||||
const XFramePaletteEntry* e = (const XFramePaletteEntry*)data;
|
||||
FILE* f = std::fopen(xframe_selection_path().c_str(), "w");
|
||||
@@ -2535,6 +2723,7 @@ static void on_xframe_palette_clicked(GtkButton*, gpointer data) {
|
||||
std::fclose(f);
|
||||
}
|
||||
g_xframe_selected = e->name;
|
||||
g_xframe_sel_mtime = xframe_selection_mtime();
|
||||
status(std::string("XFrame selected: ") + e->name +
|
||||
" \u2014 click in the layout window to place it");
|
||||
}
|
||||
@@ -2784,6 +2973,7 @@ static void on_view_inspector_toggled(GtkCheckMenuItem* item, gpointer) {
|
||||
}
|
||||
|
||||
static gboolean on_canvas_press(GtkWidget* w, GdkEventButton* e, gpointer) {
|
||||
if (e->button == 1) poll_xframe_selection(); // separate-palette selection, if any
|
||||
if (e->button == 1 && !g_xframe_selected.empty()) {
|
||||
// An XFrame widget is selected: the user expects this click to
|
||||
// place it. XFrame widgets live in the separate layout window, so
|
||||
@@ -2801,11 +2991,17 @@ static gboolean on_canvas_press(GtkWidget* w, GdkEventButton* e, gpointer) {
|
||||
if (ly > 720) ly = 720;
|
||||
FILE* f = std::fopen(xframe_place_path().c_str(), "w");
|
||||
if (f) {
|
||||
std::fprintf(f, "%d %d\n", lx, ly);
|
||||
std::string hex = colour_to_hex(g_xframe_border_color);
|
||||
std::fprintf(f, "%d %d %d %s %d\n", lx, ly,
|
||||
g_xframe_border_width,
|
||||
hex.empty() ? "-" : hex.c_str(),
|
||||
g_xframe_border_radius);
|
||||
std::fclose(f);
|
||||
}
|
||||
std::string name = g_xframe_selected;
|
||||
g_xframe_selected.clear();
|
||||
// Do not let the poll re-arm the selection we just consumed.
|
||||
g_xframe_sel_mtime = xframe_selection_mtime();
|
||||
status("Placing " + name + " in the LightGUI Layout window");
|
||||
return TRUE;
|
||||
}
|
||||
@@ -3272,6 +3468,7 @@ static void codewin_sync_from_debugger(bool force) {
|
||||
}
|
||||
|
||||
static gboolean on_codewin_poll(gpointer) {
|
||||
poll_xframe_selection(); // selection made in the separate XFrame palette
|
||||
codewin_sync_from_debugger(false);
|
||||
// Round 4: keep the test snapshot fresh even when nothing
|
||||
// debugger-side changed — typing in the code view changes the
|
||||
@@ -3696,9 +3893,13 @@ static void on_new(GtkMenuItem*, gpointer) {
|
||||
|
||||
static gboolean selftest_step(gpointer) {
|
||||
bool ok = true;
|
||||
auto check = [&](bool cond) {
|
||||
if (!cond) ok = false;
|
||||
auto check_impl = [&](bool cond, int line) {
|
||||
if (!cond) {
|
||||
ok = false;
|
||||
std::fprintf(stderr, "SELFTEST FAIL check at line %d\n", line);
|
||||
}
|
||||
};
|
||||
#define check(...) check_impl((__VA_ARGS__), __LINE__)
|
||||
std::string err;
|
||||
|
||||
// Phase 1: add / select / move / resize / delete
|
||||
@@ -4150,6 +4351,111 @@ static gboolean selftest_step(gpointer) {
|
||||
check(placed["Plot"]->model.props["title"] == "Sine");
|
||||
check(set_prop(placed["Plot"], "line-color", "#0000ff", err));
|
||||
check(placed["Plot"]->model.props["line-color"] == "rgb(0,0,255)");
|
||||
// Editable items / adjustment / text props (live preview).
|
||||
{
|
||||
Item* cb = add_widget("Combobox");
|
||||
check(cb != nullptr);
|
||||
check(find_prop("Combobox", "items") != nullptr);
|
||||
check(set_prop(cb, "items", "Red|Green|Blue", err));
|
||||
{
|
||||
GtkTreeModel* m = gtk_combo_box_get_model(
|
||||
GTK_COMBO_BOX(cb->target));
|
||||
check(gtk_tree_model_iter_n_children(m, nullptr) == 3);
|
||||
GtkTreeIter it0;
|
||||
check(gtk_tree_model_get_iter_first(m, &it0));
|
||||
gchar* s0 = nullptr;
|
||||
gtk_tree_model_get(m, &it0, 0, &s0, -1);
|
||||
check(s0 && std::strcmp(s0, "Red") == 0);
|
||||
g_free(s0);
|
||||
}
|
||||
check(set_prop(cb, "active", "2", err));
|
||||
check(gtk_combo_box_get_active(GTK_COMBO_BOX(cb->target)) == 2);
|
||||
Item* lb = add_widget("Listbox");
|
||||
check(lb != nullptr);
|
||||
check(find_prop("Listbox", "items") != nullptr);
|
||||
check(set_prop(lb, "items", "Alpha|Beta|Gamma", err));
|
||||
{
|
||||
GList* rows = gtk_container_get_children(
|
||||
GTK_CONTAINER(lb->target));
|
||||
check(g_list_length(rows) == 3);
|
||||
if (rows) {
|
||||
GtkWidget* row = GTK_WIDGET(rows->data);
|
||||
GtkWidget* kid = gtk_bin_get_child(GTK_BIN(row));
|
||||
check(kid && GTK_IS_LABEL(kid) &&
|
||||
std::strcmp(gtk_label_get_text(GTK_LABEL(kid)),
|
||||
"Alpha") == 0);
|
||||
}
|
||||
g_list_free(rows);
|
||||
}
|
||||
Item* sb = add_widget("Scrollbar");
|
||||
check(sb != nullptr);
|
||||
check(find_prop("Scrollbar", "upper") != nullptr);
|
||||
check(find_prop("Scrollbar", "step-increment") != nullptr);
|
||||
check(set_prop(sb, "upper", "200", err));
|
||||
check(set_prop(sb, "step-increment", "5", err));
|
||||
{
|
||||
GtkAdjustment* adj = gtk_range_get_adjustment(
|
||||
GTK_RANGE(sb->target));
|
||||
check(gtk_adjustment_get_upper(adj) == 200.0);
|
||||
check(gtk_adjustment_get_step_increment(adj) == 5.0);
|
||||
check(gtk_adjustment_get_lower(adj) == 0.0);
|
||||
check(gtk_adjustment_get_page_increment(adj) == 10.0);
|
||||
}
|
||||
Item* tx = add_widget("Text");
|
||||
check(tx != nullptr);
|
||||
check(find_prop("Text", "text") != nullptr);
|
||||
check(set_prop(tx, "text", "Hello from the designer", err));
|
||||
{
|
||||
GtkTextBuffer* b = gtk_text_view_get_buffer(
|
||||
GTK_TEXT_VIEW(tx->target));
|
||||
GtkTextIter s, e;
|
||||
gtk_text_buffer_get_bounds(b, &s, &e);
|
||||
gchar* txt = gtk_text_buffer_get_text(b, &s, &e, FALSE);
|
||||
check(txt && std::strcmp(txt, "Hello from the designer") == 0);
|
||||
g_free(txt);
|
||||
}
|
||||
// Empty text falls back to the widget name (Text + Memo).
|
||||
check(set_prop(tx, "text", "", err));
|
||||
{
|
||||
GtkTextBuffer* b = gtk_text_view_get_buffer(
|
||||
GTK_TEXT_VIEW(tx->target));
|
||||
GtkTextIter s, e;
|
||||
gtk_text_buffer_get_bounds(b, &s, &e);
|
||||
gchar* txt = gtk_text_buffer_get_text(b, &s, &e, FALSE);
|
||||
check(txt && std::strcmp(txt, tx->model.name.c_str()) == 0);
|
||||
g_free(txt);
|
||||
}
|
||||
check(set_prop(placed["Memo"], "text", "", err));
|
||||
{
|
||||
GtkTextBuffer* b = gtk_text_view_get_buffer(
|
||||
GTK_TEXT_VIEW(placed["Memo"]->target));
|
||||
GtkTextIter s, e;
|
||||
gtk_text_buffer_get_bounds(b, &s, &e);
|
||||
gchar* txt = gtk_text_buffer_get_text(b, &s, &e, FALSE);
|
||||
check(txt &&
|
||||
std::strcmp(txt,
|
||||
placed["Memo"]->model.name.c_str()) == 0);
|
||||
g_free(txt);
|
||||
}
|
||||
// Border: common props on a Button; settable via set_prop,
|
||||
// stored in the model, defaults 0/0 (no border).
|
||||
check(find_prop("Button", "border-width") != nullptr);
|
||||
check(find_prop("Button", "border-color") != nullptr);
|
||||
check(find_prop("Button", "border-radius") != nullptr);
|
||||
Item* bb = add_widget("Button");
|
||||
check(bb != nullptr);
|
||||
check(set_prop(bb, "border-width", "3", err));
|
||||
check(set_prop(bb, "border-color", "red", err));
|
||||
check(set_prop(bb, "border-radius", "10", err));
|
||||
check(prop_or_str(bb, "border-width", "") == "3");
|
||||
check(prop_or_str(bb, "border-color", "").find("255") !=
|
||||
std::string::npos);
|
||||
check(prop_or_str(bb, "border-radius", "") == "10");
|
||||
Item* b0 = add_widget("Button");
|
||||
check(b0 != nullptr);
|
||||
check(prop_or_str(b0, "border-width", "") == "0");
|
||||
check(prop_or_str(b0, "border-radius", "") == "0");
|
||||
}
|
||||
// Generate the whole canvas and check each new widget's code.
|
||||
Project snap7 = snapshot_project();
|
||||
GenResult gr7 = generate_project(snap7, snap7.widgets,
|
||||
@@ -4250,7 +4556,7 @@ static gboolean selftest_step(gpointer) {
|
||||
"clicked") == scratch.signals.end());
|
||||
}
|
||||
Project snap8 = snapshot_project();
|
||||
check(snap8.widgets.size() == 29);
|
||||
check(snap8.widgets.size() == 9 + 20 + 4 + 2); // +4 props +2 border
|
||||
GenResult gr8 = generate_project(snap8, snap8.widgets,
|
||||
"/tmp/lightgui_gen_test8");
|
||||
check(gr8.ok);
|
||||
@@ -4301,7 +4607,7 @@ static gboolean selftest_step(gpointer) {
|
||||
Project loaded8;
|
||||
check(load_project_file("/tmp/lightgui_test8.fdj", loaded8, err));
|
||||
load_into_canvas(loaded8);
|
||||
check(g_items.size() == 29);
|
||||
check(g_items.size() == 9 + 20 + 4 + 2); // +4 props +2 border
|
||||
for (Item* it : g_items) {
|
||||
if (it->model.name == "Button_001")
|
||||
check(std::find(it->model.signals.begin(),
|
||||
@@ -4723,6 +5029,7 @@ static gboolean selftest_step(gpointer) {
|
||||
gtk_main_quit();
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
#undef check
|
||||
|
||||
// ------------------------------------------------------- main
|
||||
|
||||
|
||||
+12
@@ -39,6 +39,9 @@ inline const std::vector<PropDef>& common_props() {
|
||||
static const std::vector<PropDef> v = {
|
||||
{"background", PropKind::Color, ""},
|
||||
{"foreground", PropKind::Color, ""},
|
||||
{"border-width", PropKind::Int, "0", 0, 100},
|
||||
{"border-color", PropKind::Color, ""},
|
||||
{"border-radius", PropKind::Int, "0", 0, 1000},
|
||||
{"tooltip-text", PropKind::Str, ""},
|
||||
};
|
||||
return v;
|
||||
@@ -81,9 +84,11 @@ inline const std::vector<PropDef>& props_for(const std::string& type) {
|
||||
kShadowValues},
|
||||
};
|
||||
static const std::vector<PropDef> combobox = {
|
||||
{"items", K::Str, "Item 1|Item 2|Item 3"},
|
||||
{"active", K::Int, "0", -1, 1000},
|
||||
};
|
||||
static const std::vector<PropDef> listbox = {
|
||||
{"items", K::Str, "Item 1|Item 2|Item 3"},
|
||||
{"selection-mode", K::Enum, "1", 0, 0, "None|Single|Browse|Multiple",
|
||||
kSelModeValues},
|
||||
};
|
||||
@@ -100,9 +105,15 @@ inline const std::vector<PropDef>& props_for(const std::string& type) {
|
||||
};
|
||||
static const std::vector<PropDef> scrollbar = {
|
||||
{"value", K::Double, "0", 0, 100},
|
||||
{"lower", K::Double, "0", -1000000, 1000000},
|
||||
{"upper", K::Double, "100", -1000000, 1000000},
|
||||
{"step-increment", K::Double, "1", 0, 1000000},
|
||||
{"page-increment", K::Double, "10", 0, 1000000},
|
||||
{"page-size", K::Double, "10", 0, 1000000},
|
||||
{"inverted", K::Bool, "false"},
|
||||
};
|
||||
static const std::vector<PropDef> text = {
|
||||
{"text", K::Str, ""},
|
||||
{"editable", K::Bool, "true"},
|
||||
{"cursor-visible", K::Bool, "true"},
|
||||
{"wrap-mode", K::Enum, "0", 0, 0, "None|Char|Word|Word-char",
|
||||
@@ -111,6 +122,7 @@ inline const std::vector<PropDef>& props_for(const std::string& type) {
|
||||
static const std::vector<PropDef> memo = {
|
||||
{"text", K::Str, ""},
|
||||
{"editable", K::Bool, "true"},
|
||||
{"cursor-visible", K::Bool, "true"},
|
||||
{"wrap-mode", K::Enum, "0", 0, 0, "None|Char|Word|Word-char",
|
||||
kWrapValues},
|
||||
};
|
||||
|
||||
+24
-5
@@ -6,6 +6,7 @@
|
||||
#include <cstdio>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -41,6 +42,15 @@ static const Spec kSpecs[] = {
|
||||
{"TreeView", "items", "Root/Leaf;Other", "gtk_tree_store_new"},
|
||||
{"StringGrid", "rows", "3", "gtk_list_store_new"},
|
||||
{"Plot", "title", "Sine", "sin("},
|
||||
{"Combobox", "items", "Red|Green|Blue", "Red"},
|
||||
{"Combobox", "active", "1", "Item 1"},
|
||||
{"Listbox", "items", "Alpha|Beta|Gamma", "Alpha"},
|
||||
{"Listbox", "selection-mode", "1", "Item 1"},
|
||||
{"Scrollbar", "upper", "200", "200.0"},
|
||||
{"Scrollbar", "step-increment", "5", "5.0"},
|
||||
{"Text", "text", "Hello from the designer", "Hello from the designer"},
|
||||
{"Memo", "text", "Memo content here", "Memo content here"},
|
||||
{"Button", "border-radius", "10", "border-radius: 10px"},
|
||||
};
|
||||
|
||||
static std::string read_file(const std::string& path) {
|
||||
@@ -55,10 +65,17 @@ int main(int argc, char** argv) {
|
||||
Project p;
|
||||
p.title = "Widget Test";
|
||||
p.class_name = "MyWindow";
|
||||
// Unique name per spec, so two specs for one type (e.g. a
|
||||
// custom-items ComboBox and a default-items ComboBox) do not collide.
|
||||
std::map<std::string, int> seen;
|
||||
std::vector<std::string> spec_names;
|
||||
int i = 0;
|
||||
for (const Spec& s : kSpecs) {
|
||||
Widget w;
|
||||
w.name = std::string(s.type) + "_001";
|
||||
char buf[16];
|
||||
std::snprintf(buf, sizeof buf, "_%03d", ++seen[s.type]);
|
||||
w.name = std::string(s.type) + buf;
|
||||
spec_names.push_back(w.name);
|
||||
w.type = s.type;
|
||||
w.x = 10 + (i % 4) * 210;
|
||||
w.y = 10 + (i / 4) * 150;
|
||||
@@ -80,8 +97,9 @@ int main(int argc, char** argv) {
|
||||
return 1;
|
||||
}
|
||||
int pass = 0, fail = 0;
|
||||
for (const Spec& s : kSpecs) {
|
||||
std::string name = std::string(s.type) + "_001";
|
||||
for (size_t k = 0; k < sizeof(kSpecs) / sizeof(kSpecs[0]); ++k) {
|
||||
const Spec& s = kSpecs[k];
|
||||
std::string name = spec_names[k];
|
||||
std::string code = read_file(out + "/" + name + ".cpp");
|
||||
bool ok = !code.empty() &&
|
||||
code.find(s.marker) != std::string::npos;
|
||||
@@ -90,8 +108,9 @@ int main(int argc, char** argv) {
|
||||
if (ok) ++pass; else ++fail;
|
||||
}
|
||||
// Every widget class file must exist, plus window + main + Makefile.
|
||||
for (const Spec& s : kSpecs) {
|
||||
std::string h = out + "/" + std::string(s.type) + "_001.h";
|
||||
for (size_t k = 0; k < sizeof(kSpecs) / sizeof(kSpecs[0]); ++k) {
|
||||
const Spec& s = kSpecs[k];
|
||||
std::string h = out + "/" + spec_names[k] + ".h";
|
||||
if (!std::filesystem::exists(h)) {
|
||||
std::printf("FAIL %-16s missing %s\n", s.type, h.c_str());
|
||||
++fail;
|
||||
|
||||
Reference in new issue
Block a user