office-gobmx/editeng/inc/editdoc.hxx
Armin Le Grand (allotropia) ca3c6d468f ITEM: ItemPool Rework (I)
Driving forward the Item reworks I now take the
ItemPool in focus: It now does not hold any items
anymore and should be renamed to something like
ItemInfoProvider/ItemHelper, since it's main purpose
is to provide the Defaults for the Item functionality.

There is that SfxItemInfo, only a struct and bundling
SlotID and ItemFlags. There are also the DefaultItems,
just handled as ptrs in an array. It is/was always
error-prone to keep these in sync. Remember that it's
also necessary for the order to not only being sorted
but being increments of one with no gaps allowed in
the WhichIDs to which the Items are bound.

I now bundled that to a new class ItemInfo that joins
WhichID, SlotID, ItemFlags and the default Item. This
is a pure virtual base class, it comes in three
derivations:

(1) ItemInfoStatic:
This is supposed to be global static and hosts the
Item in a std::unique_ptr to ensure cleanup. It is
designed to be constructed once during runtime and
being shared globally. It allows the ItemPtr to be
nullptr to mark as non-static (if initial static is
not possible for some reason) but still offers the
needed data. Most cases (95%+) are of that case.
The contained Item is owned by that instance. The
flag isStaticDefault() is set at the Item.

(2) ItemInfoDynamic:
This is supposed to be used for cases where the Item
cannot be static: Mainly for SfxSetItem (that needs
a Pool itself in the contained SfxItemSet, so lifetime
is bound to that Pool), but other cases showed up in
the transition. These instances live while the Pool
lives and get destructed when the Pool goes down.
Also uses std::unique_ptr for the Item instance for
as much automated cleanup as possible, the contained
Item is owned by that instance, the instance by the
Pool. The flag isDynamicDefault() is set at the Item.

(3) ItemInfoUser:
This is used for UserDefaults that can be set for
every ItemInfo entry to 'overshadow' the default
from the 'outside'. It uses a regular Item and
the central access methods implCreateItemEntry/
implCleanupItemEntry to manage the Item instance,
thus works like a SfxPoolItemHolder. The Item
instance can be globally shared and re-used even
when the Pool goes down. Instances belong to the
Pool and are cleaned up when the Pool goes down.
This Item does not need any further flag to be
set.

The ItemInfos are organized using a class
called ItemInfoPackage:

This bundles groups of ItemInfoStatic to
functional instances. There are derivations/
implementations of this e.g. for Writer ItemPool
bundling all the needed defaults for Writer,
similar for draw/impress, Calc and other usages.

These ItemInfoPackage can be 'registered' at an
ItemPool using it's method registerItemInfoPackage.
This does all the needed stuff to setup that
group of ItemInfos at the Pool (It even sets
internal vars First/LastWhich, that info can just
be derived from the buildup ItemInfo Ptrs).

The ItemInfoPackage has methods 'size()' and
'getItemInfo(index) to allow looping over it
and deliver the infos the Pool needs. The
(forced, pure virtual) overloads of getItemInfo
in the specific implementations check for the
ItemPtr being nullptr and create a exclusive
incarnation of ItemInfoDynamic for the Pool if
needed, returning that. The Pool owns the
ItemInfoDynamic incarnations and uses the
ItemInfoStatic directly. On shutdown it cleans
up the ItemInfoDynamic as needed.

The ItemInfoUser is used by the Pool when a
UserDefault is set/used: for SetUserDefaultItem,
GetUserDefaultItem, ResetUserDefaultItem. It
is not held in a 2nd list, but directly in the
list of ItemInfo'ptrs: To keep track of this
an unordered_map is used that helds the original
ItemInfo associated with the WhichID. That way
no two lookups (as before) are needed to get the
current Pool's default for any WhichID.

The derivations of ItemInfoPackage are
encapsulated and just allow access to an
ItemInfoPackage& with a single method as
return value. All use a static local instance
of a std::array<ItemInfoStatic, FIXED_SIZE>
which constructs all ItemInfoStatic and the
static Item instances - if already possible.
Sometimes it is necessary to overload the
constructor to set some static instances
for Items later than the lib init. These are
also just marked with nullptr as Item instance.
Some need to overload getItemInfo to complete
instances of ItemInfoStatic, if needed, or
create and deliver instances of ItemInfoDynamic.

The registerItemInfoPackage also offers a
optional lambda callback: there were two cases
where local data from the Pool was needed to
incarnate the item - just add that to the
call to registerItemInfoPackage if needed,
see examples in the adapted code.

For the re-use of Items this means that now
in SfxItemSet/SfxPoolItemHolder *true* static
Items can and will be used without RefCount
directly and globally. This is also the case
for dynamic Items, with the exception of
differing Pools for SfxSetItems which cannot
be done.

Future:
That design is already prepared to allow
solving that Pool-chaining problem: currently
there are master/sub-pools and all accesses
have to traverse that structure before even
doing anything.
For the future the idea is more to 'compose'
a Pool by registering ItemInfoPackages, e.g.
for Writer pool you may start with SfxItemPool,
register the writer-specific ItemInfoPackage,
then the one for DrawingLayer (if needed) and
the one for EditEngine.
It should also be possible to get to smaller
granularities of that packages. Ideas for
new ones will emerge. We might also think
about composing Pools which can e.g. run Writer
and Chart, so allowing to use Chart *without*
OLE stuff in Writer - just ideas...

More changes:
- Adapted all stuff, cleaned up old stuff/
  definitions
- Removed FreezeIdRanges, that can be done
  once per Pool on-demand (and cannot be
  forgotten to be called)
- Merged XOutdevItemPool with SdrItemPool
  and offered a ItemInfoPackage which joins
  both needed sets of Items
- All the cleanup hassle with Pools and
  defaults cleaned up
- Adapted all access methods of the pool
  to use that new stuff. Pool chaining
  currently stays, but I use a central
  method 'getTargetPool' instead of
  recursive calling to get the correct
  Pool for the action

Change-Id: I2b8d3d4c3cc80b1d0d0b3c0f4bd90d7656b4bab7
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/163157
Tested-by: Jenkins
Reviewed-by: Armin Le Grand <Armin.Le.Grand@me.com>
2024-02-12 10:35:33 +01:00

234 lines
7.5 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#pragma once
#include "ParagraphPortionList.hxx"
#include "editattr.hxx"
#include "edtspell.hxx"
#include "eerdll2.hxx"
#include <editeng/svxfont.hxx>
#include <editeng/EPaM.hxx>
#include <svl/itemset.hxx>
#include <svl/style.hxx>
#include <svl/itempool.hxx>
#include <svl/languageoptions.hxx>
#include <tools/lineend.hxx>
#include <o3tl/typed_flags_set.hxx>
#include "TextPortion.hxx"
#include "TextPortionList.hxx"
#include "ItemList.hxx"
#include "ContentNode.hxx"
#include "EditLineList.hxx"
#include "EditPaM.hxx"
#include "EditSelection.hxx"
#include <cstddef>
#include <memory>
#include <string_view>
#include <vector>
class ImpEditEngine;
enum class TextRotation;
#define CHARPOSGROW 16
#define DEFTAB 720
void CreateFont( SvxFont& rFont, const SfxItemSet& rSet, bool bSearchInParent = true, SvtScriptType nScriptType = SvtScriptType::NONE );
sal_uInt16 GetScriptItemId( sal_uInt16 nItemId, SvtScriptType nScriptType );
bool IsScriptItemValid( sal_uInt16 nItemId, short nScriptType );
EditCharAttrib* MakeCharAttrib( SfxItemPool& rPool, const SfxPoolItem& rAttr, sal_Int32 nS, sal_Int32 nE );
class EditDoc;
class ContentAttribsInfo
{
private:
typedef std::vector<std::unique_ptr<EditCharAttrib> > CharAttribsType;
SfxItemSet aPrevParaAttribs;
CharAttribsType aPrevCharAttribs;
public:
ContentAttribsInfo( SfxItemSet aParaAttribs );
const SfxItemSet& GetPrevParaAttribs() const { return aPrevParaAttribs; }
const CharAttribsType& GetPrevCharAttribs() const { return aPrevCharAttribs; }
void AppendCharAttrib(EditCharAttrib* pNew);
};
typedef std::vector<Color> SvxColorList;
enum class DeleteMode {
Simple, RestOfWord, RestOfContent
};
class DeletedNodeInfo
{
private:
ContentNode* mpInvalidNode;
sal_Int32 nInvalidParagraph;
public:
DeletedNodeInfo( ContentNode* pNode, sal_Int32 nPos )
: mpInvalidNode(pNode)
, nInvalidParagraph(nPos)
{
}
ContentNode* GetNode() const { return mpInvalidNode; }
sal_Int32 GetPosition() const { return nInvalidParagraph; }
};
/** Edit document
*
* Holder, responsible for the content nodes and the manipulation of those.
*/
class EditDoc
{
private:
mutable sal_Int32 mnLastCache;
std::vector<std::unique_ptr<ContentNode>> maContents;
rtl::Reference<SfxItemPool> mpItemPool;
Link<LinkParamNone*,void> maModifyHdl;
SvxFont maDefFont; //faster than ever from the pool!!
sal_uInt16 mnDefTab;
bool mbIsVertical:1;
TextRotation mnRotation;
bool mbIsFixedCellHeight:1;
bool mbModified:1;
bool mbDisableAttributeExpanding:1;
public:
EditDoc( SfxItemPool* pItemPool );
~EditDoc();
void dumpAsXml(xmlTextWriterPtr pWriter) const;
void ClearSpellErrors();
bool IsModified() const { return mbModified; }
void SetModified( bool b );
void DisableAttributeExpanding() { mbDisableAttributeExpanding = true; }
void SetModifyHdl(const Link<LinkParamNone*,void>& rLink)
{
maModifyHdl = rLink;
}
void CreateDefFont( bool bUseStyles );
const SvxFont& GetDefFont() const { return maDefFont; }
void SetDefTab(sal_uInt16 nTab)
{
mnDefTab = nTab ? nTab : DEFTAB;
}
sal_uInt16 GetDefTab() const
{
return mnDefTab;
}
void SetVertical( bool bVertical ) { mbIsVertical = bVertical; }
bool IsEffectivelyVertical() const;
bool IsTopToBottom() const;
bool GetVertical() const;
void SetRotation( TextRotation nRotation ) { mnRotation = nRotation; }
TextRotation GetRotation() const { return mnRotation; }
void SetFixedCellHeight( bool bUseFixedCellHeight )
{
mbIsFixedCellHeight = bUseFixedCellHeight;
}
bool IsFixedCellHeight() const
{
return mbIsFixedCellHeight;
}
EditPaM Clear();
EditPaM RemoveText();
void RemoveChars( EditPaM aPaM, sal_Int32 nChars );
EditPaM InsertText( EditPaM aPaM, std::u16string_view rStr );
EditPaM InsertParaBreak( EditPaM aPaM, bool bKeepEndingAttribs );
EditPaM InsertFeature( EditPaM aPaM, const SfxPoolItem& rItem );
EditPaM ConnectParagraphs( ContentNode* pLeft, ContentNode* pRight );
OUString GetText( LineEnd eEnd ) const;
sal_Int32 GetTextLen() const;
OUString GetParaAsString( sal_Int32 nNode ) const;
static OUString GetParaAsString(const ContentNode* pNode, sal_Int32 nStartPos = 0, sal_Int32 nEndPos = -1);
EditPaM GetStartPaM() const;
EditPaM GetEndPaM() const;
SfxItemPool& GetItemPool()
{
return *mpItemPool;
}
const SfxItemPool& GetItemPool() const
{
return *mpItemPool;
}
void InsertAttrib( const SfxPoolItem& rItem, ContentNode* pNode, sal_Int32 nStart, sal_Int32 nEnd );
void InsertAttrib( ContentNode* pNode, sal_Int32 nStart, sal_Int32 nEnd, const SfxPoolItem& rPoolItem );
void InsertAttribInSelection( ContentNode* pNode, sal_Int32 nStart, sal_Int32 nEnd, const SfxPoolItem& rPoolItem );
bool RemoveAttribs( ContentNode* pNode, sal_Int32 nStart, sal_Int32 nEnd, sal_uInt16 nWhich );
bool RemoveAttribs( ContentNode* pNode, sal_Int32 nStart, sal_Int32 nEnd, EditCharAttrib*& rpStarting, EditCharAttrib*& rpEnding, sal_uInt16 nWhich );
static void FindAttribs( ContentNode* pNode, sal_Int32 nStartPos, sal_Int32 nEndPos, SfxItemSet& rCurSet );
sal_Int32 GetPos(const ContentNode* pNode) const;
const ContentNode* GetObject(sal_Int32 nPos) const;
ContentNode* GetObject(sal_Int32 nPos);
sal_Int32 Count() const;
void Insert(sal_Int32 nPos, std::unique_ptr<ContentNode> p);
/// deletes
void Remove(sal_Int32 nPos);
/// does not delete
std::unique_ptr<ContentNode> Release(sal_Int32 nPos);
static OUString GetSepStr( LineEnd eEnd );
};
inline EditCharAttrib* GetAttrib(CharAttribList::AttribsType& rAttribs, std::size_t nAttr)
{
return (nAttr < rAttribs.size()) ? rAttribs[nAttr].get() : nullptr;
}
#if OSL_DEBUG_LEVEL > 0 && !defined NDEBUG
void CheckOrderedList(const CharAttribList::AttribsType& rAttribs);
#endif
class EditEngineItemPool final : public SfxItemPool
{
public:
EditEngineItemPool();
private:
virtual ~EditEngineItemPool() override;
};
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */