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