1 // -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 //
3 // Copyright (C) 2018 Gunter Königsmann <wxMaxima@physikbuch.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // SPDX-License-Identifier: GPL-2.0+
21
22 #include "LogPane.h"
23 #include "ErrorRedirector.h"
LogPane(wxWindow * parent,wxWindowID id,bool becomeLogTarget)24 LogPane::LogPane(wxWindow *parent, wxWindowID id, bool becomeLogTarget) : wxPanel(parent, id)
25 {
26 m_isLogTarget = false;
27 m_logPanelTarget = NULL;
28 wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
29
30 m_textCtrl = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition,
31 wxDefaultSize,
32 wxTE_MULTILINE | wxTE_READONLY | wxHSCROLL);
33
34 m_textCtrl->SetMinSize(wxSize(wxSystemSettings::GetMetric( wxSYS_SCREEN_X )/10,
35 wxSystemSettings::GetMetric( wxSYS_SCREEN_Y )/10));
36 vbox->Add(m_textCtrl, wxSizerFlags().Expand().Proportion(1));
37
38 if(becomeLogTarget)
39 BecomeLogTarget();
40
41 // m_logPanelTarget->SetRepetitionCounting();
42 // m_logPanelTarget->DisableTimestamp();
43 SetSizerAndFit(vbox);
44 }
45
DropLogTarget()46 void LogPane::DropLogTarget()
47 {
48 // m_logPanelTarget is automatically destroyed in this step.
49 if(m_isLogTarget)
50 {
51 m_errorRedirector = NULL;
52 wxLog::SetActiveTarget(NULL);
53 }
54 m_logPanelTarget = NULL;
55 m_isLogTarget = false;
56 }
57
BecomeLogTarget()58 void LogPane::BecomeLogTarget()
59 {
60 m_isLogTarget = true;
61 wxLog::SetActiveTarget(m_logPanelTarget = new wxLogTextCtrl(m_textCtrl));
62 m_errorRedirector = std::unique_ptr<ErrorRedirector>(new ErrorRedirector(new wxLogGui()));
63 #ifdef wxUSE_STD_IOSTREAM
64 if(!ErrorRedirector::LoggingToStdErr())
65 m_textRedirector = std::unique_ptr<wxStreamToTextRedirector>(new wxStreamToTextRedirector(m_textCtrl));
66 #endif
67 }
68
~LogPane()69 LogPane::~LogPane()
70 {
71 DropLogTarget();
72 #ifdef wxUSE_STD_IOSTREAM
73 m_textRedirector.reset();
74 #endif
75 }
76
77