7b46036b64c882c01a8e7bef4c0ad301e8b77512
[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 static struct {
30     int rows, cols;
31     WINDOW *sidebar;
32     WINDOW *main;
33 } layout;
34
35 void mutt_need_hard_redraw(void)
36 {
37     keypad(stdscr, true);
38     clearok(stdscr, true);
39     set_option(OPTNEEDREDRAW);
40 }
41
42 void mutt_refresh(void)
43 {
44     /* FIXME: UGGLY */
45     extern ssize_t UngetCount;
46
47     /* don't refresh when we are waiting for a child. */
48     if (option(OPTKEEPQUIET))
49         return;
50
51     /* don't refresh in the middle of macros unless necessary */
52     if (UngetCount && !option(OPTFORCEREFRESH))
53         return;
54
55     /* else */
56     wrefresh(stdscr);
57     if (layout.sidebar)
58         wrefresh(layout.sidebar);
59 }
60
61 void mutt_endwin(const char *msg)
62 {
63     if (!option(OPTNOCURSES)) {
64         CLEARLINE(LINES - 1);
65
66         if (layout.sidebar) {
67             delwin(layout.sidebar);
68             layout.sidebar = NULL;
69         }
70         if (layout.main) {
71             delwin(layout.main);
72             layout.main = NULL;
73         }
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     SETCOLOR(MT_COLOR_NORMAL);
89     wclear(stdscr);
90     mutt_error   = mutt_curses_error;
91     mutt_message = mutt_curses_message;
92
93     getmaxyx(stdscr, layout.rows, layout.cols);
94 }
95
96 void ui_layout_resize(void)
97 {
98     if (SigWinch) {
99         int fd;
100         struct winsize w;
101
102         layout.rows = layout.cols = -1;
103
104         if ((fd = open("/dev/tty", O_RDONLY)) != -1) {
105             if (ioctl(fd, TIOCGWINSZ, &w) != -1) {
106                 layout.rows = w.ws_row;
107                 layout.cols = w.ws_col;
108             }
109             close(fd);
110         }
111         if (layout.rows <= 0) {
112             layout.rows = atoi(getenv("LINES") ?: "24");
113         }
114         if (layout.cols <= 0) {
115             layout.cols = atoi(getenv("COLUMNS") ?: "80");
116         }
117         resizeterm(layout.rows, layout.cols);
118         SigWinch = 0;
119         /* force a real complete redraw. */
120         clearok(stdscr, true);
121     }
122 }
123
124 WINDOW *ui_layout_sidebar_w(void)
125 {
126     if (option(OPTMBOXPANE) && SidebarWidth > 1 && SidebarWidth < layout.cols)
127     {
128         int sw, sh;
129
130         if (!layout.sidebar) {
131             layout.sidebar = newwin(layout.rows - 1, SidebarWidth, 0, 0);
132         }
133
134         getmaxyx(layout.sidebar, sh, sw);
135
136         if (sh != layout.rows - 1 || sw != SidebarWidth) {
137             wresize(layout.sidebar, layout.rows - 1, SidebarWidth);
138         }
139     } else {
140         if (layout.sidebar) {
141             delwin(layout.sidebar);
142             layout.sidebar = NULL;
143         }
144     }
145     return layout.sidebar;
146 }