-void editor_load_markdown(UiText *text, cxmutstr markdown) {
+void editor_load_markdown(Resource *note, UIWIDGET textview, cxmutstr markdown) {
+ UiText *text = note->model->text;
// make sure the textbuf is initialized
editor_init_textbuf(text);
}
MDDocLinear lin = mddoc_linearization(doc);
ui_set(text, lin.content.ptr);
- editor_apply_styles(text, lin.styles);
+ editor_apply_styles(note, ui_textarea_gettextwidget(textview), text, lin.styles);
free(lin.content.ptr);
cxListFree(lin.styles);
void editor_init(UiText *text);
-void editor_load_markdown(UiText *text, cxmutstr markdown);
+void editor_load_markdown(Resource *note, UIWIDGET textview, cxmutstr markdown);
MDDoc* parse_markdown(cxstring markdown);
void mddoc_free(MDDoc *doc);
void editor_global_init();
void editor_init_textview(UiObject *obj, UIWIDGET textview);
void editor_init_textbuf(UiText *text);
-void editor_apply_styles(UiText *text, CxList /* MDDocStyleSection */ *styles);
+void editor_apply_styles(Resource *note, UIWIDGET textview, UiText *text, CxList /* MDDocStyleSection */ *styles);
cxmutstr editor_get_markdown(UiText *text, const CxAllocator *a);
UiBool editor_set_style(UiText *text, const char *style, UiBool enabled);
/*
* Applies all styles from the MDDocStyleSection list to the text buffer
*/
-void editor_apply_styles(UiText *text, CxList /* MDDocStyleSection */ *styles) {
+void editor_apply_styles(Resource *note, UIWIDGET textview, UiText *text, CxList /* MDDocStyleSection */ *styles) {
GtkTextBuffer *buffer = text->data1;
GtkTextTagTable *tagtable = gtk_text_buffer_get_tag_table(buffer);
CxIterator i = cxListIterator(styles);
cx_foreach(MDDocStyleSection*, sec, i) {
if(sec->length == MDDocStyleSection_IMAGE) {
- // TODO: insert image
+ Attachment *attachment = note_get_attachment(note, sec->link);
+ if(attachment && attachment->bin_content.length > 0) {
+ // we can use ui_image_load_data to load the image, but we
+ // need an UiGeneric object for that
+ UiGeneric imgobj;
+ memset(&imgobj, 0, sizeof(UiGeneric));
+ if(!ui_image_load_data(&imgobj, attachment->bin_content.ptr, attachment->bin_content.length)) {
+ GdkPixbuf *pixbuf = imgobj.value;
+
+ // TODO: remove code dup
+ GtkTextIter iter;
+ gtk_text_buffer_get_iter_at_offset(buffer, &iter, sec->pos);
+ GtkTextChildAnchor *anchor = gtk_text_buffer_create_child_anchor(buffer, &iter);
+ GtkWidget *image = embedded_image_create(pixbuf);
+ gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(textview), image, anchor);
+
+ EmbeddedWidget *em = malloc(sizeof(EmbeddedWidget));
+ em->widget = image;
+ em->anchor = anchor;
+ em->data1 = attachment;
+ em->data2 = NULL;
+ em->serialize = md_serialize_image;
+ g_object_ref(image);
+ g_object_ref(anchor);
+
+ g_object_set_data(G_OBJECT(anchor), "em", em);
+ } else {
+ fprintf(stderr, "Error: cannot load image data\n");
+ }
+ } else {
+ // TODO: what do we do in this case?
+ }
continue;
}
UiBool attachments;
} LoadNoteContent;
-static void note_loading_completed(LoadNoteContent *op) {
+static void note_loading_completed(UiObject *obj, LoadNoteContent *op) {
Resource *note = op->note;
+ MainWindow *wdata = obj->window;
free(op);
if(note->model) {
- editor_load_markdown(note->model->text, note->content);
+ editor_load_markdown(note, wdata->textview, note->content);
}
note->content_loaded = TRUE;
}
printf("note content: %s\n", result.ptr);
op->content = TRUE;
if(op->attachments) {
- note_loading_completed(op);
+ note_loading_completed(event->obj, op);
}
}
}
if(op->content) {
- note_loading_completed(op);
+ note_loading_completed(event->obj, op);
}
}
// TODO: destroy model->context
}
}
+
+Attachment* note_get_attachment(Resource *note, const char *path) {
+ if(!note->attachments) {
+ return NULL;
+ }
+ CxIterator i = cxListIterator(note->attachments);
+ cx_foreach(Attachment *, attachment, i) {
+ // TODO: support path, not just the name
+ if(!strcmp(attachment->name, path)) {
+ return attachment;
+ }
+ }
+ return NULL;
+}
const char* note_get_title(Resource *note);
void note_destroy(const CxAllocator *a, Resource *note);
+Attachment* note_get_attachment(Resource *note, const char *path);
+
#ifdef __cplusplus
}
#endif
new_note->model->modified = TRUE;
// initialize note content
// possible to implement something like note templates here
- editor_load_markdown(new_note->model->text, cx_mutstrn("", 0));
+ editor_load_markdown(new_note, model->window->textview, cx_mutstrn("", 0));
}
typedef struct NoteDeleteOp {
GInputStream *in = g_memory_input_stream_new_from_bytes(bytes);
GError *error = NULL;
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_stream(in, NULL, &error);
- g_object_unref(bytes);
g_object_unref(in);
if(!pixbuf) {
return 1;