diff --git a/Makefile b/Makefile index 826c19a..bd54f69 100644 --- a/Makefile +++ b/Makefile @@ -26,20 +26,31 @@ MAKEFLAGS += -j$(NPROC) # make build the lightgui binary and all widget demos # make lightgui build ./lightgui only # make widgets build widgets/build (all widget demos + palette + layout) -# make install install the XFrame widget headers (header-only) to -# $(PREFIX)/include/lightgui so generated apps build -# on any machine where LightGUI is installed +# make install install LightGUI system-wide (needs root): the +# XFrame widget headers to $(PREFIX)/include/lightgui +# so generated apps build, the designer binary to +# $(PREFIX)/bin, the UI + palette icons to +# $(PREFIX)/share/lightgui, and a desktop entry + +# icon so the app menu lists it. The widgets are +# header-only, so there are no shared libraries and +# no ldconfig step. Run from the designer via +# Tools -> Install Libraries…, which prompts for the +# root password (pkexec, sudo fallback). # make test run the layout -> LightGUI seam test (no display needed) # make clean remove built artefacts # ./ide start the designer (single process) .PHONY: all lightgui widgets install test clean -# Install location for the XFrame widget headers. The widgets are -# header-only (common/ + core/ + components/), so `make install` -# just copies the headers; generated apps then build with -# -I$(PREFIX)/include/lightgui (see src/generator.cpp). +# Install locations. The widgets are header-only (common/ + core/ + +# components/), so `make install` just copies the headers; generated +# apps then build with -I$(PREFIX)/include/lightgui (see +# src/generator.cpp). The designer binary, its Glade UI and the +# palette .ico files go alongside so an installed `lightgui` runs +# anywhere without the checkout. PREFIX ?= /usr/local INCLUDEDIR ?= $(PREFIX)/include/lightgui +BINDIR ?= $(PREFIX)/bin +SHAREDIR ?= $(PREFIX)/share/lightgui SRCS := $(wildcard src/*.cpp) OBJS := $(SRCS:.cpp=.o) @@ -72,13 +83,31 @@ widgets: # still build on all cores. MAKEFLAGS= cmake --build widgets/build -j$(NPROC) -install: +install: lightgui @echo "Installing LightGUI widget headers to $(INCLUDEDIR)" @mkdir -p $(INCLUDEDIR) @cd widgets && find common core components -name '*.h' | while read f; do \ install -D -m 644 "$$f" "$(INCLUDEDIR)/$$f"; \ done @echo "Installed $$(find widgets/common widgets/core widgets/components -name '*.h' | wc -l) headers to $(INCLUDEDIR)" + @echo "Installing designer binary to $(BINDIR)" + @mkdir -p $(BINDIR) + @install -m 755 lightgui $(BINDIR)/lightgui + @echo "Installing designer UI + palette icons to $(SHAREDIR)" + @mkdir -p $(SHAREDIR) + @install -D -m 644 ui/designer.ui $(SHAREDIR)/designer.ui + @mkdir -p $(SHAREDIR)/icons + @cd widgets/icons && find . -name '*.ico' | while read f; do \ + install -D -m 644 "$$f" "$(SHAREDIR)/icons/$$f"; \ + done + @echo "Installed $$(find widgets/icons -name '*.ico' | wc -l) palette icons to $(SHAREDIR)/icons" + @echo "Installing desktop entry + icon" + @mkdir -p $(PREFIX)/share/applications $(PREFIX)/share/icons/hicolor/512x512/apps + @install -D -m 644 lightgui.desktop $(PREFIX)/share/applications/lightgui.desktop + @install -D -m 644 flatpak/icon.png $(PREFIX)/share/icons/hicolor/512x512/apps/lightgui.png + @-update-desktop-database $(PREFIX)/share/applications 2>/dev/null; true + @-gtk-update-icon-cache -q $(PREFIX)/share/icons/hicolor 2>/dev/null; true + @echo "Installed LightGUI: binary $(BINDIR)/lightgui, headers $(INCLUDEDIR), data $(SHAREDIR)" test: python3 widgets/tests/test_layout_to_lightgui.py diff --git a/lightgui.desktop b/lightgui.desktop new file mode 100644 index 0000000..eafe665 --- /dev/null +++ b/lightgui.desktop @@ -0,0 +1,10 @@ +[Desktop Entry] +Type=Application +Name=LightGUI +GenericName=GUI Designer +Comment=Visual GUI designer for GTK 3 in C++ +Exec=lightgui +Icon=lightgui +Terminal=false +Categories=Development;IDE;GUIDesigner; +Keywords=GTK;GUI;designer;widgets;IDE; diff --git a/src/main.cpp b/src/main.cpp index 28a786f..b0988de 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,6 +20,11 @@ #include #include #include +#include +#include +#include +#include +#include #include #include #include @@ -3269,6 +3274,7 @@ static std::string xframe_icons_dir() { if (env && *env) return env; for (const std::string& cand : {std::string("widgets/icons"), std::string("../widgets/icons"), + std::string("/usr/local/share/lightgui/icons"), std::string("/opt/lightgui/widgets/icons")}) { if (g_file_test((cand + "/GtkButton.ico").c_str(), G_FILE_TEST_IS_REGULAR)) @@ -4191,26 +4197,21 @@ static void on_generate(GtkMenuItem*, gpointer) { // plain g++ / make command. Settings (generator, build type, build // directory) live in ~/.config/lightgui/cmake.conf. -static void show_build_result(const std::string& title, - const CMakeBuildResult& r) { - status(r.ok ? ("CMake build OK — " + r.exe_path) - : "CMake build FAILED — see log"); +// Generic scrollable log dialog: the log area is packed expanding so it +// fills the dialog (see the CMake console fix), long lines wrap, and the +// view starts scrolled to the end of the log. Used by the CMake build +// result and by Install Libraries. +static void show_log_dialog(const std::string& title, const std::string& head, + const std::string& log) { GtkWidget* dlg = gtk_dialog_new_with_buttons( title.c_str(), GTK_WINDOW(g_window), GTK_DIALOG_MODAL, "_Close", GTK_RESPONSE_CLOSE, nullptr); gtk_window_set_default_size(GTK_WINDOW(dlg), 700, 420); - std::string head = - r.ok ? std::string("Build succeeded.\nExecutable: ") + r.exe_path + - "\n\n" - : std::string("Build FAILED.\n\n"); - // Show the full log: the text view scrolls, so a long build log is - // fully visible instead of only its tail. - std::string log = r.output; GtkWidget* view = gtk_text_view_new(); gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE); gtk_text_view_set_monospace(GTK_TEXT_VIEW(view), TRUE); - // Wrap long lines (cmake command lines) so the whole log is readable - // without horizontal scrolling. + // Wrap long lines so the whole log is readable without horizontal + // scrolling. gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_CHAR); GtkTextBuffer* buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view)); gtk_text_buffer_set_text(buf, (head + log).c_str(), -1); @@ -4242,6 +4243,19 @@ static void show_build_result(const std::string& title, gtk_widget_destroy(dlg); } +static void show_build_result(const std::string& title, + const CMakeBuildResult& r) { + status(r.ok ? ("CMake build OK — " + r.exe_path) + : "CMake build FAILED — see log"); + std::string head = + r.ok ? std::string("Build succeeded.\nExecutable: ") + r.exe_path + + "\n\n" + : std::string("Build FAILED.\n\n"); + // Show the full log: the text view scrolls, so a long build log is + // fully visible instead of only its tail. + show_log_dialog(title, head, r.output); +} + // Configure + build `dir` with the saved CMake settings and report the // result. Returns false when `dir` holds no CMakeLists.txt. static bool cmake_build_dir(const std::string& dir) { @@ -4352,6 +4366,242 @@ static void on_cmake_settings(GtkMenuItem*, gpointer) { gtk_widget_destroy(dlg); } +// ------------------------------------------------------- Install Libraries +// Tools -> Install Libraries… : make LightGUI globally available by +// running `make install` in the checkout as root. The install target +// (Makefile) copies the widget headers to /usr/local/include/lightgui +// (so generated apps build), the designer binary to /usr/local/bin, +// the UI + palette icons to /usr/local/share/lightgui, and a desktop +// entry + icon so the app menu lists it. There are no shared libraries +// to install (the widgets are header-only), so no ldconfig step is +// needed. +// +// The install runs as root via pkexec when available (graphical root +// password prompt) or, when pkexec is missing, via sudo -S with the +// password typed into a dialog (the password is piped to sudo's stdin +// and wiped from memory immediately after; it is never written to disk +// or passed on a command line). The build step runs first as the user +// so no root-owned build artefacts land in the checkout. + +// Run `argv` (NULL-terminated, no shell) and capture combined +// stdout+stderr into `out`; `stdin_text` ("" = none) is written to the +// child's stdin first. Returns the child's exit code, or -1 on error. +static int run_argv_capture(char* const argv[], const std::string& stdin_text, + std::string& out) { + int in_pipe[2], out_pipe[2]; + if (pipe(in_pipe) != 0 || pipe(out_pipe) != 0) return -1; + pid_t pid = fork(); + if (pid < 0) return -1; + if (pid == 0) { + dup2(in_pipe[0], STDIN_FILENO); + dup2(out_pipe[1], STDOUT_FILENO); + dup2(out_pipe[1], STDERR_FILENO); + close(in_pipe[0]); + close(in_pipe[1]); + close(out_pipe[0]); + close(out_pipe[1]); + execvp(argv[0], argv); + _exit(127); + } + close(in_pipe[0]); + close(out_pipe[1]); + if (!stdin_text.empty()) { + // The child may exit without reading stdin (sudo with cached + // credentials): ignore SIGPIPE so a closed pipe ends the write + // instead of killing the designer. + void (*old_handler)(int) = signal(SIGPIPE, SIG_IGN); + const char* p = stdin_text.c_str(); + size_t left = stdin_text.size(); + while (left > 0) { + ssize_t n = write(in_pipe[1], p, left); + if (n <= 0) break; + p += n; + left -= (size_t)n; + } + signal(SIGPIPE, old_handler); + } + close(in_pipe[1]); + char buf[4096]; + ssize_t n; + while ((n = read(out_pipe[0], buf, sizeof(buf))) > 0) + out.append(buf, (size_t)n); + close(out_pipe[0]); + int st = 0; + while (waitpid(pid, &st, 0) < 0 && errno == EINTR) { + } + if (WIFEXITED(st)) return WEXITSTATUS(st); + return -1; +} + +// Absolute path of `make` (pkexec needs an absolute program path). +static std::string find_make() { + gchar* m = g_find_program_in_path("make"); + std::string r = m ? m : "/usr/bin/make"; + g_free(m); + return r; +} + +// The LightGUI checkout the designer is running from: the directory +// holding the running binary when it looks like a checkout, else the +// current directory when it does. "" when neither does (the designer +// was started from an installed binary with no checkout around). +static std::string find_checkout_dir() { + auto looks_like_checkout = [](const std::string& d) { + return g_file_test((d + "/Makefile").c_str(), + G_FILE_TEST_IS_REGULAR) && + g_file_test((d + "/src/main.cpp").c_str(), + G_FILE_TEST_IS_REGULAR); + }; + char exe[4096]; + ssize_t n = readlink("/proc/self/exe", exe, sizeof(exe) - 1); + if (n > 0) { + exe[n] = '\0'; + std::string dir = exe; + size_t slash = dir.find_last_of('/'); + if (slash != std::string::npos) { + dir = dir.substr(0, slash); + if (looks_like_checkout(dir)) return dir; + } + } + char cwd[4096]; + if (getcwd(cwd, sizeof(cwd)) && looks_like_checkout(cwd)) return cwd; + return ""; +} + +// Ask for the sudo password (used only when pkexec is missing). +// Returns false when the user cancels. +static bool ask_sudo_password(std::string& pw_out) { + GtkWidget* dlg = gtk_dialog_new_with_buttons( + "Install Libraries — sudo password", GTK_WINDOW(g_window), + GTK_DIALOG_MODAL, "_Cancel", GTK_RESPONSE_CANCEL, "_Install", + GTK_RESPONSE_ACCEPT, nullptr); + // Enter in the password entry must submit (activates_default needs + // a default response set on the dialog). + gtk_dialog_set_default_response(GTK_DIALOG(dlg), GTK_RESPONSE_ACCEPT); + GtkWidget* label = gtk_label_new( + "pkexec is not installed, so the install will run with sudo.\n" + "Enter your login password (the one sudo asks for):"); + gtk_label_set_selectable(GTK_LABEL(label), TRUE); + GtkWidget* entry = gtk_entry_new(); + gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE); + gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE); + GtkWidget* box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8); + gtk_container_set_border_width(GTK_CONTAINER(box), 12); + gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0); + gtk_box_pack_start(GTK_BOX(box), entry, FALSE, FALSE, 0); + gtk_container_add( + GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(dlg))), box); + gtk_widget_show_all(dlg); + bool ok = gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_ACCEPT; + if (ok) pw_out = gtk_entry_get_text(GTK_ENTRY(entry)); + gtk_widget_destroy(dlg); + return ok; +} + +static void on_install_libraries(GtkMenuItem*, gpointer) { + std::string repo = find_checkout_dir(); + if (repo.empty()) { + status("Install Libraries needs a LightGUI checkout"); + GtkWidget* msg = gtk_message_dialog_new( + GTK_WINDOW(g_window), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, + GTK_BUTTONS_OK, + "Install Libraries needs a LightGUI checkout.\n\n" + "Run the designer from the checkout (./ide) and try again."); + gtk_dialog_run(GTK_DIALOG(msg)); + gtk_widget_destroy(msg); + return; + } + GtkWidget* confirm = gtk_dialog_new_with_buttons( + "Install Libraries", GTK_WINDOW(g_window), GTK_DIALOG_MODAL, + "_Cancel", GTK_RESPONSE_CANCEL, "_Install", GTK_RESPONSE_ACCEPT, + nullptr); + gtk_dialog_set_default_response(GTK_DIALOG(confirm), + GTK_RESPONSE_ACCEPT); + GtkWidget* label = gtk_label_new( + ("Install LightGUI system-wide from:\n" + repo + "\n\n" + "• Widget headers → /usr/local/include/lightgui\n" + " (what generated apps include and link against)\n" + "• Designer binary → /usr/local/bin/lightgui\n" + "• UI + palette icons → /usr/local/share/lightgui\n" + "• Desktop entry + icon → the app menu\n\n" + "The widgets are header-only, so there are no shared\n" + "libraries and no ldconfig step.\n\n" + "This runs as root: a password prompt will appear.").c_str()); + gtk_label_set_selectable(GTK_LABEL(label), TRUE); + gtk_container_add( + GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(confirm))), + label); + gtk_widget_show_all(confirm); + bool go = gtk_dialog_run(GTK_DIALOG(confirm)) == GTK_RESPONSE_ACCEPT; + gtk_widget_destroy(confirm); + if (!go) return; + + std::string make = find_make(); + std::string out; + // Step 1: make sure the binary is freshly built, as the user, so + // the privileged install never rebuilds (and never leaves + // root-owned build artefacts in the checkout). + status("Install Libraries: building designer…"); + { + char* const argv[] = {const_cast(make.c_str()), + const_cast("-C"), + const_cast(repo.c_str()), + const_cast("lightgui"), nullptr}; + int rc = run_argv_capture(argv, "", out); + if (rc != 0) { + show_log_dialog("Install Libraries", + "Install FAILED — the designer did not build.\n\n" + "Rebuild it first (./ide --build) and try again.\n\n", + out); + status("Install Libraries FAILED — build failed"); + return; + } + } + // Step 2: the install itself, as root. + status("Install Libraries: installing as root…"); + out.clear(); + gchar* pkexec = g_find_program_in_path("pkexec"); + int rc; + if (pkexec) { + char* const argv[] = {pkexec, const_cast(make.c_str()), + const_cast("-C"), + const_cast(repo.c_str()), + const_cast("install"), nullptr}; + rc = run_argv_capture(argv, "", out); + g_free(pkexec); + } else { + std::string pw; + if (!ask_sudo_password(pw)) { + status("Install Libraries cancelled"); + return; + } + char* const argv[] = {const_cast("sudo"), + const_cast("-S"), + const_cast(make.c_str()), + const_cast("-C"), + const_cast(repo.c_str()), + const_cast("install"), nullptr}; + rc = run_argv_capture(argv, pw + "\n", out); + // Wipe the password from memory as soon as sudo has it. + std::fill(pw.begin(), pw.end(), '\0'); + pw.clear(); + } + if (rc == 0) { + show_log_dialog("Install Libraries", + "Install succeeded.\n\n" + "LightGUI is now available system-wide:\n" + "• run `lightgui` from anywhere\n" + "• generated apps build against\n" + " /usr/local/include/lightgui\n" + "• LightGUI is in the app menu\n\n", + out); + status("Install Libraries: installed to /usr/local"); + } else { + show_log_dialog("Install Libraries", "Install FAILED.\n\n", out); + status("Install Libraries FAILED — see log"); + } +} + // ------------------------------------------------------- file operations static void update_title() { @@ -6322,9 +6572,14 @@ int main(int argc, char** argv) { if (!gtk_builder_add_from_file(builder, "ui/designer.ui", &error)) { g_clear_error(&error); if (!gtk_builder_add_from_file(builder, "../ui/designer.ui", &error)) { - std::fprintf(stderr, "lightgui: cannot load designer.ui: %s\n", - error ? error->message : "unknown error"); - return 1; + g_clear_error(&error); + if (!gtk_builder_add_from_file( + builder, "/usr/local/share/lightgui/designer.ui", + &error)) { + std::fprintf(stderr, "lightgui: cannot load designer.ui: %s\n", + error ? error->message : "unknown error"); + return 1; + } } } @@ -6453,6 +6708,14 @@ int main(int argc, char** argv) { g_signal_connect(mi_cs, "activate", G_CALLBACK(on_cmake_settings), nullptr); + // ---- Tools ---- + // Tools -> Install Libraries… : system-wide install (see + // on_install_libraries). + GObject* mi_il = gtk_builder_get_object(builder, "mi_install_libraries"); + if (mi_il) + g_signal_connect(mi_il, "activate", + G_CALLBACK(on_install_libraries), nullptr); + // View menu -> code view window / theme picker. GObject* mi_code = gtk_builder_get_object(builder, "mi_view_code"); if (mi_code) diff --git a/ui/designer.ui b/ui/designer.ui index edba486..f78858f 100644 --- a/ui/designer.ui +++ b/ui/designer.ui @@ -211,6 +211,23 @@ + + + Tools + True + + + True + + + Install Libraries… + True + + + + + + Help