screen: add `noexcept`
[ncmpc-debian.git] / src / screen_init.cxx
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2019 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include "screen.hxx"
21 #include "Page.hxx"
22 #include "QueuePage.hxx"
23 #include "config.h"
24 #include "Options.hxx"
25 #include "Styles.hxx"
26
27 /* minimum window size */
28 static const unsigned SCREEN_MIN_COLS = 14;
29 static const unsigned SCREEN_MIN_ROWS = 5;
30
31 ScreenManager::ScreenManager(boost::asio::io_service &_io_service) noexcept
32         :io_service(_io_service),
33          layout({std::max<unsigned>(COLS, SCREEN_MIN_COLS),
34                  std::max<unsigned>(LINES, SCREEN_MIN_ROWS)}),
35          title_bar({layout.title_x, layout.title_y}, layout.size.width),
36          main_window({layout.main_x, layout.main_y}, layout.GetMainSize()),
37          progress_bar({layout.progress_x, layout.GetProgressY()}, layout.size.width),
38          status_bar(io_service,
39                     {layout.status_x, layout.GetStatusY()}, layout.size.width),
40          mode_fn_prev(&screen_queue)
41 {
42         buf_size = layout.size.width;
43         buf = new char[buf_size];
44
45         if (!options.hardware_cursor)
46                 leaveok(main_window.w, true);
47
48         keypad(main_window.w, true);
49
50 #ifdef ENABLE_COLORS
51         if (options.enable_colors) {
52                 /* set background attributes */
53                 wbkgd(stdscr, COLOR_PAIR(Style::LIST));
54                 wbkgd(main_window.w, COLOR_PAIR(Style::LIST));
55         }
56 #endif
57 }
58
59 ScreenManager::~ScreenManager() noexcept
60 {
61         delete[] buf;
62 }
63
64 void
65 ScreenManager::Exit() noexcept
66 {
67         current_page->second->OnClose();
68         current_page = pages.end();
69         pages.clear();
70 }
71
72 void
73 ScreenManager::OnResize() noexcept
74 {
75         layout = Layout({std::max<unsigned>(COLS, SCREEN_MIN_COLS),
76                          std::max<unsigned>(LINES, SCREEN_MIN_ROWS)});
77
78 #ifdef PDCURSES
79         resize_term(layout.size.height, layout.size.width);
80 #else
81         resizeterm(layout.size.height, layout.size.width);
82 #endif
83
84         title_bar.OnResize(layout.size.width);
85
86         /* main window */
87         main_window.Resize(layout.GetMainSize());
88
89         /* progress window */
90         progress_bar.OnResize({layout.progress_x, layout.GetProgressY()},
91                               layout.size.width);
92
93         /* status window */
94         status_bar.OnResize({layout.status_x, layout.GetStatusY()},
95                             layout.size.width);
96
97         buf_size = layout.size.width;
98         delete[] buf;
99         buf = new char[buf_size];
100
101         /* resize all screens */
102         current_page->second->Resize(main_window.size);
103
104         /* ? - without this the cursor becomes visible with aterm & Eterm */
105         curs_set(1);
106         curs_set(0);
107
108         Paint(true);
109 }
110
111 void
112 ScreenManager::Init(struct mpdclient *c) noexcept
113 {
114         current_page = MakePage(screen_queue);
115         current_page->second->OnOpen(*c);
116 }