]> uap-core.de Git - note.git/commitdiff
implement attachment window image viewer
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 3 Apr 2025 19:10:46 +0000 (21:10 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 3 Apr 2025 19:10:46 +0000 (21:10 +0200)
application/application.h
application/attachment.c
application/attachment.h

index 03a4fe434c141962c740af9d726726e7391020ed..345d749820086aead75df14ef67a64231bab3329 100644 (file)
@@ -173,6 +173,7 @@ typedef struct AttachmentWindow {
     
     Resource *resource;
     UiList *attachments;
+    int current_index;
     
     UiGeneric *image;
 } AttachmentWindow;
index 501c483e58a68d64f9eaa37d4e199d3534a7d46d..dbd17efd8204f3d1f0f3f8668a0b8843ee09fc15 100644 (file)
@@ -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);
+    }
+}
index d10fb165197214b1a6e1687b947e1fdcdce3be96..ea6ba615be5a4f40b20e90fec6d87ef4d890a085 100644 (file)
@@ -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