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