Cleanup restoring last position
[LibreOffice.git] / sw / inc / textboxhelper.hxx
blobebf329342d8e774361de0c3969290ee8c7772f22
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #ifndef INCLUDED_SW_INC_TEXTBOXHELPER_HXX
11 #define INCLUDED_SW_INC_TEXTBOXHELPER_HXX
13 #include <map>
14 #include <set>
15 #include <vector>
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>
22 #include "swdllapi.h"
24 class SdrPage;
25 class SdrObject;
26 class SfxItemSet;
27 class SwFrameFormat;
28 class SwFrameFormats;
29 class SwFormatAnchor;
30 class SwFormatContent;
31 class SwDoc;
32 namespace tools
34 class Rectangle;
36 class ZSortFly;
37 namespace com::sun::star::drawing
39 class XShape;
41 namespace com::sun::star::text
43 class XTextFrame;
46 /**
47 * A TextBox is a TextFrame, that is tied to a drawinglayer shape.
49 * This class provides helper methods to create, query and maintain such
50 * TextBoxes.
52 class SW_DLLPUBLIC SwTextBoxHelper
54 public:
55 /// Maps a draw format to a fly format.
56 using SavedLink = std::map<const SwFrameFormat*, const SwFrameFormat*>;
57 /// Maps a draw format to content.
58 using SavedContent = std::map<const SwFrameFormat*, SwFormatContent>;
59 /// Create a TextBox for a shape. If the third parameter is true,
60 /// the original text in the shape will be copied to the frame
61 /// The textbox is created for the shape given by the pObject parameter.
62 static void create(SwFrameFormat* pShape, SdrObject* pObject, bool bCopyText = false);
63 /// Sets the given textframe as textbox for the given (group member) shape.
64 static void set(SwFrameFormat* pShape, SdrObject* pObject,
65 css::uno::Reference<css::text::XTextFrame> xNew);
66 /// Destroy a TextBox for a shape. If the format has more textboxes
67 /// like group shapes, it will destroy only that textbox what belongs
68 /// to the given pObject shape.
69 static void destroy(const SwFrameFormat* pShape, const SdrObject* pObject);
70 /// Get interface of a shape's TextBox, if there is any.
71 static css::uno::Any queryInterface(const SwFrameFormat* pShape, const css::uno::Type& rType,
72 SdrObject* pObj);
74 /// Sync property of TextBox with the one of the shape.
75 static void syncProperty(SwFrameFormat* pShape, sal_uInt16 nWID, sal_uInt8 nMemberID,
76 const css::uno::Any& rValue, SdrObject* pObj = nullptr);
77 /// Does the same, but works on properties which lack an sw-specific WID / MemberID.
78 static void syncProperty(SwFrameFormat* pShape, std::u16string_view rPropertyName,
79 const css::uno::Any& rValue, SdrObject* pObj = nullptr);
80 /// Get a property of the underlying TextFrame.
81 static void getProperty(SwFrameFormat const* pShape, sal_uInt16 nWID, sal_uInt8 nMemberID,
82 css::uno::Any& rValue);
83 /// Get a property of the underlying TextFrame.
84 static css::uno::Any getProperty(SwFrameFormat const* pShape, const OUString& rPropName);
86 /// There are two types of enum of anchor type, so this function maps this.
87 static css::text::TextContentAnchorType mapAnchorType(const RndStdIds& rAnchorID);
89 /// Similar to syncProperty(), but used by the internal API (e.g. for UI purposes).
90 static void syncFlyFrameAttr(SwFrameFormat& rShape, SfxItemSet const& rSet, SdrObject* pObj);
92 /// Copy shape attributes to the text frame
93 static void updateTextBoxMargin(SdrObject* pObj);
95 /// Sets the anchor of the associated textframe of the given shape, and
96 /// returns true on success.
97 static bool changeAnchor(SwFrameFormat* pShape, SdrObject* pObj);
99 /// Does the positioning for the associated textframe of the shape, and
100 /// returns true on success.
101 static bool doTextBoxPositioning(SwFrameFormat* pShape, SdrObject* pObj);
103 /// Sets the correct size of textframe depending on the given SdrObject.
104 static bool syncTextBoxSize(SwFrameFormat* pShape, SdrObject* pObj);
106 // Returns true on success. Synchronize z-order of the text frame of the given textbox
107 // by setting it one level higher than the z-order of the shape of the textbox.
108 static bool DoTextBoxZOrderCorrection(SwFrameFormat* pShape, const SdrObject* pObj);
111 * If we have an associated TextFrame, then return that.
113 * If we have more textboxes for this format (group shape), that one will be
114 * returned, what belongs to the pObject.
116 * @param nType Expected frame format type.
117 * Valid types are RES_DRAWFRMFMT and RES_FLYFRMFMT.
119 * @see isTextBox
121 static SwFrameFormat* getOtherTextBoxFormat(const SwFrameFormat* pFormat, sal_uInt16 nType,
122 const SdrObject* pObject = nullptr);
123 /// If we have an associated TextFrame, then return that.
124 static SwFrameFormat*
125 getOtherTextBoxFormat(css::uno::Reference<css::drawing::XShape> const& xShape);
126 /// If we have an associated TextFrame, then return its XTextFrame.
127 static css::uno::Reference<css::text::XTextFrame>
128 getUnoTextFrame(css::uno::Reference<css::drawing::XShape> const& xShape);
129 /// Return the textbox rectangle of a draw shape (in relative twips).
130 static tools::Rectangle getRelativeTextRectangle(SdrObject* pShape);
133 * Is the frame format a text box?
135 * A text box consists of a coupled fly and draw format. Most times you
136 * just want to check for a single type, otherwise you get duplicate results.
138 * @param pFormat: Is this format have a textbox?
140 * @param nType: Expected frame format input type.
141 * Valid types are RES_DRAWFRMFMT and RES_FLYFRMFMT.
143 * @param pObject: If the pFormat has more textboxes than one, like
144 * groupshapes, the textbox what belongs to the given
145 * pObject will be inspected. If this parameter nullptr,
146 * the textbox what belongs to the pObject will only be inspected.
148 static bool isTextBox(const SwFrameFormat* pFormat, sal_uInt16 nType,
149 const SdrObject* pObject = nullptr);
151 /// Returns true if the SdrObject has a SwTextFrame otherwise false
152 static bool hasTextFrame(const SdrObject* pObj);
154 /// Count number of shapes in the document, excluding TextBoxes.
155 static sal_Int32 getCount(const SwDoc& rDoc);
156 /// Count number of shapes on the page, excluding TextBoxes.
157 static sal_Int32 getCount(SdrPage const* pPage);
158 /// Get a shape by index, excluding TextBoxes.
160 /// @throws css::lang::IndexOutOfBoundsException
161 static css::uno::Any getByIndex(SdrPage const* pPage, sal_Int32 nIndex);
162 /// Get the order of the shape, excluding TextBoxes.
163 static sal_Int32 getOrdNum(const SdrObject* pObject);
164 /// If pTextBox is a textbox, then set rWrapThrough to the surround of its shape.
165 static void getShapeWrapThrough(const SwFrameFormat* pTextBox, bool& rWrapThrough);
167 /// Saves the current shape -> textbox links in a map, so they can be restored later.
168 static void saveLinks(const SwFrameFormats& rFormats,
169 std::map<const SwFrameFormat*, const SwFrameFormat*>& rLinks);
170 /// Undo the effect of saveLinks() + individual resetLink() calls.
171 static void restoreLinks(std::set<ZSortFly>& rOld, std::vector<SwFrameFormat*>& rNew,
172 SavedLink& rSavedLinks);
174 /// Calls the method given by pFunc with every textboxes of the group given by pFormat.
175 static void synchronizeGroupTextBoxProperty(bool pFunc(SwFrameFormat*, SdrObject*),
176 SwFrameFormat* pFormat, SdrObject* pObj);
178 /// Collect all textboxes of the group given by the pGroupObj Parameter. Returns with a
179 /// vector filled with the textboxes.
180 static std::vector<SwFrameFormat*> CollectTextBoxes(const SdrObject* pGroupObject,
181 SwFrameFormat* pFormat);
183 // Compares the anchor of the first and second given formats, and decides whether sync needed.
184 static bool isAnchorSyncNeeded(const SwFrameFormat* pFirst, const SwFrameFormat* pSecond);
187 /// Textboxes are basically textframe + shape pairs. This means one shape has one frame.
188 /// This is not enough for group shapes, because they have only one shape format and
189 /// can have many frame formats. This class provides if there is a group shape for example,
190 /// it can have multiple textboxes.
191 class SwTextBoxNode
193 friend class SwTextBoxLockGuard;
195 // One TextBox-entry
196 struct SwTextBoxElement
198 // The textframe format
199 SwFrameFormat* m_pTextBoxFormat;
200 // The Draw object where the textbox belongs to
201 SdrObject* m_pDrawObject;
204 // This vector stores the textboxes what belongs to this node
205 std::vector<SwTextBoxElement> m_pTextBoxes;
206 // This is the pointer to the shape format, which has this node
207 // (and the textboxes)
208 SwFrameFormat* m_pOwnerShapeFormat;
210 // Prevents oscillating during recursive clone calling.
211 mutable bool m_bIsCloningInProgress;
213 // Protection against looping
214 bool m_bLock;
216 public:
217 // Not needed.
218 SwTextBoxNode() = delete;
220 // ctor
221 SwTextBoxNode(SwFrameFormat* pOwnerShapeFormat);
222 // dtor
223 ~SwTextBoxNode();
225 // default copy ctor is enough
226 SwTextBoxNode(const SwTextBoxNode&) = default;
228 // This method adds a textbox entry to the shape
229 // Parameters:
230 // pDrawObject: The shape what the textbox be added to.
231 // pNewTextBox: The newly created textbox format what will be added to the shape.
232 void AddTextBox(SdrObject* pDrawObject, SwFrameFormat* pNewTextBox);
234 // This will remove the textbox entry.
235 // Parameters:
236 // pDrawObject: The shape which have the textbox to be deleted.
237 void DelTextBox(const SdrObject* pDrawObject, bool bDelFromDoc = false);
239 // This will remove the textbox entry.
240 // Parameters:
241 // pTextBox: The textbox what have to be deleted.
242 void DelTextBox(const SwFrameFormat* pTextBox, bool bDelFromDoc = false);
244 // This will return with the frame format of the textbox what belongs
245 // to the given shape (pDrawObject)
246 SwFrameFormat* GetTextBox(const SdrObject* pDrawObject) const;
248 // Clears all textboxes of this node from the doc and also from here.
249 void ClearAll();
251 // If this is a group shape, that returns true.
252 bool IsGroupTextBox() const;
254 // This returns with the shape what this class belongs to.
255 SwFrameFormat* GetOwnerShape() { return m_pOwnerShapeFormat; };
257 // This will give the current number of textboxes.
258 size_t GetTextBoxCount() const { return m_pTextBoxes.size(); };
260 // Returns with a const collection of textboxes owned by this node.
261 std::map<SdrObject*, SwFrameFormat*> GetAllTextBoxes() const;
263 // Does the copy, and assign of all textboxes of this node to the given format.
264 // Important: The given format has to be a shape-format, and must have same structure
265 // as the owner shape has. If the structure different, the cloning will be aborted.
266 void Clone(SwDoc* pDoc, const SwFormatAnchor& rNewAnc, SwFrameFormat* o_pTarget, bool bSetAttr,
267 bool bMakeFrame) const;
269 private:
270 void Clone_Impl(SwDoc* pDoc, const SwFormatAnchor& rNewAnc, SwFrameFormat* o_pTarget,
271 const SdrObject* pSrcObj, SdrObject* pDestObj, bool bSetAttr,
272 bool bMakeFrame) const;
275 // Helper class for preventing unwanted sync calls.
276 class SwTextBoxLockGuard
278 SwTextBoxNode& m_rTextBoxes;
280 public:
281 SwTextBoxLockGuard(SwTextBoxNode& rTextBoxes)
282 : m_rTextBoxes(rTextBoxes)
284 m_rTextBoxes.m_bLock = true;
287 ~SwTextBoxLockGuard() { m_rTextBoxes.m_bLock = false; }
290 #endif // INCLUDED_SW_INC_TEXTBOXHELPER_HXX
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */