Added SILC Thread Queue API
[runtime.git] / apps / irssi / src / fe-text / mainwindows.c
1 /*
2  mainwindows.c : irssi
3
4     Copyright (C) 1999-2000 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
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
19 */
20
21 #include "module.h"
22 #include "module-formats.h"
23 #include "signals.h"
24 #include "commands.h"
25 #include "levels.h"
26 #include "misc.h"
27 #include "settings.h"
28 #include "printtext.h"
29
30 #include "term.h"
31 #include "gui-windows.h"
32
33 #define NEW_WINDOW_SIZE (WINDOW_MIN_SIZE + 1)
34
35 GSList *mainwindows;
36 MAIN_WINDOW_REC *active_mainwin;
37
38 int screen_reserved_top, screen_reserved_bottom;
39 static int old_screen_width, old_screen_height;
40
41 #define mainwindow_create_screen(window) \
42         term_window_create(0, \
43                            (window)->first_line + (window)->statusbar_lines_top, \
44                            (window)->width, \
45                            (window)->height - (window)->statusbar_lines)
46
47 #define mainwindow_set_screen_size(window) \
48         term_window_move((window)->screen_win, 0, \
49                          (window)->first_line + (window)->statusbar_lines_top, \
50                          (window)->width, \
51                          (window)->height - (window)->statusbar_lines);
52
53
54 static MAIN_WINDOW_REC *find_window_with_room(void)
55 {
56         MAIN_WINDOW_REC *biggest_rec;
57         GSList *tmp;
58         int space, biggest;
59
60         biggest = 0; biggest_rec = NULL;
61         for (tmp = mainwindows; tmp != NULL; tmp = tmp->next) {
62                 MAIN_WINDOW_REC *rec = tmp->data;
63
64                 space = MAIN_WINDOW_TEXT_HEIGHT(rec);
65                 if (space >= WINDOW_MIN_SIZE+NEW_WINDOW_SIZE && space > biggest) {
66                         biggest = space;
67                         biggest_rec = rec;
68                 }
69         }
70
71         return biggest_rec;
72 }
73
74 #define window_size_equals(window, mainwin) \
75         ((window)->width == (mainwin)->width && \
76          (window)->height == MAIN_WINDOW_TEXT_HEIGHT(mainwin))
77
78 static void mainwindow_resize_windows(MAIN_WINDOW_REC *window)
79 {
80         GSList *tmp;
81         int resized;
82
83         mainwindow_set_screen_size(window);
84
85         resized = FALSE;
86         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
87                 WINDOW_REC *rec = tmp->data;
88
89                 if (rec->gui_data != NULL &&
90                     WINDOW_GUI(rec)->parent == window &&
91                     !window_size_equals(rec, window)) {
92                         resized = TRUE;
93                         gui_window_resize(rec, window->width,
94                                           MAIN_WINDOW_TEXT_HEIGHT(window));
95                 }
96         }
97
98         if (resized)
99                 signal_emit("mainwindow resized", 1, window);
100 }
101
102 static void mainwindow_resize(MAIN_WINDOW_REC *window, int xdiff, int ydiff)
103 {
104         if (quitting || (xdiff == 0 && ydiff == 0))
105                 return;
106
107         window->width += xdiff;
108         window->height = window->last_line-window->first_line+1;
109         window->size_dirty = TRUE;
110 }
111
112 static GSList *get_sticky_windows_sorted(MAIN_WINDOW_REC *mainwin)
113 {
114         GSList *tmp, *list;
115
116         list = NULL;
117         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
118                 WINDOW_REC *rec = tmp->data;
119
120                 if (WINDOW_GUI(rec)->sticky && WINDOW_MAIN(rec) == mainwin) {
121                         list = g_slist_insert_sorted(list, rec, (GCompareFunc)
122                                                      window_refnum_cmp);
123                 }
124         }
125
126         return list;
127 }
128
129 void mainwindow_change_active(MAIN_WINDOW_REC *mainwin,
130                               WINDOW_REC *skip_window)
131 {
132         WINDOW_REC *window, *other;
133         GSList *tmp;
134
135         mainwin->active = NULL;
136         if (mainwin->sticky_windows) {
137                 /* sticky window */
138                 tmp = get_sticky_windows_sorted(mainwin);
139                 window = tmp->data;
140                 if (window == skip_window) {
141                         window = tmp->next == NULL ? NULL :
142                                 tmp->next->data;
143                 }
144                 g_slist_free(tmp);
145
146                 if (window != NULL) {
147                         window_set_active(window);
148                         return;
149                 }
150         }
151
152         other = NULL;
153         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
154                 WINDOW_REC *rec = tmp->data;
155
156                 if (rec != skip_window) {
157                         other = rec;
158                         break;
159                 }
160         }
161
162         window_set_active(other);
163         if (mainwindows->next != NULL)
164                 mainwindow_destroy(mainwin);
165 }
166
167 void mainwindows_recreate(void)
168 {
169         GSList *tmp;
170
171         for (tmp = mainwindows; tmp != NULL; tmp = tmp->next) {
172                 MAIN_WINDOW_REC *rec = tmp->data;
173
174                 rec->screen_win = mainwindow_create_screen(rec);
175                 rec->dirty = TRUE;
176                 textbuffer_view_set_window(WINDOW_GUI(rec->active)->view,
177                                            rec->screen_win);
178         }
179 }
180
181 MAIN_WINDOW_REC *mainwindow_create(void)
182 {
183         MAIN_WINDOW_REC *rec, *parent;
184         int space;
185
186         rec = g_new0(MAIN_WINDOW_REC, 1);
187         rec->dirty = TRUE;
188         rec->width = term_width;
189
190         if (mainwindows == NULL) {
191                 active_mainwin = rec;
192
193                 rec->first_line = screen_reserved_top;
194                 rec->last_line = term_height-1 - screen_reserved_bottom;
195                 rec->height = rec->last_line-rec->first_line+1;
196         } else {
197                 parent = WINDOW_MAIN(active_win);
198                 if (MAIN_WINDOW_TEXT_HEIGHT(parent) <
199                     WINDOW_MIN_SIZE+NEW_WINDOW_SIZE)
200                         parent = find_window_with_room();
201                 if (parent == NULL)
202                         return NULL; /* not enough space */
203
204                 space = parent->height / 2;
205                 rec->first_line = parent->first_line;
206                 rec->last_line = rec->first_line + space;
207                 rec->height = rec->last_line-rec->first_line+1;
208
209                 parent->first_line = rec->last_line+1;
210                 parent->height = parent->last_line-parent->first_line+1;
211
212                 mainwindow_resize(parent, 0, -space-1);
213         }
214
215         rec->screen_win = mainwindow_create_screen(rec);
216         term_refresh(NULL);
217
218         mainwindows = g_slist_append(mainwindows, rec);
219         signal_emit("mainwindow created", 1, rec);
220         return rec;
221 }
222
223 static MAIN_WINDOW_REC *mainwindows_find_lower(int line)
224 {
225         MAIN_WINDOW_REC *best;
226         GSList *tmp;
227
228         best = NULL;
229         for (tmp = mainwindows; tmp != NULL; tmp = tmp->next) {
230                 MAIN_WINDOW_REC *rec = tmp->data;
231
232                 if (rec->first_line > line &&
233                     (best == NULL || rec->first_line < best->first_line))
234                         best = rec;
235         }
236
237         return best;
238 }
239
240 static MAIN_WINDOW_REC *mainwindows_find_upper(int line)
241 {
242         MAIN_WINDOW_REC *best;
243         GSList *tmp;
244
245         best = NULL;
246         for (tmp = mainwindows; tmp != NULL; tmp = tmp->next) {
247                 MAIN_WINDOW_REC *rec = tmp->data;
248
249                 if (rec->last_line < line &&
250                     (best == NULL || rec->last_line > best->last_line))
251                         best = rec;
252         }
253
254         return best;
255 }
256
257 static void mainwindows_add_space(int first_line, int last_line)
258 {
259         MAIN_WINDOW_REC *rec;
260         int size;
261
262         if (last_line < first_line)
263                 return;
264
265         size = last_line-first_line+1;
266
267         rec = mainwindows_find_lower(last_line);
268         if (rec != NULL) {
269                 rec->first_line = first_line;
270                 mainwindow_resize(rec, 0, size);
271                 return;
272         }
273
274         rec = mainwindows_find_upper(first_line);
275         if (rec != NULL) {
276                 rec->last_line = last_line;
277                 mainwindow_resize(rec, 0, size);
278         }
279 }
280
281 static void gui_windows_remove_parent(MAIN_WINDOW_REC *window)
282 {
283         MAIN_WINDOW_REC *new_parent;
284         GSList *tmp;
285
286         new_parent = mainwindows->data;
287         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
288                 WINDOW_REC *rec = tmp->data;
289
290                 if (rec->gui_data != NULL && WINDOW_MAIN(rec) == window)
291                         gui_window_reparent(rec, new_parent);
292         }
293 }
294
295 void mainwindow_destroy(MAIN_WINDOW_REC *window)
296 {
297         g_return_if_fail(window != NULL);
298
299         mainwindows = g_slist_remove(mainwindows, window);
300         signal_emit("mainwindow destroyed", 1, window);
301
302         term_window_destroy(window->screen_win);
303
304         if (mainwindows != NULL) {
305                 gui_windows_remove_parent(window);
306                 if (!quitting) {
307                         mainwindows_add_space(window->first_line,
308                                               window->last_line);
309                         mainwindows_redraw();
310                 }
311         }
312
313         g_free(window);
314
315         if (active_mainwin == window) active_mainwin = NULL;
316 }
317
318 void mainwindows_redraw(void)
319 {
320         GSList *tmp;
321
322         irssi_set_dirty();
323         for (tmp = mainwindows; tmp != NULL; tmp = tmp->next) {
324                 MAIN_WINDOW_REC *rec = tmp->data;
325
326                 rec->dirty = TRUE;
327         }
328 }
329
330 static int mainwindows_compare(MAIN_WINDOW_REC *w1, MAIN_WINDOW_REC *w2)
331 {
332         return w1->first_line < w2->first_line ? -1 : 1;
333 }
334
335 static int mainwindows_compare_reverse(MAIN_WINDOW_REC *w1, MAIN_WINDOW_REC *w2)
336 {
337         return w1->first_line < w2->first_line ? 1 : -1;
338 }
339
340 GSList *mainwindows_get_sorted(int reverse)
341 {
342         GSList *tmp, *list;
343
344         list = NULL;
345         for (tmp = mainwindows; tmp != NULL; tmp = tmp->next) {
346                 list = g_slist_insert_sorted(list, tmp->data, (GCompareFunc)
347                                              (reverse ? mainwindows_compare_reverse : mainwindows_compare));
348         }
349
350         return list;
351 }
352
353 static void mainwindows_resize_smaller(int xdiff, int ydiff)
354 {
355         MAIN_WINDOW_REC *rec;
356         GSList *sorted, *tmp;
357         int space;
358
359         for (;;) {
360                 sorted = mainwindows_get_sorted(TRUE);
361                 space = 0;
362                 for (tmp = mainwindows; tmp != NULL; tmp = tmp->next) {
363                         rec = tmp->data;
364                         space += MAIN_WINDOW_TEXT_HEIGHT(rec)-WINDOW_MIN_SIZE;
365                 }
366
367                 if (space >= -ydiff)
368                         break;
369
370                 rec = sorted->data;
371                 sorted = g_slist_remove(sorted, rec);
372
373                 if (sorted != NULL) {
374                         /* terminal is too small - destroy the
375                            uppest window and try again */
376                         mainwindow_destroy(rec);
377                 } else {
378                         /* only one window in screen.. just force the resize */
379                         rec->last_line += ydiff;
380                         mainwindow_resize(rec, xdiff, ydiff);
381                         return;
382                 }
383         }
384
385         /* resize windows that have space */
386         for (tmp = sorted; tmp != NULL && ydiff < 0; tmp = tmp->next) {
387                 rec = tmp->data;
388
389                 space = MAIN_WINDOW_TEXT_HEIGHT(rec)-WINDOW_MIN_SIZE;
390                 if (space <= 0) {
391                         mainwindow_resize(rec, xdiff, 0);
392
393                         rec->first_line += ydiff;
394                         rec->last_line += ydiff;
395                         signal_emit("mainwindow moved", 1, rec);
396                         continue;
397                 }
398
399                 if (space <= 0) space = 1;
400                 if (space > -ydiff) space = -ydiff;
401                 rec->last_line += ydiff;
402                 ydiff += space;
403                 rec->first_line += ydiff;
404
405                 mainwindow_resize(rec, xdiff, -space);
406         }
407
408         if (xdiff != 0) {
409                 while (tmp != NULL) {
410                         mainwindow_resize(tmp->data, xdiff, 0);
411                         tmp = tmp->next;
412                 }
413         }
414
415         g_slist_free(sorted);
416 }
417
418 static void mainwindows_resize_bigger(int xdiff, int ydiff)
419 {
420         GSList *sorted, *tmp;
421         int moved, space;
422
423         sorted = mainwindows_get_sorted(FALSE);
424         moved = 0;
425         for (tmp = sorted; tmp != NULL; tmp = tmp->next) {
426                 MAIN_WINDOW_REC *rec = tmp->data;
427
428                 space = MAIN_WINDOW_TEXT_HEIGHT(rec)-WINDOW_MIN_SIZE;
429                 if (ydiff == 0 || (space >= 0 && tmp->next != NULL)) {
430                         mainwindow_resize(rec, xdiff, 0);
431                         if (moved > 0) {
432                                 rec->first_line += moved;
433                                 rec->last_line += moved;
434                                 signal_emit("mainwindow moved", 1, rec);
435                         }
436                         continue;
437                 }
438
439                 if (space < 0 && tmp->next != NULL) {
440                         /* space below minimum */
441                         space = -space;
442                         if (space > ydiff) space = ydiff;
443                 } else {
444                         /* lowest window - give all the extra space for it */
445                         space = ydiff;
446                 }
447                 ydiff -= space;
448                 rec->first_line += moved;
449                 moved += space;
450                 rec->last_line += moved;
451
452                 mainwindow_resize(rec, xdiff, space);
453         }
454         g_slist_free(sorted);
455 }
456
457 void mainwindows_resize_horiz(int xdiff)
458 {
459         GSList *tmp;
460
461         for (tmp = mainwindows; tmp != NULL; tmp = tmp->next) {
462                 MAIN_WINDOW_REC *rec = tmp->data;
463
464                 mainwindow_resize(rec, xdiff, 0);
465         }
466 }
467
468 void mainwindows_resize(int width, int height)
469 {
470         int xdiff, ydiff;
471
472         xdiff = width-old_screen_width;
473         ydiff = height-old_screen_height;
474         old_screen_width = width;
475         old_screen_height = height;
476
477         if (ydiff < 0)
478                 mainwindows_resize_smaller(xdiff, ydiff);
479         else if (ydiff > 0)
480                 mainwindows_resize_bigger(xdiff, ydiff);
481         else if (xdiff != 0)
482                 mainwindows_resize_horiz(xdiff);
483
484         signal_emit("terminal resized", 0);
485
486         irssi_redraw();
487 }
488
489 int mainwindows_reserve_lines(int top, int bottom)
490 {
491         MAIN_WINDOW_REC *window;
492         int ret;
493
494         ret = -1;
495         if (top != 0) {
496                 g_return_val_if_fail(top > 0 || screen_reserved_top > top, -1);
497
498                 ret = screen_reserved_top;
499                 screen_reserved_top += top;
500
501                 window = mainwindows_find_lower(-1);
502                 if (window != NULL) {
503                         window->first_line += top;
504                         mainwindow_resize(window, 0, -top);
505                 }
506         }
507
508         if (bottom != 0) {
509                 g_return_val_if_fail(bottom > 0 || screen_reserved_bottom > bottom, -1);
510
511                 ret = screen_reserved_bottom;
512                 screen_reserved_bottom += bottom;
513
514                 window = mainwindows_find_upper(term_height);
515                 if (window != NULL) {
516                         window->last_line -= bottom;
517                         mainwindow_resize(window, 0, -bottom);
518                 }
519         }
520
521         return ret;
522 }
523
524 int mainwindow_set_statusbar_lines(MAIN_WINDOW_REC *window,
525                                    int top, int bottom)
526 {
527         int ret;
528
529         ret = -1;
530         if (top != 0) {
531                 ret = window->statusbar_lines_top;
532                 window->statusbar_lines_top += top;
533                 window->statusbar_lines += top;
534         }
535
536         if (bottom != 0) {
537                 ret = window->statusbar_lines_bottom;
538                 window->statusbar_lines_bottom += bottom;
539                 window->statusbar_lines += bottom;
540         }
541
542         if (top+bottom != 0)
543                 window->size_dirty = TRUE;
544
545         return ret;
546 }
547
548 static void mainwindows_resize_two(MAIN_WINDOW_REC *grow_win,
549                                    MAIN_WINDOW_REC *shrink_win, int count)
550 {
551         irssi_set_dirty();
552
553         mainwindow_resize(grow_win, 0, count);
554         mainwindow_resize(shrink_win, 0, -count);
555         grow_win->dirty = TRUE;
556         shrink_win->dirty = TRUE;
557 }
558
559 static int try_shrink_lower(MAIN_WINDOW_REC *window, int count)
560 {
561         MAIN_WINDOW_REC *shrink_win;
562
563         shrink_win = mainwindows_find_lower(window->last_line);
564         if (shrink_win != NULL &&
565             MAIN_WINDOW_TEXT_HEIGHT(shrink_win)-count >= WINDOW_MIN_SIZE) {
566                 window->last_line += count;
567                 shrink_win->first_line += count;
568                 mainwindows_resize_two(window, shrink_win, count);
569                 return TRUE;
570         }
571
572         return FALSE;
573 }
574
575 static int try_shrink_upper(MAIN_WINDOW_REC *window, int count)
576 {
577         MAIN_WINDOW_REC *shrink_win;
578
579         shrink_win = mainwindows_find_upper(window->first_line);
580         if (shrink_win != NULL &&
581             MAIN_WINDOW_TEXT_HEIGHT(shrink_win)-count >= WINDOW_MIN_SIZE) {
582                 window->first_line -= count;
583                 shrink_win->last_line -= count;
584                 mainwindows_resize_two(window, shrink_win, count);
585                 return TRUE;
586         }
587
588         return FALSE;
589 }
590
591 static int mainwindow_grow(MAIN_WINDOW_REC *window, int count,
592                            int resize_lower)
593 {
594         if (!resize_lower || !try_shrink_lower(window, count)) {
595                 if (!try_shrink_upper(window, count)) {
596                         if (resize_lower || !try_shrink_lower(window, count))
597                                 return FALSE;
598                 }
599         }
600
601         return TRUE;
602 }
603
604 static int try_grow_lower(MAIN_WINDOW_REC *window, int count)
605 {
606         MAIN_WINDOW_REC *grow_win;
607
608         grow_win = mainwindows_find_lower(window->last_line);
609         if (grow_win != NULL) {
610                 window->last_line -= count;
611                 grow_win->first_line -= count;
612                 mainwindows_resize_two(grow_win, window, count);
613         }
614
615         return grow_win != NULL;
616 }
617
618 static int try_grow_upper(MAIN_WINDOW_REC *window, int count)
619 {
620         MAIN_WINDOW_REC *grow_win;
621
622         grow_win = mainwindows_find_upper(window->first_line);
623         if (grow_win != NULL) {
624                 window->first_line += count;
625                 grow_win->last_line += count;
626                 mainwindows_resize_two(grow_win, window, count);
627         }
628
629         return grow_win != NULL;
630 }
631
632 static int mainwindow_shrink(MAIN_WINDOW_REC *window, int count, int resize_lower)
633 {
634         if (MAIN_WINDOW_TEXT_HEIGHT(window)-count < WINDOW_MIN_SIZE)
635                 return FALSE;
636
637         if (!resize_lower || !try_grow_lower(window, count)) {
638                 if (!try_grow_upper(window, count)) {
639                         if (resize_lower || !try_grow_lower(window, count))
640                                 return FALSE;
641                 }
642         }
643
644         return TRUE;
645 }
646
647 /* Change the window height - the height includes the lines needed for
648    statusbars. If resize_lower is TRUE, the lower window is first tried
649    to be resized instead of upper window. */
650 void mainwindow_set_size(MAIN_WINDOW_REC *window, int height, int resize_lower)
651 {
652         height -= window->height;
653         if (height < 0)
654                 mainwindow_shrink(window, -height, resize_lower);
655         else
656                 mainwindow_grow(window, height, resize_lower);
657 }
658
659 void mainwindows_redraw_dirty(void)
660 {
661         GSList *tmp;
662
663         for (tmp = mainwindows; tmp != NULL; tmp = tmp->next) {
664                 MAIN_WINDOW_REC *rec = tmp->data;
665
666                 if (rec->size_dirty) {
667                         rec->size_dirty = FALSE;
668                         mainwindow_resize_windows(rec);
669                 }
670                 if (rec->dirty) {
671                         rec->dirty = FALSE;
672                         gui_window_redraw(rec->active);
673                 }
674         }
675 }
676
677 /* SYNTAX: WINDOW GROW [<lines>] */
678 static void cmd_window_grow(const char *data)
679 {
680         MAIN_WINDOW_REC *window;
681         int count;
682
683         count = *data == '\0' ? 1 : atoi(data);
684         window = WINDOW_MAIN(active_win);
685
686         if (!mainwindow_grow(window, count, FALSE)) {
687                 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
688                                    TXT_WINDOW_TOO_SMALL);
689         }
690 }
691
692 /* SYNTAX: WINDOW SHRINK [<lines>] */
693 static void cmd_window_shrink(const char *data)
694 {
695         int count;
696
697         count = *data == '\0' ? 1 : atoi(data);
698         if (!mainwindow_shrink(WINDOW_MAIN(active_win), count, FALSE)) {
699                 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
700                                    TXT_WINDOW_TOO_SMALL);
701         }
702 }
703
704 /* SYNTAX: WINDOW SIZE <lines> */
705 static void cmd_window_size(const char *data)
706 {
707         char sizestr[MAX_INT_STRLEN];
708         int size;
709
710         if (!is_numeric(data, 0)) return;
711         size = atoi(data);
712
713         size -= WINDOW_MAIN(active_win)->height -
714                 WINDOW_MAIN(active_win)->statusbar_lines;
715         if (size == 0) return;
716
717         ltoa(sizestr, size < 0 ? -size : size);
718         if (size < 0)
719                 cmd_window_shrink(sizestr);
720         else
721                 cmd_window_grow(sizestr);
722 }
723
724 /* SYNTAX: WINDOW BALANCE */
725 static void cmd_window_balance(void)
726 {
727         GSList *sorted, *tmp;
728         int avail_size, unit_size, bigger_units;
729         int windows, last_line, old_size;
730
731         windows = g_slist_length(mainwindows);
732         if (windows == 1) return;
733
734         avail_size = term_height - screen_reserved_top-screen_reserved_bottom;
735         unit_size = avail_size/windows;
736         bigger_units = avail_size%windows;
737
738         sorted = mainwindows_get_sorted(FALSE);
739         last_line = screen_reserved_top;
740         for (tmp = sorted; tmp != NULL; tmp = tmp->next) {
741                 MAIN_WINDOW_REC *rec = tmp->data;
742
743                 old_size = rec->height;
744                 rec->first_line = last_line;
745                 rec->last_line = rec->first_line + unit_size-1;
746
747                 if (bigger_units > 0) {
748                         rec->last_line++;
749                         bigger_units--;
750                 }
751
752                 rec->height = rec->last_line-rec->first_line+1;
753                 last_line = rec->last_line+1;
754
755                 mainwindow_resize(rec, 0, rec->height-old_size);
756         }
757         g_slist_free(sorted);
758
759         mainwindows_redraw();
760 }
761
762 /* SYNTAX: WINDOW HIDE [<number>|<name>] */
763 static void cmd_window_hide(const char *data)
764 {
765         WINDOW_REC *window;
766
767         if (mainwindows->next == NULL) {
768                 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
769                                    TXT_CANT_HIDE_LAST);
770                 return;
771         }
772
773         if (*data == '\0')
774                 window = active_win;
775         else if (is_numeric(data, 0)) {
776                 window = window_find_refnum(atoi(data));
777                 if (window == NULL) {
778                         printformat_window(active_win, MSGLEVEL_CLIENTERROR,
779                                            TXT_REFNUM_NOT_FOUND, data);
780                 }
781         } else {
782                 window = window_find_item(active_win->active_server, data);
783         }
784
785         if (window == NULL || !is_window_visible(window))
786                 return;
787
788         if (WINDOW_MAIN(window)->sticky_windows) {
789                 printformat_window(active_win, MSGLEVEL_CLIENTERROR,
790                                    TXT_CANT_HIDE_STICKY_WINDOWS);
791                 return;
792         }
793
794         mainwindow_destroy(WINDOW_MAIN(window));
795
796         if (active_mainwin == NULL) {
797                 active_mainwin = WINDOW_MAIN(active_win);
798                 window_set_active(active_mainwin->active);
799         }
800 }
801
802 /* SYNTAX: WINDOW SHOW <number>|<name> */
803 static void cmd_window_show(const char *data)
804 {
805         MAIN_WINDOW_REC *parent;
806         WINDOW_REC *window;
807
808         if (*data == '\0') cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
809
810         if (is_numeric(data, '\0')) {
811                 window = window_find_refnum(atoi(data));
812                 if (window == NULL) {
813                         printformat_window(active_win, MSGLEVEL_CLIENTERROR,
814                                            TXT_REFNUM_NOT_FOUND, data);
815                 }
816         } else {
817                 window = window_find_item(active_win->active_server, data);
818         }
819
820         if (window == NULL || is_window_visible(window))
821                 return;
822
823         if (WINDOW_MAIN(window)->sticky_windows) {
824                 printformat_window(active_win, MSGLEVEL_CLIENTERROR,
825                                    TXT_CANT_SHOW_STICKY_WINDOWS);
826                 return;
827         }
828
829         parent = mainwindow_create();
830         parent->active = window;
831         gui_window_reparent(window, parent);
832
833         if (settings_get_bool("autostick_split_windows"))
834                 gui_window_set_sticky(window);
835
836         active_mainwin = NULL;
837         window_set_active(window);
838 }
839
840 /* SYNTAX: WINDOW UP */
841 static void cmd_window_up(void)
842 {
843         MAIN_WINDOW_REC *rec;
844
845         rec = mainwindows_find_upper(active_mainwin->first_line);
846         if (rec == NULL)
847                 rec = mainwindows_find_upper(term_height);
848         if (rec != NULL)
849                 window_set_active(rec->active);
850 }
851
852 /* SYNTAX: WINDOW DOWN */
853 static void cmd_window_down(void)
854 {
855         MAIN_WINDOW_REC *rec;
856
857         rec = mainwindows_find_lower(active_mainwin->last_line);
858         if (rec == NULL)
859                 rec = mainwindows_find_lower(-1);
860         if (rec != NULL)
861                 window_set_active(rec->active);
862 }
863
864 #define WINDOW_STICKY_MATCH(window, sticky_parent) \
865         ((!WINDOW_GUI(window)->sticky && (sticky_parent) == NULL) || \
866          (WINDOW_GUI(window)->sticky && \
867           WINDOW_MAIN(window) == (sticky_parent)))
868
869 static int window_refnum_left(int refnum, int wrap)
870 {
871         MAIN_WINDOW_REC *find_sticky;
872         WINDOW_REC *window;
873
874         window = window_find_refnum(refnum);
875         g_return_val_if_fail(window != NULL, -1);
876
877         find_sticky = WINDOW_MAIN(window)->sticky_windows ?
878                 WINDOW_MAIN(window) : NULL;
879
880         do {
881                 refnum = window_refnum_prev(refnum, wrap);
882                 if (refnum < 0)
883                         break;
884
885                 window = window_find_refnum(refnum);
886         } while (!WINDOW_STICKY_MATCH(window, find_sticky));
887
888         return refnum;
889 }
890
891 static int window_refnum_right(int refnum, int wrap)
892 {
893         MAIN_WINDOW_REC *find_sticky;
894         WINDOW_REC *window;
895
896         window = window_find_refnum(refnum);
897         g_return_val_if_fail(window != NULL, -1);
898
899         find_sticky = WINDOW_MAIN(window)->sticky_windows ?
900                 WINDOW_MAIN(window) : NULL;
901
902         do {
903                 refnum = window_refnum_next(refnum, wrap);
904                 if (refnum < 0)
905                         break;
906
907                 window = window_find_refnum(refnum);
908         } while (!WINDOW_STICKY_MATCH(window, find_sticky));
909
910         return refnum;
911 }
912
913 /* SYNTAX: WINDOW LEFT */
914 static void cmd_window_left(const char *data, SERVER_REC *server, void *item)
915 {
916         int refnum;
917
918         refnum = window_refnum_left(active_win->refnum, TRUE);
919         if (refnum != -1)
920                 window_set_active(window_find_refnum(refnum));
921 }
922
923 /* SYNTAX: WINDOW RIGHT */
924 static void cmd_window_right(void)
925 {
926         int refnum;
927
928         refnum = window_refnum_right(active_win->refnum, TRUE);
929         if (refnum != -1)
930                 window_set_active(window_find_refnum(refnum));
931 }
932
933 static void window_reparent(WINDOW_REC *win, MAIN_WINDOW_REC *mainwin)
934 {
935         MAIN_WINDOW_REC *old_mainwin;
936
937         old_mainwin = WINDOW_MAIN(win);
938
939         if (old_mainwin != mainwin) {
940                 gui_window_set_unsticky(win);
941
942                 if (old_mainwin->active == win) {
943                         mainwindow_change_active(old_mainwin, win);
944                         if (active_mainwin == NULL) {
945                                 active_mainwin = mainwin;
946                                 window_set_active(mainwin->active);
947                         }
948                 }
949
950                 gui_window_reparent(win, mainwin);
951                 window_set_active(win);
952         }
953 }
954
955 /* SYNTAX: WINDOW STICK [<ref#>] [ON|OFF] */
956 static void cmd_window_stick(const char *data)
957 {
958         MAIN_WINDOW_REC *mainwin;
959         WINDOW_REC *win;
960
961         mainwin = active_mainwin;
962         win = active_mainwin->active;
963
964         if (is_numeric(data, ' ')) {
965                 /* ref# specified */
966                 win = window_find_refnum(atoi(data));
967                 if (win == NULL) {
968                         printformat_window(active_win, MSGLEVEL_CLIENTERROR,
969                                            TXT_REFNUM_NOT_FOUND, data);
970                         return;
971                 }
972
973                 while (*data != ' ' && *data != '\0') data++;
974                 while (*data == ' ') data++;
975         }
976
977         if (g_strncasecmp(data, "OF", 2) == 0 || i_toupper(*data) == 'N') {
978                 /* unset sticky */
979                 if (!WINDOW_GUI(win)->sticky) {
980                         printformat_window(win, MSGLEVEL_CLIENTERROR,
981                                            TXT_WINDOW_NOT_STICKY);
982                 } else {
983                         gui_window_set_unsticky(win);
984                         printformat_window(win, MSGLEVEL_CLIENTNOTICE,
985                                            TXT_WINDOW_UNSET_STICKY);
986                 }
987         } else {
988                 /* set sticky */
989                 window_reparent(win, mainwin);
990                 gui_window_set_sticky(win);
991
992                 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
993                                    TXT_WINDOW_SET_STICKY);
994         }
995 }
996
997 /* SYNTAX: WINDOW MOVE LEFT */
998 static void cmd_window_move_left(void)
999 {
1000         int refnum;
1001
1002         refnum = window_refnum_left(active_win->refnum, TRUE);
1003         if (refnum != -1)
1004                 window_set_refnum(active_win, refnum);
1005 }
1006
1007 /* SYNTAX: WINDOW MOVE RIGHT */
1008 static void cmd_window_move_right(void)
1009 {
1010         int refnum;
1011
1012         refnum = window_refnum_right(active_win->refnum, TRUE);
1013         if (refnum != -1)
1014                 window_set_refnum(active_win, refnum);
1015 }
1016
1017 /* SYNTAX: WINDOW MOVE UP */
1018 static void cmd_window_move_up(void)
1019 {
1020         MAIN_WINDOW_REC *rec;
1021
1022         rec = mainwindows_find_upper(active_mainwin->first_line);
1023         if (rec != NULL)
1024                 window_reparent(active_win, rec);
1025 }
1026
1027 /* SYNTAX: WINDOW MOVE DOWN */
1028 static void cmd_window_move_down(void)
1029 {
1030         MAIN_WINDOW_REC *rec;
1031
1032         rec = mainwindows_find_lower(active_mainwin->last_line);
1033         if (rec != NULL)
1034                 window_reparent(active_win, rec);
1035 }
1036
1037 static void windows_print_sticky(WINDOW_REC *win)
1038 {
1039         MAIN_WINDOW_REC *mainwin;
1040         GSList *tmp, *list;
1041         GString *str;
1042
1043         mainwin = WINDOW_MAIN(win);
1044
1045         /* convert to string */
1046         str = g_string_new(NULL);
1047         list = get_sticky_windows_sorted(mainwin);
1048         for (tmp = list; tmp != NULL; tmp = tmp->next) {
1049                 WINDOW_REC *rec = tmp->data;
1050
1051                 g_string_sprintfa(str, "#%d, ", rec->refnum);
1052         }
1053         g_string_truncate(str, str->len-2);
1054         g_slist_free(list);
1055
1056         printformat_window(win, MSGLEVEL_CLIENTCRAP,
1057                            TXT_WINDOW_INFO_STICKY, str->str);
1058         g_string_free(str, TRUE);
1059 }
1060
1061 static void sig_window_print_info(WINDOW_REC *win)
1062 {
1063         GUI_WINDOW_REC *gui;
1064
1065         gui = WINDOW_GUI(win);
1066         if (gui->use_scroll) {
1067                 printformat_window(win, MSGLEVEL_CLIENTCRAP,
1068                                    TXT_WINDOW_INFO_SCROLL,
1069                                    gui->scroll ? "yes" : "no");
1070         }
1071
1072         if (WINDOW_MAIN(win)->sticky_windows)
1073                 windows_print_sticky(win);
1074 }
1075
1076 void mainwindows_init(void)
1077 {
1078         old_screen_width = term_width;
1079         old_screen_height = term_height;
1080
1081         mainwindows = NULL;
1082         active_mainwin = NULL;
1083         screen_reserved_top = screen_reserved_bottom = 0;
1084
1085         command_bind("window grow", NULL, (SIGNAL_FUNC) cmd_window_grow);
1086         command_bind("window shrink", NULL, (SIGNAL_FUNC) cmd_window_shrink);
1087         command_bind("window size", NULL, (SIGNAL_FUNC) cmd_window_size);
1088         command_bind("window balance", NULL, (SIGNAL_FUNC) cmd_window_balance);
1089         command_bind("window hide", NULL, (SIGNAL_FUNC) cmd_window_hide);
1090         command_bind("window show", NULL, (SIGNAL_FUNC) cmd_window_show);
1091         command_bind("window up", NULL, (SIGNAL_FUNC) cmd_window_up);
1092         command_bind("window down", NULL, (SIGNAL_FUNC) cmd_window_down);
1093         command_bind("window left", NULL, (SIGNAL_FUNC) cmd_window_left);
1094         command_bind("window right", NULL, (SIGNAL_FUNC) cmd_window_right);
1095         command_bind("window stick", NULL, (SIGNAL_FUNC) cmd_window_stick);
1096         command_bind("window move left", NULL, (SIGNAL_FUNC) cmd_window_move_left);
1097         command_bind("window move right", NULL, (SIGNAL_FUNC) cmd_window_move_right);
1098         command_bind("window move up", NULL, (SIGNAL_FUNC) cmd_window_move_up);
1099         command_bind("window move down", NULL, (SIGNAL_FUNC) cmd_window_move_down);
1100         signal_add("window print info", (SIGNAL_FUNC) sig_window_print_info);
1101 }
1102
1103 void mainwindows_deinit(void)
1104 {
1105         while (mainwindows != NULL)
1106                 mainwindow_destroy(mainwindows->data);
1107
1108         command_unbind("window grow", (SIGNAL_FUNC) cmd_window_grow);
1109         command_unbind("window shrink", (SIGNAL_FUNC) cmd_window_shrink);
1110         command_unbind("window size", (SIGNAL_FUNC) cmd_window_size);
1111         command_unbind("window balance", (SIGNAL_FUNC) cmd_window_balance);
1112         command_unbind("window hide", (SIGNAL_FUNC) cmd_window_hide);
1113         command_unbind("window show", (SIGNAL_FUNC) cmd_window_show);
1114         command_unbind("window up", (SIGNAL_FUNC) cmd_window_up);
1115         command_unbind("window down", (SIGNAL_FUNC) cmd_window_down);
1116         command_unbind("window left", (SIGNAL_FUNC) cmd_window_left);
1117         command_unbind("window right", (SIGNAL_FUNC) cmd_window_right);
1118         command_unbind("window stick", (SIGNAL_FUNC) cmd_window_stick);
1119         command_unbind("window move left", (SIGNAL_FUNC) cmd_window_move_left);
1120         command_unbind("window move right", (SIGNAL_FUNC) cmd_window_move_right);
1121         command_unbind("window move up", (SIGNAL_FUNC) cmd_window_move_up);
1122         command_unbind("window move down", (SIGNAL_FUNC) cmd_window_move_down);
1123         signal_remove("window print info", (SIGNAL_FUNC) sig_window_print_info);
1124 }