]> git.saurik.com Git - wxWidgets.git/blob - samples/html/printing/printing.cpp
fixed bug with the caret positioning after SetValue() introduced by the last commit
[wxWidgets.git] / samples / html / printing / printing.cpp
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
31 class 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
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()
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)
111 IMPLEMENT_APP(MyApp)
112
113 // ============================================================================
114 // implementation
115 // ============================================================================
116
117 // ----------------------------------------------------------------------------
118 // the application class
119 // ----------------------------------------------------------------------------
120 // `Main program' equivalent: the program execution "starts" here
121 bool 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
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 menuFile->Append(Minimal_Open, "Open...\tCtrl-O");
160 menuFile->AppendSeparator();
161 menuFile->Append(Minimal_PageSetup, "Page Setup");
162 menuFile->Append(Minimal_PrintSetup, "Printer Setup");
163 menuFile->Append(Minimal_Print, "Print...");
164 menuFile->Append(Minimal_Preview, "Preview...");
165 menuFile->AppendSeparator();
166 menuFile->Append(Minimal_About, "&About");
167 menuFile->AppendSeparator();
168 menuFile->Append(Minimal_Quit, "&Exit");
169
170 // now append the freshly created menu to the menu bar...
171 wxMenuBar *menuBar = new wxMenuBar;
172 menuBar->Append(menuFile, "&File");
173
174 // ... and attach this menu bar to the frame
175 SetMenuBar(menuBar);
176
177 CreateStatusBar(1);
178
179 m_Html = new wxHtmlWindow(this);
180 m_Html -> SetRelatedFrame(this, "HTML : %s");
181 m_Html -> SetRelatedStatusBar(0);
182 m_Name = "test.htm";
183 m_Html -> LoadPage(m_Name);
184
185 m_Prn = new wxHtmlEasyPrinting("Easy Printing Demo", this);
186 m_Prn -> SetHeader(m_Name + "(@PAGENUM@/@PAGESCNT@)<hr>", wxPAGE_ALL);
187 }
188
189
190 // event handlers
191
192 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
193 {
194 delete m_Prn;
195 // TRUE is to force the frame to close
196 Close(TRUE);
197 }
198
199
200 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
201 {
202 wxMessageBox("HTML printing sample\n\n(c) Vaclav Slavik, 1999");
203 }
204
205
206 void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
207 {
208 m_Prn -> PrinterSetup();
209 }
210
211
212 void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
213 {
214 m_Prn -> PageSetup();
215 }
216
217
218 void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
219 {
220 m_Prn -> PrintFile(m_Name);
221 }
222
223
224 void MyFrame::OnPreview(wxCommandEvent& WXUNUSED(event))
225 {
226 m_Prn -> PreviewFile(m_Name);
227 }
228
229
230 void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
231 {
232 wxFileDialog dialog(this, "Open HTML page", "", "", "*.htm", 0);
233
234 if (dialog.ShowModal() == wxID_OK)
235 {
236 m_Name = dialog.GetPath();
237 m_Html -> LoadPage(m_Name);
238 m_Prn -> SetHeader(m_Name + "(@PAGENUM@/@PAGESCNT@)<hr>", wxPAGE_ALL);
239 }
240 }
241
242