X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/5526e819eca4465ed5520d49bccfebc6a28045e0..71cf399ff0f1ddb966b48e512b0fb4e690f5c440:/samples/html/printing/printing.cpp diff --git a/samples/html/printing/printing.cpp b/samples/html/printing/printing.cpp index 2febc3bd6c..411d7a03b6 100644 --- a/samples/html/printing/printing.cpp +++ b/samples/html/printing/printing.cpp @@ -1,18 +1,9 @@ -/* - * File: printing.cc - * Purpose: Printing demo for wxWindows class library - * Author: Julian Smart - * modified by Vaclav Slavik (wxHTML stuffs) - * Created: 1995 - * Updated: - * Copyright: (c) 1995, AIAI, University of Edinburgh - */ - -/* static const char sccsid[] = "%W% %G%"; */ - -#ifdef __GNUG__ -#pragma implementation -#endif +///////////////////////////////////////////////////////////////////////////// +// Name: printimg.cpp +// Purpose: wxHtmlEasyPrinting testing example +// Licence: wxWindows Licence +///////////////////////////////////////////////////////////////////////////// + // For compilers that support precompilation, includes "wx/wx.h". #include "wx/wxprec.h" @@ -21,292 +12,271 @@ #pragma hdrstop #endif +// for all others, include the necessary headers (this file is usually all you +// need because it includes almost all "standard" wxWidgets headers #ifndef WX_PRECOMP #include "wx/wx.h" #endif -#if !wxUSE_PRINTING_ARCHITECTURE -#error You must set wxUSE_PRINTING_ARCHITECTURE to 1 in setup.h to compile this demo. -#endif - -// Set this to 1 if you want to test PostScript printing under MSW. -// However, you'll also need to edit src/msw/makefile.nt. - -//!!! DON'T DO THAT! This is wxHTML sample now -#define wxTEST_POSTSCRIPT_IN_MSW 0 +#include "wx/image.h" +#include "wx/html/htmlwin.h" +#include "wx/html/htmprint.h" -#include -#include "wx/metafile.h" -#include "wx/print.h" -#include "wx/printdlg.h" -#include "wx/accel.h" +// ---------------------------------------------------------------------------- +// private classes +// ---------------------------------------------------------------------------- -#if wxTEST_POSTSCRIPT_IN_MSW -#include "wx/generic/printps.h" -#include "wx/generic/prntdlgg.h" -#endif - -#include -#include -#include "printing.h" - -#ifndef __WXMSW__ -#include "mondrian.xpm" -#endif - -// Global print data, to remember settings during the session -wxPrintData *g_printData = (wxPrintData*) NULL ; - -// Global page setup data -wxPageSetupData* g_pageSetupData = (wxPageSetupData*) NULL; - - -// Declare a frame -MyFrame *frame = (MyFrame *) NULL; -wxHtmlWindow *html = NULL; -int orientation = wxPORTRAIT; +// Define a new application type, each program should derive a class from wxApp +class MyApp : public wxApp +{ + public: + // override base class virtuals + // ---------------------------- -// Main proc -IMPLEMENT_APP(MyApp) + // this one is called on application startup and is a good place for the app + // initialization (doing it here and not in the ctor allows to have an error + // return: if OnInit() returns false, the application terminates) + virtual bool OnInit(); +}; -MyApp::MyApp() +// Define a new frame type: this is going to be our main frame +class MyFrame : public wxFrame { -} - -// The `main program' equivalent, creating the windows and returning the -// main frame -bool MyApp::OnInit(void) + public: + // ctor and dtor + + MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); + virtual ~MyFrame(); + + // event handlers (these functions should _not_ be virtual) + void OnQuit(wxCommandEvent& event); + void OnAbout(wxCommandEvent& event); + + void OnPageSetup(wxCommandEvent& event); + void OnPrint(wxCommandEvent& event); + void OnPreview(wxCommandEvent& event); + void OnOpen(wxCommandEvent& event); + + void OnPrintSmall(wxCommandEvent& event); + void OnPrintNormal(wxCommandEvent& event); + void OnPrintHuge(wxCommandEvent& event); + + + private: + wxHtmlWindow *m_Html; + wxHtmlEasyPrinting *m_Prn; + wxString m_Name; + // any class wishing to process wxWidgets events must use this macro + DECLARE_EVENT_TABLE() +}; + +// ---------------------------------------------------------------------------- +// constants +// ---------------------------------------------------------------------------- + +// IDs for the controls and the menu commands +enum { - g_printData = new wxPrintData; - g_pageSetupData = new wxPageSetupDialogData; - - // Create the main frame window - frame = new MyFrame((wxFrame *) NULL, (char *) "wxWindows Printing Demo", wxPoint(0, 0), wxSize(600, 400)); + // menu items + Minimal_Quit = 1, + Minimal_Print, + Minimal_Preview, + Minimal_PageSetup, + Minimal_Open, + Minimal_PrintSmall, + Minimal_PrintNormal, + Minimal_PrintHuge + +}; + +// ---------------------------------------------------------------------------- +// event tables and other macros for wxWidgets +// ---------------------------------------------------------------------------- + +// the event tables connect the wxWidgets events with the functions (event +// handlers) which process them. It can be also done at run-time, but for the +// simple menu events like this the static method is much simpler. +BEGIN_EVENT_TABLE(MyFrame, wxFrame) + EVT_MENU(Minimal_Quit, MyFrame::OnQuit) + EVT_MENU(wxID_ABOUT, MyFrame::OnAbout) + EVT_MENU(Minimal_Print, MyFrame::OnPrint) + EVT_MENU(Minimal_Preview, MyFrame::OnPreview) + EVT_MENU(Minimal_PageSetup, MyFrame::OnPageSetup) + EVT_MENU(Minimal_Open, MyFrame::OnOpen) + EVT_MENU(Minimal_PrintSmall, MyFrame::OnPrintSmall) + EVT_MENU(Minimal_PrintNormal, MyFrame::OnPrintNormal) + EVT_MENU(Minimal_PrintHuge, MyFrame::OnPrintHuge) +END_EVENT_TABLE() - // Give it a status line - frame->CreateStatusBar(2); +// Create a new application object: this macro will allow wxWidgets to create +// the application object during program execution (it's better than using a +// static object for many reasons) and also declares the accessor function +// wxGetApp() which will return the reference of the right type (i.e. MyApp and +// not wxApp) +IMPLEMENT_APP(MyApp) - // Load icon and bitmap - frame->SetIcon( wxICON( mondrian) ); +// ============================================================================ +// implementation +// ============================================================================ - // Make a menubar - wxMenu *file_menu = new wxMenu; +// ---------------------------------------------------------------------------- +// the application class +// ---------------------------------------------------------------------------- +// `Main program' equivalent: the program execution "starts" here - file_menu->Append(WXPRINT_PRINT, "&Print...", "Print"); - file_menu->Append(WXPRINT_PRINT_SETUP, "Print &Setup...", "Setup printer properties"); - file_menu->Append(WXPRINT_PAGE_SETUP, "Page Set&up...", "Page setup"); - file_menu->Append(WXPRINT_PREVIEW, "Print Pre&view", "Preview"); +bool MyApp::OnInit() +{ + if ( !wxApp::OnInit() ) + return false; - // Accelerators - wxAcceleratorEntry entries[1]; - entries[0].Set(wxACCEL_CTRL, (int) 'V', WXPRINT_PREVIEW); - wxAcceleratorTable accel(1, entries); - frame->SetAcceleratorTable(accel); - - file_menu->AppendSeparator(); - file_menu->Append(WXPRINT_QUIT, "E&xit", "Exit program"); +#if wxUSE_LIBPNG + wxImage::AddHandler(new wxPNGHandler); +#endif +#if wxUSE_LIBJPEG + wxImage::AddHandler(new wxJPEGHandler); +#endif +#if wxUSE_GIF + wxImage::AddHandler(new wxGIFHandler); +#endif - wxMenu *help_menu = new wxMenu; - help_menu->Append(WXPRINT_ABOUT, "&About", "About this demo"); + MyFrame *frame = new MyFrame(_("Printing test"), + wxDefaultPosition, wxSize(640, 480)); - wxMenuBar *menu_bar = new wxMenuBar; + // Show it and tell the application that it's our main window + // @@@ what does it do exactly, in fact? is it necessary here? + frame->Show(true); + SetTopWindow(frame); - menu_bar->Append(file_menu, "&File"); - menu_bar->Append(help_menu, "&Help"); - // Associate the menu bar with the frame - frame->SetMenuBar(menu_bar); + // success: wxApp::OnRun() will be called which will enter the main message + // loop and the application will run. If we returned false here, the + // application would exit immediately. + return true; +} - frame->Centre(wxBOTH); - frame->Show(TRUE); +// ---------------------------------------------------------------------------- +// main frame +// ---------------------------------------------------------------------------- - frame->SetStatusText("Printing demo"); - SetTopWindow(frame); +// frame constructor +MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) + : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size) +{ + // create a menu bar + wxMenu *menuFile = new wxMenu; + menuFile->Append(Minimal_Open, _("Open...\tCtrl-O")); + menuFile->AppendSeparator(); + menuFile->Append(Minimal_PageSetup, _("Page Setup")); + menuFile->Append(Minimal_Print, _("Print...")); + menuFile->Append(Minimal_Preview, _("Preview...")); + menuFile->AppendSeparator(); + menuFile->Append(wxID_ABOUT, _("&About")); + menuFile->AppendSeparator(); + menuFile->Append(Minimal_Quit, _("&Exit")); + + wxMenu *testFile = new wxMenu; + testFile->Append(Minimal_PrintSmall, _("Small Printer Fonts")); + testFile->Append(Minimal_PrintNormal, _("Normal Printer Fonts")); + testFile->Append(Minimal_PrintHuge, _("Huge Printer Fonts")); + + // now append the freshly created menu to the menu bar... + wxMenuBar *menuBar = new wxMenuBar; + menuBar->Append(menuFile, _("&File")); + menuBar->Append(testFile, _("&Test")); + + // ... and attach this menu bar to the frame + SetMenuBar(menuBar); + +#if wxUSE_STATUSBAR + CreateStatusBar(1); +#endif // wxUSE_STATUSBAR + + m_Html = new wxHtmlWindow(this); + m_Html -> SetRelatedFrame(this, _("HTML : %s")); +#if wxUSE_STATUSBAR + m_Html -> SetRelatedStatusBar(0); +#endif // wxUSE_STATUSBAR + m_Name = wxT("test.htm"); + m_Html -> LoadPage(m_Name); + + m_Prn = new wxHtmlEasyPrinting(_("Easy Printing Demo"), this); + m_Prn -> SetHeader(m_Name + wxT("(@PAGENUM@/@PAGESCNT@)
"), wxPAGE_ALL); - return TRUE; + // To specify where the AFM files are kept on Unix, + // you may wish to do something like this + // m_Prn->GetPrintData()->SetFontMetricPath(wxT("/home/julians/afm")); } -int MyApp::OnExit() +// frame destructor +MyFrame::~MyFrame() { - delete g_printData; - delete g_pageSetupData; - return 1; + delete m_Prn; + m_Prn = (wxHtmlEasyPrinting *) NULL; } -BEGIN_EVENT_TABLE(MyFrame, wxFrame) - EVT_MENU(WXPRINT_QUIT, MyFrame::OnExit) - EVT_MENU(WXPRINT_PRINT, MyFrame::OnPrint) - EVT_MENU(WXPRINT_PREVIEW, MyFrame::OnPrintPreview) - EVT_MENU(WXPRINT_PRINT_SETUP, MyFrame::OnPrintSetup) - EVT_MENU(WXPRINT_PAGE_SETUP, MyFrame::OnPageSetup) - EVT_MENU(WXPRINT_ABOUT, MyFrame::OnPrintAbout) -END_EVENT_TABLE() -// Define my frame constructor -MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size): - wxFrame(frame, -1, title, pos, size) -{ - html = new wxHtmlWindow(this); - html -> LoadPage("test.htm"); -} +// event handlers -void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event)) +void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) { - Close(TRUE); + // true is to force the frame to close + Close(true); } -void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event)) -{ - wxPrinter printer; - MyPrintout printout("My printout"); - if (!printer.Print(this, &printout, TRUE)) - wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK); -} -void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event)) +void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) { - wxPrintData printData; - printData.SetOrientation(orientation); - - // Pass two printout objects: for preview, and possible printing. - wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout, & printData); - if (!preview->Ok()) - { - delete preview; - wxMessageBox("There was a problem previewing.\nPerhaps your current printer is not set correctly?", "Previewing", wxOK); - return; - } - - wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650)); - frame->Centre(wxBOTH); - frame->Initialize(); - frame->Show(TRUE); + wxMessageBox(_("HTML printing sample\n\n(c) Vaclav Slavik, 1999")); } -void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event)) -{ - wxPrintDialogData printDialogData(* g_printData); - wxPrintDialog printerDialog(this, & printDialogData); - - printerDialog.GetPrintDialogData().SetSetupDialog(TRUE); - printerDialog.ShowModal(); - - (*g_printData) = printerDialog.GetPrintDialogData().GetPrintData(); -} void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event)) { - (*g_pageSetupData) = * g_printData; - - wxPageSetupDialog pageSetupDialog(this, g_pageSetupData); - pageSetupDialog.ShowModal(); - - (*g_printData) = pageSetupDialog.GetPageSetupData().GetPrintData(); - (*g_pageSetupData) = pageSetupDialog.GetPageSetupData(); + m_Prn -> PageSetup(); } - -void MyFrame::OnPrintAbout(wxCommandEvent& WXUNUSED(event)) +void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event)) { - (void)wxMessageBox("wxWindows printing demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk\n\nModified by Vaclav Slavik to show wxHtml features", - "About wxWindows printing demo", wxOK|wxCENTRE); + m_Prn -> PrintFile(m_Name); } -bool MyPrintout::OnPrintPage(int page) +void MyFrame::OnPreview(wxCommandEvent& WXUNUSED(event)) { - wxDC *dc = GetDC(); - if (dc) - { - if (page == 1) - DrawPageOne(dc); - - return TRUE; - } - else - return FALSE; + m_Prn -> PreviewFile(m_Name); } -bool MyPrintout::OnBeginDocument(int startPage, int endPage) -{ - if (!wxPrintout::OnBeginDocument(startPage, endPage)) - return FALSE; - return TRUE; +void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event)) +{ + wxFileDialog dialog(this, _("Open HTML page"), wxT(""), wxT(""), wxT("*.htm"), 0); + + if (dialog.ShowModal() == wxID_OK) + { + m_Name = dialog.GetPath(); + m_Html -> LoadPage(m_Name); + m_Prn -> SetHeader(m_Name + wxT("(@PAGENUM@/@PAGESCNT@)
"), wxPAGE_ALL); + } } -void MyPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) + +void MyFrame::OnPrintSmall(wxCommandEvent& WXUNUSED(event)) { - *minPage = 1; - *maxPage = 1; - *selPageFrom = 1; - *selPageTo = 1; + int fontsizes[] = { 4, 6, 8, 10, 12, 20, 24 }; + m_Prn->SetFonts(wxEmptyString, wxEmptyString, fontsizes); } -bool MyPrintout::HasPage(int pageNum) +void MyFrame::OnPrintNormal(wxCommandEvent& WXUNUSED(event)) { - return (pageNum == 1); + m_Prn->SetFonts(wxEmptyString, wxEmptyString, 0); } - -void MyPrintout::DrawPageOne(wxDC *dc) +void MyFrame::OnPrintHuge(wxCommandEvent& WXUNUSED(event)) { - int leftMargin = 20; - int topMargin = 40; - -/* You might use THIS code to set the printer DC to ROUGHLY reflect - * the screen text size. This page also draws lines of actual length 5cm - * on the page. - */ - // Get the logical pixels per inch of screen and printer - int ppiScreenX, ppiScreenY; - GetPPIScreen(&ppiScreenX, &ppiScreenY); - int ppiPrinterX, ppiPrinterY; - GetPPIPrinter(&ppiPrinterX, &ppiPrinterY); - - // Here we obtain internal cell representation of HTML document: - wxHtmlContainerCell *cell = html -> GetInternalRepresentation(); - - // Now we have to check in case our real page size is reduced - // (e.g. because we're drawing to a print preview memory DC) - int pageWidth, pageHeight; - int w, h; - dc->GetSize(&w, &h); - GetPageSizePixels(&pageWidth, &pageHeight); - - // Now we must scale it somehow. The best would be to suppose that html window - // width is equal to page width: - - float scale = (float)((float)(pageWidth - 0 * leftMargin)/((float)cell -> GetMaxLineWidth() + 2 * leftMargin)); - - // If printer pageWidth == current DC width, then this doesn't - // change. But w might be the preview bitmap width, so scale down. - float overallScale = scale * (float)(w/(float)pageWidth); - dc->SetUserScale(overallScale, overallScale); - - // Calculate conversion factor for converting millimetres into - // logical units. - // There are approx. 25.1 mm to the inch. There are ppi - // device units to the inch. Therefore 1 mm corresponds to - // ppi/25.1 device units. We also divide by the - // screen-to-printer scaling factor, because we need to - // unscale to pass logical units to DrawLine. - - dc->SetBackgroundMode(wxTRANSPARENT); - - // TESTING - - int pageWidthMM, pageHeightMM; - GetPageSizeMM(&pageWidthMM, &pageHeightMM); - - - // This is all the printing : - cell -> Draw(*dc, leftMargin, topMargin, 0, cell -> GetHeight()); + int fontsizes[] = { 20, 26, 28, 30, 32, 40, 44 }; + m_Prn->SetFonts(wxEmptyString, wxEmptyString, fontsizes); } - - -