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