From abb6556fd4e6961b285a245c67c9a3d9119ee615 Mon Sep 17 00:00:00 2001 From: Sven Balzer <4653051+Kyuusokuna@users.noreply.github.com> Date: Mon, 23 Mar 2026 12:42:48 +0100 Subject: [PATCH] implement open_file_dialog, save_file_dialog and set_window_title for linux --- src/main_linux.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++- src/work-calendar.cpp | 7 ++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/src/main_linux.cpp b/src/main_linux.cpp index b470643..defb50f 100644 --- a/src/main_linux.cpp +++ b/src/main_linux.cpp @@ -56,6 +56,8 @@ static ImGui_ImplVulkanH_Window g_MainWindowData; static uint32_t g_MinImageCount = 2; static bool g_SwapChainRebuild = false; +static GLFWwindow *window; + static void glfw_error_callback(int error, const char* description) { fprintf(stderr, "GLFW Error %d: %s\n", error, description); @@ -361,7 +363,7 @@ int main(int, char**) // Create window with Vulkan context glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - GLFWwindow* window = glfwCreateWindow(1280, 720, "Work Calendar", nullptr, nullptr); + window = glfwCreateWindow(1600, 900, "Work Calendar", nullptr, nullptr); if (!glfwVulkanSupported()) { printf("GLFW: Vulkan Not Supported\n"); @@ -527,3 +529,73 @@ int main(int, char**) return 0; } + +static int run_command_and_return_exit_code(const char *command) { + FILE *process = popen(command, "re"); + if (!process) return -1; + + return pclose(process); +} + +static char *run_command_and_return_stdout(const char *command, size_t buffer_size) { + FILE *process = popen(command, "re"); + if (!process) return NULL; + + char *result = (char *)calloc(1, buffer_size); + if (!result) return NULL; + + for (char *ptr = result; (result + buffer_size - ptr) > 1 && fgets(ptr, result + buffer_size - ptr, process); ptr = ptr + strlen(ptr)); + + if (!feof(process)) { + free(result); + pclose(process); + return NULL; + } + + if (pclose(process)) { + free(result); + return NULL; + } + + return result; +} + +char *open_file_dialog() { + char *path = NULL; + + if (run_command_and_return_exit_code("zenity --version") == 0) { + path = run_command_and_return_stdout("zenity --title=\"Open Calendar\" --modal --file-selection --file-filter=\"Workcalendar (*.wcl) | *.wcl\"", PATH_MAX); + } else if (run_command_and_return_exit_code("kdialog --version") == 0) { + path = run_command_and_return_stdout("kdialog --title \"Open Calendar\" --getopenfilename . \"Workcalendar(*.wcl)\"", PATH_MAX); + } + + if (!path) return NULL; + + size_t len = strlen(path); + if (len && path[len-1] == '\n') + path[len-1] = '\0'; + + return path; +} + +char *save_file_dialog() { + char *path = NULL; + + if (run_command_and_return_exit_code("zenity --version") == 0) { + path = run_command_and_return_stdout("zenity --title=\"Save Calendar\" --modal --file-selection --save --file-filter=\"Workcalendar (*.wcl) | *.wcl\"", PATH_MAX); + } else if (run_command_and_return_exit_code("kdialog --version") == 0) { + path = run_command_and_return_stdout("kdialog --title \"Save Calendar\" --getsavefilename . \"Workcalendar(*.wcl)\"", PATH_MAX); + } + + if (!path) return NULL; + + size_t len = strlen(path); + if (len && path[len-1] == '\n') + path[len-1] = '\0'; + + return path; +} + +void set_window_title(const char *title) { + glfwSetWindowTitle(window, title); +} diff --git a/src/work-calendar.cpp b/src/work-calendar.cpp index 7858885..9b2c8c7 100644 --- a/src/work-calendar.cpp +++ b/src/work-calendar.cpp @@ -215,6 +215,13 @@ void per_frame(){ if(save_file_path) free(save_file_path); save_file_path = new_save_file_path; save(); + + char *title = (char *)calloc(1, strlen("Work Calendar - ") + strlen(save_file_path) + 1); + memcpy(title, "Work Calendar - ", strlen("Work Calendar - ")); + memcpy(title + strlen("Work Calendar - "), save_file_path, strlen(save_file_path)); + + set_window_title(title); + free(title); } } -- 2.34.1