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 **************************************************************************/
16
17#include <iomanip>
18#include <sstream>
19#include <string>
20
21#ifdef _WIN32
22#define NOMINMAX // Disable min/max compiler nacros.
23#endif
24
25#include <wx/clipbrd.h>
26#include <wx/dcclient.h>
27#include <wx/gdicmn.h>
28#include <wx/string.h>
29#include <wx/textctrl.h>
30
31#include "tty_scroll.h"
32#include "model/config_vars.h"
33#include "observable.h"
34
35// Recursive include of winsock2.h -> redefined DrawText.
36// See: https://forums.wxwidgets.org/viewtopic.php?f=19&t=51849
37#ifdef _WIN32
38#include <wx/msw/winundef.h>
39#endif
40
47static bool IsFilterMatch(const struct Logline& ll, const std::string& s) {
48 if (ll.navmsg->source->iface.find(s) != std::string::npos) return true;
49 return ll.message.find(s) != std::string::npos;
50}
51
52static void UpdateColor(wxColour& colour, unsigned rgb) {
53 if (rgb == kUndefinedColor) {
54 colour = wxNullColour;
55 } else {
56 colour.SetRGB(rgb);
57 }
58}
59
60static std::string Timestamp(const NavmsgTimePoint& when) {
61 using namespace std;
62 using namespace std::chrono;
63
64 auto now = when.time_since_epoch();
65 auto _hours = duration_cast<hours>(now);
66 now -= _hours;
67 auto _minutes = duration_cast<minutes>(now);
68 now -= _minutes;
69 auto _seconds = duration_cast<seconds>(now);
70 now -= _seconds;
71 auto ms = duration_cast<milliseconds>(now);
72#ifdef CSTDIO_TTYSCROLL_TIMESTAMP
73 // Perhaps faster, but not type safe. Needs <cstdio>
74 char buf[128];
75 snprintf(buf, sizeof(buf), "%02ld:%02ld:%02ld.%03ld", _hours.count() % 24,
76 _minutes.count(), _seconds.count(), ms.count());
77 return buf;
78#else
79 stringstream ss;
80 ss << setw(2) << setfill('0') << _hours.count() % 24 << ":" << setw(2)
81 << _minutes.count() << ":" << setw(2) << _seconds.count() << "." << setw(3)
82 << ms.count();
83 return ss.str();
84#endif
85}
86
87wxColor StdColorsByState::operator()(NavmsgStatus ns) {
88 wxColour color;
89 static const wxColor kDarkGreen(30, 72, 56);
90 if (ns.status != NavmsgStatus::State::kOk)
91 color = wxColour("RED");
92 else if (ns.accepted == NavmsgStatus::Accepted::kFilteredNoOutput)
93 color = wxColour("CORAL");
94 else if (ns.accepted == NavmsgStatus::Accepted::kFilteredDropped)
95 color = wxColour("MAROON");
96 else if (ns.direction == NavmsgStatus::Direction::kOutput)
97 color = wxColour("BLUE");
98 else if (ns.direction == NavmsgStatus::Direction::kHandled)
99 color = kDarkGreen;
100 else // input event
101 color = wxColour("ORANGE");
102 return color;
103}
104
105wxColor UserColorsByState::operator()(NavmsgStatus ns) {
106 wxColour color;
107 if (ns.status != NavmsgStatus::State::kOk)
108 UpdateColor(color, g_dm_not_ok);
109 else if (ns.accepted == NavmsgStatus::Accepted::kFilteredNoOutput)
110 UpdateColor(color, g_dm_filtered);
111 else if (ns.accepted == NavmsgStatus::Accepted::kFilteredDropped)
112 UpdateColor(color, g_dm_dropped);
113 else if (ns.direction == NavmsgStatus::Direction::kOutput)
114 UpdateColor(color, g_dm_output);
115 else if (ns.direction == NavmsgStatus::Direction::kInput)
116 UpdateColor(color, g_dm_input);
117 else
118 UpdateColor(color, g_dm_ok);
119 return color.IsOk() ? color : defaults(ns);
120}
121
123void TtyScroll::DrawLine(wxDC& dc, const Logline& ll, int data_pos, int y) {
124 wxString ws;
125 if (!ll.message.empty()) ws << Timestamp(ll.navmsg->created_at) << " ";
126 if (ll.state.direction == NavmsgStatus::Direction::kOutput)
127 ws << " " << kUtfRightArrow << " ";
128 else if (ll.state.direction == NavmsgStatus::Direction::kInput)
129 ws << " " << kUtfLeftwardsArrowToBar << " ";
130 else if (ll.state.direction == NavmsgStatus::Direction::kInternal)
131 ws << " " << kUtfLeftRightArrow << " ";
132 else
133 ws << " " << kUtfLeftArrow << " ";
134
135 if (ll.state.status != NavmsgStatus::State::kOk)
136 ws << kUtfMultiplicationX;
137 else if (ll.state.accepted == NavmsgStatus::Accepted::kFilteredNoOutput)
138 ws << kUtfFallingDiagonal;
139 else if (ll.state.accepted == NavmsgStatus::Accepted::kFilteredDropped)
140 ws << kUtfCircledDivisionSlash;
141 else
142 ws << kUtfCheckMark;
143
144 std::stringstream error_msg;
145 if (ll.state.status != NavmsgStatus::State::kOk) {
146 error_msg << " - "
147 << (ll.error_msg.empty() ? "Unknown errror" : ll.error_msg);
148 }
149 std::string iface(ll.navmsg ? ll.navmsg->source->iface : "");
150 if (iface.size() > 30)
151 iface = std::string("...") + iface.substr(iface.size() - 27);
152 ws << iface << " ";
153
154 dc.DrawText(ws, 0, y);
155 ws = "";
156 ws << ll.message << error_msg.str();
157 dc.DrawText(ws, data_pos, y);
158 m_text_width =
159 std::max(m_text_width, GetTextExtent(ws).GetWidth() + data_pos);
160}
161
162TtyScroll::TtyScroll(wxWindow* parent, int n_lines)
163 : wxScrolledWindow(parent), m_n_lines(n_lines), m_is_paused(false) {
164 SetName("TtyScroll");
165 wxClientDC dc(this);
166 dc.GetTextExtent("Line Height", NULL, &m_line_height);
167 SetScrollRate(m_line_height, m_line_height);
168 for (unsigned i = 0; i < m_n_lines; i++) m_lines.push_back(Logline());
169 SetColors(std::make_unique<UserColorsByState>());
170 Bind(wxEVT_SIZE, [&](wxSizeEvent& ev) { OnSize(ev); });
171}
172
173void TtyScroll::OnSize(wxSizeEvent& ev) {
174 m_n_lines = ev.GetSize().y / GetCharHeight();
175 while (m_lines.size() < m_n_lines) m_lines.push_back(Logline());
176 ev.Skip();
177}
178
179void TtyScroll::Add(const Logline& ll) {
180 if (m_is_paused || !m_filter.Pass(ll.state, ll.navmsg)) return;
181 if (!m_quick_filter.empty() && !IsFilterMatch(ll, m_quick_filter)) return;
182 while (m_lines.size() > m_n_lines - 1) m_lines.pop_front();
183 m_lines.push_back(ll);
184 Refresh(true);
185}
186
187void TtyScroll::SetColors(std::unique_ptr<ColorByState> color_by_state) {
188 m_color_by_state = std::move(color_by_state);
189}
190
191void TtyScroll::OnDraw(wxDC& dc) {
192 // Update region is always in device coords, translate to logical ones
193 wxRect rect_update = GetUpdateRegion().GetBox();
194 CalcUnscrolledPosition(rect_update.x, rect_update.y, &rect_update.x,
195 &rect_update.y);
196 size_t line_from = rect_update.y / m_line_height;
197 size_t line_to = rect_update.GetBottom() / m_line_height;
198 if (line_to > m_n_lines - 1) line_to = m_n_lines - 1;
199
200 wxCoord y = line_from * m_line_height;
201 m_text_width = 0;
202 for (size_t line = line_from; line <= line_to; line++) {
203 wxString ws;
204 dc.SetTextForeground((*m_color_by_state)(m_lines[line].state));
205 DrawLine(dc, m_lines[line], 40 * GetCharWidth(), y);
206 y += m_line_height;
207 }
208 SetVirtualSize(m_text_width, (m_n_lines + 1) * m_line_height);
209}
210
212 std::stringstream ss;
213 for (auto& line : m_lines) ss << line.message << "\n";
214 if (wxTheClipboard->Open()) {
215 wxTheClipboard->SetData(new wxTextDataObject(ss.str()));
216 wxTheClipboard->Close();
217 }
218}
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.
Global variables stored in configuration file.
General observable implementation with several specializations.
Item in the log window.
Definition nmea_log.h:32
Scrolled tty like window for logging.