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/headermenucontroller.hxx>
24 #include <strings.hrc>
25 #include <classes/fwkresid.hxx>
27 #include <com/sun/star/awt/MenuItemStyle.hpp>
28 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
29 #include <com/sun/star/container/XNameContainer.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <vcl/svapp.hxx>
33 #include <rtl/ustrbuf.hxx>
34 #include <osl/mutex.hxx>
35 #include <cppuhelper/supportsservice.hxx>
36 #include <toolkit/awt/vclxmenu.hxx>
40 using namespace com::sun::star::uno
;
41 using namespace com::sun::star::lang
;
42 using namespace com::sun::star::frame
;
43 using namespace com::sun::star::beans
;
44 using namespace com::sun::star::style
;
45 using namespace com::sun::star::container
;
47 const sal_uInt16 ALL_MENUITEM_ID
= 1;
52 // XInterface, XTypeProvider, XServiceInfo
54 OUString SAL_CALL
HeaderMenuController::getImplementationName()
56 return u
"com.sun.star.comp.framework.HeaderMenuController"_ustr
;
59 sal_Bool SAL_CALL
HeaderMenuController::supportsService( const OUString
& sServiceName
)
61 return cppu::supportsService(this, sServiceName
);
64 css::uno::Sequence
< OUString
> SAL_CALL
HeaderMenuController::getSupportedServiceNames()
66 return { SERVICENAME_POPUPMENUCONTROLLER
};
69 HeaderMenuController::HeaderMenuController( const css::uno::Reference
< css::uno::XComponentContext
>& xContext
, bool _bFooter
) :
70 svt::PopupMenuControllerBase( xContext
)
75 HeaderMenuController::~HeaderMenuController()
80 void HeaderMenuController::fillPopupMenu( const Reference
< css::frame::XModel
>& rModel
, Reference
< css::awt::XPopupMenu
> const & rPopupMenu
)
82 SolarMutexGuard aSolarMutexGuard
;
84 resetPopupMenu( rPopupMenu
);
86 Reference
< XStyleFamiliesSupplier
> xStyleFamiliesSupplier( rModel
, UNO_QUERY
);
87 if (!xStyleFamiliesSupplier
.is())
90 Reference
< XNameAccess
> xStyleFamilies
= xStyleFamiliesSupplier
->getStyleFamilies();
92 OUString
aCmd( u
".uno:InsertPageHeader"_ustr
);
93 OUString
aHeaderFooterIsOnStr( u
"HeaderIsOn"_ustr
);
96 aCmd
= ".uno:InsertPageFooter";
97 aHeaderFooterIsOnStr
= "FooterIsOn";
99 static constexpr OUStringLiteral
aIsPhysicalStr( u
"IsPhysical" );
100 static constexpr OUStringLiteral
aDisplayNameStr( u
"DisplayName" );
104 Reference
< XNameContainer
> xNameContainer
;
105 if ( xStyleFamilies
->getByName(u
"PageStyles"_ustr
) >>= xNameContainer
)
107 Sequence
< OUString
> aSeqNames
= xNameContainer
->getElementNames();
110 sal_uInt16 nCount
= 0;
111 bool bAllOneState( true );
112 bool bLastCheck( true );
113 bool bFirstChecked( false );
114 bool bFirstItemInserted( false );
115 for ( sal_Int32 n
= 0; n
< aSeqNames
.getLength(); n
++ )
117 const OUString
& aName
= aSeqNames
[n
];
118 Reference
< XPropertySet
> xPropSet( xNameContainer
->getByName( aName
), UNO_QUERY
);
121 bool bIsPhysical( false );
122 if (( xPropSet
->getPropertyValue( aIsPhysicalStr
) >>= bIsPhysical
) && bIsPhysical
)
124 OUString aDisplayName
;
125 bool bHeaderIsOn( false );
126 xPropSet
->getPropertyValue( aDisplayNameStr
) >>= aDisplayName
;
127 xPropSet
->getPropertyValue( aHeaderFooterIsOnStr
) >>= bHeaderIsOn
;
129 OUStringBuffer
aStrBuf( aCmd
130 + "?PageStyle:string="
134 aStrBuf
.append( "true" );
136 aStrBuf
.append( "false" );
137 OUString
aCommand( aStrBuf
.makeStringAndClear() );
138 rPopupMenu
->insertItem(nId
, aDisplayName
, css::awt::MenuItemStyle::CHECKABLE
, nCount
);
139 if ( !bFirstItemInserted
)
141 bFirstItemInserted
= true;
142 bFirstChecked
= bHeaderIsOn
;
145 rPopupMenu
->setCommand(nId
, aCommand
);
146 rPopupMenu
->checkItem(nId
, bHeaderIsOn
);
149 // Check if all entries have the same state
150 if( bAllOneState
&& n
&& bHeaderIsOn
!= bLastCheck
)
151 bAllOneState
= false;
152 bLastCheck
= bHeaderIsOn
;
158 if ( bAllOneState
&& ( nCount
> 1 ))
160 // Insert special item for all command
161 rPopupMenu
->insertItem(ALL_MENUITEM_ID
, FwkResId(STR_MENU_HEADFOOTALL
), 0, 0);
163 OUStringBuffer
aStrBuf( aCmd
+ "?On:bool=" );
165 // Command depends on check state of first menu item entry
166 if ( !bFirstChecked
)
167 aStrBuf
.append( "true" );
169 aStrBuf
.append( "false" );
171 rPopupMenu
->setCommand(1, aStrBuf
.makeStringAndClear());
172 rPopupMenu
->insertSeparator(1);
176 catch ( const css::container::NoSuchElementException
& )
182 void SAL_CALL
HeaderMenuController::disposing( const EventObject
& )
184 Reference
< css::awt::XMenuListener
> xHolder(this);
186 std::unique_lock
aLock( m_aMutex
);
190 if ( m_xPopupMenu
.is() )
191 m_xPopupMenu
->removeMenuListener( Reference
< css::awt::XMenuListener
>(this) );
192 m_xPopupMenu
.clear();
196 void SAL_CALL
HeaderMenuController::statusChanged( const FeatureStateEvent
& Event
)
198 Reference
< css::frame::XModel
> xModel
;
200 if ( Event
.State
>>= xModel
)
202 std::unique_lock
aLock( m_aMutex
);
204 if ( m_xPopupMenu
.is() )
205 fillPopupMenu( xModel
, m_xPopupMenu
);
210 void SAL_CALL
HeaderMenuController::updatePopupMenu()
212 std::unique_lock
aLock( m_aMutex
);
214 throwIfDisposed(aLock
);
216 Reference
< css::frame::XModel
> xModel( m_xModel
);
220 svt::PopupMenuControllerBase::updatePopupMenu();
223 if ( m_xPopupMenu
.is() && m_xModel
.is() )
224 fillPopupMenu( m_xModel
, m_xPopupMenu
);
229 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
230 framework_HeaderMenuController_get_implementation(
231 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const& )
233 return cppu::acquire(new framework::HeaderMenuController(context
));
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */