LCOV - code coverage report
Current view: top level - libreoffice/sc/source/core/data - postit.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 323 500 64.6 %
Date: 2012-12-27 Functions: 55 70 78.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10