]>
git.saurik.com Git - wxWidgets.git/blob - samples/html/printing/printing.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtml sample: wxHtmlEasyPrinting test
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1998-2009 wxWidgets team
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx/wx.h".
11 #include "wx/wxprec.h"
17 // for all others, include the necessary headers (this file is usually all you
18 // need because it includes almost all "standard" wxWidgets headers
24 #include "wx/html/htmlwin.h"
25 #include "wx/html/htmprint.h"
27 #ifndef wxHAS_IMAGES_IN_RESOURCES
28 #include "../../sample.xpm"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 // Define a new application type, each program should derive a class from wxApp
37 class MyApp
: public wxApp
40 // override base class virtuals
41 // ----------------------------
43 // this one is called on application startup and is a good place for the app
44 // initialization (doing it here and not in the ctor allows to have an error
45 // return: if OnInit() returns false, the application terminates)
47 virtual bool OnInit();
50 // Define a new frame type: this is going to be our main frame
51 class MyFrame
: public wxFrame
56 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
59 // event handlers (these functions should _not_ be virtual)
60 void OnQuit(wxCommandEvent
& event
);
61 void OnAbout(wxCommandEvent
& event
);
63 void OnPageSetup(wxCommandEvent
& event
);
64 void OnPrint(wxCommandEvent
& event
);
65 void OnPreview(wxCommandEvent
& event
);
66 void OnOpen(wxCommandEvent
& event
);
68 void OnPrintSmall(wxCommandEvent
& event
);
69 void OnPrintNormal(wxCommandEvent
& event
);
70 void OnPrintHuge(wxCommandEvent
& event
);
75 wxHtmlEasyPrinting
*m_Prn
;
78 // any class wishing to process wxWidgets events must use this macro
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 // IDs for the controls and the menu commands
101 // ----------------------------------------------------------------------------
102 // event tables and other macros for wxWidgets
103 // ----------------------------------------------------------------------------
105 // the event tables connect the wxWidgets events with the functions (event
106 // handlers) which process them. It can be also done at run-time, but for the
107 // simple menu events like this the static method is much simpler.
108 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
109 EVT_MENU(Minimal_Quit
, MyFrame::OnQuit
)
110 EVT_MENU(wxID_ABOUT
, MyFrame::OnAbout
)
111 EVT_MENU(Minimal_Print
, MyFrame::OnPrint
)
112 EVT_MENU(Minimal_Preview
, MyFrame::OnPreview
)
113 EVT_MENU(Minimal_PageSetup
, MyFrame::OnPageSetup
)
114 EVT_MENU(Minimal_Open
, MyFrame::OnOpen
)
115 EVT_MENU(Minimal_PrintSmall
, MyFrame::OnPrintSmall
)
116 EVT_MENU(Minimal_PrintNormal
, MyFrame::OnPrintNormal
)
117 EVT_MENU(Minimal_PrintHuge
, MyFrame::OnPrintHuge
)
120 // Create a new application object: this macro will allow wxWidgets to create
121 // the application object during program execution (it's better than using a
122 // static object for many reasons) and also declares the accessor function
123 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
127 // ============================================================================
129 // ============================================================================
131 // ----------------------------------------------------------------------------
132 // the application class
133 // ----------------------------------------------------------------------------
134 // `Main program' equivalent: the program execution "starts" here
138 if ( !wxApp::OnInit() )
142 wxImage::AddHandler(new wxPNGHandler
);
145 wxImage::AddHandler(new wxJPEGHandler
);
148 wxImage::AddHandler(new wxGIFHandler
);
151 MyFrame
*frame
= new MyFrame(_("Printing test"),
152 wxDefaultPosition
, wxSize(640, 480));
157 // success: wxApp::OnRun() will be called which will enter the main message
158 // loop and the application will run. If we returned false here, the
159 // application would exit immediately.
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
169 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
170 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
172 SetIcon(wxICON(sample
));
175 wxMenu
*menuFile
= new wxMenu
;
176 menuFile
->Append(Minimal_Open
, _("Open...\tCtrl-O"));
177 menuFile
->AppendSeparator();
178 menuFile
->Append(Minimal_PageSetup
, _("Page &Setup"));
179 menuFile
->Append(Minimal_Preview
, _("Print pre&view..."));
180 menuFile
->Append(Minimal_Print
, _("Print...\tCtrl-P"));
181 menuFile
->AppendSeparator();
182 menuFile
->Append(wxID_ABOUT
, _("&About"));
183 menuFile
->AppendSeparator();
184 menuFile
->Append(Minimal_Quit
, _("&Exit"));
186 wxMenu
*menuFonts
= new wxMenu
;
187 menuFonts
->AppendRadioItem(Minimal_PrintSmall
, _("&Small Printer Fonts"));
188 menuFonts
->AppendRadioItem(Minimal_PrintNormal
, _("&Normal Printer Fonts"));
189 menuFonts
->AppendRadioItem(Minimal_PrintHuge
, _("&Huge Printer Fonts"));
191 // now append the freshly created menu to the menu bar...
192 wxMenuBar
*menuBar
= new wxMenuBar
;
193 menuBar
->Append(menuFile
, _("&File"));
194 menuBar
->Append(menuFonts
, _("F&onts"));
196 // ... and attach this menu bar to the frame
201 #endif // wxUSE_STATUSBAR
203 m_Html
= new wxHtmlWindow(this);
204 m_Html
-> SetRelatedFrame(this, _("HTML : %s"));
206 m_Html
-> SetRelatedStatusBar(0);
207 #endif // wxUSE_STATUSBAR
208 m_Name
= wxT("test.htm");
209 m_Html
-> LoadPage(m_Name
);
211 m_Prn
= new wxHtmlEasyPrinting(_("Easy Printing Demo"), this);
212 m_Prn
-> SetHeader(m_Name
+ wxT("(@PAGENUM@/@PAGESCNT@)<hr>"), wxPAGE_ALL
);
214 // To specify where the AFM files are kept on Unix,
215 // you may wish to do something like this
216 // m_Prn->GetPrintData()->SetFontMetricPath(wxT("/home/julians/afm"));
228 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
230 // true is to force the frame to close
235 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
237 wxMessageBox(_("HTML printing sample\n\n(c) Vaclav Slavik, 1999"));
241 void MyFrame::OnPageSetup(wxCommandEvent
& WXUNUSED(event
))
243 m_Prn
-> PageSetup();
247 void MyFrame::OnPrint(wxCommandEvent
& WXUNUSED(event
))
249 m_Prn
-> PrintFile(m_Name
);
253 void MyFrame::OnPreview(wxCommandEvent
& WXUNUSED(event
))
255 m_Prn
-> PreviewFile(m_Name
);
259 void MyFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
261 wxFileDialog
dialog(this, _("Open HTML page"), wxT(""), wxT(""), wxT("*.htm"), 0);
263 if (dialog
.ShowModal() == wxID_OK
)
265 m_Name
= dialog
.GetPath();
266 m_Html
-> LoadPage(m_Name
);
267 m_Prn
-> SetHeader(m_Name
+ wxT("(@PAGENUM@/@PAGESCNT@)<hr>"), wxPAGE_ALL
);
272 void MyFrame::OnPrintSmall(wxCommandEvent
& WXUNUSED(event
))
274 m_Prn
->SetStandardFonts(8);
277 void MyFrame::OnPrintNormal(wxCommandEvent
& WXUNUSED(event
))
279 m_Prn
->SetStandardFonts(12);
282 void MyFrame::OnPrintHuge(wxCommandEvent
& WXUNUSED(event
))
284 m_Prn
->SetStandardFonts(28);