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) {
}
-
+/*
+ * 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;
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);
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);
}
}
+/*
+ * 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));
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);
+ }
+}