1 // -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 //
3 // Copyright (C) 2004-2015 Andrej Vodopivec <andrej.vodopivec@gmail.com>
4 // Copyright (C) 2017-2018 Gunter Königsmann <wxMaxima@physikbuch.de>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 //
21 // SPDX-License-Identifier: GPL-2.0+
22
23 /*! \file
24 This file contains all the wizards the draw sidepane needs.
25 */
26
27 #include <wx/textctrl.h>
28 #include <wx/mstream.h>
29 #include <wx/zstream.h>
30 #include <wx/txtstrm.h>
31 #include <wx/string.h>
32 #include "LicenseDialog.h"
33 #include "../data/License.h"
34
LicenseDialog(wxWindow * parent)35 LicenseDialog::LicenseDialog(wxWindow *parent) :
36 wxDialog(parent, -1, _("License"), wxDefaultPosition, wxDefaultSize,
37 wxRESIZE_BORDER | wxCLOSE_BOX | wxMAXIMIZE_BOX | wxMINIMIZE_BOX)
38 {
39 wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
40 wxMemoryInputStream istream(License_gz, License_gz_len);
41 wxZlibInputStream zstream(istream);
42 wxTextInputStream textIn(zstream);
43 m_movedToStart = false;
44 wxString line;
45 wxString licenseText;
46
47 m_license = new wxTextCtrl(this, -1,
48 wxEmptyString, wxDefaultPosition,
49 wxDefaultSize,
50 wxTE_MULTILINE | wxHSCROLL | wxTE_READONLY);
51
52 wxFont fnt = m_license->GetFont();
53 wxClientDC dc(this );
54 dc.SetFont(fnt);
55 long textWidth = 0;
56 while(!istream.Eof())
57 {
58 line = textIn.ReadLine();
59 licenseText += line + wxT("\n");
60 wxSize linesize = dc.GetTextExtent(line);
61 if(linesize.x > textWidth)
62 {
63 textWidth = linesize.x;
64 m_longestLine = line;
65 }
66 }
67
68 m_license->SetMinSize(wxSize(textWidth + 20*GetContentScaleFactor(),550*GetContentScaleFactor()));
69 m_license->SetValue(licenseText);
70 vbox->Add(m_license, wxSizerFlags(10).Expand().Border(wxALL, 5));
71 wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL);
72
73 wxButton *okButton = new wxButton(this, wxID_OK, _("OK"));
74 buttonSizer->Add(okButton, wxSizerFlags());
75 okButton->SetDefault();
76 vbox->Add(buttonSizer, wxSizerFlags(0).Right());
77
78 SetName("License");
79 wxPersistenceManager::Get().RegisterAndRestore(this);
80 Connect(wxEVT_SIZE, wxSizeEventHandler(LicenseDialog::OnSize));
81 SetSizerAndFit(vbox);
82 }
83
OnSize(wxSizeEvent & event)84 void LicenseDialog::OnSize(wxSizeEvent &event)
85 {
86 wxFont fnt = m_license->GetFont();
87 wxClientDC dc(this );
88 double pointSize = 8;
89 int width;
90 do
91 {
92 #if wxCHECK_VERSION(3, 1, 2)
93 pointSize += .1;
94 fnt.SetFractionalPointSize(pointSize);
95 #else
96 pointSize += 1;
97 fnt.SetPointSize(pointSize);
98 #endif
99 dc.SetFont(fnt);
100 width = dc.GetTextExtent(m_longestLine).x;
101 } while((pointSize < 128) && (width < event.GetSize().x));
102 #if wxCHECK_VERSION(3, 1, 2)
103 pointSize -= .1;
104 fnt.SetFractionalPointSize(pointSize);
105 #else
106 pointSize -= 1;
107 fnt.SetPointSize(pointSize);
108 #endif
109 m_license->SetFont(fnt);
110 event.Skip();
111 if(!m_movedToStart)
112 {
113 m_license->SetInsertionPoint(0);
114 m_license->ShowPosition(0);
115 }
116 }
117