-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgui.cpp
More file actions
128 lines (102 loc) · 3.73 KB
/
gui.cpp
File metadata and controls
128 lines (102 loc) · 3.73 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <giomm/notification.h>
#include <giomm/socket.h>
#include <giomm/socketsource.h>
#include <glibmm/main.h>
#include <glibmm/convert.h>
#include <gtkmm/application.h>
#include <gtkmm/applicationwindow.h>
#include <gtkmm/box.h>
#include <gtkmm/entry.h>
#include <gtkmm/hvpaned.h>
#include <gtkmm/listbox.h>
#include <gtkmm/scrolledwindow.h>
#include <gtkmm/textview.h>
#include <sigc++/functors/mem_fun.h>
#include "lib/commune.h"
#define APP_ID "au.id.tedp.commune.gtkmm"
namespace {
// XXX: Shouldn't inherit from Commune but it's easier for now.
class CommuneGui: public Gtk::Application, public commune::Commune {
public:
CommuneGui(int argc, char *argv[]);
int run();
protected:
void on_startup() override;
void receiveMessage(const std::string& sender, const std::string& msg) override;
bool handleSocketEvent(Glib::IOCondition io_condition);
void processInput();
private:
Gtk::ApplicationWindow window_;
Glib::RefPtr<Gtk::TextBuffer> textbufferp_;
Gtk::VBox vbox_;
Gtk::HPaned hpane_;
Gtk::ListBox userlist_;
Gtk::ScrolledWindow userlist_scroller_;
Gtk::Entry entry_;
Gtk::ScrolledWindow textview_scroller_;
Glib::RefPtr<Gtk::TextBuffer::Mark> end_mark_;
Gtk::TextView textview_;
};
CommuneGui::CommuneGui(int argc, char *argv[]):
Gtk::Application(argc, argv, APP_ID),
commune::Commune::Commune(),
textbufferp_(Gtk::TextBuffer::create())
{
window_.set_title("Commune");
window_.set_default_size(640, 480);
hpane_.set_position(500);
textview_.set_buffer(textbufferp_);
textview_.set_editable(false);
textview_.set_cursor_visible(false);
textview_scroller_.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_ALWAYS);
// TODO: on resize, scroll to end.
//textview_scroller_.signal_configure_event().connect(/*...*/);
textview_scroller_.add(textview_);
end_mark_ = textbufferp_->create_mark("end", textbufferp_->end());
hpane_.pack1(textview_scroller_, true, true);
hpane_.pack2(userlist_, true, true);
vbox_.pack_start(hpane_, true, true);
vbox_.pack_end(entry_, false, false);
window_.add(vbox_);
entry_.grab_focus();
entry_.signal_activate().connect(sigc::slot<void>(sigc::mem_fun(this, &CommuneGui::processInput)));
window_.show_all();
}
int CommuneGui::run() {
return Gtk::Application::run(window_);
}
void CommuneGui::on_startup() {
Gtk::Application::on_startup();
Gio::signal_socket().connect(sigc::slot<bool, Glib::IOCondition>(sigc::mem_fun(this, &CommuneGui::handleSocketEvent)), Gio::Socket::create_from_fd(event_fd()), Glib::IO_IN);
}
bool CommuneGui::handleSocketEvent(Glib::IOCondition io_condition) {
commune::Commune::onEvent();
return true;
}
void CommuneGui::receiveMessage(const std::string& sender, const std::string& msg) {
std::string display_message = sender + ": " + msg + '\n';
auto pos = textbufferp_->insert(textbufferp_->end(), display_message);
textbufferp_->move_mark(end_mark_, textbufferp_->end());
textview_.scroll_to(end_mark_);
auto notification_ref = Gio::Notification::create("Commune message");
notification_ref->set_body(display_message);
send_notification("new-message", notification_ref);
}
void CommuneGui::processInput() {
if (entry_.get_text_length() == 0)
return;
std::string msg = Glib::locale_to_utf8(entry_.get_text());
if (msg.compare(0, 6, "/nick ") == 0) {
// Maybe truncate at first non-printable (eg. \n)
// Better do that for incoming nicknames.
setNick(msg.substr(6));
} else {
Commune::sendMessage(msg);
}
entry_.set_text(Glib::ustring());
}
} // anonymous namespace
int main(int argc, char *argv[]) {
return CommuneGui(argc, argv).run();
}