1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #ifndef INCLUDED_SFX2_LOKHELPER_HXX
11 #define INCLUDED_SFX2_LOKHELPER_HXX
13 #include <vcl/IDialogRenderable.hxx>
14 #include <vcl/ITiledRenderable.hxx>
15 #include <vcl/event.hxx>
16 #include <vcl/vclptr.hxx>
17 #include <vcl/window.hxx>
18 #include <sfx2/dllapi.h>
19 #include <sfx2/viewsh.hxx>
20 #include <tools/gen.hxx>
22 #include <rtl/strbuf.hxx>
23 #include <rtl/string.hxx>
25 #include <string_view>
26 #include <sfx2/app.hxx>
28 struct SFX2_DLLPUBLIC LokMouseEventData
33 MouseEventModifiers meModifiers
;
36 std::optional
<Point
> maLogicPosition
;
38 LokMouseEventData(int nType
, Point aPosition
, int nCount
, MouseEventModifiers eModifiers
, int nButtons
, int nModifier
)
40 , maPosition(aPosition
)
42 , meModifiers(eModifiers
)
44 , mnModifier(nModifier
)
48 #include <boost/property_tree/ptree_fwd.hpp>
50 namespace com::sun::star::ui
{ struct ContextChangeEventObject
; };
52 class SFX2_DLLPUBLIC SfxLokHelper
55 /// Gets the short cut accelerators.
56 static std::unordered_map
<OUString
, css::uno::Reference
<com::sun::star::ui::XAcceleratorConfiguration
>>& getAcceleratorConfs();
57 /// Create a new view shell from the current view frame.
58 /// This assumes a single document is ever loaded.
59 static int createView();
60 /// Create a new view shell for the given DocId, for multi-document support.
61 static int createView(int nDocId
);
62 /// Destroy a view shell from the global shell list.
63 static void destroyView(int nId
);
64 /// Set a view shell as current one.
65 static void setView(int nId
);
66 /// Set the edit mode for a document with callbacks disabled.
67 static void setEditMode(int nMode
, vcl::ITiledRenderable
* pDoc
);
68 /// Get view shell with id
69 static SfxViewShell
* getViewOfId(int nId
);
70 /// Get the currently active view.
71 static int getView(const SfxViewShell
* pViewShell
= nullptr);
72 /// Get the number of views of the current DocId.
73 static std::size_t getViewsCount(int nDocId
);
74 /// Get viewIds of views of the current DocId.
75 static bool getViewIds(int nDocId
, int* pArray
, size_t nSize
);
76 /// Set View Blocked for some uno commands
77 static void setBlockedCommandList(int nViewId
, const char* blockedCommandList
);
78 /// Get the document id for a view
79 static int getDocumentIdOfView(int nViewId
);
80 /// Get the default language that should be used for views
81 static const LanguageTag
& getDefaultLanguage();
82 /// Set language of the given view.
83 static void setViewLanguage(int nId
, const OUString
& rBcp47LanguageTag
);
84 /// Set the default language for views.
85 static void setDefaultLanguage(const OUString
& rBcp47LanguageTag
);
86 /// Enable/Disable AT support for the given view.
87 static void setAccessibilityState(int nId
, bool nEnabled
);
88 /// Get the language used by the loading view (used for all save operations).
89 static const LanguageTag
& getLoadLanguage();
90 /// Set the language used by the loading view (used for all save operations).
91 static void setLoadLanguage(const OUString
& rBcp47LanguageTag
);
92 /// Set the locale for the given view.
93 static void setViewLocale(int nId
, const OUString
& rBcp47LanguageTag
);
94 /// Get the device form factor that should be used for a new view.
95 static LOKDeviceFormFactor
getDeviceFormFactor();
96 /// Set the device form factor that should be used for a new view.
97 static void setDeviceFormFactor(std::u16string_view rDeviceFormFactor
);
99 /// Set timezone of the given view.
100 /// @isSet true to use @rTimezone, even if it's empty. Otherwise, no timezone.
101 /// @rTimezone the value to set (which could be empty).
102 static void setDefaultTimezone(bool isSet
, const OUString
& rTimezone
);
103 /// Get timezone of the given view. See @setDefaultTimezone.
104 static std::pair
<bool, OUString
> getDefaultTimezone();
105 /// Set the timezone of the given view.
106 static void setViewTimezone(int nId
, bool isSet
, const OUString
& rTimezone
);
107 /// Get the timezone of the given view.
108 static std::pair
<bool, OUString
> getViewTimezone(int nId
);
110 /// Iterate over any view shell, except pThisViewShell, passing it to the f function.
111 template<typename ViewShellType
, typename FunctionType
>
112 static void forEachOtherView(ViewShellType
* pThisViewShell
, FunctionType f
);
114 /// Invoke the LOK callback of all other views showing the same document as pThisView, with a payload of rKey-rPayload.
115 static void notifyOtherViews(const SfxViewShell
* pThisView
, int nType
, std::string_view rKey
,
116 const OString
& rPayload
);
117 /// Invoke the LOK callback of all views except pThisView, with a JSON payload created from the given property tree.
118 static void notifyOtherViews(const SfxViewShell
* pThisView
, int nType
,
119 const boost::property_tree::ptree
& rTree
);
120 /// Same as notifyOtherViews(), but works on a selected "other" view, not on all of them.
121 static void notifyOtherView(const SfxViewShell
* pThisView
, SfxViewShell
const* pOtherView
,
122 int nType
, std::string_view rKey
, const OString
& rPayload
);
123 /// Same as notifyOtherViews(), the property-tree version, but works on a selected "other" view, not on all of them.
124 static void notifyOtherView(const SfxViewShell
* pThisView
, SfxViewShell
const* pOtherView
,
125 int nType
, const boost::property_tree::ptree
& rTree
);
127 /// Emits a LOK_CALLBACK_STATE_CHANGED
128 static void sendUnoStatus(const SfxViewShell
* pShell
, const SfxPoolItem
* pItem
);
129 /// Emits a LOK_CALLBACK_WINDOW
130 static void notifyWindow(const SfxViewShell
* pThisView
,
131 vcl::LOKWindowId nWindowId
,
132 std::u16string_view rAction
,
133 const std::vector
<vcl::LOKPayloadItem
>& rPayload
= std::vector
<vcl::LOKPayloadItem
>());
134 /// Emits a LOK_CALLBACK_DOCUMENT_SIZE_CHANGED - if @bInvalidateAll - first invalidates all parts
135 static void notifyDocumentSizeChanged(SfxViewShell
const* pThisView
, const OString
& rPayload
, vcl::ITiledRenderable
* pDoc
, bool bInvalidateAll
= true);
136 /// Emits a LOK_CALLBACK_DOCUMENT_SIZE_CHANGED for all views of the same document - if @bInvalidateAll - first invalidates all parts
137 static void notifyDocumentSizeChangedAllViews(vcl::ITiledRenderable
* pDoc
, bool bInvalidateAll
= true);
138 /// Emits a LOK_CALLBACK_DOCUMENT_SIZE_CHANGED for all views of the same document with the same part
139 static void notifyPartSizeChangedAllViews(vcl::ITiledRenderable
* pDoc
, int nPart
);
140 /// Emits a LOK_CALLBACK_INVALIDATE_TILES, but tweaks it according to setOptionalFeatures() if needed.
141 static void notifyInvalidation(SfxViewShell
const* pThisView
, tools::Rectangle
const *);
142 /// Notifies all views with the given type and payload.
143 static void notifyAllViews(int nType
, const OString
& rPayload
);
145 /// Notify about the editing context change.
146 static void notifyContextChange(const css::ui::ContextChangeEventObject
& rEvent
);
148 /// Emits an LOK_CALLBACK_VIEW_RENDER_STATE
149 static void notifyViewRenderState(SfxViewShell
const* pViewShell
, vcl::ITiledRenderable
* pDoc
);
151 // Notify about the given type needing an update.
152 static void notifyUpdate(SfxViewShell
const* pViewShell
, int nType
);
153 // Notify about the given type needing a per-viewid update.
154 static void notifyUpdatePerViewId(SfxViewShell
const* pViewShell
, int nType
);
155 /// Same as notifyUpdatePerViewId(), pTargetShell will be notified, relevant viewId in pViewShell,
156 /// pSourceView->getLOKPayload() will be called to get the data.
157 static void notifyUpdatePerViewId(SfxViewShell
const* pTargetShell
, SfxViewShell
const* pViewShell
,
158 SfxViewShell
const* pSourceShell
, int nType
);
159 // Notify other views about the given type needing a per-viewid update.
160 static void notifyOtherViewsUpdatePerViewId(SfxViewShell
const* pViewShell
, int nType
);
162 static OString
makePayloadJSON(const SfxViewShell
* pThisView
, int nViewId
, std::string_view rKey
, const OString
& rPayload
);
163 /// Makes a LOK_CALLBACK_INVALIDATE_VISIBLE_CURSOR payload, but tweaks it according to setOptionalFeatures() if needed.
164 static OString
makeVisCursorInvalidation(int nViewId
, const OString
& rRectangle
,
165 bool bMispelledWord
= false, const OString
& rHyperlink
= "");
167 /// Helper for posting async key event
168 static void postKeyEventAsync(const VclPtr
<vcl::Window
> &xWindow
,
169 int nType
, int nCharCode
, int nKeyCode
, int nRepeat
= 0);
171 /// Helper for posting input event
172 static void postExtTextEventAsync(const VclPtr
<vcl::Window
> &xWindow
,
173 int nType
, const OUString
&rText
);
175 /// Helper for posting async mouse event
176 static void postMouseEventAsync(const VclPtr
<vcl::Window
> &xWindow
, LokMouseEventData
const & rLokMouseEventData
);
178 /// A special value to signify 'infinity'.
179 /// This value is chosen such that sal_Int32 will not overflow when manipulated.
180 static const tools::Long MaxTwips
= 1e9
;
182 /// Helper for diagnosing run-time problems
183 static void dumpState(rtl::OStringBuffer
&rState
);
185 /// Notify all views of a media update.
186 /// This could be a new insertion or property modifications to an existing one.
187 static void notifyMediaUpdate(boost::property_tree::ptree
& json
);
189 /// Process the mouse event in the currently active in-place component (if any).
190 /// Returns true if the event has been processed, and no further processing is necessary.
191 static bool testInPlaceComponentMouseEventHit(SfxViewShell
* pViewShell
, int nType
, int nX
,
192 int nY
, int nCount
, int nButtons
, int nModifier
,
193 double fScaleX
, double fScaleY
,
194 bool bNegativeX
= false);
196 static VclPtr
<vcl::Window
> getInPlaceDocWindow(SfxViewShell
* pViewShell
);
199 static int createView(SfxViewFrame
& rViewFrame
, ViewShellDocId docId
);
202 template<typename ViewShellType
, typename FunctionType
>
203 void SfxLokHelper::forEachOtherView(ViewShellType
* pThisViewShell
, FunctionType f
)
205 SfxViewShell
* pViewShell
= SfxViewShell::GetFirst();
208 auto pOtherViewShell
= dynamic_cast<ViewShellType
*>(pViewShell
);
209 if (pOtherViewShell
!= nullptr && pOtherViewShell
!= pThisViewShell
&& pOtherViewShell
->GetDocId() == pThisViewShell
->GetDocId())
213 pViewShell
= SfxViewShell::GetNext(*pViewShell
);
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */