summaryrefslogtreecommitdiff
path: root/components/chatmessage.cpp
blob: 048d7259c04338fdeec41fdad68ccc60292d3d99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "chatmessage.hpp"

ChatMessageTextItem::ChatMessageTextItem(const MessageData *data) {
    m_main_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
    m_sub_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
    m_meta_box = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
    m_author = Gtk::manage(new Gtk::Label);
    m_timestamp = Gtk::manage(new Gtk::Label);
    m_text = Gtk::manage(new Gtk::TextView);

    m_text->set_can_focus(false);
    m_text->set_editable(false);
    m_text->set_wrap_mode(Gtk::WRAP_WORD_CHAR);
    m_text->set_halign(Gtk::ALIGN_FILL);
    m_text->set_hexpand(true);
    m_text->get_buffer()->set_text(data->Content);
    m_text->show();

    m_author->set_markup("<span weight=\"bold\">" + Glib::Markup::escape_text(data->Author.Username) + "</span>");
    m_author->set_single_line_mode(true);
    m_author->set_line_wrap(false);
    m_author->set_ellipsize(Pango::ELLIPSIZE_END);
    m_author->set_xalign(0.f);
    m_author->set_can_focus(false);
    m_author->show();

    m_timestamp->set_text(data->Timestamp);
    m_timestamp->set_opacity(0.5);
    m_timestamp->set_single_line_mode(true);
    m_timestamp->set_margin_start(12);
    m_timestamp->set_can_focus(false);
    m_timestamp->show();

    m_main_box->set_hexpand(true);
    m_main_box->set_vexpand(true);
    m_main_box->set_can_focus(true);
    m_main_box->show();

    m_meta_box->set_can_focus(false);
    m_meta_box->show();

    m_sub_box->set_can_focus(false);
    m_sub_box->show();

    m_meta_box->add(*m_author);
    m_meta_box->add(*m_timestamp);
    m_sub_box->add(*m_meta_box);
    m_sub_box->add(*m_text);
    m_main_box->add(*m_sub_box);
    add(*m_main_box);
    set_margin_bottom(8);

    show();
}

void ChatMessageTextItem::AppendNewContent(std::string content) {
    auto buf = m_text->get_buffer();
    buf->set_text(buf->get_text() + "\n" + content);
}

void ChatMessageTextItem::PrependNewContent(std::string content) {
    auto buf = m_text->get_buffer();
    buf->set_text(content + "\n" + buf->get_text());
}

void ChatMessageTextItem::MarkAsDeleted() {
    auto buf = m_text->get_buffer();
    Gtk::TextBuffer::iterator start, end;
    buf->get_bounds(start, end);
    buf->insert_markup(end, "<span color='#ff0000'> [deleted]</span>");
}