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