OpenCPN Partial API docs
Loading...
Searching...
No Matches
rest_server_gui.cpp
1/***************************************************************************
2 *
3 * Project: OpenCPN
4 *
5 ***************************************************************************
6 * Copyright (C) 2010 by David S. Register *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
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. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22 ***************************************************************************
23 */
24
25#include <wx/arrstr.h>
26#include <wx/button.h>
27#include <wx/combobox.h>
28#include <wx/dialog.h>
29#include <wx/dynarray.h>
30#include <wx/event.h>
31#include <wx/gdicmn.h>
32#include <wx/sizer.h>
33#include <wx/stattext.h>
34#include <wx/string.h>
35#include <wx/window.h>
36
37#include "FontMgr.h"
38#include "rest_server_gui.h"
39#include "routemanagerdialog.h"
40
41#ifdef __ANDROID__
42#include "androidUTIL.h"
43#endif
44
45extern RouteManagerDialog* pRouteManagerDialog;
46
47static wxDialog* DisplayDlg(const std::string& msg, const std::string& txt1) {
48 auto dlg = new PINCreateDialog(
49 wxTheApp->GetTopWindow(), wxID_ANY, _("OpenCPN Server Message"), "",
50 wxDefaultPosition, wxDefaultSize, SYMBOL_STG_STYLE);
51 dlg->SetMessage(msg);
52 dlg->SetText1Message(txt1);
53 dlg->Show();
54 return dlg;
55}
56
57static void UpdateRouteMgr() {
58 if (pRouteManagerDialog && pRouteManagerDialog->IsShown()) {
59 pRouteManagerDialog->UpdateTrkListCtrl();
60 pRouteManagerDialog->UpdateWptListCtrl();
61 pRouteManagerDialog->UpdateRouteListCtrl();
62 }
63}
64
65static AcceptObjectDlgResult RunAcceptObjectDlg(const wxString& msg,
66 const wxString& check1msg) {
67 AcceptObjectDialog dlg(NULL, _("OpenCPN Server Message"), msg, check1msg);
68 int result = dlg.ShowModal();
69 bool check1 = dlg.GetCheck1Value();
70 return AcceptObjectDlgResult(result, check1);
71}
72
73RestServerDlgCtx PINCreateDialog::GetDlgCtx() {
75 ctx.run_pincode_dlg = [](const std::string& msg, const std::string& text1) {
76 return DisplayDlg(msg, text1);
77 };
78 ctx.update_route_mgr = []() { UpdateRouteMgr(); };
79 ctx.run_accept_object_dlg = [](const wxString& msg,
80 const wxString& check1msg) {
81 return RunAcceptObjectDlg(msg, check1msg);
82 };
83 ctx.top_level_refresh = []() { wxTheApp->GetTopWindow()->Refresh(); };
84 return ctx;
85}
86
87BEGIN_EVENT_TABLE(AcceptObjectDialog, wxDialog)
88EVT_BUTTON(ID_STG_CANCEL, AcceptObjectDialog::OnCancelClick)
89EVT_BUTTON(ID_STG_OK, AcceptObjectDialog::OnOKClick)
90END_EVENT_TABLE()
91
93 m_OKButton = NULL;
94 m_CancelButton = NULL;
95 premtext = NULL;
96}
97
98AcceptObjectDialog::AcceptObjectDialog(wxWindow* parent,
99 const wxString& caption,
100 const wxString& msg1,
101 const wxString msg2)
102 : AcceptObjectDialog(parent, 0, caption, "", wxDefaultPosition,
103 wxDefaultSize, SYMBOL_STG_STYLE, msg1, msg2) {}
104
105AcceptObjectDialog::AcceptObjectDialog(wxWindow* parent, wxWindowID id,
106 const wxString& caption,
107 const wxString& hint, const wxPoint& pos,
108 const wxSize& size, long style,
109 const wxString& msg1,
110 const wxString& msg2) {
111 wxFont* pif = FontMgr::Get().GetFont(_("Dialog"));
112 SetFont(*pif);
113 m_checkbox1_msg = msg2;
114 Create(parent, id, caption, hint, pos, size, style, msg1, msg2);
115#ifdef __ANDROID__
116 androidDisableRotation();
117#endif
118}
119
120AcceptObjectDialog::~AcceptObjectDialog() {
121 delete m_OKButton;
122 delete m_CancelButton;
123#ifdef __ANDROID__
124 androidEnableRotation();
125#endif
126}
127
128bool AcceptObjectDialog::Create(wxWindow* parent, wxWindowID id,
129 const wxString& caption, const wxString& hint,
130 const wxPoint& pos, const wxSize& size,
131 long style, const wxString& msg1,
132 const wxString& msg2) {
133 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS);
134 wxDialog::Create(parent, id, caption, pos, size, style);
135
136 CreateControls(hint, msg1, msg2);
137 GetSizer()->Fit(this);
138 GetSizer()->SetSizeHints(this);
139 Centre();
140
141 return TRUE;
142}
143
144void AcceptObjectDialog::CreateControls(const wxString& hint,
145 const wxString& msg1,
146 const wxString& msg2) {
147 AcceptObjectDialog* itemDialog1 = this;
148
149 wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
150 SetSizer(itemBoxSizer2);
151
152 // Add a reminder text box
153 itemBoxSizer2->AddSpacer(20);
154
155 premtext = new wxStaticText(this, -1, msg1);
156 itemBoxSizer2->Add(premtext, 0, wxEXPAND | wxALL, 10);
157
158 m_pCheck1 = new wxCheckBox(this, ID_STG_CHECK1, m_checkbox1_msg);
159 itemBoxSizer2->Add(m_pCheck1, 0, wxEXPAND | wxALL, 10);
160
161 if (!m_checkbox1_msg.Length()) m_pCheck1->Hide();
162
163 // OK/Cancel/etc.
164 wxBoxSizer* itemBoxSizer16 = new wxBoxSizer(wxHORIZONTAL);
165 itemBoxSizer2->Add(itemBoxSizer16, 0, wxALIGN_RIGHT | wxALL, 5);
166
167 m_CancelButton = new wxButton(itemDialog1, ID_STG_CANCEL, _("Cancel"),
168 wxDefaultPosition, wxDefaultSize, 0);
169 itemBoxSizer16->Add(m_CancelButton, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
170
171 m_OKButton = new wxButton(itemDialog1, ID_STG_OK, "OK", wxDefaultPosition,
172 wxDefaultSize, 0);
173 itemBoxSizer16->Add(m_OKButton, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
174 m_OKButton->SetDefault();
175}
176
177void AcceptObjectDialog::SetMessage(const wxString& msg) {
178 if (premtext) {
179 premtext->SetLabel(msg);
180 premtext->Refresh(true);
181 }
182}
183
184void AcceptObjectDialog::SetCheck1Message(const wxString& msg) {
185 m_checkbox1_msg = msg;
186 m_pCheck1->SetLabel(msg);
187 m_pCheck1->Show();
188 GetSizer()->Fit(this);
189}
190
191void AcceptObjectDialog::OnOKClick(wxCommandEvent& event) {
192 EndModal(ID_STG_OK);
193 SetReturnCode(ID_STG_OK);
194}
195
196void AcceptObjectDialog::OnCancelClick(wxCommandEvent& event) {
197 EndModal(ID_STG_CANCEL);
198#ifdef __ANDROID__
199 androidDisableRotation();
200#endif
201}
202
203BEGIN_EVENT_TABLE(PINCreateDialog, wxDialog)
204EVT_BUTTON(ID_STG_CANCEL, PINCreateDialog::OnCancelClick)
205EVT_BUTTON(ID_STG_OK, PINCreateDialog::OnOKClick)
206END_EVENT_TABLE()
207
209 m_OKButton = NULL;
210 m_CancelButton = NULL;
211 premtext = NULL;
212#ifdef __ANDROID__
213 androidEnableRotation();
214#endif
215}
216
217PINCreateDialog::PINCreateDialog(wxWindow* parent, wxWindowID id,
218 const wxString& caption, const wxString& hint,
219 const wxPoint& pos, const wxSize& size,
220 long style) {
221 wxFont* pif = FontMgr::Get().GetFont(_("Dialog"));
222 SetFont(*pif);
223 Create(parent, id, caption, hint, pos, size, style);
224#ifdef __ANDROID__
225 androidDisableRotation();
226#endif
227}
228
229PINCreateDialog::~PINCreateDialog() {
230 delete m_OKButton;
231 delete m_CancelButton;
232#ifdef __ANDROID__
233 androidEnableRotation();
234#endif
235}
236
237wxDialog* PINCreateDialog::Initiate(const std::string& msg,
238 const std::string& text1) {
239 auto dlg = new PINCreateDialog(
240 wxTheApp->GetTopWindow(), wxID_ANY, _("OpenCPN Server Message"), "",
241 wxDefaultPosition, wxDefaultSize, SYMBOL_STG_STYLE);
242 dlg->SetMessage(msg);
243 dlg->SetText1Message(text1);
244 dlg->Show();
245 return dlg;
246}
247
248void PINCreateDialog::DeInit() {
249 Close();
250 Destroy();
251}
252
253bool PINCreateDialog::Create(wxWindow* parent, wxWindowID id,
254 const wxString& caption, const wxString& hint,
255 const wxPoint& pos, const wxSize& size,
256 long style) {
257 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS);
258 wxDialog::Create(parent, id, caption, pos, size, style);
259
260 CreateControls(hint);
261 GetSizer()->Fit(this);
262 GetSizer()->SetSizeHints(this);
263 Centre();
264
265 return TRUE;
266}
267
268void PINCreateDialog::CreateControls(const wxString& hint) {
269 PINCreateDialog* itemDialog1 = this;
270
271 wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
272 SetSizer(itemBoxSizer2);
273
274 // Add a reminder text box
275 itemBoxSizer2->AddSpacer(20);
276
277 premtext = new wxStaticText(
278 this, -1, "A loooooooooooooooooooooooooooooooooooooooooooooong line\n");
279 itemBoxSizer2->Add(premtext, 0, wxEXPAND | wxALL, 10);
280
281 m_pText1 = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition,
282 wxDefaultSize, wxTE_READONLY | wxTE_CENTRE);
283 itemBoxSizer2->Add(m_pText1, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
284 m_pText1->SetMinSize(wxSize(7 * GetCharWidth(), -1));
285
286 // OK/Cancel/etc.
287 wxBoxSizer* itemBoxSizer16 = new wxBoxSizer(wxHORIZONTAL);
288 itemBoxSizer2->Add(itemBoxSizer16, 0, wxALIGN_RIGHT | wxALL, 5);
289
290 m_CancelButton = new wxButton(itemDialog1, ID_STG_CANCEL, _("Cancel"),
291 wxDefaultPosition, wxDefaultSize, 0);
292 itemBoxSizer16->Add(m_CancelButton, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
293
294 m_OKButton = new wxButton(itemDialog1, ID_STG_OK, "OK", wxDefaultPosition,
295 wxDefaultSize, 0);
296 itemBoxSizer16->Add(m_OKButton, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
297 m_OKButton->SetDefault();
298}
299
300void PINCreateDialog::SetMessage(const wxString& msg) {
301 if (premtext) {
302 premtext->SetLabel(msg);
303 premtext->Refresh(true);
304 }
305}
306
307void PINCreateDialog::SetText1Message(const wxString& msg) {
308 m_pText1->ChangeValue(msg);
309 m_pText1->Show();
310 GetSizer()->Fit(this);
311}
312
313void PINCreateDialog::OnOKClick(wxCommandEvent& event) { Close(); }
314
315void PINCreateDialog::OnCancelClick(wxCommandEvent& event) { Close(); }
"Accept Object" Dialog Definition
wxFont * GetFont(const wxString &TextElement, int requested_font_size=0)
Gets a font object for a UI element.
Definition FontMgr.cpp:186
Callbacks for handling dialogs and RouteManager updates.
Definition rest_server.h:95
Returned status from RunAcceptObjectDlg.
Definition rest_server.h:83