android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / core / text / txtcache.cxx
blob188fa7bec697f4c42ec7b282e117a5c18fd41023
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
21 #include <txtfrm.hxx>
22 #include "porlay.hxx"
24 #include <sfx2/viewsh.hxx>
25 #include <osl/diagnose.h>
26 #include <view.hxx>
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
41 assert(m_pOwner);
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()
52 SwTextLine *pRet;
53 if ( m_pObj )
54 pRet = static_cast<SwTextLine*>(m_pObj);
55 else
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 ));
79 if ( pTextLine )
81 if ( pTextLine->GetPara() )
82 return true;
84 else
85 const_cast<SwTextFrame*>(this)->mnCacheIndex = USHRT_MAX;
87 return false;
90 SwParaPortion *SwTextFrame::GetPara()
92 if ( GetCacheIdx() != USHRT_MAX )
94 SwTextLine *pLine = static_cast<SwTextLine*>(SwTextFrame::GetTextCache()->
95 Get( this, GetCacheIdx(), false ));
96 if ( pLine )
97 return pLine->GetPara();
98 else
99 mnCacheIndex = USHRT_MAX;
101 return nullptr;
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 ));
111 if ( pTextLine )
113 pTextLine->SetPara( nullptr, true/*bDelete*/ );
115 else
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 ));
136 if ( pTextLine )
138 pTextLine->SetPara( pNew, bDelete );
140 else
142 OSL_ENSURE( !pNew, "+SetPara: Losing SwParaPortion" );
143 mnCacheIndex = USHRT_MAX;
146 else if ( pNew )
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();
151 else
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>);
171 pView != nullptr;
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?
176 ++nVisibleShells;
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: */