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