OpenCPN Partial API docs
Loading...
Searching...
No Matches
SendToGpsDlg.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 "conn_params.h"
38#include "OCPNPlatform.h"
39#include "route_gui.h"
40#include "route.h"
41#include "route_point_gui.h"
42#include "route_point.h"
43#include "SendToGpsDlg.h"
44#include "ser_ports.h"
45
46extern OCPNPlatform* g_Platform;
47extern wxString g_uploadConnection;
48
49IMPLEMENT_DYNAMIC_CLASS(SendToGpsDlg, wxDialog)
50
51BEGIN_EVENT_TABLE(SendToGpsDlg, wxDialog)
52EVT_BUTTON(ID_STG_CANCEL, SendToGpsDlg::OnCancelClick)
53 EVT_BUTTON(ID_STG_OK, SendToGpsDlg::OnSendClick) END_EVENT_TABLE()
54
56 m_itemCommListBox = NULL;
57 m_pgauge = NULL;
58 m_SendButton = NULL;
59 m_CancelButton = NULL;
60 m_pRoute = NULL;
61 m_pRoutePoint = NULL;
62 premtext = NULL;
63}
64
65SendToGpsDlg::SendToGpsDlg(wxWindow* parent, wxWindowID id,
66 const wxString& caption, const wxString& hint,
67 const wxPoint& pos, const wxSize& size, long style) {
68 Create(parent, id, caption, hint, pos, size, style);
69}
70
71SendToGpsDlg::~SendToGpsDlg() {
72 delete m_itemCommListBox;
73 delete m_pgauge;
74 delete m_SendButton;
75 delete m_CancelButton;
76}
77
78bool SendToGpsDlg::Create(wxWindow* parent, wxWindowID id,
79 const wxString& caption, const wxString& hint,
80 const wxPoint& pos, const wxSize& size, long style) {
81 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS);
82 wxDialog::Create(parent, id, caption, pos, size, style);
83
84 CreateControls(hint);
85 GetSizer()->Fit(this);
86 GetSizer()->SetSizeHints(this);
87 Centre();
88
89 return TRUE;
90}
91
92void SendToGpsDlg::CreateControls(const wxString& hint) {
93 SendToGpsDlg* itemDialog1 = this;
94
95 wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
96 itemDialog1->SetSizer(itemBoxSizer2);
97
98 // Create the ScrollBox list of available com ports in a labeled static
99 // box
100 wxStaticBox* comm_box =
101 new wxStaticBox(this, wxID_ANY, _("GPS/Plotter Port"));
102
103 wxStaticBoxSizer* comm_box_sizer = new wxStaticBoxSizer(comm_box, wxVERTICAL);
104 itemBoxSizer2->Add(comm_box_sizer, 0, wxEXPAND | wxALL, 5);
105
106 wxArrayString* pSerialArray = EnumerateSerialPorts();
107
108 m_itemCommListBox = new wxComboBox(this, ID_STG_CHOICE_COMM);
109
110 // Fill in the listbox with all detected serial ports
111 for (unsigned int iPortIndex = 0; iPortIndex < pSerialArray->GetCount();
112 iPortIndex++) {
113 wxString full_port = pSerialArray->Item(iPortIndex);
114 full_port.Prepend(_T("Serial:"));
115 m_itemCommListBox->Append(full_port);
116 }
117
118 delete pSerialArray;
119
120 // Add any defined Network connections supporting "output"
121 wxArrayString netconns;
122 for (size_t i = 0; i < TheConnectionParams()->Count(); i++) {
123 ConnectionParams* cp = TheConnectionParams()->Item(i);
124 wxString netident;
125
126 if ((cp->IOSelect != DS_TYPE_INPUT) && cp->Type == NETWORK &&
127 (cp->NetProtocol == TCP)) {
128 netident << _T("TCP:") << cp->NetworkAddress << _T(":")
129 << cp->NetworkPort;
130 m_itemCommListBox->Append(netident);
131 netconns.Add(netident);
132 }
133 if ((cp->IOSelect != DS_TYPE_INPUT) && cp->Type == NETWORK &&
134 (cp->NetProtocol == UDP)) {
135 netident << _T("UDP:") << cp->NetworkAddress << _T(":")
136 << cp->NetworkPort;
137 m_itemCommListBox->Append(netident);
138 netconns.Add(netident);
139 }
140 }
141
142 // Add Bluetooth, if the platform supports it natively
143 if (g_Platform) {
144 if (g_Platform->startBluetoothScan()) {
145 wxSleep(2);
146 wxArrayString btscanResults = g_Platform->getBluetoothScanResults();
147
148 unsigned int i = 1;
149 while ((i + 1) < btscanResults.GetCount()) {
150 wxString item1 = btscanResults[i] + _T(";");
151 wxString item2 = btscanResults.Item(i + 1);
152 wxString port = item1 + item2;
153 port.Prepend(_T("Bluetooth:"));
154 m_itemCommListBox->Append(port);
155
156 i += 2;
157 }
158
159 g_Platform->stopBluetoothScan();
160 }
161 }
162
163 // Make the proper initial selection
164 if (!g_uploadConnection.IsEmpty()) {
165 if (g_uploadConnection.Lower().StartsWith("tcp") ||
166 g_uploadConnection.Lower().StartsWith("udp")) {
167 bool b_connExists = false;
168 for (unsigned int i = 0; i < netconns.GetCount(); i++) {
169 if (g_uploadConnection.IsSameAs(netconns[i])) {
170 b_connExists = true;
171 break;
172 }
173 }
174 if (b_connExists) m_itemCommListBox->SetValue(g_uploadConnection);
175 } else
176 m_itemCommListBox->SetValue(g_uploadConnection);
177 } else
178 m_itemCommListBox->SetSelection(0);
179
180 comm_box_sizer->Add(m_itemCommListBox, 0, wxEXPAND | wxALL, 5);
181
182 // Add a reminder text box
183 itemBoxSizer2->AddSpacer(20);
184
185 premtext = new wxStaticText(
186 this, -1, _("Prepare GPS for Route/Waypoint upload and press Send..."));
187 itemBoxSizer2->Add(premtext, 0, wxEXPAND | wxALL, 10);
188
189 // Create a progress gauge
190 wxStaticBox* prog_box = new wxStaticBox(this, wxID_ANY, _("Progress..."));
191
192 wxStaticBoxSizer* prog_box_sizer = new wxStaticBoxSizer(prog_box, wxVERTICAL);
193 itemBoxSizer2->Add(prog_box_sizer, 0, wxEXPAND | wxALL, 5);
194
195 m_pgauge = new wxGauge(this, -1, 100);
196 prog_box_sizer->Add(m_pgauge, 0, wxEXPAND | wxALL, 5);
197
198 // OK/Cancel/etc.
199 wxBoxSizer* itemBoxSizer16 = new wxBoxSizer(wxHORIZONTAL);
200 itemBoxSizer2->Add(itemBoxSizer16, 0, wxALIGN_RIGHT | wxALL, 5);
201
202 m_CancelButton = new wxButton(itemDialog1, ID_STG_CANCEL, _("Cancel"),
203 wxDefaultPosition, wxDefaultSize, 0);
204 itemBoxSizer16->Add(m_CancelButton, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
205
206 m_SendButton = new wxButton(itemDialog1, ID_STG_OK, _("Send"),
207 wxDefaultPosition, wxDefaultSize, 0);
208 itemBoxSizer16->Add(m_SendButton, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
209 m_SendButton->SetDefault();
210}
211
212void SendToGpsDlg::SetMessage(wxString msg) {
213 if (premtext) {
214 premtext->SetLabel(msg);
215 premtext->Refresh(true);
216 }
217}
218
219void SendToGpsDlg::OnSendClick(wxCommandEvent& event) {
220 // Get the selected comm port
221 wxString src = m_itemCommListBox->GetValue();
222 int tail = src.Find(" - ");
223 if (tail != wxNOT_FOUND) {
224 src = src.SubString(0, tail);
225 }
226 if (!src.Lower().StartsWith("tcp") && !src.Lower().StartsWith("udp") &&
227 !src.Lower().StartsWith("serial") && !src.Lower().StartsWith("usb:") &&
228 !src.Lower().StartsWith("bluetooth")) {
229 src = src.Prepend("Serial:");
230 }
231 g_uploadConnection = src; // save for persistence
232
233 wxString destPort = src.BeforeFirst(' '); // Serial:
234
235 // For Bluetooth, we need the entire string
236 if (src.Lower().Find(_T("Bluetooth")) != wxNOT_FOUND) destPort = src;
237
238 // And send it out
239 if (m_pRoute) RouteGui(*m_pRoute).SendToGPS(destPort, true, this);
240 if (m_pRoutePoint) RoutePointGui(*m_pRoutePoint).SendToGPS(destPort, this);
241
242 // Show( false );
243 // event.Skip();
244 Close();
245}
246
247void SendToGpsDlg::OnCancelClick(wxCommandEvent& event) {
248 // Show( false );
249 // event.Skip();
250 Close();
251}
Route "Send to GPS..." Dialog Definition.
Definition: SendToGpsDlg.h:56