37#include <wx/spinctrl.h>
38#include <wx/textctrl.h>
41#include "libgui/expand_panel.h"
55#include "color_types.h"
62#include "std_filesystem.h"
64static constexpr auto TopScrollWindowName =
"TopScroll";
66static const char* kInfoHeader = _(
"OpenCPN help").c_str();
67static const char* kInfo = _(R
"---(
68Normally OpenCPN sends the RMB (distance and heading to waypoint
69etc) and RMC (position, heading etc.) sentences when there is an
70active waypoint. The talker ID is by default EC.
72In certain cases these messages (ECRMB and ECRMC) are required
73also when there is no active waypoint. This option enables this.
75Obviously, this option carries risks for vessels which have an
76autopilot which is not engaged manually -- the "fake" RMB messages
77could possibly initiate the autopilot in dangerous ways.
79Devices needing this includes the NASA Clipper GPS Repeater. In this
80case the output must also be filtered so that ECRMB and ECRMC are
81the only transmitted messages.
85static const char* kInterfaceExistsMessage =
86 _(
"Warning: A driver using this interface already exists.").c_str();
88static wxString UtfArrowDown() {
89 return wxString::FromUTF8(
reinterpret_cast<const char*
>(u8
"\u25bc"));
91static wxString UtfArrowRight() {
92 return wxString::FromUTF8(
reinterpret_cast<const char*
>(u8
"\u25ba"));
94static wxString UtfCheckMark() {
95 return wxString::FromUTF8(
reinterpret_cast<const char*
>(u8
"\u2713"));
97static wxString UtfGear() {
98 return wxString::FromUTF8(
reinterpret_cast<const char*
>(u8
"\u2699"));
100static wxString UtfFilledCircle() {
101 return wxString::FromUTF8(
reinterpret_cast<const char*
>(u8
"\u25cf"));
103static wxString UtfOpenCircle() {
104 return wxString::FromUTF8(
reinterpret_cast<const char*
>(u8
"\u25c8"));
106static wxString UtfWastebasket() {
107 return wxString::FromUTF8(
reinterpret_cast<const char*
>(u8
"\U0001f5d1"));
110static bool IsWindows() {
111 return wxPlatformInfo::Get().GetOperatingSystemId() & wxOS_WINDOWS;
114static bool IsAndroid() {
122static std::string BitsToDottedMask(
const unsigned bits) {
123 const uint32_t mask = 0xffffffff << (32 - bits);
124 std::stringstream ss;
125 ss << ((mask & 0xff000000) >> 24) <<
".";
126 ss << ((mask & 0x00ff0000) >> 16) <<
".";
127 ss << ((mask & 0x0000ff00) >> 8) <<
".";
128 ss << (mask & 0x000000ff);
134 if (cp->type != new_cp->type)
continue;
137 if (cp->serial_port != new_cp->serial_port)
continue;
141 if (cp->net_protocol != new_cp->net_protocol)
continue;
142 if (cp->network_address != new_cp->network_address)
continue;
143 if (cp->network_port != new_cp->network_port)
continue;
153 if (cp->socket_can_port != new_cp->socket_can_port)
continue;
157 if (cp->serial_port != new_cp->serial_port)
continue;
168 const std::unique_ptr<ConnectionParams>& cp) {
169 return FindConnectionByIface(cp.get());
178class BitmapEnableCellRenderer :
public wxGridCellBoolRenderer {
180 BitmapEnableCellRenderer(
int _size, ColorScheme cs) : size(_size), m_cs(cs) {}
182 [[nodiscard]] BitmapEnableCellRenderer* Clone()
const override {
183 return new BitmapEnableCellRenderer(size, m_cs);
186 wxSize GetBestSize(wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
int row,
199 explicit ConnCompare(
int col) : m_col(col) {}
201 bool operator()(
const ConnectionParams* p1,
202 const ConnectionParams* p2)
const {
205 return static_cast<int>(p1->is_enabled) >
206 static_cast<int>(p2->is_enabled);
230 virtual ~ApplyCancel() =
default;
233 virtual void Apply() = 0;
238 virtual void Cancel() = 0;
242class AddConnectionButton final :
public wxButton {
244 AddConnectionButton(wxWindow* parent, obs::EventVar& evt_add_connection,
245 const std::function<
void(ConnectionParams* p,
246 bool editing)>& _start_edit_conn)
247 : wxButton(parent, wxID_ANY, _(
"Add new connection...")),
248 m_evt_add_connection(evt_add_connection),
249 m_start_edit_conn(_start_edit_conn) {
250 Bind(wxEVT_COMMAND_BUTTON_CLICKED,
251 [&](wxCommandEvent&) { OnAddConnection(); });
255 void OnAddConnection()
const { m_start_edit_conn(
nullptr,
false); }
257 obs::EventVar& m_evt_add_connection;
258 std::function<void(ConnectionParams* p,
bool editing)> m_start_edit_conn;
262class Connections final :
public wxGrid {
264 Connections(wxWindow* parent,
265 const std::vector<ConnectionParams*>& connections,
266 obs::EventVar& on_conn_update,
267 const std::function<
void(ConnectionParams* p,
bool editing)>&
269 : wxGrid(parent, wxID_ANY),
270 m_connections(connections),
271 m_header_column_widths({0, 0, 0, 0, 0, 0, 0}),
272 m_last_tooltip_cell(100),
273 m_cs(GLOBAL_COLOR_SCHEME_DAY),
274 m_on_conn_delete(on_conn_update),
275 m_on_edit_conn(on_edit_conn) {
276 ShowScrollbars(wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
277 SetTable(
new wxGridStringTable(),
true);
278 GetTable()->AppendCols(8);
281 SetDefaultRowSize(wxWindow::GetCharHeight() * 2);
282 SetColLabelSize(wxWindow::GetCharHeight() * 2);
284 static const std::array<wxString, 7> headers = {
285 "", _(
"Protocol") +
" ", _(
"In/Out"), _(
"Data port"), _(
"Status"),
"",
288 for (
auto hdr = headers.begin(); hdr != headers.end(); hdr++, ic++) {
289 SetColLabelValue(
static_cast<int>(hdr - headers.begin()), *hdr);
290 unsigned col_width = hdr->Length() * wxWindow::GetCharWidth();
291 col_width = wxMax(col_width, 6 * wxWindow::GetCharWidth());
292 m_header_column_widths[ic] =
static_cast<int>(col_width);
293 SetColSize(ic,
static_cast<int>(col_width));
297 SetLabelBackgroundColour(GetGlobalColor(
"DILG1"));
298 SetLabelTextColour(GetGlobalColor(
"DILG3"));
301 SetColAttributes(parent);
304 ReloadGrid(connections);
305 DisableDragColSize();
306 DisableDragRowSize();
307 wxWindow::Show(GetNumberRows() > 0);
309 GetGridWindow()->Bind(wxEVT_MOTION, [&](wxMouseEvent& ev) {
313 GetGridWindow()->Bind(wxEVT_MOUSEWHEEL,
314 [&](wxMouseEvent& ev) { OnWheel(ev); });
316 Bind(wxEVT_GRID_LABEL_LEFT_CLICK,
317 [&](wxGridEvent& ev) { HandleSort(ev.GetCol()); });
318 Bind(wxEVT_GRID_CELL_LEFT_CLICK,
319 [&](wxGridEvent& ev) { OnClickCell(ev.GetRow(), ev.GetCol()); });
320 Bind(wxEVT_PAINT, [&](wxPaintEvent& ev) {
321 SetColAttributes(
dynamic_cast<const wxWindow*
>(ev.GetEventObject()));
324 Bind(wxEVT_SYS_COLOUR_CHANGED,
325 [&](wxSysColourChangedEvent& ev) { OnColorChange(ev); });
326 conn_change_lstnr.Init(
327 m_conn_states.evt_conn_status_change,
328 [&](ObservedEvt&) { OnConnectionChange(m_connections); });
331 void SetColorScheme(
const ColorScheme cs) {
333 ReloadGrid(m_connections);
336 void OnColorChange() {
337 auto foreground = wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT);
338 auto background = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
339 SetLabelBackgroundColour(background);
340 SetLabelTextColour(foreground);
341 for (
int row = 0; row <= GetNumberRows(); row++) {
342 for (
unsigned col = 0; col < 7; col++) {
343 SetCellBackgroundColour(row, col, background);
344 SetCellTextColour(row, col, foreground);
349 void OnColorChange(wxSysColourChangedEvent& ev) {
354 wxSize GetEstimatedSize()
const {
356 for (
auto s : m_header_column_widths) rs += s;
357 return {
static_cast<int>(rs), -1};
361 void OnWheel(wxMouseEvent& ev) {
367 dynamic_cast<wxScrolledWindow*
>(FindWindowByName(TopScrollWindowName));
371 w->GetViewStart(&xpos, &ypos);
374 w->GetScrollPixelsPerUnit(&x, &y);
376 const int dir = ev.GetWheelRotation();
377 w->Scroll(-1, ypos - dir / y / 4);
381 void ReloadGrid(
const std::vector<ConnectionParams*>& connections) {
384 for (
auto it = connections.begin(); it != connections.end(); ++it) {
385 const auto row =
static_cast<int>(it - connections.begin());
387 SetCellValue(row, 0, (*it)->is_enabled ?
"1" :
"");
388 if ((*it)->is_enabled)
389 m_tooltips[row][0] = _(
"Enabled, click to disable");
391 m_tooltips[row][0] = _(
"Disabled, click to enable");
392 std::string protocol = NavAddr::BusToString((*it)->GetCommProtocol());
393 SetCellValue(row, 1, protocol);
394 SetCellValue(row, 2, (*it)->GetPortDirectionValueStr());
395 SetCellValue(row, 3, (*it)->GetStrippedDSPort());
396 m_tooltips[row][3] = (*it)->user_comment;
397 SetCellValue(row, 5, UtfGear());
398 m_tooltips[row][5] = _(
"Edit connection");
399 SetCellValue(row, 6, UtfWastebasket());
400 m_tooltips[row][6] = _(
"Delete connection");
401 SetCellValue(row, 7, (*it)->GetKey());
404 wxString sp(protocol);
405 unsigned size = sp.Length() * wxWindow::GetCharWidth();
406 m_header_column_widths[1] = std::max(m_header_column_widths[1], size);
407 size = (*it)->GetPortDirectionValueStr().Length() *
408 wxWindow::GetCharWidth();
409 m_header_column_widths[2] = std::max(m_header_column_widths[2], size);
410 sp = wxString((*it)->GetStrippedDSPort());
411 size = sp.Length() * wxWindow::GetCharWidth();
412 m_header_column_widths[3] = std::max(m_header_column_widths[3], size);
415 OnConnectionChange(m_connections);
420 for (
auto val : m_header_column_widths) {
421 SetColSize(ic,
static_cast<int>(val));
429 class ConnStateCompare {
431 explicit ConnStateCompare(Connections* connections)
432 : m_conns(connections) {}
433 bool operator()(
const ConnectionParams* p1,
434 const ConnectionParams* p2)
const {
435 int row1 = m_conns->FindConnectionIndex(p1);
436 int row2 = m_conns->FindConnectionIndex(p2);
437 if (row1 == -1 && row2 == -1)
return false;
438 if (row1 == -1)
return false;
439 if (row2 == -1)
return true;
440 const int v1 = m_conns->GetCellValue(row1, 4)[0];
441 const int v2 = m_conns->GetCellValue(row2, 4)[0];
444 Connections* m_conns;
451 int FindConnectionIndex(
const ConnectionParams* cp)
const {
454 auto found = find_if(
455 m_connections.begin(), m_connections.end(),
456 [key](
const ConnectionParams* cp) { return cp->GetKey() == key; });
457 if (found == m_connections.end())
return -1;
458 return static_cast<int>(found - m_connections.begin());
466 ConnectionParams* FindRowConnection(
const int row)
const {
467 auto found = find_if(m_connections.begin(), m_connections.end(),
468 [&](
const ConnectionParams* p) {
469 return GetCellValue(row, 7) == p->GetKey();
471 return found != m_connections.end() ? *found :
nullptr;
478 void EnsureRows(
size_t rows) {
479 for (
unsigned i = m_tooltips.size(); i <= rows; i++)
480 m_tooltips.emplace_back(std::vector<std::string>(7));
481 for (
unsigned i = GetNumberRows(); i <= rows; i++) AppendRows(1,
false);
485 void SetColAttributes(
const wxWindow* parent) {
488 SetDefaultCellBackgroundColour(GetGlobalColor(
"DILG1"));
490 auto enable_attr =
new wxGridCellAttr();
491 enable_attr->SetAlignment(wxALIGN_CENTRE, wxALIGN_CENTRE);
494 enable_attr->SetRenderer(
495 new BitmapEnableCellRenderer(wxWindow::GetCharWidth() * 3 / 2, m_cs));
497 enable_attr->SetRenderer(
new wxGridCellBoolRenderer());
499 enable_attr->SetEditor(
new wxGridCellBoolEditor());
500 if (IsWindows()) enable_attr->SetBackgroundColour(GetGlobalColor(
"DILG1"));
501 SetColAttr(0, enable_attr);
503 auto protocol_attr =
new wxGridCellAttr();
504 protocol_attr->SetAlignment(wxALIGN_LEFT, wxALIGN_CENTRE);
505 protocol_attr->SetReadOnly(
true);
507 protocol_attr->SetBackgroundColour(GetGlobalColor(
"DILG1"));
508 SetColAttr(1, protocol_attr);
510 auto in_out_attr =
new wxGridCellAttr();
511 in_out_attr->SetAlignment(wxALIGN_CENTRE, wxALIGN_CENTRE);
512 in_out_attr->SetReadOnly(
true);
513 if (IsWindows()) in_out_attr->SetBackgroundColour(GetGlobalColor(
"DILG1"));
514 SetColAttr(2, in_out_attr);
516 auto port_attr =
new wxGridCellAttr();
517 port_attr->SetAlignment(wxALIGN_LEFT, wxALIGN_CENTRE);
518 port_attr->SetReadOnly(
true);
519 if (IsWindows()) port_attr->SetBackgroundColour(GetGlobalColor(
"DILG1"));
520 SetColAttr(3, port_attr);
522 auto status_attr =
new wxGridCellAttr();
523 status_attr->SetAlignment(wxALIGN_CENTRE, wxALIGN_CENTRE);
524 status_attr->SetReadOnly(
true);
525 status_attr->SetFont(parent->GetFont().Scale(1.3));
526 if (IsWindows()) status_attr->SetBackgroundColour(GetGlobalColor(
"DILG1"));
527 SetColAttr(4, status_attr);
529 auto edit_attr =
new wxGridCellAttr();
530 edit_attr->SetAlignment(wxALIGN_CENTRE, wxALIGN_CENTRE);
531 edit_attr->SetFont(parent->GetFont().Scale(1.3));
532 edit_attr->SetReadOnly(
true);
533 if (IsWindows()) edit_attr->SetBackgroundColour(GetGlobalColor(
"DILG1"));
534 SetColAttr(5, edit_attr);
536 auto delete_attr =
new wxGridCellAttr();
537 delete_attr->SetAlignment(wxALIGN_LEFT, wxALIGN_CENTRE);
538 delete_attr->SetFont(parent->GetFont().Scale(1.3));
539 delete_attr->SetTextColour(*wxRED);
540 delete_attr->SetReadOnly(
true);
541 if (IsWindows()) delete_attr->SetBackgroundColour(GetGlobalColor(
"DILG1"));
542 SetColAttr(6, delete_attr);
546 void OnClickCell(
int row,
int col) {
551 }
else if (col == 6) {
557 void OnMouseMove(wxMouseEvent& ev) {
558 if (!IsShown())
return;
559 const wxPoint pt = ev.GetPosition();
560 int row = YToRow(pt.y);
561 int col = XToCol(pt.x);
562 if (col < 0 || col >= 7 || row < 0 || row >= GetNumberRows())
return;
563 if (row * 7 + col == m_last_tooltip_cell)
return;
564 m_last_tooltip_cell = row * 7 + col;
565 GetGridWindow()->SetToolTip(m_tooltips[row][col]);
570 void OnConnectionChange(
const std::vector<ConnectionParams*>& connections) {
571 bool refresh_needed =
false;
572 for (
auto it = connections.begin(); it != connections.end(); ++it) {
573 ConnState state = m_conn_states.GetDriverState(
574 (*it)->GetCommProtocol(), (*it)->GetStrippedDSPort());
575 if (!(*it)->is_enabled) state = ConnState::Disabled;
576 auto row =
static_cast<int>(it - connections.begin());
579 case ConnState::Disabled:
580 SetCellValue(row, 4, UtfFilledCircle());
581 m_tooltips[row][4] = _(
"Disabled");
582 refresh_needed =
true;
584 case ConnState::NoStats:
585 SetCellValue(row, 4, UtfOpenCircle());
586 m_tooltips[row][4] = _(
"No driver statistics available");
587 refresh_needed =
true;
589 case ConnState::NoData:
590 SetCellValue(row, 4,
"!");
591 m_tooltips[row][4] = _(
"No data flowing through connection");
592 refresh_needed =
true;
594 case ConnState::Unavailable:
595 SetCellValue(row, 4,
"X");
596 m_tooltips[row][4] = _(
"The device is unavailable");
597 refresh_needed =
true;
600 SetCellValue(row, 4, UtfCheckMark());
601 m_tooltips[row][4] = _(
"Data is flowing");
602 refresh_needed =
true;
606 if (refresh_needed) ForceRefresh();
610 void SetSortingColumn(
int col) {
611 if (GetSortingColumn() != wxNOT_FOUND) {
612 int old_col = GetSortingColumn();
613 auto label = GetColLabelValue(old_col);
614 if (label[0] == UtfArrowDown()[0])
615 SetColLabelValue(old_col, label.substr(2));
617 auto label = GetColLabelValue(col);
618 if (label[0] != UtfArrowDown()[0])
619 SetColLabelValue(col, UtfArrowDown() +
" " + label);
620 wxGrid::SetSortingColumn(col);
625 void HandleSort(
int col) {
629 std::sort(params.begin(), params.end(), ConnCompare(col));
631 std::sort(params.begin(), params.end(), ConnStateCompare(
this));
633 SetSortingColumn(col);
637 void HandleEnable(
int row) {
638 ConnectionParams* cp = FindRowConnection(row);
640 cp->is_enabled = !cp->is_enabled;
641 cp->is_setup = FALSE;
642 SetCellValue(row, 0, cp->is_enabled ?
"1" :
"");
644 m_tooltips[row][0] = _(
"Enabled, click to disable");
646 m_tooltips[row][0] = _(
"Disabled, click to enable");
650 m_conn_states.HandleDriverStats(stats);
654 if (!cp->is_enabled) {
655 SetCellValue(row, 4, UtfFilledCircle());
662 void HandleEdit(
int row) {
663 if (ConnectionParams* cp = FindRowConnection(row)) {
664 Show(GetNumberRows() > 0);
665 m_on_edit_conn(cp,
true);
670 void HandleDelete(
int row) {
671 ConnectionParams* cp = FindRowConnection(row);
672 auto found = std::find(m_connections.begin(), m_connections.end(), cp);
673 if (found != m_connections.end()) {
674 std::stringstream ss;
675 ss << _(
"Ok to delete connection on ") << (*found)->GetStrippedDSPort();
676 int rcode = OCPNMessageBox(
this, ss.str(), _(
"Delete connection?"),
678 if (rcode != wxID_OK && rcode != wxID_YES)
return;
679 delete (*found)->options_panel;
680 StopAndRemoveCommDriver((*found)->GetStrippedDSPort(),
681 (*found)->GetCommProtocol());
683 if (GetNumberRows() >
static_cast<int>(m_connections.size()))
684 DeleteRows(GetNumberRows() - 1);
685 m_on_conn_delete.Notify();
689 obs::Listener conn_change_lstnr;
690 std::vector<std::vector<std::string>> m_tooltips;
691 ConnStates m_conn_states;
692 const std::vector<ConnectionParams*>& m_connections;
693 std::array<unsigned, 7> m_header_column_widths;
694 int m_last_tooltip_cell;
696 obs::EventVar& m_on_conn_delete;
697 std::function<void(ConnectionParams* p,
bool editing)> m_on_edit_conn;
701class GeneralPanel :
public wxPanel {
703 explicit GeneralPanel(wxWindow* parent) : wxPanel(parent, wxID_ANY) {
704 auto sizer =
new wxStaticBoxSizer(wxVERTICAL,
this, _(
"General"));
706 auto flags = wxSizerFlags().Border();
707 m_upload_options =
new UploadOptionsChoice(
this);
708 sizer->Add(m_upload_options, flags);
709 sizer->Add(
new PrioritiesBtn(
this), flags);
712 void SetColorScheme(ColorScheme) {
713 DimeControl(m_upload_options);
719 class PrioritiesBtn final :
public wxButton {
721 explicit PrioritiesBtn(wxWindow* parent)
722 : wxButton(parent, wxID_ANY, _(
"Adjust Nav data priorities...")) {
723 Bind(wxEVT_COMMAND_BUTTON_CLICKED, [&](wxCommandEvent&) {
724 PriorityDlg dialog(this->GetParent());
731 class UploadOptionsChoice final :
public wxRadioBox,
public ApplyCancel {
733 explicit UploadOptionsChoice(wxWindow* parent) : wxRadioBox() {
734 wxArrayString wx_choices;
735 for (
auto& c : choices) wx_choices.Add(c);
736 Create(parent, wxID_ANY, _(
"Upload Format"), wxDefaultPosition,
737 wxDefaultSize, wx_choices, 0, wxRA_SPECIFY_ROWS);
739 UploadOptionsChoice::Cancel();
742 void Cancel()
override {
743 if (g_bGarminHostUpload)
745 else if (g_GPS_Ident ==
"FurunoGP3X")
751 void Apply()
override {
752 switch (GetSelection()) {
754 g_bGarminHostUpload =
false;
755 g_GPS_Ident =
"Generic";
758 g_bGarminHostUpload =
true;
759 g_GPS_Ident =
"Generic";
762 g_bGarminHostUpload =
false;
763 g_GPS_Ident =
"FurunoGP3X";
766 assert(
false &&
"Invalid upload case option");
770 const std::array<wxString, 3> choices = {
771 _(
"Generic NMEA 0183"), _(
"Garmin Host mode"), _(
"Furuno GP4X")};
774 UploadOptionsChoice* m_upload_options;
778class AdvancedPanel :
public wxPanel {
780 explicit AdvancedPanel(wxWindow* parent) : wxPanel(parent, wxID_ANY) {
781 auto sizer =
new wxStaticBoxSizer(wxVERTICAL,
this,
"");
782 sizer->Add(
new BearingsCheckbox(
this), wxSizerFlags().Expand());
783 sizer->Add(
new ExtraRmbRmcLine(
this), wxSizerFlags().Expand());
784 sizer->Add(
new NmeaFilterRow(
this), wxSizerFlags().Expand());
785 sizer->Add(
new TalkerIdRow(
this), wxSizerFlags().Expand());
786 sizer->Add(
new NetmaskRow(
this), wxSizerFlags().Expand());
792 class BearingsCheckbox :
public wxCheckBox,
public ApplyCancel {
794 explicit BearingsCheckbox(wxWindow* parent)
795 : wxCheckBox(parent, wxID_ANY,
796 _(
"Use magnetic bearing in output sentence APB")) {
797 wxCheckBox::SetValue(g_bMagneticAPB);
800 void Apply()
override { g_bMagneticAPB = GetValue(); }
801 void Cancel()
override { SetValue(g_bMagneticAPB); }
804 class ExtraRmbRmcLine :
public wxPanel {
805 class RmbRmcCheckbox :
public wxCheckBox,
public ApplyCancel {
807 explicit RmbRmcCheckbox(wxWindow* parent)
808 : wxCheckBox(parent, wxID_ANY,
809 _(
"Always send RMB and RMC NMEA0183 data")) {
818 explicit ExtraRmbRmcLine(wxWindow* parent) : wxPanel(parent, wxID_ANY) {
819 auto hbox =
new wxBoxSizer(wxHORIZONTAL);
820 hbox->Add(
new RmbRmcCheckbox(
this), wxSizerFlags().Expand());
821 hbox->Add(1, 1, 1, wxEXPAND);
822 hbox->Add(
new InfoButton(
this, g_btouch, kInfoHeader, kInfo),
823 wxSizerFlags().Border());
830 class ExtraRmbRmcCheckbox final :
public wxCheckBox,
public ApplyCancel {
832 explicit ExtraRmbRmcCheckbox(wxWindow* parent)
833 : wxCheckBox(parent, wxID_ANY,
834 _(
"Always send RMB and RMC NMEA0183 data")) {
843 class NmeaFilterRow :
public wxPanel,
public ApplyCancel {
844 wxCheckBox* checkbox;
845 wxTextCtrl* filter_period;
848 explicit NmeaFilterRow(wxWindow* parent) : wxPanel(parent) {
849 auto hbox =
new wxBoxSizer(wxHORIZONTAL);
850 checkbox =
new wxCheckBox(
852 _(
"Filter NMEA course and speed data. Filter period: "));
853 checkbox->SetValue(g_bfilter_cogsog);
854 hbox->Add(checkbox, wxSizerFlags().Align(wxALIGN_CENTRE));
856 new wxTextCtrl(
this, wxID_ANY,
"", wxDefaultPosition,
857 wxSize(50, 3 * wxWindow::GetCharWidth()), 0);
858 filter_period->SetValue(std::to_string(g_COGFilterSec));
859 hbox->Add(filter_period, wxSizerFlags().Border());
861 NmeaFilterRow::Cancel();
864 void Apply()
override {
865 std::stringstream ss;
866 ss << filter_period->GetValue();
867 ss >> g_COGFilterSec;
868 g_bfilter_cogsog = checkbox->GetValue();
871 void Cancel()
override {
872 std::stringstream ss;
873 ss << g_COGFilterSec;
874 filter_period->SetValue(ss.str());
875 checkbox->SetValue(g_bfilter_cogsog);
880 class TalkerIdRow :
public wxPanel,
public ApplyCancel {
881 wxTextCtrl* text_ctrl;
884 explicit TalkerIdRow(wxWindow* parent) : wxPanel(parent) {
885 auto hbox =
new wxBoxSizer(wxHORIZONTAL);
886 hbox->Add(
new wxStaticText(
this, wxID_ANY, _(
"NMEA 0183 Talker Id: ")),
887 wxSizerFlags().Align(wxALIGN_CENTRE_VERTICAL).Border());
888 text_ctrl =
new wxTextCtrl(
889 this, wxID_ANY,
"", wxDefaultPosition,
890 wxSize(4 * wxWindow::GetCharWidth(), 3 * wxWindow::GetCharWidth()));
891 text_ctrl->SetToolTip(
892 _(
"Enter a two-letter talker ID to override the default ID in NMEA "
893 "sentences generated by OpenCPN (e.g., GP, HC, EC). This affects "
894 "only sentences created by OpenCPN, not those forwarded from other "
896 text_ctrl->SetValue(g_TalkerIdText);
897 hbox->Add(text_ctrl, wxSizerFlags().Border());
899 TalkerIdRow::Cancel();
902 void Apply()
override { g_TalkerIdText = text_ctrl->GetValue(); }
903 void Cancel()
override { text_ctrl->SetValue(g_TalkerIdText); }
907 class NetmaskRow :
public wxPanel,
public ApplyCancel {
909 explicit NetmaskRow(wxWindow* parent)
911 m_spin_ctrl(new wxSpinCtrl(this, wxID_ANY)),
912 m_text(new wxStaticText(this, wxID_ANY,
"")) {
913 m_spin_ctrl->SetRange(8, 32);
914 auto hbox =
new wxBoxSizer(wxHORIZONTAL);
915 auto flags = wxSizerFlags().Align(wxALIGN_CENTRE_VERTICAL).Border();
916 hbox->Add(
new wxStaticText(
this, wxID_ANY, _(
"Netmask: ")), flags);
917 hbox->Add(m_text, flags);
918 hbox->Add(
new wxStaticText(
this, wxID_ANY, _(
"length (bits): ")), flags);
919 hbox->Add(m_spin_ctrl, flags);
921 NetmaskRow::Cancel();
923 Bind(wxEVT_SPINCTRL, [&](wxSpinEvent&) {
924 m_text->SetLabel(BitsToDottedMask(m_spin_ctrl->GetValue()));
929 void Apply()
override { g_netmask_bits = m_spin_ctrl->GetValue(); }
931 void Cancel()
override {
932 m_spin_ctrl->SetValue(g_netmask_bits);
933 m_text->SetLabel(BitsToDottedMask(m_spin_ctrl->GetValue()));
938 wxSpinCtrl* m_spin_ctrl;
939 wxStaticText* m_text;
944class TopPanel :
public wxPanel {
946 TopPanel(wxWindow* parent,
const std::vector<ConnectionParams*>& connections,
947 obs::EventVar& evt_add_connection,
948 const std::function<
void(ConnectionParams* p,
bool editing)>&
950 : wxPanel(parent, wxID_ANY),
951 m_evt_add_connection(evt_add_connection),
952 m_connections(connections) {
953 auto vbox =
new wxBoxSizer(wxVERTICAL);
954 auto conn_grid =
new Connections(
this, m_connections, m_evt_add_connection,
956 vbox->AddSpacer(wxWindow::GetCharHeight());
957 auto conn_flags = wxSizerFlags().Border();
958 if (IsAndroid()) conn_flags = wxSizerFlags().Border().Expand();
959 vbox->Add(conn_grid, conn_flags);
960 vbox->Add(
new AddConnectionButton(
this, m_evt_add_connection, on_edit_conn),
961 wxSizerFlags().Border());
962 vbox->Add(0, wxWindow::GetCharHeight());
964 wxSizerFlags().Border(wxLEFT | wxDOWN | wxRIGHT).Expand();
965 m_general_panel =
new GeneralPanel(
this);
966 m_general_panel->SetMinSize(m_general_panel->GetBestSize());
967 vbox->Add(m_general_panel, panel_flags);
969 auto advanced_panel =
new AdvancedPanel(
this);
970 m_advanced_panel = advanced_panel;
971 auto adv_box =
new wxBoxSizer(wxHORIZONTAL);
972 adv_box->Add(
new wxStaticText(
this, wxID_ANY, _(
"Advanced")));
973 adv_box->Add(
new ExpandablePanel(
this, advanced_panel,
974 [&] { GetParent()->Layout(); }));
975 vbox->Add(adv_box, wxSizerFlags().Border());
976 vbox->Add(advanced_panel,
977 wxSizerFlags().Border().ReserveSpaceEvenIfHidden());
980 vbox->SetSizeHints(
this);
985 auto on_evt_update_connections = [&, conn_grid](ObservedEvt&) {
987 conn_grid->Show(conn_grid->GetNumberRows() > 0);
991 m_add_connection_lstnr.Init(m_evt_add_connection,
992 on_evt_update_connections);
993 m_conn_grid = conn_grid;
996 void SetColorScheme(
const ColorScheme cs)
const {
997 m_conn_grid->SetColorScheme(cs);
998 m_general_panel->SetColorScheme(cs);
1000 [[nodiscard]] Connections* GetConnectionsGrid()
const {
return m_conn_grid; }
1002 obs::EventVar& m_evt_add_connection;
1005 const std::vector<ConnectionParams*>& m_connections;
1006 obs::Listener m_add_connection_lstnr;
1007 Connections* m_conn_grid;
1008 GeneralPanel* m_general_panel;
1009 AdvancedPanel* m_advanced_panel;
1015class EditPanel :
public wxScrolled<wxPanel> {
1017 EditPanel(wxWindow* parent,
const std::vector<ConnectionParams*>& connections,
1019 : wxScrolled<wxPanel>(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
1020 wxVSCROLL | wxHSCROLL),
1021 m_edited_connection(
nullptr),
1022 m_edit_panel(
nullptr) {
1023 auto vbox =
new wxBoxSizer(wxVERTICAL);
1034 new TopPanel(
this, connections, evt_add_connection, on_edit_connection);
1035 vbox->Add(m_top_panel, wxSizerFlags(1).Expand());
1036 auto on_cancel = [&] { SwitchToGrid(); };
1038 auto on_edit_click = [&](std::unique_ptr<ConnectionParams> params,
1040 HandleEditFinish(std::move(params), new_mode);
1044 AbstractConnectionEditDlg::Create(
this, on_cancel, on_edit_click);
1045 vbox->Add(m_edit_panel, wxSizerFlags(1).Expand());
1046 wxWindowBase::Layout();
1047 m_edit_panel->Hide();
1050 SetScrollRate(1, 1);
1052 SetScrollRate(10, 10);
1055 void SetColorScheme(ColorScheme cs)
const {
1056 if (m_top_panel) m_top_panel->SetColorScheme(cs);
1060 m_edited_connection = p;
1061 m_edit_panel->SetParams(std::make_unique<ConnectionParams>(*p));
1066 m_edit_panel->SetParams(
nullptr);
1070 void SwitchToEditor() {
1071 ShowScrollbars(wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
1072 m_top_panel->Hide();
1073 m_edit_panel->Scroll(0, 0);
1074 DimeControl(m_edit_panel);
1075 m_edit_panel->Show();
1080 void SwitchToGrid() {
1081 m_edit_panel->Hide();
1084 m_top_panel->Show();
1088 ShowScrollbars(wxSHOW_SB_DEFAULT, wxSHOW_SB_DEFAULT);
1091 void HandleEditFinish(std::unique_ptr<ConnectionParams> new_params,
1095 StopAndRemoveCommDriver(m_edited_connection->GetStrippedDSPort(),
1096 m_edited_connection->GetCommProtocol());
1097 assert(m_edited_connection &&
"No edited connection");
1098 int index = m_top_panel->GetConnectionsGrid()->FindConnectionIndex(
1099 m_edited_connection);
1100 assert(index != -1 &&
"Cannot look up connection index");
1103 m_edited_connection =
nullptr;
1107 if (FindConnectionByIface(&cp)) {
1108 wxMessageDialog dialog(
this, kInterfaceExistsMessage,
1109 _(
"Driver creation warning"),
1110 wxOK | wxCENTRE | wxICON_WARNING);
1115 cp.is_setup =
false;
1121 _(
"Unable to create a connection as configured. "
1122 "Connected port or address was missing.");
1123 auto& noteman = NotificationManager::GetInstance();
1124 noteman.AddNotification(NotificationSeverity::kWarning,
1125 msg.ToStdString(), 60);
1127 UpdateDatastreams();
1128 m_top_panel->m_evt_add_connection.Notify();
1132 UpdateDatastreams();
1138 TopPanel* m_top_panel;
1143 wxWindow* parent,
const std::vector<ConnectionParams*>& connections)
1144 : wxPanel(parent, wxID_ANY), m_connections(connections) {
1145 const auto vbox =
new wxBoxSizer(wxVERTICAL);
1146 vbox->Add(
new EditPanel(
this, connections, m_evt_add_connection),
1147 wxSizerFlags(1).Expand());
1153void ConnectionsDlg::DoApply(wxWindow* root) {
1154 for (wxWindow* child : root->GetChildren()) {
1155 if (
auto widget =
dynamic_cast<ApplyCancel*
>(child)) widget->Apply();
1160void ConnectionsDlg::DoCancel(wxWindow* root) {
1161 for (wxWindow* child : root->GetChildren()) {
1162 if (
auto widget =
dynamic_cast<ApplyCancel*
>(child)) widget->Cancel();
1171void ConnectionsDlg::SetColorScheme(ColorScheme cs) {
1172 auto w =
dynamic_cast<EditPanel*
>(FindWindowByName(TopScrollWindowName));
1173 if (w) w->SetColorScheme(cs);
Dialog editing existing or new connection parameters.
Connection data container close to a POD struct.
bool IsPortValid() const
Return true if port data like com port or network address are sane.
std::string GetStrippedDSPort() const
Return port string with possible windows extra data removed, in some cases empty.
std::string GetKey() const
Return string unique for each instance.
wxString GetPortDirectionValueStr() const
Return translated name for direction, like _("Input").
NavAddr::Bus GetCommProtocol() const
Return NavAddr::Bus corresponding to network address/port or serial parameters.
void ApplySettings()
Make dialog's settings the active ones, usually by updating globals.
ConnectionsDlg(wxWindow *parent, const std::vector< ConnectionParams * > &connections)
Create a new ConnectionsDlg instance.
void CancelSettings()
Restore dialog settings from currently used values, usually globals.
Generic event handling between for example MVC Model and Controller based on a shared EventVar variab...
Global color handling by name.
void MakeCommDriver(const ConnectionParams *params)
Create and register a driver for given connection.
Communication drivers factory and support.
bool g_always_send_rmb_rmc
Always send RMB and RMC n0183 messages even if there is no active route.
Global variables stored in configuration file.
std::vector< ConnectionParams * > & TheConnectionParams()
Return global list of connections.
Panel for editing a connection.
Runtime connection/driver state definitions.
Dialog and support code for editing a connection.
Options | Connections GUI tab managing connections
Misc GUI event vars, a singleton.
General purpose GUI support.
User notifications manager.
options * g_options
Global instance.
Input priorities management dialog.