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 "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 :
52 : // ============================================================================
53 :
54 : namespace {
55 :
56 : const long SC_NOTECAPTION_WIDTH = 2900; /// Default width of note caption textbox.
57 : const long SC_NOTECAPTION_MAXWIDTH_TEMP = 12000; /// Maximum width of temporary note caption textbox.
58 : const long SC_NOTECAPTION_HEIGHT = 1800; /// Default height of note caption textbox.
59 : const long SC_NOTECAPTION_CELLDIST = 600; /// Default distance of note captions to border of anchor cell.
60 : const long SC_NOTECAPTION_OFFSET_Y = -1500; /// Default Y offset of note captions to top border of anchor cell.
61 : const long SC_NOTECAPTION_OFFSET_X = 1500; /// Default X offset of note captions to left border of anchor cell.
62 : const long SC_NOTECAPTION_BORDERDIST_TEMP = 100; /// Distance of temporary note captions to visible sheet area.
63 :
64 : // ============================================================================
65 :
66 : /** Static helper functions for caption objects. */
67 : class ScCaptionUtil
68 : {
69 : public:
70 : /** Moves the caption object to the correct layer according to passed visibility. */
71 : static void SetCaptionLayer( SdrCaptionObj& rCaption, bool bShown );
72 : /** Sets basic caption settings required for note caption objects. */
73 : static void SetBasicCaptionSettings( SdrCaptionObj& rCaption, bool bShown );
74 : /** Stores the cell position of the note in the user data area of the caption. */
75 : static void SetCaptionUserData( SdrCaptionObj& rCaption, const ScAddress& rPos );
76 : /** Sets all default formatting attributes to the caption object. */
77 : static void SetDefaultItems( SdrCaptionObj& rCaption, ScDocument& rDoc );
78 : /** Updates caption item set according to the passed item set while removing shadow items. */
79 : static void SetCaptionItems( SdrCaptionObj& rCaption, const SfxItemSet& rItemSet );
80 : };
81 :
82 : // ----------------------------------------------------------------------------
83 :
84 17 : void ScCaptionUtil::SetCaptionLayer( SdrCaptionObj& rCaption, bool bShown )
85 : {
86 17 : SdrLayerID nLayer = bShown ? SC_LAYER_INTERN : SC_LAYER_HIDDEN;
87 17 : if( nLayer != rCaption.GetLayer() )
88 17 : rCaption.SetLayer( nLayer );
89 17 : }
90 :
91 16 : void ScCaptionUtil::SetBasicCaptionSettings( SdrCaptionObj& rCaption, bool bShown )
92 : {
93 16 : SetCaptionLayer( rCaption, bShown );
94 16 : rCaption.SetFixedTail();
95 16 : rCaption.SetSpecialTextBoxShadow();
96 16 : }
97 :
98 16 : void ScCaptionUtil::SetCaptionUserData( SdrCaptionObj& rCaption, const ScAddress& rPos )
99 : {
100 : // pass true to ScDrawLayer::GetObjData() to create the object data entry
101 16 : ScDrawObjData* pObjData = ScDrawLayer::GetObjData( &rCaption, true );
102 : OSL_ENSURE( pObjData, "ScCaptionUtil::SetCaptionUserData - missing drawing object user data" );
103 16 : pObjData->maStart = rPos;
104 16 : pObjData->meType = ScDrawObjData::CellNote;
105 16 : }
106 :
107 12 : void ScCaptionUtil::SetDefaultItems( SdrCaptionObj& rCaption, ScDocument& rDoc )
108 : {
109 12 : SfxItemSet aItemSet = rCaption.GetMergedItemSet();
110 :
111 : // caption tail arrow
112 24 : ::basegfx::B2DPolygon aTriangle;
113 12 : aTriangle.append( ::basegfx::B2DPoint( 10.0, 0.0 ) );
114 12 : aTriangle.append( ::basegfx::B2DPoint( 0.0, 30.0 ) );
115 12 : aTriangle.append( ::basegfx::B2DPoint( 20.0, 30.0 ) );
116 12 : aTriangle.setClosed( true );
117 : /* Line ends are now created with an empty name. The
118 : checkForUniqueItem() method then finds a unique name for the item's
119 : value. */
120 12 : aItemSet.Put( XLineStartItem( String::EmptyString(), ::basegfx::B2DPolyPolygon( aTriangle ) ) );
121 12 : aItemSet.Put( XLineStartWidthItem( 200 ) );
122 12 : aItemSet.Put( XLineStartCenterItem( false ) );
123 12 : aItemSet.Put( XFillStyleItem( XFILL_SOLID ) );
124 12 : aItemSet.Put( XFillColorItem( String::EmptyString(), ScDetectiveFunc::GetCommentColor() ) );
125 12 : aItemSet.Put( SdrCaptionEscDirItem( SDRCAPT_ESCBESTFIT ) );
126 :
127 : // shadow
128 : /* SdrShadowItem has sal_False, instead the shadow is set for the
129 : rectangle only with SetSpecialTextBoxShadow() when the object is
130 : created (item must be set to adjust objects from older files). */
131 12 : aItemSet.Put( SdrShadowItem( false ) );
132 12 : aItemSet.Put( SdrShadowXDistItem( 100 ) );
133 12 : aItemSet.Put( SdrShadowYDistItem( 100 ) );
134 :
135 : // text attributes
136 12 : aItemSet.Put( SdrTextLeftDistItem( 100 ) );
137 12 : aItemSet.Put( SdrTextRightDistItem( 100 ) );
138 12 : aItemSet.Put( SdrTextUpperDistItem( 100 ) );
139 12 : aItemSet.Put( SdrTextLowerDistItem( 100 ) );
140 12 : aItemSet.Put( SdrTextAutoGrowWidthItem( false ) );
141 12 : aItemSet.Put( SdrTextAutoGrowHeightItem( true ) );
142 : // use the default cell style to be able to modify the caption font
143 12 : const ScPatternAttr& rDefPattern = static_cast< const ScPatternAttr& >( rDoc.GetPool()->GetDefaultItem( ATTR_PATTERN ) );
144 12 : rDefPattern.FillEditItemSet( &aItemSet );
145 :
146 24 : rCaption.SetMergedItemSet( aItemSet );
147 12 : }
148 :
149 5 : void ScCaptionUtil::SetCaptionItems( SdrCaptionObj& rCaption, const SfxItemSet& rItemSet )
150 : {
151 : // copy all items
152 5 : rCaption.SetMergedItemSet( rItemSet );
153 : // reset shadow items
154 5 : rCaption.SetMergedItem( SdrShadowItem( false ) );
155 5 : rCaption.SetMergedItem( SdrShadowXDistItem( 100 ) );
156 5 : rCaption.SetMergedItem( SdrShadowYDistItem( 100 ) );
157 5 : rCaption.SetSpecialTextBoxShadow();
158 5 : }
159 :
160 : // ============================================================================
161 :
162 : /** Helper for creation and manipulation of caption drawing objects independent
163 : from cell annotations. */
164 : class ScCaptionCreator
165 : {
166 : public:
167 : /** Create a new caption. The caption will not be inserted into the document. */
168 : explicit ScCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, bool bShown, bool bTailFront );
169 : /** Manipulate an existing caption. */
170 : explicit ScCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, SdrCaptionObj& rCaption );
171 :
172 : /** Returns the drawing layer page of the sheet contained in maPos. */
173 : SdrPage* GetDrawPage();
174 : /** Returns the caption drawing obejct. */
175 14 : inline SdrCaptionObj* GetCaption() { return mpCaption; }
176 :
177 : /** Moves the caption inside the passed rectangle. Uses page area if 0 is passed. */
178 : void FitCaptionToRect( const Rectangle* pVisRect = 0 );
179 : /** Places the caption inside the passed rectangle, tries to keep the cell rectangle uncovered. Uses page area if 0 is passed. */
180 : void AutoPlaceCaption( const Rectangle* pVisRect = 0 );
181 : /** Updates caption tail and textbox according to current cell position. Uses page area if 0 is passed. */
182 : void UpdateCaptionPos( const Rectangle* pVisRect = 0 );
183 :
184 : protected:
185 : /** Helper constructor for derived classes. */
186 : explicit ScCaptionCreator( ScDocument& rDoc, const ScAddress& rPos );
187 :
188 : /** Calculates the caption tail position according to current cell position. */
189 : Point CalcTailPos( bool bTailFront );
190 : /** Implements creation of the caption object. The caption will not be inserted into the document. */
191 : void CreateCaption( bool bShown, bool bTailFront );
192 :
193 : private:
194 : /** Initializes all members. */
195 : void Initialize();
196 : /** Returns the passed rectangle if existing, page rectangle otherwise. */
197 26 : inline const Rectangle& GetVisRect( const Rectangle* pVisRect ) const { return pVisRect ? *pVisRect : maPageRect; }
198 :
199 : private:
200 : ScDocument& mrDoc;
201 : ScAddress maPos;
202 : SdrCaptionObj* mpCaption;
203 : Rectangle maPageRect;
204 : Rectangle maCellRect;
205 : bool mbNegPage;
206 : };
207 :
208 : // ----------------------------------------------------------------------------
209 :
210 0 : ScCaptionCreator::ScCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, bool bShown, bool bTailFront ) :
211 : mrDoc( rDoc ),
212 : maPos( rPos ),
213 0 : mpCaption( 0 )
214 : {
215 0 : Initialize();
216 0 : CreateCaption( bShown, bTailFront );
217 0 : }
218 :
219 9 : ScCaptionCreator::ScCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, SdrCaptionObj& rCaption ) :
220 : mrDoc( rDoc ),
221 : maPos( rPos ),
222 9 : mpCaption( &rCaption )
223 : {
224 9 : Initialize();
225 9 : }
226 :
227 18 : ScCaptionCreator::ScCaptionCreator( ScDocument& rDoc, const ScAddress& rPos ) :
228 : mrDoc( rDoc ),
229 : maPos( rPos ),
230 18 : mpCaption( 0 )
231 : {
232 18 : Initialize();
233 18 : }
234 :
235 47 : SdrPage* ScCaptionCreator::GetDrawPage()
236 : {
237 47 : ScDrawLayer* pDrawLayer = mrDoc.GetDrawLayer();
238 47 : return pDrawLayer ? pDrawLayer->GetPage( static_cast< sal_uInt16 >( maPos.Tab() ) ) : 0;
239 : }
240 :
241 14 : void ScCaptionCreator::FitCaptionToRect( const Rectangle* pVisRect )
242 : {
243 14 : const Rectangle& rVisRect = GetVisRect( pVisRect );
244 :
245 : // tail position
246 14 : Point aTailPos = mpCaption->GetTailPos();
247 14 : aTailPos.X() = ::std::max( ::std::min( aTailPos.X(), rVisRect.Right() ), rVisRect.Left() );
248 14 : aTailPos.Y() = ::std::max( ::std::min( aTailPos.Y(), rVisRect.Bottom() ), rVisRect.Top() );
249 14 : mpCaption->SetTailPos( aTailPos );
250 :
251 : // caption rectangle
252 14 : Rectangle aCaptRect = mpCaption->GetLogicRect();
253 14 : Point aCaptPos = aCaptRect.TopLeft();
254 : // move textbox inside right border of visible area
255 14 : aCaptPos.X() = ::std::min< long >( aCaptPos.X(), rVisRect.Right() - aCaptRect.GetWidth() );
256 : // move textbox inside left border of visible area (this may move it outside on right side again)
257 14 : aCaptPos.X() = ::std::max< long >( aCaptPos.X(), rVisRect.Left() );
258 : // move textbox inside bottom border of visible area
259 14 : aCaptPos.Y() = ::std::min< long >( aCaptPos.Y(), rVisRect.Bottom() - aCaptRect.GetHeight() );
260 : // move textbox inside top border of visible area (this may move it outside on bottom side again)
261 14 : aCaptPos.Y() = ::std::max< long >( aCaptPos.Y(), rVisRect.Top() );
262 : // update caption
263 14 : aCaptRect.SetPos( aCaptPos );
264 14 : mpCaption->SetLogicRect( aCaptRect );
265 14 : }
266 :
267 12 : void ScCaptionCreator::AutoPlaceCaption( const Rectangle* pVisRect )
268 : {
269 12 : const Rectangle& rVisRect = GetVisRect( pVisRect );
270 :
271 : // caption rectangle
272 12 : Rectangle aCaptRect = mpCaption->GetLogicRect();
273 12 : long nWidth = aCaptRect.GetWidth();
274 12 : long nHeight = aCaptRect.GetHeight();
275 :
276 : // n***Space contains available space between border of visible area and cell
277 12 : long nLeftSpace = maCellRect.Left() - rVisRect.Left() + 1;
278 12 : long nRightSpace = rVisRect.Right() - maCellRect.Right() + 1;
279 12 : long nTopSpace = maCellRect.Top() - rVisRect.Top() + 1;
280 12 : long nBottomSpace = rVisRect.Bottom() - maCellRect.Bottom() + 1;
281 :
282 : // nNeeded*** contains textbox dimensions plus needed distances to cell or border of visible area
283 12 : long nNeededSpaceX = nWidth + SC_NOTECAPTION_CELLDIST;
284 12 : long nNeededSpaceY = nHeight + SC_NOTECAPTION_CELLDIST;
285 :
286 : // bFitsWidth*** == true means width of textbox fits into horizontal free space of visible area
287 12 : bool bFitsWidthLeft = nNeededSpaceX <= nLeftSpace; // text box width fits into the width left of cell
288 12 : bool bFitsWidthRight = nNeededSpaceX <= nRightSpace; // text box width fits into the width right of cell
289 12 : bool bFitsWidth = nWidth <= rVisRect.GetWidth(); // text box width fits into width of visible area
290 :
291 : // bFitsHeight*** == true means height of textbox fits into vertical free space of visible area
292 12 : bool bFitsHeightTop = nNeededSpaceY <= nTopSpace; // text box height fits into the height above cell
293 12 : bool bFitsHeightBottom = nNeededSpaceY <= nBottomSpace; // text box height fits into the height below cell
294 12 : bool bFitsHeight = nHeight <= rVisRect.GetHeight(); // text box height fits into height of visible area
295 :
296 : // bFits*** == true means the textbox fits completely into free space of visible area
297 12 : bool bFitsLeft = bFitsWidthLeft && bFitsHeight;
298 12 : bool bFitsRight = bFitsWidthRight && bFitsHeight;
299 12 : bool bFitsTop = bFitsWidth && bFitsHeightTop;
300 12 : bool bFitsBottom = bFitsWidth && bFitsHeightBottom;
301 :
302 12 : Point aCaptPos;
303 : // use left/right placement if possible, or if top/bottom placement not possible
304 12 : if( bFitsLeft || bFitsRight || (!bFitsTop && !bFitsBottom) )
305 : {
306 : // prefer left in RTL sheet and right in LTR sheets
307 12 : bool bPreferLeft = bFitsLeft && (mbNegPage || !bFitsRight);
308 12 : bool bPreferRight = bFitsRight && (!mbNegPage || !bFitsLeft);
309 : // move to left, if left is preferred, or if neither left nor right fit and there is more space to the left
310 12 : if( bPreferLeft || (!bPreferRight && (nLeftSpace > nRightSpace)) )
311 0 : aCaptPos.X() = maCellRect.Left() - SC_NOTECAPTION_CELLDIST - nWidth;
312 : else // to right
313 12 : aCaptPos.X() = maCellRect.Right() + SC_NOTECAPTION_CELLDIST;
314 : // Y position according to top cell border
315 12 : aCaptPos.Y() = maCellRect.Top() + SC_NOTECAPTION_OFFSET_Y;
316 : }
317 : else // top or bottom placement
318 : {
319 : // X position
320 0 : aCaptPos.X() = maCellRect.Left() + SC_NOTECAPTION_OFFSET_X;
321 : // top placement, if possible
322 0 : if( bFitsTop )
323 0 : aCaptPos.Y() = maCellRect.Top() - SC_NOTECAPTION_CELLDIST - nHeight;
324 : else // bottom placement
325 0 : aCaptPos.Y() = maCellRect.Bottom() + SC_NOTECAPTION_CELLDIST;
326 : }
327 :
328 : // update textbox position in note caption object
329 12 : aCaptRect.SetPos( aCaptPos );
330 12 : mpCaption->SetLogicRect( aCaptRect );
331 12 : FitCaptionToRect( pVisRect );
332 12 : }
333 :
334 7 : void ScCaptionCreator::UpdateCaptionPos( const Rectangle* pVisRect )
335 : {
336 7 : ScDrawLayer* pDrawLayer = mrDoc.GetDrawLayer();
337 :
338 : // update caption position
339 7 : const Point& rOldTailPos = mpCaption->GetTailPos();
340 7 : Point aTailPos = CalcTailPos( false );
341 7 : if( rOldTailPos != aTailPos )
342 : {
343 : // create drawing undo action
344 0 : if( pDrawLayer && pDrawLayer->IsRecording() )
345 0 : pDrawLayer->AddCalcUndo( pDrawLayer->GetSdrUndoFactory().CreateUndoGeoObject( *mpCaption ) );
346 : // calculate new caption rectangle (#i98141# handle LTR<->RTL switch correctly)
347 0 : Rectangle aCaptRect = mpCaption->GetLogicRect();
348 0 : long nDiffX = (rOldTailPos.X() >= 0) ? (aCaptRect.Left() - rOldTailPos.X()) : (rOldTailPos.X() - aCaptRect.Right());
349 0 : if( mbNegPage ) nDiffX = -nDiffX - aCaptRect.GetWidth();
350 0 : long nDiffY = aCaptRect.Top() - rOldTailPos.Y();
351 0 : aCaptRect.SetPos( aTailPos + Point( nDiffX, nDiffY ) );
352 : // set new tail position and caption rectangle
353 0 : mpCaption->SetTailPos( aTailPos );
354 0 : mpCaption->SetLogicRect( aCaptRect );
355 : // fit caption into draw page
356 0 : FitCaptionToRect( pVisRect );
357 : }
358 :
359 : // update cell position in caption user data
360 7 : ScDrawObjData* pCaptData = ScDrawLayer::GetNoteCaptionData( mpCaption, maPos.Tab() );
361 7 : if( pCaptData && (maPos != pCaptData->maStart) )
362 : {
363 : // create drawing undo action
364 0 : if( pDrawLayer && pDrawLayer->IsRecording() )
365 0 : pDrawLayer->AddCalcUndo( new ScUndoObjData( mpCaption, pCaptData->maStart, pCaptData->maEnd, maPos, pCaptData->maEnd ) );
366 : // set new position
367 0 : pCaptData->maStart = maPos;
368 : }
369 7 : }
370 :
371 23 : Point ScCaptionCreator::CalcTailPos( bool bTailFront )
372 : {
373 : // tail position
374 23 : bool bTailLeft = bTailFront != mbNegPage;
375 23 : Point aTailPos = bTailLeft ? maCellRect.TopLeft() : maCellRect.TopRight();
376 : // move caption point 1/10 mm inside cell
377 23 : if( bTailLeft ) aTailPos.X() += 10; else aTailPos.X() -= 10;
378 23 : aTailPos.Y() += 10;
379 23 : return aTailPos;
380 : }
381 :
382 14 : void ScCaptionCreator::CreateCaption( bool bShown, bool bTailFront )
383 : {
384 : // create the caption drawing object
385 14 : Rectangle aTextRect( Point( 0 , 0 ), Size( SC_NOTECAPTION_WIDTH, SC_NOTECAPTION_HEIGHT ) );
386 14 : Point aTailPos = CalcTailPos( bTailFront );
387 14 : mpCaption = new SdrCaptionObj( aTextRect, aTailPos );
388 : // basic caption settings
389 14 : ScCaptionUtil::SetBasicCaptionSettings( *mpCaption, bShown );
390 14 : }
391 :
392 27 : void ScCaptionCreator::Initialize()
393 : {
394 27 : maCellRect = ScDrawLayer::GetCellRect( mrDoc, maPos, true );
395 27 : mbNegPage = mrDoc.IsNegativePage( maPos.Tab() );
396 27 : if( SdrPage* pDrawPage = GetDrawPage() )
397 : {
398 23 : maPageRect = Rectangle( Point( 0, 0 ), pDrawPage->GetSize() );
399 : /* #i98141# SdrPage::GetSize() returns negative width in RTL mode.
400 : The call to Rectangle::Adjust() orders left/right coordinate
401 : accordingly. */
402 23 : maPageRect.Justify();
403 : }
404 27 : }
405 :
406 : // ============================================================================
407 :
408 : /** Helper for creation of permanent caption drawing objects for cell notes. */
409 : class ScNoteCaptionCreator : public ScCaptionCreator
410 : {
411 : public:
412 : /** Create a new caption object and inserts it into the document. */
413 : explicit ScNoteCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, ScNoteData& rNoteData );
414 : /** Manipulate an existing caption. */
415 : explicit ScNoteCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, SdrCaptionObj& rCaption, bool bShown );
416 : };
417 :
418 : // ----------------------------------------------------------------------------
419 :
420 18 : ScNoteCaptionCreator::ScNoteCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, ScNoteData& rNoteData ) :
421 18 : ScCaptionCreator( rDoc, rPos ) // use helper c'tor that does not create the caption yet
422 : {
423 18 : SdrPage* pDrawPage = GetDrawPage();
424 : OSL_ENSURE( pDrawPage, "ScNoteCaptionCreator::ScNoteCaptionCreator - no drawing page" );
425 18 : if( pDrawPage )
426 : {
427 : // create the caption drawing object
428 14 : CreateCaption( rNoteData.mbShown, false );
429 14 : rNoteData.mpCaption = GetCaption();
430 : OSL_ENSURE( rNoteData.mpCaption, "ScNoteCaptionCreator::ScNoteCaptionCreator - missing caption object" );
431 14 : if( rNoteData.mpCaption )
432 : {
433 : // store note position in user data of caption object
434 14 : ScCaptionUtil::SetCaptionUserData( *rNoteData.mpCaption, rPos );
435 : // insert object into draw page
436 14 : pDrawPage->InsertObject( rNoteData.mpCaption );
437 : }
438 : }
439 18 : }
440 :
441 2 : ScNoteCaptionCreator::ScNoteCaptionCreator( ScDocument& rDoc, const ScAddress& rPos, SdrCaptionObj& rCaption, bool bShown ) :
442 2 : ScCaptionCreator( rDoc, rPos, rCaption )
443 : {
444 2 : SdrPage* pDrawPage = GetDrawPage();
445 : OSL_ENSURE( pDrawPage, "ScNoteCaptionCreator::ScNoteCaptionCreator - no drawing page" );
446 : OSL_ENSURE( rCaption.GetPage() == pDrawPage, "ScNoteCaptionCreator::ScNoteCaptionCreator - wrong drawing page in caption" );
447 2 : if( pDrawPage && (rCaption.GetPage() == pDrawPage) )
448 : {
449 : // store note position in user data of caption object
450 2 : ScCaptionUtil::SetCaptionUserData( rCaption, rPos );
451 : // basic caption settings
452 2 : ScCaptionUtil::SetBasicCaptionSettings( rCaption, bShown );
453 : // set correct tail position
454 2 : rCaption.SetTailPos( CalcTailPos( false ) );
455 : }
456 2 : }
457 :
458 : } // namespace
459 :
460 : // ============================================================================
461 :
462 15 : struct ScCaptionInitData
463 : {
464 : typedef ::std::auto_ptr< SfxItemSet > SfxItemSetPtr;
465 : typedef ::std::auto_ptr< OutlinerParaObject > OutlinerParaObjPtr;
466 :
467 : SfxItemSetPtr mxItemSet; /// Caption object formatting.
468 : OutlinerParaObjPtr mxOutlinerObj; /// Text object with all text portion formatting.
469 : OUString maSimpleText; /// Simple text without formatting.
470 : Point maCaptionOffset; /// Caption position relative to cell corner.
471 : Size maCaptionSize; /// Size of the caption object.
472 : bool mbDefaultPosSize; /// True = use default position and size for caption.
473 :
474 : explicit ScCaptionInitData();
475 : };
476 :
477 : // ----------------------------------------------------------------------------
478 :
479 15 : ScCaptionInitData::ScCaptionInitData() :
480 15 : mbDefaultPosSize( true )
481 : {
482 15 : }
483 :
484 : // ============================================================================
485 :
486 35 : ScNoteData::ScNoteData( bool bShown ) :
487 : mpCaption( 0 ),
488 35 : mbShown( bShown )
489 : {
490 35 : }
491 :
492 67 : ScNoteData::~ScNoteData()
493 : {
494 67 : }
495 :
496 : // ============================================================================
497 :
498 4 : ScPostIt::ScPostIt( ScDocument& rDoc, const ScAddress& rPos, bool bShown ) :
499 : mrDoc( rDoc ),
500 4 : maNoteData( bShown )
501 : {
502 4 : AutoStamp();
503 4 : CreateCaption( rPos );
504 4 : }
505 :
506 2 : ScPostIt::ScPostIt( ScDocument& rDoc, const ScAddress& rPos, const ScPostIt& rNote ) :
507 : mrDoc( rDoc ),
508 2 : maNoteData( rNote.maNoteData )
509 : {
510 2 : maNoteData.mpCaption = 0;
511 2 : CreateCaption( rPos, rNote.maNoteData.mpCaption );
512 2 : }
513 :
514 19 : ScPostIt::ScPostIt( ScDocument& rDoc, const ScAddress& rPos, const ScNoteData& rNoteData, bool bAlwaysCreateCaption ) :
515 : mrDoc( rDoc ),
516 19 : maNoteData( rNoteData )
517 : {
518 19 : if( bAlwaysCreateCaption || maNoteData.mbShown )
519 14 : CreateCaptionFromInitData( rPos );
520 19 : }
521 :
522 48 : ScPostIt::~ScPostIt()
523 : {
524 24 : RemoveCaption();
525 24 : }
526 :
527 4 : ScPostIt* ScPostIt::Clone( const ScAddress& rOwnPos, ScDocument& rDestDoc, const ScAddress& rDestPos, bool bCloneCaption ) const
528 : {
529 4 : CreateCaptionFromInitData( rOwnPos );
530 4 : return bCloneCaption ? new ScPostIt( rDestDoc, rDestPos, *this ) : new ScPostIt( rDestDoc, rDestPos, maNoteData, false );
531 : }
532 :
533 21 : void ScPostIt::AutoStamp()
534 : {
535 21 : maNoteData.maDate = ScGlobal::pLocaleData->getDate( Date( Date::SYSTEM ) );
536 21 : maNoteData.maAuthor = SvtUserOptions().GetID();
537 21 : }
538 :
539 6 : const OutlinerParaObject* ScPostIt::GetOutlinerObject() const
540 : {
541 6 : if( maNoteData.mpCaption )
542 4 : return maNoteData.mpCaption->GetOutlinerParaObject();
543 2 : if( maNoteData.mxInitData.get() )
544 2 : return maNoteData.mxInitData->mxOutlinerObj.get();
545 0 : return 0;
546 : }
547 :
548 6 : const EditTextObject* ScPostIt::GetEditTextObject() const
549 : {
550 6 : const OutlinerParaObject* pOPO = GetOutlinerObject();
551 6 : return pOPO ? &pOPO->GetTextObject() : 0;
552 : }
553 :
554 3 : OUString ScPostIt::GetText() const
555 : {
556 3 : if( const EditTextObject* pEditObj = GetEditTextObject() )
557 : {
558 3 : OUStringBuffer aBuffer;
559 6 : for( sal_Int32 nPara = 0, nParaCount = pEditObj->GetParagraphCount(); nPara < nParaCount; ++nPara )
560 : {
561 3 : if( nPara > 0 )
562 0 : aBuffer.append( sal_Unicode( '\n' ) );
563 3 : aBuffer.append( pEditObj->GetText( nPara ) );
564 : }
565 3 : return aBuffer.makeStringAndClear();
566 : }
567 0 : if( maNoteData.mxInitData.get() )
568 0 : return maNoteData.mxInitData->maSimpleText;
569 0 : return OUString();
570 : }
571 :
572 4 : void ScPostIt::SetText( const ScAddress& rPos, const OUString& rText )
573 : {
574 4 : CreateCaptionFromInitData( rPos );
575 4 : if( maNoteData.mpCaption )
576 0 : maNoteData.mpCaption->SetText( rText );
577 4 : }
578 :
579 1 : SdrCaptionObj* ScPostIt::GetOrCreateCaption( const ScAddress& rPos ) const
580 : {
581 1 : CreateCaptionFromInitData( rPos );
582 1 : return maNoteData.mpCaption;
583 : }
584 :
585 0 : void ScPostIt::ForgetCaption()
586 : {
587 : /* This function is used in undo actions to give up the responsibility for
588 : the caption object which is handled by separate drawing undo actions. */
589 0 : maNoteData.mpCaption = 0;
590 0 : maNoteData.mxInitData.reset();
591 0 : }
592 :
593 1 : void ScPostIt::ShowCaption( const ScAddress& rPos, bool bShow )
594 : {
595 1 : CreateCaptionFromInitData( rPos );
596 : // no separate drawing undo needed, handled completely inside ScUndoShowHideNote
597 1 : maNoteData.mbShown = bShow;
598 1 : if( maNoteData.mpCaption )
599 1 : ScCaptionUtil::SetCaptionLayer( *maNoteData.mpCaption, bShow );
600 1 : }
601 :
602 0 : void ScPostIt::ShowCaptionTemp( const ScAddress& rPos, bool bShow )
603 : {
604 0 : CreateCaptionFromInitData( rPos );
605 0 : if( maNoteData.mpCaption )
606 0 : ScCaptionUtil::SetCaptionLayer( *maNoteData.mpCaption, maNoteData.mbShown || bShow );
607 0 : }
608 :
609 8 : void ScPostIt::UpdateCaptionPos( const ScAddress& rPos )
610 : {
611 8 : CreateCaptionFromInitData( rPos );
612 8 : if( maNoteData.mpCaption )
613 : {
614 7 : ScCaptionCreator aCreator( mrDoc, rPos, *maNoteData.mpCaption );
615 7 : aCreator.UpdateCaptionPos();
616 : }
617 8 : }
618 :
619 : // private --------------------------------------------------------------------
620 :
621 32 : void ScPostIt::CreateCaptionFromInitData( const ScAddress& rPos ) const
622 : {
623 : OSL_ENSURE( maNoteData.mpCaption || maNoteData.mxInitData.get(), "ScPostIt::CreateCaptionFromInitData - need caption object or initial caption data" );
624 32 : if( maNoteData.mxInitData.get() )
625 : {
626 : /* This function is called from ScPostIt::Clone() when copying cells
627 : to the clipboard/undo document, and when copying cells from the
628 : clipboard/undo document. The former should always be called first,
629 : so if called in an clipboard/undo document, the caption should have
630 : been created already. */
631 : OSL_ENSURE( !mrDoc.IsUndo() && !mrDoc.IsClipboard(), "ScPostIt::CreateCaptionFromInitData - note caption should not be created in undo/clip documents" );
632 :
633 : /* #i104915# Never try to create notes in Undo document, leads to
634 : crash due to missing document members (e.g. row height array). */
635 12 : if( !maNoteData.mpCaption && !mrDoc.IsUndo() )
636 : {
637 : // ScNoteCaptionCreator c'tor creates the caption and inserts it into the document and maNoteData
638 12 : ScNoteCaptionCreator aCreator( mrDoc, rPos, maNoteData );
639 12 : if( maNoteData.mpCaption )
640 : {
641 12 : ScCaptionInitData& rInitData = *maNoteData.mxInitData;
642 :
643 : // transfer ownership of outliner object to caption, or set simple text
644 : OSL_ENSURE( rInitData.mxOutlinerObj.get() || !rInitData.maSimpleText.isEmpty(),
645 : "ScPostIt::CreateCaptionFromInitData - need either outliner para object or simple text" );
646 12 : if( rInitData.mxOutlinerObj.get() )
647 5 : maNoteData.mpCaption->SetOutlinerParaObject( rInitData.mxOutlinerObj.release() );
648 : else
649 7 : maNoteData.mpCaption->SetText( rInitData.maSimpleText );
650 :
651 : // copy all items or set default items; reset shadow items
652 12 : ScCaptionUtil::SetDefaultItems( *maNoteData.mpCaption, mrDoc );
653 12 : if( rInitData.mxItemSet.get() )
654 5 : ScCaptionUtil::SetCaptionItems( *maNoteData.mpCaption, *rInitData.mxItemSet );
655 :
656 : // set position and size of the caption object
657 12 : if( rInitData.mbDefaultPosSize )
658 : {
659 : // set other items and fit caption size to text
660 12 : maNoteData.mpCaption->SetMergedItem( SdrTextMinFrameWidthItem( SC_NOTECAPTION_WIDTH ) );
661 12 : maNoteData.mpCaption->SetMergedItem( SdrTextMaxFrameWidthItem( SC_NOTECAPTION_MAXWIDTH_TEMP ) );
662 12 : maNoteData.mpCaption->AdjustTextFrameWidthAndHeight();
663 12 : aCreator.AutoPlaceCaption();
664 : }
665 : else
666 : {
667 0 : Rectangle aCellRect = ScDrawLayer::GetCellRect( mrDoc, rPos, true );
668 0 : bool bNegPage = mrDoc.IsNegativePage( rPos.Tab() );
669 0 : long nPosX = bNegPage ? (aCellRect.Left() - rInitData.maCaptionOffset.X()) : (aCellRect.Right() + rInitData.maCaptionOffset.X());
670 0 : long nPosY = aCellRect.Top() + rInitData.maCaptionOffset.Y();
671 0 : Rectangle aCaptRect( Point( nPosX, nPosY ), rInitData.maCaptionSize );
672 0 : maNoteData.mpCaption->SetLogicRect( aCaptRect );
673 0 : aCreator.FitCaptionToRect();
674 : }
675 : }
676 : }
677 : // forget the initial caption data struct
678 12 : maNoteData.mxInitData.reset();
679 : }
680 32 : }
681 :
682 6 : void ScPostIt::CreateCaption( const ScAddress& rPos, const SdrCaptionObj* pCaption )
683 : {
684 : OSL_ENSURE( !maNoteData.mpCaption, "ScPostIt::CreateCaption - unexpected caption object found" );
685 6 : maNoteData.mpCaption = 0;
686 :
687 : /* #i104915# Never try to create notes in Undo document, leads to
688 : crash due to missing document members (e.g. row height array). */
689 : OSL_ENSURE( !mrDoc.IsUndo(), "ScPostIt::CreateCaption - note caption should not be created in undo documents" );
690 6 : if( mrDoc.IsUndo() )
691 6 : return;
692 :
693 : // drawing layer may be missing, if a note is copied into a clipboard document
694 6 : if( mrDoc.IsClipboard() )
695 0 : mrDoc.InitDrawLayer();
696 :
697 : // ScNoteCaptionCreator c'tor creates the caption and inserts it into the document and maNoteData
698 6 : ScNoteCaptionCreator aCreator( mrDoc, rPos, maNoteData );
699 6 : if( maNoteData.mpCaption )
700 : {
701 : // clone settings of passed caption
702 2 : if( pCaption )
703 : {
704 : // copy edit text object (object must be inserted into page already)
705 2 : if( OutlinerParaObject* pOPO = pCaption->GetOutlinerParaObject() )
706 2 : maNoteData.mpCaption->SetOutlinerParaObject( new OutlinerParaObject( *pOPO ) );
707 : // copy formatting items (after text has been copied to apply font formatting)
708 2 : maNoteData.mpCaption->SetMergedItemSetAndBroadcast( pCaption->GetMergedItemSet() );
709 : // move textbox position relative to new cell, copy textbox size
710 2 : Rectangle aCaptRect = pCaption->GetLogicRect();
711 2 : Point aDist = maNoteData.mpCaption->GetTailPos() - pCaption->GetTailPos();
712 2 : aCaptRect.Move( aDist.X(), aDist.Y() );
713 2 : maNoteData.mpCaption->SetLogicRect( aCaptRect );
714 2 : aCreator.FitCaptionToRect();
715 : }
716 : else
717 : {
718 : // set default formatting and default position
719 0 : ScCaptionUtil::SetDefaultItems( *maNoteData.mpCaption, mrDoc );
720 0 : aCreator.AutoPlaceCaption();
721 : }
722 :
723 : // create undo action
724 2 : if( ScDrawLayer* pDrawLayer = mrDoc.GetDrawLayer() )
725 2 : if( pDrawLayer->IsRecording() )
726 0 : pDrawLayer->AddCalcUndo( pDrawLayer->GetSdrUndoFactory().CreateUndoNewObject( *maNoteData.mpCaption ) );
727 : }
728 : }
729 :
730 24 : void ScPostIt::RemoveCaption()
731 : {
732 :
733 : /* Remove caption object only, if this note is its owner (e.g. notes in
734 : undo documents refer to captions in original document, do not remove
735 : them from drawing layer here). */
736 24 : ScDrawLayer* pDrawLayer = mrDoc.GetDrawLayer();
737 24 : if( maNoteData.mpCaption && (pDrawLayer == maNoteData.mpCaption->GetModel()) )
738 : {
739 : OSL_ENSURE( pDrawLayer, "ScPostIt::RemoveCaption - object without drawing layer" );
740 16 : SdrPage* pDrawPage = maNoteData.mpCaption->GetPage();
741 : OSL_ENSURE( pDrawPage, "ScPostIt::RemoveCaption - object without drawing page" );
742 16 : if( pDrawPage )
743 : {
744 16 : pDrawPage->RecalcObjOrdNums();
745 : // create drawing undo action (before removing the object to have valid draw page in undo action)
746 16 : bool bRecording = ( pDrawLayer && pDrawLayer->IsRecording() );
747 16 : if( bRecording )
748 2 : pDrawLayer->AddCalcUndo( pDrawLayer->GetSdrUndoFactory().CreateUndoDeleteObject( *maNoteData.mpCaption ) );
749 : // remove the object from the drawing page, delete if undo is disabled
750 16 : SdrObject* pObj = pDrawPage->RemoveObject( maNoteData.mpCaption->GetOrdNum() );
751 16 : if( !bRecording )
752 14 : SdrObject::Free( pObj );
753 : }
754 : }
755 24 : maNoteData.mpCaption = 0;
756 24 : }
757 :
758 : // ============================================================================
759 :
760 7 : void ScNoteUtil::UpdateCaptionPositions( ScDocument& rDoc, const ScRange& rRange )
761 : {
762 : // do not use ScCellIterator, it skips filtered and subtotal cells
763 14 : for( ScAddress aPos( rRange.aStart ); aPos.Tab() <= rRange.aEnd.Tab(); aPos.IncTab() )
764 26 : for( aPos.SetCol( rRange.aStart.Col() ); aPos.Col() <= rRange.aEnd.Col(); aPos.IncCol() )
765 97 : for( aPos.SetRow( rRange.aStart.Row() ); aPos.Row() <= rRange.aEnd.Row(); aPos.IncRow() )
766 78 : if( ScPostIt* pNote = rDoc.GetNotes(aPos.Tab())->findByAddress( aPos ) )
767 1 : pNote->UpdateCaptionPos( aPos );
768 7 : }
769 :
770 0 : SdrCaptionObj* ScNoteUtil::CreateTempCaption(
771 : ScDocument& rDoc, const ScAddress& rPos, SdrPage& rDrawPage,
772 : const OUString& rUserText, const Rectangle& rVisRect, bool bTailFront )
773 : {
774 0 : OUStringBuffer aBuffer( rUserText );
775 : // add plain text of invisible (!) cell note (no formatting etc.)
776 0 : SdrCaptionObj* pNoteCaption = 0;
777 0 : const ScPostIt* pNote = rDoc.GetNotes(rPos.Tab())->findByAddress( rPos );
778 0 : if( pNote && !pNote->IsCaptionShown() )
779 : {
780 0 : if( !aBuffer.isEmpty() )
781 0 : aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "\n--------\n" ) ).append( pNote->GetText() );
782 0 : pNoteCaption = pNote->GetOrCreateCaption( rPos );
783 : }
784 :
785 : // create a caption if any text exists
786 0 : if( !pNoteCaption && aBuffer.isEmpty() )
787 0 : return 0;
788 :
789 : // prepare visible rectangle (add default distance to all borders)
790 : Rectangle aVisRect(
791 0 : rVisRect.Left() + SC_NOTECAPTION_BORDERDIST_TEMP,
792 0 : rVisRect.Top() + SC_NOTECAPTION_BORDERDIST_TEMP,
793 0 : rVisRect.Right() - SC_NOTECAPTION_BORDERDIST_TEMP,
794 0 : rVisRect.Bottom() - SC_NOTECAPTION_BORDERDIST_TEMP );
795 :
796 : // create the caption object
797 0 : ScCaptionCreator aCreator( rDoc, rPos, true, bTailFront );
798 0 : SdrCaptionObj* pCaption = aCreator.GetCaption();
799 :
800 : // insert caption into page (needed to set caption text)
801 0 : rDrawPage.InsertObject( pCaption );
802 :
803 : // clone the edit text object, unless user text is present, then set this text
804 0 : if( pNoteCaption && rUserText.isEmpty() )
805 : {
806 0 : if( OutlinerParaObject* pOPO = pNoteCaption->GetOutlinerParaObject() )
807 0 : pCaption->SetOutlinerParaObject( new OutlinerParaObject( *pOPO ) );
808 : // set formatting (must be done after setting text) and resize the box to fit the text
809 0 : pCaption->SetMergedItemSetAndBroadcast( pNoteCaption->GetMergedItemSet() );
810 0 : Rectangle aCaptRect( pCaption->GetLogicRect().TopLeft(), pNoteCaption->GetLogicRect().GetSize() );
811 0 : pCaption->SetLogicRect( aCaptRect );
812 : }
813 : else
814 : {
815 : // if pNoteCaption is null, then aBuffer contains some text
816 0 : pCaption->SetText( aBuffer.makeStringAndClear() );
817 0 : ScCaptionUtil::SetDefaultItems( *pCaption, rDoc );
818 : // adjust caption size to text size
819 0 : long nMaxWidth = ::std::min< long >( aVisRect.GetWidth() * 2 / 3, SC_NOTECAPTION_MAXWIDTH_TEMP );
820 0 : pCaption->SetMergedItem( SdrTextAutoGrowWidthItem( sal_True ) );
821 0 : pCaption->SetMergedItem( SdrTextMinFrameWidthItem( SC_NOTECAPTION_WIDTH ) );
822 0 : pCaption->SetMergedItem( SdrTextMaxFrameWidthItem( nMaxWidth ) );
823 0 : pCaption->SetMergedItem( SdrTextAutoGrowHeightItem( sal_True ) );
824 0 : pCaption->AdjustTextFrameWidthAndHeight();
825 : }
826 :
827 : // move caption into visible area
828 0 : aCreator.AutoPlaceCaption( &aVisRect );
829 0 : return pCaption;
830 : }
831 :
832 2 : ScPostIt* ScNoteUtil::CreateNoteFromCaption(
833 : ScDocument& rDoc, const ScAddress& rPos, SdrCaptionObj& rCaption, bool bShown )
834 : {
835 2 : ScNoteData aNoteData( bShown );
836 2 : aNoteData.mpCaption = &rCaption;
837 2 : ScPostIt* pNote = new ScPostIt( rDoc, rPos, aNoteData, false );
838 2 : pNote->AutoStamp();
839 :
840 : // if pNote still points to the note after TakeNote(), insertion was successful
841 2 : if( rDoc.GetNotes(rPos.Tab())->insert( rPos, pNote ) )
842 : {
843 : // ScNoteCaptionCreator c'tor updates the caption object to be part of a note
844 2 : ScNoteCaptionCreator aCreator( rDoc, rPos, rCaption, bShown );
845 : }
846 : else
847 0 : return NULL;
848 :
849 2 : return pNote;
850 : }
851 :
852 7 : ScPostIt* ScNoteUtil::CreateNoteFromObjectData(
853 : ScDocument& rDoc, const ScAddress& rPos, SfxItemSet* pItemSet,
854 : OutlinerParaObject* pOutlinerObj, const Rectangle& rCaptionRect,
855 : bool bShown, bool bAlwaysCreateCaption )
856 : {
857 : OSL_ENSURE( pItemSet && pOutlinerObj, "ScNoteUtil::CreateNoteFromObjectData - item set and outliner object expected" );
858 7 : ScNoteData aNoteData( bShown );
859 7 : aNoteData.mxInitData.reset( new ScCaptionInitData );
860 7 : ScCaptionInitData& rInitData = *aNoteData.mxInitData;
861 7 : rInitData.mxItemSet.reset( pItemSet );
862 7 : rInitData.mxOutlinerObj.reset( pOutlinerObj );
863 :
864 : // convert absolute caption position to relative position
865 7 : rInitData.mbDefaultPosSize = rCaptionRect.IsEmpty();
866 7 : if( !rInitData.mbDefaultPosSize )
867 : {
868 2 : Rectangle aCellRect = ScDrawLayer::GetCellRect( rDoc, rPos, true );
869 2 : bool bNegPage = rDoc.IsNegativePage( rPos.Tab() );
870 2 : rInitData.maCaptionOffset.X() = bNegPage ? (aCellRect.Left() - rCaptionRect.Right()) : (rCaptionRect.Left() - aCellRect.Right());
871 2 : rInitData.maCaptionOffset.Y() = rCaptionRect.Top() - aCellRect.Top();
872 2 : rInitData.maCaptionSize = rCaptionRect.GetSize();
873 : }
874 :
875 : /* Create the note and insert it into the document. If the note is
876 : visible, the caption object will be created automatically. */
877 7 : ScPostIt* pNote = new ScPostIt( rDoc, rPos, aNoteData, bAlwaysCreateCaption );
878 7 : pNote->AutoStamp();
879 7 : if(rDoc.GetNotes(rPos.Tab())->insert( rPos, pNote ))
880 7 : return pNote;
881 : else
882 0 : return NULL;
883 : }
884 :
885 8 : ScPostIt* ScNoteUtil::CreateNoteFromString(
886 : ScDocument& rDoc, const ScAddress& rPos, const OUString& rNoteText,
887 : bool bShown, bool bAlwaysCreateCaption )
888 : {
889 8 : ScPostIt* pNote = 0;
890 8 : if( !rNoteText.isEmpty() )
891 : {
892 8 : ScNoteData aNoteData( bShown );
893 8 : aNoteData.mxInitData.reset( new ScCaptionInitData );
894 8 : ScCaptionInitData& rInitData = *aNoteData.mxInitData;
895 8 : rInitData.maSimpleText = rNoteText;
896 8 : rInitData.mbDefaultPosSize = true;
897 :
898 : /* Create the note and insert it into the document. If the note is
899 : visible, the caption object will be created automatically. */
900 8 : pNote = new ScPostIt( rDoc, rPos, aNoteData, bAlwaysCreateCaption );
901 8 : pNote->AutoStamp();
902 : //insert takes ownership
903 8 : if(!rDoc.GetNotes(rPos.Tab())->insert( rPos, pNote ))
904 0 : pNote = NULL;
905 : }
906 8 : return pNote;
907 : }
908 :
909 : // ============================================================================
910 : // ScNotes
911 : // ============================================================================
912 :
913 1682 : ScNotes::ScNotes(ScDocument* pDoc):
914 1682 : mpDoc(pDoc)
915 : {
916 :
917 1682 : }
918 :
919 3160 : ScNotes::~ScNotes()
920 : {
921 1580 : clear();
922 1580 : }
923 :
924 156 : ScNotes::iterator ScNotes::begin()
925 : {
926 156 : return maNoteMap.begin();
927 : }
928 :
929 171 : ScNotes::iterator ScNotes::end()
930 : {
931 171 : return maNoteMap.end();
932 : }
933 :
934 3119 : ScNotes::const_iterator ScNotes::begin() const
935 : {
936 3119 : return maNoteMap.begin();
937 : }
938 :
939 3187 : ScNotes::const_iterator ScNotes::end() const
940 : {
941 3187 : return maNoteMap.end();
942 : }
943 :
944 19 : size_t ScNotes::size() const
945 : {
946 19 : return maNoteMap.size();
947 : }
948 :
949 2 : bool ScNotes::empty() const
950 : {
951 2 : return maNoteMap.empty();
952 : }
953 :
954 185927 : ScPostIt* ScNotes::findByAddress(SCCOL nCol, SCROW nRow)
955 : {
956 185927 : ScNoteMap::iterator itr = maNoteMap.find(std::pair<SCCOL, SCROW>(nCol, nRow));
957 185927 : if (itr != maNoteMap.end())
958 39 : return itr->second;
959 :
960 185888 : return NULL;
961 : }
962 :
963 0 : const ScPostIt* ScNotes::findByAddress(SCCOL nCol, SCROW nRow) const
964 : {
965 0 : ScNoteMap::const_iterator itr = maNoteMap.find(std::pair<SCCOL, SCROW>(nCol, nRow));
966 0 : if (itr != maNoteMap.end())
967 0 : return itr->second;
968 :
969 0 : return NULL;
970 : }
971 :
972 146 : ScPostIt* ScNotes::findByAddress(const ScAddress& rPos)
973 : {
974 146 : return findByAddress(rPos.Col(), rPos.Row());
975 : }
976 :
977 0 : const ScPostIt* ScNotes::findByAddress(const ScAddress& rPos) const
978 : {
979 0 : return findByAddress(rPos.Col(), rPos.Row());
980 : }
981 :
982 35 : bool ScNotes::insert(SCCOL nCol, SCROW nRow, ScPostIt* pPostIt)
983 : {
984 35 : std::pair<iterator, bool> aResult = maNoteMap.insert(std::pair<ScAddress2D, ScPostIt*>(std::pair<SCCOL, SCROW>(nCol, nRow), pPostIt));
985 35 : if (!aResult.second)
986 0 : delete pPostIt;
987 :
988 35 : return aResult.second;
989 : }
990 :
991 21 : bool ScNotes::insert(const ScAddress& rPos, ScPostIt* pPostIt)
992 : {
993 21 : return insert(rPos.Col(), rPos.Row(), pPostIt);
994 : }
995 :
996 9 : void ScNotes::erase(SCCOL nCol, SCROW nRow, bool bForgetCaption)
997 : {
998 9 : iterator itr = maNoteMap.find(std::pair<SCCOL, SCROW>(nCol, nRow));
999 9 : if (itr != maNoteMap.end())
1000 : {
1001 5 : if (bForgetCaption)
1002 0 : itr->second->ForgetCaption();
1003 :
1004 5 : delete itr->second;
1005 5 : maNoteMap.erase(itr);
1006 : }
1007 9 : }
1008 :
1009 0 : void ScNotes::erase(const ScAddress& rPos)
1010 : {
1011 0 : erase(rPos.Col(), rPos.Row());
1012 0 : }
1013 :
1014 17 : ScPostIt* ScNotes::ReleaseNote(SCCOL nCol, SCROW nRow)
1015 : {
1016 17 : ScPostIt* pPostIt = NULL;
1017 17 : iterator itr = maNoteMap.find(std::pair<SCCOL, SCROW>(nCol, nRow));
1018 17 : if (itr!= maNoteMap.end())
1019 : {
1020 10 : pPostIt = itr->second;
1021 10 : maNoteMap.erase(itr);
1022 : }
1023 17 : return pPostIt;
1024 : }
1025 :
1026 7 : ScPostIt* ScNotes::ReleaseNote(const ScAddress& rPos)
1027 : {
1028 7 : return ReleaseNote(rPos.Col(), rPos.Row());
1029 : }
1030 :
1031 4 : ScPostIt* ScNotes::GetOrCreateNote(const ScAddress& rPos)
1032 : {
1033 4 : iterator itr = maNoteMap.find(std::pair<SCCOL, SCROW>(rPos.Col(), rPos.Row()));
1034 4 : if (itr != maNoteMap.end())
1035 0 : return itr->second;
1036 : else
1037 : {
1038 4 : ScPostIt* pPostIt = new ScPostIt(*mpDoc, rPos, false);
1039 4 : if(!insert(rPos, pPostIt))
1040 : assert(false);
1041 4 : return pPostIt;
1042 : }
1043 : }
1044 :
1045 1599 : void ScNotes::clear()
1046 : {
1047 1618 : for (iterator itr = maNoteMap.begin(); itr != maNoteMap.end(); ++itr)
1048 : {
1049 19 : delete itr->second;
1050 : }
1051 1599 : maNoteMap.clear();
1052 1599 : }
1053 :
1054 19 : void ScNotes::clone(ScDocument* pDoc, SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2, bool bCloneNoteCaption, SCTAB nTab, ScNotes& rTarget)
1055 : {
1056 19 : rTarget.clear();
1057 22 : for (ScNotes::iterator itr = maNoteMap.begin(); itr != maNoteMap.end(); ++itr)
1058 : {
1059 3 : SCCOL nCol = itr->first.first;
1060 3 : SCROW nRow = itr->first.second;
1061 :
1062 3 : if (nCol >= nCol1 && nCol <= nCol2 && nRow >= nRow1 && nRow <= nRow2)
1063 : {
1064 0 : rTarget.insert(nCol, nRow, itr->second->Clone( ScAddress(nCol, nRow, nTab), *pDoc, ScAddress(nCol, nRow, nTab), bCloneNoteCaption));
1065 : }
1066 : }
1067 19 : }
1068 :
1069 209 : void ScNotes::CopyFromClip(const ScNotes& rNotes, ScDocument* pDoc, SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2, SCsCOL nDx, SCsROW nDy, SCTAB nTab, bool bCloneCaption)
1070 : {
1071 218 : for (ScNotes::const_iterator itr = rNotes.begin(); itr != rNotes.end(); ++itr)
1072 : {
1073 9 : SCCOL nCol = itr->first.first;
1074 9 : SCROW nRow = itr->first.second;
1075 9 : if (nCol+nDx >= nCol1 && nCol+nDx <= nCol2 && nRow+nDy >= nRow1 && nRow+nDy <= nRow2)
1076 : {
1077 4 : erase(nCol+nDx, nRow+nDy);
1078 4 : insert(nCol+nDx, nRow+nDy, itr->second->Clone( ScAddress(nCol, nRow, nTab), *pDoc, ScAddress(nCol+nDx, nRow+nDy, nTab), bCloneCaption ));
1079 : }
1080 : }
1081 209 : }
1082 :
1083 957 : void ScNotes::erase(SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2, bool bForgetCaption)
1084 : {
1085 957 : ScNotes::iterator itr = maNoteMap.begin();
1086 1922 : while(itr != maNoteMap.end())
1087 : {
1088 8 : SCCOL nCol = itr->first.first;
1089 8 : SCROW nRow = itr->first.second;
1090 8 : ++itr;
1091 8 : if (nCol >= nCol1 && nCol <= nCol2 && nRow >= nRow1 && nRow <= nRow2)
1092 : {
1093 3 : erase(nCol, nRow, bForgetCaption);
1094 : }
1095 : }
1096 957 : }
1097 :
1098 9 : void ScNotes::CreateAllNoteCaptions(SCTAB nTab)
1099 : {
1100 9 : for(iterator itr = begin(), itrEnd = end(); itr != itrEnd; ++itr)
1101 : {
1102 0 : itr->second->GetOrCreateCaption(ScAddress(itr->first.first, itr->first.second, nTab));
1103 : }
1104 102 : }
1105 :
1106 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|