1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include <vcl/svapp.hxx>
20 #include <vcl/settings.hxx>
21 #include "PresenterHelpView.hxx"
22 #include "PresenterButton.hxx"
23 #include "PresenterCanvasHelper.hxx"
24 #include "PresenterGeometryHelper.hxx"
25 #include "PresenterHelper.hxx"
26 #include "PresenterWindowManager.hxx"
27 #include <com/sun/star/awt/XWindowPeer.hpp>
28 #include <com/sun/star/container/XNameAccess.hpp>
29 #include <com/sun/star/drawing/framework/XConfigurationController.hpp>
30 #include <com/sun/star/drawing/framework/XControllerManager.hpp>
31 #include <com/sun/star/rendering/CompositeOperation.hpp>
32 #include <com/sun/star/rendering/TextDirection.hpp>
33 #include <com/sun/star/util/Color.hpp>
38 using namespace ::com::sun::star
;
39 using namespace ::com::sun::star::uno
;
40 using namespace ::com::sun::star::drawing::framework
;
43 namespace sdext
{ namespace presenter
{
46 const static sal_Int32
gnHorizontalGap (20);
47 const static sal_Int32
gnVerticalBorder (30);
48 const static sal_Int32
gnVerticalButtonPadding (12);
55 const OUString
& rsLine
,
56 const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
);
60 geometry::RealSize2D maSize
;
61 double mnVerticalOffset
;
63 void CalculateSize (const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
);
66 class LineDescriptorList
70 const OUString
& rsText
,
71 const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
,
72 const sal_Int32 nMaximalWidth
);
75 const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
,
76 const sal_Int32 nMaximalWidth
);
79 const Reference
<rendering::XCanvas
>& rxCanvas
,
80 const geometry::RealRectangle2D
& rBBox
,
81 const bool bFlushLeft
,
82 const rendering::ViewState
& rViewState
,
83 rendering::RenderState
& rRenderState
,
84 const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
) const;
85 double GetHeight() const;
88 const OUString msText
;
89 std::shared_ptr
<vector
<LineDescriptor
> > mpLineDescriptors
;
91 static void SplitText (const OUString
& rsText
, vector
<OUString
>& rTextParts
);
93 const vector
<OUString
>& rTextParts
,
94 const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
,
95 const sal_Int32 nMaximalWidth
);
102 const OUString
& rsLeftText
,
103 const OUString
& rsRightText
,
104 const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
,
105 const sal_Int32 nMaximalWidth
);
106 Block(const Block
&) = delete;
107 Block
& operator=(const Block
&) = delete;
109 const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
,
110 const sal_Int32 nMaximalWidth
);
112 LineDescriptorList maLeft
;
113 LineDescriptorList maRight
;
115 } // end of anonymous namespace
117 class PresenterHelpView::TextContainer
: public vector
<std::shared_ptr
<Block
> >
121 PresenterHelpView::PresenterHelpView (
122 const Reference
<uno::XComponentContext
>& rxContext
,
123 const Reference
<XResourceId
>& rxViewId
,
124 const Reference
<frame::XController
>& rxController
,
125 const ::rtl::Reference
<PresenterController
>& rpPresenterController
)
126 : PresenterHelpViewInterfaceBase(m_aMutex
),
127 mxComponentContext(rxContext
),
132 mpPresenterController(rpPresenterController
),
141 // Get the content window via the pane anchor.
142 Reference
<XControllerManager
> xCM (rxController
, UNO_QUERY_THROW
);
143 Reference
<XConfigurationController
> xCC (
144 xCM
->getConfigurationController(), UNO_SET_THROW
);
145 mxPane
.set(xCC
->getResource(rxViewId
->getAnchor()), UNO_QUERY_THROW
);
147 mxWindow
= mxPane
->getWindow();
150 mxWindow
->addWindowListener(this);
151 mxWindow
->addPaintListener(this);
152 Reference
<awt::XWindowPeer
> xPeer (mxWindow
, UNO_QUERY
);
154 xPeer
->setBackground(util::Color(0xff000000));
155 mxWindow
->setVisible(true);
157 if (mpPresenterController
.is())
159 mpFont
= mpPresenterController
->GetViewFont(mxViewId
->getResourceURL());
160 if (mpFont
.get() != nullptr)
162 mpFont
->PrepareFont(mxCanvas
);
166 // Create the close button.
167 mpCloseButton
= PresenterButton::Create(
169 mpPresenterController
,
170 mpPresenterController
->GetTheme(),
178 catch (RuntimeException
&)
186 PresenterHelpView::~PresenterHelpView()
190 void SAL_CALL
PresenterHelpView::disposing()
194 if (mpCloseButton
.is())
196 Reference
<lang::XComponent
> xComponent (
197 static_cast<XWeak
*>(mpCloseButton
.get()), UNO_QUERY
);
198 mpCloseButton
= nullptr;
200 xComponent
->dispose();
205 mxWindow
->removeWindowListener(this);
206 mxWindow
->removePaintListener(this);
210 //----- lang::XEventListener --------------------------------------------------
212 void SAL_CALL
PresenterHelpView::disposing (const lang::EventObject
& rEventObject
)
214 if (rEventObject
.Source
== mxCanvas
)
218 else if (rEventObject
.Source
== mxWindow
)
225 //----- XWindowListener -------------------------------------------------------
227 void SAL_CALL
PresenterHelpView::windowResized (const awt::WindowEvent
&)
233 void SAL_CALL
PresenterHelpView::windowMoved (const awt::WindowEvent
&)
238 void SAL_CALL
PresenterHelpView::windowShown (const lang::EventObject
&)
244 void SAL_CALL
PresenterHelpView::windowHidden (const lang::EventObject
&)
249 //----- XPaintListener --------------------------------------------------------
251 void SAL_CALL
PresenterHelpView::windowPaint (const css::awt::PaintEvent
& rEvent
)
253 Paint(rEvent
.UpdateRect
);
256 void PresenterHelpView::Paint (const awt::Rectangle
& rUpdateBox
)
259 if ( ! mxCanvas
.is())
263 const awt::Rectangle
aWindowBox (mxWindow
->getPosSize());
264 mpPresenterController
->GetCanvasHelper()->Paint(
265 mpPresenterController
->GetViewBackground(mxViewId
->getResourceURL()),
266 Reference
<rendering::XCanvas
>(mxCanvas
, UNO_QUERY
),
268 awt::Rectangle(0,0,aWindowBox
.Width
,aWindowBox
.Height
),
271 // Paint vertical divider.
273 rendering::ViewState
aViewState(
274 geometry::AffineMatrix2D(1,0,0, 0,1,0),
275 PresenterGeometryHelper::CreatePolygon(rUpdateBox
, mxCanvas
->getDevice()));
277 rendering::RenderState
aRenderState (
278 geometry::AffineMatrix2D(1,0,0, 0,1,0),
281 rendering::CompositeOperation::SOURCE
);
282 PresenterCanvasHelper::SetDeviceColor(aRenderState
, mpFont
->mnColor
);
285 geometry::RealPoint2D((aWindowBox
.Width
/2.0), gnVerticalBorder
),
286 geometry::RealPoint2D((aWindowBox
.Width
/2.0), mnSeparatorY
- gnVerticalBorder
),
290 // Paint the horizontal separator.
292 geometry::RealPoint2D(0, mnSeparatorY
),
293 geometry::RealPoint2D(aWindowBox
.Width
, mnSeparatorY
),
298 double nY (gnVerticalBorder
);
299 for (const auto& rxBlock
: *mpTextContainer
)
301 sal_Int32 LeftX1
= gnHorizontalGap
;
302 sal_Int32 LeftX2
= aWindowBox
.Width
/2 - gnHorizontalGap
;
303 sal_Int32 RightX1
= aWindowBox
.Width
/2 + gnHorizontalGap
;
304 sal_Int32 RightX2
= aWindowBox
.Width
- gnHorizontalGap
;
305 /* check whether RTL interface or not
306 then replace the windowbox position */
307 if(AllSettings::GetLayoutRTL())
309 LeftX1
= aWindowBox
.Width
/2 + gnHorizontalGap
;
310 LeftX2
= aWindowBox
.Width
- gnHorizontalGap
;
311 RightX1
= gnHorizontalGap
;
312 RightX2
= aWindowBox
.Width
/2 - gnHorizontalGap
;
314 const double nLeftHeight (
315 rxBlock
->maLeft
.Paint(mxCanvas
,
316 geometry::RealRectangle2D(
320 aWindowBox
.Height
- gnVerticalBorder
),
325 const double nRightHeight (
326 rxBlock
->maRight
.Paint(mxCanvas
,
327 geometry::RealRectangle2D(
331 aWindowBox
.Height
- gnVerticalBorder
),
337 nY
+= ::std::max(nLeftHeight
,nRightHeight
);
340 Reference
<rendering::XSpriteCanvas
> xSpriteCanvas (mxCanvas
, UNO_QUERY
);
341 if (xSpriteCanvas
.is())
342 xSpriteCanvas
->updateScreen(false);
345 void PresenterHelpView::ReadHelpStrings()
347 mpTextContainer
.reset(new TextContainer
);
348 PresenterConfigurationAccess
aConfiguration (
350 "/org.openoffice.Office.PresenterScreen/",
351 PresenterConfigurationAccess::READ_ONLY
);
352 Reference
<container::XNameAccess
> xStrings (
353 aConfiguration
.GetConfigurationNode("PresenterScreenSettings/HelpView/HelpStrings"),
355 PresenterConfigurationAccess::ForAll(
357 [this](OUString
const&, uno::Reference
<beans::XPropertySet
> const& xProps
)
359 return this->ProcessString(xProps
);
363 void PresenterHelpView::ProcessString (
364 const Reference
<beans::XPropertySet
>& rsProperties
)
366 if ( ! rsProperties
.is())
370 PresenterConfigurationAccess::GetProperty(rsProperties
, "Left") >>= sLeftText
;
372 PresenterConfigurationAccess::GetProperty(rsProperties
, "Right") >>= sRightText
;
373 mpTextContainer
->push_back(
374 std::make_shared
<Block
>(
375 sLeftText
, sRightText
, mpFont
->mxFont
, mnMaximalWidth
));
378 void PresenterHelpView::CheckFontSize()
380 if (mpFont
.get() == nullptr)
383 sal_Int32
nBestSize (6);
385 // Scaling down and then reformatting can cause the text to be too large
386 // still. So do this again and again until the text size is
387 // small enough. Restrict the number of loops.
388 for (int nLoopCount
=0; nLoopCount
<5; ++nLoopCount
)
390 double nY
= std::accumulate(mpTextContainer
->begin(), mpTextContainer
->end(), double(0),
391 [](const double& sum
, const std::shared_ptr
<Block
>& rxBlock
) {
392 return sum
+ std::max(
393 rxBlock
->maLeft
.GetHeight(),
394 rxBlock
->maRight
.GetHeight());
397 const double nHeightDifference (nY
- (mnSeparatorY
-gnVerticalBorder
));
398 if (nHeightDifference
<= 0 && nHeightDifference
> -50)
400 // We have found a good font size that is large and leaves not
401 // too much space below the help text.
405 // Use a simple linear transformation to calculate initial guess of
406 // a size that lets all help text be shown inside the window.
407 const double nScale (double(mnSeparatorY
-gnVerticalBorder
) / nY
);
408 if (nScale
> 1.0 && nScale
< 1.05)
411 sal_Int32
nFontSizeGuess (sal_Int32(mpFont
->mnSize
* nScale
));
412 if (nHeightDifference
<=0 && mpFont
->mnSize
>nBestSize
)
413 nBestSize
= mpFont
->mnSize
;
414 mpFont
->mnSize
= nFontSizeGuess
;
415 mpFont
->mxFont
= nullptr;
416 mpFont
->PrepareFont(mxCanvas
);
419 for (auto& rxBlock
: *mpTextContainer
)
420 rxBlock
->Update(mpFont
->mxFont
, mnMaximalWidth
);
423 if (nBestSize
!= mpFont
->mnSize
)
425 mpFont
->mnSize
= nBestSize
;
426 mpFont
->mxFont
= nullptr;
427 mpFont
->PrepareFont(mxCanvas
);
430 for (auto& rxBlock
: *mpTextContainer
)
432 rxBlock
->Update(mpFont
->mxFont
, mnMaximalWidth
);
437 //----- XResourceId -----------------------------------------------------------
439 Reference
<XResourceId
> SAL_CALL
PresenterHelpView::getResourceId()
445 sal_Bool SAL_CALL
PresenterHelpView::isAnchorOnly()
451 void PresenterHelpView::ProvideCanvas()
453 if ( ! mxCanvas
.is() && mxPane
.is())
455 mxCanvas
= mxPane
->getCanvas();
456 if ( ! mxCanvas
.is())
458 Reference
<lang::XComponent
> xComponent (mxCanvas
, UNO_QUERY
);
460 xComponent
->addEventListener(static_cast<awt::XPaintListener
*>(this));
462 if (mpCloseButton
.is())
463 mpCloseButton
->SetCanvas(mxCanvas
, mxWindow
);
467 void PresenterHelpView::Resize()
469 if (!(mpCloseButton
.get() != nullptr && mxWindow
.is()))
472 const awt::Rectangle
aWindowBox (mxWindow
->getPosSize());
473 mnMaximalWidth
= (mxWindow
->getPosSize().Width
- 4*gnHorizontalGap
) / 2;
475 // Place vertical separator.
476 mnSeparatorY
= aWindowBox
.Height
477 - mpCloseButton
->GetSize().Height
- gnVerticalButtonPadding
;
479 mpCloseButton
->SetCenter(geometry::RealPoint2D(
480 aWindowBox
.Width
/2.0,
481 aWindowBox
.Height
- mpCloseButton
->GetSize().Height
/2.0));
486 void PresenterHelpView::ThrowIfDisposed()
488 if (rBHelper
.bDisposed
|| rBHelper
.bInDispose
)
490 throw lang::DisposedException (
491 "PresenterHelpView has been already disposed",
492 static_cast<uno::XWeak
*>(this));
496 //===== LineDescriptor =========================================================
500 LineDescriptor::LineDescriptor()
507 void LineDescriptor::AddPart (
508 const OUString
& rsLine
,
509 const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
)
513 CalculateSize(rxFont
);
516 bool LineDescriptor::IsEmpty() const
518 return msLine
.isEmpty();
521 void LineDescriptor::CalculateSize (
522 const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
)
524 OSL_ASSERT(rxFont
.is());
526 rendering::StringContext
aContext (msLine
, 0, msLine
.getLength());
527 Reference
<rendering::XTextLayout
> xLayout (
528 rxFont
->createTextLayout(aContext
, rendering::TextDirection::WEAK_LEFT_TO_RIGHT
, 0));
529 const geometry::RealRectangle2D
aTextBBox (xLayout
->queryTextBounds());
530 maSize
= css::geometry::RealSize2D(aTextBBox
.X2
- aTextBBox
.X1
, aTextBBox
.Y2
- aTextBBox
.Y1
);
531 mnVerticalOffset
= aTextBBox
.Y2
;
534 } // end of anonymous namespace
536 //===== LineDescriptorList ====================================================
540 LineDescriptorList::LineDescriptorList (
541 const OUString
& rsText
,
542 const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
,
543 const sal_Int32 nMaximalWidth
)
546 Update(rxFont
, nMaximalWidth
);
549 double LineDescriptorList::Paint(
550 const Reference
<rendering::XCanvas
>& rxCanvas
,
551 const geometry::RealRectangle2D
& rBBox
,
552 const bool bFlushLeft
,
553 const rendering::ViewState
& rViewState
,
554 rendering::RenderState
& rRenderState
,
555 const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
) const
557 if ( ! rxCanvas
.is())
560 double nY (rBBox
.Y1
);
561 for (const auto& rLine
: *mpLineDescriptors
)
564 /// check whether RTL interface or not
565 if(!AllSettings::GetLayoutRTL())
569 nX
= rBBox
.X2
- rLine
.maSize
.Width
;
573 nX
=rBBox
.X2
- rLine
.maSize
.Width
;
577 rRenderState
.AffineTransform
.m02
= nX
;
578 rRenderState
.AffineTransform
.m12
= nY
+ rLine
.maSize
.Height
- rLine
.mnVerticalOffset
;
580 const rendering::StringContext
aContext (rLine
.msLine
, 0, rLine
.msLine
.getLength());
581 Reference
<rendering::XTextLayout
> xLayout (
582 rxFont
->createTextLayout(aContext
, rendering::TextDirection::WEAK_LEFT_TO_RIGHT
, 0));
583 rxCanvas
->drawTextLayout (
588 nY
+= rLine
.maSize
.Height
* 1.2;
591 return nY
- rBBox
.Y1
;
594 double LineDescriptorList::GetHeight() const
596 return std::accumulate(mpLineDescriptors
->begin(), mpLineDescriptors
->end(), double(0),
597 [](const double& nHeight
, const LineDescriptor
& rLine
) {
598 return nHeight
+ rLine
.maSize
.Height
* 1.2;
602 void LineDescriptorList::Update (
603 const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
,
604 const sal_Int32 nMaximalWidth
)
606 vector
<OUString
> aTextParts
;
607 SplitText(msText
, aTextParts
);
608 FormatText(aTextParts
, rxFont
, nMaximalWidth
);
611 void LineDescriptorList::SplitText (
612 const OUString
& rsText
,
613 vector
<OUString
>& rTextParts
)
615 const sal_Char
cQuote ('\'');
616 const sal_Char
cSeparator (',');
618 sal_Int32
nIndex (0);
619 sal_Int32
nStart (0);
620 sal_Int32
nLength (rsText
.getLength());
621 bool bIsQuoted (false);
622 while (nIndex
< nLength
)
624 const sal_Int32
nQuoteIndex (rsText
.indexOf(cQuote
, nIndex
));
625 const sal_Int32
nSeparatorIndex (rsText
.indexOf(cSeparator
, nIndex
));
626 if (nQuoteIndex
>=0 && (nSeparatorIndex
==-1 || nQuoteIndex
<nSeparatorIndex
))
628 bIsQuoted
= !bIsQuoted
;
629 nIndex
= nQuoteIndex
+1;
633 const sal_Int32 nNextIndex
= nSeparatorIndex
;
638 else if ( ! bIsQuoted
)
640 rTextParts
.push_back(rsText
.copy(nStart
, nNextIndex
-nStart
));
641 nStart
= nNextIndex
+ 1;
643 nIndex
= nNextIndex
+1;
645 if (nStart
< nLength
)
646 rTextParts
.push_back(rsText
.copy(nStart
, nLength
-nStart
));
649 void LineDescriptorList::FormatText (
650 const vector
<OUString
>& rTextParts
,
651 const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
,
652 const sal_Int32 nMaximalWidth
)
654 LineDescriptor aLineDescriptor
;
656 mpLineDescriptors
.reset(new vector
<LineDescriptor
>);
658 vector
<OUString
>::const_iterator
iPart (rTextParts
.begin());
659 vector
<OUString
>::const_iterator
iEnd (rTextParts
.end());
662 if (aLineDescriptor
.IsEmpty())
664 // Avoid empty lines.
665 if (PresenterCanvasHelper::GetTextSize(
666 rxFont
, *iPart
).Width
> nMaximalWidth
)
668 const sal_Char
cSpace (' ');
670 sal_Int32
nIndex (0);
671 sal_Int32
nStart (0);
672 sal_Int32
nLength (iPart
->getLength());
673 while (nIndex
< nLength
)
675 sal_Int32
nSpaceIndex (iPart
->indexOf(cSpace
, nIndex
));
676 while (nSpaceIndex
>= 0 && PresenterCanvasHelper::GetTextSize(
677 rxFont
, iPart
->copy(nStart
, nSpaceIndex
-nStart
)).Width
<= nMaximalWidth
)
679 nIndex
= nSpaceIndex
;
680 nSpaceIndex
= iPart
->indexOf(cSpace
, nIndex
+1);
683 if (nSpaceIndex
< 0 && PresenterCanvasHelper::GetTextSize(
684 rxFont
, iPart
->copy(nStart
, nLength
-nStart
)).Width
<= nMaximalWidth
)
689 if (nIndex
== nStart
)
694 aLineDescriptor
.AddPart(iPart
->copy(nStart
, nIndex
-nStart
), rxFont
);
695 if (nIndex
!= nLength
)
697 mpLineDescriptors
->push_back(aLineDescriptor
);
698 aLineDescriptor
= LineDescriptor();
705 aLineDescriptor
.AddPart(*iPart
, rxFont
);
708 else if (PresenterCanvasHelper::GetTextSize(
709 rxFont
, aLineDescriptor
.msLine
+", "+*iPart
).Width
> nMaximalWidth
)
711 aLineDescriptor
.AddPart(",", rxFont
);
712 mpLineDescriptors
->push_back(aLineDescriptor
);
713 aLineDescriptor
= LineDescriptor();
718 aLineDescriptor
.AddPart(", "+*iPart
, rxFont
);
722 if ( ! aLineDescriptor
.IsEmpty())
724 mpLineDescriptors
->push_back(aLineDescriptor
);
728 } // end of anonymous namespace
730 //===== Block =================================================================
735 const OUString
& rsLeftText
,
736 const OUString
& rsRightText
,
737 const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
,
738 const sal_Int32 nMaximalWidth
)
739 : maLeft(rsLeftText
, rxFont
, nMaximalWidth
),
740 maRight(rsRightText
, rxFont
, nMaximalWidth
)
745 const css::uno::Reference
<css::rendering::XCanvasFont
>& rxFont
,
746 const sal_Int32 nMaximalWidth
)
748 maLeft
.Update(rxFont
, nMaximalWidth
);
749 maRight
.Update(rxFont
, nMaximalWidth
);
752 } // end of anonymous namespace
754 } } // end of namespace ::sdext::presenter
756 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */