LibreOffice Module framework (master) 1
mischelper.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#pragma once
21
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>
29
32
33#include <i18nlangtag/lang.h>
34#include <o3tl/string_view.hxx>
36#include <rtl/ustring.hxx>
37
38#include <functional>
39#include <set>
40#include <utility>
41
42namespace framework
43{
44
45// menu ids for language status bar control
47{
48 MID_LANG_SEL_1 = 1, // need to start with 1 since xPopupMenu->execute will return 0 if the menu is cancelled
63
66
79};
80
82{
83 return bool(nScriptType & SvtLanguageOptions::GetScriptTypeOfLanguage( nLang ));
84}
85
86inline void RetrieveTypeNameFromResourceURL( std::u16string_view aResourceURL, OUString& aType, OUString& aName )
87{
88 static constexpr std::u16string_view RESOURCEURL_PREFIX = u"private:resource/";
89
90 if (o3tl::starts_with(aResourceURL, RESOURCEURL_PREFIX ))
91 {
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);
105 }
106}
107
109{
110 mutable css::uno::Reference< css::linguistic2::XLanguageGuessing > m_xLanguageGuesser;
111 css::uno::Reference< css::uno::XComponentContext > m_xContext;
112
113public:
114 LanguageGuessingHelper(css::uno::Reference< css::uno::XComponentContext > _xContext) : m_xContext(std::move(_xContext)){}
115
116 css::uno::Reference< css::linguistic2::XLanguageGuessing > const & GetGuesser() const;
117};
118
119void 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 );
126
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.
129
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.
133
134//The WeakContainerListener allows breaking this cycle and retrofitting
135//afflicted implementations fairly easily.
136
137//OriginalListener owns the Broadcaster which "owns" the WeakContainerListener
138//which forwards the events to the OriginalListener without taking ownership of
139//it.
140class WeakContainerListener final : public ::cppu::WeakImplHelper<css::container::XContainerListener>
141{
142 private:
143 css::uno::WeakReference<css::container::XContainerListener> mxOwner;
144
145 public:
146 WeakContainerListener(css::uno::Reference<css::container::XContainerListener> const & xOwner)
147 : mxOwner(xOwner)
148 {
149 }
150
151 // container.XContainerListener
152 virtual void SAL_CALL elementInserted(const css::container::ContainerEvent& rEvent) override
153 {
154 css::uno::Reference<css::container::XContainerListener> xOwner(mxOwner.get(),
155 css::uno::UNO_QUERY);
156 if (xOwner.is())
157 xOwner->elementInserted(rEvent);
158 }
159
160 virtual void SAL_CALL elementRemoved(const css::container::ContainerEvent& rEvent) override
161 {
162 css::uno::Reference<css::container::XContainerListener> xOwner(mxOwner.get(),
163 css::uno::UNO_QUERY);
164 if (xOwner.is())
165 xOwner->elementRemoved(rEvent);
166 }
167
168 virtual void SAL_CALL elementReplaced(const css::container::ContainerEvent& rEvent) override
169 {
170 css::uno::Reference<css::container::XContainerListener> xOwner(mxOwner.get(),
171 css::uno::UNO_QUERY);
172 if (xOwner.is())
173 xOwner->elementReplaced(rEvent);
174 }
175
176 // lang.XEventListener
177 virtual void SAL_CALL disposing(const css::lang::EventObject& rEvent) override
178 {
179 css::uno::Reference<css::container::XContainerListener> xOwner(mxOwner.get(),
180 css::uno::UNO_QUERY);
181 if (xOwner.is())
182 xOwner->disposing(rEvent);
183
184 }
185};
186
187class WeakChangesListener final : public ::cppu::WeakImplHelper<css::util::XChangesListener>
188{
189 private:
190 css::uno::WeakReference<css::util::XChangesListener> mxOwner;
191
192 public:
193 WeakChangesListener(css::uno::Reference<css::util::XChangesListener> const & xOwner)
194 : mxOwner(xOwner)
195 {
196 }
197
198 // util.XChangesListener
199 virtual void SAL_CALL changesOccurred(const css::util::ChangesEvent& rEvent) override
200 {
201 css::uno::Reference<css::util::XChangesListener> xOwner(mxOwner.get(),
202 css::uno::UNO_QUERY);
203 if (xOwner.is())
204 xOwner->changesOccurred(rEvent);
205 }
206
207 // lang.XEventListener
208 virtual void SAL_CALL disposing(const css::lang::EventObject& rEvent) override
209 {
210 css::uno::Reference<css::util::XChangesListener> xOwner(mxOwner.get(),
211 css::uno::UNO_QUERY);
212 if (xOwner.is())
213 xOwner->disposing(rEvent);
214
215 }
216};
217
218class WeakDocumentEventListener final : public ::cppu::WeakImplHelper<css::document::XDocumentEventListener>
219{
220 private:
221 css::uno::WeakReference<css::document::XDocumentEventListener> mxOwner;
222
223 public:
224 WeakDocumentEventListener(css::uno::Reference<css::document::XDocumentEventListener> const & xOwner)
225 : mxOwner(xOwner)
226 {
227 }
228
229 virtual void SAL_CALL documentEventOccured(const css::document::DocumentEvent& rEvent) override
230 {
231 css::uno::Reference<css::document::XDocumentEventListener> xOwner(mxOwner.get(),
232 css::uno::UNO_QUERY);
233 if (xOwner.is())
234 xOwner->documentEventOccured(rEvent);
235
236 }
237
238 // lang.XEventListener
239 virtual void SAL_CALL disposing(const css::lang::EventObject& rEvent) override
240 {
241 css::uno::Reference<css::document::XDocumentEventListener> xOwner(mxOwner.get(),
242 css::uno::UNO_QUERY);
243 if (xOwner.is())
244 xOwner->disposing(rEvent);
245
246 }
247};
248
249css::uno::Reference<css::ui::XContextChangeEventListener>
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);
254
255extern 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>;
260
261} // namespace framework
262
263/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: mischelper.hxx:111
css::uno::Reference< css::linguistic2::XLanguageGuessing > const & GetGuesser() const
Definition: mischelper.cxx:41
css::uno::Reference< css::linguistic2::XLanguageGuessing > m_xLanguageGuesser
Definition: mischelper.hxx:110
LanguageGuessingHelper(css::uno::Reference< css::uno::XComponentContext > _xContext)
Definition: mischelper.hxx:114
WeakChangesListener(css::uno::Reference< css::util::XChangesListener > const &xOwner)
Definition: mischelper.hxx:193
virtual void SAL_CALL disposing(const css::lang::EventObject &rEvent) override
Definition: mischelper.hxx:208
css::uno::WeakReference< css::util::XChangesListener > mxOwner
Definition: mischelper.hxx:190
virtual void SAL_CALL changesOccurred(const css::util::ChangesEvent &rEvent) override
Definition: mischelper.hxx:199
WeakContainerListener(css::uno::Reference< css::container::XContainerListener > const &xOwner)
Definition: mischelper.hxx:146
virtual void SAL_CALL elementInserted(const css::container::ContainerEvent &rEvent) override
Definition: mischelper.hxx:152
css::uno::WeakReference< css::container::XContainerListener > mxOwner
Definition: mischelper.hxx:143
virtual void SAL_CALL elementReplaced(const css::container::ContainerEvent &rEvent) override
Definition: mischelper.hxx:168
virtual void SAL_CALL disposing(const css::lang::EventObject &rEvent) override
Definition: mischelper.hxx:177
virtual void SAL_CALL elementRemoved(const css::container::ContainerEvent &rEvent) override
Definition: mischelper.hxx:160
virtual void SAL_CALL disposing(const css::lang::EventObject &rEvent) override
Definition: mischelper.hxx:239
css::uno::WeakReference< css::document::XDocumentEventListener > mxOwner
Definition: mischelper.hxx:221
WeakDocumentEventListener(css::uno::Reference< css::document::XDocumentEventListener > const &xOwner)
Definition: mischelper.hxx:224
virtual void SAL_CALL documentEventOccured(const css::document::DocumentEvent &rEvent) override
Definition: mischelper.hxx:229
float u
OUString aName
SvtScriptType
SvtScriptType GetScriptTypeOfLanguage(LanguageType nLang)
@ MID_LANG_DEF_MORE
Definition: mischelper.hxx:62
@ MID_LANG_PARA_RESET
Definition: mischelper.hxx:77
@ MID_LANG_PARA_4
Definition: mischelper.hxx:70
@ MID_LANG_PARA_MORE
Definition: mischelper.hxx:78
@ MID_LANG_PARA_SEPARATOR
Definition: mischelper.hxx:64
@ MID_LANG_SEL_RESET
Definition: mischelper.hxx:58
@ MID_LANG_SEL_7
Definition: mischelper.hxx:54
@ MID_LANG_SEL_4
Definition: mischelper.hxx:51
@ MID_LANG_DEF_RESET
Definition: mischelper.hxx:61
@ MID_LANG_SEL_5
Definition: mischelper.hxx:52
@ MID_LANG_SEL_MORE
Definition: mischelper.hxx:59
@ MID_LANG_PARA_3
Definition: mischelper.hxx:69
@ MID_LANG_PARA_NONE
Definition: mischelper.hxx:76
@ MID_LANG_PARA_7
Definition: mischelper.hxx:73
@ MID_LANG_PARA_STRING
Definition: mischelper.hxx:65
@ MID_LANG_SEL_8
Definition: mischelper.hxx:55
@ MID_LANG_PARA_1
Definition: mischelper.hxx:67
@ MID_LANG_DEF_NONE
Definition: mischelper.hxx:60
@ MID_LANG_PARA_5
Definition: mischelper.hxx:71
@ MID_LANG_SEL_9
Definition: mischelper.hxx:56
@ MID_LANG_PARA_9
Definition: mischelper.hxx:75
@ MID_LANG_PARA_8
Definition: mischelper.hxx:74
@ MID_LANG_PARA_2
Definition: mischelper.hxx:68
@ MID_LANG_SEL_1
Definition: mischelper.hxx:48
@ MID_LANG_SEL_3
Definition: mischelper.hxx:50
@ MID_LANG_PARA_6
Definition: mischelper.hxx:72
@ MID_LANG_SEL_NONE
Definition: mischelper.hxx:57
@ MID_LANG_SEL_6
Definition: mischelper.hxx:53
@ MID_LANG_SEL_2
Definition: mischelper.hxx:49
void RetrieveTypeNameFromResourceURL(std::u16string_view aResourceURL, OUString &aType, OUString &aName)
Definition: mischelper.hxx:86
bool IsScriptTypeMatchingToLanguage(SvtScriptType nScriptType, LanguageType nLang)
Definition: mischelper.hxx:81
void FillLangItems(std::set< OUString > &rLangItems, const uno::Reference< frame::XFrame > &rxFrame, const LanguageGuessingHelper &rLangGuessHelper, SvtScriptType nScriptType, const OUString &rCurLang, const OUString &rKeyboardLang, const OUString &rGuessedTextLang)
Definition: mischelper.cxx:57
uno::Reference< ui::XContextChangeEventListener > GetFirstListenerWith_Impl(css::uno::Reference< css::uno::XComponentContext > const &xComponentContext, uno::Reference< uno::XInterface > const &xEventFocus, std::function< bool(uno::Reference< ui::XContextChangeEventListener > const &)> const &rPredicate)
Definition: mischelper.cxx:142
constexpr bool starts_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
Reference< XComponentContext > _xContext