android/GlueIOIOPort: fix spurious errors after IOIO baud rate change
[xcsoar.git] / src / PageActions.cpp
blobe34f0eb95fee035c5668d3a76186629d167b33a4
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "PageActions.hpp"
25 #include "UIActions.hpp"
26 #include "UIState.hpp"
27 #include "Interface.hpp"
28 #include "ActionInterface.hpp"
29 #include "MainWindow.hpp"
30 #include "CrossSection/CrossSectionWidget.hpp"
31 #include "InfoBoxes/InfoBoxSettings.hpp"
32 #include "Pan.hpp"
33 #include "UIGlobals.hpp"
34 #include "MapWindow/GlueMapWindow.hpp"
36 #if defined(ENABLE_SDL) && defined(main)
37 /* on some platforms, SDL wraps the main() function and clutters our
38 namespace with a macro called "main" */
39 #undef main
40 #endif
42 namespace PageActions {
43 /**
44 * Call this when we're about to leave the current page. This
45 * function checks if settings need to be remembered.
47 static void LeavePage();
49 /**
50 * Restore the map zoom afte switching to a configured page.
52 static void RestoreMapZoom();
54 /**
55 * Loads the layout without updating current page information in
56 * #UIState.
58 static void LoadLayout(const PageLayout &layout);
61 void
62 PageActions::LeavePage()
64 PagesState &state = CommonInterface::SetUIState().pages;
66 if (state.special_page.IsDefined())
67 return;
69 PageState &page = state.pages[state.current_index];
71 const GlueMapWindow *map = UIGlobals::GetMapIfActive();
72 if (map != nullptr)
73 page.map_scale = map->VisibleProjection().GetMapScale();
76 void
77 PageActions::RestoreMapZoom()
79 const PagesState &state = CommonInterface::SetUIState().pages;
80 if (state.special_page.IsDefined())
81 return;
83 const PageState &page = state.pages[state.current_index];
84 const PageSettings &settings = CommonInterface::GetUISettings().pages;
86 if (settings.distinct_zoom && positive(page.map_scale)) {
87 GlueMapWindow *map = UIGlobals::GetMapIfActive();
88 if (map != nullptr) {
89 map->SetMapScale(page.map_scale);
90 map->QuickRedraw();
95 static const PageLayout &
96 GetConfiguredLayout()
98 const PageSettings &settings = CommonInterface::GetUISettings().pages;
99 const PagesState &state = CommonInterface::GetUIState().pages;
101 return settings.pages[state.current_index];
104 const PageLayout &
105 PageActions::GetCurrentLayout()
107 const PagesState &state = CommonInterface::GetUIState().pages;
109 return state.special_page.IsDefined()
110 ? state.special_page
111 : GetConfiguredLayout();
114 void
115 PageActions::Update()
117 LoadLayout(GetCurrentLayout());
121 unsigned
122 PageActions::NextIndex()
124 const PageSettings &settings = CommonInterface::GetUISettings().pages;
125 const PagesState &state = CommonInterface::GetUIState().pages;
127 if (state.special_page.IsDefined())
128 /* if a "special" page is active, any page switch will return to
129 the last configured page */
130 return state.current_index;
132 return (state.current_index + 1) % settings.n_pages;
136 void
137 PageActions::Next()
139 LeavePage();
141 PagesState &state = CommonInterface::SetUIState().pages;
143 state.current_index = NextIndex();
144 state.special_page.SetUndefined();
146 Update();
147 RestoreMapZoom();
150 unsigned
151 PageActions::PrevIndex()
153 const PageSettings &settings = CommonInterface::GetUISettings().pages;
154 const PagesState &state = CommonInterface::GetUIState().pages;
156 if (state.special_page.IsDefined())
157 /* if a "special" page is active, any page switch will return to
158 the last configured page */
159 return state.current_index;
161 return (state.current_index + settings.n_pages - 1)
162 % settings.n_pages;
166 void
167 PageActions::Prev()
169 LeavePage();
171 PagesState &state = CommonInterface::SetUIState().pages;
173 state.current_index = PrevIndex();
174 state.special_page.SetUndefined();
176 Update();
177 RestoreMapZoom();
180 void
181 PageActions::LoadLayout(const PageLayout &layout)
183 UIState &ui_state = CommonInterface::SetUIState();
185 if (!layout.valid)
186 return;
188 DisablePan();
190 if (!layout.infobox_config.enabled) {
191 CommonInterface::main_window->SetFullScreen(true);
192 ui_state.auxiliary_enabled = false;
193 } else {
194 if (!layout.infobox_config.auto_switch &&
195 layout.infobox_config.panel < InfoBoxSettings::MAX_PANELS) {
196 CommonInterface::main_window->SetFullScreen(false);
197 ui_state.auxiliary_enabled = true;
198 ui_state.auxiliary_index = layout.infobox_config.panel;
200 else {
201 CommonInterface::main_window->SetFullScreen(false);
202 ui_state.auxiliary_enabled = false;
206 switch (layout.bottom) {
207 case PageLayout::Bottom::NOTHING:
208 CommonInterface::main_window->SetBottomWidget(nullptr);
209 break;
211 case PageLayout::Bottom::CROSS_SECTION:
212 CommonInterface::main_window->SetBottomWidget(new CrossSectionWidget());
213 break;
215 case PageLayout::Bottom::MAX:
216 gcc_unreachable();
219 switch (layout.main) {
220 case PageLayout::Main::MAP:
221 CommonInterface::main_window->ActivateMap();
222 break;
224 case PageLayout::Main::FLARM_RADAR:
225 UIActions::ShowTrafficRadar();
226 break;
228 case PageLayout::Main::THERMAL_ASSISTANT:
229 UIActions::ShowThermalAssistant();
230 break;
232 case PageLayout::Main::MAX:
233 gcc_unreachable();
236 ActionInterface::UpdateDisplayMode();
237 ActionInterface::SendUIState();
240 void
241 PageActions::OpenLayout(const PageLayout &layout)
243 PagesState &state = CommonInterface::SetUIState().pages;
244 state.special_page = layout;
246 LoadLayout(layout);
249 void
250 PageActions::Restore()
252 PageLayout &special_page = CommonInterface::SetUIState().pages.special_page;
253 if (!special_page.IsDefined())
254 return;
256 special_page.SetUndefined();
258 LoadLayout(GetConfiguredLayout());
259 RestoreMapZoom();
262 void
263 PageActions::DeferredRestore()
265 CommonInterface::main_window->DeferredRestorePage();
268 GlueMapWindow *
269 PageActions::ShowMap()
271 PageLayout layout = GetCurrentLayout();
272 if (layout.main != PageLayout::Main::MAP) {
273 /* not showing map currently: activate it */
275 if (GetConfiguredLayout().main == PageLayout::Main::MAP)
276 /* the configured page is a map page: restore it */
277 Restore();
278 else {
279 /* generate a "special" map page based on the current page */
280 layout.main = PageLayout::Main::MAP;
281 OpenLayout(layout);
285 return CommonInterface::main_window->ActivateMap();
288 GlueMapWindow *
289 PageActions::ShowOnlyMap()
291 OpenLayout(PageLayout::FullScreen());
292 return CommonInterface::main_window->ActivateMap();
295 void
296 PageActions::ShowTrafficRadar()
298 PageLayout layout = GetCurrentLayout();
299 if (layout.main == PageLayout::Main::FLARM_RADAR)
300 /* already showing the traffic radar */
301 return;
303 if (GetConfiguredLayout().main == PageLayout::Main::FLARM_RADAR)
304 /* the configured page is a traffic radar page: restore it */
305 Restore();
306 else {
307 /* generate a "special" page based on the current page */
308 layout.main = PageLayout::Main::FLARM_RADAR;
309 layout.bottom = PageLayout::Bottom::NOTHING;
310 OpenLayout(layout);
315 void
316 PageActions::ShowThermalAssistant()
318 PageLayout layout = GetCurrentLayout();
319 if (layout.main == PageLayout::Main::THERMAL_ASSISTANT)
320 /* already showing the traffic radar */
321 return;
323 if (GetConfiguredLayout().main == PageLayout::Main::THERMAL_ASSISTANT)
324 /* the configured page is a traffic radar page: restore it */
325 Restore();
326 else {
327 /* generate a "special" page based on the current page */
328 layout.main = PageLayout::Main::THERMAL_ASSISTANT;
329 layout.bottom = PageLayout::Bottom::NOTHING;
330 OpenLayout(layout);