X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c801d85f158c4cba50b588807daabdcbd0ed3853..7be07660b4dec326ec99f84edac9b862ac34d224:/samples/printing/printing.cpp diff --git a/samples/printing/printing.cpp b/samples/printing/printing.cpp index d11143d205..af82cf63d8 100644 --- a/samples/printing/printing.cpp +++ b/samples/printing/printing.cpp @@ -25,35 +25,35 @@ #include "wx/postscrp.h" #endif -#if !USE_PRINTING_ARCHITECTURE -#error You must set USE_PRINTING_ARCHITECTURE to 1 in wx_setup.h to compile this demo. +#if !wxUSE_PRINTING_ARCHITECTURE +#error You must set wxUSE_PRINTING_ARCHITECTURE to 1 in wx_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. +#define wxTEST_POSTSCRIPT_IN_MSW 0 + #include #include "wx/metafile.h" #include "wx/print.h" #include "wx/printdlg.h" + +#if wxTEST_POSTSCRIPT_IN_MSW #include "wx/generic/printps.h" #include "wx/generic/prntdlgg.h" +#endif #include "printing.h" // Declare a frame -MyFrame *frame = NULL; +MyFrame *frame = (MyFrame *) NULL; int orientation = wxPORTRAIT; // Main proc IMPLEMENT_APP(MyApp) -// Must initialise these in OnInit, not statically -wxPen *red_pen; -wxFont *labelFont; -wxFont *itemFont; - -float zoom_factor = 1.0; - -#ifdef __X__ -#include "aiai.xbm" +#if defined(__WXGTK__) || defined(__WXMOTIF__) +#include "folder.xpm" #endif // Writes a header on a page. Margin units are in millimetres. @@ -67,25 +67,20 @@ MyApp::MyApp() // main frame bool MyApp::OnInit(void) { - // Create a red pen - red_pen = new wxPen("RED", 3, wxSOLID); - - // Create a small font - itemFont = new wxFont(11, wxROMAN, wxNORMAL, wxNORMAL); - labelFont = new wxFont(12, wxROMAN, wxITALIC, wxBOLD); + m_testFont = new wxFont(10, wxSWISS, wxNORMAL, wxNORMAL); // Create the main frame window - frame = new MyFrame(NULL, "wxWindows Printing Demo", wxPoint(0, 0), wxSize(400, 400)); + frame = new MyFrame((wxFrame *) NULL, (char *) "wxWindows Printing Demo", wxPoint(0, 0), wxSize(400, 400)); // Give it a status line frame->CreateStatusBar(2); // Load icon and bitmap -#ifdef __WINDOWS__ - frame->SetIcon(wxIcon("aiai_icn")); +#ifdef __WXMSW__ + frame->SetIcon(wxIcon("mondrian")); #endif #ifdef __X__ - frame->SetIcon(wxIcon(aiai_bits, aiai_width, aiai_height)); +// frame->SetIcon(wxIcon(mondrian_bits, mondrian_width, mondrian_height)); #endif // Make a menubar @@ -96,7 +91,7 @@ bool MyApp::OnInit(void) file_menu->Append(WXPRINT_PAGE_SETUP, "Page Set&up...", "Page setup"); file_menu->Append(WXPRINT_PREVIEW, "Print Pre&view", "Preview"); -#ifdef __WINDOWS__ +#if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW file_menu->AppendSeparator(); file_menu->Append(WXPRINT_PRINT_PS, "Print PostScript...", "Print (PostScript)"); file_menu->Append(WXPRINT_PRINT_SETUP_PS, "Print Setup PostScript...", "Setup printer properties (PostScript)"); @@ -122,11 +117,6 @@ bool MyApp::OnInit(void) // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction canvas->SetScrollbars(20, 20, 50, 50); - // This ensures that the fonts get created as _screen_ - // fonts, not printer fonts. - canvas->SetFont(itemFont); - canvas->SetFont(labelFont); - frame->canvas = canvas; frame->Centre(wxBOTH); @@ -145,28 +135,30 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) 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) +#if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW EVT_MENU(WXPRINT_PRINT_PS, MyFrame::OnPrintPS) EVT_MENU(WXPRINT_PREVIEW_PS, MyFrame::OnPrintPreviewPS) EVT_MENU(WXPRINT_PRINT_SETUP_PS, MyFrame::OnPrintSetupPS) EVT_MENU(WXPRINT_PAGE_SETUP_PS, MyFrame::OnPageSetupPS) - EVT_MENU(WXPRINT_ABOUT, MyFrame::OnPrintAbout) +#endif 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) { - canvas = NULL; + canvas = (MyCanvas *) NULL; } -void MyFrame::OnExit(wxCommandEvent& event) +void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event)) { Close(TRUE); } -void MyFrame::OnPrint(wxCommandEvent& event) +void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event)) { -#ifdef __WINDOWS__ +#ifdef __WXMSW__ wxGetApp().SetPrintMode(wxPRINT_WINDOWS); #else wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT); @@ -177,18 +169,9 @@ void MyFrame::OnPrint(wxCommandEvent& event) wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK); } -void MyFrame::OnPrintPS(wxCommandEvent& event) -{ - wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT); - - wxPostScriptPrinter printer; - MyPrintout printout("My printout"); - printer.Print(this, &printout, TRUE); -} - -void MyFrame::OnPrintPreview(wxCommandEvent& event) +void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event)) { -#ifdef __WINDOWS__ +#ifdef __WXMSW__ wxGetApp().SetPrintMode(wxPRINT_WINDOWS); #else wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT); @@ -211,24 +194,9 @@ void MyFrame::OnPrintPreview(wxCommandEvent& event) frame->Show(TRUE); } -void MyFrame::OnPrintPreviewPS(wxCommandEvent& event) +void MyFrame::OnPrintSetup(wxCommandEvent& WXUNUSED(event)) { - wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT); - - wxPrintData printData; - printData.SetOrientation(orientation); - - // Pass two printout objects: for preview, and possible printing. - wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout, & printData); - wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650)); - frame->Centre(wxBOTH); - frame->Initialize(); - frame->Show(TRUE); -} - -void MyFrame::OnPrintSetup(wxCommandEvent& event) -{ -#ifdef __WINDOWS__ +#ifdef __WXMSW__ wxGetApp().SetPrintMode(wxPRINT_WINDOWS); #else wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT); @@ -236,7 +204,7 @@ void MyFrame::OnPrintSetup(wxCommandEvent& event) wxPrintData data; data.SetOrientation(orientation); -#ifdef __WINDOWS__ +#ifdef __WXMSW__ wxPrintDialog printerDialog(this, & data); #else wxGenericPrintDialog printerDialog(this, & data); @@ -247,9 +215,9 @@ void MyFrame::OnPrintSetup(wxCommandEvent& event) orientation = printerDialog.GetPrintData().GetOrientation(); } -void MyFrame::OnPageSetup(wxCommandEvent& event) +void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event)) { -#ifdef __WINDOWS__ +#ifdef __WXMSW__ wxGetApp().SetPrintMode(wxPRINT_WINDOWS); #else wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT); @@ -257,7 +225,7 @@ void MyFrame::OnPageSetup(wxCommandEvent& event) wxPageSetupData data; data.SetOrientation(orientation); -#ifdef __WINDOWS__ +#ifdef __WXMSW__ wxPageSetupDialog pageSetupDialog(this, & data); #else wxGenericPageSetupDialog pageSetupDialog(this, & data); @@ -268,7 +236,32 @@ void MyFrame::OnPageSetup(wxCommandEvent& event) orientation = data.GetOrientation(); } -void MyFrame::OnPrintSetupPS(wxCommandEvent& event) +#if defined(__WXMSW__) && wxTEST_POSTSCRIPT_IN_MSW +void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event)) +{ + wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT); + + wxPostScriptPrinter printer; + MyPrintout printout("My printout"); + printer.Print(this, &printout, TRUE); +} + +void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event)) +{ + wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT); + + wxPrintData printData; + printData.SetOrientation(orientation); + + // Pass two printout objects: for preview, and possible printing. + wxPrintPreview *preview = new wxPrintPreview(new MyPrintout, new MyPrintout, & printData); + wxPreviewFrame *frame = new wxPreviewFrame(preview, this, "Demo Print Preview", wxPoint(100, 100), wxSize(600, 650)); + frame->Centre(wxBOTH); + frame->Initialize(); + frame->Show(TRUE); +} + +void MyFrame::OnPrintSetupPS(wxCommandEvent& WXUNUSED(event)) { wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT); @@ -282,7 +275,7 @@ void MyFrame::OnPrintSetupPS(wxCommandEvent& event) orientation = printerDialog.GetPrintData().GetOrientation(); } -void MyFrame::OnPageSetupPS(wxCommandEvent& event) +void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event)) { wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT); @@ -294,8 +287,10 @@ void MyFrame::OnPageSetupPS(wxCommandEvent& event) orientation = pageSetupDialog.GetPageSetupData().GetOrientation(); } +#endif -void MyFrame::OnPrintAbout(wxCommandEvent& event) + +void MyFrame::OnPrintAbout(wxCommandEvent& WXUNUSED(event)) { (void)wxMessageBox("wxWindows printing demo\nAuthor: Julian Smart julian.smart@ukonline.co.uk", "About wxWindows printing demo", wxOK|wxCENTRE); @@ -303,25 +298,36 @@ void MyFrame::OnPrintAbout(wxCommandEvent& event) void MyFrame::Draw(wxDC& dc) { - dc.SetFont(itemFont); + dc.SetFont(* wxGetApp().m_testFont); + dc.SetBackgroundMode(wxTRANSPARENT); - dc.SetBrush(wxCYAN_BRUSH); - dc.SetPen(wxRED_PEN); + dc.SetBrush(* wxCYAN_BRUSH); + dc.SetPen(* wxRED_PEN); - dc.DrawRectangle(0.0, 30.0, 200.0, 100.0); - dc.DrawText("Rectangle 200 by 100", 40.0, 40.0); + dc.DrawRectangle(0, 30, 200, 100); + dc.DrawText("Rectangle 200 by 100", 40, 40); - dc.DrawEllipse(50.0, 140.0, 100.0, 50.0); + dc.DrawEllipse(50, 140, 100, 50); - dc.DrawText("Test message: this is in 11 point text", 10.0, 180.0); + dc.DrawText("Test message: this is in 10 point text", 10, 180); - dc.SetPen(wxBLACK_PEN); - dc.DrawLine(0.0, 0.0, 200.0, 200.0); - dc.DrawLine(200.0, 0.0, 0.0, 200.0); + dc.SetPen(* wxBLACK_PEN); + dc.DrawLine(0, 0, 200, 200); + dc.DrawLine(200, 0, 0, 200); + +#if defined(__WXGTK__) || defined(__WXMOTIF__) + wxIcon my_icon( folder_xpm ); +#elif defined(__WXMSW__) + wxIcon my_icon( "mondrian" ); +#elif +#error "Platform not supported." +#endif + + dc.DrawIcon( my_icon, 100, 100); } -void MyFrame::OnSize(wxSizeEvent& event) +void MyFrame::OnSize(wxSizeEvent& event ) { wxFrame::OnSize(event); } @@ -346,15 +352,14 @@ void MyCanvas::OnDraw(wxDC& dc) frame->Draw(dc); } -void MyCanvas::OnEvent(wxMouseEvent& event) +void MyCanvas::OnEvent(wxMouseEvent& WXUNUSED(event)) { } -// Define the behaviour for the frame closing -// - must delete all frames except for the main one. bool MyFrame::OnClose(void) { Show(FALSE); + delete wxGetApp().m_testFont; return TRUE; } @@ -373,7 +378,7 @@ bool MyPrintout::OnPrintPage(int page) char buf[200]; sprintf(buf, "PAGE %d", page); - dc->DrawText(buf, 10.0, 10.0); + dc->DrawText(buf, 10, 10); return TRUE; } @@ -438,7 +443,7 @@ void MyPrintout::DrawPageOne(wxDC *dc) // Set the scale and origin dc->SetUserScale(actualScale, actualScale); - dc->SetDeviceOrigin(posX, posY); + dc->SetDeviceOrigin( (long)posX, (long)posY ); frame->Draw(*dc); } @@ -485,14 +490,14 @@ void MyPrintout::DrawPageTwo(wxDC *dc) // Draw 50 mm by 50 mm L shape float logUnitsFactor = (float)(ppiPrinterX/(scale*25.1)); float logUnits = (float)(50*logUnitsFactor); - dc->SetPen(wxBLACK_PEN); - dc->DrawLine(50.0, 50.0, (float)(50.0 + logUnits), 50.0); - dc->DrawLine(50.0, 50.0, 50.0, (float)(50.0 + logUnits)); + dc->SetPen(* wxBLACK_PEN); + dc->DrawLine(50, 50, (long)(50.0 + logUnits), 50); + dc->DrawLine(50, 50, 50, (long)(50.0 + logUnits)); - dc->SetFont(itemFont); + dc->SetFont(* wxGetApp().m_testFont); dc->SetBackgroundMode(wxTRANSPARENT); - dc->DrawText("Some test text", 200.0, 200.0); + dc->DrawText("Some test text", 200, 200 ); // TESTING @@ -510,8 +515,10 @@ void MyPrintout::DrawPageTwo(wxDC *dc) float rightMarginLogical = (float)(logUnitsFactor*(pageWidthMM - rightMargin)); dc->SetPen(wxBLACK_PEN); - dc->DrawLine(leftMarginLogical, topMarginLogical, rightMarginLogical, topMarginLogical); - dc->DrawLine(leftMarginLogical, bottomMarginLogical, rightMarginLogical, bottomMarginLogical); + dc->DrawLine( (long)leftMarginLogical, (long)topMarginLogical, + (long)rightMarginLogical, (long)topMarginLogical); + dc->DrawLine( (long)leftMarginLogical, (long)bottomMarginLogical, + (long)rightMarginLogical, (long)bottomMarginLogical); WritePageHeader(this, dc, "A header", logUnitsFactor); } @@ -519,7 +526,7 @@ void MyPrintout::DrawPageTwo(wxDC *dc) // Writes a header on a page. Margin units are in millimetres. bool WritePageHeader(wxPrintout *printout, wxDC *dc, char *text, float mmToLogical) { - static wxFont *headerFont = NULL; + static wxFont *headerFont = (wxFont *) NULL; if (!headerFont) { headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD); @@ -541,10 +548,11 @@ bool WritePageHeader(wxPrintout *printout, wxDC *dc, char *text, float mmToLogic long xExtent, yExtent; dc->GetTextExtent(text, &xExtent, &yExtent); float xPos = (float)(((((pageWidthMM - leftMargin - rightMargin)/2.0)+leftMargin)*mmToLogical) - (xExtent/2.0)); - dc->DrawText(text, (long)xPos, topMarginLogical); + dc->DrawText(text, (long)xPos, (long)topMarginLogical); - dc->SetPen(wxBLACK_PEN); - dc->DrawLine(leftMarginLogical, topMarginLogical+yExtent, rightMarginLogical, topMarginLogical+yExtent); + dc->SetPen(* wxBLACK_PEN); + dc->DrawLine( (long)leftMarginLogical, (long)(topMarginLogical+yExtent), + (long)rightMarginLogical, (long)topMarginLogical+yExtent ); return TRUE; }