OpenCPN Partial API docs
Loading...
Searching...
No Matches
TTYWindow.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/sizer.h>
26#include <wx/statbox.h>
27#include <wx/bmpbuttn.h>
28#include <wx/dcmemory.h>
29#include <wx/dialog.h>
30#include <wx/settings.h>
31#include <wx/dcscreen.h>
32
33#include "TTYWindow.h"
34#include "TTYScroll.h"
35#include "WindowDestroyListener.h"
36#include "color_handler.h"
37#include "ocpn_plugin.h"
38#include "FontMgr.h"
39
40IMPLEMENT_DYNAMIC_CLASS(TTYWindow, wxFrame)
41
42BEGIN_EVENT_TABLE(TTYWindow, wxFrame)
43EVT_CLOSE(TTYWindow::OnCloseWindow)
44END_EVENT_TABLE()
45
46TTYWindow::TTYWindow() : m_window_destroy_listener(NULL), m_pScroll(NULL) {}
47
48TTYWindow::TTYWindow(wxWindow* parent, int n_lines,
49 WindowDestroyListener* listener)
50 : m_window_destroy_listener(listener), m_pScroll(NULL) {
51 wxFrame::Create(
52 parent, -1, _T("Title"), wxDefaultPosition, wxDefaultSize,
53 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFRAME_FLOAT_ON_PARENT);
54
55 wxBoxSizer* bSizerOuterContainer = new wxBoxSizer(wxVERTICAL);
56 SetSizer(bSizerOuterContainer);
57
58 m_tFilter = new wxTextCtrl(this, wxID_ANY);
59
60 m_pScroll = new TTYScroll(this, n_lines, *m_tFilter);
61 m_pScroll->Scroll(-1, 1000); // start with full scroll down
62
63 bSizerOuterContainer->Add(m_pScroll, 1, wxEXPAND, 5);
64
65 wxStaticBox* psbf = new wxStaticBox(this, wxID_ANY, _("Filter"));
66 wxStaticBoxSizer* sbSizer2 = new wxStaticBoxSizer(psbf, wxVERTICAL);
67 sbSizer2->Add(m_tFilter, 1, wxALL | wxEXPAND, 5);
68 bSizerOuterContainer->Add(sbSizer2, 0, wxEXPAND, 5);
69
70 wxBoxSizer* bSizerBottomContainer = new wxBoxSizer(wxHORIZONTAL);
71 bSizerOuterContainer->Add(bSizerBottomContainer, 0, wxEXPAND, 5);
72
73 wxStaticBox* psb = new wxStaticBox(this, wxID_ANY, _("Legend"));
74 wxStaticBoxSizer* sbSizer1 = new wxStaticBoxSizer(psb, wxVERTICAL);
75
76 CreateLegendBitmap();
77 wxBitmapButton* bb = new wxBitmapButton(this, wxID_ANY, m_bm_legend);
78 sbSizer1->Add(bb, 1, wxALL | wxEXPAND, 5);
79 bSizerBottomContainer->Add(sbSizer1, 0, wxALIGN_LEFT | wxALL, 5);
80
81 wxStaticBox* buttonBox = new wxStaticBox(this, wxID_ANY, wxEmptyString);
82 wxStaticBoxSizer* bbSizer1 = new wxStaticBoxSizer(buttonBox, wxVERTICAL);
83
84 m_buttonPause = new wxButton(this, wxID_ANY, _("Pause"), wxDefaultPosition,
85 wxDefaultSize, 0);
86 m_buttonCopy = new wxButton(this, wxID_ANY, _("Copy"), wxDefaultPosition,
87 wxDefaultSize, 0);
88 m_buttonCopy->SetToolTip(_("Copy NMEA Debug window to clipboard."));
89
90 bbSizer1->Add(m_buttonPause, 0, wxALL, 5);
91 bbSizer1->Add(m_buttonCopy, 0, wxALL, 5);
92 bSizerBottomContainer->Add(bbSizer1, 1, wxALL | wxEXPAND, 5);
93
94 m_buttonCopy->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
95 wxCommandEventHandler(TTYWindow::OnCopyClick), NULL,
96 this);
97 m_buttonPause->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
98 wxCommandEventHandler(TTYWindow::OnPauseClick), NULL,
99 this);
100
101 bpause = false;
102}
103
104TTYWindow::~TTYWindow() {
105 if (m_pScroll) {
106 delete m_pScroll;
107 m_pScroll = NULL;
108 }
109}
110
111void TTYWindow::CreateLegendBitmap() {
112 double dip_factor = OCPN_GetWinDIPScaleFactor();
113 wxScreenDC dcs;
114 wxFont *pmetricFont = FontMgr::Get().GetFont(_("Dialog"));
115
116 int width, height;
117 dcs.GetTextExtent("M", &width, &height, NULL, NULL, pmetricFont);
118 double ref_dim = height * dip_factor;
119
120 m_bm_legend.Create(25 * ref_dim, 6.5 * ref_dim);
121 wxMemoryDC dc;
122 dc.SelectObject(m_bm_legend);
123 if (m_bm_legend.IsOk()) {
124 dc.SetBackground(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)));
125 dc.Clear();
126
127 wxFont f(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
128 dc.SetFont(f);
129
130 int yp = ref_dim * 1.25;
131 int y = ref_dim * .25;
132 int bsize = ref_dim;
133 int text_x = ref_dim * 1.5;
134 int boff = ref_dim *.25;
135
136 wxBrush b1(wxColour(_T("DARK GREEN")));
137 dc.SetBrush(b1);
138 dc.DrawRectangle(boff, y, bsize, bsize);
139 dc.SetTextForeground(wxColour(_T("DARK GREEN")));
140 dc.DrawText(_("Message accepted"), text_x, y);
141
142 y += yp;
143 wxBrush b2(wxColour(_T("CORAL")));
144 dc.SetBrush(b2);
145 dc.DrawRectangle(boff, y, bsize, bsize);
146 dc.SetTextForeground(wxColour(_T("CORAL")));
147 dc.DrawText(
148 _("Input message filtered, output message filtered and dropped"), text_x,
149 y);
150
151 y += yp;
152 wxBrush b3(wxColour(_T("MAROON")));
153 dc.SetBrush(b3);
154 dc.DrawRectangle(boff, y, bsize, bsize);
155 dc.SetTextForeground(wxColour(_T("MAROON")));
156 dc.DrawText(_("Input Message filtered and dropped"), text_x, y);
157
158 y += yp;
159 wxBrush b4(wxColour(_T("BLUE")));
160 dc.SetBrush(b4);
161 dc.DrawRectangle(boff, y, bsize, bsize);
162 dc.SetTextForeground(wxColour(_T("BLUE")));
163 dc.DrawText(_("Output Message"), text_x, y);
164
165 y += yp;
166 wxBrush b5(wxColour(_T("RED")));
167 dc.SetBrush(b5);
168 dc.DrawRectangle(boff, y, bsize, bsize);
169 dc.SetTextForeground(wxColour(_T("RED")));
170 dc.DrawText(_("Information Message or Message with errors"), text_x, y);
171 }
172 dc.SelectObject(wxNullBitmap);
173}
174
175void TTYWindow::OnPauseClick(wxCommandEvent& event) {
176 if (!bpause) {
177 bpause = true;
178 m_pScroll->Pause(true);
179 m_buttonPause->SetLabel(_("Resume"));
180 } else {
181 bpause = false;
182 m_pScroll->Pause(false);
183
184 m_buttonPause->SetLabel(_("Pause"));
185 }
186}
187
188void TTYWindow::OnCopyClick(wxCommandEvent& event) { m_pScroll->Copy(); }
189
190void TTYWindow::OnCloseWindow(wxCloseEvent& event) {
191 if (m_window_destroy_listener) {
192 m_window_destroy_listener->DestroyWindow();
193 } else {
194 Destroy();
195 }
196}
197
198void TTYWindow::Add(const wxString& line) {
199 if (m_pScroll) m_pScroll->Add(line);
200}