1*6d739b60SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*6d739b60SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*6d739b60SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*6d739b60SAndrew Rist * distributed with this work for additional information
6*6d739b60SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*6d739b60SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*6d739b60SAndrew Rist * "License"); you may not use this file except in compliance
9*6d739b60SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*6d739b60SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*6d739b60SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*6d739b60SAndrew Rist * software distributed under the License is distributed on an
15*6d739b60SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*6d739b60SAndrew Rist * KIND, either express or implied. See the License for the
17*6d739b60SAndrew Rist * specific language governing permissions and limitations
18*6d739b60SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*6d739b60SAndrew Rist *************************************************************/
21*6d739b60SAndrew Rist
22*6d739b60SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_framework.hxx"
26cdf0e10cSrcweir #include <uielement/headermenucontroller.hxx>
27cdf0e10cSrcweir
28cdf0e10cSrcweir //_________________________________________________________________________________________________________________
29cdf0e10cSrcweir // my own includes
30cdf0e10cSrcweir //_________________________________________________________________________________________________________________
31cdf0e10cSrcweir #include <threadhelp/resetableguard.hxx>
32cdf0e10cSrcweir #include "services.h"
33cdf0e10cSrcweir
34cdf0e10cSrcweir #ifndef __FRAMEWORK_CLASSES_RESOURCE_HRC_
35cdf0e10cSrcweir #include <classes/resource.hrc>
36cdf0e10cSrcweir #endif
37cdf0e10cSrcweir #include <classes/fwlresid.hxx>
38cdf0e10cSrcweir
39cdf0e10cSrcweir //_________________________________________________________________________________________________________________
40cdf0e10cSrcweir // interface includes
41cdf0e10cSrcweir //_________________________________________________________________________________________________________________
42cdf0e10cSrcweir #include <com/sun/star/awt/XDevice.hpp>
43cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
44cdf0e10cSrcweir #include <com/sun/star/awt/MenuItemStyle.hpp>
45cdf0e10cSrcweir #include <com/sun/star/frame/XDispatchProvider.hpp>
46cdf0e10cSrcweir #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
47cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
48cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
49cdf0e10cSrcweir
50cdf0e10cSrcweir //_________________________________________________________________________________________________________________
51cdf0e10cSrcweir // includes of other projects
52cdf0e10cSrcweir //_________________________________________________________________________________________________________________
53cdf0e10cSrcweir
54cdf0e10cSrcweir #ifndef _VCL_MENU_HXX_
55cdf0e10cSrcweir #include <vcl/menu.hxx>
56cdf0e10cSrcweir #endif
57cdf0e10cSrcweir #include <vcl/svapp.hxx>
58cdf0e10cSrcweir #include <vcl/i18nhelp.hxx>
59cdf0e10cSrcweir #include <tools/urlobj.hxx>
60cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
61cdf0e10cSrcweir //#include <tools/solar.hrc>
62cdf0e10cSrcweir #include <dispatch/uieventloghelper.hxx>
63cdf0e10cSrcweir #include <vos/mutex.hxx>
64cdf0e10cSrcweir
65cdf0e10cSrcweir //_________________________________________________________________________________________________________________
66cdf0e10cSrcweir // Defines
67cdf0e10cSrcweir //_________________________________________________________________________________________________________________
68cdf0e10cSrcweir //
69cdf0e10cSrcweir
70cdf0e10cSrcweir using namespace com::sun::star::uno;
71cdf0e10cSrcweir using namespace com::sun::star::lang;
72cdf0e10cSrcweir using namespace com::sun::star::frame;
73cdf0e10cSrcweir using namespace com::sun::star::beans;
74cdf0e10cSrcweir using namespace com::sun::star::util;
75cdf0e10cSrcweir using namespace com::sun::star::style;
76cdf0e10cSrcweir using namespace com::sun::star::container;
77cdf0e10cSrcweir
78cdf0e10cSrcweir // Copied from Writer module
79cdf0e10cSrcweir //#define RID_SW_SHELLRES (RID_SW_START + 1250 + 1)
80cdf0e10cSrcweir //#define STR_ALLPAGE_HEADFOOT 14
81cdf0e10cSrcweir
82cdf0e10cSrcweir const sal_uInt16 ALL_MENUITEM_ID = 1;
83cdf0e10cSrcweir
84cdf0e10cSrcweir namespace framework
85cdf0e10cSrcweir {
86cdf0e10cSrcweir
DEFINE_XSERVICEINFO_MULTISERVICE(HeaderMenuController,OWeakObject,SERVICENAME_POPUPMENUCONTROLLER,IMPLEMENTATIONNAME_HEADERMENUCONTROLLER)87cdf0e10cSrcweir DEFINE_XSERVICEINFO_MULTISERVICE ( HeaderMenuController ,
88cdf0e10cSrcweir OWeakObject ,
89cdf0e10cSrcweir SERVICENAME_POPUPMENUCONTROLLER ,
90cdf0e10cSrcweir IMPLEMENTATIONNAME_HEADERMENUCONTROLLER
91cdf0e10cSrcweir )
92cdf0e10cSrcweir
93cdf0e10cSrcweir DEFINE_INIT_SERVICE ( HeaderMenuController, {} )
94cdf0e10cSrcweir
95cdf0e10cSrcweir HeaderMenuController::HeaderMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager,bool _bFooter ) :
96cdf0e10cSrcweir svt::PopupMenuControllerBase( xServiceManager )
97cdf0e10cSrcweir ,m_bFooter(_bFooter)
98cdf0e10cSrcweir {
99cdf0e10cSrcweir }
100cdf0e10cSrcweir
~HeaderMenuController()101cdf0e10cSrcweir HeaderMenuController::~HeaderMenuController()
102cdf0e10cSrcweir {
103cdf0e10cSrcweir }
104cdf0e10cSrcweir
105cdf0e10cSrcweir // private function
fillPopupMenu(const Reference<::com::sun::star::frame::XModel> & rModel,Reference<css::awt::XPopupMenu> & rPopupMenu)106cdf0e10cSrcweir void HeaderMenuController::fillPopupMenu( const Reference< ::com::sun::star::frame::XModel >& rModel, Reference< css::awt::XPopupMenu >& rPopupMenu )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir VCLXPopupMenu* pPopupMenu = (VCLXPopupMenu *)VCLXMenu::GetImplementation( rPopupMenu );
109cdf0e10cSrcweir PopupMenu* pVCLPopupMenu = 0;
110cdf0e10cSrcweir
111cdf0e10cSrcweir vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
112cdf0e10cSrcweir
113cdf0e10cSrcweir resetPopupMenu( rPopupMenu );
114cdf0e10cSrcweir if ( pPopupMenu )
115cdf0e10cSrcweir pVCLPopupMenu = (PopupMenu *)pPopupMenu->GetMenu();
116cdf0e10cSrcweir
117cdf0e10cSrcweir Reference< XStyleFamiliesSupplier > xStyleFamiliesSupplier( rModel, UNO_QUERY );
118cdf0e10cSrcweir if ( pVCLPopupMenu && xStyleFamiliesSupplier.is())
119cdf0e10cSrcweir {
120cdf0e10cSrcweir Reference< XNameAccess > xStyleFamilies = xStyleFamiliesSupplier->getStyleFamilies();
121cdf0e10cSrcweir
122cdf0e10cSrcweir rtl::OUString aCmd( RTL_CONSTASCII_USTRINGPARAM( ".uno:InsertPageHeader" ));
123cdf0e10cSrcweir rtl::OUString aHeaderFooterIsOnStr(RTL_CONSTASCII_USTRINGPARAM( "HeaderIsOn" ));
124cdf0e10cSrcweir if ( m_bFooter )
125cdf0e10cSrcweir {
126cdf0e10cSrcweir aCmd = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:InsertPageFooter" ));
127cdf0e10cSrcweir aHeaderFooterIsOnStr = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "FooterIsOn" ));
128cdf0e10cSrcweir }
129cdf0e10cSrcweir const rtl::OUString aIsPhysicalStr( RTL_CONSTASCII_USTRINGPARAM( "IsPhysical" ));
130cdf0e10cSrcweir const rtl::OUString aDisplayNameStr( RTL_CONSTASCII_USTRINGPARAM( "DisplayName" ));
131cdf0e10cSrcweir
132cdf0e10cSrcweir try
133cdf0e10cSrcweir {
134cdf0e10cSrcweir Reference< XNameContainer > xNameContainer;
135cdf0e10cSrcweir if ( xStyleFamilies->getByName( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PageStyles" ))) >>= xNameContainer )
136cdf0e10cSrcweir {
137cdf0e10cSrcweir Sequence< rtl::OUString > aSeqNames = xNameContainer->getElementNames();
138cdf0e10cSrcweir
139cdf0e10cSrcweir sal_uInt16 nId = 2;
140cdf0e10cSrcweir sal_uInt16 nCount = 0;
141cdf0e10cSrcweir sal_Bool bAllOneState( sal_True );
142cdf0e10cSrcweir sal_Bool bLastCheck( sal_True );
143cdf0e10cSrcweir sal_Bool bFirstChecked( sal_False );
144cdf0e10cSrcweir sal_Bool bFirstItemInserted( sal_False );
145cdf0e10cSrcweir for ( sal_Int32 n = 0; n < aSeqNames.getLength(); n++ )
146cdf0e10cSrcweir {
147cdf0e10cSrcweir rtl::OUString aName = aSeqNames[n];
148cdf0e10cSrcweir Reference< XPropertySet > xPropSet( xNameContainer->getByName( aName ), UNO_QUERY );
149cdf0e10cSrcweir if ( xPropSet.is() )
150cdf0e10cSrcweir {
151cdf0e10cSrcweir sal_Bool bIsPhysical( sal_False );
152cdf0e10cSrcweir if (( xPropSet->getPropertyValue( aIsPhysicalStr ) >>= bIsPhysical ) && bIsPhysical )
153cdf0e10cSrcweir {
154cdf0e10cSrcweir rtl::OUString aDisplayName;
155cdf0e10cSrcweir sal_Bool bHeaderIsOn( sal_False );
156cdf0e10cSrcweir xPropSet->getPropertyValue( aDisplayNameStr ) >>= aDisplayName;
157cdf0e10cSrcweir xPropSet->getPropertyValue( aHeaderFooterIsOnStr ) >>= bHeaderIsOn;
158cdf0e10cSrcweir
159cdf0e10cSrcweir rtl::OUStringBuffer aStrBuf( aCmd );
160cdf0e10cSrcweir aStrBuf.appendAscii( "?PageStyle:string=");
161cdf0e10cSrcweir aStrBuf.append( aDisplayName );
162cdf0e10cSrcweir aStrBuf.appendAscii( "&On:bool=" );
163cdf0e10cSrcweir if ( !bHeaderIsOn )
164cdf0e10cSrcweir aStrBuf.appendAscii( "true" );
165cdf0e10cSrcweir else
166cdf0e10cSrcweir aStrBuf.appendAscii( "false" );
167cdf0e10cSrcweir rtl::OUString aCommand( aStrBuf.makeStringAndClear() );
168cdf0e10cSrcweir pVCLPopupMenu->InsertItem( nId, aDisplayName, MIB_CHECKABLE );
169cdf0e10cSrcweir if ( !bFirstItemInserted )
170cdf0e10cSrcweir {
171cdf0e10cSrcweir bFirstItemInserted = sal_True;
172cdf0e10cSrcweir bFirstChecked = bHeaderIsOn;
173cdf0e10cSrcweir }
174cdf0e10cSrcweir
175cdf0e10cSrcweir pVCLPopupMenu->SetItemCommand( nId, aCommand );
176cdf0e10cSrcweir
177cdf0e10cSrcweir if ( bHeaderIsOn )
178cdf0e10cSrcweir pVCLPopupMenu->CheckItem( nId, sal_True );
179cdf0e10cSrcweir ++nId;
180cdf0e10cSrcweir
181cdf0e10cSrcweir // Check if all entries have the same state
182cdf0e10cSrcweir if( bAllOneState && n && bHeaderIsOn != bLastCheck )
183cdf0e10cSrcweir bAllOneState = sal_False;
184cdf0e10cSrcweir bLastCheck = bHeaderIsOn;
185cdf0e10cSrcweir ++nCount;
186cdf0e10cSrcweir }
187cdf0e10cSrcweir }
188cdf0e10cSrcweir }
189cdf0e10cSrcweir
190cdf0e10cSrcweir if ( bAllOneState && ( nCount > 1 ))
191cdf0e10cSrcweir {
192cdf0e10cSrcweir // Insert special item for all command
193cdf0e10cSrcweir pVCLPopupMenu->InsertItem( ALL_MENUITEM_ID, String( FwlResId( STR_MENU_HEADFOOTALL )), 0, 0 );
194cdf0e10cSrcweir
195cdf0e10cSrcweir rtl::OUStringBuffer aStrBuf( aCmd );
196cdf0e10cSrcweir aStrBuf.appendAscii( "?On:bool=" );
197cdf0e10cSrcweir
198cdf0e10cSrcweir // Command depends on check state of first menu item entry
199cdf0e10cSrcweir if ( !bFirstChecked )
200cdf0e10cSrcweir aStrBuf.appendAscii( "true" );
201cdf0e10cSrcweir else
202cdf0e10cSrcweir aStrBuf.appendAscii( "false" );
203cdf0e10cSrcweir
204cdf0e10cSrcweir pVCLPopupMenu->SetItemCommand( 1, aStrBuf.makeStringAndClear() );
205cdf0e10cSrcweir pVCLPopupMenu->InsertSeparator( 1 );
206cdf0e10cSrcweir }
207cdf0e10cSrcweir }
208cdf0e10cSrcweir }
209cdf0e10cSrcweir catch ( com::sun::star::container::NoSuchElementException& )
210cdf0e10cSrcweir {
211cdf0e10cSrcweir }
212cdf0e10cSrcweir }
213cdf0e10cSrcweir }
214cdf0e10cSrcweir
215cdf0e10cSrcweir // XEventListener
disposing(const EventObject &)216cdf0e10cSrcweir void SAL_CALL HeaderMenuController::disposing( const EventObject& ) throw ( RuntimeException )
217cdf0e10cSrcweir {
218cdf0e10cSrcweir Reference< css::awt::XMenuListener > xHolder(( OWeakObject *)this, UNO_QUERY );
219cdf0e10cSrcweir
220cdf0e10cSrcweir osl::MutexGuard aLock( m_aMutex );
221cdf0e10cSrcweir m_xFrame.clear();
222cdf0e10cSrcweir m_xDispatch.clear();
223cdf0e10cSrcweir m_xServiceManager.clear();
224cdf0e10cSrcweir
225cdf0e10cSrcweir if ( m_xPopupMenu.is() )
226cdf0e10cSrcweir m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY ));
227cdf0e10cSrcweir m_xPopupMenu.clear();
228cdf0e10cSrcweir }
229cdf0e10cSrcweir
230cdf0e10cSrcweir // XStatusListener
statusChanged(const FeatureStateEvent & Event)231cdf0e10cSrcweir void SAL_CALL HeaderMenuController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException )
232cdf0e10cSrcweir {
233cdf0e10cSrcweir Reference< com::sun::star::frame::XModel > xModel;
234cdf0e10cSrcweir
235cdf0e10cSrcweir if ( Event.State >>= xModel )
236cdf0e10cSrcweir {
237cdf0e10cSrcweir osl::MutexGuard aLock( m_aMutex );
238cdf0e10cSrcweir m_xModel = xModel;
239cdf0e10cSrcweir if ( m_xPopupMenu.is() )
240cdf0e10cSrcweir fillPopupMenu( xModel, m_xPopupMenu );
241cdf0e10cSrcweir }
242cdf0e10cSrcweir }
243cdf0e10cSrcweir
244cdf0e10cSrcweir // XMenuListener
impl_select(const Reference<XDispatch> & _xDispatch,const::com::sun::star::util::URL & aTargetURL)245cdf0e10cSrcweir void HeaderMenuController::impl_select(const Reference< XDispatch >& _xDispatch,const ::com::sun::star::util::URL& aTargetURL)
246cdf0e10cSrcweir {
247cdf0e10cSrcweir Sequence<PropertyValue> aArgs;
248cdf0e10cSrcweir if(::comphelper::UiEventsLogger::isEnabled()) //#i88653#
249cdf0e10cSrcweir UiEventLogHelper(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(m_bFooter ? "FooterMenuController" : "HeaderMenuController"))).log(m_xServiceManager, m_xFrame, aTargetURL, aArgs);
250cdf0e10cSrcweir OSL_ENSURE(_xDispatch.is(),"HeaderMenuController::impl_select: No dispatch");
251cdf0e10cSrcweir if ( _xDispatch.is() )
252cdf0e10cSrcweir _xDispatch->dispatch( aTargetURL, aArgs );
253cdf0e10cSrcweir }
254cdf0e10cSrcweir
updatePopupMenu()255cdf0e10cSrcweir void SAL_CALL HeaderMenuController::updatePopupMenu() throw (::com::sun::star::uno::RuntimeException)
256cdf0e10cSrcweir {
257cdf0e10cSrcweir osl::ResettableMutexGuard aLock( m_aMutex );
258cdf0e10cSrcweir
259cdf0e10cSrcweir throwIfDisposed();
260cdf0e10cSrcweir
261cdf0e10cSrcweir Reference< com::sun::star::frame::XModel > xModel( m_xModel );
262cdf0e10cSrcweir aLock.clear();
263cdf0e10cSrcweir
264cdf0e10cSrcweir if ( !xModel.is() )
265cdf0e10cSrcweir svt::PopupMenuControllerBase::updatePopupMenu();
266cdf0e10cSrcweir
267cdf0e10cSrcweir aLock.reset();
268cdf0e10cSrcweir if ( m_xPopupMenu.is() && m_xModel.is() )
269cdf0e10cSrcweir fillPopupMenu( m_xModel, m_xPopupMenu );
270cdf0e10cSrcweir }
271cdf0e10cSrcweir
272cdf0e10cSrcweir }
273