feat: add common dialogs, i.e. open/save dialog

This commit is contained in:
2026-04-01 23:45:28 +02:00
parent b140825121
commit 19090d3ea5
15 changed files with 2164 additions and 79 deletions
+19 -3
View File
@@ -5,6 +5,7 @@
*/
#include "wordprocessor.hpp"
#include <gui/dialogs.hpp>
static inline int wp_min_int(int a, int b) {
return a < b ? a : b;
@@ -1203,14 +1204,29 @@ void wp_set_filepath(WordProcessorState* wp, const char* path) {
montauk::strncpy(wp->filename, path, 63);
}
void wp_open_save_pathbar(WordProcessorState* wp) {
void wp_start_pathbar(WordProcessorState* wp, bool save_mode, const char* initial_text) {
if (!initial_text) initial_text = "";
wp->show_pathbar = true;
wp->pathbar_save_mode = true;
montauk::strncpy(wp->pathbar_text, wp->filepath, 255);
wp->pathbar_save_mode = save_mode;
montauk::strncpy(wp->pathbar_text, initial_text, 255);
wp->pathbar_text[255] = '\0';
wp->pathbar_len = montauk::slen(wp->pathbar_text);
wp->pathbar_cursor = wp->pathbar_len;
}
void wp_open_save_pathbar(WordProcessorState* wp) {
char path[256] = {};
const char* initial_path = wp->filepath[0] ? wp->filepath : "";
const char* suggested_name = wp->filename[0] ? wp->filename : "document.mwp";
char msg[160] = {};
if (dialogs::save_file("Save Document", initial_path, suggested_name, path, sizeof(path), msg, sizeof(msg))) {
wp_set_filepath(wp, path);
wp_save_file(wp);
} else if (msg[0]) {
wp_start_pathbar(wp, true, initial_path[0] ? initial_path : suggested_name);
}
}
static int wp_serialized_size(WordProcessorState* wp) {
int size = 4 + 2 + 1 + 1 + 1 + 2 + 2;
for (int i = 0; i < wp->run_count; i++)
+7 -6
View File
@@ -5,14 +5,15 @@
*/
#include "wordprocessor.hpp"
#include <gui/dialogs.hpp>
static void wp_open_pathbar_for_open(WordProcessorState* wp) {
wp->show_pathbar = !wp->show_pathbar;
wp->pathbar_save_mode = false;
if (wp->show_pathbar) {
montauk::strncpy(wp->pathbar_text, wp->filepath, 255);
wp->pathbar_len = montauk::slen(wp->pathbar_text);
wp->pathbar_cursor = wp->pathbar_len;
char path[256] = {};
char msg[160] = {};
if (dialogs::open_file("Open Document", wp->filepath, path, sizeof(path), msg, sizeof(msg))) {
wp_load_file(wp, path);
} else if (msg[0]) {
wp_start_pathbar(wp, false, wp->filepath);
}
}
@@ -346,6 +346,7 @@ int wp_wrap_line_start(WordProcessorState* wp, int line_idx);
void wp_ensure_cursor_visible(WordProcessorState* wp, int view_h);
void wp_set_filepath(WordProcessorState* wp, const char* path);
void wp_start_pathbar(WordProcessorState* wp, bool save_mode, const char* initial_text);
void wp_open_save_pathbar(WordProcessorState* wp);
void wp_save_file(WordProcessorState* wp);
void wp_load_file(WordProcessorState* wp, const char* path);