tdf#62032 use style list level when changing style
[LibreOffice.git] / sc / inc / postit.hxx
blob4bf519a9fbc6b434e536e5168cda4283c72cfe6d
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #pragma once
22 #include <rtl/ustring.hxx>
23 #include <svl/itemset.hxx>
24 #include "address.hxx"
25 #include "scdllapi.h"
27 #include <memory>
28 #include <optional>
29 #include <string_view>
31 class EditTextObject;
32 class OutlinerParaObject;
33 class SdrCaptionObj;
34 class SdrPage;
36 class ScDocument;
37 namespace tools { class Rectangle; }
38 struct ScCaptionInitData;
40 /** Some desperate attempt to fight against the caption object ownership mess,
41 to which none of shared/weak/plain pointer is a cure.
43 class ScCaptionPtr
45 public:
46 ScCaptionPtr();
47 explicit ScCaptionPtr( SdrCaptionObj* p );
48 ScCaptionPtr( const ScCaptionPtr& r );
49 ScCaptionPtr(ScCaptionPtr&& r) noexcept;
50 ~ScCaptionPtr();
52 ScCaptionPtr& operator=( const ScCaptionPtr& r );
53 ScCaptionPtr& operator=(ScCaptionPtr&& r) noexcept;
54 explicit operator bool() const { return mpCaption != nullptr; }
55 const SdrCaptionObj* get() const { return mpCaption; }
56 SdrCaptionObj* get() { return mpCaption; }
57 const SdrCaptionObj* operator->() const { return mpCaption; }
58 SdrCaptionObj* operator->() { return mpCaption; }
59 const SdrCaptionObj& operator*() const { return *mpCaption; }
60 SdrCaptionObj& operator*() { return *mpCaption; }
62 // Does not default to nullptr to make it visually obvious where such is used.
63 void reset( SdrCaptionObj* p );
65 /** Insert to draw page. The caption object is owned by the draw page then.
67 void insertToDrawPage( SdrPage& rDrawPage );
69 /** Remove from draw page. The caption object is not owned anymore by the
70 draw page then.
72 void removeFromDrawPage( SdrPage& rDrawPage );
74 /** Remove from draw page and free caption object if no Undo recording.
76 void removeFromDrawPageAndFree( bool bIgnoreUndo = false );
78 /** Release all management of the SdrCaptionObj* in all instances of this
79 list and dissolve. The SdrCaptionObj pointer returned is ready to be
80 managed elsewhere.
82 SdrCaptionObj* release();
84 /** Forget the SdrCaptionObj pointer in this one instance.
85 Decrements a use count but does not destroy the object, it's up to the
86 caller to manage this mess...
88 void forget();
90 /** Flag that this instance is in Undo, so drawing layer owns it. */
91 void setNotOwner();
93 oslInterlockedCount getRefs() const;
95 private:
97 struct Head
99 ScCaptionPtr* mpFirst; ///< first in list
100 oslInterlockedCount mnRefs; ///< use count
102 Head() = delete;
103 explicit Head( ScCaptionPtr* );
106 Head* mpHead; ///< points to the "master" entry
107 mutable ScCaptionPtr* mpNext; ///< next in list
108 SdrCaptionObj* mpCaption; ///< the caption object, managed by head master
109 bool mbNotOwner; ///< whether this caption object is owned by something else, e.g. held in Undo
110 /* TODO: can that be moved to Head?
111 * It's unclear when to reset, so
112 * each instance has its own flag.
113 * The last reference count
114 * decrement automatically has the
115 * then current state available.
116 * */
118 void newHead(); //< Allocate a new Head and init.
119 void incRef() const;
120 bool decRef() const; //< @returns <TRUE/> if the last reference was decremented.
121 void decRefAndDestroy(); //< Destroys caption object if the last reference was decremented.
123 /** Remove from current list and close gap.
125 Usually there are only very few instances, so maintaining a doubly
126 linked list isn't worth memory/performance wise and a simple walk does
129 void removeFromList();
131 /** Replace this instance with pNew in a list, if any.
133 Used by move-ctor and move assignment operator.
135 void replaceInList(ScCaptionPtr* pNew) noexcept;
137 /** Dissolve list when the caption object is released or gone. */
138 void dissolve();
140 /** Just clear everything, while dissolving the list. */
141 void clear();
144 /** Internal data for a cell annotation. */
145 struct ScNoteData
147 typedef std::shared_ptr< ScCaptionInitData > ScCaptionInitDataRef;
149 OUString maDate; /// Creation date of the note.
150 OUString maAuthor; /// Author of the note.
151 ScCaptionInitDataRef mxInitData; /// Initial data for invisible notes without SdrObject.
152 ScCaptionPtr mxCaption; /// Drawing object representing the cell note.
153 bool mbShown; /// True = note is visible.
155 explicit ScNoteData( bool bShown = false );
159 * Additional class containing cell annotation data.
161 class SC_DLLPUBLIC ScPostIt
163 public:
164 static sal_uInt32 mnLastPostItId;
166 /** Creates an empty note and its caption object and places it according to
167 the passed cell position. */
168 explicit ScPostIt( ScDocument& rDoc, const ScAddress& rPos, sal_uInt32 nPostItId = 0 );
170 /** Copy constructor. Clones the note and its caption to a new document. */
171 explicit ScPostIt( ScDocument& rDoc, const ScAddress& rPos, const ScPostIt& rNote, sal_uInt32 nPostItId = 0 );
173 /** Creates a note from the passed note data with existing caption object.
175 @param bAlwaysCreateCaption Instead of a pointer to an existing
176 caption object, the passed note data structure may contain a
177 reference to an ScCaptionInitData structure containing information
178 about how to construct a missing caption object. If sal_True is passed,
179 the caption drawing object will be created immediately from that
180 data. If sal_False is passed and the note is not visible, it will
181 continue to cache that data until the caption object is requested.
183 explicit ScPostIt(
184 ScDocument& rDoc, const ScAddress& rPos,
185 const ScNoteData& rNoteData, bool bAlwaysCreateCaption, sal_uInt32 nPostItId = 0 );
187 /** Removes the caption object from drawing layer, if this note is its owner. */
188 ~ScPostIt();
190 /** Clones this note and its caption object, if specified.
192 @param bCloneCaption If sal_True is passed, clones the caption object and
193 inserts it into the drawing layer of the destination document. If
194 sal_False is passed, the cloned note will refer to the old caption
195 object (used e.g. in Undo documents to restore the pointer to the
196 existing caption object).
198 std::unique_ptr<ScPostIt> Clone(
199 const ScAddress& rOwnPos,
200 ScDocument& rDestDoc, const ScAddress& rDestPos,
201 bool bCloneCaption ) const;
203 /** Returns the note id. */
204 sal_uInt32 GetId() const { return mnPostItId; }
206 /** Returns the data struct containing all note settings. */
207 const ScNoteData& GetNoteData() const { return maNoteData;}
209 /** Returns the creation date of this note. */
210 const OUString& GetDate() const { return maNoteData.maDate;}
211 /** Sets a new creation date for this note. */
212 void SetDate( const OUString& rDate );
214 /** Returns the author date of this note. */
215 const OUString& GetAuthor() const { return maNoteData.maAuthor;}
216 /** Sets a new author date for this note. */
217 void SetAuthor( const OUString& rAuthor );
219 /** Sets date and author from system settings. */
220 void AutoStamp();
222 /** Returns the pointer to the current outliner object, or null. */
223 const OutlinerParaObject* GetOutlinerObject() const;
224 /** Returns the pointer to the current edit text object, or null. */
225 const EditTextObject* GetEditTextObject() const;
227 /** Returns the caption text of this note. */
228 OUString GetText() const;
229 /** Returns true, if the caption text of this note contains line breaks. */
230 bool HasMultiLineText() const;
231 /** Changes the caption text of this note. All text formatting will be lost. */
232 void SetText( const ScAddress& rPos, const OUString& rText );
234 /** Returns an existing note caption object. returns null, if the note
235 contains initial caption data needed to construct a caption object.
236 The SdrCaptionObj* returned is still managed by the underlying
237 ScNoteData::ScCaptionPtr and must not be stored elsewhere. */
238 SdrCaptionObj* GetCaption() const { return maNoteData.mxCaption.get();}
239 /** Returns the caption object of this note. Creates the caption object, if
240 the note contains initial caption data instead of the caption.
241 The SdrCaptionObj* returned is still managed by the underlying
242 ScNoteData::ScCaptionPtr and must not be stored elsewhere. */
243 SdrCaptionObj* GetOrCreateCaption( const ScAddress& rPos ) const;
245 /** Forgets the pointer to the note caption object.
247 @param bPreserveData
248 If true then the note text is remembered in maNoteData to be able
249 to later reconstruct a caption from it.
251 void ForgetCaption( bool bPreserveData = false );
253 /** Shows or hides the note caption object. */
254 void ShowCaption( const ScAddress& rPos, bool bShow );
255 /** Returns true, if the caption object is visible. */
256 bool IsCaptionShown() const { return maNoteData.mbShown;}
258 /** Shows or hides the caption temporarily (does not change internal visibility state). */
259 void ShowCaptionTemp( const ScAddress& rPos, bool bShow = true );
261 /** Updates caption position according to position of the passed cell. */
262 void UpdateCaptionPos( const ScAddress& rPos );
264 private:
265 ScPostIt( const ScPostIt& ) = delete;
266 ScPostIt& operator=( const ScPostIt& ) = delete;
268 /** Creates the caption object from initial caption data if existing. */
269 void CreateCaptionFromInitData( const ScAddress& rPos ) const;
270 /** Creates a new caption object at the passed cell position, clones passed existing caption. */
271 void CreateCaption( const ScAddress& rPos, const SdrCaptionObj* pCaption = nullptr );
272 /** Removes the caption object from the drawing layer, if this note is its owner. */
273 void RemoveCaption();
275 private:
276 ScDocument& mrDoc; /// Parent document containing the note.
277 mutable ScNoteData maNoteData; /// Note data with pointer to caption object.
278 sal_uInt32 mnPostItId;
281 class SC_DLLPUBLIC ScNoteUtil
283 public:
285 /** Creates and returns a caption object for a temporary caption. */
286 static ScCaptionPtr CreateTempCaption( ScDocument& rDoc, const ScAddress& rPos,
287 SdrPage& rDrawPage, std::u16string_view rUserText,
288 const tools::Rectangle& rVisRect, bool bTailFront );
290 /** Creates a cell note using the passed caption drawing object.
292 This function is used in import filters to reuse the imported drawing
293 object as note caption object.
295 @param pCaption The drawing object for the cell note. This object MUST
296 be inserted into the document at the correct drawing page already.
297 The underlying ScPostIt::ScNoteData::ScCaptionPtr takes managing
298 ownership of the pointer.
300 @return Pointer to the new cell note object if insertion was
301 successful (i.e. the passed cell position was valid), null
302 otherwise. The Calc document is the owner of the note object. The
303 passed item set and outliner object are deleted automatically if
304 creation of the note was not successful.
306 static ScPostIt* CreateNoteFromCaption(
307 ScDocument& rDoc, const ScAddress& rPos,
308 SdrCaptionObj* pCaption );
310 /** Creates a cell note based on the passed caption object data.
312 This function is used in import filters to use an existing imported
313 item set and outliner object to create a note caption object. For
314 performance reasons, it is possible to specify that the caption drawing
315 object for the cell note is not created yet but the note caches the
316 passed data needed to create the caption object on demand (see
317 parameter bAlwaysCreateCaption).
319 @param pItemSet Pointer to an item set on heap memory containing all
320 formatting attributes of the caption object. This function takes
321 ownership of the passed item set.
323 @param rOutlinerObj An outliner object containing (formatted) text
324 for the caption object.
326 @param rCaptionRect The absolute position and size of the caption
327 object. The rectangle may be empty, in this case the default
328 position and size is used.
330 @return Pointer to the new cell note object if insertion was
331 successful (i.e. the passed cell position was valid), null
332 otherwise. The Calc document is the owner of the note object.
334 static ScPostIt* CreateNoteFromObjectData(
335 ScDocument& rDoc, const ScAddress& rPos,
336 SfxItemSet&& oItemSet,
337 const OutlinerParaObject& rOutlinerObj,
338 const tools::Rectangle& rCaptionRect, bool bShown );
340 /** Creates a cell note based on the passed string and inserts it into the
341 document.
343 @param rNoteText The text used to create the note caption object. Must
344 not be empty.
346 @param bAlwaysCreateCaption If sal_True is passed, the caption drawing
347 object will be created immediately. If sal_False is passed, the caption
348 drawing object will not be created if the note is not visible
349 (bShown = sal_False), but the cell note will cache the passed data.
350 MUST be set to sal_False outside of import filter implementations!
352 @return Pointer to the new cell note object if insertion was
353 successful (i.e. the passed cell position was valid), null
354 otherwise. The Calc document is the owner of the note object.
356 static ScPostIt* CreateNoteFromString(
357 ScDocument& rDoc, const ScAddress& rPos,
358 const OUString& rNoteText, bool bShown,
359 bool bAlwaysCreateCaption, sal_uInt32 nPostItId = 0 );
363 namespace sc {
365 struct NoteEntry
367 ScAddress maPos;
368 const ScPostIt* mpNote;
370 NoteEntry( const ScAddress& rPos, const ScPostIt* pNote );
375 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */