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