1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "txtcache.hxx"
24 #include <sfx2/viewsh.hxx>
25 #include <osl/diagnose.h>
28 SwTextLine::SwTextLine( SwTextFrame
const *pFrame
, std::unique_ptr
<SwParaPortion
> pNew
) :
29 SwCacheObj( static_cast<void const *>(pFrame
) ),
30 m_pLine( std::move(pNew
) )
34 SwTextLine::~SwTextLine()
38 void SwTextLine::UpdateCachePos()
40 // note: SwTextFrame lives longer than its SwTextLine, see ~SwTextFrame
42 const_cast<SwTextFrame
*>(static_cast<SwTextFrame
const *>(m_pOwner
))->SetCacheIdx(GetCachePos());
45 SwCacheObj
*SwTextLineAccess::NewObj()
47 return new SwTextLine( static_cast<SwTextFrame
const *>(m_pOwner
) );
50 SwParaPortion
*SwTextLineAccess::GetPara()
54 pRet
= static_cast<SwTextLine
*>(m_pObj
);
57 pRet
= static_cast<SwTextLine
*>(Get(false));
58 const_cast<SwTextFrame
*>(static_cast<SwTextFrame
const *>(m_pOwner
))->SetCacheIdx( pRet
->GetCachePos() );
60 if ( !pRet
->GetPara() )
61 pRet
->SetPara( new SwParaPortion
, true/*bDelete*/ );
62 return pRet
->GetPara();
65 SwTextLineAccess::SwTextLineAccess( const SwTextFrame
*pOwn
) :
66 SwCacheAccess( *SwTextFrame::GetTextCache(), pOwn
, pOwn
->GetCacheIdx() )
70 bool SwTextLineAccess::IsAvailable() const
72 return m_pObj
&& static_cast<SwTextLine
*>(m_pObj
)->GetPara();
75 bool SwTextFrame::HasPara_() const
77 SwTextLine
*pTextLine
= static_cast<SwTextLine
*>(SwTextFrame::GetTextCache()->
78 Get( this, GetCacheIdx(), false ));
81 if ( pTextLine
->GetPara() )
85 const_cast<SwTextFrame
*>(this)->mnCacheIndex
= USHRT_MAX
;
90 SwParaPortion
*SwTextFrame::GetPara()
92 if ( GetCacheIdx() != USHRT_MAX
)
94 SwTextLine
*pLine
= static_cast<SwTextLine
*>(SwTextFrame::GetTextCache()->
95 Get( this, GetCacheIdx(), false ));
97 return pLine
->GetPara();
99 mnCacheIndex
= USHRT_MAX
;
104 void SwTextFrame::ClearPara()
106 OSL_ENSURE( !IsLocked(), "+SwTextFrame::ClearPara: this is locked." );
107 if ( !IsLocked() && GetCacheIdx() != USHRT_MAX
)
109 SwTextLine
*pTextLine
= static_cast<SwTextLine
*>(SwTextFrame::GetTextCache()->
110 Get( this, GetCacheIdx(), false ));
113 pTextLine
->SetPara( nullptr, true/*bDelete*/ );
116 mnCacheIndex
= USHRT_MAX
;
120 void SwTextFrame::RemoveFromCache()
122 if (GetCacheIdx() != USHRT_MAX
)
124 s_pTextCache
->Delete(this, GetCacheIdx());
125 SetCacheIdx(USHRT_MAX
);
129 void SwTextFrame::SetPara( SwParaPortion
*pNew
, bool bDelete
)
131 if ( GetCacheIdx() != USHRT_MAX
)
133 // Only change the information, the CacheObj stays there
134 SwTextLine
*pTextLine
= static_cast<SwTextLine
*>(SwTextFrame::GetTextCache()->
135 Get( this, GetCacheIdx(), false ));
138 pTextLine
->SetPara( pNew
, bDelete
);
142 OSL_ENSURE( !pNew
, "+SetPara: Losing SwParaPortion" );
143 mnCacheIndex
= USHRT_MAX
;
147 { // Insert a new one
148 SwTextLine
*pTextLine
= new SwTextLine( this, std::unique_ptr
<SwParaPortion
>(pNew
) );
149 if (SwTextFrame::GetTextCache()->Insert(pTextLine
, false))
150 mnCacheIndex
= pTextLine
->GetCachePos();
153 OSL_FAIL( "+SetPara: InsertCache failed." );
158 /** Prevent the SwParaPortions of the *visible* paragraphs from being deleted;
159 they would just be recreated on the next paint.
161 Heuristic: 100 per view are visible
163 If the cache is too small, enlarge it to ensure there are sufficient free
164 entries for the layout so it doesn't have to throw away a node's
165 SwParaPortion when it starts formatting the next node.
167 SwSaveSetLRUOfst::SwSaveSetLRUOfst()
169 sal_uInt16
nVisibleShells(0);
170 for (auto pView
= SfxViewShell::GetFirst(true, checkSfxViewShell
<SwView
>);
172 pView
= SfxViewShell::GetNext(*pView
, true, checkSfxViewShell
<SwView
>))
174 // Apparently we are not interested here what document pView is for, but only in the
175 // total number of shells in the process?
179 sal_uInt16
const nPreserved(100 * nVisibleShells
);
180 SwCache
& rCache(*SwTextFrame::GetTextCache());
181 if (rCache
.GetCurMax() < nPreserved
+ 250)
183 rCache
.IncreaseMax(nPreserved
+ 250 - rCache
.GetCurMax());
185 rCache
.SetLRUOfst(nPreserved
);
188 SwSaveSetLRUOfst::~SwSaveSetLRUOfst()
190 SwTextFrame::GetTextCache()->ResetLRUOfst();
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */