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"
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"
45 #include "Renderer/NOAAListRenderer.hpp"
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
{
66 WndButton
*details_button
, *add_button
, *update_button
, *remove_button
;
68 TrivialArray
<NOAAListItem
, 20> stations
;
71 void CreateButtons(WidgetDialog
&dialog
);
76 void OpenDetails(unsigned index
);
77 void DetailsClicked();
83 /* virtual methods from class Widget */
84 virtual void Prepare(ContainerWindow
&parent
,
85 const PixelRect
&rc
) override
;
86 virtual void Unprepare() override
;
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
{
98 virtual void OnActivateItem(unsigned index
) override
;
101 /* virtual methods from class ActionListener */
102 virtual void OnAction(int id
) override
;
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
);
115 NOAAListWidget::Prepare(ContainerWindow
&parent
, const PixelRect
&rc
)
117 const DialogLook
&look
= UIGlobals::GetDialogLook();
118 CreateList(parent
, look
, rc
, NOAAListRenderer::GetHeight(look
));
123 NOAAListWidget::Unprepare()
129 NOAAListWidget::UpdateList()
133 for (auto i
= noaa_store
->begin(), end
= noaa_store
->end(); i
!= end
; ++i
) {
135 item
.code
= i
->GetCodeT();
137 stations
.push_back(item
);
140 std::sort(stations
.begin(), stations
.end());
142 ListControl
&list
= GetList();
143 list
.SetLength(stations
.size());
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
);
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());
163 NOAAListWidget::AddClicked()
165 TCHAR code
[5] = _T("");
166 if (!dlgTextEntryShowModal(code
, 5, _("Airport ICAO code")))
169 if (_tcslen(code
) != 4) {
170 ShowMessageBox(_("Please enter the FOUR letter code of the desired station."),
175 if (!NOAAStore::IsValidCode(code
)) {
176 ShowMessageBox(_("Please don't use special characters in the four letter code of the desired station."),
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
);
194 NOAAListWidget::UpdateClicked()
196 DialogJobRunner
runner(UIGlobals::GetMainWindow(),
197 UIGlobals::GetDialogLook(),
198 _("Download"), true);
199 NOAAUpdater::Update(*noaa_store
, runner
);
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
)
216 noaa_store
->erase(stations
[index
].iterator
);
217 noaa_store
->SaveToProfile();
223 NOAAListWidget::OpenDetails(unsigned index
)
225 assert(index
< stations
.size());
226 dlgNOAADetailsShowModal(UIGlobals::GetMainWindow(),
227 stations
[index
].iterator
);
232 NOAAListWidget::DetailsClicked()
234 if (!stations
.empty())
235 OpenDetails(GetList().GetCursorIndex());
239 NOAAListWidget::OnActivateItem(unsigned index
)
245 NOAAListWidget::OnAction(int id
)
247 switch ((Buttons
)id
) {
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
);
276 dialog
.StealWidget();
281 dlgNOAAListShowModal()
283 ShowMessageBox(_("This function is not available on your platform yet."),