]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/html/printing/printing.cpp
new VC++ project files
[wxWidgets.git] / samples / html / printing / printing.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: printimg.cpp
3// Purpose: wxHtmlEasyPrinting testing example
4/////////////////////////////////////////////////////////////////////////////
5
6
7// For compilers that support precompilation, includes "wx/wx.h".
8#include <wx/wxprec.h>
9
10#ifdef __BORLANDC__
11#pragma hdrstop
12#endif
13
14// for all others, include the necessary headers (this file is usually all you
15// need because it includes almost all "standard" wxWindows headers
16#ifndef WX_PRECOMP
17#include <wx/wx.h>
18#endif
19
20#include <wx/image.h>
21#include <wx/html/htmlwin.h>
22
23#include <wx/html/htmprint.h>
24
25
26// ----------------------------------------------------------------------------
27// private classes
28// ----------------------------------------------------------------------------
29
30// Define a new application type, each program should derive a class from wxApp
31class MyApp : public wxApp
32{
33 public:
34 // override base class virtuals
35 // ----------------------------
36
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)
40
41 virtual bool OnInit();
42};
43
44// Define a new frame type: this is going to be our main frame
45class 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
76enum
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.
96BEGIN_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)
104END_EVENT_TABLE()
105
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)
111IMPLEMENT_APP(MyApp)
112
113// ============================================================================
114// implementation
115// ============================================================================
116
117// ----------------------------------------------------------------------------
118// the application class
119// ----------------------------------------------------------------------------
120// `Main program' equivalent: the program execution "starts" here
121bool MyApp::OnInit()
122{
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
132
133 MyFrame *frame = new MyFrame("Printing test",
134 wxPoint(150, 50), wxSize(640, 480));
135
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);
140
141
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}
147
148// ----------------------------------------------------------------------------
149// main frame
150// ----------------------------------------------------------------------------
151
152
153// frame constructor
154MyFrame::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);
186
187 m_Prn = new wxHtmlEasyPrinting("Easy Printing Demo", this);
188 m_Prn -> SetHeader(m_Name + "(@PAGENUM@/@PAGESCNT@)<hr>", wxPAGE_ALL);
189}
190
191
192// event handlers
193
194void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
195{
196 delete m_Prn;
197 // TRUE is to force the frame to close
198 Close(TRUE);
199}
200
201
202void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
203{
204 wxMessageBox("HTML printing sample\n\n(c) Vaclav Slavik, 1999");
205}
206
207
208void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
209{
210 m_Prn -> PrinterSetup();
211}
212
213
214void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
215{
216 m_Prn -> PageSetup();
217}
218
219
220void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
221{
222 m_Prn -> PrintFile(m_Name);
223}
224
225
226void MyFrame::OnPreview(wxCommandEvent& WXUNUSED(event))
227{
228 m_Prn -> PreviewFile(m_Name);
229}
230
231
232void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
233{
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 }
242}
243
244