talkatu/talkatu

06b60cbecd02
Parents 7e0ffe7001ed
Children 2f95d8c8d2e8
Initial TalkatuHistory rendering of attachments
--- a/talkatu/talkatuhistory.c Wed Jan 29 08:35:45 2020 -0600
+++ b/talkatu/talkatuhistory.c Fri Jan 31 08:39:41 2020 -0600
@@ -1,6 +1,6 @@
/*
* talkatu
- * Copyright (C) 2017-2019 Gary Kramlich <grim@reaperworld.com>
+ * Copyright (C) 2017-2020 Gary Kramlich <grim@reaperworld.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -103,6 +103,38 @@
}
}
+static void
+talkatu_history_insert_child_anchor_cb(GtkTextBuffer *buffer, GtkTextIter *iter,
+ GtkTextChildAnchor *anchor,
+ gpointer data)
+{
+ TalkatuAttachment *attachment = NULL;
+ GtkTextView *view = GTK_TEXT_VIEW(data);
+
+ attachment = g_object_get_data(G_OBJECT(anchor), "talkatu:attachment");
+ if(TALKATU_IS_ATTACHMENT(attachment)) {
+ GIcon *icon = NULL;
+ GtkWidget *preview = NULL;
+
+ icon = talkatu_attachment_get_preview(attachment);
+
+ preview = gtk_image_new_from_gicon(icon, GTK_ICON_SIZE_DIALOG);
+ gtk_image_set_pixel_size(GTK_IMAGE(preview), 128);
+
+ gtk_text_view_add_child_at_anchor(view, preview, anchor);
+ gtk_widget_show(preview);
+ }
+}
+
+static void
+talkatu_history_buffer_set_cb(GObject *view, GParamSpec *pspec, gpointer data) {
+ GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
+
+ g_signal_connect_after(G_OBJECT(buffer), "insert-child-anchor",
+ G_CALLBACK(talkatu_history_insert_child_anchor_cb),
+ view);
+}
+
/******************************************************************************
* GtkTextViewClass overrides
*****************************************************************************/
@@ -119,6 +151,9 @@
gtk_text_view_set_editable(GTK_TEXT_VIEW(history), FALSE);
gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(history), FALSE);
+ g_signal_connect(G_OBJECT(history), "notify::buffer",
+ G_CALLBACK(talkatu_history_buffer_set_cb), NULL);
+
history->auto_scroll = TRUE;
g_signal_connect(G_OBJECT(history), "notify::vadjustment",
--- a/talkatu/talkatuhistorybuffer.c Wed Jan 29 08:35:45 2020 -0600
+++ b/talkatu/talkatuhistorybuffer.c Fri Jan 31 08:39:41 2020 -0600
@@ -53,15 +53,43 @@
* The backing class to #TalkatuHistoryBuffer.
*/
+typedef struct {
+ GtkTextBuffer *buffer;
+ GtkTextIter *iter;
+} TalkatuHistoryBufferForeachAttachmentData;
+
G_DEFINE_TYPE(TalkatuHistoryBuffer, talkatu_history_buffer, GTK_TYPE_TEXT_BUFFER);
/******************************************************************************
+ * Helpers
+ *****************************************************************************/
+static void
+talkatu_history_buffer_add_attachment(TalkatuAttachment *attachment,
+ gpointer data)
+{
+ TalkatuHistoryBufferForeachAttachmentData *d = NULL;
+ GtkTextChildAnchor *anchor = NULL;
+
+ d = (TalkatuHistoryBufferForeachAttachmentData *)data;
+
+ /* we have to create the anchor first so that we can add the attachment to
+ * it before it's inserted into the buffer.
+ */
+ anchor = gtk_text_child_anchor_new();
+ g_object_set_data_full(G_OBJECT(anchor), "talkatu:attachment",
+ g_object_ref(attachment), g_object_unref);
+ gtk_text_buffer_insert_child_anchor(d->buffer, d->iter, anchor);
+ g_object_unref(G_OBJECT(anchor));
+}
+
+/******************************************************************************
* GObject Stuff
*****************************************************************************/
static void
talkatu_history_buffer_real_write_message(TalkatuHistoryBuffer *talkatu_buffer,
TalkatuMessage *message)
{
+ TalkatuHistoryBufferForeachAttachmentData data;
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(talkatu_buffer);
GtkTextIter start, end;
GDateTime *timestamp = NULL;
@@ -115,6 +143,13 @@
NULL
);
g_free(contents);
+
+ /* add any and all attachments */
+ data.buffer = buffer;
+ data.iter = &end;
+ talkatu_message_foreach_attachment(message,
+ talkatu_history_buffer_add_attachment,
+ &data);
}
/******************************************************************************