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