Merge Irssi 0.8.16-rc1
[silc.git] / apps / irssi / src / fe-text / gui-windows.c
1 /*
2  gui-windows.c : irssi
3
4     Copyright (C) 1999-2001 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "module.h"
22 #include "signals.h"
23 #include "misc.h"
24 #include "settings.h"
25 #include "special-vars.h"
26
27 #include "term.h"
28 #include "gui-entry.h"
29 #include "gui-windows.h"
30 #include "gui-printtext.h"
31
32 static int window_create_override;
33
34 static GUI_WINDOW_REC *gui_window_init(WINDOW_REC *window,
35                                        MAIN_WINDOW_REC *parent)
36 {
37         GUI_WINDOW_REC *gui;
38
39         window->width = parent->width;
40         window->height = MAIN_WINDOW_TEXT_HEIGHT(parent);
41
42         gui = g_new0(GUI_WINDOW_REC, 1);
43         gui->parent = parent;
44         gui->view = textbuffer_view_create(textbuffer_create(),
45                                            window->width, window->height,
46                                            settings_get_bool("scroll"),
47                                            term_type == TERM_TYPE_UTF8);
48         textbuffer_view_set_default_indent(gui->view,
49                                            settings_get_int("indent"),
50                                            !settings_get_bool("indent_always"),
51                                            get_default_indent_func());
52         if (parent->active == window)
53                 textbuffer_view_set_window(gui->view, parent->screen_win);
54         return gui;
55 }
56
57 static void gui_window_deinit(GUI_WINDOW_REC *gui)
58 {
59         textbuffer_view_destroy(gui->view);
60         g_free(gui);
61 }
62
63 static void sig_window_create_override(gpointer tab)
64 {
65         window_create_override = GPOINTER_TO_INT(tab);
66 }
67
68 static void gui_window_created(WINDOW_REC *window, void *automatic)
69 {
70         MAIN_WINDOW_REC *parent;
71         int new_parent;
72
73         g_return_if_fail(window != NULL);
74
75         new_parent = window_create_override == 0 ||
76                 window_create_override == 2 ||
77                 active_win == NULL || WINDOW_GUI(active_win) == NULL;
78         parent = !new_parent ? WINDOW_MAIN(active_win) : mainwindow_create();
79         if (parent == NULL) {
80                 /* not enough space for new window, but we really can't
81                    abort creation of the window anymore, so create hidden
82                    window instead. */
83                 parent = WINDOW_MAIN(active_win);
84         }
85         window_create_override = -1;
86
87         if (parent->active == NULL) parent->active = window;
88         window->gui_data = gui_window_init(window, parent);
89
90         /* set only non-automatic windows sticky so that the windows
91            irssi creates at startup wont get sticky. */
92         if (automatic == NULL &&
93             (parent->sticky_windows ||
94              (new_parent && settings_get_bool("autostick_split_windows"))))
95                 gui_window_set_sticky(window);
96
97         signal_emit("gui window created", 1, window);
98 }
99
100 static void gui_window_destroyed(WINDOW_REC *window)
101 {
102         MAIN_WINDOW_REC *parent;
103         GUI_WINDOW_REC *gui;
104
105         g_return_if_fail(window != NULL);
106
107         gui = WINDOW_GUI(window);
108         parent = gui->parent;
109
110         gui_window_set_unsticky(window);
111
112         signal_emit("gui window destroyed", 1, window);
113
114         gui_window_deinit(gui);
115         window->gui_data = NULL;
116
117         if (parent->active == window)
118                 mainwindow_change_active(parent, window);
119 }
120
121 void gui_window_resize(WINDOW_REC *window, int width, int height)
122 {
123         GUI_WINDOW_REC *gui;
124
125         if (window->width == width && window->height == height)
126                 return;
127
128         gui = WINDOW_GUI(window);
129
130         irssi_set_dirty();
131         WINDOW_MAIN(window)->dirty = TRUE;
132
133         window->width = width;
134         window->height = height;
135         textbuffer_view_resize(gui->view, width, height);
136 }
137
138 void gui_window_scroll(WINDOW_REC *window, int lines)
139 {
140         g_return_if_fail(window != NULL);
141
142         textbuffer_view_scroll(WINDOW_GUI(window)->view, lines);
143         signal_emit("gui page scrolled", 1, window);
144 }
145
146 void gui_window_scroll_line(WINDOW_REC *window, LINE_REC *line)
147 {
148         g_return_if_fail(window != NULL);
149         g_return_if_fail(line != NULL);
150
151         textbuffer_view_scroll_line(WINDOW_GUI(window)->view, line);
152         signal_emit("gui page scrolled", 1, window);
153 }
154
155 void gui_window_set_sticky(WINDOW_REC *window)
156 {
157         GUI_WINDOW_REC *gui = WINDOW_GUI(window);
158
159         if (!gui->sticky) {
160                 gui->sticky = TRUE;
161                 gui->parent->sticky_windows++;
162         }
163 }
164
165 void gui_window_set_unsticky(WINDOW_REC *window)
166 {
167         GUI_WINDOW_REC *gui = WINDOW_GUI(window);
168
169         if (gui->sticky) {
170                 gui->sticky = FALSE;
171                 gui->parent->sticky_windows--;
172         }
173 }
174
175 void gui_window_reparent(WINDOW_REC *window, MAIN_WINDOW_REC *parent)
176 {
177         MAIN_WINDOW_REC *oldparent;
178
179         oldparent = WINDOW_MAIN(window);
180         if (oldparent == parent)
181                 return;
182
183         gui_window_set_unsticky(window);
184         textbuffer_view_set_window(WINDOW_GUI(window)->view, NULL);
185
186         WINDOW_MAIN(window) = parent;
187         if (parent->sticky_windows)
188                 gui_window_set_sticky(window);
189
190         if (MAIN_WINDOW_TEXT_HEIGHT(parent) !=
191             MAIN_WINDOW_TEXT_HEIGHT(oldparent) ||
192             parent->width != oldparent->width) {
193                 gui_window_resize(window, parent->width,
194                                   MAIN_WINDOW_TEXT_HEIGHT(parent));
195         }
196 }
197
198 void gui_windows_reset_settings(void)
199 {
200         GSList *tmp;
201
202         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
203                 WINDOW_REC *rec = tmp->data;
204                 GUI_WINDOW_REC *gui = WINDOW_GUI(rec);
205
206                 textbuffer_view_set_default_indent(gui->view,
207                                                    settings_get_int("indent"),
208                                                    !settings_get_bool("indent_always"),
209                                                    get_default_indent_func());
210
211                 textbuffer_view_set_scroll(gui->view,
212                                            gui->use_scroll ? gui->scroll :
213                                            settings_get_bool("scroll"));
214         }
215 }
216
217 static MAIN_WINDOW_REC *mainwindow_find_unsticky(void)
218 {
219         GSList *tmp;
220
221         for (tmp = mainwindows; tmp != NULL; tmp = tmp->next) {
222                 MAIN_WINDOW_REC *rec = tmp->data;
223
224                 if (!rec->sticky_windows)
225                         return rec;
226         }
227
228         /* all windows are sticky, fallback to active window */
229         return active_mainwin;
230 }
231
232 static void signal_window_changed(WINDOW_REC *window)
233 {
234         MAIN_WINDOW_REC *parent;
235         WINDOW_REC *old_window;
236
237         g_return_if_fail(window != NULL);
238
239         if (quitting) return;
240
241         parent = WINDOW_MAIN(window);
242         if (is_window_visible(window)) {
243                 /* already visible */
244                 active_mainwin = parent;
245         } else if (active_mainwin == NULL) {
246                 /* no main window set yet */
247                 active_mainwin = parent;
248         } else if (WINDOW_GUI(window)->sticky) {
249                 /* window is sticky, switch to correct main window */
250                 if (parent != active_mainwin)
251                         active_mainwin = parent;
252         } else {
253                 /* move window to active main window */
254                 if (active_mainwin->sticky_windows) {
255                         /* active mainwindow is sticky, we'll need to
256                            set the window active somewhere else */
257                         active_mainwin = mainwindow_find_unsticky();
258                 }
259                 gui_window_reparent(window, active_mainwin);
260         }
261
262         old_window = active_mainwin->active;
263         if (old_window != NULL && old_window != window)
264                 textbuffer_view_set_window(WINDOW_GUI(old_window)->view, NULL);
265
266         active_mainwin->active = window;
267
268         textbuffer_view_set_window(WINDOW_GUI(window)->view,
269                                    active_mainwin->screen_win);
270         if (WINDOW_GUI(window)->view->dirty)
271                 active_mainwin->dirty = TRUE;
272 }
273
274 static void read_settings(void)
275 {
276         gui_windows_reset_settings();
277 }
278
279 void gui_windows_init(void)
280 {
281         settings_add_bool("lookandfeel", "autostick_split_windows", TRUE);
282         settings_add_int("lookandfeel", "indent", 10);
283         settings_add_bool("lookandfeel", "indent_always", FALSE);
284         settings_add_bool("lookandfeel", "scroll", TRUE);
285
286         window_create_override = -1;
287
288         read_settings();
289         signal_add("gui window create override", (SIGNAL_FUNC) sig_window_create_override);
290         signal_add("window created", (SIGNAL_FUNC) gui_window_created);
291         signal_add("window destroyed", (SIGNAL_FUNC) gui_window_destroyed);
292         signal_add_first("window changed", (SIGNAL_FUNC) signal_window_changed);
293         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
294 }
295
296 void gui_windows_deinit(void)
297 {
298         while (windows != NULL)
299                 window_destroy(windows->data);
300
301         signal_remove("gui window create override", (SIGNAL_FUNC) sig_window_create_override);
302         signal_remove("window created", (SIGNAL_FUNC) gui_window_created);
303         signal_remove("window destroyed", (SIGNAL_FUNC) gui_window_destroyed);
304         signal_remove("window changed", (SIGNAL_FUNC) signal_window_changed);
305         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
306 }