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 96 : void ScCaptionUtil::SetCaptionLayer( SdrCaptionObj& rCaption, bool bShown )
80 : {
81 96 : SdrLayerID nLayer = bShown ? SC_LAYER_INTERN : SC_LAYER_HIDDEN;
82 96 : if( nLayer != rCaption.GetLayer() )
83 96 : rCaption.SetLayer( nLayer );
84 96 : }
85 :
86 92 : void ScCaptionUtil::SetBasicCaptionSettings( SdrCaptionObj& rCaption, bool bShown )
87 : {
88 92 : SetCaptionLayer( rCaption, bShown );
89 92 : rCaption.SetFixedTail();
90 92 : rCaption.SetSpecialTextBoxShadow();
91 92 : }
92 :
93 92 : void ScCaptionUtil::SetCaptionUserData( SdrCaptionObj& rCaption, const ScAddress& rPos )
94 : {
95 : // pass true to ScDrawLayer::GetObjData() to create the object data entry
96 92 : ScDrawObjData* pObjData = ScDrawLayer::GetObjData( &rCaption, true );
97 : OSL_ENSURE( pObjData, "ScCaptionUtil::SetCaptionUserData - missing drawing object user data" );
98 92 : pObjData->maStart = rPos;
99 92 : pObjData->meType = ScDrawObjData::CellNote;
100 92 : }
101 :
102 78 : void ScCaptionUtil::SetDefaultItems( SdrCaptionObj& rCaption, ScDocument& rDoc )
103 : {
104 78 : SfxItemSet aItemSet = rCaption.GetMergedItemSet();
105 :
106 : // caption tail arrow
107 156 : ::basegfx::B2DPolygon aTriangle;
108 78 : aTriangle.append( ::basegfx::B2DPoint( 10.0, 0.0 ) );
109 78 : aTriangle.append( ::basegfx::B2DPoint( 0.0, 30.0 ) );
110 78 : aTriangle.append( ::basegfx::B2DPoint( 20.0, 30.0 ) );
111 78 : 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 78 : aItemSet.Put( XLineStartItem( OUString(), ::basegfx::B2DPolyPolygon( aTriangle ) ) );
116 78 : aItemSet.Put( XLineStartWidthItem( 200 ) );
117 78 : aItemSet.Put( XLineStartCenterItem( false ) );
118 78 : aItemSet.Put( XFillStyleItem( drawing::FillStyle_SOLID ) );
119 78 : aItemSet.Put( XFillColorItem( OUString(), ScDetectiveFunc::GetCommentColor() ) );
120 78 : 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 78 : aItemSet.Put( makeSdrShadowItem( false ) );
127 78 : aItemSet.Put( makeSdrShadowXDistItem( 100 ) );
128 78 : aItemSet.Put( makeSdrShadowYDistItem( 100 ) );
129 :
130 : // text attributes
131 78 : aItemSet.Put( makeSdrTextLeftDistItem( 100 ) );
132 78 : aItemSet.Put( makeSdrTextRightDistItem( 100 ) );
133 78 : aItemSet.Put( makeSdrTextUpperDistItem( 100 ) );
134 78 : aItemSet.Put( makeSdrTextLowerDistItem( 100 ) );
135 78 : aItemSet.Put( makeSdrTextAutoGrowWidthItem( false ) );
136 78 : aItemSet.Put( makeSdrTextAutoGrowHeightItem( true ) );
137 : // use the default cell style to be able to modify the caption font
138 78 : const ScPatternAttr& rDefPattern = static_cast< const ScPatternAttr& >( rDoc.GetPool()->GetDefaultItem( ATTR_PATTERN ) );
139 78 : rDefPattern.FillEditItemSet( &aItemSet );
140 :
141 156 : rCaption.SetMergedItemSet( aItemSet );
142 78 : }
143 :
144 18 : void ScCaptionUtil::SetCaptionItems( SdrCaptionObj& rCaption, const SfxItemSet& rItemSet )
145 : {
146 : // copy all items
147 18 : rCaption.SetMergedItemSet( rItemSet );
148 : // reset shadow items
149 18 : rCaption.SetMergedItem( makeSdrShadowItem( false ) );
150 18 : rCaption.SetMergedItem( makeSdrShadowXDistItem( 100 ) );
151 18 : rCaption.SetMergedItem( makeSdrShadowYDistItem( 100 ) );
152 18 : rCaption.SetSpecialTextBoxShadow();
153 18 : }
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 86 : 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 158 : 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 30 : ScCaptionCreator::ScCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, SdrCaptionObj& rCaption ) :
211 : mrDoc( rDoc ),
212 : maPos( rPos ),
213 30 : mpCaption( &rCaption )
214 : {
215 30 : Initialize();
216 30 : }
217 :
218 144 : ScCaptionCreator::ScCaptionCreator( ScDocument& rDoc, const ScAddress& rPos ) :
219 : mrDoc( rDoc ),
220 : maPos( rPos ),
221 144 : mpCaption( 0 )
222 : {
223 144 : Initialize();
224 144 : }
225 :
226 324 : SdrPage* ScCaptionCreator::GetDrawPage()
227 : {
228 324 : ScDrawLayer* pDrawLayer = mrDoc.GetDrawLayer();
229 324 : return pDrawLayer ? pDrawLayer->GetPage( static_cast< sal_uInt16 >( maPos.Tab() ) ) : 0;
230 : }
231 :
232 88 : void ScCaptionCreator::FitCaptionToRect( const Rectangle* pVisRect )
233 : {
234 88 : const Rectangle& rVisRect = GetVisRect( pVisRect );
235 :
236 : // tail position
237 88 : Point aTailPos = mpCaption->GetTailPos();
238 88 : aTailPos.X() = ::std::max( ::std::min( aTailPos.X(), rVisRect.Right() ), rVisRect.Left() );
239 88 : aTailPos.Y() = ::std::max( ::std::min( aTailPos.Y(), rVisRect.Bottom() ), rVisRect.Top() );
240 88 : mpCaption->SetTailPos( aTailPos );
241 :
242 : // caption rectangle
243 88 : Rectangle aCaptRect = mpCaption->GetLogicRect();
244 88 : Point aCaptPos = aCaptRect.TopLeft();
245 : // move textbox inside right border of visible area
246 88 : 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 88 : aCaptPos.X() = ::std::max< long >( aCaptPos.X(), rVisRect.Left() );
249 : // move textbox inside bottom border of visible area
250 88 : 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 88 : aCaptPos.Y() = ::std::max< long >( aCaptPos.Y(), rVisRect.Top() );
253 : // update caption
254 88 : aCaptRect.SetPos( aCaptPos );
255 88 : mpCaption->SetLogicRect( aCaptRect );
256 88 : }
257 :
258 70 : void ScCaptionCreator::AutoPlaceCaption( const Rectangle* pVisRect )
259 : {
260 70 : const Rectangle& rVisRect = GetVisRect( pVisRect );
261 :
262 : // caption rectangle
263 70 : Rectangle aCaptRect = mpCaption->GetLogicRect();
264 70 : long nWidth = aCaptRect.GetWidth();
265 70 : long nHeight = aCaptRect.GetHeight();
266 :
267 : // n***Space contains available space between border of visible area and cell
268 70 : long nLeftSpace = maCellRect.Left() - rVisRect.Left() + 1;
269 70 : long nRightSpace = rVisRect.Right() - maCellRect.Right() + 1;
270 70 : long nTopSpace = maCellRect.Top() - rVisRect.Top() + 1;
271 70 : long nBottomSpace = rVisRect.Bottom() - maCellRect.Bottom() + 1;
272 :
273 : // nNeeded*** contains textbox dimensions plus needed distances to cell or border of visible area
274 70 : long nNeededSpaceX = nWidth + SC_NOTECAPTION_CELLDIST;
275 70 : long nNeededSpaceY = nHeight + SC_NOTECAPTION_CELLDIST;
276 :
277 : // bFitsWidth*** == true means width of textbox fits into horizontal free space of visible area
278 70 : bool bFitsWidthLeft = nNeededSpaceX <= nLeftSpace; // text box width fits into the width left of cell
279 70 : bool bFitsWidthRight = nNeededSpaceX <= nRightSpace; // text box width fits into the width right of cell
280 70 : 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 70 : bool bFitsHeightTop = nNeededSpaceY <= nTopSpace; // text box height fits into the height above cell
284 70 : bool bFitsHeightBottom = nNeededSpaceY <= nBottomSpace; // text box height fits into the height below cell
285 70 : 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 70 : bool bFitsLeft = bFitsWidthLeft && bFitsHeight;
289 70 : bool bFitsRight = bFitsWidthRight && bFitsHeight;
290 70 : bool bFitsTop = bFitsWidth && bFitsHeightTop;
291 70 : bool bFitsBottom = bFitsWidth && bFitsHeightBottom;
292 :
293 70 : Point aCaptPos;
294 : // use left/right placement if possible, or if top/bottom placement not possible
295 70 : if( bFitsLeft || bFitsRight || (!bFitsTop && !bFitsBottom) )
296 : {
297 : // prefer left in RTL sheet and right in LTR sheets
298 70 : bool bPreferLeft = bFitsLeft && (mbNegPage || !bFitsRight);
299 70 : 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 70 : if( bPreferLeft || (!bPreferRight && (nLeftSpace > nRightSpace)) )
302 0 : aCaptPos.X() = maCellRect.Left() - SC_NOTECAPTION_CELLDIST - nWidth;
303 : else // to right
304 70 : aCaptPos.X() = maCellRect.Right() + SC_NOTECAPTION_CELLDIST;
305 : // Y position according to top cell border
306 70 : 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 70 : aCaptRect.SetPos( aCaptPos );
321 70 : mpCaption->SetLogicRect( aCaptRect );
322 70 : FitCaptionToRect( pVisRect );
323 70 : }
324 :
325 24 : void ScCaptionCreator::UpdateCaptionPos( const Rectangle* pVisRect )
326 : {
327 24 : ScDrawLayer* pDrawLayer = mrDoc.GetDrawLayer();
328 :
329 : // update caption position
330 24 : const Point& rOldTailPos = mpCaption->GetTailPos();
331 24 : Point aTailPos = CalcTailPos( false );
332 24 : if( rOldTailPos != aTailPos )
333 : {
334 : // create drawing undo action
335 2 : if( pDrawLayer && pDrawLayer->IsRecording() )
336 0 : pDrawLayer->AddCalcUndo( new SdrUndoGeoObj( *mpCaption ) );
337 : // calculate new caption rectangle (#i98141# handle LTR<->RTL switch correctly)
338 2 : Rectangle aCaptRect = mpCaption->GetLogicRect();
339 2 : long nDiffX = (rOldTailPos.X() >= 0) ? (aCaptRect.Left() - rOldTailPos.X()) : (rOldTailPos.X() - aCaptRect.Right());
340 2 : if( mbNegPage ) nDiffX = -nDiffX - aCaptRect.GetWidth();
341 2 : long nDiffY = aCaptRect.Top() - rOldTailPos.Y();
342 2 : aCaptRect.SetPos( aTailPos + Point( nDiffX, nDiffY ) );
343 : // set new tail position and caption rectangle
344 2 : mpCaption->SetTailPos( aTailPos );
345 2 : mpCaption->SetLogicRect( aCaptRect );
346 : // fit caption into draw page
347 2 : FitCaptionToRect( pVisRect );
348 : }
349 :
350 : // update cell position in caption user data
351 24 : ScDrawObjData* pCaptData = ScDrawLayer::GetNoteCaptionData( mpCaption, maPos.Tab() );
352 24 : if( pCaptData && (maPos != pCaptData->maStart) )
353 : {
354 : // create drawing undo action
355 2 : if( pDrawLayer && pDrawLayer->IsRecording() )
356 0 : pDrawLayer->AddCalcUndo( new ScUndoObjData( mpCaption, pCaptData->maStart, pCaptData->maEnd, maPos, pCaptData->maEnd ) );
357 : // set new position
358 2 : pCaptData->maStart = maPos;
359 : }
360 24 : }
361 :
362 116 : Point ScCaptionCreator::CalcTailPos( bool bTailFront )
363 : {
364 : // tail position
365 116 : bool bTailLeft = bTailFront != mbNegPage;
366 116 : Point aTailPos = bTailLeft ? maCellRect.TopLeft() : maCellRect.TopRight();
367 : // move caption point 1/10 mm inside cell
368 116 : if( bTailLeft ) aTailPos.X() += 10; else aTailPos.X() -= 10;
369 116 : aTailPos.Y() += 10;
370 116 : return aTailPos;
371 : }
372 :
373 86 : void ScCaptionCreator::CreateCaption( bool bShown, bool bTailFront )
374 : {
375 : // create the caption drawing object
376 86 : Rectangle aTextRect( Point( 0 , 0 ), Size( SC_NOTECAPTION_WIDTH, SC_NOTECAPTION_HEIGHT ) );
377 86 : Point aTailPos = CalcTailPos( bTailFront );
378 86 : mpCaption = new SdrCaptionObj( aTextRect, aTailPos );
379 : // basic caption settings
380 86 : ScCaptionUtil::SetBasicCaptionSettings( *mpCaption, bShown );
381 86 : }
382 :
383 174 : void ScCaptionCreator::Initialize()
384 : {
385 174 : maCellRect = ScDrawLayer::GetCellRect( mrDoc, maPos, true );
386 174 : mbNegPage = mrDoc.IsNegativePage( maPos.Tab() );
387 174 : if( SdrPage* pDrawPage = GetDrawPage() )
388 : {
389 116 : 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 116 : maPageRect.Justify();
394 : }
395 174 : }
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 144 : ScNoteCaptionCreator::ScNoteCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, ScNoteData& rNoteData ) :
408 144 : ScCaptionCreator( rDoc, rPos ) // use helper c'tor that does not create the caption yet
409 : {
410 144 : SdrPage* pDrawPage = GetDrawPage();
411 : OSL_ENSURE( pDrawPage, "ScNoteCaptionCreator::ScNoteCaptionCreator - no drawing page" );
412 144 : if( pDrawPage )
413 : {
414 : // create the caption drawing object
415 86 : CreateCaption( rNoteData.mbShown, false );
416 86 : rNoteData.mpCaption = GetCaption();
417 : OSL_ENSURE( rNoteData.mpCaption, "ScNoteCaptionCreator::ScNoteCaptionCreator - missing caption object" );
418 86 : if( rNoteData.mpCaption )
419 : {
420 : // store note position in user data of caption object
421 86 : ScCaptionUtil::SetCaptionUserData( *rNoteData.mpCaption, rPos );
422 : // insert object into draw page
423 86 : pDrawPage->InsertObject( rNoteData.mpCaption );
424 : }
425 : }
426 144 : }
427 :
428 6 : ScNoteCaptionCreator::ScNoteCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, SdrCaptionObj& rCaption, bool bShown ) :
429 6 : ScCaptionCreator( rDoc, rPos, rCaption )
430 : {
431 6 : 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 6 : if( pDrawPage && (rCaption.GetPage() == pDrawPage) )
435 : {
436 : // store note position in user data of caption object
437 6 : ScCaptionUtil::SetCaptionUserData( rCaption, rPos );
438 : // basic caption settings
439 6 : ScCaptionUtil::SetBasicCaptionSettings( rCaption, bShown );
440 : // set correct tail position
441 6 : rCaption.SetTailPos( CalcTailPos( false ) );
442 : }
443 6 : }
444 :
445 : } // namespace
446 :
447 70 : 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 70 : ScCaptionInitData::ScCaptionInitData() :
463 70 : mbDefaultPosSize( true )
464 : {
465 70 : }
466 :
467 190 : ScNoteData::ScNoteData( bool bShown ) :
468 : mpCaption( 0 ),
469 190 : mbShown( bShown )
470 : {
471 190 : }
472 :
473 422 : ScNoteData::~ScNoteData()
474 : {
475 422 : }
476 :
477 54 : ScPostIt::ScPostIt( ScDocument& rDoc, const ScAddress& rPos, bool bShown ) :
478 : mrDoc( rDoc ),
479 54 : maNoteData( bShown )
480 : {
481 54 : AutoStamp();
482 54 : CreateCaption( rPos );
483 54 : }
484 :
485 52 : ScPostIt::ScPostIt( ScDocument& rDoc, const ScAddress& rPos, const ScPostIt& rNote ) :
486 : mrDoc( rDoc ),
487 52 : maNoteData( rNote.maNoteData )
488 : {
489 52 : maNoteData.mpCaption = 0;
490 52 : CreateCaption( rPos, rNote.maNoteData.mpCaption );
491 52 : }
492 :
493 134 : ScPostIt::ScPostIt( ScDocument& rDoc, const ScAddress& rPos, const ScNoteData& rNoteData, bool bAlwaysCreateCaption ) :
494 : mrDoc( rDoc ),
495 134 : maNoteData( rNoteData )
496 : {
497 134 : if( bAlwaysCreateCaption || maNoteData.mbShown )
498 46 : CreateCaptionFromInitData( rPos );
499 134 : }
500 :
501 468 : ScPostIt::~ScPostIt()
502 : {
503 234 : RemoveCaption();
504 234 : }
505 :
506 110 : ScPostIt* ScPostIt::Clone( const ScAddress& rOwnPos, ScDocument& rDestDoc, const ScAddress& rDestPos, bool bCloneCaption ) const
507 : {
508 110 : CreateCaptionFromInitData( rOwnPos );
509 110 : return bCloneCaption ? new ScPostIt( rDestDoc, rDestPos, *this ) : new ScPostIt( rDestDoc, rDestPos, maNoteData, false );
510 : }
511 :
512 32 : void ScPostIt::SetDate( const OUString& rDate )
513 : {
514 32 : maNoteData.maDate = rDate;
515 32 : }
516 :
517 44 : void ScPostIt::SetAuthor( const OUString& rAuthor )
518 : {
519 44 : maNoteData.maAuthor = rAuthor;
520 44 : }
521 :
522 130 : void ScPostIt::AutoStamp()
523 : {
524 130 : maNoteData.maDate = ScGlobal::pLocaleData->getDate( Date( Date::SYSTEM ) );
525 130 : maNoteData.maAuthor = SvtUserOptions().GetID();
526 130 : }
527 :
528 90 : const OutlinerParaObject* ScPostIt::GetOutlinerObject() const
529 : {
530 90 : if( maNoteData.mpCaption )
531 30 : return maNoteData.mpCaption->GetOutlinerParaObject();
532 60 : if( maNoteData.mxInitData.get() )
533 20 : return maNoteData.mxInitData->mxOutlinerObj.get();
534 40 : return 0;
535 : }
536 :
537 90 : const EditTextObject* ScPostIt::GetEditTextObject() const
538 : {
539 90 : const OutlinerParaObject* pOPO = GetOutlinerObject();
540 90 : return pOPO ? &pOPO->GetTextObject() : 0;
541 : }
542 :
543 76 : OUString ScPostIt::GetText() const
544 : {
545 76 : if( const EditTextObject* pEditObj = GetEditTextObject() )
546 : {
547 32 : OUStringBuffer aBuffer;
548 64 : for( sal_Int32 nPara = 0, nParaCount = pEditObj->GetParagraphCount(); nPara < nParaCount; ++nPara )
549 : {
550 32 : if( nPara > 0 )
551 0 : aBuffer.append( '\n' );
552 32 : aBuffer.append( pEditObj->GetText( nPara ) );
553 : }
554 32 : return aBuffer.makeStringAndClear();
555 : }
556 44 : if( maNoteData.mxInitData.get() )
557 4 : return maNoteData.mxInitData->maSimpleText;
558 40 : return OUString();
559 : }
560 :
561 46 : void ScPostIt::SetText( const ScAddress& rPos, const OUString& rText )
562 : {
563 46 : CreateCaptionFromInitData( rPos );
564 46 : if( maNoteData.mpCaption )
565 12 : maNoteData.mpCaption->SetText( rText );
566 46 : }
567 :
568 28 : SdrCaptionObj* ScPostIt::GetOrCreateCaption( const ScAddress& rPos ) const
569 : {
570 28 : CreateCaptionFromInitData( rPos );
571 28 : return maNoteData.mpCaption;
572 : }
573 :
574 20 : 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 20 : maNoteData.mpCaption = 0;
579 20 : maNoteData.mxInitData.reset();
580 20 : }
581 :
582 4 : void ScPostIt::ShowCaption( const ScAddress& rPos, bool bShow )
583 : {
584 4 : CreateCaptionFromInitData( rPos );
585 : // no separate drawing undo needed, handled completely inside ScUndoShowHideNote
586 4 : maNoteData.mbShown = bShow;
587 4 : if( maNoteData.mpCaption )
588 4 : ScCaptionUtil::SetCaptionLayer( *maNoteData.mpCaption, bShow );
589 4 : }
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 30 : void ScPostIt::UpdateCaptionPos( const ScAddress& rPos )
599 : {
600 30 : CreateCaptionFromInitData( rPos );
601 30 : if( maNoteData.mpCaption )
602 : {
603 24 : ScCaptionCreator aCreator( mrDoc, rPos, *maNoteData.mpCaption );
604 24 : aCreator.UpdateCaptionPos();
605 : }
606 30 : }
607 :
608 : // private --------------------------------------------------------------------
609 :
610 264 : 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 264 : 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 52 : if( !maNoteData.mpCaption && !mrDoc.IsUndo() )
625 : {
626 : // ScNoteCaptionCreator c'tor creates the caption and inserts it into the document and maNoteData
627 52 : ScNoteCaptionCreator aCreator( mrDoc, rPos, maNoteData );
628 52 : if( maNoteData.mpCaption )
629 : {
630 52 : 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 52 : if( rInitData.mxOutlinerObj.get() )
636 18 : maNoteData.mpCaption->SetOutlinerParaObject( rInitData.mxOutlinerObj.release() );
637 : else
638 34 : maNoteData.mpCaption->SetText( rInitData.maSimpleText );
639 :
640 : // copy all items or set default items; reset shadow items
641 52 : ScCaptionUtil::SetDefaultItems( *maNoteData.mpCaption, mrDoc );
642 52 : if( rInitData.mxItemSet.get() )
643 18 : ScCaptionUtil::SetCaptionItems( *maNoteData.mpCaption, *rInitData.mxItemSet );
644 :
645 : // set position and size of the caption object
646 52 : if( rInitData.mbDefaultPosSize )
647 : {
648 : // set other items and fit caption size to text
649 44 : maNoteData.mpCaption->SetMergedItem( makeSdrTextMinFrameWidthItem( SC_NOTECAPTION_WIDTH ) );
650 44 : maNoteData.mpCaption->SetMergedItem( makeSdrTextMaxFrameWidthItem( SC_NOTECAPTION_MAXWIDTH_TEMP ) );
651 44 : maNoteData.mpCaption->AdjustTextFrameWidthAndHeight();
652 44 : aCreator.AutoPlaceCaption();
653 : }
654 : else
655 : {
656 8 : Rectangle aCellRect = ScDrawLayer::GetCellRect( mrDoc, rPos, true );
657 8 : bool bNegPage = mrDoc.IsNegativePage( rPos.Tab() );
658 8 : long nPosX = bNegPage ? (aCellRect.Left() - rInitData.maCaptionOffset.X()) : (aCellRect.Right() + rInitData.maCaptionOffset.X());
659 8 : long nPosY = aCellRect.Top() + rInitData.maCaptionOffset.Y();
660 8 : Rectangle aCaptRect( Point( nPosX, nPosY ), rInitData.maCaptionSize );
661 8 : maNoteData.mpCaption->SetLogicRect( aCaptRect );
662 8 : aCreator.FitCaptionToRect();
663 : }
664 : }
665 : }
666 : // forget the initial caption data struct
667 52 : maNoteData.mxInitData.reset();
668 : }
669 264 : }
670 :
671 106 : void ScPostIt::CreateCaption( const ScAddress& rPos, const SdrCaptionObj* pCaption )
672 : {
673 : OSL_ENSURE( !maNoteData.mpCaption, "ScPostIt::CreateCaption - unexpected caption object found" );
674 106 : 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 106 : if( mrDoc.IsUndo() )
680 120 : return;
681 :
682 : // drawing layer may be missing, if a note is copied into a clipboard document
683 92 : if( mrDoc.IsClipboard() )
684 6 : mrDoc.InitDrawLayer();
685 :
686 : // ScNoteCaptionCreator c'tor creates the caption and inserts it into the document and maNoteData
687 92 : ScNoteCaptionCreator aCreator( mrDoc, rPos, maNoteData );
688 92 : if( maNoteData.mpCaption )
689 : {
690 : // clone settings of passed caption
691 34 : if( pCaption )
692 : {
693 : // copy edit text object (object must be inserted into page already)
694 8 : if( OutlinerParaObject* pOPO = pCaption->GetOutlinerParaObject() )
695 8 : maNoteData.mpCaption->SetOutlinerParaObject( new OutlinerParaObject( *pOPO ) );
696 : // copy formatting items (after text has been copied to apply font formatting)
697 8 : maNoteData.mpCaption->SetMergedItemSetAndBroadcast( pCaption->GetMergedItemSet() );
698 : // move textbox position relative to new cell, copy textbox size
699 8 : Rectangle aCaptRect = pCaption->GetLogicRect();
700 8 : Point aDist = maNoteData.mpCaption->GetTailPos() - pCaption->GetTailPos();
701 8 : aCaptRect.Move( aDist.X(), aDist.Y() );
702 8 : maNoteData.mpCaption->SetLogicRect( aCaptRect );
703 8 : aCreator.FitCaptionToRect();
704 : }
705 : else
706 : {
707 : // set default formatting and default position
708 26 : ScCaptionUtil::SetDefaultItems( *maNoteData.mpCaption, mrDoc );
709 26 : aCreator.AutoPlaceCaption();
710 : }
711 :
712 : // create undo action
713 34 : if( ScDrawLayer* pDrawLayer = mrDoc.GetDrawLayer() )
714 34 : if( pDrawLayer->IsRecording() )
715 0 : pDrawLayer->AddCalcUndo( new SdrUndoNewObj( *maNoteData.mpCaption ) );
716 : }
717 : }
718 :
719 234 : 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 234 : ScDrawLayer* pDrawLayer = mrDoc.GetDrawLayer();
726 234 : if( maNoteData.mpCaption && (pDrawLayer == maNoteData.mpCaption->GetModel()) )
727 : {
728 : OSL_ENSURE( pDrawLayer, "ScPostIt::RemoveCaption - object without drawing layer" );
729 84 : SdrPage* pDrawPage = maNoteData.mpCaption->GetPage();
730 : OSL_ENSURE( pDrawPage, "ScPostIt::RemoveCaption - object without drawing page" );
731 84 : if( pDrawPage )
732 : {
733 84 : pDrawPage->RecalcObjOrdNums();
734 : // create drawing undo action (before removing the object to have valid draw page in undo action)
735 84 : bool bRecording = ( pDrawLayer && pDrawLayer->IsRecording() );
736 84 : if( bRecording )
737 6 : pDrawLayer->AddCalcUndo( new SdrUndoDelObj( *maNoteData.mpCaption ) );
738 : // remove the object from the drawing page, delete if undo is disabled
739 84 : SdrObject* pObj = pDrawPage->RemoveObject( maNoteData.mpCaption->GetOrdNum() );
740 84 : if( !bRecording )
741 78 : SdrObject::Free( pObj );
742 : }
743 : }
744 234 : maNoteData.mpCaption = 0;
745 234 : }
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 6 : ScPostIt* ScNoteUtil::CreateNoteFromCaption(
810 : ScDocument& rDoc, const ScAddress& rPos, SdrCaptionObj& rCaption, bool bShown )
811 : {
812 6 : ScNoteData aNoteData( bShown );
813 6 : aNoteData.mpCaption = &rCaption;
814 6 : ScPostIt* pNote = new ScPostIt( rDoc, rPos, aNoteData, false );
815 6 : pNote->AutoStamp();
816 :
817 6 : rDoc.SetNote(rPos, pNote);
818 :
819 : // ScNoteCaptionCreator c'tor updates the caption object to be part of a note
820 6 : ScNoteCaptionCreator aCreator( rDoc, rPos, rCaption, bShown );
821 :
822 6 : return pNote;
823 : }
824 :
825 34 : 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 34 : ScNoteData aNoteData( bShown );
832 34 : aNoteData.mxInitData.reset( new ScCaptionInitData );
833 34 : ScCaptionInitData& rInitData = *aNoteData.mxInitData;
834 34 : rInitData.mxItemSet.reset( pItemSet );
835 34 : rInitData.mxOutlinerObj.reset( pOutlinerObj );
836 :
837 : // convert absolute caption position to relative position
838 34 : rInitData.mbDefaultPosSize = rCaptionRect.IsEmpty();
839 34 : if( !rInitData.mbDefaultPosSize )
840 : {
841 22 : Rectangle aCellRect = ScDrawLayer::GetCellRect( rDoc, rPos, true );
842 22 : bool bNegPage = rDoc.IsNegativePage( rPos.Tab() );
843 22 : rInitData.maCaptionOffset.X() = bNegPage ? (aCellRect.Left() - rCaptionRect.Right()) : (rCaptionRect.Left() - aCellRect.Right());
844 22 : rInitData.maCaptionOffset.Y() = rCaptionRect.Top() - aCellRect.Top();
845 22 : 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 34 : ScPostIt* pNote = new ScPostIt( rDoc, rPos, aNoteData, bAlwaysCreateCaption );
851 34 : pNote->AutoStamp();
852 :
853 34 : rDoc.SetNote(rPos, pNote);
854 :
855 34 : return pNote;
856 : }
857 :
858 36 : ScPostIt* ScNoteUtil::CreateNoteFromString(
859 : ScDocument& rDoc, const ScAddress& rPos, const OUString& rNoteText,
860 : bool bShown, bool bAlwaysCreateCaption )
861 : {
862 36 : ScPostIt* pNote = 0;
863 36 : if( !rNoteText.isEmpty() )
864 : {
865 36 : ScNoteData aNoteData( bShown );
866 36 : aNoteData.mxInitData.reset( new ScCaptionInitData );
867 36 : ScCaptionInitData& rInitData = *aNoteData.mxInitData;
868 36 : rInitData.maSimpleText = rNoteText;
869 36 : 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 36 : pNote = new ScPostIt( rDoc, rPos, aNoteData, bAlwaysCreateCaption );
874 36 : pNote->AutoStamp();
875 : //insert takes ownership
876 36 : rDoc.SetNote(rPos, pNote);
877 : }
878 36 : return pNote;
879 : }
880 :
881 : namespace sc {
882 :
883 68 : NoteEntry::NoteEntry( const ScAddress& rPos, const ScPostIt* pNote ) :
884 68 : maPos(rPos), mpNote(pNote) {}
885 :
886 228 : }
887 :
888 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|