Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "postit.hxx"
21 :
22 : #include <rtl/ustrbuf.hxx>
23 : #include <unotools/useroptions.hxx>
24 : #include <svx/svdpage.hxx>
25 : #include <svx/svdocapt.hxx>
26 : #include <editeng/outlobj.hxx>
27 : #include <editeng/editobj.hxx>
28 : #include <basegfx/polygon/b2dpolygon.hxx>
29 :
30 : #include "scitems.hxx"
31 : #include <svx/xlnstit.hxx>
32 : #include <svx/xlnstwit.hxx>
33 : #include <svx/xlnstcit.hxx>
34 : #include <svx/sxcecitm.hxx>
35 : #include <svx/xflclit.hxx>
36 : #include <svx/sdshitm.hxx>
37 : #include <svx/sdsxyitm.hxx>
38 : #include <tools/gen.hxx>
39 :
40 : #include "table.hxx"
41 : #include "document.hxx"
42 : #include "docpool.hxx"
43 : #include "patattr.hxx"
44 : #include "formulacell.hxx"
45 : #include "drwlayer.hxx"
46 : #include "userdat.hxx"
47 : #include "detfunc.hxx"
48 :
49 : #include <utility>
50 :
51 : using namespace com::sun::star;
52 :
53 : namespace {
54 :
55 : const long SC_NOTECAPTION_WIDTH = 2900; /// Default width of note caption textbox.
56 : const long SC_NOTECAPTION_MAXWIDTH_TEMP = 12000; /// Maximum width of temporary note caption textbox.
57 : const long SC_NOTECAPTION_HEIGHT = 1800; /// Default height of note caption textbox.
58 : const long SC_NOTECAPTION_CELLDIST = 600; /// Default distance of note captions to border of anchor cell.
59 : const long SC_NOTECAPTION_OFFSET_Y = -1500; /// Default Y offset of note captions to top border of anchor cell.
60 : const long SC_NOTECAPTION_OFFSET_X = 1500; /// Default X offset of note captions to left border of anchor cell.
61 : const long SC_NOTECAPTION_BORDERDIST_TEMP = 100; /// Distance of temporary note captions to visible sheet area.
62 :
63 : /** Static helper functions for caption objects. */
64 : class ScCaptionUtil
65 : {
66 : public:
67 : /** Moves the caption object to the correct layer according to passed visibility. */
68 : static void SetCaptionLayer( SdrCaptionObj& rCaption, bool bShown );
69 : /** Sets basic caption settings required for note caption objects. */
70 : static void SetBasicCaptionSettings( SdrCaptionObj& rCaption, bool bShown );
71 : /** Stores the cell position of the note in the user data area of the caption. */
72 : static void SetCaptionUserData( SdrCaptionObj& rCaption, const ScAddress& rPos );
73 : /** Sets all default formatting attributes to the caption object. */
74 : static void SetDefaultItems( SdrCaptionObj& rCaption, ScDocument& rDoc );
75 : /** Updates caption item set according to the passed item set while removing shadow items. */
76 : static void SetCaptionItems( SdrCaptionObj& rCaption, const SfxItemSet& rItemSet );
77 : };
78 :
79 50 : void ScCaptionUtil::SetCaptionLayer( SdrCaptionObj& rCaption, bool bShown )
80 : {
81 50 : SdrLayerID nLayer = bShown ? SC_LAYER_INTERN : SC_LAYER_HIDDEN;
82 50 : if( nLayer != rCaption.GetLayer() )
83 50 : rCaption.SetLayer( nLayer );
84 50 : }
85 :
86 48 : void ScCaptionUtil::SetBasicCaptionSettings( SdrCaptionObj& rCaption, bool bShown )
87 : {
88 48 : SetCaptionLayer( rCaption, bShown );
89 48 : rCaption.SetFixedTail();
90 48 : rCaption.SetSpecialTextBoxShadow();
91 48 : }
92 :
93 48 : void ScCaptionUtil::SetCaptionUserData( SdrCaptionObj& rCaption, const ScAddress& rPos )
94 : {
95 : // pass true to ScDrawLayer::GetObjData() to create the object data entry
96 48 : ScDrawObjData* pObjData = ScDrawLayer::GetObjData( &rCaption, true );
97 : OSL_ENSURE( pObjData, "ScCaptionUtil::SetCaptionUserData - missing drawing object user data" );
98 48 : pObjData->maStart = rPos;
99 48 : pObjData->meType = ScDrawObjData::CellNote;
100 48 : }
101 :
102 41 : void ScCaptionUtil::SetDefaultItems( SdrCaptionObj& rCaption, ScDocument& rDoc )
103 : {
104 41 : SfxItemSet aItemSet = rCaption.GetMergedItemSet();
105 :
106 : // caption tail arrow
107 82 : ::basegfx::B2DPolygon aTriangle;
108 41 : aTriangle.append( ::basegfx::B2DPoint( 10.0, 0.0 ) );
109 41 : aTriangle.append( ::basegfx::B2DPoint( 0.0, 30.0 ) );
110 41 : aTriangle.append( ::basegfx::B2DPoint( 20.0, 30.0 ) );
111 41 : aTriangle.setClosed( true );
112 : /* Line ends are now created with an empty name. The
113 : checkForUniqueItem() method then finds a unique name for the item's
114 : value. */
115 41 : aItemSet.Put( XLineStartItem( OUString(), ::basegfx::B2DPolyPolygon( aTriangle ) ) );
116 41 : aItemSet.Put( XLineStartWidthItem( 200 ) );
117 41 : aItemSet.Put( XLineStartCenterItem( false ) );
118 41 : aItemSet.Put( XFillStyleItem( drawing::FillStyle_SOLID ) );
119 41 : aItemSet.Put( XFillColorItem( OUString(), ScDetectiveFunc::GetCommentColor() ) );
120 41 : aItemSet.Put( SdrCaptionEscDirItem( SDRCAPT_ESCBESTFIT ) );
121 :
122 : // shadow
123 : /* SdrShadowItem has sal_False, instead the shadow is set for the
124 : rectangle only with SetSpecialTextBoxShadow() when the object is
125 : created (item must be set to adjust objects from older files). */
126 41 : aItemSet.Put( makeSdrShadowItem( false ) );
127 41 : aItemSet.Put( makeSdrShadowXDistItem( 100 ) );
128 41 : aItemSet.Put( makeSdrShadowYDistItem( 100 ) );
129 :
130 : // text attributes
131 41 : aItemSet.Put( makeSdrTextLeftDistItem( 100 ) );
132 41 : aItemSet.Put( makeSdrTextRightDistItem( 100 ) );
133 41 : aItemSet.Put( makeSdrTextUpperDistItem( 100 ) );
134 41 : aItemSet.Put( makeSdrTextLowerDistItem( 100 ) );
135 41 : aItemSet.Put( makeSdrTextAutoGrowWidthItem( false ) );
136 41 : aItemSet.Put( makeSdrTextAutoGrowHeightItem( true ) );
137 : // use the default cell style to be able to modify the caption font
138 41 : const ScPatternAttr& rDefPattern = static_cast< const ScPatternAttr& >( rDoc.GetPool()->GetDefaultItem( ATTR_PATTERN ) );
139 41 : rDefPattern.FillEditItemSet( &aItemSet );
140 :
141 82 : rCaption.SetMergedItemSet( aItemSet );
142 41 : }
143 :
144 9 : void ScCaptionUtil::SetCaptionItems( SdrCaptionObj& rCaption, const SfxItemSet& rItemSet )
145 : {
146 : // copy all items
147 9 : rCaption.SetMergedItemSet( rItemSet );
148 : // reset shadow items
149 9 : rCaption.SetMergedItem( makeSdrShadowItem( false ) );
150 9 : rCaption.SetMergedItem( makeSdrShadowXDistItem( 100 ) );
151 9 : rCaption.SetMergedItem( makeSdrShadowYDistItem( 100 ) );
152 9 : rCaption.SetSpecialTextBoxShadow();
153 9 : }
154 :
155 : /** Helper for creation and manipulation of caption drawing objects independent
156 : from cell annotations. */
157 : class ScCaptionCreator
158 : {
159 : public:
160 : /** Create a new caption. The caption will not be inserted into the document. */
161 : explicit ScCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, bool bShown, bool bTailFront );
162 : /** Manipulate an existing caption. */
163 : explicit ScCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, SdrCaptionObj& rCaption );
164 :
165 : /** Returns the drawing layer page of the sheet contained in maPos. */
166 : SdrPage* GetDrawPage();
167 : /** Returns the caption drawing object. */
168 45 : inline SdrCaptionObj* GetCaption() { return mpCaption; }
169 :
170 : /** Moves the caption inside the passed rectangle. Uses page area if 0 is passed. */
171 : void FitCaptionToRect( const Rectangle* pVisRect = 0 );
172 : /** Places the caption inside the passed rectangle, tries to keep the cell rectangle uncovered. Uses page area if 0 is passed. */
173 : void AutoPlaceCaption( const Rectangle* pVisRect = 0 );
174 : /** Updates caption tail and textbox according to current cell position. Uses page area if 0 is passed. */
175 : void UpdateCaptionPos( const Rectangle* pVisRect = 0 );
176 :
177 : protected:
178 : /** Helper constructor for derived classes. */
179 : explicit ScCaptionCreator( ScDocument& rDoc, const ScAddress& rPos );
180 :
181 : /** Calculates the caption tail position according to current cell position. */
182 : Point CalcTailPos( bool bTailFront );
183 : /** Implements creation of the caption object. The caption will not be inserted into the document. */
184 : void CreateCaption( bool bShown, bool bTailFront );
185 :
186 : private:
187 : /** Initializes all members. */
188 : void Initialize();
189 : /** Returns the passed rectangle if existing, page rectangle otherwise. */
190 83 : inline const Rectangle& GetVisRect( const Rectangle* pVisRect ) const { return pVisRect ? *pVisRect : maPageRect; }
191 :
192 : private:
193 : ScDocument& mrDoc;
194 : ScAddress maPos;
195 : SdrCaptionObj* mpCaption;
196 : Rectangle maPageRect;
197 : Rectangle maCellRect;
198 : bool mbNegPage;
199 : };
200 :
201 0 : ScCaptionCreator::ScCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, bool bShown, bool bTailFront ) :
202 : mrDoc( rDoc ),
203 : maPos( rPos ),
204 0 : mpCaption( 0 )
205 : {
206 0 : Initialize();
207 0 : CreateCaption( bShown, bTailFront );
208 0 : }
209 :
210 15 : ScCaptionCreator::ScCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, SdrCaptionObj& rCaption ) :
211 : mrDoc( rDoc ),
212 : maPos( rPos ),
213 15 : mpCaption( &rCaption )
214 : {
215 15 : Initialize();
216 15 : }
217 :
218 75 : ScCaptionCreator::ScCaptionCreator( ScDocument& rDoc, const ScAddress& rPos ) :
219 : mrDoc( rDoc ),
220 : maPos( rPos ),
221 75 : mpCaption( 0 )
222 : {
223 75 : Initialize();
224 75 : }
225 :
226 168 : SdrPage* ScCaptionCreator::GetDrawPage()
227 : {
228 168 : ScDrawLayer* pDrawLayer = mrDoc.GetDrawLayer();
229 168 : return pDrawLayer ? pDrawLayer->GetPage( static_cast< sal_uInt16 >( maPos.Tab() ) ) : 0;
230 : }
231 :
232 46 : void ScCaptionCreator::FitCaptionToRect( const Rectangle* pVisRect )
233 : {
234 46 : const Rectangle& rVisRect = GetVisRect( pVisRect );
235 :
236 : // tail position
237 46 : Point aTailPos = mpCaption->GetTailPos();
238 46 : aTailPos.X() = ::std::max( ::std::min( aTailPos.X(), rVisRect.Right() ), rVisRect.Left() );
239 46 : aTailPos.Y() = ::std::max( ::std::min( aTailPos.Y(), rVisRect.Bottom() ), rVisRect.Top() );
240 46 : mpCaption->SetTailPos( aTailPos );
241 :
242 : // caption rectangle
243 46 : Rectangle aCaptRect = mpCaption->GetLogicRect();
244 46 : Point aCaptPos = aCaptRect.TopLeft();
245 : // move textbox inside right border of visible area
246 46 : aCaptPos.X() = ::std::min< long >( aCaptPos.X(), rVisRect.Right() - aCaptRect.GetWidth() );
247 : // move textbox inside left border of visible area (this may move it outside on right side again)
248 46 : aCaptPos.X() = ::std::max< long >( aCaptPos.X(), rVisRect.Left() );
249 : // move textbox inside bottom border of visible area
250 46 : aCaptPos.Y() = ::std::min< long >( aCaptPos.Y(), rVisRect.Bottom() - aCaptRect.GetHeight() );
251 : // move textbox inside top border of visible area (this may move it outside on bottom side again)
252 46 : aCaptPos.Y() = ::std::max< long >( aCaptPos.Y(), rVisRect.Top() );
253 : // update caption
254 46 : aCaptRect.SetPos( aCaptPos );
255 46 : mpCaption->SetLogicRect( aCaptRect );
256 46 : }
257 :
258 37 : void ScCaptionCreator::AutoPlaceCaption( const Rectangle* pVisRect )
259 : {
260 37 : const Rectangle& rVisRect = GetVisRect( pVisRect );
261 :
262 : // caption rectangle
263 37 : Rectangle aCaptRect = mpCaption->GetLogicRect();
264 37 : long nWidth = aCaptRect.GetWidth();
265 37 : long nHeight = aCaptRect.GetHeight();
266 :
267 : // n***Space contains available space between border of visible area and cell
268 37 : long nLeftSpace = maCellRect.Left() - rVisRect.Left() + 1;
269 37 : long nRightSpace = rVisRect.Right() - maCellRect.Right() + 1;
270 37 : long nTopSpace = maCellRect.Top() - rVisRect.Top() + 1;
271 37 : long nBottomSpace = rVisRect.Bottom() - maCellRect.Bottom() + 1;
272 :
273 : // nNeeded*** contains textbox dimensions plus needed distances to cell or border of visible area
274 37 : long nNeededSpaceX = nWidth + SC_NOTECAPTION_CELLDIST;
275 37 : long nNeededSpaceY = nHeight + SC_NOTECAPTION_CELLDIST;
276 :
277 : // bFitsWidth*** == true means width of textbox fits into horizontal free space of visible area
278 37 : bool bFitsWidthLeft = nNeededSpaceX <= nLeftSpace; // text box width fits into the width left of cell
279 37 : bool bFitsWidthRight = nNeededSpaceX <= nRightSpace; // text box width fits into the width right of cell
280 37 : bool bFitsWidth = nWidth <= rVisRect.GetWidth(); // text box width fits into width of visible area
281 :
282 : // bFitsHeight*** == true means height of textbox fits into vertical free space of visible area
283 37 : bool bFitsHeightTop = nNeededSpaceY <= nTopSpace; // text box height fits into the height above cell
284 37 : bool bFitsHeightBottom = nNeededSpaceY <= nBottomSpace; // text box height fits into the height below cell
285 37 : bool bFitsHeight = nHeight <= rVisRect.GetHeight(); // text box height fits into height of visible area
286 :
287 : // bFits*** == true means the textbox fits completely into free space of visible area
288 37 : bool bFitsLeft = bFitsWidthLeft && bFitsHeight;
289 37 : bool bFitsRight = bFitsWidthRight && bFitsHeight;
290 37 : bool bFitsTop = bFitsWidth && bFitsHeightTop;
291 37 : bool bFitsBottom = bFitsWidth && bFitsHeightBottom;
292 :
293 37 : Point aCaptPos;
294 : // use left/right placement if possible, or if top/bottom placement not possible
295 37 : if( bFitsLeft || bFitsRight || (!bFitsTop && !bFitsBottom) )
296 : {
297 : // prefer left in RTL sheet and right in LTR sheets
298 37 : bool bPreferLeft = bFitsLeft && (mbNegPage || !bFitsRight);
299 37 : bool bPreferRight = bFitsRight && (!mbNegPage || !bFitsLeft);
300 : // move to left, if left is preferred, or if neither left nor right fit and there is more space to the left
301 37 : if( bPreferLeft || (!bPreferRight && (nLeftSpace > nRightSpace)) )
302 0 : aCaptPos.X() = maCellRect.Left() - SC_NOTECAPTION_CELLDIST - nWidth;
303 : else // to right
304 37 : aCaptPos.X() = maCellRect.Right() + SC_NOTECAPTION_CELLDIST;
305 : // Y position according to top cell border
306 37 : aCaptPos.Y() = maCellRect.Top() + SC_NOTECAPTION_OFFSET_Y;
307 : }
308 : else // top or bottom placement
309 : {
310 : // X position
311 0 : aCaptPos.X() = maCellRect.Left() + SC_NOTECAPTION_OFFSET_X;
312 : // top placement, if possible
313 0 : if( bFitsTop )
314 0 : aCaptPos.Y() = maCellRect.Top() - SC_NOTECAPTION_CELLDIST - nHeight;
315 : else // bottom placement
316 0 : aCaptPos.Y() = maCellRect.Bottom() + SC_NOTECAPTION_CELLDIST;
317 : }
318 :
319 : // update textbox position in note caption object
320 37 : aCaptRect.SetPos( aCaptPos );
321 37 : mpCaption->SetLogicRect( aCaptRect );
322 37 : FitCaptionToRect( pVisRect );
323 37 : }
324 :
325 12 : void ScCaptionCreator::UpdateCaptionPos( const Rectangle* pVisRect )
326 : {
327 12 : ScDrawLayer* pDrawLayer = mrDoc.GetDrawLayer();
328 :
329 : // update caption position
330 12 : const Point& rOldTailPos = mpCaption->GetTailPos();
331 12 : Point aTailPos = CalcTailPos( false );
332 12 : if( rOldTailPos != aTailPos )
333 : {
334 : // create drawing undo action
335 1 : if( pDrawLayer && pDrawLayer->IsRecording() )
336 0 : pDrawLayer->AddCalcUndo( new SdrUndoGeoObj( *mpCaption ) );
337 : // calculate new caption rectangle (#i98141# handle LTR<->RTL switch correctly)
338 1 : Rectangle aCaptRect = mpCaption->GetLogicRect();
339 1 : long nDiffX = (rOldTailPos.X() >= 0) ? (aCaptRect.Left() - rOldTailPos.X()) : (rOldTailPos.X() - aCaptRect.Right());
340 1 : if( mbNegPage ) nDiffX = -nDiffX - aCaptRect.GetWidth();
341 1 : long nDiffY = aCaptRect.Top() - rOldTailPos.Y();
342 1 : aCaptRect.SetPos( aTailPos + Point( nDiffX, nDiffY ) );
343 : // set new tail position and caption rectangle
344 1 : mpCaption->SetTailPos( aTailPos );
345 1 : mpCaption->SetLogicRect( aCaptRect );
346 : // fit caption into draw page
347 1 : FitCaptionToRect( pVisRect );
348 : }
349 :
350 : // update cell position in caption user data
351 12 : ScDrawObjData* pCaptData = ScDrawLayer::GetNoteCaptionData( mpCaption, maPos.Tab() );
352 12 : if( pCaptData && (maPos != pCaptData->maStart) )
353 : {
354 : // create drawing undo action
355 1 : if( pDrawLayer && pDrawLayer->IsRecording() )
356 0 : pDrawLayer->AddCalcUndo( new ScUndoObjData( mpCaption, pCaptData->maStart, pCaptData->maEnd, maPos, pCaptData->maEnd ) );
357 : // set new position
358 1 : pCaptData->maStart = maPos;
359 : }
360 12 : }
361 :
362 60 : Point ScCaptionCreator::CalcTailPos( bool bTailFront )
363 : {
364 : // tail position
365 60 : bool bTailLeft = bTailFront != mbNegPage;
366 60 : Point aTailPos = bTailLeft ? maCellRect.TopLeft() : maCellRect.TopRight();
367 : // move caption point 1/10 mm inside cell
368 60 : if( bTailLeft ) aTailPos.X() += 10; else aTailPos.X() -= 10;
369 60 : aTailPos.Y() += 10;
370 60 : return aTailPos;
371 : }
372 :
373 45 : void ScCaptionCreator::CreateCaption( bool bShown, bool bTailFront )
374 : {
375 : // create the caption drawing object
376 45 : Rectangle aTextRect( Point( 0 , 0 ), Size( SC_NOTECAPTION_WIDTH, SC_NOTECAPTION_HEIGHT ) );
377 45 : Point aTailPos = CalcTailPos( bTailFront );
378 45 : mpCaption = new SdrCaptionObj( aTextRect, aTailPos );
379 : // basic caption settings
380 45 : ScCaptionUtil::SetBasicCaptionSettings( *mpCaption, bShown );
381 45 : }
382 :
383 90 : void ScCaptionCreator::Initialize()
384 : {
385 90 : maCellRect = ScDrawLayer::GetCellRect( mrDoc, maPos, true );
386 90 : mbNegPage = mrDoc.IsNegativePage( maPos.Tab() );
387 90 : if( SdrPage* pDrawPage = GetDrawPage() )
388 : {
389 60 : maPageRect = Rectangle( Point( 0, 0 ), pDrawPage->GetSize() );
390 : /* #i98141# SdrPage::GetSize() returns negative width in RTL mode.
391 : The call to Rectangle::Adjust() orders left/right coordinate
392 : accordingly. */
393 60 : maPageRect.Justify();
394 : }
395 90 : }
396 :
397 : /** Helper for creation of permanent caption drawing objects for cell notes. */
398 : class ScNoteCaptionCreator : public ScCaptionCreator
399 : {
400 : public:
401 : /** Create a new caption object and inserts it into the document. */
402 : explicit ScNoteCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, ScNoteData& rNoteData );
403 : /** Manipulate an existing caption. */
404 : explicit ScNoteCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, SdrCaptionObj& rCaption, bool bShown );
405 : };
406 :
407 75 : ScNoteCaptionCreator::ScNoteCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, ScNoteData& rNoteData ) :
408 75 : ScCaptionCreator( rDoc, rPos ) // use helper c'tor that does not create the caption yet
409 : {
410 75 : SdrPage* pDrawPage = GetDrawPage();
411 : OSL_ENSURE( pDrawPage, "ScNoteCaptionCreator::ScNoteCaptionCreator - no drawing page" );
412 75 : if( pDrawPage )
413 : {
414 : // create the caption drawing object
415 45 : CreateCaption( rNoteData.mbShown, false );
416 45 : rNoteData.mpCaption = GetCaption();
417 : OSL_ENSURE( rNoteData.mpCaption, "ScNoteCaptionCreator::ScNoteCaptionCreator - missing caption object" );
418 45 : if( rNoteData.mpCaption )
419 : {
420 : // store note position in user data of caption object
421 45 : ScCaptionUtil::SetCaptionUserData( *rNoteData.mpCaption, rPos );
422 : // insert object into draw page
423 45 : pDrawPage->InsertObject( rNoteData.mpCaption );
424 : }
425 : }
426 75 : }
427 :
428 3 : ScNoteCaptionCreator::ScNoteCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, SdrCaptionObj& rCaption, bool bShown ) :
429 3 : ScCaptionCreator( rDoc, rPos, rCaption )
430 : {
431 3 : SdrPage* pDrawPage = GetDrawPage();
432 : OSL_ENSURE( pDrawPage, "ScNoteCaptionCreator::ScNoteCaptionCreator - no drawing page" );
433 : OSL_ENSURE( rCaption.GetPage() == pDrawPage, "ScNoteCaptionCreator::ScNoteCaptionCreator - wrong drawing page in caption" );
434 3 : if( pDrawPage && (rCaption.GetPage() == pDrawPage) )
435 : {
436 : // store note position in user data of caption object
437 3 : ScCaptionUtil::SetCaptionUserData( rCaption, rPos );
438 : // basic caption settings
439 3 : ScCaptionUtil::SetBasicCaptionSettings( rCaption, bShown );
440 : // set correct tail position
441 3 : rCaption.SetTailPos( CalcTailPos( false ) );
442 : }
443 3 : }
444 :
445 : } // namespace
446 :
447 37 : struct ScCaptionInitData
448 : {
449 : typedef ::std::unique_ptr< SfxItemSet > SfxItemSetPtr;
450 : typedef ::std::unique_ptr< OutlinerParaObject > OutlinerParaObjPtr;
451 :
452 : SfxItemSetPtr mxItemSet; /// Caption object formatting.
453 : OutlinerParaObjPtr mxOutlinerObj; /// Text object with all text portion formatting.
454 : OUString maSimpleText; /// Simple text without formatting.
455 : Point maCaptionOffset; /// Caption position relative to cell corner.
456 : Size maCaptionSize; /// Size of the caption object.
457 : bool mbDefaultPosSize; /// True = use default position and size for caption.
458 :
459 : explicit ScCaptionInitData();
460 : };
461 :
462 37 : ScCaptionInitData::ScCaptionInitData() :
463 37 : mbDefaultPosSize( true )
464 : {
465 37 : }
466 :
467 102 : ScNoteData::ScNoteData( bool bShown ) :
468 : mpCaption( 0 ),
469 102 : mbShown( bShown )
470 : {
471 102 : }
472 :
473 222 : ScNoteData::~ScNoteData()
474 : {
475 222 : }
476 :
477 28 : ScPostIt::ScPostIt( ScDocument& rDoc, const ScAddress& rPos, bool bShown ) :
478 : mrDoc( rDoc ),
479 28 : maNoteData( bShown )
480 : {
481 28 : AutoStamp();
482 28 : CreateCaption( rPos );
483 28 : }
484 :
485 26 : ScPostIt::ScPostIt( ScDocument& rDoc, const ScAddress& rPos, const ScPostIt& rNote ) :
486 : mrDoc( rDoc ),
487 26 : maNoteData( rNote.maNoteData )
488 : {
489 26 : maNoteData.mpCaption = 0;
490 26 : CreateCaption( rPos, rNote.maNoteData.mpCaption );
491 26 : }
492 :
493 69 : ScPostIt::ScPostIt( ScDocument& rDoc, const ScAddress& rPos, const ScNoteData& rNoteData, bool bAlwaysCreateCaption ) :
494 : mrDoc( rDoc ),
495 69 : maNoteData( rNoteData )
496 : {
497 69 : if( bAlwaysCreateCaption || maNoteData.mbShown )
498 25 : CreateCaptionFromInitData( rPos );
499 69 : }
500 :
501 240 : ScPostIt::~ScPostIt()
502 : {
503 120 : RemoveCaption();
504 120 : }
505 :
506 55 : ScPostIt* ScPostIt::Clone( const ScAddress& rOwnPos, ScDocument& rDestDoc, const ScAddress& rDestPos, bool bCloneCaption ) const
507 : {
508 55 : CreateCaptionFromInitData( rOwnPos );
509 55 : return bCloneCaption ? new ScPostIt( rDestDoc, rDestPos, *this ) : new ScPostIt( rDestDoc, rDestPos, maNoteData, false );
510 : }
511 :
512 15 : void ScPostIt::SetDate( const OUString& rDate )
513 : {
514 15 : maNoteData.maDate = rDate;
515 15 : }
516 :
517 22 : void ScPostIt::SetAuthor( const OUString& rAuthor )
518 : {
519 22 : maNoteData.maAuthor = rAuthor;
520 22 : }
521 :
522 68 : void ScPostIt::AutoStamp()
523 : {
524 68 : maNoteData.maDate = ScGlobal::pLocaleData->getDate( Date( Date::SYSTEM ) );
525 68 : maNoteData.maAuthor = SvtUserOptions().GetID();
526 68 : }
527 :
528 48 : const OutlinerParaObject* ScPostIt::GetOutlinerObject() const
529 : {
530 48 : if( maNoteData.mpCaption )
531 18 : return maNoteData.mpCaption->GetOutlinerParaObject();
532 30 : if( maNoteData.mxInitData.get() )
533 10 : return maNoteData.mxInitData->mxOutlinerObj.get();
534 20 : return 0;
535 : }
536 :
537 48 : const EditTextObject* ScPostIt::GetEditTextObject() const
538 : {
539 48 : const OutlinerParaObject* pOPO = GetOutlinerObject();
540 48 : return pOPO ? &pOPO->GetTextObject() : 0;
541 : }
542 :
543 39 : OUString ScPostIt::GetText() const
544 : {
545 39 : if( const EditTextObject* pEditObj = GetEditTextObject() )
546 : {
547 17 : OUStringBuffer aBuffer;
548 34 : for( sal_Int32 nPara = 0, nParaCount = pEditObj->GetParagraphCount(); nPara < nParaCount; ++nPara )
549 : {
550 17 : if( nPara > 0 )
551 0 : aBuffer.append( '\n' );
552 17 : aBuffer.append( pEditObj->GetText( nPara ) );
553 : }
554 17 : return aBuffer.makeStringAndClear();
555 : }
556 22 : if( maNoteData.mxInitData.get() )
557 2 : return maNoteData.mxInitData->maSimpleText;
558 20 : return OUString();
559 : }
560 :
561 24 : void ScPostIt::SetText( const ScAddress& rPos, const OUString& rText )
562 : {
563 24 : CreateCaptionFromInitData( rPos );
564 24 : if( maNoteData.mpCaption )
565 6 : maNoteData.mpCaption->SetText( rText );
566 24 : }
567 :
568 17 : SdrCaptionObj* ScPostIt::GetOrCreateCaption( const ScAddress& rPos ) const
569 : {
570 17 : CreateCaptionFromInitData( rPos );
571 17 : return maNoteData.mpCaption;
572 : }
573 :
574 10 : void ScPostIt::ForgetCaption()
575 : {
576 : /* This function is used in undo actions to give up the responsibility for
577 : the caption object which is handled by separate drawing undo actions. */
578 10 : maNoteData.mpCaption = 0;
579 10 : maNoteData.mxInitData.reset();
580 10 : }
581 :
582 2 : void ScPostIt::ShowCaption( const ScAddress& rPos, bool bShow )
583 : {
584 2 : CreateCaptionFromInitData( rPos );
585 : // no separate drawing undo needed, handled completely inside ScUndoShowHideNote
586 2 : maNoteData.mbShown = bShow;
587 2 : if( maNoteData.mpCaption )
588 2 : ScCaptionUtil::SetCaptionLayer( *maNoteData.mpCaption, bShow );
589 2 : }
590 :
591 0 : void ScPostIt::ShowCaptionTemp( const ScAddress& rPos, bool bShow )
592 : {
593 0 : CreateCaptionFromInitData( rPos );
594 0 : if( maNoteData.mpCaption )
595 0 : ScCaptionUtil::SetCaptionLayer( *maNoteData.mpCaption, maNoteData.mbShown || bShow );
596 0 : }
597 :
598 15 : void ScPostIt::UpdateCaptionPos( const ScAddress& rPos )
599 : {
600 15 : CreateCaptionFromInitData( rPos );
601 15 : if( maNoteData.mpCaption )
602 : {
603 12 : ScCaptionCreator aCreator( mrDoc, rPos, *maNoteData.mpCaption );
604 12 : aCreator.UpdateCaptionPos();
605 : }
606 15 : }
607 :
608 : // private --------------------------------------------------------------------
609 :
610 138 : void ScPostIt::CreateCaptionFromInitData( const ScAddress& rPos ) const
611 : {
612 : OSL_ENSURE( maNoteData.mpCaption || maNoteData.mxInitData.get(), "ScPostIt::CreateCaptionFromInitData - need caption object or initial caption data" );
613 138 : if( maNoteData.mxInitData.get() )
614 : {
615 : /* This function is called from ScPostIt::Clone() when copying cells
616 : to the clipboard/undo document, and when copying cells from the
617 : clipboard/undo document. The former should always be called first,
618 : so if called in an clipboard/undo document, the caption should have
619 : been created already. */
620 : OSL_ENSURE( !mrDoc.IsUndo() && !mrDoc.IsClipboard(), "ScPostIt::CreateCaptionFromInitData - note caption should not be created in undo/clip documents" );
621 :
622 : /* #i104915# Never try to create notes in Undo document, leads to
623 : crash due to missing document members (e.g. row height array). */
624 28 : if( !maNoteData.mpCaption && !mrDoc.IsUndo() )
625 : {
626 : // ScNoteCaptionCreator c'tor creates the caption and inserts it into the document and maNoteData
627 28 : ScNoteCaptionCreator aCreator( mrDoc, rPos, maNoteData );
628 28 : if( maNoteData.mpCaption )
629 : {
630 28 : ScCaptionInitData& rInitData = *maNoteData.mxInitData;
631 :
632 : // transfer ownership of outliner object to caption, or set simple text
633 : OSL_ENSURE( rInitData.mxOutlinerObj.get() || !rInitData.maSimpleText.isEmpty(),
634 : "ScPostIt::CreateCaptionFromInitData - need either outliner para object or simple text" );
635 28 : if( rInitData.mxOutlinerObj.get() )
636 9 : maNoteData.mpCaption->SetOutlinerParaObject( rInitData.mxOutlinerObj.release() );
637 : else
638 19 : maNoteData.mpCaption->SetText( rInitData.maSimpleText );
639 :
640 : // copy all items or set default items; reset shadow items
641 28 : ScCaptionUtil::SetDefaultItems( *maNoteData.mpCaption, mrDoc );
642 28 : if( rInitData.mxItemSet.get() )
643 9 : ScCaptionUtil::SetCaptionItems( *maNoteData.mpCaption, *rInitData.mxItemSet );
644 :
645 : // set position and size of the caption object
646 28 : if( rInitData.mbDefaultPosSize )
647 : {
648 : // set other items and fit caption size to text
649 24 : maNoteData.mpCaption->SetMergedItem( makeSdrTextMinFrameWidthItem( SC_NOTECAPTION_WIDTH ) );
650 24 : maNoteData.mpCaption->SetMergedItem( makeSdrTextMaxFrameWidthItem( SC_NOTECAPTION_MAXWIDTH_TEMP ) );
651 24 : maNoteData.mpCaption->AdjustTextFrameWidthAndHeight();
652 24 : aCreator.AutoPlaceCaption();
653 : }
654 : else
655 : {
656 4 : Rectangle aCellRect = ScDrawLayer::GetCellRect( mrDoc, rPos, true );
657 4 : bool bNegPage = mrDoc.IsNegativePage( rPos.Tab() );
658 4 : long nPosX = bNegPage ? (aCellRect.Left() - rInitData.maCaptionOffset.X()) : (aCellRect.Right() + rInitData.maCaptionOffset.X());
659 4 : long nPosY = aCellRect.Top() + rInitData.maCaptionOffset.Y();
660 4 : Rectangle aCaptRect( Point( nPosX, nPosY ), rInitData.maCaptionSize );
661 4 : maNoteData.mpCaption->SetLogicRect( aCaptRect );
662 4 : aCreator.FitCaptionToRect();
663 : }
664 : }
665 : }
666 : // forget the initial caption data struct
667 28 : maNoteData.mxInitData.reset();
668 : }
669 138 : }
670 :
671 54 : void ScPostIt::CreateCaption( const ScAddress& rPos, const SdrCaptionObj* pCaption )
672 : {
673 : OSL_ENSURE( !maNoteData.mpCaption, "ScPostIt::CreateCaption - unexpected caption object found" );
674 54 : maNoteData.mpCaption = 0;
675 :
676 : /* #i104915# Never try to create notes in Undo document, leads to
677 : crash due to missing document members (e.g. row height array). */
678 : OSL_ENSURE( !mrDoc.IsUndo(), "ScPostIt::CreateCaption - note caption should not be created in undo documents" );
679 54 : if( mrDoc.IsUndo() )
680 61 : return;
681 :
682 : // drawing layer may be missing, if a note is copied into a clipboard document
683 47 : if( mrDoc.IsClipboard() )
684 3 : mrDoc.InitDrawLayer();
685 :
686 : // ScNoteCaptionCreator c'tor creates the caption and inserts it into the document and maNoteData
687 47 : ScNoteCaptionCreator aCreator( mrDoc, rPos, maNoteData );
688 47 : if( maNoteData.mpCaption )
689 : {
690 : // clone settings of passed caption
691 17 : if( pCaption )
692 : {
693 : // copy edit text object (object must be inserted into page already)
694 4 : if( OutlinerParaObject* pOPO = pCaption->GetOutlinerParaObject() )
695 4 : maNoteData.mpCaption->SetOutlinerParaObject( new OutlinerParaObject( *pOPO ) );
696 : // copy formatting items (after text has been copied to apply font formatting)
697 4 : maNoteData.mpCaption->SetMergedItemSetAndBroadcast( pCaption->GetMergedItemSet() );
698 : // move textbox position relative to new cell, copy textbox size
699 4 : Rectangle aCaptRect = pCaption->GetLogicRect();
700 4 : Point aDist = maNoteData.mpCaption->GetTailPos() - pCaption->GetTailPos();
701 4 : aCaptRect.Move( aDist.X(), aDist.Y() );
702 4 : maNoteData.mpCaption->SetLogicRect( aCaptRect );
703 4 : aCreator.FitCaptionToRect();
704 : }
705 : else
706 : {
707 : // set default formatting and default position
708 13 : ScCaptionUtil::SetDefaultItems( *maNoteData.mpCaption, mrDoc );
709 13 : aCreator.AutoPlaceCaption();
710 : }
711 :
712 : // create undo action
713 17 : if( ScDrawLayer* pDrawLayer = mrDoc.GetDrawLayer() )
714 17 : if( pDrawLayer->IsRecording() )
715 0 : pDrawLayer->AddCalcUndo( new SdrUndoNewObj( *maNoteData.mpCaption ) );
716 : }
717 : }
718 :
719 120 : void ScPostIt::RemoveCaption()
720 : {
721 :
722 : /* Remove caption object only, if this note is its owner (e.g. notes in
723 : undo documents refer to captions in original document, do not remove
724 : them from drawing layer here). */
725 120 : ScDrawLayer* pDrawLayer = mrDoc.GetDrawLayer();
726 120 : if( maNoteData.mpCaption && (pDrawLayer == maNoteData.mpCaption->GetModel()) )
727 : {
728 : OSL_ENSURE( pDrawLayer, "ScPostIt::RemoveCaption - object without drawing layer" );
729 44 : SdrPage* pDrawPage = maNoteData.mpCaption->GetPage();
730 : OSL_ENSURE( pDrawPage, "ScPostIt::RemoveCaption - object without drawing page" );
731 44 : if( pDrawPage )
732 : {
733 44 : pDrawPage->RecalcObjOrdNums();
734 : // create drawing undo action (before removing the object to have valid draw page in undo action)
735 44 : bool bRecording = ( pDrawLayer && pDrawLayer->IsRecording() );
736 44 : if( bRecording )
737 3 : pDrawLayer->AddCalcUndo( new SdrUndoDelObj( *maNoteData.mpCaption ) );
738 : // remove the object from the drawing page, delete if undo is disabled
739 44 : SdrObject* pObj = pDrawPage->RemoveObject( maNoteData.mpCaption->GetOrdNum() );
740 44 : if( !bRecording )
741 41 : SdrObject::Free( pObj );
742 : }
743 : }
744 120 : maNoteData.mpCaption = 0;
745 120 : }
746 :
747 0 : SdrCaptionObj* ScNoteUtil::CreateTempCaption(
748 : ScDocument& rDoc, const ScAddress& rPos, SdrPage& rDrawPage,
749 : const OUString& rUserText, const Rectangle& rVisRect, bool bTailFront )
750 : {
751 0 : OUStringBuffer aBuffer( rUserText );
752 : // add plain text of invisible (!) cell note (no formatting etc.)
753 0 : SdrCaptionObj* pNoteCaption = 0;
754 0 : const ScPostIt* pNote = rDoc.GetNote( rPos );
755 0 : if( pNote && !pNote->IsCaptionShown() )
756 : {
757 0 : if( !aBuffer.isEmpty() )
758 0 : aBuffer.append( "\n--------\n" ).append( pNote->GetText() );
759 0 : pNoteCaption = pNote->GetOrCreateCaption( rPos );
760 : }
761 :
762 : // create a caption if any text exists
763 0 : if( !pNoteCaption && aBuffer.isEmpty() )
764 0 : return 0;
765 :
766 : // prepare visible rectangle (add default distance to all borders)
767 : Rectangle aVisRect(
768 0 : rVisRect.Left() + SC_NOTECAPTION_BORDERDIST_TEMP,
769 0 : rVisRect.Top() + SC_NOTECAPTION_BORDERDIST_TEMP,
770 0 : rVisRect.Right() - SC_NOTECAPTION_BORDERDIST_TEMP,
771 0 : rVisRect.Bottom() - SC_NOTECAPTION_BORDERDIST_TEMP );
772 :
773 : // create the caption object
774 0 : ScCaptionCreator aCreator( rDoc, rPos, true, bTailFront );
775 0 : SdrCaptionObj* pCaption = aCreator.GetCaption();
776 :
777 : // insert caption into page (needed to set caption text)
778 0 : rDrawPage.InsertObject( pCaption );
779 :
780 : // clone the edit text object, unless user text is present, then set this text
781 0 : if( pNoteCaption && rUserText.isEmpty() )
782 : {
783 0 : if( OutlinerParaObject* pOPO = pNoteCaption->GetOutlinerParaObject() )
784 0 : pCaption->SetOutlinerParaObject( new OutlinerParaObject( *pOPO ) );
785 : // set formatting (must be done after setting text) and resize the box to fit the text
786 0 : pCaption->SetMergedItemSetAndBroadcast( pNoteCaption->GetMergedItemSet() );
787 0 : Rectangle aCaptRect( pCaption->GetLogicRect().TopLeft(), pNoteCaption->GetLogicRect().GetSize() );
788 0 : pCaption->SetLogicRect( aCaptRect );
789 : }
790 : else
791 : {
792 : // if pNoteCaption is null, then aBuffer contains some text
793 0 : pCaption->SetText( aBuffer.makeStringAndClear() );
794 0 : ScCaptionUtil::SetDefaultItems( *pCaption, rDoc );
795 : // adjust caption size to text size
796 0 : long nMaxWidth = ::std::min< long >( aVisRect.GetWidth() * 2 / 3, SC_NOTECAPTION_MAXWIDTH_TEMP );
797 0 : pCaption->SetMergedItem( makeSdrTextAutoGrowWidthItem( true ) );
798 0 : pCaption->SetMergedItem( makeSdrTextMinFrameWidthItem( SC_NOTECAPTION_WIDTH ) );
799 0 : pCaption->SetMergedItem( makeSdrTextMaxFrameWidthItem( nMaxWidth ) );
800 0 : pCaption->SetMergedItem( makeSdrTextAutoGrowHeightItem( true ) );
801 0 : pCaption->AdjustTextFrameWidthAndHeight();
802 : }
803 :
804 : // move caption into visible area
805 0 : aCreator.AutoPlaceCaption( &aVisRect );
806 0 : return pCaption;
807 : }
808 :
809 3 : ScPostIt* ScNoteUtil::CreateNoteFromCaption(
810 : ScDocument& rDoc, const ScAddress& rPos, SdrCaptionObj& rCaption, bool bShown )
811 : {
812 3 : ScNoteData aNoteData( bShown );
813 3 : aNoteData.mpCaption = &rCaption;
814 3 : ScPostIt* pNote = new ScPostIt( rDoc, rPos, aNoteData, false );
815 3 : pNote->AutoStamp();
816 :
817 3 : rDoc.SetNote(rPos, pNote);
818 :
819 : // ScNoteCaptionCreator c'tor updates the caption object to be part of a note
820 3 : ScNoteCaptionCreator aCreator( rDoc, rPos, rCaption, bShown );
821 :
822 3 : return pNote;
823 : }
824 :
825 17 : ScPostIt* ScNoteUtil::CreateNoteFromObjectData(
826 : ScDocument& rDoc, const ScAddress& rPos, SfxItemSet* pItemSet,
827 : OutlinerParaObject* pOutlinerObj, const Rectangle& rCaptionRect,
828 : bool bShown, bool bAlwaysCreateCaption )
829 : {
830 : OSL_ENSURE( pItemSet && pOutlinerObj, "ScNoteUtil::CreateNoteFromObjectData - item set and outliner object expected" );
831 17 : ScNoteData aNoteData( bShown );
832 17 : aNoteData.mxInitData.reset( new ScCaptionInitData );
833 17 : ScCaptionInitData& rInitData = *aNoteData.mxInitData;
834 17 : rInitData.mxItemSet.reset( pItemSet );
835 17 : rInitData.mxOutlinerObj.reset( pOutlinerObj );
836 :
837 : // convert absolute caption position to relative position
838 17 : rInitData.mbDefaultPosSize = rCaptionRect.IsEmpty();
839 17 : if( !rInitData.mbDefaultPosSize )
840 : {
841 11 : Rectangle aCellRect = ScDrawLayer::GetCellRect( rDoc, rPos, true );
842 11 : bool bNegPage = rDoc.IsNegativePage( rPos.Tab() );
843 11 : rInitData.maCaptionOffset.X() = bNegPage ? (aCellRect.Left() - rCaptionRect.Right()) : (rCaptionRect.Left() - aCellRect.Right());
844 11 : rInitData.maCaptionOffset.Y() = rCaptionRect.Top() - aCellRect.Top();
845 11 : rInitData.maCaptionSize = rCaptionRect.GetSize();
846 : }
847 :
848 : /* Create the note and insert it into the document. If the note is
849 : visible, the caption object will be created automatically. */
850 17 : ScPostIt* pNote = new ScPostIt( rDoc, rPos, aNoteData, bAlwaysCreateCaption );
851 17 : pNote->AutoStamp();
852 :
853 17 : rDoc.SetNote(rPos, pNote);
854 :
855 17 : return pNote;
856 : }
857 :
858 20 : ScPostIt* ScNoteUtil::CreateNoteFromString(
859 : ScDocument& rDoc, const ScAddress& rPos, const OUString& rNoteText,
860 : bool bShown, bool bAlwaysCreateCaption )
861 : {
862 20 : ScPostIt* pNote = 0;
863 20 : if( !rNoteText.isEmpty() )
864 : {
865 20 : ScNoteData aNoteData( bShown );
866 20 : aNoteData.mxInitData.reset( new ScCaptionInitData );
867 20 : ScCaptionInitData& rInitData = *aNoteData.mxInitData;
868 20 : rInitData.maSimpleText = rNoteText;
869 20 : rInitData.mbDefaultPosSize = true;
870 :
871 : /* Create the note and insert it into the document. If the note is
872 : visible, the caption object will be created automatically. */
873 20 : pNote = new ScPostIt( rDoc, rPos, aNoteData, bAlwaysCreateCaption );
874 20 : pNote->AutoStamp();
875 : //insert takes ownership
876 20 : rDoc.SetNote(rPos, pNote);
877 : }
878 20 : return pNote;
879 : }
880 :
881 : namespace sc {
882 :
883 34 : NoteEntry::NoteEntry( const ScAddress& rPos, const ScPostIt* pNote ) :
884 34 : maPos(rPos), mpNote(pNote) {}
885 :
886 156 : }
887 :
888 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|