Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include <uielement/headermenucontroller.hxx>
30 : :
31 : : #include <threadhelp/resetableguard.hxx>
32 : : #include "services.h"
33 : :
34 : : #include <classes/resource.hrc>
35 : : #include <classes/fwlresid.hxx>
36 : :
37 : : #include <com/sun/star/awt/XDevice.hpp>
38 : : #include <com/sun/star/beans/PropertyValue.hpp>
39 : : #include <com/sun/star/awt/MenuItemStyle.hpp>
40 : : #include <com/sun/star/frame/XDispatchProvider.hpp>
41 : : #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
42 : : #include <com/sun/star/container/XNameContainer.hpp>
43 : : #include <com/sun/star/beans/XPropertySet.hpp>
44 : :
45 : : #include <vcl/menu.hxx>
46 : : #include <vcl/svapp.hxx>
47 : : #include <vcl/i18nhelp.hxx>
48 : : #include <rtl/ustrbuf.hxx>
49 : : #include <osl/mutex.hxx>
50 : :
51 : : //_________________________________________________________________________________________________________________
52 : : // Defines
53 : : //_________________________________________________________________________________________________________________
54 : :
55 : : using namespace com::sun::star::uno;
56 : : using namespace com::sun::star::lang;
57 : : using namespace com::sun::star::frame;
58 : : using namespace com::sun::star::beans;
59 : : using namespace com::sun::star::util;
60 : : using namespace com::sun::star::style;
61 : : using namespace com::sun::star::container;
62 : :
63 : : const sal_uInt16 ALL_MENUITEM_ID = 1;
64 : :
65 : : namespace framework
66 : : {
67 : :
68 [ + - ][ + - ]: 28 : DEFINE_XSERVICEINFO_MULTISERVICE ( HeaderMenuController ,
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ # # ]
[ # # ]
69 : : OWeakObject ,
70 : : SERVICENAME_POPUPMENUCONTROLLER ,
71 : : IMPLEMENTATIONNAME_HEADERMENUCONTROLLER
72 : : )
73 : :
74 : 8 : DEFINE_INIT_SERVICE ( HeaderMenuController, {} )
75 : :
76 : 8 : HeaderMenuController::HeaderMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager,bool _bFooter ) :
77 : : svt::PopupMenuControllerBase( xServiceManager )
78 : 8 : ,m_bFooter(_bFooter)
79 : : {
80 : 8 : }
81 : :
82 : 8 : HeaderMenuController::~HeaderMenuController()
83 : : {
84 [ - + ]: 12 : }
85 : :
86 : : // private function
87 : 0 : void HeaderMenuController::fillPopupMenu( const Reference< ::com::sun::star::frame::XModel >& rModel, Reference< css::awt::XPopupMenu >& rPopupMenu )
88 : : {
89 : 0 : VCLXPopupMenu* pPopupMenu = (VCLXPopupMenu *)VCLXMenu::GetImplementation( rPopupMenu );
90 : 0 : PopupMenu* pVCLPopupMenu = 0;
91 : :
92 [ # # ]: 0 : SolarMutexGuard aSolarMutexGuard;
93 : :
94 [ # # ]: 0 : resetPopupMenu( rPopupMenu );
95 [ # # ]: 0 : if ( pPopupMenu )
96 : 0 : pVCLPopupMenu = (PopupMenu *)pPopupMenu->GetMenu();
97 : :
98 [ # # ]: 0 : Reference< XStyleFamiliesSupplier > xStyleFamiliesSupplier( rModel, UNO_QUERY );
99 [ # # ][ # # ]: 0 : if ( pVCLPopupMenu && xStyleFamiliesSupplier.is())
[ # # ]
100 : : {
101 [ # # ][ # # ]: 0 : Reference< XNameAccess > xStyleFamilies = xStyleFamiliesSupplier->getStyleFamilies();
102 : :
103 [ # # ]: 0 : rtl::OUString aCmd( RTL_CONSTASCII_USTRINGPARAM( ".uno:InsertPageHeader" ));
104 [ # # ]: 0 : rtl::OUString aHeaderFooterIsOnStr(RTL_CONSTASCII_USTRINGPARAM( "HeaderIsOn" ));
105 [ # # ]: 0 : if ( m_bFooter )
106 : : {
107 [ # # ]: 0 : aCmd = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:InsertPageFooter" ));
108 [ # # ]: 0 : aHeaderFooterIsOnStr = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "FooterIsOn" ));
109 : : }
110 [ # # ]: 0 : const rtl::OUString aIsPhysicalStr( RTL_CONSTASCII_USTRINGPARAM( "IsPhysical" ));
111 [ # # ]: 0 : const rtl::OUString aDisplayNameStr( RTL_CONSTASCII_USTRINGPARAM( "DisplayName" ));
112 : :
113 : : try
114 : : {
115 : 0 : Reference< XNameContainer > xNameContainer;
116 [ # # ][ # # ]: 0 : if ( xStyleFamilies->getByName( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PageStyles" ))) >>= xNameContainer )
[ # # ][ # # ]
[ # # ]
117 : : {
118 [ # # ][ # # ]: 0 : Sequence< rtl::OUString > aSeqNames = xNameContainer->getElementNames();
119 : :
120 : 0 : sal_uInt16 nId = 2;
121 : 0 : sal_uInt16 nCount = 0;
122 : 0 : sal_Bool bAllOneState( sal_True );
123 : 0 : sal_Bool bLastCheck( sal_True );
124 : 0 : sal_Bool bFirstChecked( sal_False );
125 : 0 : sal_Bool bFirstItemInserted( sal_False );
126 [ # # ]: 0 : for ( sal_Int32 n = 0; n < aSeqNames.getLength(); n++ )
127 : : {
128 [ # # ]: 0 : rtl::OUString aName = aSeqNames[n];
129 [ # # ][ # # ]: 0 : Reference< XPropertySet > xPropSet( xNameContainer->getByName( aName ), UNO_QUERY );
[ # # ]
130 [ # # ]: 0 : if ( xPropSet.is() )
131 : : {
132 : 0 : sal_Bool bIsPhysical( sal_False );
133 [ # # ][ # # ]: 0 : if (( xPropSet->getPropertyValue( aIsPhysicalStr ) >>= bIsPhysical ) && bIsPhysical )
[ # # ][ # # ]
[ # # ]
[ # # # # ]
134 : : {
135 : 0 : rtl::OUString aDisplayName;
136 : 0 : sal_Bool bHeaderIsOn( sal_False );
137 [ # # ][ # # ]: 0 : xPropSet->getPropertyValue( aDisplayNameStr ) >>= aDisplayName;
138 [ # # ][ # # ]: 0 : xPropSet->getPropertyValue( aHeaderFooterIsOnStr ) >>= bHeaderIsOn;
139 : :
140 [ # # ]: 0 : rtl::OUStringBuffer aStrBuf( aCmd );
141 [ # # ]: 0 : aStrBuf.appendAscii( "?PageStyle:string=");
142 [ # # ]: 0 : aStrBuf.append( aDisplayName );
143 [ # # ]: 0 : aStrBuf.appendAscii( "&On:bool=" );
144 [ # # ]: 0 : if ( !bHeaderIsOn )
145 [ # # ]: 0 : aStrBuf.appendAscii( "true" );
146 : : else
147 [ # # ]: 0 : aStrBuf.appendAscii( "false" );
148 [ # # ]: 0 : rtl::OUString aCommand( aStrBuf.makeStringAndClear() );
149 [ # # ][ # # ]: 0 : pVCLPopupMenu->InsertItem( nId, aDisplayName, MIB_CHECKABLE );
[ # # ]
150 [ # # ]: 0 : if ( !bFirstItemInserted )
151 : : {
152 : 0 : bFirstItemInserted = sal_True;
153 : 0 : bFirstChecked = bHeaderIsOn;
154 : : }
155 : :
156 [ # # ][ # # ]: 0 : pVCLPopupMenu->SetItemCommand( nId, aCommand );
[ # # ]
157 : :
158 [ # # ]: 0 : if ( bHeaderIsOn )
159 [ # # ]: 0 : pVCLPopupMenu->CheckItem( nId, sal_True );
160 : 0 : ++nId;
161 : :
162 : : // Check if all entries have the same state
163 [ # # ][ # # ]: 0 : if( bAllOneState && n && bHeaderIsOn != bLastCheck )
[ # # ]
164 : 0 : bAllOneState = sal_False;
165 : 0 : bLastCheck = bHeaderIsOn;
166 : 0 : ++nCount;
167 : : }
168 : : }
169 : 0 : }
170 : :
171 [ # # ][ # # ]: 0 : if ( bAllOneState && ( nCount > 1 ))
172 : : {
173 : : // Insert special item for all command
174 [ # # ][ # # ]: 0 : pVCLPopupMenu->InsertItem( ALL_MENUITEM_ID, String( FwlResId( STR_MENU_HEADFOOTALL )), 0, 0 );
[ # # ][ # # ]
175 : :
176 [ # # ]: 0 : rtl::OUStringBuffer aStrBuf( aCmd );
177 [ # # ]: 0 : aStrBuf.appendAscii( "?On:bool=" );
178 : :
179 : : // Command depends on check state of first menu item entry
180 [ # # ]: 0 : if ( !bFirstChecked )
181 [ # # ]: 0 : aStrBuf.appendAscii( "true" );
182 : : else
183 [ # # ]: 0 : aStrBuf.appendAscii( "false" );
184 : :
185 [ # # ][ # # ]: 0 : pVCLPopupMenu->SetItemCommand( 1, aStrBuf.makeStringAndClear() );
[ # # ][ # # ]
186 [ # # ]: 0 : pVCLPopupMenu->InsertSeparator( 1 );
187 [ # # ]: 0 : }
188 [ # # ]: 0 : }
189 : : }
190 [ # # ]: 0 : catch ( const com::sun::star::container::NoSuchElementException& )
191 : : {
192 : 0 : }
193 [ # # ]: 0 : }
194 : 0 : }
195 : :
196 : : // XEventListener
197 : 0 : void SAL_CALL HeaderMenuController::disposing( const EventObject& ) throw ( RuntimeException )
198 : : {
199 [ # # ]: 0 : Reference< css::awt::XMenuListener > xHolder(( OWeakObject *)this, UNO_QUERY );
200 : :
201 [ # # ]: 0 : osl::MutexGuard aLock( m_aMutex );
202 : 0 : m_xFrame.clear();
203 : 0 : m_xDispatch.clear();
204 : 0 : m_xServiceManager.clear();
205 : :
206 [ # # ]: 0 : if ( m_xPopupMenu.is() )
207 [ # # ][ # # ]: 0 : m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY ));
[ # # ]
208 [ # # ]: 0 : m_xPopupMenu.clear();
209 : 0 : }
210 : :
211 : : // XStatusListener
212 : 4 : void SAL_CALL HeaderMenuController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException )
213 : : {
214 : 4 : Reference< com::sun::star::frame::XModel > xModel;
215 : :
216 [ - + ][ + - ]: 4 : if ( Event.State >>= xModel )
217 : : {
218 [ # # ]: 0 : osl::MutexGuard aLock( m_aMutex );
219 [ # # ]: 0 : m_xModel = xModel;
220 [ # # ]: 0 : if ( m_xPopupMenu.is() )
221 [ # # ][ # # ]: 0 : fillPopupMenu( xModel, m_xPopupMenu );
222 : 4 : }
223 : 4 : }
224 : :
225 : : // XMenuListener
226 : 0 : void HeaderMenuController::impl_select(const Reference< XDispatch >& _xDispatch,const ::com::sun::star::util::URL& aTargetURL)
227 : : {
228 [ # # ]: 0 : Sequence<PropertyValue> aArgs;
229 : : OSL_ENSURE(_xDispatch.is(),"HeaderMenuController::impl_select: No dispatch");
230 [ # # ]: 0 : if ( _xDispatch.is() )
231 [ # # ][ # # ]: 0 : _xDispatch->dispatch( aTargetURL, aArgs );
[ # # ]
232 : 0 : }
233 : :
234 : 4 : void SAL_CALL HeaderMenuController::updatePopupMenu() throw (::com::sun::star::uno::RuntimeException)
235 : : {
236 [ + - ]: 4 : osl::ResettableMutexGuard aLock( m_aMutex );
237 : :
238 [ + - ]: 4 : throwIfDisposed();
239 : :
240 : 4 : Reference< com::sun::star::frame::XModel > xModel( m_xModel );
241 [ + - ]: 4 : aLock.clear();
242 : :
243 [ + - ]: 4 : if ( !xModel.is() )
244 [ + - ]: 4 : svt::PopupMenuControllerBase::updatePopupMenu();
245 : :
246 [ + - ]: 4 : aLock.reset();
247 [ - + ][ # # ]: 4 : if ( m_xPopupMenu.is() && m_xModel.is() )
[ - + ]
248 [ # # ][ + - ]: 4 : fillPopupMenu( m_xModel, m_xPopupMenu );
249 : 4 : }
250 : :
251 : : }
252 : :
253 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|