From 439dec53e9842ded31d67c0ce1f084b7dd27775c Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Thu, 12 Feb 2026 15:54:05 +0100 Subject: [PATCH] save notes when closing a window --- application/note.c | 6 +++--- application/note.h | 3 ++- application/notebook.c | 4 ++-- application/window.c | 14 ++++++++++++++ ui/cocoa/toolkit.m | 8 ++++++++ ui/gtk/toolkit.c | 18 ++++++++++++++++++ ui/gtk/window.c | 19 +++++++++++++++++++ ui/motif/toolkit.c | 8 ++++++++ ui/qt/toolkit.cpp | 8 ++++++++ ui/ui/toolkit.h | 3 +++ ui/win32/toolkit.c | 8 ++++++++ 11 files changed, 93 insertions(+), 6 deletions(-) diff --git a/application/note.c b/application/note.c index f6c028f..19fdabd 100644 --- a/application/note.c +++ b/application/note.c @@ -184,7 +184,7 @@ void note_add_attachment(Note *note, Attachment *attachment) { cxListAdd(note->resource->attachments, attachment); } -void note_save(UiObject *obj, NotebookModel *notebook, Note *note) { +void note_save(UiObject *obj, NotebookModel *notebook, Note *note, execresult_func result, void *userdata) { NoteModel *m = note->model; char *title = ui_get(m->title); @@ -202,9 +202,9 @@ void note_save(UiObject *obj, NotebookModel *notebook, Note *note) { if(note->resource_id == 0) { // new note - note_store_new_note_async(obj, note, NULL, NULL); + note_store_new_note_async(obj, note, result, userdata); } else { - note_store_update_note_async(obj, note, NULL, NULL); + note_store_update_note_async(obj, note, result, userdata); } if(note->resource->attachments) { diff --git a/application/note.h b/application/note.h index 48645c8..11cf335 100644 --- a/application/note.h +++ b/application/note.h @@ -31,6 +31,7 @@ #include "application.h" #include "editor.h" +#include "store.h" #include @@ -53,7 +54,7 @@ void note_load_content(UiObject *obj, NotebookModel *notebook, Note *note); void note_add_attachment(Note *note, Attachment *attachment); -void note_save(UiObject *obj, NotebookModel *notebook, Note *note); +void note_save(UiObject *obj, NotebookModel *notebook, Note *note, execresult_func result, void *userdata); void note_update_current_style(NoteModel *note, MDActiveStyles *style); diff --git a/application/notebook.c b/application/notebook.c index 6cf0d8b..7946556 100644 --- a/application/notebook.c +++ b/application/notebook.c @@ -80,7 +80,7 @@ void notebookmodel_detach(NotebookModel *model) { if(model->current_note) { // TODO: model->modified doesnt work yet, remove || 1 when it works if(model->current_note->model->modified || 1) { - note_save(model->window->obj, model, model->current_note); + note_save(model->window->obj, model, model->current_note, NULL, NULL); } // TODO: workaround for a toolkit bug, that reattaching a document @@ -169,7 +169,7 @@ void notebookmodel_detach_current_note(NotebookModel *model) { Note *current_note = model->current_note; // TODO: model->modified doesnt work yet, remove || 1 when it works if(current_note->model->modified || 1) { - note_save(model->window->obj, model, model->current_note); + note_save(model->window->obj, model, model->current_note, NULL, NULL); } // TODO: in theory ui_detach_document2 should save the state // of all vars, but it seems this doesn't work diff --git a/application/window.c b/application/window.c index 884d8ab..4059c6e 100644 --- a/application/window.c +++ b/application/window.c @@ -39,7 +39,21 @@ #include #include +static void note_saved(UiEvent *event, int error, void *userdata) { + ui_app_unref(); +} + static void window_closed(UiEvent *event, MainWindow *wdata) { + // save current note + NotebookModel *notebook = wdata->current_notebook; + if(notebook && notebook->current_note && notebook->current_note->model) { + Note *note = notebook->current_note; + //if(note->model->modified) { + note_save(application_global_obj(), notebook, note, note_saved, NULL); + ui_app_ref(); + //} + } + note_store_remove_listener(wdata); } diff --git a/ui/cocoa/toolkit.m b/ui/cocoa/toolkit.m index ae6f4c7..e2ef998 100644 --- a/ui/cocoa/toolkit.m +++ b/ui/cocoa/toolkit.m @@ -120,6 +120,14 @@ void ui_main(void) { } } +void ui_app_ref(void) { + // TODO +} + +void ui_app_unref(void) { + // TODO +} + /* ------------------- Window Visibility functions ------------------- */ void ui_show(UiObject *obj) { diff --git a/ui/gtk/toolkit.c b/ui/gtk/toolkit.c index 9009bca..f974bae 100644 --- a/ui/gtk/toolkit.c +++ b/ui/gtk/toolkit.c @@ -135,6 +135,24 @@ void ui_main() { } } + +void ui_app_ref(void) { +#ifdef UI_APPLICATION + g_application_hold(G_APPLICATION(app)); +#else + // TODO +#endif +} + +void ui_app_unref(void) { +#ifdef UI_APPLICATION + g_application_release(G_APPLICATION(app)); +#else + // TODO +#endif +} + + #ifndef UI_GTK2 void ui_app_quit() { g_application_quit(G_APPLICATION(app)); // TODO: fix, does not work diff --git a/ui/gtk/window.c b/ui/gtk/window.c index c9d7eb4..58af741 100644 --- a/ui/gtk/window.c +++ b/ui/gtk/window.c @@ -166,6 +166,25 @@ static UiObject* create_window(const char *title, UiBool sidebar, UiBool splitvi obj->widget = gtk_window_new(GTK_WINDOW_TOPLEVEL); #endif +#if GTK_MAJOR_VERSION < 4 + const char *window_pos = ui_get_property("ui.gtk.window.position"); + GtkWindowPosition wpos = GTK_WIN_POS_CENTER; + if(window_pos) { + if(!strcmp(window_pos, "none")) { + wpos = GTK_WIN_POS_NONE; + } else if(!strcmp(window_pos, "center")) { + //wpos = GTK_WIN_POS_CENTER; + } else if(!strcmp(window_pos, "mouse")) { + wpos = GTK_WIN_POS_MOUSE; + } else if(!strcmp(window_pos, "center_always")) { + wpos = GTK_WIN_POS_CENTER_ALWAYS; + } else { + fprintf(stderr, "Error: unknown value '%s' for property ui.gtk.window.positionhint", window_pos); + } + } + gtk_window_set_position(GTK_WINDOW(obj->widget), wpos); +#endif + #if GTK_CHECK_VERSION(4, 0, 0) obj->ctx->action_map = G_ACTION_MAP(obj->widget); #endif diff --git a/ui/motif/toolkit.c b/ui/motif/toolkit.c index d70c25f..a9d605e 100644 --- a/ui/motif/toolkit.c +++ b/ui/motif/toolkit.c @@ -142,6 +142,14 @@ void ui_main() { } } +void ui_app_ref(void) { + // TODO +} + +void ui_app_unref(void) { + // TODO +} + void ui_app_quit() { XtAppSetExitFlag(app); } diff --git a/ui/qt/toolkit.cpp b/ui/qt/toolkit.cpp index bc355d0..91bc4ed 100644 --- a/ui/qt/toolkit.cpp +++ b/ui/qt/toolkit.cpp @@ -83,6 +83,14 @@ void ui_main() { } } +void ui_app_ref(void) { + // TODO +} + +void ui_app_unref(void) { + // TODO +} + void ui_show(UiObject *obj) { obj->widget->show(); } diff --git a/ui/ui/toolkit.h b/ui/ui/toolkit.h index 8833f90..b798e66 100644 --- a/ui/ui/toolkit.h +++ b/ui/ui/toolkit.h @@ -738,6 +738,9 @@ UIEXPORT void ui_list_class_set_iter(UiList *list, void *iter); UIEXPORT void ui_object_set(UiObject *obj, const char *key, void *data); UIEXPORT void* ui_object_get(UiObject *obj, const char *key); +UIEXPORT void ui_app_ref(void); +UIEXPORT void ui_app_unref(void); + #ifdef __cplusplus } #endif diff --git a/ui/win32/toolkit.c b/ui/win32/toolkit.c index 24346f0..a692aec 100644 --- a/ui/win32/toolkit.c +++ b/ui/win32/toolkit.c @@ -99,6 +99,14 @@ void ui_main() { uic_store_app_properties(); } +void ui_app_ref(void) { + // TODO +} + +void ui_app_unref(void) { + // TODO +} + void ui_show(UiObject *obj) { ui_set_visible(obj->widget, TRUE); } -- 2.47.3