LibreOffice Module framework (master) 1
headermenucontroller.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21
22#include <services.h>
23
24#include <strings.hrc>
25#include <classes/fwkresid.hxx>
26
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>
31
32#include <vcl/svapp.hxx>
33#include <rtl/ustrbuf.hxx>
34#include <osl/mutex.hxx>
37
38// Defines
39
40using namespace com::sun::star::uno;
41using namespace com::sun::star::lang;
42using namespace com::sun::star::frame;
43using namespace com::sun::star::beans;
44using namespace com::sun::star::util;
45using namespace com::sun::star::style;
46using namespace com::sun::star::container;
47
48const sal_uInt16 ALL_MENUITEM_ID = 1;
49
50namespace framework
51{
52
53// XInterface, XTypeProvider, XServiceInfo
54
56{
57 return "com.sun.star.comp.framework.HeaderMenuController";
58}
59
60sal_Bool SAL_CALL HeaderMenuController::supportsService( const OUString& sServiceName )
61{
63}
64
65css::uno::Sequence< OUString > SAL_CALL HeaderMenuController::getSupportedServiceNames()
66{
68}
69
70HeaderMenuController::HeaderMenuController( const css::uno::Reference< css::uno::XComponentContext >& xContext, bool _bFooter ) :
71 svt::PopupMenuControllerBase( xContext )
72 ,m_bFooter(_bFooter)
73{
74}
75
77{
78}
79
80// private function
82{
83 SolarMutexGuard aSolarMutexGuard;
84
85 resetPopupMenu( rPopupMenu );
86
87 Reference< XStyleFamiliesSupplier > xStyleFamiliesSupplier( rModel, UNO_QUERY );
88 if (!xStyleFamiliesSupplier.is())
89 return;
90
91 Reference< XNameAccess > xStyleFamilies = xStyleFamiliesSupplier->getStyleFamilies();
92
93 OUString aCmd( ".uno:InsertPageHeader" );
94 OUString aHeaderFooterIsOnStr( "HeaderIsOn" );
95 if ( m_bFooter )
96 {
97 aCmd = ".uno:InsertPageFooter";
98 aHeaderFooterIsOnStr = "FooterIsOn";
99 }
100 static constexpr OUStringLiteral aIsPhysicalStr( u"IsPhysical" );
101 static constexpr OUStringLiteral aDisplayNameStr( u"DisplayName" );
102
103 try
104 {
105 Reference< XNameContainer > xNameContainer;
106 if ( xStyleFamilies->getByName("PageStyles") >>= xNameContainer )
107 {
108 Sequence< OUString > aSeqNames = xNameContainer->getElementNames();
109
110 sal_uInt16 nId = 2;
111 sal_uInt16 nCount = 0;
112 bool bAllOneState( true );
113 bool bLastCheck( true );
114 bool bFirstChecked( false );
115 bool bFirstItemInserted( false );
116 for ( sal_Int32 n = 0; n < aSeqNames.getLength(); n++ )
117 {
118 OUString aName = aSeqNames[n];
119 Reference< XPropertySet > xPropSet( xNameContainer->getByName( aName ), UNO_QUERY );
120 if ( xPropSet.is() )
121 {
122 bool bIsPhysical( false );
123 if (( xPropSet->getPropertyValue( aIsPhysicalStr ) >>= bIsPhysical ) && bIsPhysical )
124 {
125 OUString aDisplayName;
126 bool bHeaderIsOn( false );
127 xPropSet->getPropertyValue( aDisplayNameStr ) >>= aDisplayName;
128 xPropSet->getPropertyValue( aHeaderFooterIsOnStr ) >>= bHeaderIsOn;
129
130 OUStringBuffer aStrBuf( aCmd
131 + "?PageStyle:string="
132 + aDisplayName
133 + "&On:bool=" );
134 if ( !bHeaderIsOn )
135 aStrBuf.append( "true" );
136 else
137 aStrBuf.append( "false" );
138 OUString aCommand( aStrBuf.makeStringAndClear() );
139 rPopupMenu->insertItem(nId, aDisplayName, css::awt::MenuItemStyle::CHECKABLE, nCount);
140 if ( !bFirstItemInserted )
141 {
142 bFirstItemInserted = true;
143 bFirstChecked = bHeaderIsOn;
144 }
145
146 rPopupMenu->setCommand(nId, aCommand);
147 rPopupMenu->checkItem(nId, bHeaderIsOn);
148 ++nId;
149
150 // Check if all entries have the same state
151 if( bAllOneState && n && bHeaderIsOn != bLastCheck )
152 bAllOneState = false;
153 bLastCheck = bHeaderIsOn;
154 ++nCount;
155 }
156 }
157 }
158
159 if ( bAllOneState && ( nCount > 1 ))
160 {
161 // Insert special item for all command
162 rPopupMenu->insertItem(ALL_MENUITEM_ID, FwkResId(STR_MENU_HEADFOOTALL), 0, 0);
163
164 OUStringBuffer aStrBuf( aCmd + "?On:bool=" );
165
166 // Command depends on check state of first menu item entry
167 if ( !bFirstChecked )
168 aStrBuf.append( "true" );
169 else
170 aStrBuf.append( "false" );
171
172 rPopupMenu->setCommand(1, aStrBuf.makeStringAndClear());
173 rPopupMenu->insertSeparator(1);
174 }
175 }
176 }
177 catch ( const css::container::NoSuchElementException& )
178 {
179 }
180}
181
182// XEventListener
183void SAL_CALL HeaderMenuController::disposing( const EventObject& )
184{
186
187 std::unique_lock aLock( m_aMutex );
188 m_xFrame.clear();
189 m_xDispatch.clear();
190
191 if ( m_xPopupMenu.is() )
192 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(this) );
193 m_xPopupMenu.clear();
194}
195
196// XStatusListener
197void SAL_CALL HeaderMenuController::statusChanged( const FeatureStateEvent& Event )
198{
200
201 if ( Event.State >>= xModel )
202 {
203 std::unique_lock aLock( m_aMutex );
205 if ( m_xPopupMenu.is() )
206 fillPopupMenu( xModel, m_xPopupMenu );
207 }
208}
209
210// XMenuListener
212{
213 std::unique_lock aLock( m_aMutex );
214
215 throwIfDisposed(aLock);
216
218 aLock.unlock();
219
220 if ( !xModel.is() )
221 svt::PopupMenuControllerBase::updatePopupMenu();
222
223 aLock.lock();
224 if ( m_xPopupMenu.is() && m_xModel.is() )
225 fillPopupMenu( m_xModel, m_xPopupMenu );
226}
227
228}
229
230extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
232 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
233{
234 return cppu::acquire(new framework::HeaderMenuController(context));
235}
236
237/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr OUStringLiteral sServiceName
css::uno::Reference< css::lang::XComponent > m_xFrame
css::uno::Reference< css::frame::XDispatch > m_xDispatch
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
void fillPopupMenu(const css::uno::Reference< css::frame::XModel > &rModel, css::uno::Reference< css::awt::XPopupMenu > const &rPopupMenu)
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
virtual OUString SAL_CALL getImplementationName() override
virtual void SAL_CALL updatePopupMenu() override
css::uno::Reference< css::frame::XModel > m_xModel
HeaderMenuController(const css::uno::Reference< css::uno::XComponentContext > &xContext, bool _bFooter=false)
virtual sal_Bool SAL_CALL supportsService(const OUString &sServiceName) override
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &Event) override
int nCount
float u
OUString FwkResId(TranslateId aId)
Definition: fwkresid.cxx:22
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * framework_HeaderMenuController_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
const sal_uInt16 ALL_MENUITEM_ID
OUString aName
sal_Int64 n
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
constexpr OUStringLiteral SERVICENAME_POPUPMENUCONTROLLER
Definition: services.h:33
sal_Int16 nId
Reference< XModel > xModel
OUString aCommand
unsigned char sal_Bool
std::mutex m_aMutex