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