Move the rest of mutt "things" into a window as well.
[apps/madmutt.git] / lib-ui / layout.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19
20 #include <lib-ui/lib-ui.h>
21
22 #include <termios.h>
23 #ifdef HAVE_SYS_IOCTL_H
24 #  include <sys/ioctl.h>
25 #elif defined(HAVE_IOCTL_H)
26 #  include <ioctl.h>
27 #endif
28
29 WINDOW *sidebar_w, *main_w;
30
31 void mutt_need_hard_redraw(void)
32 {
33     keypad(main_w, true);
34     clearok(main_w, true);
35     set_option(OPTNEEDREDRAW);
36 }
37
38 void mutt_refresh(void)
39 {
40     /* FIXME: UGGLY */
41     extern ssize_t UngetCount;
42
43     /* don't refresh when we are waiting for a child. */
44     if (option(OPTKEEPQUIET))
45         return;
46
47     /* don't refresh in the middle of macros unless necessary */
48     if (UngetCount && !option(OPTFORCEREFRESH))
49         return;
50
51     /* else */
52     if (sidebar_w)
53         wnoutrefresh(sidebar_w);
54     if (main_w)
55         wnoutrefresh(main_w);
56     wrefresh(stdscr);
57 }
58
59 void mutt_endwin(const char *msg)
60 {
61     if (!option(OPTNOCURSES)) {
62         CLEARLINE(main_w, LINES - 1);
63
64         if (sidebar_w) {
65             delwin(sidebar_w);
66             sidebar_w = NULL;
67         }
68 #if 0
69         if (main_w) {
70             delwin(main_w);
71             main_w = NULL;
72         }
73 #endif
74
75         wattrset(stdscr, A_NORMAL);
76         mutt_refresh();
77         endwin();
78     }
79
80     if (!m_strisempty(msg)) {
81         puts(msg);
82         fflush(stdout);
83     }
84 }
85
86 void ui_layout_init(void)
87 {
88     main_w = newwin(LINES - 1, COLS, 0, 0);
89     SETCOLOR(main_w, MT_COLOR_NORMAL);
90     wclear(main_w);
91     mutt_error   = mutt_curses_error;
92     mutt_message = mutt_curses_message;
93 }
94
95 void ui_layout_resize(void)
96 {
97     if (SigWinch) {
98         int fd, rows = -1, cols = -1;
99         struct winsize w;
100
101         if ((fd = open("/dev/tty", O_RDONLY)) != -1) {
102             if (ioctl(fd, TIOCGWINSZ, &w) != -1) {
103                 rows = w.ws_row;
104                 cols = w.ws_col;
105             }
106             close(fd);
107         }
108         if (rows <= 0) {
109             rows = atoi(getenv("LINES") ?: "24");
110         }
111         if (cols <= 0) {
112             cols = atoi(getenv("COLUMNS") ?: "80");
113         }
114         resizeterm(rows, cols);
115         SigWinch = 0;
116         /* force a real complete redraw. */
117         clearok(main_w, true);
118     }
119 }
120
121 WINDOW *ui_layout_sidebar_w(void)
122 {
123     if (option(OPTMBOXPANE) && SidebarWidth > 1 && SidebarWidth < COLS)
124     {
125         int sw, sh;
126
127         if (!sidebar_w) {
128             sidebar_w = newwin(LINES - 1, SidebarWidth, 0, 0);
129             wresize(main_w, LINES - 1, COLS - SidebarWidth);
130             mvwin(main_w, 0, SidebarWidth);
131         }
132
133         getmaxyx(sidebar_w, sh, sw);
134
135         if (sh != LINES - 1 || sw != SidebarWidth) {
136             wresize(sidebar_w, LINES - 1, SidebarWidth);
137             wresize(main_w, LINES - 1, COLS - SidebarWidth);
138             mvwin(main_w, 0, SidebarWidth);
139         }
140     } else {
141         if (sidebar_w) {
142             delwin(sidebar_w);
143             sidebar_w = NULL;
144             wresize(main_w, LINES - 1, COLS);
145             mvwin(main_w, 0, 0);
146         }
147     }
148     return sidebar_w;
149 }