printing.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: samples/printing.cpp
  3. // Purpose: Printing demo for wxWidgets
  4. // Author: Julian Smart
  5. // Modified by: Francesco Montorsi
  6. // Created: 1995
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // For compilers that support precompilation, includes "wx/wx.h".
  11. #include "wx/wxprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #ifndef WX_PRECOMP
  16. #include "wx/wx.h"
  17. #include "wx/log.h"
  18. #endif
  19. #if !wxUSE_PRINTING_ARCHITECTURE
  20. #error "You must set wxUSE_PRINTING_ARCHITECTURE to 1 in setup.h, and recompile the library."
  21. #endif
  22. #include <ctype.h>
  23. #include "wx/metafile.h"
  24. #include "wx/print.h"
  25. #include "wx/printdlg.h"
  26. #include "wx/image.h"
  27. #include "wx/accel.h"
  28. #if wxUSE_POSTSCRIPT
  29. #include "wx/generic/printps.h"
  30. #include "wx/generic/prntdlgg.h"
  31. #endif
  32. #if wxUSE_GRAPHICS_CONTEXT
  33. #include "wx/graphics.h"
  34. #endif
  35. #ifdef __WXMAC__
  36. #include "wx/osx/printdlg.h"
  37. #endif
  38. #include "printing.h"
  39. #ifndef wxHAS_IMAGES_IN_RESOURCES
  40. #include "../sample.xpm"
  41. #endif
  42. // Global print data, to remember settings during the session
  43. wxPrintData *g_printData = NULL;
  44. // Global page setup data
  45. wxPageSetupDialogData* g_pageSetupData = NULL;
  46. // ----------------------------------------------------------------------------
  47. // MyApp
  48. // ----------------------------------------------------------------------------
  49. IMPLEMENT_APP(MyApp)
  50. bool MyApp::OnInit(void)
  51. {
  52. if ( !wxApp::OnInit() )
  53. return false;
  54. wxInitAllImageHandlers();
  55. // init global objects
  56. // -------------------
  57. g_printData = new wxPrintData;
  58. // You could set an initial paper size here
  59. #if 0
  60. g_printData->SetPaperId(wxPAPER_LETTER); // for Americans
  61. g_printData->SetPaperId(wxPAPER_A4); // for everyone else
  62. #endif
  63. g_pageSetupData = new wxPageSetupDialogData;
  64. // copy over initial paper size from print record
  65. (*g_pageSetupData) = *g_printData;
  66. // Set some initial page margins in mm.
  67. g_pageSetupData->SetMarginTopLeft(wxPoint(15, 15));
  68. g_pageSetupData->SetMarginBottomRight(wxPoint(15, 15));
  69. // init local GUI objects
  70. // ----------------------
  71. #if 0
  72. wxImage image( wxT("test.jpg") );
  73. image.SetAlpha();
  74. int i,j;
  75. for (i = 0; i < image.GetWidth(); i++)
  76. for (j = 0; j < image.GetHeight(); j++)
  77. image.SetAlpha( i, j, 50 );
  78. m_bitmap = image;
  79. #endif
  80. m_angle = 30;
  81. m_testFont.Create(10, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
  82. // Create the main frame window
  83. // ----------------------------
  84. MyFrame* frame = new MyFrame((wxFrame *) NULL, wxT("wxWidgets Printing Demo"),
  85. wxPoint(0, 0), wxSize(400, 400));
  86. frame->Centre(wxBOTH);
  87. frame->Show();
  88. return true;
  89. }
  90. int MyApp::OnExit()
  91. {
  92. delete g_printData;
  93. delete g_pageSetupData;
  94. return wxApp::OnExit();
  95. }
  96. void MyApp::Draw(wxDC&dc)
  97. {
  98. // This routine just draws a bunch of random stuff on the screen so that we
  99. // can check that different types of object are being drawn consistently
  100. // between the screen image, the print preview image (at various zoom
  101. // levels), and the printed page.
  102. dc.SetBackground(*wxWHITE_BRUSH);
  103. // dc.Clear();
  104. dc.SetFont(m_testFont);
  105. // dc.SetBackgroundMode(wxTRANSPARENT);
  106. dc.SetPen(*wxBLACK_PEN);
  107. dc.SetBrush(*wxLIGHT_GREY_BRUSH);
  108. dc.DrawRectangle(0, 0, 230, 350);
  109. dc.DrawLine(0, 0, 229, 349);
  110. dc.DrawLine(229, 0, 0, 349);
  111. dc.SetBrush(*wxTRANSPARENT_BRUSH);
  112. dc.SetBrush(*wxCYAN_BRUSH);
  113. dc.SetPen(*wxRED_PEN);
  114. dc.DrawRoundedRectangle(0, 20, 200, 80, 20);
  115. dc.DrawText( wxT("Rectangle 200 by 80"), 40, 40);
  116. dc.SetPen( wxPen(*wxBLACK, 0, wxPENSTYLE_DOT_DASH) );
  117. dc.DrawEllipse(50, 140, 100, 50);
  118. dc.SetPen(*wxRED_PEN);
  119. dc.DrawText( wxT("Test message: this is in 10 point text"), 10, 180);
  120. #if wxUSE_UNICODE
  121. const char *test = "Hebrew שלום -- Japanese (日本語)";
  122. wxString tmp = wxConvUTF8.cMB2WC( test );
  123. dc.DrawText( tmp, 10, 200 );
  124. #endif
  125. wxPoint points[5];
  126. points[0].x = 0;
  127. points[0].y = 0;
  128. points[1].x = 20;
  129. points[1].y = 0;
  130. points[2].x = 20;
  131. points[2].y = 20;
  132. points[3].x = 10;
  133. points[3].y = 20;
  134. points[4].x = 10;
  135. points[4].y = -20;
  136. dc.DrawPolygon( 5, points, 20, 250, wxODDEVEN_RULE );
  137. dc.DrawPolygon( 5, points, 50, 250, wxWINDING_RULE );
  138. dc.DrawArc( 20, 330, 40, 300, 20, 300 );
  139. {
  140. wxDCBrushChanger changeBrush(dc, *wxTRANSPARENT_BRUSH);
  141. dc.DrawArc( 60, 330, 80, 300, 60, 300 );
  142. }
  143. dc.DrawEllipticArc( 80, 250, 60, 30, 0.0, 270.0 );
  144. points[0].x = 150;
  145. points[0].y = 250;
  146. points[1].x = 180;
  147. points[1].y = 250;
  148. points[2].x = 180;
  149. points[2].y = 220;
  150. points[3].x = 200;
  151. points[3].y = 220;
  152. dc.DrawSpline( 4, points );
  153. wxString str;
  154. int i = 0;
  155. str.Printf( wxT("---- Text at angle %d ----"), i );
  156. dc.DrawRotatedText( str, 100, 300, i );
  157. i = m_angle;
  158. str.Printf( wxT("---- Text at angle %d ----"), i );
  159. dc.DrawRotatedText( str, 100, 300, i );
  160. wxIcon my_icon = wxICON(sample);
  161. dc.DrawIcon( my_icon, 100, 100);
  162. if (m_bitmap.IsOk())
  163. dc.DrawBitmap( m_bitmap, 10, 10 );
  164. #if wxUSE_GRAPHICS_CONTEXT
  165. wxGraphicsContext *gc = NULL;
  166. wxPrinterDC *printer_dc = wxDynamicCast( &dc, wxPrinterDC );
  167. if (printer_dc)
  168. gc = wxGraphicsContext::Create( *printer_dc );
  169. wxWindowDC *window_dc = wxDynamicCast( &dc, wxWindowDC );
  170. if (window_dc)
  171. gc = wxGraphicsContext::Create( *window_dc );
  172. #ifdef __WXMSW__
  173. wxEnhMetaFileDC *emf_dc = wxDynamicCast( &dc, wxEnhMetaFileDC );
  174. if (emf_dc)
  175. gc = wxGraphicsContext::Create( *emf_dc );
  176. #endif
  177. if (gc)
  178. {
  179. // make a path that contains a circle and some lines, centered at 100,100
  180. gc->SetPen( *wxRED_PEN );
  181. wxGraphicsPath path = gc->CreatePath();
  182. path.AddCircle( 50.0, 50.0, 50.0 );
  183. path.MoveToPoint(0.0, 50.0);
  184. path.AddLineToPoint(100.0, 50.0);
  185. path.MoveToPoint(50.0, 0.0);
  186. path.AddLineToPoint(50.0, 100.0 );
  187. path.CloseSubpath();
  188. path.AddRectangle(25.0, 25.0, 50.0, 50.0);
  189. gc->StrokePath(path);
  190. // draw some text
  191. wxString text("Text by wxGraphicsContext");
  192. gc->SetFont( m_testFont, *wxBLACK );
  193. gc->DrawText(text, 25.0, 60.0);
  194. // draw rectangle around the text
  195. double w, h, d, el;
  196. gc->GetTextExtent(text, &w, &h, &d, &el);
  197. gc->SetPen( *wxBLACK_PEN );
  198. gc->DrawRectangle(25.0, 60.0, w, h);
  199. delete gc;
  200. }
  201. #endif
  202. }
  203. // ----------------------------------------------------------------------------
  204. // MyFrame
  205. // ----------------------------------------------------------------------------
  206. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  207. EVT_MENU(wxID_EXIT, MyFrame::OnExit)
  208. EVT_MENU(wxID_PRINT, MyFrame::OnPrint)
  209. EVT_MENU(wxID_PREVIEW, MyFrame::OnPrintPreview)
  210. EVT_MENU(WXPRINT_PAGE_SETUP, MyFrame::OnPageSetup)
  211. EVT_MENU(wxID_ABOUT, MyFrame::OnPrintAbout)
  212. #if wxUSE_POSTSCRIPT
  213. EVT_MENU(WXPRINT_PRINT_PS, MyFrame::OnPrintPS)
  214. EVT_MENU(WXPRINT_PREVIEW_PS, MyFrame::OnPrintPreviewPS)
  215. EVT_MENU(WXPRINT_PAGE_SETUP_PS, MyFrame::OnPageSetupPS)
  216. #endif
  217. #ifdef __WXMAC__
  218. EVT_MENU(WXPRINT_PAGE_MARGINS, MyFrame::OnPageMargins)
  219. #endif
  220. EVT_MENU(WXPRINT_ANGLEUP, MyFrame::OnAngleUp)
  221. EVT_MENU(WXPRINT_ANGLEDOWN, MyFrame::OnAngleDown)
  222. EVT_MENU_RANGE(WXPRINT_FRAME_MODAL_APP,
  223. WXPRINT_FRAME_MODAL_NON,
  224. MyFrame::OnPreviewFrameModalityKind)
  225. wxEND_EVENT_TABLE()
  226. MyFrame::MyFrame(wxFrame *frame, const wxString&title, const wxPoint&pos, const wxSize&size)
  227. : wxFrame(frame, wxID_ANY, title, pos, size)
  228. {
  229. m_canvas = NULL;
  230. m_previewModality = wxPreviewFrame_AppModal;
  231. #if wxUSE_STATUSBAR
  232. // Give us a status line
  233. CreateStatusBar(2);
  234. SetStatusText(wxT("Printing demo"));
  235. #endif // wxUSE_STATUSBAR
  236. // Load icon and bitmap
  237. SetIcon( wxICON( sample) );
  238. // Make a menubar
  239. wxMenu *file_menu = new wxMenu;
  240. file_menu->Append(wxID_PRINT, wxT("&Print..."), wxT("Print"));
  241. file_menu->Append(WXPRINT_PAGE_SETUP, wxT("Page Set&up..."), wxT("Page setup"));
  242. #ifdef __WXMAC__
  243. file_menu->Append(WXPRINT_PAGE_MARGINS, wxT("Page Margins..."), wxT("Page margins"));
  244. #endif
  245. file_menu->Append(wxID_PREVIEW, wxT("Print Pre&view"), wxT("Preview"));
  246. wxMenu * const menuModalKind = new wxMenu;
  247. menuModalKind->AppendRadioItem(WXPRINT_FRAME_MODAL_APP, "&App modal");
  248. menuModalKind->AppendRadioItem(WXPRINT_FRAME_MODAL_WIN, "&Window modal");
  249. menuModalKind->AppendRadioItem(WXPRINT_FRAME_MODAL_NON, "&Not modal");
  250. file_menu->AppendSubMenu(menuModalKind, "Preview frame &modal kind");
  251. #if wxUSE_ACCEL
  252. // Accelerators
  253. wxAcceleratorEntry entries[1];
  254. entries[0].Set(wxACCEL_CTRL, (int) 'V', wxID_PREVIEW);
  255. wxAcceleratorTable accel(1, entries);
  256. SetAcceleratorTable(accel);
  257. #endif
  258. #if wxUSE_POSTSCRIPT
  259. file_menu->AppendSeparator();
  260. file_menu->Append(WXPRINT_PRINT_PS, wxT("Print PostScript..."), wxT("Print (PostScript)"));
  261. file_menu->Append(WXPRINT_PAGE_SETUP_PS, wxT("Page Setup PostScript..."), wxT("Page setup (PostScript)"));
  262. file_menu->Append(WXPRINT_PREVIEW_PS, wxT("Print Preview PostScript"), wxT("Preview (PostScript)"));
  263. #endif
  264. file_menu->AppendSeparator();
  265. file_menu->Append(WXPRINT_ANGLEUP, wxT("Angle up\tAlt-U"), wxT("Raise rotated text angle"));
  266. file_menu->Append(WXPRINT_ANGLEDOWN, wxT("Angle down\tAlt-D"), wxT("Lower rotated text angle"));
  267. file_menu->AppendSeparator();
  268. file_menu->Append(wxID_EXIT, wxT("E&xit"), wxT("Exit program"));
  269. wxMenu *help_menu = new wxMenu;
  270. help_menu->Append(wxID_ABOUT, wxT("&About"), wxT("About this demo"));
  271. wxMenuBar *menu_bar = new wxMenuBar;
  272. menu_bar->Append(file_menu, wxT("&File"));
  273. menu_bar->Append(help_menu, wxT("&Help"));
  274. // Associate the menu bar with the frame
  275. SetMenuBar(menu_bar);
  276. // create the canvas
  277. // -----------------
  278. m_canvas = new MyCanvas(this, wxPoint(0, 0), wxSize(100, 100),
  279. wxRETAINED|wxHSCROLL|wxVSCROLL);
  280. // Give it scrollbars: the virtual canvas is 20 * 50 = 1000 pixels in each direction
  281. m_canvas->SetScrollbars(20, 20, 50, 50);
  282. }
  283. void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
  284. {
  285. Close(true /*force closing*/);
  286. }
  287. void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
  288. {
  289. wxPrintDialogData printDialogData(* g_printData);
  290. wxPrinter printer(&printDialogData);
  291. MyPrintout printout(this, wxT("My printout"));
  292. if (!printer.Print(this, &printout, true /*prompt*/))
  293. {
  294. if (wxPrinter::GetLastError() == wxPRINTER_ERROR)
  295. {
  296. wxLogError(wxT("There was a problem printing. Perhaps your current printer is not set correctly?"));
  297. }
  298. else
  299. {
  300. wxLogMessage(wxT("You canceled printing"));
  301. }
  302. }
  303. else
  304. {
  305. (*g_printData) = printer.GetPrintDialogData().GetPrintData();
  306. }
  307. }
  308. void MyFrame::OnPrintPreview(wxCommandEvent& WXUNUSED(event))
  309. {
  310. // Pass two printout objects: for preview, and possible printing.
  311. wxPrintDialogData printDialogData(* g_printData);
  312. wxPrintPreview *preview =
  313. new wxPrintPreview(new MyPrintout(this), new MyPrintout(this), &printDialogData);
  314. if (!preview->IsOk())
  315. {
  316. delete preview;
  317. wxLogError(wxT("There was a problem previewing.\nPerhaps your current printer is not set correctly?"));
  318. return;
  319. }
  320. wxPreviewFrame *frame =
  321. new wxPreviewFrame(preview, this, wxT("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
  322. frame->Centre(wxBOTH);
  323. frame->InitializeWithModality(m_previewModality);
  324. frame->Show();
  325. }
  326. void MyFrame::OnPageSetup(wxCommandEvent& WXUNUSED(event))
  327. {
  328. (*g_pageSetupData) = *g_printData;
  329. wxPageSetupDialog pageSetupDialog(this, g_pageSetupData);
  330. pageSetupDialog.ShowModal();
  331. (*g_printData) = pageSetupDialog.GetPageSetupDialogData().GetPrintData();
  332. (*g_pageSetupData) = pageSetupDialog.GetPageSetupDialogData();
  333. }
  334. #if wxUSE_POSTSCRIPT
  335. void MyFrame::OnPrintPS(wxCommandEvent& WXUNUSED(event))
  336. {
  337. wxPrintDialogData printDialogData(* g_printData);
  338. wxPostScriptPrinter printer(&printDialogData);
  339. MyPrintout printout(this, wxT("My printout"));
  340. printer.Print(this, &printout, true/*prompt*/);
  341. (*g_printData) = printer.GetPrintDialogData().GetPrintData();
  342. }
  343. void MyFrame::OnPrintPreviewPS(wxCommandEvent& WXUNUSED(event))
  344. {
  345. // Pass two printout objects: for preview, and possible printing.
  346. wxPrintDialogData printDialogData(* g_printData);
  347. wxPrintPreview *preview = new wxPrintPreview(new MyPrintout(this), new MyPrintout(this), &printDialogData);
  348. wxPreviewFrame *frame =
  349. new wxPreviewFrame(preview, this, wxT("Demo Print Preview"), wxPoint(100, 100), wxSize(600, 650));
  350. frame->Centre(wxBOTH);
  351. frame->Initialize();
  352. frame->Show();
  353. }
  354. void MyFrame::OnPageSetupPS(wxCommandEvent& WXUNUSED(event))
  355. {
  356. (*g_pageSetupData) = * g_printData;
  357. wxGenericPageSetupDialog pageSetupDialog(this, g_pageSetupData);
  358. pageSetupDialog.ShowModal();
  359. (*g_printData) = pageSetupDialog.GetPageSetupDialogData().GetPrintData();
  360. (*g_pageSetupData) = pageSetupDialog.GetPageSetupDialogData();
  361. }
  362. #endif
  363. #ifdef __WXMAC__
  364. void MyFrame::OnPageMargins(wxCommandEvent& WXUNUSED(event))
  365. {
  366. (*g_pageSetupData) = *g_printData;
  367. wxMacPageMarginsDialog pageMarginsDialog(this, g_pageSetupData);
  368. pageMarginsDialog.ShowModal();
  369. (*g_printData) = pageMarginsDialog.GetPageSetupDialogData().GetPrintData();
  370. (*g_pageSetupData) = pageMarginsDialog.GetPageSetupDialogData();
  371. }
  372. #endif
  373. void MyFrame::OnPrintAbout(wxCommandEvent& WXUNUSED(event))
  374. {
  375. (void)wxMessageBox(wxT("wxWidgets printing demo\nAuthor: Julian Smart"),
  376. wxT("About wxWidgets printing demo"), wxOK|wxCENTRE);
  377. }
  378. void MyFrame::OnAngleUp(wxCommandEvent& WXUNUSED(event))
  379. {
  380. wxGetApp().IncrementAngle();
  381. m_canvas->Refresh();
  382. }
  383. void MyFrame::OnAngleDown(wxCommandEvent& WXUNUSED(event))
  384. {
  385. wxGetApp().DecrementAngle();
  386. m_canvas->Refresh();
  387. }
  388. void MyFrame::OnPreviewFrameModalityKind(wxCommandEvent& event)
  389. {
  390. m_previewModality = static_cast<wxPreviewFrameModalityKind>(
  391. wxPreviewFrame_AppModal +
  392. (event.GetId() - WXPRINT_FRAME_MODAL_APP));
  393. }
  394. // ----------------------------------------------------------------------------
  395. // MyCanvas
  396. // ----------------------------------------------------------------------------
  397. wxBEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
  398. // EVT_PAINT(MyCanvas::OnPaint)
  399. wxEND_EVENT_TABLE()
  400. MyCanvas::MyCanvas(wxFrame *frame, const wxPoint&pos, const wxSize&size, long style)
  401. : wxScrolledWindow(frame, wxID_ANY, pos, size, style)
  402. {
  403. SetBackgroundColour(*wxWHITE);
  404. }
  405. //void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(evt))
  406. void MyCanvas::OnDraw(wxDC& dc)
  407. {
  408. //wxPaintDC dc(this);
  409. wxGetApp().Draw(dc);
  410. }
  411. // ----------------------------------------------------------------------------
  412. // MyPrintout
  413. // ----------------------------------------------------------------------------
  414. bool MyPrintout::OnPrintPage(int page)
  415. {
  416. wxDC *dc = GetDC();
  417. if (dc)
  418. {
  419. if (page == 1)
  420. DrawPageOne();
  421. else if (page == 2)
  422. DrawPageTwo();
  423. // Draw page numbers at top left corner of printable area, sized so that
  424. // screen size of text matches paper size.
  425. MapScreenSizeToPage();
  426. dc->DrawText(wxString::Format(wxT("PAGE %d"), page), 0, 0);
  427. return true;
  428. }
  429. else
  430. return false;
  431. }
  432. bool MyPrintout::OnBeginDocument(int startPage, int endPage)
  433. {
  434. if (!wxPrintout::OnBeginDocument(startPage, endPage))
  435. return false;
  436. return true;
  437. }
  438. void MyPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
  439. {
  440. *minPage = 1;
  441. *maxPage = 2;
  442. *selPageFrom = 1;
  443. *selPageTo = 2;
  444. }
  445. bool MyPrintout::HasPage(int pageNum)
  446. {
  447. return (pageNum == 1 || pageNum == 2);
  448. }
  449. void MyPrintout::DrawPageOne()
  450. {
  451. // You might use THIS code if you were scaling graphics of known size to fit
  452. // on the page. The commented-out code illustrates different ways of scaling
  453. // the graphics.
  454. // We know the graphic is 230x350. If we didn't know this, we'd need to
  455. // calculate it.
  456. wxCoord maxX = 230;
  457. wxCoord maxY = 350;
  458. // This sets the user scale and origin of the DC so that the image fits
  459. // within the paper rectangle (but the edges could be cut off by printers
  460. // that can't print to the edges of the paper -- which is most of them. Use
  461. // this if your image already has its own margins.
  462. // FitThisSizeToPaper(wxSize(maxX, maxY));
  463. // wxRect fitRect = GetLogicalPaperRect();
  464. // This sets the user scale and origin of the DC so that the image fits
  465. // within the page rectangle, which is the printable area on Mac and MSW
  466. // and is the entire page on other platforms.
  467. // FitThisSizeToPage(wxSize(maxX, maxY));
  468. // wxRect fitRect = GetLogicalPageRect();
  469. // This sets the user scale and origin of the DC so that the image fits
  470. // within the page margins as specified by g_PageSetupData, which you can
  471. // change (on some platforms, at least) in the Page Setup dialog. Note that
  472. // on Mac, the native Page Setup dialog doesn't let you change the margins
  473. // of a wxPageSetupDialogData object, so you'll have to write your own dialog or
  474. // use the Mac-only wxMacPageMarginsDialog, as we do in this program.
  475. FitThisSizeToPageMargins(wxSize(maxX, maxY), *g_pageSetupData);
  476. wxRect fitRect = GetLogicalPageMarginsRect(*g_pageSetupData);
  477. // This sets the user scale and origin of the DC so that the image appears
  478. // on the paper at the same size that it appears on screen (i.e., 10-point
  479. // type on screen is 10-point on the printed page) and is positioned in the
  480. // top left corner of the page rectangle (just as the screen image appears
  481. // in the top left corner of the window).
  482. // MapScreenSizeToPage();
  483. // wxRect fitRect = GetLogicalPageRect();
  484. // You could also map the screen image to the entire paper at the same size
  485. // as it appears on screen.
  486. // MapScreenSizeToPaper();
  487. // wxRect fitRect = GetLogicalPaperRect();
  488. // You might also wish to do you own scaling in order to draw objects at
  489. // full native device resolution. In this case, you should do the following.
  490. // Note that you can use the GetLogicalXXXRect() commands to obtain the
  491. // appropriate rect to scale to.
  492. // MapScreenSizeToDevice();
  493. // wxRect fitRect = GetLogicalPageRect();
  494. // Each of the preceding Fit or Map routines positions the origin so that
  495. // the drawn image is positioned at the top left corner of the reference
  496. // rectangle. You can easily center or right- or bottom-justify the image as
  497. // follows.
  498. // This offsets the image so that it is centered within the reference
  499. // rectangle defined above.
  500. wxCoord xoff = (fitRect.width - maxX) / 2;
  501. wxCoord yoff = (fitRect.height - maxY) / 2;
  502. OffsetLogicalOrigin(xoff, yoff);
  503. // This offsets the image so that it is positioned at the bottom right of
  504. // the reference rectangle defined above.
  505. // wxCoord xoff = (fitRect.width - maxX);
  506. // wxCoord yoff = (fitRect.height - maxY);
  507. // OffsetLogicalOrigin(xoff, yoff);
  508. wxGetApp().Draw(*GetDC());
  509. }
  510. void MyPrintout::DrawPageTwo()
  511. {
  512. // You might use THIS code to set the printer DC to ROUGHLY reflect
  513. // the screen text size. This page also draws lines of actual length
  514. // 5cm on the page.
  515. // Compare this to DrawPageOne(), which uses the really convenient routines
  516. // from wxPrintout to fit the screen image onto the printed page. This page
  517. // illustrates how to do all the scaling calculations yourself, if you're so
  518. // inclined.
  519. wxDC *dc = GetDC();
  520. // Get the logical pixels per inch of screen and printer
  521. int ppiScreenX, ppiScreenY;
  522. GetPPIScreen(&ppiScreenX, &ppiScreenY);
  523. int ppiPrinterX, ppiPrinterY;
  524. GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
  525. // This scales the DC so that the printout roughly represents the screen
  526. // scaling. The text point size _should_ be the right size but in fact is
  527. // too small for some reason. This is a detail that will need to be
  528. // addressed at some point but can be fudged for the moment.
  529. float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
  530. // Now we have to check in case our real page size is reduced (e.g. because
  531. // we're drawing to a print preview memory DC)
  532. int pageWidth, pageHeight;
  533. int w, h;
  534. dc->GetSize(&w, &h);
  535. GetPageSizePixels(&pageWidth, &pageHeight);
  536. // If printer pageWidth == current DC width, then this doesn't change. But w
  537. // might be the preview bitmap width, so scale down.
  538. float overallScale = scale * (float)(w/(float)pageWidth);
  539. dc->SetUserScale(overallScale, overallScale);
  540. // Calculate conversion factor for converting millimetres into logical
  541. // units. There are approx. 25.4 mm to the inch. There are ppi device units
  542. // to the inch. Therefore 1 mm corresponds to ppi/25.4 device units. We also
  543. // divide by the screen-to-printer scaling factor, because we need to
  544. // unscale to pass logical units to DrawLine.
  545. // Draw 50 mm by 50 mm L shape
  546. float logUnitsFactor = (float)(ppiPrinterX/(scale*25.4));
  547. float logUnits = (float)(50*logUnitsFactor);
  548. dc->SetPen(* wxBLACK_PEN);
  549. dc->DrawLine(50, 250, (long)(50.0 + logUnits), 250);
  550. dc->DrawLine(50, 250, 50, (long)(250.0 + logUnits));
  551. dc->SetBackgroundMode(wxTRANSPARENT);
  552. dc->SetBrush(*wxTRANSPARENT_BRUSH);
  553. { // GetTextExtent demo:
  554. wxString words[7] = { wxT("This "), wxT("is "), wxT("GetTextExtent "),
  555. wxT("testing "), wxT("string. "), wxT("Enjoy "), wxT("it!") };
  556. wxCoord w, h;
  557. long x = 200, y= 250;
  558. wxFont fnt(15, wxSWISS, wxNORMAL, wxNORMAL);
  559. dc->SetFont(fnt);
  560. for (int i = 0; i < 7; i++)
  561. {
  562. wxString word = words[i];
  563. word.Remove( word.Len()-1, 1 );
  564. dc->GetTextExtent(word, &w, &h);
  565. dc->DrawRectangle(x, y, w, h);
  566. dc->GetTextExtent(words[i], &w, &h);
  567. dc->DrawText(words[i], x, y);
  568. x += w;
  569. }
  570. }
  571. dc->SetFont(wxGetApp().GetTestFont());
  572. dc->DrawText(wxT("Some test text"), 200, 300 );
  573. // TESTING
  574. int leftMargin = 20;
  575. int rightMargin = 20;
  576. int topMargin = 20;
  577. int bottomMargin = 20;
  578. int pageWidthMM, pageHeightMM;
  579. GetPageSizeMM(&pageWidthMM, &pageHeightMM);
  580. float leftMarginLogical = (float)(logUnitsFactor*leftMargin);
  581. float topMarginLogical = (float)(logUnitsFactor*topMargin);
  582. float bottomMarginLogical = (float)(logUnitsFactor*(pageHeightMM - bottomMargin));
  583. float rightMarginLogical = (float)(logUnitsFactor*(pageWidthMM - rightMargin));
  584. dc->SetPen(* wxRED_PEN);
  585. dc->DrawLine( (long)leftMarginLogical, (long)topMarginLogical,
  586. (long)rightMarginLogical, (long)topMarginLogical);
  587. dc->DrawLine( (long)leftMarginLogical, (long)bottomMarginLogical,
  588. (long)rightMarginLogical, (long)bottomMarginLogical);
  589. WritePageHeader(this, dc, wxT("A header"), logUnitsFactor);
  590. }
  591. // Writes a header on a page. Margin units are in millimetres.
  592. bool MyPrintout::WritePageHeader(wxPrintout *printout, wxDC *dc, const wxString&text, float mmToLogical)
  593. {
  594. #if 0
  595. static wxFont *headerFont = (wxFont *) NULL;
  596. if (!headerFont)
  597. {
  598. headerFont = wxTheFontList->FindOrCreateFont(16, wxSWISS, wxNORMAL, wxBOLD);
  599. }
  600. dc->SetFont(headerFont);
  601. #endif
  602. int pageWidthMM, pageHeightMM;
  603. printout->GetPageSizeMM(&pageWidthMM, &pageHeightMM);
  604. wxUnusedVar(pageHeightMM);
  605. int leftMargin = 10;
  606. int topMargin = 10;
  607. int rightMargin = 10;
  608. float leftMarginLogical = (float)(mmToLogical*leftMargin);
  609. float topMarginLogical = (float)(mmToLogical*topMargin);
  610. float rightMarginLogical = (float)(mmToLogical*(pageWidthMM - rightMargin));
  611. wxCoord xExtent, yExtent;
  612. dc->GetTextExtent(text, &xExtent, &yExtent);
  613. float xPos = (float)(((((pageWidthMM - leftMargin - rightMargin)/2.0)+leftMargin)*mmToLogical) - (xExtent/2.0));
  614. dc->DrawText(text, (long)xPos, (long)topMarginLogical);
  615. dc->SetPen(* wxBLACK_PEN);
  616. dc->DrawLine( (long)leftMarginLogical, (long)(topMarginLogical+yExtent),
  617. (long)rightMarginLogical, (long)topMarginLogical+yExtent );
  618. return true;
  619. }