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