Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / inc / helper / mischelper.hxx
blob04284e8ff8669c832883f0d0e63e03dddb87a80c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #pragma once
22 #include <com/sun/star/linguistic2/XLanguageGuessing.hpp>
23 #include <com/sun/star/document/XDocumentEventListener.hpp>
24 #include <com/sun/star/util/XChangesListener.hpp>
25 #include <com/sun/star/container/XContainerListener.hpp>
26 #include <com/sun/star/frame/XFrame.hpp>
27 #include <com/sun/star/uno/XComponentContext.hpp>
28 #include <com/sun/star/ui/XContextChangeEventListener.hpp>
30 #include <cppuhelper/implbase.hxx>
31 #include <cppuhelper/weakref.hxx>
33 #include <i18nlangtag/lang.h>
34 #include <o3tl/string_view.hxx>
35 #include <svl/languageoptions.hxx>
36 #include <rtl/ustring.hxx>
38 #include <functional>
39 #include <set>
40 #include <utility>
42 namespace framework
45 // menu ids for language status bar control
46 enum LangMenuIDs
48 MID_LANG_SEL_1 = 1, // need to start with 1 since xPopupMenu->execute will return 0 if the menu is cancelled
49 MID_LANG_SEL_2,
50 MID_LANG_SEL_3,
51 MID_LANG_SEL_4,
52 MID_LANG_SEL_5,
53 MID_LANG_SEL_6,
54 MID_LANG_SEL_7,
55 MID_LANG_SEL_8,
56 MID_LANG_SEL_9,
57 MID_LANG_SEL_NONE,
58 MID_LANG_SEL_RESET,
59 MID_LANG_SEL_MORE,
60 MID_LANG_DEF_NONE,
61 MID_LANG_DEF_RESET,
62 MID_LANG_DEF_MORE,
64 MID_LANG_PARA_SEPARATOR,
65 MID_LANG_PARA_STRING,
67 MID_LANG_PARA_1,
68 MID_LANG_PARA_2,
69 MID_LANG_PARA_3,
70 MID_LANG_PARA_4,
71 MID_LANG_PARA_5,
72 MID_LANG_PARA_6,
73 MID_LANG_PARA_7,
74 MID_LANG_PARA_8,
75 MID_LANG_PARA_9,
76 MID_LANG_PARA_NONE,
77 MID_LANG_PARA_RESET,
78 MID_LANG_PARA_MORE,
81 inline bool IsScriptTypeMatchingToLanguage( SvtScriptType nScriptType, LanguageType nLang )
83 return bool(nScriptType & SvtLanguageOptions::GetScriptTypeOfLanguage( nLang ));
86 inline void RetrieveTypeNameFromResourceURL( std::u16string_view aResourceURL, OUString& aType, OUString& aName )
88 static constexpr std::u16string_view RESOURCEURL_PREFIX = u"private:resource/";
90 if (o3tl::starts_with(aResourceURL, RESOURCEURL_PREFIX ))
92 size_t nIdx = RESOURCEURL_PREFIX.size();
93 while (nIdx < aResourceURL.size() && aResourceURL[nIdx]=='/')
94 ++nIdx;
95 if (nIdx >= aResourceURL.size())
96 return;
97 aType = o3tl::getToken(aResourceURL, u'/', nIdx);
98 if (nIdx == std::u16string_view::npos)
99 return;
100 while (nIdx < aResourceURL.size() && aResourceURL[nIdx]=='/')
101 ++nIdx;
102 if (nIdx >= aResourceURL.size())
103 return;
104 aName = o3tl::getToken(aResourceURL, u'/', nIdx);
108 class LanguageGuessingHelper
110 mutable css::uno::Reference< css::linguistic2::XLanguageGuessing > m_xLanguageGuesser;
111 css::uno::Reference< css::uno::XComponentContext > m_xContext;
113 public:
114 LanguageGuessingHelper(css::uno::Reference< css::uno::XComponentContext > _xContext) : m_xContext(std::move(_xContext)){}
116 css::uno::Reference< css::linguistic2::XLanguageGuessing > const & GetGuesser() const;
119 void FillLangItems( std::set< OUString > &rLangItems,
120 const css::uno::Reference< css::frame::XFrame > &rxFrame,
121 const LanguageGuessingHelper & rLangGuessHelper,
122 SvtScriptType nScriptType,
123 const OUString & rCurLang,
124 const OUString & rKeyboardLang,
125 const OUString & rGuessedTextLang );
127 //It's common for an object to want to create and own a Broadcaster and set
128 //itself as a Listener on its own Broadcaster member.
130 //However, calling addListener on a Broadcaster means that the Broadcaster adds
131 //a reference to the Listener leading to an ownership cycle where the Listener
132 //owns the Broadcaster which "owns" the Listener.
134 //The WeakContainerListener allows breaking this cycle and retrofitting
135 //afflicted implementations fairly easily.
137 //OriginalListener owns the Broadcaster which "owns" the WeakContainerListener
138 //which forwards the events to the OriginalListener without taking ownership of
139 //it.
140 class WeakContainerListener final : public ::cppu::WeakImplHelper<css::container::XContainerListener>
142 private:
143 css::uno::WeakReference<css::container::XContainerListener> mxOwner;
145 public:
146 WeakContainerListener(css::uno::Reference<css::container::XContainerListener> const & xOwner)
147 : mxOwner(xOwner)
151 // container.XContainerListener
152 virtual void SAL_CALL elementInserted(const css::container::ContainerEvent& rEvent) override
154 css::uno::Reference<css::container::XContainerListener> xOwner(mxOwner.get(),
155 css::uno::UNO_QUERY);
156 if (xOwner.is())
157 xOwner->elementInserted(rEvent);
160 virtual void SAL_CALL elementRemoved(const css::container::ContainerEvent& rEvent) override
162 css::uno::Reference<css::container::XContainerListener> xOwner(mxOwner.get(),
163 css::uno::UNO_QUERY);
164 if (xOwner.is())
165 xOwner->elementRemoved(rEvent);
168 virtual void SAL_CALL elementReplaced(const css::container::ContainerEvent& rEvent) override
170 css::uno::Reference<css::container::XContainerListener> xOwner(mxOwner.get(),
171 css::uno::UNO_QUERY);
172 if (xOwner.is())
173 xOwner->elementReplaced(rEvent);
176 // lang.XEventListener
177 virtual void SAL_CALL disposing(const css::lang::EventObject& rEvent) override
179 css::uno::Reference<css::container::XContainerListener> xOwner(mxOwner.get(),
180 css::uno::UNO_QUERY);
181 if (xOwner.is())
182 xOwner->disposing(rEvent);
187 class WeakChangesListener final : public ::cppu::WeakImplHelper<css::util::XChangesListener>
189 private:
190 css::uno::WeakReference<css::util::XChangesListener> mxOwner;
192 public:
193 WeakChangesListener(css::uno::Reference<css::util::XChangesListener> const & xOwner)
194 : mxOwner(xOwner)
198 // util.XChangesListener
199 virtual void SAL_CALL changesOccurred(const css::util::ChangesEvent& rEvent) override
201 css::uno::Reference<css::util::XChangesListener> xOwner(mxOwner.get(),
202 css::uno::UNO_QUERY);
203 if (xOwner.is())
204 xOwner->changesOccurred(rEvent);
207 // lang.XEventListener
208 virtual void SAL_CALL disposing(const css::lang::EventObject& rEvent) override
210 css::uno::Reference<css::util::XChangesListener> xOwner(mxOwner.get(),
211 css::uno::UNO_QUERY);
212 if (xOwner.is())
213 xOwner->disposing(rEvent);
218 class WeakDocumentEventListener final : public ::cppu::WeakImplHelper<css::document::XDocumentEventListener>
220 private:
221 css::uno::WeakReference<css::document::XDocumentEventListener> mxOwner;
223 public:
224 WeakDocumentEventListener(css::uno::Reference<css::document::XDocumentEventListener> const & xOwner)
225 : mxOwner(xOwner)
229 virtual void SAL_CALL documentEventOccured(const css::document::DocumentEvent& rEvent) override
231 css::uno::Reference<css::document::XDocumentEventListener> xOwner(mxOwner.get(),
232 css::uno::UNO_QUERY);
233 if (xOwner.is())
234 xOwner->documentEventOccured(rEvent);
238 // lang.XEventListener
239 virtual void SAL_CALL disposing(const css::lang::EventObject& rEvent) override
241 css::uno::Reference<css::document::XDocumentEventListener> xOwner(mxOwner.get(),
242 css::uno::UNO_QUERY);
243 if (xOwner.is())
244 xOwner->disposing(rEvent);
249 css::uno::Reference<css::ui::XContextChangeEventListener>
250 GetFirstListenerWith_Impl(
251 css::uno::Reference<css::uno::XComponentContext> const & xComponentContext,
252 css::uno::Reference<css::uno::XInterface> const& xEventFocus,
253 std::function<bool (css::uno::Reference<css::ui::XContextChangeEventListener> const&)> const& rPredicate);
255 extern auto (*g_pGetMultiplexerListener)(
256 css::uno::Reference<css::uno::XComponentContext> const & xComponentContext,
257 css::uno::Reference<css::uno::XInterface> const&,
258 std::function<bool (css::uno::Reference<css::ui::XContextChangeEventListener> const&)> const&)
259 -> css::uno::Reference<css::ui::XContextChangeEventListener>;
261 } // namespace framework
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */