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 const 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 SAL_RET_MAYBENULL
static SwFrameFormat
*
126 getOtherTextBoxFormat(const SwFrameFormat
* pFormat
, sal_uInt16 nType
,
127 const SdrObject
* pObject
= nullptr);
128 /// If we have an associated TextFrame, then return that.
129 SAL_RET_MAYBENULL
static SwFrameFormat
*
130 getOtherTextBoxFormat(css::uno::Reference
<css::drawing::XShape
> const& xShape
);
131 /// If we have an associated TextFrame, then return its XTextFrame.
132 static css::uno::Reference
<css::text::XTextFrame
>
133 getUnoTextFrame(css::uno::Reference
<css::drawing::XShape
> const& xShape
);
134 /// Return the textbox rectangle of a draw shape (in relative twips).
135 static tools::Rectangle
getRelativeTextRectangle(SdrObject
* pShape
);
138 * Is the frame format a text box?
140 * A text box consists of a coupled fly and draw format. Most times you
141 * just want to check for a single type, otherwise you get duplicate results.
143 * @param pFormat: Is this format have a textbox?
145 * @param nType: Expected frame format input type.
146 * Valid types are RES_DRAWFRMFMT and RES_FLYFRMFMT.
148 * @param pObject: If the pFormat has more textboxes than one, like
149 * groupshapes, the textbox what belongs to the given
150 * pObject will be inspected. If this parameter nullptr,
151 * the textbox what belongs to the pObject will only be inspected.
153 static bool isTextBox(const SwFrameFormat
* pFormat
, sal_uInt16 nType
,
154 const SdrObject
* pObject
= nullptr);
156 /// Returns true if the SdrObject has a SwTextFrame otherwise false
157 static bool hasTextFrame(const SdrObject
* pObj
);
159 /// Count number of shapes in the document, excluding TextBoxes.
160 static sal_Int32
getCount(const SwDoc
& rDoc
);
161 /// Count number of shapes on the page, excluding TextBoxes.
162 static sal_Int32
getCount(SdrPage
const* pPage
);
163 /// Get a shape by index, excluding TextBoxes.
165 /// @throws css::lang::IndexOutOfBoundsException
166 static css::uno::Any
getByIndex(SdrPage
const* pPage
, sal_Int32 nIndex
);
167 /// Get the order of the shape, excluding TextBoxes.
168 static sal_Int32
getOrdNum(const SdrObject
* pObject
);
169 /// If pTextBox is a textbox, then set rWrapThrough to the surround of its shape.
170 static void getShapeWrapThrough(const SwFrameFormat
* pTextBox
, bool& rWrapThrough
);
172 /// Saves the current shape -> textbox links in a map, so they can be restored later.
173 static void saveLinks(const sw::FrameFormats
<sw::SpzFrameFormat
*>& rFormats
,
174 std::map
<const SwFrameFormat
*, const SwFrameFormat
*>& rLinks
);
175 /// Undo the effect of saveLinks() + individual resetLink() calls.
176 static void restoreLinks(std::set
<ZSortFly
>& rOld
, std::vector
<SwFrameFormat
*>& rNew
,
177 SavedLink
& rSavedLinks
);
179 /// Calls the method given by pFunc with every textboxes of the group given by pFormat.
180 static void synchronizeGroupTextBoxProperty(bool pFunc(SwFrameFormat
*, SdrObject
*),
181 SwFrameFormat
* pFormat
, SdrObject
* pObj
);
183 /// Collect all textboxes of the group given by the pGroupObj Parameter. Returns with a
184 /// vector filled with the textboxes.
185 static std::vector
<SwFrameFormat
*> CollectTextBoxes(const SdrObject
* pGroupObject
,
186 SwFrameFormat
* pFormat
);
188 // Compares the anchor of the first and second given formats, and decides whether sync needed.
189 static bool isAnchorSyncNeeded(const SwFrameFormat
* pFirst
, const SwFrameFormat
* pSecond
);
191 /// Was the textbox created from a Microsoft import by a paragraph frame property (framePr)
192 static bool TextBoxIsFramePr(const SwFrameFormat
& rFrameFormat
);
195 /// Textboxes are basically textframe + shape pairs. This means one shape has one frame.
196 /// This is not enough for group shapes, because they have only one shape format and
197 /// can have many frame formats. This class provides if there is a group shape for example,
198 /// it can have multiple textboxes.
201 friend class SwTextBoxLockGuard
;
204 struct SwTextBoxElement
206 // The textframe format
207 SwFrameFormat
* m_pTextBoxFormat
;
208 // The Draw object where the textbox belongs to
209 SdrObject
* m_pDrawObject
;
212 // This vector stores the textboxes what belongs to this node
213 std::vector
<SwTextBoxElement
> m_pTextBoxes
;
214 // This is the pointer to the shape format, which has this node
215 // (and the textboxes)
216 SwFrameFormat
* m_pOwnerShapeFormat
;
218 // Prevents oscillating during recursive clone calling.
219 mutable bool m_bIsCloningInProgress
;
221 // Protection against looping
226 SwTextBoxNode() = delete;
229 SwTextBoxNode(SwFrameFormat
* pOwnerShapeFormat
);
233 // default copy ctor is enough
234 SwTextBoxNode(const SwTextBoxNode
&) = default;
236 // This method adds a textbox entry to the shape
238 // pDrawObject: The shape what the textbox be added to.
239 // pNewTextBox: The newly created textbox format what will be added to the shape.
240 void AddTextBox(SdrObject
* pDrawObject
, SwFrameFormat
* pNewTextBox
);
242 // This will remove the textbox entry.
244 // pDrawObject: The shape which have the textbox to be deleted.
245 void DelTextBox(const SdrObject
* pDrawObject
, bool bDelFromDoc
= false);
247 // This will remove the textbox entry.
249 // pTextBox: The textbox what have to be deleted.
250 void DelTextBox(const SwFrameFormat
* pTextBox
, bool bDelFromDoc
= false);
252 // This will return with the frame format of the textbox what belongs
253 // to the given shape (pDrawObject)
254 SwFrameFormat
* GetTextBox(const SdrObject
* pDrawObject
) const;
256 // Clears all textboxes of this node from the doc and also from here.
259 // If this is a group shape, that returns true.
260 bool IsGroupTextBox() const;
262 // This returns with the shape what this class belongs to.
263 SwFrameFormat
* GetOwnerShape() { return m_pOwnerShapeFormat
; };
265 // This will give the current number of textboxes.
266 size_t GetTextBoxCount() const { return m_pTextBoxes
.size(); };
268 // Returns with a const collection of textboxes owned by this node.
269 std::map
<SdrObject
*, SwFrameFormat
*> GetAllTextBoxes() const;
271 // Does the copy, and assign of all textboxes of this node to the given format.
272 // Important: The given format has to be a shape-format, and must have same structure
273 // as the owner shape has. If the structure different, the cloning will be aborted.
274 void Clone(SwDoc
* pDoc
, const SwFormatAnchor
& rNewAnc
, SwFrameFormat
* o_pTarget
, bool bSetAttr
,
275 bool bMakeFrame
) const;
278 void Clone_Impl(SwDoc
* pDoc
, const SwFormatAnchor
& rNewAnc
, SwFrameFormat
* o_pTarget
,
279 const SdrObject
* pSrcObj
, SdrObject
* pDestObj
, bool bSetAttr
,
280 bool bMakeFrame
) const;
283 // Helper class for preventing unwanted sync calls.
284 class SwTextBoxLockGuard
286 SwTextBoxNode
& m_rTextBoxes
;
289 SwTextBoxLockGuard(SwTextBoxNode
& rTextBoxes
)
290 : m_rTextBoxes(rTextBoxes
)
292 m_rTextBoxes
.m_bLock
= true;
295 ~SwTextBoxLockGuard() { m_rTextBoxes
.m_bLock
= false; }
298 #endif // INCLUDED_SW_INC_TEXTBOXHELPER_HXX
300 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */