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