From c4da7d4c790fb2b2ce30f99e3983bab957b30300 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Thu, 3 Apr 2025 21:10:46 +0200 Subject: [PATCH] implement attachment window image viewer --- application/application.h | 1 + application/attachment.c | 67 ++++++++++++++++++++++++++++++++++++--- application/attachment.h | 5 +++ 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/application/application.h b/application/application.h index 03a4fe4..345d749 100644 --- a/application/application.h +++ b/application/application.h @@ -173,6 +173,7 @@ typedef struct AttachmentWindow { Resource *resource; UiList *attachments; + int current_index; UiGeneric *image; } AttachmentWindow; diff --git a/application/attachment.c b/application/attachment.c index 501c483..dbd17ef 100644 --- a/application/attachment.c +++ b/application/attachment.c @@ -62,6 +62,11 @@ void attachment_create_ui_model(UiContext *ctx, Attachment *attachment, Resource model->parent_note = resource; } +/* + * sets the attachments image + * + * img: toolkit image object (for example for gtk this would be GdkPixbuf*) + */ void attachment_set_image(Attachment *attachment, void *img) { // TODO: replace this with new toolkit function for setting UiGeneric values if(attachment->ui->img->set) { @@ -73,7 +78,10 @@ void attachment_set_image(Attachment *attachment, void *img) { } - +/* + * itemlist create_ui function + * used by the note horizontal attachments list + */ void attachment_item(UiObject *obj, int index, void *elm, void *userdata) { Attachment *attachment = elm; @@ -95,7 +103,13 @@ void action_attachment_clicked(UiEvent *event, void *userdata) { ui_show(attachment_window); } - +/* + * creates a attachment list window + * + * resource: parent resource of the attachments + * selected_attachment (optional): if non null, this attachment will be pre- + * selected in the window + */ UiObject* attachment_window_create(Resource *resource, Attachment *selected_attachment) { cxmutstr title = cx_asprintf("%s - attachments", note_get_title(resource)); UiObject *obj = ui_simple_window(title.ptr, NULL); @@ -104,20 +118,34 @@ UiObject* attachment_window_create(Resource *resource, Attachment *selected_atta AttachmentWindow *wdata = attachment_window_create_data(obj, resource); obj->window = wdata; + // create UI ui_headerbar(obj) { ui_headerbar_start(obj) { - ui_button(obj, .icon = UI_ICON_GO_BACK); - ui_button(obj, .icon = UI_ICON_GO_FORWARD); + ui_button(obj, .icon = UI_ICON_GO_BACK, .onclick = action_attachment_prev); + ui_button(obj, .icon = UI_ICON_GO_FORWARD, .onclick = action_attachment_next); } } - ui_imageviewer(obj, .value = wdata->image, .autoscale = TRUE, .scrollarea = TRUE, .useradjustable = TRUE, .fill = UI_ON); + // pre-select selected_attachment + if(selected_attachment) { + CxList *attachments = wdata->attachments->data; + size_t index = cxListFind(attachments, selected_attachment); + if(cxListIndexValid(attachments, index)) { + wdata->current_index = (int)index; + attachment_window_set_image(wdata, selected_attachment); + } + } return obj; } +/* + * updates the attachments list in wdata from the NoteModel + * this adds all attachments from model->attachments to wdata->attachments + */ static void update_attachments(AttachmentWindow *wdata, NoteModel *model) { + ui_list_clear(wdata->attachments); void *elm = ui_list_first(model->attachments); while(elm) { ui_list_append(wdata->attachments, elm); @@ -125,6 +153,9 @@ static void update_attachments(AttachmentWindow *wdata, NoteModel *model) { } } +/* + * creates and initializes the AttachmentWindow window data object + */ AttachmentWindow* attachment_window_create_data(UiObject *obj, Resource *resource) { AttachmentWindow *wdata = ui_malloc(obj->ctx, sizeof(AttachmentWindow)); memset(wdata, 0, sizeof(AttachmentWindow)); @@ -133,8 +164,34 @@ AttachmentWindow* attachment_window_create_data(UiObject *obj, Resource *resourc wdata->resource = resource; wdata->attachments = ui_list_new(obj->ctx, NULL); wdata->image = ui_generic_new(obj->ctx, NULL); + wdata->current_index = -1; update_attachments(wdata, resource->model); return wdata; } + +void attachment_window_set_image(AttachmentWindow *wdata, Attachment *attachment) { + UiGeneric *g = attachment->ui->img; + void *img = g->get(g); + wdata->image->set(wdata->image, img, UI_IMAGE_OBJECT_TYPE); +} + +void action_attachment_prev(UiEvent *event, void *userdata) { + AttachmentWindow *wdata = event->window; + if(wdata->current_index > 0) { + wdata->current_index--; + Attachment *a = ui_list_get(wdata->attachments, wdata->current_index); + attachment_window_set_image(wdata, a); + } +} + +void action_attachment_next(UiEvent *event, void *userdata) { + AttachmentWindow *wdata = event->window; + int numattachments = ui_list_count(wdata->attachments); + if(wdata->current_index + 1 < numattachments) { + wdata->current_index++; + Attachment *a = ui_list_get(wdata->attachments, wdata->current_index); + attachment_window_set_image(wdata, a); + } +} diff --git a/application/attachment.h b/application/attachment.h index d10fb16..ea6ba61 100644 --- a/application/attachment.h +++ b/application/attachment.h @@ -55,6 +55,11 @@ UiObject* attachment_window_create(Resource *resource, Attachment *selected_atta AttachmentWindow* attachment_window_create_data(UiObject *obj, Resource *resource); +void attachment_window_set_image(AttachmentWindow *wdata, Attachment *attachment); + +void action_attachment_prev(UiEvent *event, void *userdata); +void action_attachment_next(UiEvent *event, void *userdata); + #ifdef __cplusplus } #endif -- 2.43.5