2 mainwindows-layout.c : irssi
4 Copyright (C) 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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "lib-config/iconfig.h"
27 #include "mainwindows.h"
28 #include "gui-windows.h"
29 #include "textbuffer-view.h"
31 static void sig_layout_window_save(WINDOW_REC *window, CONFIG_NODE *node)
36 gui = WINDOW_GUI(window);
38 iconfig_node_set_bool(node, "sticky", TRUE);
39 active = gui->parent->active;
41 iconfig_node_set_int(node, "parent", active->refnum);
45 iconfig_node_set_bool(node, "scroll", gui->scroll);
48 static void sig_layout_window_restore(WINDOW_REC *window, CONFIG_NODE *node)
53 gui = WINDOW_GUI(window);
55 parent = window_find_refnum(config_node_get_int(node, "parent", -1));
57 gui_window_reparent(window, WINDOW_MAIN(parent));
59 if (config_node_get_bool(node, "sticky", FALSE))
60 gui_window_set_sticky(window);
61 if (config_node_get_str(node, "scroll", NULL) != NULL) {
62 gui->use_scroll = TRUE;
63 gui->scroll = config_node_get_bool(node, "scroll", TRUE);
64 textbuffer_view_set_scroll(gui->view, gui->scroll);
68 static void main_window_save(MAIN_WINDOW_REC *window, CONFIG_NODE *node)
70 char num[MAX_INT_STRLEN];
72 ltoa(num, window->active->refnum);
73 node = config_node_section(node, num, NODE_TYPE_BLOCK);
75 iconfig_node_set_int(node, "first_line", window->first_line);
76 iconfig_node_set_int(node, "lines", window->height);
79 static void sig_layout_save(void)
83 iconfig_set_str(NULL, "mainwindows", NULL);
84 node = iconfig_node_traverse("mainwindows", TRUE);
86 g_slist_foreach(mainwindows, (GFunc) main_window_save, node);
89 static int window_node_cmp(CONFIG_NODE *n1, CONFIG_NODE *n2)
91 return config_node_get_int(n1, "first_line", 0) >
92 config_node_get_int(n2, "first_line", 0) ? -1 : 1;
95 /* Returns list of mainwindow nodes sorted by first_line
96 (lowest in screen first) */
97 static GSList *get_sorted_windows_config(CONFIG_NODE *node)
102 tmp = config_node_first(node->value);
103 for (; tmp != NULL; tmp = config_node_next(tmp)) {
104 output = g_slist_insert_sorted(output, tmp->data,
105 (GCompareFunc) window_node_cmp);
111 static void sig_layout_restore(void)
113 MAIN_WINDOW_REC *lower_window;
116 GSList *tmp, *sorted_config;
117 int avail_height, height, *heights;
118 int i, lower_size, windows_count, diff;
120 node = iconfig_node_traverse("mainwindows", FALSE);
121 if (node == NULL) return;
123 sorted_config = get_sorted_windows_config(node);
124 windows_count = g_slist_length(sorted_config);
126 /* calculate the saved terminal height */
127 avail_height = term_height -
128 screen_reserved_top - screen_reserved_bottom;
130 heights = g_new0(int, windows_count);
131 for (i = 0, tmp = sorted_config; tmp != NULL; tmp = tmp->next, i++) {
132 CONFIG_NODE *node = tmp->data;
134 heights[i] = config_node_get_int(node, "lines", 0);
135 height += heights[i];
138 if (avail_height <= (WINDOW_MIN_SIZE*2)+1) {
139 /* we can fit only one window to screen -
140 give it all the height we can */
142 heights[0] = avail_height;
143 } else if (height != avail_height) {
144 /* Terminal's height is different from the saved one.
145 Resize the windows so they fit to screen. */
146 while (height > avail_height &&
147 windows_count*(WINDOW_MIN_SIZE+1) > avail_height) {
148 /* all windows can't fit into screen,
149 remove the lowest ones */
153 /* try to keep the windows' size about the same in percents */
154 for (i = 0; i < windows_count; i++) {
155 int size = avail_height*heights[i]/height;
156 if (size < WINDOW_MIN_SIZE+1)
157 size = WINDOW_MIN_SIZE+1;
161 /* give/remove the last bits */
163 for (i = 0; i < windows_count; i++)
164 height += heights[i];
166 diff = height < avail_height ? 1 : -1;
167 for (i = 0; height != avail_height; i++) {
168 if (i == windows_count)
171 if (heights[i] > WINDOW_MIN_SIZE+1) {
178 /* create all the visible windows with correct size */
179 lower_window = NULL; lower_size = 0;
180 for (i = 0, tmp = sorted_config; i < windows_count; tmp = tmp->next, i++) {
181 CONFIG_NODE *node = tmp->data;
183 /* create a new window + mainwindow */
184 signal_emit("gui window create override", 1,
187 window = window_create(NULL, TRUE);
188 window_set_refnum(window, atoi(node->key));
191 mainwindow_set_size(lower_window, lower_size, FALSE);
193 window_set_active(window);
194 active_mainwin = WINDOW_MAIN(window);
196 lower_window = WINDOW_MAIN(window);
197 lower_size = heights[i];
198 if (lower_size < WINDOW_MIN_SIZE+1)
199 lower_size = WINDOW_MIN_SIZE+1;
201 g_slist_free(sorted_config);
205 mainwindow_set_size(lower_window, lower_size, FALSE);
208 static void sig_layout_reset(void)
210 iconfig_set_str(NULL, "mainwindows", NULL);
213 void mainwindows_layout_init(void)
215 signal_add("layout save window", (SIGNAL_FUNC) sig_layout_window_save);
216 signal_add("layout restore window", (SIGNAL_FUNC) sig_layout_window_restore);
217 signal_add("layout save", (SIGNAL_FUNC) sig_layout_save);
218 signal_add_first("layout restore", (SIGNAL_FUNC) sig_layout_restore);
219 signal_add("layout reset", (SIGNAL_FUNC) sig_layout_reset);
222 void mainwindows_layout_deinit(void)
224 signal_remove("layout save window", (SIGNAL_FUNC) sig_layout_window_save);
225 signal_remove("layout restore window", (SIGNAL_FUNC) sig_layout_window_restore);
226 signal_remove("layout save", (SIGNAL_FUNC) sig_layout_save);
227 signal_remove("layout restore", (SIGNAL_FUNC) sig_layout_restore);
228 signal_remove("layout reset", (SIGNAL_FUNC) sig_layout_reset);