SRC += notebook.c
SRC += note.c
SRC += editor.c
+SRC += attachment.c
SRC += $(APP_PLATFORM_SRC)
OBJ = $(SRC:%.c=../build/application/%$(OBJ_EXT))
bool modified;
};
-
-typedef enum AttachmentType {
- NOTE_ATTACHMENT_FILE = 0,
- NOTE_ATTACHMENT_IMAGE
-} AttachmentType;
typedef struct AttachmentModel {
UiContext *ctx;
Resource *parent_note;
- int64_t resource_id;
- AttachmentType type;
- char *name;
- char *temp_path;
- char *temp_data;
-
- void *data;
+ UiGeneric *img;
} AttachmentModel;
void application_init();
--- /dev/null
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2025 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "attachment.h"
+
+Attachment* attachment_create(
+ const CxAllocator *a,
+ int64_t parent,
+ AttachmentType type,
+ const char *name)
+{
+ Attachment *attachment = cxCalloc(a, 1, sizeof(Attachment));
+ attachment->parent_resource_id = parent;
+ attachment->name = cx_strdup_a(a, cx_str(name)).ptr;
+ attachment->type = type;
+ return attachment;
+}
+
+void attachment_create_ui_model(UiContext *ctx, Attachment *attachment) {
+ if(attachment->ui) {
+ return;
+ }
+
+ const CxAllocator *a = ui_allocator(ctx);
+ AttachmentModel *model = cxCalloc(a, 1, sizeof(AttachmentModel));
+ attachment->ui = model;
+
+ if(attachment->type == NOTE_ATTACHMENT_IMAGE) {
+ model->img = ui_generic_new(ctx, NULL);
+ }
+ model->ctx = ctx;
+}
+
+void attachment_set_image(Attachment *attachment, void *img) {
+ // TODO: replace this with new toolkit function for setting UiGeneric values
+ if(attachment->ui->img->set) {
+ attachment->ui->img->set(attachment->ui->img, img, UI_IMAGE_OBJECT_TYPE);
+ } else {
+ attachment->ui->img->value = img;
+ attachment->ui->img->type = UI_IMAGE_OBJECT_TYPE;
+ }
+}
+
+
+
+void attachment_item(UiObject *obj, int index, void *elm, void *userdata) {
+ Attachment *attachment = elm;
+
+ if(attachment->type == NOTE_ATTACHMENT_IMAGE) {
+ ui_imageviewer(obj, .value = attachment->ui->img, .scrollarea = FALSE);
+ }
+}
--- /dev/null
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2025 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef ATTACHMENT_H
+#define ATTACHMENT_H
+
+#include "application.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+Attachment* attachment_create(
+ const CxAllocator *a,
+ int64_t parent,
+ AttachmentType type,
+ const char *name);
+
+void attachment_create_ui_model(UiContext *ctx, Attachment *attachment);
+
+void attachment_set_image(Attachment *attachment, void *img);
+
+/*
+ * create UI for an attachment item
+ */
+void attachment_item(UiObject *obj, int index, void *elm, void *userdata);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* ATTACHMENT_H */
+
#include "gtk-text.h"
#include "editor.h"
#include "note.h"
+#include "attachment.h"
#include <cx/buffer.h>
#include <cx/array_list.h>
}
static void editor_attach_image(NoteEditor *editor, GdkPixbuf *pixbuf, char *attachment_path) {
+ MainWindow *wdata = editor->obj->window;
+ Resource *note = wdata->current_notebook->current_note;
+ NoteModel *model = note->model;
+
+ // create attachment
+ Attachment *attachment = attachment_create(model->note_allocator, note->resource_id, NOTE_ATTACHMENT_IMAGE, util_resource_name(attachment_path));
+ attachment_create_ui_model(model->ctx, attachment);
+ g_object_ref(pixbuf);
+ attachment_set_image(attachment, pixbuf);
+
+ // insert image
GtkTextView *textview = GTK_TEXT_VIEW(editor->textview);
GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
gtk_widget_set_size_request(image, width, height);
gtk_text_view_add_child_at_anchor(textview, image, anchor);
-
+
// remember widget and store a reference in the textbuffer
// TODO: we need a cleanup strategy
EmbeddedWidget *em = malloc(sizeof(EmbeddedWidget));
em->widget = image;
em->anchor = anchor;
- em->name = attachment_path ? strdup(util_resource_name(attachment_path)) : NULL;
- em->path = attachment_path;
+ //em->name = attachment_path ? strdup(util_resource_name(attachment_path)) : NULL;
+ //em->path = attachment_path;
em->data1 = pixbuf;
em->data2 = NULL;
em->serialize = md_serialize_image;
cxListAdd(embedded_objects->objects, em);
// add attachment to note
- MainWindow *wdata = editor->obj->window;
- Resource *note = wdata->current_notebook->current_note;
- NoteModel *model = note->model;
-
- // TODO: this is just a test
- ui_list_append(model->attachments, em);
+ ui_list_append(model->attachments, attachment);
ui_list_update(model->attachments);
}
#endif
-typedef struct NotebookModel NotebookModel;
-typedef struct NoteModel NoteModel;
+typedef struct NotebookModel NotebookModel;
+typedef struct NoteModel NoteModel;
+typedef struct AttachmentModel AttachmentModel;
typedef struct UserSettings UserSettings;
NoteModel *model;
};
+typedef enum AttachmentType {
+ NOTE_ATTACHMENT_FILE = 0,
+ NOTE_ATTACHMENT_IMAGE
+} AttachmentType;
+
struct Attachment {
- // db fields
-
+ // db
int64_t attachment_id;
int64_t attachment_resource_id;
int64_t parent_resource_id;
int type;
- // temp fields
+ // temp (from table resources)
char *name;
cxmutstr bin_content;
+ bool content_loaded;
+
+ // ui
+ AttachmentModel *ui;
};
extern DBUClass *usersettings_class;
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
- * Copyright 2024 Olaf Wintermann. All rights reserved.
+ * Copyright 2025 Olaf Wintermann. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
#include "editor.h"
#include "note.h"
#include "menu.h"
+#include "attachment.h"
#include <cx/array_list.h>
#include <cx/hash_map.h>
ui_show(obj);
}
-void attachment_item(UiObject *obj, int index, void *elm, void *userdata) {
- ui_button(obj, .label = "Attachment Dummy");
-}
-
MainWindow* window_init_data(UiObject *obj) {
MainWindow *wdata = ui_calloc(obj->ctx, 1, sizeof(MainWindow));
obj->window = wdata;
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
- * Copyright 2024 Olaf Wintermann. All rights reserved.
+ * Copyright 2025 Olaf Wintermann. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
void* window_notelist_getvalue(void *data, int col);
-void attachment_item(UiObject *obj, int index, void *elm, void *userdata);
-
void action_notebook_selected(UiEvent *event, void *userdata);
void action_note_selected(UiEvent *event, void *userdata);
void action_note_activated(UiEvent *event, void *userdata);