1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef SDEXT_PRESENTER_TEXT_VIEW_HXX
29*cdf0e10cSrcweir #define SDEXT_PRESENTER_TEXT_VIEW_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "PresenterTheme.hxx"
32*cdf0e10cSrcweir #include <com/sun/star/accessibility/XAccessibleText.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/awt/Size.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/geometry/RealPoint2D.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/geometry/RealSize2D.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/i18n/XBreakIterator.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/i18n/XScriptTypeDetector.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/rendering/XCanvas.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/style/ParagraphAdjust.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/text/XText.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
42*cdf0e10cSrcweir #include <cppuhelper/compbase1.hxx>
43*cdf0e10cSrcweir #include <cppuhelper/basemutex.hxx>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir namespace css = ::com::sun::star;
46*cdf0e10cSrcweir namespace cssu = ::com::sun::star::uno;
47*cdf0e10cSrcweir namespace cssa = ::com::sun::star::accessibility;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir namespace sdext { namespace presenter {
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir class PresenterTextCaret
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir public:
54*cdf0e10cSrcweir     PresenterTextCaret (
55*cdf0e10cSrcweir         const ::boost::function<css::awt::Rectangle(const sal_Int32,const sal_Int32)>&
56*cdf0e10cSrcweir             rCharacterBoundsAccess,
57*cdf0e10cSrcweir         const ::boost::function<void(const css::awt::Rectangle&)>&
58*cdf0e10cSrcweir             rInvalidator);
59*cdf0e10cSrcweir     ~PresenterTextCaret (void);
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir     void ShowCaret (void);
62*cdf0e10cSrcweir     void HideCaret (void);
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir     sal_Int32 GetParagraphIndex (void) const;
65*cdf0e10cSrcweir     sal_Int32 GetCharacterIndex (void) const;
66*cdf0e10cSrcweir     void SetPosition (
67*cdf0e10cSrcweir         const sal_Int32 nParagraphIndex,
68*cdf0e10cSrcweir         const sal_Int32 nCharacterIndex);
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir     bool IsVisible (void) const;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     /** Set a (possibly empty) functor that broadcasts changes of the caret
73*cdf0e10cSrcweir         position.  This is used when a PresenterTextView object is set at
74*cdf0e10cSrcweir         the accessibility object so that accessibility events can be sent
75*cdf0e10cSrcweir         when the caret changes position.
76*cdf0e10cSrcweir     */
77*cdf0e10cSrcweir     void SetCaretMotionBroadcaster (
78*cdf0e10cSrcweir         const ::boost::function<void(sal_Int32,sal_Int32,sal_Int32,sal_Int32)>& rBroadcaster);
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir     css::awt::Rectangle GetBounds (void) const;
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir private:
83*cdf0e10cSrcweir     sal_Int32 mnParagraphIndex;
84*cdf0e10cSrcweir     sal_Int32 mnCharacterIndex;
85*cdf0e10cSrcweir     sal_Int32 mnCaretBlinkTaskId;
86*cdf0e10cSrcweir     bool mbIsCaretVisible;
87*cdf0e10cSrcweir     const ::boost::function<css::awt::Rectangle(const sal_Int32,const sal_Int32)> maCharacterBoundsAccess;
88*cdf0e10cSrcweir     const ::boost::function<void(const css::awt::Rectangle&)> maInvalidator;
89*cdf0e10cSrcweir     ::boost::function<void(sal_Int32,sal_Int32,sal_Int32,sal_Int32)> maBroadcaster;
90*cdf0e10cSrcweir     css::awt::Rectangle maCaretBounds;
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     void InvertCaret (void);
93*cdf0e10cSrcweir };
94*cdf0e10cSrcweir typedef ::boost::shared_ptr<PresenterTextCaret> SharedPresenterTextCaret;
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir //===== PresenterTextParagraph ================================================
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir class PresenterTextParagraph
102*cdf0e10cSrcweir {
103*cdf0e10cSrcweir public:
104*cdf0e10cSrcweir     PresenterTextParagraph (
105*cdf0e10cSrcweir         const sal_Int32 nParagraphIndex,
106*cdf0e10cSrcweir         const cssu::Reference<css::i18n::XBreakIterator>& rxBreakIterator,
107*cdf0e10cSrcweir         const cssu::Reference<css::i18n::XScriptTypeDetector>& rxScriptTypeDetector,
108*cdf0e10cSrcweir         const cssu::Reference<css::text::XTextRange>& rxTextRange,
109*cdf0e10cSrcweir         const SharedPresenterTextCaret& rpCaret);
110*cdf0e10cSrcweir     PresenterTextParagraph (
111*cdf0e10cSrcweir         const sal_Int32 nParagraphIndex,
112*cdf0e10cSrcweir         const cssu::Reference<css::i18n::XBreakIterator>& rxBreakIterator,
113*cdf0e10cSrcweir         const cssu::Reference<css::i18n::XScriptTypeDetector>& rxScriptTypeDetector,
114*cdf0e10cSrcweir         const ::rtl::OUString& rsText,
115*cdf0e10cSrcweir         const SharedPresenterTextCaret& rpCaret);
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir     void Paint (
118*cdf0e10cSrcweir         const cssu::Reference<css::rendering::XCanvas>& rxCanvas,
119*cdf0e10cSrcweir         const css::geometry::RealSize2D& rSize,
120*cdf0e10cSrcweir         const PresenterTheme::SharedFontDescriptor& rpFont,
121*cdf0e10cSrcweir         const css::rendering::ViewState& rViewState,
122*cdf0e10cSrcweir         css::rendering::RenderState& rRenderState,
123*cdf0e10cSrcweir         const double nTopOffset,
124*cdf0e10cSrcweir         const double nClipTop,
125*cdf0e10cSrcweir         const double nClipBottom);
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir     sal_Int32 GetParagraphIndex (void) const;
128*cdf0e10cSrcweir     double GetTotalTextHeight (void);
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir     sal_Int32 GetCharacterOffset (void) const;
131*cdf0e10cSrcweir     void SetCharacterOffset (const sal_Int32 nCharacterOffset);
132*cdf0e10cSrcweir     sal_Int32 GetCharacterCount (void) const;
133*cdf0e10cSrcweir     sal_Unicode GetCharacter (const sal_Int32 nGlobalCharacterIndex) const;
134*cdf0e10cSrcweir     ::rtl::OUString GetText (void) const;
135*cdf0e10cSrcweir     cssa::TextSegment GetTextSegment (
136*cdf0e10cSrcweir         const sal_Int32 nOffset,
137*cdf0e10cSrcweir         const sal_Int32 nGlobalCharacterIndex,
138*cdf0e10cSrcweir         const sal_Int16 nTextType) const;
139*cdf0e10cSrcweir     cssa::TextSegment GetWordTextSegment (
140*cdf0e10cSrcweir         const sal_Int32 nOffset,
141*cdf0e10cSrcweir         const sal_Int32 nIndex) const;
142*cdf0e10cSrcweir     cssa::TextSegment CreateTextSegment (
143*cdf0e10cSrcweir         sal_Int32 nStartIndex,
144*cdf0e10cSrcweir         sal_Int32 nEndIndex) const;
145*cdf0e10cSrcweir     css::awt::Rectangle GetCharacterBounds (
146*cdf0e10cSrcweir         sal_Int32 nGlobalCharacterIndex,
147*cdf0e10cSrcweir         const bool bCaretBox);
148*cdf0e10cSrcweir     sal_Int32 GetIndexAtPoint (const css::awt::Point& rPoint) const;
149*cdf0e10cSrcweir     void SetupCellArray (
150*cdf0e10cSrcweir         const PresenterTheme::SharedFontDescriptor& rpFont);
151*cdf0e10cSrcweir     void Format (
152*cdf0e10cSrcweir         const double nY,
153*cdf0e10cSrcweir         const double nWidth,
154*cdf0e10cSrcweir         const PresenterTheme::SharedFontDescriptor& rpFont);
155*cdf0e10cSrcweir     sal_Int32 GetWordBoundary(
156*cdf0e10cSrcweir         const sal_Int32 nLocalCharacterIndex,
157*cdf0e10cSrcweir         const sal_Int32 nDistance);
158*cdf0e10cSrcweir     sal_Int32 GetCaretPosition (void) const;
159*cdf0e10cSrcweir     void SetCaretPosition (const sal_Int32 nPosition) const;
160*cdf0e10cSrcweir     void SetOrigin (const double nXOrigin, const double nYOrigin);
161*cdf0e10cSrcweir     css::awt::Point GetRelativeLocation (void) const;
162*cdf0e10cSrcweir     css::awt::Size GetSize (void);
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir private:
165*cdf0e10cSrcweir     ::rtl::OUString msParagraphText;
166*cdf0e10cSrcweir     const sal_Int32 mnParagraphIndex;
167*cdf0e10cSrcweir     SharedPresenterTextCaret mpCaret;
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir     /** A portion of a string that encodes one unicode cell.  It describes
170*cdf0e10cSrcweir         number of characters in the unicode string that make up the cell and its
171*cdf0e10cSrcweir         width in pixel (with respect to some configuration that is stored
172*cdf0e10cSrcweir         externally or implicitly).
173*cdf0e10cSrcweir     */
174*cdf0e10cSrcweir     class Cell
175*cdf0e10cSrcweir     {
176*cdf0e10cSrcweir     public:
177*cdf0e10cSrcweir         Cell (const sal_Int32 nCharacterIndex, const sal_Int32 nCharacterCount, const double nCellWidth);
178*cdf0e10cSrcweir         sal_Int32 mnCharacterIndex;
179*cdf0e10cSrcweir         sal_Int32 mnCharacterCount;
180*cdf0e10cSrcweir         double mnCellWidth;
181*cdf0e10cSrcweir     };
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir     class Line
184*cdf0e10cSrcweir     {
185*cdf0e10cSrcweir     public:
186*cdf0e10cSrcweir         Line (const sal_Int32 nLineStartCharacterIndex, const sal_Int32 nLineEndCharacterIndex);
187*cdf0e10cSrcweir         sal_Int32 mnLineStartCharacterIndex;
188*cdf0e10cSrcweir         sal_Int32 mnLineEndCharacterIndex;
189*cdf0e10cSrcweir         sal_Int32 mnLineStartCellIndex;
190*cdf0e10cSrcweir         sal_Int32 mnLineEndCellIndex;
191*cdf0e10cSrcweir         cssu::Reference<css::rendering::XTextLayout> mxLayoutedLine;
192*cdf0e10cSrcweir         double mnBaseLine;
193*cdf0e10cSrcweir         double mnWidth;
194*cdf0e10cSrcweir         cssu::Sequence<css::geometry::RealRectangle2D> maCellBoxes;
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir         sal_Int32 GetLength (void) const;
197*cdf0e10cSrcweir         void ProvideLayoutedLine (
198*cdf0e10cSrcweir             const ::rtl::OUString& rsParagraphText,
199*cdf0e10cSrcweir             const PresenterTheme::SharedFontDescriptor& rpFont,
200*cdf0e10cSrcweir             const sal_Int8 nTextDirection);
201*cdf0e10cSrcweir         void ProvideCellBoxes (void);
202*cdf0e10cSrcweir         bool IsEmpty (void) const;
203*cdf0e10cSrcweir     };
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir     cssu::Reference<css::i18n::XBreakIterator> mxBreakIterator;
207*cdf0e10cSrcweir     cssu::Reference<css::i18n::XScriptTypeDetector> mxScriptTypeDetector;
208*cdf0e10cSrcweir     ::std::vector<Line> maLines;
209*cdf0e10cSrcweir     ::std::vector<sal_Int32> maWordBoundaries;
210*cdf0e10cSrcweir     // Offset of the top of the paragraph with respect to the origin of the
211*cdf0e10cSrcweir     // whole text (specified by mnXOrigin and mnYOrigin).
212*cdf0e10cSrcweir     double mnVerticalOffset;
213*cdf0e10cSrcweir     double mnXOrigin;
214*cdf0e10cSrcweir     double mnYOrigin;
215*cdf0e10cSrcweir     double mnWidth;
216*cdf0e10cSrcweir     double mnAscent;
217*cdf0e10cSrcweir     double mnDescent;
218*cdf0e10cSrcweir     double mnLineHeight;
219*cdf0e10cSrcweir     css::style::ParagraphAdjust meAdjust;
220*cdf0e10cSrcweir     sal_Int8 mnWritingMode;
221*cdf0e10cSrcweir     /// The index of the first character in this paragraph with respect to
222*cdf0e10cSrcweir     /// the whole text.
223*cdf0e10cSrcweir     sal_Int32 mnCharacterOffset;
224*cdf0e10cSrcweir     ::std::vector<Cell> maCells;
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir     void AddWord (
227*cdf0e10cSrcweir         const double nWidth,
228*cdf0e10cSrcweir         css::i18n::Boundary& rCurrentLine,
229*cdf0e10cSrcweir         const sal_Int32 nWordBoundary,
230*cdf0e10cSrcweir         const PresenterTheme::SharedFontDescriptor& rpFont);
231*cdf0e10cSrcweir     void AddLine (
232*cdf0e10cSrcweir         css::i18n::Boundary& rCurrentLine);
233*cdf0e10cSrcweir     sal_Int8 GetTextDirection (void) const;
234*cdf0e10cSrcweir     bool IsTextReferencePointLeft (void) const;
235*cdf0e10cSrcweir };
236*cdf0e10cSrcweir typedef ::boost::shared_ptr<PresenterTextParagraph> SharedPresenterTextParagraph;
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir /** A simple text view that paints text onto a given canvas.
242*cdf0e10cSrcweir */
243*cdf0e10cSrcweir class PresenterTextView
244*cdf0e10cSrcweir {
245*cdf0e10cSrcweir public:
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir     PresenterTextView (
248*cdf0e10cSrcweir         const css::uno::Reference<css::uno::XComponentContext>& rxContext,
249*cdf0e10cSrcweir         const css::uno::Reference<css::rendering::XCanvas>& rxCanvas,
250*cdf0e10cSrcweir         const ::boost::function<void(const ::css::awt::Rectangle&)>& rInvalidator);
251*cdf0e10cSrcweir     /** Create a new instance that does no output but only provides
252*cdf0e10cSrcweir         geometric information to an accessibility object.
253*cdf0e10cSrcweir     */
254*cdf0e10cSrcweir     PresenterTextView (
255*cdf0e10cSrcweir         const css::uno::Reference<css::uno::XComponentContext>& rxContext,
256*cdf0e10cSrcweir         const css::uno::Reference<css::rendering::XCanvas>& rxCanvas);
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir     void SetText (const css::uno::Reference<css::text::XText>& rxText);
259*cdf0e10cSrcweir     void SetText (const ::rtl::OUString& rsText);
260*cdf0e10cSrcweir     void SetTextChangeBroadcaster (const ::boost::function<void(void)>& rBroadcaster);
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir     void SetLocation (const css::geometry::RealPoint2D& rLocation);
263*cdf0e10cSrcweir     void SetSize (const css::geometry::RealSize2D& rSize);
264*cdf0e10cSrcweir     double GetTotalTextHeight (void);
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir     void SetFont (const PresenterTheme::SharedFontDescriptor& rpFont);
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir     void SetOffset (
269*cdf0e10cSrcweir         const double nLeft,
270*cdf0e10cSrcweir         const double nTop);
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir     /** Move the caret forward or backward by character or by word.
273*cdf0e10cSrcweir         @param nDistance
274*cdf0e10cSrcweir             Should be either -1 or +1 to move caret backwards or forwards,
275*cdf0e10cSrcweir             respectively.
276*cdf0e10cSrcweir         @param nTextType
277*cdf0e10cSrcweir             Valid values are the
278*cdf0e10cSrcweir             com::sun::star::accessibility::AccessibleTextType constants.
279*cdf0e10cSrcweir     */
280*cdf0e10cSrcweir     void MoveCaret (
281*cdf0e10cSrcweir         const sal_Int32 nDistance,
282*cdf0e10cSrcweir         const sal_Int16 nTextType);
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir     void Paint (const css::awt::Rectangle& rUpdateBox);
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir     SharedPresenterTextCaret GetCaret (void) const;
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir     sal_Int32 GetParagraphCount (void) const;
289*cdf0e10cSrcweir     SharedPresenterTextParagraph GetParagraph (const sal_Int32 nParagraphIndex) const;
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir private:
292*cdf0e10cSrcweir     css::uno::Reference<css::rendering::XCanvas> mxCanvas;
293*cdf0e10cSrcweir     bool mbDoOuput;
294*cdf0e10cSrcweir     css::uno::Reference<css::i18n::XBreakIterator> mxBreakIterator;
295*cdf0e10cSrcweir     css::uno::Reference<css::i18n::XScriptTypeDetector> mxScriptTypeDetector;
296*cdf0e10cSrcweir     css::geometry::RealPoint2D maLocation;
297*cdf0e10cSrcweir     css::geometry::RealSize2D maSize;
298*cdf0e10cSrcweir     PresenterTheme::SharedFontDescriptor mpFont;
299*cdf0e10cSrcweir     ::std::vector<SharedPresenterTextParagraph> maParagraphs;
300*cdf0e10cSrcweir     SharedPresenterTextCaret mpCaret;
301*cdf0e10cSrcweir     double mnLeftOffset;
302*cdf0e10cSrcweir     double mnTopOffset;
303*cdf0e10cSrcweir     const ::boost::function<void(const ::css::awt::Rectangle&)> maInvalidator;
304*cdf0e10cSrcweir     bool mbIsFormatPending;
305*cdf0e10cSrcweir     sal_Int32 mnCharacterCount;
306*cdf0e10cSrcweir     ::boost::function<void(void)> maTextChangeBroadcaster;
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir     void RequestFormat (void);
309*cdf0e10cSrcweir     void Format (void);
310*cdf0e10cSrcweir     SharedPresenterTextParagraph GetParagraphForCharacterIndex (const sal_Int32 nCharacterIndex) const;
311*cdf0e10cSrcweir     sal_Int32 GetCharacterOffset (const sal_Int32 nParagraphIndex) const;
312*cdf0e10cSrcweir     css::awt::Rectangle GetCaretBounds (
313*cdf0e10cSrcweir         const sal_Int32 nParagraphIndex,
314*cdf0e10cSrcweir         const sal_Int32 nCharacterIndex) const;
315*cdf0e10cSrcweir };
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir } } // end of namespace ::sdext::presenter
318*cdf0e10cSrcweir 
319*cdf0e10cSrcweir #endif
320