Dialogs/*: add "override" and "final"
[xcsoar.git] / src / Dialogs / Weather / NOAAList.cpp
blob1e8b0ba9bbc6dcdf9f83ee514bc859d3c79570e0
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 "Dialogs/Weather.hpp"
25 #include "Dialogs/Message.hpp"
26 #include "Dialogs/JobDialog.hpp"
27 #include "Language/Language.hpp"
28 #include "Weather/Features.hpp"
30 #ifdef HAVE_NOAA
32 #include "UIGlobals.hpp"
33 #include "Look/DialogLook.hpp"
34 #include "Dialogs/WidgetDialog.hpp"
35 #include "Dialogs/TextEntry.hpp"
36 #include "Form/Form.hpp"
37 #include "Form/Button.hpp"
38 #include "Widget/ListWidget.hpp"
39 #include "Weather/NOAAGlue.hpp"
40 #include "Weather/NOAAStore.hpp"
41 #include "Weather/NOAAUpdater.hpp"
42 #include "Weather/METAR.hpp"
43 #include "Util/TrivialArray.hpp"
44 #include "Compiler.h"
45 #include "Renderer/NOAAListRenderer.hpp"
47 struct NOAAListItem
49 StaticString<5> code;
50 NOAAStore::iterator iterator;
52 bool operator<(const NOAAListItem &i2) const {
53 return _tcscmp(code, i2.code) == -1;
57 class NOAAListWidget final
58 : public ListWidget, private ActionListener {
59 enum Buttons {
60 DETAILS,
61 ADD,
62 UPDATE,
63 REMOVE,
66 WndButton *details_button, *add_button, *update_button, *remove_button;
68 TrivialArray<NOAAListItem, 20> stations;
70 public:
71 void CreateButtons(WidgetDialog &dialog);
73 private:
74 void UpdateList();
76 void OpenDetails(unsigned index);
77 void DetailsClicked();
78 void AddClicked();
79 void UpdateClicked();
80 void RemoveClicked();
82 public:
83 /* virtual methods from class Widget */
84 virtual void Prepare(ContainerWindow &parent,
85 const PixelRect &rc) override;
86 virtual void Unprepare() override;
88 protected:
89 /* virtual methods from ListItemRenderer */
90 virtual void OnPaintItem(Canvas &canvas, const PixelRect rc,
91 unsigned idx) override;
93 /* virtual methods from ListCursorHandler */
94 virtual bool CanActivateItem(unsigned index) const override {
95 return true;
98 virtual void OnActivateItem(unsigned index) override;
100 private:
101 /* virtual methods from class ActionListener */
102 virtual void OnAction(int id) override;
105 void
106 NOAAListWidget::CreateButtons(WidgetDialog &dialog)
108 details_button = dialog.AddButton(_("Details"), *this, DETAILS);
109 add_button = dialog.AddButton(_("Add"), *this, ADD);
110 update_button = dialog.AddButton(_("Update"), *this, UPDATE);
111 remove_button = dialog.AddButton(_("Remove"), *this, REMOVE);
114 void
115 NOAAListWidget::Prepare(ContainerWindow &parent, const PixelRect &rc)
117 const DialogLook &look = UIGlobals::GetDialogLook();
118 CreateList(parent, look, rc, NOAAListRenderer::GetHeight(look));
119 UpdateList();
122 void
123 NOAAListWidget::Unprepare()
125 DeleteWindow();
128 void
129 NOAAListWidget::UpdateList()
131 stations.clear();
133 for (auto i = noaa_store->begin(), end = noaa_store->end(); i != end; ++i) {
134 NOAAListItem item;
135 item.code = i->GetCodeT();
136 item.iterator = i;
137 stations.push_back(item);
140 std::sort(stations.begin(), stations.end());
142 ListControl &list = GetList();
143 list.SetLength(stations.size());
144 list.Invalidate();
146 const bool empty = stations.empty(), full = stations.full();
147 add_button->SetEnabled(!full);
148 update_button->SetEnabled(!empty);
149 remove_button->SetEnabled(!empty);
150 details_button->SetEnabled(!empty);
153 void
154 NOAAListWidget::OnPaintItem(Canvas &canvas, const PixelRect rc, unsigned index)
156 assert(index < stations.size());
158 NOAAListRenderer::Draw(canvas, rc, *stations[index].iterator,
159 UIGlobals::GetDialogLook());
162 inline void
163 NOAAListWidget::AddClicked()
165 TCHAR code[5] = _T("");
166 if (!dlgTextEntryShowModal(code, 5, _("Airport ICAO code")))
167 return;
169 if (_tcslen(code) != 4) {
170 ShowMessageBox(_("Please enter the FOUR letter code of the desired station."),
171 _("Error"), MB_OK);
172 return;
175 if (!NOAAStore::IsValidCode(code)) {
176 ShowMessageBox(_("Please don't use special characters in the four letter code of the desired station."),
177 _("Error"), MB_OK);
178 return;
181 NOAAStore::iterator i = noaa_store->AddStation(code);
182 noaa_store->SaveToProfile();
184 DialogJobRunner runner(UIGlobals::GetMainWindow(),
185 UIGlobals::GetDialogLook(),
186 _("Download"), true);
188 NOAAUpdater::Update(*i, runner);
190 UpdateList();
193 inline void
194 NOAAListWidget::UpdateClicked()
196 DialogJobRunner runner(UIGlobals::GetMainWindow(),
197 UIGlobals::GetDialogLook(),
198 _("Download"), true);
199 NOAAUpdater::Update(*noaa_store, runner);
200 UpdateList();
203 inline void
204 NOAAListWidget::RemoveClicked()
206 unsigned index = GetList().GetCursorIndex();
207 assert(index < stations.size());
209 StaticString<256> tmp;
210 tmp.Format(_("Do you want to remove station %s?"),
211 stations[index].code.c_str());
213 if (ShowMessageBox(tmp, _("Remove"), MB_YESNO) == IDNO)
214 return;
216 noaa_store->erase(stations[index].iterator);
217 noaa_store->SaveToProfile();
219 UpdateList();
222 void
223 NOAAListWidget::OpenDetails(unsigned index)
225 assert(index < stations.size());
226 dlgNOAADetailsShowModal(UIGlobals::GetMainWindow(),
227 stations[index].iterator);
228 UpdateList();
231 inline void
232 NOAAListWidget::DetailsClicked()
234 if (!stations.empty())
235 OpenDetails(GetList().GetCursorIndex());
238 void
239 NOAAListWidget::OnActivateItem(unsigned index)
241 OpenDetails(index);
244 void
245 NOAAListWidget::OnAction(int id)
247 switch ((Buttons)id) {
248 case DETAILS:
249 DetailsClicked();
250 break;
252 case ADD:
253 AddClicked();
254 break;
256 case UPDATE:
257 UpdateClicked();
258 break;
260 case REMOVE:
261 RemoveClicked();
262 break;
266 void
267 dlgNOAAListShowModal()
269 NOAAListWidget widget;
270 WidgetDialog dialog(UIGlobals::GetDialogLook());
271 dialog.CreateFull(UIGlobals::GetMainWindow(), _("METAR and TAF"), &widget);
272 dialog.AddButton(_("Close"), mrOK);
273 widget.CreateButtons(dialog);
275 dialog.ShowModal();
276 dialog.StealWidget();
279 #else
280 void
281 dlgNOAAListShowModal()
283 ShowMessageBox(_("This function is not available on your platform yet."),
284 _("Error"), MB_OK);
286 #endif