OpenCPN Partial API docs
TTYScroll.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/textctrl.h>
26 #include <wx/dcclient.h>
27 #include <wx/clipbrd.h>
28 
29 #include "TTYScroll.h"
30 
31 TTYScroll::TTYScroll(wxWindow *parent, int n_lines, wxTextCtrl &tFilter)
32  : wxScrolledWindow(parent), m_nLines(n_lines), m_tFilter(tFilter) {
33  bpause = false;
34  wxClientDC dc(this);
35  dc.GetTextExtent(_T("Line Height"), NULL, &m_hLine);
36 
37  SetScrollRate(0, m_hLine);
38  SetVirtualSize(-1, (m_nLines + 1) * m_hLine);
39  m_plineArray = new wxArrayString;
40  for (unsigned int i = 0; i < m_nLines; i++) m_plineArray->Add(_T(""));
41 }
42 
43 TTYScroll::~TTYScroll() { delete m_plineArray; }
44 
45 void TTYScroll::Add(const wxString &line) {
46  wxString filter = m_tFilter.GetValue();
47  if (!bpause && (filter.IsEmpty() || line.Contains(filter))) {
48  if (m_plineArray->GetCount() > m_nLines - 1) { // shuffle the arraystring
49  wxArrayString *p_newArray = new wxArrayString;
50 
51  for (unsigned int i = 1; i < m_plineArray->GetCount(); i++)
52  p_newArray->Add(m_plineArray->Item(i));
53 
54  delete m_plineArray;
55  m_plineArray = p_newArray;
56  }
57 
58  m_plineArray->Add(line);
59  Refresh(true);
60  }
61 }
62 
63 void TTYScroll::OnDraw(wxDC &dc) {
64  // update region is always in device coords, translate to logical ones
65  wxRect rectUpdate = GetUpdateRegion().GetBox();
66  CalcUnscrolledPosition(rectUpdate.x, rectUpdate.y, &rectUpdate.x,
67  &rectUpdate.y);
68 
69  size_t lineFrom = rectUpdate.y / m_hLine,
70  lineTo = rectUpdate.GetBottom() / m_hLine;
71 
72  if (lineTo > m_nLines - 1) lineTo = m_nLines - 1;
73 
74  wxCoord y = lineFrom * m_hLine;
75  wxString lss;
76  for (size_t line = lineFrom; line <= lineTo; line++) {
77  wxCoord yPhys;
78  CalcScrolledPosition(0, y, NULL, &yPhys);
79 
80  wxString ls = m_plineArray->Item(line);
81  if (ls.Mid(0, 7) == _T("<GREEN>")) {
82  dc.SetTextForeground(wxColour(_T("DARK GREEN")));
83  lss = ls.Mid(7);
84  } else if (ls.Mid(0, 7) == _T("<GOLD>")) {
85  dc.SetTextForeground(wxColour(_T("GOLD")));
86  lss = ls.Mid(7);
87  } else if (ls.Mid(0, 6) == _T("<BLUE>")) {
88  dc.SetTextForeground(wxColour(_T("BLUE")));
89  lss = ls.Mid(6);
90  } else if (ls.Mid(0, 5) == _T("<RED>")) {
91  dc.SetTextForeground(wxColour(_T("RED")));
92  lss = ls.Mid(5);
93  } else if (ls.Mid(0, 7) == _T("<BROWN>")) {
94  dc.SetTextForeground(wxColour(_T("BROWN")));
95  lss = ls.Mid(7);
96  } else if (ls.Mid(0, 8) == _T("<SIENNA>")) {
97  dc.SetTextForeground(wxColour(_T("SIENNA")));
98  lss = ls.Mid(8);
99  } else if (ls.Mid(0, 8) == _T("<MAROON>")) {
100  dc.SetTextForeground(wxColour(_T("MAROON")));
101  lss = ls.Mid(8);
102  } else if (ls.Mid(0, 7) == _T("<CORAL>")) {
103  dc.SetTextForeground(wxColour(_T("CORAL")));
104  lss = ls.Mid(7);
105  }
106  dc.DrawText(lss, 0, y);
107  y += m_hLine;
108  }
109 }
110 
111 void TTYScroll::Copy() {
112  wxString theText;
113  for (unsigned int i = 0; i < m_plineArray->GetCount(); i++) {
114  theText.append(m_plineArray->Item(i));
115  theText.append("\n");
116  }
117  // Write scrolled text to the clipboard
118  if (wxTheClipboard->Open()) {
119  wxTheClipboard->SetData(new wxTextDataObject(theText));
120  wxTheClipboard->Close();
121  }
122 }