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 "gl_headers.h" // Must be included before anything using GL stuff
19
20#include <wx/arrstr.h>
21#include <wx/button.h>
22#include <wx/combobox.h>
23#include <wx/dialog.h>
24#include <wx/dynarray.h>
25#include <wx/event.h>
26#include <wx/gdicmn.h>
27#include <wx/sizer.h>
28#include <wx/stattext.h>
29#include <wx/string.h>
30#include <wx/window.h>
31
32#include "font_mgr.h"
33#include "rest_server_gui.h"
34#include "routemanagerdialog.h"
35
36#ifdef __ANDROID__
37#include "androidUTIL.h"
38#endif
39
40static wxDialog* DisplayDlg(const std::string& msg, const std::string& txt1) {
41 auto dlg = new PINCreateDialog(
42 wxTheApp->GetTopWindow(), wxID_ANY, _("OpenCPN Server Message"), "",
43 wxDefaultPosition, wxDefaultSize, SYMBOL_STG_STYLE);
44 dlg->SetMessage(msg);
45 dlg->SetText1Message(txt1);
46 dlg->Show();
47 return dlg;
48}
49
50static void UpdateRouteMgr() {
51 if (pRouteManagerDialog && pRouteManagerDialog->IsShown()) {
52 pRouteManagerDialog->UpdateTrkListCtrl();
53 pRouteManagerDialog->UpdateWptListCtrl();
54 pRouteManagerDialog->UpdateRouteListCtrl();
55 }
56}
57
58static AcceptObjectDlgResult RunAcceptObjectDlg(const wxString& msg,
59 const wxString& check1msg) {
60 AcceptObjectDialog dlg(NULL, _("OpenCPN Server Message"), msg, check1msg);
61 int result = dlg.ShowModal();
62 bool check1 = dlg.GetCheck1Value();
63 return AcceptObjectDlgResult(result, check1);
64}
65
66RestServerDlgCtx PINCreateDialog::GetDlgCtx() {
68 ctx.run_pincode_dlg = [](const std::string& msg, const std::string& text1) {
69 return DisplayDlg(msg, text1);
70 };
71 ctx.update_route_mgr = []() { UpdateRouteMgr(); };
72 ctx.run_accept_object_dlg = [](const wxString& msg,
73 const wxString& check1msg) {
74 return RunAcceptObjectDlg(msg, check1msg);
75 };
76 ctx.top_level_refresh = []() { wxTheApp->GetTopWindow()->Refresh(); };
77 return ctx;
78}
79
80BEGIN_EVENT_TABLE(AcceptObjectDialog, wxDialog)
81EVT_BUTTON(ID_STG_CANCEL, AcceptObjectDialog::OnCancelClick)
82EVT_BUTTON(ID_STG_OK, AcceptObjectDialog::OnOKClick)
83END_EVENT_TABLE()
84
86 m_OKButton = NULL;
87 m_CancelButton = NULL;
88 premtext = NULL;
89}
90
91AcceptObjectDialog::AcceptObjectDialog(wxWindow* parent,
92 const wxString& caption,
93 const wxString& msg1,
94 const wxString msg2)
95 : AcceptObjectDialog(parent, 0, caption, "", wxDefaultPosition,
96 wxDefaultSize, SYMBOL_STG_STYLE, msg1, msg2) {}
97
98AcceptObjectDialog::AcceptObjectDialog(wxWindow* parent, wxWindowID id,
99 const wxString& caption,
100 const wxString& hint, const wxPoint& pos,
101 const wxSize& size, long style,
102 const wxString& msg1,
103 const wxString& msg2) {
104 wxFont* pif = FontMgr::Get().GetFont(_("Dialog"));
105 SetFont(*pif);
106 m_checkbox1_msg = msg2;
107 Create(parent, id, caption, hint, pos, size, style, msg1, msg2);
108#ifdef __ANDROID__
109 androidDisableRotation();
110#endif
111}
112
113AcceptObjectDialog::~AcceptObjectDialog() {
114 delete m_OKButton;
115 delete m_CancelButton;
116#ifdef __ANDROID__
117 androidEnableRotation();
118#endif
119}
120
121bool AcceptObjectDialog::Create(wxWindow* parent, wxWindowID id,
122 const wxString& caption, const wxString& hint,
123 const wxPoint& pos, const wxSize& size,
124 long style, const wxString& msg1,
125 const wxString& msg2) {
126 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS);
127 wxDialog::Create(parent, id, caption, pos, size, style);
128
129 CreateControls(hint, msg1, msg2);
130 GetSizer()->Fit(this);
131 GetSizer()->SetSizeHints(this);
132 Centre();
133
134 return TRUE;
135}
136
137void AcceptObjectDialog::CreateControls(const wxString& hint,
138 const wxString& msg1,
139 const wxString& msg2) {
140 AcceptObjectDialog* itemDialog1 = this;
141
142 wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
143 SetSizer(itemBoxSizer2);
144
145 // Add a reminder text box
146 itemBoxSizer2->AddSpacer(20);
147
148 premtext = new wxStaticText(this, -1, msg1);
149 itemBoxSizer2->Add(premtext, 0, wxEXPAND | wxALL, 10);
150
151 m_pCheck1 = new wxCheckBox(this, ID_STG_CHECK1, m_checkbox1_msg);
152 itemBoxSizer2->Add(m_pCheck1, 0, wxEXPAND | wxALL, 10);
153
154 if (!m_checkbox1_msg.Length()) m_pCheck1->Hide();
155
156 // OK/Cancel/etc.
157 wxBoxSizer* itemBoxSizer16 = new wxBoxSizer(wxHORIZONTAL);
158 itemBoxSizer2->Add(itemBoxSizer16, 0, wxALIGN_RIGHT | wxALL, 5);
159
160 m_CancelButton = new wxButton(itemDialog1, ID_STG_CANCEL, _("Cancel"),
161 wxDefaultPosition, wxDefaultSize, 0);
162 itemBoxSizer16->Add(m_CancelButton, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
163
164 m_OKButton = new wxButton(itemDialog1, ID_STG_OK, "OK", wxDefaultPosition,
165 wxDefaultSize, 0);
166 itemBoxSizer16->Add(m_OKButton, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
167 m_OKButton->SetDefault();
168}
169
170void AcceptObjectDialog::SetMessage(const wxString& msg) {
171 if (premtext) {
172 premtext->SetLabel(msg);
173 premtext->Refresh(true);
174 }
175}
176
177void AcceptObjectDialog::SetCheck1Message(const wxString& msg) {
178 m_checkbox1_msg = msg;
179 m_pCheck1->SetLabel(msg);
180 m_pCheck1->Show();
181 GetSizer()->Fit(this);
182}
183
184void AcceptObjectDialog::OnOKClick(wxCommandEvent& event) {
185 EndModal(ID_STG_OK);
186 SetReturnCode(ID_STG_OK);
187}
188
189void AcceptObjectDialog::OnCancelClick(wxCommandEvent& event) {
190 EndModal(ID_STG_CANCEL);
191#ifdef __ANDROID__
192 androidDisableRotation();
193#endif
194}
195
196BEGIN_EVENT_TABLE(PINCreateDialog, wxDialog)
197EVT_BUTTON(ID_STG_CANCEL, PINCreateDialog::OnCancelClick)
198EVT_BUTTON(ID_STG_OK, PINCreateDialog::OnOKClick)
199END_EVENT_TABLE()
200
202 m_OKButton = NULL;
203 m_CancelButton = NULL;
204 premtext = NULL;
205#ifdef __ANDROID__
206 androidEnableRotation();
207#endif
208}
209
210PINCreateDialog::PINCreateDialog(wxWindow* parent, wxWindowID id,
211 const wxString& caption, const wxString& hint,
212 const wxPoint& pos, const wxSize& size,
213 long style) {
214 wxFont* pif = FontMgr::Get().GetFont(_("Dialog"));
215 SetFont(*pif);
216 Create(parent, id, caption, hint, pos, size, style);
217#ifdef __ANDROID__
218 androidDisableRotation();
219#endif
220}
221
222PINCreateDialog::~PINCreateDialog() {
223 delete m_OKButton;
224 delete m_CancelButton;
225#ifdef __ANDROID__
226 androidEnableRotation();
227#endif
228}
229
230wxDialog* PINCreateDialog::Initiate(const std::string& msg,
231 const std::string& text1) {
232 auto dlg = new PINCreateDialog(
233 wxTheApp->GetTopWindow(), wxID_ANY, _("OpenCPN Server Message"), "",
234 wxDefaultPosition, wxDefaultSize, SYMBOL_STG_STYLE);
235 dlg->SetMessage(msg);
236 dlg->SetText1Message(text1);
237 dlg->Show();
238 return dlg;
239}
240
241void PINCreateDialog::DeInit() {
242 Close();
243 Destroy();
244}
245
246bool PINCreateDialog::Create(wxWindow* parent, wxWindowID id,
247 const wxString& caption, const wxString& hint,
248 const wxPoint& pos, const wxSize& size,
249 long style) {
250 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS);
251 wxDialog::Create(parent, id, caption, pos, size, style);
252
253 CreateControls(hint);
254 GetSizer()->Fit(this);
255 GetSizer()->SetSizeHints(this);
256 Centre();
257
258 return TRUE;
259}
260
261void PINCreateDialog::CreateControls(const wxString& hint) {
262 PINCreateDialog* itemDialog1 = this;
263
264 wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
265 SetSizer(itemBoxSizer2);
266
267 // Add a reminder text box
268 itemBoxSizer2->AddSpacer(20);
269
270 premtext = new wxStaticText(
271 this, -1, "A loooooooooooooooooooooooooooooooooooooooooooooong line\n");
272 itemBoxSizer2->Add(premtext, 0, wxEXPAND | wxALL, 10);
273
274 m_pText1 = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition,
275 wxDefaultSize, wxTE_READONLY | wxTE_CENTRE);
276 itemBoxSizer2->Add(m_pText1, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
277 m_pText1->SetMinSize(wxSize(7 * GetCharWidth(), -1));
278
279 // OK/Cancel/etc.
280 wxBoxSizer* itemBoxSizer16 = new wxBoxSizer(wxHORIZONTAL);
281 itemBoxSizer2->Add(itemBoxSizer16, 0, wxALIGN_RIGHT | wxALL, 5);
282
283 m_CancelButton = new wxButton(itemDialog1, ID_STG_CANCEL, _("Cancel"),
284 wxDefaultPosition, wxDefaultSize, 0);
285 itemBoxSizer16->Add(m_CancelButton, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
286
287 m_OKButton = new wxButton(itemDialog1, ID_STG_OK, "OK", wxDefaultPosition,
288 wxDefaultSize, 0);
289 itemBoxSizer16->Add(m_OKButton, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
290 m_OKButton->SetDefault();
291}
292
293void PINCreateDialog::SetMessage(const wxString& msg) {
294 if (premtext) {
295 premtext->SetLabel(msg);
296 premtext->Refresh(true);
297 }
298}
299
300void PINCreateDialog::SetText1Message(const wxString& msg) {
301 m_pText1->ChangeValue(msg);
302 m_pText1->Show();
303 GetSizer()->Fit(this);
304}
305
306void PINCreateDialog::OnOKClick(wxCommandEvent& event) { Close(); }
307
308void 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:193
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.
Platform independent GL includes.
REST server dialogs.
Manage routes dialog.
Returned status from RunAcceptObjectDlg.
Definition rest_server.h:75