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"
30 #include "fe-windows.h"
31 #include "window-items.h"
32 #include "printtext.h"
34 void window_item_add(WINDOW_REC *window, WI_ITEM_REC *item, int automatic)
36 g_return_if_fail(window != NULL);
37 g_return_if_fail(item != NULL);
38 g_return_if_fail(item->window == NULL);
40 item->window = window;
42 if (window->items == NULL) {
43 window->active = item;
44 window->active_server = item->server;
47 if (!automatic || settings_get_bool("window_auto_change")) {
49 signal_emit("window changed automatic", 1, window);
50 window_set_active(window);
53 window->items = g_slist_append(window->items, item);
54 signal_emit("window item new", 2, window, item);
56 if (g_slist_length(window->items) == 1 ||
57 (!automatic && settings_get_bool("autofocus_new_items"))) {
58 window->active = NULL;
59 window_item_set_active(window, item);
63 void window_item_remove(WI_ITEM_REC *item)
67 g_return_if_fail(item != NULL);
69 window = window_item_window(item);
75 window->items = g_slist_remove(window->items, item);
77 if (window->active == item) {
78 window_item_set_active(window, window->items == NULL ? NULL :
82 signal_emit("window item remove", 2, window, item);
85 void window_item_destroy(WI_ITEM_REC *item)
89 window = window_item_window(item);
90 window_item_remove(item);
94 void window_item_change_server(WI_ITEM_REC *item, void *server)
98 g_return_if_fail(item != NULL);
100 window = window_item_window(item);
101 item->server = server;
103 signal_emit("window item server changed", 2, window, item);
104 if (window->active == item) window_change_server(window, item->server);
107 void window_item_set_active(WINDOW_REC *window, WI_ITEM_REC *item)
109 g_return_if_fail(window != NULL);
111 if (item != NULL && window_item_window(item) != window) {
112 /* move item to different window */
113 window_item_remove(item);
114 window_item_add(window, item, FALSE);
117 if (window->active != item) {
118 window->active = item;
119 if (item != NULL && window->active_server != item->server)
120 window_change_server(window, item->server);
121 signal_emit("window item changed", 2, window, item);
125 /* Return TRUE if `item' is the active window item in the window.
126 `item' can be NULL. */
127 int window_item_is_active(WI_ITEM_REC *item)
134 window = window_item_window(item);
138 return window->active == item;
141 void window_item_prev(WINDOW_REC *window)
146 g_return_if_fail(window != NULL);
149 for (tmp = window->items; tmp != NULL; tmp = tmp->next) {
150 WI_ITEM_REC *rec = tmp->data;
152 if (rec != window->active)
155 /* current channel. did we find anything?
156 if not, go to the last channel */
157 if (last != NULL) break;
162 window_item_set_active(window, last);
165 void window_item_next(WINDOW_REC *window)
171 g_return_if_fail(window != NULL);
173 next = NULL; gone = FALSE;
174 for (tmp = window->items; tmp != NULL; tmp = tmp->next) {
175 WI_ITEM_REC *rec = tmp->data;
177 if (rec == window->active)
181 /* found the next channel */
187 next = rec; /* fallback to first channel */
192 window_item_set_active(window, next);
195 WI_ITEM_REC *window_item_find_window(WINDOW_REC *window,
196 void *server, const char *name)
200 for (tmp = window->items; tmp != NULL; tmp = tmp->next) {
201 WI_ITEM_REC *rec = tmp->data;
203 if ((server == NULL || rec->server == server) &&
204 g_strcasecmp(name, rec->name) == 0) return rec;
210 /* Find wanted window item by name. `server' can be NULL. */
211 WI_ITEM_REC *window_item_find(void *server, const char *name)
216 g_return_val_if_fail(name != NULL, NULL);
218 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
219 WINDOW_REC *rec = tmp->data;
221 item = window_item_find_window(rec, server, name);
222 if (item != NULL) return item;
228 static int window_bind_has_sticky(WINDOW_REC *window)
232 for (tmp = window->bound_items; tmp != NULL; tmp = tmp->next) {
233 WINDOW_BIND_REC *rec = tmp->data;
242 void window_item_create(WI_ITEM_REC *item, int automatic)
245 WINDOW_BIND_REC *bind;
246 GSList *tmp, *sorted;
247 int clear_waiting, reuse_unused_windows;
249 g_return_if_fail(item != NULL);
251 reuse_unused_windows = settings_get_bool("reuse_unused_windows");
253 clear_waiting = TRUE;
255 sorted = windows_get_sorted();
256 for (tmp = sorted; tmp != NULL; tmp = tmp->next) {
257 WINDOW_REC *rec = tmp->data;
259 /* is item bound to this window? */
260 if (item->server != NULL) {
261 bind = window_bind_find(rec, item->server->tag,
265 window_bind_destroy(rec, bind);
267 clear_waiting = FALSE;
272 /* use this window IF:
273 - reuse_unused_windows is ON
274 - window has no existing items
276 - window has no sticky binds (/LAYOUT SAVEd)
277 - we already haven't found "good enough" window,
279 - this is the active window
280 - old window had some temporary bounds and this
283 if (reuse_unused_windows && rec->items == NULL &&
284 rec->name == NULL && !window_bind_has_sticky(rec) &&
285 (window == NULL || rec == active_win ||
286 window->bound_items != NULL))
289 g_slist_free(sorted);
291 if (window == NULL && !settings_get_bool("autocreate_windows")) {
292 /* never create new windows automatically */
296 if (window == NULL) {
297 /* create new window to use */
298 if (settings_get_bool("autocreate_split_windows")) {
299 signal_emit("gui window create override", 1,
302 window = window_create(item, automatic);
304 /* use existing window */
305 window_item_add(window, item, automatic);
309 window_bind_remove_unsticky(window);
312 static void signal_window_item_changed(WINDOW_REC *window, WI_ITEM_REC *item)
314 g_return_if_fail(window != NULL);
316 if (g_slist_length(window->items) > 1) {
317 /* default to printing "talking with ...",
318 you can override it it you wish */
319 printformat(item->server, item->name, MSGLEVEL_CLIENTNOTICE,
320 TXT_TALKING_WITH, item->name);
324 void window_items_init(void)
326 settings_add_bool("lookandfeel", "reuse_unused_windows", FALSE);
327 settings_add_bool("lookandfeel", "autocreate_windows", TRUE);
328 settings_add_bool("lookandfeel", "autocreate_split_windows", FALSE);
329 settings_add_bool("lookandfeel", "autofocus_new_items", TRUE);
331 signal_add_last("window item changed", (SIGNAL_FUNC) signal_window_item_changed);
334 void window_items_deinit(void)
336 signal_remove("window item changed", (SIGNAL_FUNC) signal_window_item_changed);