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