printing.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: printing.cpp
  3. // Purpose: wxHtml sample: wxHtmlEasyPrinting test
  4. // Author: Vaclav Slavik
  5. // Copyright: (c) 1998-2009 wxWidgets team
  6. // Licence: wxWindows licence
  7. /////////////////////////////////////////////////////////////////////////////
  8. // For compilers that support precompilation, includes "wx/wx.h".
  9. #include "wx/wxprec.h"
  10. #ifdef __BORLANDC__
  11. #pragma hdrstop
  12. #endif
  13. // for all others, include the necessary headers (this file is usually all you
  14. // need because it includes almost all "standard" wxWidgets headers
  15. #ifndef WX_PRECOMP
  16. #include "wx/wx.h"
  17. #endif
  18. #include "wx/image.h"
  19. #include "wx/html/htmlwin.h"
  20. #include "wx/html/htmprint.h"
  21. #ifndef wxHAS_IMAGES_IN_RESOURCES
  22. #include "../../sample.xpm"
  23. #endif
  24. // ----------------------------------------------------------------------------
  25. // private classes
  26. // ----------------------------------------------------------------------------
  27. // Define a new application type, each program should derive a class from wxApp
  28. class MyApp : public wxApp
  29. {
  30. public:
  31. // override base class virtuals
  32. // ----------------------------
  33. // this one is called on application startup and is a good place for the app
  34. // initialization (doing it here and not in the ctor allows to have an error
  35. // return: if OnInit() returns false, the application terminates)
  36. virtual bool OnInit();
  37. };
  38. // Define a new frame type: this is going to be our main frame
  39. class MyFrame : public wxFrame
  40. {
  41. public:
  42. // ctor and dtor
  43. MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
  44. virtual ~MyFrame();
  45. // event handlers (these functions should _not_ be virtual)
  46. void OnQuit(wxCommandEvent& event);
  47. void OnAbout(wxCommandEvent& event);
  48. void OnPageSetup(wxCommandEvent& event);
  49. void OnPrint(wxCommandEvent& event);
  50. void OnPreview(wxCommandEvent& event);
  51. void OnOpen(wxCommandEvent& event);
  52. void OnPrintSmall(wxCommandEvent& event);
  53. void OnPrintNormal(wxCommandEvent& event);
  54. void OnPrintHuge(wxCommandEvent& event);
  55. private:
  56. wxHtmlWindow *m_Html;
  57. wxHtmlEasyPrinting *m_Prn;
  58. wxString m_Name;
  59. // any class wishing to process wxWidgets events must use this macro
  60. wxDECLARE_EVENT_TABLE();
  61. };
  62. // ----------------------------------------------------------------------------
  63. // constants
  64. // ----------------------------------------------------------------------------
  65. // IDs for the controls and the menu commands
  66. enum
  67. {
  68. // menu items
  69. Minimal_Quit = 1,
  70. Minimal_Print,
  71. Minimal_Preview,
  72. Minimal_PageSetup,
  73. Minimal_Open,
  74. Minimal_PrintSmall,
  75. Minimal_PrintNormal,
  76. Minimal_PrintHuge
  77. };
  78. // ----------------------------------------------------------------------------
  79. // event tables and other macros for wxWidgets
  80. // ----------------------------------------------------------------------------
  81. // the event tables connect the wxWidgets events with the functions (event
  82. // handlers) which process them. It can be also done at run-time, but for the
  83. // simple menu events like this the static method is much simpler.
  84. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  85. EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
  86. EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
  87. EVT_MENU(Minimal_Print, MyFrame::OnPrint)
  88. EVT_MENU(Minimal_Preview, MyFrame::OnPreview)
  89. EVT_MENU(Minimal_PageSetup, MyFrame::OnPageSetup)
  90. EVT_MENU(Minimal_Open, MyFrame::OnOpen)
  91. EVT_MENU(Minimal_PrintSmall, MyFrame::OnPrintSmall)
  92. EVT_MENU(Minimal_PrintNormal, MyFrame::OnPrintNormal)
  93. EVT_MENU(Minimal_PrintHuge, MyFrame::OnPrintHuge)
  94. wxEND_EVENT_TABLE()
  95. // Create a new application object: this macro will allow wxWidgets to create
  96. // the application object during program execution (it's better than using a
  97. // static object for many reasons) and also declares the accessor function
  98. // wxGetApp() which will return the reference of the right type (i.e. MyApp and
  99. // not wxApp)
  100. IMPLEMENT_APP(MyApp)
  101. // ============================================================================
  102. // implementation
  103. // ============================================================================
  104. // ----------------------------------------------------------------------------
  105. // the application class
  106. // ----------------------------------------------------------------------------
  107. // `Main program' equivalent: the program execution "starts" here
  108. bool MyApp::OnInit()
  109. {
  110. if ( !wxApp::OnInit() )
  111. return false;
  112. #if wxUSE_LIBPNG
  113. wxImage::AddHandler(new wxPNGHandler);
  114. #endif
  115. #if wxUSE_LIBJPEG
  116. wxImage::AddHandler(new wxJPEGHandler);
  117. #endif
  118. #if wxUSE_GIF
  119. wxImage::AddHandler(new wxGIFHandler);
  120. #endif
  121. MyFrame *frame = new MyFrame(_("Printing test"),
  122. wxDefaultPosition, wxSize(640, 480));
  123. // Show it
  124. frame->Show(true);
  125. // success: wxApp::OnRun() will be called which will enter the main message
  126. // loop and the application will run. If we returned false here, the
  127. // application would exit immediately.
  128. return true;
  129. }
  130. // ----------------------------------------------------------------------------
  131. // main frame
  132. // ----------------------------------------------------------------------------
  133. // frame constructor
  134. MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
  135. : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
  136. {
  137. SetIcon(wxICON(sample));
  138. // create a menu bar
  139. wxMenu *menuFile = new wxMenu;
  140. menuFile->Append(Minimal_Open, _("Open...\tCtrl-O"));
  141. menuFile->AppendSeparator();
  142. menuFile->Append(Minimal_PageSetup, _("Page &Setup"));
  143. menuFile->Append(Minimal_Preview, _("Print pre&view..."));
  144. menuFile->Append(Minimal_Print, _("Print...\tCtrl-P"));
  145. menuFile->AppendSeparator();
  146. menuFile->Append(wxID_ABOUT, _("&About"));
  147. menuFile->AppendSeparator();
  148. menuFile->Append(Minimal_Quit, _("&Exit"));
  149. wxMenu *menuFonts = new wxMenu;
  150. menuFonts->AppendRadioItem(Minimal_PrintSmall, _("&Small Printer Fonts"));
  151. menuFonts->AppendRadioItem(Minimal_PrintNormal, _("&Normal Printer Fonts"));
  152. menuFonts->AppendRadioItem(Minimal_PrintHuge, _("&Huge Printer Fonts"));
  153. // now append the freshly created menu to the menu bar...
  154. wxMenuBar *menuBar = new wxMenuBar;
  155. menuBar->Append(menuFile, _("&File"));
  156. menuBar->Append(menuFonts, _("F&onts"));
  157. // ... and attach this menu bar to the frame
  158. SetMenuBar(menuBar);
  159. #if wxUSE_STATUSBAR
  160. CreateStatusBar(1);
  161. #endif // wxUSE_STATUSBAR
  162. m_Html = new wxHtmlWindow(this);
  163. m_Html -> SetRelatedFrame(this, _("HTML : %s"));
  164. #if wxUSE_STATUSBAR
  165. m_Html -> SetRelatedStatusBar(0);
  166. #endif // wxUSE_STATUSBAR
  167. m_Name = wxT("test.htm");
  168. m_Html -> LoadPage(m_Name);
  169. m_Prn = new wxHtmlEasyPrinting(_("Easy Printing Demo"), this);
  170. m_Prn -> SetHeader(m_Name + wxT("(@PAGENUM@/@PAGESCNT@)<hr>"), wxPAGE_ALL);
  171. // To specify where the AFM files are kept on Unix,
  172. // you may wish to do something like this
  173. // m_Prn->GetPrintData()->SetFontMetricPath(wxT("/home/julians/afm"));
  174. }
  175. // frame destructor
  176. MyFrame::~MyFrame()
  177. {
  178. wxDELETE(m_Prn);
  179. }
  180. // event handlers
  181. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  182. {
  183. // true is to force the frame to close
  184. Close(true);
  185. }
  186. void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
  187. {
  188. wxMessageBox(_("HTML printing sample\n\n(c) Vaclav Slavik, 1999"));
  189. }
  190. void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
  191. {
  192. m_Prn -> PageSetup();
  193. }
  194. void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
  195. {
  196. m_Prn -> PrintFile(m_Name);
  197. }
  198. void MyFrame::OnPreview(wxCommandEvent& WXUNUSED(event))
  199. {
  200. m_Prn -> PreviewFile(m_Name);
  201. }
  202. void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
  203. {
  204. wxFileDialog dialog(this, _("Open HTML page"), wxT(""), wxT(""), wxT("*.htm"), 0);
  205. if (dialog.ShowModal() == wxID_OK)
  206. {
  207. m_Name = dialog.GetPath();
  208. m_Html -> LoadPage(m_Name);
  209. m_Prn -> SetHeader(m_Name + wxT("(@PAGENUM@/@PAGESCNT@)<hr>"), wxPAGE_ALL);
  210. }
  211. }
  212. void MyFrame::OnPrintSmall(wxCommandEvent& WXUNUSED(event))
  213. {
  214. m_Prn->SetStandardFonts(8);
  215. }
  216. void MyFrame::OnPrintNormal(wxCommandEvent& WXUNUSED(event))
  217. {
  218. m_Prn->SetStandardFonts(12);
  219. }
  220. void MyFrame::OnPrintHuge(wxCommandEvent& WXUNUSED(event))
  221. {
  222. m_Prn->SetStandardFonts(28);
  223. }