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