]>
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". | |
3ce369e6 | 8 | #include <wx/wxprec.h> |
5526e819 VS |
9 | |
10 | #ifdef __BORLANDC__ | |
11 | #pragma hdrstop | |
12 | #endif | |
13 | ||
3ce369e6 VS |
14 | // for all others, include the necessary headers (this file is usually all you |
15 | // need because it includes almost all "standard" wxWindows headers | |
5526e819 | 16 | #ifndef WX_PRECOMP |
3ce369e6 | 17 | #include <wx/wx.h> |
5526e819 VS |
18 | #endif |
19 | ||
3ce369e6 VS |
20 | #include <wx/image.h> |
21 | #include <wx/html/htmlwin.h> | |
5526e819 | 22 | |
3ce369e6 | 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: | |
48 | // ctor(s) | |
49 | ||
50 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
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 | ||
62 | ||
63 | private: | |
64 | wxHtmlWindow *m_Html; | |
65 | wxHtmlEasyPrinting *m_Prn; | |
66 | wxString m_Name; | |
67 | // any class wishing to process wxWindows events must use this macro | |
68 | DECLARE_EVENT_TABLE() | |
69 | }; | |
70 | ||
71 | // ---------------------------------------------------------------------------- | |
72 | // constants | |
73 | // ---------------------------------------------------------------------------- | |
74 | ||
75 | // IDs for the controls and the menu commands | |
76 | enum | |
77 | { | |
78 | // menu items | |
79 | Minimal_Quit = 1, | |
80 | Minimal_About, | |
81 | Minimal_Print, | |
82 | Minimal_Preview, | |
83 | Minimal_PageSetup, | |
84 | Minimal_PrintSetup, | |
85 | Minimal_Open | |
86 | ||
87 | }; | |
88 | ||
89 | // ---------------------------------------------------------------------------- | |
90 | // event tables and other macros for wxWindows | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
93 | // the event tables connect the wxWindows events with the functions (event | |
94 | // handlers) which process them. It can be also done at run-time, but for the | |
95 | // simple menu events like this the static method is much simpler. | |
96 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
97 | EVT_MENU(Minimal_Quit, MyFrame::OnQuit) | |
98 | EVT_MENU(Minimal_About, MyFrame::OnAbout) | |
99 | EVT_MENU(Minimal_Print, MyFrame::OnPrint) | |
100 | EVT_MENU(Minimal_Preview, MyFrame::OnPreview) | |
101 | EVT_MENU(Minimal_PageSetup, MyFrame::OnPageSetup) | |
102 | EVT_MENU(Minimal_PrintSetup, MyFrame::OnPrintSetup) | |
103 | EVT_MENU(Minimal_Open, MyFrame::OnOpen) | |
104 | END_EVENT_TABLE() | |
5526e819 | 105 | |
3ce369e6 VS |
106 | // Create a new application object: this macro will allow wxWindows to create |
107 | // the application object during program execution (it's better than using a | |
108 | // static object for many reasons) and also declares the accessor function | |
109 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
110 | // not wxApp) | |
5526e819 VS |
111 | IMPLEMENT_APP(MyApp) |
112 | ||
3ce369e6 VS |
113 | // ============================================================================ |
114 | // implementation | |
115 | // ============================================================================ | |
5526e819 | 116 | |
3ce369e6 VS |
117 | // ---------------------------------------------------------------------------- |
118 | // the application class | |
119 | // ---------------------------------------------------------------------------- | |
120 | // `Main program' equivalent: the program execution "starts" here | |
121 | bool MyApp::OnInit() | |
5526e819 | 122 | { |
3ce369e6 VS |
123 | #if wxUSE_LIBPNG |
124 | wxImage::AddHandler(new wxPNGHandler); | |
125 | #endif | |
126 | #if wxUSE_LIBJPEG | |
127 | wxImage::AddHandler(new wxJPEGHandler); | |
128 | #endif | |
129 | #if wxUSE_GIF | |
130 | wxImage::AddHandler(new wxGIFHandler); | |
131 | #endif | |
5526e819 | 132 | |
3ce369e6 VS |
133 | MyFrame *frame = new MyFrame("Printing test", |
134 | wxPoint(150, 50), wxSize(640, 480)); | |
5526e819 | 135 | |
3ce369e6 VS |
136 | // Show it and tell the application that it's our main window |
137 | // @@@ what does it do exactly, in fact? is it necessary here? | |
138 | frame->Show(TRUE); | |
139 | SetTopWindow(frame); | |
5526e819 | 140 | |
5526e819 | 141 | |
3ce369e6 VS |
142 | // success: wxApp::OnRun() will be called which will enter the main message |
143 | // loop and the application will run. If we returned FALSE here, the | |
144 | // application would exit immediately. | |
145 | return TRUE; | |
146 | } | |
5526e819 | 147 | |
3ce369e6 VS |
148 | // ---------------------------------------------------------------------------- |
149 | // main frame | |
150 | // ---------------------------------------------------------------------------- | |
5526e819 | 151 | |
5526e819 | 152 | |
3ce369e6 VS |
153 | // frame constructor |
154 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
155 | : wxFrame((wxFrame *)NULL, -1, title, pos, size) | |
156 | { | |
157 | // create a menu bar | |
158 | wxMenu *menuFile = new wxMenu; | |
159 | wxMenu *menuNav = new wxMenu; | |
160 | ||
161 | menuFile->Append(Minimal_Open, "Open...\tCtrl-O"); | |
162 | menuFile->AppendSeparator(); | |
163 | menuFile->Append(Minimal_PageSetup, "Page Setup"); | |
164 | menuFile->Append(Minimal_PrintSetup, "Printer Setup"); | |
165 | menuFile->Append(Minimal_Print, "Print..."); | |
166 | menuFile->Append(Minimal_Preview, "Preview..."); | |
167 | menuFile->AppendSeparator(); | |
168 | menuFile->Append(Minimal_About, "&About"); | |
169 | menuFile->AppendSeparator(); | |
170 | menuFile->Append(Minimal_Quit, "&Exit"); | |
171 | ||
172 | // now append the freshly created menu to the menu bar... | |
173 | wxMenuBar *menuBar = new wxMenuBar; | |
174 | menuBar->Append(menuFile, "&File"); | |
175 | ||
176 | // ... and attach this menu bar to the frame | |
177 | SetMenuBar(menuBar); | |
178 | ||
179 | CreateStatusBar(1); | |
180 | ||
181 | m_Html = new wxHtmlWindow(this); | |
182 | m_Html -> SetRelatedFrame(this, "HTML : %s"); | |
183 | m_Html -> SetRelatedStatusBar(0); | |
184 | m_Name = "test.htm"; | |
185 | m_Html -> LoadPage(m_Name); | |
5526e819 | 186 | |
3ce369e6 VS |
187 | m_Prn = new wxHtmlEasyPrinting("Easy Printing Demo", this); |
188 | m_Prn -> SetHeader(m_Name + "(@PAGENUM@/@PAGESCNT@)<hr>", wxPAGE_ALL); | |
5526e819 VS |
189 | } |
190 | ||
5526e819 | 191 | |
3ce369e6 | 192 | // event handlers |
5526e819 | 193 | |
3ce369e6 | 194 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
5526e819 | 195 | { |
3ce369e6 VS |
196 | delete m_Prn; |
197 | // TRUE is to force the frame to close | |
198 | Close(TRUE); | |
5526e819 VS |
199 | } |
200 | ||
5526e819 | 201 | |
3ce369e6 | 202 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
5526e819 | 203 | { |
3ce369e6 | 204 | wxMessageBox("HTML printing sample\n\n(c) Vaclav Slavik, 1999"); |
5526e819 VS |
205 | } |
206 | ||
5526e819 VS |
207 | |
208 | void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event)) | |
209 | { | |
3ce369e6 | 210 | m_Prn -> PrinterSetup(); |
5526e819 VS |
211 | } |
212 | ||
213 | ||
3ce369e6 | 214 | void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event)) |
5526e819 | 215 | { |
3ce369e6 | 216 | m_Prn -> PageSetup(); |
5526e819 VS |
217 | } |
218 | ||
219 | ||
3ce369e6 | 220 | void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event)) |
5526e819 | 221 | { |
3ce369e6 | 222 | m_Prn -> PrintFile(m_Name); |
5526e819 VS |
223 | } |
224 | ||
5526e819 | 225 | |
3ce369e6 | 226 | void MyFrame::OnPreview(wxCommandEvent& WXUNUSED(event)) |
5526e819 | 227 | { |
3ce369e6 | 228 | m_Prn -> PreviewFile(m_Name); |
5526e819 VS |
229 | } |
230 | ||
231 | ||
3ce369e6 | 232 | void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event)) |
5526e819 | 233 | { |
3ce369e6 VS |
234 | wxFileDialog dialog(this, "Open HTML page", "", "", "*.htm", 0); |
235 | ||
236 | if (dialog.ShowModal() == wxID_OK) | |
237 | { | |
238 | m_Name = dialog.GetPath(); | |
239 | m_Html -> LoadPage(m_Name); | |
240 | m_Prn -> SetHeader(m_Name + "(@PAGENUM@/@PAGESCNT@)<hr>", wxPAGE_ALL); | |
241 | } | |
5526e819 VS |
242 | } |
243 | ||
244 |