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