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 .
20 #ifndef INCLUDED_FRAMEWORK_INC_HELPER_MISCHELPER_HXX
21 #define INCLUDED_FRAMEWORK_INC_HELPER_MISCHELPER_HXX
23 #include <com/sun/star/linguistic2/XLanguageGuessing.hpp>
24 #include <com/sun/star/container/XNameAccess.hpp>
25 #include <com/sun/star/document/XDocumentEventListener.hpp>
26 #include <com/sun/star/lang/XEventListener.hpp>
27 #include <com/sun/star/util/XChangesListener.hpp>
28 #include <com/sun/star/container/XContainerListener.hpp>
29 #include <com/sun/star/frame/XFrame.hpp>
30 #include <com/sun/star/uno/XComponentContext.hpp>
32 #include <cppuhelper/implbase1.hxx>
34 #include <i18nlangtag/lang.h>
35 #include <svl/languageoptions.hxx>
36 #include <rtl/ustring.hxx>
37 #include <fwidllapi.h>
44 // menu ids for language status bar control
47 MID_LANG_SEL_1
= 1, // need to start with 1 since xPopupMenu->execute will return 0 if the menu is cancelled
60 MID_LANG_PARA_SEPARATOR
,
77 inline bool IsScriptTypeMatchingToLanguage( SvtScriptType nScriptType
, LanguageType nLang
)
79 return bool(nScriptType
& SvtLanguageOptions::GetScriptTypeOfLanguage( nLang
));
82 inline void RetrieveTypeNameFromResourceURL( const OUString
& aResourceURL
, OUString
& aType
, OUString
& aName
)
84 const sal_Int32 RESOURCEURL_PREFIX_SIZE
= 17;
85 const char RESOURCEURL_PREFIX
[] = "private:resource/";
87 if (( aResourceURL
.startsWith( RESOURCEURL_PREFIX
) ) &&
88 ( aResourceURL
.getLength() > RESOURCEURL_PREFIX_SIZE
))
90 OUString
aTmpStr( aResourceURL
.copy( RESOURCEURL_PREFIX_SIZE
));
95 OUString sToken
= aTmpStr
.getToken( 0, '/', nToken
);
96 if ( !sToken
.isEmpty() )
100 else if ( nPart
== 1 )
111 class FWI_DLLPUBLIC LanguageGuessingHelper
113 mutable ::com::sun::star::uno::Reference
< ::com::sun::star::linguistic2::XLanguageGuessing
> m_xLanguageGuesser
;
114 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
> m_xContext
;
117 LanguageGuessingHelper(const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& _xContext
) : m_xContext(_xContext
){}
119 ::com::sun::star::uno::Reference
< ::com::sun::star::linguistic2::XLanguageGuessing
> GetGuesser() const;
122 FWI_DLLPUBLIC OUString
RetrieveLabelFromCommand( const OUString
& aCmdURL
123 ,const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& _xContext
124 ,::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
>& _xUICommandLabels
125 ,const ::com::sun::star::uno::Reference
< ::com::sun::star::frame::XFrame
>& _xFrame
126 ,OUString
& _rModuleIdentifier
128 ,const sal_Char
* _pName
);
130 FWI_DLLPUBLIC
void FillLangItems( std::set
< OUString
> &rLangItems
,
131 const ::com::sun::star::uno::Reference
< ::com::sun::star::frame::XFrame
> &rxFrame
,
132 const LanguageGuessingHelper
& rLangGuessHelper
,
133 SvtScriptType nScriptType
,
134 const OUString
& rCurLang
,
135 const OUString
& rKeyboardLang
,
136 const OUString
& rGuessedTextLang
);
138 //It's common for an object to want to create and own a Broadcaster and set
139 //itself as a Listener on its own Broadcaster member.
141 //However, calling addListener on a Broadcaster means that the Broadcaster adds
142 //a reference to the Listener leading to an ownership cycle where the Listener
143 //owns the Broadcaster which "owns" the Listener.
145 //The WeakContainerListener allows breaking this cycle and retrofitting
146 //afflicted implentations fairly easily.
148 //OriginalListener owns the Broadcaster which "owns" the WeakContainerListener
149 //which forwards the events to the OriginalListener without taking ownership of
151 class WeakContainerListener
: public ::cppu::WeakImplHelper1
<com::sun::star::container::XContainerListener
>
154 com::sun::star::uno::WeakReference
<com::sun::star::container::XContainerListener
> mxOwner
;
157 WeakContainerListener(com::sun::star::uno::Reference
<com::sun::star::container::XContainerListener
> xOwner
)
162 virtual ~WeakContainerListener()
166 // container.XContainerListener
167 virtual void SAL_CALL
elementInserted(const com::sun::star::container::ContainerEvent
& rEvent
)
168 throw(com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
170 com::sun::star::uno::Reference
<com::sun::star::container::XContainerListener
> xOwner(mxOwner
.get(),
171 com::sun::star::uno::UNO_QUERY
);
173 xOwner
->elementInserted(rEvent
);
176 virtual void SAL_CALL
elementRemoved(const com::sun::star::container::ContainerEvent
& rEvent
)
177 throw(com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
179 com::sun::star::uno::Reference
<com::sun::star::container::XContainerListener
> xOwner(mxOwner
.get(),
180 com::sun::star::uno::UNO_QUERY
);
182 xOwner
->elementRemoved(rEvent
);
185 virtual void SAL_CALL
elementReplaced(const com::sun::star::container::ContainerEvent
& rEvent
)
186 throw(com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
188 com::sun::star::uno::Reference
<com::sun::star::container::XContainerListener
> xOwner(mxOwner
.get(),
189 com::sun::star::uno::UNO_QUERY
);
191 xOwner
->elementReplaced(rEvent
);
194 // lang.XEventListener
195 virtual void SAL_CALL
disposing(const com::sun::star::lang::EventObject
& rEvent
)
196 throw(com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
198 com::sun::star::uno::Reference
<com::sun::star::container::XContainerListener
> xOwner(mxOwner
.get(),
199 com::sun::star::uno::UNO_QUERY
);
201 xOwner
->disposing(rEvent
);
206 class WeakChangesListener
: public ::cppu::WeakImplHelper1
<com::sun::star::util::XChangesListener
>
209 com::sun::star::uno::WeakReference
<com::sun::star::util::XChangesListener
> mxOwner
;
212 WeakChangesListener(com::sun::star::uno::Reference
<com::sun::star::util::XChangesListener
> xOwner
)
217 virtual ~WeakChangesListener()
221 // util.XChangesListener
222 virtual void SAL_CALL
changesOccurred(const com::sun::star::util::ChangesEvent
& rEvent
)
223 throw(com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
225 com::sun::star::uno::Reference
<com::sun::star::util::XChangesListener
> xOwner(mxOwner
.get(),
226 com::sun::star::uno::UNO_QUERY
);
228 xOwner
->changesOccurred(rEvent
);
231 // lang.XEventListener
232 virtual void SAL_CALL
disposing(const com::sun::star::lang::EventObject
& rEvent
)
233 throw(com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
235 com::sun::star::uno::Reference
<com::sun::star::util::XChangesListener
> xOwner(mxOwner
.get(),
236 com::sun::star::uno::UNO_QUERY
);
238 xOwner
->disposing(rEvent
);
243 class WeakEventListener
: public ::cppu::WeakImplHelper1
<com::sun::star::lang::XEventListener
>
246 com::sun::star::uno::WeakReference
<com::sun::star::lang::XEventListener
> mxOwner
;
249 WeakEventListener(com::sun::star::uno::Reference
<com::sun::star::lang::XEventListener
> xOwner
)
254 virtual ~WeakEventListener()
258 // lang.XEventListener
259 virtual void SAL_CALL
disposing(const com::sun::star::lang::EventObject
& rEvent
)
260 throw(com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
262 com::sun::star::uno::Reference
<com::sun::star::lang::XEventListener
> xOwner(mxOwner
.get(),
263 com::sun::star::uno::UNO_QUERY
);
265 xOwner
->disposing(rEvent
);
270 class WeakDocumentEventListener
: public ::cppu::WeakImplHelper1
<com::sun::star::document::XDocumentEventListener
>
273 com::sun::star::uno::WeakReference
<com::sun::star::document::XDocumentEventListener
> mxOwner
;
276 WeakDocumentEventListener(com::sun::star::uno::Reference
<com::sun::star::document::XDocumentEventListener
> xOwner
)
281 virtual ~WeakDocumentEventListener()
285 virtual void SAL_CALL
documentEventOccured(const com::sun::star::document::DocumentEvent
& rEvent
)
286 throw(com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
288 com::sun::star::uno::Reference
<com::sun::star::document::XDocumentEventListener
> xOwner(mxOwner
.get(),
289 com::sun::star::uno::UNO_QUERY
);
291 xOwner
->documentEventOccured(rEvent
);
295 // lang.XEventListener
296 virtual void SAL_CALL
disposing(const com::sun::star::lang::EventObject
& rEvent
)
297 throw(com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
299 com::sun::star::uno::Reference
<com::sun::star::document::XDocumentEventListener
> xOwner(mxOwner
.get(),
300 com::sun::star::uno::UNO_QUERY
);
302 xOwner
->disposing(rEvent
);
307 } // namespace framework
309 #endif // INCLUDED_FRAMEWORK_INC_HELPER_MISCHELPER_HXX
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */