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/.
10 #ifndef INCLUDED_SW_INC_TEXTBOXHELPER_HXX
11 #define INCLUDED_SW_INC_TEXTBOXHELPER_HXX
17 #include <com/sun/star/uno/Any.hxx>
18 #include <com/sun/star/uno/Type.h>
19 #include <com/sun/star/text/TextContentAnchorType.hpp>
20 #include <svx/swframetypes.hxx>
29 class SwFormatContent
;
36 namespace com::sun::star::drawing
40 namespace com::sun::star::text
46 template <class T
> class FrameFormats
;
51 * A TextBox is a TextFrame, that is tied to a drawinglayer shape.
53 * This class provides helper methods to create, query and maintain such
56 class SW_DLLPUBLIC SwTextBoxHelper
59 /// Maps a draw format to a fly format.
60 using SavedLink
= std::map
<const SwFrameFormat
*, const SwFrameFormat
*>;
61 /// Maps a draw format to content.
62 using SavedContent
= std::map
<const SwFrameFormat
*, SwFormatContent
>;
63 /// Create a TextBox for a shape. If the third parameter is true,
64 /// the original text in the shape will be copied to the frame
65 /// The textbox is created for the shape given by the pObject parameter.
66 static void create(SwFrameFormat
* pShape
, SdrObject
* pObject
, bool bCopyText
= false);
67 /// Sets the given textframe as textbox for the given (group member) shape.
68 static void set(SwFrameFormat
* pShape
, SdrObject
* pObject
,
69 css::uno::Reference
<css::text::XTextFrame
> xNew
);
70 /// Destroy a TextBox for a shape. If the format has more textboxes
71 /// like group shapes, it will destroy only that textbox what belongs
72 /// to the given pObject shape.
73 static void destroy(const SwFrameFormat
* pShape
, const SdrObject
* pObject
);
74 /// Get interface of a shape's TextBox, if there is any.
75 static css::uno::Any
queryInterface(const SwFrameFormat
* pShape
, const css::uno::Type
& rType
,
78 /// Sync property of TextBox with the one of the shape.
79 static void syncProperty(SwFrameFormat
* pShape
, sal_uInt16 nWID
, sal_uInt8 nMemberID
,
80 const css::uno::Any
& rValue
, SdrObject
* pObj
= nullptr);
81 /// Does the same, but works on properties which lack an sw-specific WID / MemberID.
82 static void syncProperty(SwFrameFormat
* pShape
, std::u16string_view rPropertyName
,
83 const css::uno::Any
& rValue
, SdrObject
* pObj
= nullptr);
84 /// Get a property of the underlying TextFrame.
85 static void getProperty(SwFrameFormat
const* pShape
, sal_uInt16 nWID
, sal_uInt8 nMemberID
,
86 css::uno::Any
& rValue
);
87 /// Get a property of the underlying TextFrame.
88 static css::uno::Any
getProperty(SwFrameFormat
const* pShape
, const OUString
& rPropName
);
90 /// There are two types of enum of anchor type, so this function maps this.
91 static css::text::TextContentAnchorType
mapAnchorType(const RndStdIds
& rAnchorID
);
93 /// Similar to syncProperty(), but used by the internal API (e.g. for UI purposes).
94 static void syncFlyFrameAttr(SwFrameFormat
& rShape
, SfxItemSet
const& rSet
, SdrObject
* pObj
);
96 /// Copy shape attributes to the text frame
97 static void updateTextBoxMargin(SdrObject
* pObj
);
99 /// Sets the anchor of the associated textframe of the given shape, and
100 /// returns true on success.
101 static bool changeAnchor(SwFrameFormat
* pShape
, SdrObject
* pObj
);
103 /// Does the positioning for the associated textframe of the shape, and
104 /// returns true on success.
105 static bool doTextBoxPositioning(SwFrameFormat
* pShape
, SdrObject
* pObj
);
107 /// Sets the correct size of textframe depending on the given SdrObject.
108 static bool syncTextBoxSize(SwFrameFormat
* pShape
, SdrObject
* pObj
);
110 // Returns true on success. Synchronize z-order of the text frame of the given textbox
111 // by setting it one level higher than the z-order of the shape of the textbox.
112 static bool DoTextBoxZOrderCorrection(SwFrameFormat
* pShape
, const SdrObject
* pObj
);
115 * If we have an associated TextFrame, then return that.
117 * If we have more textboxes for this format (group shape), that one will be
118 * returned, what belongs to the pObject.
120 * @param nType Expected frame format type.
121 * Valid types are RES_DRAWFRMFMT and RES_FLYFRMFMT.
125 static SwFrameFormat
* getOtherTextBoxFormat(const SwFrameFormat
* pFormat
, sal_uInt16 nType
,
126 const SdrObject
* pObject
= nullptr);
127 /// If we have an associated TextFrame, then return that.
128 static SwFrameFormat
*
129 getOtherTextBoxFormat(css::uno::Reference
<css::drawing::XShape
> const& xShape
);
130 /// If we have an associated TextFrame, then return its XTextFrame.
131 static css::uno::Reference
<css::text::XTextFrame
>
132 getUnoTextFrame(css::uno::Reference
<css::drawing::XShape
> const& xShape
);
133 /// Return the textbox rectangle of a draw shape (in relative twips).
134 static tools::Rectangle
getRelativeTextRectangle(SdrObject
* pShape
);
137 * Is the frame format a text box?
139 * A text box consists of a coupled fly and draw format. Most times you
140 * just want to check for a single type, otherwise you get duplicate results.
142 * @param pFormat: Is this format have a textbox?
144 * @param nType: Expected frame format input type.
145 * Valid types are RES_DRAWFRMFMT and RES_FLYFRMFMT.
147 * @param pObject: If the pFormat has more textboxes than one, like
148 * groupshapes, the textbox what belongs to the given
149 * pObject will be inspected. If this parameter nullptr,
150 * the textbox what belongs to the pObject will only be inspected.
152 static bool isTextBox(const SwFrameFormat
* pFormat
, sal_uInt16 nType
,
153 const SdrObject
* pObject
= nullptr);
155 /// Returns true if the SdrObject has a SwTextFrame otherwise false
156 static bool hasTextFrame(const SdrObject
* pObj
);
158 /// Count number of shapes in the document, excluding TextBoxes.
159 static sal_Int32
getCount(const SwDoc
& rDoc
);
160 /// Count number of shapes on the page, excluding TextBoxes.
161 static sal_Int32
getCount(SdrPage
const* pPage
);
162 /// Get a shape by index, excluding TextBoxes.
164 /// @throws css::lang::IndexOutOfBoundsException
165 static css::uno::Any
getByIndex(SdrPage
const* pPage
, sal_Int32 nIndex
);
166 /// Get the order of the shape, excluding TextBoxes.
167 static sal_Int32
getOrdNum(const SdrObject
* pObject
);
168 /// If pTextBox is a textbox, then set rWrapThrough to the surround of its shape.
169 static void getShapeWrapThrough(const SwFrameFormat
* pTextBox
, bool& rWrapThrough
);
171 /// Saves the current shape -> textbox links in a map, so they can be restored later.
172 static void saveLinks(const sw::FrameFormats
<sw::SpzFrameFormat
*>& rFormats
,
173 std::map
<const SwFrameFormat
*, const SwFrameFormat
*>& rLinks
);
174 /// Undo the effect of saveLinks() + individual resetLink() calls.
175 static void restoreLinks(std::set
<ZSortFly
>& rOld
, std::vector
<SwFrameFormat
*>& rNew
,
176 SavedLink
& rSavedLinks
);
178 /// Calls the method given by pFunc with every textboxes of the group given by pFormat.
179 static void synchronizeGroupTextBoxProperty(bool pFunc(SwFrameFormat
*, SdrObject
*),
180 SwFrameFormat
* pFormat
, SdrObject
* pObj
);
182 /// Collect all textboxes of the group given by the pGroupObj Parameter. Returns with a
183 /// vector filled with the textboxes.
184 static std::vector
<SwFrameFormat
*> CollectTextBoxes(const SdrObject
* pGroupObject
,
185 SwFrameFormat
* pFormat
);
187 // Compares the anchor of the first and second given formats, and decides whether sync needed.
188 static bool isAnchorSyncNeeded(const SwFrameFormat
* pFirst
, const SwFrameFormat
* pSecond
);
191 /// Textboxes are basically textframe + shape pairs. This means one shape has one frame.
192 /// This is not enough for group shapes, because they have only one shape format and
193 /// can have many frame formats. This class provides if there is a group shape for example,
194 /// it can have multiple textboxes.
197 friend class SwTextBoxLockGuard
;
200 struct SwTextBoxElement
202 // The textframe format
203 SwFrameFormat
* m_pTextBoxFormat
;
204 // The Draw object where the textbox belongs to
205 SdrObject
* m_pDrawObject
;
208 // This vector stores the textboxes what belongs to this node
209 std::vector
<SwTextBoxElement
> m_pTextBoxes
;
210 // This is the pointer to the shape format, which has this node
211 // (and the textboxes)
212 SwFrameFormat
* m_pOwnerShapeFormat
;
214 // Prevents oscillating during recursive clone calling.
215 mutable bool m_bIsCloningInProgress
;
217 // Protection against looping
222 SwTextBoxNode() = delete;
225 SwTextBoxNode(SwFrameFormat
* pOwnerShapeFormat
);
229 // default copy ctor is enough
230 SwTextBoxNode(const SwTextBoxNode
&) = default;
232 // This method adds a textbox entry to the shape
234 // pDrawObject: The shape what the textbox be added to.
235 // pNewTextBox: The newly created textbox format what will be added to the shape.
236 void AddTextBox(SdrObject
* pDrawObject
, SwFrameFormat
* pNewTextBox
);
238 // This will remove the textbox entry.
240 // pDrawObject: The shape which have the textbox to be deleted.
241 void DelTextBox(const SdrObject
* pDrawObject
, bool bDelFromDoc
= false);
243 // This will remove the textbox entry.
245 // pTextBox: The textbox what have to be deleted.
246 void DelTextBox(const SwFrameFormat
* pTextBox
, bool bDelFromDoc
= false);
248 // This will return with the frame format of the textbox what belongs
249 // to the given shape (pDrawObject)
250 SwFrameFormat
* GetTextBox(const SdrObject
* pDrawObject
) const;
252 // Clears all textboxes of this node from the doc and also from here.
255 // If this is a group shape, that returns true.
256 bool IsGroupTextBox() const;
258 // This returns with the shape what this class belongs to.
259 SwFrameFormat
* GetOwnerShape() { return m_pOwnerShapeFormat
; };
261 // This will give the current number of textboxes.
262 size_t GetTextBoxCount() const { return m_pTextBoxes
.size(); };
264 // Returns with a const collection of textboxes owned by this node.
265 std::map
<SdrObject
*, SwFrameFormat
*> GetAllTextBoxes() const;
267 // Does the copy, and assign of all textboxes of this node to the given format.
268 // Important: The given format has to be a shape-format, and must have same structure
269 // as the owner shape has. If the structure different, the cloning will be aborted.
270 void Clone(SwDoc
* pDoc
, const SwFormatAnchor
& rNewAnc
, SwFrameFormat
* o_pTarget
, bool bSetAttr
,
271 bool bMakeFrame
) const;
274 void Clone_Impl(SwDoc
* pDoc
, const SwFormatAnchor
& rNewAnc
, SwFrameFormat
* o_pTarget
,
275 const SdrObject
* pSrcObj
, SdrObject
* pDestObj
, bool bSetAttr
,
276 bool bMakeFrame
) const;
279 // Helper class for preventing unwanted sync calls.
280 class SwTextBoxLockGuard
282 SwTextBoxNode
& m_rTextBoxes
;
285 SwTextBoxLockGuard(SwTextBoxNode
& rTextBoxes
)
286 : m_rTextBoxes(rTextBoxes
)
288 m_rTextBoxes
.m_bLock
= true;
291 ~SwTextBoxLockGuard() { m_rTextBoxes
.m_bLock
= false; }
294 #endif // INCLUDED_SW_INC_TEXTBOXHELPER_HXX
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */