4 Copyright (C) 1999-2001 Timo Sirainen
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.
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.
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.
25 #include "special-vars.h"
28 #include "gui-entry.h"
29 #include "gui-windows.h"
30 #include "gui-printtext.h"
32 static int window_create_override;
34 static GUI_WINDOW_REC *gui_window_init(WINDOW_REC *window,
35 MAIN_WINDOW_REC *parent)
39 window->width = parent->width;
40 window->height = MAIN_WINDOW_TEXT_HEIGHT(parent);
42 gui = g_new0(GUI_WINDOW_REC, 1);
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);
57 static void gui_window_deinit(GUI_WINDOW_REC *gui)
59 textbuffer_view_destroy(gui->view);
63 static void sig_window_create_override(gpointer tab)
65 window_create_override = GPOINTER_TO_INT(tab);
68 static void gui_window_created(WINDOW_REC *window, void *automatic)
70 MAIN_WINDOW_REC *parent;
73 g_return_if_fail(window != NULL);
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();
80 /* not enough space for new window, but we really can't
81 abort creation of the window anymore, so create hidden
83 parent = WINDOW_MAIN(active_win);
85 window_create_override = -1;
87 if (parent->active == NULL) parent->active = window;
88 window->gui_data = gui_window_init(window, parent);
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);
97 signal_emit("gui window created", 1, window);
100 static void gui_window_destroyed(WINDOW_REC *window)
102 MAIN_WINDOW_REC *parent;
105 g_return_if_fail(window != NULL);
107 gui = WINDOW_GUI(window);
108 parent = gui->parent;
110 gui_window_set_unsticky(window);
112 signal_emit("gui window destroyed", 1, window);
114 gui_window_deinit(gui);
115 window->gui_data = NULL;
117 if (parent->active == window)
118 mainwindow_change_active(parent, window);
121 void gui_window_resize(WINDOW_REC *window, int width, int height)
125 if (window->width == width && window->height == height)
128 gui = WINDOW_GUI(window);
131 WINDOW_MAIN(window)->dirty = TRUE;
133 window->width = width;
134 window->height = height;
135 textbuffer_view_resize(gui->view, width, height);
138 void gui_window_scroll(WINDOW_REC *window, int lines)
140 g_return_if_fail(window != NULL);
142 textbuffer_view_scroll(WINDOW_GUI(window)->view, lines);
143 signal_emit("gui page scrolled", 1, window);
146 void gui_window_scroll_line(WINDOW_REC *window, LINE_REC *line)
148 g_return_if_fail(window != NULL);
149 g_return_if_fail(line != NULL);
151 textbuffer_view_scroll_line(WINDOW_GUI(window)->view, line);
152 signal_emit("gui page scrolled", 1, window);
155 void gui_window_set_sticky(WINDOW_REC *window)
157 GUI_WINDOW_REC *gui = WINDOW_GUI(window);
161 gui->parent->sticky_windows++;
165 void gui_window_set_unsticky(WINDOW_REC *window)
167 GUI_WINDOW_REC *gui = WINDOW_GUI(window);
171 gui->parent->sticky_windows--;
175 void gui_window_reparent(WINDOW_REC *window, MAIN_WINDOW_REC *parent)
177 MAIN_WINDOW_REC *oldparent;
179 oldparent = WINDOW_MAIN(window);
180 if (oldparent == parent)
183 gui_window_set_unsticky(window);
184 textbuffer_view_set_window(WINDOW_GUI(window)->view, NULL);
186 WINDOW_MAIN(window) = parent;
187 if (parent->sticky_windows)
188 gui_window_set_sticky(window);
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));
198 void gui_windows_reset_settings(void)
202 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
203 WINDOW_REC *rec = tmp->data;
204 GUI_WINDOW_REC *gui = WINDOW_GUI(rec);
206 textbuffer_view_set_default_indent(gui->view,
207 settings_get_int("indent"),
208 !settings_get_bool("indent_always"),
209 get_default_indent_func());
211 textbuffer_view_set_scroll(gui->view,
212 gui->use_scroll ? gui->scroll :
213 settings_get_bool("scroll"));
217 static MAIN_WINDOW_REC *mainwindow_find_unsticky(void)
221 for (tmp = mainwindows; tmp != NULL; tmp = tmp->next) {
222 MAIN_WINDOW_REC *rec = tmp->data;
224 if (!rec->sticky_windows)
228 /* all windows are sticky, fallback to active window */
229 return active_mainwin;
232 static void signal_window_changed(WINDOW_REC *window)
234 MAIN_WINDOW_REC *parent;
235 WINDOW_REC *old_window;
237 g_return_if_fail(window != NULL);
239 if (quitting) return;
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;
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();
259 gui_window_reparent(window, active_mainwin);
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);
266 active_mainwin->active = window;
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;
274 static void read_settings(void)
276 gui_windows_reset_settings();
279 void gui_windows_init(void)
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);
286 window_create_override = -1;
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);
296 void gui_windows_deinit(void)
298 while (windows != NULL)
299 window_destroy(windows->data);
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);