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 <classes/resource.hrc>
25 #include <classes/fwlresid.hxx>
27 #include <com/sun/star/awt/XDevice.hpp>
28 #include <com/sun/star/beans/PropertyValue.hpp>
29 #include <com/sun/star/awt/MenuItemStyle.hpp>
30 #include <com/sun/star/frame/XDispatchProvider.hpp>
31 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
32 #include <com/sun/star/container/XNameContainer.hpp>
33 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <vcl/menu.hxx>
36 #include <vcl/svapp.hxx>
37 #include <vcl/i18nhelp.hxx>
38 #include <rtl/ustrbuf.hxx>
39 #include <osl/mutex.hxx>
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::beans
;
47 using namespace com::sun::star::util
;
48 using namespace com::sun::star::style
;
49 using namespace com::sun::star::container
;
51 const sal_uInt16 ALL_MENUITEM_ID
= 1;
56 DEFINE_XSERVICEINFO_MULTISERVICE_2 ( HeaderMenuController
,
58 SERVICENAME_POPUPMENUCONTROLLER
,
59 IMPLEMENTATIONNAME_HEADERMENUCONTROLLER
62 DEFINE_INIT_SERVICE ( HeaderMenuController
, {} )
64 HeaderMenuController::HeaderMenuController( const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& xContext
, bool _bFooter
) :
65 svt::PopupMenuControllerBase( xContext
)
70 HeaderMenuController::~HeaderMenuController()
75 void HeaderMenuController::fillPopupMenu( const Reference
< ::com::sun::star::frame::XModel
>& rModel
, Reference
< css::awt::XPopupMenu
>& rPopupMenu
)
77 VCLXPopupMenu
* pPopupMenu
= static_cast<VCLXPopupMenu
*>(VCLXMenu::GetImplementation( rPopupMenu
));
78 PopupMenu
* pVCLPopupMenu
= 0;
80 SolarMutexGuard aSolarMutexGuard
;
82 resetPopupMenu( rPopupMenu
);
84 pVCLPopupMenu
= static_cast<PopupMenu
*>(pPopupMenu
->GetMenu());
86 Reference
< XStyleFamiliesSupplier
> xStyleFamiliesSupplier( rModel
, UNO_QUERY
);
87 if ( pVCLPopupMenu
&& xStyleFamiliesSupplier
.is())
89 Reference
< XNameAccess
> xStyleFamilies
= xStyleFamiliesSupplier
->getStyleFamilies();
91 OUString
aCmd( ".uno:InsertPageHeader" );
92 OUString
aHeaderFooterIsOnStr( "HeaderIsOn" );
95 aCmd
= ".uno:InsertPageFooter";
96 aHeaderFooterIsOnStr
= "FooterIsOn";
98 const OUString
aIsPhysicalStr( "IsPhysical" );
99 const OUString
aDisplayNameStr( "DisplayName" );
103 Reference
< XNameContainer
> xNameContainer
;
104 if ( xStyleFamilies
->getByName("PageStyles") >>= xNameContainer
)
106 Sequence
< OUString
> aSeqNames
= xNameContainer
->getElementNames();
109 sal_uInt16 nCount
= 0;
110 bool bAllOneState( true );
111 bool bLastCheck( true );
112 bool bFirstChecked( false );
113 bool bFirstItemInserted( false );
114 for ( sal_Int32 n
= 0; n
< aSeqNames
.getLength(); n
++ )
116 OUString aName
= aSeqNames
[n
];
117 Reference
< XPropertySet
> xPropSet( xNameContainer
->getByName( aName
), UNO_QUERY
);
120 bool bIsPhysical( false );
121 if (( xPropSet
->getPropertyValue( aIsPhysicalStr
) >>= bIsPhysical
) && bIsPhysical
)
123 OUString aDisplayName
;
124 bool bHeaderIsOn( false );
125 xPropSet
->getPropertyValue( aDisplayNameStr
) >>= aDisplayName
;
126 xPropSet
->getPropertyValue( aHeaderFooterIsOnStr
) >>= bHeaderIsOn
;
128 OUStringBuffer
aStrBuf( aCmd
);
129 aStrBuf
.appendAscii( "?PageStyle:string=");
130 aStrBuf
.append( aDisplayName
);
131 aStrBuf
.appendAscii( "&On:bool=" );
133 aStrBuf
.appendAscii( "true" );
135 aStrBuf
.appendAscii( "false" );
136 OUString
aCommand( aStrBuf
.makeStringAndClear() );
137 pVCLPopupMenu
->InsertItem( nId
, aDisplayName
, MenuItemBits::CHECKABLE
);
138 if ( !bFirstItemInserted
)
140 bFirstItemInserted
= true;
141 bFirstChecked
= bHeaderIsOn
;
144 pVCLPopupMenu
->SetItemCommand( nId
, aCommand
);
147 pVCLPopupMenu
->CheckItem( nId
, true );
150 // Check if all entries have the same state
151 if( bAllOneState
&& n
&& bHeaderIsOn
!= bLastCheck
)
152 bAllOneState
= false;
153 bLastCheck
= bHeaderIsOn
;
159 if ( bAllOneState
&& ( nCount
> 1 ))
161 // Insert special item for all command
162 pVCLPopupMenu
->InsertItem( ALL_MENUITEM_ID
, FwlResId(STR_MENU_HEADFOOTALL
).toString(), MenuItemBits::NONE
, OString(), 0 );
164 OUStringBuffer
aStrBuf( aCmd
);
165 aStrBuf
.appendAscii( "?On:bool=" );
167 // Command depends on check state of first menu item entry
168 if ( !bFirstChecked
)
169 aStrBuf
.appendAscii( "true" );
171 aStrBuf
.appendAscii( "false" );
173 pVCLPopupMenu
->SetItemCommand( 1, aStrBuf
.makeStringAndClear() );
174 pVCLPopupMenu
->InsertSeparator(OString(), 1);
178 catch ( const com::sun::star::container::NoSuchElementException
& )
185 void SAL_CALL
HeaderMenuController::disposing( const EventObject
& ) throw ( RuntimeException
, std::exception
)
187 Reference
< css::awt::XMenuListener
> xHolder(( OWeakObject
*)this, UNO_QUERY
);
189 osl::MutexGuard
aLock( m_aMutex
);
193 if ( m_xPopupMenu
.is() )
194 m_xPopupMenu
->removeMenuListener( Reference
< css::awt::XMenuListener
>(( OWeakObject
*)this, UNO_QUERY
));
195 m_xPopupMenu
.clear();
199 void SAL_CALL
HeaderMenuController::statusChanged( const FeatureStateEvent
& Event
) throw ( RuntimeException
, std::exception
)
201 Reference
< com::sun::star::frame::XModel
> xModel
;
203 if ( Event
.State
>>= xModel
)
205 osl::MutexGuard
aLock( m_aMutex
);
207 if ( m_xPopupMenu
.is() )
208 fillPopupMenu( xModel
, m_xPopupMenu
);
213 void HeaderMenuController::impl_select(const Reference
< XDispatch
>& _xDispatch
,const ::com::sun::star::util::URL
& aTargetURL
)
215 Sequence
<PropertyValue
> aArgs
;
216 OSL_ENSURE(_xDispatch
.is(),"HeaderMenuController::impl_select: No dispatch");
217 if ( _xDispatch
.is() )
218 _xDispatch
->dispatch( aTargetURL
, aArgs
);
221 void SAL_CALL
HeaderMenuController::updatePopupMenu() throw (::com::sun::star::uno::RuntimeException
, std::exception
)
223 osl::ResettableMutexGuard
aLock( m_aMutex
);
227 Reference
< com::sun::star::frame::XModel
> xModel( m_xModel
);
231 svt::PopupMenuControllerBase::updatePopupMenu();
234 if ( m_xPopupMenu
.is() && m_xModel
.is() )
235 fillPopupMenu( m_xModel
, m_xPopupMenu
);
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */