Avoid potential negative array index access to cached text.
[LibreOffice.git] / include / sfx2 / bindings.hxx
blob2b85b4dbd065aea4e61e87e9a718811a8b458cb1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifndef INCLUDED_SFX2_BINDINGS_HXX
20 #define INCLUDED_SFX2_BINDINGS_HXX
22 #include <sal/config.h>
24 #include <cstddef>
25 #include <memory>
26 #include <optional>
27 #include <string_view>
29 #include <o3tl/typed_flags_set.hxx>
30 #include <sfx2/dllapi.h>
31 #include <sal/types.h>
32 #include <tools/link.hxx>
33 #include <com/sun/star/uno/Reference.h>
34 #include <svl/poolitem.hxx>
35 #include <svl/SfxBroadcaster.hxx>
36 #include <sfx2/shell.hxx>
38 namespace com::sun::star::frame { class XDispatch; }
39 namespace com::sun::star::frame { class XDispatchProvider; }
40 namespace com::sun::star::frame { class XDispatchRecorder; }
41 namespace com::sun::star::frame { class XFrame; }
42 namespace com::sun::star::util { struct URL; }
44 class SfxSlot;
45 class SfxSlotServer;
46 class SfxControllerItem;
47 class SfxStateCache;
48 class SfxItemSet;
49 class SfxDispatcher;
50 class SfxBindings_Impl;
51 class Timer;
52 class SfxWorkWindow;
53 struct SfxFoundCache_Impl;
54 class SfxFoundCacheArr_Impl;
55 class SfxPoolItemHolder;
57 enum class SfxCallMode : sal_uInt16
59 SLOT = 0x00, // sync/async from Slot
60 SYNCHRON = 0x01, // synchronously in the same Stackframe
61 ASYNCHRON = 0x02, // asynchronously via AppEvent
62 RECORD = 0x04, // take into account while recording
63 API = 0x08 // API call (silent)
66 namespace o3tl {
68 template<> struct typed_flags<SfxCallMode>: is_typed_flags<SfxCallMode, 0x0F>
69 {};
73 class SFX2_DLLPUBLIC SfxBindings final : public SfxBroadcaster
75 /* [Description]
77 In each SFx application one instance of the SfxBindings-Class will
78 exists from <SfxApplication::Init()> until <SfxApplication::Exit()>.
79 This instance is automatically created and destroyed by SfxApplication.
80 However these instances will be handled by the Macro <SFX_BINDINGS>
81 or the associated <SfxViewFrame>.
83 The SfxBindings manages all of its Slot-Ids bound by the registered
84 controllers and keeps a cache of the <Slot-Server> respectively.
85 (it is what we call the combination of SfxShell instance and SfxSlot).
86 In the SfxBindings it is stored, if and in this case which controllers
87 that are dirty and which Slot-Server-Caches are dirty respectively.
88 It summarizes status queries (calls to the status methods specified
89 in the IDL) that are served by the same state methods, and handles
90 the simulation of <Pseudo-Slots>.
94 friend class SfxApplication;
95 friend class SfxShell;
96 friend class SfxBindings_Impl;
98 std::unique_ptr< SfxBindings_Impl> pImpl; // Data of the Bindings instance
99 SfxDispatcher* pDispatcher; // Dispatcher, to be used
100 sal_uInt16 nRegLevel; // Lock-Level while Reconfig
102 private:
103 SAL_DLLPRIVATE SfxPoolItemHolder Execute_Impl( sal_uInt16 nSlot, const SfxPoolItem **pArgs, sal_uInt16 nModi,
104 SfxCallMode nCall, const SfxPoolItem **pInternalArgs, bool bGlobalOnly=false);
105 SAL_DLLPRIVATE void SetSubBindings_Impl( SfxBindings* );
106 SAL_DLLPRIVATE void UpdateSlotServer_Impl(); // Update SlotServer
107 SAL_DLLPRIVATE std::optional<SfxItemSet> CreateSet_Impl(SfxStateCache& rCache, const SfxSlot* &pRealSlot,
108 const SfxSlotServer**, SfxFoundCacheArr_Impl&);
109 SAL_DLLPRIVATE std::size_t GetSlotPos( sal_uInt16 nId, std::size_t nStartSearchAt = 0 );
110 SAL_DLLPRIVATE void Update_Impl(SfxStateCache& rCache);
111 static SAL_DLLPRIVATE void UpdateControllers_Impl(
112 const SfxFoundCache_Impl& rFound,
113 const SfxPoolItem *pItem,
114 SfxItemState eItemState );
115 SAL_DLLPRIVATE SfxStateCache* GetStateCache( sal_uInt16 nId, std::size_t * pPos);
116 DECL_DLLPRIVATE_LINK( NextJob, Timer *, void );
117 SAL_DLLPRIVATE bool NextJob_Impl(Timer const * pTimer);
119 public:
120 SfxBindings();
121 virtual ~SfxBindings() override;
123 void HidePopups( bool bHide = true );
125 void SetDispatcher(SfxDispatcher *pDisp);
127 void Update( sal_uInt16 nId ); // For example, from Menu::Activate
128 void Update();
129 SAL_DLLPRIVATE void StartUpdate_Impl(bool bComplete=false);
130 void Invalidate( sal_uInt16 nId );
131 void Invalidate( const sal_uInt16* pIds );
132 void InvalidateShell( const SfxShell &rSh, bool bDeep = false );
133 void InvalidateAll( bool bWithMsg );
134 void SetState( const SfxItemSet &rSet );
135 void SetState( const SfxPoolItem &rItem );
136 void Invalidate( sal_uInt16 nId, bool bWithItem, bool bWithMsg=false);
137 bool IsInUpdate() const;
138 void SetVisibleState( sal_uInt16 nId, bool bShow );
140 SfxStateCache* GetStateCache( sal_uInt16 nId);
141 SAL_DLLPRIVATE SfxStateCache* GetAnyStateCache_Impl( sal_uInt16 nId );
143 * @param rpState the caller has to delete the pointer
145 SfxItemState QueryState( sal_uInt16 nSID, std::unique_ptr<SfxPoolItem> &rpState );
146 template<class T>
147 SfxItemState QueryState( TypedWhichId<T> nSID, std::unique_ptr<T> &rpState )
149 std::unique_ptr<SfxPoolItem> tmp;
150 auto ret = QueryState(sal_uInt16(nSID), tmp);
151 rpState.reset(static_cast<T*>(tmp.release()));
152 return ret;
155 void QueryControlState ( sal_uInt16 nSID, boost::property_tree::ptree& rState );
157 sal_uInt16 QuerySlotId( const css::util::URL& aURL );
159 SfxPoolItemHolder ExecuteSynchron( sal_uInt16 nSlot,
160 const SfxPoolItem **pArgs = nullptr);
161 bool Execute( sal_uInt16 nSlot,
162 const SfxPoolItem **pArgs = nullptr,
163 SfxCallMode nCall = SfxCallMode::SLOT);
165 SAL_DLLPRIVATE void SetDispatchProvider_Impl( const css::uno::Reference< css::frame::XDispatchProvider > & rFrame );
166 void SetActiveFrame( const css::uno::Reference< css::frame::XFrame > & rFrame );
167 css::uno::Reference< css::frame::XFrame > GetActiveFrame() const;
168 // Reconfig
169 sal_uInt16 EnterRegistrations( std::string_view pFile = {}, int nLine = 0);
170 void LeaveRegistrations( std::string_view pFile = {}, int nLine = 0 );
171 void Register( SfxControllerItem& rBinding );
172 void Release( SfxControllerItem& rBinding );
173 SfxDispatcher* GetDispatcher() const
174 { return pDispatcher; }
175 const css::uno::Reference< css::frame::XDispatchRecorder >& GetRecorder() const;
176 css::uno::Reference < css::frame::XDispatch >
177 GetDispatch( const SfxSlot*, const css::util::URL& aURL, bool bMasterCommand );
178 SAL_DLLPRIVATE void ContextChanged_Impl();
179 SAL_DLLPRIVATE void Execute_Impl( SfxRequest& rReq, const SfxSlot* pSlot, SfxShell* pShell );
180 SAL_DLLPRIVATE void DeleteControllers_Impl();
181 SAL_DLLPRIVATE SfxDispatcher* GetDispatcher_Impl() { return pDispatcher; }
182 SAL_DLLPRIVATE void ClearCache_Impl( sal_uInt16 nSlotId );
183 SAL_DLLPRIVATE void RegisterInternal_Impl( SfxControllerItem& rBinding );
184 SAL_DLLPRIVATE void Register_Impl( SfxControllerItem& rBinding, bool );
185 SAL_DLLPRIVATE SfxWorkWindow* GetWorkWindow_Impl() const;
186 SAL_DLLPRIVATE void SetWorkWindow_Impl( std::unique_ptr<SfxWorkWindow> );
187 SAL_DLLPRIVATE SfxBindings* GetSubBindings_Impl() const;
188 SAL_DLLPRIVATE void SetRecorder_Impl( css::uno::Reference< css::frame::XDispatchRecorder > const & );
189 SAL_DLLPRIVATE void InvalidateSlotsInMap_Impl();
190 SAL_DLLPRIVATE void AddSlotToInvalidateSlotsMap_Impl( sal_uInt16 nId );
191 Timer& GetTimer();
194 #ifdef DBG_UTIL
195 #define ENTERREGISTRATIONS() EnterRegistrations(__FILE__, __LINE__)
196 #define LEAVEREGISTRATIONS() LeaveRegistrations(__FILE__, __LINE__)
197 #define DENTERREGISTRATIONS() \
198 EnterRegistrations( Concat2View(OString::Concat(__FILE__) + "(" + OString::number(reinterpret_cast<sal_Int64>(this)) + ")"), __LINE__ )
199 #define DLEAVEREGISTRATIONS( ) \
200 LeaveRegistrations( Concat2View(OString::Concat(__FILE__) + "(" + OString::number(reinterpret_cast<sal_Int64>(this)) + ")"), __LINE__ )
201 #else
202 #define ENTERREGISTRATIONS() EnterRegistrations()
203 #define LEAVEREGISTRATIONS() LeaveRegistrations()
204 #define DENTERREGISTRATIONS() EnterRegistrations()
205 #define DLEAVEREGISTRATIONS() LeaveRegistrations()
206 #endif
209 #endif
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */