talkatu/talkatu

Fix a few things found in review

2020-06-29, Gary Kramlich
a831357ccddd
Fix a few things found in review
/*
* talkatu
* 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
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <gtk/gtk.h>
#include <talkatu/talkatuhistory.h>
#include <talkatu/talkatuattachmentpreview.h>
#include <talkatu/talkatuhistorybuffer.h>
#include <talkatu/talkatumessage.h>
/**
* SECTION:talkatuhistory
* @Title: History View
* @Short_description: Chat history widget
*
* This widget is used to display the conversation history. It doesn't care if
* it is multi user or one to one.
*/
/**
* TALKATU_TYPE_HISTORY:
*
* The standard _get_type macro for #TalkatuHistory.
*/
/**
* TalkatuHistory:
*
* A #TalkatuView subclass that is used to display a conversation.
*/
struct _TalkatuHistory {
TalkatuView parent;
GtkAdjustment *vadjustment;
gboolean auto_scroll;
};
G_DEFINE_TYPE(TalkatuHistory, talkatu_history, TALKATU_TYPE_VIEW)
/******************************************************************************
* Callbacks
*****************************************************************************/
static void
talkatu_history_vadjustment_changed_cb(GtkAdjustment *adjustment,
gpointer data)
{
TalkatuHistory *history = TALKATU_HISTORY(data);
if(history->auto_scroll) {
gdouble upper, pagesize;
upper = gtk_adjustment_get_upper(adjustment);
pagesize = gtk_adjustment_get_page_size(adjustment);
gtk_adjustment_set_value(adjustment, upper - pagesize);
}
}
static void
talkatu_history_vadjustment_value_changed_cb(GtkAdjustment *adjustment,
gpointer data)
{
TalkatuHistory *history = TALKATU_HISTORY(data);
gdouble current, upper, pagesize;
current = gtk_adjustment_get_value(adjustment);
upper = gtk_adjustment_get_upper(adjustment);
pagesize = gtk_adjustment_get_page_size(adjustment);
history->auto_scroll = (current + pagesize >= upper);
}
static void
talkatu_history_vadjustment_notify_cb(GObject *obj, GParamSpec *pspec,
gpointer data)
{
TalkatuHistory *history = TALKATU_HISTORY(obj);
GtkAdjustment *adjustment = NULL;
adjustment = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(obj));
if(g_set_object(&history->vadjustment, adjustment)) {
history->auto_scroll = TRUE;
g_signal_connect(G_OBJECT(adjustment), "value-changed",
G_CALLBACK(talkatu_history_vadjustment_value_changed_cb),
history);
g_signal_connect(G_OBJECT(adjustment), "changed",
G_CALLBACK(talkatu_history_vadjustment_changed_cb),
history);
}
}
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)) {
GtkWidget *preview = talkatu_attachment_preview_new(attachment);
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_handlers_disconnect_by_func(G_OBJECT(buffer),
G_CALLBACK(talkatu_history_insert_child_anchor_cb),
view);
g_signal_connect_after(G_OBJECT(buffer), "insert-child-anchor",
G_CALLBACK(talkatu_history_insert_child_anchor_cb),
view);
}
/******************************************************************************
* GtkTextViewClass overrides
*****************************************************************************/
static GtkTextBuffer *
talkatu_history_create_buffer(GtkTextView *view) {
return talkatu_history_buffer_new();
}
/******************************************************************************
* GObject Stuff
*****************************************************************************/
static void
talkatu_history_init(TalkatuHistory *history) {
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",
G_CALLBACK(talkatu_history_vadjustment_notify_cb), NULL);
}
static void
talkatu_history_finalize(GObject *obj) {
TalkatuHistory *history = TALKATU_HISTORY(obj);
g_clear_object(&history->vadjustment);
G_OBJECT_CLASS(talkatu_history_parent_class)->finalize(obj);
}
static void
talkatu_history_class_init(TalkatuHistoryClass *klass) {
GObjectClass *obj_class = G_OBJECT_CLASS(klass);
GtkTextViewClass *text_view_class = GTK_TEXT_VIEW_CLASS(klass);
obj_class->finalize = talkatu_history_finalize;
text_view_class->create_buffer = talkatu_history_create_buffer;
}
/******************************************************************************
* Public API
*****************************************************************************/
/**
* talkatu_history_new:
*
* Creates a new #TalkatuHistory for displaying chat history.
*
* Returns: (transfer full): The new #TalkatuHistory instance.
*/
GtkWidget *talkatu_history_new(void) {
return GTK_WIDGET(g_object_new(
TALKATU_TYPE_HISTORY,
NULL
));
}
/**
* talkatu_history_page_up:
* @history: The #TalkatuHistory instance.
*
* Scrolls @history up one page.
*/
void
talkatu_history_page_up(TalkatuHistory *history) {
g_return_if_fail(TALKATU_IS_HISTORY(history));
if(history->vadjustment != NULL) {
double value = 0.0;
value = gtk_adjustment_get_value(history->vadjustment);
value -= gtk_adjustment_get_page_increment(history->vadjustment);
gtk_adjustment_set_value(history->vadjustment, value);
}
}
/**
* talkatu_history_page_down:
* @history: The #TalkatuHistory instance.
*
* Scrolls @history down one page.
*/
void
talkatu_history_page_down(TalkatuHistory *history) {
g_return_if_fail(TALKATU_IS_HISTORY(history));
if(history->vadjustment != NULL) {
double value = 0.0;
value = gtk_adjustment_get_value(history->vadjustment);
value += gtk_adjustment_get_page_increment(history->vadjustment);
gtk_adjustment_set_value(history->vadjustment, value);
}
}