OpenCPN Partial API docs
Loading...
Searching...
No Matches
peer_client_dlg.cpp
1/***************************************************************************
2 *
3 * Project: OpenCPN
4 * Purpose: Peer-peer data sharing.
5 * Author: David Register
6 *
7 ***************************************************************************
8 * Copyright (C) 2022 by David Register *
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24 **************************************************************************/
25
26#include <cassert>
27#include <iostream>
28#include <sstream>
29
30#include <wx/fileconf.h>
31#include <wx/json_defs.h>
32#include <wx/jsonreader.h>
33#include <wx/tokenzr.h>
34
35#include <curl/curl.h>
36
37#include "model/peer_client.h"
38#include "model/rest_server.h"
39#include "model/semantic_vers.h"
40#include "model/config_vars.h"
41
42#include "peer_client_dlg.h"
43#include "ocpn_frame.h"
44#include "FontMgr.h"
45#include "gui_lib.h"
46
47struct MemoryStruct {
48 char* memory;
49 size_t size;
50};
51
52PinConfirmDlg::PinConfirmDlg() {
53 m_ok_btn = NULL;
54 m_cancel_btn = NULL;
55 premtext = NULL;
56}
57
58PinConfirmDlg::PinConfirmDlg(wxWindow* parent, wxWindowID id,
59 const wxString& caption, const wxString& hint,
60 const wxPoint& pos, const wxSize& size,
61 long style) {
62 wxFont* pif = FontMgr::Get().GetFont(_("Dialog"));
63 SetFont(*pif);
64 Create(parent, id, caption, hint, pos, size, style);
65}
66
67PinConfirmDlg::~PinConfirmDlg() {
68 delete m_ok_btn;
69 delete m_cancel_btn;
70}
71
73void PinConfirmDlg::OnTextChange(wxCommandEvent&) {
74 auto txt = m_pin_textctrl->GetValue().ToStdString();
75 int value = -1;
76 if (txt.size() >= 4) {
77 try {
78 value = std::stoi(txt);
79 } catch (std::exception&) {
80 ;
81 }
82 }
83 m_ok_btn->Enable(value >= 0 && txt.size() >= 4);
84}
85
86bool PinConfirmDlg::Create(wxWindow* parent, wxWindowID id,
87 const wxString& caption, const wxString& hint,
88 const wxPoint& pos, const wxSize& size, long style) {
89 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS);
90 wxDialog::Create(parent, id, caption, pos, size, style);
91
92 CreateControls(hint);
93 GetSizer()->Fit(this);
94 GetSizer()->SetSizeHints(this);
95 Centre();
96 return TRUE;
97}
98
99void PinConfirmDlg::CreateControls(const wxString&) {
100 wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
101 SetSizer(itemBoxSizer2);
102
103 // Add a reminder text box
104 itemBoxSizer2->AddSpacer(20);
105
106 premtext = new wxStaticText(
107 this, -1, "A loooooooooooooooooooooooooooooooooooooooooooooong line\n");
108 itemBoxSizer2->Add(premtext, 0, wxEXPAND | wxALL, 10);
109
110 m_pin_textctrl = new wxTextCtrl(this, wxID_ANY, " ", wxDefaultPosition,
111 wxDefaultSize, wxTE_CENTRE);
112 itemBoxSizer2->Add(m_pin_textctrl, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 10);
113
114 // OK/Cancel/etc.
115 wxBoxSizer* itemBoxSizer16 = new wxBoxSizer(wxHORIZONTAL);
116 itemBoxSizer2->Add(itemBoxSizer16, 0, wxALIGN_RIGHT | wxALL, 5);
117
118 m_cancel_btn = new wxButton(this, wxID_CANCEL);
119
120 m_cancel_btn->Bind(wxEVT_COMMAND_BUTTON_CLICKED,
121 [&](wxCommandEvent e) { OnCancelClick(e); });
122 itemBoxSizer16->Add(m_cancel_btn, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
123
124 m_ok_btn = new wxButton(this, wxID_OK);
125 itemBoxSizer16->Add(m_ok_btn, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
126 m_ok_btn->SetDefault();
127 m_ok_btn->Bind(wxEVT_COMMAND_BUTTON_CLICKED,
128 [&](wxCommandEvent e) { OnOKClick(e); });
129 m_ok_btn->Enable(false);
130 m_pin_textctrl->Bind(wxEVT_TEXT,
131 [&](wxCommandEvent& ev) { OnTextChange(ev); });
132}
133
134void PinConfirmDlg::SetMessage(const wxString& msg) {
135 if (premtext) {
136 premtext->SetLabel(msg);
137 premtext->Refresh(true);
138 }
139}
140
141void PinConfirmDlg::SetPincodeText(const wxString& message) {
142 m_pin_textctrl->ChangeValue(message);
143 m_pin_textctrl->Show();
144 GetSizer()->Fit(this);
145}
146
147void PinConfirmDlg::OnOKClick(wxCommandEvent&) { EndModal(wxID_OK); }
148
149void PinConfirmDlg::OnCancelClick(wxCommandEvent&) { EndModal(wxID_CANCEL); }
wxFont * GetFont(const wxString &TextElement, int requested_font_size=0)
Gets a font object for a UI element.
Definition FontMgr.cpp:186
General purpose GUI support.