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