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