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/complextoolbarcontroller.hxx"
21 :
22 : #include <com/sun/star/util/URLTransformer.hpp>
23 : #include <com/sun/star/util/XURLTransformer.hpp>
24 : #include <com/sun/star/frame/XDispatchProvider.hpp>
25 : #include <com/sun/star/beans/PropertyValue.hpp>
26 : #include <com/sun/star/lang/DisposedException.hpp>
27 : #include <com/sun/star/frame/status/ItemStatus.hpp>
28 : #include <com/sun/star/frame/status/ItemState.hpp>
29 : #include <com/sun/star/frame/status/Visibility.hpp>
30 : #include <com/sun/star/frame/XControlNotificationListener.hpp>
31 :
32 : #include <comphelper/processfactory.hxx>
33 : #include <svtools/toolboxcontroller.hxx>
34 : #include <osl/mutex.hxx>
35 : #include <vcl/svapp.hxx>
36 : #include <vcl/mnemonic.hxx>
37 : #include <vcl/toolbox.hxx>
38 : #include <vcl/settings.hxx>
39 :
40 : using namespace ::com::sun::star;
41 : using namespace ::com::sun::star::awt;
42 : using namespace ::com::sun::star::uno;
43 : using namespace ::com::sun::star::beans;
44 : using namespace ::com::sun::star::lang;
45 : using namespace ::com::sun::star::frame;
46 : using namespace ::com::sun::star::frame::status;
47 : using namespace ::com::sun::star::util;
48 :
49 : namespace framework
50 : {
51 :
52 0 : ComplexToolbarController::ComplexToolbarController(
53 : const Reference< XComponentContext >& rxContext,
54 : const Reference< XFrame >& rFrame,
55 : ToolBox* pToolbar,
56 : sal_uInt16 nID,
57 : const OUString& aCommand ) :
58 : svt::ToolboxController( rxContext, rFrame, aCommand )
59 : , m_pToolbar( pToolbar )
60 : , m_nID( nID )
61 0 : , m_bMadeInvisible( false )
62 : {
63 0 : m_xURLTransformer.set( URLTransformer::create(m_xContext) );
64 0 : }
65 :
66 0 : ComplexToolbarController::~ComplexToolbarController()
67 : {
68 0 : }
69 :
70 0 : void SAL_CALL ComplexToolbarController::dispose()
71 : throw ( RuntimeException, std::exception )
72 : {
73 0 : SolarMutexGuard aSolarMutexGuard;
74 :
75 0 : m_pToolbar->SetItemWindow( m_nID, 0 );
76 0 : svt::ToolboxController::dispose();
77 :
78 0 : m_xURLTransformer.clear();
79 0 : m_pToolbar = 0;
80 0 : m_nID = 0;
81 0 : }
82 :
83 0 : Sequence<PropertyValue> ComplexToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
84 : {
85 0 : Sequence<PropertyValue> aArgs( 1 );
86 :
87 : // Add key modifier to argument list
88 0 : aArgs[0].Name = "KeyModifier";
89 0 : aArgs[0].Value <<= KeyModifier;
90 0 : return aArgs;
91 : }
92 :
93 0 : void SAL_CALL ComplexToolbarController::execute( sal_Int16 KeyModifier )
94 : throw ( RuntimeException, std::exception )
95 : {
96 0 : Reference< XDispatch > xDispatch;
97 0 : Reference< XURLTransformer > xURLTransformer;
98 0 : OUString aCommandURL;
99 0 : ::com::sun::star::util::URL aTargetURL;
100 0 : Sequence<PropertyValue> aArgs;
101 :
102 : {
103 0 : SolarMutexGuard aSolarMutexGuard;
104 :
105 0 : if ( m_bDisposed )
106 0 : throw DisposedException();
107 :
108 0 : if ( m_bInitialized &&
109 0 : m_xFrame.is() &&
110 0 : !m_aCommandURL.isEmpty() )
111 : {
112 0 : xURLTransformer = m_xURLTransformer;
113 0 : xDispatch = getDispatchFromCommand( m_aCommandURL );
114 0 : aCommandURL = m_aCommandURL;
115 0 : aTargetURL = getInitializedURL();
116 0 : aArgs = getExecuteArgs(KeyModifier);
117 0 : }
118 : }
119 :
120 0 : if ( xDispatch.is() && !aTargetURL.Complete.isEmpty() )
121 : {
122 : // Execute dispatch asynchronously
123 0 : ExecuteInfo* pExecuteInfo = new ExecuteInfo;
124 0 : pExecuteInfo->xDispatch = xDispatch;
125 0 : pExecuteInfo->aTargetURL = aTargetURL;
126 0 : pExecuteInfo->aArgs = aArgs;
127 0 : Application::PostUserEvent( STATIC_LINK(0, ComplexToolbarController , ExecuteHdl_Impl), pExecuteInfo );
128 0 : }
129 0 : }
130 :
131 0 : void ComplexToolbarController::statusChanged( const FeatureStateEvent& Event )
132 : throw ( RuntimeException, std::exception )
133 : {
134 0 : SolarMutexGuard aSolarMutexGuard;
135 :
136 0 : if ( m_bDisposed )
137 0 : return;
138 :
139 0 : if ( m_pToolbar )
140 : {
141 0 : m_pToolbar->EnableItem( m_nID, Event.IsEnabled );
142 :
143 0 : sal_uInt16 nItemBits = m_pToolbar->GetItemBits( m_nID );
144 0 : nItemBits &= ~TIB_CHECKABLE;
145 0 : TriState eTri = TRISTATE_FALSE;
146 :
147 : bool bValue;
148 0 : OUString aStrValue;
149 0 : ItemStatus aItemState;
150 0 : Visibility aItemVisibility;
151 0 : ControlCommand aControlCommand;
152 :
153 0 : if ( Event.State >>= bValue )
154 : {
155 : // Boolean, treat it as checked/unchecked
156 0 : if ( m_bMadeInvisible )
157 0 : m_pToolbar->ShowItem( m_nID, true );
158 0 : m_pToolbar->CheckItem( m_nID, bValue );
159 0 : if ( bValue )
160 0 : eTri = TRISTATE_TRUE;
161 0 : nItemBits |= TIB_CHECKABLE;
162 : }
163 0 : else if ( Event.State >>= aStrValue )
164 : {
165 0 : OUString aText( MnemonicGenerator::EraseAllMnemonicChars( aStrValue ) );
166 0 : m_pToolbar->SetItemText( m_nID, aText );
167 0 : m_pToolbar->SetQuickHelpText( m_nID, aText );
168 :
169 0 : if ( m_bMadeInvisible )
170 0 : m_pToolbar->ShowItem( m_nID, true );
171 : }
172 0 : else if ( Event.State >>= aItemState )
173 : {
174 0 : eTri = TRISTATE_INDET;
175 0 : nItemBits |= TIB_CHECKABLE;
176 0 : if ( m_bMadeInvisible )
177 0 : m_pToolbar->ShowItem( m_nID, true );
178 : }
179 0 : else if ( Event.State >>= aItemVisibility )
180 : {
181 0 : m_pToolbar->ShowItem( m_nID, aItemVisibility.bVisible );
182 0 : m_bMadeInvisible = !aItemVisibility.bVisible;
183 : }
184 0 : else if ( Event.State >>= aControlCommand )
185 : {
186 0 : executeControlCommand( aControlCommand );
187 0 : if ( m_bMadeInvisible )
188 0 : m_pToolbar->ShowItem( m_nID, true );
189 : }
190 :
191 0 : else if ( m_bMadeInvisible )
192 0 : m_pToolbar->ShowItem( m_nID, true );
193 :
194 0 : m_pToolbar->SetItemState( m_nID, eTri );
195 0 : m_pToolbar->SetItemBits( m_nID, nItemBits );
196 0 : }
197 : }
198 :
199 0 : IMPL_STATIC_LINK_NOINSTANCE( ComplexToolbarController, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
200 : {
201 0 : const sal_uInt32 nRef = Application::ReleaseSolarMutex();
202 : try
203 : {
204 : // Asynchronous execution as this can lead to our own destruction!
205 : // Framework can recycle our current frame and the layout manager disposes all user interface
206 : // elements if a component gets detached from its frame!
207 0 : pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
208 : }
209 0 : catch ( const Exception& )
210 : {
211 : }
212 :
213 0 : Application::AcquireSolarMutex( nRef );
214 0 : delete pExecuteInfo;
215 0 : return 0;
216 : }
217 :
218 0 : IMPL_STATIC_LINK_NOINSTANCE( ComplexToolbarController, Notify_Impl, NotifyInfo*, pNotifyInfo )
219 : {
220 0 : const sal_uInt32 nRef = Application::ReleaseSolarMutex();
221 : try
222 : {
223 : // Asynchronous execution: As this can lead to our own destruction!
224 : // Framework can recycle our current frame and the layout manager disposes all user interface
225 : // elements if a component gets detached from its frame!
226 0 : frame::ControlEvent aEvent;
227 0 : aEvent.aURL = pNotifyInfo->aSourceURL;
228 0 : aEvent.Event = pNotifyInfo->aEventName;
229 0 : aEvent.aInformation = pNotifyInfo->aInfoSeq;
230 0 : pNotifyInfo->xNotifyListener->controlEvent( aEvent );
231 : }
232 0 : catch ( const Exception& )
233 : {
234 : }
235 :
236 0 : Application::AcquireSolarMutex( nRef );
237 0 : delete pNotifyInfo;
238 0 : return 0;
239 : }
240 :
241 0 : void ComplexToolbarController::addNotifyInfo(
242 : const OUString& aEventName,
243 : const uno::Reference< frame::XDispatch >& xDispatch,
244 : const uno::Sequence< beans::NamedValue >& rInfo )
245 : {
246 0 : uno::Reference< frame::XControlNotificationListener > xControlNotify( xDispatch, uno::UNO_QUERY );
247 :
248 0 : if ( xControlNotify.is() )
249 : {
250 : // Execute notification asynchronously
251 0 : NotifyInfo* pNotifyInfo = new NotifyInfo;
252 :
253 0 : pNotifyInfo->aEventName = aEventName;
254 0 : pNotifyInfo->xNotifyListener = xControlNotify;
255 0 : pNotifyInfo->aSourceURL = getInitializedURL();
256 :
257 : // Add frame as source to the information sequence
258 0 : sal_Int32 nCount = rInfo.getLength();
259 0 : uno::Sequence< beans::NamedValue > aInfoSeq( rInfo );
260 0 : aInfoSeq.realloc( nCount+1 );
261 0 : aInfoSeq[nCount].Name = "Source";
262 0 : aInfoSeq[nCount].Value = uno::makeAny( getFrameInterface() );
263 0 : pNotifyInfo->aInfoSeq = aInfoSeq;
264 :
265 0 : Application::PostUserEvent( STATIC_LINK(0, ComplexToolbarController, Notify_Impl), pNotifyInfo );
266 0 : }
267 0 : }
268 :
269 0 : sal_Int32 ComplexToolbarController::getFontSizePixel( const Window* pWindow )
270 : {
271 0 : const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
272 0 : const Font& rFont = rSettings.GetAppFont();
273 :
274 : // Calculate height of the application font used by window
275 0 : sal_Int32 nHeight = sal_Int32( rFont.GetHeight() );
276 0 : ::Size aPixelSize = pWindow->LogicToPixel( ::Size( 0, nHeight ), MAP_APPFONT );
277 0 : return aPixelSize.Height();
278 : }
279 :
280 0 : uno::Reference< frame::XDispatch > ComplexToolbarController::getDispatchFromCommand( const OUString& aCommand ) const
281 : {
282 0 : uno::Reference< frame::XDispatch > xDispatch;
283 :
284 0 : if ( m_bInitialized && m_xFrame.is() && !aCommand.isEmpty() )
285 : {
286 0 : URLToDispatchMap::const_iterator pIter = m_aListenerMap.find( aCommand );
287 0 : if ( pIter != m_aListenerMap.end() )
288 0 : xDispatch = pIter->second;
289 : }
290 :
291 0 : return xDispatch;
292 : }
293 :
294 0 : const ::com::sun::star::util::URL& ComplexToolbarController::getInitializedURL()
295 : {
296 0 : if ( m_aURL.Complete.isEmpty() )
297 : {
298 0 : m_aURL.Complete = m_aCommandURL;
299 0 : m_xURLTransformer->parseStrict( m_aURL );
300 : }
301 0 : return m_aURL;
302 : }
303 :
304 0 : void ComplexToolbarController::notifyFocusGet()
305 : {
306 : // send focus get notification
307 0 : uno::Sequence< beans::NamedValue > aInfo;
308 : addNotifyInfo( OUString( "FocusSet" ),
309 : getDispatchFromCommand( m_aCommandURL ),
310 0 : aInfo );
311 0 : }
312 :
313 0 : void ComplexToolbarController::notifyFocusLost()
314 : {
315 : // send focus lost notification
316 0 : uno::Sequence< beans::NamedValue > aInfo;
317 : addNotifyInfo( OUString( "FocusLost" ),
318 : getDispatchFromCommand( m_aCommandURL ),
319 0 : aInfo );
320 0 : }
321 :
322 0 : void ComplexToolbarController::notifyTextChanged( const OUString& aText )
323 : {
324 : // send text changed notification
325 0 : uno::Sequence< beans::NamedValue > aInfo( 1 );
326 0 : aInfo[0].Name = "Text";
327 0 : aInfo[0].Value <<= aText;
328 : addNotifyInfo( OUString( "TextChanged" ),
329 : getDispatchFromCommand( m_aCommandURL ),
330 0 : aInfo );
331 0 : }
332 :
333 : } // namespace
334 :
335 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|