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 <uielement/toolbarmodemenucontroller.hxx>
23 #include <com/sun/star/awt/MenuItemStyle.hpp>
24 #include <com/sun/star/ui/UIElementType.hpp>
25 #include <com/sun/star/frame/XModuleManager.hpp>
26 #include <com/sun/star/frame/ModuleManager.hpp>
29 #include <toolkit/awt/vclxmenu.hxx>
30 #include <officecfg/Office/Common.hxx>
31 #include <vcl/svapp.hxx>
32 #include <vcl/EnumContext.hxx>
33 #include <rtl/ustrbuf.hxx>
34 #include <comphelper/processfactory.hxx>
35 #include <comphelper/propertysequence.hxx>
36 #include <comphelper/types.hxx>
37 #include <unotools/confignode.hxx>
38 #include <cppuhelper/supportsservice.hxx>
42 using namespace ::com::sun::star
;
43 using namespace ::com::sun::star::uno
;
44 using namespace ::com::sun::star::lang
;
45 using namespace ::com::sun::star::frame
;
46 using namespace ::com::sun::star::util
;
51 // XInterface, XTypeProvider, XServiceInfo
53 OUString SAL_CALL
ToolbarModeMenuController::getImplementationName()
55 return u
"com.sun.star.comp.framework.ToolbarModeMenuController"_ustr
;
58 sal_Bool SAL_CALL
ToolbarModeMenuController::supportsService( const OUString
& sServiceName
)
60 return cppu::supportsService(this, sServiceName
);
63 css::uno::Sequence
< OUString
> SAL_CALL
ToolbarModeMenuController::getSupportedServiceNames()
65 return { SERVICENAME_POPUPMENUCONTROLLER
};
69 ToolbarModeMenuController::ToolbarModeMenuController( const css::uno::Reference
< css::uno::XComponentContext
>& xContext
) :
70 svt::PopupMenuControllerBase( xContext
),
71 m_xContext( xContext
)
75 ToolbarModeMenuController::~ToolbarModeMenuController()
79 void ToolbarModeMenuController::fillPopupMenu( Reference
< css::awt::XPopupMenu
> const & rPopupMenu
)
81 if ( officecfg::Office::Common::Misc::DisableUICustomization::get() )
84 SolarMutexGuard aSolarMutexGuard
;
85 resetPopupMenu( rPopupMenu
);
87 const Reference
<XComponentContext
>& xContext (::comphelper::getProcessComponentContext() );
88 const Reference
<frame::XModuleManager
> xModuleManager
= frame::ModuleManager::create( xContext
);
89 vcl::EnumContext::Application eApp
= vcl::EnumContext::GetApplicationEnum(xModuleManager
->identify(m_xFrame
));
91 OUStringBuffer
aPath("org.openoffice.Office.UI.ToolbarMode/Applications/");
94 case vcl::EnumContext::Application::Writer
:
95 aPath
.append("Writer");
97 case vcl::EnumContext::Application::Calc
:
100 case vcl::EnumContext::Application::Impress
:
101 aPath
.append("Impress");
103 case vcl::EnumContext::Application::Draw
:
104 aPath
.append("Draw");
106 case vcl::EnumContext::Application::Formula
:
107 aPath
.append("Formula");
109 case vcl::EnumContext::Application::Base
:
110 aPath
.append("Base");
115 aPath
.append("/Modes");
117 const utl::OConfigurationTreeRoot
aModesNode(
119 aPath
.makeStringAndClear(),
121 if ( !aModesNode
.isValid() )
124 const Sequence
<OUString
> aModeNodeNames (aModesNode
.getNodeNames());
125 const sal_Int32
nCount(aModeNodeNames
.getLength());
126 tools::Long nCountToolbar
= 0;
128 for ( sal_Int32 nReadIndex
= 0; nReadIndex
< nCount
; ++nReadIndex
)
130 const utl::OConfigurationNode
aModeNode(aModesNode
.openNode(aModeNodeNames
[nReadIndex
]));
131 if ( !aModeNode
.isValid() )
134 OUString aLabel
= comphelper::getString( aModeNode
.getNodeValue( u
"Label"_ustr
) );
135 OUString aCommandArg
= comphelper::getString( aModeNode
.getNodeValue( u
"CommandArg"_ustr
) );
136 tools::Long nPosition
= comphelper::getINT32( aModeNode
.getNodeValue( u
"MenuPosition"_ustr
) );
137 bool isExperimental
= comphelper::getBOOL( aModeNode
.getNodeValue( u
"IsExperimental"_ustr
) );
138 bool hasNotebookbar
= comphelper::getBOOL( aModeNode
.getNodeValue( u
"HasNotebookbar"_ustr
) );
140 // Allow Notebookbar only in experimental mode
141 if ( isExperimental
&& !officecfg::Office::Common::Misc::ExperimentalMode::get() )
146 m_xPopupMenu
->insertItem( nReadIndex
+1, aLabel
, css::awt::MenuItemStyle::RADIOCHECK
, nPosition
);
147 rPopupMenu
->setCommand( nReadIndex
+1, aCommandArg
);
149 rPopupMenu
->insertSeparator(nCountToolbar
);
153 void SAL_CALL
ToolbarModeMenuController::disposing( const EventObject
& )
155 Reference
< css::awt::XMenuListener
> xHolder(this);
157 std::unique_lock
aLock( m_aMutex
);
161 if ( m_xPopupMenu
.is() )
162 m_xPopupMenu
->removeMenuListener( Reference
< css::awt::XMenuListener
>(this) );
163 m_xPopupMenu
.clear();
167 void SAL_CALL
ToolbarModeMenuController::statusChanged( const FeatureStateEvent
& Event
)
169 OUString
aFeatureURL( Event
.FeatureURL
.Complete
);
171 // All other status events will be processed here
172 std::unique_lock
aLock( m_aMutex
);
173 Reference
< css::awt::XPopupMenu
> xPopupMenu( m_xPopupMenu
);
176 if ( !xPopupMenu
.is() )
179 SolarMutexGuard aGuard
;
181 bool bSetCheckmark
= false;
182 bool bCheckmark
= false;
183 for (sal_Int16 i
= 0, nCount
= xPopupMenu
->getItemCount(); i
< nCount
; ++i
)
185 sal_Int16 nId
= xPopupMenu
->getItemId(i
);
189 OUString aCmd
= xPopupMenu
->getCommand(nId
);
190 if ( aCmd
== aFeatureURL
)
192 // Enable/disable item
193 xPopupMenu
->enableItem(nId
, Event
.IsEnabled
);
196 if ( Event
.State
>>= bCheckmark
)
197 bSetCheckmark
= true;
200 xPopupMenu
->checkItem(nId
, bCheckmark
);
205 if ( Event
.State
>>= aItemText
)
206 xPopupMenu
->setItemText(nId
, aItemText
);
213 void SAL_CALL
ToolbarModeMenuController::itemSelected( const css::awt::MenuEvent
& rEvent
)
215 auto aArgs(comphelper::InitPropertySequence({{"Mode", Any(m_xPopupMenu
->getCommand(rEvent
.MenuId
))}}));
216 dispatchCommand(m_aCommandURL
, aArgs
);
219 void SAL_CALL
ToolbarModeMenuController::itemActivated( const css::awt::MenuEvent
& )
221 const Reference
<frame::XModuleManager
> xModuleManager
= frame::ModuleManager::create( m_xContext
);
222 vcl::EnumContext::Application eApp
= vcl::EnumContext::GetApplicationEnum(xModuleManager
->identify(m_xFrame
));
224 OUStringBuffer
aPath("org.openoffice.Office.UI.ToolbarMode/Applications/");
227 case vcl::EnumContext::Application::Writer
:
228 aPath
.append("Writer");
230 case vcl::EnumContext::Application::Calc
:
231 aPath
.append("Calc");
233 case vcl::EnumContext::Application::Impress
:
234 aPath
.append("Impress");
236 case vcl::EnumContext::Application::Draw
:
237 aPath
.append("Draw");
239 case vcl::EnumContext::Application::Formula
:
240 aPath
.append("Formula");
242 case vcl::EnumContext::Application::Base
:
243 aPath
.append("Base");
249 const utl::OConfigurationTreeRoot
aModesNode(
251 aPath
.makeStringAndClear(),
253 if ( !aModesNode
.isValid() )
256 OUString aMode
= comphelper::getString( aModesNode
.getNodeValue( u
"Active"_ustr
) );
258 for ( int i
= 0; i
< m_xPopupMenu
->getItemCount(); ++i
)
260 sal_Int16
nItemId(m_xPopupMenu
->getItemId(i
));
261 m_xPopupMenu
->checkItem(nItemId
, aMode
== m_xPopupMenu
->getCommand(nItemId
));
265 // XPopupMenuController
266 void SAL_CALL
ToolbarModeMenuController::setPopupMenu( const Reference
< css::awt::XPopupMenu
>& xPopupMenu
)
268 std::unique_lock
aLock( m_aMutex
);
270 throwIfDisposed(aLock
);
272 if ( m_xFrame
.is() && !m_xPopupMenu
.is() )
274 // Create popup menu on demand
275 SolarMutexGuard aSolarMutexGuard
;
277 m_xPopupMenu
= dynamic_cast<VCLXPopupMenu
*>(xPopupMenu
.get());
278 assert(bool(xPopupMenu
) == bool(m_xPopupMenu
) && "we only support VCLXPopupMenu");
279 m_xPopupMenu
->addMenuListener( Reference
< css::awt::XMenuListener
>(this) );
280 fillPopupMenu( m_xPopupMenu
);
286 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
287 framework_ToolbarModeMenuController_get_implementation(
288 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const& )
290 return cppu::acquire(new framework::ToolbarModeMenuController(context
));
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */