OpenCPN Partial API docs
Loading...
Searching...
No Matches
tty_scroll.cpp
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2013 by David S. Register *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 **************************************************************************/
19
20#include <iomanip>
21#include <sstream>
22#include <string>
23
24#ifdef _WIN32
25#define NOMINMAX // Disable min/max compiler nacros.
26#endif
27
28#include <wx/clipbrd.h>
29#include <wx/dcclient.h>
30#include <wx/string.h>
31#include <wx/textctrl.h>
32
33#include "tty_scroll.h"
34#include "observable.h"
35
36// Recursive include of winsock2.h -> redefined DrawText.
37// See: https://forums.wxwidgets.org/viewtopic.php?f=19&t=51849
38#ifdef _WIN32
39#include <wx/msw/winundef.h>
40#endif
41
47const wxString kUtfCheckMark = wxString::FromUTF8(u8"\u2713");
48const wxString kUtfCircledDivisionSlash = wxString::FromUTF8(u8"\u2298");
49const wxString kUtfFallingDiagonal = wxString::FromUTF8(u8"\u269F");
50const wxString kUtfIdenticalTo = wxString::FromUTF8(u8"\u2261");
51const wxString kUtfLeftArrow = wxString::FromUTF8(u8"\u2190");
52const wxString kUtfLeftRightArrow = wxString::FromUTF8(u8"\u2194");
53const wxString kUtfLeftwardsArrowToBar = wxString::FromUTF8(u8"\u21E4");
54const wxString kUtfMultiplicationX = wxString::FromUTF8(u8"\u2716");
55const wxString kUtfRightArrow = wxString::FromUTF8(u8"\u2192");
56
58static bool IsFilterMatch(const struct Logline& ll, const std::string& s) {
59 if (ll.navmsg->source->iface.find(s) != std::string::npos) return true;
60 return ll.message.find(s) != std::string::npos;
61}
62
63static std::string Timestamp(const NavmsgTimePoint& when) {
64 using namespace std;
65 using namespace std::chrono;
66
67 auto now = when.time_since_epoch();
68 auto _hours = duration_cast<hours>(now);
69 now -= _hours;
70 auto _minutes = duration_cast<minutes>(now);
71 now -= _minutes;
72 auto _seconds = duration_cast<seconds>(now);
73 now -= _seconds;
74 auto ms = duration_cast<milliseconds>(now);
75#ifdef CSTDIO_TTYSCROLL_TIMESTAMP
76 // Perhaps faster, but not type safe. Needs <cstdio>
77 char buf[128];
78 snprintf(buf, sizeof(buf), "%02ld:%02ld:%02ld.%03ld", _hours.count() % 24,
79 _minutes.count(), _seconds.count(), ms.count());
80 return buf;
81#else
82 stringstream ss;
83 ss << setw(2) << setfill('0') << _hours.count() % 24 << ":" << setw(2)
84 << _minutes.count() << ":" << setw(2) << _seconds.count() << "." << setw(3)
85 << ms.count();
86 return ss.str();
87#endif
88}
89
90wxColor StdColorsByState::operator()(NavmsgStatus ns) {
91 wxColour color;
92 static const wxColor kDarkGreen(30, 72, 56);
93 if (ns.status != NavmsgStatus::State::kOk)
94 color = wxColour("RED");
95 else if (ns.accepted == NavmsgStatus::Accepted::kFilteredNoOutput)
96 color = wxColour("CORAL");
97 else if (ns.accepted == NavmsgStatus::Accepted::kFilteredDropped)
98 color = wxColour("MAROON");
99 else if (ns.direction == NavmsgStatus::Direction::kOutput)
100 color = wxColour("BLUE");
101 else if (ns.direction == NavmsgStatus::Direction::kInput)
102 color = wxColour("ORANGE");
103 else
104 color = kDarkGreen;
105 return color;
106}
107
109void TtyScroll::DrawLine(wxDC& dc, const Logline& ll, int data_pos, int y) {
110 wxString ws;
111 if (!ll.message.empty()) ws << Timestamp(ll.navmsg->created_at) << " ";
112 if (ll.state.direction == NavmsgStatus::Direction::kOutput)
113 ws << " " << kUtfRightArrow << " ";
114 else if (ll.state.direction == NavmsgStatus::Direction::kInput)
115 ws << " " << kUtfLeftwardsArrowToBar << " ";
116 else if (ll.state.direction == NavmsgStatus::Direction::kInternal)
117 ws << " " << kUtfLeftRightArrow << " ";
118 else
119 ws << " " << kUtfLeftArrow << " ";
120
121 if (ll.state.status != NavmsgStatus::State::kOk)
122 ws << kUtfMultiplicationX;
123 else if (ll.state.accepted == NavmsgStatus::Accepted::kFilteredNoOutput)
124 ws << kUtfFallingDiagonal;
125 else if (ll.state.accepted == NavmsgStatus::Accepted::kFilteredDropped)
126 ws << kUtfCircledDivisionSlash;
127 else
128 ws << kUtfCheckMark;
129
130 std::stringstream error_msg;
131 if (ll.state.status != NavmsgStatus::State::kOk) {
132 error_msg << " - "
133 << (ll.error_msg.empty() ? "Unknown errror" : ll.error_msg);
134 }
135 std::string iface(ll.navmsg ? ll.navmsg->source->iface : "");
136 if (iface.size() > 30)
137 iface = std::string("...") + iface.substr(iface.size() - 27);
138 ws << iface << " ";
139
140 dc.DrawText(ws, 0, y);
141 ws = "";
142 ws << ll.message << error_msg.str();
143 dc.DrawText(ws, data_pos, y);
144 m_text_width =
145 std::max(m_text_width, GetTextExtent(ws).GetWidth() + data_pos);
146}
147
148TtyScroll::TtyScroll(wxWindow* parent, int n_lines)
149 : wxScrolledWindow(parent), m_n_lines(n_lines), m_is_paused(false) {
150 SetName("TtyScroll");
151 wxClientDC dc(this);
152 dc.GetTextExtent("Line Height", NULL, &m_line_height);
153 SetScrollRate(m_line_height, m_line_height);
154 for (unsigned i = 0; i < m_n_lines; i++) m_lines.push_back(Logline());
155 SetColors(std::make_unique<StdColorsByState>());
156 Bind(wxEVT_SIZE, [&](wxSizeEvent& ev) { OnSize(ev); });
157}
158
159void TtyScroll::OnSize(wxSizeEvent& ev) {
160 m_n_lines = ev.GetSize().y / GetCharHeight();
161 while (m_lines.size() < m_n_lines) m_lines.push_back(Logline());
162 ev.Skip();
163}
164
165void TtyScroll::Add(const Logline& ll) {
166 if (m_is_paused || !m_filter.Pass(ll.state, ll.navmsg)) return;
167 if (!m_quick_filter.empty() && !IsFilterMatch(ll, m_quick_filter)) return;
168 while (m_lines.size() > m_n_lines - 1) m_lines.pop_front();
169 m_lines.push_back(ll);
170 Refresh(true);
171}
172
173void TtyScroll::SetColors(std::unique_ptr<ColorByState> color_by_state) {
174 m_color_by_state = std::move(color_by_state);
175}
176
177void TtyScroll::OnDraw(wxDC& dc) {
178 // Update region is always in device coords, translate to logical ones
179 wxRect rect_update = GetUpdateRegion().GetBox();
180 CalcUnscrolledPosition(rect_update.x, rect_update.y, &rect_update.x,
181 &rect_update.y);
182 size_t line_from = rect_update.y / m_line_height;
183 size_t line_to = rect_update.GetBottom() / m_line_height;
184 if (line_to > m_n_lines - 1) line_to = m_n_lines - 1;
185
186 wxCoord y = line_from * m_line_height;
187 m_text_width = 0;
188 for (size_t line = line_from; line <= line_to; line++) {
189 wxString ws;
190 dc.SetTextForeground((*m_color_by_state)(m_lines[line].state));
191 DrawLine(dc, m_lines[line], 40 * GetCharWidth(), y);
192 y += m_line_height;
193 }
194 SetVirtualSize(m_text_width, (m_n_lines + 1) * m_line_height);
195}
196
198 std::stringstream ss;
199 for (auto& line : m_lines) ss << line.message << "\n";
200 if (wxTheClipboard->Open()) {
201 wxTheClipboard->SetData(new wxTextDataObject(ss.str()));
202 wxTheClipboard->Close();
203 }
204}
bool Pass(NavmsgStatus status, const std::shared_ptr< const NavMsg > &msg)
Return true if message is not matched by filter.
Representation of message status as determined by the multiplexer.
void SetColors(std::unique_ptr< ColorByState > color_by_state)
Set color scheme.
virtual void Add(const Logline &line)
Add a line to bottom of window, typically discarding top-most line.
void DrawLine(wxDC &dc, const Logline &ll, int data_pos, int y)
Draw a single line in the log window.
void CopyToClipboard() const
Copy message contents to clipboard.
TtyScroll(wxWindow *parent, int n_lines)
Create a TtyScroll instance.
Item in the log window.
Definition nmea_log.h:10
Scrolled TTY-like window for logging, related utilities.