-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminalwindow.cpp
295 lines (258 loc) · 9.91 KB
/
terminalwindow.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "svanterm.h"
extern bool broadcast_active;
extern FindWindow *find_window;
void TerminalWindow::update_title() {
auto focus = get_focus();
if (focus == NULL)
return;
Terminal *terminal = dynamic_cast<Terminal *>(focus->get_parent()->get_parent());
if (terminal == NULL)
return;
if (const char *terminal_title = vte_terminal_get_window_title((VteTerminal *)terminal->vte)) {
auto title = std::string(terminal_title);
title.append(" - SvanTerm2000");
set_title(title);
}
}
void TerminalWindow::resize_terminal(int direction) {
auto widget = get_focus();
while (!dynamic_cast<TabFrame *>(widget)) {
if (auto splitter = dynamic_cast<Splitter *>(widget->get_parent())) {
if ((splitter->get_orientation() == Gtk::ORIENTATION_VERTICAL &&
(direction == GDK_KEY_Down || direction == GDK_KEY_Up)) ||
(splitter->get_orientation() == Gtk::ORIENTATION_HORIZONTAL &&
(direction == GDK_KEY_Left || direction == GDK_KEY_Right))) {
auto position = splitter->get_position();
if (direction == GDK_KEY_Up || direction == GDK_KEY_Left)
position -= 50;
else
position += 50;
splitter->set_position(position);
return;
}
}
widget = widget->get_parent();
}
}
void TerminalWindow::walk_terminal(int direction) {
auto focus_widget = get_focus();
auto widget = focus_widget;
std::vector<Terminal *> possible_terminals;
while (!dynamic_cast<TabFrame *>(widget)) {
if (auto splitter = dynamic_cast<Splitter *>(widget->get_parent())) {
if ((splitter->get_orientation() == Gtk::ORIENTATION_VERTICAL &&
(direction == GDK_KEY_Down || direction == GDK_KEY_Up)) ||
(splitter->get_orientation() == Gtk::ORIENTATION_HORIZONTAL &&
(direction == GDK_KEY_Left || direction == GDK_KEY_Right))) {
if (splitter->get_child1() == widget &&
(direction == GDK_KEY_Down || direction == GDK_KEY_Right)) {
possible_terminals = build_terminal_list(splitter->get_child2());
if (possible_terminals.size())
break;
} else if (splitter->get_child2() == widget &&
(direction == GDK_KEY_Up || direction == GDK_KEY_Left)) {
possible_terminals = build_terminal_list(splitter->get_child1());
if (possible_terminals.size())
break;
}
}
}
widget = widget->get_parent();
}
if (possible_terminals.size()) {
Terminal *nearest_terminal = possible_terminals[0];
int nearest_x, nearest_y, this_x, this_y;
focus_widget->translate_coordinates(*nearest_terminal, 0, 0, nearest_x, nearest_y);
for (auto terminal : possible_terminals) {
focus_widget->translate_coordinates(*terminal, 0, 0, this_x, this_y);
if (abs(this_x) + abs(this_y) < abs(nearest_x) + abs(nearest_y)) {
nearest_terminal = terminal;
nearest_x = this_x;
nearest_y = this_y;
}
}
nearest_terminal->focus_vte();
}
}
bool TerminalWindow::confirm_broadcast() {
Gtk::MessageDialog dialog("This will broadcast your keyboard input to all terminals in the current tab, continue?",
false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_YES_NO, true);
return dialog.run() == Gtk::RESPONSE_YES;
}
bool TerminalWindow::KeyPress(GdkEventKey* event) {
TerminalWindow *window;
auto focus_child = get_focus();
if (focus_child == NULL)
return false;
Terminal *terminal = dynamic_cast<Terminal *>(focus_child->get_parent()->get_parent());
if (terminal == NULL)
return false;
/* Shift-Insert is implemented in VTE, however it seems to fail under certain circumstances.
We implement it here instead */
if (event->state & GDK_SHIFT_MASK && (event->keyval == GDK_KEY_Insert ||
event->state & GDK_CONTROL_MASK && event->keyval == GDK_KEY_V)) {
vte_terminal_paste_clipboard(VTE_TERMINAL(focus_child->gobj()));
return true;
}
if ((event->state & GDK_CONTROL_MASK) && (event->state & GDK_SHIFT_MASK)) {
switch (event->keyval) {
case GDK_KEY_W:
delete get_tab_frame(get_focus());
return true;
case GDK_KEY_F:
find_window->show_find(this);
return true;
case GDK_KEY_I:
terminal->searchbar.set_search_mode();
return true;
case GDK_KEY_T:
tabcontrol.add_tab();
show_all_children();
return true;
case GDK_KEY_J:
cycle_terminals(-1);
return true;
case GDK_KEY_K:
cycle_terminals(1);
return true;
case GDK_KEY_N:
window = new TerminalWindow;
window->tabcontrol.add_tab();
window->show_all();
return true;
case GDK_KEY_M:
get_tab_frame(terminal)->edit_title();
return true;
case GDK_KEY_D:
terminal->kill_vte();
return true;
case GDK_KEY_S:
terminal->notifications_enabled = !terminal->notifications_enabled;
terminal->update_title();
return true;
case GDK_KEY_B:
if (broadcast_active || confirm_broadcast()) {
broadcast_active = !broadcast_active;
update_active_terminals();
}
return true;
case GDK_KEY_Up:
case GDK_KEY_Down:
case GDK_KEY_Left:
case GDK_KEY_Right:
resize_terminal(event->keyval);
return true;
case GDK_KEY_E:
case GDK_KEY_R:
Gtk::Container *parent = terminal->get_parent();
Terminal *new_terminal = manage(new Terminal);
if (event->keyval == GDK_KEY_E)
manage(new Splitter(parent, terminal, new_terminal, Gtk::ORIENTATION_HORIZONTAL));
else
manage(new Splitter(parent, terminal, new_terminal, Gtk::ORIENTATION_VERTICAL));
show_all_children();
new_terminal->focus_vte();
return true;
}
}
if ((event->state & GDK_CONTROL_MASK) && !(event->state & GDK_SHIFT_MASK)) {
switch (event->keyval) {
case GDK_KEY_Up:
case GDK_KEY_Down:
case GDK_KEY_Left:
case GDK_KEY_Right:
walk_terminal(event->keyval);
return true;
}
}
if (!(event->state & GDK_CONTROL_MASK) && (event->state & GDK_SHIFT_MASK)) {
switch (event->keyval) {
case GDK_KEY_Up:
cycle_windows(-1);
return true;
case GDK_KEY_Down:
cycle_windows(1);
return true;
case GDK_KEY_Left:
tabcontrol.prev_page();
return true;
case GDK_KEY_Right:
tabcontrol.next_page();
return true;
}
}
if (broadcast_active) {
gboolean ret;
for (auto terminal : build_terminal_list(tabcontrol.get_nth_page(tabcontrol.get_current_page()))) {
g_signal_emit_by_name(terminal->vte, "key-press-event", event, &ret);
}
return true;
}
return false;
}
void TerminalWindow::cycle_terminals(int offset) {
Gtk::Widget *focused = get_focus();
if (!focused)
return;
Terminal *terminal = dynamic_cast<Terminal *>(focused->get_parent()->get_parent());
auto list = build_terminal_list(get_tab_frame(terminal));
auto it = find(list.begin(), list.end(), terminal);
it += offset;
if (it >= list.end())
it = list.begin();
else if (it < list.begin())
it = list.end() - 1;
(*it)->focus_vte();
}
void TerminalWindow::cycle_windows(int offset) {
auto list = list_toplevels();
auto it = find(list.begin(), list.end(), this);
while (true) {
it += offset;
if (it >= list.end())
it = list.begin();
else if (it < list.begin())
it = list.end() - 1;
if (dynamic_cast<TerminalWindow *>(*it))
break;
}
(*it)->present();
}
TerminalWindow::TerminalWindow() {
resize(900, 600);
signal_delete_event().connect(mem_fun(this, &TerminalWindow::delete_event));
signal_key_press_event().connect(mem_fun(this, &TerminalWindow::KeyPress), false);
signal_focus_in_event().connect(mem_fun(this, &TerminalWindow::got_focus));
add(tabcontrol);
set_title("SvanTerm2000");
try {
set_icon_from_file(getexepath().append("/svanterm.png"));
} catch (...) {
printf("Failed to set icon");
fflush(stdout);
}
show_all();
};
bool TerminalWindow::delete_event(GdkEventAny *event) {
for (auto tabframe : tabcontrol.get_children())
delete tabframe;
for (auto it : list_toplevels()) {
if (TerminalWindow *terminal = dynamic_cast<TerminalWindow *>(it)) {
if (terminal != this) {
delete this;
return FALSE;
}
}
}
notify_uninit();
Gtk::Main::quit();
return FALSE;
}
bool TerminalWindow::got_focus(GdkEventFocus* event) {
if (get_focus_child() == NULL) {
auto terminal_list = build_terminal_list(tabcontrol.get_nth_page(tabcontrol.get_current_page()));
if (terminal_list.size() > 0)
terminal_list[0]->focus_vte();
}
return false;
}