tdf#125800: accessing cond format props in UNO throws error
[LibreOffice.git] / sc / inc / postit.hxx
blob11c5d9b5f84b1b067531862eb1257dfab1a47277
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 #ifndef INCLUDED_SC_INC_POSTIT_HXX
21 #define INCLUDED_SC_INC_POSTIT_HXX
23 #include <rtl/ustring.hxx>
24 #include "address.hxx"
25 #include "scdllapi.h"
27 #include <memory>
29 class EditTextObject;
30 class OutlinerParaObject;
31 class SdrCaptionObj;
32 class SdrPage;
34 class SfxItemSet;
35 class ScDocument;
36 namespace tools { class Rectangle; }
37 struct ScCaptionInitData;
39 /** Some desperate attempt to fight against the caption object ownership mess,
40 to which none of shared/weak/plain pointer is a cure.
42 class ScCaptionPtr
44 public:
45 ScCaptionPtr();
46 explicit ScCaptionPtr( SdrCaptionObj* p );
47 ScCaptionPtr( const ScCaptionPtr& r );
48 ScCaptionPtr( ScCaptionPtr&& r );
49 ~ScCaptionPtr();
51 ScCaptionPtr& operator=( const ScCaptionPtr& r );
52 ScCaptionPtr& operator=( ScCaptionPtr&& r );
53 explicit operator bool() const { return mpCaption != nullptr; }
54 const SdrCaptionObj* get() const { return mpCaption; }
55 SdrCaptionObj* get() { return mpCaption; }
56 const SdrCaptionObj* operator->() const { return mpCaption; }
57 SdrCaptionObj* operator->() { return mpCaption; }
58 const SdrCaptionObj& operator*() const { return *mpCaption; }
59 SdrCaptionObj& operator*() { return *mpCaption; }
61 // Does not default to nullptr to make it visually obvious where such is used.
62 void reset( SdrCaptionObj* p );
64 /** Insert to draw page. The caption object is owned by the draw page then.
66 void insertToDrawPage( SdrPage& rDrawPage );
68 /** Remove from draw page. The caption object is not owned anymore by the
69 draw page then.
71 void removeFromDrawPage( SdrPage& rDrawPage );
73 /** Remove from draw page and free caption object if no Undo recording.
75 void removeFromDrawPageAndFree( bool bIgnoreUndo = false );
77 /** Release all management of the SdrCaptionObj* in all instances of this
78 list and dissolve. The SdrCaptionObj pointer returned is ready to be
79 managed elsewhere.
81 SdrCaptionObj* release();
83 /** Forget the SdrCaptionObj pointer in this one instance.
84 Decrements a use count but does not destroy the object, it's up to the
85 caller to manage this mess..
87 void forget();
89 /** Flag that this instance is in Undo, so drawing layer owns it. */
90 void setNotOwner();
92 oslInterlockedCount getRefs() const;
94 private:
96 struct Head
98 ScCaptionPtr* mpFirst; ///< first in list
99 oslInterlockedCount mnRefs; ///< use count
101 Head() = delete;
102 explicit Head( ScCaptionPtr* );
105 Head* mpHead; ///< points to the "master" entry
106 mutable ScCaptionPtr* mpNext; ///< next in list
107 SdrCaptionObj* mpCaption; ///< the caption object, managed by head master
108 bool mbNotOwner; ///< whether this caption object is owned by something else, e.g. held in Undo
109 /* TODO: can that be moved to Head?
110 * It's unclear when to reset, so
111 * each instance has its own flag.
112 * The last reference count
113 * decrement automatically has the
114 * then current state available.
115 * */
117 void newHead(); //< Allocate a new Head and init.
118 void incRef() const;
119 bool decRef() const; //< @returns <TRUE/> if the last reference was decremented.
120 void decRefAndDestroy(); //< Destroys caption object if the last reference was decremented.
122 /** Remove from current list and close gap.
124 Usually there are only very few instances, so maintaining a doubly
125 linked list isn't worth memory/performance wise and a simple walk does
128 void removeFromList();
130 /** Replace this instance with pNew in a list, if any.
132 Used by move-ctor and move assignment operator.
134 void replaceInList( ScCaptionPtr* pNew );
136 /** Dissolve list when the caption object is released or gone. */
137 void dissolve();
139 /** Just clear everything, while dissolving the list. */
140 void clear();
143 /** Internal data for a cell annotation. */
144 struct SC_DLLPUBLIC ScNoteData
146 typedef std::shared_ptr< ScCaptionInitData > ScCaptionInitDataRef;
148 OUString maDate; /// Creation date of the note.
149 OUString maAuthor; /// Author of the note.
150 ScCaptionInitDataRef mxInitData; /// Initial data for invisible notes without SdrObject.
151 ScCaptionPtr mxCaption; /// Drawing object representing the cell note.
152 bool mbShown; /// True = note is visible.
154 explicit ScNoteData( bool bShown = false );
158 * Additional class containing cell annotation data.
160 class SC_DLLPUBLIC ScPostIt
162 public:
163 static sal_uInt32 mnLastPostItId;
165 /** Creates an empty note and its caption object and places it according to
166 the passed cell position. */
167 explicit ScPostIt( ScDocument& rDoc, const ScAddress& rPos, sal_uInt32 nPostItId = 0 );
169 /** Copy constructor. Clones the note and its caption to a new document. */
170 explicit ScPostIt( ScDocument& rDoc, const ScAddress& rPos, const ScPostIt& rNote, sal_uInt32 nPostItId = 0 );
172 /** Creates a note from the passed note data with existing caption object.
174 @param bAlwaysCreateCaption Instead of a pointer to an existing
175 caption object, the passed note data structure may contain a
176 reference to an ScCaptionInitData structure containing information
177 about how to construct a missing caption object. If sal_True is passed,
178 the caption drawing object will be created immediately from that
179 data. If sal_False is passed and the note is not visible, it will
180 continue to cache that data until the caption object is requested.
182 explicit ScPostIt(
183 ScDocument& rDoc, const ScAddress& rPos,
184 const ScNoteData& rNoteData, bool bAlwaysCreateCaption, sal_uInt32 nPostItId = 0 );
186 /** Removes the caption object from drawing layer, if this note is its owner. */
187 ~ScPostIt();
189 /** Clones this note and its caption object, if specified.
191 @param bCloneCaption If sal_True is passed, clones the caption object and
192 inserts it into the drawing layer of the destination document. If
193 sal_False is passed, the cloned note will refer to the old caption
194 object (used e.g. in Undo documents to restore the pointer to the
195 existing caption object).
197 std::unique_ptr<ScPostIt> Clone(
198 const ScAddress& rOwnPos,
199 ScDocument& rDestDoc, const ScAddress& rDestPos,
200 bool bCloneCaption ) const;
202 /** Returns the note id. */
203 sal_uInt32 GetId() const { return mnPostItId; }
205 /** Returns the data struct containing all note settings. */
206 const ScNoteData& GetNoteData() const { return maNoteData;}
208 /** Returns the creation date of this note. */
209 const OUString& GetDate() const { return maNoteData.maDate;}
210 /** Sets a new creation date for this note. */
211 void SetDate( const OUString& rDate );
213 /** Returns the author date of this note. */
214 const OUString& GetAuthor() const { return maNoteData.maAuthor;}
215 /** Sets a new author date for this note. */
216 void SetAuthor( const OUString& rAuthor );
218 /** Sets date and author from system settings. */
219 void AutoStamp();
221 /** Returns the pointer to the current outliner object, or null. */
222 const OutlinerParaObject* GetOutlinerObject() const;
223 /** Returns the pointer to the current edit text object, or null. */
224 const EditTextObject* GetEditTextObject() const;
226 /** Returns the caption text of this note. */
227 OUString GetText() const;
228 /** Returns true, if the caption text of this note contains line breaks. */
229 bool HasMultiLineText() const;
230 /** Changes the caption text of this note. All text formatting will be lost. */
231 void SetText( const ScAddress& rPos, const OUString& rText );
233 /** Returns an existing note caption object. returns null, if the note
234 contains initial caption data needed to construct a caption object.
235 The SdrCaptionObj* returned is still managed by the underlying
236 ScNoteData::ScCaptionPtr and must not be stored elsewhere. */
237 SdrCaptionObj* GetCaption() const { return maNoteData.mxCaption.get();}
238 /** Returns the caption object of this note. Creates the caption object, if
239 the note contains initial caption data instead of the caption.
240 The SdrCaptionObj* returned is still managed by the underlying
241 ScNoteData::ScCaptionPtr and must not be stored elsewhere. */
242 SdrCaptionObj* GetOrCreateCaption( const ScAddress& rPos ) const;
244 /** Forgets the pointer to the note caption object.
246 @param bPreserveData
247 If true then the note text is remembered in maNoteData to be able
248 to later reconstruct a caption from it.
250 void ForgetCaption( bool bPreserveData = false );
252 /** Shows or hides the note caption object. */
253 void ShowCaption( const ScAddress& rPos, bool bShow );
254 /** Returns true, if the caption object is visible. */
255 bool IsCaptionShown() const { return maNoteData.mbShown;}
257 /** Shows or hides the caption temporarily (does not change internal visibility state). */
258 void ShowCaptionTemp( const ScAddress& rPos, bool bShow = true );
260 /** Updates caption position according to position of the passed cell. */
261 void UpdateCaptionPos( const ScAddress& rPos );
263 private:
264 ScPostIt( const ScPostIt& ) = delete;
265 ScPostIt& operator=( const ScPostIt& ) = delete;
267 /** Creates the caption object from initial caption data if existing. */
268 void CreateCaptionFromInitData( const ScAddress& rPos ) const;
269 /** Creates a new caption object at the passed cell position, clones passed existing caption. */
270 void CreateCaption( const ScAddress& rPos, const SdrCaptionObj* pCaption = nullptr );
271 /** Removes the caption object from the drawing layer, if this note is its owner. */
272 void RemoveCaption();
274 private:
275 ScDocument& mrDoc; /// Parent document containing the note.
276 mutable ScNoteData maNoteData; /// Note data with pointer to caption object.
277 sal_uInt32 mnPostItId;
280 class SC_DLLPUBLIC ScNoteUtil
282 public:
284 /** Creates and returns a caption object for a temporary caption. */
285 static ScCaptionPtr CreateTempCaption( ScDocument& rDoc, const ScAddress& rPos,
286 SdrPage& rDrawPage, const OUString& rUserText,
287 const tools::Rectangle& rVisRect, bool bTailFront );
289 /** Creates a cell note using the passed caption drawing object.
291 This function is used in import filters to reuse the imported drawing
292 object as note caption object.
294 @param pCaption The drawing object for the cell note. This object MUST
295 be inserted into the document at the correct drawing page already.
296 The underlying ScPostIt::ScNoteData::ScCaptionPtr takes managing
297 ownership of the pointer.
299 @return Pointer to the new cell note object if insertion was
300 successful (i.e. the passed cell position was valid), null
301 otherwise. The Calc document is the owner of the note object. The
302 passed item set and outliner object are deleted automatically if
303 creation of the note was not successful.
305 static ScPostIt* CreateNoteFromCaption(
306 ScDocument& rDoc, const ScAddress& rPos,
307 SdrCaptionObj* pCaption );
309 /** Creates a cell note based on the passed caption object data.
311 This function is used in import filters to use an existing imported
312 item set and outliner object to create a note caption object. For
313 performance reasons, it is possible to specify that the caption drawing
314 object for the cell note is not created yet but the note caches the
315 passed data needed to create the caption object on demand (see
316 parameter bAlwaysCreateCaption).
318 @param pItemSet Pointer to an item set on heap memory containing all
319 formatting attributes of the caption object. This function takes
320 ownership of the passed item set.
322 @param pOutlinerObj Pointer to an outliner object on heap memory
323 containing (formatted) text for the caption object. This function
324 takes ownership of the passed outliner 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 std::unique_ptr<SfxItemSet> pItemSet, OutlinerParaObject* pOutlinerObj,
337 const tools::Rectangle& rCaptionRect, bool bShown );
339 /** Creates a cell note based on the passed string and inserts it into the
340 document.
342 @param rNoteText The text used to create the note caption object. Must
343 not be empty.
345 @param bAlwaysCreateCaption If sal_True is passed, the caption drawing
346 object will be created immediately. If sal_False is passed, the caption
347 drawing object will not be created if the note is not visible
348 (bShown = sal_False), but the cell note will cache the passed data.
349 MUST be set to sal_False outside of import filter implementations!
351 @return Pointer to the new cell note object if insertion was
352 successful (i.e. the passed cell position was valid), null
353 otherwise. The Calc document is the owner of the note object.
355 static ScPostIt* CreateNoteFromString(
356 ScDocument& rDoc, const ScAddress& rPos,
357 const OUString& rNoteText, bool bShown,
358 bool bAlwaysCreateCaption, sal_uInt32 nPostItId = 0 );
362 namespace sc {
364 struct NoteEntry
366 ScAddress const maPos;
367 const ScPostIt* mpNote;
369 NoteEntry( const ScAddress& rPos, const ScPostIt* pNote );
374 #endif
375 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */