diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
commit | 940b4d1848e8c70ab7642901a68594e8016caffc (patch) | |
tree | eb72f344ee6c3d9b80a7ecc079ea79e9fba8676d /sfx2/source/control | |
parent | Initial commit. (diff) | |
download | libreoffice-upstream.tar.xz libreoffice-upstream.zip |
Adding upstream version 1:7.0.4.upstream/1%7.0.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sfx2/source/control')
34 files changed, 16654 insertions, 0 deletions
diff --git a/sfx2/source/control/asyncfunc.cxx b/sfx2/source/control/asyncfunc.cxx new file mode 100644 index 000000000..b81639af5 --- /dev/null +++ b/sfx2/source/control/asyncfunc.cxx @@ -0,0 +1,28 @@ +/* -*- 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/. + */ + +#include <asyncfunc.hxx> + +AsyncFunc::AsyncFunc(const std::function<void()>& rAsyncFunc) + : m_pAsyncFunc(rAsyncFunc) +{ +} + +AsyncFunc::~AsyncFunc() {} + +void AsyncFunc::Execute() +{ + if (m_pAsyncFunc) + m_pAsyncFunc(); +} + +//XUnoTunnel +UNO3_GETIMPLEMENTATION_IMPL(AsyncFunc) + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/bindings.cxx b/sfx2/source/control/bindings.cxx new file mode 100644 index 000000000..1baa03845 --- /dev/null +++ b/sfx2/source/control/bindings.cxx @@ -0,0 +1,1767 @@ +/* -*- 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 . + */ + +#include <sal/config.h> + +#include <iomanip> + +#include <sal/log.hxx> +#include <svl/itempool.hxx> +#include <svl/itemiter.hxx> +#include <svl/eitem.hxx> +#include <svl/intitem.hxx> +#include <svl/stritem.hxx> +#include <vcl/svapp.hxx> +#include <vcl/timer.hxx> +#include <com/sun/star/frame/XDispatch.hpp> +#include <com/sun/star/frame/XDispatchProvider.hpp> +#include <com/sun/star/frame/DispatchResultState.hpp> +#include <itemdel.hxx> + +//Includes below due to nInReschedule +#include <sfx2/bindings.hxx> +#include <sfx2/msg.hxx> +#include <statcach.hxx> +#include <sfx2/ctrlitem.hxx> +#include <sfx2/app.hxx> +#include <sfx2/dispatch.hxx> +#include <sfx2/module.hxx> +#include <sfx2/request.hxx> +#include <workwin.hxx> +#include <unoctitm.hxx> +#include <sfx2/viewfrm.hxx> +#include <sfx2/objsh.hxx> +#include <sfx2/msgpool.hxx> + +#include <cstddef> +#include <memory> +#include <unordered_map> +#include <utility> +#include <vector> + +using namespace ::com::sun::star; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::util; + +#define TIMEOUT_FIRST 300 +#define TIMEOUT_UPDATING 20 + +typedef std::unordered_map< sal_uInt16, bool > InvalidateSlotMap; + +struct SfxFoundCache_Impl +{ + sal_uInt16 nWhichId; // If available: Which-Id, else: nSlotId + const SfxSlot* pSlot; // Pointer to <Master-Slot> + SfxStateCache& rCache; // Pointer to StatusCache + + SfxFoundCache_Impl(sal_uInt16 nW, const SfxSlot *pS, SfxStateCache& rC) + : nWhichId(nW) + , pSlot(pS) + , rCache(rC) + {} +}; + +class SfxFoundCacheArr_Impl +{ + typedef std::vector<std::unique_ptr<SfxFoundCache_Impl> > DataType; + DataType maData; + +public: + + SfxFoundCache_Impl& operator[] ( size_t i ) + { + return *maData[i]; + } + + size_t size() const + { + return maData.size(); + } + + void push_back( SfxFoundCache_Impl* p ) + { + maData.push_back(std::unique_ptr<SfxFoundCache_Impl>(p)); + } +}; + +class SfxBindings_Impl +{ +public: + css::uno::Reference< css::frame::XDispatchRecorder > xRecorder; + css::uno::Reference< css::frame::XDispatchProvider > xProv; + SfxWorkWindow* pWorkWin; + SfxBindings* pSubBindings; + std::vector<std::unique_ptr<SfxStateCache>> pCaches; // One cache for each binding + std::size_t nCachedFunc1; // index for the last one called + std::size_t nCachedFunc2; // index for the second last called + std::size_t nMsgPos; // Message-Position relative the one to be updated + bool bContextChanged; + bool bMsgDirty; // Has a MessageServer been invalidated? + bool bAllMsgDirty; // Has a MessageServer been invalidated? + bool bAllDirty; // After InvalidateAll + bool bCtrlReleased; // while EnterRegistrations + AutoTimer aAutoTimer; // for volatile Slots + bool bInUpdate; // for Assertions + bool bInNextJob; // for Assertions + bool bFirstRound; // First round in Update + sal_uInt16 nOwnRegLevel; // Counts the real Locks, except those of the Super Bindings + InvalidateSlotMap m_aInvalidateSlots; // store slots which are invalidated while in update +}; + +SfxBindings::SfxBindings() +: pImpl(new SfxBindings_Impl), + pDispatcher(nullptr), + nRegLevel(1) // first becomes 0, when the Dispatcher is set + +{ + pImpl->nMsgPos = 0; + pImpl->bAllMsgDirty = true; + pImpl->bContextChanged = false; + pImpl->bMsgDirty = true; + pImpl->bAllDirty = true; + pImpl->nCachedFunc1 = 0; + pImpl->nCachedFunc2 = 0; + pImpl->bCtrlReleased = false; + pImpl->bFirstRound = false; + pImpl->bInNextJob = false; + pImpl->bInUpdate = false; + pImpl->pSubBindings = nullptr; + pImpl->pWorkWin = nullptr; + pImpl->nOwnRegLevel = nRegLevel; + + // all caches are valid (no pending invalidate-job) + // create the list of caches + pImpl->aAutoTimer.SetInvokeHandler( LINK(this, SfxBindings, NextJob) ); + pImpl->aAutoTimer.SetDebugName( "sfx::SfxBindings aAutoTimer" ); +} + + +SfxBindings::~SfxBindings() + +/* [Description] + + Destructor of the SfxBindings class. The one, for each <SfxApplication> + existing Instance is automatically destroyed by the <SfxApplication> + after the execution of <SfxApplication::Exit()>. + + The still existing <SfxControllerItem> instances, which are registered + by the SfxBindings instance, are automatically destroyed in the Destructor. + These are usually the Floating-Toolboxen, Value-Sets + etc. Arrays of SfxControllerItems may at this time no longer exist. +*/ + +{ + // The SubBindings should not be locked! + pImpl->pSubBindings = nullptr; + + ENTERREGISTRATIONS(); + + pImpl->aAutoTimer.Stop(); + DeleteControllers_Impl(); + + // Delete Caches + pImpl->pCaches.clear(); + + DELETEZ( pImpl->pWorkWin ); +} + + +void SfxBindings::DeleteControllers_Impl() +{ + // in the first round delete Controllers + std::size_t nCount = pImpl->pCaches.size(); + std::size_t nCache; + for ( nCache = 0; nCache < nCount; ++nCache ) + { + // Remember were you are + SfxStateCache *pCache = pImpl->pCaches[nCache].get(); + sal_uInt16 nSlotId = pCache->GetId(); + + // Re-align, because the cache may have been reduced + std::size_t nNewCount = pImpl->pCaches.size(); + if ( nNewCount < nCount ) + { + nCache = GetSlotPos(nSlotId); + if ( nCache >= nNewCount || + nSlotId != pImpl->pCaches[nCache]->GetId() ) + --nCache; + nCount = nNewCount; + } + } + + // Delete all Caches + for ( nCache = pImpl->pCaches.size(); nCache > 0; --nCache ) + { + // Get Cache via css::sdbcx::Index + SfxStateCache *pCache = pImpl->pCaches[ nCache-1 ].get(); + + // unbind all controllers in the cache + SfxControllerItem *pNext; + for ( SfxControllerItem *pCtrl = pCache->GetItemLink(); + pCtrl; pCtrl = pNext ) + { + pNext = pCtrl->GetItemLink(); + pCtrl->UnBind(); + } + + if ( pCache->GetInternalController() ) + pCache->GetInternalController()->UnBind(); + + // Delete Cache + pImpl->pCaches.erase(pImpl->pCaches.begin() + nCache - 1); + } +} + + +void SfxBindings::HidePopups( bool bHide ) +{ + // Hide SfxChildWindows + DBG_ASSERT( pDispatcher, "HidePopups not allowed without dispatcher" ); + if ( pImpl->pWorkWin ) + pImpl->pWorkWin->HidePopups_Impl( bHide ); +} + +void SfxBindings::Update_Impl(SfxStateCache& rCache /*The up to date SfxStatusCache*/) +{ + if (rCache.GetDispatch().is() && rCache.GetItemLink()) + { + rCache.SetCachedState(true); + if (!rCache.GetInternalController()) + return; + } + + if ( !pDispatcher ) + return; + + // gather together all with the same status method which are dirty + SfxDispatcher &rDispat = *pDispatcher; + const SfxSlot *pRealSlot = nullptr; + const SfxSlotServer* pMsgServer = nullptr; + SfxFoundCacheArr_Impl aFound; + std::unique_ptr<SfxItemSet> pSet = CreateSet_Impl(rCache, pRealSlot, &pMsgServer, aFound); + bool bUpdated = false; + if ( pSet ) + { + // Query Status + if ( rDispat.FillState_( *pMsgServer, *pSet, pRealSlot ) ) + { + // Post Status + for ( size_t nPos = 0; nPos < aFound.size(); ++nPos ) + { + const SfxFoundCache_Impl& rFound = aFound[nPos]; + sal_uInt16 nWhich = rFound.nWhichId; + const SfxPoolItem *pItem = nullptr; + SfxItemState eState = pSet->GetItemState(nWhich, true, &pItem); + if ( eState == SfxItemState::DEFAULT && SfxItemPool::IsWhich(nWhich) ) + pItem = &pSet->Get(nWhich); + UpdateControllers_Impl( rFound, pItem, eState ); + } + bUpdated = true; + } + + pSet.reset(); + } + + if (!bUpdated) + { + SfxFoundCache_Impl aFoundCache(0, pRealSlot, rCache); + UpdateControllers_Impl( aFoundCache, nullptr, SfxItemState::DISABLED); + } +} + +void SfxBindings::InvalidateSlotsInMap_Impl() +{ + for (auto const& slot : pImpl->m_aInvalidateSlots) + Invalidate( slot.first ); + + pImpl->m_aInvalidateSlots.clear(); +} + + +void SfxBindings::AddSlotToInvalidateSlotsMap_Impl( sal_uInt16 nId ) +{ + pImpl->m_aInvalidateSlots[nId] = true; +} + + +void SfxBindings::Update +( + sal_uInt16 nId // the bound and up-to-date Slot-Id +) +{ + if ( pDispatcher ) + pDispatcher->Flush(); + + if ( pImpl->pSubBindings ) + pImpl->pSubBindings->Update( nId ); + + SfxStateCache* pCache = GetStateCache( nId ); + if ( !pCache ) + return; + + pImpl->bInUpdate = true; + if ( pImpl->bMsgDirty ) + { + UpdateSlotServer_Impl(); + pCache = GetStateCache( nId ); + } + + if (pCache) + { + bool bInternalUpdate = true; + if( pCache->GetDispatch().is() && pCache->GetItemLink() ) + { + pCache->SetCachedState(true); + bInternalUpdate = ( pCache->GetInternalController() != nullptr ); + } + + if ( bInternalUpdate ) + { + // Query Status + const SfxSlotServer* pMsgServer = pDispatcher ? pCache->GetSlotServer(*pDispatcher, pImpl->xProv) : nullptr; + if ( !pCache->IsControllerDirty() ) + { + pImpl->bInUpdate = false; + InvalidateSlotsInMap_Impl(); + return; + } + if (!pMsgServer) + { + pCache->SetState(SfxItemState::DISABLED, nullptr); + pImpl->bInUpdate = false; + InvalidateSlotsInMap_Impl(); + return; + } + + Update_Impl(*pCache); + } + + pImpl->bAllDirty = false; + } + + pImpl->bInUpdate = false; + InvalidateSlotsInMap_Impl(); +} + + +void SfxBindings::Update() +{ + if ( pImpl->pSubBindings ) + pImpl->pSubBindings->Update(); + + if ( !pDispatcher ) + return; + + if ( nRegLevel ) + return; + + pImpl->bInUpdate = true; + pDispatcher->Flush(); + pDispatcher->Update_Impl(); + while ( !NextJob_Impl(nullptr) ) + ; // loop + pImpl->bInUpdate = false; + InvalidateSlotsInMap_Impl(); +} + + +void SfxBindings::SetState +( + const SfxItemSet& rSet // status values to be set +) +{ + // when locked then only invalidate + if ( nRegLevel ) + { + SfxItemIter aIter(rSet); + for ( const SfxPoolItem *pItem = aIter.GetCurItem(); + pItem; + pItem = aIter.NextItem() ) + Invalidate( pItem->Which() ); + } + else + { + // Status may be accepted only if all slot-pointers are set + if ( pImpl->bMsgDirty ) + UpdateSlotServer_Impl(); + + // Iterate over the itemset, update if the slot bound + //! Bug: Use WhichIter and possibly send VoidItems up + SfxItemIter aIter(rSet); + for ( const SfxPoolItem *pItem = aIter.GetCurItem(); + pItem; + pItem = aIter.NextItem() ) + { + SfxStateCache* pCache = + GetStateCache( rSet.GetPool()->GetSlotId(pItem->Which()) ); + if ( pCache ) + { + // Update status + if ( !pCache->IsControllerDirty() ) + pCache->Invalidate(false); + pCache->SetState( SfxItemState::DEFAULT, pItem ); + + //! Not implemented: Updates from EnumSlots via master slots + } + } + } +} + + +void SfxBindings::SetState +( + const SfxPoolItem& rItem // Status value to be set +) +{ + if ( nRegLevel ) + { + Invalidate( rItem.Which() ); + } + else + { + // Status may be accepted only if all slot-pointers are set + if ( pImpl->bMsgDirty ) + UpdateSlotServer_Impl(); + + //update if the slot bound + DBG_ASSERT( SfxItemPool::IsSlot( rItem.Which() ), + "cannot set items with which-id" ); + SfxStateCache* pCache = GetStateCache( rItem.Which() ); + if ( pCache ) + { + // Update Status + if ( !pCache->IsControllerDirty() ) + pCache->Invalidate(false); + pCache->SetState( SfxItemState::DEFAULT, &rItem ); + + //! Not implemented: Updates from EnumSlots via master slots + } + } +} + + +SfxStateCache* SfxBindings::GetAnyStateCache_Impl( sal_uInt16 nId ) +{ + SfxStateCache* pCache = GetStateCache( nId ); + if ( !pCache && pImpl->pSubBindings ) + return pImpl->pSubBindings->GetAnyStateCache_Impl( nId ); + return pCache; +} + +SfxStateCache* SfxBindings::GetStateCache +( + sal_uInt16 nId /* Slot-Id, which SfxStatusCache is to be found */ +) +{ + return GetStateCache(nId, nullptr); +} + +SfxStateCache* SfxBindings::GetStateCache +( + sal_uInt16 nId, /* Slot-Id, which SfxStatusCache is to be found */ + std::size_t * pPos /* NULL for instance the position from which the + bindings are to be searched binary. Returns the + position back for where the nId was found, + or where it was inserted. */ +) +{ + // is the specified function bound? + const std::size_t nStart = ( pPos ? *pPos : 0 ); + const std::size_t nPos = GetSlotPos( nId, nStart ); + + if ( nPos < pImpl->pCaches.size() && + pImpl->pCaches[nPos]->GetId() == nId ) + { + if ( pPos ) + *pPos = nPos; + return pImpl->pCaches[nPos].get(); + } + return nullptr; +} + + +void SfxBindings::InvalidateAll +( + bool bWithMsg /* true Mark Slot Server as invalid + false Slot Server remains valid */ +) +{ + DBG_ASSERT( !pImpl->bInUpdate, "SfxBindings::Invalidate while in update" ); + + if ( pImpl->pSubBindings ) + pImpl->pSubBindings->InvalidateAll( bWithMsg ); + + // everything is already set dirty or downing => nothing to do + if ( !pDispatcher || + ( pImpl->bAllDirty && ( !bWithMsg || pImpl->bAllMsgDirty ) ) || + SfxGetpApp()->IsDowning() ) + { + return; + } + + pImpl->bAllMsgDirty = pImpl->bAllMsgDirty || bWithMsg; + pImpl->bMsgDirty = pImpl->bMsgDirty || pImpl->bAllMsgDirty || bWithMsg; + pImpl->bAllDirty = true; + + for (std::unique_ptr<SfxStateCache>& pCache : pImpl->pCaches) + pCache->Invalidate(bWithMsg); + + pImpl->nMsgPos = 0; + if ( !nRegLevel ) + { + pImpl->aAutoTimer.Stop(); + pImpl->aAutoTimer.SetTimeout(TIMEOUT_FIRST); + pImpl->aAutoTimer.Start(); + } +} + + +void SfxBindings::Invalidate +( + const sal_uInt16* pIds /* numerically sorted NULL-terminated array of + slot IDs (individual, not as a couple!) */ +) +{ + if ( pImpl->bInUpdate ) + { + sal_Int32 i = 0; + while ( pIds[i] != 0 ) + AddSlotToInvalidateSlotsMap_Impl( pIds[i++] ); + + if ( pImpl->pSubBindings ) + pImpl->pSubBindings->Invalidate( pIds ); + return; + } + + if ( pImpl->pSubBindings ) + pImpl->pSubBindings->Invalidate( pIds ); + + // everything is already set dirty or downing => nothing to do + if ( !pDispatcher || pImpl->bAllDirty || SfxGetpApp()->IsDowning() ) + return; + + // Search binary in always smaller areas + for ( std::size_t n = GetSlotPos(*pIds); + *pIds && n < pImpl->pCaches.size(); + n = GetSlotPos(*pIds, n) ) + { + // If SID is ever bound, then invalidate the cache + SfxStateCache *pCache = pImpl->pCaches[n].get(); + if ( pCache->GetId() == *pIds ) + pCache->Invalidate(false); + + // Next SID + if ( !*++pIds ) + break; + assert( *pIds > *(pIds-1) ); + } + + // if not enticed to start update timer + pImpl->nMsgPos = 0; + if ( !nRegLevel ) + { + pImpl->aAutoTimer.Stop(); + pImpl->aAutoTimer.SetTimeout(TIMEOUT_FIRST); + pImpl->aAutoTimer.Start(); + } +} + + +void SfxBindings::InvalidateShell +( + const SfxShell& rSh, /* <SfxShell> whose Slot-Ids should be + invalidated */ + bool bDeep /* true + also the SfxShell's inherited slot IDs are invalidated + + false + the inherited and not overridden Slot-Ids are + invalidated */ + // for now always bDeep +) +{ + DBG_ASSERT( !pImpl->bInUpdate, "SfxBindings::Invalidate while in update" ); + + if ( pImpl->pSubBindings ) + pImpl->pSubBindings->InvalidateShell( rSh, bDeep ); + + if ( !pDispatcher || pImpl->bAllDirty || SfxGetpApp()->IsDowning() ) + return; + + // flush now already, it is done in GetShellLevel (rsh) anyway, + // important so that is set correctly: pImpl-> ball(Msg)Dirty + pDispatcher->Flush(); + + if ((pImpl->bAllDirty && pImpl->bAllMsgDirty) || SfxGetpApp()->IsDowning()) + { + // if the next one is anyway, then all the servers are collected + return; + } + + // Find Level + sal_uInt16 nLevel = pDispatcher->GetShellLevel(rSh); + if ( nLevel == USHRT_MAX ) + return; + + for (std::unique_ptr<SfxStateCache>& pCache : pImpl->pCaches) + { + const SfxSlotServer *pMsgServer = + pCache->GetSlotServer(*pDispatcher, pImpl->xProv); + if ( pMsgServer && pMsgServer->GetShellLevel() == nLevel ) + pCache->Invalidate(false); + } + pImpl->nMsgPos = 0; + if ( !nRegLevel ) + { + pImpl->aAutoTimer.Stop(); + pImpl->aAutoTimer.SetTimeout(TIMEOUT_FIRST); + pImpl->aAutoTimer.Start(); + pImpl->bFirstRound = true; + } +} + + +void SfxBindings::Invalidate +( + sal_uInt16 nId // Status value to be set +) +{ + if ( pImpl->bInUpdate ) + { + AddSlotToInvalidateSlotsMap_Impl( nId ); + if ( pImpl->pSubBindings ) + pImpl->pSubBindings->Invalidate( nId ); + return; + } + + if ( pImpl->pSubBindings ) + pImpl->pSubBindings->Invalidate( nId ); + + if ( !pDispatcher || pImpl->bAllDirty || SfxGetpApp()->IsDowning() ) + return; + + SfxStateCache* pCache = GetStateCache(nId); + if ( pCache ) + { + pCache->Invalidate(false); + pImpl->nMsgPos = std::min(GetSlotPos(nId), pImpl->nMsgPos); + if ( !nRegLevel ) + { + pImpl->aAutoTimer.Stop(); + pImpl->aAutoTimer.SetTimeout(TIMEOUT_FIRST); + pImpl->aAutoTimer.Start(); + } + } +} + + +void SfxBindings::Invalidate +( + sal_uInt16 nId, // Status value to be set + bool bWithItem, // Clear StateCache? + bool bWithMsg // Get new SlotServer? +) +{ + DBG_ASSERT( !pImpl->bInUpdate, "SfxBindings::Invalidate while in update" ); + + if ( pImpl->pSubBindings ) + pImpl->pSubBindings->Invalidate( nId, bWithItem, bWithMsg ); + + if ( SfxGetpApp()->IsDowning() ) + return; + + SfxStateCache* pCache = GetStateCache(nId); + if ( !pCache ) + return; + + if ( bWithItem ) + pCache->ClearCache(); + pCache->Invalidate(bWithMsg); + + if ( !pDispatcher || pImpl->bAllDirty ) + return; + + pImpl->nMsgPos = std::min(GetSlotPos(nId), pImpl->nMsgPos); + if ( !nRegLevel ) + { + pImpl->aAutoTimer.Stop(); + pImpl->aAutoTimer.SetTimeout(TIMEOUT_FIRST); + pImpl->aAutoTimer.Start(); + } +} + + +std::size_t SfxBindings::GetSlotPos( sal_uInt16 nId, std::size_t nStartSearchAt ) +{ + // answer immediately if a function-seek comes repeated + if ( pImpl->nCachedFunc1 < pImpl->pCaches.size() && + pImpl->pCaches[pImpl->nCachedFunc1]->GetId() == nId ) + { + return pImpl->nCachedFunc1; + } + if ( pImpl->nCachedFunc2 < pImpl->pCaches.size() && + pImpl->pCaches[pImpl->nCachedFunc2]->GetId() == nId ) + { + // swap the caches + std::swap(pImpl->nCachedFunc1, pImpl->nCachedFunc2); + return pImpl->nCachedFunc1; + } + + // binary search, if not found, seek to target-position + if ( pImpl->pCaches.size() <= nStartSearchAt ) + { + return 0; + } + if ( pImpl->pCaches.size() == (nStartSearchAt+1) ) + { + return pImpl->pCaches[nStartSearchAt]->GetId() >= nId ? 0 : 1; + } + std::size_t nLow = nStartSearchAt; + std::size_t nMid = 0; + std::size_t nHigh = 0; + bool bFound = false; + nHigh = pImpl->pCaches.size() - 1; + while ( !bFound && nLow <= nHigh ) + { + nMid = (nLow + nHigh) >> 1; + DBG_ASSERT( nMid < pImpl->pCaches.size(), "bsearch is buggy" ); + int nDiff = static_cast<int>(nId) - static_cast<int>( (pImpl->pCaches[nMid])->GetId() ); + if ( nDiff < 0) + { if ( nMid == 0 ) + break; + nHigh = nMid - 1; + } + else if ( nDiff > 0 ) + { nLow = nMid + 1; + if ( nLow == 0 ) + break; + } + else + bFound = true; + } + std::size_t nPos = bFound ? nMid : nLow; + DBG_ASSERT( nPos <= pImpl->pCaches.size(), "" ); + DBG_ASSERT( nPos == pImpl->pCaches.size() || + nId <= pImpl->pCaches[nPos]->GetId(), "" ); + DBG_ASSERT( nPos == nStartSearchAt || + nId > pImpl->pCaches[nPos-1]->GetId(), "" ); + DBG_ASSERT( ( (nPos+1) >= pImpl->pCaches.size() ) || + nId < pImpl->pCaches[nPos+1]->GetId(), "" ); + pImpl->nCachedFunc2 = pImpl->nCachedFunc1; + pImpl->nCachedFunc1 = nPos; + return nPos; +} + +void SfxBindings::RegisterInternal_Impl( SfxControllerItem& rItem ) +{ + Register_Impl( rItem, true ); + +} + +void SfxBindings::Register( SfxControllerItem& rItem ) +{ + Register_Impl( rItem, false ); +} + +void SfxBindings::Register_Impl( SfxControllerItem& rItem, bool bInternal ) +{ +// DBG_ASSERT( nRegLevel > 0, "registration without EnterRegistrations" ); + DBG_ASSERT( !pImpl->bInNextJob, "SfxBindings::Register while status-updating" ); + + // insert new cache if it does not already exist + sal_uInt16 nId = rItem.GetId(); + std::size_t nPos = GetSlotPos(nId); + if ( nPos >= pImpl->pCaches.size() || + pImpl->pCaches[nPos]->GetId() != nId ) + { + pImpl->pCaches.insert( pImpl->pCaches.begin() + nPos, std::make_unique<SfxStateCache>(nId) ); + DBG_ASSERT( nPos == 0 || + pImpl->pCaches[nPos]->GetId() > + pImpl->pCaches[nPos-1]->GetId(), "" ); + DBG_ASSERT( (nPos == pImpl->pCaches.size()-1) || + pImpl->pCaches[nPos]->GetId() < + pImpl->pCaches[nPos+1]->GetId(), "" ); + pImpl->bMsgDirty = true; + } + + // enqueue the new binding + if ( bInternal ) + { + pImpl->pCaches[nPos]->SetInternalController( &rItem ); + } + else + { + SfxControllerItem *pOldItem = pImpl->pCaches[nPos]->ChangeItemLink(&rItem); + rItem.ChangeItemLink(pOldItem); + } +} + + +void SfxBindings::Release( SfxControllerItem& rItem ) +{ + DBG_ASSERT( !pImpl->bInNextJob, "SfxBindings::Release while status-updating" ); + ENTERREGISTRATIONS(); + + // find the bound function + sal_uInt16 nId = rItem.GetId(); + std::size_t nPos = GetSlotPos(nId); + SfxStateCache* pCache = (nPos < pImpl->pCaches.size()) ? pImpl->pCaches[nPos].get() : nullptr; + if ( pCache && pCache->GetId() == nId ) + { + if ( pCache->GetInternalController() == &rItem ) + { + pCache->ReleaseInternalController(); + } + else + { + // is this the first binding in the list? + SfxControllerItem* pItem = pCache->GetItemLink(); + if ( pItem == &rItem ) + pCache->ChangeItemLink( rItem.GetItemLink() ); + else + { + // search the binding in the list + while ( pItem && pItem->GetItemLink() != &rItem ) + pItem = pItem->GetItemLink(); + + // unlink it if it was found + if ( pItem ) + pItem->ChangeItemLink( rItem.GetItemLink() ); + } + } + + // was this the last controller? + if ( pCache->GetItemLink() == nullptr && !pCache->GetInternalController() ) + { + pImpl->bCtrlReleased = true; + } + } + + LEAVEREGISTRATIONS(); +} + + +const SfxPoolItem* SfxBindings::ExecuteSynchron( sal_uInt16 nId, const SfxPoolItem** ppItems ) +{ + if( !nId || !pDispatcher ) + return nullptr; + + return Execute_Impl( nId, ppItems, 0, SfxCallMode::SYNCHRON, nullptr ); +} + +bool SfxBindings::Execute( sal_uInt16 nId, const SfxPoolItem** ppItems, SfxCallMode nCallMode ) +{ + if( !nId || !pDispatcher ) + return false; + + const SfxPoolItem* pRet = Execute_Impl( nId, ppItems, 0, nCallMode, nullptr ); + return ( pRet != nullptr ); +} + +const SfxPoolItem* SfxBindings::Execute_Impl( sal_uInt16 nId, const SfxPoolItem** ppItems, sal_uInt16 nModi, SfxCallMode nCallMode, + const SfxPoolItem **ppInternalArgs, bool bGlobalOnly ) +{ + SfxStateCache *pCache = GetStateCache( nId ); + if ( !pCache ) + { + SfxBindings *pBind = pImpl->pSubBindings; + while ( pBind ) + { + if ( pBind->GetStateCache( nId ) ) + return pBind->Execute_Impl( nId, ppItems, nModi, nCallMode, ppInternalArgs, bGlobalOnly ); + pBind = pBind->pImpl->pSubBindings; + } + } + + SfxDispatcher &rDispatcher = *pDispatcher; + rDispatcher.Flush(); + + // get SlotServer (Slot+ShellLevel) and Shell from cache + std::unique_ptr<SfxStateCache> xCache; + if ( !pCache ) + { + // Execution of non cached slots (Accelerators don't use Controllers) + // slot is uncached, use SlotCache to handle external dispatch providers + xCache.reset(new SfxStateCache(nId)); + pCache = xCache.get(); + pCache->GetSlotServer( rDispatcher, pImpl->xProv ); + } + + if ( pCache->GetDispatch().is() ) + { + DBG_ASSERT( !ppInternalArgs, "Internal args get lost when dispatched!" ); + + SfxItemPool &rPool = GetDispatcher()->GetFrame()->GetObjectShell()->GetPool(); + SfxRequest aReq( nId, nCallMode, rPool ); + aReq.SetModifier( nModi ); + if( ppItems ) + while( *ppItems ) + aReq.AppendItem( **ppItems++ ); + + // cache binds to an external dispatch provider + sal_Int16 eRet = pCache->Dispatch( aReq.GetArgs(), nCallMode == SfxCallMode::SYNCHRON ); + std::unique_ptr<SfxPoolItem> pPool; + if ( eRet == css::frame::DispatchResultState::DONTKNOW ) + pPool.reset( new SfxVoidItem( nId ) ); + else + pPool.reset( new SfxBoolItem( nId, eRet == css::frame::DispatchResultState::SUCCESS) ); + + auto pTemp = pPool.get(); + DeleteItemOnIdle( std::move(pPool) ); + return pTemp; + } + + // slot is handled internally by SfxDispatcher + if ( pImpl->bMsgDirty ) + UpdateSlotServer_Impl(); + + SfxShell *pShell=nullptr; + const SfxSlot *pSlot=nullptr; + + const SfxSlotServer* pServer = pCache->GetSlotServer( rDispatcher, pImpl->xProv ); + if ( !pServer ) + { + return nullptr; + } + else + { + pShell = rDispatcher.GetShell( pServer->GetShellLevel() ); + pSlot = pServer->GetSlot(); + } + + if ( bGlobalOnly ) + if ( dynamic_cast< const SfxModule *>( pShell ) == nullptr && dynamic_cast< const SfxApplication *>( pShell ) == nullptr && dynamic_cast< const SfxViewFrame *>( pShell ) == nullptr ) + return nullptr; + + SfxItemPool &rPool = pShell->GetPool(); + SfxRequest aReq( nId, nCallMode, rPool ); + aReq.SetModifier( nModi ); + if( ppItems ) + while( *ppItems ) + aReq.AppendItem( **ppItems++ ); + if ( ppInternalArgs ) + { + SfxAllItemSet aSet( rPool ); + for ( const SfxPoolItem **pArg = ppInternalArgs; *pArg; ++pArg ) + aSet.Put( **pArg ); + aReq.SetInternalArgs_Impl( aSet ); + } + + Execute_Impl( aReq, pSlot, pShell ); + + const SfxPoolItem* pRet = aReq.GetReturnValue(); + if ( !pRet ) + { + std::unique_ptr<SfxPoolItem> pVoid(new SfxVoidItem( nId )); + pRet = pVoid.get(); + DeleteItemOnIdle( std::move(pVoid) ); + } + + return pRet; +} + +void SfxBindings::Execute_Impl( SfxRequest& aReq, const SfxSlot* pSlot, SfxShell* pShell ) +{ + SfxItemPool &rPool = pShell->GetPool(); + + if ( SfxSlotKind::Attribute == pSlot->GetKind() ) + { + // Which value has to be mapped for Attribute slots + const sal_uInt16 nSlotId = pSlot->GetSlotId(); + aReq.SetSlot( nSlotId ); + if ( pSlot->IsMode(SfxSlotMode::TOGGLE) ) + { + // The value is attached to a toggleable attribute (Bools) + sal_uInt16 nWhich = pSlot->GetWhich(rPool); + SfxItemSet aSet(rPool, {{nWhich, nWhich}}); + SfxStateFunc aFunc = pSlot->GetStateFnc(); + pShell->CallState( aFunc, aSet ); + const SfxPoolItem *pOldItem; + SfxItemState eState = aSet.GetItemState(nWhich, true, &pOldItem); + if ( eState == SfxItemState::DISABLED ) + return; + + if ( SfxItemState::DEFAULT == eState && SfxItemPool::IsWhich(nWhich) ) + pOldItem = &aSet.Get(nWhich); + + if ( SfxItemState::SET == eState || + ( SfxItemState::DEFAULT == eState && + SfxItemPool::IsWhich(nWhich) && + pOldItem ) ) + { + if ( auto pOldBoolItem = dynamic_cast< const SfxBoolItem *>( pOldItem ) ) + { + // we can toggle Bools + bool bOldValue = pOldBoolItem->GetValue(); + std::unique_ptr<SfxBoolItem> pNewItem(static_cast<SfxBoolItem*>(pOldItem->Clone())); + pNewItem->SetValue( !bOldValue ); + aReq.AppendItem( *pNewItem ); + } + else if ( dynamic_cast< const SfxEnumItemInterface *>( pOldItem ) != nullptr && + static_cast<const SfxEnumItemInterface *>(pOldItem)->HasBoolValue()) + { + // and Enums with Bool-Interface + std::unique_ptr<SfxEnumItemInterface> pNewItem( + static_cast<SfxEnumItemInterface*>(pOldItem->Clone())); + pNewItem->SetBoolValue(!static_cast<const SfxEnumItemInterface *>(pOldItem)->GetBoolValue()); + aReq.AppendItem( *pNewItem ); + } + else { + OSL_FAIL( "Toggle only for Enums and Bools allowed" ); + } + } + else if ( SfxItemState::DONTCARE == eState ) + { + // Create one Status-Item for each Factory + std::unique_ptr<SfxPoolItem> pNewItem = pSlot->GetType()->CreateItem(); + DBG_ASSERT( pNewItem, "Toggle to slot without ItemFactory" ); + pNewItem->SetWhich( nWhich ); + + if ( auto pNewBoolItem = dynamic_cast<SfxBoolItem *>( pNewItem.get() ) ) + { + // we can toggle Bools + pNewBoolItem->SetValue( true ); + aReq.AppendItem( *pNewItem ); + } + else if ( dynamic_cast< const SfxEnumItemInterface *>( pNewItem.get() ) != nullptr && + static_cast<SfxEnumItemInterface *>(pNewItem.get())->HasBoolValue()) + { + // and Enums with Bool-Interface + static_cast<SfxEnumItemInterface*>(pNewItem.get())->SetBoolValue(true); + aReq.AppendItem( *pNewItem ); + } + else { + OSL_FAIL( "Toggle only for Enums and Bools allowed" ); + } + } + else { + OSL_FAIL( "suspicious Toggle-Slot" ); + } + } + + pDispatcher->Execute_( *pShell, *pSlot, aReq, aReq.GetCallMode() | SfxCallMode::RECORD ); + } + else + pDispatcher->Execute_( *pShell, *pSlot, aReq, aReq.GetCallMode() | SfxCallMode::RECORD ); +} + + +void SfxBindings::UpdateSlotServer_Impl() +{ + // synchronize + pDispatcher->Flush(); + + if ( pImpl->bAllMsgDirty ) + { + if ( !nRegLevel ) + { + pImpl->bContextChanged = false; + } + else + pImpl->bContextChanged = true; + } + + for (std::unique_ptr<SfxStateCache>& pCache : pImpl->pCaches) + { + //GetSlotServer can modify pImpl->pCaches + pCache->GetSlotServer(*pDispatcher, pImpl->xProv); + } + pImpl->bMsgDirty = pImpl->bAllMsgDirty = false; + + Broadcast( SfxHint(SfxHintId::DocChanged) ); +} + + +std::unique_ptr<SfxItemSet> SfxBindings::CreateSet_Impl +( + SfxStateCache& rCache, // in: Status-Cache from nId + const SfxSlot*& pRealSlot, // out: RealSlot to nId + const SfxSlotServer** pMsgServer, // out: Slot-Server to nId + SfxFoundCacheArr_Impl& rFound // out: List of Caches for Siblings +) +{ + DBG_ASSERT( !pImpl->bMsgDirty, "CreateSet_Impl with dirty MessageServer" ); + assert(pDispatcher); + + const SfxSlotServer* pMsgSvr = rCache.GetSlotServer(*pDispatcher, pImpl->xProv); + if (!pMsgSvr) + return nullptr; + + pRealSlot = nullptr; + *pMsgServer = pMsgSvr; + + sal_uInt16 nShellLevel = pMsgSvr->GetShellLevel(); + SfxShell *pShell = pDispatcher->GetShell( nShellLevel ); + if ( !pShell ) // rare GPF when browsing through update from Inet-Notify + return nullptr; + + SfxItemPool &rPool = pShell->GetPool(); + + // get the status method, which is served by the rCache + SfxStateFunc pFnc = nullptr; + pRealSlot = pMsgSvr->GetSlot(); + + pFnc = pRealSlot->GetStateFnc(); + + // the RealSlot is always on + SfxFoundCache_Impl *pFound = new SfxFoundCache_Impl( + pRealSlot->GetWhich(rPool), pRealSlot, rCache); + rFound.push_back( pFound ); + + // Search through the bindings for slots served by the same function. This , // will only affect slots which are present in the found interface. + + // The position of the Statecaches in StateCache-Array + std::size_t nCachePos = pImpl->nMsgPos; + const SfxSlot *pSibling = pRealSlot->GetNextSlot(); + + // the Slots ODF and interfaces are linked in a circle + while ( pSibling > pRealSlot ) + { + SfxStateFunc pSiblingFnc=nullptr; + SfxStateCache *pSiblingCache = + GetStateCache( pSibling->GetSlotId(), &nCachePos ); + + // Is the slot cached ? + if ( pSiblingCache ) + { + const SfxSlotServer *pServ = pSiblingCache->GetSlotServer(*pDispatcher, pImpl->xProv); + if ( pServ && pServ->GetShellLevel() == nShellLevel ) + pSiblingFnc = pServ->GetSlot()->GetStateFnc(); + } + + // Does the slot have to be updated at all? + bool bInsert = pSiblingCache && pSiblingCache->IsControllerDirty(); + + // It is not enough to ask for the same shell!! + bool bSameMethod = pSiblingCache && pFnc == pSiblingFnc; + + if ( bInsert && bSameMethod ) + { + SfxFoundCache_Impl *pFoundCache = new SfxFoundCache_Impl( + pSibling->GetWhich(rPool), + pSibling, *pSiblingCache); + + rFound.push_back( pFoundCache ); + } + + pSibling = pSibling->GetNextSlot(); + } + + // Create a Set from the ranges + std::unique_ptr<sal_uInt16[]> pRanges(new sal_uInt16[rFound.size() * 2 + 1]); + int j = 0; + size_t i = 0; + while ( i < rFound.size() ) + { + pRanges[j++] = rFound[i].nWhichId; + // consecutive numbers + for ( ; i < rFound.size()-1; ++i ) + if ( rFound[i].nWhichId+1 != rFound[i+1].nWhichId ) + break; + pRanges[j++] = rFound[i++].nWhichId; + } + pRanges[j] = 0; // terminating NULL + std::unique_ptr<SfxItemSet> pSet(new SfxItemSet(rPool, pRanges.get())); + pRanges.reset(); + return pSet; +} + + +void SfxBindings::UpdateControllers_Impl +( + const SfxFoundCache_Impl& rFound, // Cache, Slot, Which etc. + const SfxPoolItem* pItem, // item to send to controller + SfxItemState eState // state of item +) +{ + SfxStateCache& rCache = rFound.rCache; + const SfxSlot* pSlot = rFound.pSlot; + DBG_ASSERT( !pSlot || rCache.GetId() == pSlot->GetSlotId(), "SID mismatch" ); + + // bound until now, the Controller to update the Slot. + if (!rCache.IsControllerDirty()) + return; + + if ( SfxItemState::DONTCARE == eState ) + { + // ambiguous + rCache.SetState( SfxItemState::DONTCARE, INVALID_POOL_ITEM ); + } + else if ( SfxItemState::DEFAULT == eState && + SfxItemPool::IsSlot(rFound.nWhichId) ) + { + // no Status or Default but without Pool + SfxVoidItem aVoid(0); + rCache.SetState( SfxItemState::UNKNOWN, &aVoid ); + } + else if ( SfxItemState::DISABLED == eState ) + rCache.SetState(SfxItemState::DISABLED, nullptr); + else + rCache.SetState(SfxItemState::DEFAULT, pItem); +} + +IMPL_LINK( SfxBindings, NextJob, Timer *, pTimer, void ) +{ + NextJob_Impl(pTimer); +} + +bool SfxBindings::NextJob_Impl(Timer const * pTimer) +{ + const unsigned MAX_INPUT_DELAY = 200; + + if ( Application::GetLastInputInterval() < MAX_INPUT_DELAY && pTimer ) + { + pImpl->aAutoTimer.SetTimeout(TIMEOUT_UPDATING); + return true; + } + + SfxApplication *pSfxApp = SfxGetpApp(); + + if( pDispatcher ) + pDispatcher->Update_Impl(); + + // modifying the SfxObjectInterface-stack without SfxBindings => nothing to do + SfxViewFrame* pFrame = pDispatcher ? pDispatcher->GetFrame() : nullptr; + if ( (pFrame && !pFrame->GetObjectShell()->AcceptStateUpdate()) || pSfxApp->IsDowning() || pImpl->pCaches.empty() ) + { + return true; + } + if ( !pDispatcher || !pDispatcher->IsFlushed() ) + { + return true; + } + + // if possible Update all server / happens in its own time slice + if ( pImpl->bMsgDirty ) + { + UpdateSlotServer_Impl(); + return false; + } + + pImpl->bAllDirty = false; + pImpl->aAutoTimer.SetTimeout(TIMEOUT_UPDATING); + + // at least 10 loops and further if more jobs are available but no input + bool bPreEmptive = pTimer; + sal_uInt16 nLoops = 10; + pImpl->bInNextJob = true; + const std::size_t nCount = pImpl->pCaches.size(); + while ( pImpl->nMsgPos < nCount ) + { + // iterate through the bound functions + bool bJobDone = false; + while ( !bJobDone ) + { + SfxStateCache* pCache = pImpl->pCaches[pImpl->nMsgPos].get(); + DBG_ASSERT( pCache, "invalid SfxStateCache-position in job queue" ); + bool bWasDirty = pCache->IsControllerDirty(); + if ( bWasDirty ) + { + Update_Impl(*pCache); + DBG_ASSERT(nCount == pImpl->pCaches.size(), "Reschedule in StateChanged => buff"); + } + + // skip to next function binding + ++pImpl->nMsgPos; + + // keep job if it is not completed, but any input is available + bJobDone = pImpl->nMsgPos >= nCount; + if ( bJobDone && pImpl->bFirstRound ) + { + + // Update of the preferred shell has been done, now may + // also the others shells be updated + bJobDone = false; + pImpl->bFirstRound = false; + pImpl->nMsgPos = 0; + } + + if ( bWasDirty && !bJobDone && bPreEmptive && (--nLoops == 0) ) + { + pImpl->bInNextJob = false; + return false; + } + } + } + + pImpl->nMsgPos = 0; + + pImpl->aAutoTimer.Stop(); + + // Update round is finished + pImpl->bInNextJob = false; + Broadcast(SfxHint(SfxHintId::UpdateDone)); + return true; +} + + +sal_uInt16 SfxBindings::EnterRegistrations(const char *pFile, int nLine) +{ + SAL_INFO( + "sfx.control", + std::setw(std::min(nRegLevel, sal_uInt16(8))) << ' ' << "this = " << this + << " Level = " << nRegLevel << " SfxBindings::EnterRegistrations " + << (pFile + ? SAL_STREAM("File: " << pFile << " Line: " << nLine) : "")); + + // When bindings are locked, also lock sub bindings. + if ( pImpl->pSubBindings ) + { + pImpl->pSubBindings->ENTERREGISTRATIONS(); + + // These EnterRegistrations are not "real" for the SubBindings + pImpl->pSubBindings->pImpl->nOwnRegLevel--; + + // Synchronize Bindings + pImpl->pSubBindings->nRegLevel = nRegLevel + pImpl->pSubBindings->pImpl->nOwnRegLevel + 1; + } + + pImpl->nOwnRegLevel++; + + // check if this is the outer most level + if ( ++nRegLevel == 1 ) + { + // stop background-processing + pImpl->aAutoTimer.Stop(); + + // flush the cache + pImpl->nCachedFunc1 = 0; + pImpl->nCachedFunc2 = 0; + + // Mark if the all of the Caches have disappeared. + pImpl->bCtrlReleased = false; + } + + return nRegLevel; +} + + +void SfxBindings::LeaveRegistrations( const char *pFile, int nLine ) +{ + DBG_ASSERT( nRegLevel, "Leave without Enter" ); + + // Only when the SubBindings are still locked by the Superbindings, + // remove this lock (i.e. if there are more locks than "real" ones) + if ( pImpl->pSubBindings && pImpl->pSubBindings->nRegLevel > pImpl->pSubBindings->pImpl->nOwnRegLevel ) + { + // Synchronize Bindings + pImpl->pSubBindings->nRegLevel = nRegLevel + pImpl->pSubBindings->pImpl->nOwnRegLevel; + + // This LeaveRegistrations is not "real" for SubBindings + pImpl->pSubBindings->pImpl->nOwnRegLevel++; + pImpl->pSubBindings->LEAVEREGISTRATIONS(); + } + + pImpl->nOwnRegLevel--; + + // check if this is the outer most level + if ( --nRegLevel == 0 && SfxGetpApp() && !SfxGetpApp()->IsDowning() ) + { + if ( pImpl->bContextChanged ) + { + pImpl->bContextChanged = false; + } + + SfxViewFrame* pFrame = pDispatcher->GetFrame(); + + // If possible remove unused Caches, for example prepare PlugInInfo + if ( pImpl->bCtrlReleased ) + { + for ( sal_uInt16 nCache = pImpl->pCaches.size(); nCache > 0; --nCache ) + { + // Get Cache via css::sdbcx::Index + SfxStateCache *pCache = pImpl->pCaches[nCache-1].get(); + + // No interested Controller present + if ( pCache->GetItemLink() == nullptr && !pCache->GetInternalController() ) + { + // Remove Cache. Safety: first remove and then delete + pImpl->pCaches.erase(pImpl->pCaches.begin() + nCache - 1); + } + } + } + + // restart background-processing + pImpl->nMsgPos = 0; + if ( !pFrame || !pFrame->GetObjectShell() ) + return; + if ( !pImpl->pCaches.empty() ) + { + pImpl->aAutoTimer.Stop(); + pImpl->aAutoTimer.SetTimeout(TIMEOUT_FIRST); + pImpl->aAutoTimer.Start(); + } + } + + SAL_INFO( + "sfx.control", + std::setw(std::min(nRegLevel, sal_uInt16(8))) << ' ' << "this = " << this + << " Level = " << nRegLevel << " SfxBindings::LeaveRegistrations " + << (pFile + ? SAL_STREAM("File: " << pFile << " Line: " << nLine) : "")); +} + + +void SfxBindings::SetDispatcher( SfxDispatcher *pDisp ) +{ + SfxDispatcher *pOldDispat = pDispatcher; + if ( pDisp == pDispatcher ) + return; + + if ( pOldDispat ) + { + SfxBindings* pBind = pOldDispat->GetBindings(); + while ( pBind ) + { + if ( pBind->pImpl->pSubBindings == this && pBind->pDispatcher != pDisp ) + pBind->SetSubBindings_Impl( nullptr ); + pBind = pBind->pImpl->pSubBindings; + } + } + + pDispatcher = pDisp; + + css::uno::Reference < css::frame::XDispatchProvider > xProv; + if ( pDisp ) + xProv.set( pDisp->GetFrame()->GetFrame().GetFrameInterface(), UNO_QUERY ); + + SetDispatchProvider_Impl( xProv ); + InvalidateAll( true ); + + if ( pDispatcher && !pOldDispat ) + { + if ( pImpl->pSubBindings && pImpl->pSubBindings->pDispatcher != pOldDispat ) + { + OSL_FAIL( "SubBindings already set before activating!" ); + pImpl->pSubBindings->ENTERREGISTRATIONS(); + } + LEAVEREGISTRATIONS(); + } + else if( !pDispatcher ) + { + ENTERREGISTRATIONS(); + if ( pImpl->pSubBindings && pImpl->pSubBindings->pDispatcher != pOldDispat ) + { + OSL_FAIL( "SubBindings still set even when deactivating!" ); + pImpl->pSubBindings->LEAVEREGISTRATIONS(); + } + } + + Broadcast( SfxHint( SfxHintId::DataChanged ) ); + + if ( !pDisp ) + return; + + SfxBindings* pBind = pDisp->GetBindings(); + while ( pBind && pBind != this ) + { + if ( !pBind->pImpl->pSubBindings ) + { + pBind->SetSubBindings_Impl( this ); + break; + } + + pBind = pBind->pImpl->pSubBindings; + } +} + + +void SfxBindings::ClearCache_Impl( sal_uInt16 nSlotId ) +{ + SfxStateCache* pCache = GetStateCache(nSlotId); + if (!pCache) + return; + pCache->ClearCache(); +} + + +void SfxBindings::StartUpdate_Impl( bool bComplete ) +{ + if ( pImpl->pSubBindings ) + pImpl->pSubBindings->StartUpdate_Impl( bComplete ); + + if ( !bComplete ) + // Update may be interrupted + NextJob_Impl(&pImpl->aAutoTimer); + else + // Update all slots in a row + NextJob_Impl(nullptr); +} + + +SfxItemState SfxBindings::QueryState( sal_uInt16 nSlot, std::unique_ptr<SfxPoolItem> &rpState ) +{ + css::uno::Reference< css::frame::XDispatch > xDisp; + SfxStateCache *pCache = GetStateCache( nSlot ); + if ( pCache ) + xDisp = pCache->GetDispatch(); + if ( xDisp.is() || !pCache ) + { + const SfxSlot* pSlot = SfxSlotPool::GetSlotPool( pDispatcher->GetFrame() ).GetSlot( nSlot ); + if ( !pSlot || !pSlot->pUnoName ) + return SfxItemState::DISABLED; + + css::util::URL aURL; + OUString aCmd( ".uno:" ); + aURL.Protocol = aCmd; + aURL.Path = OUString::createFromAscii(pSlot->GetUnoName()); + aCmd += aURL.Path; + aURL.Complete = aCmd; + aURL.Main = aCmd; + + if ( !xDisp.is() ) + xDisp = pImpl->xProv->queryDispatch( aURL, OUString(), 0 ); + + if ( xDisp.is() ) + { + css::uno::Reference< css::lang::XUnoTunnel > xTunnel( xDisp, css::uno::UNO_QUERY ); + SfxOfficeDispatch* pDisp = nullptr; + if ( xTunnel.is() ) + { + sal_Int64 nImplementation = xTunnel->getSomething(SfxOfficeDispatch::impl_getStaticIdentifier()); + pDisp = reinterpret_cast< SfxOfficeDispatch* >( sal::static_int_cast< sal_IntPtr >( nImplementation )); + } + + if ( !pDisp ) + { + bool bDeleteCache = false; + if ( !pCache ) + { + pCache = new SfxStateCache( nSlot ); + pCache->GetSlotServer( *GetDispatcher_Impl(), pImpl->xProv ); + bDeleteCache = true; + } + + SfxItemState eState = SfxItemState::SET; + rtl::Reference<BindDispatch_Impl> xBind(new BindDispatch_Impl( xDisp, aURL, pCache, pSlot )); + xDisp->addStatusListener( xBind.get(), aURL ); + if ( !xBind->GetStatus().IsEnabled ) + { + eState = SfxItemState::DISABLED; + } + else + { + css::uno::Any aAny = xBind->GetStatus().State; + const css::uno::Type& aType = aAny.getValueType(); + + if ( aType == cppu::UnoType<bool>::get() ) + { + bool bTemp = false; + aAny >>= bTemp ; + rpState.reset(new SfxBoolItem( nSlot, bTemp )); + } + else if ( aType == ::cppu::UnoType< ::cppu::UnoUnsignedShortType >::get() ) + { + sal_uInt16 nTemp = 0; + aAny >>= nTemp ; + rpState.reset(new SfxUInt16Item( nSlot, nTemp )); + } + else if ( aType == cppu::UnoType<sal_uInt32>::get() ) + { + sal_uInt32 nTemp = 0; + aAny >>= nTemp ; + rpState.reset(new SfxUInt32Item( nSlot, nTemp )); + } + else if ( aType == cppu::UnoType<OUString>::get() ) + { + OUString sTemp ; + aAny >>= sTemp ; + rpState.reset(new SfxStringItem( nSlot, sTemp )); + } + else + rpState.reset(new SfxVoidItem( nSlot )); + } + + xDisp->removeStatusListener( xBind.get(), aURL ); + xBind->Release(); + xBind.clear(); + if ( bDeleteCache ) + DELETEZ( pCache ); + return eState; + } + } + } + + // Then test at the dispatcher to check if the returned items from + // there are always DELETE_ON_IDLE, a copy of it has to be made in + // order to allow for transition of ownership. + const SfxPoolItem *pItem = nullptr; + SfxItemState eState = pDispatcher->QueryState( nSlot, pItem ); + if ( eState == SfxItemState::SET ) + { + DBG_ASSERT( pItem, "SfxItemState::SET but no item!" ); + if ( pItem ) + rpState.reset(pItem->Clone()); + } + else if ( eState == SfxItemState::DEFAULT && pItem ) + { + rpState.reset(pItem->Clone()); + } + + return eState; +} + +void SfxBindings::QueryControlState( sal_uInt16 nSlot, boost::property_tree::ptree& rState ) +{ + if ( SfxGetpApp()->IsDowning() ) + return; + + if ( pDispatcher ) + pDispatcher->Flush(); + + if ( pImpl->pSubBindings ) + pImpl->pSubBindings->QueryControlState( nSlot, rState ); + + SfxStateCache* pCache = GetStateCache( nSlot ); + if ( pCache ) + { + if ( pImpl->bMsgDirty ) + { + UpdateSlotServer_Impl(); + pCache = GetStateCache( nSlot ); + } + + if (pCache && pCache->GetItemLink() ) + { + pCache->GetState(rState); + } + } +} + +void SfxBindings::SetSubBindings_Impl( SfxBindings *pSub ) +{ + if ( pImpl->pSubBindings ) + { + pImpl->pSubBindings->SetDispatchProvider_Impl( css::uno::Reference< css::frame::XDispatchProvider > () ); + } + + pImpl->pSubBindings = pSub; + + if ( pSub ) + { + pImpl->pSubBindings->SetDispatchProvider_Impl( pImpl->xProv ); + } +} + +SfxBindings* SfxBindings::GetSubBindings_Impl() const +{ + return pImpl->pSubBindings; +} + +void SfxBindings::SetWorkWindow_Impl( SfxWorkWindow* pWork ) +{ + pImpl->pWorkWin = pWork; +} + +SfxWorkWindow* SfxBindings::GetWorkWindow_Impl() const +{ + return pImpl->pWorkWin; +} + +bool SfxBindings::IsInUpdate() const +{ + bool bInUpdate = pImpl->bInUpdate; + if ( !bInUpdate && pImpl->pSubBindings ) + bInUpdate = pImpl->pSubBindings->IsInUpdate(); + return bInUpdate; +} + +void SfxBindings::SetVisibleState( sal_uInt16 nId, bool bShow ) +{ + SfxStateCache *pCache = GetStateCache( nId ); + if ( pCache ) + pCache->SetVisibleState( bShow ); +} + +void SfxBindings::SetActiveFrame( const css::uno::Reference< css::frame::XFrame > & rFrame ) +{ + if ( rFrame.is() || !pDispatcher ) + SetDispatchProvider_Impl( css::uno::Reference< css::frame::XDispatchProvider > ( rFrame, css::uno::UNO_QUERY ) ); + else + SetDispatchProvider_Impl( css::uno::Reference< css::frame::XDispatchProvider > ( + pDispatcher->GetFrame()->GetFrame().GetFrameInterface(), css::uno::UNO_QUERY ) ); +} + +css::uno::Reference< css::frame::XFrame > SfxBindings::GetActiveFrame() const +{ + const css::uno::Reference< css::frame::XFrame > xFrame( pImpl->xProv, css::uno::UNO_QUERY ); + if ( xFrame.is() || !pDispatcher ) + return xFrame; + else + return pDispatcher->GetFrame()->GetFrame().GetFrameInterface(); +} + +void SfxBindings::SetDispatchProvider_Impl( const css::uno::Reference< css::frame::XDispatchProvider > & rProv ) +{ + bool bInvalidate = ( rProv != pImpl->xProv ); + if ( bInvalidate ) + { + pImpl->xProv = rProv; + InvalidateAll( true ); + } + + if ( pImpl->pSubBindings ) + pImpl->pSubBindings->SetDispatchProvider_Impl( pImpl->xProv ); +} + +const css::uno::Reference< css::frame::XDispatchRecorder >& SfxBindings::GetRecorder() const +{ + return pImpl->xRecorder; +} + +void SfxBindings::SetRecorder_Impl( css::uno::Reference< css::frame::XDispatchRecorder > const & rRecorder ) +{ + pImpl->xRecorder = rRecorder; +} + +void SfxBindings::ContextChanged_Impl() +{ + if ( !pImpl->bInUpdate && ( !pImpl->bContextChanged || !pImpl->bAllMsgDirty ) ) + { + InvalidateAll( true ); + } +} + +uno::Reference < frame::XDispatch > SfxBindings::GetDispatch( const SfxSlot* pSlot, const util::URL& aURL, bool bMasterCommand ) +{ + uno::Reference < frame::XDispatch > xRet; + SfxStateCache* pCache = GetStateCache( pSlot->nSlotId ); + if ( pCache && !bMasterCommand ) + xRet = pCache->GetInternalDispatch(); + if ( !xRet.is() ) + { + // dispatches for slaves are unbound, they don't have a state + SfxOfficeDispatch* pDispatch = bMasterCommand ? + new SfxOfficeDispatch( pDispatcher, pSlot, aURL ) : + new SfxOfficeDispatch( *this, pDispatcher, pSlot, aURL ); + + pDispatch->SetMasterUnoCommand( bMasterCommand ); + xRet.set( pDispatch ); + if ( !pCache ) + pCache = GetStateCache( pSlot->nSlotId ); + + DBG_ASSERT( pCache, "No cache for OfficeDispatch!" ); + if ( pCache && !bMasterCommand ) + pCache->SetInternalDispatch( xRet ); + } + + return xRet; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/charmapcontrol.cxx b/sfx2/source/control/charmapcontrol.cxx new file mode 100644 index 000000000..69e1a197d --- /dev/null +++ b/sfx2/source/control/charmapcontrol.cxx @@ -0,0 +1,200 @@ +/* -*- 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 . + */ + +#include <comphelper/dispatchcommand.hxx> +#include <officecfg/Office/Common.hxx> +#include <charmapcontrol.hxx> +#include <sfx2/charmappopup.hxx> +#include <sfx2/viewfrm.hxx> + +using namespace css; + +SfxCharmapCtrl::SfxCharmapCtrl(CharmapPopup* pControl, weld::Widget* pParent) + : WeldToolbarPopup(pControl->getFrameInterface(), pParent, "sfx/ui/charmapcontrol.ui", "charmapctrl") + , m_xControl(pControl) + , m_xVirDev(VclPtr<VirtualDevice>::Create()) + , m_aRecentCharView{SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev)} + , m_aFavCharView{SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev), + SvxCharView(m_xVirDev)} + , m_xDlgBtn(m_xBuilder->weld_button("specialchardlg")) + , m_xRecentCharView{std::make_unique<weld::CustomWeld>(*m_xBuilder, "viewchar1", m_aRecentCharView[0]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "viewchar2", m_aRecentCharView[1]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "viewchar3", m_aRecentCharView[2]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "viewchar4", m_aRecentCharView[3]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "viewchar5", m_aRecentCharView[4]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "viewchar6", m_aRecentCharView[5]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "viewchar7", m_aRecentCharView[6]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "viewchar8", m_aRecentCharView[7]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "viewchar9", m_aRecentCharView[8]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "viewchar10", m_aRecentCharView[9]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "viewchar11", m_aRecentCharView[10]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "viewchar12", m_aRecentCharView[11]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "viewchar13", m_aRecentCharView[12]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "viewchar14", m_aRecentCharView[13]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "viewchar15", m_aRecentCharView[14]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "viewchar16", m_aRecentCharView[15])} + , m_xFavCharView{std::make_unique<weld::CustomWeld>(*m_xBuilder, "favchar1", m_aFavCharView[0]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "favchar2", m_aFavCharView[1]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "favchar3", m_aFavCharView[2]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "favchar4", m_aFavCharView[3]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "favchar5", m_aFavCharView[4]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "favchar6", m_aFavCharView[5]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "favchar7", m_aFavCharView[6]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "favchar8", m_aFavCharView[7]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "favchar9", m_aFavCharView[8]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "favchar10", m_aFavCharView[9]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "favchar11", m_aFavCharView[10]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "favchar12", m_aFavCharView[11]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "favchar13", m_aFavCharView[12]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "favchar14", m_aFavCharView[13]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "favchar15", m_aFavCharView[14]), + std::make_unique<weld::CustomWeld>(*m_xBuilder, "favchar16", m_aFavCharView[15])} +{ + for(int i = 0; i < 16; i++) + { + m_aRecentCharView[i].setMouseClickHdl(LINK(this,SfxCharmapCtrl, CharClickHdl)); + m_aFavCharView[i].setMouseClickHdl(LINK(this,SfxCharmapCtrl, CharClickHdl)); + } + + m_xDlgBtn->connect_clicked(LINK(this, SfxCharmapCtrl, OpenDlgHdl)); + + getRecentCharacterList(); + updateRecentCharControl(); + getFavCharacterList(); + updateFavCharControl(); +} + +SfxCharmapCtrl::~SfxCharmapCtrl() +{ +} + +void SfxCharmapCtrl::getFavCharacterList() +{ + //retrieve recent character list + css::uno::Sequence< OUString > rFavCharList( officecfg::Office::Common::FavoriteCharacters::FavoriteCharacterList::get() ); + std::copy(rFavCharList.begin(), rFavCharList.end(), std::back_inserter(m_aFavCharList)); + + //retrieve recent character font list + css::uno::Sequence< OUString > rFavCharFontList( officecfg::Office::Common::FavoriteCharacters::FavoriteCharacterFontList::get() ); + std::copy(rFavCharFontList.begin(), rFavCharFontList.end(), std::back_inserter(m_aFavCharFontList)); +} + +void SfxCharmapCtrl::updateFavCharControl() +{ + int i = 0; + for ( std::deque< OUString >::iterator it = m_aFavCharList.begin(), it2 = m_aFavCharFontList.begin(); + it != m_aFavCharList.end() || it2 != m_aFavCharFontList.end(); + ++it, ++it2, i++) + { + m_aFavCharView[i].SetText(*it); + vcl::Font rFont = m_aFavCharView[i].GetFont(); + rFont.SetFamilyName( *it2 ); + m_aFavCharView[i].SetFont(rFont); + m_aFavCharView[i].Show(); + } + + for(; i < 16 ; i++) + { + m_aFavCharView[i].SetText(OUString()); + m_aFavCharView[i].Hide(); + } +} + +void SfxCharmapCtrl::getRecentCharacterList() +{ + //retrieve recent character list + css::uno::Sequence< OUString > rRecentCharList( officecfg::Office::Common::RecentCharacters::RecentCharacterList::get() ); + std::copy(rRecentCharList.begin(), rRecentCharList.end(), std::back_inserter(m_aRecentCharList)); + + //retrieve recent character font list + css::uno::Sequence< OUString > rRecentCharFontList( officecfg::Office::Common::RecentCharacters::RecentCharacterFontList::get() ); + std::copy(rRecentCharFontList.begin(), rRecentCharFontList.end(), std::back_inserter(m_aRecentCharFontList)); +} + +void SfxCharmapCtrl::updateRecentCharControl() +{ + int i = 0; + for ( std::deque< OUString >::iterator it = m_aRecentCharList.begin(), it2 = m_aRecentCharFontList.begin(); + it != m_aRecentCharList.end() || it2 != m_aRecentCharFontList.end(); + ++it, ++it2, i++) + { + m_aRecentCharView[i].SetText(*it); + vcl::Font rFont = m_aRecentCharView[i].GetFont(); + rFont.SetFamilyName( *it2 ); + m_aRecentCharView[i].SetFont(rFont); + m_aRecentCharView[i].Show(); + } + + for(; i < 16 ; i++) + { + m_aRecentCharView[i].SetText(OUString()); + m_aRecentCharView[i].Hide(); + } +} + +IMPL_LINK(SfxCharmapCtrl, CharClickHdl, SvxCharView*, pView, void) +{ + m_xControl->EndPopupMode(); + + pView->InsertCharToDoc(); +} + +IMPL_LINK_NOARG(SfxCharmapCtrl, OpenDlgHdl, weld::Button&, void) +{ + m_xControl->EndPopupMode(); + + uno::Reference<frame::XFrame> xFrame = SfxViewFrame::Current()->GetFrame().GetFrameInterface(); + comphelper::dispatchCommand(".uno:InsertSymbol", xFrame, {}); +} + +void SfxCharmapCtrl::GrabFocus() +{ + m_aFavCharView[0].GrabFocus(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/charwin.cxx b/sfx2/source/control/charwin.cxx new file mode 100644 index 000000000..ac9ea6fb2 --- /dev/null +++ b/sfx2/source/control/charwin.cxx @@ -0,0 +1,270 @@ +/* -*- 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 . + */ + +#include <vcl/settings.hxx> +#include <vcl/virdev.hxx> +#include <vcl/event.hxx> +#include <vcl/svapp.hxx> +#include <sfx2/charwin.hxx> +#include <comphelper/dispatchcommand.hxx> +#include <comphelper/processfactory.hxx> + +#include <com/sun/star/beans/PropertyValue.hpp> + +using namespace com::sun::star; + +SvxCharView::SvxCharView(const VclPtr<VirtualDevice>& rVirDev) + : mxVirDev(rVirDev) + , mnY(0) + , maPosition(0,0) + , maHasInsert(true) +{ +} + +void SvxCharView::SetDrawingArea(weld::DrawingArea* pDrawingArea) +{ + CustomWidgetController::SetDrawingArea(pDrawingArea); + const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings(); + vcl::Font aFont = rStyleSettings.GetLabelFont(); + const Size aFontSize = aFont.GetFontSize(); + aFont.SetFontSize(Size(aFontSize.Width() * 2.5, aFontSize.Height() * 2.5)); + mxVirDev->Push(PUSH_ALLFONT); + mxVirDev->SetFont(aFont); + pDrawingArea->set_size_request(mxVirDev->approximate_digit_width() * 2, + mxVirDev->GetTextHeight()); + mxVirDev->Pop(); +} + +void SvxCharView::GetFocus() +{ + Invalidate(); +} + +void SvxCharView::LoseFocus() +{ + Invalidate(); +} + +bool SvxCharView::MouseButtonDown(const MouseEvent& rMEvt) +{ + if ( rMEvt.IsLeft() ) + { + if ( !(rMEvt.GetClicks() % 2) && maHasInsert ) + { + InsertCharToDoc(); + } + + maMouseClickHdl.Call(this); + } + + if (rMEvt.IsRight()) + { + Point aPosition(rMEvt.GetPosPixel()); + maPosition = aPosition; + GrabFocus(); + Invalidate(); + createContextMenu(); + } + + return true; +} + +bool SvxCharView::KeyInput(const KeyEvent& rKEvt) +{ + bool bRet = false; + vcl::KeyCode aCode = rKEvt.GetKeyCode(); + switch (aCode.GetCode()) + { + case KEY_SPACE: + case KEY_RETURN: + InsertCharToDoc(); + bRet = true; + break; + } + return bRet; +} + +void SvxCharView::InsertCharToDoc() +{ + if (GetText().isEmpty()) + return; + + uno::Sequence<beans::PropertyValue> aArgs(2); + aArgs[0].Name = "Symbols"; + aArgs[0].Value <<= GetText(); + + aArgs[1].Name = "FontName"; + aArgs[1].Value <<= maFont.GetFamilyName(); + + comphelper::dispatchCommand(".uno:InsertSymbol", aArgs); +} + +void SvxCharView::createContextMenu() +{ + weld::DrawingArea* pDrawingArea = GetDrawingArea(); + std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(pDrawingArea, "sfx/ui/charviewmenu.ui")); + std::unique_ptr<weld::Menu> xItemMenu(xBuilder->weld_menu("charviewmenu")); + ContextMenuSelect(xItemMenu->popup_at_rect(pDrawingArea, tools::Rectangle(maPosition, Size(1,1)))); + Invalidate(); +} + +void SvxCharView::ContextMenuSelect(const OString& rMenuId) +{ + if (rMenuId == "clearchar") + maClearClickHdl.Call(this); + else if (rMenuId == "clearallchar") + maClearAllClickHdl.Call(this); +} + +void SvxCharView::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle&) +{ + rRenderContext.SetFont(maFont); + + const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings(); + const Color aWindowTextColor(rStyleSettings.GetFieldTextColor()); + Color aHighlightColor(rStyleSettings.GetHighlightColor()); + Color aHighlightTextColor(rStyleSettings.GetHighlightTextColor()); + Color aFillColor(rStyleSettings.GetWindowColor()); + Color aTextColor(rStyleSettings.GetWindowTextColor()); + + const OUString aText = GetText(); + + Size aSize(GetOutputSizePixel()); + long nAvailWidth = aSize.Width(); + long nWinHeight = aSize.Height(); + + bool bGotBoundary = true; + bool bShrankFont = false; + vcl::Font aOrigFont(rRenderContext.GetFont()); + Size aFontSize(aOrigFont.GetFontSize()); + ::tools::Rectangle aBoundRect; + + for (long nFontHeight = aFontSize.Height(); nFontHeight > 0; nFontHeight -= 1) + { + if (!rRenderContext.GetTextBoundRect( aBoundRect, aText ) || aBoundRect.IsEmpty()) + { + bGotBoundary = false; + break; + } + + //only shrink in the single glyph large view mode + long nTextWidth = aBoundRect.GetWidth(); + if (nAvailWidth > nTextWidth) + break; + vcl::Font aFont(aOrigFont); + aFontSize.setHeight( nFontHeight ); + aFont.SetFontSize(aFontSize); + rRenderContext.SetFont(aFont); + mnY = (nWinHeight - rRenderContext.GetTextHeight()) / 2; + bShrankFont = true; + } + + Point aPoint(2, mnY); + + if (!bGotBoundary) + aPoint.setX( (aSize.Width() - rRenderContext.GetTextWidth(aText)) / 2 ); + else + { + // adjust position + aBoundRect += aPoint; + + // vertical adjustment + int nYLDelta = aBoundRect.Top(); + int nYHDelta = aSize.Height() - aBoundRect.Bottom(); + if( nYLDelta <= 0 ) + aPoint.AdjustY( -(nYLDelta - 1) ); + else if( nYHDelta <= 0 ) + aPoint.AdjustY(nYHDelta - 1 ); + + // centrally align glyph + aPoint.setX( -aBoundRect.Left() + (aSize.Width() - aBoundRect.GetWidth()) / 2 ); + } + + if (HasFocus()) + { + rRenderContext.SetFillColor(aHighlightColor); + rRenderContext.DrawRect(tools::Rectangle(Point(0, 0), aSize)); + + rRenderContext.SetTextColor(aHighlightTextColor); + rRenderContext.DrawText(aPoint, aText); + } + else + { + rRenderContext.SetFillColor(aFillColor); + rRenderContext.DrawRect(tools::Rectangle(Point(0, 0), aSize)); + + rRenderContext.SetTextColor(aWindowTextColor); + rRenderContext.DrawText(aPoint, aText); + } + rRenderContext.SetFillColor(aFillColor); + rRenderContext.SetTextColor(aTextColor); + + if (bShrankFont) + rRenderContext.SetFont(aOrigFont); +} + +void SvxCharView::setMouseClickHdl(const Link<SvxCharView*,void> &rLink) +{ + maMouseClickHdl = rLink; +} + +void SvxCharView::setClearClickHdl(const Link<SvxCharView*,void> &rLink) +{ + maClearClickHdl = rLink; +} + +void SvxCharView::setClearAllClickHdl(const Link<SvxCharView*,void> &rLink) +{ + maClearAllClickHdl = rLink; +} + +void SvxCharView::SetFont( const vcl::Font& rFont ) +{ + long nWinHeight = GetOutputSizePixel().Height(); + maFont = rFont; + maFont.SetWeight(WEIGHT_NORMAL); + maFont.SetAlignment(ALIGN_TOP); + maFont.SetFontSize(mxVirDev->PixelToLogic(Size(0, nWinHeight / 2))); + maFont.SetTransparent(true); + + mxVirDev->Push(PUSH_ALLFONT); + mxVirDev->SetFont(maFont); + mnY = (nWinHeight - mxVirDev->GetTextHeight()) / 2; + mxVirDev->Pop(); + + Invalidate(); +} + +void SvxCharView::Resize() +{ + SetFont(GetFont()); //force recalculation of size +} + +void SvxCharView::SetText( const OUString& rText ) +{ + m_sText = rText; + Invalidate(); +} + +void SvxCharView::SetHasInsert( bool bInsert ) +{ + maHasInsert = bInsert; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/sfx2/source/control/ctrlfactoryimpl.cxx b/sfx2/source/control/ctrlfactoryimpl.cxx new file mode 100644 index 000000000..925ba7b75 --- /dev/null +++ b/sfx2/source/control/ctrlfactoryimpl.cxx @@ -0,0 +1,62 @@ +/* -*- 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 . + */ + +#include <ctrlfactoryimpl.hxx> + +const SfxStbCtrlFactory& SfxStbCtrlFactArr_Impl::operator []( size_t i ) const +{ + return maData[i]; +} + +SfxStbCtrlFactory& SfxStbCtrlFactArr_Impl::operator []( size_t i ) +{ + return maData[i]; +} + +void SfxStbCtrlFactArr_Impl::push_back( const SfxStbCtrlFactory& p ) +{ + maData.push_back(p); +} + +size_t SfxStbCtrlFactArr_Impl::size() const +{ + return maData.size(); +} + +const SfxTbxCtrlFactory& SfxTbxCtrlFactArr_Impl::operator []( size_t i ) const +{ + return maData[i]; +} + +SfxTbxCtrlFactory& SfxTbxCtrlFactArr_Impl::operator []( size_t i ) +{ + return maData[i]; +} + +void SfxTbxCtrlFactArr_Impl::push_back( const SfxTbxCtrlFactory& p ) +{ + maData.push_back(p); +} + +size_t SfxTbxCtrlFactArr_Impl::size() const +{ + return maData.size(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/ctrlitem.cxx b/sfx2/source/control/ctrlitem.cxx new file mode 100644 index 000000000..1b039e44a --- /dev/null +++ b/sfx2/source/control/ctrlitem.cxx @@ -0,0 +1,351 @@ +/* -*- 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 . + */ + +#include <sal/log.hxx> +#include <svl/itempool.hxx> + +#include <sfx2/ctrlitem.hxx> +#include <sfx2/bindings.hxx> +#include <sfx2/dispatch.hxx> +#include <statcach.hxx> +#include <sfx2/viewfrm.hxx> + +// returns the next registered SfxControllerItem with the same id + +SfxControllerItem* SfxControllerItem::GetItemLink() +{ + return pNext == this ? nullptr : pNext; +} + + +// returns sal_True if this binding is really bound to a function + +bool SfxControllerItem::IsBound() const +{ + return pNext != this; +} + + +// registers with the id at the bindings + +void SfxControllerItem::Bind( sal_uInt16 nNewId, SfxBindings *pBindinx ) +{ + DBG_ASSERT(pBindings || pBindinx, "No Bindings"); + + if ( IsBound() ) { + DBG_ASSERT(pBindings, "No Bindings"); + pBindings->Release(*this); + } + + nId = nNewId; + pNext = nullptr; + + if (pBindinx) + pBindings = pBindinx; + pBindings->Register(*this); +} + +void SfxControllerItem::BindInternal_Impl( sal_uInt16 nNewId, SfxBindings *pBindinx ) +{ + DBG_ASSERT(pBindings || pBindinx, "No Bindings"); + + if ( IsBound() ) { + DBG_ASSERT(pBindings, "No Bindings"); + pBindings->Release(*this); + } + + nId = nNewId; + pNext = nullptr; + + if (pBindinx) + pBindings = pBindinx; + pBindings->RegisterInternal_Impl(*this); +} + + +void SfxControllerItem::UnBind() + +/* [Description] + + Unbinds the connection of this SfxControllerItems with the SfxBindings + instance with which it to time is bound. From this time on it does not + receive any status notifications (<SfxControllerItem::StateChented()>) + anymore. + + [Cross-reference] + + <SfxControllerItem::ReBind()> + <SfxControllerItem::ClearCache()> +*/ +{ + DBG_ASSERT(pBindings, "No Bindings"); + DBG_ASSERT( IsBound(), "unbindings unbound SfxControllerItem" ); + + pBindings->Release(*this); + pNext = this; +} + + +void SfxControllerItem::ReBind() + +/* [Description] + + Binds this SfxControllerItem with the SfxBindings instance again, + with which it was last bound. From this time on it does receive status + notifications (<SfxControllerItem::StateChented()>) again. + + [Cross-reference] + + <SfxControllerItem::UnBind()> + <SfxControllerItem::ClearCache()> +*/ + +{ + DBG_ASSERT(pBindings, "No Bindings"); + DBG_ASSERT( !IsBound(), "bindings rebound SfxControllerItem" ); + + pBindings->Register(*this); +} + + +void SfxControllerItem::ClearCache() + +/* [Description] + + Clears the cache status for this SfxControllerItem. That is by the next + status update is the <SfxPoolItem> sent in any case, even if the same was + sent before. This is needed if a controller can be switched on and note + that status themselves. + + [Example] + + The combined controller for adjusting the surface type and the concrete + expression (blue color, or hatching X) can be changed in type, but is then + notified of the next selection again, even if it the same data. + + [Cross-reference] + + <SfxControllerItem::UnBind()> + <SfxControllerItem::ReBind()> +*/ + + +{ + DBG_ASSERT(pBindings, "No Bindings"); + + pBindings->ClearCache_Impl( GetId() ); +} + + +// replaces the successor in the list of bindings of the same id + +SfxControllerItem* SfxControllerItem::ChangeItemLink( SfxControllerItem* pNewLink ) +{ + SfxControllerItem* pOldLink = pNext; + pNext = pNewLink; + return pOldLink == this ? nullptr : pOldLink; +} + + +// changes the id of unbound functions (e.g. for sub-menu-ids) + +void SfxControllerItem::SetId( sal_uInt16 nItemId ) +{ + DBG_ASSERT( !IsBound(), "changing id of bound binding" ); + nId = nItemId; +} + + +// creates an atomic item for a controller without registration. + +SfxControllerItem::SfxControllerItem(): + nId(0), + pNext(this), + pBindings(nullptr) +{ +} + + +// creates a representation of the function nId and registers it + +SfxControllerItem::SfxControllerItem( sal_uInt16 nID, SfxBindings &rBindings ): + nId(nID), + pNext(this), + pBindings(&rBindings) +{ + Bind(nId, &rBindings); +} + + +// unregisters the item in the bindings + +SfxControllerItem::~SfxControllerItem() +{ + dispose(); +} + +void SfxControllerItem::dispose() +{ + if ( IsBound() ) + UnBind(); +} + +void SfxControllerItem::StateChanged +( + sal_uInt16, // <SID> of the triggering slot + SfxItemState, // <SfxItemState> of 'pState' + const SfxPoolItem* // Slot-Status, NULL or IsInvalidItem() +) + +/* [Description] + + This virtual method is called by the SFx to inform the <SfxControllerItem>s + is about that state of the slots 'NSID' has changed. The new value and the + value determined by this status is given as 'pState' or 'eState'. + + The status of a slot may change, for example when the MDI window is + switched or when the slot was invalidated explicitly with + <SfxBindings::Invalidate()>. + + Beware! The method is not called when the slot is invalid, however + has again assumed the same value. + + This base class need not be called, further interim steps however + (eg <SfxToolboxControl> ) should be called. +*/ + +{ +} + +void SfxControllerItem::GetControlState +( + sal_uInt16, + boost::property_tree::ptree& +) +{ +} + +void SfxStatusForwarder::StateChanged +( + sal_uInt16 nSID, // <SID> of the triggering slot + SfxItemState eState, // <SfxItemState> of 'pState' + const SfxPoolItem* pState // Slot-Status, NULL or IsInvalidItem() +) + +{ + pMaster->StateChanged( nSID, eState, pState ); +} + + +SfxStatusForwarder::SfxStatusForwarder( + sal_uInt16 nSlotId, + SfxControllerItem& rMaster ): + SfxControllerItem( nSlotId, rMaster.GetBindings() ), + pMaster( &rMaster ) +{ +} + + +SfxItemState SfxControllerItem::GetItemState +( + const SfxPoolItem* pState /* Pointer to <SfxPoolItem>, which + Status should be queried. */ +) + +/* [Description] + + Static method to determine the status of the SfxPoolItem-Pointers, to be + used in the method <SfxControllerItem::StateChanged(const SfxPoolItem*)> + + [Return value] + + SfxItemState SfxItemState::UNKNOWN + Enabled, but no further status information available. + Typical for <Slot>s, which anyway are sometimes + disabled, but otherwise do not change their appearance. + + SfxItemState::DISABLED + Disabled and no further status information available. + All other values that may appear should be reset to + default. + + SfxItemState::DONTCARE + Enabled but there were only ambiguous values available + (i.e. non that can be queried). + + SfxItemState::DEFAULT + Enabled and with available values, which are queried + by 'pState'. The Type is thus clearly defined in the + entire Program and specified through the Slot. +*/ + +{ + return !pState + ? SfxItemState::DISABLED + : IsInvalidItem(pState) + ? SfxItemState::DONTCARE + : pState->IsVoidItem() && !pState->Which() + ? SfxItemState::UNKNOWN + : SfxItemState::DEFAULT; +} + + +MapUnit SfxControllerItem::GetCoreMetric() const + +/* [Description] + + Gets the measurement unit from the competent pool, in which the Status + item exist. +*/ + +{ + SfxStateCache *pCache = pBindings->GetStateCache( nId ); + SfxDispatcher *pDispat = pBindings->GetDispatcher_Impl(); + + if ( !pDispat ) + { + SfxViewFrame* pViewFrame = SfxViewFrame::Current(); + if ( !pViewFrame ) + SfxViewFrame::GetFirst(); + if ( pViewFrame ) + pDispat = pViewFrame->GetDispatcher(); + } + + if ( pDispat && pCache ) + { + const SfxSlotServer *pServer = pCache->GetSlotServer( *pDispat ); + if ( pServer ) + { + SfxShell *pSh = pDispat->GetShell( pServer->GetShellLevel() ); + SfxItemPool &rPool = pSh->GetPool(); + sal_uInt16 nWhich = rPool.GetWhich( nId ); + + // invalidate slot and its message|slot server as 'global' information + // about the validated message|slot server is not made available + pCache->Invalidate( true ); + + return rPool.GetMetric( nWhich ); + } + } + + SAL_INFO( "sfx.control", "W1: Can not find ItemPool!" ); + return MapUnit::Map100thMM; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/dispatch.cxx b/sfx2/source/control/dispatch.cxx new file mode 100644 index 000000000..48874f59a --- /dev/null +++ b/sfx2/source/control/dispatch.cxx @@ -0,0 +1,2064 @@ +/* -*- 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 . + */ + +#include <config_feature_desktop.h> + +#include <algorithm> +#include <cstddef> +#include <deque> +#include <vector> + +#include <stdlib.h> + +#include <boost/property_tree/json_parser.hpp> + +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/frame/XDispatchRecorderSupplier.hpp> +#include <com/sun/star/frame/XLayoutManager.hpp> +#include <com/sun/star/frame/XPopupMenuController.hpp> +#include <com/sun/star/uno/XComponentContext.hpp> +#include <com/sun/star/ui/ContextMenuExecuteEvent.hpp> + +#include <LibreOfficeKit/LibreOfficeKitEnums.h> +#include <comphelper/lok.hxx> +#include <comphelper/processfactory.hxx> +#include <comphelper/propertyvalue.hxx> +#include <rtl/strbuf.hxx> +#include <sal/log.hxx> +#include <sfx2/app.hxx> +#include <sfx2/bindings.hxx> +#include <sfx2/childwin.hxx> +#include <sfx2/dispatch.hxx> +#include <sfx2/docfile.hxx> +#include <hintpost.hxx> +#include <sfx2/ipclient.hxx> +#include <sfx2/module.hxx> +#include <sfx2/msg.hxx> +#include <sfx2/msgpool.hxx> +#include <sfx2/objface.hxx> +#include <sfx2/request.hxx> +#include <sfx2/sfxsids.hrc> +#include <sfx2/viewfrm.hxx> +#include <sfx2/viewsh.hxx> +#include <svl/eitem.hxx> +#include <svl/itemiter.hxx> +#include <svl/itempool.hxx> +#include <toolkit/awt/vclxmenu.hxx> +#include <toolkit/helper/vclunohelper.hxx> +#include <tools/debug.hxx> +#include <vcl/idle.hxx> + +#include <sfxtypes.hxx> +#include <slotserv.hxx> +#include <workwin.hxx> + +typedef std::vector<SfxShell*> SfxShellStack_Impl; + +namespace { + +struct SfxToDo_Impl +{ + SfxShell* pCluster; + bool bPush; + bool bDelete; + bool bDeleted; + bool bUntil; + + SfxToDo_Impl( bool bOpPush, bool bOpDelete, bool bOpUntil, SfxShell& rCluster ) + : pCluster(&rCluster) + , bPush(bOpPush) + , bDelete(bOpDelete) + , bDeleted(false) + , bUntil(bOpUntil) + {} +}; + +struct SfxObjectBars_Impl +{ + ToolbarId eId; // ConfigId of the Toolbox + sal_uInt16 nPos; + SfxVisibilityFlags nFlags; // special visibility flags + + SfxObjectBars_Impl() : eId(ToolbarId::None), nPos(0), nFlags(SfxVisibilityFlags::Invisible) {} +}; + +} + +struct SfxDispatcher_Impl +{ + //When the dispatched is locked, SfxRequests accumulate in aReqArr for + //later dispatch when unlocked via Post + + //The pointers are typically deleted in Post, only if we never get around + //to posting them do we delete the unposted requests. + std::vector<std::unique_ptr<SfxRequest>> + aReqArr; + SfxShellStack_Impl aStack; // active functionality + Idle aIdle; // for Flush + std::deque<SfxToDo_Impl> aToDoStack; // not processed Push/Pop + SfxViewFrame* pFrame; // NULL or associated Frame + tools::SvRef<SfxHintPoster> + xPoster; // Execute asynchronous + bool bFlushing; // sal_True during Flush //? + bool bUpdated; // Update_Impl has run + bool bLocked; // No Execute + bool bInvalidateOnUnlock; // because someone asked + bool bActive; // not to be confused with set! + bool* pInCallAliveFlag; // view the Destructor Stack + SfxObjectBars_Impl aObjBars[SFX_OBJECTBAR_MAX]; + SfxObjectBars_Impl aFixedObjBars[SFX_OBJECTBAR_MAX]; + std::vector<sal_uInt32> aChildWins; + bool bNoUI; // UI only from Parent Dispatcher + bool bReadOnly; // Document is ReadOnly + bool bQuiet; // Only use parent dispatcher + + SfxSlotFilterState nFilterEnabling; // 1==filter enabled slots, + // 2==ReadOnlyDoc overturned + o3tl::span<sal_uInt16 const> + pFilterSIDs; // sorted Array of SIDs + SfxDisableFlags nDisableFlags; + bool bFlushed; + std::deque< std::deque<SfxToDo_Impl> > aToDoCopyStack; +}; + +/** This method checks if the stack of the SfxDispatchers is flushed, or if + push- or pop- commands are pending. +*/ +bool SfxDispatcher::IsFlushed() const +{ + return xImp->bFlushed; +} + +/** This method performs outstanding push- and pop- commands. For <SfxShell>s, + which are new on the stack, the <SfxShell::Activate(bool)> is invoked + with bMDI == sal_True, for SfxShells that are removed from the stack, the + <SfxShell::Deactivate(bool)> is invoked with bMDI == sal_True +*/ +void SfxDispatcher::Flush() +{ + if (!xImp->bFlushed) FlushImpl(); +} + +/** With this method, a <SfxShell> pushed on to the SfxDispatcher. + The SfxShell is first marked for push and a timer is set up. + First when the timer has counted down to zero the push + ( <SfxDispatcher::Flush()> ) is actually performed and the + <SfxBindings> is invalidated. While the timer is counting down + the opposing push and pop commands on the same SfxShell are + leveled out. +*/ +void SfxDispatcher::Push(SfxShell& rShell) + +{ + Pop( rShell, SfxDispatcherPopFlags::PUSH ); +} + +/** This method checks whether a particular <SfxShell> instance is + on the SfxDispatcher. + + @returns true The SfxShell instance is on the SfxDispatcher. + false The SfxShell instance is not on the SfxDispatcher. +*/ +bool SfxDispatcher::IsActive(const SfxShell& rShell) + +{ + return CheckVirtualStack(rShell); +} + +/** With this method it can be determined whether the SfxDispatcher is + locked or unlocked. A locked SfxDispatcher does not perform <SfxRequest>s + and no longer provides any status information. It behaves as if all the + slots are disabled. + + The dispatcher is also marked as blocked, if all Dispatcher are locked + (<SfxApplication::LockDispatcher()>) or the associated top frame is in the + modal-mode and if the specified slot are handled as frame-specific + (ie, not served by the application). +*/ +bool SfxDispatcher::IsLocked() const +{ + return xImp->bLocked; +} + +/** With this method it can be determined if the SfxDispacher is the + applications dispatcher. + + @return bool it is the application dispatcher. +*/ +bool SfxDispatcher::IsAppDispatcher() const +{ + return !xImp->pFrame; +} + +/** Helper function to check whether a slot can be executed and + check the execution itself +*/ +void SfxDispatcher::Call_Impl(SfxShell& rShell, const SfxSlot &rSlot, SfxRequest &rReq, bool bRecord) +{ + SFX_STACK(SfxDispatcher::Call_Impl); + + // The slot may be called (meaning enabled) + if ( !rSlot.IsMode(SfxSlotMode::FASTCALL) && !rShell.CanExecuteSlot_Impl(rSlot) && !rShell.IsConditionalFastCall(rReq) ) + return; + + if ( GetFrame() ) + { + // Recording may start + css::uno::Reference< css::beans::XPropertySet > xSet( + GetFrame()->GetFrame().GetFrameInterface(), + css::uno::UNO_QUERY); + + if ( xSet.is() ) + { + css::uno::Any aProp = xSet->getPropertyValue("DispatchRecorderSupplier"); + css::uno::Reference< css::frame::XDispatchRecorderSupplier > xSupplier; + css::uno::Reference< css::frame::XDispatchRecorder > xRecorder; + aProp >>= xSupplier; + if(xSupplier.is()) + xRecorder = xSupplier->getDispatchRecorder(); + + if ( bRecord && xRecorder.is() && !rSlot.IsMode(SfxSlotMode::NORECORD) ) + rReq.Record_Impl( rShell, rSlot, xRecorder, GetFrame() ); + } + } + // Get all that is needed, because the slot may not have survived the + // Execute if it is a 'pseudo slot' for macros or verbs. + bool bAutoUpdate = rSlot.IsMode(SfxSlotMode::AUTOUPDATE); + + // API-call parentheses and document-lock during the calls + { + // 'this' must respond in the Destructor + bool bThisDispatcherAlive = true; + bool *pOldInCallAliveFlag = xImp->pInCallAliveFlag; + xImp->pInCallAliveFlag = &bThisDispatcherAlive; + + SfxExecFunc pFunc = rSlot.GetExecFnc(); + rShell.CallExec( pFunc, rReq ); + + // If 'this' is still alive + if ( bThisDispatcherAlive ) + xImp->pInCallAliveFlag = pOldInCallAliveFlag; + else + { + if ( pOldInCallAliveFlag ) + { + // also protect nested stack frames + *pOldInCallAliveFlag = false; + } + + // do nothing after this object is dead + return; + } + } + + if ( rReq.IsDone() ) + { + SfxBindings *pBindings = GetBindings(); + + // When AutoUpdate update immediately + if ( bAutoUpdate && pBindings ) + { + pBindings->Invalidate(rSlot.GetSlotId()); + pBindings->Update(rSlot.GetSlotId()); + } + } +} + +void SfxDispatcher::Construct_Impl() +{ + xImp.reset(new SfxDispatcher_Impl); + xImp->bFlushed = true; + + xImp->bFlushing = false; + xImp->bUpdated = false; + xImp->bLocked = false; + xImp->bActive = false; + xImp->bNoUI = false; + xImp->bReadOnly = false; + xImp->bQuiet = false; + xImp->pInCallAliveFlag = nullptr; + xImp->nFilterEnabling = SfxSlotFilterState::DISABLED; + xImp->nDisableFlags = SfxDisableFlags::NONE; + + xImp->bInvalidateOnUnlock = false; + + for (SfxObjectBars_Impl & rObjBar : xImp->aObjBars) + rObjBar.eId = ToolbarId::None; + + xImp->xPoster = new SfxHintPoster(std::bind(&SfxDispatcher::PostMsgHandler, this, std::placeholders::_1)); + + xImp->aIdle.SetPriority(TaskPriority::HIGH_IDLE ); + xImp->aIdle.SetInvokeHandler( LINK(this, SfxDispatcher, EventHdl_Impl ) ); + xImp->aIdle.SetDebugName( "sfx::SfxDispatcher_Impl aIdle" ); +} + +SfxDispatcher::SfxDispatcher() +{ + Construct_Impl(); + xImp->pFrame = nullptr; +} + +/** The constructor of the SfxDispatcher class places a stack of empty + <SfxShell> pointers. It is not initially locked and is considered flushed. +*/ +SfxDispatcher::SfxDispatcher(SfxViewFrame *pViewFrame) +{ + Construct_Impl(); + xImp->pFrame = pViewFrame; +} + +/** The destructor of the SfxDispatcher class should not be called when the + SfxDispatcher instance is active. It may, however, still be a <SfxShell> + pointer on the stack. +*/ +SfxDispatcher::~SfxDispatcher() +{ + SAL_INFO("sfx.control", "Delete Dispatcher " << reinterpret_cast<sal_Int64>(this)); + DBG_ASSERT( !xImp->bActive, "deleting active Dispatcher" ); + + // So that no timer by Reschedule in PlugComm strikes the LeaveRegistrations + xImp->aIdle.Stop(); + xImp->xPoster->SetEventHdl( std::function<void (std::unique_ptr<SfxRequest>)>() ); + + // Notify the stack variables in Call_Impl + if ( xImp->pInCallAliveFlag ) + *xImp->pInCallAliveFlag = false; + + // Get bindings and application + SfxApplication *pSfxApp = SfxGetpApp(); + SfxBindings* pBindings = GetBindings(); + + // When not flushed, revive the bindings + if (pBindings && !pSfxApp->IsDowning() && !xImp->bFlushed) + pBindings->DLEAVEREGISTRATIONS(); + + // may unregister the bindings + while ( pBindings ) + { + if ( pBindings->GetDispatcher_Impl() == this) + pBindings->SetDispatcher(nullptr); + pBindings = pBindings->GetSubBindings_Impl(); + } +} + +/** With this method, one or more <SfxShell> are popped from the SfxDispatcher. + The SfxShell is marked for popping and a timer is set up. Only when the + timer has reached the end, the pop is actually performed + ( <SfxDispatcher::Flush()> ) and the <SfxBindings> is invalidated. + While the timer is running the opposing push and pop commands on one + SfxShell cancel each other out. + + @param rShell the stack to take the SfxShell instance. + @param nMode SfxDispatcherPopFlags::POP_UNTIL + Also all 'rShell' of SfxShells are taken from the + stack. + + SfxDispatcherPopFlags::POP_DELETE + All SfxShells actually taken from the stack + will be deleted. + + SfxDispatcherPopFlags::PUSH (InPlace use only) + The Shell is pushed. +*/ +void SfxDispatcher::Pop(SfxShell& rShell, SfxDispatcherPopFlags nMode) +{ + DBG_ASSERT( rShell.GetInterface(), + "pushing SfxShell without previous RegisterInterface()" ); + + bool bDelete = bool(nMode & SfxDispatcherPopFlags::POP_DELETE); + bool bUntil = bool(nMode & SfxDispatcherPopFlags::POP_UNTIL); + bool bPush = bool(nMode & SfxDispatcherPopFlags::PUSH); + + SfxApplication *pSfxApp = SfxGetpApp(); + + SAL_INFO( + "sfx.control", + "-SfxDispatcher(" << this << (bPush ? ")::Push(" : ")::Pop(") + << (rShell.GetInterface() + ? rShell.GetInterface()->GetClassName() : SAL_STREAM(&rShell)) + << (bDelete ? ") with delete" : ")") + << (bUntil ? " (up to)" : "")); + + // same shell as on top of the to-do stack? + if(!xImp->aToDoStack.empty() && xImp->aToDoStack.front().pCluster == &rShell) + { + // cancel inverse actions + if ( xImp->aToDoStack.front().bPush != bPush ) + xImp->aToDoStack.pop_front(); + else + { + DBG_ASSERT( bPush, "SfxInterface pushed more than once" ); + DBG_ASSERT( !bPush, "SfxInterface popped more than once" ); + } + } + else + { + // Remember Action + xImp->aToDoStack.push_front( SfxToDo_Impl(bPush, bDelete, bUntil, rShell) ); + if (xImp->bFlushed) + { + SAL_INFO("sfx.control", "Unflushed dispatcher!"); + xImp->bFlushed = false; + xImp->bUpdated = false; + + // Put bindings to sleep + SfxBindings* pBindings = GetBindings(); + if ( pBindings ) + pBindings->DENTERREGISTRATIONS(); + } + } + + if(!pSfxApp->IsDowning() && !xImp->aToDoStack.empty()) + { + // No immediate update is requested + xImp->aIdle.Start(); + } + else + { + // but to do nothing + xImp->aIdle.Stop(); + + // Bindings may wake up again + if(xImp->aToDoStack.empty()) + { + SfxBindings* pBindings = GetBindings(); + if ( pBindings ) + pBindings->DLEAVEREGISTRATIONS(); + } + } +} + + +/** This handler is called after <SfxDispatcher::Invalidate()> or after + changes on the stack (<SfxDispatcher::Push()> and <SfxDispatcher::Pop()) + + It flushes the Stack, if it is dirty, thus it actually executes the + pending Push and Pop commands. +*/ +IMPL_LINK_NOARG( SfxDispatcher, EventHdl_Impl, Timer *, void ) +{ + Flush(); + Update_Impl(); + SfxBindings* pBindings = GetBindings(); + if ( pBindings ) + pBindings->StartUpdate_Impl(); +} + +/** With this method it can be tested whether the <SfxShell> rShell is on the + stack, when it was flushed. This way the SfxDispatcher is not actually + flushed. + + This method is intended among other things to make assertions possible + without the side effect of having to flush the SfxDispathcer. +*/ +bool SfxDispatcher::CheckVirtualStack(const SfxShell& rShell) +{ + SFX_STACK(SfxDispatcher::CheckVirtualStack); + + SfxShellStack_Impl aStack( xImp->aStack ); + for(std::deque<SfxToDo_Impl>::reverse_iterator i = xImp->aToDoStack.rbegin(); i != xImp->aToDoStack.rend(); ++i) + { + if(i->bPush) + aStack.push_back(i->pCluster); + else + { + SfxShell* pPopped(nullptr); + do + { + DBG_ASSERT( !aStack.empty(), "popping from empty stack" ); + pPopped = aStack.back(); + aStack.pop_back(); + } + while(i->bUntil && pPopped != i->pCluster); + DBG_ASSERT(pPopped == i->pCluster, "popping unpushed SfxInterface"); + } + } + + bool bReturn = std::find(aStack.begin(), aStack.end(), &rShell) != aStack.end(); + return bReturn; +} + +/** Determines the position of a given SfxShell in the stack of the dispatcher. + If possible this is flushed before. + + [Return value] + + sal_uInt16 == USRT_MAX + The SfxShell is not on this SfxDispatcher. + + < USHRT_MAX + Position of the SfxShell on the Dispatcher + from the top count stating with 0. +*/ +sal_uInt16 SfxDispatcher::GetShellLevel(const SfxShell& rShell) +{ + SFX_STACK(SfxDispatcher::GetShellLevel); + Flush(); + + for ( size_t n = 0; n < xImp->aStack.size(); ++n ) + if ( *( xImp->aStack.rbegin() + n ) == &rShell ) + return n; + + return USHRT_MAX; +} + +/** Returns a pointer to the <SfxShell> which is at the position nIdx + (from the top, last pushed is 0) on the stack. + + Thus the SfxDispatcher is not flushed. + + Is the stack not deep enough a NULL-Pointer is returned. +*/ +SfxShell *SfxDispatcher::GetShell(sal_uInt16 nIdx) const +{ + sal_uInt16 nShellCount = xImp->aStack.size(); + if ( nIdx < nShellCount ) + return *(xImp->aStack.rbegin() + nIdx); + return nullptr; +} + +/** This method returns a pointer to the <SfxBinding> Instance on which the + SfxDispatcher is currently bound. A SfxDispatcher is only bound to + the SfxBindings when it is <UI-aktiv>. If it is not UI-active, + a NULL-pointer is returned. + + The returned pointer is only valid in the immediate context of the method + call. +*/ +SfxBindings* SfxDispatcher::GetBindings() const +{ + if ( xImp->pFrame ) + return &xImp->pFrame->GetBindings(); + else + return nullptr; +} + +/** Returns a pointer to the <SfxViewFrame> instance, which belongs to + this SfxDispatcher. If it is about the application dispatcher, + a NULL-pointer is returned. +*/ +SfxViewFrame* SfxDispatcher::GetFrame() const +{ + return xImp->pFrame; +} + +/** This method controls the activation of a dispatcher. + + Since the application dispatcher is always active, either as a sub + dispatcher of the <SfxViewFrame> dispatcher or as itself, it is never + activated as a whole, instead only its individual <SfxShell>s at + <SfxDispatcher::Push(SfxShell&)>. + + When activating a SfxDispatcher all of the SfxShells located on its stack + are called with the handler <SfxShell::Activate(bool)>, starting with + the lowest. +*/ +void SfxDispatcher::DoActivate_Impl(bool bMDI) +{ + SFX_STACK(SfxDispatcher::DoActivate); + if ( bMDI ) + { + SAL_INFO("sfx.control", "Activate Dispatcher " << reinterpret_cast<sal_Int64>(this)); + DBG_ASSERT( !xImp->bActive, "Activation error" ); + + xImp->bActive = true; + xImp->bUpdated = false; + SfxBindings* pBindings = GetBindings(); + if ( pBindings ) + { + pBindings->SetDispatcher(this); + pBindings->SetActiveFrame( xImp->pFrame->GetFrame().GetFrameInterface() ); + } + } + else + { + SAL_INFO("sfx.control", "Non-MDI-Activate Dispatcher " << reinterpret_cast<sal_Int64>(this)); + } + + if ( IsAppDispatcher() ) + return; + + for ( int i = int(xImp->aStack.size()) - 1; i >= 0; --i ) + (*(xImp->aStack.rbegin() + i ))->DoActivate_Impl(xImp->pFrame, bMDI); + + if ( bMDI && xImp->pFrame ) + { + xImp->pFrame->GetFrame().GetWorkWindow_Impl()->HidePopups_Impl( false, 1 ); + } + + if(!xImp->aToDoStack.empty()) + { + // No immediate update is requested + xImp->aIdle.Start(); + } +} + +/** This method controls the deactivation of a dispatcher. + + Since the application dispatcher is always active, either as a sub + dispatcher of the <SfxViewFrame> dispatcher or as itself, it is never + deactivated as a whole, instead only its individual <SfxShell>s at + <SfxDispatcher::Pop(SfxShell&)>. + + When deactivating a SfxDispatcher all of the SfxShells located on its stack + are called with the handler <SfxShell::Deactivate(bool)>, starting with + the lowest. +*/ +void SfxDispatcher::DoDeactivate_Impl(bool bMDI, SfxViewFrame const * pNew) +{ + SFX_STACK(SfxDispatcher::DoDeactivate); + + SfxApplication *pSfxApp = SfxGetpApp(); + + if ( bMDI ) + { + SAL_INFO("sfx.control", "Deactivate Dispatcher " << this); + DBG_ASSERT( xImp->bActive, "Deactivate error" ); + xImp->bActive = false; + + if ( xImp->pFrame && !(xImp->pFrame->GetObjectShell()->IsInPlaceActive() ) ) + { + SfxWorkWindow *pWorkWin = xImp->pFrame->GetFrame().GetWorkWindow_Impl(); + if ( pWorkWin ) + { + for (size_t n=0; n<xImp->aChildWins.size();) + { + SfxChildWindow *pWin = pWorkWin->GetChildWindow_Impl( static_cast<sal_uInt16>( xImp->aChildWins[n] & 0xFFFF ) ); + if (!pWin || pWin->GetAlignment() == SfxChildAlignment::NOALIGNMENT) + xImp->aChildWins.erase(xImp->aChildWins.begin()+n); + else + n++; + } + } + } + } + else { + SAL_INFO("sfx.control", "Non-MDI-DeActivate Dispatcher " << this); + } + + if ( IsAppDispatcher() && !pSfxApp->IsDowning() ) + return; + + for ( size_t i = 0; i < xImp->aStack.size(); ++i ) + (*(xImp->aStack.rbegin() + i))->DoDeactivate_Impl(xImp->pFrame, bMDI); + + bool bHidePopups = bMDI && xImp->pFrame; + if ( pNew && xImp->pFrame ) + { + css::uno::Reference< css::frame::XFrame > xOldFrame = + pNew->GetFrame().GetFrameInterface()->getCreator(); + + css::uno::Reference< css::frame::XFrame > xMyFrame = + GetFrame()->GetFrame().GetFrameInterface(); + + if ( xOldFrame == xMyFrame ) + bHidePopups = false; + } + + if ( bHidePopups ) + { + xImp->pFrame->GetFrame().GetWorkWindow_Impl()->HidePopups_Impl( true, 1 ); + } + + Flush(); +} + +/** This method searches in SfxDispatcher after <SfxShell> , from the Slot Id + nSlot currently being handled. For this, the dispatcher is first flushed. + + @param nSlot the searchable Slot-Id + @param ppShell the SfxShell, which are currently handled the nSlot + @param ppSlot the SfxSlot, which are currently handled the nSlot + + @return int sal_True + The SfxShell was found, ppShell and ppSlot are valid. + + sal_False + The SfxShell was not found, ppShell and ppSlot are invalid. +*/ +bool SfxDispatcher::GetShellAndSlot_Impl(sal_uInt16 nSlot, SfxShell** ppShell, + const SfxSlot** ppSlot, bool bOwnShellsOnly, bool bRealSlot) +{ + SFX_STACK(SfxDispatcher::GetShellAndSlot_Impl); + + Flush(); + SfxSlotServer aSvr; + if ( FindServer_(nSlot, aSvr) ) + { + if ( bOwnShellsOnly && aSvr.GetShellLevel() >= xImp->aStack.size() ) + return false; + + *ppShell = GetShell(aSvr.GetShellLevel()); + *ppSlot = aSvr.GetSlot(); + if ( nullptr == (*ppSlot)->GetExecFnc() && bRealSlot ) + *ppSlot = (*ppShell)->GetInterface()->GetRealSlot(*ppSlot); + // Check only real slots as enum slots don't have an execute function! + return !bRealSlot || !((nullptr == *ppSlot) || (nullptr == (*ppSlot)->GetExecFnc()) ); + } + + return false; +} + +/** This method performs a request for a cached <Slot-Server>. + + @param rShell to the calling <SfxShell> + @param rSlot to the calling <SfxSlot> + @param rReq function to be performed (Id and optional parameters) + @param eCallMode Synchronously, asynchronously or as shown in the slot +*/ +void SfxDispatcher::Execute_(SfxShell& rShell, const SfxSlot& rSlot, + SfxRequest& rReq, SfxCallMode eCallMode) +{ + SFX_STACK(SfxDispatcher::Execute_); + DBG_ASSERT( !xImp->bFlushing, "recursive call to dispatcher" ); + DBG_ASSERT( xImp->aToDoStack.empty(), "unprepared InPlace _Execute" ); + + if ( IsLocked() ) + return; + + if ( bool(eCallMode & SfxCallMode::ASYNCHRON) || + ( (eCallMode & SfxCallMode::SYNCHRON) == SfxCallMode::SLOT && + rSlot.IsMode(SfxSlotMode::ASYNCHRON) ) ) + { + sal_uInt16 nShellCount = xImp->aStack.size(); + for ( sal_uInt16 n=0; n<nShellCount; n++ ) + { + if ( &rShell == *(xImp->aStack.rbegin() + n) ) + { + if ( bool(eCallMode & SfxCallMode::RECORD) ) + rReq.AllowRecording( true ); + xImp->xPoster->Post(std::make_unique<SfxRequest>(rReq)); + return; + } + } + } + else + Call_Impl( rShell, rSlot, rReq, SfxCallMode::RECORD==(eCallMode&SfxCallMode::RECORD) ); +} + +/** Helper function to put from rItem below the Which-ID in the pool of the + Item Sets rSet. +*/ +static void MappedPut_Impl(SfxAllItemSet &rSet, const SfxPoolItem &rItem) +{ + // Put with mapped Which-Id if possible + const SfxItemPool *pPool = rSet.GetPool(); + sal_uInt16 nWhich = rItem.Which(); + if ( SfxItemPool::IsSlot(nWhich) ) + nWhich = pPool->GetWhich(nWhich); + rSet.Put( rItem, nWhich ); +} + +const SfxSlot* SfxDispatcher::GetSlot( const OUString& rCommand ) +{ + // Count the number of Shells on the linked Dispatcher + Flush(); + sal_uInt16 nTotCount = xImp->aStack.size(); + + for ( sal_uInt16 i = 0; i < nTotCount; ++i ) + { + SfxShell *pObjShell = GetShell(i); + SfxInterface *pIFace = pObjShell->GetInterface(); + const SfxSlot *pSlot = pIFace->GetSlot( rCommand ); + if ( pSlot ) + return pSlot; + } + + return nullptr; +} + +const SfxPoolItem* SfxDispatcher::Execute(sal_uInt16 nSlot, SfxCallMode nCall, + SfxItemSet const * pArgs, SfxItemSet const * pInternalArgs, sal_uInt16 nModi) +{ + if ( IsLocked() ) + return nullptr; + + SfxShell *pShell = nullptr; + const SfxSlot *pSlot = nullptr; + if ( GetShellAndSlot_Impl( nSlot, &pShell, &pSlot, false, true ) ) + { + SfxAllItemSet aSet( pShell->GetPool() ); + if ( pArgs ) + { + SfxItemIter aIter(*pArgs); + for ( const SfxPoolItem *pArg = aIter.GetCurItem(); + pArg; + pArg = aIter.NextItem() ) + MappedPut_Impl( aSet, *pArg ); + } + SfxRequest aReq(nSlot, nCall, aSet); + if (pInternalArgs) + aReq.SetInternalArgs_Impl( *pInternalArgs ); + aReq.SetModifier( nModi ); + + Execute_( *pShell, *pSlot, aReq, nCall ); + return aReq.GetReturnValue(); + } + return nullptr; +} + +/** Method to execute a <SfxSlot>s over the Slot-Id. + + @param nSlot the Id of the executing function + @param eCall SfxCallMode::SYNCRHON, ..._ASYNCHRON or ..._SLOT + @param pArgs Zero terminated C-Array of Parameters + @param pInternalArgs Zero terminated C-Array of Parameters + + @return const SfxPoolItem* Pointer to the SfxPoolItem valid to the next run + though the Message-Loop, which contains the return + value. + + Or a NULL-Pointer, when the function was not + executed (for example canceled by the user). +*/ +const SfxPoolItem* SfxDispatcher::Execute(sal_uInt16 nSlot, SfxCallMode eCall, + const SfxPoolItem **pArgs, sal_uInt16 nModi, const SfxPoolItem **pInternalArgs) +{ + if ( IsLocked() ) + return nullptr; + + SfxShell *pShell = nullptr; + const SfxSlot *pSlot = nullptr; + if ( GetShellAndSlot_Impl( nSlot, &pShell, &pSlot, false, true ) ) + { + std::unique_ptr<SfxRequest> pReq; + if ( pArgs && *pArgs ) + { + SfxAllItemSet aSet( pShell->GetPool() ); + for ( const SfxPoolItem **pArg = pArgs; *pArg; ++pArg ) + MappedPut_Impl( aSet, **pArg ); + pReq.reset(new SfxRequest( nSlot, eCall, aSet )); + } + else + pReq.reset(new SfxRequest( nSlot, eCall, pShell->GetPool() )); + pReq->SetModifier( nModi ); + if( pInternalArgs && *pInternalArgs) + { + SfxAllItemSet aSet( SfxGetpApp()->GetPool() ); + for ( const SfxPoolItem **pArg = pInternalArgs; *pArg; ++pArg ) + aSet.Put( **pArg ); + pReq->SetInternalArgs_Impl( aSet ); + } + Execute_( *pShell, *pSlot, *pReq, eCall ); + const SfxPoolItem* pRet = pReq->GetReturnValue(); + return pRet; + } + return nullptr; +} + +/** Method to execute a <SfxSlot>s over the Slot-Id. + + @param nSlot the Id of the executing function + @param eCall SfxCallMode::SYNCRHON, ..._ASYNCHRON or ..._SLOT + @param rArgs <SfxItemSet> with the parameters + + @return const SfxPoolItem* Pointer to the SfxPoolItem valid to the next run + though the Message-Loop, which contains the return + value. + + Or a NULL-Pointer, when the function was not + executed (for example canceled by the user). +*/ +const SfxPoolItem* SfxDispatcher::Execute(sal_uInt16 nSlot, SfxCallMode eCall, + const SfxItemSet &rArgs) +{ + if ( IsLocked() ) + return nullptr; + + SfxShell *pShell = nullptr; + const SfxSlot *pSlot = nullptr; + if ( GetShellAndSlot_Impl( nSlot, &pShell, &pSlot, false, true ) ) + { + SfxAllItemSet aSet( pShell->GetPool() ); + SfxItemIter aIter(rArgs); + for ( const SfxPoolItem *pArg = aIter.GetCurItem(); + pArg; + pArg = aIter.NextItem() ) + MappedPut_Impl( aSet, *pArg ); + SfxRequest aReq( nSlot, eCall, aSet ); + aReq.SetModifier( 0 ); + Execute_( *pShell, *pSlot, aReq, eCall ); + return aReq.GetReturnValue(); + } + return nullptr; +} + +/** Method to execute a <SfxSlot>s over the Slot-Id. + + [Note] + + The parameters are copied, can therefore be passed on as the address + of stack objects. + + @param nSlot the Id of the executing function + @param eCall SfxCallMode::SYNCRHON, ..._ASYNCHRON or ..._SLOT + @param args list of SfxPoolItem arguments + + @return Pointer to the SfxPoolItem valid to the next run + though the Message-Loop, which contains the return + value. + + Or a NULL-Pointer, when the function was not + executed (for example canceled by the user). + + [Example] + + pDispatcher->Execute( SID_OPENDOCUMENT, SfxCallMode::SYNCHRON, + { &SfxStringItem( SID_FILE_NAME, "\\tmp\\temp.sdd" ), + &SfxStringItem( SID_FILTER_NAME, "StarDraw Presentation" ), + &SfxBoolItem( SID_DOC_READONLY, sal_False ), + }); +*/ +const SfxPoolItem* SfxDispatcher::ExecuteList(sal_uInt16 nSlot, SfxCallMode eCall, + std::initializer_list<SfxPoolItem const*> args, + std::initializer_list<SfxPoolItem const*> internalargs) +{ + if ( IsLocked() ) + return nullptr; + + SfxShell *pShell = nullptr; + const SfxSlot *pSlot = nullptr; + if ( GetShellAndSlot_Impl( nSlot, &pShell, &pSlot, false, true ) ) + { + SfxAllItemSet aSet( pShell->GetPool() ); + + for (const SfxPoolItem *pArg : args) + { + assert(pArg); + MappedPut_Impl( aSet, *pArg ); + } + + SfxRequest aReq(nSlot, eCall, aSet); + + if (internalargs.begin() != internalargs.end()) + { + SfxAllItemSet aInternalSet(SfxGetpApp()->GetPool()); + for (const SfxPoolItem *pArg : internalargs) + { + assert(pArg); + aInternalSet.Put(*pArg); + } + aReq.SetInternalArgs_Impl(aInternalSet); + } + + Execute_( *pShell, *pSlot, aReq, eCall ); + return aReq.GetReturnValue(); + } + return nullptr; +} + +/** Helper method to receive the asynchronously executed <SfxRequest>s. +*/ +void SfxDispatcher::PostMsgHandler(std::unique_ptr<SfxRequest> pReq) +{ + DBG_ASSERT( !xImp->bFlushing, "recursive call to dispatcher" ); + SFX_STACK(SfxDispatcher::PostMsgHandler); + + // Has also the Pool not yet died? + if ( pReq->IsCancelled() ) + return; + + if ( !IsLocked() ) + { + Flush(); + SfxSlotServer aSvr; + if ( FindServer_(pReq->GetSlot(), aSvr ) ) // HACK(x), whatever that was supposed to mean + { + const SfxSlot *pSlot = aSvr.GetSlot(); + SfxShell *pSh = GetShell(aSvr.GetShellLevel()); + + // When the pSlot is a "Pseudoslot" for macros or Verbs, it can + // be destroyed in the Call_Impl, thus do not use it anymore! + pReq->SetSynchronCall( false ); + Call_Impl( *pSh, *pSlot, *pReq, pReq->AllowsRecording() ); //! why bRecord? + } + } + else + { + if ( xImp->bLocked ) + xImp->aReqArr.emplace_back(std::move(pReq)); + else + xImp->xPoster->Post(std::move(pReq)); + } +} + +void SfxDispatcher::SetMenu_Impl() +{ +#if HAVE_FEATURE_DESKTOP + if ( !xImp->pFrame ) + return; + + SfxViewFrame* pTop = xImp->pFrame->GetTopViewFrame(); + if ( !pTop || pTop->GetBindings().GetDispatcher() != this ) + return; + + SfxFrame& rFrame = pTop->GetFrame(); + if ( !rFrame.IsMenuBarOn_Impl() ) + return; + + css::uno::Reference < css::beans::XPropertySet > xPropSet( rFrame.GetFrameInterface(), css::uno::UNO_QUERY ); + if ( xPropSet.is() ) + { + css::uno::Reference< css::frame::XLayoutManager > xLayoutManager; + css::uno::Any aValue = xPropSet->getPropertyValue("LayoutManager"); + aValue >>= xLayoutManager; + if ( xLayoutManager.is() ) + { + OUString aMenuBarURL( "private:resource/menubar/menubar" ); + if ( !xLayoutManager->isElementVisible( aMenuBarURL ) ) + xLayoutManager->createElement( aMenuBarURL ); + } + } +#endif +} + +void SfxDispatcher::Update_Impl( bool bForce ) +{ + SFX_STACK(SfxDispatcher::Update_Impl); + + Flush(); + + if ( !xImp->pFrame ) + return; + + bool bUpdate = bForce; + if ( xImp->pFrame ) + { + SfxWorkWindow *pWork = xImp->pFrame->GetFrame().GetWorkWindow_Impl(); + SfxDispatcher *pAct = pWork->GetBindings().GetDispatcher_Impl(); + if (pAct == this) + { + if ( !bUpdate ) + bUpdate = !xImp->bUpdated; + xImp->bUpdated = true; + } + } + + if ( !bUpdate || xImp->pFrame->GetFrame().IsClosing_Impl() ) + return; + + SfxViewFrame* pTop = xImp->pFrame ? xImp->pFrame->GetTopViewFrame() : nullptr; + bool bUIActive = pTop && pTop->GetBindings().GetDispatcher() == this; + + if ( !bUIActive && pTop && GetBindings() == &pTop->GetBindings() ) + // keep own tools internally for collecting + GetBindings()->GetDispatcher()->xImp->bUpdated = false; + + css::uno::Reference< css::frame::XFrame > xFrame; + SfxBindings* pBindings = GetBindings(); + if (pBindings) + { + pBindings->DENTERREGISTRATIONS(); + xFrame = pBindings->GetActiveFrame(); + } + css::uno::Reference< css::beans::XPropertySet > xPropSet( xFrame, css::uno::UNO_QUERY ); + css::uno::Reference< css::frame::XLayoutManager > xLayoutManager; + if ( xPropSet.is() ) + { + try + { + css::uno::Any aValue = xPropSet->getPropertyValue("LayoutManager"); + aValue >>= xLayoutManager; + } + catch (const css::uno::Exception&) + { + } + } + + if ( xLayoutManager.is() ) + xLayoutManager->lock(); + + bool bIsIPActive = xImp->pFrame && xImp->pFrame->GetObjectShell()->IsInPlaceActive(); + SfxInPlaceClient *pClient = xImp->pFrame ? xImp->pFrame->GetViewShell()->GetUIActiveClient() : nullptr; + if ( bUIActive && /* !bIsIPActive && */ ( !pClient || !pClient->IsObjectUIActive() ) ) + SetMenu_Impl(); + + SfxWorkWindow *pWorkWin = xImp->pFrame->GetFrame().GetWorkWindow_Impl(); + pWorkWin->ResetStatusBar_Impl(); + + { + SfxWorkWindow *pWork = xImp->pFrame->GetFrame().GetWorkWindow_Impl(); + SfxDispatcher *pAct = pWork->GetBindings().GetDispatcher_Impl(); + if (pAct == this) + { + pWork->ResetObjectBars_Impl(); + pWork->ResetChildWindows_Impl(); + } + } + + bool bIsActive = false; + SfxDispatcher *pActDispat = pWorkWin->GetBindings().GetDispatcher_Impl(); + if ( !bIsActive && this == pActDispat ) + bIsActive = true; + + Update_Impl_( bUIActive, !bIsIPActive, bIsIPActive, pWorkWin ); + if (bUIActive || bIsActive) + pWorkWin->UpdateObjectBars_Impl(); + + if ( pBindings ) + pBindings->DLEAVEREGISTRATIONS(); + + if ( xLayoutManager.is() ) + xLayoutManager->unlock(); + + if ( SfxViewShell::Current() && SfxViewShell::Current()->GetDispatcher() ) + { + const SfxPoolItem *pItem; + SfxViewShell::Current()->GetDispatcher()->QueryState(SID_NOTEBOOKBAR, pItem); + } +} + +void SfxDispatcher::Update_Impl_( bool bUIActive, bool bIsMDIApp, bool bIsIPOwner, SfxWorkWindow *pTaskWin ) +{ + SfxWorkWindow *pWorkWin = xImp->pFrame->GetFrame().GetWorkWindow_Impl(); + bool bIsActive = false; + SfxDispatcher *pActDispat = pWorkWin->GetBindings().GetDispatcher_Impl(); + if ( pActDispat && !bIsActive ) + { + if ( this == pActDispat ) + bIsActive = true; + } + + for (SfxObjectBars_Impl & rObjBar : xImp->aObjBars) + rObjBar.eId = ToolbarId::None; + xImp->aChildWins.clear(); + + // bQuiet: own shells aren't considered for UI and SlotServer + // bNoUI: own Shells aren't considered forms UI + if ( xImp->bQuiet || xImp->bNoUI || (xImp->pFrame && xImp->pFrame->GetObjectShell()->IsPreview()) ) + return; + + StatusBarId eStatBarId = StatusBarId::None; + + SfxSlotPool* pSlotPool = &SfxSlotPool::GetSlotPool( GetFrame() ); + sal_uInt16 nTotCount = xImp->aStack.size(); + for ( sal_uInt16 nShell = nTotCount; nShell > 0; --nShell ) + { + SfxShell *pShell = GetShell( nShell-1 ); + SfxInterface *pIFace = pShell->GetInterface(); + + // don't consider shells if "Hidden" or "Quiet" + bool bReadOnlyShell = IsReadOnlyShell_Impl( nShell-1 ); + sal_uInt16 nNo; + for ( nNo = 0; pIFace && nNo<pIFace->GetObjectBarCount(); ++nNo ) + { + sal_uInt16 nPos = pIFace->GetObjectBarPos(nNo); + SfxVisibilityFlags nFlags = pIFace->GetObjectBarFlags(nNo); + if ( bReadOnlyShell && !( nFlags & SfxVisibilityFlags::ReadonlyDoc ) ) + continue; + + // check whether toolbar needs activation of a special feature + SfxShellFeature nFeature = pIFace->GetObjectBarFeature(nNo); + if ((nFeature != SfxShellFeature::NONE) && !pShell->HasUIFeature(nFeature)) + continue; + + // check for toolboxes that are exclusively for a viewer + if ( xImp->pFrame) + { + bool bViewerTbx( nFlags & SfxVisibilityFlags::Viewer ); + SfxObjectShell* pSh = xImp->pFrame->GetObjectShell(); + const SfxBoolItem* pItem = SfxItemSet::GetItem<SfxBoolItem>(pSh->GetMedium()->GetItemSet(), SID_VIEWONLY, false); + bool bIsViewer = pItem && pItem->GetValue(); + if ( bIsViewer != bViewerTbx ) + continue; + } + + // always register toolbars, allows to switch them on + bool bVisible = pIFace->IsObjectBarVisible(nNo); + if ( !bVisible ) + nFlags = SfxVisibilityFlags::Invisible; + + SfxObjectBars_Impl& rBar = xImp->aObjBars[nPos]; + rBar.nPos = nPos; + rBar.nFlags = nFlags; + rBar.eId = pIFace->GetObjectBarId(nNo); + + if ( bUIActive || bIsActive ) + { + pWorkWin->SetObjectBar_Impl(nPos, nFlags, rBar.eId); + } + + if ( !bVisible ) + rBar.eId = ToolbarId::None; + } + + for ( nNo=0; pIFace && nNo<pIFace->GetChildWindowCount(); nNo++ ) + { + sal_uInt32 nId = pIFace->GetChildWindowId(nNo); + const SfxSlot *pSlot = pSlotPool->GetSlot( static_cast<sal_uInt16>(nId) ); + SAL_WARN_IF( !pSlot, "sfx.control", "Childwindow slot missing: " << nId ); + if ( bReadOnlyShell ) + { + // only show ChildWindows if their slot is allowed for readonly documents + if ( pSlot && !pSlot->IsMode( SfxSlotMode::READONLYDOC ) ) + continue; + } + + SfxShellFeature nFeature = pIFace->GetChildWindowFeature(nNo); + if ((nFeature != SfxShellFeature::NONE) && !pShell->HasUIFeature(nFeature)) + continue; + + // slot decides whether a ChildWindow is shown when document is OLE server or OLE client + SfxVisibilityFlags nMode = SfxVisibilityFlags::Standard; + if( pSlot ) + { + if ( pSlot->IsMode(SfxSlotMode::CONTAINER) ) + { + if ( pWorkWin->IsVisible_Impl( SfxVisibilityFlags::Client ) ) + nMode |= SfxVisibilityFlags::Client; + } + else + { + if ( pWorkWin->IsVisible_Impl( SfxVisibilityFlags::Server ) ) + nMode |= SfxVisibilityFlags::Server; + } + } + + if ( bUIActive || bIsActive ) + pWorkWin->SetChildWindowVisible_Impl( nId, true, nMode ); + if ( bUIActive || bIsActive || !pWorkWin->IsFloating( static_cast<sal_uInt16>( nId & 0xFFFF ) ) ) + xImp->aChildWins.push_back( nId ); + } + + if ( bIsMDIApp || bIsIPOwner ) + { + StatusBarId eId = pIFace ? pIFace->GetStatusBarId() : StatusBarId::None; + if (eId != StatusBarId::None) + eStatBarId = eId; + } + } + + for ( sal_uInt16 nPos=0; nPos<SFX_OBJECTBAR_MAX; nPos++ ) + { + SfxObjectBars_Impl& rFixed = xImp->aFixedObjBars[nPos]; + if (rFixed.eId != ToolbarId::None) + { + SfxObjectBars_Impl& rBar = xImp->aObjBars[nPos]; + rBar = rFixed; + pWorkWin->SetObjectBar_Impl(rFixed.nPos, rFixed.nFlags, + rFixed.eId); + } + } + + if ( !pTaskWin || ( !bIsMDIApp && !bIsIPOwner ) ) + return; + + bool bIsTaskActive = false; + + SfxDispatcher *pActDispatcher = pTaskWin->GetBindings().GetDispatcher_Impl(); + if ( pActDispatcher && !bIsTaskActive ) + { + if ( this == pActDispatcher ) + bIsTaskActive = true; + } + + if (bIsTaskActive && eStatBarId != StatusBarId::None && xImp->pFrame) + { + // internal frames also may control statusbar + xImp->pFrame->GetFrame().GetWorkWindow_Impl()->SetStatusBar_Impl(eStatBarId); + } +} + +/** Helper method to execute the outstanding push and pop commands. +*/ +void SfxDispatcher::FlushImpl() +{ + SFX_STACK(SfxDispatcher::FlushImpl); + + SAL_INFO("sfx.control", "Flushing dispatcher!"); + + xImp->aIdle.Stop(); + + xImp->bFlushing = !xImp->bFlushing; + if ( !xImp->bFlushing ) + { + xImp->bFlushing = true; + return; + } + + SfxApplication *pSfxApp = SfxGetpApp(); + + // Re-build the true stack in the first round + std::deque<SfxToDo_Impl> aToDoCopy; + bool bModify = false; + for(std::deque<SfxToDo_Impl>::reverse_iterator i = xImp->aToDoStack.rbegin(); i != xImp->aToDoStack.rend(); ++i) + { + bModify = true; + + if(i->bPush) + { + // Actually push + DBG_ASSERT( std::find(xImp->aStack.begin(), xImp->aStack.end(), i->pCluster) == xImp->aStack.end(), + "pushed SfxShell already on stack" ); + xImp->aStack.push_back(i->pCluster); + i->pCluster->SetDisableFlags(xImp->nDisableFlags); + + // Mark the moved shell + aToDoCopy.push_front(*i); + } + else + { + // Actually pop + SfxShell* pPopped = nullptr; + bool bFound = false; + do + { + DBG_ASSERT( !xImp->aStack.empty(), "popping from empty stack" ); + pPopped = xImp->aStack.back(); + xImp->aStack.pop_back(); + pPopped->SetDisableFlags( SfxDisableFlags::NONE ); + bFound = (pPopped == i->pCluster); + + // Mark the moved Shell + aToDoCopy.push_front(SfxToDo_Impl(false, i->bDelete, false, *pPopped)); + } + while(i->bUntil && !bFound); + DBG_ASSERT( bFound, "wrong SfxShell popped" ); + } + } + xImp->aToDoStack.clear(); + + // Invalidate bindings, if possible + if ( !pSfxApp->IsDowning() ) + { + InvalidateBindings_Impl( bModify ); + } + + xImp->bFlushing = false; + xImp->bUpdated = false; // not only when bModify, if Doc/Template-Config + xImp->bFlushed = true; + SAL_INFO("sfx.control", "Successfully flushed dispatcher!"); + + //fdo#70703 FlushImpl may call back into itself so use aToDoCopyStack to talk + //to outer levels of ourself. If DoActivate_Impl/DoDeactivate_Impl deletes + //an entry, then they will walk back up aToDoCopyStack and set outer + //levels's entries to bDeleted + xImp->aToDoCopyStack.push_back(aToDoCopy); + std::deque<SfxToDo_Impl>& rToDoCopy = xImp->aToDoCopyStack.back(); + // Activate the Shells and possible delete them in the 2nd round + for(std::deque<SfxToDo_Impl>::reverse_iterator i = rToDoCopy.rbegin(); i != rToDoCopy.rend(); ++i) + { + if (i->bDeleted) + continue; + if (!xImp->bActive) + continue; + if (i->bPush) + i->pCluster->DoActivate_Impl(xImp->pFrame, true); + else + i->pCluster->DoDeactivate_Impl(xImp->pFrame, true); + } + + aToDoCopy = xImp->aToDoCopyStack.back(); + xImp->aToDoCopyStack.pop_back(); + + for(std::deque<SfxToDo_Impl>::reverse_iterator i = aToDoCopy.rbegin(); i != aToDoCopy.rend(); ++i) + { + if (i->bDelete && !i->bDeleted) + { + if (!xImp->aToDoCopyStack.empty()) + { + //fdo#70703 if there is an outer FlushImpl then inform it that + //we have deleted this cluster + for (auto & elem : xImp->aToDoCopyStack) + { + for (auto & subelem : elem) + { + if (subelem.pCluster == i->pCluster) + subelem.bDeleted = true; + } + } + } + delete i->pCluster; + } + } + bool bAwakeBindings = !aToDoCopy.empty(); + if( bAwakeBindings ) + aToDoCopy.clear(); + + // If more changes have occurred on the stack when + // Activate/Deactivate/Delete: + if (!xImp->bFlushed) + // If Push/Pop has been called by someone, then also EnterReg was called! + FlushImpl(); + + if( bAwakeBindings && GetBindings() ) + GetBindings()->DLEAVEREGISTRATIONS(); + + for (SfxObjectBars_Impl & rFixedObjBar : xImp->aFixedObjBars) + rFixedObjBar.eId = ToolbarId::None; + + SAL_INFO("sfx.control", "SfxDispatcher(" << this << ")::Flush() done"); +} + +/** With this method a filter set, the target slots can be enabled or disabled. + The passed array must be retained until the destructor or the next + <SetSlotFilter()>, it is not deleted from the dispatcher, so it can thus be + static. + + In read-only documents the quasi ReadOnlyDoc Flag of slots can be + overturned by the use of 'bEnable == 2', so this will be displayed again. + On the other slots it has no effect. + + // HACK(here should be used an enum) ??? + @param nEnable 1==true: only enable specified slots, disable all other + 0==false: disable specified slots, first enable all other + @param nCount Number of SIDs in the following Array + @param pSIDs sorted Array of 'nCount' SIDs + + [Example] + + Targeted disabling of Slots 1, 2 and 3: + + static sal_uInt16 const pSIDs[] = { 1, 2, 3 }; + pDisp->SetSlotFilter( sal_False, sizeof(pSIDs)/sizeof(sal_uInt16), pSIDs ); + + only permit Slots 5, 6 and 7: + + static sal_uInt16 const pSIDs[] = { 5, 6, 7 }; + pDisp->SetSlotFilter( sal_True, sizeof(pSIDs)/sizeof(sal_uInt16), pSIDs ); + + Turn-off Filter: + + pDisp->SetSlotFilter(); +*/ +void SfxDispatcher::SetSlotFilter(SfxSlotFilterState nEnable, + o3tl::span<sal_uInt16 const> pSIDs) +{ +#ifdef DBG_UTIL + // Check Array + for ( std::size_t n = 1; n < pSIDs.size(); ++n ) + DBG_ASSERT( pSIDs[n] > pSIDs[n-1], "SetSlotFilter: SIDs not sorted" ); +#endif + + xImp->nFilterEnabling = nEnable; + xImp->pFilterSIDs = pSIDs; + + GetBindings()->InvalidateAll(true); +} + +extern "C" { + +static int SfxCompareSIDs_Impl(const void* pSmaller, const void* pBigger) +{ + return static_cast<long>(*static_cast<sal_uInt16 const *>(pSmaller)) - static_cast<long>(*static_cast<sal_uInt16 const *>(pBigger)); +} + +} + +/** Searches for 'nSID' in the Filter set by <SetSlotFilter()> and + returns sal_True, if the SIDis allowed, or sal_False, if it is + disabled by the Filter. + + @return 0 => disabled + 1 => enabled + 2 => enabled even if ReadOnlyDoc +*/ +SfxSlotFilterState SfxDispatcher::IsSlotEnabledByFilter_Impl( sal_uInt16 nSID ) const +{ + // no filter? + if ( xImp->pFilterSIDs.empty() ) + // => all SIDs allowed + return SfxSlotFilterState::ENABLED; + + // search + bool bFound = nullptr != bsearch( &nSID, xImp->pFilterSIDs.data(), xImp->pFilterSIDs.size(), + sizeof(sal_uInt16), SfxCompareSIDs_Impl ); + + // even if ReadOnlyDoc + if ( SfxSlotFilterState::ENABLED_READONLY == xImp->nFilterEnabling ) + return bFound ? SfxSlotFilterState::ENABLED_READONLY : SfxSlotFilterState::ENABLED; + // Otherwise after Negative/Positive Filter + else if ( SfxSlotFilterState::ENABLED == xImp->nFilterEnabling ) + return bFound ? SfxSlotFilterState::ENABLED : SfxSlotFilterState::DISABLED; + else + return bFound ? SfxSlotFilterState::DISABLED : SfxSlotFilterState::ENABLED; +} + +/** This helper method searches for the <Slot-Server> which currently serves + the nSlot. As the result, rServe is filled accordingly. + + If known the SfxInterface which is currently served by nSlot can be + passed along. + + The SfxDispatcher is flushed while searching for nSlot. + + @param nSlot Slot-Id to search for + @param rServer <SfxSlotServer>-Instance to fill + + @return true + The Slot was found, rServer is valid. + + false + The Slot is currently not served, rServer is invalid. +*/ +bool SfxDispatcher::FindServer_(sal_uInt16 nSlot, SfxSlotServer& rServer) +{ + SFX_STACK(SfxDispatcher::FindServer_); + + // Dispatcher locked? (nevertheless let SID_HELP_PI through) + if ( IsLocked() ) + { + xImp->bInvalidateOnUnlock = true; + return false; + } + + // Count the number of Shells in the linked dispatchers. + Flush(); + sal_uInt16 nTotCount = xImp->aStack.size(); + + // Verb-Slot? + if (nSlot >= SID_VERB_START && nSlot <= SID_VERB_END) + { + for ( sal_uInt16 nShell = 0;; ++nShell ) + { + SfxShell *pSh = GetShell(nShell); + if ( pSh == nullptr ) + return false; + if ( dynamic_cast< const SfxViewShell *>( pSh ) != nullptr ) + { + const SfxSlot* pSlot = pSh->GetVerbSlot_Impl(nSlot); + if ( pSlot ) + { + rServer.SetShellLevel(nShell); + rServer.SetSlot( pSlot ); + return true; + } + } + } + } + + // SID check against set filter + SfxSlotFilterState nSlotEnableMode = SfxSlotFilterState::DISABLED; + if ( xImp->pFrame ) + { + nSlotEnableMode = IsSlotEnabledByFilter_Impl( nSlot ); + if ( SfxSlotFilterState::DISABLED == nSlotEnableMode ) + return false; + } + + // In Quiet-Mode only Parent-Dispatcher + if ( xImp->bQuiet ) + { + return false; + } + + bool bReadOnly = ( SfxSlotFilterState::ENABLED_READONLY != nSlotEnableMode && xImp->bReadOnly ); + + // search through all the shells of the chained dispatchers + // from top to bottom + sal_uInt16 nFirstShell = 0; + for ( sal_uInt16 i = nFirstShell; i < nTotCount; ++i ) + { + SfxShell *pObjShell = GetShell(i); + SfxInterface *pIFace = pObjShell->GetInterface(); + const SfxSlot *pSlot = pIFace->GetSlot(nSlot); + + if ( pSlot && pSlot->nDisableFlags != SfxDisableFlags::NONE && + ( static_cast<int>(pSlot->nDisableFlags) & static_cast<int>(pObjShell->GetDisableFlags()) ) != 0 ) + return false; + + if ( pSlot && !( pSlot->nFlags & SfxSlotMode::READONLYDOC ) && bReadOnly ) + return false; + + if ( pSlot ) + { + // Slot belongs to Container? + bool bIsContainerSlot = pSlot->IsMode(SfxSlotMode::CONTAINER); + bool bIsInPlace = xImp->pFrame && xImp->pFrame->GetObjectShell()->IsInPlaceActive(); + + // Shell belongs to Server? + // AppDispatcher or IPFrame-Dispatcher + bool bIsServerShell = !xImp->pFrame || bIsInPlace; + + // Of course ShellServer-Slots are also executable even when it is + // executed on a container dispatcher without an IPClient. + if ( !bIsServerShell ) + { + SfxViewShell *pViewSh = xImp->pFrame->GetViewShell(); + bIsServerShell = !pViewSh || !pViewSh->GetUIActiveClient(); + } + + // Shell belongs to Container? + // AppDispatcher or no IPFrameDispatcher + bool bIsContainerShell = !xImp->pFrame || !bIsInPlace; + // Shell and Slot match + if ( !( ( bIsContainerSlot && bIsContainerShell ) || + ( !bIsContainerSlot && bIsServerShell ) ) ) + pSlot = nullptr; + } + + if ( pSlot ) + { + rServer.SetSlot(pSlot); + rServer.SetShellLevel(i); + return true; + } + } + + return false; +} + +/** Helper method to obtain the status of the <Slot-Server>s rSvr. + The required slots IDs (partly converted to Which-IDs of the pool) + must be present in rstate. + + The SfxDispatcher is flushed before the query. + + @param rSvr Slot-Server to query + @param rState SfxItemSet to be filled + @param pRealSlot The actual Slot if possible +*/ +bool SfxDispatcher::FillState_(const SfxSlotServer& rSvr, SfxItemSet& rState, + const SfxSlot* pRealSlot) +{ + SFX_STACK(SfxDispatcher::FillState_); + + const SfxSlot *pSlot = rSvr.GetSlot(); + if ( pSlot && IsLocked() ) + { + xImp->bInvalidateOnUnlock = true; + return false; + } + + if ( pSlot ) + { + DBG_ASSERT(xImp->bFlushed, + "Dispatcher not flushed after retrieving slot servers!"); + if (!xImp->bFlushed) + return false; + + // Determine the object and call the Message of this object + SfxShell *pSh = GetShell(rSvr.GetShellLevel()); + DBG_ASSERT(pSh, "ObjectShell not found"); + + SfxStateFunc pFunc; + + if (pRealSlot) + pFunc = pRealSlot->GetStateFnc(); + else + pFunc = pSlot->GetStateFnc(); + + pSh->CallState( pFunc, rState ); +#ifdef DBG_UTIL + // To examine the conformity of IDL (SlotMap) and current Items + if ( rState.Count() ) + { + SfxInterface *pIF = pSh->GetInterface(); + SfxItemIter aIter( rState ); + for ( const SfxPoolItem *pItem = aIter.GetCurItem(); + pItem; + pItem = aIter.NextItem() ) + { + if ( !IsInvalidItem(pItem) && !pItem->IsVoidItem() ) + { + sal_uInt16 nSlotId = rState.GetPool()->GetSlotId(pItem->Which()); + SAL_INFO_IF( + typeid(pItem) != *pIF->GetSlot(nSlotId)->GetType()->Type(), + "sfx.control", + "item-type unequal to IDL (=> no BASIC) with SID: " + << nSlotId << " in " << pIF->GetClassName()); + } + } + } +#endif + + return true; + } + + return false; +} + +void SfxDispatcher::ExecutePopup( vcl::Window *pWin, const Point *pPos ) +{ + SfxDispatcher &rDisp = *SfxGetpApp()->GetDispatcher_Impl(); + sal_uInt16 nShLevel = 0; + SfxShell *pSh; + + if ( rDisp.xImp->bQuiet ) + nShLevel = rDisp.xImp->aStack.size(); + + for ( pSh = rDisp.GetShell(nShLevel); pSh; ++nShLevel, pSh = rDisp.GetShell(nShLevel) ) + { + const OUString& rResName = pSh->GetInterface()->GetPopupMenuName(); + if ( !rResName.isEmpty() ) + { + rDisp.ExecutePopup( rResName, pWin, pPos ); + return; + } + } +} + +void SfxDispatcher::ExecutePopup( const OUString& rResName, vcl::Window* pWin, const Point* pPos ) +{ + css::uno::Sequence< css::uno::Any > aArgs( 3 ); + aArgs[0] <<= comphelper::makePropertyValue( "Value", rResName ); + aArgs[1] <<= comphelper::makePropertyValue( "Frame", GetFrame()->GetFrame().GetFrameInterface() ); + aArgs[2] <<= comphelper::makePropertyValue( "IsContextMenu", true ); + + css::uno::Reference< css::uno::XComponentContext > xContext = comphelper::getProcessComponentContext(); + css::uno::Reference< css::frame::XPopupMenuController > xPopupController( + xContext->getServiceManager()->createInstanceWithArgumentsAndContext( + "com.sun.star.comp.framework.ResourceMenuController", aArgs, xContext ), css::uno::UNO_QUERY ); + + css::uno::Reference< css::awt::XPopupMenu > xPopupMenu( xContext->getServiceManager()->createInstanceWithContext( + "com.sun.star.awt.PopupMenu", xContext ), css::uno::UNO_QUERY ); + + if ( !xPopupController.is() || !xPopupMenu.is() ) + return; + + vcl::Window* pWindow = pWin ? pWin : xImp->pFrame->GetFrame().GetWorkWindow_Impl()->GetWindow(); + Point aPos = pPos ? *pPos : pWindow->GetPointerPosPixel(); + + css::ui::ContextMenuExecuteEvent aEvent; + aEvent.SourceWindow = VCLUnoHelper::GetInterface( pWindow ); + aEvent.ExecutePosition.X = aPos.X(); + aEvent.ExecutePosition.Y = aPos.Y(); + + xPopupController->setPopupMenu( xPopupMenu ); + VCLXMenu* pAwtMenu = comphelper::getUnoTunnelImplementation<VCLXMenu>( xPopupMenu ); + PopupMenu* pVCLMenu = static_cast< PopupMenu* >( pAwtMenu->GetMenu() ); + if (comphelper::LibreOfficeKit::isActive()) + { + boost::property_tree::ptree aMenu = fillPopupMenu(pVCLMenu); + boost::property_tree::ptree aRoot; + aRoot.add_child("menu", aMenu); + + std::stringstream aStream; + boost::property_tree::write_json(aStream, aRoot, true); + if (SfxViewShell* pViewShell = xImp->pFrame->GetViewShell()) + pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_CONTEXT_MENU, aStream.str().c_str()); + } + else + { + OUString aMenuURL = "private:resource/popupmenu/" + rResName; + if (pVCLMenu && GetFrame()->GetViewShell()->TryContextMenuInterception(*pVCLMenu, aMenuURL, aEvent)) + { + pVCLMenu->Execute(pWindow, aPos); + } + } + + css::uno::Reference< css::lang::XComponent > xComponent( xPopupController, css::uno::UNO_QUERY ); + if ( xComponent.is() ) + xComponent->dispose(); +} + +/** With this method the SfxDispatcher can be locked and released. A locked + SfxDispatcher does not perform <SfxRequest>s and does no longer provide + status information. It behaves as if all the slots were disabled. +*/ +void SfxDispatcher::Lock( bool bLock ) +{ + SfxBindings* pBindings = GetBindings(); + if ( !bLock && xImp->bLocked && xImp->bInvalidateOnUnlock ) + { + if ( pBindings ) + pBindings->InvalidateAll(true); + xImp->bInvalidateOnUnlock = false; + } + else if ( pBindings ) + pBindings->InvalidateAll(false); + xImp->bLocked = bLock; + if ( !bLock ) + { + for(size_t i = 0; i < xImp->aReqArr.size(); ++i) + xImp->xPoster->Post(std::move(xImp->aReqArr[i])); + xImp->aReqArr.clear(); + } +} + +ToolbarId SfxDispatcher::GetObjectBarId( sal_uInt16 nPos ) const +{ + return xImp->aObjBars[nPos].eId; +} + +void SfxDispatcher::HideUI( bool bHide ) +{ + bool bWasHidden = xImp->bNoUI; + xImp->bNoUI = bHide; + if ( xImp->pFrame ) + { + SfxViewFrame* pTop = xImp->pFrame->GetTopViewFrame(); + if ( pTop && pTop->GetBindings().GetDispatcher() == this ) + { + SfxFrame& rFrame = pTop->GetFrame(); + if ( rFrame.IsMenuBarOn_Impl() ) + { + css::uno::Reference < css::beans::XPropertySet > xPropSet( rFrame.GetFrameInterface(), css::uno::UNO_QUERY ); + if ( xPropSet.is() ) + { + css::uno::Reference< css::frame::XLayoutManager > xLayoutManager; + css::uno::Any aValue = xPropSet->getPropertyValue("LayoutManager"); + aValue >>= xLayoutManager; + if ( xLayoutManager.is() ) + xLayoutManager->setVisible( !bHide ); + } + } + } + } + + if ( bHide != bWasHidden ) + Update_Impl( true ); +} + +void SfxDispatcher::SetReadOnly_Impl( bool bOn ) +{ + xImp->bReadOnly = bOn; +} + +bool SfxDispatcher::GetReadOnly_Impl() const +{ + return xImp->bReadOnly; +} + +/** With 'bOn' the Dispatcher is quasi dead and transfers everything to the + Parent-Dispatcher. +*/ +void SfxDispatcher::SetQuietMode_Impl( bool bOn ) +{ + xImp->bQuiet = bOn; + SfxBindings* pBindings = GetBindings(); + if ( pBindings ) + pBindings->InvalidateAll(true); +} + +SfxItemState SfxDispatcher::QueryState( sal_uInt16 nSlot, const SfxPoolItem* &rpState ) +{ + SfxShell *pShell = nullptr; + const SfxSlot *pSlot = nullptr; + if ( GetShellAndSlot_Impl( nSlot, &pShell, &pSlot, false, true ) ) + { + rpState = pShell->GetSlotState(nSlot); + if ( !rpState ) + return SfxItemState::DISABLED; + else + return SfxItemState::DEFAULT; + } + + return SfxItemState::DISABLED; +} + +SfxItemState SfxDispatcher::QueryState( sal_uInt16 nSID, css::uno::Any& rAny ) +{ + SfxShell *pShell = nullptr; + const SfxSlot *pSlot = nullptr; + if ( GetShellAndSlot_Impl( nSID, &pShell, &pSlot, false, true ) ) + { + const SfxPoolItem* pItem = pShell->GetSlotState( nSID ); + if ( !pItem ) + return SfxItemState::DISABLED; + else + { + css::uno::Any aState; + if ( !pItem->IsVoidItem() ) + { + sal_uInt16 nSubId( 0 ); + SfxItemPool& rPool = pShell->GetPool(); + sal_uInt16 nWhich = rPool.GetWhich( nSID ); + if ( rPool.GetMetric( nWhich ) == MapUnit::MapTwip ) + nSubId |= CONVERT_TWIPS; + pItem->QueryValue( aState, static_cast<sal_uInt8>(nSubId) ); + } + rAny = aState; + + return SfxItemState::DEFAULT; + } + } + + return SfxItemState::DISABLED; +} + +bool SfxDispatcher::IsReadOnlyShell_Impl( sal_uInt16 nShell ) const +{ + sal_uInt16 nShellCount = xImp->aStack.size(); + if ( nShell < nShellCount ) + { + SfxShell* pShell = *( xImp->aStack.rbegin() + nShell ); + if( dynamic_cast< const SfxModule *>( pShell ) != nullptr || dynamic_cast< const SfxApplication *>( pShell ) != nullptr || dynamic_cast< const SfxViewFrame *>( pShell ) != nullptr ) + return false; + else + return xImp->bReadOnly; + } + return true; +} + +void SfxDispatcher::RemoveShell_Impl( SfxShell& rShell ) +{ + Flush(); + + sal_uInt16 nCount = xImp->aStack.size(); + for ( sal_uInt16 n=0; n<nCount; ++n ) + { + if ( xImp->aStack[n] == &rShell ) + { + xImp->aStack.erase( xImp->aStack.begin() + n ); + rShell.SetDisableFlags( SfxDisableFlags::NONE ); + rShell.DoDeactivate_Impl(xImp->pFrame, true); + break; + } + } + + if ( !SfxGetpApp()->IsDowning() ) + { + xImp->bUpdated = false; + InvalidateBindings_Impl(true); + } +} + +void SfxDispatcher::InvalidateBindings_Impl( bool bModify ) +{ + // App-Dispatcher? + if ( IsAppDispatcher() ) + { + for ( SfxViewFrame *pFrame = SfxViewFrame::GetFirst(); + pFrame; + pFrame = SfxViewFrame::GetNext( *pFrame ) ) + pFrame->GetBindings().InvalidateAll(bModify); + } + else + { + SfxDispatcher *pDisp = GetBindings()->GetDispatcher_Impl(); + if ( pDisp == this ) + { + GetBindings()->InvalidateAll( bModify ); + } + } +} + +bool SfxDispatcher::IsUpdated_Impl() const +{ + return xImp->bUpdated; +} + +void SfxDispatcher::SetDisableFlags( SfxDisableFlags nFlags ) +{ + xImp->nDisableFlags = nFlags; + for ( SfxShellStack_Impl::reverse_iterator it = xImp->aStack.rbegin(); it != xImp->aStack.rend(); ++it ) + (*it)->SetDisableFlags( nFlags ); +} + +SfxDisableFlags SfxDispatcher::GetDisableFlags() const +{ + return xImp->nDisableFlags; +} + +SfxModule* SfxDispatcher::GetModule() const +{ + for ( sal_uInt16 nShell = 0;; ++nShell ) + { + SfxShell *pSh = GetShell(nShell); + if ( pSh == nullptr ) + return nullptr; + if ( dynamic_cast< const SfxModule *>( pSh ) != nullptr ) + return static_cast<SfxModule*>(pSh); + } +} + +boost::property_tree::ptree SfxDispatcher::fillPopupMenu(Menu* pMenu) +{ + // Activate this menu first + pMenu->HandleMenuActivateEvent(pMenu); + pMenu->HandleMenuDeActivateEvent(pMenu); + + boost::property_tree::ptree aTree; + // If last item inserted is some valid text + bool bIsLastItemText = false; + sal_uInt16 nCount = pMenu->GetItemCount(); + for (sal_uInt16 nPos = 0; nPos < nCount; nPos++) + { + boost::property_tree::ptree aItemTree; + const MenuItemType aItemType = pMenu->GetItemType(nPos); + + if (aItemType == MenuItemType::DONTKNOW) + continue; + + if (aItemType == MenuItemType::SEPARATOR) + { + if (bIsLastItemText) + aItemTree.put("type", "separator"); + bIsLastItemText = false; + } + else + { + const sal_uInt16 nItemId = pMenu->GetItemId(nPos); + OUString aCommandURL = pMenu->GetItemCommand(nItemId); + + if (aCommandURL.isEmpty()) + { + const SfxSlot *pSlot = SFX_SLOTPOOL().GetSlot(nItemId); + if (pSlot) + aCommandURL = pSlot->GetCommandString(); + } + + const OUString aItemText = pMenu->GetItemText(nItemId); + Menu* pPopupSubmenu = pMenu->GetPopupMenu(nItemId); + + if (!aItemText.isEmpty()) + aItemTree.put("text", aItemText.toUtf8().getStr()); + + if (pPopupSubmenu) + { + boost::property_tree::ptree aSubmenu = fillPopupMenu(pPopupSubmenu); + if (aSubmenu.empty()) + continue; + + aItemTree.put("type", "menu"); + if (!aCommandURL.isEmpty()) + aItemTree.put("command", aCommandURL.toUtf8().getStr()); + aItemTree.push_back(std::make_pair("menu", aSubmenu)); + } + else + { + // no point in exposing choices that don't have the .uno: + // command + if (aCommandURL.isEmpty()) + continue; + + aItemTree.put("type", "command"); + aItemTree.put("command", aCommandURL.toUtf8().getStr()); + } + + aItemTree.put("enabled", pMenu->IsItemEnabled(nItemId)); + + MenuItemBits aItemBits = pMenu->GetItemBits(nItemId); + bool bHasChecks = true; + if (aItemBits & MenuItemBits::CHECKABLE) + aItemTree.put("checktype", "checkmark"); + else if (aItemBits & MenuItemBits::RADIOCHECK) + aItemTree.put("checktype", "radio"); + else if (aItemBits & MenuItemBits::AUTOCHECK) + aItemTree.put("checktype", "auto"); + else + bHasChecks = false; + + if (bHasChecks) + aItemTree.put("checked", pMenu->IsItemChecked(nItemId)); + } + + if (!aItemTree.empty()) + { + aTree.push_back(std::make_pair("", aItemTree)); + if (aItemType != MenuItemType::SEPARATOR) + bIsLastItemText = true; + } + } + + return aTree; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/emojicontrol.cxx b/sfx2/source/control/emojicontrol.cxx new file mode 100644 index 000000000..b2961c35d --- /dev/null +++ b/sfx2/source/control/emojicontrol.cxx @@ -0,0 +1,180 @@ +/* -*- 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 . + */ + +#include <emojicontrol.hxx> +#include <sfx2/emojipopup.hxx> +#include <emojiview.hxx> +#include <thumbnailviewitem.hxx> +#include <rtl/ustrbuf.hxx> +#include <vcl/tabpage.hxx> +#include <comphelper/propertysequence.hxx> +#include <comphelper/dispatchcommand.hxx> +#include <officecfg/Office/Common.hxx> + +const char FILTER_PEOPLE[] = "people"; +const char FILTER_NATURE[] = "nature"; +const char FILTER_FOOD[] = "food"; +const char FILTER_ACTIVITY[] = "activity"; +const char FILTER_TRAVEL[] = "travel"; +const char FILTER_OBJECTS[] = "objects"; +const char FILTER_SYMBOLS[] = "symbols"; +const char FILTER_FLAGS[] = "flags"; +const char FILTER_UNICODE9[] = "unicode9"; + +using namespace com::sun::star; + +SfxEmojiControl::SfxEmojiControl(EmojiPopup* pControl, vcl::Window* pParent) + : ToolbarPopup(pControl->getFrameInterface(), pParent, "emojictrl", "sfx/ui/emojicontrol.ui") +{ + get(mpTabControl, "tabcontrol"); + get(mpEmojiView, "emoji_view"); + + sal_uInt16 nCurPageId = mpTabControl->GetPageId(FILTER_PEOPLE); + TabPage *pTabPage = mpTabControl->GetTabPage(nCurPageId); + ConvertLabelToUnicode(nCurPageId); + pTabPage->Show(); + + nCurPageId = mpTabControl->GetPageId(FILTER_NATURE); + mpTabControl->SetTabPage(nCurPageId, pTabPage); + ConvertLabelToUnicode(nCurPageId); + pTabPage->Show(); + + nCurPageId = mpTabControl->GetPageId(FILTER_FOOD); + mpTabControl->SetTabPage(nCurPageId, pTabPage); + ConvertLabelToUnicode(nCurPageId); + pTabPage->Show(); + + nCurPageId = mpTabControl->GetPageId(FILTER_ACTIVITY); + mpTabControl->SetTabPage(nCurPageId, pTabPage); + ConvertLabelToUnicode(nCurPageId); + pTabPage->Show(); + + nCurPageId = mpTabControl->GetPageId(FILTER_TRAVEL); + mpTabControl->SetTabPage(nCurPageId, pTabPage); + ConvertLabelToUnicode(nCurPageId); + pTabPage->Show(); + + nCurPageId = mpTabControl->GetPageId(FILTER_OBJECTS); + mpTabControl->SetTabPage(nCurPageId, pTabPage); + ConvertLabelToUnicode(nCurPageId); + pTabPage->Show(); + + nCurPageId = mpTabControl->GetPageId(FILTER_SYMBOLS); + mpTabControl->SetTabPage(nCurPageId, pTabPage); + ConvertLabelToUnicode(nCurPageId); + pTabPage->Show(); + + nCurPageId = mpTabControl->GetPageId(FILTER_FLAGS); + mpTabControl->SetTabPage(nCurPageId, pTabPage); + ConvertLabelToUnicode(nCurPageId); + pTabPage->Show(); + + nCurPageId = mpTabControl->GetPageId(FILTER_UNICODE9); + mpTabControl->SetTabPage(nCurPageId, pTabPage); + ConvertLabelToUnicode(nCurPageId); + + vcl::Font rFont = mpTabControl->GetControlFont(); + rFont.SetFontHeight(TAB_FONT_SIZE); + mpTabControl->SetControlFont(rFont); + pTabPage->Show(); + + mpEmojiView->SetStyle(mpEmojiView->GetStyle() | WB_VSCROLL); + mpEmojiView->setItemMaxTextLength(ITEM_MAX_TEXT_LENGTH); + mpEmojiView->setItemDimensions(ITEM_MAX_WIDTH, 0, ITEM_MAX_HEIGHT, ITEM_PADDING); + + mpEmojiView->Populate(); + mpEmojiView->filterItems(ViewFilter_Category(FILTER_CATEGORY::PEOPLE)); + + mpEmojiView->setInsertEmojiHdl(LINK(this, SfxEmojiControl, InsertHdl)); + mpEmojiView->Show(); + mpEmojiView->ShowTooltips(true); + + mpTabControl->SetActivatePageHdl(LINK(this, SfxEmojiControl, ActivatePageHdl)); +} + +SfxEmojiControl::~SfxEmojiControl() +{ + disposeOnce(); +} + +void SfxEmojiControl::dispose() +{ + mpTabControl.clear(); + mpEmojiView.clear(); + + ToolbarPopup::dispose(); +} + +void SfxEmojiControl::ConvertLabelToUnicode(sal_uInt16 nPageId) +{ + OUStringBuffer sHexText = ""; + OUString sLabel = mpTabControl->GetPageText(nPageId); + sHexText.appendUtf32(sLabel.toUInt32(16)); + mpTabControl->SetPageText(nPageId, sHexText.toString()); +} + +FILTER_CATEGORY SfxEmojiControl::getCurrentFilter() const +{ + const sal_uInt16 nCurPageId = mpTabControl->GetCurPageId(); + + if (nCurPageId == mpTabControl->GetPageId(FILTER_PEOPLE)) + return FILTER_CATEGORY::PEOPLE; + else if (nCurPageId == mpTabControl->GetPageId(FILTER_NATURE)) + return FILTER_CATEGORY::NATURE; + else if (nCurPageId == mpTabControl->GetPageId(FILTER_FOOD)) + return FILTER_CATEGORY::FOOD; + else if (nCurPageId == mpTabControl->GetPageId(FILTER_ACTIVITY)) + return FILTER_CATEGORY::ACTIVITY; + else if (nCurPageId == mpTabControl->GetPageId(FILTER_TRAVEL)) + return FILTER_CATEGORY::TRAVEL; + else if (nCurPageId == mpTabControl->GetPageId(FILTER_OBJECTS)) + return FILTER_CATEGORY::OBJECTS; + else if (nCurPageId == mpTabControl->GetPageId(FILTER_SYMBOLS)) + return FILTER_CATEGORY::SYMBOLS; + else if (nCurPageId == mpTabControl->GetPageId(FILTER_FLAGS)) + return FILTER_CATEGORY::FLAGS; + else if (nCurPageId == mpTabControl->GetPageId(FILTER_UNICODE9)) + return FILTER_CATEGORY::UNICODE9; + + return FILTER_CATEGORY::PEOPLE; +} + +IMPL_LINK_NOARG(SfxEmojiControl, ActivatePageHdl, TabControl*, void) +{ + mpEmojiView->filterItems(ViewFilter_Category(getCurrentFilter())); +} + +IMPL_STATIC_LINK(SfxEmojiControl, InsertHdl, ThumbnailViewItem*, pItem, void) +{ + const OUString& sHexText = pItem->getTitle(); + sal_uInt32 cEmojiChar = sHexText.toUInt32(16); + + uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() ); + OUString sFontName(officecfg::Office::Common::Misc::EmojiFont::get(xContext)); + + uno::Sequence<beans::PropertyValue> aArgs( comphelper::InitPropertySequence({ + { "Symbols", uno::Any(OUString(&cEmojiChar, 1)) }, + // add font settings here + { "FontName", uno::Any(sFontName) } + })); + + comphelper::dispatchCommand(".uno:InsertSymbol", aArgs); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/emojipopup.cxx b/sfx2/source/control/emojipopup.cxx new file mode 100644 index 000000000..2fe9173ea --- /dev/null +++ b/sfx2/source/control/emojipopup.cxx @@ -0,0 +1,65 @@ +/* -*- 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 . + */ +#include <sfx2/emojipopup.hxx> +#include <emojicontrol.hxx> +#include <vcl/toolbox.hxx> + +EmojiPopup::EmojiPopup(const css::uno::Reference<css::uno::XComponentContext>& rContext) + : PopupWindowController(rContext, nullptr, OUString()) +{ +} + +void EmojiPopup::initialize( const css::uno::Sequence< css::uno::Any >& rArguments ) +{ + PopupWindowController::initialize(rArguments); + + ToolBox* pToolBox = nullptr; + sal_uInt16 nId = 0; + if (getToolboxId(nId, &pToolBox) && pToolBox->GetItemCommand(nId) == m_aCommandURL) + pToolBox->SetItemBits(nId, ToolBoxItemBits::DROPDOWNONLY | pToolBox->GetItemBits(nId)); +} + +EmojiPopup::~EmojiPopup() +{ +} + +VclPtr<vcl::Window> EmojiPopup::createVclPopupWindow(vcl::Window* pParent) +{ + return VclPtr<SfxEmojiControl>::Create(this, pParent); +} + +OUString EmojiPopup::getImplementationName() +{ + return "com.sun.star.comp.sfx2.InsertEmojiToolBoxControl"; +} + +css::uno::Sequence<OUString> EmojiPopup::getSupportedServiceNames() +{ + return { "com.sun.star.frame.ToolbarController" }; +} + +extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * +com_sun_star_comp_sfx2_InsertEmojiToolBoxControl_get_implementation( + css::uno::XComponentContext* rContext, + css::uno::Sequence<css::uno::Any> const & ) +{ + return cppu::acquire(new EmojiPopup(rContext)); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/emojiview.cxx b/sfx2/source/control/emojiview.cxx new file mode 100644 index 000000000..8bc65609d --- /dev/null +++ b/sfx2/source/control/emojiview.cxx @@ -0,0 +1,219 @@ +/* -*- 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/. + */ + +#include <emojiview.hxx> +#include <emojiviewitem.hxx> +#include <rtl/bootstrap.hxx> +#include <sal/log.hxx> +#include <config_folders.h> +#include <officecfg/Office/Common.hxx> +#include <comphelper/processfactory.hxx> +#include <vcl/event.hxx> + +#include <orcus/json_document_tree.hpp> +#include <orcus/config.hpp> +#include <orcus/pstring.hpp> +#include <string> +#include <fstream> + +#include <vcl/builderfactory.hxx> +using namespace ::com::sun::star; + +bool ViewFilter_Category::isFilteredCategory(FILTER_CATEGORY filter, const OUString &rCategory) +{ + bool bRet = true; + + if (filter == FILTER_CATEGORY::PEOPLE) + bRet = rCategory.match("people"); + else if (filter == FILTER_CATEGORY::NATURE) + bRet = rCategory.match("nature"); + else if (filter == FILTER_CATEGORY::FOOD) + bRet = rCategory.match("food"); + else if (filter == FILTER_CATEGORY::ACTIVITY) + bRet = rCategory.match("activity"); + else if (filter == FILTER_CATEGORY::TRAVEL) + bRet = rCategory.match("travel"); + else if (filter == FILTER_CATEGORY::OBJECTS) + bRet = rCategory.match("objects"); + else if (filter == FILTER_CATEGORY::SYMBOLS) + bRet = rCategory.match("symbols"); + else if (filter == FILTER_CATEGORY::FLAGS) + bRet = rCategory.match("flags"); + else if (filter == FILTER_CATEGORY::UNICODE9) + bRet = rCategory.match("unicode9"); + + return bRet; +} + +bool ViewFilter_Category::operator () (const ThumbnailViewItem *pItem) +{ + const EmojiViewItem *pViewItem = dynamic_cast<const EmojiViewItem*>(pItem); + if (pViewItem) + return isFilteredCategory(mCategory, pViewItem->getCategory()); + + return true; +} + +EmojiView::EmojiView (vcl::Window *pParent) + : ThumbnailView(pParent, WB_TABSTOP | WB_VSCROLL) +{ + // locate json data file + OUString sPath("$BRAND_BASE_DIR/" LIBO_SHARE_FOLDER "/emojiconfig/emoji.json"); + rtl::Bootstrap::expandMacros(sPath); + std::string strPath = OUStringToOString(sPath.copy(strlen("file://")), RTL_TEXTENCODING_UTF8).getStr(); + + std::ifstream file(strPath); + if(!file.is_open()) + return; + + msJSONData = std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); + if(msJSONData.empty()) + return; + + uno::Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext() ); + OUString sFontName(officecfg::Office::Common::Misc::EmojiFont::get(xContext)); + vcl::Font aFont = GetControlFont(); + aFont.SetFamilyName( sFontName ); + SetControlFont(aFont); +} + +VCL_BUILDER_FACTORY(EmojiView) + +EmojiView::~EmojiView() +{ + disposeOnce(); +} + +void EmojiView::Populate() +{ + if (msJSONData.empty()) + { + SAL_WARN("sfx", "Emoji config data is empty"); + return; + } + + // Populate view using the orcus json parser + using node = orcus::json::node; + + // default json config + orcus::json_config config; + + orcus::json::document_tree aEmojiInfo; + + // Load JSON string into a document tree. + aEmojiInfo.load(msJSONData, config); + + node root = aEmojiInfo.get_document_root(); + std::vector<orcus::pstring> keys = root.keys(); + + for (auto const& key : keys) + { + node value = root.child(key); + + if(value.type() == orcus::json::node_t::object) + { + // iterate each element to get the keys + std::vector<orcus::pstring> aEmojiParams = value.keys(); + OUString sTitle, sCategory, sName; + bool bDuplicate = false; + + for (auto const& emojiParam : aEmojiParams) + { + node prop = value.child(emojiParam); + + // get values of parameters in AppendItem() function + if(emojiParam == "unicode") + { + sTitle = OStringToOUString(OString( prop.string_value().get(), prop.string_value().size() ), RTL_TEXTENCODING_UTF8); + } + else if(emojiParam == "category") + { + sCategory = OStringToOUString(OString( prop.string_value().get(), prop.string_value().size() ), RTL_TEXTENCODING_UTF8); + } + else if(emojiParam == "name") + { + sName = OStringToOUString(OString( prop.string_value().get(), prop.string_value().size() ), RTL_TEXTENCODING_UTF8); + } + else if(emojiParam == "duplicate") + { + bDuplicate = true; + } + } + + // Don't append if a duplicate emoji + if(!bDuplicate) + { + AppendItem(sTitle, sCategory, sName); + } + } + } +} + +void EmojiView::ApplySettings(vcl::RenderContext& rRenderContext) +{ + ThumbnailView::ApplySettings(rRenderContext); + mpItemAttrs->aFontSize.setX(ITEM_MAX_WIDTH - 2*ITEM_PADDING); + mpItemAttrs->aFontSize.setY(ITEM_MAX_HEIGHT - 2*ITEM_PADDING); +} + +void EmojiView::MouseButtonDown( const MouseEvent& rMEvt ) +{ + GrabFocus(); + + if (rMEvt.IsLeft()) + { + size_t nPos = ImplGetItem(rMEvt.GetPosPixel()); + ThumbnailViewItem* pItem = ImplGetItem(nPos); + + if(pItem) + maInsertEmojiHdl.Call(pItem); + } +} + +void EmojiView::KeyInput( const KeyEvent& rKEvt ) +{ + vcl::KeyCode aKeyCode = rKEvt.GetKeyCode(); + + if(aKeyCode == ( KEY_MOD1 | KEY_A ) ) + { + for (ThumbnailViewItem* pItem : mFilteredItemList) + { + if (!pItem->isSelected()) + { + pItem->setSelection(true); + } + } + + if (IsReallyVisible() && IsUpdateMode()) + Invalidate(); + return; + } + + ThumbnailView::KeyInput(rKEvt); +} + +void EmojiView::setInsertEmojiHdl(const Link<ThumbnailViewItem*, void> &rLink) +{ + maInsertEmojiHdl = rLink; +} + +void EmojiView::AppendItem(const OUString &rTitle, const OUString &rCategory, const OUString &rName) +{ + std::unique_ptr<EmojiViewItem> pItem(new EmojiViewItem(*this, getNextItemId())); + + pItem->maTitle = rTitle; + pItem->setCategory(rCategory); + pItem->setHelpText(rName); + + ThumbnailView::AppendItem(std::move(pItem)); + + CalculateItemPositions(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/emojiviewitem.cxx b/sfx2/source/control/emojiviewitem.cxx new file mode 100644 index 000000000..4ee1a55e4 --- /dev/null +++ b/sfx2/source/control/emojiviewitem.cxx @@ -0,0 +1,86 @@ +/* -*- 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/. + */ + +#include <emojiviewitem.hxx> + +#include <basegfx/matrix/b2dhommatrixtools.hxx> +#include <drawinglayer/primitive2d/PolyPolygonSelectionPrimitive2D.hxx> +#include <drawinglayer/primitive2d/textlayoutdevice.hxx> +#include <drawinglayer/processor2d/baseprocessor2d.hxx> +#include <com/sun/star/lang/Locale.hpp> +#include <rtl/ustrbuf.hxx> +#include <tools/poly.hxx> + +using namespace basegfx; +using namespace basegfx::utils; +using namespace drawinglayer::attribute; +using namespace drawinglayer::primitive2d; + +EmojiViewItem::EmojiViewItem (ThumbnailViewBase &rView, sal_uInt16 nId) + : ThumbnailViewItem(rView, nId) +{ +} + +EmojiViewItem::~EmojiViewItem () +{ +} + + +void EmojiViewItem::calculateItemsPosition (const long /*nThumbnailHeight*/, + const long /*nPadding*/, sal_uInt32 nMaxTextLength, + const ThumbnailItemAttributes *pAttrs) +{ + drawinglayer::primitive2d::TextLayouterDevice aTextDev; + aTextDev.setFontAttribute(pAttrs->aFontAttr, + pAttrs->aFontSize.getX(), pAttrs->aFontSize.getY(), + css::lang::Locale() ); + + Size aRectSize = maDrawArea.GetSize(); + Point aPos = maDrawArea.TopLeft(); + + // Calculate text position + aPos.setY( maDrawArea.getY() + (aRectSize.Height() - aTextDev.getTextHeight())/3 ); + aPos.setX( maDrawArea.Left() + (aRectSize.Width() - aTextDev.getTextWidth(maTitle,0,nMaxTextLength))/2 ); + maTextPos = aPos; +} + + +void EmojiViewItem::Paint(drawinglayer::processor2d::BaseProcessor2D *pProcessor, + const ThumbnailItemAttributes *pAttrs) +{ + BColor aFillColor = pAttrs->aFillColor; + + drawinglayer::primitive2d::Primitive2DContainer aSeq(2); + double fTransparence = 0.0; + + // Draw background + if( mbSelected && mbHover) + aFillColor = pAttrs->aSelectHighlightColor; + else if (mbSelected || mbHover) + aFillColor = pAttrs->aHighlightColor; + + if (mbHover) + fTransparence = pAttrs->fHighlightTransparence; + + aSeq[0] = drawinglayer::primitive2d::Primitive2DReference( + new PolyPolygonSelectionPrimitive2D( B2DPolyPolygon(::tools::Polygon(maDrawArea,5,5).getB2DPolygon()), + aFillColor, + fTransparence, + 0.0, + true)); + + OUStringBuffer sHexText = ""; + sHexText.appendUtf32(maTitle.toUInt32(16)); + + addTextPrimitives(sHexText.toString(), pAttrs, maTextPos, aSeq); + + pProcessor->process(aSeq); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/itemdel.cxx b/sfx2/source/control/itemdel.cxx new file mode 100644 index 000000000..756133c47 --- /dev/null +++ b/sfx2/source/control/itemdel.cxx @@ -0,0 +1,84 @@ +/* -*- 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 . + */ + +#include <sal/config.h> + +#include <itemdel.hxx> +#include <svl/poolitem.hxx> +#include <vcl/idle.hxx> + +#include <tools/debug.hxx> + +class SfxItemDisruptor_Impl +{ + std::unique_ptr<SfxPoolItem> pItem; + Idle m_Idle; + +private: + DECL_LINK( Delete, Timer*, void ); + +public: + explicit SfxItemDisruptor_Impl(std::unique_ptr<SfxPoolItem> pItemToDesrupt); + void LaunchDeleteOnIdle(); + ~SfxItemDisruptor_Impl(); + SfxItemDisruptor_Impl(const SfxItemDisruptor_Impl&) = delete; + SfxItemDisruptor_Impl& operator=(const SfxItemDisruptor_Impl&) = delete; +}; + +SfxItemDisruptor_Impl::SfxItemDisruptor_Impl(std::unique_ptr<SfxPoolItem> pItemToDisrupt) + : pItem(std::move(pItemToDisrupt)) + , m_Idle("sfx SfxItemDisruptor_Impl::Delete") +{ + m_Idle.SetInvokeHandler(LINK(this, SfxItemDisruptor_Impl, Delete)); + m_Idle.SetPriority(TaskPriority::DEFAULT_IDLE); + m_Idle.SetDebugName("sfx::SfxItemDisruptor_Impl m_Idle"); + + DBG_ASSERT( 0 == pItem->GetRefCount(), "disrupting pooled item" ); + pItem->SetKind(SfxItemKind::DeleteOnIdle); +} + +void SfxItemDisruptor_Impl::LaunchDeleteOnIdle() +{ + m_Idle.Start(); +} + +SfxItemDisruptor_Impl::~SfxItemDisruptor_Impl() +{ + m_Idle.Stop(); + + // reset RefCount (was set to SFX_ITEMS_SPECIAL before!) + pItem->SetRefCount( 0 ); + + pItem.reset(); +} + +IMPL_LINK_NOARG(SfxItemDisruptor_Impl, Delete, Timer*, void) +{ + delete this; +} + +void DeleteItemOnIdle(std::unique_ptr<SfxPoolItem> pItem) +{ + DBG_ASSERT( 0 == pItem->GetRefCount(), "deleting item in use" ); + SfxItemDisruptor_Impl *pDesruptor = new SfxItemDisruptor_Impl(std::move(pItem)); + pDesruptor->LaunchDeleteOnIdle(); + // coverity[leaked_storage] - pDesruptor takes care of its own destruction at idle time +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/minfitem.cxx b/sfx2/source/control/minfitem.cxx new file mode 100644 index 000000000..e14b827bd --- /dev/null +++ b/sfx2/source/control/minfitem.cxx @@ -0,0 +1,71 @@ +/* -*- 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 . + */ + +#include <sfx2/minfitem.hxx> +#include <sal/log.hxx> + +SfxPoolItem* SfxMacroInfoItem::CreateDefault() { SAL_WARN( "sfx", "No SfxMacroInfItem factory available"); return nullptr; } + + +SfxMacroInfoItem::SfxMacroInfoItem( + sal_uInt16 nWhichId, // Slot-ID + const BasicManager* pMgr, + const OUString &rLibName, + const OUString &rModuleName, + const OUString &rMethodName, + const OUString &rComment) : + SfxPoolItem(nWhichId), + pBasicManager(pMgr), + aLibName(rLibName), + aModuleName(rModuleName), + aMethodName(rMethodName), + aCommentText(rComment) +{ +} + +// op == + +bool SfxMacroInfoItem::operator==( const SfxPoolItem& rCmp) const +{ + const SfxMacroInfoItem rItem = static_cast<const SfxMacroInfoItem&>(rCmp); + return SfxPoolItem::operator==(rCmp) && + pBasicManager == rItem.pBasicManager && + aLibName == rItem.aLibName && + aModuleName == rItem.aModuleName && + aMethodName == rItem.aMethodName && + aCommentText == rItem.aCommentText; +} + +SfxMacroInfoItem* SfxMacroInfoItem::Clone( SfxItemPool *) const +{ + return new SfxMacroInfoItem(*this); +} + +OUString SfxMacroInfoItem::GetQualifiedName() const +{ + OUString aMacroName = aLibName + + "." + + aModuleName + + "." + + aMethodName; + return aMacroName; +} + + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/msg.cxx b/sfx2/source/control/msg.cxx new file mode 100644 index 000000000..852501c34 --- /dev/null +++ b/sfx2/source/control/msg.cxx @@ -0,0 +1,56 @@ +/* -*- 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 . + */ + +#include <svl/itempool.hxx> +#include <sfx2/msg.hxx> + +#include <climits> + +SfxSlotKind SfxSlot::GetKind() const +{ + if( !nMasterSlotId && !nValue) + return SfxSlotKind::Standard; + if ( nMasterSlotId && fnExec==nullptr && fnState==nullptr ) + { + assert(false); + return SfxSlotKind::Standard; + } + else + return SfxSlotKind::Attribute; +} + + +sal_uInt16 SfxSlot::GetWhich( const SfxItemPool &rPool ) const +{ + if ( !nMasterSlotId || nMasterSlotId == USHRT_MAX ) + const_cast<SfxSlot*>(this) -> nMasterSlotId = rPool.GetWhich(nSlotId); + return nMasterSlotId; +} + +OString SfxSlot::GetCommand() const +{ + return OStringLiteral(".uno:") + pUnoName; +} + +OUString SfxSlot::GetCommandString() const +{ + return OStringToOUString(GetCommand(), RTL_TEXTENCODING_UTF8); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/msgpool.cxx b/sfx2/source/control/msgpool.cxx new file mode 100644 index 000000000..28a1c8ba8 --- /dev/null +++ b/sfx2/source/control/msgpool.cxx @@ -0,0 +1,336 @@ +/* -*- 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 . + */ + +#include <sal/log.hxx> +#include <osl/diagnose.h> + +// due to pSlotPool +#include <appdata.hxx> +#include <sfx2/msgpool.hxx> +#include <sfx2/msg.hxx> +#include <sfx2/app.hxx> +#include <sfx2/objface.hxx> +#include <sfx2/sfxresid.hxx> +#include <sfx2/module.hxx> + +#include <sfx2/strings.hrc> + +SfxSlotPool::SfxSlotPool(SfxSlotPool *pParent) + : _pParentPool( pParent ) + , _nCurGroup(0) + , _nCurInterface(0) + , _nCurMsg(0) +{ +} + +SfxSlotPool::~SfxSlotPool() +{ + _pParentPool = nullptr; + // swap out _vInterfaces because ~SfxInterface() might call ReleaseInterface() + std::vector<SfxInterface*> tmpInterfaces; + tmpInterfaces.swap(_vInterfaces); + for ( SfxInterface *pIF : tmpInterfaces ) + delete pIF; +} + +namespace +{ + const char* getGidResId(SfxGroupId nId) + { + if (nId == SfxGroupId::Intern) + return STR_GID_INTERN; + else if (nId == SfxGroupId::Application) + return STR_GID_APPLICATION; + else if (nId == SfxGroupId::View) + return STR_GID_VIEW; + else if (nId == SfxGroupId::Document) + return STR_GID_DOCUMENT; + else if (nId == SfxGroupId::Edit) + return STR_GID_EDIT; + else if (nId == SfxGroupId::Macro) + return STR_GID_MACRO; + else if (nId == SfxGroupId::Options) + return STR_GID_OPTIONS; + else if (nId == SfxGroupId::Math) + return STR_GID_MATH; + else if (nId == SfxGroupId::Navigator) + return STR_GID_NAVIGATOR; + else if (nId == SfxGroupId::Insert) + return STR_GID_INSERT; + else if (nId == SfxGroupId::Format) + return STR_GID_FORMAT; + else if (nId == SfxGroupId::Template) + return STR_GID_TEMPLATE; + else if (nId == SfxGroupId::Text) + return STR_GID_TEXT; + else if (nId == SfxGroupId::Frame) + return STR_GID_FRAME; + else if (nId == SfxGroupId::Graphic) + return STR_GID_GRAPHIC; + else if (nId == SfxGroupId::Table) + return STR_GID_TABLE; + else if (nId == SfxGroupId::Enumeration) + return STR_GID_ENUMERATION; + else if (nId == SfxGroupId::Data) + return STR_GID_DATA; + else if (nId == SfxGroupId::Special) + return STR_GID_SPECIAL; + else if (nId == SfxGroupId::Image) + return STR_GID_IMAGE; + else if (nId == SfxGroupId::Chart) + return STR_GID_CHART; + else if (nId == SfxGroupId::Explorer) + return STR_GID_EXPLORER; + else if (nId == SfxGroupId::Connector) + return STR_GID_CONNECTOR; + else if (nId == SfxGroupId::Modify) + return STR_GID_MODIFY; + else if (nId == SfxGroupId::Drawing) + return STR_GID_DRAWING; + else if (nId == SfxGroupId::Controls) + return STR_GID_CONTROLS; + return nullptr; + } +} + +// registers the availability of the Interface of functions + +void SfxSlotPool::RegisterInterface( SfxInterface& rInterface ) +{ + // add to the list of SfxObjectInterface instances + _vInterfaces.push_back(&rInterface); + + // Stop at a (single) Null-slot (for syntactic reasons the interfaces + // always contain at least one slot) + if ( rInterface.Count() != 0 && !rInterface.pSlots[0].nSlotId ) + return; + + // possibly add Interface-id and group-ids of funcs to the list of groups + if ( _pParentPool ) + { + // The Groups in parent Slotpool are also known here + _vGroups.insert( _vGroups.end(), _pParentPool->_vGroups.begin(), _pParentPool->_vGroups.end() ); + } + + for ( size_t nFunc = 0; nFunc < rInterface.Count(); ++nFunc ) + { + SfxSlot &rDef = rInterface.pSlots[nFunc]; + if ( rDef.GetGroupId() != SfxGroupId::NONE && + std::find(_vGroups.begin(), _vGroups.end(), rDef.GetGroupId()) == _vGroups.end() ) + { + if (rDef.GetGroupId() == SfxGroupId::Intern) + _vGroups.insert(_vGroups.begin(), rDef.GetGroupId()); + else + _vGroups.push_back(rDef.GetGroupId()); + } + } +} + + +const std::type_info* SfxSlotPool::GetSlotType( sal_uInt16 nId ) const +{ + const SfxSlot* pSlot = GetSlot( nId ); + return pSlot ? pSlot->GetType()->Type() : nullptr; +} + + +// unregisters the availability of the Interface of functions + +void SfxSlotPool::ReleaseInterface( SfxInterface& rInterface ) +{ + // remove from the list of SfxInterface instances + auto i = std::find(_vInterfaces.begin(), _vInterfaces.end(), &rInterface); + if(i != _vInterfaces.end()) + _vInterfaces.erase(i); +} + +// get the first SfxMessage for a special Id (e.g. for getting check-mode) + +const SfxSlot* SfxSlotPool::GetSlot( sal_uInt16 nId ) const +{ + // First, search their own interfaces + for (SfxInterface* _pInterface : _vInterfaces) + { + const SfxSlot *pDef = _pInterface->GetSlot(nId); + if ( pDef ) + return pDef; + } + + // Then try any of the possible existing parent + return _pParentPool ? _pParentPool->GetSlot( nId ) : nullptr; +} + + +// skips to the next group + +OUString SfxSlotPool::SeekGroup( sal_uInt16 nNo ) +{ + // if the group exists, use it + if ( nNo < _vGroups.size() ) + { + _nCurGroup = nNo; + if ( _pParentPool ) + { + // In most cases, the order of the IDs agree + sal_uInt16 nParentCount = _pParentPool->_vGroups.size(); + if ( nNo < nParentCount && _vGroups[nNo] == _pParentPool->_vGroups[nNo] ) + _pParentPool->_nCurGroup = nNo; + else + { + // Otherwise search. If the group is not found in the parent + // pool, _nCurGroup is set outside the valid range + sal_uInt16 i; + for ( i=1; i<nParentCount; i++ ) + if ( _vGroups[nNo] == _pParentPool->_vGroups[i] ) + break; + _pParentPool->_nCurGroup = i; + } + } + + const char* pResId = getGidResId(_vGroups[_nCurGroup]); + if (!pResId) + { + OSL_FAIL( "GroupId-Name not defined in SFX!" ); + return OUString(); + } + + return SfxResId(pResId); + } + + return OUString(); +} + + +sal_uInt16 SfxSlotPool::GetGroupCount() const +{ + return _vGroups.size(); +} + + +// internal search loop + +const SfxSlot* SfxSlotPool::SeekSlot( sal_uInt16 nStartInterface ) +{ + // The numbering starts at the interfaces of the parent pool + sal_uInt16 nFirstInterface = _pParentPool ? _pParentPool->_vInterfaces.size() : 0; + + // have reached the end of the Parent-Pools? + if ( nStartInterface < nFirstInterface && + _pParentPool->_nCurGroup >= _pParentPool->_vGroups.size() ) + nStartInterface = nFirstInterface; + + // Is the Interface still in the Parent-Pool? + if ( nStartInterface < nFirstInterface ) + { + SAL_WARN_IF(!_pParentPool, "sfx.control", "No parent pool!"); + _nCurInterface = nStartInterface; + return _pParentPool->SeekSlot( nStartInterface ); + } + + // find the first func-def with the current group id + sal_uInt16 nCount = _vInterfaces.size() + nFirstInterface; + for ( _nCurInterface = nStartInterface; + _nCurInterface < nCount; + ++_nCurInterface ) + { + SfxInterface* pInterface = _vInterfaces[_nCurInterface-nFirstInterface]; + for ( _nCurMsg = 0; + _nCurMsg < pInterface->Count(); + ++_nCurMsg ) + { + const SfxSlot& rMsg = pInterface->pSlots[_nCurMsg]; + if (rMsg.GetGroupId() == _vGroups.at(_nCurGroup)) + return &rMsg; + } + } + + return nullptr; +} + + +// skips to the next func in the current group + +const SfxSlot* SfxSlotPool::NextSlot() +{ + // The numbering starts at the interfaces of the parent pool + sal_uInt16 nFirstInterface = _pParentPool ? _pParentPool->_vInterfaces.size() : 0; + + if ( _nCurInterface < nFirstInterface && _nCurGroup >= _pParentPool->_vGroups.size() ) + _nCurInterface = nFirstInterface; + + if ( _nCurInterface < nFirstInterface ) + { + SAL_WARN_IF(!_pParentPool, "sfx.control", "No parent pool!"); + const SfxSlot *pSlot = _pParentPool->NextSlot(); + _nCurInterface = _pParentPool->_nCurInterface; + if ( pSlot ) + return pSlot; + if ( _nCurInterface == nFirstInterface ) + // parent pool is ready + return SeekSlot( nFirstInterface ); + } + + sal_uInt16 nInterface = _nCurInterface - nFirstInterface; + // possibly we are already at the end + if ( nInterface >= _vInterfaces.size() ) + return nullptr; + + // look for further matching func-defs within the same Interface + SfxInterface* pInterface = _vInterfaces[nInterface]; + while ( ++_nCurMsg < pInterface->Count() ) + { + SfxSlot& rMsg = pInterface->pSlots[_nCurMsg]; + if (rMsg.GetGroupId() == _vGroups.at(_nCurGroup)) + return &rMsg; + } + + return SeekSlot(++_nCurInterface ); +} + + +// Query SlotName with help text + + +const SfxSlot* SfxSlotPool::GetUnoSlot( const OUString& rName ) const +{ + const SfxSlot *pSlot = nullptr; + for (auto const & nInterface: _vInterfaces) + { + pSlot = nInterface->GetSlot( rName ); + if ( pSlot ) + break; + } + + if ( !pSlot && _pParentPool ) + pSlot = _pParentPool->GetUnoSlot( rName ); + + return pSlot; +} + +SfxSlotPool& SfxSlotPool::GetSlotPool( SfxViewFrame *pFrame ) +{ + SfxModule *pMod = SfxModule::GetActiveModule( pFrame ); + if ( pMod && pMod->GetSlotPool() ) + return *pMod->GetSlotPool(); + else + return *SfxGetpApp()->Get_Impl()->pSlotPool; +} + + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/objface.cxx b/sfx2/source/control/objface.cxx new file mode 100644 index 000000000..d794d715a --- /dev/null +++ b/sfx2/source/control/objface.cxx @@ -0,0 +1,465 @@ +/* -*- 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 . + */ + +#include <assert.h> +#include <stdlib.h> + +#include <sal/log.hxx> + +#include <sfx2/module.hxx> +#include <sfx2/objface.hxx> +#include <sfx2/msg.hxx> +#include <sfx2/app.hxx> +#include <sfx2/msgpool.hxx> + +extern "C" { + +static int +SfxCompareSlots_qsort( const void* pSmaller, const void* pBigger ) +{ + return static_cast<int>(static_cast<SfxSlot const *>(pSmaller)->GetSlotId()) - + static_cast<int>(static_cast<SfxSlot const *>(pBigger)->GetSlotId()); +} + +static int +SfxCompareSlots_bsearch( const void* pSmaller, const void* pBigger ) +{ + return static_cast<int>(*static_cast<sal_uInt16 const *>(pSmaller)) - + static_cast<int>(static_cast<SfxSlot const *>(pBigger)->GetSlotId()); +} + +} + +namespace { + +struct SfxObjectUI_Impl +{ + sal_uInt16 nPos; + SfxVisibilityFlags nFlags; + sal_uInt32 nObjId; + bool bContext; + SfxShellFeature nFeature; + + SfxObjectUI_Impl(sal_uInt16 n, SfxVisibilityFlags f, sal_uInt32 nId, SfxShellFeature nFeat) : + nPos(n), + nFlags(f), + nObjId(nId), + bContext(false), + nFeature(nFeat) + { + } +}; + +} + +struct SfxInterface_Impl +{ + std::vector<std::unique_ptr<SfxObjectUI_Impl>> + aObjectBars; // registered ObjectBars + std::vector<std::unique_ptr<SfxObjectUI_Impl>> + aChildWindows; // registered ChildWindows + OUString aPopupName; // registered PopupMenu + StatusBarId eStatBarResId; // registered StatusBar + SfxModule* pModule; + bool bRegistered; + + SfxInterface_Impl() + : eStatBarResId(StatusBarId::None) + , pModule(nullptr) + , bRegistered(false) + { + } +}; + +static SfxObjectUI_Impl* CreateObjectBarUI_Impl(sal_uInt16 nPos, SfxVisibilityFlags nFlags, ToolbarId eId, SfxShellFeature nFeature); + +// constructor, registers a new unit +SfxInterface::SfxInterface( const char *pClassName, + bool bUsableSuperClass, + SfxInterfaceId nId, + const SfxInterface* pParent, + SfxSlot &rSlotMap, sal_uInt16 nSlotCount ): + pName(pClassName), + pGenoType(pParent), + nClassId(nId), + bSuperClass(bUsableSuperClass), + pImplData(new SfxInterface_Impl) +{ + SetSlotMap( rSlotMap, nSlotCount ); +} + +void SfxInterface::Register( SfxModule* pMod ) +{ + pImplData->bRegistered = true; + pImplData->pModule = pMod; + if ( pMod ) + pMod->GetSlotPool()->RegisterInterface(*this); + else + SfxGetpApp()->GetAppSlotPool_Impl().RegisterInterface(*this); +} + +void SfxInterface::SetSlotMap( SfxSlot& rSlotMap, sal_uInt16 nSlotCount ) +{ + pSlots = &rSlotMap; + nCount = nSlotCount; + SfxSlot* pIter = pSlots; + if ( 1 == nCount && !pIter->pNextSlot ) + pIter->pNextSlot = pIter; + + if ( !pIter->pNextSlot ) + { + // sort the SfxSlots by id + qsort( pSlots, nCount, sizeof(SfxSlot), SfxCompareSlots_qsort ); + + // link masters and slaves + sal_uInt16 nIter = 1; + for ( pIter = pSlots; nIter <= nCount; ++pIter, ++nIter ) + { + + assert( nIter == nCount || + pIter->GetSlotId() != (pIter+1)->GetSlotId() ); + + if ( nullptr == pIter->GetNextSlot() ) + { + // Slots referring in circle to the next with the same + // Status method. + SfxSlot *pLastSlot = pIter; + for ( sal_uInt16 n = nIter; n < Count(); ++n ) + { + SfxSlot *pCurSlot = pSlots+n; + if ( pCurSlot->GetStateFnc() == pIter->GetStateFnc() ) + { + pLastSlot->pNextSlot = pCurSlot; + pLastSlot = pCurSlot; + } + } + pLastSlot->pNextSlot = pIter; + } + } + } +#ifdef DBG_UTIL + else + { + sal_uInt16 nIter = 1; + for ( SfxSlot *pNext = pIter+1; nIter < nCount; ++pNext, ++nIter ) + { + + if ( pNext->GetSlotId() <= pIter->GetSlotId() ) + SAL_WARN( "sfx.control", "Wrong order" ); + + const SfxSlot *pCurSlot = pIter; + do + { + pCurSlot = pCurSlot->pNextSlot; + if ( pCurSlot->GetStateFnc() != pIter->GetStateFnc() ) + { + SAL_WARN("sfx.control", "Linked Slots with different State Methods : " + << pCurSlot->GetSlotId() + << " , " << pIter->GetSlotId() ); + } + } + while ( pCurSlot != pIter ); + + pIter = pNext; + } + } +#endif +} + + +SfxInterface::~SfxInterface() +{ + SfxModule *pMod = pImplData->pModule; + bool bRegistered = pImplData->bRegistered; + assert( bRegistered ); + if ( bRegistered ) + { + if ( pMod ) + { + // can return nullptr if we are called from the SfxSlotPool destructor + if (pMod->GetSlotPool()) + pMod->GetSlotPool()->ReleaseInterface(*this); + } + else + SfxGetpApp()->GetAppSlotPool_Impl().ReleaseInterface(*this); + } +} + + +// searches for the specified func + +const SfxSlot* SfxInterface::GetSlot( sal_uInt16 nFuncId ) const +{ + + assert( pSlots ); + assert( nCount ); + + // find the id using binary search + void* p = bsearch( &nFuncId, pSlots, nCount, sizeof(SfxSlot), + SfxCompareSlots_bsearch ); + if ( !p && pGenoType ) + return pGenoType->GetSlot( nFuncId ); + + return static_cast<const SfxSlot*>(p); +} + +const SfxSlot* SfxInterface::GetSlot( const OUString& rCommand ) const +{ + static const char UNO_COMMAND[] = ".uno:"; + + OUString aCommand( rCommand ); + if ( aCommand.startsWith( UNO_COMMAND ) ) + aCommand = aCommand.copy( sizeof( UNO_COMMAND )-1 ); + + for ( sal_uInt16 n=0; n<nCount; n++ ) + { + if ( (pSlots+n)->pUnoName && + aCommand.compareToIgnoreAsciiCaseAscii( (pSlots+n)->GetUnoName() ) == 0 ) + return pSlots+n; + } + + return pGenoType ? pGenoType->GetSlot( aCommand ) : nullptr; +} + + +const SfxSlot* SfxInterface::GetRealSlot( const SfxSlot *pSlot ) const +{ + + assert( pSlots ); + assert( nCount ); + + if ( !ContainsSlot_Impl(pSlot) ) + { + if(pGenoType) + return pGenoType->GetRealSlot(pSlot); + SAL_WARN( "sfx.control", "unknown Slot" ); + return nullptr; + } + + return nullptr; +} + + +void SfxInterface::RegisterPopupMenu( const OUString& rResourceName ) +{ + pImplData->aPopupName = rResourceName; +} + +void SfxInterface::RegisterObjectBar(sal_uInt16 nPos, SfxVisibilityFlags nFlags, ToolbarId eId) +{ + RegisterObjectBar(nPos, nFlags, eId, SfxShellFeature::NONE); +} + +void SfxInterface::RegisterObjectBar(sal_uInt16 nPos, SfxVisibilityFlags nFlags, ToolbarId eId, SfxShellFeature nFeature) +{ + SfxObjectUI_Impl* pUI = CreateObjectBarUI_Impl(nPos, nFlags, eId, nFeature); + if ( pUI ) + pImplData->aObjectBars.emplace_back(pUI); +} + +SfxObjectUI_Impl* CreateObjectBarUI_Impl(sal_uInt16 nPos, SfxVisibilityFlags nFlags, ToolbarId eId, SfxShellFeature nFeature) +{ + if (nFlags == SfxVisibilityFlags::Invisible) + nFlags |= SfxVisibilityFlags::Standard; + + return new SfxObjectUI_Impl(nPos, nFlags, static_cast<sal_uInt32>(eId), nFeature); +} + +ToolbarId SfxInterface::GetObjectBarId(sal_uInt16 nNo) const +{ + bool bGenoType = (pGenoType != nullptr && pGenoType->UseAsSuperClass()); + if ( bGenoType ) + { + // Are there toolbars in the super class? + sal_uInt16 nBaseCount = pGenoType->GetObjectBarCount(); + if ( nNo < nBaseCount ) + // The Super class comes first + return pGenoType->GetObjectBarId(nNo); + else + nNo = nNo - nBaseCount; + } + + assert( nNo<pImplData->aObjectBars.size() ); + + return static_cast<ToolbarId>(pImplData->aObjectBars[nNo]->nObjId); +} + +sal_uInt16 SfxInterface::GetObjectBarPos( sal_uInt16 nNo ) const +{ + bool bGenoType = (pGenoType != nullptr && pGenoType->UseAsSuperClass()); + if ( bGenoType ) + { + // Are there toolbars in the super class? + sal_uInt16 nBaseCount = pGenoType->GetObjectBarCount(); + if ( nNo < nBaseCount ) + // The Super class comes first + return pGenoType->GetObjectBarPos( nNo ); + else + nNo = nNo - nBaseCount; + } + + assert( nNo<pImplData->aObjectBars.size() ); + + return pImplData->aObjectBars[nNo]->nPos; +} + +SfxVisibilityFlags SfxInterface::GetObjectBarFlags( sal_uInt16 nNo ) const +{ + bool bGenoType = (pGenoType != nullptr && pGenoType->UseAsSuperClass()); + if ( bGenoType ) + { + // Are there toolbars in the super class? + sal_uInt16 nBaseCount = pGenoType->GetObjectBarCount(); + if ( nNo < nBaseCount ) + // The Super class comes first + return pGenoType->GetObjectBarFlags( nNo ); + else + nNo = nNo - nBaseCount; + } + + assert( nNo<pImplData->aObjectBars.size() ); + + return pImplData->aObjectBars[nNo]->nFlags; +} + +sal_uInt16 SfxInterface::GetObjectBarCount() const +{ + if (pGenoType && pGenoType->UseAsSuperClass()) + return pImplData->aObjectBars.size() + pGenoType->GetObjectBarCount(); + else + return pImplData->aObjectBars.size(); +} + +void SfxInterface::RegisterChildWindow(sal_uInt16 nId, bool bContext) +{ + RegisterChildWindow(nId, bContext, SfxShellFeature::NONE); +} + +void SfxInterface::RegisterChildWindow(sal_uInt16 nId, bool bContext, SfxShellFeature nFeature) +{ + SfxObjectUI_Impl* pUI = new SfxObjectUI_Impl(0, SfxVisibilityFlags::Invisible, nId, nFeature); + pUI->bContext = bContext; + pImplData->aChildWindows.emplace_back(pUI); +} + +void SfxInterface::RegisterStatusBar(StatusBarId eId) +{ + pImplData->eStatBarResId = eId; +} + +sal_uInt32 SfxInterface::GetChildWindowId (sal_uInt16 nNo) const +{ + if ( pGenoType ) + { + // Are there ChildWindows in the superclass? + sal_uInt16 nBaseCount = pGenoType->GetChildWindowCount(); + if ( nNo < nBaseCount ) + // The Super class comes first + return pGenoType->GetChildWindowId( nNo ); + else + nNo = nNo - nBaseCount; + } + + assert( nNo<pImplData->aChildWindows.size() ); + + sal_uInt32 nRet = pImplData->aChildWindows[nNo]->nObjId; + if ( pImplData->aChildWindows[nNo]->bContext ) + nRet += sal_uInt16( nClassId ) << 16; + return nRet; +} + +SfxShellFeature SfxInterface::GetChildWindowFeature (sal_uInt16 nNo) const +{ + if ( pGenoType ) + { + // Are there ChildWindows in the superclass? + sal_uInt16 nBaseCount = pGenoType->GetChildWindowCount(); + if ( nNo < nBaseCount ) + // The Super class comes first + return pGenoType->GetChildWindowFeature( nNo ); + else + nNo = nNo - nBaseCount; + } + + assert( nNo<pImplData->aChildWindows.size() ); + + return pImplData->aChildWindows[nNo]->nFeature; +} + + +sal_uInt16 SfxInterface::GetChildWindowCount() const +{ + if (pGenoType) + return pImplData->aChildWindows.size() + pGenoType->GetChildWindowCount(); + else + return pImplData->aChildWindows.size(); +} + +const OUString& SfxInterface::GetPopupMenuName() const +{ + return pImplData->aPopupName; +} + +StatusBarId SfxInterface::GetStatusBarId() const +{ + if (pImplData->eStatBarResId == StatusBarId::None && pGenoType) + return pGenoType->GetStatusBarId(); + else + return pImplData->eStatBarResId; +} + +SfxShellFeature SfxInterface::GetObjectBarFeature ( sal_uInt16 nNo ) const +{ + bool bGenoType = (pGenoType != nullptr && pGenoType->UseAsSuperClass()); + if ( bGenoType ) + { + // Are there toolbars in the super class? + sal_uInt16 nBaseCount = pGenoType->GetObjectBarCount(); + if ( nNo < nBaseCount ) + // The Super class comes first + return pGenoType->GetObjectBarFeature( nNo ); + else + nNo = nNo - nBaseCount; + } + + assert( nNo<pImplData->aObjectBars.size() ); + + return pImplData->aObjectBars[nNo]->nFeature; +} + +bool SfxInterface::IsObjectBarVisible(sal_uInt16 nNo) const +{ + bool bGenoType = (pGenoType != nullptr && pGenoType->UseAsSuperClass()); + if ( bGenoType ) + { + // Are there toolbars in the super class? + sal_uInt16 nBaseCount = pGenoType->GetObjectBarCount(); + if ( nNo < nBaseCount ) + // The Super class comes first + return pGenoType->IsObjectBarVisible( nNo ); + else + nNo = nNo - nBaseCount; + } + + assert( nNo<pImplData->aObjectBars.size() ); + + return true; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/recentdocsview.cxx b/sfx2/source/control/recentdocsview.cxx new file mode 100644 index 000000000..a5aae89ac --- /dev/null +++ b/sfx2/source/control/recentdocsview.cxx @@ -0,0 +1,428 @@ +/* -*- 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 . + */ + +#include <sal/log.hxx> +#include <comphelper/base64.hxx> +#include <recentdocsview.hxx> +#include <sfx2/sfxresid.hxx> +#include <tools/diagnose_ex.h> +#include <unotools/historyoptions.hxx> +#include <vcl/builderfactory.hxx> +#include <vcl/event.hxx> +#include <vcl/pngread.hxx> +#include <vcl/ptrstyle.hxx> +#include <vcl/svapp.hxx> +#include <tools/stream.hxx> +#include <tools/urlobj.hxx> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/embed/StorageFactory.hpp> +#include <com/sun/star/embed/ElementModes.hpp> +#include <com/sun/star/embed/XStorage.hpp> +#include <com/sun/star/frame/XDispatch.hpp> +#include <sfx2/strings.hrc> +#include <bitmaps.hlst> +#include <vcl/virdev.hxx> +#include "recentdocsviewitem.hxx" +#include <sfx2/app.hxx> + +#include <officecfg/Office/Common.hxx> + +using namespace ::com::sun::star; +using namespace com::sun::star::uno; +using namespace com::sun::star::lang; +using namespace com::sun::star::beans; + +namespace { + +/// Set (larger) font for the Welcome message. +void SetMessageFont(vcl::RenderContext& rRenderContext) +{ + vcl::Font aFont(rRenderContext.GetFont()); + aFont.SetFontHeight(aFont.GetFontHeight() * 1.3); + rRenderContext.SetFont(aFont); +} + +bool IsDocEncrypted(const OUString& rURL) +{ + uno::Reference< uno::XComponentContext > xContext(::comphelper::getProcessComponentContext()); + bool bIsEncrypted = false; + + try + { + uno::Reference<lang::XSingleServiceFactory> xStorageFactory = embed::StorageFactory::create(xContext); + + uno::Sequence<uno::Any> aArgs (2); + aArgs[0] <<= rURL; + aArgs[1] <<= embed::ElementModes::READ; + uno::Reference<embed::XStorage> xDocStorage ( + xStorageFactory->createInstanceWithArguments(aArgs), + uno::UNO_QUERY); + uno::Reference< beans::XPropertySet > xStorageProps( xDocStorage, uno::UNO_QUERY ); + if ( xStorageProps.is() ) + { + try + { + xStorageProps->getPropertyValue("HasEncryptedEntries") + >>= bIsEncrypted; + } catch( uno::Exception& ) {} + } + } + catch (const uno::Exception&) + { + TOOLS_WARN_EXCEPTION("sfx", + "caught exception trying to find out if doc is encrypted" << rURL); + } + + return bIsEncrypted; +} + +} + +namespace sfx2 +{ + +static std::map<ApplicationType,OUString> BitmapForExtension = +{ + { ApplicationType::TYPE_WRITER, SFX_FILE_THUMBNAIL_TEXT }, + { ApplicationType::TYPE_CALC, SFX_FILE_THUMBNAIL_SHEET }, + { ApplicationType::TYPE_IMPRESS, SFX_FILE_THUMBNAIL_PRESENTATION }, + { ApplicationType::TYPE_DRAW, SFX_FILE_THUMBNAIL_DRAWING }, + { ApplicationType::TYPE_DATABASE, SFX_FILE_THUMBNAIL_DATABASE }, + { ApplicationType::TYPE_MATH, SFX_FILE_THUMBNAIL_MATH } +}; + +static std::map<ApplicationType,OUString> EncryptedBitmapForExtension = +{ + { ApplicationType::TYPE_WRITER, BMP_128X128_WRITER_DOC }, + { ApplicationType::TYPE_CALC, BMP_128X128_CALC_DOC }, + { ApplicationType::TYPE_IMPRESS, BMP_128X128_IMPRESS_DOC }, + { ApplicationType::TYPE_DRAW, BMP_128X128_DRAW_DOC }, + // FIXME: icon for encrypted db doc doesn't exist + { ApplicationType::TYPE_DATABASE, BMP_128X128_CALC_DOC }, + { ApplicationType::TYPE_MATH, BMP_128X128_MATH_DOC } +}; + +static constexpr long gnTextHeight = 30; +static constexpr long gnItemPadding = 5; + +RecentDocsView::RecentDocsView( vcl::Window* pParent ) + : ThumbnailView(pParent) + , mnFileTypes(ApplicationType::TYPE_NONE) + , mnLastMouseDownItem(THUMBNAILVIEW_ITEM_NOTFOUND) + , maWelcomeImage() + , maWelcomeLine1(SfxResId(STR_WELCOME_LINE1)) + , maWelcomeLine2(SfxResId(STR_WELCOME_LINE2)) +{ + tools::Rectangle aScreen = Application::GetScreenPosSizePixel(Application::GetDisplayBuiltInScreen()); + mnItemMaxSize = std::min(aScreen.GetWidth(),aScreen.GetHeight()) > 800 ? 256 : 192; + + SetStyle(GetStyle() | WB_VSCROLL); + setItemMaxTextLength( 30 ); + setItemDimensions( mnItemMaxSize, mnItemMaxSize, gnTextHeight, gnItemPadding ); + + maFillColor = Color(officecfg::Office::Common::Help::StartCenter::StartCenterThumbnailsBackgroundColor::get()); + maTextColor = Color(officecfg::Office::Common::Help::StartCenter::StartCenterThumbnailsTextColor::get()); + maHighlightColor = Color(officecfg::Office::Common::Help::StartCenter::StartCenterThumbnailsHighlightColor::get()); + maHighlightTextColor = Color(officecfg::Office::Common::Help::StartCenter::StartCenterThumbnailsHighlightTextColor::get()); + mfHighlightTransparence = 0.25; +} + +VCL_BUILDER_FACTORY(RecentDocsView) + +bool RecentDocsView::typeMatchesExtension(ApplicationType type, const OUString &rExt) +{ + bool bRet = false; + + if (rExt == "odt" || rExt == "fodt" || rExt == "doc" || rExt == "docx" || + rExt == "rtf" || rExt == "txt" || rExt == "odm" || rExt == "otm") + { + bRet = static_cast<bool>(type & ApplicationType::TYPE_WRITER); + } + else if (rExt == "ods" || rExt == "fods" || rExt == "xls" || rExt == "xlsx") + { + bRet = static_cast<bool>(type & ApplicationType::TYPE_CALC); + } + else if (rExt == "odp" || rExt == "fodp" || rExt == "pps" || rExt == "ppt" || + rExt == "pptx") + { + bRet = static_cast<bool>(type & ApplicationType::TYPE_IMPRESS); + } + else if (rExt == "odg" || rExt == "fodg") + { + bRet = static_cast<bool>(type & ApplicationType::TYPE_DRAW); + } + else if (rExt == "odb") + { + bRet = static_cast<bool>(type & ApplicationType::TYPE_DATABASE); + } + else if (rExt == "odf") + { + bRet = static_cast<bool>(type & ApplicationType::TYPE_MATH); + } + else + { + bRet = static_cast<bool>(type & ApplicationType::TYPE_OTHER); + } + + return bRet; +} + +bool RecentDocsView::isAcceptedFile(const OUString &rURL) const +{ + INetURLObject aUrl(rURL); + OUString aExt = aUrl.getExtension(); + return (mnFileTypes & ApplicationType::TYPE_WRITER && typeMatchesExtension(ApplicationType::TYPE_WRITER, aExt)) || + (mnFileTypes & ApplicationType::TYPE_CALC && typeMatchesExtension(ApplicationType::TYPE_CALC, aExt)) || + (mnFileTypes & ApplicationType::TYPE_IMPRESS && typeMatchesExtension(ApplicationType::TYPE_IMPRESS, aExt)) || + (mnFileTypes & ApplicationType::TYPE_DRAW && typeMatchesExtension(ApplicationType::TYPE_DRAW, aExt)) || + (mnFileTypes & ApplicationType::TYPE_DATABASE && typeMatchesExtension(ApplicationType::TYPE_DATABASE,aExt)) || + (mnFileTypes & ApplicationType::TYPE_MATH && typeMatchesExtension(ApplicationType::TYPE_MATH, aExt)) || + (mnFileTypes & ApplicationType::TYPE_OTHER && typeMatchesExtension(ApplicationType::TYPE_OTHER, aExt)); +} + +BitmapEx RecentDocsView::getDefaultThumbnail(const OUString &rURL) +{ + BitmapEx aImg; + INetURLObject aUrl(rURL); + OUString aExt = aUrl.getExtension(); + + const std::map<ApplicationType,OUString>& rWhichMap = IsDocEncrypted( rURL) ? + EncryptedBitmapForExtension : BitmapForExtension; + + std::map<ApplicationType,OUString>::const_iterator mIt = + std::find_if( rWhichMap.begin(), rWhichMap.end(), + [aExt] ( const std::pair<ApplicationType,OUString>& aEntry ) + { return typeMatchesExtension( aEntry.first, aExt); } ); + + if (mIt != rWhichMap.end()) + aImg = BitmapEx(mIt->second); + else + aImg = BitmapEx(SFX_FILE_THUMBNAIL_DEFAULT); + + return aImg; +} + +void RecentDocsView::insertItem(const OUString &rURL, const OUString &rTitle, const BitmapEx &rThumbnail, sal_uInt16 nId) +{ + AppendItem( std::make_unique<RecentDocsViewItem>(*this, rURL, rTitle, rThumbnail, nId, mnItemMaxSize) ); +} + +void RecentDocsView::Reload() +{ + Clear(); + + Sequence< Sequence< PropertyValue > > aHistoryList = SvtHistoryOptions().GetList( ePICKLIST ); + for ( int i = 0; i < aHistoryList.getLength(); i++ ) + { + const Sequence< PropertyValue >& rRecentEntry = aHistoryList[i]; + + OUString aURL; + OUString aTitle; + BitmapEx aThumbnail; + BitmapEx aModule; + + for ( const auto& rProp : rRecentEntry ) + { + Any a = rProp.Value; + + if (rProp.Name == "URL") + a >>= aURL; + //fdo#74834: only load thumbnail if the corresponding option is not disabled in the configuration + else if (rProp.Name == "Thumbnail" && officecfg::Office::Common::History::RecentDocsThumbnail::get()) + { + OUString aBase64; + a >>= aBase64; + if (!aBase64.isEmpty()) + { + Sequence<sal_Int8> aDecoded; + comphelper::Base64::decode(aDecoded, aBase64); + + SvMemoryStream aStream(aDecoded.getArray(), aDecoded.getLength(), StreamMode::READ); + vcl::PNGReader aReader(aStream); + aThumbnail = aReader.Read(); + } + } + } + + aModule = getDefaultThumbnail(aURL); + if (!aModule.IsEmpty() && !aThumbnail.IsEmpty()) { + ScopedVclPtr<VirtualDevice> m_pVirDev(VclPtr<VirtualDevice>::Create()); + Size aSize(aThumbnail.GetSizePixel()); + m_pVirDev->SetOutputSizePixel(aSize); + m_pVirDev->DrawBitmapEx(Point(), aThumbnail); + m_pVirDev->DrawBitmapEx(Point(aSize.Width()-53,aSize.Height()-53), Size(48, 48), aModule); + aThumbnail = m_pVirDev->GetBitmapEx(Point(), aSize); + m_pVirDev.disposeAndClear(); + } + + if(!aURL.isEmpty()) + { + INetURLObject aURLObj( aURL ); + //Remove extension from url's last segment and use it as title + aTitle = aURLObj.GetBase(); //DecodeMechanism::WithCharset + } + + if (isAcceptedFile(aURL)) + { + insertItem(aURL, aTitle, aThumbnail, i+1); + } + } + + CalculateItemPositions(); + Invalidate(); +} + +void RecentDocsView::MouseButtonDown( const MouseEvent& rMEvt ) +{ + if (rMEvt.IsLeft()) + { + mnLastMouseDownItem = ImplGetItem(rMEvt.GetPosPixel()); + + // ignore to avoid stuff done in ThumbnailView; we don't do selections etc. + return; + } + + ThumbnailView::MouseButtonDown(rMEvt); +} + +void RecentDocsView::MouseButtonUp(const MouseEvent& rMEvt) +{ + if (rMEvt.IsLeft()) + { + if( rMEvt.GetClicks() > 1 ) + return; + + size_t nPos = ImplGetItem(rMEvt.GetPosPixel()); + ThumbnailViewItem* pItem = ImplGetItem(nPos); + + if (pItem && nPos == mnLastMouseDownItem) + { + pItem->MouseButtonUp(rMEvt); + + ThumbnailViewItem* pNewItem = ImplGetItem(nPos); + if(pNewItem) + pNewItem->setHighlight(true); + } + + mnLastMouseDownItem = THUMBNAILVIEW_ITEM_NOTFOUND; + + if (pItem) + return; + } + ThumbnailView::MouseButtonUp(rMEvt); +} + +void RecentDocsView::OnItemDblClicked(ThumbnailViewItem *pItem) +{ + RecentDocsViewItem* pRecentItem = dynamic_cast< RecentDocsViewItem* >(pItem); + if (pRecentItem) + pRecentItem->OpenDocument(); +} + +void RecentDocsView::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle &aRect) +{ + // Set preferred width + if (mFilteredItemList.empty()) + { + rRenderContext.Push(PushFlags::FONT); + SetMessageFont(rRenderContext); + set_width_request(std::max(rRenderContext.GetTextWidth(maWelcomeLine1), + rRenderContext.GetTextWidth(maWelcomeLine2))); + rRenderContext.Pop(); + } + else + { + set_width_request(gnTextHeight + mnItemMaxSize + 2 * gnItemPadding); + } + + if (mItemList.empty()) + { + if (maWelcomeImage.IsEmpty()) + { + const long aWidth(aRect.GetWidth() > aRect.getHeight() ? aRect.GetHeight()/2 : aRect.GetWidth()/2); + maWelcomeImage = SfxApplication::GetApplicationLogo(aWidth); + } + + // No recent files to be shown yet. Show a welcome screen. + rRenderContext.Push(PushFlags::FONT | PushFlags::TEXTCOLOR); + SetMessageFont(rRenderContext); + SetTextColor(maTextColor); + + long nTextHeight = rRenderContext.GetTextHeight(); + + const Size& rImgSize = maWelcomeImage.GetSizePixel(); + const Size& rSize = GetSizePixel(); + + const int nX = (rSize.Width() - rImgSize.Width())/2; + int nY = (rSize.Height() - 3 * nTextHeight - rImgSize.Height())/2; + Point aImgPoint(nX, nY); + rRenderContext.DrawBitmapEx(aImgPoint, rImgSize, maWelcomeImage); + + nY = nY + rImgSize.Height(); + rRenderContext.DrawText(tools::Rectangle(0, nY + 1 * nTextHeight, rSize.Width(), nY + nTextHeight), + maWelcomeLine1, + DrawTextFlags::Center); + rRenderContext.DrawText(tools::Rectangle(0, nY + 2 * nTextHeight, rSize.Width(), rSize.Height()), + maWelcomeLine2, + DrawTextFlags::MultiLine | DrawTextFlags::WordBreak | DrawTextFlags::Center); + + rRenderContext.Pop(); + } + else + { + ThumbnailView::Paint(rRenderContext, aRect); + } +} + +void RecentDocsView::LoseFocus() +{ + deselectItems(); + + ThumbnailView::LoseFocus(); +} + +void RecentDocsView::Clear() +{ + Invalidate(); + ThumbnailView::Clear(); +} + +IMPL_STATIC_LINK( RecentDocsView, ExecuteHdl_Impl, void*, p, void ) +{ + LoadRecentFile* pLoadRecentFile = static_cast< LoadRecentFile*>(p); + try + { + // Asynchronous execution as this can lead to our own destruction! + // Framework can recycle our current frame and the layout manager disposes all user interface + // elements if a component gets detached from its frame! + pLoadRecentFile->xDispatch->dispatch( pLoadRecentFile->aTargetURL, pLoadRecentFile->aArgSeq ); + } + catch ( const Exception& ) + { + } + + if ( !pLoadRecentFile->pView->IsDisposed() ) + pLoadRecentFile->pView->SetPointer( PointerStyle::Arrow ); + + delete pLoadRecentFile; +} + +} // namespace sfx2 + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/recentdocsviewitem.cxx b/sfx2/source/control/recentdocsviewitem.cxx new file mode 100644 index 000000000..7fa4bfaeb --- /dev/null +++ b/sfx2/source/control/recentdocsviewitem.cxx @@ -0,0 +1,223 @@ +/* -*- 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/. + */ + +#include <com/sun/star/frame/Desktop.hpp> +#include <com/sun/star/util/URLTransformer.hpp> +#include <drawinglayer/primitive2d/discretebitmapprimitive2d.hxx> +#include <drawinglayer/processor2d/baseprocessor2d.hxx> +#include <i18nutil/paper.hxx> +#include <officecfg/Office/Common.hxx> +#include <recentdocsview.hxx> +#include <sfx2/templatelocalview.hxx> +#include <tools/urlobj.hxx> +#include <unotools/historyoptions.hxx> +#include <vcl/svapp.hxx> +#include <vcl/event.hxx> +#include <vcl/ptrstyle.hxx> + +#include <bitmaps.hlst> +#include "recentdocsviewitem.hxx" + +using namespace basegfx; +using namespace com::sun::star; +using namespace com::sun::star::uno; +using namespace drawinglayer::primitive2d; +using namespace drawinglayer::processor2d; + +RecentDocsViewItem::RecentDocsViewItem(sfx2::RecentDocsView &rView, const OUString &rURL, + const OUString &rTitle, const BitmapEx &rThumbnail, sal_uInt16 nId, long nThumbnailSize) + : ThumbnailViewItem(rView, nId), + mrParentView(rView), + maURL(rURL), + m_bRemoveIconHighlighted(false), + m_aRemoveRecentBitmap(BMP_RECENTDOC_REMOVE), + m_aRemoveRecentBitmapHighlighted(BMP_RECENTDOC_REMOVE_HIGHLIGHTED) +{ + OUString aTitle(rTitle); + INetURLObject aURLObj(rURL); + + if( aURLObj.GetProtocol() == INetProtocol::File ) + m_sHelpText = aURLObj.getFSysPath(FSysStyle::Detect); + if( m_sHelpText.isEmpty() ) + m_sHelpText = aURLObj.GetURLNoPass(); + + if (aTitle.isEmpty()) + aTitle = aURLObj.GetLastName(INetURLObject::DecodeMechanism::WithCharset); + + BitmapEx aThumbnail(rThumbnail); + //fdo#74834: only load thumbnail if the corresponding option is not disabled in the configuration + if (aThumbnail.IsEmpty() && aURLObj.GetProtocol() == INetProtocol::File && + officecfg::Office::Common::History::RecentDocsThumbnail::get()) + aThumbnail = ThumbnailView::readThumbnail(rURL); + + if (aThumbnail.IsEmpty()) + { + // Use the default thumbnail if we have nothing else + BitmapEx aExt(sfx2::RecentDocsView::getDefaultThumbnail(rURL)); + Size aExtSize(aExt.GetSizePixel()); + + // attempt to make it appear as if it is on a piece of paper + long nPaperHeight; + long nPaperWidth; + if (sfx2::RecentDocsView::typeMatchesExtension( + sfx2::ApplicationType::TYPE_IMPRESS, aURLObj.getExtension())) + { + // Swap width and height (PAPER_SCREEN_4_3 definition make it needed) + PaperInfo aInfo(PAPER_SCREEN_4_3); + nPaperHeight = aInfo.getWidth(); + nPaperWidth = aInfo.getHeight(); + } + else + { + PaperInfo aInfo(PaperInfo::getSystemDefaultPaper()); + nPaperHeight = aInfo.getHeight(); + nPaperWidth = aInfo.getWidth(); + } + double ratio = double(nThumbnailSize) / double(std::max(nPaperHeight, nPaperWidth)); + Size aThumbnailSize(nPaperWidth * ratio, nPaperHeight * ratio); + + if (aExtSize.Width() > aThumbnailSize.Width() || aExtSize.Height() > aThumbnailSize.Height()) + { + aExt = TemplateLocalView::scaleImg(aExt, aThumbnailSize.Width(), aThumbnailSize.Height()); + aExtSize = aExt.GetSizePixel(); + } + + // create empty, and copy the default thumbnail in + sal_uInt8 nAlpha = 255; + aThumbnail = BitmapEx(Bitmap(aThumbnailSize, 24), AlphaMask(aThumbnailSize, &nAlpha)); + + aThumbnail.CopyPixel( + ::tools::Rectangle(Point((aThumbnailSize.Width() - aExtSize.Width()) / 2, (aThumbnailSize.Height() - aExtSize.Height()) / 2), aExtSize), + ::tools::Rectangle(Point(0, 0), aExtSize), + &aExt); + } + + maTitle = aTitle; + maPreview1 = TemplateLocalView::scaleImg(aThumbnail, nThumbnailSize, nThumbnailSize); +} + +::tools::Rectangle RecentDocsViewItem::updateHighlight(bool bVisible, const Point& rPoint) +{ + ::tools::Rectangle aRect(ThumbnailViewItem::updateHighlight(bVisible, rPoint)); + + if (bVisible && getRemoveIconArea().IsInside(rPoint)) + { + if (!m_bRemoveIconHighlighted) + aRect.Union(getRemoveIconArea()); + + m_bRemoveIconHighlighted = true; + } + else + { + if (m_bRemoveIconHighlighted) + aRect.Union(getRemoveIconArea()); + + m_bRemoveIconHighlighted = false; + } + + return aRect; +} + +::tools::Rectangle RecentDocsViewItem::getRemoveIconArea() const +{ + ::tools::Rectangle aArea(getDrawArea()); + Size aSize(m_aRemoveRecentBitmap.GetSizePixel()); + + return ::tools::Rectangle( + Point(aArea.Right() - aSize.Width() - THUMBNAILVIEW_ITEM_CORNER, aArea.Top() + THUMBNAILVIEW_ITEM_CORNER), + aSize); +} + +OUString RecentDocsViewItem::getHelpText() const +{ + return m_sHelpText; +} + +void RecentDocsViewItem::Paint(drawinglayer::processor2d::BaseProcessor2D *pProcessor, const ThumbnailItemAttributes *pAttrs) +{ + ThumbnailViewItem::Paint(pProcessor, pAttrs); + + // paint the remove icon when highlighted + if (isHighlighted()) + { + drawinglayer::primitive2d::Primitive2DContainer aSeq(1); + + Point aIconPos(getRemoveIconArea().TopLeft()); + + aSeq[0] = drawinglayer::primitive2d::Primitive2DReference(new DiscreteBitmapPrimitive2D( + m_bRemoveIconHighlighted ? m_aRemoveRecentBitmapHighlighted : m_aRemoveRecentBitmap, + B2DPoint(aIconPos.X(), aIconPos.Y()))); + + pProcessor->process(aSeq); + } +} + +void RecentDocsViewItem::MouseButtonUp(const MouseEvent& rMEvt) +{ + if (rMEvt.IsLeft()) + { + if (getRemoveIconArea().IsInside(rMEvt.GetPosPixel())) + { + SvtHistoryOptions().DeleteItem(ePICKLIST, maURL); + mrParent.Reload(); + return; + } + + OpenDocument(); + return; + } +} + +void RecentDocsViewItem::OpenDocument() +{ + // show busy mouse pointer + mrParentView.SetPointer(PointerStyle::Wait); + + Reference<frame::XDispatch> xDispatch; + Reference<frame::XDispatchProvider> xDispatchProvider; + css::util::URL aTargetURL; + Sequence<beans::PropertyValue> aArgsList; + + uno::Reference<frame::XDesktop2> xDesktop = frame::Desktop::create(::comphelper::getProcessComponentContext()); + uno::Reference<frame::XFrame> xActiveFrame = xDesktop->getActiveFrame(); + + //osl::ClearableMutexGuard aLock(m_aMutex); + xDispatchProvider.set(xActiveFrame, UNO_QUERY); + //aLock.clear(); + + aTargetURL.Complete = maURL; + Reference<util::XURLTransformer> xTrans(util::URLTransformer::create(::comphelper::getProcessComponentContext())); + xTrans->parseStrict(aTargetURL); + + aArgsList.realloc(2); + aArgsList[0].Name = "Referer"; + aArgsList[0].Value <<= OUString("private:user"); + + // documents will never be opened as templates + aArgsList[1].Name = "AsTemplate"; + aArgsList[1].Value <<= false; + + xDispatch = xDispatchProvider->queryDispatch(aTargetURL, "_default", 0); + + if (!xDispatch.is()) + return; + + // Call dispatch asynchronously as we can be destroyed while dispatch is + // executed. VCL is not able to survive this as it wants to call listeners + // after select!!! + sfx2::LoadRecentFile *const pLoadRecentFile = new sfx2::LoadRecentFile; + pLoadRecentFile->xDispatch = xDispatch; + pLoadRecentFile->aTargetURL = aTargetURL; + pLoadRecentFile->aArgSeq = aArgsList; + pLoadRecentFile->pView = &mrParentView; + + Application::PostUserEvent(LINK(nullptr, sfx2::RecentDocsView, ExecuteHdl_Impl), pLoadRecentFile, true); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/recentdocsviewitem.hxx b/sfx2/source/control/recentdocsviewitem.hxx new file mode 100644 index 000000000..fb259c2d3 --- /dev/null +++ b/sfx2/source/control/recentdocsviewitem.hxx @@ -0,0 +1,67 @@ +/* -*- 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/. + */ + +#ifndef INCLUDED_SFX2_RECENTDOCSVIEWITEM_HXX +#define INCLUDED_SFX2_RECENTDOCSVIEWITEM_HXX + +#include <thumbnailviewitem.hxx> + +class ThumbnailView; + +namespace sfx2 +{ + class RecentDocsView; +} + +class RecentDocsViewItem final : public ThumbnailViewItem +{ +public: + RecentDocsViewItem(sfx2::RecentDocsView &rView, const OUString &rURL, + const OUString &rTitle, const BitmapEx& rThumbnail, sal_uInt16 nId, long nThumbnailSize); + + /** Updates own highlight status based on the aPoint position. + + Calls the ancestor's updateHighlight, and then takes care of m_bRemoveIconHighlighted. + + Returns rectangle that needs to be invalidated. + */ + virtual tools::Rectangle updateHighlight(bool bVisible, const Point& rPoint) override; + + /// Text to be used for the tooltip. + virtual OUString getHelpText() const override; + + virtual void Paint(drawinglayer::processor2d::BaseProcessor2D *pProcessor, + const ThumbnailItemAttributes *pAttrs) override; + + virtual void MouseButtonUp(const MouseEvent& rMEvt) override; + + /// Called when the user clicks a document - it will open it. + void OpenDocument(); + +private: + sfx2::RecentDocsView& mrParentView; + + /// Return area where is the icon to remove document from the recent documents. + tools::Rectangle getRemoveIconArea() const; + + OUString maURL; + + OUString m_sHelpText; + + /// Is the icon that the user can click to remove the document from the recent documents highlighted? + bool m_bRemoveIconHighlighted; + + BitmapEx m_aRemoveRecentBitmap; + + BitmapEx m_aRemoveRecentBitmapHighlighted; +}; + +#endif // INCLUDED_SFX2_RECENTDOCSVIEWITEM_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/request.cxx b/sfx2/source/control/request.cxx new file mode 100644 index 000000000..277c5745b --- /dev/null +++ b/sfx2/source/control/request.cxx @@ -0,0 +1,754 @@ +/* -*- 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 . + */ + +#include <memory> +#include <com/sun/star/frame/DispatchStatement.hpp> +#include <com/sun/star/container/XIndexReplace.hpp> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/uno/Sequence.hxx> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/util/URLTransformer.hpp> +#include <com/sun/star/util/XURLTransformer.hpp> +#include <com/sun/star/frame/XDispatchRecorderSupplier.hpp> +#include <svl/itemiter.hxx> +#include <sal/log.hxx> +#include <osl/diagnose.h> +#include <tools/debug.hxx> + +#include <svl/itempool.hxx> +#include <itemdel.hxx> + +#include <comphelper/processfactory.hxx> + +#include <svl/hint.hxx> + +#include <sfx2/request.hxx> +#include <sfx2/dispatch.hxx> +#include <sfx2/msg.hxx> +#include <sfx2/viewfrm.hxx> +#include <sfx2/objface.hxx> +#include <sfx2/sfxuno.hxx> + + +using namespace ::com::sun::star; + +struct SfxRequest_Impl: public SfxListener + +/* [Description] + + Implementation structure of the <SfxRequest> class. +*/ + +{ + SfxRequest* pAnti; // Owner because of dying pool + OUString aTarget; // if possible from target object set by App + SfxItemPool* pPool; // ItemSet build with this pool + std::unique_ptr<SfxPoolItem> pRetVal; // Return value belongs to itself + SfxShell* pShell; // run from this shell + const SfxSlot* pSlot; // executed Slot + sal_uInt16 nModifier; // which Modifier was pressed? + bool bDone; // at all executed + bool bIgnored; // Cancelled by the User + bool bCancelled; // no longer notify + SfxCallMode nCallMode; // Synch/Asynch/API/Record + bool bAllowRecording; + std::unique_ptr<SfxAllItemSet> + pInternalArgs; + SfxViewFrame* pViewFrame; + + css::uno::Reference< css::frame::XDispatchRecorder > xRecorder; + css::uno::Reference< css::util::XURLTransformer > xTransform; + + explicit SfxRequest_Impl( SfxRequest *pOwner ) + : pAnti( pOwner) + , pPool(nullptr) + , pShell(nullptr) + , pSlot(nullptr) + , nModifier(0) + , bDone(false) + , bIgnored(false) + , bCancelled(false) + , nCallMode( SfxCallMode::SYNCHRON ) + , bAllowRecording( false ) + , pViewFrame(nullptr) + { + } + + void SetPool( SfxItemPool *pNewPool ); + virtual void Notify( SfxBroadcaster &rBC, const SfxHint &rHint ) override; + void Record( const uno::Sequence < beans::PropertyValue >& rArgs ); +}; + + +void SfxRequest_Impl::Notify( SfxBroadcaster&, const SfxHint &rHint ) +{ + if ( rHint.GetId() == SfxHintId::Dying ) + pAnti->Cancel(); +} + + +void SfxRequest_Impl::SetPool( SfxItemPool *pNewPool ) +{ + if ( pNewPool != pPool ) + { + if ( pPool ) + EndListening( pPool->BC() ); + pPool = pNewPool; + if ( pNewPool ) + StartListening( pNewPool->BC() ); + } +} + + +SfxRequest::~SfxRequest() +{ + // Leave out Done() marked requests with 'rem' + if ( pImpl->xRecorder.is() && !pImpl->bDone && !pImpl->bIgnored ) + pImpl->Record( uno::Sequence < beans::PropertyValue >() ); + + // Clear object + pArgs.reset(); + if ( pImpl->pRetVal ) + DeleteItemOnIdle(std::move(pImpl->pRetVal)); +} + + +SfxRequest::SfxRequest +( + const SfxRequest& rOrig +) +: SfxHint( rOrig ), + nSlot(rOrig.nSlot), + pArgs(rOrig.pArgs? new SfxAllItemSet(*rOrig.pArgs): nullptr), + pImpl( new SfxRequest_Impl(this) ) +{ + pImpl->bAllowRecording = rOrig.pImpl->bAllowRecording; + pImpl->bDone = false; + pImpl->bIgnored = false; + pImpl->pShell = nullptr; + pImpl->pSlot = nullptr; + pImpl->nCallMode = rOrig.pImpl->nCallMode; + pImpl->aTarget = rOrig.pImpl->aTarget; + pImpl->nModifier = rOrig.pImpl->nModifier; + + // deep copy needed ! + pImpl->pInternalArgs.reset( rOrig.pImpl->pInternalArgs ? new SfxAllItemSet(*rOrig.pImpl->pInternalArgs) : nullptr); + + if ( pArgs ) + pImpl->SetPool( pArgs->GetPool() ); + else + pImpl->SetPool( rOrig.pImpl->pPool ); + + // setup macro recording if it was in the original SfxRequest + if (!rOrig.pImpl->pViewFrame || !rOrig.pImpl->xRecorder.is()) + return; + + nSlot = rOrig.nSlot; + pImpl->pViewFrame = rOrig.pImpl->pViewFrame; + if (pImpl->pViewFrame->GetDispatcher()->GetShellAndSlot_Impl(nSlot, &pImpl->pShell, &pImpl->pSlot, true, true)) + { + pImpl->SetPool( &pImpl->pShell->GetPool() ); + pImpl->xRecorder = SfxRequest::GetMacroRecorder(pImpl->pViewFrame); + if (pImpl->xRecorder) + pImpl->xTransform = util::URLTransformer::create(comphelper::getProcessComponentContext()); + pImpl->aTarget = pImpl->pShell->GetName(); + } + else + { + SAL_WARN("sfx", "Recording unsupported slot: " << pImpl->pPool->GetSlotId(nSlot)); + } +} + + +SfxRequest::SfxRequest +( + SfxViewFrame* pViewFrame, + sal_uInt16 nSlotId + +) + +/* [Description] + + With this constructor events can subsequently be recorded that are not run + across SfxDispatcher (eg from KeyInput() or mouse events). For this, a + SfxRequest instance is created by this constructor and then proceed + exactly as with a SfxRequest that in a <Slot-Execute-Method> is given as a + parameter. +*/ + +: nSlot(nSlotId), + pImpl( new SfxRequest_Impl(this) ) +{ + pImpl->bDone = false; + pImpl->bIgnored = false; + pImpl->SetPool( &pViewFrame->GetPool() ); + pImpl->pShell = nullptr; + pImpl->pSlot = nullptr; + pImpl->nCallMode = SfxCallMode::SYNCHRON; + pImpl->pViewFrame = pViewFrame; + if( pImpl->pViewFrame->GetDispatcher()->GetShellAndSlot_Impl( nSlotId, &pImpl->pShell, &pImpl->pSlot, true, true ) ) + { + pImpl->SetPool( &pImpl->pShell->GetPool() ); + pImpl->xRecorder = SfxRequest::GetMacroRecorder( pViewFrame ); + if (pImpl->xRecorder) + pImpl->xTransform = util::URLTransformer::create(comphelper::getProcessComponentContext()); + pImpl->aTarget = pImpl->pShell->GetName(); + } + else + { + SAL_WARN( "sfx", "Recording unsupported slot: " << pImpl->pPool->GetSlotId(nSlotId) ); + } +} + + +SfxRequest::SfxRequest +( + sal_uInt16 nSlotId, // executed <Slot-Id> + SfxCallMode nMode, // Synch/API/... + SfxItemPool& rPool // necessary for the SfxItemSet for parameters +) + +// creates a SfxRequest without arguments + +: nSlot(nSlotId), + pImpl( new SfxRequest_Impl(this) ) +{ + pImpl->bDone = false; + pImpl->bIgnored = false; + pImpl->SetPool( &rPool ); + pImpl->pShell = nullptr; + pImpl->pSlot = nullptr; + pImpl->nCallMode = nMode; +} + +SfxRequest::SfxRequest +( + const SfxSlot* pSlot, // executed <Slot-Id> + const css::uno::Sequence < css::beans::PropertyValue >& rArgs, + SfxCallMode nMode, // Synch/API/... + SfxItemPool& rPool // necessary for the SfxItemSet for parameters +) +: nSlot(pSlot->GetSlotId()), + pArgs(new SfxAllItemSet(rPool)), + pImpl( new SfxRequest_Impl(this) ) +{ + pImpl->bDone = false; + pImpl->bIgnored = false; + pImpl->SetPool( &rPool ); + pImpl->pShell = nullptr; + pImpl->pSlot = nullptr; + pImpl->nCallMode = nMode; + TransformParameters( nSlot, rArgs, *pArgs, pSlot ); +} + + +SfxRequest::SfxRequest +( + sal_uInt16 nSlotId, + SfxCallMode nMode, + const SfxAllItemSet& rSfxArgs +) + +// creates a SfxRequest with arguments + +: nSlot(nSlotId), + pArgs(new SfxAllItemSet(rSfxArgs)), + pImpl( new SfxRequest_Impl(this) ) +{ + pImpl->bDone = false; + pImpl->bIgnored = false; + pImpl->SetPool( rSfxArgs.GetPool() ); + pImpl->pShell = nullptr; + pImpl->pSlot = nullptr; + pImpl->nCallMode = nMode; +} + + +SfxRequest::SfxRequest +( + sal_uInt16 nSlotId, + SfxCallMode nMode, + const SfxAllItemSet& rSfxArgs, + const SfxAllItemSet& rSfxInternalArgs +) +: SfxRequest(nSlotId, nMode, rSfxArgs) +{ + SetInternalArgs_Impl(rSfxInternalArgs); +} + +SfxCallMode SfxRequest::GetCallMode() const +{ + return pImpl->nCallMode; +} + + +bool SfxRequest::IsSynchronCall() const +{ + return SfxCallMode::SYNCHRON == ( SfxCallMode::SYNCHRON & pImpl->nCallMode ); +} + + +void SfxRequest::SetSynchronCall( bool bSynchron ) +{ + if ( bSynchron ) + pImpl->nCallMode |= SfxCallMode::SYNCHRON; + else + pImpl->nCallMode &= ~SfxCallMode::SYNCHRON; +} + +void SfxRequest::SetInternalArgs_Impl( const SfxAllItemSet& rArgs ) +{ + pImpl->pInternalArgs.reset( new SfxAllItemSet( rArgs ) ); +} + +const SfxItemSet* SfxRequest::GetInternalArgs_Impl() const +{ + return pImpl->pInternalArgs.get(); +} + + +void SfxRequest_Impl::Record +( + const uno::Sequence < beans::PropertyValue >& rArgs // current Parameter +) + +/* [Description] + + Internal helper method to create a repeatable description of the just + executed SfxRequest. +*/ + +{ + if(!xRecorder.is()) + return; + + OUString aCmd = ".uno:" + OUString::createFromAscii( pSlot->GetUnoName() ); + + uno::Reference< container::XIndexReplace > xReplace( xRecorder, uno::UNO_QUERY ); + if ( xReplace.is() && aCmd == ".uno:InsertText" ) + { + sal_Int32 nCount = xReplace->getCount(); + if ( nCount ) + { + frame::DispatchStatement aStatement; + uno::Any aElement = xReplace->getByIndex(nCount-1); + if ( (aElement >>= aStatement) && aStatement.aCommand == aCmd ) + { + OUString aStr; + OUString aNew; + aStatement.aArgs[0].Value >>= aStr; + rArgs[0].Value >>= aNew; + aStr += aNew; + aStatement.aArgs[0].Value <<= aStr; + aElement <<= aStatement; + xReplace->replaceByIndex( nCount-1, aElement ); + return; + } + } + } + + css::util::URL aURL; + aURL.Complete = aCmd; + xTransform->parseStrict(aURL); + + if (bDone) + xRecorder->recordDispatch(aURL,rArgs); + else + xRecorder->recordDispatchAsComment(aURL,rArgs); +} + + +void SfxRequest::Record_Impl +( + SfxShell& rSh, // the <SfxShell>, which has executed the Request + const SfxSlot& rSlot, // the <SfxSlot>, which has executed the Request + const css::uno::Reference< css::frame::XDispatchRecorder >& xRecorder, + SfxViewFrame* pViewFrame +) + +/* [Description] + + This internal method marks the specified SfxMakro SfxRequest as recorded in + SfxMakro. Pointer to the parameters in Done() is used again, thus has to + still be alive. +*/ + +{ + pImpl->pShell = &rSh; + pImpl->pSlot = &rSlot; + pImpl->xRecorder = xRecorder; + if (pImpl->xRecorder && !pImpl->xTransform) + pImpl->xTransform = util::URLTransformer::create(comphelper::getProcessComponentContext()); + pImpl->aTarget = rSh.GetName(); + pImpl->pViewFrame = pViewFrame; +} + + +void SfxRequest::SetArgs( const SfxAllItemSet& rArgs ) +{ + pArgs.reset(new SfxAllItemSet(rArgs)); + pImpl->SetPool( pArgs->GetPool() ); +} + + +void SfxRequest::AppendItem(const SfxPoolItem &rItem) +{ + if(!pArgs) + pArgs.reset( new SfxAllItemSet(*pImpl->pPool) ); + pArgs->Put(rItem, rItem.Which()); +} + + +void SfxRequest::RemoveItem( sal_uInt16 nID ) +{ + if (pArgs) + { + pArgs->ClearItem(nID); + if ( !pArgs->Count() ) + pArgs.reset(); + } +} + +void SfxRequest::SetReturnValue(const SfxPoolItem &rItem) +{ + DBG_ASSERT(!pImpl->pRetVal, "Set Return value multiple times?"); + pImpl->pRetVal.reset(rItem.Clone()); +} + + +const SfxPoolItem* SfxRequest::GetReturnValue() const +{ + return pImpl->pRetVal.get(); +} + + +void SfxRequest::Done +( + const SfxItemSet& rSet /* parameters passed on by the application, + that for example were asked for by the user + in a dialogue, 0 if no parameters have been + set */ +) + +/* [Description] + + This method must be called in the <Execute-Method> of the <SfxSlot>s, which + has performed the SfxRequest when the execution actually took place. If + 'Done()' is not called, then the SfxRequest is considered canceled. + + Any return values are passed only when 'Done()' was called. Similar, when + recording a macro only true statements are generated if 'Done()' was + called; for SfxRequests that were not identified as such will instead + be commented out by inserting ('rem'). + + [Note] + + 'Done ()' is not called, for example when a dialog started by the function + was canceled by the user or if the execution could not be performed due to + a wrong context (without use of separate <SfxShell>s). 'Done ()' will be + launched, when executing the function led to a regular error + (for example, file could not be opened). +*/ + +{ + Done_Impl( &rSet ); + + // Keep items if possible, so they can be queried by StarDraw. + if ( !pArgs ) + { + pArgs.reset( new SfxAllItemSet( rSet ) ); + pImpl->SetPool( pArgs->GetPool() ); + } + else + { + SfxItemIter aIter(rSet); + for (const SfxPoolItem* pItem = aIter.GetCurItem(); pItem; pItem = aIter.NextItem()) + { + if(!IsInvalidItem(pItem)) + pArgs->Put(*pItem,pItem->Which()); + } + } +} + + +void SfxRequest::Done( bool bRelease ) +// [<SfxRequest::Done(SfxItemSet&)>] +{ + Done_Impl( pArgs.get() ); + if( bRelease ) + pArgs.reset(); +} + + +void SfxRequest::ForgetAllArgs() +{ + pArgs.reset(); + pImpl->pInternalArgs.reset(); +} + + +bool SfxRequest::IsCancelled() const +{ + return pImpl->bCancelled; +} + + +void SfxRequest::Cancel() + +/* [Description] + + Marks this request as no longer executable. For example, if called when + the target (more precisely, its pool) dies. +*/ + +{ + pImpl->bCancelled = true; + pImpl->SetPool( nullptr ); + pArgs.reset(); +} + + +void SfxRequest::Ignore() + +/* [Description] + + If this method is called instead of <SfxRequest::Done()>, then this + request is not recorded. + + [Example] + + The selecting of tools in StarDraw should not be recorded, but the same + slots are to be used from the generation of the tools to the generated + objects. Thus can NoRecords not be specified, i.e. should not be recorded. +*/ + +{ + // Mark as actually executed + pImpl->bIgnored = true; +} + + +void SfxRequest::Done_Impl +( + const SfxItemSet* pSet /* parameters passed on by the application, + that for example were asked for by the user + in a dialogue, 0 if no parameters have been + set */ + +) + +/* [Description] + + Internal method to mark SfxRequest with 'done' and to evaluate the + parameters in 'pSet' in case it is recorded. +*/ + +{ + // Mark as actually executed + pImpl->bDone = true; + + // not Recording + if ( !pImpl->xRecorder.is() ) + return; + + // was running a different slot than requested (Delegation) + if ( nSlot != pImpl->pSlot->GetSlotId() ) + { + // Search Slot again + pImpl->pSlot = pImpl->pShell->GetInterface()->GetSlot(nSlot); + DBG_ASSERT( pImpl->pSlot, "delegated SlotId not found" ); + if ( !pImpl->pSlot ) // playing it safe + return; + } + + // recordable? + // new Recording uses UnoName! + SAL_WARN_IF( !pImpl->pSlot->pUnoName, "sfx", "Recording not exported slot: " + << pImpl->pSlot->GetSlotId() ); + + if ( !pImpl->pSlot->pUnoName ) // playing it safe + return; + + // often required values + SfxItemPool &rPool = pImpl->pShell->GetPool(); + + // Property-Slot? + if ( !pImpl->pSlot->IsMode(SfxSlotMode::METHOD) ) + { + // get the property as SfxPoolItem + const SfxPoolItem *pItem; + sal_uInt16 nWhich = rPool.GetWhich(pImpl->pSlot->GetSlotId()); + SfxItemState eState = pSet ? pSet->GetItemState( nWhich, false, &pItem ) : SfxItemState::UNKNOWN; + SAL_WARN_IF( SfxItemState::SET != eState, "sfx", "Recording property not available: " + << pImpl->pSlot->GetSlotId() ); + uno::Sequence < beans::PropertyValue > aSeq; + if ( eState == SfxItemState::SET ) + TransformItems( pImpl->pSlot->GetSlotId(), *pSet, aSeq, pImpl->pSlot ); + pImpl->Record( aSeq ); + } + + // record everything in a single statement? + else if ( pImpl->pSlot->IsMode(SfxSlotMode::RECORDPERSET) ) + { + uno::Sequence < beans::PropertyValue > aSeq; + if ( pSet ) + TransformItems( pImpl->pSlot->GetSlotId(), *pSet, aSeq, pImpl->pSlot ); + pImpl->Record( aSeq ); + } + + // record each item as a single statement + else if ( pImpl->pSlot->IsMode(SfxSlotMode::RECORDPERITEM) ) + { + if ( pSet ) + { + // iterate over Items + SfxItemIter aIter(*pSet); + for ( const SfxPoolItem* pItem = aIter.GetCurItem(); pItem; pItem = aIter.NextItem() ) + { + // to determine the slot ID for the individual item + sal_uInt16 nSlotId = rPool.GetSlotId( pItem->Which() ); + if ( nSlotId == nSlot ) + { + // play it safe; repair the wrong flags + OSL_FAIL( "recursion RecordPerItem - use RecordPerSet!" ); + SfxSlot *pSlot = const_cast<SfxSlot*>(pImpl->pSlot); + pSlot->nFlags &= ~SfxSlotMode::RECORDPERITEM; + pSlot->nFlags &= SfxSlotMode::RECORDPERSET; + } + + // Record a Sub-Request + SfxRequest aReq( pImpl->pViewFrame, nSlotId ); + if ( aReq.pImpl->pSlot ) + aReq.AppendItem( *pItem ); + aReq.Done(); + } + } + else + { + //HACK(think about this again) + pImpl->Record( uno::Sequence < beans::PropertyValue >() ); + } + } +} + + +bool SfxRequest::IsDone() const + +/* [Description] + + With this method it can be queried whether the SfxRequest was actually + executed or not. If a SfxRequest was not executed, then this is for example + because it was canceled by the user or the context for this request was + wrong, this was not implemented on a separate <SfxShell>. + + SfxRequest instances that return false will not be recorded. + + [Cross-reference] + + <SfxRequest::Done(const SfxItemSet&)> + <SfxRequest::Done()> +*/ + +{ + return pImpl->bDone; +} + + +css::uno::Reference< css::frame::XDispatchRecorder > SfxRequest::GetMacroRecorder( SfxViewFrame const * pView ) + +/* [Description] + + This recorder is an attempt for dispatch () to get calls from the Frame. + This is then available through a property by a supplier but only when + recording was turned on. + + (See also SfxViewFrame::MiscExec_Impl() and SID_RECORDING) +*/ + +{ + css::uno::Reference< css::frame::XDispatchRecorder > xRecorder; + + css::uno::Reference< css::beans::XPropertySet > xSet( + (pView ? pView : SfxViewFrame::Current())->GetFrame().GetFrameInterface(), + css::uno::UNO_QUERY); + + if(xSet.is()) + { + css::uno::Any aProp = xSet->getPropertyValue("DispatchRecorderSupplier"); + css::uno::Reference< css::frame::XDispatchRecorderSupplier > xSupplier; + aProp >>= xSupplier; + if(xSupplier.is()) + xRecorder = xSupplier->getDispatchRecorder(); + } + + return xRecorder; +} + +bool SfxRequest::HasMacroRecorder( SfxViewFrame const * pView ) +{ + return GetMacroRecorder( pView ).is(); +} + + +bool SfxRequest::IsAPI() const + +/* [Description] + + Returns true if this SfxRequest was generated by an API (for example BASIC), + otherwise false. +*/ + +{ + return SfxCallMode::API == ( SfxCallMode::API & pImpl->nCallMode ); +} + + +void SfxRequest::SetModifier( sal_uInt16 nModi ) +{ + pImpl->nModifier = nModi; +} + + +sal_uInt16 SfxRequest::GetModifier() const +{ + return pImpl->nModifier; +} + + +void SfxRequest::AllowRecording( bool bSet ) +{ + pImpl->bAllowRecording = bSet; +} + +bool SfxRequest::AllowsRecording() const +{ + bool bAllow = pImpl->bAllowRecording; + if( !bAllow ) + bAllow = ( SfxCallMode::API != ( SfxCallMode::API & pImpl->nCallMode ) ) && + ( SfxCallMode::RECORD == ( SfxCallMode::RECORD & pImpl->nCallMode ) ); + return bAllow; +} + +void SfxRequest::ReleaseArgs() +{ + pArgs.reset(); + pImpl->pInternalArgs.reset(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/sfxstatuslistener.cxx b/sfx2/source/control/sfxstatuslistener.cxx new file mode 100644 index 000000000..9452d9e7a --- /dev/null +++ b/sfx2/source/control/sfxstatuslistener.cxx @@ -0,0 +1,226 @@ +/* -*- 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 . + */ + +#include <sfx2/sfxstatuslistener.hxx> +#include <svl/poolitem.hxx> +#include <svl/eitem.hxx> +#include <svl/stritem.hxx> +#include <svl/intitem.hxx> +#include <svl/visitem.hxx> +#include <cppuhelper/weak.hxx> +#include <comphelper/processfactory.hxx> +#include <vcl/svapp.hxx> +#include <com/sun/star/util/URLTransformer.hpp> +#include <com/sun/star/util/XURLTransformer.hpp> +#include <com/sun/star/lang/XUnoTunnel.hpp> +#include <com/sun/star/frame/status/ItemStatus.hpp> +#include <com/sun/star/frame/status/Visibility.hpp> +#include <com/sun/star/frame/XDispatchProvider.hpp> + +#include <sfx2/viewfrm.hxx> +#include <sfx2/dispatch.hxx> +#include <unoctitm.hxx> +#include <sfx2/msgpool.hxx> +#include <sfx2/msg.hxx> + +using namespace ::cppu; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::frame; +using namespace ::com::sun::star::frame::status; +using namespace ::com::sun::star::lang; +using namespace ::com::sun::star::util; + +SfxStatusListener::SfxStatusListener( const Reference< XDispatchProvider >& rDispatchProvider, sal_uInt16 nSlotId, const OUString& rCommand ) : + m_nSlotID( nSlotId ), + m_xDispatchProvider( rDispatchProvider ) +{ + m_aCommand.Complete = rCommand; + Reference< XURLTransformer > xTrans( URLTransformer::create( ::comphelper::getProcessComponentContext() ) ); + xTrans->parseStrict( m_aCommand ); + if ( rDispatchProvider.is() ) + m_xDispatch = rDispatchProvider->queryDispatch( m_aCommand, OUString(), 0 ); +} + +SfxStatusListener::~SfxStatusListener() +{ +} + +// old sfx controller item C++ API +void SfxStatusListener::StateChanged( SfxItemState, const SfxPoolItem* ) +{ + // must be implemented by sub class +} + +void SfxStatusListener::UnBind() +{ + if ( m_xDispatch.is() ) + { + Reference< XStatusListener > aStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY ); + m_xDispatch->removeStatusListener( aStatusListener, m_aCommand ); + m_xDispatch.clear(); + } +} + +void SfxStatusListener::ReBind() +{ + Reference< XStatusListener > aStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY ); + if ( m_xDispatch.is() ) + m_xDispatch->removeStatusListener( aStatusListener, m_aCommand ); + if ( m_xDispatchProvider.is() ) + { + try + { + m_xDispatch = m_xDispatchProvider->queryDispatch( m_aCommand, OUString(), 0 ); + if ( m_xDispatch.is() ) + m_xDispatch->addStatusListener( aStatusListener, m_aCommand ); + } + catch( Exception& ) + { + } + } +} + +// new UNO API +void SAL_CALL SfxStatusListener::dispose() +{ + if ( m_xDispatch.is() && !m_aCommand.Complete.isEmpty() ) + { + try + { + Reference< XStatusListener > aStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY ); + m_xDispatch->removeStatusListener( aStatusListener, m_aCommand ); + } + catch ( Exception& ) + { + } + } + + m_xDispatch.clear(); + m_xDispatchProvider.clear(); +} + +void SAL_CALL SfxStatusListener::addEventListener( const Reference< XEventListener >& ) +{ + // do nothing - this is a wrapper class which does not support listeners +} + +void SAL_CALL SfxStatusListener::removeEventListener( const Reference< XEventListener >& ) +{ + // do nothing - this is a wrapper class which does not support listeners +} + +void SAL_CALL SfxStatusListener::disposing( const EventObject& Source ) +{ + SolarMutexGuard aGuard; + + if ( Source.Source == Reference< XInterface >( m_xDispatch, UNO_QUERY )) + m_xDispatch.clear(); + else if ( Source.Source == Reference< XInterface >( m_xDispatchProvider, UNO_QUERY )) + m_xDispatchProvider.clear(); +} + +void SAL_CALL SfxStatusListener::statusChanged( const FeatureStateEvent& rEvent) +{ + SolarMutexGuard aGuard; + + SfxViewFrame* pViewFrame = nullptr; + if ( m_xDispatch.is() ) + { + Reference< XUnoTunnel > xTunnel( m_xDispatch, UNO_QUERY ); + SfxOfficeDispatch* pDisp = nullptr; + if ( xTunnel.is() ) + { + sal_Int64 nImplementation = xTunnel->getSomething(SfxOfficeDispatch::impl_getStaticIdentifier()); + pDisp = reinterpret_cast< SfxOfficeDispatch* >(sal::static_int_cast< sal_IntPtr >( nImplementation )); + } + + if ( pDisp ) + pViewFrame = pDisp->GetDispatcher_Impl()->GetFrame(); + } + + SfxSlotPool& rPool = SfxSlotPool::GetSlotPool( pViewFrame ); + const SfxSlot* pSlot = rPool.GetSlot( m_nSlotID ); + + SfxItemState eState = SfxItemState::DISABLED; + std::unique_ptr<SfxPoolItem> pItem; + if ( rEvent.IsEnabled ) + { + eState = SfxItemState::DEFAULT; + css::uno::Type aType = rEvent.State.getValueType(); + + if ( aType == ::cppu::UnoType<void>::get() ) + { + pItem.reset(new SfxVoidItem( m_nSlotID )); + eState = SfxItemState::UNKNOWN; + } + else if ( aType == cppu::UnoType< bool >::get() ) + { + bool bTemp = false; + rEvent.State >>= bTemp ; + pItem.reset(new SfxBoolItem( m_nSlotID, bTemp )); + } + else if ( aType == cppu::UnoType< ::cppu::UnoUnsignedShortType >::get() ) + { + sal_uInt16 nTemp = 0; + rEvent.State >>= nTemp ; + pItem.reset(new SfxUInt16Item( m_nSlotID, nTemp )); + } + else if ( aType == cppu::UnoType<sal_uInt32>::get() ) + { + sal_uInt32 nTemp = 0; + rEvent.State >>= nTemp ; + pItem.reset(new SfxUInt32Item( m_nSlotID, nTemp )); + } + else if ( aType == cppu::UnoType<OUString>::get() ) + { + OUString sTemp ; + rEvent.State >>= sTemp ; + pItem.reset(new SfxStringItem( m_nSlotID, sTemp )); + } + else if ( aType == cppu::UnoType< css::frame::status::ItemStatus >::get() ) + { + ItemStatus aItemStatus; + rEvent.State >>= aItemStatus; + eState = static_cast<SfxItemState>(aItemStatus.State); + pItem.reset(new SfxVoidItem( m_nSlotID )); + } + else if ( aType == cppu::UnoType< css::frame::status::Visibility >::get() ) + { + Visibility aVisibilityStatus; + rEvent.State >>= aVisibilityStatus; + pItem.reset(new SfxVisibilityItem( m_nSlotID, aVisibilityStatus.bVisible )); + } + else + { + if ( pSlot ) + pItem = pSlot->GetType()->CreateItem(); + if ( pItem ) + { + pItem->SetWhich( m_nSlotID ); + pItem->PutValue( rEvent.State, 0 ); + } + else + pItem.reset(new SfxVoidItem( m_nSlotID )); + } + } + + StateChanged( eState, pItem.get() ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/shell.cxx b/sfx2/source/control/shell.cxx new file mode 100644 index 000000000..353dde1a8 --- /dev/null +++ b/sfx2/source/control/shell.cxx @@ -0,0 +1,726 @@ +/* -*- 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 . + */ + +#include <com/sun/star/embed/VerbDescriptor.hpp> +#include <com/sun/star/embed/VerbAttributes.hpp> +#include <officecfg/Office/Common.hxx> +#include <rtl/ustring.hxx> +#include <sal/log.hxx> +#include <osl/diagnose.h> +#include <svl/itempool.hxx> +#include <svl/undo.hxx> +#include <itemdel.hxx> +#include <svtools/asynclink.hxx> +#include <unotools/configmgr.hxx> +#include <comphelper/lok.hxx> +#include <sfx2/shell.hxx> +#include <sfx2/bindings.hxx> +#include <sfx2/dispatch.hxx> +#include <sfx2/viewfrm.hxx> +#include <sfx2/objface.hxx> +#include <sfx2/objsh.hxx> +#include <sfx2/viewsh.hxx> +#include <sfx2/request.hxx> +#include <sfx2/sfxsids.hrc> +#include <statcach.hxx> +#include <sidebar/ContextChangeBroadcaster.hxx> +#include <com/sun/star/ui/dialogs/XSLTFilterDialog.hpp> +#include <tools/debug.hxx> + +#include <memory> +#include <vector> +#include <map> + +using namespace com::sun::star; + +struct SfxShell_Impl: public SfxBroadcaster +{ + OUString aObjectName; // Name of Sbx-Objects + // Maps the Which() field to a pointer to a SfxPoolItem + std::map<sal_uInt16, std::unique_ptr<SfxPoolItem>> + m_Items; // Data exchange on Item level + SfxViewShell* pViewSh; // SfxViewShell if Shell is + // ViewFrame/ViewShell/SubShell list + SfxViewFrame* pFrame; // Frame, if <UI-active> + SfxRepeatTarget* pRepeatTarget; // SbxObjectRef xParent; + bool bActive; + SfxDisableFlags nDisableFlags; + std::unique_ptr<svtools::AsynchronLink> pExecuter; + std::unique_ptr<svtools::AsynchronLink> pUpdater; + std::vector<std::unique_ptr<SfxSlot> > aSlotArr; + + css::uno::Sequence < css::embed::VerbDescriptor > aVerbList; + ::sfx2::sidebar::ContextChangeBroadcaster maContextChangeBroadcaster; + + SfxShell_Impl() + : pViewSh(nullptr) + , pFrame(nullptr) + , pRepeatTarget(nullptr) + , bActive(false) + , nDisableFlags(SfxDisableFlags::NONE) + { + } + + virtual ~SfxShell_Impl() override { pExecuter.reset(); pUpdater.reset();} +}; + + +void SfxShell::EmptyExecStub(SfxShell *, SfxRequest &) +{ +} + +void SfxShell::EmptyStateStub(SfxShell *, SfxItemSet &) +{ +} + +SfxShell::SfxShell() +: pImpl(new SfxShell_Impl), + pPool(nullptr), + pUndoMgr(nullptr) +{ +} + +SfxShell::SfxShell( SfxViewShell *pViewSh ) +: pImpl(new SfxShell_Impl), + pPool(nullptr), + pUndoMgr(nullptr) +{ + pImpl->pViewSh = pViewSh; +} + +SfxShell::~SfxShell() +{ +} + +void SfxShell::SetName( const OUString &rName ) +{ + pImpl->aObjectName = rName; +} + +const OUString& SfxShell::GetName() const +{ + return pImpl->aObjectName; +} + +SfxDispatcher* SfxShell::GetDispatcher() const +{ + return pImpl->pFrame ? pImpl->pFrame->GetDispatcher() : nullptr; +} + +SfxViewShell* SfxShell::GetViewShell() const +{ + return pImpl->pViewSh; +} + +SfxViewFrame* SfxShell::GetFrame() const +{ + if ( pImpl->pFrame ) + return pImpl->pFrame; + if ( pImpl->pViewSh ) + return pImpl->pViewSh->GetViewFrame(); + return nullptr; +} + +const SfxPoolItem* SfxShell::GetItem +( + sal_uInt16 nSlotId // Slot-Id of the querying <SfxPoolItem>s +) const +{ + auto const it = pImpl->m_Items.find( nSlotId ); + if (it != pImpl->m_Items.end()) + return it->second.get(); + return nullptr; +} + +void SfxShell::PutItem +( + const SfxPoolItem& rItem /* Instance, of which a copy is created, + which is stored in the SfxShell in a list. */ +) +{ + DBG_ASSERT( dynamic_cast< const SfxSetItem* >( &rItem) == nullptr, "SetItems aren't allowed here" ); + DBG_ASSERT( SfxItemPool::IsSlot( rItem.Which() ), + "items with Which-Ids aren't allowed here" ); + + // MSC made a mess here of WNT/W95, beware of changes + SfxPoolItem *pItem = rItem.Clone(); + SfxPoolItemHint aItemHint( pItem ); + sal_uInt16 nWhich = rItem.Which(); + + auto const it = pImpl->m_Items.find(nWhich); + if (it != pImpl->m_Items.end()) + { + // Replace Item + pImpl->m_Items.erase( it ); + pImpl->m_Items.insert(std::make_pair(nWhich, std::unique_ptr<SfxPoolItem>(pItem))); + + // if active, notify Bindings + SfxDispatcher *pDispat = GetDispatcher(); + if ( pDispat ) + { + SfxBindings* pBindings = pDispat->GetBindings(); + pBindings->Broadcast( aItemHint ); + sal_uInt16 nSlotId = nWhich; //pItem->GetSlotId(); + SfxStateCache* pCache = pBindings->GetStateCache( nSlotId ); + if ( pCache ) + { + pCache->SetState( SfxItemState::DEFAULT, pItem, true ); + pCache->SetCachedState( true ); + } + } + return; + } + else + { + Broadcast( aItemHint ); + pImpl->m_Items.insert(std::make_pair(nWhich, std::unique_ptr<SfxPoolItem>(pItem))); + } +} + +SfxInterface* SfxShell::GetInterface() const +{ + return GetStaticInterface(); +} + +SfxUndoManager* SfxShell::GetUndoManager() +{ + return pUndoMgr; +} + +void SfxShell::SetUndoManager( SfxUndoManager *pNewUndoMgr ) +{ + OSL_ENSURE( ( pUndoMgr == nullptr ) || ( pNewUndoMgr == nullptr ) || ( pUndoMgr == pNewUndoMgr ), + "SfxShell::SetUndoManager: exchanging one non-NULL manager with another non-NULL manager? Suspicious!" ); + // there's at least one client of our UndoManager - the DocumentUndoManager at the SfxBaseModel - which + // caches the UndoManager, and registers itself as listener. If exchanging non-NULL UndoManagers is really + // a supported scenario (/me thinks it is not), then we would need to notify all such clients instances. + + pUndoMgr = pNewUndoMgr; + if (pUndoMgr && !utl::ConfigManager::IsFuzzing()) + { + pUndoMgr->SetMaxUndoActionCount( + officecfg::Office::Common::Undo::Steps::get()); + } +} + +SfxRepeatTarget* SfxShell::GetRepeatTarget() const +{ + return pImpl->pRepeatTarget; +} + +void SfxShell::SetRepeatTarget( SfxRepeatTarget *pTarget ) +{ + pImpl->pRepeatTarget = pTarget; +} + +void SfxShell::Invalidate +( + sal_uInt16 nId /* Invalidated Slot-Id or Which-Id. + If these are 0 (default), then all + by this Shell currently handled Slot-Ids are + invalidated. */ +) +{ + if ( !GetViewShell() ) + { + OSL_FAIL( "wrong Invalidate method called!" ); + return; + } + + Invalidate_Impl( GetViewShell()->GetViewFrame()->GetBindings(), nId ); +} + +void SfxShell::Invalidate_Impl( SfxBindings& rBindings, sal_uInt16 nId ) +{ + if ( nId == 0 ) + { + rBindings.InvalidateShell( *this ); + } + else + { + const SfxInterface *pIF = GetInterface(); + do + { + const SfxSlot *pSlot = pIF->GetSlot(nId); + if ( pSlot ) + { + // Invalidate the Slot itself + rBindings.Invalidate( pSlot->GetSlotId() ); + return; + } + + pIF = pIF->GetGenoType(); + } + + while ( pIF ); + + SAL_INFO( "sfx.control", "W3: invalidating slot-id unknown in shell" ); + } +} + +void SfxShell::HandleOpenXmlFilterSettings(SfxRequest & rReq) +{ + try + { + uno::Reference < ui::dialogs::XExecutableDialog > xDialog = ui::dialogs::XSLTFilterDialog::create( ::comphelper::getProcessComponentContext() ); + xDialog->execute(); + } + catch (const uno::Exception&) + { + } + rReq.Ignore (); +} + +void SfxShell::DoActivate_Impl( SfxViewFrame *pFrame, bool bMDI ) +{ +#ifdef DBG_UTIL + const SfxInterface *p_IF = GetInterface(); + if ( !p_IF ) + return; +#endif + SAL_INFO( + "sfx.control", + "SfxShell::DoActivate() " << this << " " << GetInterface()->GetClassName() + << " bMDI " << (bMDI ? "MDI" : "")); + + if ( bMDI ) + { + // Remember Frame, in which it was activated + pImpl->pFrame = pFrame; + pImpl->bActive = true; + } + + // Notify Subclass + Activate(bMDI); +} + +void SfxShell::DoDeactivate_Impl( SfxViewFrame const *pFrame, bool bMDI ) +{ +#ifdef DBG_UTIL + const SfxInterface *p_IF = GetInterface(); + if ( !p_IF ) + return; +#endif + SAL_INFO( + "sfx.control", + "SfxShell::DoDeactivate()" << this << " " << GetInterface()->GetClassName() + << " bMDI " << (bMDI ? "MDI" : "")); + + // Only when it comes from a Frame + // (not when for instance by popping BASIC-IDE from AppDisp) + if ( bMDI && pImpl->pFrame == pFrame ) + { + // deliver + pImpl->pFrame = nullptr; + pImpl->bActive = false; + } + + // Notify Subclass + Deactivate(bMDI); +} + +bool SfxShell::IsActive() const +{ + return pImpl->bActive; +} + +void SfxShell::Activate +( + bool /*bMDI*/ /* TRUE + the <SfxDispatcher>, on which the SfxShell is + located, is activated or the SfxShell instance + was pushed on an active SfxDispatcher. + (compare with SystemWindow::IsMDIActivate()) + + FALSE + the <SfxViewFrame>, on which SfxDispatcher + the SfxShell instance is located, was + activated. (for example by a closing dialog) */ +) +{ + BroadcastContextForActivation(true); +} + +void SfxShell::Deactivate +( + bool /*bMDI*/ /* TRUE + the <SfxDispatcher>, on which the SfxShell is + located, is inactivated or the SfxShell instance + was popped on an active SfxDispatcher. + (compare with SystemWindow::IsMDIActivate()) + + FALSE + the <SfxViewFrame>, on which SfxDispatcher + the SfxShell instance is located, was + deactivated. (for example by a dialog) */ +) +{ + BroadcastContextForActivation(false); +} + +bool SfxShell::CanExecuteSlot_Impl( const SfxSlot &rSlot ) +{ + // Get Slot status + SfxItemPool &rPool = GetPool(); + const sal_uInt16 nId = rSlot.GetWhich( rPool ); + SfxItemSet aSet(rPool, {{nId, nId}}); + SfxStateFunc pFunc = rSlot.GetStateFnc(); + CallState( pFunc, aSet ); + return aSet.GetItemState(nId) != SfxItemState::DISABLED; +} + +bool SfxShell::IsConditionalFastCall( const SfxRequest &rReq ) +{ + sal_uInt16 nId = rReq.GetSlot(); + bool bRet = false; + + if (nId == SID_UNDO || nId == SID_REDO) + { + const SfxItemSet* pArgs = rReq.GetArgs(); + if (pArgs && pArgs->HasItem(SID_REPAIRPACKAGE)) + bRet = true; + } + return bRet; +} + + +static void ShellCall_Impl( void* pObj, void* pArg ) +{ + static_cast<SfxShell*>(pObj)->ExecuteSlot( *static_cast<SfxRequest*>(pArg) ); +} + +void SfxShell::ExecuteSlot( SfxRequest& rReq, bool bAsync ) +{ + if( !bAsync ) + ExecuteSlot( rReq ); + else + { + if( !pImpl->pExecuter ) + pImpl->pExecuter.reset( new svtools::AsynchronLink( + Link<void*,void>( this, ShellCall_Impl ) ) ); + pImpl->pExecuter->Call( new SfxRequest( rReq ) ); + } +} + +const SfxPoolItem* SfxShell::ExecuteSlot +( + SfxRequest &rReq, // the relayed <SfxRequest> + const SfxInterface* pIF // default = 0 means get virtually +) +{ + if ( !pIF ) + pIF = GetInterface(); + + sal_uInt16 nSlot = rReq.GetSlot(); + const SfxSlot* pSlot = nullptr; + if ( nSlot >= SID_VERB_START && nSlot <= SID_VERB_END ) + pSlot = GetVerbSlot_Impl(nSlot); + if ( !pSlot ) + pSlot = pIF->GetSlot(nSlot); + DBG_ASSERT( pSlot, "slot not supported" ); + + SfxExecFunc pFunc = pSlot->GetExecFnc(); + if ( pFunc ) + CallExec( pFunc, rReq ); + + return rReq.GetReturnValue(); +} + +const SfxPoolItem* SfxShell::GetSlotState +( + sal_uInt16 nSlotId, // Slot-Id to the Slots in question + const SfxInterface* pIF, // default = 0 means get virtually + SfxItemSet* pStateSet // SfxItemSet of the Slot-State method +) +{ + // Get Slot on the given Interface + if ( !pIF ) + pIF = GetInterface(); + SfxItemState eState = SfxItemState::UNKNOWN; + SfxItemPool &rPool = GetPool(); + + const SfxSlot* pSlot = nullptr; + if ( nSlotId >= SID_VERB_START && nSlotId <= SID_VERB_END ) + pSlot = GetVerbSlot_Impl(nSlotId); + if ( !pSlot ) + pSlot = pIF->GetSlot(nSlotId); + if ( pSlot ) + // Map on Which-Id if possible + nSlotId = pSlot->GetWhich( rPool ); + + // Get Item and Item status + const SfxPoolItem *pItem = nullptr; + SfxItemSet aSet( rPool, {{nSlotId, nSlotId}} ); // else pItem dies too soon + if ( pSlot ) + { + // Call Status method + SfxStateFunc pFunc = pSlot->GetStateFnc(); + if ( pFunc ) + CallState( pFunc, aSet ); + eState = aSet.GetItemState( nSlotId, true, &pItem ); + + // get default Item if possible + if ( eState == SfxItemState::DEFAULT ) + { + if ( SfxItemPool::IsWhich(nSlotId) ) + pItem = &rPool.GetDefaultItem(nSlotId); + else + eState = SfxItemState::DONTCARE; + } + } + else + eState = SfxItemState::UNKNOWN; + + // Evaluate Item and item status and possibly maintain them in pStateSet + std::unique_ptr<SfxPoolItem> pRetItem; + if ( eState <= SfxItemState::DISABLED ) + { + if ( pStateSet ) + pStateSet->DisableItem(nSlotId); + return nullptr; + } + else if ( eState == SfxItemState::DONTCARE ) + { + if ( pStateSet ) + pStateSet->ClearItem(nSlotId); + pRetItem.reset( new SfxVoidItem(0) ); + } + else + { + if ( pStateSet && pStateSet->Put( *pItem ) ) + return &pStateSet->Get( pItem->Which() ); + pRetItem.reset(pItem->Clone()); + } + auto pTemp = pRetItem.get(); + DeleteItemOnIdle(std::move(pRetItem)); + + return pTemp; +} + +static SFX_EXEC_STUB(SfxShell, VerbExec) +static void SfxStubSfxShellVerbState(SfxShell *, SfxItemSet& rSet) +{ + SfxShell::VerbState( rSet ); +} + +void SfxShell::SetVerbs(const css::uno::Sequence < css::embed::VerbDescriptor >& aVerbs) +{ + SfxViewShell *pViewSh = dynamic_cast<SfxViewShell*>( this ); + + DBG_ASSERT(pViewSh, "Only call SetVerbs at the ViewShell!"); + if ( !pViewSh ) + return; + + // First make all Statecaches dirty, so that no-one no longer tries to use + // the Slots + { + SfxBindings *pBindings = + pViewSh->GetViewFrame()->GetDispatcher()->GetBindings(); + sal_uInt16 nCount = pImpl->aSlotArr.size(); + for (sal_uInt16 n1=0; n1<nCount ; n1++) + { + sal_uInt16 nId = SID_VERB_START + n1; + pBindings->Invalidate(nId, false, true); + } + } + + sal_uInt16 nr=0; + for (sal_Int32 n=0; n<aVerbs.getLength(); n++) + { + sal_uInt16 nSlotId = SID_VERB_START + nr++; + DBG_ASSERT(nSlotId <= SID_VERB_END, "Too many Verbs!"); + if (nSlotId > SID_VERB_END) + break; + + SfxSlot *pNewSlot = new SfxSlot; + pNewSlot->nSlotId = nSlotId; + pNewSlot->nGroupId = SfxGroupId::NONE; + + // Verb slots must be executed asynchronously, so that they can be + // destroyed while executing. + pNewSlot->nFlags = SfxSlotMode::ASYNCHRON | SfxSlotMode::CONTAINER; + pNewSlot->nMasterSlotId = 0; + pNewSlot->nValue = 0; + pNewSlot->fnExec = SFX_STUB_PTR(SfxShell,VerbExec); + pNewSlot->fnState = SFX_STUB_PTR(SfxShell,VerbState); + pNewSlot->pType = nullptr; // HACK(SFX_TYPE(SfxVoidItem)) ??? + pNewSlot->nArgDefCount = 0; + pNewSlot->pFirstArgDef = nullptr; + pNewSlot->pUnoName = nullptr; + + if (!pImpl->aSlotArr.empty()) + { + SfxSlot& rSlot = *pImpl->aSlotArr[0]; + pNewSlot->pNextSlot = rSlot.pNextSlot; + rSlot.pNextSlot = pNewSlot; + } + else + pNewSlot->pNextSlot = pNewSlot; + + pImpl->aSlotArr.insert(pImpl->aSlotArr.begin() + static_cast<sal_uInt16>(n), std::unique_ptr<SfxSlot>(pNewSlot)); + } + + pImpl->aVerbList = aVerbs; + + // The status of SID_OBJECT is collected in the controller directly on + // the Shell, it is thus enough to encourage a new status update + SfxBindings* pBindings = pViewSh->GetViewFrame()->GetDispatcher()->GetBindings(); + pBindings->Invalidate(SID_OBJECT, true, true); +} + +const css::uno::Sequence < css::embed::VerbDescriptor >& SfxShell::GetVerbs() const +{ + return pImpl->aVerbList; +} + +void SfxShell::VerbExec(SfxRequest& rReq) +{ + sal_uInt16 nId = rReq.GetSlot(); + SfxViewShell *pViewShell = GetViewShell(); + if ( !pViewShell ) + return; + + bool bReadOnly = pViewShell->GetObjectShell()->IsReadOnly(); + const css::uno::Sequence < css::embed::VerbDescriptor > aList = pViewShell->GetVerbs(); + sal_Int32 nVerb = 0; + for (const auto& rVerb : aList) + { + // check for ReadOnly verbs + if ( bReadOnly && !(rVerb.VerbAttributes & embed::VerbAttributes::MS_VERBATTR_NEVERDIRTIES) ) + continue; + + // check for verbs that shouldn't appear in the menu + if ( !(rVerb.VerbAttributes & embed::VerbAttributes::MS_VERBATTR_ONCONTAINERMENU) ) + continue; + + if (nId == SID_VERB_START + nVerb++) + { + pViewShell->DoVerb(rVerb.VerbID); + rReq.Done(); + return; + } + } +} + +void SfxShell::VerbState(SfxItemSet& ) +{ +} + +const SfxSlot* SfxShell::GetVerbSlot_Impl(sal_uInt16 nId) const +{ + css::uno::Sequence < css::embed::VerbDescriptor > rList = pImpl->aVerbList; + + DBG_ASSERT(nId >= SID_VERB_START && nId <= SID_VERB_END,"Wrong VerbId!"); + sal_uInt16 nIndex = nId - SID_VERB_START; + DBG_ASSERT(nIndex < rList.getLength(),"Wrong VerbId!"); + + if (nIndex < rList.getLength()) + return pImpl->aSlotArr[nIndex].get(); + else + return nullptr; +} + +SfxObjectShell* SfxShell::GetObjectShell() +{ + if ( GetViewShell() ) + return GetViewShell()->GetViewFrame()->GetObjectShell(); + else + return nullptr; +} + +bool SfxShell::HasUIFeature(SfxShellFeature) const +{ + return false; +} + +static void DispatcherUpdate_Impl( void*, void* pArg ) +{ + static_cast<SfxDispatcher*>(pArg)->Update_Impl( true ); + static_cast<SfxDispatcher*>(pArg)->GetBindings()->InvalidateAll(false); +} + +void SfxShell::UIFeatureChanged() +{ + SfxViewFrame *pFrame = GetFrame(); + if ( pFrame && pFrame->IsVisible() ) + { + // Also force an update, if dispatcher is already updated otherwise + // something my get stuck in the bunkered tools. Asynchronous call to + // prevent recursion. + if ( !pImpl->pUpdater ) + pImpl->pUpdater.reset( new svtools::AsynchronLink( Link<void*,void>( this, DispatcherUpdate_Impl ) ) ); + + // Multiple views allowed + pImpl->pUpdater->Call( pFrame->GetDispatcher(), true ); + } +} + +void SfxShell::SetDisableFlags( SfxDisableFlags nFlags ) +{ + pImpl->nDisableFlags = nFlags; +} + +SfxDisableFlags SfxShell::GetDisableFlags() const +{ + return pImpl->nDisableFlags; +} + +std::unique_ptr<SfxItemSet> SfxShell::CreateItemSet( sal_uInt16 ) +{ + return nullptr; +} + +void SfxShell::ApplyItemSet( sal_uInt16, const SfxItemSet& ) +{ +} + +void SfxShell::SetContextName (const OUString& rsContextName) +{ + pImpl->maContextChangeBroadcaster.Initialize(rsContextName); +} + +void SfxShell::SetViewShell_Impl( SfxViewShell* pView ) +{ + pImpl->pViewSh = pView; +} + +void SfxShell::BroadcastContextForActivation (const bool bIsActivated) +{ + // Avoids activation and de-activation (can be seen on switching view) from causing + // the sidebar to re-build. Such switching can happen as we change view to render + // using LOK for example, and is un-necessary for Online. + if (comphelper::LibreOfficeKit::isDialogPainting()) + return; + + SfxViewFrame* pViewFrame = GetFrame(); + if (pViewFrame != nullptr) + { + if (bIsActivated) + pImpl->maContextChangeBroadcaster.Activate(pViewFrame->GetFrame().GetFrameInterface()); + else + pImpl->maContextChangeBroadcaster.Deactivate(pViewFrame->GetFrame().GetFrameInterface()); + } +} + +bool SfxShell::SetContextBroadcasterEnabled (const bool bIsEnabled) +{ + return pImpl->maContextChangeBroadcaster.SetBroadcasterEnabled(bIsEnabled); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/sorgitm.cxx b/sfx2/source/control/sorgitm.cxx new file mode 100644 index 000000000..6f23e3fcf --- /dev/null +++ b/sfx2/source/control/sorgitm.cxx @@ -0,0 +1,88 @@ +/* -*- 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 . + */ + + +#include <sfx2/sfxsids.hrc> +#include <sorgitm.hxx> +#include <osl/diagnose.h> + +SfxPoolItem* SfxScriptOrganizerItem::CreateDefault() { return new SfxScriptOrganizerItem; } + + +SfxScriptOrganizerItem::SfxScriptOrganizerItem() : + + SfxStringItem() + +{ +} + +SfxScriptOrganizerItem* SfxScriptOrganizerItem::Clone( SfxItemPool * ) const +{ + return new SfxScriptOrganizerItem( *this ); +} + +bool SfxScriptOrganizerItem::operator==( const SfxPoolItem& rItem) const +{ + return SfxStringItem::operator==(rItem) && + aLanguage == static_cast<const SfxScriptOrganizerItem &>(rItem).aLanguage; +} + + +bool SfxScriptOrganizerItem::QueryValue( css::uno::Any& rVal, sal_uInt8 nMemberId ) const +{ + OUString aValue; + nMemberId &= ~CONVERT_TWIPS; + switch ( nMemberId ) + { + case 0: + case MID_SCRIPT_ORGANIZER_LANGUAGE: + aValue = aLanguage; + break; + default: + OSL_FAIL("Wrong MemberId!"); + return false; + } + + rVal <<= aValue; + + return true; +} + +bool SfxScriptOrganizerItem::PutValue( const css::uno::Any& rVal, sal_uInt8 nMemberId ) +{ + OUString aValue; + bool bRet = false; + nMemberId &= ~CONVERT_TWIPS; + switch ( nMemberId ) + { + case 0: + case MID_SCRIPT_ORGANIZER_LANGUAGE: + bRet = (rVal >>= aValue); + if ( bRet ) + aLanguage = aValue; + break; + default: + OSL_FAIL("Wrong MemberId!"); + return false; + } + + return bRet; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/statcach.cxx b/sfx2/source/control/statcach.cxx new file mode 100644 index 000000000..dfdc10f83 --- /dev/null +++ b/sfx2/source/control/statcach.cxx @@ -0,0 +1,499 @@ +/* -*- 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 . + */ + + +#ifdef __sun +#include <ctime> +#endif + +#include <framework/dispatchhelper.hxx> +#include <com/sun/star/frame/DispatchResultState.hpp> +#include <com/sun/star/frame/XFrame.hpp> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <cppuhelper/weak.hxx> +#include <svl/eitem.hxx> +#include <svl/intitem.hxx> +#include <svl/stritem.hxx> +#include <svl/visitem.hxx> + +#include <sfx2/app.hxx> +#include <statcach.hxx> +#include <sfx2/msg.hxx> +#include <sfx2/ctrlitem.hxx> +#include <sfx2/dispatch.hxx> +#include <sfx2/sfxuno.hxx> +#include <unoctitm.hxx> +#include <sfx2/msgpool.hxx> +#include <sfx2/viewfrm.hxx> + +using namespace ::com::sun::star; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::util; + +BindDispatch_Impl::BindDispatch_Impl( const css::uno::Reference< css::frame::XDispatch > & rDisp, const css::util::URL& rURL, SfxStateCache *pStateCache, const SfxSlot* pS ) + : xDisp( rDisp ) + , aURL( rURL ) + , pCache( pStateCache ) + , pSlot( pS ) +{ + DBG_ASSERT( pCache && pSlot, "Invalid BindDispatch!"); + aStatus.IsEnabled = true; +} + +void SAL_CALL BindDispatch_Impl::disposing( const css::lang::EventObject& ) +{ + if ( xDisp.is() ) + { + xDisp->removeStatusListener( static_cast<css::frame::XStatusListener*>(this), aURL ); + xDisp.clear(); + } +} + +void SAL_CALL BindDispatch_Impl::statusChanged( const css::frame::FeatureStateEvent& rEvent ) +{ + aStatus = rEvent; + if ( !pCache ) + return; + + css::uno::Reference< css::frame::XStatusListener > xKeepAlive( static_cast<cppu::OWeakObject*>(this), css::uno::UNO_QUERY ); + if ( aStatus.Requery ) + pCache->Invalidate( true ); + else + { + std::unique_ptr<SfxPoolItem> pItem; + sal_uInt16 nId = pCache->GetId(); + SfxItemState eState = SfxItemState::DISABLED; + if ( !aStatus.IsEnabled ) + { + // default + } + else if (aStatus.State.hasValue()) + { + eState = SfxItemState::DEFAULT; + css::uno::Any aAny = aStatus.State; + + const css::uno::Type& aType = aAny.getValueType(); + if ( aType == cppu::UnoType< bool >::get() ) + { + bool bTemp = false; + aAny >>= bTemp ; + pItem.reset( new SfxBoolItem( nId, bTemp ) ); + } + else if ( aType == ::cppu::UnoType< ::cppu::UnoUnsignedShortType >::get() ) + { + sal_uInt16 nTemp = 0; + aAny >>= nTemp ; + pItem.reset( new SfxUInt16Item( nId, nTemp ) ); + } + else if ( aType == cppu::UnoType<sal_uInt32>::get() ) + { + sal_uInt32 nTemp = 0; + aAny >>= nTemp ; + pItem.reset( new SfxUInt32Item( nId, nTemp ) ); + } + else if ( aType == cppu::UnoType<OUString>::get() ) + { + OUString sTemp ; + aAny >>= sTemp ; + pItem.reset( new SfxStringItem( nId, sTemp ) ); + } + else + { + if ( pSlot ) + pItem = pSlot->GetType()->CreateItem(); + if ( pItem ) + { + pItem->SetWhich( nId ); + pItem->PutValue( aAny, 0 ); + } + else + pItem.reset( new SfxVoidItem( nId ) ); + } + } + else + { + // DONTCARE status + pItem.reset( new SfxVoidItem(0) ); + eState = SfxItemState::UNKNOWN; + } + + for ( SfxControllerItem *pCtrl = pCache->GetItemLink(); + pCtrl; + pCtrl = pCtrl->GetItemLink() ) + pCtrl->StateChanged( nId, eState, pItem.get() ); + } +} + +void BindDispatch_Impl::Release() +{ + if ( xDisp.is() ) + { + xDisp->removeStatusListener( static_cast<css::frame::XStatusListener*>(this), aURL ); + xDisp.clear(); + } + pCache = nullptr; +} + + +sal_Int16 BindDispatch_Impl::Dispatch( const css::uno::Sequence < css::beans::PropertyValue >& aProps, bool bForceSynchron ) +{ + sal_Int16 eRet = css::frame::DispatchResultState::DONTKNOW; + + if ( xDisp.is() && aStatus.IsEnabled ) + { + ::rtl::Reference< ::framework::DispatchHelper > xHelper( new ::framework::DispatchHelper(nullptr)); + css::uno::Any aResult = xHelper->executeDispatch(xDisp, aURL, bForceSynchron, aProps); + + css::frame::DispatchResultEvent aEvent; + aResult >>= aEvent; + + eRet = aEvent.State; + } + + return eRet; +} + + +// This constructor for an invalid cache that is updated in the first request. + +SfxStateCache::SfxStateCache( sal_uInt16 nFuncId ): + nId(nFuncId), + pInternalController(nullptr), + pController(nullptr), + pLastItem( nullptr ), + eLastState( SfxItemState::UNKNOWN ), + bItemVisible( true ) +{ + bCtrlDirty = true; + bSlotDirty = true; + bItemDirty = true; +} + + +// The Destructor checks by assertion, even if controllers are registered. + +SfxStateCache::~SfxStateCache() +{ + DBG_ASSERT( pController == nullptr && pInternalController == nullptr, "there are still Controllers registered" ); + if ( !IsInvalidItem(pLastItem) ) + delete pLastItem; + if ( mxDispatch.is() ) + mxDispatch->Release(); +} + + +// invalidates the cache (next request will force update) +void SfxStateCache::Invalidate( bool bWithMsg ) +{ + bCtrlDirty = true; + if ( bWithMsg ) + { + bSlotDirty = true; + aSlotServ.SetSlot( nullptr ); + if ( mxDispatch.is() ) + mxDispatch->Release(); + mxDispatch.clear(); + } +} + + +// gets the corresponding function from the dispatcher or the cache + +const SfxSlotServer* SfxStateCache::GetSlotServer( SfxDispatcher &rDispat , const css::uno::Reference< css::frame::XDispatchProvider > & xProv ) +{ + + if ( bSlotDirty ) + { + // get the SlotServer; we need it for internal controllers anyway, but also in most cases + rDispat.FindServer_( nId, aSlotServ ); + + DBG_ASSERT( !mxDispatch.is(), "Old Dispatch not removed!" ); + + // we don't need to check the dispatch provider if we only have an internal controller + if ( xProv.is() ) + { + const SfxSlot* pSlot = aSlotServ.GetSlot(); + if ( !pSlot ) + // get the slot - even if it is disabled on the dispatcher + pSlot = SfxSlotPool::GetSlotPool( rDispat.GetFrame() ).GetSlot( nId ); + + if ( !pSlot || !pSlot->pUnoName ) + { + bSlotDirty = false; + bCtrlDirty = true; + return aSlotServ.GetSlot()? &aSlotServ: nullptr; + } + + // create the dispatch URL from the slot data + css::util::URL aURL; + OUString aCmd = ".uno:"; + aURL.Protocol = aCmd; + aURL.Path = OUString::createFromAscii( pSlot->GetUnoName() ); + aCmd += aURL.Path; + aURL.Complete = aCmd; + aURL.Main = aCmd; + + // try to get a dispatch object for this command + css::uno::Reference< css::frame::XDispatch > xDisp = xProv->queryDispatch( aURL, OUString(), 0 ); + if ( xDisp.is() ) + { + // test the dispatch object if it is just a wrapper for a SfxDispatcher + css::uno::Reference< css::lang::XUnoTunnel > xTunnel( xDisp, css::uno::UNO_QUERY ); + SfxOfficeDispatch* pDisp = nullptr; + if ( xTunnel.is() ) + { + sal_Int64 nImplementation = xTunnel->getSomething(SfxOfficeDispatch::impl_getStaticIdentifier()); + pDisp = reinterpret_cast< SfxOfficeDispatch* >(sal::static_int_cast< sal_IntPtr >( nImplementation )); + } + + if ( pDisp ) + { + // The intercepting object is an SFX component + // If this dispatch object does not use the wanted dispatcher or the AppDispatcher, it's treated like any other UNO component + // (intercepting by internal dispatches) + SfxDispatcher *pDispatcher = pDisp->GetDispatcher_Impl(); + if ( pDispatcher == &rDispat || pDispatcher == SfxGetpApp()->GetAppDispatcher_Impl() ) + { + // so we can use it directly + bSlotDirty = false; + bCtrlDirty = true; + return aSlotServ.GetSlot()? &aSlotServ: nullptr; + } + } + + // so the dispatch object isn't a SfxDispatcher wrapper or it is one, but it uses another dispatcher, but not rDispat + mxDispatch = new BindDispatch_Impl( xDisp, aURL, this, pSlot ); + + // flags must be set before adding StatusListener because the dispatch object will set the state + bSlotDirty = false; + bCtrlDirty = true; + xDisp->addStatusListener( mxDispatch.get(), aURL ); + } + else if ( rDispat.GetFrame() ) + { + css::uno::Reference < css::frame::XDispatchProvider > xFrameProv( + rDispat.GetFrame()->GetFrame().GetFrameInterface(), css::uno::UNO_QUERY ); + if ( xFrameProv != xProv ) + return GetSlotServer( rDispat, xFrameProv ); + } + } + + bSlotDirty = false; + bCtrlDirty = true; + } + + // we *always* return a SlotServer (if there is one); but in case of an external dispatch we might not use it + // for the "real" (non internal) controllers + return aSlotServ.GetSlot()? &aSlotServ: nullptr; +} + + +// Set Status in all Controllers + +void SfxStateCache::SetState +( + SfxItemState eState, // <SfxItemState> from 'pState' + const SfxPoolItem* pState, // Slot Status, 0 or -1 + bool bMaybeDirty +) + +/* [Description] + + This method distributes the status of all of this SID bound + <SfxControllerItem>s. If the value is the same as before, and if neither + controller was registered nor invalidated inbetween, then no value is + passed. This way the flickering is for example avoided in ListBoxes. +*/ +{ + SetState_Impl( eState, pState, bMaybeDirty ); +} + +void SfxStateCache::GetState +( + boost::property_tree::ptree& rState +) +{ + if ( !mxDispatch.is() && pController ) + { + for ( SfxControllerItem *pCtrl = pController; + pCtrl; + pCtrl = pCtrl->GetItemLink() ) + pCtrl->GetControlState( nId, rState ); + } +} + +void SfxStateCache::SetVisibleState( bool bShow ) +{ + if ( bShow == bItemVisible ) + return; + + SfxItemState eState( SfxItemState::DEFAULT ); + const SfxPoolItem* pState( nullptr ); + bool bDeleteItem( false ); + + bItemVisible = bShow; + if ( bShow ) + { + if ( IsInvalidItem(pLastItem) || ( pLastItem == nullptr )) + { + pState = new SfxVoidItem( nId ); + bDeleteItem = true; + } + else + pState = pLastItem; + + eState = eLastState; + } + else + { + pState = new SfxVisibilityItem( nId, false ); + bDeleteItem = true; + } + + // Update Controller + if ( !mxDispatch.is() && pController ) + { + for ( SfxControllerItem *pCtrl = pController; + pCtrl; + pCtrl = pCtrl->GetItemLink() ) + pCtrl->StateChanged( nId, eState, pState ); + } + + if ( pInternalController ) + pInternalController->StateChanged( nId, eState, pState ); + + if ( bDeleteItem ) + delete pState; +} + + +void SfxStateCache::SetState_Impl +( + SfxItemState eState, // <SfxItemState> from 'pState' + const SfxPoolItem* pState, // Slot Status, 0 or -1 + bool bMaybeDirty +) +{ + // If a hard update occurs between enter- and leave-registrations is a + // can also intermediate Cached exist without controller. + if ( !pController && !pInternalController ) + return; + + DBG_ASSERT( bMaybeDirty || !bSlotDirty, "setting state of dirty message" ); + DBG_ASSERT( SfxControllerItem::GetItemState(pState) == eState, "invalid SfxItemState" ); + + // does the controller have to be notified at all? + bool bNotify = bItemDirty; + if ( !bItemDirty ) + { + bool bBothAvailable = pLastItem && pState && + !IsInvalidItem(pState) && !IsInvalidItem(pLastItem); + DBG_ASSERT( !bBothAvailable || pState != pLastItem, "setting state with own item" ); + if ( bBothAvailable ) + bNotify = typeid(*pState) != typeid(*pLastItem) || + *pState != *pLastItem; + else + bNotify = ( pState != pLastItem ) || ( eState != eLastState ); + } + + if ( bNotify ) + { + // Update Controller + if ( !mxDispatch.is() && pController ) + { + for ( SfxControllerItem *pCtrl = pController; + pCtrl; + pCtrl = pCtrl->GetItemLink() ) + pCtrl->StateChanged( nId, eState, pState ); + } + + if ( pInternalController ) + static_cast<SfxDispatchController_Impl *>(pInternalController)->StateChanged( nId, eState, pState, &aSlotServ ); + + // Remember new value + if ( !IsInvalidItem(pLastItem) ) + DELETEZ(pLastItem); + if ( pState && !IsInvalidItem(pState) ) + pLastItem = pState->Clone(); + else + pLastItem = nullptr; + eLastState = eState; + bItemDirty = false; + } + + bCtrlDirty = false; +} + + +// Set old status again in all the controllers + +void SfxStateCache::SetCachedState( bool bAlways ) +{ + DBG_ASSERT(pController==nullptr||pController->GetId()==nId, "Cache with wrong ControllerItem" ); + + // Only update if cached item exists and also able to process. + // (If the State is sent, it must be ensured that a SlotServer is present, + // see SfxControllerItem:: GetCoreMetric()) + if ( !(bAlways || ( !bItemDirty && !bSlotDirty )) ) + return; + + // Update Controller + if ( !mxDispatch.is() && pController ) + { + for ( SfxControllerItem *pCtrl = pController; + pCtrl; + pCtrl = pCtrl->GetItemLink() ) + pCtrl->StateChanged( nId, eLastState, pLastItem ); + } + + if ( pInternalController ) + static_cast<SfxDispatchController_Impl *>(pInternalController)->StateChanged( nId, eLastState, pLastItem, &aSlotServ ); + + // Controller is now ok + bCtrlDirty = true; +} + + +css::uno::Reference< css::frame::XDispatch > SfxStateCache::GetDispatch() const +{ + if ( mxDispatch.is() ) + return mxDispatch->xDisp; + return css::uno::Reference< css::frame::XDispatch > (); +} + +sal_Int16 SfxStateCache::Dispatch( const SfxItemSet* pSet, bool bForceSynchron ) +{ + // protect pDispatch against destruction in the call + rtl::Reference<BindDispatch_Impl> xKeepAlive( mxDispatch ); + sal_Int16 eRet = css::frame::DispatchResultState::DONTKNOW; + + if ( mxDispatch.is() ) + { + uno::Sequence < beans::PropertyValue > aArgs; + if (pSet) + TransformItems( nId, *pSet, aArgs ); + + eRet = mxDispatch->Dispatch( aArgs, bForceSynchron ); + } + + return eRet; +} + + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/templatecontaineritem.cxx b/sfx2/source/control/templatecontaineritem.cxx new file mode 100644 index 000000000..e30b6b7b1 --- /dev/null +++ b/sfx2/source/control/templatecontaineritem.cxx @@ -0,0 +1,24 @@ +/* -*- 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/. + */ + +#include <templatecontaineritem.hxx> + +TemplateContainerItem::TemplateContainerItem (sal_uInt16 nId) + : mnId(nId) + , mnRegionId(0) +{ +} + +TemplateContainerItem::~TemplateContainerItem () +{ +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ + + diff --git a/sfx2/source/control/templatedefaultview.cxx b/sfx2/source/control/templatedefaultview.cxx new file mode 100644 index 000000000..65415520b --- /dev/null +++ b/sfx2/source/control/templatedefaultview.cxx @@ -0,0 +1,92 @@ +/* -*- 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/. + */ + +#include <templatedefaultview.hxx> +#include <sfx2/thumbnailview.hxx> +#include <templateviewitem.hxx> +#include <sfx2/sfxresid.hxx> +#include <vcl/builderfactory.hxx> +#include <vcl/event.hxx> +#include <vcl/svapp.hxx> + +#include <sfx2/strings.hrc> + +#define MNI_OPEN 1 +#define MNI_EDIT 2 + +#include <officecfg/Office/Common.hxx> + +VCL_BUILDER_FACTORY(TemplateDefaultView) + +static constexpr int gnItemPadding(5); //TODO:: Change padding to 10. It looks really crowded and occupied. +static constexpr long gnTextHeight = 30; + +TemplateDefaultView::TemplateDefaultView( Window* pParent) + : TemplateLocalView(pParent) +{ + tools::Rectangle aScreen = Application::GetScreenPosSizePixel(Application::GetDisplayBuiltInScreen()); + mnItemMaxSize = std::min(aScreen.GetWidth(),aScreen.GetHeight()) > 800 ? 256 : 192; + ThumbnailView::setItemDimensions( mnItemMaxSize, mnItemMaxSize, gnTextHeight, gnItemPadding ); + updateThumbnailDimensions(mnItemMaxSize); + + // startcenter specific settings + maFillColor = Color(officecfg::Office::Common::Help::StartCenter::StartCenterThumbnailsBackgroundColor::get()); + maTextColor = Color(officecfg::Office::Common::Help::StartCenter::StartCenterThumbnailsTextColor::get()); + maHighlightColor = Color(officecfg::Office::Common::Help::StartCenter::StartCenterThumbnailsHighlightColor::get()); + maHighlightTextColor = Color(officecfg::Office::Common::Help::StartCenter::StartCenterThumbnailsHighlightTextColor::get()); + mfHighlightTransparence = 0.25; +} + +void TemplateDefaultView::reload() +{ + TemplateLocalView::reload(); + // Set preferred width + set_width_request(gnTextHeight + mnItemMaxSize + 2*gnItemPadding); +} + +void TemplateDefaultView::showAllTemplates() +{ + mnCurRegionId = 0; + + insertItems(maAllTemplates, false); +} + +void TemplateDefaultView::KeyInput( const KeyEvent& rKEvt ) +{ + ThumbnailView::KeyInput(rKEvt); +} + +void TemplateDefaultView::MouseButtonDown( const MouseEvent& rMEvt ) +{ + if( rMEvt.IsLeft() && rMEvt.GetClicks() == 1 ) + { + size_t nPos = ImplGetItem(rMEvt.GetPosPixel()); + ThumbnailViewItem* pItem = ImplGetItem(nPos); + TemplateViewItem* pViewItem = dynamic_cast<TemplateViewItem*>(pItem); + if(pViewItem) + maOpenTemplateHdl.Call(pViewItem); + return; + } + + TemplateLocalView::MouseButtonDown(rMEvt); +} + +void TemplateDefaultView::createContextMenu() +{ + ScopedVclPtrInstance<PopupMenu> pItemMenu; + pItemMenu->InsertItem(MNI_OPEN,SfxResId(STR_OPEN)); + pItemMenu->InsertItem(MNI_EDIT,SfxResId(STR_EDIT_TEMPLATE)); + deselectItems(); + maSelectedItem->setSelection(true); + pItemMenu->SetSelectHdl(LINK(this, TemplateLocalView, ContextMenuSelectHdl)); + pItemMenu->Execute(this, tools::Rectangle(maPosition,Size(1,1)), PopupMenuFlags::ExecuteDown); + Invalidate(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/templatelocalview.cxx b/sfx2/source/control/templatelocalview.cxx new file mode 100644 index 000000000..ecaddc2c9 --- /dev/null +++ b/sfx2/source/control/templatelocalview.cxx @@ -0,0 +1,1344 @@ +/* -*- 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/. + */ + +#include <sfx2/templatelocalview.hxx> + +#include <comphelper/string.hxx> +#include <sfx2/doctempl.hxx> +#include <sfx2/inputdlg.hxx> +#include <sfx2/sfxresid.hxx> +#include <templatecontaineritem.hxx> +#include <templateviewitem.hxx> +#include <sfx2/docfac.hxx> +#include <tools/urlobj.hxx> +#include <unotools/moduleoptions.hxx> +#include <vcl/help.hxx> +#include <vcl/svapp.hxx> +#include <vcl/weld.hxx> +#include <vcl/commandevent.hxx> +#include <vcl/event.hxx> + +#include <sfx2/strings.hrc> +#include <bitmaps.hlst> + +#define MNI_OPEN 1 +#define MNI_EDIT 2 +//#define MNI_DEFAULT_TEMPLATE 3 +#define MNI_DELETE 4 +#define MNI_RENAME 5 + +using namespace ::com::sun::star; + +bool ViewFilter_Application::isFilteredExtension(FILTER_APPLICATION filter, const OUString &rExt) +{ + bool bRet = rExt == "ott" || rExt == "stw" || rExt == "oth" || rExt == "dot" || rExt == "dotx" || rExt == "otm" + || rExt == "ots" || rExt == "stc" || rExt == "xlt" || rExt == "xltm" || rExt == "xltx" + || rExt == "otp" || rExt == "sti" || rExt == "pot" || rExt == "potm" || rExt == "potx" + || rExt == "otg" || rExt == "std"; + + if (filter == FILTER_APPLICATION::WRITER) + { + bRet = rExt == "ott" || rExt == "stw" || rExt == "oth" || rExt == "dot" || rExt == "dotx" || rExt == "otm"; + } + else if (filter == FILTER_APPLICATION::CALC) + { + bRet = rExt == "ots" || rExt == "stc" || rExt == "xlt" || rExt == "xltm" || rExt == "xltx"; + } + else if (filter == FILTER_APPLICATION::IMPRESS) + { + bRet = rExt == "otp" || rExt == "sti" || rExt == "pot" || rExt == "potm" || rExt == "potx"; + } + else if (filter == FILTER_APPLICATION::DRAW) + { + bRet = rExt == "otg" || rExt == "std"; + } + + return bRet; +} + +bool ViewFilter_Application::isValid (const OUString &rPath) const +{ + INetURLObject aUrl(rPath); + return isFilteredExtension(mApp, aUrl.getExtension()); +} + +bool ViewFilter_Application::operator () (const ThumbnailViewItem *pItem) +{ + const TemplateViewItem *pTempItem = dynamic_cast<const TemplateViewItem*>(pItem); + if (pTempItem) + return isValid(pTempItem->getPath()); + + return true; +} + +TemplateLocalView::TemplateLocalView ( vcl::Window* pParent) + : ThumbnailView(pParent, WB_TABSTOP), + mnCurRegionId(0), + maSelectedItem(nullptr), + mnThumbnailWidth(TEMPLATE_THUMBNAIL_MAX_WIDTH), + mnThumbnailHeight(TEMPLATE_THUMBNAIL_MAX_HEIGHT), + maPosition(0,0), + mpDocTemplates(new SfxDocumentTemplates) +{ +} + +TemplateLocalView::~TemplateLocalView() +{ + disposeOnce(); +} + +void TemplateLocalView::dispose() +{ + maRegions.clear(); + + maAllTemplates.clear(); + + mpDocTemplates.reset(); + ThumbnailView::dispose(); +} + +void TemplateLocalView::Populate () +{ + maRegions.clear(); + maAllTemplates.clear(); + + sal_uInt16 nCount = mpDocTemplates->GetRegionCount(); + for (sal_uInt16 i = 0; i < nCount; ++i) + { + OUString aRegionName(mpDocTemplates->GetFullRegionName(i)); + + std::unique_ptr<TemplateContainerItem> pItem(new TemplateContainerItem( i+1 )); + pItem->mnRegionId = i; + pItem->maTitle = aRegionName; + + sal_uInt16 nEntries = mpDocTemplates->GetCount(i); + + for (sal_uInt16 j = 0; j < nEntries; ++j) + { + OUString aName = mpDocTemplates->GetName(i,j); + OUString aURL = mpDocTemplates->GetPath(i,j); + + TemplateItemProperties aProperties; + aProperties.nId = j+1; + aProperties.nDocId = j; + aProperties.nRegionId = i; + aProperties.aName = aName; + aProperties.aPath = aURL; + aProperties.aRegionName = aRegionName; + aProperties.aThumbnail = TemplateLocalView::fetchThumbnail(aURL, + mnThumbnailWidth, + mnThumbnailHeight); + + pItem->maTemplates.push_back(aProperties); + maAllTemplates.push_back(aProperties); + } + + maRegions.push_back(std::move(pItem)); + } +} + +void TemplateLocalView::reload () +{ + mpDocTemplates->Update(); + + Populate(); + + // Check if we are currently browsing a region or root folder + if (mnCurRegionId) + { + sal_uInt16 nRegionId = mnCurRegionId - 1; //Is offset by 1 + + for (auto const & pRegion : maRegions) + { + if (pRegion->mnRegionId == nRegionId) + { + showRegion(pRegion.get()); + break; + } + } + } + else + showAllTemplates(); + + //No items should be selected by default + deselectItems(); +} + +void TemplateLocalView::showAllTemplates() +{ + mnCurRegionId = 0; + + insertItems(maAllTemplates, false, true); +} + +void TemplateLocalView::showRegion(TemplateContainerItem const *pItem) +{ + mnCurRegionId = pItem->mnRegionId+1; + + insertItems(pItem->maTemplates); +} + +IMPL_LINK(TemplateLocalView, ContextMenuSelectHdl, Menu*, pMenu, bool) +{ + sal_uInt16 nMenuId = pMenu->GetCurItemId(); + + switch(nMenuId) + { + case MNI_OPEN: + maOpenTemplateHdl.Call(maSelectedItem); + break; + case MNI_EDIT: + maEditTemplateHdl.Call(maSelectedItem); + break; + case MNI_RENAME: + { + InputDialog aTitleEditDlg(GetFrameWeld(), SfxResId(STR_RENAME_TEMPLATE)); + OUString sOldTitle = maSelectedItem->getTitle(); + aTitleEditDlg.SetEntryText(sOldTitle); + aTitleEditDlg.HideHelpBtn(); + + if (!aTitleEditDlg.run()) + break; + OUString sNewTitle = comphelper::string::strip(aTitleEditDlg.GetEntryText(), ' '); + + if ( !sNewTitle.isEmpty() && sNewTitle != sOldTitle ) + { + maSelectedItem->setTitle(sNewTitle); + } + } + break; + case MNI_DELETE: + { + std::unique_ptr<weld::MessageDialog> xQueryDlg(Application::CreateMessageDialog(GetFrameWeld(), VclMessageType::Question, VclButtonsType::YesNo, + SfxResId(STR_QMSG_SEL_TEMPLATE_DELETE))); + if (xQueryDlg->run() != RET_YES) + break; + + reload(); + } + break; + default: + break; + } + + return false; +} + +bool TemplateLocalView::renameItem(ThumbnailViewItem* pItem, const OUString& sNewTitle) +{ + sal_uInt16 nRegionId = 0; + sal_uInt16 nDocId = USHRT_MAX; + TemplateViewItem* pDocItem = dynamic_cast<TemplateViewItem*>( pItem ); + + if ( pDocItem ) + { + nRegionId = pDocItem->mnRegionId; + nDocId = pDocItem->mnDocId; + } + + return mpDocTemplates->SetName( sNewTitle, nRegionId, nDocId ); +} + +void TemplateLocalView::insertItems(const std::vector<TemplateItemProperties> &rTemplates, bool isRegionSelected, bool bShowCategoryInTooltip) +{ + std::vector<std::unique_ptr<ThumbnailViewItem>> aItems(rTemplates.size()); + for (size_t i = 0, n = rTemplates.size(); i < n; ++i ) + { + const TemplateItemProperties *pCur = &rTemplates[i]; + + std::unique_ptr<TemplateViewItem> pChild; + if(isRegionSelected) + pChild.reset(new TemplateViewItem(*this, pCur->nId)); + else + pChild.reset(new TemplateViewItem(*this, i+1)); + + pChild->mnDocId = pCur->nDocId; + pChild->mnRegionId = pCur->nRegionId; + pChild->maTitle = pCur->aName; + pChild->setPath(pCur->aPath); + + if(!bShowCategoryInTooltip) + pChild->setHelpText(pCur->aName); + else + { + OUString sHelpText = SfxResId(STR_TEMPLATE_TOOLTIP); + sHelpText = (sHelpText.replaceFirst("$1", pCur->aName)).replaceFirst("$2", pCur->aRegionName); + pChild->setHelpText(sHelpText); + } + + pChild->maPreview1 = pCur->aThumbnail; + + if(IsDefaultTemplate(pCur->aPath)) + pChild->showDefaultIcon(true); + + if ( pCur->aThumbnail.IsEmpty() ) + { + // Use the default thumbnail if we have nothing else + pChild->maPreview1 = TemplateLocalView::getDefaultThumbnail(pCur->aPath); + } + + aItems[i] = std::move(pChild); + } + + updateItems(std::move(aItems)); +} + +void TemplateLocalView::updateThumbnailDimensions(long itemMaxSize) +{ + mnThumbnailWidth = itemMaxSize; + mnThumbnailHeight = itemMaxSize; +} + + +void TemplateLocalView::MouseButtonDown( const MouseEvent& rMEvt ) +{ + GrabFocus(); + ThumbnailView::MouseButtonDown(rMEvt); +} + +void TemplateLocalView::RequestHelp( const HelpEvent& rHEvt ) +{ + if ( rHEvt.GetMode() & HelpEventMode::QUICK ) + { + tools::Rectangle aRect( OutputToScreenPixel( GetPosPixel() ), GetSizePixel() ); + Help::ShowQuickHelp( this, aRect, GetQuickHelpText(), + QuickHelpFlags::CtrlText | QuickHelpFlags::TipStyleBalloon ); + return; + } + + ThumbnailView::RequestHelp( rHEvt ); +} + +void TemplateLocalView::Command( const CommandEvent& rCEvt ) +{ + if ( rCEvt.GetCommand() == CommandEventId::ContextMenu ) + { + if(rCEvt.IsMouseEvent()) + { + deselectItems(); + size_t nPos = ImplGetItem(rCEvt.GetMousePosPixel()); + Point aPosition (rCEvt.GetMousePosPixel()); + maPosition = aPosition; + ThumbnailViewItem* pItem = ImplGetItem(nPos); + const TemplateViewItem *pViewItem = dynamic_cast<const TemplateViewItem*>(pItem); + + if(pViewItem) + { + maSelectedItem = dynamic_cast<TemplateViewItem*>(pItem); + maCreateContextMenuHdl.Call(pItem); + } + } + else + { + for (ThumbnailViewItem* pItem : mFilteredItemList) + { + //create context menu for the first selected item + if (pItem->isSelected()) + { + deselectItems(); + pItem->setSelection(true); + tools::Rectangle aRect = pItem->getDrawArea(); + maPosition = aRect.Center(); + maSelectedItem = dynamic_cast<TemplateViewItem*>(pItem); + maCreateContextMenuHdl.Call(pItem); + break; + } + } + } + } + + ThumbnailView::Command(rCEvt); +} + +void TemplateLocalView::KeyInput( const KeyEvent& rKEvt ) +{ + vcl::KeyCode aKeyCode = rKEvt.GetKeyCode(); + + if(aKeyCode == ( KEY_MOD1 | KEY_A ) ) + { + for (ThumbnailViewItem* pItem : mFilteredItemList) + { + if (!pItem->isSelected()) + { + pItem->setSelection(true); + } + } + + if (IsReallyVisible() && IsUpdateMode()) + Invalidate(); + return; + } + else if( aKeyCode == KEY_DELETE && !mFilteredItemList.empty()) + { + std::unique_ptr<weld::MessageDialog> xQueryDlg(Application::CreateMessageDialog(GetFrameWeld(), VclMessageType::Question, VclButtonsType::YesNo, + SfxResId(STR_QMSG_SEL_TEMPLATE_DELETE))); + if (xQueryDlg->run() != RET_YES) + return; + + reload(); + } + + ThumbnailView::KeyInput(rKEvt); +} + + +void TemplateLocalView::setCreateContextMenuHdl(const Link<ThumbnailViewItem*,void> &rLink) +{ + maCreateContextMenuHdl = rLink; +} + +void TemplateLocalView::setOpenTemplateHdl(const Link<ThumbnailViewItem*,void> &rLink) +{ + maOpenTemplateHdl = rLink; +} + +void TemplateLocalView::setEditTemplateHdl(const Link<ThumbnailViewItem*,void> &rLink) +{ + maEditTemplateHdl = rLink; +} + +BitmapEx TemplateLocalView::scaleImg (const BitmapEx &rImg, long width, long height) +{ + BitmapEx aImg = rImg; + + if (!rImg.IsEmpty()) + { + Size aSize = rImg.GetSizePixel(); + + if (aSize.Width() == 0) + aSize.setWidth( 1 ); + + if (aSize.Height() == 0) + aSize.setHeight( 1 ); + + // make the picture fit the given width/height constraints + double nRatio = std::min(double(width)/double(aSize.Width()), double(height)/double(aSize.Height())); + + aImg.Scale(Size(aSize.Width() * nRatio, aSize.Height() * nRatio)); + } + + return aImg; +} + +bool TemplateLocalView::IsDefaultTemplate(const OUString& rPath) +{ + SvtModuleOptions aModOpt; + const css::uno::Sequence<OUString> &aServiceNames = aModOpt.GetAllServiceNames(); + + return std::any_of(aServiceNames.begin(), aServiceNames.end(), [&rPath](const OUString& rName) { + return SfxObjectFactory::GetStandardTemplate(rName).match(rPath); }); +} + +BitmapEx TemplateLocalView::getDefaultThumbnail( const OUString& rPath ) +{ + BitmapEx aImg; + INetURLObject aUrl(rPath); + OUString aExt = aUrl.getExtension(); + + if ( ViewFilter_Application::isFilteredExtension( FILTER_APPLICATION::WRITER, aExt) ) + aImg = BitmapEx(SFX_THUMBNAIL_TEXT); + else if ( ViewFilter_Application::isFilteredExtension( FILTER_APPLICATION::CALC, aExt) ) + aImg = BitmapEx(SFX_THUMBNAIL_SHEET); + else if ( ViewFilter_Application::isFilteredExtension( FILTER_APPLICATION::IMPRESS, aExt) ) + aImg = BitmapEx(SFX_THUMBNAIL_PRESENTATION); + else if ( ViewFilter_Application::isFilteredExtension( FILTER_APPLICATION::DRAW, aExt) ) + aImg = BitmapEx(SFX_THUMBNAIL_DRAWING); + + return aImg; +} + +BitmapEx TemplateLocalView::fetchThumbnail (const OUString &msURL, long width, long height) +{ + return TemplateLocalView::scaleImg(ThumbnailView::readThumbnail(msURL), width, height); +} + +void TemplateLocalView::OnItemDblClicked (ThumbnailViewItem *pItem) +{ + TemplateViewItem* pViewItem = dynamic_cast<TemplateViewItem*>(pItem); + + if( pViewItem ) + maOpenTemplateHdl.Call(pViewItem); +} + +SfxTemplateLocalView::SfxTemplateLocalView(std::unique_ptr<weld::ScrolledWindow> xWindow, + std::unique_ptr<weld::Menu> xMenu) + : SfxThumbnailView(std::move(xWindow), std::move(xMenu)) + , mnCurRegionId(0) + , maSelectedItem(nullptr) + , maPosition(0,0) + , mpDocTemplates(new SfxDocumentTemplates) +{ +} + +SfxTemplateLocalView::~SfxTemplateLocalView() +{ +} + +void SfxTemplateLocalView::Populate() +{ + maRegions.clear(); + maAllTemplates.clear(); + + sal_uInt16 nCount = mpDocTemplates->GetRegionCount(); + for (sal_uInt16 i = 0; i < nCount; ++i) + { + OUString aRegionName(mpDocTemplates->GetFullRegionName(i)); + + std::unique_ptr<TemplateContainerItem> pItem(new TemplateContainerItem( i+1 )); + pItem->mnRegionId = i; + pItem->maTitle = aRegionName; + + sal_uInt16 nEntries = mpDocTemplates->GetCount(i); + + for (sal_uInt16 j = 0; j < nEntries; ++j) + { + OUString aName = mpDocTemplates->GetName(i,j); + OUString aURL = mpDocTemplates->GetPath(i,j); + + TemplateItemProperties aProperties; + aProperties.nId = j+1; + aProperties.nDocId = j; + aProperties.nRegionId = i; + aProperties.aName = aName; + aProperties.aPath = aURL; + aProperties.aRegionName = aRegionName; + aProperties.aThumbnail = TemplateLocalView::fetchThumbnail(aURL, + TEMPLATE_THUMBNAIL_MAX_WIDTH, + TEMPLATE_THUMBNAIL_MAX_HEIGHT); + + pItem->maTemplates.push_back(aProperties); + maAllTemplates.push_back(aProperties); + } + + maRegions.push_back(std::move(pItem)); + } +} + +void SfxTemplateLocalView::reload() +{ + mpDocTemplates->Update(); + + Populate(); + + // Check if we are currently browsing a region or root folder + if (mnCurRegionId) + { + sal_uInt16 nRegionId = mnCurRegionId - 1; //Is offset by 1 + + for (auto const & pRegion : maRegions) + { + if (pRegion->mnRegionId == nRegionId) + { + showRegion(pRegion.get()); + break; + } + } + } + else + showAllTemplates(); + + //No items should be selected by default + deselectItems(); +} + +void SfxTemplateLocalView::showAllTemplates() +{ + mnCurRegionId = 0; + + insertItems(maAllTemplates, false, true); + + maOpenRegionHdl.Call(nullptr); +} + +void SfxTemplateLocalView::showRegion(TemplateContainerItem const *pItem) +{ + mnCurRegionId = pItem->mnRegionId+1; + + insertItems(pItem->maTemplates); + + maOpenRegionHdl.Call(nullptr); +} + +void SfxTemplateLocalView::showRegion(const OUString &rName) +{ + for (auto const & pRegion : maRegions) + { + if (pRegion->maTitle == rName) + { + showRegion(pRegion.get()); + break; + } + } +} + +TemplateContainerItem* SfxTemplateLocalView::getRegion(OUString const & rName) +{ + for (auto const & pRegion : maRegions) + if (pRegion->maTitle == rName) + return pRegion.get(); + + return nullptr; +} + +void SfxTemplateLocalView::createContextMenu(const bool bIsDefault) +{ + mxContextMenu->clear(); + mxContextMenu->append("open",SfxResId(STR_OPEN)); + mxContextMenu->append("edit",SfxResId(STR_EDIT_TEMPLATE)); + + if(!bIsDefault) + mxContextMenu->append("default",SfxResId(STR_DEFAULT_TEMPLATE)); + else + mxContextMenu->append("default",SfxResId(STR_RESET_DEFAULT)); + + mxContextMenu->append_separator("separator"); + mxContextMenu->append("rename",SfxResId(STR_SFX_RENAME)); + mxContextMenu->append("delete",SfxResId(STR_DELETE)); + deselectItems(); + maSelectedItem->setSelection(true); + maItemStateHdl.Call(maSelectedItem); + ContextMenuSelectHdl(mxContextMenu->popup_at_rect(GetDrawingArea(), tools::Rectangle(maPosition, Size(1,1)))); + Invalidate(); +} + +void SfxTemplateLocalView::ContextMenuSelectHdl(const OString& rIdent) +{ + if (rIdent == "open") + maOpenTemplateHdl.Call(maSelectedItem); + else if (rIdent == "edit") + maEditTemplateHdl.Call(maSelectedItem); + else if (rIdent == "rename") + { + InputDialog aTitleEditDlg(GetDrawingArea(), SfxResId(STR_RENAME_TEMPLATE)); + OUString sOldTitle = maSelectedItem->getTitle(); + aTitleEditDlg.SetEntryText(sOldTitle); + aTitleEditDlg.HideHelpBtn(); + + if (!aTitleEditDlg.run()) + return; + OUString sNewTitle = comphelper::string::strip(aTitleEditDlg.GetEntryText(), ' '); + + if ( !sNewTitle.isEmpty() && sNewTitle != sOldTitle ) + { + maSelectedItem->setTitle(sNewTitle); + } + } + else if (rIdent == "delete") + { + std::unique_ptr<weld::MessageDialog> xQueryDlg(Application::CreateMessageDialog(GetDrawingArea(), VclMessageType::Question, VclButtonsType::YesNo, + SfxResId(STR_QMSG_SEL_TEMPLATE_DELETE))); + if (xQueryDlg->run() != RET_YES) + return; + + maDeleteTemplateHdl.Call(maSelectedItem); + reload(); + } + else if (rIdent == "default") + maDefaultTemplateHdl.Call(maSelectedItem); +} + +sal_uInt16 SfxTemplateLocalView::getRegionId(size_t pos) const +{ + assert(pos < maRegions.size()); + + return maRegions[pos]->mnId; +} + +sal_uInt16 SfxTemplateLocalView::getRegionId(OUString const & sRegion) const +{ + for (auto const & pRegion : maRegions) + { + if (pRegion->maTitle == sRegion) + return pRegion->mnId; + } + + return 0; +} + +OUString SfxTemplateLocalView::getRegionName(const sal_uInt16 nRegionId) const +{ + return mpDocTemplates->GetRegionName(nRegionId); +} + +OUString SfxTemplateLocalView::getRegionItemName(const sal_uInt16 nItemId) const +{ + for (auto const & pRegion : maRegions) + { + if (pRegion->mnId == nItemId) + return pRegion->maTitle; + } + + return OUString(); +} + +std::vector<OUString> SfxTemplateLocalView::getFolderNames() +{ + size_t n = maRegions.size(); + std::vector<OUString> ret(n); + + for (size_t i = 0; i < n; ++i) + ret[i] = maRegions[i]->maTitle; + + return ret; +} + +std::vector<TemplateItemProperties> +SfxTemplateLocalView::getFilteredItems(const std::function<bool (const TemplateItemProperties&)> &rFunc) const +{ + std::vector<TemplateItemProperties> aItems; + + if (mnCurRegionId) + { + TemplateContainerItem *pFolderItem = maRegions[mnCurRegionId-1].get(); + + for (const TemplateItemProperties & rItemProps : pFolderItem->maTemplates) + { + if (rFunc(rItemProps)) + aItems.push_back(rItemProps); + } + } + else + { + for (auto const & pFolderItem : maRegions) + { + for (const TemplateItemProperties & rItemProps : pFolderItem->maTemplates) + { + if (rFunc(rItemProps)) + aItems.push_back(rItemProps); + } + } + } + + return aItems; +} + +sal_uInt16 SfxTemplateLocalView::createRegion(const OUString &rName) +{ + sal_uInt16 nRegionId = mpDocTemplates->GetRegionCount(); // Next regionId + sal_uInt16 nItemId = getNextItemId(); + + if (!mpDocTemplates->InsertDir(rName,nRegionId)) + return 0; + + // Insert to the region cache list and to the thumbnail item list + std::unique_ptr<TemplateContainerItem> pItem(new TemplateContainerItem( nItemId )); + pItem->mnRegionId = nRegionId; + pItem->maTitle = rName; + + maRegions.push_back(std::move(pItem)); + + return nItemId; +} + +bool SfxTemplateLocalView::renameRegion(const OUString &rTitle, const OUString &rNewTitle) +{ + TemplateContainerItem *pRegion = getRegion(rTitle); + + if(pRegion) + { + sal_uInt16 nRegionId = pRegion->mnRegionId; + return mpDocTemplates->SetName( rNewTitle, nRegionId, USHRT_MAX/*nDocId*/ ); + } + return false; +} + +bool SfxTemplateLocalView::removeRegion(const sal_uInt16 nItemId) +{ + sal_uInt16 nRegionId = USHRT_MAX; + + // Remove from the region cache list + for (auto pRegionIt = maRegions.begin(); pRegionIt != maRegions.end();) + { + if ( (*pRegionIt)->mnId == nItemId ) + { + if (!mpDocTemplates->Delete((*pRegionIt)->mnRegionId,USHRT_MAX)) + return false; + + nRegionId = (*pRegionIt)->mnRegionId; + + pRegionIt = maRegions.erase(pRegionIt); + } + else + { + // Synchronize regions cache ids with SfxDocumentTemplates + if (nRegionId != USHRT_MAX && (*pRegionIt)->mnRegionId > nRegionId) + --(*pRegionIt)->mnRegionId; + + ++pRegionIt; + } + } + + if (nRegionId == USHRT_MAX) + return false; + + // Synchronize view regions ids with SfxDocumentTemplates + for (auto const& region : maRegions) + { + if (region->mnRegionId > nRegionId) + --region->mnRegionId; + } + + return true; +} + +bool SfxTemplateLocalView::removeTemplate (const sal_uInt16 nItemId, const sal_uInt16 nSrcItemId) +{ + for (auto const & pRegion : maRegions) + { + if (pRegion->mnId == nSrcItemId) + { + TemplateContainerItem *pItem = pRegion.get(); + auto pIter = std::find_if(pItem->maTemplates.begin(), pItem->maTemplates.end(), + [nItemId](const TemplateItemProperties& rTemplate) { return rTemplate.nId == nItemId; }); + if (pIter != pItem->maTemplates.end()) + { + if (!mpDocTemplates->Delete(pItem->mnRegionId,pIter->nDocId)) + return false; + + pIter = pItem->maTemplates.erase(pIter); + + if (pRegion->mnRegionId == mnCurRegionId-1) + { + RemoveItem(nItemId); + Invalidate(); + } + + // Update Doc Idx for all templates that follow + for (; pIter != pItem->maTemplates.end(); ++pIter) + pIter->nDocId = pIter->nDocId - 1; + } + + CalculateItemPositions(); + break; + } + } + + return true; +} + +bool SfxTemplateLocalView::moveTemplate (const ThumbnailViewItem *pItem, const sal_uInt16 nSrcItem, + const sal_uInt16 nTargetItem) +{ + TemplateContainerItem *pTarget = nullptr; + TemplateContainerItem *pSrc = nullptr; + + for (auto const & pRegion : maRegions) + { + if (pRegion->mnId == nTargetItem) + pTarget = pRegion.get(); + else if (pRegion->mnId == nSrcItem) + pSrc = pRegion.get(); + } + + if (pTarget && pSrc) + { + sal_uInt16 nSrcRegionId = pSrc->mnRegionId; + sal_uInt16 nTargetRegion = pTarget->mnRegionId; + sal_uInt16 nTargetIdx = mpDocTemplates->GetCount(nTargetRegion); // Next Idx + + const TemplateViewItem *pViewItem = static_cast<const TemplateViewItem*>(pItem); + + bool bCopy = !mpDocTemplates->Move(nTargetRegion,nTargetIdx,nSrcRegionId,pViewItem->mnDocId); + + if (bCopy) + { + OUString sQuery = SfxResId(STR_MSG_QUERY_COPY).replaceFirst("$1", pViewItem->maTitle).replaceFirst("$2", + getRegionName(nTargetRegion)); + + std::unique_ptr<weld::MessageDialog> xQueryDlg(Application::CreateMessageDialog(GetDrawingArea(), VclMessageType::Question, VclButtonsType::YesNo, sQuery)); + if (xQueryDlg->run() != RET_YES) + return false; + + if (!mpDocTemplates->Copy(nTargetRegion,nTargetIdx,nSrcRegionId,pViewItem->mnDocId)) + return false; + } + // move template to destination + + TemplateItemProperties aTemplateItem; + aTemplateItem.nId = nTargetIdx + 1; + aTemplateItem.nDocId = nTargetIdx; + aTemplateItem.nRegionId = nTargetRegion; + aTemplateItem.aName = pViewItem->maTitle; + aTemplateItem.aPath = mpDocTemplates->GetPath(nTargetRegion,nTargetIdx); + aTemplateItem.aRegionName = pViewItem->maHelpText; + aTemplateItem.aThumbnail = pViewItem->maPreview1; + + pTarget->maTemplates.push_back(aTemplateItem); + + if (!bCopy) + { + // remove template from region cached data + + std::vector<TemplateItemProperties>::iterator aIter; + for (aIter = pSrc->maTemplates.begin(); aIter != pSrc->maTemplates.end();) + { + if (aIter->nDocId == pViewItem->mnDocId) + { + aIter = pSrc->maTemplates.erase(aIter); + } + else + { + // Keep region document id synchronized with SfxDocumentTemplates + if (aIter->nDocId > pViewItem->mnDocId) + --aIter->nDocId; + + ++aIter; + } + } + + // Keep view document id synchronized with SfxDocumentTemplates + for (auto const& item : mItemList) + { + auto pTemplateViewItem = static_cast<TemplateViewItem*>(item.get()); + if (pTemplateViewItem->mnDocId > pViewItem->mnDocId) + --pTemplateViewItem->mnDocId; + } + } + + CalculateItemPositions(); + Invalidate(); + + return true; + } + + return false; +} + +void SfxTemplateLocalView::moveTemplates(const std::set<const ThumbnailViewItem*, selection_cmp_fn> &rItems, + const sal_uInt16 nTargetItem) +{ + TemplateContainerItem *pTarget = nullptr; + TemplateContainerItem *pSrc = nullptr; + + for (auto const & pRegion : maRegions) + { + if (pRegion->mnId == nTargetItem) + pTarget = pRegion.get(); + } + + if (!pTarget) + return; + + bool refresh = false; + + sal_uInt16 nTargetRegion = pTarget->mnRegionId; + sal_uInt16 nTargetIdx = mpDocTemplates->GetCount(nTargetRegion); // Next Idx + std::vector<sal_uInt16> aItemIds; // List of moved items ids (also prevents the invalidation of rItems iterators when we remove them as we go) + + std::set<const ThumbnailViewItem*,selection_cmp_fn>::const_iterator aSelIter; + for ( aSelIter = rItems.begin(); aSelIter != rItems.end(); ++aSelIter, ++nTargetIdx ) + { + const TemplateViewItem *pViewItem = static_cast<const TemplateViewItem*>(*aSelIter); + sal_uInt16 nSrcRegionId = pViewItem->mnRegionId; + + for (auto const & pRegion : maRegions) + { + if (pRegion->mnRegionId == nSrcRegionId) + pSrc = pRegion.get(); + } + + if(pSrc) + { + bool bCopy = !mpDocTemplates->Move(nTargetRegion,nTargetIdx,nSrcRegionId,pViewItem->mnDocId); + + if (bCopy) + { + OUString sQuery = SfxResId(STR_MSG_QUERY_COPY).replaceFirst("$1", pViewItem->maTitle).replaceFirst("$2", + getRegionName(nTargetRegion)); + std::unique_ptr<weld::MessageDialog> xQueryDlg(Application::CreateMessageDialog(GetDrawingArea(), VclMessageType::Question, VclButtonsType::YesNo, sQuery)); + if (xQueryDlg->run() != RET_YES) + { + OUString sMsg(SfxResId(STR_MSG_ERROR_LOCAL_MOVE)); + sMsg = sMsg.replaceFirst("$1",getRegionName(nTargetRegion)); + std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(GetDrawingArea(), + VclMessageType::Warning, VclButtonsType::Ok, sMsg.replaceFirst( "$2",pViewItem->maTitle))); + xBox->run(); + + return; //return if any single move operation fails + } + + if (!mpDocTemplates->Copy(nTargetRegion,nTargetIdx,nSrcRegionId,pViewItem->mnDocId)) + { + continue; + } + } + + // move template to destination + + TemplateItemProperties aTemplateItem; + aTemplateItem.nId = nTargetIdx + 1; + aTemplateItem.nDocId = nTargetIdx; + aTemplateItem.nRegionId = nTargetRegion; + aTemplateItem.aName = pViewItem->maTitle; + aTemplateItem.aPath = mpDocTemplates->GetPath(nTargetRegion,nTargetIdx); + aTemplateItem.aRegionName = pViewItem->maHelpText; + aTemplateItem.aThumbnail = pViewItem->maPreview1; + + pTarget->maTemplates.push_back(aTemplateItem); + + if (!bCopy) + { + // remove template from region cached data + + std::vector<TemplateItemProperties>::iterator pPropIter; + for (pPropIter = pSrc->maTemplates.begin(); pPropIter != pSrc->maTemplates.end();) + { + if (pPropIter->nDocId == pViewItem->mnDocId) + { + pPropIter = pSrc->maTemplates.erase(pPropIter); + aItemIds.push_back(pViewItem->mnDocId + 1);//mnid + } + else + { + // Keep region document id synchronized with SfxDocumentTemplates + if (pPropIter->nDocId > pViewItem->mnDocId) + --pPropIter->nDocId; + + ++pPropIter; + } + } + + // Keep view document id synchronized with SfxDocumentTemplates + for (auto const& item : mItemList) + { + auto pTemplateViewItem = static_cast<TemplateViewItem*>(item.get()); + if (pTemplateViewItem->mnDocId > pViewItem->mnDocId) + --pTemplateViewItem->mnDocId; + } + } + } + + refresh = true; + } + + // Remove items from the current view + for (auto const& itemId : aItemIds) + RemoveItem(itemId); + + if (refresh) + { + CalculateItemPositions(); + Invalidate(); + } +} + +bool SfxTemplateLocalView::copyFrom (TemplateContainerItem *pItem, const OUString &rPath) +{ + sal_uInt16 nId = 1; + sal_uInt16 nDocId = 0; + sal_uInt16 nRegionId = pItem->mnRegionId; + OUString aPath(rPath); + + if (!pItem->maTemplates.empty()) + { + nId = pItem->maTemplates.back().nId+1; + nDocId = pItem->maTemplates.back().nDocId+1; + } + + if (mpDocTemplates->CopyFrom(nRegionId,nDocId,aPath)) + { + TemplateItemProperties aTemplate; + aTemplate.nId = nId; + aTemplate.nDocId = nDocId; + aTemplate.nRegionId = nRegionId; + aTemplate.aName = aPath; + aTemplate.aThumbnail = SfxTemplateLocalView::fetchThumbnail(rPath, + TEMPLATE_THUMBNAIL_MAX_WIDTH, + TEMPLATE_THUMBNAIL_MAX_HEIGHT); + aTemplate.aPath = rPath; + aTemplate.aRegionName = getRegionName(nRegionId); + + pItem->maTemplates.push_back(aTemplate); + + CalculateItemPositions(); + + return true; + } + + return false; +} + +bool SfxTemplateLocalView::exportTo(const sal_uInt16 nItemId, const sal_uInt16 nRegionItemId, const OUString &rName) +{ + for (auto const & pRegItem : maRegions) + { + if (pRegItem->mnId == nRegionItemId) + { + for (auto const& elem : pRegItem->maTemplates) + { + if (elem.nId == nItemId) + { + return mpDocTemplates->CopyTo(pRegItem->mnRegionId,elem.nDocId,rName); + } + } + + break; + } + } + + return false; +} + +bool SfxTemplateLocalView::renameItem(ThumbnailViewItem* pItem, const OUString& sNewTitle) +{ + sal_uInt16 nRegionId = 0; + sal_uInt16 nDocId = USHRT_MAX; + TemplateViewItem* pDocItem = dynamic_cast<TemplateViewItem*>( pItem ); + + if ( pDocItem ) + { + nRegionId = pDocItem->mnRegionId; + nDocId = pDocItem->mnDocId; + } + + return mpDocTemplates->SetName( sNewTitle, nRegionId, nDocId ); +} + +void SfxTemplateLocalView::insertItems(const std::vector<TemplateItemProperties> &rTemplates, bool isRegionSelected, bool bShowCategoryInTooltip) +{ + std::vector<std::unique_ptr<ThumbnailViewItem>> aItems(rTemplates.size()); + for (size_t i = 0, n = rTemplates.size(); i < n; ++i ) + { + const TemplateItemProperties *pCur = &rTemplates[i]; + + std::unique_ptr<TemplateViewItem> pChild; + if(isRegionSelected) + pChild.reset(new TemplateViewItem(*this, pCur->nId)); + else + pChild.reset(new TemplateViewItem(*this, i+1)); + + pChild->mnDocId = pCur->nDocId; + pChild->mnRegionId = pCur->nRegionId; + pChild->maTitle = pCur->aName; + pChild->setPath(pCur->aPath); + + if(!bShowCategoryInTooltip) + pChild->setHelpText(pCur->aName); + else + { + OUString sHelpText = SfxResId(STR_TEMPLATE_TOOLTIP); + sHelpText = (sHelpText.replaceFirst("$1", pCur->aName)).replaceFirst("$2", pCur->aRegionName); + pChild->setHelpText(sHelpText); + } + + pChild->maPreview1 = pCur->aThumbnail; + + if(IsDefaultTemplate(pCur->aPath)) + pChild->showDefaultIcon(true); + + if ( pCur->aThumbnail.IsEmpty() ) + { + // Use the default thumbnail if we have nothing else + pChild->maPreview1 = SfxTemplateLocalView::getDefaultThumbnail(pCur->aPath); + } + + aItems[i] = std::move(pChild); + } + + updateItems(std::move(aItems)); +} + +bool SfxTemplateLocalView::MouseButtonDown( const MouseEvent& rMEvt ) +{ + GrabFocus(); + return SfxThumbnailView::MouseButtonDown(rMEvt); +} + +bool SfxTemplateLocalView::Command(const CommandEvent& rCEvt) +{ + if (rCEvt.GetCommand() != CommandEventId::ContextMenu) + return CustomWidgetController::Command(rCEvt); + + if (rCEvt.IsMouseEvent()) + { + deselectItems(); + size_t nPos = ImplGetItem(rCEvt.GetMousePosPixel()); + Point aPosition(rCEvt.GetMousePosPixel()); + maPosition = aPosition; + ThumbnailViewItem* pItem = ImplGetItem(nPos); + const TemplateViewItem *pViewItem = dynamic_cast<const TemplateViewItem*>(pItem); + + if(pViewItem) + { + maSelectedItem = dynamic_cast<TemplateViewItem*>(pItem); + maCreateContextMenuHdl.Call(pItem); + } + } + else + { + for (ThumbnailViewItem* pItem : mFilteredItemList) + { + //create context menu for the first selected item + if (pItem->isSelected()) + { + deselectItems(); + pItem->setSelection(true); + maItemStateHdl.Call(pItem); + tools::Rectangle aRect = pItem->getDrawArea(); + maPosition = aRect.Center(); + maSelectedItem = dynamic_cast<TemplateViewItem*>(pItem); + maCreateContextMenuHdl.Call(pItem); + break; + } + } + } + return true; +} + +bool SfxTemplateLocalView::KeyInput( const KeyEvent& rKEvt ) +{ + vcl::KeyCode aKeyCode = rKEvt.GetKeyCode(); + + if(aKeyCode == ( KEY_MOD1 | KEY_A ) ) + { + for (ThumbnailViewItem* pItem : mFilteredItemList) + { + if (!pItem->isSelected()) + { + pItem->setSelection(true); + maItemStateHdl.Call(pItem); + } + } + + if (IsReallyVisible() && IsUpdateMode()) + Invalidate(); + return true; + } + else if( aKeyCode == KEY_DELETE && !mFilteredItemList.empty()) + { + std::unique_ptr<weld::MessageDialog> xQueryDlg(Application::CreateMessageDialog(GetDrawingArea(), VclMessageType::Question, VclButtonsType::YesNo, + SfxResId(STR_QMSG_SEL_TEMPLATE_DELETE))); + if (xQueryDlg->run() != RET_YES) + return true; + + //copy to avoid changing filtered item list during deletion + ThumbnailValueItemList mFilteredItemListCopy = mFilteredItemList; + + for (ThumbnailViewItem* pItem : mFilteredItemListCopy) + { + if (pItem->isSelected()) + { + maDeleteTemplateHdl.Call(pItem); + } + } + reload(); + } + + return SfxThumbnailView::KeyInput(rKEvt); +} + +void SfxTemplateLocalView::setOpenRegionHdl(const Link<void*,void> &rLink) +{ + maOpenRegionHdl = rLink; +} + +void SfxTemplateLocalView::setCreateContextMenuHdl(const Link<ThumbnailViewItem*,void> &rLink) +{ + maCreateContextMenuHdl = rLink; +} + +void SfxTemplateLocalView::setOpenTemplateHdl(const Link<ThumbnailViewItem*,void> &rLink) +{ + maOpenTemplateHdl = rLink; +} + +void SfxTemplateLocalView::setEditTemplateHdl(const Link<ThumbnailViewItem*,void> &rLink) +{ + maEditTemplateHdl = rLink; +} + +void SfxTemplateLocalView::setDeleteTemplateHdl(const Link<ThumbnailViewItem*,void> &rLink) +{ + maDeleteTemplateHdl = rLink; +} + +void SfxTemplateLocalView::setDefaultTemplateHdl(const Link<ThumbnailViewItem*,void> &rLink) +{ + maDefaultTemplateHdl = rLink; +} + +BitmapEx SfxTemplateLocalView::scaleImg (const BitmapEx &rImg, long width, long height) +{ + BitmapEx aImg = rImg; + + if (!rImg.IsEmpty()) + { + Size aSize = rImg.GetSizePixel(); + + if (aSize.Width() == 0) + aSize.setWidth( 1 ); + + if (aSize.Height() == 0) + aSize.setHeight( 1 ); + + // make the picture fit the given width/height constraints + double nRatio = std::min(double(width)/double(aSize.Width()), double(height)/double(aSize.Height())); + + aImg.Scale(Size(aSize.Width() * nRatio, aSize.Height() * nRatio)); + } + + return aImg; +} + +bool SfxTemplateLocalView::IsDefaultTemplate(const OUString& rPath) +{ + SvtModuleOptions aModOpt; + const css::uno::Sequence<OUString> &aServiceNames = aModOpt.GetAllServiceNames(); + + return std::any_of(aServiceNames.begin(), aServiceNames.end(), [&rPath](const OUString& rName) { + return SfxObjectFactory::GetStandardTemplate(rName).match(rPath); }); +} + +void SfxTemplateLocalView::RemoveDefaultTemplateIcon(const OUString& rPath) +{ + for (const std::unique_ptr<ThumbnailViewItem>& pItem : mItemList) + { + TemplateViewItem* pViewItem = dynamic_cast<TemplateViewItem*>(pItem.get()); + if (pViewItem && pViewItem->getPath().match(rPath)) + { + pViewItem->showDefaultIcon(false); + Invalidate(); + return; + } + } +} + +BitmapEx SfxTemplateLocalView::getDefaultThumbnail( const OUString& rPath ) +{ + BitmapEx aImg; + INetURLObject aUrl(rPath); + OUString aExt = aUrl.getExtension(); + + if ( ViewFilter_Application::isFilteredExtension( FILTER_APPLICATION::WRITER, aExt) ) + aImg = BitmapEx(SFX_THUMBNAIL_TEXT); + else if ( ViewFilter_Application::isFilteredExtension( FILTER_APPLICATION::CALC, aExt) ) + aImg = BitmapEx(SFX_THUMBNAIL_SHEET); + else if ( ViewFilter_Application::isFilteredExtension( FILTER_APPLICATION::IMPRESS, aExt) ) + aImg = BitmapEx(SFX_THUMBNAIL_PRESENTATION); + else if ( ViewFilter_Application::isFilteredExtension( FILTER_APPLICATION::DRAW, aExt) ) + aImg = BitmapEx(SFX_THUMBNAIL_DRAWING); + + return aImg; +} + +BitmapEx SfxTemplateLocalView::fetchThumbnail (const OUString &msURL, long width, long height) +{ + return SfxTemplateLocalView::scaleImg(ThumbnailView::readThumbnail(msURL), width, height); +} + +void SfxTemplateLocalView::OnItemDblClicked (ThumbnailViewItem *pItem) +{ + TemplateViewItem* pViewItem = dynamic_cast<TemplateViewItem*>(pItem); + + if( pViewItem ) + maOpenTemplateHdl.Call(pViewItem); +} + + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/templatesearchview.cxx b/sfx2/source/control/templatesearchview.cxx new file mode 100644 index 000000000..759c888ce --- /dev/null +++ b/sfx2/source/control/templatesearchview.cxx @@ -0,0 +1,245 @@ +/* -*- 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/. + */ + +#include <templatesearchview.hxx> +#include <templatesearchviewitem.hxx> +#include <sfx2/templatelocalview.hxx> +#include <sfx2/sfxresid.hxx> +#include <tools/urlobj.hxx> +#include <vcl/svapp.hxx> +#include <vcl/weld.hxx> +#include <vcl/commandevent.hxx> +#include <vcl/event.hxx> + +#include <sfx2/strings.hrc> +#include <bitmaps.hlst> + +#define MNI_OPEN "open" +#define MNI_EDIT "edit" +#define MNI_DEFAULT_TEMPLATE "default" +#define MNI_DELETE "delete" + +TemplateSearchView::TemplateSearchView(std::unique_ptr<weld::ScrolledWindow> xWindow, + std::unique_ptr<weld::Menu> xMenu) + : SfxThumbnailView(std::move(xWindow), std::move(xMenu)) + , maSelectedItem(nullptr) + , maPosition(0,0) +{ +} + +bool TemplateSearchView::MouseButtonDown( const MouseEvent& rMEvt ) +{ + GrabFocus(); + return SfxThumbnailView::MouseButtonDown(rMEvt); +} + +bool TemplateSearchView::KeyInput( const KeyEvent& rKEvt ) +{ + vcl::KeyCode aKeyCode = rKEvt.GetKeyCode(); + + if(aKeyCode == ( KEY_MOD1 | KEY_A ) ) + { + for (ThumbnailViewItem* pItem : mFilteredItemList) + { + if (!pItem->isSelected()) + { + pItem->setSelection(true); + maItemStateHdl.Call(pItem); + } + } + + if (IsReallyVisible() && IsUpdateMode()) + Invalidate(); + return true; + } + else if( aKeyCode == KEY_DELETE && !mFilteredItemList.empty()) + { + std::unique_ptr<weld::MessageDialog> xQueryDlg(Application::CreateMessageDialog(GetDrawingArea(), VclMessageType::Question, VclButtonsType::YesNo, + SfxResId(STR_QMSG_SEL_TEMPLATE_DELETE))); + if (xQueryDlg->run() != RET_YES) + return true; + + //copy to avoid changing filtered item list during deletion + ThumbnailValueItemList mFilteredItemListCopy = mFilteredItemList; + + for (ThumbnailViewItem* pItem : mFilteredItemListCopy) + { + if (pItem->isSelected()) + { + maDeleteTemplateHdl.Call(pItem); + RemoveItem(pItem->mnId); + + CalculateItemPositions(); + } + } + } + + return SfxThumbnailView::KeyInput(rKEvt); +} + +bool TemplateSearchView::Command(const CommandEvent& rCEvt) +{ + if (rCEvt.GetCommand() != CommandEventId::ContextMenu) + return CustomWidgetController::Command(rCEvt); + + if (rCEvt.IsMouseEvent()) + { + deselectItems(); + size_t nPos = ImplGetItem(rCEvt.GetMousePosPixel()); + Point aPosition(rCEvt.GetMousePosPixel()); + maPosition = aPosition; + ThumbnailViewItem* pItem = ImplGetItem(nPos); + const TemplateViewItem *pViewItem = dynamic_cast<const TemplateViewItem*>(pItem); + + if(pViewItem) + { + maSelectedItem = dynamic_cast<TemplateViewItem*>(pItem); + maCreateContextMenuHdl.Call(pItem); + } + } + else + { + for (ThumbnailViewItem* pItem : mFilteredItemList) + { + //create context menu for the first selected item + if (pItem->isSelected()) + { + deselectItems(); + pItem->setSelection(true); + maItemStateHdl.Call(pItem); + tools::Rectangle aRect = pItem->getDrawArea(); + maPosition = aRect.Center(); + maSelectedItem = dynamic_cast<TemplateViewItem*>(pItem); + maCreateContextMenuHdl.Call(pItem); + break; + } + } + } + return true; +} + +void TemplateSearchView::createContextMenu(const bool bIsDefault) +{ + mxContextMenu->clear(); + mxContextMenu->append(MNI_OPEN,SfxResId(STR_OPEN)); + mxContextMenu->append(MNI_EDIT,SfxResId(STR_EDIT_TEMPLATE)); + + if (!bIsDefault) + mxContextMenu->append(MNI_DEFAULT_TEMPLATE,SfxResId(STR_DEFAULT_TEMPLATE)); + else + mxContextMenu->append(MNI_DEFAULT_TEMPLATE,SfxResId(STR_RESET_DEFAULT)); + + mxContextMenu->append_separator("separator"); + mxContextMenu->append(MNI_DELETE,SfxResId(STR_DELETE)); + maSelectedItem->setSelection(true); + maItemStateHdl.Call(maSelectedItem); + ContextMenuSelectHdl(mxContextMenu->popup_at_rect(GetDrawingArea(), tools::Rectangle(maPosition, Size(1,1)))); + Invalidate(); +} + +void TemplateSearchView::ContextMenuSelectHdl(const OString& rIdent) +{ + if (rIdent == MNI_OPEN) + maOpenTemplateHdl.Call(maSelectedItem); + else if (rIdent == MNI_EDIT) + maEditTemplateHdl.Call(maSelectedItem); + else if (rIdent == MNI_DELETE) + { + std::unique_ptr<weld::MessageDialog> xQueryDlg(Application::CreateMessageDialog(GetDrawingArea(), VclMessageType::Question, VclButtonsType::YesNo, + SfxResId(STR_QMSG_SEL_TEMPLATE_DELETE))); + if (xQueryDlg->run() != RET_YES) + return; + + maDeleteTemplateHdl.Call(maSelectedItem); + RemoveItem(maSelectedItem->mnId); + + CalculateItemPositions(); + } + else if (rIdent == MNI_DEFAULT_TEMPLATE) + maDefaultTemplateHdl.Call(maSelectedItem); +} + +void TemplateSearchView::setCreateContextMenuHdl(const Link<ThumbnailViewItem*,void> &rLink) +{ + maCreateContextMenuHdl = rLink; +} + +void TemplateSearchView::setOpenTemplateHdl(const Link<ThumbnailViewItem*, void> &rLink) +{ + maOpenTemplateHdl = rLink; +} + +void TemplateSearchView::setEditTemplateHdl(const Link<ThumbnailViewItem*,void> &rLink) +{ + maEditTemplateHdl = rLink; +} + +void TemplateSearchView::setDeleteTemplateHdl(const Link<ThumbnailViewItem*,void> &rLink) +{ + maDeleteTemplateHdl = rLink; +} + +void TemplateSearchView::setDefaultTemplateHdl(const Link<ThumbnailViewItem*,void> &rLink) +{ + maDefaultTemplateHdl = rLink; +} + +void TemplateSearchView::OnItemDblClicked (ThumbnailViewItem *pItem) +{ + maOpenTemplateHdl.Call(pItem); +} + +void TemplateSearchView::AppendItem(sal_uInt16 nAssocItemId, sal_uInt16 nRegionId, sal_uInt16 nIdx, + const OUString &rTitle, const OUString &rSubtitle, + const OUString &rPath, + const BitmapEx &rImage) +{ + std::unique_ptr<TemplateSearchViewItem> pItem(new TemplateSearchViewItem(*this, getNextItemId())); + pItem->mnAssocId = nAssocItemId; + pItem->mnDocId = nIdx; + pItem->mnRegionId = nRegionId; + + if(!rImage.IsEmpty()) + pItem->maPreview1 = rImage; + else + pItem->maPreview1 = getDefaultThumbnail(rPath); + + pItem->maTitle = rTitle; + pItem->setHelpText(rSubtitle); + pItem->setPath(rPath); + + if(TemplateLocalView::IsDefaultTemplate(rPath)) + pItem->showDefaultIcon(true); + + SfxThumbnailView::AppendItem(std::move(pItem)); + + CalculateItemPositions(); +} + +BitmapEx TemplateSearchView::getDefaultThumbnail( const OUString& rPath ) +{ + BitmapEx aImg; + INetURLObject aUrl(rPath); + OUString aExt = aUrl.getExtension(); + + if ( ViewFilter_Application::isFilteredExtension( FILTER_APPLICATION::WRITER, aExt) ) + aImg = BitmapEx(SFX_THUMBNAIL_TEXT); + else if ( ViewFilter_Application::isFilteredExtension( FILTER_APPLICATION::CALC, aExt) ) + aImg = BitmapEx(SFX_THUMBNAIL_SHEET); + else if ( ViewFilter_Application::isFilteredExtension( FILTER_APPLICATION::IMPRESS, aExt) ) + aImg = BitmapEx(SFX_THUMBNAIL_PRESENTATION); + else if ( ViewFilter_Application::isFilteredExtension( FILTER_APPLICATION::DRAW, aExt) ) + aImg = BitmapEx(SFX_THUMBNAIL_DRAWING); + + return aImg; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ + + diff --git a/sfx2/source/control/templateviewitem.cxx b/sfx2/source/control/templateviewitem.cxx new file mode 100644 index 000000000..c1b660969 --- /dev/null +++ b/sfx2/source/control/templateviewitem.cxx @@ -0,0 +1,123 @@ +/* -*- 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/. + */ + +#include <templateviewitem.hxx> + +#include <basegfx/matrix/b2dhommatrixtools.hxx> +#include <basegfx/polygon/b2dpolygon.hxx> +#include <drawinglayer/attribute/fillgraphicattribute.hxx> +#include <drawinglayer/primitive2d/fillgraphicprimitive2d.hxx> +#include <drawinglayer/primitive2d/polygonprimitive2d.hxx> +#include <drawinglayer/primitive2d/PolyPolygonColorPrimitive2D.hxx> +#include <drawinglayer/primitive2d/PolyPolygonSelectionPrimitive2D.hxx> +#include <drawinglayer/primitive2d/discretebitmapprimitive2d.hxx> +#include <drawinglayer/processor2d/baseprocessor2d.hxx> +#include <tools/poly.hxx> +#include <vcl/graph.hxx> + +#include <bitmaps.hlst> + +using namespace basegfx; +using namespace basegfx::utils; +using namespace drawinglayer::attribute; +using namespace drawinglayer::primitive2d; + +TemplateViewItem::TemplateViewItem (ThumbnailViewBase &rView, sal_uInt16 nId) + : ThumbnailViewItem(rView, nId), + mnRegionId(USHRT_MAX), + mnDocId(USHRT_MAX), + maDefaultBitmap(BMP_DEFAULT), + mbIsDefaultTemplate(false) +{ +} + +TemplateViewItem::~TemplateViewItem () +{ +} + +::tools::Rectangle TemplateViewItem::getDefaultIconArea() const +{ + ::tools::Rectangle aArea(getDrawArea()); + Size aSize(maDefaultBitmap.GetSizePixel()); + + return ::tools::Rectangle( + Point(aArea.Left() + THUMBNAILVIEW_ITEM_CORNER, aArea.Top() + THUMBNAILVIEW_ITEM_CORNER), + aSize); +} + +void TemplateViewItem::Paint(drawinglayer::processor2d::BaseProcessor2D *pProcessor, + const ThumbnailItemAttributes *pAttrs) +{ + BColor aFillColor = pAttrs->aFillColor; + + drawinglayer::primitive2d::Primitive2DContainer aSeq(5); + double fTransparence = 0.0; + + // Draw background + if( mbSelected && mbHover) + aFillColor = pAttrs->aSelectHighlightColor; + else if (mbSelected || mbHover) + aFillColor = pAttrs->aHighlightColor; + + if (mbHover) + fTransparence = pAttrs->fHighlightTransparence; + + aSeq[0] = drawinglayer::primitive2d::Primitive2DReference( + new PolyPolygonSelectionPrimitive2D( B2DPolyPolygon(::tools::Polygon(maDrawArea,5,5).getB2DPolygon()), + aFillColor, + fTransparence, + 0.0, + true)); + + // Draw thumbnail + Size aImageSize = maPreview1.GetSizePixel(); + + float fWidth = aImageSize.Width(); + float fHeight = aImageSize.Height(); + float fPosX = maPrev1Pos.getX(); + float fPosY = maPrev1Pos.getY(); + + B2DPolygon aBounds; + aBounds.append(B2DPoint(fPosX,fPosY)); + aBounds.append(B2DPoint(fPosX+fWidth,fPosY)); + aBounds.append(B2DPoint(fPosX+fWidth,fPosY+fHeight)); + aBounds.append(B2DPoint(fPosX,fPosY+fHeight)); + aBounds.setClosed(true); + + aSeq[1] = drawinglayer::primitive2d::Primitive2DReference( new PolyPolygonColorPrimitive2D( + B2DPolyPolygon(aBounds), COL_WHITE.getBColor())); + + aSeq[2] = drawinglayer::primitive2d::Primitive2DReference( new FillGraphicPrimitive2D( + createTranslateB2DHomMatrix(maPrev1Pos.X(),maPrev1Pos.Y()), + FillGraphicAttribute(Graphic(maPreview1), + B2DRange( + B2DPoint(0,0), + B2DPoint(aImageSize.Width(),aImageSize.Height())), + false) + )); + + // draw thumbnail borders + aSeq[3] = drawinglayer::primitive2d::Primitive2DReference(createBorderLine(aBounds)); + + if(mbIsDefaultTemplate) + { + Point aIconPos(getDefaultIconArea().TopLeft()); + + aSeq[4] = drawinglayer::primitive2d::Primitive2DReference(new DiscreteBitmapPrimitive2D( maDefaultBitmap, + B2DPoint(aIconPos.X(), aIconPos.Y()))); + } + + addTextPrimitives(maTitle, pAttrs, maTextPos, aSeq); + + pProcessor->process(aSeq); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ + + diff --git a/sfx2/source/control/thumbnailview.cxx b/sfx2/source/control/thumbnailview.cxx new file mode 100644 index 000000000..996aceae0 --- /dev/null +++ b/sfx2/source/control/thumbnailview.cxx @@ -0,0 +1,2296 @@ +/* -*- 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/. + */ + +#include <sfx2/thumbnailview.hxx> +#include <thumbnailviewitem.hxx> + +#include <utility> + +#include "thumbnailviewacc.hxx" + +#include <basegfx/color/bcolortools.hxx> +#include <comphelper/processfactory.hxx> +#include <drawinglayer/attribute/fontattribute.hxx> +#include <drawinglayer/primitive2d/PolyPolygonColorPrimitive2D.hxx> +#include <drawinglayer/primitive2d/textlayoutdevice.hxx> +#include <drawinglayer/processor2d/baseprocessor2d.hxx> +#include <drawinglayer/processor2d/processorfromoutputdevice.hxx> +#include <o3tl/safeint.hxx> +#include <rtl/ustring.hxx> +#include <sal/log.hxx> +#include <svtools/optionsdrawinglayer.hxx> +#include <tools/diagnose_ex.h> +#include <unotools/ucbstreamhelper.hxx> +#include <vcl/svapp.hxx> +#include <vcl/scrbar.hxx> +#include <vcl/settings.hxx> +#include <vcl/commandevent.hxx> +#include <vcl/event.hxx> +#include <vcl/pngread.hxx> + +#include <com/sun/star/accessibility/AccessibleEventId.hpp> +#include <com/sun/star/embed/ElementModes.hpp> +#include <com/sun/star/embed/StorageFactory.hpp> +#include <com/sun/star/embed/XStorage.hpp> + +#include <memory> + +using namespace basegfx; +using namespace basegfx::utils; +using namespace drawinglayer::attribute; +using namespace drawinglayer::primitive2d; + +constexpr int gnFineness = 5; + +ThumbnailView::ThumbnailView (vcl::Window *pParent, WinBits nWinStyle) + : Control( pParent, nWinStyle ) + , mpItemAttrs(new ThumbnailItemAttributes) +{ + ImplInit(); +} + +ThumbnailView::~ThumbnailView() +{ + disposeOnce(); +} + +void ThumbnailView::dispose() +{ + css::uno::Reference< css::lang::XComponent> xComponent(GetAccessible(false), css::uno::UNO_QUERY); + + if (xComponent.is()) + xComponent->dispose (); + + mpScrBar.disposeAndClear(); + mpItemAttrs.reset(); + + ImplDeleteItems(); + Control::dispose(); +} + +void ThumbnailView::MouseMove(const MouseEvent& rMEvt) +{ + size_t nItemCount = mFilteredItemList.size(); + Point aPoint = rMEvt.GetPosPixel(); + OUString aHelp; + + for (size_t i = 0; i < nItemCount; i++) + { + ThumbnailViewItem *pItem = mFilteredItemList[i]; + + if (pItem->mbVisible && !rMEvt.IsLeaveWindow() && pItem->getDrawArea().IsInside(aPoint)) + { + aHelp = pItem->getHelpText(); + } + + ::tools::Rectangle aToInvalidate(pItem->updateHighlight(pItem->mbVisible && !rMEvt.IsLeaveWindow(), aPoint)); + + if (!aToInvalidate.IsEmpty() && IsReallyVisible() && IsUpdateMode()) + Invalidate(aToInvalidate); + } + + if (mbShowTooltips) + SetQuickHelpText(aHelp); +} + +void ThumbnailView::AppendItem(std::unique_ptr<ThumbnailViewItem> pItem) +{ + if (maFilterFunc(pItem.get())) + { + // Save current start,end range, iterator might get invalidated + size_t nSelStartPos = 0; + ThumbnailViewItem *pSelStartItem = nullptr; + + if (mpStartSelRange != mFilteredItemList.end()) + { + pSelStartItem = *mpStartSelRange; + nSelStartPos = mpStartSelRange - mFilteredItemList.begin(); + } + + mFilteredItemList.push_back(pItem.get()); + mpStartSelRange = pSelStartItem != nullptr ? mFilteredItemList.begin() + nSelStartPos : mFilteredItemList.end(); + } + + mItemList.push_back(std::move(pItem)); +} + +void ThumbnailView::ImplInit() +{ + mpScrBar = nullptr; + mnItemWidth = 0; + mnItemHeight = 0; + mnItemPadding = 0; + mnVisLines = 0; + mnLines = 0; + mnFirstLine = 0; + mnCols = 0; + mbScroll = false; + mbHasVisibleItems = false; + mbShowTooltips = false; + maFilterFunc = ViewFilterAll(); + maFillColor = GetSettings().GetStyleSettings().GetFieldColor(); + maTextColor = GetSettings().GetStyleSettings().GetWindowTextColor(); + maHighlightColor = GetSettings().GetStyleSettings().GetHighlightColor(); + maHighlightTextColor = GetSettings().GetStyleSettings().GetWindowTextColor(); + maSelectHighlightColor = GetSettings().GetStyleSettings().GetActiveColor(); + maSelectHighlightTextColor = GetSettings().GetStyleSettings().GetActiveTextColor(); + + const SvtOptionsDrawinglayer aSvtOptionsDrawinglayer; + mfHighlightTransparence = aSvtOptionsDrawinglayer.GetTransparentSelectionPercent() * 0.01; + + mpStartSelRange = mFilteredItemList.end(); + + ApplySettings(*this); +} + +void ThumbnailView::ImplDeleteItems() +{ + const size_t n = mItemList.size(); + + for ( size_t i = 0; i < n; ++i ) + { + ThumbnailViewItem *const pItem = mItemList[i].get(); + + // deselect all current selected items and fire events + if (pItem->isSelected()) + { + pItem->setSelection(false); + // fire accessible event??? + } + + if ( pItem->isVisible() && ImplHasAccessibleListeners() ) + { + css::uno::Any aOldAny, aNewAny; + + aOldAny <<= pItem->GetAccessible( false ); + ImplFireAccessibleEvent( css::accessibility::AccessibleEventId::CHILD, aOldAny, aNewAny ); + } + + mItemList[i].reset(); + } + + mItemList.clear(); + mFilteredItemList.clear(); + + mpStartSelRange = mFilteredItemList.end(); +} + +void ThumbnailView::ApplySettings(vcl::RenderContext& rRenderContext) +{ + const StyleSettings& rStyleSettings = rRenderContext.GetSettings().GetStyleSettings(); + + ApplyControlFont(*this, rStyleSettings.GetAppFont()); + ApplyControlForeground(*this, rStyleSettings.GetButtonTextColor()); + rRenderContext.SetTextFillColor(); + rRenderContext.SetBackground(maFillColor); + + mpItemAttrs->aFillColor = maFillColor.getBColor(); + mpItemAttrs->aTextColor = maTextColor.getBColor(); + mpItemAttrs->aHighlightColor = maHighlightColor.getBColor(); + mpItemAttrs->aHighlightTextColor = maHighlightTextColor.getBColor(); + mpItemAttrs->aSelectHighlightColor = maSelectHighlightColor.getBColor(); + mpItemAttrs->aSelectHighlightTextColor = maSelectHighlightTextColor.getBColor(); + mpItemAttrs->fHighlightTransparence = mfHighlightTransparence; + mpItemAttrs->aFontAttr = getFontAttributeFromVclFont(mpItemAttrs->aFontSize,GetFont(),false,true); + mpItemAttrs->nMaxTextLength = 0; +} + +void ThumbnailView::DrawItem(ThumbnailViewItem const *pItem) +{ + if (pItem->isVisible()) + { + ::tools::Rectangle aRect = pItem->getDrawArea(); + + if (!aRect.IsEmpty()) + Invalidate(aRect); + } +} + +void ThumbnailView::OnItemDblClicked (ThumbnailViewItem*) +{ +} + +css::uno::Reference< css::accessibility::XAccessible > ThumbnailView::CreateAccessible() +{ + return new ThumbnailViewAcc( this ); +} + +css::uno::Reference< css::accessibility::XAccessible > ThumbnailView::getAccessible() +{ + return GetAccessible(); +} + +void ThumbnailView::CalculateItemPositions (bool bScrollBarUsed) +{ + if (!mnItemHeight || !mnItemWidth) + return; + + Size aWinSize = GetOutputSizePixel(); + size_t nItemCount = mFilteredItemList.size(); + WinBits nStyle = GetStyle(); + VclPtr<ScrollBar> pDelScrBar; + long nScrBarWidth = 0; + + // consider the scrolling + if ( nStyle & WB_VSCROLL ) + { + if ( !mpScrBar ) + { + mpScrBar = VclPtr<ScrollBar>::Create( this, WB_VSCROLL | WB_DRAG ); + mpScrBar->SetScrollHdl( LINK( this, ThumbnailView, ImplScrollHdl ) ); + } + + // adapt the width because of the changed settings + nScrBarWidth = GetSettings().GetStyleSettings().GetScrollBarSize(); + } + else + { + if ( mpScrBar ) + { + // delete ScrollBar not until later, to prevent recursive calls + pDelScrBar = mpScrBar; + mpScrBar = nullptr; + } + } + + // calculate window scroll ratio + float nScrollRatio; + if( bScrollBarUsed && mpScrBar ) + nScrollRatio = static_cast<float>(mpScrBar->GetThumbPos()) / + static_cast<float>(mpScrBar->GetRangeMax() - mpScrBar->GetVisibleSize()); + else + nScrollRatio = 0; + + // calculate maximum number of visible columns + mnCols = static_cast<sal_uInt16>((aWinSize.Width()-nScrBarWidth) / mnItemWidth); + + if (!mnCols) + mnCols = 1; + + // calculate maximum number of visible rows + mnVisLines = static_cast<sal_uInt16>(aWinSize.Height() / mnItemHeight); + + // calculate empty space + long nHSpace = aWinSize.Width()-nScrBarWidth - mnCols*mnItemWidth; + long nVSpace = aWinSize.Height() - mnVisLines*mnItemHeight; + long nHItemSpace = nHSpace / (mnCols+1); + long nVItemSpace = nVSpace / (mnVisLines+1); + + // calculate maximum number of rows + // Floor( (M+N-1)/N )==Ceiling( M/N ) + mnLines = (static_cast<long>(nItemCount)+mnCols-1) / mnCols; + + if ( !mnLines ) + mnLines = 1; + + if ( mnLines <= mnVisLines ) + mnFirstLine = 0; + else if ( mnFirstLine > o3tl::make_unsigned(mnLines-mnVisLines) ) + mnFirstLine = static_cast<sal_uInt16>(mnLines-mnVisLines); + + mbHasVisibleItems = true; + + long nFullSteps = (mnLines > mnVisLines) ? mnLines - mnVisLines + 1 : 1; + + long nItemHeightOffset = mnItemHeight + nVItemSpace; + long nHiddenLines = static_cast<long>((nFullSteps - 1) * nScrollRatio); + + // calculate offsets + long nStartX = nHItemSpace; + long nStartY = nVItemSpace; + + // calculate and draw items + long x = nStartX; + long y = nStartY - ((nFullSteps - 1) * nScrollRatio - nHiddenLines) * nItemHeightOffset; + + // draw items + // Unless we are scrolling (via scrollbar) we just use the precalculated + // mnFirstLine -- our nHiddenLines calculation takes into account only + // what the user has done with the scrollbar but not any changes of selection + // using the keyboard, meaning we could accidentally hide the selected item + // if we believe the scrollbar (fdo#72287). + size_t nFirstItem = (bScrollBarUsed ? nHiddenLines : mnFirstLine) * mnCols; + size_t nLastItem = nFirstItem + (mnVisLines + 1) * mnCols; + + // If want also draw parts of items in the last line, + // then we add one more line if parts of this line are visible + + size_t nCurCount = 0; + for ( size_t i = 0; i < nItemCount; i++ ) + { + ThumbnailViewItem *const pItem = mFilteredItemList[i]; + + if ((nCurCount >= nFirstItem) && (nCurCount < nLastItem)) + { + if( !pItem->isVisible()) + { + if ( ImplHasAccessibleListeners() ) + { + css::uno::Any aOldAny, aNewAny; + + aNewAny <<= pItem->GetAccessible( false ); + ImplFireAccessibleEvent( css::accessibility::AccessibleEventId::CHILD, aOldAny, aNewAny ); + } + + pItem->show(true); + } + + pItem->setDrawArea(::tools::Rectangle( Point(x,y), Size(mnItemWidth, mnItemHeight) )); + pItem->calculateItemsPosition(mnThumbnailHeight,mnItemPadding,mpItemAttrs->nMaxTextLength,mpItemAttrs.get()); + + if ( !((nCurCount+1) % mnCols) ) + { + x = nStartX; + y += mnItemHeight+nVItemSpace; + } + else + x += mnItemWidth+nHItemSpace; + } + else + { + if( pItem->isVisible()) + { + if ( ImplHasAccessibleListeners() ) + { + css::uno::Any aOldAny, aNewAny; + + aOldAny <<= pItem->GetAccessible( false ); + ImplFireAccessibleEvent( css::accessibility::AccessibleEventId::CHILD, aOldAny, aNewAny ); + } + + pItem->show(false); + } + + } + + ++nCurCount; + } + + // arrange ScrollBar, set values and show it + if ( mpScrBar ) + { + mnLines = (nCurCount+mnCols-1)/mnCols; + + // check if scroll is needed + mbScroll = mnLines > mnVisLines; + + + Point aPos( aWinSize.Width() - nScrBarWidth, 0 ); + Size aSize( nScrBarWidth, aWinSize.Height() ); + + mpScrBar->SetPosSizePixel( aPos, aSize ); + mpScrBar->SetRangeMax(mnLines * gnFineness); + mpScrBar->SetVisibleSize(mnVisLines * gnFineness); + if (!bScrollBarUsed) + mpScrBar->SetThumbPos( static_cast<long>(mnFirstLine)*gnFineness ); + long nPageSize = mnVisLines; + if ( nPageSize < 1 ) + nPageSize = 1; + mpScrBar->SetPageSize( nPageSize ); + mpScrBar->Show( mbScroll ); + mpScrBar->Enable( mbScroll ); + } + + // delete ScrollBar + pDelScrBar.disposeAndClear(); +} + +size_t ThumbnailView::ImplGetItem( const Point& rPos ) const +{ + if ( !mbHasVisibleItems ) + { + return THUMBNAILVIEW_ITEM_NOTFOUND; + } + + for (size_t i = 0; i < mFilteredItemList.size(); ++i) + { + if (mFilteredItemList[i]->isVisible() && mFilteredItemList[i]->getDrawArea().IsInside(rPos)) + return i; + } + + return THUMBNAILVIEW_ITEM_NOTFOUND; +} + +ThumbnailViewItem* ThumbnailView::ImplGetItem( size_t nPos ) +{ + return ( nPos < mFilteredItemList.size() ) ? mFilteredItemList[nPos] : nullptr; +} + +sal_uInt16 ThumbnailView::ImplGetVisibleItemCount() const +{ + sal_uInt16 nRet = 0; + const size_t nItemCount = mItemList.size(); + + for ( size_t n = 0; n < nItemCount; ++n ) + { + if ( mItemList[n]->isVisible() ) + ++nRet; + } + + return nRet; +} + +ThumbnailViewItem* ThumbnailView::ImplGetVisibleItem( sal_uInt16 nVisiblePos ) +{ + const size_t nItemCount = mItemList.size(); + + for ( size_t n = 0; n < nItemCount; ++n ) + { + ThumbnailViewItem *const pItem = mItemList[n].get(); + + if ( pItem->isVisible() && !nVisiblePos-- ) + return pItem; + } + + return nullptr; +} + +void ThumbnailView::ImplFireAccessibleEvent( short nEventId, const css::uno::Any& rOldValue, const css::uno::Any& rNewValue ) +{ + ThumbnailViewAcc* pAcc = ThumbnailViewAcc::getImplementation( GetAccessible( false ) ); + + if( pAcc ) + pAcc->FireAccessibleEvent( nEventId, rOldValue, rNewValue ); +} + +bool ThumbnailView::ImplHasAccessibleListeners() +{ + ThumbnailViewAcc* pAcc = ThumbnailViewAcc::getImplementation( GetAccessible( false ) ); + return( pAcc && pAcc->HasAccessibleListeners() ); +} + +IMPL_LINK( ThumbnailView,ImplScrollHdl, ScrollBar*, pScrollBar, void ) +{ + if ( pScrollBar->GetDelta() ) + { + CalculateItemPositions(true); + + if ( IsReallyVisible() && IsUpdateMode() ) + Invalidate(); + } +} + +void ThumbnailView::KeyInput( const KeyEvent& rKEvt ) +{ + // Get the last selected item in the list + size_t nLastPos = 0; + bool bFoundLast = false; + for ( long i = mFilteredItemList.size() - 1; !bFoundLast && i >= 0; --i ) + { + ThumbnailViewItem* pItem = mFilteredItemList[i]; + if ( pItem->isSelected() ) + { + nLastPos = i; + bFoundLast = true; + } + } + + bool bValidRange = false; + bool bHasSelRange = mpStartSelRange != mFilteredItemList.end(); + size_t nNextPos = nLastPos; + vcl::KeyCode aKeyCode = rKEvt.GetKeyCode(); + ThumbnailViewItem* pNext = nullptr; + + if (aKeyCode.IsShift() && bHasSelRange) + { + //If the last element selected is the start range position + //search for the first selected item + size_t nSelPos = mpStartSelRange - mFilteredItemList.begin(); + + if (nLastPos == nSelPos) + { + while (nLastPos && mFilteredItemList[nLastPos-1]->isSelected()) + --nLastPos; + } + } + + switch ( aKeyCode.GetCode() ) + { + case KEY_RIGHT: + if (!mFilteredItemList.empty()) + { + if ( bFoundLast && nLastPos + 1 < mFilteredItemList.size() ) + { + bValidRange = true; + nNextPos = nLastPos + 1; + } + + pNext = mFilteredItemList[nNextPos]; + } + break; + case KEY_LEFT: + if (!mFilteredItemList.empty()) + { + if ( nLastPos > 0 ) + { + bValidRange = true; + nNextPos = nLastPos - 1; + } + + pNext = mFilteredItemList[nNextPos]; + } + break; + case KEY_DOWN: + if (!mFilteredItemList.empty()) + { + if ( bFoundLast ) + { + //If we are in the second last row just go the one in + //the row below, if there's not row below just go to the + //last item but for the last row don't do anything. + if ( nLastPos + mnCols < mFilteredItemList.size( ) ) + { + bValidRange = true; + nNextPos = nLastPos + mnCols; + } + else + { + int curRow = nLastPos/mnCols; + + if (curRow < mnLines-1) + nNextPos = mFilteredItemList.size()-1; + } + } + + pNext = mFilteredItemList[nNextPos]; + } + break; + case KEY_UP: + if (!mFilteredItemList.empty()) + { + if ( nLastPos >= mnCols ) + { + bValidRange = true; + nNextPos = nLastPos - mnCols; + } + + pNext = mFilteredItemList[nNextPos]; + } + break; + case KEY_RETURN: + { + if ( bFoundLast ) + OnItemDblClicked( mFilteredItemList[nLastPos] ); + } + [[fallthrough]]; + default: + Control::KeyInput( rKEvt ); + } + + if ( pNext ) + { + if (aKeyCode.IsShift() && bValidRange) + { + std::pair<size_t,size_t> aRange; + size_t nSelPos = mpStartSelRange - mFilteredItemList.begin(); + + if (nLastPos < nSelPos) + { + if (nNextPos > nLastPos) + { + if ( nNextPos > nSelPos) + aRange = std::make_pair(nLastPos,nNextPos); + else + aRange = std::make_pair(nLastPos,nNextPos-1); + } + else + aRange = std::make_pair(nNextPos,nLastPos-1); + } + else if (nLastPos == nSelPos) + { + if (nNextPos > nLastPos) + aRange = std::make_pair(nLastPos+1,nNextPos); + else + aRange = std::make_pair(nNextPos,nLastPos-1); + } + else + { + if (nNextPos > nLastPos) + aRange = std::make_pair(nLastPos+1,nNextPos); + else + { + if ( nNextPos < nSelPos) + aRange = std::make_pair(nNextPos,nLastPos); + else + aRange = std::make_pair(nNextPos+1,nLastPos); + } + } + + for (size_t i = aRange.first; i <= aRange.second; ++i) + { + if (i != nSelPos) + { + ThumbnailViewItem *pCurItem = mFilteredItemList[i]; + + pCurItem->setSelection(!pCurItem->isSelected()); + + if (pCurItem->isVisible()) + DrawItem(pCurItem); + } + } + } + else if (!aKeyCode.IsShift()) + { + deselectItems(); + SelectItem(pNext->mnId); + + //Mark it as the selection range start position + mpStartSelRange = mFilteredItemList.begin() + nNextPos; + } + + MakeItemVisible(pNext->mnId); + } +} + +void ThumbnailView::MakeItemVisible( sal_uInt16 nItemId ) +{ + // Get the item row + size_t nPos = 0; + bool bFound = false; + for ( size_t i = 0; !bFound && i < mFilteredItemList.size(); ++i ) + { + ThumbnailViewItem* pItem = mFilteredItemList[i]; + if ( pItem->mnId == nItemId ) + { + nPos = i; + bFound = true; + } + } + sal_uInt16 nRow = mnCols ? nPos / mnCols : 0; + + // Move the visible rows as little as possible to include that one + if ( nRow < mnFirstLine ) + mnFirstLine = nRow; + else if ( nRow > mnFirstLine + mnVisLines ) + mnFirstLine = nRow - mnVisLines; + + CalculateItemPositions(); + Invalidate(); +} + +void ThumbnailView::MouseButtonDown( const MouseEvent& rMEvt ) +{ + if ( !rMEvt.IsLeft() ) + { + Control::MouseButtonDown( rMEvt ); + return; + } + + size_t nPos = ImplGetItem(rMEvt.GetPosPixel()); + ThumbnailViewItem* pItem = ImplGetItem(nPos); + + if ( !pItem ) + { + deselectItems(); + Control::MouseButtonDown( rMEvt ); + return; + } + + if ( rMEvt.GetClicks() == 2 ) + { + OnItemDblClicked(pItem); + return; + } + + if(rMEvt.GetClicks() == 1) + { + if (rMEvt.IsMod1()) + { + //Keep selected item group state and just invert current desired one state + pItem->setSelection(!pItem->isSelected()); + + //This one becomes the selection range start position if it changes its state to selected otherwise resets it + mpStartSelRange = pItem->isSelected() ? mFilteredItemList.begin() + nPos : mFilteredItemList.end(); + } + else if (rMEvt.IsShift() && mpStartSelRange != mFilteredItemList.end()) + { + std::pair<size_t,size_t> aNewRange; + aNewRange.first = mpStartSelRange - mFilteredItemList.begin(); + aNewRange.second = nPos; + + if (aNewRange.first > aNewRange.second) + std::swap(aNewRange.first,aNewRange.second); + + //Deselect the ones outside of it + for (size_t i = 0, n = mFilteredItemList.size(); i < n; ++i) + { + ThumbnailViewItem *pCurItem = mFilteredItemList[i]; + + if (pCurItem->isSelected() && (i < aNewRange.first || i > aNewRange.second)) + { + pCurItem->setSelection(false); + + if (pCurItem->isVisible()) + DrawItem(pCurItem); + } + } + + size_t nSelPos = mpStartSelRange - mFilteredItemList.begin(); + + //Select the items between start range and the selected item + if (nSelPos != nPos) + { + int dir = nSelPos < nPos ? 1 : -1; + size_t nCurPos = nSelPos + dir; + + while (nCurPos != nPos) + { + ThumbnailViewItem *pCurItem = mFilteredItemList[nCurPos]; + + if (!pCurItem->isSelected()) + { + pCurItem->setSelection(true); + + if (pCurItem->isVisible()) + DrawItem(pCurItem); + } + + nCurPos += dir; + } + } + + pItem->setSelection(true); + } + else + { + //If we got a group of selected items deselect the rest and only keep the desired one + //mark items as not selected to not fire unnecessary change state events. + pItem->setSelection(false); + deselectItems(); + pItem->setSelection(true); + + //Mark as initial selection range position and reset end one + mpStartSelRange = mFilteredItemList.begin() + nPos; + } + + if (!pItem->isHighlighted()) + DrawItem(pItem); + + //fire accessible event?? + } +} + +void ThumbnailView::Command( const CommandEvent& rCEvt ) +{ + if ( (rCEvt.GetCommand() == CommandEventId::Wheel) || + (rCEvt.GetCommand() == CommandEventId::StartAutoScroll) || + (rCEvt.GetCommand() == CommandEventId::AutoScroll) ) + { + if ( HandleScrollCommand( rCEvt, nullptr, mpScrBar ) ) + return; + } + + Control::Command( rCEvt ); +} + +void ThumbnailView::Paint(vcl::RenderContext& rRenderContext, const ::tools::Rectangle& rRect) +{ + size_t nItemCount = mItemList.size(); + + // Draw background + drawinglayer::primitive2d::Primitive2DContainer aSeq(1); + aSeq[0] = drawinglayer::primitive2d::Primitive2DReference( + new PolyPolygonColorPrimitive2D( + B2DPolyPolygon( ::tools::Polygon(::tools::Rectangle(Point(), GetOutputSizePixel()), 0, 0).getB2DPolygon()), + maFillColor.getBColor())); + + // Create the processor and process the primitives + const drawinglayer::geometry::ViewInformation2D aNewViewInfos; + + std::unique_ptr<drawinglayer::processor2d::BaseProcessor2D> pProcessor( + drawinglayer::processor2d::createBaseProcessor2DFromOutputDevice(rRenderContext, aNewViewInfos)); + pProcessor->process(aSeq); + + // draw items + for (size_t i = 0; i < nItemCount; i++) + { + ThumbnailViewItem *const pItem = mItemList[i].get(); + + if (pItem->isVisible()) + { + pItem->Paint(pProcessor.get(), mpItemAttrs.get()); + } + } + + if (mpScrBar && mpScrBar->IsVisible()) + mpScrBar->Invalidate(rRect); +} + +void ThumbnailView::GetFocus() +{ + // Select the first item if nothing selected + int nSelected = -1; + for (size_t i = 0, n = mItemList.size(); i < n && nSelected == -1; ++i) + { + if (mItemList[i]->isSelected()) + nSelected = i; + } + + if (nSelected == -1 && !mItemList.empty()) + { + SelectItem(1); + } + + // Tell the accessible object that we got the focus. + ThumbnailViewAcc* pAcc = ThumbnailViewAcc::getImplementation( GetAccessible( false ) ); + if( pAcc ) + pAcc->GetFocus(); + + Control::GetFocus(); +} + +void ThumbnailView::LoseFocus() +{ + Control::LoseFocus(); + + // Tell the accessible object that we lost the focus. + ThumbnailViewAcc* pAcc = ThumbnailViewAcc::getImplementation( GetAccessible( false ) ); + if( pAcc ) + pAcc->LoseFocus(); +} + +void ThumbnailView::Resize() +{ + Control::Resize(); + CalculateItemPositions(); + + if ( IsReallyVisible() && IsUpdateMode() ) + Invalidate(); +} + +void ThumbnailView::StateChanged( StateChangedType nType ) +{ + Control::StateChanged( nType ); + + if ( nType == StateChangedType::InitShow ) + { + if ( IsReallyVisible() && IsUpdateMode() ) + Invalidate(); + } + else if ( nType == StateChangedType::UpdateMode ) + { + if ( IsReallyVisible() && IsUpdateMode() ) + Invalidate(); + } + else if ( nType == StateChangedType::Text ) + { + } + else if ( (nType == StateChangedType::Zoom) || + (nType == StateChangedType::ControlFont) ) + { + Invalidate(); + } + else if ( nType == StateChangedType::ControlForeground ) + { + Invalidate(); + } + else if ( nType == StateChangedType::ControlBackground ) + { + Invalidate(); + } + else if ( (nType == StateChangedType::Style) || (nType == StateChangedType::Enable) ) + { + Invalidate(); + } +} + +void ThumbnailView::DataChanged( const DataChangedEvent& rDCEvt ) +{ + Control::DataChanged( rDCEvt ); + + if ( (rDCEvt.GetType() == DataChangedEventType::FONTS) || + (rDCEvt.GetType() == DataChangedEventType::DISPLAY) || + (rDCEvt.GetType() == DataChangedEventType::FONTSUBSTITUTION) || + ((rDCEvt.GetType() == DataChangedEventType::SETTINGS) && + (rDCEvt.GetFlags() & AllSettingsFlags::STYLE)) ) + { + Invalidate(); + } +} + +void ThumbnailView::Clear() +{ + ImplDeleteItems(); + + // reset variables + mnFirstLine = 0; + + CalculateItemPositions(); + + if ( IsReallyVisible() && IsUpdateMode() ) + Invalidate(); +} + +void ThumbnailView::updateItems (std::vector<std::unique_ptr<ThumbnailViewItem>> items) +{ + ImplDeleteItems(); + + // reset variables + mnFirstLine = 0; + + mItemList = std::move(items); + + filterItems(maFilterFunc); +} + +size_t ThumbnailView::GetItemPos( sal_uInt16 nItemId ) const +{ + for ( size_t i = 0, n = mFilteredItemList.size(); i < n; ++i ) { + if ( mFilteredItemList[i]->mnId == nItemId ) { + return i; + } + } + return THUMBNAILVIEW_ITEM_NOTFOUND; +} + +sal_uInt16 ThumbnailView::GetItemId( size_t nPos ) const +{ + return ( nPos < mFilteredItemList.size() ) ? mFilteredItemList[nPos]->mnId : 0 ; +} + +sal_uInt16 ThumbnailView::GetItemId( const Point& rPos ) const +{ + size_t nItemPos = ImplGetItem( rPos ); + if ( nItemPos != THUMBNAILVIEW_ITEM_NOTFOUND ) + return GetItemId( nItemPos ); + + return 0; +} + +sal_uInt16 ThumbnailView::getNextItemId() const +{ + return mItemList.empty() ? 1 : mItemList.back()->mnId + 1; +} + +void ThumbnailView::setItemMaxTextLength(sal_uInt32 nLength) +{ + mpItemAttrs->nMaxTextLength = nLength; +} + +void ThumbnailView::setItemDimensions(long itemWidth, long thumbnailHeight, long displayHeight, int itemPadding) +{ + mnItemWidth = itemWidth + 2*itemPadding; + mnThumbnailHeight = thumbnailHeight; + mnDisplayHeight = displayHeight; + mnItemPadding = itemPadding; + mnItemHeight = mnDisplayHeight + mnThumbnailHeight + 2*itemPadding; +} + +void ThumbnailView::SelectItem( sal_uInt16 nItemId ) +{ + size_t nItemPos = GetItemPos( nItemId ); + if ( nItemPos == THUMBNAILVIEW_ITEM_NOTFOUND ) + return; + + ThumbnailViewItem* pItem = mFilteredItemList[nItemPos]; + if (pItem->isSelected()) + return; + + pItem->setSelection(true); + + if (IsReallyVisible() && IsUpdateMode()) + Invalidate(); + + bool bNewOut = IsReallyVisible() && IsUpdateMode(); + + // if necessary scroll to the visible area + if (mbScroll && nItemId && mnCols) + { + sal_uInt16 nNewLine = static_cast<sal_uInt16>(nItemPos / mnCols); + if ( nNewLine < mnFirstLine ) + { + mnFirstLine = nNewLine; + } + else if ( mnVisLines != 0 && nNewLine > o3tl::make_unsigned(mnFirstLine+mnVisLines-1) ) + { + mnFirstLine = static_cast<sal_uInt16>(nNewLine-mnVisLines+1); + } + } + + if ( bNewOut ) + { + if ( IsReallyVisible() && IsUpdateMode() ) + Invalidate(); + } + + if( !ImplHasAccessibleListeners() ) + return; + + // focus event (select) + ThumbnailViewItemAcc* pItemAcc = ThumbnailViewItemAcc::getImplementation( pItem->GetAccessible( false ) ); + + if( pItemAcc ) + { + css::uno::Any aOldAny, aNewAny; + aNewAny <<= css::uno::Reference< css::uno::XInterface >( + static_cast< ::cppu::OWeakObject* >( pItemAcc )); + ImplFireAccessibleEvent( css::accessibility::AccessibleEventId::ACTIVE_DESCENDANT_CHANGED, aOldAny, aNewAny ); + } + + // selection event + css::uno::Any aOldAny, aNewAny; + ImplFireAccessibleEvent( css::accessibility::AccessibleEventId::SELECTION_CHANGED, aOldAny, aNewAny ); +} + +bool ThumbnailView::IsItemSelected( sal_uInt16 nItemId ) const +{ + size_t nItemPos = GetItemPos( nItemId ); + if ( nItemPos == THUMBNAILVIEW_ITEM_NOTFOUND ) + return false; + + ThumbnailViewItem* pItem = mFilteredItemList[nItemPos]; + return pItem->isSelected(); +} + +void ThumbnailView::deselectItems() +{ + for (std::unique_ptr<ThumbnailViewItem>& p : mItemList) + { + if (p->isSelected()) + { + p->setSelection(false); + } + } + + if (IsReallyVisible() && IsUpdateMode()) + Invalidate(); +} + +void ThumbnailView::ShowTooltips( bool bShowTooltips ) +{ + mbShowTooltips = bShowTooltips; +} + +void ThumbnailView::filterItems(const std::function<bool (const ThumbnailViewItem*)> &func) +{ + mnFirstLine = 0; // start at the top of the list instead of the current position + maFilterFunc = func; + + size_t nSelPos = 0; + bool bHasSelRange = false; + ThumbnailViewItem *curSel = mpStartSelRange != mFilteredItemList.end() ? *mpStartSelRange : nullptr; + + mFilteredItemList.clear(); + + for (size_t i = 0, n = mItemList.size(); i < n; ++i) + { + ThumbnailViewItem *const pItem = mItemList[i].get(); + + if (maFilterFunc(pItem)) + { + if (curSel == pItem) + { + nSelPos = i; + bHasSelRange = true; + } + + mFilteredItemList.push_back(pItem); + } + else + { + if( pItem->isVisible()) + { + if ( ImplHasAccessibleListeners() ) + { + css::uno::Any aOldAny, aNewAny; + + aOldAny <<= pItem->GetAccessible( false ); + ImplFireAccessibleEvent( css::accessibility::AccessibleEventId::CHILD, aOldAny, aNewAny ); + } + + pItem->show(false); + pItem->setSelection(false); + } + } + } + + mpStartSelRange = bHasSelRange ? mFilteredItemList.begin() + nSelPos : mFilteredItemList.end(); + CalculateItemPositions(); + + Invalidate(); +} + +bool ThumbnailViewBase::renameItem(ThumbnailViewItem*, const OUString&) +{ + // Do nothing by default + return false; +} + +ThumbnailViewBase::~ThumbnailViewBase() +{ +} + +BitmapEx ThumbnailView::readThumbnail(const OUString &msURL) +{ + using namespace ::com::sun::star; + using namespace ::com::sun::star::uno; + + // Load the thumbnail from a template document. + uno::Reference<io::XInputStream> xIStream; + + uno::Reference< uno::XComponentContext > xContext(::comphelper::getProcessComponentContext()); + try + { + uno::Reference<lang::XSingleServiceFactory> xStorageFactory = embed::StorageFactory::create(xContext); + + uno::Sequence<uno::Any> aArgs (2); + aArgs[0] <<= msURL; + aArgs[1] <<= embed::ElementModes::READ; + uno::Reference<embed::XStorage> xDocStorage ( + xStorageFactory->createInstanceWithArguments(aArgs), + uno::UNO_QUERY); + + try + { + if (xDocStorage.is()) + { + uno::Reference<embed::XStorage> xStorage ( + xDocStorage->openStorageElement( + "Thumbnails", + embed::ElementModes::READ)); + if (xStorage.is()) + { + uno::Reference<io::XStream> xThumbnailCopy ( + xStorage->cloneStreamElement("thumbnail.png")); + if (xThumbnailCopy.is()) + xIStream = xThumbnailCopy->getInputStream(); + } + } + } + catch (const uno::Exception&) + { + TOOLS_WARN_EXCEPTION("sfx", + "caught exception while trying to access Thumbnail/thumbnail.png of " << msURL); + } + + try + { + // An (older) implementation had a bug - The storage + // name was "Thumbnail" instead of "Thumbnails". The + // old name is still used as fallback but this code can + // be removed soon. + if ( ! xIStream.is()) + { + uno::Reference<embed::XStorage> xStorage ( + xDocStorage->openStorageElement( "Thumbnail", + embed::ElementModes::READ)); + if (xStorage.is()) + { + uno::Reference<io::XStream> xThumbnailCopy ( + xStorage->cloneStreamElement("thumbnail.png")); + if (xThumbnailCopy.is()) + xIStream = xThumbnailCopy->getInputStream(); + } + } + } + catch (const uno::Exception&) + { + TOOLS_WARN_EXCEPTION("sfx", + "caught exception while trying to access Thumbnails/thumbnail.png of " << msURL); + } + } + catch (const uno::Exception&) + { + TOOLS_WARN_EXCEPTION("sfx", + "caught exception while trying to access thumbnail of " + << msURL); + } + + // Extract the image from the stream. + BitmapEx aThumbnail; + if (xIStream.is()) + { + std::unique_ptr<SvStream> pStream ( + ::utl::UcbStreamHelper::CreateStream (xIStream)); + vcl::PNGReader aReader (*pStream); + aThumbnail = aReader.Read (); + } + + // Note that the preview is returned without scaling it to the desired + // width. This gives the caller the chance to take advantage of a + // possibly larger resolution then was asked for. + return aThumbnail; +} + +SfxThumbnailView::SfxThumbnailView(std::unique_ptr<weld::ScrolledWindow> xWindow, std::unique_ptr<weld::Menu> xMenu) + : mnThumbnailHeight(0) + , mnDisplayHeight(0) + , mpItemAttrs(new ThumbnailItemAttributes) + , mxScrolledWindow(std::move(xWindow)) + , mxContextMenu(std::move(xMenu)) +{ + mxScrolledWindow->set_user_managed_scrolling(); + ImplInit(); + mxScrolledWindow->connect_vadjustment_changed(LINK(this, SfxThumbnailView, ImplScrollHdl)); +} + +SfxThumbnailView::~SfxThumbnailView() +{ + css::uno::Reference< css::lang::XComponent> xComponent(mxAccessible, css::uno::UNO_QUERY); + + if (xComponent.is()) + xComponent->dispose(); + + mpItemAttrs.reset(); + + ImplDeleteItems(); +} + +bool SfxThumbnailView::MouseMove(const MouseEvent& rMEvt) +{ + size_t nItemCount = mFilteredItemList.size(); + Point aPoint = rMEvt.GetPosPixel(); + + for (size_t i = 0; i < nItemCount; i++) + { + ThumbnailViewItem *pItem = mFilteredItemList[i]; + ::tools::Rectangle aToInvalidate(pItem->updateHighlight(pItem->mbVisible && !rMEvt.IsLeaveWindow(), aPoint)); + if (!aToInvalidate.IsEmpty() && IsReallyVisible() && IsUpdateMode()) + Invalidate(aToInvalidate); + } + + return true; +} + +OUString SfxThumbnailView::RequestHelp(tools::Rectangle& rHelpRect) +{ + if (!mbShowTooltips) + return OUString(); + + Point aPos = rHelpRect.TopLeft(); + size_t nItemCount = mFilteredItemList.size(); + for (size_t i = 0; i < nItemCount; i++) + { + ThumbnailViewItem *pItem = mFilteredItemList[i]; + if (!pItem->mbVisible) + continue; + const tools::Rectangle& rDrawArea = pItem->getDrawArea(); + if (pItem->mbVisible && rDrawArea.IsInside(aPos)) + { + rHelpRect = rDrawArea; + return pItem->getHelpText(); + } + } + + return OUString(); +} + +void SfxThumbnailView::AppendItem(std::unique_ptr<ThumbnailViewItem> pItem) +{ + if (maFilterFunc(pItem.get())) + { + // Save current start,end range, iterator might get invalidated + size_t nSelStartPos = 0; + ThumbnailViewItem *pSelStartItem = nullptr; + + if (mpStartSelRange != mFilteredItemList.end()) + { + pSelStartItem = *mpStartSelRange; + nSelStartPos = mpStartSelRange - mFilteredItemList.begin(); + } + + mFilteredItemList.push_back(pItem.get()); + mpStartSelRange = pSelStartItem != nullptr ? mFilteredItemList.begin() + nSelStartPos : mFilteredItemList.end(); + } + + mItemList.push_back(std::move(pItem)); +} + +void SfxThumbnailView::ImplInit() +{ + mnItemWidth = 0; + mnItemHeight = 0; + mnItemPadding = 0; + mnVisLines = 0; + mnLines = 0; + mnFirstLine = 0; + mnCols = 0; + mbScroll = false; + mbHasVisibleItems = false; + mbShowTooltips = false; + mbIsMultiSelectionEnabled = true; + maFilterFunc = ViewFilterAll(); + + const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings(); + maFillColor = rSettings.GetFieldColor(); + maTextColor = rSettings.GetWindowTextColor(); + maHighlightColor = rSettings.GetHighlightColor(); + maHighlightTextColor = rSettings.GetWindowTextColor(); + maSelectHighlightColor = rSettings.GetActiveColor(); + maSelectHighlightTextColor = rSettings.GetActiveTextColor(); + + const SvtOptionsDrawinglayer aSvtOptionsDrawinglayer; + mfHighlightTransparence = aSvtOptionsDrawinglayer.GetTransparentSelectionPercent() * 0.01; + + mpStartSelRange = mFilteredItemList.end(); + + mpItemAttrs->aFillColor = maFillColor.getBColor(); + mpItemAttrs->aTextColor = maTextColor.getBColor(); + mpItemAttrs->aHighlightColor = maHighlightColor.getBColor(); + mpItemAttrs->aHighlightTextColor = maHighlightTextColor.getBColor(); + mpItemAttrs->aSelectHighlightColor = maSelectHighlightColor.getBColor(); + mpItemAttrs->aSelectHighlightTextColor = maSelectHighlightTextColor.getBColor(); + mpItemAttrs->fHighlightTransparence = mfHighlightTransparence; + + mpItemAttrs->nMaxTextLength = 0; +} + +void SfxThumbnailView::ImplDeleteItems() +{ + const size_t n = mItemList.size(); + + for ( size_t i = 0; i < n; ++i ) + { + ThumbnailViewItem *const pItem = mItemList[i].get(); + + // deselect all current selected items and fire events + if (pItem->isSelected()) + { + pItem->setSelection(false); + maItemStateHdl.Call(pItem); + + // fire accessible event??? + } + + if ( pItem->isVisible() && ImplHasAccessibleListeners() ) + { + css::uno::Any aOldAny, aNewAny; + + aOldAny <<= pItem->GetAccessible( false ); + ImplFireAccessibleEvent( css::accessibility::AccessibleEventId::CHILD, aOldAny, aNewAny ); + } + + mItemList[i].reset(); + } + + mItemList.clear(); + mFilteredItemList.clear(); + + mpStartSelRange = mFilteredItemList.end(); +} + +void SfxThumbnailView::DrawItem(ThumbnailViewItem const *pItem) +{ + if (pItem->isVisible()) + { + ::tools::Rectangle aRect = pItem->getDrawArea(); + + if (!aRect.IsEmpty()) + Invalidate(aRect); + } +} + +void SfxThumbnailView::OnItemDblClicked (ThumbnailViewItem*) +{ +} + +css::uno::Reference< css::accessibility::XAccessible > SfxThumbnailView::CreateAccessible() +{ + mxAccessible.set(new SfxThumbnailViewAcc(this)); + return mxAccessible; +} + +css::uno::Reference< css::accessibility::XAccessible > SfxThumbnailView::getAccessible() +{ + return mxAccessible; +} + +void SfxThumbnailView::CalculateItemPositions(bool bScrollBarUsed) +{ + if (!mnItemHeight || !mnItemWidth) + return; + + Size aWinSize = GetOutputSizePixel(); + size_t nItemCount = mFilteredItemList.size(); + + // calculate window scroll ratio + float nScrollRatio; + if (bScrollBarUsed) + nScrollRatio = static_cast<float>(mxScrolledWindow->vadjustment_get_value()) / + static_cast<float>(mxScrolledWindow->vadjustment_get_upper()-2); + else + nScrollRatio = 0; + + // calculate ScrollBar width + long nScrBarWidth = mxScrolledWindow->get_vscroll_width(); + + // calculate maximum number of visible columns + mnCols = static_cast<sal_uInt16>((aWinSize.Width()-nScrBarWidth) / mnItemWidth); + + if (!mnCols) + mnCols = 1; + + // calculate maximum number of visible rows + mnVisLines = static_cast<sal_uInt16>(aWinSize.Height() / mnItemHeight); + + // calculate empty space + long nHSpace = aWinSize.Width()-nScrBarWidth - mnCols*mnItemWidth; + long nVSpace = aWinSize.Height() - mnVisLines*mnItemHeight; + long nHItemSpace = nHSpace / (mnCols+1); + long nVItemSpace = nVSpace / (mnVisLines+1); + + // calculate maximum number of rows + // Floor( (M+N-1)/N )==Ceiling( M/N ) + mnLines = (static_cast<long>(nItemCount)+mnCols-1) / mnCols; + + if ( !mnLines ) + mnLines = 1; + + if ( mnLines <= mnVisLines ) + mnFirstLine = 0; + else if ( mnFirstLine > o3tl::make_unsigned(mnLines-mnVisLines) ) + mnFirstLine = static_cast<sal_uInt16>(mnLines-mnVisLines); + + mbHasVisibleItems = true; + + long nItemHeightOffset = mnItemHeight + nVItemSpace; + long nHiddenLines = (static_cast<long>( + ( mnLines - 1 ) * nItemHeightOffset * nScrollRatio ) - + nVItemSpace ) / + nItemHeightOffset; + + // calculate offsets + long nStartX = nHItemSpace; + long nStartY = nVItemSpace; + + // calculate and draw items + long x = nStartX; + long y = nStartY - ( mnLines - 1 ) * nItemHeightOffset * nScrollRatio + + nHiddenLines * nItemHeightOffset; + + // draw items + // Unless we are scrolling (via scrollbar) we just use the precalculated + // mnFirstLine -- our nHiddenLines calculation takes into account only + // what the user has done with the scrollbar but not any changes of selection + // using the keyboard, meaning we could accidentally hide the selected item + // if we believe the scrollbar (fdo#72287). + size_t nFirstItem = (bScrollBarUsed ? nHiddenLines : mnFirstLine) * mnCols; + size_t nLastItem = nFirstItem + (mnVisLines + 1) * mnCols; + + // If want also draw parts of items in the last line, + // then we add one more line if parts of this line are visible + + size_t nCurCount = 0; + for ( size_t i = 0; i < nItemCount; i++ ) + { + ThumbnailViewItem *const pItem = mFilteredItemList[i]; + + if ((nCurCount >= nFirstItem) && (nCurCount < nLastItem)) + { + if( !pItem->isVisible()) + { + if ( ImplHasAccessibleListeners() ) + { + css::uno::Any aOldAny, aNewAny; + + aNewAny <<= pItem->GetAccessible( false ); + ImplFireAccessibleEvent( css::accessibility::AccessibleEventId::CHILD, aOldAny, aNewAny ); + } + + pItem->show(true); + + maItemStateHdl.Call(pItem); + } + + pItem->setDrawArea(::tools::Rectangle( Point(x,y), Size(mnItemWidth, mnItemHeight) )); + pItem->calculateItemsPosition(mnThumbnailHeight,mnItemPadding,mpItemAttrs->nMaxTextLength,mpItemAttrs.get()); + + if ( !((nCurCount+1) % mnCols) ) + { + x = nStartX; + y += mnItemHeight+nVItemSpace; + } + else + x += mnItemWidth+nHItemSpace; + } + else + { + if( pItem->isVisible()) + { + if ( ImplHasAccessibleListeners() ) + { + css::uno::Any aOldAny, aNewAny; + + aOldAny <<= pItem->GetAccessible( false ); + ImplFireAccessibleEvent( css::accessibility::AccessibleEventId::CHILD, aOldAny, aNewAny ); + } + + pItem->show(false); + + maItemStateHdl.Call(pItem); + } + + } + + ++nCurCount; + } + + // arrange ScrollBar, set values and show it + mnLines = (nCurCount+mnCols-1)/mnCols; + + // check if scroll is needed + mbScroll = mnLines > mnVisLines; + + mxScrolledWindow->vadjustment_set_upper((nCurCount+mnCols-1)*gnFineness/mnCols); + mxScrolledWindow->vadjustment_set_page_size(mnVisLines); + if (!bScrollBarUsed) + mxScrolledWindow->vadjustment_set_value(static_cast<long>(mnFirstLine)*gnFineness); + long nPageSize = mnVisLines; + if ( nPageSize < 1 ) + nPageSize = 1; + mxScrolledWindow->vadjustment_set_page_increment(nPageSize); + mxScrolledWindow->set_vpolicy(mbScroll ? VclPolicyType::ALWAYS : VclPolicyType::NEVER); +} + +size_t SfxThumbnailView::ImplGetItem( const Point& rPos ) const +{ + if ( !mbHasVisibleItems ) + { + return THUMBNAILVIEW_ITEM_NOTFOUND; + } + + for (size_t i = 0; i < mFilteredItemList.size(); ++i) + { + if (mFilteredItemList[i]->isVisible() && mFilteredItemList[i]->getDrawArea().IsInside(rPos)) + return i; + } + + return THUMBNAILVIEW_ITEM_NOTFOUND; +} + +ThumbnailViewItem* SfxThumbnailView::ImplGetItem( size_t nPos ) +{ + return ( nPos < mFilteredItemList.size() ) ? mFilteredItemList[nPos] : nullptr; +} + +sal_uInt16 SfxThumbnailView::ImplGetVisibleItemCount() const +{ + sal_uInt16 nRet = 0; + const size_t nItemCount = mItemList.size(); + + for ( size_t n = 0; n < nItemCount; ++n ) + { + if ( mItemList[n]->isVisible() ) + ++nRet; + } + + return nRet; +} + +ThumbnailViewItem* SfxThumbnailView::ImplGetVisibleItem( sal_uInt16 nVisiblePos ) +{ + const size_t nItemCount = mItemList.size(); + + for ( size_t n = 0; n < nItemCount; ++n ) + { + ThumbnailViewItem *const pItem = mItemList[n].get(); + + if ( pItem->isVisible() && !nVisiblePos-- ) + return pItem; + } + + return nullptr; +} + +void SfxThumbnailView::ImplFireAccessibleEvent( short nEventId, const css::uno::Any& rOldValue, const css::uno::Any& rNewValue ) +{ + ThumbnailViewAcc* pAcc = ThumbnailViewAcc::getImplementation(mxAccessible); + + if( pAcc ) + pAcc->FireAccessibleEvent( nEventId, rOldValue, rNewValue ); +} + +bool SfxThumbnailView::ImplHasAccessibleListeners() +{ + ThumbnailViewAcc* pAcc = ThumbnailViewAcc::getImplementation(mxAccessible); + return( pAcc && pAcc->HasAccessibleListeners() ); +} + +IMPL_LINK_NOARG(SfxThumbnailView, ImplScrollHdl, weld::ScrolledWindow&, void) +{ + CalculateItemPositions(true); + if (IsReallyVisible() && IsUpdateMode()) + Invalidate(); +} + +bool SfxThumbnailView::KeyInput( const KeyEvent& rKEvt ) +{ + bool bHandled = true; + + // Get the last selected item in the list + size_t nLastPos = 0; + bool bFoundLast = false; + for ( long i = mFilteredItemList.size() - 1; !bFoundLast && i >= 0; --i ) + { + ThumbnailViewItem* pItem = mFilteredItemList[i]; + if ( pItem->isSelected() ) + { + nLastPos = i; + bFoundLast = true; + } + } + + bool bValidRange = false; + bool bHasSelRange = mpStartSelRange != mFilteredItemList.end(); + size_t nNextPos = nLastPos; + vcl::KeyCode aKeyCode = rKEvt.GetKeyCode(); + ThumbnailViewItem* pNext = nullptr; + + if (aKeyCode.IsShift() && bHasSelRange) + { + //If the last element selected is the start range position + //search for the first selected item + size_t nSelPos = mpStartSelRange - mFilteredItemList.begin(); + + if (nLastPos == nSelPos) + { + while (nLastPos && mFilteredItemList[nLastPos-1]->isSelected()) + --nLastPos; + } + } + + switch ( aKeyCode.GetCode() ) + { + case KEY_RIGHT: + if (!mFilteredItemList.empty()) + { + if ( bFoundLast && nLastPos + 1 < mFilteredItemList.size() ) + { + bValidRange = true; + nNextPos = nLastPos + 1; + } + + pNext = mFilteredItemList[nNextPos]; + } + break; + case KEY_LEFT: + if (!mFilteredItemList.empty()) + { + if ( nLastPos > 0 ) + { + bValidRange = true; + nNextPos = nLastPos - 1; + } + + pNext = mFilteredItemList[nNextPos]; + } + break; + case KEY_DOWN: + if (!mFilteredItemList.empty()) + { + if ( bFoundLast ) + { + //If we are in the second last row just go the one in + //the row below, if there's not row below just go to the + //last item but for the last row don't do anything. + if ( nLastPos + mnCols < mFilteredItemList.size( ) ) + { + bValidRange = true; + nNextPos = nLastPos + mnCols; + } + else + { + int curRow = nLastPos/mnCols; + + if (curRow < mnLines-1) + nNextPos = mFilteredItemList.size()-1; + } + } + + pNext = mFilteredItemList[nNextPos]; + } + break; + case KEY_UP: + if (!mFilteredItemList.empty()) + { + if ( nLastPos >= mnCols ) + { + bValidRange = true; + nNextPos = nLastPos - mnCols; + } + + pNext = mFilteredItemList[nNextPos]; + } + break; + case KEY_RETURN: + { + if ( bFoundLast ) + OnItemDblClicked( mFilteredItemList[nLastPos] ); + } + [[fallthrough]]; + default: + bHandled = CustomWidgetController::KeyInput(rKEvt); + } + + if ( pNext && mbIsMultiSelectionEnabled) + { + if (aKeyCode.IsShift() && bValidRange) + { + std::pair<size_t,size_t> aRange; + size_t nSelPos = mpStartSelRange - mFilteredItemList.begin(); + + if (nLastPos < nSelPos) + { + if (nNextPos > nLastPos) + { + if ( nNextPos > nSelPos) + aRange = std::make_pair(nLastPos,nNextPos); + else + aRange = std::make_pair(nLastPos,nNextPos-1); + } + else + aRange = std::make_pair(nNextPos,nLastPos-1); + } + else if (nLastPos == nSelPos) + { + if (nNextPos > nLastPos) + aRange = std::make_pair(nLastPos+1,nNextPos); + else + aRange = std::make_pair(nNextPos,nLastPos-1); + } + else + { + if (nNextPos > nLastPos) + aRange = std::make_pair(nLastPos+1,nNextPos); + else + { + if ( nNextPos < nSelPos) + aRange = std::make_pair(nNextPos,nLastPos); + else + aRange = std::make_pair(nNextPos+1,nLastPos); + } + } + + for (size_t i = aRange.first; i <= aRange.second; ++i) + { + if (i != nSelPos) + { + ThumbnailViewItem *pCurItem = mFilteredItemList[i]; + + pCurItem->setSelection(!pCurItem->isSelected()); + + if (pCurItem->isVisible()) + DrawItem(pCurItem); + + maItemStateHdl.Call(pCurItem); + } + } + } + else if (!aKeyCode.IsShift()) + { + deselectItems(); + SelectItem(pNext->mnId); + + //Mark it as the selection range start position + mpStartSelRange = mFilteredItemList.begin() + nNextPos; + } + + MakeItemVisible(pNext->mnId); + } + else if(pNext && !mbIsMultiSelectionEnabled) + { + deselectItems(); + SelectItem(pNext->mnId); + MakeItemVisible(pNext->mnId); + } + return bHandled; +} + +void SfxThumbnailView::MakeItemVisible( sal_uInt16 nItemId ) +{ + // Get the item row + size_t nPos = 0; + bool bFound = false; + for ( size_t i = 0; !bFound && i < mFilteredItemList.size(); ++i ) + { + ThumbnailViewItem* pItem = mFilteredItemList[i]; + if ( pItem->mnId == nItemId ) + { + nPos = i; + bFound = true; + } + } + sal_uInt16 nRow = mnCols ? nPos / mnCols : 0; + + // Move the visible rows as little as possible to include that one + if ( nRow < mnFirstLine ) + mnFirstLine = nRow; + else if ( nRow > mnFirstLine + mnVisLines ) + mnFirstLine = nRow - mnVisLines; + + CalculateItemPositions(); + Invalidate(); +} + +bool SfxThumbnailView::MouseButtonDown( const MouseEvent& rMEvt ) +{ + if (!rMEvt.IsLeft()) + { + return CustomWidgetController::MouseButtonDown( rMEvt ); + } + + size_t nPos = ImplGetItem(rMEvt.GetPosPixel()); + ThumbnailViewItem* pItem = ImplGetItem(nPos); + + if ( !pItem ) + { + deselectItems(); + return CustomWidgetController::MouseButtonDown( rMEvt ); + } + + if ( rMEvt.GetClicks() == 2 ) + { + OnItemDblClicked(pItem); + return true; + } + + if ( rMEvt.GetClicks() == 1 && !mbIsMultiSelectionEnabled ) + { + deselectItems(); + pItem->setSelection(!pItem->isSelected()); + + if (!pItem->isHighlighted()) + DrawItem(pItem); + + maItemStateHdl.Call(pItem); + } + else if(rMEvt.GetClicks() == 1) + { + if (rMEvt.IsMod1()) + { + //Keep selected item group state and just invert current desired one state + pItem->setSelection(!pItem->isSelected()); + + //This one becomes the selection range start position if it changes its state to selected otherwise resets it + mpStartSelRange = pItem->isSelected() ? mFilteredItemList.begin() + nPos : mFilteredItemList.end(); + } + else if (rMEvt.IsShift() && mpStartSelRange != mFilteredItemList.end()) + { + std::pair<size_t,size_t> aNewRange; + aNewRange.first = mpStartSelRange - mFilteredItemList.begin(); + aNewRange.second = nPos; + + if (aNewRange.first > aNewRange.second) + std::swap(aNewRange.first,aNewRange.second); + + //Deselect the ones outside of it + for (size_t i = 0, n = mFilteredItemList.size(); i < n; ++i) + { + ThumbnailViewItem *pCurItem = mFilteredItemList[i]; + + if (pCurItem->isSelected() && (i < aNewRange.first || i > aNewRange.second)) + { + pCurItem->setSelection(false); + + if (pCurItem->isVisible()) + DrawItem(pCurItem); + + maItemStateHdl.Call(pCurItem); + } + } + + size_t nSelPos = mpStartSelRange - mFilteredItemList.begin(); + + //Select the items between start range and the selected item + if (nSelPos != nPos) + { + int dir = nSelPos < nPos ? 1 : -1; + size_t nCurPos = nSelPos + dir; + + while (nCurPos != nPos) + { + ThumbnailViewItem *pCurItem = mFilteredItemList[nCurPos]; + + if (!pCurItem->isSelected()) + { + pCurItem->setSelection(true); + + if (pCurItem->isVisible()) + DrawItem(pCurItem); + + maItemStateHdl.Call(pCurItem); + } + + nCurPos += dir; + } + } + + pItem->setSelection(true); + } + else + { + //If we got a group of selected items deselect the rest and only keep the desired one + //mark items as not selected to not fire unnecessary change state events. + pItem->setSelection(false); + deselectItems(); + pItem->setSelection(true); + + //Mark as initial selection range position and reset end one + mpStartSelRange = mFilteredItemList.begin() + nPos; + } + + if (!pItem->isHighlighted()) + DrawItem(pItem); + + maItemStateHdl.Call(pItem); + + //fire accessible event?? + } + return true; +} + +void SfxThumbnailView::SetDrawingArea(weld::DrawingArea* pDrawingArea) +{ + CustomWidgetController::SetDrawingArea(pDrawingArea); + + if (vcl::Window* pDefaultDevice = dynamic_cast<vcl::Window*>(Application::GetDefaultDevice())) + { + OutputDevice& rDevice = pDrawingArea->get_ref_device(); + pDefaultDevice->SetPointFont(rDevice, pDrawingArea->get_font()); + mpItemAttrs->aFontAttr = getFontAttributeFromVclFont(mpItemAttrs->aFontSize, rDevice.GetFont(), false, true); + } + + SetOutputSizePixel(pDrawingArea->get_preferred_size()); +} + +void SfxThumbnailView::Paint(vcl::RenderContext& rRenderContext, const ::tools::Rectangle& /*rRect*/) +{ + rRenderContext.Push(PushFlags::ALL); + + rRenderContext.SetTextFillColor(); + rRenderContext.SetBackground(maFillColor); + + size_t nItemCount = mItemList.size(); + + // Draw background + drawinglayer::primitive2d::Primitive2DContainer aSeq(1); + aSeq[0] = drawinglayer::primitive2d::Primitive2DReference( + new PolyPolygonColorPrimitive2D( + B2DPolyPolygon( ::tools::Polygon(::tools::Rectangle(Point(), GetOutputSizePixel()), 0, 0).getB2DPolygon()), + maFillColor.getBColor())); + + // Create the processor and process the primitives + const drawinglayer::geometry::ViewInformation2D aNewViewInfos; + + std::unique_ptr<drawinglayer::processor2d::BaseProcessor2D> pProcessor( + drawinglayer::processor2d::createBaseProcessor2DFromOutputDevice(rRenderContext, aNewViewInfos)); + pProcessor->process(aSeq); + + // draw items + for (size_t i = 0; i < nItemCount; i++) + { + ThumbnailViewItem *const pItem = mItemList[i].get(); + + if (pItem->isVisible()) + { + pItem->Paint(pProcessor.get(), mpItemAttrs.get()); + } + } + + rRenderContext.Pop(); +} + +void SfxThumbnailView::GetFocus() +{ + // Select the first item if nothing selected + int nSelected = -1; + for (size_t i = 0, n = mItemList.size(); i < n && nSelected == -1; ++i) + { + if (mItemList[i]->isSelected()) + nSelected = i; + } + + if (nSelected == -1 && !mItemList.empty()) + { + SelectItem(1); + } + + // Tell the accessible object that we got the focus. + ThumbnailViewAcc* pAcc = ThumbnailViewAcc::getImplementation(mxAccessible); + if( pAcc ) + pAcc->GetFocus(); + + CustomWidgetController::GetFocus(); +} + +void SfxThumbnailView::LoseFocus() +{ + CustomWidgetController::LoseFocus(); + + // Tell the accessible object that we lost the focus. + ThumbnailViewAcc* pAcc = ThumbnailViewAcc::getImplementation(mxAccessible); + if( pAcc ) + pAcc->LoseFocus(); +} + +void SfxThumbnailView::Resize() +{ + CustomWidgetController::Resize(); + CalculateItemPositions(); + + if ( IsReallyVisible() && IsUpdateMode() ) + Invalidate(); +} + +void SfxThumbnailView::RemoveItem( sal_uInt16 nItemId ) +{ + size_t nPos = GetItemPos( nItemId ); + + if ( nPos == THUMBNAILVIEW_ITEM_NOTFOUND ) + return; + + if ( nPos < mFilteredItemList.size() ) { + + // keep it alive until after we have deleted it from the filter item list + std::unique_ptr<ThumbnailViewItem> xKeepAliveViewItem; + + // delete item from the thumbnail list + for (auto it = mItemList.begin(); it != mItemList.end(); ++it) + { + if ((*it)->mnId == nItemId) + { + xKeepAliveViewItem = std::move(*it); + mItemList.erase(it); + break; + } + } + + // delete item from the filter item list + ThumbnailValueItemList::iterator it = mFilteredItemList.begin(); + ::std::advance( it, nPos ); + + if ((*it)->isSelected()) + { + (*it)->setSelection(false); + maItemStateHdl.Call(*it); + } + + mFilteredItemList.erase( it ); + mpStartSelRange = mFilteredItemList.end(); + } + + CalculateItemPositions(); + + if ( IsReallyVisible() && IsUpdateMode() ) + Invalidate(); +} + +void SfxThumbnailView::Clear() +{ + ImplDeleteItems(); + + // reset variables + mnFirstLine = 0; + + CalculateItemPositions(); + + if ( IsReallyVisible() && IsUpdateMode() ) + Invalidate(); +} + +void SfxThumbnailView::updateItems (std::vector<std::unique_ptr<ThumbnailViewItem>> items) +{ + ImplDeleteItems(); + + // reset variables + mnFirstLine = 0; + + mItemList = std::move(items); + + filterItems(maFilterFunc); +} + +size_t SfxThumbnailView::GetItemPos( sal_uInt16 nItemId ) const +{ + for ( size_t i = 0, n = mFilteredItemList.size(); i < n; ++i ) { + if ( mFilteredItemList[i]->mnId == nItemId ) { + return i; + } + } + return THUMBNAILVIEW_ITEM_NOTFOUND; +} + +sal_uInt16 SfxThumbnailView::GetItemId( size_t nPos ) const +{ + return ( nPos < mFilteredItemList.size() ) ? mFilteredItemList[nPos]->mnId : 0 ; +} + +sal_uInt16 SfxThumbnailView::GetItemId( const Point& rPos ) const +{ + size_t nItemPos = ImplGetItem( rPos ); + if ( nItemPos != THUMBNAILVIEW_ITEM_NOTFOUND ) + return GetItemId( nItemPos ); + + return 0; +} + +sal_uInt16 SfxThumbnailView::getNextItemId() const +{ + return mItemList.empty() ? 1 : mItemList.back()->mnId + 1; +} + +void SfxThumbnailView::setItemMaxTextLength(sal_uInt32 nLength) +{ + mpItemAttrs->nMaxTextLength = nLength; +} + +void SfxThumbnailView::setItemDimensions(long itemWidth, long thumbnailHeight, long displayHeight, int itemPadding) +{ + mnItemWidth = itemWidth + 2*itemPadding; + mnThumbnailHeight = thumbnailHeight; + mnDisplayHeight = displayHeight; + mnItemPadding = itemPadding; + mnItemHeight = mnDisplayHeight + mnThumbnailHeight + 2*itemPadding; +} + +void SfxThumbnailView::SelectItem( sal_uInt16 nItemId ) +{ + size_t nItemPos = GetItemPos( nItemId ); + if ( nItemPos == THUMBNAILVIEW_ITEM_NOTFOUND ) + return; + + ThumbnailViewItem* pItem = mFilteredItemList[nItemPos]; + if (pItem->isSelected()) + return; + + pItem->setSelection(true); + maItemStateHdl.Call(pItem); + + if (IsReallyVisible() && IsUpdateMode()) + Invalidate(); + + bool bNewOut = IsReallyVisible() && IsUpdateMode(); + + // if necessary scroll to the visible area + if (mbScroll && nItemId && mnCols) + { + sal_uInt16 nNewLine = static_cast<sal_uInt16>(nItemPos / mnCols); + if ( nNewLine < mnFirstLine ) + { + mnFirstLine = nNewLine; + } + else if ( mnVisLines != 0 && nNewLine > o3tl::make_unsigned(mnFirstLine+mnVisLines-1) ) + { + mnFirstLine = static_cast<sal_uInt16>(nNewLine-mnVisLines+1); + } + } + + if ( bNewOut ) + { + if ( IsReallyVisible() && IsUpdateMode() ) + Invalidate(); + } + + if( !ImplHasAccessibleListeners() ) + return; + + // focus event (select) + ThumbnailViewItemAcc* pItemAcc = ThumbnailViewItemAcc::getImplementation( pItem->GetAccessible( false ) ); + + if( pItemAcc ) + { + css::uno::Any aOldAny, aNewAny; + aNewAny <<= css::uno::Reference< css::uno::XInterface >( + static_cast< ::cppu::OWeakObject* >( pItemAcc )); + ImplFireAccessibleEvent( css::accessibility::AccessibleEventId::ACTIVE_DESCENDANT_CHANGED, aOldAny, aNewAny ); + } + + // selection event + css::uno::Any aOldAny, aNewAny; + ImplFireAccessibleEvent( css::accessibility::AccessibleEventId::SELECTION_CHANGED, aOldAny, aNewAny ); +} + +bool SfxThumbnailView::IsItemSelected( sal_uInt16 nItemId ) const +{ + size_t nItemPos = GetItemPos( nItemId ); + if ( nItemPos == THUMBNAILVIEW_ITEM_NOTFOUND ) + return false; + + ThumbnailViewItem* pItem = mFilteredItemList[nItemPos]; + return pItem->isSelected(); +} + +void SfxThumbnailView::deselectItems() +{ + for (std::unique_ptr<ThumbnailViewItem>& p : mItemList) + { + if (p->isSelected()) + { + p->setSelection(false); + + maItemStateHdl.Call(p.get()); + } + } + + if (IsReallyVisible() && IsUpdateMode()) + Invalidate(); +} + +void SfxThumbnailView::ShowTooltips( bool bShowTooltips ) +{ + mbShowTooltips = bShowTooltips; +} + +void SfxThumbnailView::SetMultiSelectionEnabled( bool bIsMultiSelectionEnabled ) +{ + mbIsMultiSelectionEnabled = bIsMultiSelectionEnabled; +} + +void SfxThumbnailView::filterItems(const std::function<bool (const ThumbnailViewItem*)> &func) +{ + mnFirstLine = 0; // start at the top of the list instead of the current position + maFilterFunc = func; + + size_t nSelPos = 0; + bool bHasSelRange = false; + ThumbnailViewItem *curSel = mpStartSelRange != mFilteredItemList.end() ? *mpStartSelRange : nullptr; + + mFilteredItemList.clear(); + + for (size_t i = 0, n = mItemList.size(); i < n; ++i) + { + ThumbnailViewItem *const pItem = mItemList[i].get(); + + if (maFilterFunc(pItem)) + { + if (curSel == pItem) + { + nSelPos = i; + bHasSelRange = true; + } + + mFilteredItemList.push_back(pItem); + } + else + { + if( pItem->isVisible()) + { + if ( ImplHasAccessibleListeners() ) + { + css::uno::Any aOldAny, aNewAny; + + aOldAny <<= pItem->GetAccessible( false ); + ImplFireAccessibleEvent( css::accessibility::AccessibleEventId::CHILD, aOldAny, aNewAny ); + } + + pItem->show(false); + pItem->setSelection(false); + + maItemStateHdl.Call(pItem); + } + } + } + + mpStartSelRange = bHasSelRange ? mFilteredItemList.begin() + nSelPos : mFilteredItemList.end(); + CalculateItemPositions(); + + Invalidate(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/thumbnailviewacc.cxx b/sfx2/source/control/thumbnailviewacc.cxx new file mode 100644 index 000000000..e1a7a59c9 --- /dev/null +++ b/sfx2/source/control/thumbnailviewacc.cxx @@ -0,0 +1,1359 @@ +/* -*- 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 . + */ + +#include "thumbnailviewacc.hxx" + +#include <comphelper/servicehelper.hxx> +#include <sfx2/thumbnailview.hxx> +#include <thumbnailviewitem.hxx> +#include <unotools/accessiblestatesethelper.hxx> +#include <vcl/svapp.hxx> +#include <vcl/settings.hxx> +#include <sal/log.hxx> +#include <tools/debug.hxx> +#include <tools/diagnose_ex.h> + +#include <com/sun/star/accessibility/AccessibleEventId.hpp> +#include <com/sun/star/accessibility/AccessibleRole.hpp> +#include <com/sun/star/accessibility/AccessibleStateType.hpp> +#include <com/sun/star/lang/IndexOutOfBoundsException.hpp> + +using namespace ::com::sun::star; + +ThumbnailViewAcc::ThumbnailViewAcc( ThumbnailView* pParent ) : + ValueSetAccComponentBase (m_aMutex), + mpParent( pParent ), + mbIsFocused(false) +{ +} + +ThumbnailViewAcc::~ThumbnailViewAcc() +{ +} + +void ThumbnailViewAcc::FireAccessibleEvent( short nEventId, const uno::Any& rOldValue, const uno::Any& rNewValue ) +{ + if( !nEventId ) + return; + + ::std::vector< uno::Reference< accessibility::XAccessibleEventListener > > aTmpListeners( mxEventListeners ); + accessibility::AccessibleEventObject aEvtObject; + + aEvtObject.EventId = nEventId; + aEvtObject.Source = static_cast<uno::XWeak*>(this); + aEvtObject.NewValue = rNewValue; + aEvtObject.OldValue = rOldValue; + + for (auto const& tmpListener : aTmpListeners) + { + try + { + tmpListener->notifyEvent( aEvtObject ); + } + catch(const uno::Exception&) + { + } + } +} + +namespace +{ + class theValueSetAccUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theValueSetAccUnoTunnelId > {}; +} + +const uno::Sequence< sal_Int8 >& ThumbnailViewAcc::getUnoTunnelId() +{ + return theValueSetAccUnoTunnelId::get().getSeq(); +} + +ThumbnailViewAcc* ThumbnailViewAcc::getImplementation( const uno::Reference< uno::XInterface >& rxData ) + throw() +{ + try + { + return comphelper::getUnoTunnelImplementation<ThumbnailViewAcc>(rxData); + } + catch(const css::uno::Exception&) + { + return nullptr; + } +} + +void ThumbnailViewAcc::GetFocus() +{ + mbIsFocused = true; + + // Broadcast the state change. + css::uno::Any aOldState, aNewState; + aNewState <<= css::accessibility::AccessibleStateType::FOCUSED; + FireAccessibleEvent( + css::accessibility::AccessibleEventId::STATE_CHANGED, + aOldState, aNewState); +} + +void ThumbnailViewAcc::LoseFocus() +{ + mbIsFocused = false; + + // Broadcast the state change. + css::uno::Any aOldState, aNewState; + aOldState <<= css::accessibility::AccessibleStateType::FOCUSED; + FireAccessibleEvent( + css::accessibility::AccessibleEventId::STATE_CHANGED, + aOldState, aNewState); +} + +uno::Reference< accessibility::XAccessibleContext > SAL_CALL ThumbnailViewAcc::getAccessibleContext() +{ + ThrowIfDisposed(); + return this; +} + +sal_Int32 SAL_CALL ThumbnailViewAcc::getAccessibleChildCount() +{ + const SolarMutexGuard aSolarGuard; + ThrowIfDisposed(); + + sal_Int32 nCount = mpParent->ImplGetVisibleItemCount(); + return nCount; +} + +uno::Reference< accessibility::XAccessible > SAL_CALL ThumbnailViewAcc::getAccessibleChild( sal_Int32 i ) +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + ThumbnailViewItem* pItem = getItem (sal::static_int_cast< sal_uInt16 >(i)); + + if( !pItem ) + throw lang::IndexOutOfBoundsException(); + + uno::Reference< accessibility::XAccessible > xRet = pItem->GetAccessible( /*bIsTransientChildrenDisabled*/false ); + return xRet; +} + +uno::Reference< accessibility::XAccessible > SAL_CALL ThumbnailViewAcc::getAccessibleParent() +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + vcl::Window* pParent = mpParent->GetParent(); + uno::Reference< accessibility::XAccessible > xRet; + + if( pParent ) + xRet = pParent->GetAccessible(); + + return xRet; +} + +sal_Int32 SAL_CALL ThumbnailViewAcc::getAccessibleIndexInParent() +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + vcl::Window* pParent = mpParent->GetParent(); + sal_Int32 nRet = 0; + + if( pParent ) + { + bool bFound = false; + + for( sal_uInt16 i = 0, nCount = pParent->GetChildCount(); ( i < nCount ) && !bFound; i++ ) + { + if( pParent->GetChild( i ) == mpParent ) + { + nRet = i; + bFound = true; + } + } + } + + return nRet; +} + +sal_Int16 SAL_CALL ThumbnailViewAcc::getAccessibleRole() +{ + ThrowIfDisposed(); + // #i73746# As the Java Access Bridge (v 2.0.1) uses "managesDescendants" + // always if the role is LIST, we need a different role in this case + return accessibility::AccessibleRole::LIST; +} + +OUString SAL_CALL ThumbnailViewAcc::getAccessibleDescription() +{ + ThrowIfDisposed(); + return "ThumbnailView"; +} + +OUString SAL_CALL ThumbnailViewAcc::getAccessibleName() +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + OUString aRet; + + if ( mpParent ) + { + aRet = mpParent->GetAccessibleName(); + if (aRet.isEmpty()) + { + vcl::Window* pLabel = mpParent->GetAccessibleRelationLabeledBy(); + if (pLabel && pLabel != mpParent) + aRet = OutputDevice::GetNonMnemonicString( pLabel->GetText() ); + } + } + + return aRet; +} + +uno::Reference< accessibility::XAccessibleRelationSet > SAL_CALL ThumbnailViewAcc::getAccessibleRelationSet() +{ + ThrowIfDisposed(); + return uno::Reference< accessibility::XAccessibleRelationSet >(); +} + +uno::Reference< accessibility::XAccessibleStateSet > SAL_CALL ThumbnailViewAcc::getAccessibleStateSet() +{ + ThrowIfDisposed(); + ::utl::AccessibleStateSetHelper* pStateSet = new ::utl::AccessibleStateSetHelper(); + + // Set some states. + pStateSet->AddState (accessibility::AccessibleStateType::ENABLED); + pStateSet->AddState (accessibility::AccessibleStateType::SENSITIVE); + pStateSet->AddState (accessibility::AccessibleStateType::SHOWING); + pStateSet->AddState (accessibility::AccessibleStateType::VISIBLE); + pStateSet->AddState (accessibility::AccessibleStateType::MANAGES_DESCENDANTS); + pStateSet->AddState (accessibility::AccessibleStateType::FOCUSABLE); + if (mbIsFocused) + pStateSet->AddState (accessibility::AccessibleStateType::FOCUSED); + + return pStateSet; +} + +lang::Locale SAL_CALL ThumbnailViewAcc::getLocale() +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + uno::Reference< accessibility::XAccessible > xParent( getAccessibleParent() ); + lang::Locale aRet( "", "", "" ); + + if( xParent.is() ) + { + uno::Reference< accessibility::XAccessibleContext > xParentContext( xParent->getAccessibleContext() ); + + if( xParentContext.is() ) + aRet = xParentContext->getLocale (); + } + + return aRet; +} + +void SAL_CALL ThumbnailViewAcc::addAccessibleEventListener( const uno::Reference< accessibility::XAccessibleEventListener >& rxListener ) +{ + ThrowIfDisposed(); + ::osl::MutexGuard aGuard (m_aMutex); + + if( !rxListener.is() ) + return; + + bool bFound = false; + + for (auto const& eventListener : mxEventListeners) + { + if( eventListener == rxListener ) + { + bFound = true; + break; + } + } + + if (!bFound) + mxEventListeners.push_back( rxListener ); +} + +void SAL_CALL ThumbnailViewAcc::removeAccessibleEventListener( const uno::Reference< accessibility::XAccessibleEventListener >& rxListener ) +{ + ThrowIfDisposed(); + ::osl::MutexGuard aGuard (m_aMutex); + + if( rxListener.is() ) + { + std::vector< uno::Reference< accessibility::XAccessibleEventListener > >::iterator aIter = + std::find(mxEventListeners.begin(), mxEventListeners.end(), rxListener); + + if (aIter != mxEventListeners.end()) + mxEventListeners.erase( aIter ); + } +} + +sal_Bool SAL_CALL ThumbnailViewAcc::containsPoint( const awt::Point& aPoint ) +{ + ThrowIfDisposed(); + const awt::Rectangle aRect( getBounds() ); + const Point aSize( aRect.Width, aRect.Height ); + const Point aNullPoint, aTestPoint( aPoint.X, aPoint.Y ); + + return tools::Rectangle( aNullPoint, aSize ).IsInside( aTestPoint ); +} + +uno::Reference< accessibility::XAccessible > SAL_CALL ThumbnailViewAcc::getAccessibleAtPoint( const awt::Point& aPoint ) +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + const sal_uInt16 nItemId = mpParent->GetItemId( Point( aPoint.X, aPoint.Y ) ); + uno::Reference< accessibility::XAccessible > xRet; + + if ( nItemId ) + { + const size_t nItemPos = mpParent->GetItemPos( nItemId ); + + if( THUMBNAILVIEW_ITEM_NONEITEM != nItemPos ) + { + ThumbnailViewItem *const pItem = mpParent->mFilteredItemList[nItemPos]; + xRet = pItem->GetAccessible( /*bIsTransientChildrenDisabled*/false ); + } + } + + return xRet; +} + +awt::Rectangle SAL_CALL ThumbnailViewAcc::getBounds() +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + const Point aOutPos( mpParent->GetPosPixel() ); + const Size aOutSize( mpParent->GetOutputSizePixel() ); + awt::Rectangle aRet; + + aRet.X = aOutPos.X(); + aRet.Y = aOutPos.Y(); + aRet.Width = aOutSize.Width(); + aRet.Height = aOutSize.Height(); + + return aRet; +} + +awt::Point SAL_CALL ThumbnailViewAcc::getLocation() +{ + ThrowIfDisposed(); + const awt::Rectangle aRect( getBounds() ); + awt::Point aRet; + + aRet.X = aRect.X; + aRet.Y = aRect.Y; + + return aRet; +} + +awt::Point SAL_CALL ThumbnailViewAcc::getLocationOnScreen() +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + const Point aScreenPos( mpParent->OutputToAbsoluteScreenPixel( Point() ) ); + awt::Point aRet; + + aRet.X = aScreenPos.X(); + aRet.Y = aScreenPos.Y(); + + return aRet; +} + +awt::Size SAL_CALL ThumbnailViewAcc::getSize() +{ + ThrowIfDisposed(); + const awt::Rectangle aRect( getBounds() ); + awt::Size aRet; + + aRet.Width = aRect.Width; + aRet.Height = aRect.Height; + + return aRet; +} + +void SAL_CALL ThumbnailViewAcc::grabFocus() +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + mpParent->GrabFocus(); +} + +sal_Int32 SAL_CALL ThumbnailViewAcc::getForeground( ) +{ + ThrowIfDisposed(); + Color nColor = Application::GetSettings().GetStyleSettings().GetWindowTextColor(); + return static_cast<sal_Int32>(nColor); +} + +sal_Int32 SAL_CALL ThumbnailViewAcc::getBackground( ) +{ + ThrowIfDisposed(); + Color nColor = Application::GetSettings().GetStyleSettings().GetWindowColor(); + return static_cast<sal_Int32>(nColor); +} + +void SAL_CALL ThumbnailViewAcc::selectAccessibleChild( sal_Int32 nChildIndex ) +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + ThumbnailViewItem* pItem = getItem (sal::static_int_cast< sal_uInt16 >(nChildIndex)); + + if(pItem == nullptr) + throw lang::IndexOutOfBoundsException(); + + mpParent->SelectItem( pItem->mnId ); +} + +sal_Bool SAL_CALL ThumbnailViewAcc::isAccessibleChildSelected( sal_Int32 nChildIndex ) +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + ThumbnailViewItem* pItem = getItem (sal::static_int_cast< sal_uInt16 >(nChildIndex)); + + if (pItem == nullptr) + throw lang::IndexOutOfBoundsException(); + + return mpParent->IsItemSelected( pItem->mnId ); +} + +void SAL_CALL ThumbnailViewAcc::clearAccessibleSelection() +{ + ThrowIfDisposed(); +} + +void SAL_CALL ThumbnailViewAcc::selectAllAccessibleChildren() +{ + ThrowIfDisposed(); + // unsupported due to single selection only +} + +sal_Int32 SAL_CALL ThumbnailViewAcc::getSelectedAccessibleChildCount() +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + sal_Int32 nRet = 0; + + for( sal_uInt16 i = 0, nCount = getItemCount(); i < nCount; i++ ) + { + ThumbnailViewItem* pItem = getItem (i); + + if( pItem && mpParent->IsItemSelected( pItem->mnId ) ) + ++nRet; + } + + return nRet; +} + +uno::Reference< accessibility::XAccessible > SAL_CALL ThumbnailViewAcc::getSelectedAccessibleChild( sal_Int32 nSelectedChildIndex ) +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + uno::Reference< accessibility::XAccessible > xRet; + + for( sal_uInt16 i = 0, nCount = getItemCount(), nSel = 0; ( i < nCount ) && !xRet.is(); i++ ) + { + ThumbnailViewItem* pItem = getItem(i); + + if( pItem && mpParent->IsItemSelected( pItem->mnId ) && ( nSelectedChildIndex == static_cast< sal_Int32 >( nSel++ ) ) ) + xRet = pItem->GetAccessible( /*bIsTransientChildrenDisabled*/false ); + } + + return xRet; +} + +void SAL_CALL ThumbnailViewAcc::deselectAccessibleChild( sal_Int32 ) +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + // Because of the single selection we can reset the whole selection when + // the specified child is currently selected. +//FIXME TODO if (isAccessibleChildSelected(nChildIndex)) +//FIXME TODO ; +} + +sal_Int64 SAL_CALL ThumbnailViewAcc::getSomething( const uno::Sequence< sal_Int8 >& rId ) +{ + sal_Int64 nRet; + + if( isUnoTunnelId<ThumbnailViewAcc>(rId) ) + nRet = reinterpret_cast< sal_Int64 >( this ); + else + nRet = 0; + + return nRet; +} + +void SAL_CALL ThumbnailViewAcc::disposing() +{ + ::std::vector<uno::Reference<accessibility::XAccessibleEventListener> > aListenerListCopy; + + { + // Make a copy of the list and clear the original. + const SolarMutexGuard aSolarGuard; + ::osl::MutexGuard aGuard (m_aMutex); + aListenerListCopy = mxEventListeners; + mxEventListeners.clear(); + + // Reset the pointer to the parent. It has to be the one who has + // disposed us because he is dying. + mpParent = nullptr; + } + + // Inform all listeners that this objects is disposing. + lang::EventObject aEvent (static_cast<accessibility::XAccessible*>(this)); + for (auto const& listener : aListenerListCopy) + { + try + { + listener->disposing (aEvent); + } + catch(const uno::Exception&) + { + // Ignore exceptions. + } + } +} + +sal_uInt16 ThumbnailViewAcc::getItemCount() const +{ + return mpParent->ImplGetVisibleItemCount(); +} + +ThumbnailViewItem* ThumbnailViewAcc::getItem (sal_uInt16 nIndex) const +{ + return mpParent->ImplGetVisibleItem (nIndex); +} + +void ThumbnailViewAcc::ThrowIfDisposed() +{ + if (rBHelper.bDisposed || rBHelper.bInDispose) + { + SAL_WARN("sfx", "Calling disposed object. Throwing exception:"); + throw lang::DisposedException ( + "object has been already disposed", + static_cast<uno::XWeak*>(this)); + } + else + { + DBG_ASSERT (mpParent!=nullptr, "ValueSetAcc not disposed but mpParent == NULL"); + } +} + +SfxThumbnailViewAcc::SfxThumbnailViewAcc( SfxThumbnailView* pParent ) : + ValueSetAccComponentBase (m_aMutex), + mpParent( pParent ) +{ +} + +SfxThumbnailViewAcc::~SfxThumbnailViewAcc() +{ +} + +namespace +{ + class theSfxValueSetAccUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theSfxValueSetAccUnoTunnelId > {}; +} + +const uno::Sequence< sal_Int8 >& SfxThumbnailViewAcc::getUnoTunnelId() +{ + return theSfxValueSetAccUnoTunnelId::get().getSeq(); +} + +uno::Reference< accessibility::XAccessibleContext > SAL_CALL SfxThumbnailViewAcc::getAccessibleContext() +{ + ThrowIfDisposed(); + return this; +} + +sal_Int32 SAL_CALL SfxThumbnailViewAcc::getAccessibleChildCount() +{ + const SolarMutexGuard aSolarGuard; + ThrowIfDisposed(); + + sal_Int32 nCount = mpParent->ImplGetVisibleItemCount(); + return nCount; +} + +uno::Reference< accessibility::XAccessible > SAL_CALL SfxThumbnailViewAcc::getAccessibleChild( sal_Int32 i ) +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + ThumbnailViewItem* pItem = getItem (sal::static_int_cast< sal_uInt16 >(i)); + + if( !pItem ) + throw lang::IndexOutOfBoundsException(); + + uno::Reference< accessibility::XAccessible > xRet = pItem->GetAccessible( /*bIsTransientChildrenDisabled*/false ); + return xRet; +} + +uno::Reference< accessibility::XAccessible > SAL_CALL SfxThumbnailViewAcc::getAccessibleParent() +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + return mpParent->GetDrawingArea()->get_accessible_parent(); +} + +sal_Int32 SAL_CALL SfxThumbnailViewAcc::getAccessibleIndexInParent() +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + + // -1 for child not found/no parent (according to specification) + sal_Int32 nRet = -1; + + uno::Reference<accessibility::XAccessible> xParent(getAccessibleParent()); + if (!xParent) + return nRet; + + try + { + uno::Reference<accessibility::XAccessibleContext> xParentContext(xParent->getAccessibleContext()); + + // iterate over parent's children and search for this object + if ( xParentContext.is() ) + { + sal_Int32 nChildCount = xParentContext->getAccessibleChildCount(); + for ( sal_Int32 nChild = 0; ( nChild < nChildCount ) && ( -1 == nRet ); ++nChild ) + { + uno::Reference<XAccessible> xChild(xParentContext->getAccessibleChild(nChild)); + if ( xChild.get() == this ) + nRet = nChild; + } + } + } + catch (const uno::Exception&) + { + TOOLS_WARN_EXCEPTION( "sfx", "OAccessibleContextHelper::getAccessibleIndexInParent" ); + } + + return nRet; +} + +sal_Int16 SAL_CALL SfxThumbnailViewAcc::getAccessibleRole() +{ + ThrowIfDisposed(); + // #i73746# As the Java Access Bridge (v 2.0.1) uses "managesDescendants" + // always if the role is LIST, we need a different role in this case + return accessibility::AccessibleRole::LIST; +} + +OUString SAL_CALL SfxThumbnailViewAcc::getAccessibleDescription() +{ + ThrowIfDisposed(); + return "ThumbnailView"; +} + +OUString SAL_CALL SfxThumbnailViewAcc::getAccessibleName() +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + OUString aRet; + + if (mpParent) + { + aRet = mpParent->GetAccessibleName(); + } + + return aRet; +} + +uno::Reference< accessibility::XAccessibleRelationSet > SAL_CALL SfxThumbnailViewAcc::getAccessibleRelationSet() +{ + ThrowIfDisposed(); + return uno::Reference< accessibility::XAccessibleRelationSet >(); +} + +uno::Reference< accessibility::XAccessibleStateSet > SAL_CALL SfxThumbnailViewAcc::getAccessibleStateSet() +{ + ThrowIfDisposed(); + ::utl::AccessibleStateSetHelper* pStateSet = new ::utl::AccessibleStateSetHelper(); + + // Set some states. + pStateSet->AddState (accessibility::AccessibleStateType::ENABLED); + pStateSet->AddState (accessibility::AccessibleStateType::SENSITIVE); + pStateSet->AddState (accessibility::AccessibleStateType::SHOWING); + pStateSet->AddState (accessibility::AccessibleStateType::VISIBLE); + pStateSet->AddState (accessibility::AccessibleStateType::MANAGES_DESCENDANTS); + pStateSet->AddState (accessibility::AccessibleStateType::FOCUSABLE); + + return pStateSet; +} + +lang::Locale SAL_CALL SfxThumbnailViewAcc::getLocale() +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + uno::Reference< accessibility::XAccessible > xParent( getAccessibleParent() ); + lang::Locale aRet( "", "", "" ); + + if( xParent.is() ) + { + uno::Reference< accessibility::XAccessibleContext > xParentContext( xParent->getAccessibleContext() ); + + if( xParentContext.is() ) + aRet = xParentContext->getLocale (); + } + + return aRet; +} + +void SAL_CALL SfxThumbnailViewAcc::addAccessibleEventListener( const uno::Reference< accessibility::XAccessibleEventListener >& rxListener ) +{ + ThrowIfDisposed(); + ::osl::MutexGuard aGuard (m_aMutex); + + if( !rxListener.is() ) + return; + + bool bFound = false; + + for (auto const& eventListener : mxEventListeners) + { + if( eventListener == rxListener ) + { + bFound = true; + break; + } + } + + if (!bFound) + mxEventListeners.push_back( rxListener ); +} + +void SAL_CALL SfxThumbnailViewAcc::removeAccessibleEventListener( const uno::Reference< accessibility::XAccessibleEventListener >& rxListener ) +{ + ThrowIfDisposed(); + ::osl::MutexGuard aGuard (m_aMutex); + + if( rxListener.is() ) + { + std::vector< uno::Reference< accessibility::XAccessibleEventListener > >::iterator aIter = + std::find(mxEventListeners.begin(), mxEventListeners.end(), rxListener); + + if (aIter != mxEventListeners.end()) + mxEventListeners.erase( aIter ); + } +} + +sal_Bool SAL_CALL SfxThumbnailViewAcc::containsPoint( const awt::Point& aPoint ) +{ + ThrowIfDisposed(); + const awt::Rectangle aRect( getBounds() ); + const Point aSize( aRect.Width, aRect.Height ); + const Point aNullPoint, aTestPoint( aPoint.X, aPoint.Y ); + + return tools::Rectangle( aNullPoint, aSize ).IsInside( aTestPoint ); +} + +uno::Reference< accessibility::XAccessible > SAL_CALL SfxThumbnailViewAcc::getAccessibleAtPoint( const awt::Point& aPoint ) +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + const sal_uInt16 nItemId = mpParent->GetItemId( Point( aPoint.X, aPoint.Y ) ); + uno::Reference< accessibility::XAccessible > xRet; + + if ( nItemId ) + { + const size_t nItemPos = mpParent->GetItemPos( nItemId ); + + if( THUMBNAILVIEW_ITEM_NONEITEM != nItemPos ) + { + ThumbnailViewItem *const pItem = mpParent->mFilteredItemList[nItemPos]; + xRet = pItem->GetAccessible( /*bIsTransientChildrenDisabled*/false ); + } + } + + return xRet; +} + +awt::Rectangle SAL_CALL SfxThumbnailViewAcc::getBounds() +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + const Point aOutPos; + const Size aOutSize( mpParent->GetOutputSizePixel() ); + awt::Rectangle aRet; + + aRet.X = aOutPos.X(); + aRet.Y = aOutPos.Y(); + aRet.Width = aOutSize.Width(); + aRet.Height = aOutSize.Height(); + + return aRet; +} + +awt::Point SAL_CALL SfxThumbnailViewAcc::getLocation() +{ + ThrowIfDisposed(); + const awt::Rectangle aRect( getBounds() ); + awt::Point aRet; + + aRet.X = aRect.X; + aRet.Y = aRect.Y; + + return aRet; +} + +awt::Point SAL_CALL SfxThumbnailViewAcc::getLocationOnScreen() +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + awt::Point aScreenLoc(0, 0); + + uno::Reference<accessibility::XAccessible> xParent(getAccessibleParent()); + if (xParent) + { + uno::Reference<accessibility::XAccessibleContext> xParentContext(xParent->getAccessibleContext()); + uno::Reference<accessibility::XAccessibleComponent> xParentComponent(xParentContext, css::uno::UNO_QUERY); + OSL_ENSURE( xParentComponent.is(), "SfxThumbnailViewAcc::getLocationOnScreen: no parent component!" ); + if ( xParentComponent.is() ) + { + awt::Point aParentScreenLoc( xParentComponent->getLocationOnScreen() ); + awt::Point aOwnRelativeLoc( getLocation() ); + aScreenLoc.X = aParentScreenLoc.X + aOwnRelativeLoc.X; + aScreenLoc.Y = aParentScreenLoc.Y + aOwnRelativeLoc.Y; + } + } + + return aScreenLoc; +} + +awt::Size SAL_CALL SfxThumbnailViewAcc::getSize() +{ + ThrowIfDisposed(); + const awt::Rectangle aRect( getBounds() ); + awt::Size aRet; + + aRet.Width = aRect.Width; + aRet.Height = aRect.Height; + + return aRet; +} + +void SAL_CALL SfxThumbnailViewAcc::grabFocus() +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + mpParent->GrabFocus(); +} + +sal_Int32 SAL_CALL SfxThumbnailViewAcc::getForeground( ) +{ + ThrowIfDisposed(); + Color nColor = Application::GetSettings().GetStyleSettings().GetWindowTextColor(); + return static_cast<sal_Int32>(nColor); +} + +sal_Int32 SAL_CALL SfxThumbnailViewAcc::getBackground( ) +{ + ThrowIfDisposed(); + Color nColor = Application::GetSettings().GetStyleSettings().GetWindowColor(); + return static_cast<sal_Int32>(nColor); +} + +void SAL_CALL SfxThumbnailViewAcc::selectAccessibleChild( sal_Int32 nChildIndex ) +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + ThumbnailViewItem* pItem = getItem (sal::static_int_cast< sal_uInt16 >(nChildIndex)); + + if(pItem == nullptr) + throw lang::IndexOutOfBoundsException(); + + mpParent->SelectItem( pItem->mnId ); +} + +sal_Bool SAL_CALL SfxThumbnailViewAcc::isAccessibleChildSelected( sal_Int32 nChildIndex ) +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + ThumbnailViewItem* pItem = getItem (sal::static_int_cast< sal_uInt16 >(nChildIndex)); + + if (pItem == nullptr) + throw lang::IndexOutOfBoundsException(); + + return mpParent->IsItemSelected( pItem->mnId ); +} + +void SAL_CALL SfxThumbnailViewAcc::clearAccessibleSelection() +{ + ThrowIfDisposed(); +} + +void SAL_CALL SfxThumbnailViewAcc::selectAllAccessibleChildren() +{ + ThrowIfDisposed(); + // unsupported due to single selection only +} + +sal_Int32 SAL_CALL SfxThumbnailViewAcc::getSelectedAccessibleChildCount() +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + sal_Int32 nRet = 0; + + for( sal_uInt16 i = 0, nCount = getItemCount(); i < nCount; i++ ) + { + ThumbnailViewItem* pItem = getItem (i); + + if( pItem && mpParent->IsItemSelected( pItem->mnId ) ) + ++nRet; + } + + return nRet; +} + +uno::Reference< accessibility::XAccessible > SAL_CALL SfxThumbnailViewAcc::getSelectedAccessibleChild( sal_Int32 nSelectedChildIndex ) +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + uno::Reference< accessibility::XAccessible > xRet; + + for( sal_uInt16 i = 0, nCount = getItemCount(), nSel = 0; ( i < nCount ) && !xRet.is(); i++ ) + { + ThumbnailViewItem* pItem = getItem(i); + + if( pItem && mpParent->IsItemSelected( pItem->mnId ) && ( nSelectedChildIndex == static_cast< sal_Int32 >( nSel++ ) ) ) + xRet = pItem->GetAccessible( /*bIsTransientChildrenDisabled*/false ); + } + + return xRet; +} + +void SAL_CALL SfxThumbnailViewAcc::deselectAccessibleChild( sal_Int32 ) +{ + ThrowIfDisposed(); + const SolarMutexGuard aSolarGuard; + // Because of the single selection we can reset the whole selection when + // the specified child is currently selected. +//FIXME TODO if (isAccessibleChildSelected(nChildIndex)) +//FIXME TODO ; +} + +sal_Int64 SAL_CALL SfxThumbnailViewAcc::getSomething( const uno::Sequence< sal_Int8 >& rId ) +{ + sal_Int64 nRet; + + if( isUnoTunnelId<SfxThumbnailViewAcc>(rId) ) + nRet = reinterpret_cast< sal_Int64 >( this ); + else + nRet = 0; + + return nRet; +} + +void SAL_CALL SfxThumbnailViewAcc::disposing() +{ + ::std::vector<uno::Reference<accessibility::XAccessibleEventListener> > aListenerListCopy; + + { + // Make a copy of the list and clear the original. + const SolarMutexGuard aSolarGuard; + ::osl::MutexGuard aGuard (m_aMutex); + aListenerListCopy = mxEventListeners; + mxEventListeners.clear(); + + // Reset the pointer to the parent. It has to be the one who has + // disposed us because he is dying. + mpParent = nullptr; + } + + // Inform all listeners that this objects is disposing. + lang::EventObject aEvent (static_cast<accessibility::XAccessible*>(this)); + for (auto const& listener : aListenerListCopy) + { + try + { + listener->disposing (aEvent); + } + catch(const uno::Exception&) + { + // Ignore exceptions. + } + } +} + +sal_uInt16 SfxThumbnailViewAcc::getItemCount() const +{ + return mpParent->ImplGetVisibleItemCount(); +} + +ThumbnailViewItem* SfxThumbnailViewAcc::getItem (sal_uInt16 nIndex) const +{ + return mpParent->ImplGetVisibleItem (nIndex); +} + +void SfxThumbnailViewAcc::ThrowIfDisposed() +{ + if (rBHelper.bDisposed || rBHelper.bInDispose) + { + SAL_WARN("sfx", "Calling disposed object. Throwing exception:"); + throw lang::DisposedException ( + "object has been already disposed", + static_cast<uno::XWeak*>(this)); + } + else + { + DBG_ASSERT (mpParent!=nullptr, "ValueSetAcc not disposed but mpParent == NULL"); + } +} + +ThumbnailViewItemAcc::ThumbnailViewItemAcc( ThumbnailViewItem* pParent, bool bIsTransientChildrenDisabled ) : + mpParent( pParent ), + mbIsTransientChildrenDisabled( bIsTransientChildrenDisabled ) +{ +} + +ThumbnailViewItemAcc::~ThumbnailViewItemAcc() +{ +} + +void ThumbnailViewItemAcc::ParentDestroyed() +{ + const ::osl::MutexGuard aGuard( maMutex ); + mpParent = nullptr; +} + +namespace +{ + class theValueItemAccUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theValueItemAccUnoTunnelId > {}; +} + +const uno::Sequence< sal_Int8 >& ThumbnailViewItemAcc::getUnoTunnelId() +{ + return theValueItemAccUnoTunnelId::get().getSeq(); +} + +ThumbnailViewItemAcc* ThumbnailViewItemAcc::getImplementation( const uno::Reference< uno::XInterface >& rxData ) + throw() +{ + try + { + return comphelper::getUnoTunnelImplementation<ThumbnailViewItemAcc>(rxData); + } + catch(const css::uno::Exception&) + { + return nullptr; + } +} + +uno::Reference< accessibility::XAccessibleContext > SAL_CALL ThumbnailViewItemAcc::getAccessibleContext() +{ + return this; +} + +sal_Int32 SAL_CALL ThumbnailViewItemAcc::getAccessibleChildCount() +{ + return 0; +} + +uno::Reference< accessibility::XAccessible > SAL_CALL ThumbnailViewItemAcc::getAccessibleChild( sal_Int32 ) +{ + throw lang::IndexOutOfBoundsException(); +} + +uno::Reference< accessibility::XAccessible > SAL_CALL ThumbnailViewItemAcc::getAccessibleParent() +{ + const SolarMutexGuard aSolarGuard; + uno::Reference< accessibility::XAccessible > xRet; + + if( mpParent ) + xRet = mpParent->mrParent.getAccessible(); + + return xRet; +} + +sal_Int32 SAL_CALL ThumbnailViewItemAcc::getAccessibleIndexInParent() +{ + const SolarMutexGuard aSolarGuard; + // The index defaults to -1 to indicate the child does not belong to its + // parent. + sal_Int32 nIndexInParent = -1; + + if( mpParent ) + { + bool bDone = false; + + sal_uInt16 nCount = mpParent->mrParent.ImplGetVisibleItemCount(); + ThumbnailViewItem* pItem; + for (sal_uInt16 i=0; i<nCount && !bDone; i++) + { + // Guard the retrieval of the i-th child with a try/catch block + // just in case the number of children changes in the meantime. + try + { + pItem = mpParent->mrParent.ImplGetVisibleItem (i); + } + catch (const lang::IndexOutOfBoundsException&) + { + pItem = nullptr; + } + + // Do not create an accessible object for the test. + if (pItem != nullptr && pItem->mxAcc.is()) + if (pItem->GetAccessible( mbIsTransientChildrenDisabled ).get() == this ) + { + nIndexInParent = i; + bDone = true; + } + } + } + + return nIndexInParent; +} + +sal_Int16 SAL_CALL ThumbnailViewItemAcc::getAccessibleRole() +{ + return accessibility::AccessibleRole::LIST_ITEM; +} + +OUString SAL_CALL ThumbnailViewItemAcc::getAccessibleDescription() +{ + return OUString(); +} + +OUString SAL_CALL ThumbnailViewItemAcc::getAccessibleName() +{ + const SolarMutexGuard aSolarGuard; + OUString aRet; + + if( mpParent ) + { + aRet = mpParent->maTitle; + + if( aRet.isEmpty() ) + { + aRet = "Item " + OUString::number(static_cast<sal_Int32>(mpParent->mnId)); + } + } + + return aRet; +} + +uno::Reference< accessibility::XAccessibleRelationSet > SAL_CALL ThumbnailViewItemAcc::getAccessibleRelationSet() +{ + return uno::Reference< accessibility::XAccessibleRelationSet >(); +} + +uno::Reference< accessibility::XAccessibleStateSet > SAL_CALL ThumbnailViewItemAcc::getAccessibleStateSet() +{ + const SolarMutexGuard aSolarGuard; + ::utl::AccessibleStateSetHelper* pStateSet = new ::utl::AccessibleStateSetHelper; + + if( mpParent ) + { + pStateSet->AddState (accessibility::AccessibleStateType::ENABLED); + pStateSet->AddState (accessibility::AccessibleStateType::SENSITIVE); + pStateSet->AddState (accessibility::AccessibleStateType::SHOWING); + pStateSet->AddState (accessibility::AccessibleStateType::VISIBLE); + if ( !mbIsTransientChildrenDisabled ) + pStateSet->AddState (accessibility::AccessibleStateType::TRANSIENT); + + // SELECTABLE + pStateSet->AddState( accessibility::AccessibleStateType::SELECTABLE ); + // pStateSet->AddState( accessibility::AccessibleStateType::FOCUSABLE ); + + // SELECTED + if( mpParent->isSelected() ) + { + pStateSet->AddState( accessibility::AccessibleStateType::SELECTED ); + // pStateSet->AddState( accessibility::AccessibleStateType::FOCUSED ); + } + } + + return pStateSet; +} + +lang::Locale SAL_CALL ThumbnailViewItemAcc::getLocale() +{ + const SolarMutexGuard aSolarGuard; + uno::Reference< accessibility::XAccessible > xParent( getAccessibleParent() ); + lang::Locale aRet( "", "", "" ); + + if( xParent.is() ) + { + uno::Reference< accessibility::XAccessibleContext > xParentContext( xParent->getAccessibleContext() ); + + if( xParentContext.is() ) + aRet = xParentContext->getLocale(); + } + + return aRet; +} + +void SAL_CALL ThumbnailViewItemAcc::addAccessibleEventListener( const uno::Reference< accessibility::XAccessibleEventListener >& rxListener ) +{ + const ::osl::MutexGuard aGuard( maMutex ); + + if( !rxListener.is() ) + return; + + bool bFound = false; + + for (auto const& eventListener : mxEventListeners) + { + if( eventListener == rxListener ) + { + bFound = true; + break; + } + } + + if (!bFound) + mxEventListeners.push_back( rxListener ); +} + +void SAL_CALL ThumbnailViewItemAcc::removeAccessibleEventListener( const uno::Reference< accessibility::XAccessibleEventListener >& rxListener ) +{ + const ::osl::MutexGuard aGuard( maMutex ); + + if( rxListener.is() ) + { + std::vector< uno::Reference< accessibility::XAccessibleEventListener > >::iterator aIter = + std::find(mxEventListeners.begin(), mxEventListeners.end(), rxListener); + + if (aIter != mxEventListeners.end()) + mxEventListeners.erase( aIter ); + } +} + +sal_Bool SAL_CALL ThumbnailViewItemAcc::containsPoint( const awt::Point& aPoint ) +{ + const awt::Rectangle aRect( getBounds() ); + const Point aSize( aRect.Width, aRect.Height ); + const Point aNullPoint, aTestPoint( aPoint.X, aPoint.Y ); + + return tools::Rectangle( aNullPoint, aSize ).IsInside( aTestPoint ); +} + +uno::Reference< accessibility::XAccessible > SAL_CALL ThumbnailViewItemAcc::getAccessibleAtPoint( const awt::Point& ) +{ + uno::Reference< accessibility::XAccessible > xRet; + return xRet; +} + +awt::Rectangle SAL_CALL ThumbnailViewItemAcc::getBounds() +{ + const SolarMutexGuard aSolarGuard; + awt::Rectangle aRet; + + if( mpParent ) + { + tools::Rectangle aRect( mpParent->getDrawArea() ); + tools::Rectangle aParentRect; + + // get position of the accessible parent in screen coordinates + uno::Reference< XAccessible > xParent = getAccessibleParent(); + if ( xParent.is() ) + { + uno::Reference<XAccessibleComponent> xParentComponent(xParent->getAccessibleContext(), uno::UNO_QUERY); + if (xParentComponent.is()) + { + awt::Size aParentSize = xParentComponent->getSize(); + aParentRect = tools::Rectangle(0, 0, aParentSize.Width, aParentSize.Height); + } + } + + aRect.Intersection( aParentRect ); + + aRet.X = aRect.Left(); + aRet.Y = aRect.Top(); + aRet.Width = aRect.GetWidth(); + aRet.Height = aRect.GetHeight(); + } + + return aRet; +} + +awt::Point SAL_CALL ThumbnailViewItemAcc::getLocation() +{ + const awt::Rectangle aRect( getBounds() ); + awt::Point aRet; + + aRet.X = aRect.X; + aRet.Y = aRect.Y; + + return aRet; +} + +awt::Point SAL_CALL ThumbnailViewItemAcc::getLocationOnScreen() +{ + const SolarMutexGuard aSolarGuard; + awt::Point aRet; + + if( mpParent ) + { + const Point aPos = mpParent->getDrawArea().TopLeft(); + + aRet.X = aPos.X(); + aRet.Y = aPos.Y(); + + // get position of the accessible parent in screen coordinates + uno::Reference< XAccessible > xParent = getAccessibleParent(); + if ( xParent.is() ) + { + uno::Reference<XAccessibleComponent> xParentComponent(xParent->getAccessibleContext(), uno::UNO_QUERY); + if (xParentComponent.is()) + { + awt::Point aParentScreenLoc = xParentComponent->getLocationOnScreen(); + aRet.X += aParentScreenLoc.X; + aRet.Y += aParentScreenLoc.Y; + } + } + } + + return aRet; +} + +awt::Size SAL_CALL ThumbnailViewItemAcc::getSize() +{ + const awt::Rectangle aRect( getBounds() ); + awt::Size aRet; + + aRet.Width = aRect.Width; + aRet.Height = aRect.Height; + + return aRet; +} + +void SAL_CALL ThumbnailViewItemAcc::grabFocus() +{ + // nothing to do +} + +sal_Int32 SAL_CALL ThumbnailViewItemAcc::getForeground( ) +{ + Color nColor = Application::GetSettings().GetStyleSettings().GetWindowTextColor(); + return static_cast<sal_Int32>(nColor); +} + +sal_Int32 SAL_CALL ThumbnailViewItemAcc::getBackground( ) +{ + return static_cast<sal_Int32>(Application::GetSettings().GetStyleSettings().GetWindowColor()); +} + +sal_Int64 SAL_CALL ThumbnailViewItemAcc::getSomething( const uno::Sequence< sal_Int8 >& rId ) +{ + sal_Int64 nRet; + + if( isUnoTunnelId<ThumbnailViewItemAcc>(rId) ) + nRet = reinterpret_cast< sal_Int64 >( this ); + else + nRet = 0; + + return nRet; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/thumbnailviewacc.hxx b/sfx2/source/control/thumbnailviewacc.hxx new file mode 100644 index 000000000..1aaed600a --- /dev/null +++ b/sfx2/source/control/thumbnailviewacc.hxx @@ -0,0 +1,312 @@ +/* -*- 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 . + */ + +#ifndef INCLUDED_SFX2_SOURCE_CONTROL_THUMBNAILVIEWACC_HXX +#define INCLUDED_SFX2_SOURCE_CONTROL_THUMBNAILVIEWACC_HXX + +#include <osl/mutex.hxx> +#include <cppuhelper/implbase.hxx> +#include <cppuhelper/compbase.hxx> +#include <cppuhelper/basemutex.hxx> + +#include <com/sun/star/lang/XUnoTunnel.hpp> +#include <com/sun/star/accessibility/XAccessible.hpp> +#include <com/sun/star/accessibility/XAccessibleContext.hpp> +#include <com/sun/star/accessibility/XAccessibleComponent.hpp> +#include <com/sun/star/accessibility/XAccessibleSelection.hpp> +#include <com/sun/star/accessibility/XAccessibleEventBroadcaster.hpp> + +#include <vcl/vclptr.hxx> +#include <vector> + +class ThumbnailView; +class SfxThumbnailView; +class ThumbnailViewItem; + +typedef ::cppu::WeakComponentImplHelper< + css::accessibility::XAccessible, + css::accessibility::XAccessibleEventBroadcaster, + css::accessibility::XAccessibleContext, + css::accessibility::XAccessibleComponent, + css::accessibility::XAccessibleSelection, + css::lang::XUnoTunnel > + ValueSetAccComponentBase; + +class ThumbnailViewAcc : + public ::cppu::BaseMutex, + public ValueSetAccComponentBase +{ +public: + + ThumbnailViewAcc( ThumbnailView* pParent ); + virtual ~ThumbnailViewAcc() override; + + void FireAccessibleEvent( short nEventId, + const css::uno::Any& rOldValue, + const css::uno::Any& rNewValue ); + + bool HasAccessibleListeners() const { return( mxEventListeners.size() > 0 ); } + + static ThumbnailViewAcc* getImplementation( const css::uno::Reference< css::uno::XInterface >& rxData ) throw(); + +public: + + /** Called by the corresponding ValueSet when it gets the focus. + Stores the new focus state and broadcasts a state change event. + */ + void GetFocus(); + + /** Called by the corresponding ValueSet when it loses the focus. + Stores the new focus state and broadcasts a state change event. + */ + void LoseFocus(); + + // XAccessible + virtual css::uno::Reference< css::accessibility::XAccessibleContext > SAL_CALL getAccessibleContext( ) override; + + // XAccessibleEventBroadcaster + virtual void SAL_CALL addAccessibleEventListener( const css::uno::Reference< css::accessibility::XAccessibleEventListener >& xListener ) override; + virtual void SAL_CALL removeAccessibleEventListener( const css::uno::Reference< css::accessibility::XAccessibleEventListener >& xListener ) override; + + // XAccessibleContext + virtual sal_Int32 SAL_CALL getAccessibleChildCount( ) override; + virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getAccessibleChild( sal_Int32 i ) override; + virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getAccessibleParent( ) override; + virtual sal_Int32 SAL_CALL getAccessibleIndexInParent( ) override; + virtual sal_Int16 SAL_CALL getAccessibleRole( ) override; + virtual OUString SAL_CALL getAccessibleDescription( ) override; + virtual OUString SAL_CALL getAccessibleName( ) override; + virtual css::uno::Reference< css::accessibility::XAccessibleRelationSet > SAL_CALL getAccessibleRelationSet( ) override; + virtual css::uno::Reference< css::accessibility::XAccessibleStateSet > SAL_CALL getAccessibleStateSet( ) override; + virtual css::lang::Locale SAL_CALL getLocale( ) override; + + // XAccessibleComponent + virtual sal_Bool SAL_CALL containsPoint( const css::awt::Point& aPoint ) override; + virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getAccessibleAtPoint( const css::awt::Point& aPoint ) override; + virtual css::awt::Rectangle SAL_CALL getBounds( ) override; + virtual css::awt::Point SAL_CALL getLocation( ) override; + virtual css::awt::Point SAL_CALL getLocationOnScreen( ) override; + virtual css::awt::Size SAL_CALL getSize( ) override; + virtual void SAL_CALL grabFocus( ) override; + virtual sal_Int32 SAL_CALL getForeground( ) override; + virtual sal_Int32 SAL_CALL getBackground( ) override; + + // XAccessibleSelection + virtual void SAL_CALL selectAccessibleChild( sal_Int32 nChildIndex ) override; + virtual sal_Bool SAL_CALL isAccessibleChildSelected( sal_Int32 nChildIndex ) override; + virtual void SAL_CALL clearAccessibleSelection( ) override; + virtual void SAL_CALL selectAllAccessibleChildren( ) override; + virtual sal_Int32 SAL_CALL getSelectedAccessibleChildCount( ) override; + virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getSelectedAccessibleChild( sal_Int32 nSelectedChildIndex ) override; + virtual void SAL_CALL deselectAccessibleChild( sal_Int32 nSelectedChildIndex ) override; + + // XUnoTunnel + static const css::uno::Sequence< sal_Int8 >& getUnoTunnelId(); + virtual sal_Int64 SAL_CALL getSomething( const css::uno::Sequence< sal_Int8 >& rId ) override; + +private: + ::std::vector< css::uno::Reference< + css::accessibility::XAccessibleEventListener > > mxEventListeners; + VclPtr<ThumbnailView> mpParent; + /// The current FOCUSED state. + bool mbIsFocused; + + /** Tell all listeners that the object is dying. This callback is + usually called from the WeakComponentImplHelper class. + */ + virtual void SAL_CALL disposing() override; + + /** Return the number of items. This takes the None-Item into account. + */ + sal_uInt16 getItemCount() const; + + /** Return the item associated with the given index. The None-Item is + taken into account which, when present, is taken to be the first + (with index 0) item. + @param nIndex + Index of the item to return. The index 0 denotes the None-Item + when present. + @return + Returns NULL when the given index is out of range. + */ + ThumbnailViewItem* getItem (sal_uInt16 nIndex) const; + + /** Check whether or not the object has been disposed (or is in the + state of being disposed). If that is the case then + DisposedException is thrown to inform the (indirect) caller of the + foul deed. + + @throws css::lang::DisposedException + */ + void ThrowIfDisposed(); +}; + +class SfxThumbnailViewAcc : + public ::cppu::BaseMutex, + public ValueSetAccComponentBase +{ +public: + + SfxThumbnailViewAcc( SfxThumbnailView* pParent ); + virtual ~SfxThumbnailViewAcc() override; + +public: + + // XAccessible + virtual css::uno::Reference< css::accessibility::XAccessibleContext > SAL_CALL getAccessibleContext( ) override; + + // XAccessibleEventBroadcaster + virtual void SAL_CALL addAccessibleEventListener( const css::uno::Reference< css::accessibility::XAccessibleEventListener >& xListener ) override; + virtual void SAL_CALL removeAccessibleEventListener( const css::uno::Reference< css::accessibility::XAccessibleEventListener >& xListener ) override; + + // XAccessibleContext + virtual sal_Int32 SAL_CALL getAccessibleChildCount( ) override; + virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getAccessibleChild( sal_Int32 i ) override; + virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getAccessibleParent( ) override; + virtual sal_Int32 SAL_CALL getAccessibleIndexInParent( ) override; + virtual sal_Int16 SAL_CALL getAccessibleRole( ) override; + virtual OUString SAL_CALL getAccessibleDescription( ) override; + virtual OUString SAL_CALL getAccessibleName( ) override; + virtual css::uno::Reference< css::accessibility::XAccessibleRelationSet > SAL_CALL getAccessibleRelationSet( ) override; + virtual css::uno::Reference< css::accessibility::XAccessibleStateSet > SAL_CALL getAccessibleStateSet( ) override; + virtual css::lang::Locale SAL_CALL getLocale( ) override; + + // XAccessibleComponent + virtual sal_Bool SAL_CALL containsPoint( const css::awt::Point& aPoint ) override; + virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getAccessibleAtPoint( const css::awt::Point& aPoint ) override; + virtual css::awt::Rectangle SAL_CALL getBounds( ) override; + virtual css::awt::Point SAL_CALL getLocation( ) override; + virtual css::awt::Point SAL_CALL getLocationOnScreen( ) override; + virtual css::awt::Size SAL_CALL getSize( ) override; + virtual void SAL_CALL grabFocus( ) override; + virtual sal_Int32 SAL_CALL getForeground( ) override; + virtual sal_Int32 SAL_CALL getBackground( ) override; + + // XAccessibleSelection + virtual void SAL_CALL selectAccessibleChild( sal_Int32 nChildIndex ) override; + virtual sal_Bool SAL_CALL isAccessibleChildSelected( sal_Int32 nChildIndex ) override; + virtual void SAL_CALL clearAccessibleSelection( ) override; + virtual void SAL_CALL selectAllAccessibleChildren( ) override; + virtual sal_Int32 SAL_CALL getSelectedAccessibleChildCount( ) override; + virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getSelectedAccessibleChild( sal_Int32 nSelectedChildIndex ) override; + virtual void SAL_CALL deselectAccessibleChild( sal_Int32 nSelectedChildIndex ) override; + + // XUnoTunnel + static const css::uno::Sequence< sal_Int8 >& getUnoTunnelId(); + virtual sal_Int64 SAL_CALL getSomething( const css::uno::Sequence< sal_Int8 >& rId ) override; + +private: + ::std::vector< css::uno::Reference< + css::accessibility::XAccessibleEventListener > > mxEventListeners; + SfxThumbnailView* mpParent; + + /** Tell all listeners that the object is dying. This callback is + usually called from the WeakComponentImplHelper class. + */ + virtual void SAL_CALL disposing() override; + + /** Return the number of items. This takes the None-Item into account. + */ + sal_uInt16 getItemCount() const; + + /** Return the item associated with the given index. The None-Item is + taken into account which, when present, is taken to be the first + (with index 0) item. + @param nIndex + Index of the item to return. The index 0 denotes the None-Item + when present. + @return + Returns NULL when the given index is out of range. + */ + ThumbnailViewItem* getItem (sal_uInt16 nIndex) const; + + /** Check whether or not the object has been disposed (or is in the + state of being disposed). If that is the case then + DisposedException is thrown to inform the (indirect) caller of the + foul deed. + + @throws css::lang::DisposedException + */ + void ThrowIfDisposed(); +}; + + +class ThumbnailViewItemAcc : public ::cppu::WeakImplHelper< css::accessibility::XAccessible, + css::accessibility::XAccessibleEventBroadcaster, + css::accessibility::XAccessibleContext, + css::accessibility::XAccessibleComponent, + css::lang::XUnoTunnel > +{ +private: + + ::std::vector< css::uno::Reference< css::accessibility::XAccessibleEventListener > > + mxEventListeners; + ::osl::Mutex maMutex; + ThumbnailViewItem* mpParent; + bool mbIsTransientChildrenDisabled; + +public: + + ThumbnailViewItemAcc( ThumbnailViewItem* pParent, bool bIsTransientChildrenDisabled ); + virtual ~ThumbnailViewItemAcc() override; + + void ParentDestroyed(); + + static ThumbnailViewItemAcc* getImplementation( const css::uno::Reference< css::uno::XInterface >& rxData ) throw(); + +public: + + // XAccessible + virtual css::uno::Reference< css::accessibility::XAccessibleContext > SAL_CALL getAccessibleContext( ) override; + + // XAccessibleEventBroadcaster + virtual void SAL_CALL addAccessibleEventListener( const css::uno::Reference< css::accessibility::XAccessibleEventListener >& xListener ) override; + virtual void SAL_CALL removeAccessibleEventListener( const css::uno::Reference< css::accessibility::XAccessibleEventListener >& xListener ) override; + + // XAccessibleContext + virtual sal_Int32 SAL_CALL getAccessibleChildCount( ) override; + virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getAccessibleChild( sal_Int32 i ) override; + virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getAccessibleParent( ) override; + virtual sal_Int32 SAL_CALL getAccessibleIndexInParent( ) override; + virtual sal_Int16 SAL_CALL getAccessibleRole( ) override; + virtual OUString SAL_CALL getAccessibleDescription( ) override; + virtual OUString SAL_CALL getAccessibleName( ) override; + virtual css::uno::Reference< css::accessibility::XAccessibleRelationSet > SAL_CALL getAccessibleRelationSet( ) override; + virtual css::uno::Reference< css::accessibility::XAccessibleStateSet > SAL_CALL getAccessibleStateSet( ) override; + virtual css::lang::Locale SAL_CALL getLocale( ) override; + + // XAccessibleComponent + virtual sal_Bool SAL_CALL containsPoint( const css::awt::Point& aPoint ) override; + virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getAccessibleAtPoint( const css::awt::Point& aPoint ) override; + virtual css::awt::Rectangle SAL_CALL getBounds( ) override; + virtual css::awt::Point SAL_CALL getLocation( ) override; + virtual css::awt::Point SAL_CALL getLocationOnScreen( ) override; + virtual css::awt::Size SAL_CALL getSize( ) override; + virtual void SAL_CALL grabFocus( ) override; + virtual sal_Int32 SAL_CALL getForeground( ) override; + virtual sal_Int32 SAL_CALL getBackground( ) override; + + // XUnoTunnel + static const css::uno::Sequence< sal_Int8 >& getUnoTunnelId(); + virtual sal_Int64 SAL_CALL getSomething( const css::uno::Sequence< sal_Int8 >& rId ) override; +}; + +#endif // INCLUDED_SFX2_SOURCE_CONTROL_THUMBNAILVIEWACC_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/thumbnailviewitem.cxx b/sfx2/source/control/thumbnailviewitem.cxx new file mode 100644 index 000000000..40a68ec3e --- /dev/null +++ b/sfx2/source/control/thumbnailviewitem.cxx @@ -0,0 +1,278 @@ +/* -*- 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 . + */ + +#include <thumbnailviewitem.hxx> + +#include <sfx2/thumbnailview.hxx> +#include "thumbnailviewacc.hxx" + +#include <basegfx/matrix/b2dhommatrixtools.hxx> +#include <basegfx/polygon/b2dpolygon.hxx> +#include <drawinglayer/attribute/fillgraphicattribute.hxx> +#include <drawinglayer/primitive2d/fillgraphicprimitive2d.hxx> +#include <drawinglayer/primitive2d/polygonprimitive2d.hxx> +#include <drawinglayer/primitive2d/PolyPolygonSelectionPrimitive2D.hxx> +#include <drawinglayer/primitive2d/textlayoutdevice.hxx> +#include <drawinglayer/primitive2d/textprimitive2d.hxx> +#include <drawinglayer/processor2d/baseprocessor2d.hxx> +#include <vcl/graph.hxx> +#include <vcl/texteng.hxx> + +using namespace basegfx; +using namespace basegfx::utils; +using namespace ::com::sun::star; +using namespace drawinglayer::attribute; +using namespace drawinglayer::primitive2d; + +ThumbnailViewItem::ThumbnailViewItem(ThumbnailViewBase &rView, sal_uInt16 nId) + : mrParent(rView) + , mnId(nId) + , mbVisible(true) + , mbSelected(false) + , mbHover(false) + , mxAcc() +{ +} + +ThumbnailViewItem::~ThumbnailViewItem() +{ + if( mxAcc.is() ) + { + static_cast< ThumbnailViewItemAcc* >( mxAcc.get() )->ParentDestroyed(); + } +} + +void ThumbnailViewItem::show (bool bVisible) +{ + mbVisible = bVisible; +} + +void ThumbnailViewItem::setSelection (bool state) +{ + mbSelected = state; +} + +void ThumbnailViewItem::setHighlight (bool state) +{ + mbHover = state; +} + +::tools::Rectangle ThumbnailViewItem::updateHighlight(bool bVisible, const Point& rPoint) +{ + bool bNeedsPaint = false; + + if (bVisible && getDrawArea().IsInside(rPoint)) + { + if (!isHighlighted()) + bNeedsPaint = true; + setHighlight(true); + } + else + { + if (isHighlighted()) + bNeedsPaint = true; + setHighlight(false); + } + + if (bNeedsPaint) + return getDrawArea(); + + return ::tools::Rectangle(); +} + +void ThumbnailViewItem::setTitle (const OUString& rTitle) +{ + if (mrParent.renameItem(this, rTitle)) + maTitle = rTitle; +} + +uno::Reference< accessibility::XAccessible > const & ThumbnailViewItem::GetAccessible( bool bIsTransientChildrenDisabled ) +{ + if( !mxAcc.is() ) + mxAcc = new ThumbnailViewItemAcc( this, bIsTransientChildrenDisabled ); + + return mxAcc; +} + +void ThumbnailViewItem::setDrawArea (const ::tools::Rectangle &area) +{ + maDrawArea = area; +} + +void ThumbnailViewItem::calculateItemsPosition (const long nThumbnailHeight, + const long nPadding, sal_uInt32 nMaxTextLength, + const ThumbnailItemAttributes *pAttrs) +{ + drawinglayer::primitive2d::TextLayouterDevice aTextDev; + aTextDev.setFontAttribute(pAttrs->aFontAttr, + pAttrs->aFontSize.getX(), pAttrs->aFontSize.getY(), + css::lang::Locale() ); + + Size aRectSize = maDrawArea.GetSize(); + Size aImageSize = maPreview1.GetSizePixel(); + + // Calculate thumbnail position + Point aPos = maDrawArea.TopLeft(); + aPos.setX( maDrawArea.getX() + (aRectSize.Width()-aImageSize.Width())/2 ); + aPos.setY( maDrawArea.getY() + nPadding + (nThumbnailHeight-aImageSize.Height())/2 ); + maPrev1Pos = aPos; + + // Calculate text position + aPos.setY( maDrawArea.getY() + nThumbnailHeight + nPadding * 2 ); + aPos.setX( maDrawArea.Left() + (aRectSize.Width() - aTextDev.getTextWidth(maTitle,0,nMaxTextLength))/2 ); + maTextPos = aPos; +} + +void ThumbnailViewItem::Paint (drawinglayer::processor2d::BaseProcessor2D *pProcessor, + const ThumbnailItemAttributes *pAttrs) +{ + BColor aFillColor = pAttrs->aFillColor; + drawinglayer::primitive2d::Primitive2DContainer aSeq(4); + double fTransparence = 0.0; + + // Draw background + if( mbSelected && mbHover) + aFillColor = pAttrs->aSelectHighlightColor; + else if (mbSelected || mbHover) + aFillColor = pAttrs->aHighlightColor; + + if (mbHover) + fTransparence = pAttrs->fHighlightTransparence; + + sal_uInt32 nPrimitive = 0; + aSeq[nPrimitive++] = drawinglayer::primitive2d::Primitive2DReference( + new PolyPolygonSelectionPrimitive2D( B2DPolyPolygon(::tools::Polygon(maDrawArea, THUMBNAILVIEW_ITEM_CORNER, THUMBNAILVIEW_ITEM_CORNER).getB2DPolygon()), + aFillColor, + fTransparence, + 0.0, + true)); + + // Draw thumbnail + Point aPos = maPrev1Pos; + Size aImageSize = maPreview1.GetSizePixel(); + + aSeq[nPrimitive++] = drawinglayer::primitive2d::Primitive2DReference( new FillGraphicPrimitive2D( + createTranslateB2DHomMatrix(aPos.X(),aPos.Y()), + FillGraphicAttribute(Graphic(maPreview1), + B2DRange( + B2DPoint(0,0), + B2DPoint(aImageSize.Width(),aImageSize.Height())), + false) + )); + + // draw thumbnail borders + float fWidth = aImageSize.Width() - 1; + float fHeight = aImageSize.Height() - 1; + float fPosX = maPrev1Pos.getX(); + float fPosY = maPrev1Pos.getY(); + + B2DPolygon aBounds; + aBounds.append(B2DPoint(fPosX,fPosY)); + aBounds.append(B2DPoint(fPosX+fWidth,fPosY)); + aBounds.append(B2DPoint(fPosX+fWidth,fPosY+fHeight)); + aBounds.append(B2DPoint(fPosX,fPosY+fHeight)); + aBounds.setClosed(true); + + aSeq[nPrimitive++] = drawinglayer::primitive2d::Primitive2DReference(createBorderLine(aBounds)); + + // Draw text below thumbnail + addTextPrimitives(maTitle, pAttrs, maTextPos, aSeq); + + pProcessor->process(aSeq); +} + +void ThumbnailViewItem::addTextPrimitives (const OUString& rText, const ThumbnailItemAttributes *pAttrs, Point aPos, drawinglayer::primitive2d::Primitive2DContainer& rSeq) +{ + // adjust text drawing position according to text font + drawinglayer::primitive2d::TextLayouterDevice aTextDev; + aTextDev.setFontAttribute( + pAttrs->aFontAttr, + pAttrs->aFontSize.getX(), + pAttrs->aFontSize.getY(), + css::lang::Locale()); + + aPos.setY(aPos.getY() + aTextDev.getTextHeight()); + + OUString aText (rText); + + TextEngine aTextEngine; + aTextEngine.SetMaxTextWidth(maDrawArea.getWidth()); + aTextEngine.SetText(rText); + + sal_Int32 nPrimitives = rSeq.size(); + rSeq.resize(nPrimitives + aTextEngine.GetLineCount(0)); + + // Create the text primitives + sal_uInt16 nLineStart = 0; + for (sal_uInt16 i=0; i < aTextEngine.GetLineCount(0); ++i) + { + sal_Int32 nLineLength = aTextEngine.GetLineLen(0, i); + double nLineWidth = aTextDev.getTextWidth (aText, nLineStart, nLineLength); + + bool bTooLong = (aPos.getY() + aTextEngine.GetCharHeight()) > maDrawArea.Bottom(); + if (bTooLong && (nLineLength + nLineStart) < rText.getLength()) + { + // Add the '...' to the last line to show, even though it may require to shorten the line + double nDotsWidth = aTextDev.getTextWidth("...",0,3); + + sal_Int32 nLength = nLineLength - 1; + while ( nDotsWidth + aTextDev.getTextWidth(aText, nLineStart, nLength) > maDrawArea.getWidth() && nLength > 0) + { + --nLength; + } + + aText = aText.copy(0, nLineStart+nLength) + "..."; + nLineLength = nLength + 3; + } + + double nLineX = maDrawArea.Left() + (maDrawArea.getWidth() - nLineWidth) / 2.0; + + basegfx::B2DHomMatrix aTextMatrix( createScaleTranslateB2DHomMatrix( + pAttrs->aFontSize.getX(), pAttrs->aFontSize.getY(), + nLineX, double( aPos.Y() ) ) ); + + // setup color + BColor aTextColor = pAttrs->aTextColor; + if( mbSelected && mbHover) + aTextColor = pAttrs->aSelectHighlightTextColor; + else if (mbSelected || mbHover) + aTextColor = pAttrs->aHighlightTextColor; + + rSeq[nPrimitives++] = drawinglayer::primitive2d::Primitive2DReference( + new TextSimplePortionPrimitive2D(aTextMatrix, + aText, nLineStart, nLineLength, + std::vector<double>(), + pAttrs->aFontAttr, + css::lang::Locale(), + aTextColor)); + nLineStart += nLineLength; + aPos.setY(aPos.getY() + aTextEngine.GetCharHeight()); + + if (bTooLong) + break; + } +} + +drawinglayer::primitive2d::PolygonHairlinePrimitive2D* +ThumbnailViewItem::createBorderLine (const basegfx::B2DPolygon& rPolygon) +{ + return new PolygonHairlinePrimitive2D(rPolygon, Color(128, 128, 128).getBColor()); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/source/control/unoctitm.cxx b/sfx2/source/control/unoctitm.cxx new file mode 100644 index 000000000..00b9c7188 --- /dev/null +++ b/sfx2/source/control/unoctitm.cxx @@ -0,0 +1,1266 @@ +/* -*- 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 . + */ + +#include <config_java.h> + +#include <rtl/strbuf.hxx> +#include <tools/debug.hxx> +#include <svl/eitem.hxx> +#include <svl/intitem.hxx> +#include <svl/itemset.hxx> +#include <svl/visitem.hxx> +#include <svtools/javacontext.hxx> +#include <svtools/javainteractionhandler.hxx> +#include <svl/itempool.hxx> +#include <tools/urlobj.hxx> +#include <com/sun/star/awt/FontDescriptor.hpp> +#include <com/sun/star/awt/Point.hpp> +#include <com/sun/star/awt/Size.hpp> +#include <com/sun/star/util/URLTransformer.hpp> +#include <com/sun/star/util/XURLTransformer.hpp> +#include <com/sun/star/frame/Desktop.hpp> +#include <com/sun/star/frame/XFrame.hpp> +#include <com/sun/star/frame/status/FontHeight.hpp> +#include <com/sun/star/frame/status/ItemStatus.hpp> +#include <com/sun/star/frame/status/ItemState.hpp> +#include <com/sun/star/frame/status/Template.hpp> +#include <com/sun/star/frame/DispatchResultState.hpp> +#include <com/sun/star/frame/ModuleManager.hpp> +#include <com/sun/star/frame/status/Visibility.hpp> +#include <comphelper/processfactory.hxx> +#include <officecfg/Office/Common.hxx> +#include <uno/current_context.hxx> +#include <vcl/svapp.hxx> +#include <vcl/uitest/logger.hxx> +#include <boost/property_tree/json_parser.hpp> + +#include <sfx2/app.hxx> +#include <unoctitm.hxx> +#include <sfx2/viewfrm.hxx> +#include <sfx2/frame.hxx> +#include <sfx2/ctrlitem.hxx> +#include <sfx2/sfxuno.hxx> +#include <sfx2/bindings.hxx> +#include <sfx2/dispatch.hxx> +#include <sfx2/sfxsids.hrc> +#include <sfx2/request.hxx> +#include <sfx2/msg.hxx> +#include <sfx2/viewsh.hxx> +#include <slotserv.hxx> +#include <osl/file.hxx> +#include <rtl/ustring.hxx> +#include <unotools/pathoptions.hxx> +#include <osl/time.h> +#include <sfx2/lokhelper.hxx> + +#include <map> +#include <memory> + +#include <sal/log.hxx> +#include <LibreOfficeKit/LibreOfficeKitEnums.h> +#include <comphelper/lok.hxx> + +using namespace ::com::sun::star; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::util; + +namespace { + +enum URLTypeId +{ + URLType_BOOL, + URLType_BYTE, + URLType_SHORT, + URLType_LONG, + URLType_HYPER, + URLType_STRING, + URLType_FLOAT, + URLType_DOUBLE, + URLType_COUNT +}; + +} + +const char* const URLTypeNames[URLType_COUNT] = +{ + "bool", + "byte", + "short", + "long", + "hyper", + "string", + "float", + "double" +}; + +static void InterceptLOKStateChangeEvent( sal_uInt16 nSID, SfxViewFrame* pViewFrame, const css::frame::FeatureStateEvent& aEvent, const SfxPoolItem* pState ); + +void SfxStatusDispatcher::ReleaseAll() +{ + css::lang::EventObject aObject; + aObject.Source = static_cast<cppu::OWeakObject*>(this); + aListeners.disposeAndClear( aObject ); +} + +void SAL_CALL SfxStatusDispatcher::dispatch( const css::util::URL&, const css::uno::Sequence< css::beans::PropertyValue >& ) +{ +} + +void SAL_CALL SfxStatusDispatcher::dispatchWithNotification( + const css::util::URL&, + const css::uno::Sequence< css::beans::PropertyValue >&, + const css::uno::Reference< css::frame::XDispatchResultListener >& ) +{ +} + +SfxStatusDispatcher::SfxStatusDispatcher() + : aListeners( aMutex ) +{ +} + +void SAL_CALL SfxStatusDispatcher::addStatusListener(const css::uno::Reference< css::frame::XStatusListener > & aListener, const css::util::URL& aURL) +{ + aListeners.addInterface( aURL.Complete, aListener ); + if ( aURL.Complete == ".uno:LifeTime" ) + { + css::frame::FeatureStateEvent aEvent; + aEvent.FeatureURL = aURL; + aEvent.Source = static_cast<css::frame::XDispatch*>(this); + aEvent.IsEnabled = true; + aEvent.Requery = false; + aListener->statusChanged( aEvent ); + } +} + +void SAL_CALL SfxStatusDispatcher::removeStatusListener( const css::uno::Reference< css::frame::XStatusListener > & aListener, const css::util::URL& aURL ) +{ + aListeners.removeInterface( aURL.Complete, aListener ); +} + + +// XUnoTunnel +sal_Int64 SAL_CALL SfxOfficeDispatch::getSomething( const css::uno::Sequence< sal_Int8 >& aIdentifier ) +{ + if ( aIdentifier == impl_getStaticIdentifier() ) + return sal::static_int_cast< sal_Int64 >( reinterpret_cast< sal_IntPtr >( this )); + else + return 0; +} + +SfxOfficeDispatch::SfxOfficeDispatch( SfxBindings& rBindings, SfxDispatcher* pDispat, const SfxSlot* pSlot, const css::util::URL& rURL ) + : pImpl( new SfxDispatchController_Impl( this, &rBindings, pDispat, pSlot, rURL )) +{ + // pImpl is an adapter that shows a css::frame::XDispatch-Interface to the outside and uses a SfxControllerItem to monitor a state + +} + +SfxOfficeDispatch::SfxOfficeDispatch( SfxDispatcher* pDispat, const SfxSlot* pSlot, const css::util::URL& rURL ) + : pImpl( new SfxDispatchController_Impl( this, nullptr, pDispat, pSlot, rURL )) +{ + // pImpl is an adapter that shows a css::frame::XDispatch-Interface to the outside and uses a SfxControllerItem to monitor a state +} + +SfxOfficeDispatch::~SfxOfficeDispatch() +{ + if ( pImpl ) + { + // when dispatch object is released, destroy its connection to this object and destroy it + pImpl->UnBindController(); + } +} + +const css::uno::Sequence< sal_Int8 >& SfxOfficeDispatch::impl_getStaticIdentifier() +{ + // {38 57 CA 80 09 36 11 d4 83 FE 00 50 04 52 6B 21} + static const sal_uInt8 pGUID[16] = { 0x38, 0x57, 0xCA, 0x80, 0x09, 0x36, 0x11, 0xd4, 0x83, 0xFE, 0x00, 0x50, 0x04, 0x52, 0x6B, 0x21 }; + static css::uno::Sequence< sal_Int8 > seqID(reinterpret_cast<const sal_Int8*>(pGUID), 16) ; + return seqID ; +} + +#if HAVE_FEATURE_JAVA +// The JavaContext contains an interaction handler which is used when +// the creation of a Java Virtual Machine fails. There shall only be one +// user notification (message box) even if the same error (interaction) +// reoccurs. The effect is, that if a user selects a menu entry than they +// may get only one notification that a JRE is not selected. +// This function checks if a JavaContext is already available (typically +// created by Desktop::Main() in app.cxx), and creates new one if not. +namespace { +std::unique_ptr< css::uno::ContextLayer > EnsureJavaContext() +{ + css::uno::Reference< css::uno::XCurrentContext > xContext(css::uno::getCurrentContext()); + if (xContext.is()) + { + css::uno::Reference< css::task::XInteractionHandler > xHandler; + xContext->getValueByName(JAVA_INTERACTION_HANDLER_NAME) >>= xHandler; + if (xHandler.is()) + return nullptr; // No need to add new layer: JavaContext already present + } + return std::make_unique< css::uno::ContextLayer >(new svt::JavaContext(xContext)); +} +} +#endif + +void SAL_CALL SfxOfficeDispatch::dispatch( const css::util::URL& aURL, const css::uno::Sequence< css::beans::PropertyValue >& aArgs ) +{ + // ControllerItem is the Impl class + if ( pImpl ) + { +#if HAVE_FEATURE_JAVA + std::unique_ptr< css::uno::ContextLayer > layer(EnsureJavaContext()); +#endif + pImpl->dispatch( aURL, aArgs, css::uno::Reference < css::frame::XDispatchResultListener >() ); + } +} + +void SAL_CALL SfxOfficeDispatch::dispatchWithNotification( const css::util::URL& aURL, + const css::uno::Sequence< css::beans::PropertyValue >& aArgs, + const css::uno::Reference< css::frame::XDispatchResultListener >& rListener ) +{ + // ControllerItem is the Impl class + if ( pImpl ) + { +#if HAVE_FEATURE_JAVA + std::unique_ptr< css::uno::ContextLayer > layer(EnsureJavaContext()); +#endif + pImpl->dispatch( aURL, aArgs, rListener ); + } +} + +void SAL_CALL SfxOfficeDispatch::addStatusListener(const css::uno::Reference< css::frame::XStatusListener > & aListener, const css::util::URL& aURL) +{ + GetListeners().addInterface( aURL.Complete, aListener ); + if ( pImpl ) + { + // ControllerItem is the Impl class + pImpl->addStatusListener( aListener, aURL ); + } +} + +SfxDispatcher* SfxOfficeDispatch::GetDispatcher_Impl() +{ + return pImpl->GetDispatcher(); +} + +void SfxOfficeDispatch::SetFrame(const css::uno::Reference< css::frame::XFrame >& xFrame) +{ + if ( pImpl ) + pImpl->SetFrame( xFrame ); +} + +void SfxOfficeDispatch::SetMasterUnoCommand( bool bSet ) +{ + if ( pImpl ) + pImpl->setMasterSlaveCommand( bSet ); +} + +// Determine if URL contains a master/slave command which must be handled a little bit different +bool SfxOfficeDispatch::IsMasterUnoCommand( const css::util::URL& aURL ) +{ + return aURL.Protocol == ".uno:" && ( aURL.Path.indexOf( '.' ) > 0 ); +} + +OUString SfxOfficeDispatch::GetMasterUnoCommand( const css::util::URL& aURL ) +{ + OUString aMasterCommand; + if ( IsMasterUnoCommand( aURL )) + { + sal_Int32 nIndex = aURL.Path.indexOf( '.' ); + if ( nIndex > 0 ) + aMasterCommand = aURL.Path.copy( 0, nIndex ); + } + + return aMasterCommand; +} + +SfxDispatchController_Impl::SfxDispatchController_Impl( + SfxOfficeDispatch* pDisp, + SfxBindings* pBind, + SfxDispatcher* pDispat, + const SfxSlot* pSlot, + const css::util::URL& rURL ) + : aDispatchURL( rURL ) + , pDispatcher( pDispat ) + , pBindings( pBind ) + , pLastState( nullptr ) + , pDispatch( pDisp ) + , bMasterSlave( false ) + , bVisible( true ) +{ + if ( aDispatchURL.Protocol == "slot:" && pSlot->pUnoName ) + { + aDispatchURL.Complete = ".uno:" + OUString::createFromAscii(pSlot->pUnoName); + Reference< XURLTransformer > xTrans( URLTransformer::create( ::comphelper::getProcessComponentContext() ) ); + xTrans->parseStrict( aDispatchURL ); + } + + sal_uInt16 nSlot = pSlot->GetSlotId(); + SetId( nSlot ); + if ( pBindings ) + { + // Bind immediately to enable the cache to recycle dispatches when asked for the same command + // a command in "slot" or in ".uno" notation must be treated as identical commands! + pBindings->ENTERREGISTRATIONS(); + BindInternal_Impl( nSlot, pBindings ); + pBindings->LEAVEREGISTRATIONS(); + } +} + +SfxDispatchController_Impl::~SfxDispatchController_Impl() +{ + if ( pLastState && !IsInvalidItem( pLastState ) ) + delete pLastState; + + if ( pDispatch ) + { + // disconnect + pDispatch->pImpl = nullptr; + + // force all listeners to release the dispatch object + css::lang::EventObject aObject; + aObject.Source = static_cast<cppu::OWeakObject*>(pDispatch); + pDispatch->GetListeners().disposeAndClear( aObject ); + } +} + +void SfxDispatchController_Impl::SetFrame(const css::uno::Reference< css::frame::XFrame >& _xFrame) +{ + xFrame = _xFrame; +} + +void SfxDispatchController_Impl::setMasterSlaveCommand( bool bSet ) +{ + bMasterSlave = bSet; +} + +void SfxDispatchController_Impl::UnBindController() +{ + pDispatch = nullptr; + if ( IsBound() ) + { + GetBindings().ENTERREGISTRATIONS(); + SfxControllerItem::UnBind(); + GetBindings().LEAVEREGISTRATIONS(); + } +} + +void SfxDispatchController_Impl::addParametersToArgs( const css::util::URL& aURL, css::uno::Sequence< css::beans::PropertyValue >& rArgs ) +{ + // Extract the parameter from the URL and put them into the property value sequence + sal_Int32 nQueryIndex = aURL.Complete.indexOf( '?' ); + if ( nQueryIndex <= 0 ) + return; + + OUString aParamString( aURL.Complete.copy( nQueryIndex+1 )); + sal_Int32 nIndex = 0; + do + { + OUString aToken = aParamString.getToken( 0, '&', nIndex ); + + sal_Int32 nParmIndex = 0; + OUString aParamType; + OUString aParamName = aToken.getToken( 0, '=', nParmIndex ); + OUString aValue = aToken.getToken( 0, '=', nParmIndex ); + + if ( !aParamName.isEmpty() ) + { + nParmIndex = 0; + aToken = aParamName; + aParamName = aToken.getToken( 0, ':', nParmIndex ); + aParamType = aToken.getToken( 0, ':', nParmIndex ); + } + + sal_Int32 nLen = rArgs.getLength(); + rArgs.realloc( nLen+1 ); + rArgs[nLen].Name = aParamName; + + if ( aParamType.isEmpty() ) + { + // Default: LONG + rArgs[nLen].Value <<= aValue.toInt32(); + } + else if ( aParamType.equalsAsciiL( URLTypeNames[URLType_BOOL], 4 )) + { + // sal_Bool support + rArgs[nLen].Value <<= aValue.toBoolean(); + } + else if ( aParamType.equalsAsciiL( URLTypeNames[URLType_BYTE], 4 )) + { + // sal_uInt8 support + rArgs[nLen].Value <<= sal_Int8( aValue.toInt32() ); + } + else if ( aParamType.equalsAsciiL( URLTypeNames[URLType_LONG], 4 )) + { + // LONG support + rArgs[nLen].Value <<= aValue.toInt32(); + } + else if ( aParamType.equalsAsciiL( URLTypeNames[URLType_SHORT], 5 )) + { + // SHORT support + rArgs[nLen].Value <<= sal_Int16( aValue.toInt32() ); + } + else if ( aParamType.equalsAsciiL( URLTypeNames[URLType_HYPER], 5 )) + { + // HYPER support + rArgs[nLen].Value <<= aValue.toInt64(); + } + else if ( aParamType.equalsAsciiL( URLTypeNames[URLType_FLOAT], 5 )) + { + // FLOAT support + rArgs[nLen].Value <<= aValue.toFloat(); + } + else if ( aParamType.equalsAsciiL( URLTypeNames[URLType_STRING], 6 )) + { + // STRING support + rArgs[nLen].Value <<= INetURLObject::decode( aValue, INetURLObject::DecodeMechanism::WithCharset ); + } + else if ( aParamType.equalsAsciiL( URLTypeNames[URLType_DOUBLE], 6)) + { + // DOUBLE support + rArgs[nLen].Value <<= aValue.toDouble(); + } + } + while ( nIndex >= 0 ); +} + +MapUnit SfxDispatchController_Impl::GetCoreMetric( SfxItemPool const & rPool, sal_uInt16 nSlotId ) +{ + sal_uInt16 nWhich = rPool.GetWhich( nSlotId ); + return rPool.GetMetric( nWhich ); +} + +OUString SfxDispatchController_Impl::getSlaveCommand( const css::util::URL& rURL ) +{ + OUString aSlaveCommand; + sal_Int32 nIndex = rURL.Path.indexOf( '.' ); + if (( nIndex > 0 ) && ( nIndex < rURL.Path.getLength() )) + aSlaveCommand = rURL.Path.copy( nIndex+1 ); + return aSlaveCommand; +} + +namespace { + +/// Class that collects the usage information - how many times what .uno: command was used. +class UsageInfo { + + typedef std::map<OUString, int> UsageMap; + + /// Are we collecting the info? We cache the value because the call to save can happen very late. + bool mbIsCollecting; + + /// Command vs. how many times it was used + UsageMap maUsage; + + /// config path, get it long before atexit time + OUString msConfigPath; + +public: + UsageInfo() : mbIsCollecting(false) + { + } + + ~UsageInfo() + { + save(); + } + + /// Increment command's use. + void increment(const OUString &rCommand); + + /// Save the usage data for the next session. + void save(); + + /// Modify the flag whether we are collecting. + void setCollecting(bool bIsCollecting) + { + mbIsCollecting = bIsCollecting; + if (mbIsCollecting) + { + msConfigPath = SvtPathOptions().GetConfigPath(); + msConfigPath += "usage/"; + } + } +}; + +void UsageInfo::increment(const OUString &rCommand) +{ + UsageMap::iterator it = maUsage.find(rCommand); + + if (it != maUsage.end()) + ++(it->second); + else + maUsage[rCommand] = 1; +} + +void UsageInfo::save() +{ + if (!mbIsCollecting) + return; + + osl::Directory::createPath(msConfigPath); + + //get system time information. + TimeValue systemTime; + TimeValue localTime; + oslDateTime localDateTime; + osl_getSystemTime( &systemTime ); + osl_getLocalTimeFromSystemTime( &systemTime, &localTime ); + osl_getDateTimeFromTimeValue( &localTime, &localDateTime ); + + char time[1024]; + sprintf(time,"%4i-%02i-%02iT%02i_%02i_%02i", localDateTime.Year, localDateTime.Month, localDateTime.Day, localDateTime.Hours, localDateTime.Minutes, localDateTime.Seconds); + + //filename type: usage-YYYY-MM-DDTHH_MM_SS.csv + OUString filename = "usage-" + OUString::createFromAscii(time) + ".csv"; + OUString path = msConfigPath + filename; + + osl::File file(path); + + if( file.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write | osl_File_OpenFlag_Create) == osl::File::E_None ) + { + OStringBuffer aUsageInfoMsg("Document Type;Command;Count"); + + for (auto const& elem : maUsage) + aUsageInfoMsg.append("\n").append(elem.first.toUtf8()).append(";").append(OString::number(elem.second)); + + sal_uInt64 written = 0; + auto s = aUsageInfoMsg.makeStringAndClear(); + file.write(s.getStr(), s.getLength(), written); + file.close(); + } +} + +class theUsageInfo : public rtl::Static<UsageInfo, theUsageInfo> {}; + +/// Extracts information about the command + args, and stores that. +void collectUsageInformation(const util::URL& rURL, const uno::Sequence<beans::PropertyValue>& rArgs) +{ + bool bCollecting = getenv("LO_COLLECT_USAGE") || officecfg::Office::Common::Misc::CollectUsageInformation::get(); + theUsageInfo::get().setCollecting(bCollecting); + if (!bCollecting) + return; + + OUStringBuffer aBuffer; + + // app identification [uh, several UNO calls :-(] + uno::Reference<uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext(); + uno::Reference<frame::XModuleManager2> xModuleManager(frame::ModuleManager::create(xContext)); + uno::Reference<frame::XDesktop2> xDesktop = frame::Desktop::create(xContext); + uno::Reference<frame::XFrame> xFrame = xDesktop->getCurrentFrame(); + + OUString aModule(xModuleManager->identify(xFrame)); + sal_Int32 nLastDot = aModule.lastIndexOf('.'); + if (nLastDot >= 0) + aModule = aModule.copy(nLastDot + 1); + + aBuffer.append(aModule); + aBuffer.append(';'); + + // command + aBuffer.append(rURL.Protocol); + aBuffer.append(rURL.Path); + sal_Int32 nCount = rArgs.getLength(); + + // parameters - only their names, not the values (could be sensitive!) + if (nCount > 0) + { + aBuffer.append('('); + for (sal_Int32 n = 0; n < nCount; n++) + { + const css::beans::PropertyValue& rProp = rArgs[n]; + if (n > 0) + aBuffer.append(','); + aBuffer.append(rProp.Name); + } + aBuffer.append(')'); + } + + OUString aCommand(aBuffer.makeStringAndClear()); + + // store + theUsageInfo::get().increment(aCommand); +} + +void collectUIInformation(const util::URL& rURL, const css::uno::Sequence< css::beans::PropertyValue >& rArgs) +{ + static const char* pFile = std::getenv("LO_COLLECT_UIINFO"); + if (!pFile) + return; + + UITestLogger::getInstance().logCommand("Send UNO Command (\"" + rURL.Complete + "\") ", rArgs); +} + +} + +void SfxDispatchController_Impl::dispatch( const css::util::URL& aURL, + const css::uno::Sequence< css::beans::PropertyValue >& aArgs, + const css::uno::Reference< css::frame::XDispatchResultListener >& rListener ) +{ + collectUsageInformation(aURL, aArgs); + collectUIInformation(aURL, aArgs); + + SolarMutexGuard aGuard; + if ( + !(pDispatch && + ( + (aURL.Protocol == ".uno:" && aURL.Path == aDispatchURL.Path) || + (aURL.Protocol == "slot:" && aURL.Path.toInt32() == GetId()) + )) + ) + return; + + if ( !pDispatcher && pBindings ) + pDispatcher = GetBindings().GetDispatcher_Impl(); + + css::uno::Sequence< css::beans::PropertyValue > lNewArgs; + sal_Int32 nCount = aArgs.getLength(); + + // Support for URL based arguments + INetURLObject aURLObj( aURL.Complete ); + if ( aURLObj.HasParam() ) + addParametersToArgs( aURL, lNewArgs ); + + // Try to find call mode and frame name inside given arguments... + SfxCallMode nCall = SfxCallMode::RECORD; + sal_Int32 nMarkArg = -1; + + // Filter arguments which shouldn't be part of the sequence property value + sal_uInt16 nModifier(0); + std::vector< css::beans::PropertyValue > aAddArgs; + for( sal_Int32 n=0; n<nCount; n++ ) + { + const css::beans::PropertyValue& rProp = aArgs[n]; + if( rProp.Name == "SynchronMode" ) + { + bool bTemp; + if( rProp.Value >>= bTemp ) + nCall = bTemp ? SfxCallMode::SYNCHRON : SfxCallMode::ASYNCHRON; + } + else if( rProp.Name == "Bookmark" ) + { + nMarkArg = n; + aAddArgs.push_back( aArgs[n] ); + } + else if( rProp.Name == "KeyModifier" ) + rProp.Value >>= nModifier; + else + aAddArgs.push_back( aArgs[n] ); + } + + // Add needed arguments to sequence property value + sal_uInt32 nAddArgs = aAddArgs.size(); + if ( nAddArgs > 0 ) + { + sal_uInt32 nIndex( lNewArgs.getLength() ); + + lNewArgs.realloc( nIndex + nAddArgs ); + std::copy(aAddArgs.begin(), aAddArgs.end(), std::next(lNewArgs.begin(), nIndex)); + } + + // Overwrite possible detected synchron argument, if real listener exists (currently no other way) + if ( rListener.is() ) + nCall = SfxCallMode::SYNCHRON; + + if( GetId() == SID_JUMPTOMARK && nMarkArg == - 1 ) + { + // we offer dispatches for SID_JUMPTOMARK if the URL points to a bookmark inside the document + // so we must retrieve this as an argument from the parsed URL + lNewArgs.realloc( lNewArgs.getLength()+1 ); + nMarkArg = lNewArgs.getLength()-1; + lNewArgs[nMarkArg].Name = "Bookmark"; + lNewArgs[nMarkArg].Value <<= aURL.Mark; + } + + css::uno::Reference< css::frame::XFrame > xFrameRef(xFrame.get(), css::uno::UNO_QUERY); + if (! xFrameRef.is() && pDispatcher) + { + SfxViewFrame* pViewFrame = pDispatcher->GetFrame(); + if (pViewFrame) + xFrameRef = pViewFrame->GetFrame().GetFrameInterface(); + } + + bool bSuccess = false; + const SfxPoolItem* pItem = nullptr; + MapUnit eMapUnit( MapUnit::Map100thMM ); + + // Extra scope so that aInternalSet is destroyed before + // rListener->dispatchFinished potentially calls + // framework::Desktop::terminate -> SfxApplication::Deinitialize -> + // ~CntItemPool: + if (pDispatcher) + { + SfxAllItemSet aInternalSet( SfxGetpApp()->GetPool() ); + if (xFrameRef.is()) // an empty set is no problem ... but an empty frame reference can be a problem ! + aInternalSet.Put( SfxUnoFrameItem( SID_FILLFRAME, xFrameRef ) ); + + SfxShell* pShell( nullptr ); + // #i102619# Retrieve metric from shell before execution - the shell could be destroyed after execution + if ( pDispatcher->GetBindings() ) + { + if ( !pDispatcher->IsLocked() ) + { + const SfxSlot *pSlot = nullptr; + if ( pDispatcher->GetShellAndSlot_Impl( GetId(), &pShell, &pSlot, false, false ) ) + { + if ( bMasterSlave ) + { + // Extract slave command and add argument to the args list. Master slot MUST + // have an argument that has the same name as the master slot and type is SfxStringItem. + sal_Int32 nIndex = lNewArgs.getLength(); + lNewArgs.realloc( nIndex+1 ); + lNewArgs[nIndex].Name = OUString::createFromAscii( pSlot->pUnoName ); + lNewArgs[nIndex].Value <<= SfxDispatchController_Impl::getSlaveCommand( aDispatchURL ); + } + + eMapUnit = GetCoreMetric( pShell->GetPool(), GetId() ); + std::unique_ptr<SfxAllItemSet> xSet(new SfxAllItemSet(pShell->GetPool())); + TransformParameters(GetId(), lNewArgs, *xSet, pSlot); + if (xSet->Count()) + { + // execute with arguments - call directly + pItem = pDispatcher->Execute(GetId(), nCall, xSet.get(), &aInternalSet, nModifier); + if ( pItem != nullptr ) + { + if (const SfxBoolItem* pBoolItem = dynamic_cast<const SfxBoolItem*>(pItem)) + bSuccess = pBoolItem->GetValue(); + else if ( !pItem->IsVoidItem() ) + bSuccess = true; // all other types are true + } + // else bSuccess = false look to line 664 it is false + } + else + { + // Be sure to delete this before we send a dispatch + // request, which will destroy the current shell. + xSet.reset(); + + // execute using bindings, enables support for toggle/enum etc. + SfxRequest aReq( GetId(), nCall, pShell->GetPool() ); + aReq.SetModifier( nModifier ); + aReq.SetInternalArgs_Impl(aInternalSet); + pDispatcher->GetBindings()->Execute_Impl( aReq, pSlot, pShell ); + pItem = aReq.GetReturnValue(); + bSuccess = aReq.IsDone() || pItem != nullptr; + } + } + else + SAL_INFO("sfx.control", "MacroPlayer: Unknown slot dispatched!"); + } + } + else + { + eMapUnit = GetCoreMetric( SfxGetpApp()->GetPool(), GetId() ); + // AppDispatcher + SfxAllItemSet aSet( SfxGetpApp()->GetPool() ); + TransformParameters( GetId(), lNewArgs, aSet ); + + if ( aSet.Count() ) + pItem = pDispatcher->Execute(GetId(), nCall, &aSet, &aInternalSet, nModifier); + else + // SfxRequests take empty sets as argument sets, GetArgs() returning non-zero! + pItem = pDispatcher->Execute(GetId(), nCall, nullptr, &aInternalSet, nModifier); + + // no bindings, no invalidate ( usually done in SfxDispatcher::Call_Impl()! ) + if (SfxApplication* pApp = SfxApplication::Get()) + { + SfxDispatcher* pAppDispat = pApp->GetAppDispatcher_Impl(); + if ( pAppDispat ) + { + const SfxPoolItem* pState=nullptr; + SfxItemState eState = pDispatcher->QueryState( GetId(), pState ); + StateChanged( GetId(), eState, pState ); + } + } + + bSuccess = (pItem != nullptr); + } + } + + if ( !rListener.is() ) + return; + + css::frame::DispatchResultEvent aEvent; + if ( bSuccess ) + aEvent.State = css::frame::DispatchResultState::SUCCESS; + else + aEvent.State = css::frame::DispatchResultState::FAILURE; + + aEvent.Source = static_cast<css::frame::XDispatch*>(pDispatch); + if ( bSuccess && pItem && !pItem->IsVoidItem() ) + { + sal_uInt16 nSubId( 0 ); + if ( eMapUnit == MapUnit::MapTwip ) + nSubId |= CONVERT_TWIPS; + pItem->QueryValue( aEvent.Result, static_cast<sal_uInt8>(nSubId) ); + } + + rListener->dispatchFinished( aEvent ); +} + +SfxDispatcher* SfxDispatchController_Impl::GetDispatcher() +{ + if ( !pDispatcher && pBindings ) + pDispatcher = GetBindings().GetDispatcher_Impl(); + return pDispatcher; +} + +void SfxDispatchController_Impl::addStatusListener(const css::uno::Reference< css::frame::XStatusListener > & aListener, const css::util::URL& aURL) +{ + SolarMutexGuard aGuard; + if ( !pDispatch ) + return; + + // Use alternative QueryState call to have a valid UNO representation of the state. + css::uno::Any aState; + if ( !pDispatcher && pBindings ) + pDispatcher = GetBindings().GetDispatcher_Impl(); + SfxItemState eState = pDispatcher ? pDispatcher->QueryState( GetId(), aState ) : SfxItemState::DONTCARE; + + if ( eState == SfxItemState::DONTCARE ) + { + // Use special uno struct to transport don't care state + css::frame::status::ItemStatus aItemStatus; + aItemStatus.State = css::frame::status::ItemState::DONT_CARE; + aState <<= aItemStatus; + } + + css::frame::FeatureStateEvent aEvent; + aEvent.FeatureURL = aURL; + aEvent.Source = static_cast<css::frame::XDispatch*>(pDispatch); + aEvent.Requery = false; + if ( bVisible ) + { + aEvent.IsEnabled = eState != SfxItemState::DISABLED; + aEvent.State = aState; + } + else + { + css::frame::status::Visibility aVisibilityStatus; + aVisibilityStatus.bVisible = false; + + // MBA: we might decide to *not* disable "invisible" slots, but this would be + // a change that needs to adjust at least the testtool + aEvent.IsEnabled = false; + aEvent.State <<= aVisibilityStatus; + } + + aListener->statusChanged( aEvent ); +} + +void SfxDispatchController_Impl::sendStatusChanged(const OUString& rURL, const css::frame::FeatureStateEvent& rEvent) +{ + ::cppu::OInterfaceContainerHelper* pContnr = pDispatch->GetListeners().getContainer(rURL); + if (!pContnr) + return; + ::cppu::OInterfaceIteratorHelper aIt(*pContnr); + while (aIt.hasMoreElements()) + { + try + { + static_cast<css::frame::XStatusListener*>(aIt.next())->statusChanged(rEvent); + } + catch (const css::uno::RuntimeException&) + { + aIt.remove(); + } + } +} + +void SfxDispatchController_Impl::StateChanged( sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState, SfxSlotServer const * pSlotServ ) +{ + if ( !pDispatch ) + return; + + // Bindings instance notifies controller about a state change, listeners must be notified also + // Don't cache visibility state changes as they are volatile. We need our real state to send it + // to our controllers after visibility is set to true. + bool bNotify = true; + if ( pState && !IsInvalidItem( pState ) ) + { + if ( dynamic_cast< const SfxVisibilityItem *>( pState ) == nullptr ) + { + if (pLastState && !IsInvalidItem(pLastState)) + { + bNotify = typeid(*pState) != typeid(*pLastState) || *pState != *pLastState; + delete pLastState; + } + pLastState = !IsInvalidItem(pState) ? pState->Clone() : pState; + bVisible = true; + } + else + bVisible = static_cast<const SfxVisibilityItem *>(pState)->GetValue(); + } + else + { + if ( pLastState && !IsInvalidItem( pLastState ) ) + delete pLastState; + pLastState = pState; + } + + if (!bNotify) + return; + + css::uno::Any aState; + if ( ( eState >= SfxItemState::DEFAULT ) && pState && !IsInvalidItem( pState ) && !pState->IsVoidItem() ) + { + // Retrieve metric from pool to have correct sub ID when calling QueryValue + sal_uInt16 nSubId( 0 ); + MapUnit eMapUnit( MapUnit::Map100thMM ); + + // retrieve the core metric + // it's enough to check the objectshell, the only shell that does not use the pool of the document + // is SfxViewFrame, but it hasn't any metric parameters + // TODO/LATER: what about the FormShell? Does it use any metric data?! Perhaps it should use the Pool of the document! + if ( pSlotServ && pDispatcher ) + { + SfxShell* pShell = pDispatcher->GetShell( pSlotServ->GetShellLevel() ); + DBG_ASSERT( pShell, "Can't get core metric without shell!" ); + if ( pShell ) + eMapUnit = GetCoreMetric( pShell->GetPool(), nSID ); + } + + if ( eMapUnit == MapUnit::MapTwip ) + nSubId |= CONVERT_TWIPS; + + pState->QueryValue( aState, static_cast<sal_uInt8>(nSubId) ); + } + else if ( eState == SfxItemState::DONTCARE ) + { + // Use special uno struct to transport don't care state + css::frame::status::ItemStatus aItemStatus; + aItemStatus.State = css::frame::status::ItemState::DONT_CARE; + aState <<= aItemStatus; + } + + css::frame::FeatureStateEvent aEvent; + aEvent.FeatureURL = aDispatchURL; + aEvent.Source = static_cast<css::frame::XDispatch*>(pDispatch); + aEvent.IsEnabled = eState != SfxItemState::DISABLED; + aEvent.Requery = false; + aEvent.State = aState; + + if (pDispatcher && pDispatcher->GetFrame()) + { + InterceptLOKStateChangeEvent(nSID, pDispatcher->GetFrame(), aEvent, pState); + } + + const css::uno::Sequence<OUString> aContainedTypes = pDispatch->GetListeners().getContainedTypes(); + for (const OUString& rName: aContainedTypes) + { + if (rName == aDispatchURL.Main || rName == aDispatchURL.Complete) + sendStatusChanged(rName, aEvent); + } +} + +void SfxDispatchController_Impl::StateChanged( sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState ) +{ + StateChanged( nSID, eState, pState, nullptr ); +} + +static void InterceptLOKStateChangeEvent(sal_uInt16 nSID, SfxViewFrame* pViewFrame, const css::frame::FeatureStateEvent& aEvent, const SfxPoolItem* pState) +{ + if (!comphelper::LibreOfficeKit::isActive()) + return; + + OUStringBuffer aBuffer; + aBuffer.append(aEvent.FeatureURL.Complete); + aBuffer.append(u'='); + + if (aEvent.FeatureURL.Path == "Bold" || + aEvent.FeatureURL.Path == "CenterPara" || + aEvent.FeatureURL.Path == "CharBackgroundExt" || + aEvent.FeatureURL.Path == "ControlCodes" || + aEvent.FeatureURL.Path == "DefaultBullet" || + aEvent.FeatureURL.Path == "DefaultNumbering" || + aEvent.FeatureURL.Path == "Italic" || + aEvent.FeatureURL.Path == "JustifyPara" || + aEvent.FeatureURL.Path == "LeftPara" || + aEvent.FeatureURL.Path == "OutlineFont" || + aEvent.FeatureURL.Path == "RightPara" || + aEvent.FeatureURL.Path == "Shadowed" || + aEvent.FeatureURL.Path == "SpellOnline" || + aEvent.FeatureURL.Path == "OnlineAutoFormat" || + aEvent.FeatureURL.Path == "SubScript" || + aEvent.FeatureURL.Path == "SuperScript" || + aEvent.FeatureURL.Path == "Strikeout" || + aEvent.FeatureURL.Path == "Underline" || + aEvent.FeatureURL.Path == "ModifiedStatus" || + aEvent.FeatureURL.Path == "TrackChanges" || + aEvent.FeatureURL.Path == "ShowTrackedChanges" || + aEvent.FeatureURL.Path == "NextTrackedChange" || + aEvent.FeatureURL.Path == "PreviousTrackedChange" || + aEvent.FeatureURL.Path == "AlignLeft" || + aEvent.FeatureURL.Path == "AlignHorizontalCenter" || + aEvent.FeatureURL.Path == "AlignRight" || + aEvent.FeatureURL.Path == "DocumentRepair" || + aEvent.FeatureURL.Path == "ObjectAlignLeft" || + aEvent.FeatureURL.Path == "ObjectAlignRight" || + aEvent.FeatureURL.Path == "AlignCenter") + { + bool bTemp = false; + aEvent.State >>= bTemp; + aBuffer.append(bTemp); + } + else if (aEvent.FeatureURL.Path == "CharFontName") + { + css::awt::FontDescriptor aFontDesc; + aEvent.State >>= aFontDesc; + aBuffer.append(aFontDesc.Name); + } + else if (aEvent.FeatureURL.Path == "FontHeight") + { + css::frame::status::FontHeight aFontHeight; + aEvent.State >>= aFontHeight; + aBuffer.append(aFontHeight.Height); + } + else if (aEvent.FeatureURL.Path == "StyleApply") + { + css::frame::status::Template aTemplate; + aEvent.State >>= aTemplate; + aBuffer.append(aTemplate.StyleName); + } + else if (aEvent.FeatureURL.Path == "BackColor" || + aEvent.FeatureURL.Path == "BackgroundColor" || + aEvent.FeatureURL.Path == "CharBackColor" || + aEvent.FeatureURL.Path == "Color" || + aEvent.FeatureURL.Path == "FontColor" || + aEvent.FeatureURL.Path == "FrameLineColor") + { + sal_Int32 nColor = -1; + aEvent.State >>= nColor; + aBuffer.append(nColor); + } + else if (aEvent.FeatureURL.Path == "Undo" || + aEvent.FeatureURL.Path == "Redo") + { + const SfxUInt32Item* pUndoConflict = dynamic_cast< const SfxUInt32Item * >( pState ); + if ( pUndoConflict && pUndoConflict->GetValue() > 0 ) + { + aBuffer.append("disabled"); + } + else + { + aBuffer.append(aEvent.IsEnabled ? OUStringLiteral("enabled") : OUStringLiteral("disabled")); + } + } + else if (aEvent.FeatureURL.Path == "Cut" || + aEvent.FeatureURL.Path == "Copy" || + aEvent.FeatureURL.Path == "Paste" || + aEvent.FeatureURL.Path == "SelectAll" || + aEvent.FeatureURL.Path == "InsertAnnotation" || + aEvent.FeatureURL.Path == "DeleteAnnotation" || + aEvent.FeatureURL.Path == "ResolveAnnotation" || + aEvent.FeatureURL.Path == "InsertRowsBefore" || + aEvent.FeatureURL.Path == "InsertRowsAfter" || + aEvent.FeatureURL.Path == "InsertColumnsBefore" || + aEvent.FeatureURL.Path == "InsertColumnsAfter" || + aEvent.FeatureURL.Path == "InsertSymbol" || + aEvent.FeatureURL.Path == "InsertPage" || + aEvent.FeatureURL.Path == "DeletePage" || + aEvent.FeatureURL.Path == "DuplicatePage" || + aEvent.FeatureURL.Path == "DeleteRows" || + aEvent.FeatureURL.Path == "DeleteColumns" || + aEvent.FeatureURL.Path == "DeleteTable" || + aEvent.FeatureURL.Path == "SelectTable" || + aEvent.FeatureURL.Path == "EntireRow" || + aEvent.FeatureURL.Path == "EntireColumn" || + aEvent.FeatureURL.Path == "EntireCell" || + aEvent.FeatureURL.Path == "SortAscending" || + aEvent.FeatureURL.Path == "SortDescending" || + aEvent.FeatureURL.Path == "AcceptAllTrackedChanges" || + aEvent.FeatureURL.Path == "RejectAllTrackedChanges" || + aEvent.FeatureURL.Path == "TableDialog" || + aEvent.FeatureURL.Path == "FormatCellDialog" || + aEvent.FeatureURL.Path == "FontDialog" || + aEvent.FeatureURL.Path == "ParagraphDialog" || + aEvent.FeatureURL.Path == "OutlineBullet" || + aEvent.FeatureURL.Path == "InsertIndexesEntry" || + aEvent.FeatureURL.Path == "TransformDialog" || + aEvent.FeatureURL.Path == "EditRegion" || + aEvent.FeatureURL.Path == "ThesaurusDialog" || + aEvent.FeatureURL.Path == "OutlineRight" || + aEvent.FeatureURL.Path == "OutlineLeft" || + aEvent.FeatureURL.Path == "OutlineDown" || + aEvent.FeatureURL.Path == "OutlineUp") + + { + aBuffer.append(aEvent.IsEnabled ? OUStringLiteral("enabled") : OUStringLiteral("disabled")); + } + else if (aEvent.FeatureURL.Path == "AssignLayout" || + aEvent.FeatureURL.Path == "StatusSelectionMode" || + aEvent.FeatureURL.Path == "Signature" || + aEvent.FeatureURL.Path == "SelectionMode" || + aEvent.FeatureURL.Path == "StatusBarFunc") + { + sal_Int32 aInt32; + + if (aEvent.IsEnabled && (aEvent.State >>= aInt32)) + { + aBuffer.append(OUString::number(aInt32)); + } + } + else if (aEvent.FeatureURL.Path == "TransformPosX" || + aEvent.FeatureURL.Path == "TransformPosY" || + aEvent.FeatureURL.Path == "TransformWidth" || + aEvent.FeatureURL.Path == "TransformHeight") + { + const SfxViewShell* pViewShell = SfxViewShell::Current(); + if (aEvent.IsEnabled && pViewShell && pViewShell->isLOKMobilePhone()) + { + boost::property_tree::ptree aTree; + boost::property_tree::ptree aState; + OUString aStr(aEvent.FeatureURL.Complete); + + aTree.put("commandName", aStr.toUtf8().getStr()); + pViewFrame->GetBindings().QueryControlState(nSID, aState); + aTree.add_child("state", aState); + + aBuffer.setLength(0); + std::stringstream aStream; + boost::property_tree::write_json(aStream, aTree); + aBuffer.appendAscii(aStream.str().c_str()); + } + } + else if (aEvent.FeatureURL.Path == "StatusDocPos" || + aEvent.FeatureURL.Path == "RowColSelCount" || + aEvent.FeatureURL.Path == "StatusPageStyle" || + aEvent.FeatureURL.Path == "StateTableCell" || + aEvent.FeatureURL.Path == "StatePageNumber" || + aEvent.FeatureURL.Path == "StateWordCount" || + aEvent.FeatureURL.Path == "PageStyleName" || + aEvent.FeatureURL.Path == "PageStatus" || + aEvent.FeatureURL.Path == "LayoutStatus" || + aEvent.FeatureURL.Path == "Context") + { + OUString aString; + + if (aEvent.IsEnabled && (aEvent.State >>= aString)) + { + aBuffer.append(aString); + } + } + else if (aEvent.FeatureURL.Path == "InsertMode" || + aEvent.FeatureURL.Path == "WrapText" || + aEvent.FeatureURL.Path == "NumberFormatCurrency" || + aEvent.FeatureURL.Path == "NumberFormatPercent" || + aEvent.FeatureURL.Path == "NumberFormatDecimal" || + aEvent.FeatureURL.Path == "NumberFormatDate") + { + bool aBool; + + if (aEvent.IsEnabled && (aEvent.State >>= aBool)) + { + aBuffer.append(OUString::boolean(aBool)); + } + } + else if (aEvent.FeatureURL.Path == "ToggleMergeCells") + { + bool aBool; + + if (aEvent.IsEnabled && (aEvent.State >>= aBool)) + { + aBuffer.append(OUString::boolean(aBool)); + } + else + { + aBuffer.append("disabled"); + } + } + else if (aEvent.FeatureURL.Path == "Position") + { + css::awt::Point aPoint; + + if (aEvent.IsEnabled && (aEvent.State >>= aPoint)) + { + aBuffer.append(OUString::number(aPoint.X)).append(" / ").append(OUString::number(aPoint.Y)); + } + } + else if (aEvent.FeatureURL.Path == "Size") + { + css::awt::Size aSize; + + if (aEvent.IsEnabled && (aEvent.State >>= aSize)) + { + aBuffer.append(OUString::number(aSize.Width)).append(" x ").append(OUString::number(aSize.Height)); + } + } + else if (aEvent.FeatureURL.Path == "LanguageStatus") + { + OUString sValue; + css::uno::Sequence< OUString > aSeq; + + if (aEvent.IsEnabled) + { + if (aEvent.State >>= sValue) + { + aBuffer.append(sValue); + } + else if (aEvent.State >>= aSeq) + { + aBuffer.append(aSeq[0]); + } + } + } + else if (aEvent.FeatureURL.Path == "InsertPageHeader" || + aEvent.FeatureURL.Path == "InsertPageFooter") + { + if (aEvent.IsEnabled) + { + css::uno::Sequence< OUString > aSeq; + if (aEvent.State >>= aSeq) + { + aBuffer.append(u'{'); + for (sal_Int32 itSeq = 0; itSeq < aSeq.getLength(); itSeq++) + { + aBuffer.append("\"").append(aSeq[itSeq]); + if (itSeq != aSeq.getLength() - 1) + aBuffer.append("\":true,"); + else + aBuffer.append("\":true"); + } + aBuffer.append(u'}'); + } + } + } + else if (aEvent.FeatureURL.Path == "TableColumWidth" || + aEvent.FeatureURL.Path == "TableRowHeight") + { + sal_Int32 nValue; + if (aEvent.State >>= nValue) + { + float nScaleValue = 1000.0; + nValue *= nScaleValue; + sal_Int32 nConvertedValue = OutputDevice::LogicToLogic(nValue, MapUnit::MapTwip, MapUnit::MapInch); + aBuffer.append(OUString::number(nConvertedValue / nScaleValue)); + } + } + else + { + // Try to send JSON state version + SfxLokHelper::sendUnoStatus(SfxViewShell::Current(), pState); + + return; + } + + OUString payload = aBuffer.makeStringAndClear(); + if (const SfxViewShell* pViewShell = pViewFrame->GetViewShell()) + pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_STATE_CHANGED, payload.toUtf8().getStr()); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |