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 :
21 : #include "postit.hxx"
22 :
23 : #include <rtl/ustrbuf.hxx>
24 : #include <unotools/useroptions.hxx>
25 : #include <svx/svdpage.hxx>
26 : #include <svx/svdocapt.hxx>
27 : #include <editeng/outlobj.hxx>
28 : #include <editeng/editobj.hxx>
29 : #include <basegfx/polygon/b2dpolygon.hxx>
30 :
31 : #include "scitems.hxx"
32 : #include <svx/xlnstit.hxx>
33 : #include <svx/xlnstwit.hxx>
34 : #include <svx/xlnstcit.hxx>
35 : #include <svx/sxcecitm.hxx>
36 : #include <svx/xflclit.hxx>
37 : #include <svx/sdshitm.hxx>
38 : #include <svx/sdsxyitm.hxx>
39 : #include <tools/gen.hxx>
40 :
41 : #include "table.hxx"
42 : #include "document.hxx"
43 : #include "docpool.hxx"
44 : #include "patattr.hxx"
45 : #include "formulacell.hxx"
46 : #include "drwlayer.hxx"
47 : #include "userdat.hxx"
48 : #include "detfunc.hxx"
49 :
50 : #include <utility>
51 :
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 36 : void ScCaptionUtil::SetCaptionLayer( SdrCaptionObj& rCaption, bool bShown )
80 : {
81 36 : SdrLayerID nLayer = bShown ? SC_LAYER_INTERN : SC_LAYER_HIDDEN;
82 36 : if( nLayer != rCaption.GetLayer() )
83 36 : rCaption.SetLayer( nLayer );
84 36 : }
85 :
86 35 : void ScCaptionUtil::SetBasicCaptionSettings( SdrCaptionObj& rCaption, bool bShown )
87 : {
88 35 : SetCaptionLayer( rCaption, bShown );
89 35 : rCaption.SetFixedTail();
90 35 : rCaption.SetSpecialTextBoxShadow();
91 35 : }
92 :
93 35 : void ScCaptionUtil::SetCaptionUserData( SdrCaptionObj& rCaption, const ScAddress& rPos )
94 : {
95 : // pass true to ScDrawLayer::GetObjData() to create the object data entry
96 35 : ScDrawObjData* pObjData = ScDrawLayer::GetObjData( &rCaption, true );
97 : OSL_ENSURE( pObjData, "ScCaptionUtil::SetCaptionUserData - missing drawing object user data" );
98 35 : pObjData->maStart = rPos;
99 35 : pObjData->meType = ScDrawObjData::CellNote;
100 35 : }
101 :
102 30 : void ScCaptionUtil::SetDefaultItems( SdrCaptionObj& rCaption, ScDocument& rDoc )
103 : {
104 30 : SfxItemSet aItemSet = rCaption.GetMergedItemSet();
105 :
106 : // caption tail arrow
107 60 : ::basegfx::B2DPolygon aTriangle;
108 30 : aTriangle.append( ::basegfx::B2DPoint( 10.0, 0.0 ) );
109 30 : aTriangle.append( ::basegfx::B2DPoint( 0.0, 30.0 ) );
110 30 : aTriangle.append( ::basegfx::B2DPoint( 20.0, 30.0 ) );
111 30 : 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 30 : aItemSet.Put( XLineStartItem( OUString(), ::basegfx::B2DPolyPolygon( aTriangle ) ) );
116 30 : aItemSet.Put( XLineStartWidthItem( 200 ) );
117 30 : aItemSet.Put( XLineStartCenterItem( false ) );
118 30 : aItemSet.Put( XFillStyleItem( XFILL_SOLID ) );
119 30 : aItemSet.Put( XFillColorItem( OUString(), ScDetectiveFunc::GetCommentColor() ) );
120 30 : 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 30 : aItemSet.Put( SdrShadowItem( false ) );
127 30 : aItemSet.Put( SdrShadowXDistItem( 100 ) );
128 30 : aItemSet.Put( SdrShadowYDistItem( 100 ) );
129 :
130 : // text attributes
131 30 : aItemSet.Put( SdrTextLeftDistItem( 100 ) );
132 30 : aItemSet.Put( SdrTextRightDistItem( 100 ) );
133 30 : aItemSet.Put( SdrTextUpperDistItem( 100 ) );
134 30 : aItemSet.Put( SdrTextLowerDistItem( 100 ) );
135 30 : aItemSet.Put( SdrTextAutoGrowWidthItem( false ) );
136 30 : aItemSet.Put( SdrTextAutoGrowHeightItem( true ) );
137 : // use the default cell style to be able to modify the caption font
138 30 : const ScPatternAttr& rDefPattern = static_cast< const ScPatternAttr& >( rDoc.GetPool()->GetDefaultItem( ATTR_PATTERN ) );
139 30 : rDefPattern.FillEditItemSet( &aItemSet );
140 :
141 60 : rCaption.SetMergedItemSet( aItemSet );
142 30 : }
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( SdrShadowItem( false ) );
150 9 : rCaption.SetMergedItem( SdrShadowXDistItem( 100 ) );
151 9 : rCaption.SetMergedItem( SdrShadowYDistItem( 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 32 : 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 59 : 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 61 : ScCaptionCreator::ScCaptionCreator( ScDocument& rDoc, const ScAddress& rPos ) :
219 : mrDoc( rDoc ),
220 : maPos( rPos ),
221 61 : mpCaption( 0 )
222 : {
223 61 : Initialize();
224 61 : }
225 :
226 140 : SdrPage* ScCaptionCreator::GetDrawPage()
227 : {
228 140 : ScDrawLayer* pDrawLayer = mrDoc.GetDrawLayer();
229 140 : return pDrawLayer ? pDrawLayer->GetPage( static_cast< sal_uInt16 >( maPos.Tab() ) ) : 0;
230 : }
231 :
232 33 : void ScCaptionCreator::FitCaptionToRect( const Rectangle* pVisRect )
233 : {
234 33 : const Rectangle& rVisRect = GetVisRect( pVisRect );
235 :
236 : // tail position
237 33 : Point aTailPos = mpCaption->GetTailPos();
238 33 : aTailPos.X() = ::std::max( ::std::min( aTailPos.X(), rVisRect.Right() ), rVisRect.Left() );
239 33 : aTailPos.Y() = ::std::max( ::std::min( aTailPos.Y(), rVisRect.Bottom() ), rVisRect.Top() );
240 33 : mpCaption->SetTailPos( aTailPos );
241 :
242 : // caption rectangle
243 33 : Rectangle aCaptRect = mpCaption->GetLogicRect();
244 33 : Point aCaptPos = aCaptRect.TopLeft();
245 : // move textbox inside right border of visible area
246 33 : 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 33 : aCaptPos.X() = ::std::max< long >( aCaptPos.X(), rVisRect.Left() );
249 : // move textbox inside bottom border of visible area
250 33 : 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 33 : aCaptPos.Y() = ::std::max< long >( aCaptPos.Y(), rVisRect.Top() );
253 : // update caption
254 33 : aCaptRect.SetPos( aCaptPos );
255 33 : mpCaption->SetLogicRect( aCaptRect );
256 33 : }
257 :
258 26 : void ScCaptionCreator::AutoPlaceCaption( const Rectangle* pVisRect )
259 : {
260 26 : const Rectangle& rVisRect = GetVisRect( pVisRect );
261 :
262 : // caption rectangle
263 26 : Rectangle aCaptRect = mpCaption->GetLogicRect();
264 26 : long nWidth = aCaptRect.GetWidth();
265 26 : long nHeight = aCaptRect.GetHeight();
266 :
267 : // n***Space contains available space between border of visible area and cell
268 26 : long nLeftSpace = maCellRect.Left() - rVisRect.Left() + 1;
269 26 : long nRightSpace = rVisRect.Right() - maCellRect.Right() + 1;
270 26 : long nTopSpace = maCellRect.Top() - rVisRect.Top() + 1;
271 26 : long nBottomSpace = rVisRect.Bottom() - maCellRect.Bottom() + 1;
272 :
273 : // nNeeded*** contains textbox dimensions plus needed distances to cell or border of visible area
274 26 : long nNeededSpaceX = nWidth + SC_NOTECAPTION_CELLDIST;
275 26 : long nNeededSpaceY = nHeight + SC_NOTECAPTION_CELLDIST;
276 :
277 : // bFitsWidth*** == true means width of textbox fits into horizontal free space of visible area
278 26 : bool bFitsWidthLeft = nNeededSpaceX <= nLeftSpace; // text box width fits into the width left of cell
279 26 : bool bFitsWidthRight = nNeededSpaceX <= nRightSpace; // text box width fits into the width right of cell
280 26 : 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 26 : bool bFitsHeightTop = nNeededSpaceY <= nTopSpace; // text box height fits into the height above cell
284 26 : bool bFitsHeightBottom = nNeededSpaceY <= nBottomSpace; // text box height fits into the height below cell
285 26 : 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 26 : bool bFitsLeft = bFitsWidthLeft && bFitsHeight;
289 26 : bool bFitsRight = bFitsWidthRight && bFitsHeight;
290 26 : bool bFitsTop = bFitsWidth && bFitsHeightTop;
291 26 : bool bFitsBottom = bFitsWidth && bFitsHeightBottom;
292 :
293 26 : Point aCaptPos;
294 : // use left/right placement if possible, or if top/bottom placement not possible
295 26 : if( bFitsLeft || bFitsRight || (!bFitsTop && !bFitsBottom) )
296 : {
297 : // prefer left in RTL sheet and right in LTR sheets
298 26 : bool bPreferLeft = bFitsLeft && (mbNegPage || !bFitsRight);
299 26 : 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 26 : if( bPreferLeft || (!bPreferRight && (nLeftSpace > nRightSpace)) )
302 0 : aCaptPos.X() = maCellRect.Left() - SC_NOTECAPTION_CELLDIST - nWidth;
303 : else // to right
304 26 : aCaptPos.X() = maCellRect.Right() + SC_NOTECAPTION_CELLDIST;
305 : // Y position according to top cell border
306 26 : 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 26 : aCaptRect.SetPos( aCaptPos );
321 26 : mpCaption->SetLogicRect( aCaptRect );
322 26 : FitCaptionToRect( pVisRect );
323 26 : }
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( pDrawLayer->GetSdrUndoFactory().CreateUndoGeoObject( *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 47 : Point ScCaptionCreator::CalcTailPos( bool bTailFront )
363 : {
364 : // tail position
365 47 : bool bTailLeft = bTailFront != mbNegPage;
366 47 : Point aTailPos = bTailLeft ? maCellRect.TopLeft() : maCellRect.TopRight();
367 : // move caption point 1/10 mm inside cell
368 47 : if( bTailLeft ) aTailPos.X() += 10; else aTailPos.X() -= 10;
369 47 : aTailPos.Y() += 10;
370 47 : return aTailPos;
371 : }
372 :
373 32 : void ScCaptionCreator::CreateCaption( bool bShown, bool bTailFront )
374 : {
375 : // create the caption drawing object
376 32 : Rectangle aTextRect( Point( 0 , 0 ), Size( SC_NOTECAPTION_WIDTH, SC_NOTECAPTION_HEIGHT ) );
377 32 : Point aTailPos = CalcTailPos( bTailFront );
378 32 : mpCaption = new SdrCaptionObj( aTextRect, aTailPos );
379 : // basic caption settings
380 32 : ScCaptionUtil::SetBasicCaptionSettings( *mpCaption, bShown );
381 32 : }
382 :
383 76 : void ScCaptionCreator::Initialize()
384 : {
385 76 : maCellRect = ScDrawLayer::GetCellRect( mrDoc, maPos, true );
386 76 : mbNegPage = mrDoc.IsNegativePage( maPos.Tab() );
387 76 : if( SdrPage* pDrawPage = GetDrawPage() )
388 : {
389 47 : 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 47 : maPageRect.Justify();
394 : }
395 76 : }
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 61 : ScNoteCaptionCreator::ScNoteCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, ScNoteData& rNoteData ) :
408 61 : ScCaptionCreator( rDoc, rPos ) // use helper c'tor that does not create the caption yet
409 : {
410 61 : SdrPage* pDrawPage = GetDrawPage();
411 : OSL_ENSURE( pDrawPage, "ScNoteCaptionCreator::ScNoteCaptionCreator - no drawing page" );
412 61 : if( pDrawPage )
413 : {
414 : // create the caption drawing object
415 32 : CreateCaption( rNoteData.mbShown, false );
416 32 : rNoteData.mpCaption = GetCaption();
417 : OSL_ENSURE( rNoteData.mpCaption, "ScNoteCaptionCreator::ScNoteCaptionCreator - missing caption object" );
418 32 : if( rNoteData.mpCaption )
419 : {
420 : // store note position in user data of caption object
421 32 : ScCaptionUtil::SetCaptionUserData( *rNoteData.mpCaption, rPos );
422 : // insert object into draw page
423 32 : pDrawPage->InsertObject( rNoteData.mpCaption );
424 : }
425 : }
426 61 : }
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 27 : struct ScCaptionInitData
448 : {
449 : typedef ::std::auto_ptr< SfxItemSet > SfxItemSetPtr;
450 : typedef ::std::auto_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 27 : ScCaptionInitData::ScCaptionInitData() :
463 27 : mbDefaultPosSize( true )
464 : {
465 27 : }
466 :
467 71 : ScNoteData::ScNoteData( bool bShown ) :
468 : mpCaption( 0 ),
469 71 : mbShown( bShown )
470 : {
471 71 : }
472 :
473 142 : ScNoteData::~ScNoteData()
474 : {
475 142 : }
476 :
477 25 : ScPostIt::ScPostIt( ScDocument& rDoc, const ScAddress& rPos, bool bShown ) :
478 : mrDoc( rDoc ),
479 25 : maNoteData( bShown )
480 : {
481 25 : AutoStamp();
482 25 : CreateCaption( rPos );
483 25 : }
484 :
485 24 : ScPostIt::ScPostIt( ScDocument& rDoc, const ScAddress& rPos, const ScPostIt& rNote ) :
486 : mrDoc( rDoc ),
487 24 : maNoteData( rNote.maNoteData )
488 : {
489 24 : maNoteData.mpCaption = 0;
490 24 : CreateCaption( rPos, rNote.maNoteData.mpCaption );
491 24 : }
492 :
493 57 : ScPostIt::ScPostIt( ScDocument& rDoc, const ScAddress& rPos, const ScNoteData& rNoteData, bool bAlwaysCreateCaption ) :
494 : mrDoc( rDoc ),
495 57 : maNoteData( rNoteData )
496 : {
497 57 : if( bAlwaysCreateCaption || maNoteData.mbShown )
498 16 : CreateCaptionFromInitData( rPos );
499 57 : }
500 :
501 164 : ScPostIt::~ScPostIt()
502 : {
503 82 : RemoveCaption();
504 82 : }
505 :
506 51 : ScPostIt* ScPostIt::Clone( const ScAddress& rOwnPos, ScDocument& rDestDoc, const ScAddress& rDestPos, bool bCloneCaption ) const
507 : {
508 51 : CreateCaptionFromInitData( rOwnPos );
509 51 : return bCloneCaption ? new ScPostIt( rDestDoc, rDestPos, *this ) : new ScPostIt( rDestDoc, rDestPos, maNoteData, false );
510 : }
511 :
512 8 : const ScNoteData& ScPostIt::GetNoteData() const
513 : {
514 8 : return maNoteData;
515 : }
516 :
517 3 : const OUString& ScPostIt::GetDate() const
518 : {
519 3 : return maNoteData.maDate;
520 : }
521 :
522 15 : void ScPostIt::SetDate( const OUString& rDate )
523 : {
524 15 : maNoteData.maDate = rDate;
525 15 : }
526 :
527 3 : const OUString& ScPostIt::GetAuthor() const
528 : {
529 3 : return maNoteData.maAuthor;
530 : }
531 :
532 21 : void ScPostIt::SetAuthor( const OUString& rAuthor )
533 : {
534 21 : maNoteData.maAuthor = rAuthor;
535 21 : }
536 :
537 55 : void ScPostIt::AutoStamp()
538 : {
539 55 : maNoteData.maDate = ScGlobal::pLocaleData->getDate( Date( Date::SYSTEM ) );
540 55 : maNoteData.maAuthor = SvtUserOptions().GetID();
541 55 : }
542 :
543 39 : const OutlinerParaObject* ScPostIt::GetOutlinerObject() const
544 : {
545 39 : if( maNoteData.mpCaption )
546 9 : return maNoteData.mpCaption->GetOutlinerParaObject();
547 30 : if( maNoteData.mxInitData.get() )
548 10 : return maNoteData.mxInitData->mxOutlinerObj.get();
549 20 : return 0;
550 : }
551 :
552 39 : const EditTextObject* ScPostIt::GetEditTextObject() const
553 : {
554 39 : const OutlinerParaObject* pOPO = GetOutlinerObject();
555 39 : return pOPO ? &pOPO->GetTextObject() : 0;
556 : }
557 :
558 33 : OUString ScPostIt::GetText() const
559 : {
560 33 : if( const EditTextObject* pEditObj = GetEditTextObject() )
561 : {
562 11 : OUStringBuffer aBuffer;
563 22 : for( sal_Int32 nPara = 0, nParaCount = pEditObj->GetParagraphCount(); nPara < nParaCount; ++nPara )
564 : {
565 11 : if( nPara > 0 )
566 0 : aBuffer.append( '\n' );
567 11 : aBuffer.append( pEditObj->GetText( nPara ) );
568 : }
569 11 : return aBuffer.makeStringAndClear();
570 : }
571 22 : if( maNoteData.mxInitData.get() )
572 2 : return maNoteData.mxInitData->maSimpleText;
573 20 : return OUString();
574 : }
575 :
576 21 : void ScPostIt::SetText( const ScAddress& rPos, const OUString& rText )
577 : {
578 21 : CreateCaptionFromInitData( rPos );
579 21 : if( maNoteData.mpCaption )
580 4 : maNoteData.mpCaption->SetText( rText );
581 21 : }
582 :
583 1 : SdrCaptionObj* ScPostIt::GetCaption() const
584 : {
585 1 : return maNoteData.mpCaption;
586 : }
587 :
588 13 : SdrCaptionObj* ScPostIt::GetOrCreateCaption( const ScAddress& rPos ) const
589 : {
590 13 : CreateCaptionFromInitData( rPos );
591 13 : return maNoteData.mpCaption;
592 : }
593 :
594 10 : void ScPostIt::ForgetCaption()
595 : {
596 : /* This function is used in undo actions to give up the responsibility for
597 : the caption object which is handled by separate drawing undo actions. */
598 10 : maNoteData.mpCaption = 0;
599 10 : maNoteData.mxInitData.reset();
600 10 : }
601 :
602 1 : void ScPostIt::ShowCaption( const ScAddress& rPos, bool bShow )
603 : {
604 1 : CreateCaptionFromInitData( rPos );
605 : // no separate drawing undo needed, handled completely inside ScUndoShowHideNote
606 1 : maNoteData.mbShown = bShow;
607 1 : if( maNoteData.mpCaption )
608 1 : ScCaptionUtil::SetCaptionLayer( *maNoteData.mpCaption, bShow );
609 1 : }
610 :
611 9 : bool ScPostIt::IsCaptionShown() const
612 : {
613 9 : return maNoteData.mbShown;
614 : }
615 :
616 0 : void ScPostIt::ShowCaptionTemp( const ScAddress& rPos, bool bShow )
617 : {
618 0 : CreateCaptionFromInitData( rPos );
619 0 : if( maNoteData.mpCaption )
620 0 : ScCaptionUtil::SetCaptionLayer( *maNoteData.mpCaption, maNoteData.mbShown || bShow );
621 0 : }
622 :
623 15 : void ScPostIt::UpdateCaptionPos( const ScAddress& rPos )
624 : {
625 15 : CreateCaptionFromInitData( rPos );
626 15 : if( maNoteData.mpCaption )
627 : {
628 12 : ScCaptionCreator aCreator( mrDoc, rPos, *maNoteData.mpCaption );
629 12 : aCreator.UpdateCaptionPos();
630 : }
631 15 : }
632 :
633 : // private --------------------------------------------------------------------
634 :
635 117 : void ScPostIt::CreateCaptionFromInitData( const ScAddress& rPos ) const
636 : {
637 : OSL_ENSURE( maNoteData.mpCaption || maNoteData.mxInitData.get(), "ScPostIt::CreateCaptionFromInitData - need caption object or initial caption data" );
638 117 : if( maNoteData.mxInitData.get() )
639 : {
640 : /* This function is called from ScPostIt::Clone() when copying cells
641 : to the clipboard/undo document, and when copying cells from the
642 : clipboard/undo document. The former should always be called first,
643 : so if called in an clipboard/undo document, the caption should have
644 : been created already. */
645 : OSL_ENSURE( !mrDoc.IsUndo() && !mrDoc.IsClipboard(), "ScPostIt::CreateCaptionFromInitData - note caption should not be created in undo/clip documents" );
646 :
647 : /* #i104915# Never try to create notes in Undo document, leads to
648 : crash due to missing document members (e.g. row height array). */
649 19 : if( !maNoteData.mpCaption && !mrDoc.IsUndo() )
650 : {
651 : // ScNoteCaptionCreator c'tor creates the caption and inserts it into the document and maNoteData
652 19 : ScNoteCaptionCreator aCreator( mrDoc, rPos, maNoteData );
653 19 : if( maNoteData.mpCaption )
654 : {
655 19 : ScCaptionInitData& rInitData = *maNoteData.mxInitData;
656 :
657 : // transfer ownership of outliner object to caption, or set simple text
658 : OSL_ENSURE( rInitData.mxOutlinerObj.get() || !rInitData.maSimpleText.isEmpty(),
659 : "ScPostIt::CreateCaptionFromInitData - need either outliner para object or simple text" );
660 19 : if( rInitData.mxOutlinerObj.get() )
661 9 : maNoteData.mpCaption->SetOutlinerParaObject( rInitData.mxOutlinerObj.release() );
662 : else
663 10 : maNoteData.mpCaption->SetText( rInitData.maSimpleText );
664 :
665 : // copy all items or set default items; reset shadow items
666 19 : ScCaptionUtil::SetDefaultItems( *maNoteData.mpCaption, mrDoc );
667 19 : if( rInitData.mxItemSet.get() )
668 9 : ScCaptionUtil::SetCaptionItems( *maNoteData.mpCaption, *rInitData.mxItemSet );
669 :
670 : // set position and size of the caption object
671 19 : if( rInitData.mbDefaultPosSize )
672 : {
673 : // set other items and fit caption size to text
674 15 : maNoteData.mpCaption->SetMergedItem( SdrTextMinFrameWidthItem( SC_NOTECAPTION_WIDTH ) );
675 15 : maNoteData.mpCaption->SetMergedItem( SdrTextMaxFrameWidthItem( SC_NOTECAPTION_MAXWIDTH_TEMP ) );
676 15 : maNoteData.mpCaption->AdjustTextFrameWidthAndHeight();
677 15 : aCreator.AutoPlaceCaption();
678 : }
679 : else
680 : {
681 4 : Rectangle aCellRect = ScDrawLayer::GetCellRect( mrDoc, rPos, true );
682 4 : bool bNegPage = mrDoc.IsNegativePage( rPos.Tab() );
683 4 : long nPosX = bNegPage ? (aCellRect.Left() - rInitData.maCaptionOffset.X()) : (aCellRect.Right() + rInitData.maCaptionOffset.X());
684 4 : long nPosY = aCellRect.Top() + rInitData.maCaptionOffset.Y();
685 4 : Rectangle aCaptRect( Point( nPosX, nPosY ), rInitData.maCaptionSize );
686 4 : maNoteData.mpCaption->SetLogicRect( aCaptRect );
687 4 : aCreator.FitCaptionToRect();
688 : }
689 : }
690 : }
691 : // forget the initial caption data struct
692 19 : maNoteData.mxInitData.reset();
693 : }
694 117 : }
695 :
696 49 : void ScPostIt::CreateCaption( const ScAddress& rPos, const SdrCaptionObj* pCaption )
697 : {
698 : OSL_ENSURE( !maNoteData.mpCaption, "ScPostIt::CreateCaption - unexpected caption object found" );
699 49 : maNoteData.mpCaption = 0;
700 :
701 : /* #i104915# Never try to create notes in Undo document, leads to
702 : crash due to missing document members (e.g. row height array). */
703 : OSL_ENSURE( !mrDoc.IsUndo(), "ScPostIt::CreateCaption - note caption should not be created in undo documents" );
704 49 : if( mrDoc.IsUndo() )
705 56 : return;
706 :
707 : // drawing layer may be missing, if a note is copied into a clipboard document
708 42 : if( mrDoc.IsClipboard() )
709 3 : mrDoc.InitDrawLayer();
710 :
711 : // ScNoteCaptionCreator c'tor creates the caption and inserts it into the document and maNoteData
712 42 : ScNoteCaptionCreator aCreator( mrDoc, rPos, maNoteData );
713 42 : if( maNoteData.mpCaption )
714 : {
715 : // clone settings of passed caption
716 13 : if( pCaption )
717 : {
718 : // copy edit text object (object must be inserted into page already)
719 2 : if( OutlinerParaObject* pOPO = pCaption->GetOutlinerParaObject() )
720 2 : maNoteData.mpCaption->SetOutlinerParaObject( new OutlinerParaObject( *pOPO ) );
721 : // copy formatting items (after text has been copied to apply font formatting)
722 2 : maNoteData.mpCaption->SetMergedItemSetAndBroadcast( pCaption->GetMergedItemSet() );
723 : // move textbox position relative to new cell, copy textbox size
724 2 : Rectangle aCaptRect = pCaption->GetLogicRect();
725 2 : Point aDist = maNoteData.mpCaption->GetTailPos() - pCaption->GetTailPos();
726 2 : aCaptRect.Move( aDist.X(), aDist.Y() );
727 2 : maNoteData.mpCaption->SetLogicRect( aCaptRect );
728 2 : aCreator.FitCaptionToRect();
729 : }
730 : else
731 : {
732 : // set default formatting and default position
733 11 : ScCaptionUtil::SetDefaultItems( *maNoteData.mpCaption, mrDoc );
734 11 : aCreator.AutoPlaceCaption();
735 : }
736 :
737 : // create undo action
738 13 : if( ScDrawLayer* pDrawLayer = mrDoc.GetDrawLayer() )
739 13 : if( pDrawLayer->IsRecording() )
740 0 : pDrawLayer->AddCalcUndo( pDrawLayer->GetSdrUndoFactory().CreateUndoNewObject( *maNoteData.mpCaption ) );
741 : }
742 : }
743 :
744 82 : void ScPostIt::RemoveCaption()
745 : {
746 :
747 : /* Remove caption object only, if this note is its owner (e.g. notes in
748 : undo documents refer to captions in original document, do not remove
749 : them from drawing layer here). */
750 82 : ScDrawLayer* pDrawLayer = mrDoc.GetDrawLayer();
751 82 : if( maNoteData.mpCaption && (pDrawLayer == maNoteData.mpCaption->GetModel()) )
752 : {
753 : OSL_ENSURE( pDrawLayer, "ScPostIt::RemoveCaption - object without drawing layer" );
754 28 : SdrPage* pDrawPage = maNoteData.mpCaption->GetPage();
755 : OSL_ENSURE( pDrawPage, "ScPostIt::RemoveCaption - object without drawing page" );
756 28 : if( pDrawPage )
757 : {
758 28 : pDrawPage->RecalcObjOrdNums();
759 : // create drawing undo action (before removing the object to have valid draw page in undo action)
760 28 : bool bRecording = ( pDrawLayer && pDrawLayer->IsRecording() );
761 28 : if( bRecording )
762 3 : pDrawLayer->AddCalcUndo( pDrawLayer->GetSdrUndoFactory().CreateUndoDeleteObject( *maNoteData.mpCaption ) );
763 : // remove the object from the drawing page, delete if undo is disabled
764 28 : SdrObject* pObj = pDrawPage->RemoveObject( maNoteData.mpCaption->GetOrdNum() );
765 28 : if( !bRecording )
766 25 : SdrObject::Free( pObj );
767 : }
768 : }
769 82 : maNoteData.mpCaption = 0;
770 82 : }
771 :
772 0 : SdrCaptionObj* ScNoteUtil::CreateTempCaption(
773 : ScDocument& rDoc, const ScAddress& rPos, SdrPage& rDrawPage,
774 : const OUString& rUserText, const Rectangle& rVisRect, bool bTailFront )
775 : {
776 0 : OUStringBuffer aBuffer( rUserText );
777 : // add plain text of invisible (!) cell note (no formatting etc.)
778 0 : SdrCaptionObj* pNoteCaption = 0;
779 0 : const ScPostIt* pNote = rDoc.GetNote( rPos );
780 0 : if( pNote && !pNote->IsCaptionShown() )
781 : {
782 0 : if( !aBuffer.isEmpty() )
783 0 : aBuffer.append( "\n--------\n" ).append( pNote->GetText() );
784 0 : pNoteCaption = pNote->GetOrCreateCaption( rPos );
785 : }
786 :
787 : // create a caption if any text exists
788 0 : if( !pNoteCaption && aBuffer.isEmpty() )
789 0 : return 0;
790 :
791 : // prepare visible rectangle (add default distance to all borders)
792 : Rectangle aVisRect(
793 0 : rVisRect.Left() + SC_NOTECAPTION_BORDERDIST_TEMP,
794 0 : rVisRect.Top() + SC_NOTECAPTION_BORDERDIST_TEMP,
795 0 : rVisRect.Right() - SC_NOTECAPTION_BORDERDIST_TEMP,
796 0 : rVisRect.Bottom() - SC_NOTECAPTION_BORDERDIST_TEMP );
797 :
798 : // create the caption object
799 0 : ScCaptionCreator aCreator( rDoc, rPos, true, bTailFront );
800 0 : SdrCaptionObj* pCaption = aCreator.GetCaption();
801 :
802 : // insert caption into page (needed to set caption text)
803 0 : rDrawPage.InsertObject( pCaption );
804 :
805 : // clone the edit text object, unless user text is present, then set this text
806 0 : if( pNoteCaption && rUserText.isEmpty() )
807 : {
808 0 : if( OutlinerParaObject* pOPO = pNoteCaption->GetOutlinerParaObject() )
809 0 : pCaption->SetOutlinerParaObject( new OutlinerParaObject( *pOPO ) );
810 : // set formatting (must be done after setting text) and resize the box to fit the text
811 0 : pCaption->SetMergedItemSetAndBroadcast( pNoteCaption->GetMergedItemSet() );
812 0 : Rectangle aCaptRect( pCaption->GetLogicRect().TopLeft(), pNoteCaption->GetLogicRect().GetSize() );
813 0 : pCaption->SetLogicRect( aCaptRect );
814 : }
815 : else
816 : {
817 : // if pNoteCaption is null, then aBuffer contains some text
818 0 : pCaption->SetText( aBuffer.makeStringAndClear() );
819 0 : ScCaptionUtil::SetDefaultItems( *pCaption, rDoc );
820 : // adjust caption size to text size
821 0 : long nMaxWidth = ::std::min< long >( aVisRect.GetWidth() * 2 / 3, SC_NOTECAPTION_MAXWIDTH_TEMP );
822 0 : pCaption->SetMergedItem( SdrTextAutoGrowWidthItem( true ) );
823 0 : pCaption->SetMergedItem( SdrTextMinFrameWidthItem( SC_NOTECAPTION_WIDTH ) );
824 0 : pCaption->SetMergedItem( SdrTextMaxFrameWidthItem( nMaxWidth ) );
825 0 : pCaption->SetMergedItem( SdrTextAutoGrowHeightItem( true ) );
826 0 : pCaption->AdjustTextFrameWidthAndHeight();
827 : }
828 :
829 : // move caption into visible area
830 0 : aCreator.AutoPlaceCaption( &aVisRect );
831 0 : return pCaption;
832 : }
833 :
834 3 : ScPostIt* ScNoteUtil::CreateNoteFromCaption(
835 : ScDocument& rDoc, const ScAddress& rPos, SdrCaptionObj& rCaption, bool bShown )
836 : {
837 3 : ScNoteData aNoteData( bShown );
838 3 : aNoteData.mpCaption = &rCaption;
839 3 : ScPostIt* pNote = new ScPostIt( rDoc, rPos, aNoteData, false );
840 3 : pNote->AutoStamp();
841 :
842 3 : rDoc.SetNote(rPos, pNote);
843 :
844 : // ScNoteCaptionCreator c'tor updates the caption object to be part of a note
845 3 : ScNoteCaptionCreator aCreator( rDoc, rPos, rCaption, bShown );
846 :
847 3 : return pNote;
848 : }
849 :
850 16 : ScPostIt* ScNoteUtil::CreateNoteFromObjectData(
851 : ScDocument& rDoc, const ScAddress& rPos, SfxItemSet* pItemSet,
852 : OutlinerParaObject* pOutlinerObj, const Rectangle& rCaptionRect,
853 : bool bShown, bool bAlwaysCreateCaption )
854 : {
855 : OSL_ENSURE( pItemSet && pOutlinerObj, "ScNoteUtil::CreateNoteFromObjectData - item set and outliner object expected" );
856 16 : ScNoteData aNoteData( bShown );
857 16 : aNoteData.mxInitData.reset( new ScCaptionInitData );
858 16 : ScCaptionInitData& rInitData = *aNoteData.mxInitData;
859 16 : rInitData.mxItemSet.reset( pItemSet );
860 16 : rInitData.mxOutlinerObj.reset( pOutlinerObj );
861 :
862 : // convert absolute caption position to relative position
863 16 : rInitData.mbDefaultPosSize = rCaptionRect.IsEmpty();
864 16 : if( !rInitData.mbDefaultPosSize )
865 : {
866 11 : Rectangle aCellRect = ScDrawLayer::GetCellRect( rDoc, rPos, true );
867 11 : bool bNegPage = rDoc.IsNegativePage( rPos.Tab() );
868 11 : rInitData.maCaptionOffset.X() = bNegPage ? (aCellRect.Left() - rCaptionRect.Right()) : (rCaptionRect.Left() - aCellRect.Right());
869 11 : rInitData.maCaptionOffset.Y() = rCaptionRect.Top() - aCellRect.Top();
870 11 : rInitData.maCaptionSize = rCaptionRect.GetSize();
871 : }
872 :
873 : /* Create the note and insert it into the document. If the note is
874 : visible, the caption object will be created automatically. */
875 16 : ScPostIt* pNote = new ScPostIt( rDoc, rPos, aNoteData, bAlwaysCreateCaption );
876 16 : pNote->AutoStamp();
877 :
878 16 : rDoc.SetNote(rPos, pNote);
879 :
880 16 : return pNote;
881 : }
882 :
883 11 : ScPostIt* ScNoteUtil::CreateNoteFromString(
884 : ScDocument& rDoc, const ScAddress& rPos, const OUString& rNoteText,
885 : bool bShown, bool bAlwaysCreateCaption )
886 : {
887 11 : ScPostIt* pNote = 0;
888 11 : if( !rNoteText.isEmpty() )
889 : {
890 11 : ScNoteData aNoteData( bShown );
891 11 : aNoteData.mxInitData.reset( new ScCaptionInitData );
892 11 : ScCaptionInitData& rInitData = *aNoteData.mxInitData;
893 11 : rInitData.maSimpleText = rNoteText;
894 11 : rInitData.mbDefaultPosSize = true;
895 :
896 : /* Create the note and insert it into the document. If the note is
897 : visible, the caption object will be created automatically. */
898 11 : pNote = new ScPostIt( rDoc, rPos, aNoteData, bAlwaysCreateCaption );
899 11 : pNote->AutoStamp();
900 : //insert takes ownership
901 11 : rDoc.SetNote(rPos, pNote);
902 : }
903 11 : return pNote;
904 : }
905 :
906 : namespace sc {
907 :
908 34 : NoteEntry::NoteEntry( const ScAddress& rPos, const ScPostIt* pNote ) :
909 34 : maPos(rPos), mpNote(pNote) {}
910 :
911 102 : }
912 :
913 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|