4 Copyright (C) 2000 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
22 #include "module-formats.h"
31 #include "fe-windows.h"
32 #include "window-items.h"
33 #include "printtext.h"
35 void window_item_add(WINDOW_REC *window, WI_ITEM_REC *item, int automatic)
37 g_return_if_fail(window != NULL);
38 g_return_if_fail(item != NULL);
39 g_return_if_fail(item->window == NULL);
41 item->window = window;
43 if (window->items == NULL) {
44 window->active = item;
45 window->active_server = item->server;
48 if (!automatic || settings_get_bool("window_auto_change")) {
50 signal_emit("window changed automatic", 1, window);
51 window_set_active(window);
54 window->items = g_slist_append(window->items, item);
55 signal_emit("window item new", 2, window, item);
57 if (g_slist_length(window->items) == 1 ||
58 (!automatic && settings_get_bool("autofocus_new_items"))) {
59 window->active = NULL;
60 window_item_set_active(window, item);
64 void window_item_remove(WI_ITEM_REC *item)
68 g_return_if_fail(item != NULL);
70 window = window_item_window(item);
76 window->items = g_slist_remove(window->items, item);
78 if (window->active == item) {
79 window_item_set_active(window, window->items == NULL ? NULL :
83 signal_emit("window item remove", 2, window, item);
86 void window_item_destroy(WI_ITEM_REC *item)
90 window = window_item_window(item);
91 window_item_remove(item);
95 void window_item_change_server(WI_ITEM_REC *item, void *server)
99 g_return_if_fail(item != NULL);
101 window = window_item_window(item);
102 item->server = server;
104 signal_emit("window item server changed", 2, window, item);
105 if (window->active == item) window_change_server(window, item->server);
108 void window_item_set_active(WINDOW_REC *window, WI_ITEM_REC *item)
110 g_return_if_fail(window != NULL);
112 if (item != NULL && window_item_window(item) != window) {
113 /* move item to different window */
114 window_item_remove(item);
115 window_item_add(window, item, FALSE);
118 if (window->active != item) {
119 window->active = item;
120 if (item != NULL && window->active_server != item->server)
121 window_change_server(window, item->server);
122 signal_emit("window item changed", 2, window, item);
126 /* Return TRUE if `item' is the active window item in the window.
127 `item' can be NULL. */
128 int window_item_is_active(WI_ITEM_REC *item)
135 window = window_item_window(item);
139 return window->active == item;
142 void window_item_prev(WINDOW_REC *window)
147 g_return_if_fail(window != NULL);
150 for (tmp = window->items; tmp != NULL; tmp = tmp->next) {
151 WI_ITEM_REC *rec = tmp->data;
153 if (rec != window->active)
156 /* current channel. did we find anything?
157 if not, go to the last channel */
158 if (last != NULL) break;
163 window_item_set_active(window, last);
166 void window_item_next(WINDOW_REC *window)
172 g_return_if_fail(window != NULL);
174 next = NULL; gone = FALSE;
175 for (tmp = window->items; tmp != NULL; tmp = tmp->next) {
176 WI_ITEM_REC *rec = tmp->data;
178 if (rec == window->active)
182 /* found the next channel */
188 next = rec; /* fallback to first channel */
193 window_item_set_active(window, next);
196 WI_ITEM_REC *window_item_find_window(WINDOW_REC *window,
197 void *server, const char *name)
199 CHANNEL_REC *channel;
202 for (tmp = window->items; tmp != NULL; tmp = tmp->next) {
203 WI_ITEM_REC *rec = tmp->data;
205 if ((server == NULL || rec->server == server) &&
206 g_strcasecmp(name, rec->visible_name) == 0)
210 /* try with channel name too, it's not necessarily
211 same as visible_name (!channels) */
212 channel = channel_find(server, name);
213 if (channel != NULL && window_item_window(channel) == window)
214 return (WI_ITEM_REC *) channel;
219 /* Find wanted window item by name. `server' can be NULL. */
220 WI_ITEM_REC *window_item_find(void *server, const char *name)
225 g_return_val_if_fail(name != NULL, NULL);
227 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
228 WINDOW_REC *rec = tmp->data;
230 item = window_item_find_window(rec, server, name);
231 if (item != NULL) return item;
237 static int window_bind_has_sticky(WINDOW_REC *window)
241 for (tmp = window->bound_items; tmp != NULL; tmp = tmp->next) {
242 WINDOW_BIND_REC *rec = tmp->data;
251 void window_item_create(WI_ITEM_REC *item, int automatic)
254 WINDOW_BIND_REC *bind;
255 GSList *tmp, *sorted;
256 int clear_waiting, reuse_unused_windows;
258 g_return_if_fail(item != NULL);
260 reuse_unused_windows = settings_get_bool("reuse_unused_windows");
262 clear_waiting = TRUE;
264 sorted = windows_get_sorted();
265 for (tmp = sorted; tmp != NULL; tmp = tmp->next) {
266 WINDOW_REC *rec = tmp->data;
268 /* is item bound to this window? */
269 if (item->server != NULL) {
270 bind = window_bind_find(rec, item->server->tag,
274 window_bind_destroy(rec, bind);
276 clear_waiting = FALSE;
281 /* use this window IF:
282 - reuse_unused_windows is ON
283 - window has no existing items
285 - window has no sticky binds (/LAYOUT SAVEd)
286 - we already haven't found "good enough" window,
288 - this is the active window
289 - old window had some temporary bounds and this
292 if (reuse_unused_windows && rec->items == NULL &&
293 rec->name == NULL && !window_bind_has_sticky(rec) &&
294 (window == NULL || rec == active_win ||
295 window->bound_items != NULL))
298 g_slist_free(sorted);
300 if (window == NULL && !settings_get_bool("autocreate_windows")) {
301 /* never create new windows automatically */
305 if (window == NULL) {
306 /* create new window to use */
307 if (settings_get_bool("autocreate_split_windows")) {
308 signal_emit("gui window create override", 1,
311 window = window_create(item, automatic);
313 /* use existing window */
314 window_item_add(window, item, automatic);
318 window_bind_remove_unsticky(window);
321 static void signal_window_item_changed(WINDOW_REC *window, WI_ITEM_REC *item)
323 g_return_if_fail(window != NULL);
325 if (g_slist_length(window->items) > 1) {
326 /* default to printing "talking with ...",
327 you can override it it you wish */
328 printformat(item->server, item->visible_name,
329 MSGLEVEL_CLIENTNOTICE,
330 TXT_TALKING_WITH, item->visible_name);
334 void window_items_init(void)
336 settings_add_bool("lookandfeel", "reuse_unused_windows", FALSE);
337 settings_add_bool("lookandfeel", "autocreate_windows", TRUE);
338 settings_add_bool("lookandfeel", "autocreate_split_windows", FALSE);
339 settings_add_bool("lookandfeel", "autofocus_new_items", TRUE);
341 signal_add_last("window item changed", (SIGNAL_FUNC) signal_window_item_changed);
344 void window_items_deinit(void)
346 signal_remove("window item changed", (SIGNAL_FUNC) signal_window_item_changed);