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/.
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 .
22 #include <rtl/ustring.hxx>
23 #include <svl/itemset.hxx>
24 #include <rtl/ref.hxx>
25 #include "address.hxx"
29 #include <string_view>
32 class OutlinerParaObject
;
37 namespace tools
{ class Rectangle
; }
38 struct ScCaptionInitData
;
40 /** Internal data for a cell annotation. */
43 typedef std::shared_ptr
< ScCaptionInitData
> ScCaptionInitDataRef
;
45 OUString maDate
; /// Creation date of the note.
46 OUString maAuthor
; /// Author of the note.
47 ScCaptionInitDataRef mxInitData
; /// Initial data for invisible notes without SdrObject.
48 rtl::Reference
<SdrCaptionObj
> mxCaption
; /// Drawing object representing the cell note.
49 bool mbShown
; /// True = note is visible.
51 explicit ScNoteData( bool bShown
= false );
55 * Additional class containing cell annotation data.
57 class SAL_DLLPUBLIC_RTTI ScPostIt
60 static SC_DLLPUBLIC sal_uInt32 mnLastPostItId
;
62 /** Creates an empty note and its caption object and places it according to
63 the passed cell position. */
64 explicit ScPostIt( ScDocument
& rDoc
, const ScAddress
& rPos
, sal_uInt32 nPostItId
= 0 );
66 /** Copy constructor. Clones the note and its caption to a new document. */
67 explicit ScPostIt( ScDocument
& rDoc
, const ScAddress
& rPos
, const ScPostIt
& rNote
, sal_uInt32 nPostItId
= 0 );
69 /** Creates a note from the passed note data with existing caption object.
71 @param bAlwaysCreateCaption Instead of a pointer to an existing
72 caption object, the passed note data structure may contain a
73 reference to an ScCaptionInitData structure containing information
74 about how to construct a missing caption object. If sal_True is passed,
75 the caption drawing object will be created immediately from that
76 data. If sal_False is passed and the note is not visible, it will
77 continue to cache that data until the caption object is requested.
80 ScDocument
& rDoc
, const ScAddress
& rPos
,
81 ScNoteData aNoteData
, bool bAlwaysCreateCaption
, sal_uInt32 nPostItId
= 0 );
83 /** Removes the caption object from drawing layer, if this note is its owner. */
86 /** Clones this note and its caption object, if specified.
88 @param bCloneCaption If sal_True is passed, clones the caption object and
89 inserts it into the drawing layer of the destination document. If
90 sal_False is passed, the cloned note will refer to the old caption
91 object (used e.g. in Undo documents to restore the pointer to the
92 existing caption object).
94 std::unique_ptr
<ScPostIt
> Clone(
95 const ScAddress
& rOwnPos
,
96 ScDocument
& rDestDoc
, const ScAddress
& rDestPos
,
97 bool bCloneCaption
) const;
99 /** Returns the note id. */
100 sal_uInt32
GetId() const { return mnPostItId
; }
102 /** Returns the data struct containing all note settings. */
103 const ScNoteData
& GetNoteData() const { return maNoteData
;}
105 /** Returns the creation date of this note. */
106 const OUString
& GetDate() const { return maNoteData
.maDate
;}
107 /** Sets a new creation date for this note. */
108 void SetDate( const OUString
& rDate
);
110 /** Returns the author date of this note. */
111 const OUString
& GetAuthor() const { return maNoteData
.maAuthor
;}
112 /** Sets a new author date for this note. */
113 SC_DLLPUBLIC
void SetAuthor( const OUString
& rAuthor
);
115 /** Sets date and author from system settings. */
116 void AutoStamp(bool bCreate
= true);
118 /** Returns the pointer to the current outliner object, or null. */
119 const OutlinerParaObject
* GetOutlinerObject() const;
120 /** Returns the pointer to the current edit text object, or null. */
121 SC_DLLPUBLIC
const EditTextObject
* GetEditTextObject() const;
123 /** Returns the caption text of this note. */
124 SC_DLLPUBLIC OUString
GetText() const;
125 /** Changes the caption text of this note. All text formatting will be lost. */
126 SC_DLLPUBLIC
void SetText( const ScAddress
& rPos
, const OUString
& rText
);
128 /** Returns an existing note caption object. returns null, if the note
129 contains initial caption data needed to construct a caption object.
130 The SdrCaptionObj* returned is still managed by the underlying
131 ScNoteData::ScCaptionPtr and must not be stored elsewhere. */
132 SdrCaptionObj
* GetCaption() const { return maNoteData
.mxCaption
.get();}
133 /** Returns the caption object of this note. Creates the caption object, if
134 the note contains initial caption data instead of the caption.
135 The SdrCaptionObj* returned is still managed by the underlying
136 ScNoteData::ScCaptionPtr and must not be stored elsewhere. */
137 SC_DLLPUBLIC SdrCaptionObj
* GetOrCreateCaption( const ScAddress
& rPos
) const;
139 /** Forgets the pointer to the note caption object.
142 If true then the note text is remembered in maNoteData to be able
143 to later reconstruct a caption from it.
145 void ForgetCaption( bool bPreserveData
= false );
147 /** Shows or hides the note caption object. */
148 SC_DLLPUBLIC
void ShowCaption( const ScAddress
& rPos
, bool bShow
);
149 /** Returns true, if the caption object is visible. */
150 bool IsCaptionShown() const { return maNoteData
.mbShown
;}
152 /** Shows or hides the caption temporarily (does not change internal visibility state). */
153 void ShowCaptionTemp( const ScAddress
& rPos
, bool bShow
= true );
155 /** Updates caption position according to position of the passed cell. */
156 void UpdateCaptionPos( const ScAddress
& rPos
);
158 static OString
NoteRangeToJsonString(const ScDocument
& rDoc
, const ScAddress
& rPos
);
161 ScPostIt( const ScPostIt
& ) = delete;
162 ScPostIt
& operator=( const ScPostIt
& ) = delete;
164 /** Creates the caption object from initial caption data if existing. */
165 void CreateCaptionFromInitData( const ScAddress
& rPos
) const;
166 /** Creates a new caption object at the passed cell position, clones passed existing caption. */
167 void CreateCaption( const ScAddress
& rPos
, const SdrCaptionObj
* pCaption
= nullptr );
168 /** Removes the caption object from the drawing layer, if this note is its owner. */
169 void RemoveCaption();
172 ScDocument
& mrDoc
; /// Parent document containing the note.
173 mutable ScNoteData maNoteData
; /// Note data with pointer to caption object.
174 sal_uInt32 mnPostItId
;
177 class GenerateNoteCaption
180 virtual void Generate(SdrCaptionObj
& rCaptionObj
) = 0;
181 virtual OUString
GetSimpleText() const = 0;
182 virtual OUString
GetAuthorName() const = 0;
183 virtual ~GenerateNoteCaption() {};
186 class SC_DLLPUBLIC ScNoteUtil
188 static ScPostIt
* InsertNote(ScDocument
& rDoc
, const ScAddress
& rPos
, ScNoteData
&& rNoteData
,
189 bool bAlwaysCreateCaption
, sal_uInt32 nPostItId
,
190 bool bShouldAutoStamp
= true);
192 static ScNoteData
CreateNoteData(ScDocument
& rDoc
, const ScAddress
& rPos
,
193 const tools::Rectangle
& rCaptionRect
, bool bShown
);
196 /** Creates and returns a caption object for a temporary caption. */
197 static rtl::Reference
<SdrCaptionObj
> CreateTempCaption( ScDocument
& rDoc
, const ScAddress
& rPos
,
198 SdrPage
& rDrawPage
, std::u16string_view rUserText
,
199 const tools::Rectangle
& rVisRect
, bool bTailFront
);
201 /** Creates a cell note using the passed caption drawing object.
203 This function is used in import filters to reuse the imported drawing
204 object as note caption object.
206 @param pCaption The drawing object for the cell note. This object MUST
207 be inserted into the document at the correct drawing page already.
208 The underlying ScPostIt::ScNoteData::ScCaptionPtr takes managing
209 ownership of the pointer.
211 @param bHasStyle Is there a drawing style set for the note.
213 @return Pointer to the new cell note object if insertion was
214 successful (i.e. the passed cell position was valid), null
215 otherwise. The Calc document is the owner of the note object. The
216 passed item set and outliner object are deleted automatically if
217 creation of the note was not successful.
219 static ScPostIt
* CreateNoteFromCaption(
220 ScDocument
& rDoc
, const ScAddress
& rPos
,
221 SdrCaptionObj
* pCaption
, bool bHasStyle
);
223 /** Creates a cell note based on the passed caption object data.
225 This function is used in import filters to use an existing imported
226 item set and outliner object to create a note caption object. For
227 performance reasons, it is possible to specify that the caption drawing
228 object for the cell note is not created yet but the note caches the
229 passed data needed to create the caption object on demand (see
230 parameter bAlwaysCreateCaption).
232 @param pItemSet Pointer to an item set on heap memory containing all
233 formatting attributes of the caption object. This function takes
234 ownership of the passed item set.
236 @param rStyleName Drawing style associated with the caption object.
238 @param rOutlinerObj An outliner object containing (formatted) text
239 for the caption object.
241 @param rCaptionRect The absolute position and size of the caption
242 object. The rectangle may be empty, in this case the default
243 position and size is used.
245 @return Pointer to the new cell note object if insertion was
246 successful (i.e. the passed cell position was valid), null
247 otherwise. The Calc document is the owner of the note object.
249 static ScPostIt
* CreateNoteFromObjectData(
250 ScDocument
& rDoc
, const ScAddress
& rPos
,
251 const SfxItemSet
& rItemSet
, const OUString
& rStyleName
,
252 const OutlinerParaObject
& rOutlinerObj
,
253 const tools::Rectangle
& rCaptionRect
, bool bShown
);
255 // similar to above, except xGenerator is a functor to apply import
256 // properties to the caption object to finalize it on demand
257 static ScPostIt
* CreateNoteFromGenerator(
258 ScDocument
& rDoc
, const ScAddress
& rPos
,
259 std::unique_ptr
<GenerateNoteCaption
> xGenerator
,
260 const tools::Rectangle
& rCaptionRect
, bool bShown
);
262 /** Creates a cell note based on the passed string and inserts it into the
265 @param rNoteText The text used to create the note caption object. Must
268 @param bAlwaysCreateCaption If sal_True is passed, the caption drawing
269 object will be created immediately. If sal_False is passed, the caption
270 drawing object will not be created if the note is not visible
271 (bShown = sal_False), but the cell note will cache the passed data.
272 MUST be set to sal_False outside of import filter implementations!
274 @return Pointer to the new cell note object if insertion was
275 successful (i.e. the passed cell position was valid), null
276 otherwise. The Calc document is the owner of the note object.
278 static ScPostIt
* CreateNoteFromString(
279 ScDocument
& rDoc
, const ScAddress
& rPos
,
280 const OUString
& rNoteText
, bool bShown
,
281 bool bAlwaysCreateCaption
, sal_uInt32 nPostItId
= 0 );
290 const ScPostIt
* mpNote
;
292 NoteEntry( const ScAddress
& rPos
, const ScPostIt
* pNote
);
297 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */