-
Notifications
You must be signed in to change notification settings - Fork 3
/
DisplayText.cpp
183 lines (145 loc) · 4.29 KB
/
DisplayText.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
//-----------------------------------------------------------------------------
//
// Copyright (C) 2000-2005 Christoph Oelckers
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// DisplayText.cpp : Display a text in a dialog box.
//
#include "stdafx.h"
#include "ZEd.h"
enum
{
DT_SAVE=100,
DT_PRINT,
};
class CDisplayText : public wxDialog
{
wxTextCtrl * m_Text;
wxFont * m_Font;
public:
CDisplayText(wxWindow * parent, const char * caption, wxString text);
~CDisplayText();
void OnSave(wxCommandEvent & event);
#ifdef PRINTTEXT
void OnPrint(wxCommandEvent & event);
wxString TextToHTML();
#endif
DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE(CDisplayText, wxDialog)
EVT_BUTTON(DT_SAVE, CDisplayText::OnSave)
#ifdef PRINTTEXT
EVT_BUTTON(DT_PRINT, CDisplayText::OnPrint)
#endif
END_EVENT_TABLE()
CDisplayText::CDisplayText(wxWindow * parent, const char * caption, wxString text)
: wxDialog(parent, -1, wxString(caption), wxDefaultPosition, wxDefaultSize, wxCAPTION|wxCLOSE_BOX|wxRESIZE_BORDER|wxMAXIMIZE_BOX|wxSYSTEM_MENU)
{
wxBoxSizer * sizer1 = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer * sizer2 = new wxBoxSizer(wxVERTICAL);
wxButton * button;
button = new wxButton(this, wxID_OK, "OK", wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT);
sizer2->Add(button, 0, wxEXPAND|wxALL, 4);
button = new wxButton(this, DT_SAVE, "Save", wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT);
sizer2->Add(button, 0, wxEXPAND|wxALL, 4);
#ifdef PRINTTEXT
button = new wxButton(this, DT_PRINT, "Print", wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT);
sizer2->Add(button, 0, wxEXPAND|wxALL, 4);
#endif
m_Text = new wxTextCtrl(this, -1, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE|wxTE_READONLY|wxTE_RICH2|wxTE_NOHIDESEL|wxHSCROLL);
sizer1->Add(m_Text, 1, wxEXPAND|wxALL, 10);
sizer1->Add(sizer2, 0, wxEXPAND|wxALL, 10);
SetSizerAndFit(sizer1);
Layout();
SetSize(800,550);
Center();
m_Text->SetFocus();
m_Font = wxFont::New(wxSize(6,10), wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false,
"Courier New", wxFONTENCODING_SYSTEM);
wxTextAttr attr;
attr.SetFont(*m_Font);
m_Text->SetDefaultStyle(attr);
m_Text->AppendText(text);
m_Text->SetInsertionPoint(0);
}
CDisplayText::~CDisplayText()
{
delete m_Font;
}
void CDisplayText::OnSave(wxCommandEvent & event)
{
DirectorySaver ds("SaveText");
wxFileDialog fdlg (this, "Save text as", ds.Dir(), "", "Text Files|*.txt|all files|*.*||",
wxSAVE|wxOVERWRITE_PROMPT|wxCHANGE_DIR);
if (fdlg.ShowModal()==wxID_OK)
{
wxBusyCursor wait;
m_Text->SaveFile(fdlg.GetPath());
}
}
#ifdef PRINTTEXT
wxString CDisplayText::TextToHTML()
{
wxString htmlString;
htmlString << "<html><head><title></title></head><body><tt><FONT SIZE=-2>";
wxChar ch;
wxString text = m_Text->GetValue();
int max = (int)text.length();
for(int pos = 0; pos < max; pos++)
{
ch = text[pos];
switch(ch)
{
case '\n':
{
htmlString << "<br />";
}
break;
case '<':
{
htmlString << "<";
}
break;
case '&':
{
htmlString << "&";
}
break;
case ' ':
{
htmlString << " ";
}
break;
default:
{
htmlString << ch;
}
break;
}
}
htmlString << "</font></tt></body></html>";
return htmlString;
}
void CDisplayText::OnPrint(wxCommandEvent & event)
{
wxString text = TextToHTML();
wxHtmlEasyPrinting print("ZEd", this);
print.SetHeader(GetTitle()+ " - (@PAGENUM@/@PAGESCNT@)<hr>");
print.PrintText(text);
}
#endif
void DisplayText(wxWindow * parent, const char * caption, wxString text)
{
CDisplayText dt(parent, caption, text);
dt.ShowModal();
}