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 <wx/clipbrd.h>
21#include <wx/dcclient.h>
22#include <wx/string.h>
23#include <wx/textctrl.h>
24
25#include "tty_scroll.h"
26
32TtyScroll::TtyScroll(wxWindow* parent, int n_lines, wxTextCtrl& filter)
33 : wxScrolledWindow(parent), m_n_lines(n_lines), m_filter(filter) {
34 m_is_paused = false;
35 wxClientDC dc(this);
36 dc.GetTextExtent("Line Height", NULL, &m_line_height);
37
38 SetScrollRate(0, m_line_height);
39 SetVirtualSize(-1, (m_n_lines + 1) * m_line_height);
40 for (unsigned i = 0; i < m_n_lines; i++) m_lines.push_back("");
41}
42
43void TtyScroll::Add(const wxString& line) {
44 wxString filter = m_filter.GetValue();
45 if (!m_is_paused && (filter.IsEmpty() || line.Contains(filter))) {
46 while (m_lines.size() > m_n_lines - 1) m_lines.pop_front();
47 m_lines.push_back(line);
48 Refresh(true);
49 }
50}
51
52void TtyScroll::OnDraw(wxDC& dc) {
53 // update region is always in device coords, translate to logical ones
54 wxRect rect_update = GetUpdateRegion().GetBox();
55 CalcUnscrolledPosition(rect_update.x, rect_update.y, &rect_update.x,
56 &rect_update.y);
57
58 size_t line_from = rect_update.y / m_line_height,
59 line_to = rect_update.GetBottom() / m_line_height;
60
61 if (line_to > m_n_lines - 1) line_to = m_n_lines - 1;
62
63 wxCoord y = line_from * m_line_height;
64 wxString lss;
65 for (size_t line = line_from; line <= line_to; line++) {
66 wxCoord y_phys;
67 CalcScrolledPosition(0, y, NULL, &y_phys);
68
69 wxString ls = m_lines[line];
70 if (ls.Mid(0, 7) == "<GREEN>") {
71 dc.SetTextForeground(wxColour("DARK GREEN"));
72 lss = ls.Mid(7);
73 } else if (ls.Mid(0, 6) == ("<BLUE>")) {
74 dc.SetTextForeground(wxColour("BLUE"));
75 lss = ls.Mid(6);
76 } else if (ls.Mid(0, 5) == "<RED>") {
77 dc.SetTextForeground(wxColour("RED"));
78 lss = ls.Mid(5);
79 } else if (ls.Mid(0, 8) == "<MAROON>") {
80 dc.SetTextForeground(wxColour("MAROON"));
81 lss = ls.Mid(8);
82 } else if (ls.Mid(0, 7) == "<CORAL>") {
83 dc.SetTextForeground(wxColour("CORAL"));
84 lss = ls.Mid(7);
85 } else {
86 lss = ls;
87 }
88 dc.DrawText(lss, 0, y);
89 y += m_line_height;
90 }
91}
92
93void TtyScroll::Copy(bool n0183) {
94 wxString the_text;
95 for (auto& s : m_lines) {
96 if (n0183) {
97 int pos = 0;
98 if ((pos = s.Find("$")) != wxNOT_FOUND) {
99 the_text.append(s.Mid(pos) + "\n");
100 } else if ((pos = s.Find("!")) != wxNOT_FOUND) {
101 the_text.append(s.Mid(pos) + "\n");
102 }
103 } else {
104 the_text.append(s);
105 the_text.append("\n");
106 }
107 }
108 // Write scrolled text to the clipboard
109 if (wxTheClipboard->Open()) {
110 wxTheClipboard->SetData(new wxTextDataObject(the_text));
111 wxTheClipboard->Close();
112 }
113}
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.
TtyScroll(wxWindow *parent, int n_lines, wxTextCtrl &filter)
Create a TtyScroll instance.
Scrolled TTY-like window for logging, etc....