OpenCPN Partial API docs
Loading...
Searching...
No Matches
TTYWindow.cpp
1/******************************************************************************
2 *
3 * Project: OpenCPN
4 *
5 ***************************************************************************
6 * Copyright (C) 2013 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/sizer.h>
26#include <wx/statbox.h>
27#include <wx/bmpbuttn.h>
28#include <wx/dcmemory.h>
29#include <wx/dialog.h>
30#include <wx/settings.h>
31#include <wx/dcscreen.h>
32
33#include "TTYWindow.h"
34#include "tty_scroll.h"
35#include "WindowDestroyListener.h"
36#include "color_handler.h"
37#include "ocpn_plugin.h"
38#include "FontMgr.h"
39
40BEGIN_EVENT_TABLE(TTYWindow, wxFrame)
41EVT_CLOSE(TTYWindow::OnCloseWindow)
42END_EVENT_TABLE()
43
44TTYWindow::TTYWindow() : m_window_destroy_listener(NULL), m_tty_scroll(NULL) {}
45
46TTYWindow::TTYWindow(wxWindow* parent, int n_lines,
47 WindowDestroyListener* listener)
48 : m_window_destroy_listener(listener), m_tty_scroll(NULL) {
49 wxFrame::Create(
50 parent, -1, _T("Title"), wxDefaultPosition, wxDefaultSize,
51 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFRAME_FLOAT_ON_PARENT,
52 "NmeaDebugWindow");
53
54 wxBoxSizer* bSizerOuterContainer = new wxBoxSizer(wxVERTICAL);
55 SetSizer(bSizerOuterContainer);
56
57 m_filter = new wxTextCtrl(this, wxID_ANY);
58
59 m_tty_scroll = new TtyScroll(this, n_lines, *m_filter);
60 m_tty_scroll->Scroll(-1, 1000); // start with full scroll down
61
62 bSizerOuterContainer->Add(m_tty_scroll, 1, wxEXPAND, 5);
63
64 wxStaticBox* psbf = new wxStaticBox(this, wxID_ANY, _("Filter"));
65 wxStaticBoxSizer* sbSizer2 = new wxStaticBoxSizer(psbf, wxVERTICAL);
66 sbSizer2->Add(m_filter, 1, wxALL | wxEXPAND, 5);
67 bSizerOuterContainer->Add(sbSizer2, 0, wxEXPAND, 5);
68
69 wxBoxSizer* bSizerBottomContainer = new wxBoxSizer(wxHORIZONTAL);
70 bSizerOuterContainer->Add(bSizerBottomContainer, 0, wxEXPAND, 5);
71
72 wxStaticBox* psb = new wxStaticBox(this, wxID_ANY, _("Legend"));
73 wxStaticBoxSizer* sbSizer1 = new wxStaticBoxSizer(psb, wxVERTICAL);
74 bSizerBottomContainer->Add(sbSizer1, 0, wxALIGN_LEFT | wxALL, 5);
75
76 CreateLegendBitmap();
77 wxBitmapButton* bb = new wxBitmapButton(this, wxID_ANY, m_bm_legend);
78 sbSizer1->Add(bb, 1, wxALL | wxEXPAND, 5);
79
80 wxStaticBox* buttonBox = new wxStaticBox(this, wxID_ANY, "Tools");
81 wxStaticBoxSizer* bbSizer1 = new wxStaticBoxSizer(buttonBox, wxVERTICAL);
82
83 m_btn_pause = new wxButton(this, wxID_ANY, _("Pause"), wxDefaultPosition,
84 wxDefaultSize, 0);
85 m_btn_copy = new wxButton(this, wxID_ANY, _("Copy all"), wxDefaultPosition,
86 wxDefaultSize, 0);
87 m_btn_copy->SetToolTip(_("Copy all NMEA Debug window to clipboard."));
88 m_btn_copy_N0183 = new wxButton(this, wxID_ANY, _("Copy NMEA 0183"),
89 wxDefaultPosition, wxDefaultSize, 0);
90 m_btn_copy_N0183->SetToolTip(
91 _("Copy only pure NMEA 0183 sentences to clipboard."));
92
93 bbSizer1->Add(m_btn_pause, 0, wxALL, 5);
94 bbSizer1->Add(m_btn_copy, 0, wxALL, 5);
95 bbSizer1->Add(m_btn_copy_N0183, 0, wxALL, 5);
96 bSizerBottomContainer->Add(bbSizer1, 1, wxALL | wxEXPAND, 5);
97
98 m_btn_copy->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
99 wxCommandEventHandler(TTYWindow::OnCopyClick), NULL,
100 this);
101 m_btn_copy_N0183->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
102 wxCommandEventHandler(TTYWindow::OnCopyN0183Click),
103 NULL, this);
104 m_btn_pause->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
105 wxCommandEventHandler(TTYWindow::OnPauseClick), NULL,
106 this);
107
108 m_is_paused = false;
109}
110
111TTYWindow::~TTYWindow() {
112 if (m_tty_scroll) {
113 delete m_tty_scroll;
114 m_tty_scroll = NULL;
115 }
116}
117
118void TTYWindow::CreateLegendBitmap() {
119 double dip_factor = OCPN_GetWinDIPScaleFactor();
120 wxScreenDC dcs;
121 wxFont font(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
122 wxFONTWEIGHT_NORMAL);
123 int width, height;
124 dcs.GetTextExtent("M", &width, &height, NULL, NULL, &font);
125 double ref_dim = height * dip_factor;
126
127 m_bm_legend.Create(36 * width * dip_factor, 6.5 * ref_dim);
128 wxMemoryDC dc;
129 dc.SelectObject(m_bm_legend);
130 if (m_bm_legend.IsOk()) {
131 dc.SetBackground(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)));
132 dc.Clear();
133 dc.SetFont(font);
134
135 int yp = ref_dim * 1.25;
136 int y = ref_dim * .25;
137 int bsize = ref_dim;
138 int text_x = ref_dim * 1.5;
139 int boff = ref_dim * .25;
140
141 wxBrush b1(wxColour(_T("DARK GREEN")));
142 dc.SetBrush(b1);
143 dc.DrawRectangle(boff, y, bsize, bsize);
144 dc.SetTextForeground(wxColour(_T("DARK GREEN")));
145 dc.DrawText(_("Message accepted"), text_x, y);
146
147 y += yp;
148 wxBrush b2(wxColour(_T("CORAL")));
149 dc.SetBrush(b2);
150 dc.DrawRectangle(boff, y, bsize, bsize);
151 dc.SetTextForeground(wxColour(_T("CORAL")));
152 // Indicate message has been filtered and dropped.
153 // This could be an input or output message.
154 // For input messages, only used if m_legacy_input_filter_behaviour is true.
155 dc.DrawText(
156 _("Input message filtered, output message filtered and dropped"),
157 text_x, y);
158
159 y += yp;
160 wxBrush b3(wxColour(_T("MAROON")));
161 dc.SetBrush(b3);
162 dc.DrawRectangle(boff, y, bsize, bsize);
163 dc.SetTextForeground(wxColour(_T("MAROON")));
164 dc.DrawText(_("Input Message filtered and dropped"), text_x, y);
165
166 y += yp;
167 wxBrush b4(wxColour(_T("BLUE")));
168 dc.SetBrush(b4);
169 dc.DrawRectangle(boff, y, bsize, bsize);
170 // Indicate message has been sent successfully.
171 dc.SetTextForeground(wxColour(_T("BLUE")));
172 dc.DrawText(_("Output Message"), text_x, y);
173
174 y += yp;
175 wxBrush b5(wxColour(_T("RED")));
176 dc.SetBrush(b5);
177 dc.DrawRectangle(boff, y, bsize, bsize);
178 dc.SetTextForeground(wxColour(_T("RED")));
179 // Indicate message has error (parse error, wrong checksum, cannot be sent,
180 // etc). This could be an input or output message.
181 dc.DrawText(_("Message with errors"), text_x, y);
182 }
183 dc.SelectObject(wxNullBitmap);
184}
185
186void TTYWindow::OnPauseClick(wxCommandEvent&) {
187 if (!m_is_paused) {
188 m_is_paused = true;
189 m_tty_scroll->Pause(true);
190 m_btn_pause->SetLabel(_("Resume"));
191 } else {
192 m_is_paused = false;
193 m_tty_scroll->Pause(false);
194
195 m_btn_pause->SetLabel(_("Pause"));
196 }
197}
198
199void TTYWindow::OnCopyClick(wxCommandEvent&) { m_tty_scroll->Copy(false); }
200
201void TTYWindow::OnCopyN0183Click(wxCommandEvent&) { m_tty_scroll->Copy(true); }
202
203void TTYWindow::OnCloseWindow(wxCloseEvent&) {
204 if (m_window_destroy_listener) {
205 m_window_destroy_listener->DestroyWindow();
206 } else {
207 Destroy();
208 }
209}
210
211void TTYWindow::Add(const wxString& line) {
212 if (m_tty_scroll) m_tty_scroll->Add(line);
213}
Scrolled TTY-like window for logging, etc.
Definition tty_scroll.h:35
void Pause(bool pause)
Set the window to ignore Add() or not depending on pause.
Definition tty_scroll.h:57
void Copy(bool n183)
Copy visible content to clipboard.
virtual void Add(const wxString &line)
Add a line to bottom of window, typically discarding top-most line.
PlugIn Object Definition/API.
Scrolled TTY-like window for logging, etc....