]> git.saurik.com Git - wxWidgets.git/blob - samples/html/printing/printing.cpp
ignore .cached files created by the sample
[wxWidgets.git] / samples / html / printing / printing.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: printimg.cpp
3 // Purpose: wxHtmlEasyPrinting testing example
4 // Licence: wxWindows Licence
5 /////////////////////////////////////////////////////////////////////////////
6
7
8 // For compilers that support precompilation, includes "wx/wx.h".
9 #include "wx/wxprec.h"
10
11 #ifdef __BORLANDC__
12 #pragma hdrstop
13 #endif
14
15 // for all others, include the necessary headers (this file is usually all you
16 // need because it includes almost all "standard" wxWidgets headers
17 #ifndef WX_PRECOMP
18 #include "wx/wx.h"
19 #endif
20
21 #include "wx/image.h"
22 #include "wx/html/htmlwin.h"
23 #include "wx/html/htmprint.h"
24
25
26 // ----------------------------------------------------------------------------
27 // private classes
28 // ----------------------------------------------------------------------------
29
30 // Define a new application type, each program should derive a class from wxApp
31 class MyApp : public wxApp
32 {
33 public:
34 // override base class virtuals
35 // ----------------------------
36
37 // this one is called on application startup and is a good place for the app
38 // initialization (doing it here and not in the ctor allows to have an error
39 // return: if OnInit() returns false, the application terminates)
40
41 virtual bool OnInit();
42 };
43
44 // Define a new frame type: this is going to be our main frame
45 class MyFrame : public wxFrame
46 {
47 public:
48 // ctor and dtor
49
50 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
51 virtual ~MyFrame();
52
53 // event handlers (these functions should _not_ be virtual)
54 void OnQuit(wxCommandEvent& event);
55 void OnAbout(wxCommandEvent& event);
56
57 void OnPageSetup(wxCommandEvent& event);
58 void OnPrint(wxCommandEvent& event);
59 void OnPreview(wxCommandEvent& event);
60 void OnOpen(wxCommandEvent& event);
61
62 void OnPrintSmall(wxCommandEvent& event);
63 void OnPrintNormal(wxCommandEvent& event);
64 void OnPrintHuge(wxCommandEvent& event);
65
66
67 private:
68 wxHtmlWindow *m_Html;
69 wxHtmlEasyPrinting *m_Prn;
70 wxString m_Name;
71 // any class wishing to process wxWidgets events must use this macro
72 DECLARE_EVENT_TABLE()
73 };
74
75 // ----------------------------------------------------------------------------
76 // constants
77 // ----------------------------------------------------------------------------
78
79 // IDs for the controls and the menu commands
80 enum
81 {
82 // menu items
83 Minimal_Quit = 1,
84 Minimal_Print,
85 Minimal_Preview,
86 Minimal_PageSetup,
87 Minimal_Open,
88 Minimal_PrintSmall,
89 Minimal_PrintNormal,
90 Minimal_PrintHuge
91
92 };
93
94 // ----------------------------------------------------------------------------
95 // event tables and other macros for wxWidgets
96 // ----------------------------------------------------------------------------
97
98 // the event tables connect the wxWidgets events with the functions (event
99 // handlers) which process them. It can be also done at run-time, but for the
100 // simple menu events like this the static method is much simpler.
101 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
102 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
103 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
104 EVT_MENU(Minimal_Print, MyFrame::OnPrint)
105 EVT_MENU(Minimal_Preview, MyFrame::OnPreview)
106 EVT_MENU(Minimal_PageSetup, MyFrame::OnPageSetup)
107 EVT_MENU(Minimal_Open, MyFrame::OnOpen)
108 EVT_MENU(Minimal_PrintSmall, MyFrame::OnPrintSmall)
109 EVT_MENU(Minimal_PrintNormal, MyFrame::OnPrintNormal)
110 EVT_MENU(Minimal_PrintHuge, MyFrame::OnPrintHuge)
111 END_EVENT_TABLE()
112
113 // Create a new application object: this macro will allow wxWidgets to create
114 // the application object during program execution (it's better than using a
115 // static object for many reasons) and also declares the accessor function
116 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
117 // not wxApp)
118 IMPLEMENT_APP(MyApp)
119
120 // ============================================================================
121 // implementation
122 // ============================================================================
123
124 // ----------------------------------------------------------------------------
125 // the application class
126 // ----------------------------------------------------------------------------
127 // `Main program' equivalent: the program execution "starts" here
128
129 bool MyApp::OnInit()
130 {
131 #if wxUSE_LIBPNG
132 wxImage::AddHandler(new wxPNGHandler);
133 #endif
134 #if wxUSE_LIBJPEG
135 wxImage::AddHandler(new wxJPEGHandler);
136 #endif
137 #if wxUSE_GIF
138 wxImage::AddHandler(new wxGIFHandler);
139 #endif
140
141 MyFrame *frame = new MyFrame(_("Printing test"),
142 wxDefaultPosition, wxSize(640, 480));
143
144 // Show it and tell the application that it's our main window
145 // @@@ what does it do exactly, in fact? is it necessary here?
146 frame->Show(true);
147 SetTopWindow(frame);
148
149
150 // success: wxApp::OnRun() will be called which will enter the main message
151 // loop and the application will run. If we returned false here, the
152 // application would exit immediately.
153 return true;
154 }
155
156 // ----------------------------------------------------------------------------
157 // main frame
158 // ----------------------------------------------------------------------------
159
160
161 // frame constructor
162 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
163 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
164 {
165 // create a menu bar
166 wxMenu *menuFile = new wxMenu;
167 menuFile->Append(Minimal_Open, _("Open...\tCtrl-O"));
168 menuFile->AppendSeparator();
169 menuFile->Append(Minimal_PageSetup, _("Page Setup"));
170 menuFile->Append(Minimal_Print, _("Print..."));
171 menuFile->Append(Minimal_Preview, _("Preview..."));
172 menuFile->AppendSeparator();
173 menuFile->Append(wxID_ABOUT, _("&About"));
174 menuFile->AppendSeparator();
175 menuFile->Append(Minimal_Quit, _("&Exit"));
176
177 wxMenu *testFile = new wxMenu;
178 testFile->Append(Minimal_PrintSmall, _("Small Printer Fonts"));
179 testFile->Append(Minimal_PrintNormal, _("Normal Printer Fonts"));
180 testFile->Append(Minimal_PrintHuge, _("Huge Printer Fonts"));
181
182 // now append the freshly created menu to the menu bar...
183 wxMenuBar *menuBar = new wxMenuBar;
184 menuBar->Append(menuFile, _("&File"));
185 menuBar->Append(testFile, _("&Test"));
186
187 // ... and attach this menu bar to the frame
188 SetMenuBar(menuBar);
189
190 #if wxUSE_STATUSBAR
191 CreateStatusBar(1);
192 #endif // wxUSE_STATUSBAR
193
194 m_Html = new wxHtmlWindow(this);
195 m_Html -> SetRelatedFrame(this, _("HTML : %s"));
196 #if wxUSE_STATUSBAR
197 m_Html -> SetRelatedStatusBar(0);
198 #endif // wxUSE_STATUSBAR
199 m_Name = wxT("test.htm");
200 m_Html -> LoadPage(m_Name);
201
202 m_Prn = new wxHtmlEasyPrinting(_("Easy Printing Demo"), this);
203 m_Prn -> SetHeader(m_Name + wxT("(@PAGENUM@/@PAGESCNT@)<hr>"), wxPAGE_ALL);
204
205 // To specify where the AFM files are kept on Unix,
206 // you may wish to do something like this
207 // m_Prn->GetPrintData()->SetFontMetricPath(wxT("/home/julians/afm"));
208 }
209
210 // frame destructor
211 MyFrame::~MyFrame()
212 {
213 delete m_Prn;
214 m_Prn = (wxHtmlEasyPrinting *) NULL;
215 }
216
217
218 // event handlers
219
220 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
221 {
222 // true is to force the frame to close
223 Close(true);
224 }
225
226
227 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
228 {
229 wxMessageBox(_("HTML printing sample\n\n(c) Vaclav Slavik, 1999"));
230 }
231
232
233 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
234 {
235 m_Prn -> PageSetup();
236 }
237
238
239 void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
240 {
241 m_Prn -> PrintFile(m_Name);
242 }
243
244
245 void MyFrame::OnPreview(wxCommandEvent& WXUNUSED(event))
246 {
247 m_Prn -> PreviewFile(m_Name);
248 }
249
250
251 void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
252 {
253 wxFileDialog dialog(this, _("Open HTML page"), wxT(""), wxT(""), wxT("*.htm"), 0);
254
255 if (dialog.ShowModal() == wxID_OK)
256 {
257 m_Name = dialog.GetPath();
258 m_Html -> LoadPage(m_Name);
259 m_Prn -> SetHeader(m_Name + wxT("(@PAGENUM@/@PAGESCNT@)<hr>"), wxPAGE_ALL);
260 }
261 }
262
263
264 void MyFrame::OnPrintSmall(wxCommandEvent& WXUNUSED(event))
265 {
266 int fontsizes[] = { 4, 6, 8, 10, 12, 20, 24 };
267 m_Prn->SetFonts(wxEmptyString, wxEmptyString, fontsizes);
268 }
269
270 void MyFrame::OnPrintNormal(wxCommandEvent& WXUNUSED(event))
271 {
272 m_Prn->SetFonts(wxEmptyString, wxEmptyString, 0);
273 }
274
275 void MyFrame::OnPrintHuge(wxCommandEvent& WXUNUSED(event))
276 {
277 int fontsizes[] = { 20, 26, 28, 30, 32, 40, 44 };
278 m_Prn->SetFonts(wxEmptyString, wxEmptyString, fontsizes);
279 }