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