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 #include <uifactory/menubarfactory.hxx>
22 #include <uielement/menubarwrapper.hxx>
24 #include <com/sun/star/frame/ModuleManager.hpp>
25 #include <com/sun/star/frame/XFrame.hpp>
26 #include <com/sun/star/frame/XModel.hpp>
27 #include <com/sun/star/lang/XInitialization.hpp>
28 #include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
29 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
30 #include <com/sun/star/uno/XComponentContext.hpp>
32 #include <vcl/menu.hxx>
33 #include <vcl/svapp.hxx>
34 #include <rtl/ustrbuf.hxx>
36 using namespace com::sun::star::uno
;
37 using namespace com::sun::star::lang
;
38 using namespace com::sun::star::frame
;
39 using namespace com::sun::star::beans
;
40 using namespace com::sun::star::util
;
41 using namespace ::com::sun::star::ui
;
46 MenuBarFactory::MenuBarFactory( const css::uno::Reference
< css::uno::XComponentContext
>& xContext
)
47 : m_xContext( xContext
)
51 MenuBarFactory::~MenuBarFactory()
56 Reference
< XUIElement
> SAL_CALL
MenuBarFactory::createUIElement(
57 const OUString
& ResourceURL
,
58 const Sequence
< PropertyValue
>& Args
)
60 Reference
< css::ui::XUIElement
> xMenuBar(
61 static_cast<OWeakObject
*>(new MenuBarWrapper(m_xContext
)), UNO_QUERY
);
62 CreateUIElement(ResourceURL
, Args
, "private:resource/menubar/", xMenuBar
, m_xContext
);
66 void MenuBarFactory::CreateUIElement(const OUString
& ResourceURL
67 ,const Sequence
< PropertyValue
>& Args
68 ,const OUString
& ResourceType
69 ,const Reference
< css::ui::XUIElement
>& _xMenuBar
70 ,const css::uno::Reference
< css::uno::XComponentContext
>& _rxContext
)
72 sal_Int32
nConfigPropertyIndex( Args
.getLength() );
73 sal_Int32
nURLPropertyIndex( Args
.getLength() );
74 Reference
< XUIConfigurationManager
> xCfgMgr
;
75 Reference
< XFrame
> xFrame
;
76 OUString
aResourceURL( ResourceURL
);
78 for ( sal_Int32 n
= 0; n
< Args
.getLength(); n
++ )
80 if ( Args
[n
].Name
== "ConfigurationSource" )
82 nConfigPropertyIndex
= n
;
83 Args
[n
].Value
>>= xCfgMgr
;
85 else if ( Args
[n
].Name
== "ResourceURL" )
87 nURLPropertyIndex
= n
;
88 Args
[n
].Value
>>= aResourceURL
;
90 else if ( Args
[n
].Name
== "Frame" )
91 Args
[n
].Value
>>= xFrame
;
93 if (!aResourceURL
.startsWith(ResourceType
))
94 throw IllegalArgumentException();
96 // Identify frame and determine document based ui configuration manager/module ui configuration manager
97 if ( xFrame
.is() && !xCfgMgr
.is() )
99 bool bHasSettings( false );
100 Reference
< XModel
> xModel
;
102 Reference
< XController
> xController
= xFrame
->getController();
103 if ( xController
.is() )
104 xModel
= xController
->getModel();
108 Reference
< XUIConfigurationManagerSupplier
> xUIConfigurationManagerSupplier( xModel
, UNO_QUERY
);
109 if ( xUIConfigurationManagerSupplier
.is() )
111 xCfgMgr
= xUIConfigurationManagerSupplier
->getUIConfigurationManager();
112 bHasSettings
= xCfgMgr
->hasSettings( aResourceURL
);
118 Reference
< css::frame::XModuleManager2
> xModuleManager
=
119 ModuleManager::create( _rxContext
);
120 OUString aModuleIdentifier
= xModuleManager
->identify( Reference
<XInterface
>( xFrame
, UNO_QUERY
) );
121 if ( !aModuleIdentifier
.isEmpty() )
123 Reference
< XModuleUIConfigurationManagerSupplier
> xModuleCfgSupplier
=
124 theModuleUIConfigurationManagerSupplier::get( _rxContext
);
125 xCfgMgr
= xModuleCfgSupplier
->getUIConfigurationManager( aModuleIdentifier
);
130 sal_Int32
nSeqLength( Args
.getLength() );
131 if ( Args
.getLength() == nConfigPropertyIndex
)
133 if ( Args
.getLength() == nURLPropertyIndex
)
135 if ( nConfigPropertyIndex
== nURLPropertyIndex
)
138 Sequence
< Any
> aPropSeq( nSeqLength
);
139 for ( sal_Int32 n
= 0; n
< aPropSeq
.getLength(); n
++ )
141 PropertyValue aPropValue
;
142 if ( n
== nURLPropertyIndex
)
144 aPropValue
.Name
= "ResourceURL";
145 aPropValue
.Value
<<= aResourceURL
;
147 else if ( n
== nConfigPropertyIndex
)
149 aPropValue
.Name
= "ConfigurationSource";
150 aPropValue
.Value
<<= xCfgMgr
;
153 aPropValue
= Args
[n
];
155 aPropSeq
[n
] <<= aPropValue
;
158 SolarMutexGuard aGuard
;
159 Reference
< XInitialization
> xInit( _xMenuBar
, UNO_QUERY
);
160 xInit
->initialize( aPropSeq
);
163 } // namespace framework
165 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
166 com_sun_star_comp_framework_MenuBarFactory_get_implementation(
167 css::uno::XComponentContext
*context
,
168 css::uno::Sequence
<css::uno::Any
> const &)
170 return cppu::acquire(new framework::MenuBarFactory(context
));
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */