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