bb7abc338d40e847480777b23797c54eaa29ec16
[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     /* don't refresh when we are waiting for a child. */
45     if (option(OPTKEEPQUIET))
46         return;
47
48 #if 0 /* MC: WHY ? */
49     /* don't refresh in the middle of macros unless necessary */
50     if (UngetCount && !option(OPTFORCEREFRESH))
51         return;
52 #endif
53
54     /* else */
55     wrefresh(stdscr);
56     if (layout.sidebar)
57         wrefresh(layout.sidebar);
58 }
59
60 void mutt_endwin(const char *msg)
61 {
62     if (!option(OPTNOCURSES)) {
63         CLEARLINE(LINES - 1);
64
65         if (layout.sidebar) {
66             delwin(layout.sidebar);
67             layout.sidebar = NULL;
68         }
69         if (layout.main) {
70             delwin(layout.main);
71             layout.main = NULL;
72         }
73
74         wattrset(stdscr, A_NORMAL);
75         mutt_refresh();
76         endwin();
77     }
78
79     if (!m_strisempty(msg)) {
80         puts(msg);
81         fflush(stdout);
82     }
83 }
84
85 void ui_layout_init(void)
86 {
87     SETCOLOR(MT_COLOR_NORMAL);
88     wclear(stdscr);
89     mutt_error   = mutt_curses_error;
90     mutt_message = mutt_curses_message;
91
92     getmaxyx(stdscr, layout.rows, layout.cols);
93 }
94
95 void ui_layout_resize(void)
96 {
97     if (SigWinch) {
98         int fd;
99         struct winsize w;
100
101         layout.rows = layout.cols = -1;
102
103         if ((fd = open("/dev/tty", O_RDONLY)) != -1) {
104             if (ioctl(fd, TIOCGWINSZ, &w) != -1) {
105                 layout.rows = w.ws_row;
106                 layout.cols = w.ws_col;
107             }
108             close(fd);
109         }
110         if (layout.rows <= 0) {
111             layout.rows = atoi(getenv("LINES") ?: "24");
112         }
113         if (layout.cols <= 0) {
114             layout.cols = atoi(getenv("COLUMNS") ?: "80");
115         }
116         resizeterm(layout.rows, layout.cols);
117         SigWinch = 0;
118         /* force a real complete redraw. */
119         clearok(stdscr, true);
120     }
121 }
122
123 WINDOW *ui_layout_sidebar_w(void)
124 {
125     if (option(OPTMBOXPANE) && SidebarWidth > 1 && SidebarWidth < layout.cols)
126     {
127         int sw, sh;
128
129         if (!layout.sidebar) {
130             layout.sidebar = newwin(layout.rows - 1, SidebarWidth, 0, 0);
131         }
132
133         getmaxyx(layout.sidebar, sh, sw);
134
135         if (sh != layout.rows - 1 || sw != SidebarWidth) {
136             wresize(layout.sidebar, layout.rows - 1, SidebarWidth);
137         }
138     } else {
139         if (layout.sidebar) {
140             delwin(layout.sidebar);
141             layout.sidebar = NULL;
142         }
143     }
144     return layout.sidebar;
145 }