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