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