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