fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / framework / source / uielement / headermenucontroller.cxx
blob0d9424e513cc9d429175d9095d72aa92c4694c4c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
22 #include "services.h"
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>
41 // Defines
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;
53 namespace framework
56 DEFINE_XSERVICEINFO_MULTISERVICE_2 ( HeaderMenuController ,
57 OWeakObject ,
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 )
66 ,m_bFooter(_bFooter)
70 HeaderMenuController::~HeaderMenuController()
74 // private function
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 );
83 if ( pPopupMenu )
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" );
93 if ( m_bFooter )
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();
108 sal_uInt16 nId = 2;
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 );
118 if ( xPropSet.is() )
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=" );
132 if ( !bHeaderIsOn )
133 aStrBuf.appendAscii( "true" );
134 else
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 );
146 if ( bHeaderIsOn )
147 pVCLPopupMenu->CheckItem( nId, true );
148 ++nId;
150 // Check if all entries have the same state
151 if( bAllOneState && n && bHeaderIsOn != bLastCheck )
152 bAllOneState = false;
153 bLastCheck = bHeaderIsOn;
154 ++nCount;
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" );
170 else
171 aStrBuf.appendAscii( "false" );
173 pVCLPopupMenu->SetItemCommand( 1, aStrBuf.makeStringAndClear() );
174 pVCLPopupMenu->InsertSeparator(OString(), 1);
178 catch ( const com::sun::star::container::NoSuchElementException& )
184 // XEventListener
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 );
190 m_xFrame.clear();
191 m_xDispatch.clear();
193 if ( m_xPopupMenu.is() )
194 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY ));
195 m_xPopupMenu.clear();
198 // XStatusListener
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 );
206 m_xModel = xModel;
207 if ( m_xPopupMenu.is() )
208 fillPopupMenu( xModel, m_xPopupMenu );
212 // XMenuListener
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 );
225 throwIfDisposed();
227 Reference< com::sun::star::frame::XModel > xModel( m_xModel );
228 aLock.clear();
230 if ( !xModel.is() )
231 svt::PopupMenuControllerBase::updatePopupMenu();
233 aLock.reset();
234 if ( m_xPopupMenu.is() && m_xModel.is() )
235 fillPopupMenu( m_xModel, m_xPopupMenu );
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */