+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * Copyright © 2006 Pierre Habouzit
+ */
+
+#include <lib-ui/lib-ui.h>
+
+#include <termios.h>
+#ifdef HAVE_SYS_IOCTL_H
+# include <sys/ioctl.h>
+#elif defined(HAVE_IOCTL_H)
+# include <ioctl.h>
+#endif
+
+static struct {
+ int rows, cols;
+ WINDOW *sidebar;
+ WINDOW *main;
+} layout;
+
+void mutt_need_hard_redraw(void)
+{
+ keypad(stdscr, true);
+ clearok(stdscr, true);
+ set_option(OPTNEEDREDRAW);
+}
+
+void mutt_refresh(void)
+{
+ /* don't refresh when we are waiting for a child. */
+ if (option(OPTKEEPQUIET))
+ return;
+
+#if 0 /* MC: WHY ? */
+ /* don't refresh in the middle of macros unless necessary */
+ if (UngetCount && !option(OPTFORCEREFRESH))
+ return;
+#endif
+
+ /* else */
+ wrefresh(stdscr);
+ if (layout.sidebar)
+ wrefresh(layout.sidebar);
+}
+
+void mutt_endwin(const char *msg)
+{
+ if (!option(OPTNOCURSES)) {
+ CLEARLINE(LINES - 1);
+
+ if (layout.sidebar) {
+ delwin(layout.sidebar);
+ layout.sidebar = NULL;
+ }
+ if (layout.main) {
+ delwin(layout.main);
+ layout.main = NULL;
+ }
+
+ wattrset(stdscr, A_NORMAL);
+ mutt_refresh();
+ endwin();
+ }
+
+ if (!m_strisempty(msg)) {
+ puts(msg);
+ fflush(stdout);
+ }
+}
+
+void ui_layout_init(void)
+{
+ SETCOLOR(MT_COLOR_NORMAL);
+ wclear(stdscr);
+ mutt_error = mutt_curses_error;
+ mutt_message = mutt_curses_message;
+
+ getmaxyx(stdscr, layout.rows, layout.cols);
+}
+
+void ui_layout_resize(void)
+{
+ if (SigWinch) {
+ int fd;
+ struct winsize w;
+
+ layout.rows = layout.cols = -1;
+
+ if ((fd = open("/dev/tty", O_RDONLY)) != -1) {
+ if (ioctl(fd, TIOCGWINSZ, &w) != -1) {
+ layout.rows = w.ws_row;
+ layout.cols = w.ws_col;
+ }
+ close(fd);
+ }
+ if (layout.rows <= 0) {
+ layout.rows = atoi(getenv("LINES") ?: "24");
+ }
+ if (layout.cols <= 0) {
+ layout.cols = atoi(getenv("COLUMNS") ?: "80");
+ }
+ resizeterm(layout.rows, layout.cols);
+ SigWinch = 0;
+ /* force a real complete redraw. */
+ clearok(stdscr, true);
+ }
+}
+
+WINDOW *ui_layout_sidebar_w(void)
+{
+ if (option(OPTMBOXPANE) && SidebarWidth > 1 && SidebarWidth < layout.cols)
+ {
+ int sw, sh;
+
+ if (!layout.sidebar) {
+ layout.sidebar = newwin(layout.rows - 1, SidebarWidth, 0, 0);
+ }
+
+ getmaxyx(layout.sidebar, sh, sw);
+
+ if (sh != layout.rows - 1 || sw != SidebarWidth) {
+ wresize(layout.sidebar, layout.rows - 1, SidebarWidth);
+ }
+ } else {
+ if (layout.sidebar) {
+ delwin(layout.sidebar);
+ layout.sidebar = NULL;
+ }
+ }
+ return layout.sidebar;
+}