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