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