X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2049ba38adafa0ec146880de29f26e32dd69a125..f234c60c4aac2e077b16b8fbb3009ad45a84c73b:/samples/printing/printing.cpp diff --git a/samples/printing/printing.cpp b/samples/printing/printing.cpp index 14e1fb48c5..4ace50d72a 100644 --- a/samples/printing/printing.cpp +++ b/samples/printing/printing.cpp @@ -22,40 +22,41 @@ #ifndef WX_PRECOMP #include "wx/wx.h" -#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" + +#include "wx/accel.h" + +#if wxTEST_POSTSCRIPT_IN_MSW #include "wx/generic/printps.h" #include "wx/generic/prntdlgg.h" +#endif #include "printing.h" +#ifndef __WXMSW__ +#include "mondrian.xpm" +#endif + // 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" -#endif - // Writes a header on a page. Margin units are in millimetres. bool WritePageHeader(wxPrintout *printout, wxDC *dc, char *text, float mmToLogical); @@ -67,26 +68,16 @@ 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 __WXMSW__ - frame->SetIcon(wxIcon("aiai_icn")); -#endif -#ifdef __X__ - frame->SetIcon(wxIcon(aiai_bits, aiai_width, aiai_height)); -#endif + frame->SetIcon( wxICON( mondrian) ); // Make a menubar wxMenu *file_menu = new wxMenu; @@ -96,7 +87,13 @@ 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 __WXMSW__ + // Accelerators + wxAcceleratorEntry entries[1]; + entries[0].Set(wxACCEL_CTRL, (int) 'V', WXPRINT_PREVIEW); + wxAcceleratorTable accel(1, entries); + frame->SetAcceleratorTable(accel); + +#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 +119,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); @@ -139,60 +131,49 @@ bool MyApp::OnInit(void) return TRUE; } +int MyApp::OnExit() +{ + delete wxGetApp().m_testFont; + return 1; +} + 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) +#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 __WXMSW__ - wxGetApp().SetPrintMode(wxPRINT_WINDOWS); -#else - wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT); -#endif 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::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 __WXMSW__ - wxGetApp().SetPrintMode(wxPRINT_WINDOWS); -#else - wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT); -#endif wxPrintData printData; printData.SetOrientation(orientation); @@ -211,67 +192,54 @@ 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 __WXMSW__ - wxGetApp().SetPrintMode(wxPRINT_WINDOWS); -#else - wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT); -#endif wxPrintData data; data.SetOrientation(orientation); -#ifdef __WXMSW__ wxPrintDialog printerDialog(this, & data); -#else - wxGenericPrintDialog printerDialog(this, & data); -#endif + printerDialog.GetPrintData().SetSetupDialog(TRUE); printerDialog.ShowModal(); orientation = printerDialog.GetPrintData().GetOrientation(); } -void MyFrame::OnPageSetup(wxCommandEvent& event) +void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event)) { -#ifdef __WXMSW__ - wxGetApp().SetPrintMode(wxPRINT_WINDOWS); -#else - wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT); -#endif wxPageSetupData data; data.SetOrientation(orientation); -#ifdef __WXMSW__ wxPageSetupDialog pageSetupDialog(this, & data); -#else - wxGenericPageSetupDialog pageSetupDialog(this, & data); -#endif pageSetupDialog.ShowModal(); data = pageSetupDialog.GetPageSetupData(); 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)) +{ + 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)) +{ wxPrintData data; data.SetOrientation(orientation); @@ -282,10 +250,8 @@ void MyFrame::OnPrintSetupPS(wxCommandEvent& event) orientation = printerDialog.GetPrintData().GetOrientation(); } -void MyFrame::OnPageSetupPS(wxCommandEvent& event) +void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event)) { - wxGetApp().SetPrintMode(wxPRINT_POSTSCRIPT); - wxPageSetupData data; data.SetOrientation(orientation); @@ -294,8 +260,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 +271,30 @@ 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, 30, 200, 100); + dc.DrawText("Rectangle 200 by 100", 40, 40); - dc.DrawRectangle(0.0, 30.0, 200.0, 100.0); - dc.DrawText("Rectangle 200 by 100", 40.0, 40.0); + dc.DrawEllipse(50, 140, 100, 50); - dc.DrawEllipse(50.0, 140.0, 100.0, 50.0); + dc.DrawText("Test message: this is in 10 point text", 10, 180); - dc.DrawText("Test message: this is in 11 point text", 10.0, 180.0); + dc.SetPen(* wxBLACK_PEN); + dc.DrawLine(0, 0, 200, 200); + dc.DrawLine(200, 0, 0, 200); + + wxIcon my_icon = wxICON(mondrian) ; - 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.DrawIcon( my_icon, 100, 100); } -void MyFrame::OnSize(wxSizeEvent& event) +void MyFrame::OnSize(wxSizeEvent& event ) { wxFrame::OnSize(event); } @@ -346,19 +319,10 @@ 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); - - return TRUE; -} - bool MyPrintout::OnPrintPage(int page) { wxDC *dc = GetDC(); @@ -370,10 +334,11 @@ bool MyPrintout::OnPrintPage(int page) DrawPageTwo(dc); dc->SetDeviceOrigin(0, 0); + dc->SetUserScale(1.0, 1.0); char buf[200]; sprintf(buf, "PAGE %d", page); - dc->DrawText(buf, 10.0, 10.0); + dc->DrawText(buf, 10, 10); return TRUE; } @@ -438,7 +403,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 +450,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, 250, (long)(50.0 + logUnits), 250); + dc->DrawLine(50, 250, 50, (long)(250.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 @@ -509,9 +474,11 @@ void MyPrintout::DrawPageTwo(wxDC *dc) float bottomMarginLogical = (float)(logUnitsFactor*(pageHeightMM - bottomMargin)); float rightMarginLogical = (float)(logUnitsFactor*(pageWidthMM - rightMargin)); - dc->SetPen(wxBLACK_PEN); - dc->DrawLine(leftMarginLogical, topMarginLogical, rightMarginLogical, topMarginLogical); - dc->DrawLine(leftMarginLogical, bottomMarginLogical, rightMarginLogical, bottomMarginLogical); + dc->SetPen(* wxRED_PEN); + 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,12 +486,14 @@ 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); } dc->SetFont(headerFont); +*/ int pageWidthMM, pageHeightMM; @@ -541,10 +510,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; }