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/.
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 .
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>
45 // menu ids for language status bar control
48 MID_LANG_SEL_1
= 1, // need to start with 1 since xPopupMenu->execute will return 0 if the menu is cancelled
64 MID_LANG_PARA_SEPARATOR
,
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
]=='/')
95 if (nIdx
>= aResourceURL
.size())
97 aType
= o3tl::getToken(aResourceURL
, u
'/', nIdx
);
98 if (nIdx
== std::u16string_view::npos
)
100 while (nIdx
< aResourceURL
.size() && aResourceURL
[nIdx
]=='/')
102 if (nIdx
>= aResourceURL
.size())
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
;
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
140 class WeakContainerListener final
: public ::cppu::WeakImplHelper
<css::container::XContainerListener
>
143 css::uno::WeakReference
<css::container::XContainerListener
> mxOwner
;
146 WeakContainerListener(css::uno::Reference
<css::container::XContainerListener
> const & 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
);
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
);
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
);
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
);
182 xOwner
->disposing(rEvent
);
187 class WeakChangesListener final
: public ::cppu::WeakImplHelper
<css::util::XChangesListener
>
190 css::uno::WeakReference
<css::util::XChangesListener
> mxOwner
;
193 WeakChangesListener(css::uno::Reference
<css::util::XChangesListener
> const & 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
);
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
);
213 xOwner
->disposing(rEvent
);
218 class WeakDocumentEventListener final
: public ::cppu::WeakImplHelper
<css::document::XDocumentEventListener
>
221 css::uno::WeakReference
<css::document::XDocumentEventListener
> mxOwner
;
224 WeakDocumentEventListener(css::uno::Reference
<css::document::XDocumentEventListener
> const & 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
);
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
);
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: */