Line data Source code
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 :
20 : #include <uielement/headermenucontroller.hxx>
21 :
22 : #include "services.h"
23 :
24 : #include <classes/resource.hrc>
25 : #include <classes/fwlresid.hxx>
26 :
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>
34 :
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>
40 :
41 : // Defines
42 :
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;
50 :
51 : const sal_uInt16 ALL_MENUITEM_ID = 1;
52 :
53 : namespace framework
54 : {
55 :
56 23 : DEFINE_XSERVICEINFO_MULTISERVICE_2 ( HeaderMenuController ,
57 : OWeakObject ,
58 : SERVICENAME_POPUPMENUCONTROLLER ,
59 : IMPLEMENTATIONNAME_HEADERMENUCONTROLLER
60 : )
61 :
62 6 : DEFINE_INIT_SERVICE ( HeaderMenuController, {} )
63 :
64 6 : HeaderMenuController::HeaderMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& xContext, bool _bFooter ) :
65 : svt::PopupMenuControllerBase( xContext )
66 6 : ,m_bFooter(_bFooter)
67 : {
68 6 : }
69 :
70 9 : HeaderMenuController::~HeaderMenuController()
71 : {
72 9 : }
73 :
74 : // private function
75 0 : void HeaderMenuController::fillPopupMenu( const Reference< ::com::sun::star::frame::XModel >& rModel, Reference< css::awt::XPopupMenu >& rPopupMenu )
76 : {
77 0 : VCLXPopupMenu* pPopupMenu = static_cast<VCLXPopupMenu *>(VCLXMenu::GetImplementation( rPopupMenu ));
78 0 : PopupMenu* pVCLPopupMenu = 0;
79 :
80 0 : SolarMutexGuard aSolarMutexGuard;
81 :
82 0 : resetPopupMenu( rPopupMenu );
83 0 : if ( pPopupMenu )
84 0 : pVCLPopupMenu = static_cast<PopupMenu *>(pPopupMenu->GetMenu());
85 :
86 0 : Reference< XStyleFamiliesSupplier > xStyleFamiliesSupplier( rModel, UNO_QUERY );
87 0 : if ( pVCLPopupMenu && xStyleFamiliesSupplier.is())
88 : {
89 0 : Reference< XNameAccess > xStyleFamilies = xStyleFamiliesSupplier->getStyleFamilies();
90 :
91 0 : OUString aCmd( ".uno:InsertPageHeader" );
92 0 : OUString aHeaderFooterIsOnStr( "HeaderIsOn" );
93 0 : if ( m_bFooter )
94 : {
95 0 : aCmd = ".uno:InsertPageFooter";
96 0 : aHeaderFooterIsOnStr = "FooterIsOn";
97 : }
98 0 : const OUString aIsPhysicalStr( "IsPhysical" );
99 0 : const OUString aDisplayNameStr( "DisplayName" );
100 :
101 : try
102 : {
103 0 : Reference< XNameContainer > xNameContainer;
104 0 : if ( xStyleFamilies->getByName("PageStyles") >>= xNameContainer )
105 : {
106 0 : Sequence< OUString > aSeqNames = xNameContainer->getElementNames();
107 :
108 0 : sal_uInt16 nId = 2;
109 0 : sal_uInt16 nCount = 0;
110 0 : bool bAllOneState( true );
111 0 : bool bLastCheck( true );
112 0 : bool bFirstChecked( false );
113 0 : bool bFirstItemInserted( false );
114 0 : for ( sal_Int32 n = 0; n < aSeqNames.getLength(); n++ )
115 : {
116 0 : OUString aName = aSeqNames[n];
117 0 : Reference< XPropertySet > xPropSet( xNameContainer->getByName( aName ), UNO_QUERY );
118 0 : if ( xPropSet.is() )
119 : {
120 0 : bool bIsPhysical( false );
121 0 : if (( xPropSet->getPropertyValue( aIsPhysicalStr ) >>= bIsPhysical ) && bIsPhysical )
122 : {
123 0 : OUString aDisplayName;
124 0 : bool bHeaderIsOn( false );
125 0 : xPropSet->getPropertyValue( aDisplayNameStr ) >>= aDisplayName;
126 0 : xPropSet->getPropertyValue( aHeaderFooterIsOnStr ) >>= bHeaderIsOn;
127 :
128 0 : OUStringBuffer aStrBuf( aCmd );
129 0 : aStrBuf.appendAscii( "?PageStyle:string=");
130 0 : aStrBuf.append( aDisplayName );
131 0 : aStrBuf.appendAscii( "&On:bool=" );
132 0 : if ( !bHeaderIsOn )
133 0 : aStrBuf.appendAscii( "true" );
134 : else
135 0 : aStrBuf.appendAscii( "false" );
136 0 : OUString aCommand( aStrBuf.makeStringAndClear() );
137 0 : pVCLPopupMenu->InsertItem( nId, aDisplayName, MenuItemBits::CHECKABLE );
138 0 : if ( !bFirstItemInserted )
139 : {
140 0 : bFirstItemInserted = true;
141 0 : bFirstChecked = bHeaderIsOn;
142 : }
143 :
144 0 : pVCLPopupMenu->SetItemCommand( nId, aCommand );
145 :
146 0 : if ( bHeaderIsOn )
147 0 : pVCLPopupMenu->CheckItem( nId, true );
148 0 : ++nId;
149 :
150 : // Check if all entries have the same state
151 0 : if( bAllOneState && n && bHeaderIsOn != bLastCheck )
152 0 : bAllOneState = false;
153 0 : bLastCheck = bHeaderIsOn;
154 0 : ++nCount;
155 : }
156 : }
157 0 : }
158 :
159 0 : if ( bAllOneState && ( nCount > 1 ))
160 : {
161 : // Insert special item for all command
162 0 : pVCLPopupMenu->InsertItem( ALL_MENUITEM_ID, FwlResId(STR_MENU_HEADFOOTALL).toString(), MenuItemBits::NONE, OString(), 0 );
163 :
164 0 : OUStringBuffer aStrBuf( aCmd );
165 0 : aStrBuf.appendAscii( "?On:bool=" );
166 :
167 : // Command depends on check state of first menu item entry
168 0 : if ( !bFirstChecked )
169 0 : aStrBuf.appendAscii( "true" );
170 : else
171 0 : aStrBuf.appendAscii( "false" );
172 :
173 0 : pVCLPopupMenu->SetItemCommand( 1, aStrBuf.makeStringAndClear() );
174 0 : pVCLPopupMenu->InsertSeparator(OString(), 1);
175 0 : }
176 0 : }
177 : }
178 0 : catch ( const com::sun::star::container::NoSuchElementException& )
179 : {
180 0 : }
181 0 : }
182 0 : }
183 :
184 : // XEventListener
185 0 : void SAL_CALL HeaderMenuController::disposing( const EventObject& ) throw ( RuntimeException, std::exception )
186 : {
187 0 : Reference< css::awt::XMenuListener > xHolder(static_cast<OWeakObject *>(this), UNO_QUERY );
188 :
189 0 : osl::MutexGuard aLock( m_aMutex );
190 0 : m_xFrame.clear();
191 0 : m_xDispatch.clear();
192 :
193 0 : if ( m_xPopupMenu.is() )
194 0 : m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(static_cast<OWeakObject *>(this), UNO_QUERY ));
195 0 : m_xPopupMenu.clear();
196 0 : }
197 :
198 : // XStatusListener
199 2 : void SAL_CALL HeaderMenuController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException, std::exception )
200 : {
201 2 : Reference< com::sun::star::frame::XModel > xModel;
202 :
203 2 : if ( Event.State >>= xModel )
204 : {
205 0 : osl::MutexGuard aLock( m_aMutex );
206 0 : m_xModel = xModel;
207 0 : if ( m_xPopupMenu.is() )
208 0 : fillPopupMenu( xModel, m_xPopupMenu );
209 2 : }
210 2 : }
211 :
212 : // XMenuListener
213 0 : void HeaderMenuController::impl_select(const Reference< XDispatch >& _xDispatch,const ::com::sun::star::util::URL& aTargetURL)
214 : {
215 0 : Sequence<PropertyValue> aArgs;
216 : OSL_ENSURE(_xDispatch.is(),"HeaderMenuController::impl_select: No dispatch");
217 0 : if ( _xDispatch.is() )
218 0 : _xDispatch->dispatch( aTargetURL, aArgs );
219 0 : }
220 :
221 2 : void SAL_CALL HeaderMenuController::updatePopupMenu() throw (::com::sun::star::uno::RuntimeException, std::exception)
222 : {
223 2 : osl::ResettableMutexGuard aLock( m_aMutex );
224 :
225 2 : throwIfDisposed();
226 :
227 4 : Reference< com::sun::star::frame::XModel > xModel( m_xModel );
228 2 : aLock.clear();
229 :
230 2 : if ( !xModel.is() )
231 2 : svt::PopupMenuControllerBase::updatePopupMenu();
232 :
233 2 : aLock.reset();
234 2 : if ( m_xPopupMenu.is() && m_xModel.is() )
235 2 : fillPopupMenu( m_xModel, m_xPopupMenu );
236 2 : }
237 :
238 : }
239 :
240 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|