From 3bc39143316d4c1a8330f84a2d95d1db2e48ebab Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Sun, 3 Aug 2025 21:07:29 +0200 Subject: [PATCH] implement insert-image button --- application/editor.h | 1 + application/gtk-text.c | 8 ++++++++ application/note.c | 3 +++ application/note.h | 1 + application/window.c | 16 +++++++++++++++- application/window.h | 1 + 6 files changed, 29 insertions(+), 1 deletion(-) diff --git a/application/editor.h b/application/editor.h index e973d5d..c2aba27 100644 --- a/application/editor.h +++ b/application/editor.h @@ -135,6 +135,7 @@ void editor_init_textview(UiObject *obj, UIWIDGET textview); void editor_init_textbuf(UiText *text); void editor_apply_styles(Note *note, UIWIDGET textview, UiText *text, CxList /* MDDocStyleSection */ *styles); cxmutstr editor_get_markdown(UiText *text, const CxAllocator *a); +void editor_insert_image_file(UiText *text, const char *filepath); UiBool editor_set_style(UiText *text, const char *style, UiBool enabled); void editor_set_paragraph_style(UiText *text, const char *style); diff --git a/application/gtk-text.c b/application/gtk-text.c index 0ef8ab7..2f9ad3f 100644 --- a/application/gtk-text.c +++ b/application/gtk-text.c @@ -1298,3 +1298,11 @@ void editor_insert_list(UiText *text, UiBool ordered) { const char *style = ordered ? EDITOR_STYLE_ORDLIST0 : EDITOR_STYLE_LIST0; editor_insert_list_element(editor, &iter, style, 0); } + +void editor_insert_image_file(UiText *text, const char *filepath) { + GtkTextBuffer *buffer = text->data1; + NoteEditor *editor = g_object_get_data(text->obj, "editor"); + + GFile *file = g_file_new_for_path(filepath); + editor_dnd_add_file(editor, file); +} diff --git a/application/note.c b/application/note.c index 86cdea8..de5214f 100644 --- a/application/note.c +++ b/application/note.c @@ -253,6 +253,9 @@ void note_insert_list(NoteModel *note, UiBool ordered) { editor_insert_list(note->text, ordered); } +void note_insert_image(NoteModel *note, const char *filepath) { + editor_insert_image_file(note->text, filepath); +} void note_update_title(NotebookModel *notebook, Note *note) { diff --git a/application/note.h b/application/note.h index 190d1d5..9486647 100644 --- a/application/note.h +++ b/application/note.h @@ -61,6 +61,7 @@ void note_text_style_set_emphasis(NoteModel *note, UiBool enabled); void note_text_style_set_underline(NoteModel *note, UiBool enabled); void note_text_style_set_code(NoteModel *note, UiBool enabled); void note_insert_list(NoteModel *note, UiBool ordered); +void note_insert_image(NoteModel *note, const char *filepath); void note_update_title(NotebookModel *notebook, Note *note); diff --git a/application/window.c b/application/window.c index 6388993..6bf6754 100644 --- a/application/window.c +++ b/application/window.c @@ -95,7 +95,7 @@ void window_create() { ui_togglebutton(obj, .label = "code", .varname = "note_textnote_code", .onchange = action_textnote_style_code); ui_button(obj, .icon = "view-list-bullet", .onclick = action_textnote_insertlist, .onclickdata = ACTION_DATA_INSERT_LIST_UNORDERED); ui_button(obj, .icon = "view-list-ordered", .onclick = action_textnote_insertlist, .onclickdata = ACTION_DATA_INSERT_LIST_ORDERED); - ui_button(obj, .icon = "insert-image"); + ui_button(obj, .icon = "insert-image", .onclick = action_textnote_insertimg); ui_button(obj, .icon = "insert-link"); } ui_hbox_w(obj, wdata->attachments, .margin = 10) { @@ -519,3 +519,17 @@ void action_textnote_insertlist(UiEvent *event, void *userdata) { note_insert_list(notebook->current_note->model, (UiBool)ordered); } } + +static void insertimg_file(UiEvent *event, void *userdata) { + MainWindow *window = event->window; + NotebookModel *notebook = window->current_notebook; + UiFileList *flist = event->eventdata; + if(flist->nfiles == 1 && notebook && notebook->current_note && notebook->current_note->model) { + const char *file = flist->files[0]; + note_insert_image(notebook->current_note->model, file); + } +} + +void action_textnote_insertimg(UiEvent *event, void *userdata) { + ui_openfiledialog(event->obj, UI_FILEDIALOG_SELECT_SINGLE, insertimg_file, NULL); +} diff --git a/application/window.h b/application/window.h index 63fc9bb..b0ea783 100644 --- a/application/window.h +++ b/application/window.h @@ -80,6 +80,7 @@ void action_textnote_style_emphasis(UiEvent *event, void *userdata); void action_textnote_style_underline(UiEvent *event, void *userdata); void action_textnote_style_code(UiEvent *event, void *userdata); void action_textnote_insertlist(UiEvent *event, void *userdata); +void action_textnote_insertimg(UiEvent *event, void *userdata); -- 2.47.3