Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "PreviewRenderer.hxx"
21 :
22 : #include "DrawDocShell.hxx"
23 : #include "drawdoc.hxx"
24 : #include "drawview.hxx"
25 : #include "sdpage.hxx"
26 : #include "ViewShell.hxx"
27 : #include <vcl/virdev.hxx>
28 : #include <vcl/settings.hxx>
29 :
30 : #include <svx/svdpagv.hxx>
31 : #include <svx/svdoutl.hxx>
32 : #include <editeng/eeitem.hxx>
33 : #include <editeng/editstat.hxx>
34 : #include <vcl/svapp.hxx>
35 : #include <tools/diagnose_ex.h>
36 : #include <svx/sdr/contact/viewobjectcontact.hxx>
37 : #include <svx/sdr/contact/viewcontact.hxx>
38 :
39 : using namespace ::com::sun::star;
40 : using namespace ::com::sun::star::uno;
41 :
42 : namespace sd {
43 :
44 : const int PreviewRenderer::snSubstitutionTextSize = 11;
45 : const int PreviewRenderer::snFrameWidth = 1;
46 :
47 : namespace {
48 : /** This incarnation of the ViewObjectContactRedirector filters away all
49 : PageObj objects, unconditionally.
50 : */
51 : class ViewRedirector : public ::sdr::contact::ViewObjectContactRedirector
52 : {
53 : public:
54 : ViewRedirector (void);
55 : virtual ~ViewRedirector (void);
56 : virtual drawinglayer::primitive2d::Primitive2DSequence createRedirectedPrimitive2DSequence(
57 : const sdr::contact::ViewObjectContact& rOriginal,
58 : const sdr::contact::DisplayInfo& rDisplayInfo) SAL_OVERRIDE;
59 : };
60 : }
61 :
62 : //===== PreviewRenderer =======================================================
63 :
64 114 : PreviewRenderer::PreviewRenderer (
65 : OutputDevice* pTemplate,
66 : const bool bHasFrame)
67 0 : : mpPreviewDevice (new VirtualDevice()),
68 : mpDocShellOfView(NULL),
69 228 : maFrameColor (svtools::ColorConfig().GetColorValue(svtools::DOCBOUNDARIES).nColor),
70 342 : mbHasFrame(bHasFrame)
71 : {
72 114 : if (pTemplate != NULL)
73 : {
74 0 : mpPreviewDevice->SetDigitLanguage (pTemplate->GetDigitLanguage());
75 0 : mpPreviewDevice->SetBackground(pTemplate->GetBackground());
76 : }
77 : else
78 : {
79 114 : mpPreviewDevice->SetBackground(Wallpaper(
80 228 : Application::GetSettings().GetStyleSettings().GetWindowColor()));
81 : }
82 114 : }
83 :
84 228 : PreviewRenderer::~PreviewRenderer (void)
85 : {
86 114 : if (mpDocShellOfView != NULL)
87 108 : EndListening (*mpDocShellOfView);
88 114 : }
89 :
90 0 : Image PreviewRenderer::RenderPage (
91 : const SdPage* pPage,
92 : const sal_Int32 nWidth,
93 : const OUString& rSubstitutionText,
94 : const bool bObeyHighContrastMode,
95 : const bool bDisplayPresentationObjects)
96 : {
97 0 : if (pPage != NULL)
98 : {
99 0 : const Size aPageModelSize (pPage->GetSize());
100 : const double nAspectRatio (
101 0 : double(aPageModelSize.Width()) / double(aPageModelSize.Height()));
102 0 : const sal_Int32 nFrameWidth (mbHasFrame ? snFrameWidth : 0);
103 : const sal_Int32 nHeight (sal::static_int_cast<sal_Int32>(
104 0 : (nWidth - 2*nFrameWidth) / nAspectRatio + 2*nFrameWidth + 0.5));
105 : return RenderPage (
106 : pPage,
107 : Size(nWidth,nHeight),
108 : rSubstitutionText,
109 : bObeyHighContrastMode,
110 0 : bDisplayPresentationObjects);
111 : }
112 : else
113 0 : return Image();
114 : }
115 :
116 181 : Image PreviewRenderer::RenderPage (
117 : const SdPage* pPage,
118 : Size aPixelSize,
119 : const OUString& rSubstitutionText,
120 : const bool bObeyHighContrastMode,
121 : const bool bDisplayPresentationObjects)
122 : {
123 181 : Image aPreview;
124 :
125 181 : if (pPage != NULL)
126 : {
127 : try
128 : {
129 181 : if (Initialize(pPage, aPixelSize, bObeyHighContrastMode))
130 : {
131 181 : PaintPage(pPage, bDisplayPresentationObjects);
132 181 : PaintSubstitutionText(rSubstitutionText);
133 181 : PaintFrame();
134 :
135 181 : Size aSize (mpPreviewDevice->GetOutputSizePixel());
136 905 : aPreview = Image(mpPreviewDevice->GetBitmap (
137 362 : mpPreviewDevice->PixelToLogic(Point(0,0)),
138 362 : mpPreviewDevice->PixelToLogic(aSize)));
139 :
140 181 : Cleanup();
141 : }
142 : }
143 0 : catch (const com::sun::star::uno::Exception&)
144 : {
145 : DBG_UNHANDLED_EXCEPTION();
146 : }
147 : }
148 :
149 181 : return aPreview;
150 : }
151 :
152 0 : Image PreviewRenderer::RenderSubstitution (
153 : const Size& rPreviewPixelSize,
154 : const OUString& rSubstitutionText)
155 : {
156 0 : Image aPreview;
157 :
158 : try
159 : {
160 : // Set size.
161 0 : mpPreviewDevice->SetOutputSizePixel(rPreviewPixelSize);
162 :
163 : // Adjust contrast mode.
164 : const bool bUseContrast (
165 0 : Application::GetSettings().GetStyleSettings().GetHighContrastMode());
166 0 : mpPreviewDevice->SetDrawMode (bUseContrast
167 : ? ViewShell::OUTPUT_DRAWMODE_CONTRAST
168 0 : : ViewShell::OUTPUT_DRAWMODE_COLOR);
169 :
170 : // Set a map mode that makes a typical substitution text completely
171 : // visible.
172 0 : MapMode aMapMode (mpPreviewDevice->GetMapMode());
173 0 : aMapMode.SetMapUnit(MAP_100TH_MM);
174 0 : const double nFinalScale (25.0 * rPreviewPixelSize.Width() / 28000.0);
175 0 : aMapMode.SetScaleX(nFinalScale);
176 0 : aMapMode.SetScaleY(nFinalScale);
177 0 : const sal_Int32 nFrameWidth (mbHasFrame ? snFrameWidth : 0);
178 0 : aMapMode.SetOrigin(mpPreviewDevice->PixelToLogic(
179 0 : Point(nFrameWidth,nFrameWidth),aMapMode));
180 0 : mpPreviewDevice->SetMapMode (aMapMode);
181 :
182 : // Clear the background.
183 : const Rectangle aPaintRectangle (
184 : Point(0,0),
185 0 : mpPreviewDevice->GetOutputSizePixel());
186 0 : mpPreviewDevice->EnableMapMode(false);
187 0 : mpPreviewDevice->SetLineColor();
188 0 : svtools::ColorConfig aColorConfig;
189 0 : mpPreviewDevice->SetFillColor(aColorConfig.GetColorValue(svtools::DOCCOLOR).nColor);
190 0 : mpPreviewDevice->DrawRect (aPaintRectangle);
191 0 : mpPreviewDevice->EnableMapMode(true);
192 :
193 : // Paint substitution text and a frame around it.
194 0 : PaintSubstitutionText (rSubstitutionText);
195 0 : PaintFrame();
196 :
197 0 : const Size aSize (mpPreviewDevice->GetOutputSizePixel());
198 0 : aPreview = Image(mpPreviewDevice->GetBitmap(
199 0 : mpPreviewDevice->PixelToLogic(Point(0,0)),
200 0 : mpPreviewDevice->PixelToLogic(aSize)));
201 : }
202 0 : catch (const com::sun::star::uno::Exception&)
203 : {
204 : DBG_UNHANDLED_EXCEPTION();
205 : }
206 :
207 0 : return aPreview;
208 : }
209 :
210 181 : bool PreviewRenderer::Initialize (
211 : const SdPage* pPage,
212 : const Size& rPixelSize,
213 : const bool bObeyHighContrastMode)
214 : {
215 181 : if (pPage == NULL)
216 0 : return false;
217 :
218 181 : SdrModel* pModel = pPage->GetModel();
219 181 : if (pModel == NULL)
220 0 : return false;
221 :
222 181 : SetupOutputSize(*pPage, rPixelSize);
223 :
224 : SdDrawDocument* pDocument
225 181 : = static_cast<SdDrawDocument*>(pPage->GetModel());
226 181 : DrawDocShell* pDocShell = pDocument->GetDocSh();
227 :
228 : // Create view
229 181 : ProvideView (pDocShell);
230 181 : if (mpView.get() == NULL)
231 0 : return false;
232 :
233 : // Adjust contrast mode.
234 : bool bUseContrast (bObeyHighContrastMode
235 181 : && Application::GetSettings().GetStyleSettings().GetHighContrastMode());
236 181 : mpPreviewDevice->SetDrawMode (bUseContrast
237 : ? ViewShell::OUTPUT_DRAWMODE_CONTRAST
238 362 : : ViewShell::OUTPUT_DRAWMODE_COLOR);
239 181 : mpPreviewDevice->SetSettings(Application::GetSettings());
240 :
241 : // Tell the view to show the given page.
242 181 : SdPage* pNonConstPage = const_cast<SdPage*>(pPage);
243 181 : if (pPage->IsMasterPage())
244 : {
245 4 : mpView->ShowSdrPage(mpView->GetModel()->GetMasterPage(pPage->GetPageNum()));
246 : }
247 : else
248 : {
249 177 : mpView->ShowSdrPage(pNonConstPage);
250 : }
251 :
252 : // Make sure that a page view exists.
253 181 : SdrPageView* pPageView = mpView->GetSdrPageView();
254 :
255 181 : if (pPageView == NULL)
256 0 : return false;
257 :
258 : // #i121224# No need to set SetApplicationBackgroundColor (which is the color
259 : // of the area 'behind' the page (formally called 'Wiese') since the page previews
260 : // produced exactly cover the page's area, so it would never be visible. What
261 : // needs to be set is the ApplicationDocumentColor which is derived from
262 : // svtools::DOCCOLOR normally
263 181 : Color aApplicationDocumentColor;
264 :
265 181 : if (pPageView->GetApplicationDocumentColor() == COL_AUTO)
266 : {
267 181 : svtools::ColorConfig aColorConfig;
268 181 : aApplicationDocumentColor = aColorConfig.GetColorValue( svtools::DOCCOLOR ).nColor;
269 : }
270 : else
271 : {
272 0 : aApplicationDocumentColor = pPageView->GetApplicationDocumentColor();
273 : }
274 :
275 181 : pPageView->SetApplicationDocumentColor(aApplicationDocumentColor);
276 181 : SdrOutliner& rOutliner(pDocument->GetDrawOutliner(NULL));
277 181 : rOutliner.SetBackgroundColor(aApplicationDocumentColor);
278 181 : rOutliner.SetDefaultLanguage(pDocument->GetLanguage(EE_CHAR_LANGUAGE));
279 181 : mpPreviewDevice->SetBackground(Wallpaper(aApplicationDocumentColor));
280 181 : mpPreviewDevice->Erase();
281 :
282 181 : return true;
283 : }
284 :
285 181 : void PreviewRenderer::Cleanup (void)
286 : {
287 181 : mpView->HideSdrPage();
288 181 : }
289 :
290 181 : void PreviewRenderer::PaintPage (
291 : const SdPage* pPage,
292 : const bool bDisplayPresentationObjects)
293 : {
294 : // Paint the page.
295 181 : Rectangle aPaintRectangle (Point(0,0), pPage->GetSize());
296 181 : vcl::Region aRegion (aPaintRectangle);
297 :
298 : // Turn off online spelling and redlining.
299 181 : SdrOutliner* pOutliner = NULL;
300 181 : sal_uLong nSavedControlWord (0);
301 181 : if (mpDocShellOfView!=NULL && mpDocShellOfView->GetDoc()!=NULL)
302 : {
303 181 : pOutliner = &mpDocShellOfView->GetDoc()->GetDrawOutliner();
304 181 : nSavedControlWord = pOutliner->GetControlWord();
305 181 : pOutliner->SetControlWord((nSavedControlWord & ~EE_CNTRL_ONLINESPELLING));
306 : }
307 :
308 : // Use a special redirector to prevent PresObj shapes from being painted.
309 362 : boost::scoped_ptr<ViewRedirector> pRedirector;
310 181 : if ( ! bDisplayPresentationObjects)
311 181 : pRedirector.reset(new ViewRedirector());
312 :
313 : try
314 : {
315 181 : mpView->CompleteRedraw(mpPreviewDevice.get(), aRegion, pRedirector.get());
316 : }
317 0 : catch (const ::com::sun::star::uno::Exception&)
318 : {
319 : DBG_UNHANDLED_EXCEPTION();
320 : }
321 :
322 : // Restore the previous online spelling and redlining states.
323 181 : if (pOutliner != NULL)
324 362 : pOutliner->SetControlWord(nSavedControlWord);
325 181 : }
326 :
327 181 : void PreviewRenderer::PaintSubstitutionText (const OUString& rSubstitutionText)
328 : {
329 181 : if (!rSubstitutionText.isEmpty())
330 : {
331 : // Set the font size.
332 0 : const vcl::Font& rOriginalFont (mpPreviewDevice->GetFont());
333 0 : vcl::Font aFont (mpPreviewDevice->GetSettings().GetStyleSettings().GetAppFont());
334 0 : sal_Int32 nHeight (mpPreviewDevice->PixelToLogic(Size(0,snSubstitutionTextSize)).Height());
335 0 : aFont.SetHeight(nHeight);
336 0 : mpPreviewDevice->SetFont (aFont);
337 :
338 : // Paint the substitution text.
339 : Rectangle aTextBox (
340 : Point(0,0),
341 0 : mpPreviewDevice->PixelToLogic(
342 0 : mpPreviewDevice->GetOutputSizePixel()));
343 : sal_uInt16 nTextStyle =
344 : TEXT_DRAW_CENTER
345 : | TEXT_DRAW_VCENTER
346 : | TEXT_DRAW_MULTILINE
347 0 : | TEXT_DRAW_WORDBREAK;
348 0 : mpPreviewDevice->DrawText (aTextBox, rSubstitutionText, nTextStyle);
349 :
350 : // Restore the font.
351 0 : mpPreviewDevice->SetFont (rOriginalFont);
352 : }
353 181 : }
354 :
355 181 : void PreviewRenderer::PaintFrame (void)
356 : {
357 181 : if (mbHasFrame)
358 : {
359 : // Paint a frame around the preview.
360 : Rectangle aPaintRectangle (
361 : Point(0,0),
362 0 : mpPreviewDevice->GetOutputSizePixel());
363 0 : mpPreviewDevice->EnableMapMode(false);
364 0 : mpPreviewDevice->SetLineColor(maFrameColor);
365 0 : mpPreviewDevice->SetFillColor();
366 0 : mpPreviewDevice->DrawRect(aPaintRectangle);
367 0 : mpPreviewDevice->EnableMapMode(true);
368 : }
369 181 : }
370 :
371 181 : void PreviewRenderer::SetupOutputSize (
372 : const SdPage& rPage,
373 : const Size& rFramePixelSize)
374 : {
375 : // First set the map mode to some arbitrary scale that is numerically
376 : // stable.
377 181 : MapMode aMapMode (mpPreviewDevice->GetMapMode());
378 181 : aMapMode.SetMapUnit(MAP_PIXEL);
379 :
380 : // Adapt it to the desired width.
381 181 : const Size aPageModelSize (rPage.GetSize());
382 181 : if (aPageModelSize.Width()>0 || aPageModelSize.Height()>0)
383 : {
384 181 : const sal_Int32 nFrameWidth (mbHasFrame ? snFrameWidth : 0);
385 : aMapMode.SetScaleX(
386 181 : Fraction(rFramePixelSize.Width()-2*nFrameWidth-1, aPageModelSize.Width()));
387 : aMapMode.SetScaleY(
388 181 : Fraction(rFramePixelSize.Height()-2*nFrameWidth-1, aPageModelSize.Height()));
389 181 : aMapMode.SetOrigin(mpPreviewDevice->PixelToLogic(Point(nFrameWidth,nFrameWidth),aMapMode));
390 : }
391 : else
392 : {
393 : // We should never get here.
394 : OSL_ASSERT(false);
395 0 : aMapMode.SetScaleX(1.0);
396 0 : aMapMode.SetScaleY(1.0);
397 : }
398 181 : mpPreviewDevice->SetMapMode (aMapMode);
399 181 : mpPreviewDevice->SetOutputSizePixel(rFramePixelSize);
400 181 : }
401 :
402 181 : void PreviewRenderer::ProvideView (DrawDocShell* pDocShell)
403 : {
404 181 : if (pDocShell != mpDocShellOfView)
405 : {
406 : // Destroy the view that is connected to the current doc shell.
407 108 : mpView.reset();
408 :
409 : // Switch our attention, i.e. listening for DYING events, to
410 : // the new doc shell.
411 108 : if (mpDocShellOfView != NULL)
412 0 : EndListening (*mpDocShellOfView);
413 108 : mpDocShellOfView = pDocShell;
414 108 : if (mpDocShellOfView != NULL)
415 108 : StartListening (*mpDocShellOfView);
416 : }
417 181 : if (mpView.get() == NULL)
418 : {
419 108 : mpView.reset (new DrawView (pDocShell, mpPreviewDevice.get(), NULL));
420 : }
421 181 : mpView->SetPreviewRenderer(true);
422 : #if 1
423 181 : mpView->SetPageVisible(false);
424 181 : mpView->SetPageBorderVisible(true);
425 181 : mpView->SetBordVisible(false);
426 181 : mpView->SetGridVisible(false);
427 181 : mpView->SetHlplVisible(false);
428 181 : mpView->SetGlueVisible(false);
429 :
430 : #else
431 : // This works in the slide sorter but prevents the master page
432 : // background being painted in the list of current master pages in the
433 : // task manager.
434 : mpView->SetPagePaintingAllowed(false);
435 : #endif
436 181 : }
437 :
438 0 : Image PreviewRenderer::ScaleBitmap (
439 : const BitmapEx& rBitmapEx,
440 : int nWidth)
441 : {
442 0 : Image aPreview;
443 :
444 : do
445 : {
446 : // Adjust contrast mode.
447 0 : bool bUseContrast = Application::GetSettings().GetStyleSettings().
448 0 : GetHighContrastMode();
449 0 : mpPreviewDevice->SetDrawMode (bUseContrast
450 : ? ViewShell::OUTPUT_DRAWMODE_CONTRAST
451 0 : : ViewShell::OUTPUT_DRAWMODE_COLOR);
452 :
453 : // Set output size.
454 0 : Size aSize (rBitmapEx.GetSizePixel());
455 0 : if (aSize.Width() <= 0)
456 0 : break;
457 : Size aFrameSize (
458 : nWidth,
459 0 : (long)((nWidth*1.0 * aSize.Height()) / aSize.Width() + 0.5));
460 0 : Size aPreviewSize (aFrameSize.Width()-2,aFrameSize.Height()-2);
461 0 : MapMode aMapMode (mpPreviewDevice->GetMapMode());
462 0 : aMapMode.SetMapUnit(MAP_PIXEL);
463 0 : aMapMode.SetOrigin (Point());
464 0 : aMapMode.SetScaleX (1.0);
465 0 : aMapMode.SetScaleY (1.0);
466 0 : mpPreviewDevice->SetMapMode (aMapMode);
467 0 : mpPreviewDevice->SetOutputSize (aFrameSize);
468 :
469 : // Paint a frame around the preview.
470 0 : mpPreviewDevice->SetLineColor (maFrameColor);
471 0 : mpPreviewDevice->SetFillColor ();
472 0 : mpPreviewDevice->DrawRect (Rectangle(Point(0,0), aFrameSize));
473 :
474 : // Paint the bitmap scaled to the desired width.
475 0 : BitmapEx aScaledBitmap (rBitmapEx.GetBitmap());
476 0 : aScaledBitmap.Scale (aPreviewSize, BMP_SCALE_BESTQUALITY);
477 0 : mpPreviewDevice->DrawBitmap (
478 : Point(1,1),
479 : aPreviewSize,
480 0 : aScaledBitmap.GetBitmap());
481 :
482 : // Get the resulting bitmap.
483 0 : aPreview = Image(mpPreviewDevice->GetBitmap(Point(0,0), aFrameSize));
484 : }
485 : while (false);
486 :
487 0 : return aPreview;
488 : }
489 :
490 2151 : void PreviewRenderer::Notify(SfxBroadcaster&, const SfxHint& rHint)
491 : {
492 2151 : const SfxSimpleHint* pSimpleHint = dynamic_cast<const SfxSimpleHint*>(&rHint);
493 2151 : if (pSimpleHint && mpDocShellOfView && pSimpleHint->GetId() == SFX_HINT_DYING)
494 : {
495 : // The doc shell is dying. Our view uses its item pool and
496 : // has to be destroyed as well. The next call to
497 : // ProvideView will create a new one (for another
498 : // doc shell, of course.)
499 0 : mpView.reset();
500 0 : mpDocShellOfView = NULL;
501 : }
502 2151 : }
503 :
504 : //===== ViewRedirector ========================================================
505 :
506 : namespace {
507 :
508 181 : ViewRedirector::ViewRedirector (void)
509 : {
510 181 : }
511 :
512 362 : ViewRedirector::~ViewRedirector (void)
513 : {
514 362 : }
515 :
516 326 : drawinglayer::primitive2d::Primitive2DSequence ViewRedirector::createRedirectedPrimitive2DSequence(
517 : const sdr::contact::ViewObjectContact& rOriginal,
518 : const sdr::contact::DisplayInfo& rDisplayInfo)
519 : {
520 326 : SdrObject* pObject = rOriginal.GetViewContact().TryToGetSdrObject();
521 :
522 326 : if (pObject==NULL || pObject->GetPage() == NULL)
523 : {
524 : // not a SdrObject visualisation (maybe e.g. page) or no page
525 : return sdr::contact::ViewObjectContactRedirector::createRedirectedPrimitive2DSequence(
526 : rOriginal,
527 4 : rDisplayInfo);
528 : }
529 :
530 322 : const bool bDoCreateGeometry (pObject->GetPage()->checkVisibility( rOriginal, rDisplayInfo, true));
531 :
532 644 : if ( ! bDoCreateGeometry
533 322 : && (pObject->GetObjInventor() != SdrInventor || pObject->GetObjIdentifier() != OBJ_PAGE))
534 : {
535 27 : return drawinglayer::primitive2d::Primitive2DSequence();
536 : }
537 :
538 295 : if (pObject->IsEmptyPresObj())
539 48 : return drawinglayer::primitive2d::Primitive2DSequence();
540 :
541 : return sdr::contact::ViewObjectContactRedirector::createRedirectedPrimitive2DSequence(
542 : rOriginal,
543 247 : rDisplayInfo);
544 : }
545 :
546 : } // end of anonymous namespace
547 :
548 114 : } // end of namespace ::sd
549 :
550 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|