merged tag ooo/OOO330_m14
[LibreOffice.git] / framework / source / helper / titlebarupdate.cxx
blob9b6501af4c20a25e8bcb34b326c14259c74a9777
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_framework.hxx"
31 #ifndef __FRAMEWORK_HELPER_TITLEBARUPDATE_HXX_
32 #include <helper/titlebarupdate.hxx>
33 #endif
35 //_________________________________________________________________________________________________________________
36 // my own includes
37 //_________________________________________________________________________________________________________________
39 #ifndef __FRAMEWORK_PATTERN_WINDOW_HXX_
40 #include <pattern/window.hxx>
41 #endif
43 #ifndef __FRAMEWORK_THREADHELP_WRITEGUARD_HXX_
44 #include <threadhelp/writeguard.hxx>
45 #endif
47 #ifndef __FRAMEWORK_THREADHELP_READGUARD_HXX_
48 #include <threadhelp/readguard.hxx>
49 #endif
51 #ifndef __FRAMEWORK_MACROS_GENERIC_HXX_
52 #include <macros/generic.hxx>
53 #endif
55 #ifndef __FRAMEWORK_SERVICES_H_
56 #include <services.h>
57 #endif
59 #ifndef __FRAMEWORK_PROPETIES_H_
60 #include <properties.h>
61 #endif
63 //_________________________________________________________________________________________________________________
64 // interface includes
65 //_________________________________________________________________________________________________________________
67 #ifndef _COM_SUN_STAR_AWT_XWINDOW_HPP_
68 #include <com/sun/star/awt/XWindow.hpp>
69 #endif
71 #ifndef _COM_SUN_STAR_LANG_XSERVICXEINFO_HPP_
72 #include <com/sun/star/lang/XServiceInfo.hpp>
73 #endif
75 #ifndef _COM_SUN_STAR_LANG_ILLEGALARGUMENTEXCEPTION_HPP_
76 #include <com/sun/star/lang/IllegalArgumentException.hpp>
77 #endif
79 #ifndef _COM_SUN_STAR_FRAME_XMODULEMANAGER_HPP_
80 #include <com/sun/star/frame/XModuleManager.hpp>
81 #endif
83 #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_
84 #include <com/sun/star/container/XNameAccess.hpp>
85 #endif
87 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
88 #include <com/sun/star/beans/XPropertySet.hpp>
89 #endif
91 #ifndef _COM_SUN_STAR_BEANS_XMATERIALHOLDER_HPP_
92 #include <com/sun/star/beans/XMaterialHolder.hpp>
93 #endif
95 #ifndef _COM_SUN_STAR_FRAME_XTITLECHANGEBROADCASTER_HPP_
96 #include <com/sun/star/frame/XTitleChangeBroadcaster.hpp>
97 #endif
99 #ifndef _COM_SUN_STAR_BEANS_NAMEDVALUE_HPP_
100 #include <com/sun/star/beans/NamedValue.hpp>
101 #endif
103 //_________________________________________________________________________________________________________________
104 // other includes
105 //_________________________________________________________________________________________________________________
107 #ifndef _COMPHELPER_SEQUENCEASHASHMAP_HXX
108 #include <comphelper/sequenceashashmap.hxx>
109 #endif
111 #ifndef _UTL_CONFIGMGR_HXX
112 #include <unotools/configmgr.hxx>
113 #endif
115 #ifndef _UTL_BOOTSTRAP_HXX
116 #include <unotools/bootstrap.hxx>
117 #endif
119 #ifndef _SV_WINDOW_HXX
120 #include <vcl/window.hxx>
121 #endif
123 #ifndef _SV_SYSWIN_HXX
124 #include <vcl/syswin.hxx>
125 #endif
127 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
128 #include <toolkit/unohlp.hxx>
129 #endif
131 #ifndef _SV_SVAPP_HXX
132 #include <vcl/svapp.hxx>
133 #endif
135 #ifndef _SV_WRKWIN_HXX
136 #include <vcl/wrkwin.hxx>
137 #endif
139 //_________________________________________________________________________________________________________________
140 // namespace
142 namespace framework{
144 //_________________________________________________________________________________________________________________
145 // const
147 static const ::sal_Int32 INVALID_ICON_ID = -1;
148 static const ::sal_Int32 DEFAULT_ICON_ID = 0;
150 //_________________________________________________________________________________________________________________
151 // definitions
153 //*****************************************************************************************************************
154 // XInterface, XTypeProvider
156 DEFINE_XINTERFACE_5(TitleBarUpdate ,
157 OWeakObject ,
158 DIRECT_INTERFACE (css::lang::XTypeProvider ),
159 DIRECT_INTERFACE (css::lang::XInitialization ),
160 DIRECT_INTERFACE (css::frame::XFrameActionListener ),
161 DIRECT_INTERFACE (css::frame::XTitleChangeListener ),
162 DERIVED_INTERFACE(css::lang::XEventListener,css::frame::XFrameActionListener))
164 DEFINE_XTYPEPROVIDER_5(TitleBarUpdate ,
165 css::lang::XTypeProvider ,
166 css::lang::XInitialization ,
167 css::frame::XFrameActionListener,
168 css::frame::XTitleChangeListener,
169 css::lang::XEventListener )
171 //*****************************************************************************************************************
172 TitleBarUpdate::TitleBarUpdate(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
173 : ThreadHelpBase (&Application::GetSolarMutex())
174 , m_xSMGR (xSMGR )
175 , m_xFrame ( )
179 //*****************************************************************************************************************
180 TitleBarUpdate::~TitleBarUpdate()
184 //*****************************************************************************************************************
185 void SAL_CALL TitleBarUpdate::initialize(const css::uno::Sequence< css::uno::Any >& lArguments)
186 throw(css::uno::Exception ,
187 css::uno::RuntimeException)
189 // check arguments
190 css::uno::Reference< css::frame::XFrame > xFrame;
191 if (lArguments.getLength() < 1)
192 throw css::lang::IllegalArgumentException(
193 DECLARE_ASCII("Empty argument list!"),
194 static_cast< ::cppu::OWeakObject* >(this),
197 lArguments[0] >>= xFrame;
198 if (!xFrame.is())
199 throw css::lang::IllegalArgumentException(
200 DECLARE_ASCII("No valid frame specified!"),
201 static_cast< ::cppu::OWeakObject* >(this),
204 // SYNCHRONIZED ->
205 WriteGuard aWriteLock(m_aLock);
206 // hold the frame as weak reference(!) so it can die everytimes :-)
207 m_xFrame = xFrame;
208 aWriteLock.unlock();
209 // <- SYNCHRONIZED
211 // start listening
212 xFrame->addFrameActionListener(this);
214 css::uno::Reference< css::frame::XTitleChangeBroadcaster > xBroadcaster(xFrame, css::uno::UNO_QUERY);
215 if (xBroadcaster.is ())
216 xBroadcaster->addTitleChangeListener (this);
219 //*****************************************************************************************************************
220 void SAL_CALL TitleBarUpdate::frameAction(const css::frame::FrameActionEvent& aEvent)
221 throw(css::uno::RuntimeException)
223 // we are interested on events only, which must trigger a title bar update
224 // because component was changed.
225 if (
226 (aEvent.Action == css::frame::FrameAction_COMPONENT_ATTACHED ) ||
227 (aEvent.Action == css::frame::FrameAction_COMPONENT_REATTACHED) ||
228 (aEvent.Action == css::frame::FrameAction_COMPONENT_DETACHING )
231 impl_forceUpdate ();
235 //*****************************************************************************************************************
236 void SAL_CALL TitleBarUpdate::titleChanged(const css::frame::TitleChangedEvent& /* aEvent */)
237 throw (css::uno::RuntimeException)
239 impl_forceUpdate ();
242 //*****************************************************************************************************************
243 void SAL_CALL TitleBarUpdate::disposing(const css::lang::EventObject&)
244 throw(css::uno::RuntimeException)
246 // nothing todo here - because we hold the frame as weak reference only
249 //*****************************************************************************************************************
250 ::sal_Bool TitleBarUpdate::implst_getModuleInfo(const css::uno::Reference< css::frame::XFrame >& xFrame,
251 TModuleInfo& rInfo )
253 if ( ! xFrame.is ())
254 return sal_False;
256 // SYNCHRONIZED ->
257 ReadGuard aReadLock(m_aLock);
258 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
259 aReadLock.unlock();
260 // <- SYNCHRONIZED
264 css::uno::Reference< css::frame::XModuleManager > xModuleManager(
265 xSMGR->createInstance(SERVICENAME_MODULEMANAGER),
266 css::uno::UNO_QUERY_THROW);
268 css::uno::Reference< css::container::XNameAccess > xConfig(
269 xModuleManager,
270 css::uno::UNO_QUERY_THROW);
272 rInfo.sID = xModuleManager->identify(xFrame);
273 ::comphelper::SequenceAsHashMap lProps = xConfig->getByName (rInfo.sID);
275 rInfo.sUIName = lProps.getUnpackedValueOrDefault (OFFICEFACTORY_PROPNAME_UINAME, ::rtl::OUString());
276 rInfo.nIcon = lProps.getUnpackedValueOrDefault (OFFICEFACTORY_PROPNAME_ICON , INVALID_ICON_ID );
278 // Note: If we could retrieve a module id ... everything is OK.
279 // UIName and Icon ID are optional values !
280 ::sal_Bool bSuccess = (rInfo.sID.getLength () > 0);
281 return bSuccess;
283 catch(const css::uno::Exception&)
286 return sal_False;
289 //*****************************************************************************************************************
290 void TitleBarUpdate::impl_forceUpdate()
292 // SYNCHRONIZED ->
293 ReadGuard aReadLock(m_aLock);
294 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR ;
295 css::uno::Reference< css::frame::XFrame > xFrame(m_xFrame.get(), css::uno::UNO_QUERY);
296 aReadLock.unlock();
297 // <- SYNCHRONIZED
299 // frame already gone ? We hold it weak only ...
300 if ( ! xFrame.is())
301 return;
303 // no window -> no chance to set/update title and icon
304 css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow();
305 if ( ! xWindow.is())
306 return;
308 impl_updateIcon (xFrame);
309 impl_updateTitle (xFrame);
312 //*****************************************************************************************************************
313 void TitleBarUpdate::impl_updateIcon(const css::uno::Reference< css::frame::XFrame >& xFrame)
315 css::uno::Reference< css::frame::XController > xController = xFrame->getController ();
316 css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow ();
318 if (
319 ( ! xController.is() ) ||
320 ( ! xWindow.is() )
322 return;
324 // a) set default value to an invalid one. So we can start further searches for right icon id, if
325 // first steps failed!
326 sal_Int32 nIcon = INVALID_ICON_ID;
328 // b) try to find information on controller property set directly
329 // Don't forget to catch possible exceptions - because these property is an optional one!
330 css::uno::Reference< css::beans::XPropertySet > xSet( xController, css::uno::UNO_QUERY );
331 if ( xSet.is() )
335 xSet->getPropertyValue( CONTROLLER_PROPNAME_ICONID ) >>= nIcon;
337 catch(const css::uno::Exception&)
341 // c) if b) failed ... identify the used module and retrieve set icon from module config.
342 // Tirck :-) Module was already specified outside and aInfo contains all needed informations.
343 if ( nIcon == INVALID_ICON_ID )
345 TModuleInfo aInfo;
346 if (implst_getModuleInfo(xFrame, aInfo))
347 nIcon = aInfo.nIcon;
350 // d) if all steps failed - use fallback :-)
351 // ... means using the global staroffice icon
352 if( nIcon == INVALID_ICON_ID )
353 nIcon = DEFAULT_ICON_ID;
355 // e) set icon on container window now
356 // Don't forget SolarMutex! We use vcl directly :-(
357 // Check window pointer for right WorkWindow class too!!!
359 // VCL SYNCHRONIZED ->
360 ::vos::OClearableGuard aSolarLock( Application::GetSolarMutex() );
362 Window* pWindow = (VCLUnoHelper::GetWindow( xWindow ));
363 if (
364 ( pWindow ) &&
365 ( pWindow->GetType() == WINDOW_WORKWINDOW )
368 WorkWindow* pWorkWindow = (WorkWindow*)pWindow;
369 pWorkWindow->SetIcon( (sal_uInt16)nIcon );
371 css::uno::Reference< css::frame::XModel > xModel = xController->getModel();
372 rtl::OUString aURL;
373 if( xModel.is() )
374 aURL = xModel->getURL();
375 pWorkWindow->SetRepresentedURL( aURL );
378 aSolarLock.clear();
379 // <- VCL SYNCHRONIZED
382 //*****************************************************************************************************************
383 void TitleBarUpdate::impl_updateTitle(const css::uno::Reference< css::frame::XFrame >& xFrame)
385 // no window ... no chance to set any title -> return
386 css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow ();
387 if ( ! xWindow.is() )
388 return;
390 css::uno::Reference< css::frame::XTitle > xTitle(xFrame, css::uno::UNO_QUERY);
391 if ( ! xTitle.is() )
392 return;
394 const ::rtl::OUString sTitle = xTitle->getTitle ();
396 // VCL SYNCHRONIZED ->
397 ::vos::OClearableGuard aSolarLock( Application::GetSolarMutex() );
399 Window* pWindow = (VCLUnoHelper::GetWindow( xWindow ));
400 if (
401 ( pWindow ) &&
402 ( pWindow->GetType() == WINDOW_WORKWINDOW )
405 WorkWindow* pWorkWindow = (WorkWindow*)pWindow;
406 pWorkWindow->SetText( sTitle );
409 aSolarLock.clear();
410 // <- VCL SYNCHRONIZED
413 } // namespace framework