]> git.saurik.com Git - wxWidgets.git/blame - src/richtext/richtextprint.cpp
Call Dismiss rather than Show(false) on the infobar to correct the layout.
[wxWidgets.git] / src / richtext / richtextprint.cpp
CommitLineData
44219ff0
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/richtext/richtextprint.cpp
3// Purpose: Rich text printing classes
4// Author: Julian Smart
5// Created: 2006-10-24
6// RCS-ID: $Id$
7// Copyright: (c) Julian Smart
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11// For compilers that support precompilation, includes "wx/wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
18#if wxUSE_RICHTEXT && wxUSE_PRINTING_ARCHITECTURE && wxUSE_STREAMS
19
20#ifndef WX_PRECOMP
21 #include "wx/log.h"
22 #include "wx/intl.h"
23 #include "wx/dc.h"
24 #include "wx/settings.h"
25 #include "wx/msgdlg.h"
26#endif
27
28#include "wx/datetime.h"
29#include "wx/print.h"
30#include "wx/printdlg.h"
31#include "wx/richtext/richtextprint.h"
32#include "wx/wfstream.h"
33
34/*!
35 * wxRichTextPrintout
36 */
37
38wxRichTextPrintout::wxRichTextPrintout(const wxString& title) : wxPrintout(title)
39{
40 m_numPages = wxRICHTEXT_PRINT_MAX_PAGES;
41
42 SetMargins(); // to default values
43}
44
45wxRichTextPrintout::~wxRichTextPrintout()
46{
47}
48
49void wxRichTextPrintout::OnPreparePrinting()
50{
51 wxBusyCursor wait;
52
f1b18eba 53 m_numPages = 1;
44219ff0
JS
54
55 m_pageBreaksStart.Clear();
56 m_pageBreaksEnd.Clear();
603f702b 57 m_pageYOffsets.Clear();
03647350 58
44219ff0 59 int lastStartPos = 0;
03647350 60
44219ff0
JS
61 wxRect rect, headerRect, footerRect;
62
63 /// Sets the DC scaling and returns important page rectangles
64 CalculateScaling(GetDC(), rect, headerRect, footerRect);
65
66 if (GetRichTextBuffer())
67 {
68 GetRichTextBuffer()->Invalidate(wxRICHTEXT_ALL);
69
70 GetRichTextBuffer()->Layout(*GetDC(), rect, wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT);
03647350 71
44219ff0 72 // Now calculate the page breaks
03647350 73
44219ff0 74 int yOffset = 0;
03647350 75
44219ff0 76 wxRichTextLine* lastLine = NULL;
03647350 77
44219ff0
JS
78 wxRichTextObjectList::compatibility_iterator node = GetRichTextBuffer()->GetChildren().GetFirst();
79 while (node)
80 {
81 // child is a paragraph
82 wxRichTextParagraph* child = wxDynamicCast(node->GetData(), wxRichTextParagraph);
83 wxASSERT (child != NULL);
603f702b 84 if (child)
44219ff0 85 {
603f702b
JS
86 wxRichTextLineList::compatibility_iterator node2 = child->GetLines().GetFirst();
87 while (node2)
88 {
89 wxRichTextLine* line = node2->GetData();
ea160b2e 90
603f702b
JS
91 int lineY = child->GetPosition().y + line->GetPosition().y - yOffset;
92 bool hasHardPageBreak = ((node2 == child->GetLines().GetFirst()) && child->GetAttributes().HasPageBreak());
44219ff0 93
603f702b
JS
94 // Break the page if either we're going off the bottom, or this paragraph specifies
95 // an explicit page break
03647350 96
603f702b
JS
97 if (((lineY + line->GetSize().y) > rect.GetBottom()) || hasHardPageBreak)
98 {
99 // New page starting at this line
100 int newY = rect.y;
03647350 101
603f702b 102 // We increase the offset by the difference between new and old positions
03647350 103
603f702b
JS
104 int increaseOffsetBy = lineY - newY;
105 yOffset += increaseOffsetBy;
03647350 106
603f702b
JS
107 if (!lastLine)
108 lastLine = line;
03647350 109
603f702b
JS
110 m_pageBreaksStart.Add(lastStartPos);
111 m_pageBreaksEnd.Add(lastLine->GetAbsoluteRange().GetEnd());
112 m_pageYOffsets.Add(yOffset);
44219ff0 113
603f702b 114 lastStartPos = line->GetAbsoluteRange().GetStart();
44219ff0 115 lastLine = line;
03647350 116
603f702b
JS
117 m_numPages ++;
118
119 // Now create page breaks for the rest of the line, if it's larger than the page height
120 int contentLeft = line->GetSize().y - rect.GetHeight();
121 while (contentLeft >= 0)
122 {
123 yOffset += rect.GetHeight();
124 contentLeft -= rect.GetHeight();
125
126 m_pageBreaksStart.Add(lastStartPos);
127 m_pageBreaksEnd.Add(lastLine->GetAbsoluteRange().GetEnd());
128 m_pageYOffsets.Add(yOffset);
129 }
130 }
131
132 lastLine = line;
133
134 node2 = node2->GetNext();
44219ff0 135 }
44219ff0
JS
136 }
137
138 node = node->GetNext();
139 }
140
141 // Closing page break
603f702b 142 if (m_pageBreaksStart.GetCount() == 0 || (m_pageBreaksEnd[m_pageBreaksEnd.GetCount()-1] < (GetRichTextBuffer()->GetOwnRange().GetEnd()-1)))
44219ff0
JS
143 {
144 m_pageBreaksStart.Add(lastStartPos);
603f702b
JS
145 m_pageBreaksEnd.Add(GetRichTextBuffer()->GetOwnRange().GetEnd());
146 m_pageYOffsets.Add(yOffset);
44219ff0
JS
147 }
148 }
149}
150
151bool wxRichTextPrintout::OnBeginDocument(int startPage, int endPage)
152{
153 if (!wxPrintout::OnBeginDocument(startPage, endPage)) return false;
154
155 return true;
156}
157
158bool wxRichTextPrintout::OnPrintPage(int page)
159{
160 wxDC *dc = GetDC();
161 if (dc)
162 {
163 if (HasPage(page))
164 RenderPage(dc, page);
165 return true;
166 }
167 else return false;
168}
169
170void wxRichTextPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
171{
172 *minPage = 1;
173 *maxPage = m_numPages;
174 *selPageFrom = 1;
175 *selPageTo = m_numPages;
176}
177
178bool wxRichTextPrintout::HasPage(int pageNum)
179{
180 return pageNum > 0 && pageNum <= m_numPages;
181}
182
183void wxRichTextPrintout::RenderPage(wxDC *dc, int page)
184{
185 if (!GetRichTextBuffer())
186 return;
187
188 wxBusyCursor wait;
03647350 189
44219ff0
JS
190 wxRect textRect, headerRect, footerRect;
191
192 /// Sets the DC scaling and returns important page rectangles
193 CalculateScaling(dc, textRect, headerRect, footerRect);
194
195 if (page > 1 || m_headerFooterData.GetShowOnFirstPage())
196 {
a1b806b9 197 if (m_headerFooterData.GetFont().IsOk())
44219ff0
JS
198 dc->SetFont(m_headerFooterData.GetFont());
199 else
200 dc->SetFont(*wxNORMAL_FONT);
a1b806b9 201 if (m_headerFooterData.GetTextColour().IsOk())
44219ff0
JS
202 dc->SetTextForeground(m_headerFooterData.GetTextColour());
203 else
204 dc->SetTextForeground(*wxBLACK);
04ee05f9 205 dc->SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
44219ff0 206
03647350 207 // Draw header, if any
44219ff0
JS
208 wxRichTextOddEvenPage oddEven = ((page % 2) == 1) ? wxRICHTEXT_PAGE_ODD : wxRICHTEXT_PAGE_EVEN;
209
210 wxString headerTextCentre = m_headerFooterData.GetHeaderText(oddEven, wxRICHTEXT_PAGE_CENTRE);
211 wxString headerTextLeft = m_headerFooterData.GetHeaderText(oddEven, wxRICHTEXT_PAGE_LEFT);
212 wxString headerTextRight = m_headerFooterData.GetHeaderText(oddEven, wxRICHTEXT_PAGE_RIGHT);
213
214 if (!headerTextLeft.IsEmpty())
215 {
216 SubstituteKeywords(headerTextLeft, GetTitle(), page, m_numPages);
217
218 //int tx, ty;
219 //dc->GetTextExtent(headerTextLeft, & tx, & ty);
03647350 220
44219ff0
JS
221 int x = headerRect.GetLeft();
222 int y = headerRect.GetX();
223 dc->DrawText(headerTextLeft, x, y);
224 }
225 if (!headerTextCentre.IsEmpty())
226 {
227 SubstituteKeywords(headerTextCentre, GetTitle(), page, m_numPages);
228
229 int tx, ty;
230 dc->GetTextExtent(headerTextCentre, & tx, & ty);
231
232 int x = headerRect.GetWidth()/2 - tx/2 + headerRect.GetLeft();
233 int y = headerRect.GetY();
234 dc->DrawText(headerTextCentre, x, y);
235 }
236 if (!headerTextRight.IsEmpty())
237 {
238 SubstituteKeywords(headerTextRight, GetTitle(), page, m_numPages);
239
240 int tx, ty;
241 dc->GetTextExtent(headerTextRight, & tx, & ty);
242
243 int x = headerRect.GetRight() - tx;
244 int y = headerRect.GetY();
245 dc->DrawText(headerTextRight, x, y);
246 }
247
03647350 248 // Draw footer, if any
44219ff0
JS
249 wxString footerTextCentre = m_headerFooterData.GetFooterText(oddEven, wxRICHTEXT_PAGE_CENTRE);
250 wxString footerTextLeft = m_headerFooterData.GetFooterText(oddEven, wxRICHTEXT_PAGE_LEFT);
251 wxString footerTextRight = m_headerFooterData.GetFooterText(oddEven, wxRICHTEXT_PAGE_RIGHT);
252
253 if (!footerTextLeft.IsEmpty())
254 {
255 SubstituteKeywords(footerTextLeft, GetTitle(), page, m_numPages);
256
257 int tx, ty;
258 dc->GetTextExtent(footerTextLeft, & tx, & ty);
259
260 int x = footerRect.GetLeft();
261 int y = footerRect.GetBottom() - ty;
262 dc->DrawText(footerTextLeft, x, y);
263 }
264 if (!footerTextCentre.IsEmpty())
265 {
266 SubstituteKeywords(footerTextCentre, GetTitle(), page, m_numPages);
267
268 int tx, ty;
269 dc->GetTextExtent(footerTextCentre, & tx, & ty);
270
271 int x = footerRect.GetWidth()/2 - tx/2 + footerRect.GetLeft();
272 int y = footerRect.GetBottom() - ty;
273 dc->DrawText(footerTextCentre, x, y);
274 }
275 if (!footerTextRight.IsEmpty())
276 {
277 SubstituteKeywords(footerTextRight, GetTitle(), page, m_numPages);
278
279 int tx, ty;
280 dc->GetTextExtent(footerTextRight, & tx, & ty);
281
282 int x = footerRect.GetRight() - tx;
283 int y = footerRect.GetBottom() - ty;
284 dc->DrawText(footerTextRight, x, y);
285 }
03647350 286 }
44219ff0
JS
287
288 wxRichTextRange rangeToDraw(m_pageBreaksStart[page-1], m_pageBreaksEnd[page-1]);
603f702b
JS
289
290 wxPoint oldOrigin = dc->GetLogicalOrigin();
291 double scaleX, scaleY;
292 dc->GetUserScale(& scaleX, & scaleY);
293
294 int yOffset = 0;
295 if (page > 1)
296 yOffset = m_pageYOffsets[page-2];
297
298 if (yOffset != oldOrigin.y)
299 dc->SetLogicalOrigin(oldOrigin.x, oldOrigin.y + yOffset);
300
301 dc->SetClippingRegion(wxRect(textRect.x, textRect.y + yOffset, textRect.width, textRect.height));
302
303 GetRichTextBuffer()->Draw(*dc, rangeToDraw, wxRichTextSelection(), textRect, 0 /* descent */, wxRICHTEXT_DRAW_IGNORE_CACHE|wxRICHTEXT_DRAW_PRINT /* flags */);
304
305 dc->DestroyClippingRegion();
44219ff0 306
603f702b
JS
307 if (yOffset != oldOrigin.y)
308 dc->SetLogicalOrigin(oldOrigin.x, oldOrigin.y);
44219ff0
JS
309}
310
311void wxRichTextPrintout::SetMargins(int top, int bottom, int left, int right)
312{
313 m_marginTop = top;
314 m_marginBottom = bottom;
315 m_marginLeft = left;
316 m_marginRight = right;
317}
318
319/// Calculate scaling and rectangles, setting the device context scaling
320void wxRichTextPrintout::CalculateScaling(wxDC* dc, wxRect& textRect, wxRect& headerRect, wxRect& footerRect)
321{
322 // Get the logical pixels per inch of screen and printer
323 int ppiScreenX, ppiScreenY;
324 GetPPIScreen(&ppiScreenX, &ppiScreenY);
325 int ppiPrinterX, ppiPrinterY;
326 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
327
328 // This scales the DC so that the printout roughly represents the
329 // the screen scaling.
330 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
331
332 // Now we have to check in case our real page size is reduced
333 // (e.g. because we're drawing to a print preview memory DC)
334 int pageWidth, pageHeight;
335 int w, h;
336 dc->GetSize(&w, &h);
337 GetPageSizePixels(&pageWidth, &pageHeight);
03647350 338
44219ff0
JS
339 // If printer pageWidth == current DC width, then this doesn't
340 // change. But w might be the preview bitmap width, so scale down.
341 float previewScale = (float)(w/(float)pageWidth);
342 float overallScale = scale * previewScale;
343
344 // The dimensions used for indentation etc. have to be unscaled
345 // during printing to be correct when scaling is applied.
603f702b 346 // if (!IsPreview())
44219ff0 347 m_richTextBuffer->SetScale(scale);
03647350 348
44219ff0
JS
349 // Calculate margins
350 int marginLeft = wxRichTextObject::ConvertTenthsMMToPixels(ppiPrinterX, m_marginLeft);
351 int marginTop = wxRichTextObject::ConvertTenthsMMToPixels(ppiPrinterX, m_marginTop);
352 int marginRight = wxRichTextObject::ConvertTenthsMMToPixels(ppiPrinterX, m_marginRight);
353 int marginBottom = wxRichTextObject::ConvertTenthsMMToPixels(ppiPrinterX, m_marginBottom);
03647350 354
44219ff0
JS
355 // Header and footer margins
356 int headerMargin = wxRichTextObject::ConvertTenthsMMToPixels(ppiPrinterX, m_headerFooterData.GetHeaderMargin());
357 int footerMargin = wxRichTextObject::ConvertTenthsMMToPixels(ppiPrinterX, m_headerFooterData.GetFooterMargin());
03647350 358
44219ff0
JS
359 dc->SetUserScale(overallScale, overallScale);
360
2786b623
JS
361 wxRect rect((int) (marginLeft/scale), (int) (marginTop/scale),
362 (int) ((pageWidth - marginLeft - marginRight)/scale), (int)((pageHeight - marginTop - marginBottom)/scale));
03647350 363
44219ff0
JS
364 headerRect = wxRect(0, 0, 0, 0);
365
366 if (!m_headerFooterData.GetHeaderText(wxRICHTEXT_PAGE_ODD, wxRICHTEXT_PAGE_LEFT).IsEmpty() ||
367 !m_headerFooterData.GetHeaderText(wxRICHTEXT_PAGE_ODD, wxRICHTEXT_PAGE_CENTRE).IsEmpty() ||
368 !m_headerFooterData.GetHeaderText(wxRICHTEXT_PAGE_ODD, wxRICHTEXT_PAGE_RIGHT).IsEmpty() ||
369
370 !m_headerFooterData.GetHeaderText(wxRICHTEXT_PAGE_EVEN, wxRICHTEXT_PAGE_LEFT).IsEmpty() ||
371 !m_headerFooterData.GetHeaderText(wxRICHTEXT_PAGE_EVEN, wxRICHTEXT_PAGE_CENTRE).IsEmpty() ||
372 !m_headerFooterData.GetHeaderText(wxRICHTEXT_PAGE_EVEN, wxRICHTEXT_PAGE_RIGHT).IsEmpty())
373 {
a1b806b9 374 if (m_headerFooterData.GetFont().IsOk())
44219ff0
JS
375 dc->SetFont(m_headerFooterData.GetFont());
376 else
377 dc->SetFont(*wxNORMAL_FONT);
03647350 378
44219ff0 379 int charHeight = dc->GetCharHeight();
03647350 380
2786b623 381 int headerHeight = (int) (charHeight + headerMargin/scale);
44219ff0
JS
382
383 headerRect = wxRect(rect.x, rect.y, rect.width, headerHeight);
384
385 rect.y += headerHeight;
03647350 386 rect.height -= headerHeight;
44219ff0
JS
387 }
388
389 footerRect = wxRect(0, 0, 0, 0);
390
391 if (!m_headerFooterData.GetFooterText(wxRICHTEXT_PAGE_ODD, wxRICHTEXT_PAGE_LEFT).IsEmpty() ||
392 !m_headerFooterData.GetFooterText(wxRICHTEXT_PAGE_ODD, wxRICHTEXT_PAGE_CENTRE).IsEmpty() ||
393 !m_headerFooterData.GetFooterText(wxRICHTEXT_PAGE_ODD, wxRICHTEXT_PAGE_RIGHT).IsEmpty() ||
394
395 !m_headerFooterData.GetFooterText(wxRICHTEXT_PAGE_EVEN, wxRICHTEXT_PAGE_LEFT).IsEmpty() ||
396 !m_headerFooterData.GetFooterText(wxRICHTEXT_PAGE_EVEN, wxRICHTEXT_PAGE_CENTRE).IsEmpty() ||
397 !m_headerFooterData.GetFooterText(wxRICHTEXT_PAGE_EVEN, wxRICHTEXT_PAGE_RIGHT).IsEmpty())
398 {
a1b806b9 399 if (m_headerFooterData.GetFont().IsOk())
44219ff0
JS
400 dc->SetFont(m_headerFooterData.GetFont());
401 else
402 dc->SetFont(*wxNORMAL_FONT);
03647350 403
44219ff0 404 int charHeight = dc->GetCharHeight();
03647350 405
2786b623 406 int footerHeight = (int) (charHeight + footerMargin/scale);
44219ff0
JS
407
408 footerRect = wxRect(rect.x, rect.y + rect.height, rect.width, footerHeight);
409
03647350 410 rect.height -= footerHeight;
44219ff0
JS
411 }
412
413 textRect = rect;
414}
415
416bool wxRichTextPrintout::SubstituteKeywords(wxString& str, const wxString& title, int pageNum, int pageCount)
417{
418 wxString num;
419
420 num.Printf(wxT("%i"), pageNum);
421 str.Replace(wxT("@PAGENUM@"), num);
422
423 num.Printf(wxT("%lu"), (unsigned long) pageCount);
424 str.Replace(wxT("@PAGESCNT@"), num);
425
426 wxDateTime now = wxDateTime::Now();
427
428 str.Replace(wxT("@DATE@"), now.FormatDate());
429 str.Replace(wxT("@TIME@"), now.FormatTime());
430
431 str.Replace(wxT("@TITLE@"), title);
432
433 return true;
434}
435
436/*!
437 * wxRichTextPrinting
438 */
439
440wxRichTextPrinting::wxRichTextPrinting(const wxString& name, wxWindow *parentWindow)
441{
442 m_richTextBufferPrinting = NULL;
443 m_richTextBufferPreview = NULL;
444
445 m_parentWindow = parentWindow;
446 m_title = name;
447 m_printData = NULL;
03647350 448
44219ff0
JS
449 m_previewRect = wxRect(wxPoint(100, 100), wxSize(800, 800));
450
451 m_pageSetupData = new wxPageSetupDialogData;
452 m_pageSetupData->EnableMargins(true);
453 m_pageSetupData->SetMarginTopLeft(wxPoint(25, 25));
454 m_pageSetupData->SetMarginBottomRight(wxPoint(25, 25));
455}
456
457wxRichTextPrinting::~wxRichTextPrinting()
458{
459 delete m_printData;
460 delete m_pageSetupData;
461 delete m_richTextBufferPrinting;
462 delete m_richTextBufferPreview;
463}
464
465wxPrintData *wxRichTextPrinting::GetPrintData()
466{
467 if (m_printData == NULL)
468 m_printData = new wxPrintData();
469 return m_printData;
470}
471
5c0a71a2
JS
472/// Set print and page setup data
473void wxRichTextPrinting::SetPrintData(const wxPrintData& printData)
474{
475 (*GetPrintData()) = printData;
476}
477
478void wxRichTextPrinting::SetPageSetupData(const wxPageSetupData& pageSetupData)
479{
480 (*GetPageSetupData()) = pageSetupData;
481}
482
44219ff0
JS
483/// Set the rich text buffer pointer, deleting the existing object if present
484void wxRichTextPrinting::SetRichTextBufferPrinting(wxRichTextBuffer* buf)
485{
486 if (m_richTextBufferPrinting)
487 {
488 delete m_richTextBufferPrinting;
489 m_richTextBufferPrinting = NULL;
490 }
491 m_richTextBufferPrinting = buf;
492}
493
494void wxRichTextPrinting::SetRichTextBufferPreview(wxRichTextBuffer* buf)
495{
496 if (m_richTextBufferPreview)
497 {
498 delete m_richTextBufferPreview;
499 m_richTextBufferPreview = NULL;
500 }
501 m_richTextBufferPreview = buf;
502}
503
504bool wxRichTextPrinting::PreviewFile(const wxString& richTextFile)
505{
506 SetRichTextBufferPreview(new wxRichTextBuffer);
507
508 if (!m_richTextBufferPreview->LoadFile(richTextFile))
509 {
510 SetRichTextBufferPreview(NULL);
511 return false;
512 }
513 else
514 SetRichTextBufferPrinting(new wxRichTextBuffer(*m_richTextBufferPreview));
03647350 515
44219ff0
JS
516 wxRichTextPrintout *p1 = CreatePrintout();
517 p1->SetRichTextBuffer(m_richTextBufferPreview);
518
519 wxRichTextPrintout *p2 = CreatePrintout();
520 p2->SetRichTextBuffer(m_richTextBufferPrinting);
521 return DoPreview(p1, p2);
522}
523
524bool wxRichTextPrinting::PreviewBuffer(const wxRichTextBuffer& buffer)
525{
526 SetRichTextBufferPreview(new wxRichTextBuffer(buffer));
527 SetRichTextBufferPrinting(new wxRichTextBuffer(buffer));
528
529 wxRichTextPrintout *p1 = CreatePrintout();
530 p1->SetRichTextBuffer(m_richTextBufferPreview);
03647350 531
44219ff0
JS
532 wxRichTextPrintout *p2 = CreatePrintout();
533 p2->SetRichTextBuffer(m_richTextBufferPrinting);
534
535 return DoPreview(p1, p2);
536}
537
538bool wxRichTextPrinting::PrintFile(const wxString& richTextFile)
539{
540 SetRichTextBufferPrinting(new wxRichTextBuffer);
541
542 if (!m_richTextBufferPrinting->LoadFile(richTextFile))
543 {
544 SetRichTextBufferPrinting(NULL);
545 return false;
546 }
547
548 wxRichTextPrintout *p = CreatePrintout();
549 p->SetRichTextBuffer(m_richTextBufferPrinting);
550
551 bool ret = DoPrint(p);
552 delete p;
553 return ret;
554}
555
556bool wxRichTextPrinting::PrintBuffer(const wxRichTextBuffer& buffer)
557{
558 SetRichTextBufferPrinting(new wxRichTextBuffer(buffer));
559
560 wxRichTextPrintout *p = CreatePrintout();
561 p->SetRichTextBuffer(m_richTextBufferPrinting);
562
563 bool ret = DoPrint(p);
564 delete p;
565 return ret;
566}
567
568bool wxRichTextPrinting::DoPreview(wxRichTextPrintout *printout1, wxRichTextPrintout *printout2)
569{
570 // Pass two printout objects: for preview, and possible printing.
571 wxPrintDialogData printDialogData(*GetPrintData());
572 wxPrintPreview *preview = new wxPrintPreview(printout1, printout2, &printDialogData);
a1b806b9 573 if (!preview->IsOk())
44219ff0
JS
574 {
575 delete preview;
576 return false;
577 }
578
579 wxPreviewFrame *frame = new wxPreviewFrame(preview, m_parentWindow,
580 m_title + _(" Preview"),
581 m_previewRect.GetPosition(), m_previewRect.GetSize());
582 frame->Centre(wxBOTH);
583 frame->Initialize();
584 frame->Show(true);
585 return true;
586}
587
588bool wxRichTextPrinting::DoPrint(wxRichTextPrintout *printout)
589{
590 wxPrintDialogData printDialogData(*GetPrintData());
591 wxPrinter printer(&printDialogData);
592
593 if (!printer.Print(m_parentWindow, printout, true))
594 {
595 return false;
596 }
597
598 (*GetPrintData()) = printer.GetPrintDialogData().GetPrintData();
599 return true;
600}
601
602void wxRichTextPrinting::PageSetup()
603{
a1b806b9 604 if (!GetPrintData()->IsOk())
44219ff0
JS
605 {
606 wxLogError(_("There was a problem during page setup: you may need to set a default printer."));
607 return;
608 }
609
610 m_pageSetupData->SetPrintData(*GetPrintData());
611 wxPageSetupDialog pageSetupDialog(m_parentWindow, m_pageSetupData);
612
613 if (pageSetupDialog.ShowModal() == wxID_OK)
614 {
615 (*GetPrintData()) = pageSetupDialog.GetPageSetupData().GetPrintData();
616 (*m_pageSetupData) = pageSetupDialog.GetPageSetupData();
617 }
618}
619
620wxRichTextPrintout *wxRichTextPrinting::CreatePrintout()
621{
622 wxRichTextPrintout *p = new wxRichTextPrintout(m_title);
623
624 p->SetHeaderFooterData(GetHeaderFooterData());
625 p->SetMargins(10*m_pageSetupData->GetMarginTopLeft().y,
626 10*m_pageSetupData->GetMarginBottomRight().y,
627 10*m_pageSetupData->GetMarginTopLeft().x,
628 10*m_pageSetupData->GetMarginBottomRight().x);
629
630 return p;
631}
632
633/// Set/get header text, e.g. wxRICHTEXT_PAGE_ODD, wxRICHTEXT_PAGE_LEFT
634void wxRichTextPrinting::SetHeaderText(const wxString& text, wxRichTextOddEvenPage page, wxRichTextPageLocation location)
635{
636 m_headerFooterData.SetHeaderText(text, page, location);
637}
638
639wxString wxRichTextPrinting::GetHeaderText(wxRichTextOddEvenPage page, wxRichTextPageLocation location) const
640{
641 return m_headerFooterData.GetHeaderText(page, location);
642}
643
644/// Set/get footer text, e.g. wxRICHTEXT_PAGE_ODD, wxRICHTEXT_PAGE_LEFT
645void wxRichTextPrinting::SetFooterText(const wxString& text, wxRichTextOddEvenPage page, wxRichTextPageLocation location)
646{
647 m_headerFooterData.SetFooterText(text, page, location);
648}
649
650wxString wxRichTextPrinting::GetFooterText(wxRichTextOddEvenPage page, wxRichTextPageLocation location) const
651{
652 return m_headerFooterData.GetFooterText(page, location);
653}
654
655/*!
656 * Header/footer data
657 */
658
659IMPLEMENT_CLASS(wxRichTextHeaderFooterData, wxObject)
660
661/// Copy
662void wxRichTextHeaderFooterData::Copy(const wxRichTextHeaderFooterData& data)
663{
664 int i;
665 for (i = 0; i < 12; i++)
666 m_text[i] = data.m_text[i];
667 m_font = data.m_font;
668 m_colour = data.m_colour;
669 m_headerMargin = data.m_headerMargin;
670 m_footerMargin = data.m_footerMargin;
671 m_showOnFirstPage = data.m_showOnFirstPage;
672}
673
674/// Set/get text
675void wxRichTextHeaderFooterData::SetText(const wxString& text, int headerFooter, wxRichTextOddEvenPage page, wxRichTextPageLocation location)
676{
677 int idx = headerFooter + (2 * (int) page) + (4 * (int) location);
678 wxASSERT( idx >= 0 && idx < 12 );
679
680 if (idx >= 0 && idx < 12)
681 m_text[idx] = text;
682}
683
684wxString wxRichTextHeaderFooterData::GetText(int headerFooter, wxRichTextOddEvenPage page, wxRichTextPageLocation location) const
685{
686 int idx = headerFooter + (2 * (int) page) + (4 * (int) location);
687 wxASSERT( idx >= 0 && idx < 12 );
688
689 if (idx >= 0 && idx < 12)
690 return m_text[idx];
691 else
692 return wxEmptyString;
693}
694
695/// Set/get header text, e.g. wxRICHTEXT_PAGE_ODD, wxRICHTEXT_PAGE_LEFT
696void wxRichTextHeaderFooterData::SetHeaderText(const wxString& text, wxRichTextOddEvenPage page, wxRichTextPageLocation location)
697{
698 if (page == wxRICHTEXT_PAGE_ALL)
699 {
700 SetText(text, 0, wxRICHTEXT_PAGE_ODD, location);
701 SetText(text, 0, wxRICHTEXT_PAGE_EVEN, location);
702 }
703 else
704 SetText(text, 0, page, location);
705}
706
707wxString wxRichTextHeaderFooterData::GetHeaderText(wxRichTextOddEvenPage page, wxRichTextPageLocation location) const
708{
709 return GetText(0, page, location);
710}
711
712/// Set/get footer text, e.g. wxRICHTEXT_PAGE_ODD, wxRICHTEXT_PAGE_LEFT
713void wxRichTextHeaderFooterData::SetFooterText(const wxString& text, wxRichTextOddEvenPage page, wxRichTextPageLocation location)
714{
715 if (page == wxRICHTEXT_PAGE_ALL)
716 {
717 SetText(text, 1, wxRICHTEXT_PAGE_ODD, location);
718 SetText(text, 1, wxRICHTEXT_PAGE_EVEN, location);
719 }
720 else
721 SetText(text, 1, page, location);
722}
723
724wxString wxRichTextHeaderFooterData::GetFooterText(wxRichTextOddEvenPage page, wxRichTextPageLocation location) const
725{
726 return GetText(1, page, location);
727}
728
729/// Clear all text
730void wxRichTextHeaderFooterData::Clear()
731{
732 int i;
733 for (i = 0; i < 12; i++)
734 m_text[i] = wxEmptyString;
735}
736
737#endif // wxUSE_RICHTEXT & wxUSE_PRINTING_ARCHITECTURE
738