1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: titlebarupdate.cxx,v $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_framework.hxx"
35 #ifndef __FRAMEWORK_HELPER_TITLEBARUPDATE_HXX_
36 #include <helper/titlebarupdate.hxx>
39 //_________________________________________________________________________________________________________________
41 //_________________________________________________________________________________________________________________
43 #ifndef __FRAMEWORK_PATTERN_WINDOW_HXX_
44 #include <pattern/window.hxx>
47 #ifndef __FRAMEWORK_THREADHELP_WRITEGUARD_HXX_
48 #include <threadhelp/writeguard.hxx>
51 #ifndef __FRAMEWORK_THREADHELP_READGUARD_HXX_
52 #include <threadhelp/readguard.hxx>
55 #ifndef __FRAMEWORK_MACROS_GENERIC_HXX_
56 #include <macros/generic.hxx>
59 #ifndef __FRAMEWORK_SERVICES_H_
63 #ifndef __FRAMEWORK_PROPETIES_H_
64 #include <properties.h>
67 //_________________________________________________________________________________________________________________
69 //_________________________________________________________________________________________________________________
71 #ifndef _COM_SUN_STAR_AWT_XWINDOW_HPP_
72 #include <com/sun/star/awt/XWindow.hpp>
75 #ifndef _COM_SUN_STAR_LANG_XSERVICXEINFO_HPP_
76 #include <com/sun/star/lang/XServiceInfo.hpp>
79 #ifndef _COM_SUN_STAR_LANG_ILLEGALARGUMENTEXCEPTION_HPP_
80 #include <com/sun/star/lang/IllegalArgumentException.hpp>
83 #ifndef _COM_SUN_STAR_FRAME_XMODULEMANAGER_HPP_
84 #include <com/sun/star/frame/XModuleManager.hpp>
87 #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_
88 #include <com/sun/star/container/XNameAccess.hpp>
91 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
92 #include <com/sun/star/beans/XPropertySet.hpp>
95 #ifndef _COM_SUN_STAR_BEANS_XMATERIALHOLDER_HPP_
96 #include <com/sun/star/beans/XMaterialHolder.hpp>
99 #ifndef _COM_SUN_STAR_FRAME_XTITLECHANGEBROADCASTER_HPP_
100 #include <com/sun/star/frame/XTitleChangeBroadcaster.hpp>
103 #ifndef _COM_SUN_STAR_BEANS_NAMEDVALUE_HPP_
104 #include <com/sun/star/beans/NamedValue.hpp>
107 //_________________________________________________________________________________________________________________
109 //_________________________________________________________________________________________________________________
111 #ifndef _COMPHELPER_SEQUENCEASHASHMAP_HXX
112 #include <comphelper/sequenceashashmap.hxx>
115 #ifndef _UTL_CONFIGMGR_HXX
116 #include <unotools/configmgr.hxx>
119 #ifndef _UTL_BOOTSTRAP_HXX
120 #include <unotools/bootstrap.hxx>
123 #ifndef _SV_WINDOW_HXX
124 #include <vcl/window.hxx>
127 #ifndef _SV_SYSWIN_HXX
128 #include <vcl/syswin.hxx>
131 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
132 #include <toolkit/unohlp.hxx>
135 #ifndef _SV_SVAPP_HXX
136 #include <vcl/svapp.hxx>
139 #ifndef _SV_WRKWIN_HXX
140 #include <vcl/wrkwin.hxx>
143 //_________________________________________________________________________________________________________________
148 //_________________________________________________________________________________________________________________
151 static const ::sal_Int32 INVALID_ICON_ID
= -1;
152 static const ::sal_Int32 DEFAULT_ICON_ID
= 0;
154 //_________________________________________________________________________________________________________________
157 //*****************************************************************************************************************
158 // XInterface, XTypeProvider
160 DEFINE_XINTERFACE_5(TitleBarUpdate
,
162 DIRECT_INTERFACE (css::lang::XTypeProvider
),
163 DIRECT_INTERFACE (css::lang::XInitialization
),
164 DIRECT_INTERFACE (css::frame::XFrameActionListener
),
165 DIRECT_INTERFACE (css::frame::XTitleChangeListener
),
166 DERIVED_INTERFACE(css::lang::XEventListener
,css::frame::XFrameActionListener
))
168 DEFINE_XTYPEPROVIDER_5(TitleBarUpdate
,
169 css::lang::XTypeProvider
,
170 css::lang::XInitialization
,
171 css::frame::XFrameActionListener
,
172 css::frame::XTitleChangeListener
,
173 css::lang::XEventListener
)
175 //*****************************************************************************************************************
176 TitleBarUpdate::TitleBarUpdate(const css::uno::Reference
< css::lang::XMultiServiceFactory
>& xSMGR
)
177 : ThreadHelpBase (&Application::GetSolarMutex())
183 //*****************************************************************************************************************
184 TitleBarUpdate::~TitleBarUpdate()
188 //*****************************************************************************************************************
189 void SAL_CALL
TitleBarUpdate::initialize(const css::uno::Sequence
< css::uno::Any
>& lArguments
)
190 throw(css::uno::Exception
,
191 css::uno::RuntimeException
)
194 css::uno::Reference
< css::frame::XFrame
> xFrame
;
195 if (lArguments
.getLength() < 1)
196 throw css::lang::IllegalArgumentException(
197 DECLARE_ASCII("Empty argument list!"),
198 static_cast< ::cppu::OWeakObject
* >(this),
201 lArguments
[0] >>= xFrame
;
203 throw css::lang::IllegalArgumentException(
204 DECLARE_ASCII("No valid frame specified!"),
205 static_cast< ::cppu::OWeakObject
* >(this),
209 WriteGuard
aWriteLock(m_aLock
);
210 // hold the frame as weak reference(!) so it can die everytimes :-)
216 xFrame
->addFrameActionListener(this);
218 css::uno::Reference
< css::frame::XTitleChangeBroadcaster
> xBroadcaster(xFrame
, css::uno::UNO_QUERY
);
219 if (xBroadcaster
.is ())
220 xBroadcaster
->addTitleChangeListener (this);
223 //*****************************************************************************************************************
224 void SAL_CALL
TitleBarUpdate::frameAction(const css::frame::FrameActionEvent
& aEvent
)
225 throw(css::uno::RuntimeException
)
227 // we are interested on events only, which must trigger a title bar update
228 // because component was changed.
230 (aEvent
.Action
== css::frame::FrameAction_COMPONENT_ATTACHED
) ||
231 (aEvent
.Action
== css::frame::FrameAction_COMPONENT_REATTACHED
) ||
232 (aEvent
.Action
== css::frame::FrameAction_COMPONENT_DETACHING
)
239 //*****************************************************************************************************************
240 void SAL_CALL
TitleBarUpdate::titleChanged(const css::frame::TitleChangedEvent
& /* aEvent */)
241 throw (css::uno::RuntimeException
)
246 //*****************************************************************************************************************
247 void SAL_CALL
TitleBarUpdate::disposing(const css::lang::EventObject
&)
248 throw(css::uno::RuntimeException
)
250 // nothing todo here - because we hold the frame as weak reference only
253 //*****************************************************************************************************************
254 ::sal_Bool
TitleBarUpdate::implst_getModuleInfo(const css::uno::Reference
< css::frame::XFrame
>& xFrame
,
261 ReadGuard
aReadLock(m_aLock
);
262 css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
= m_xSMGR
;
268 css::uno::Reference
< css::frame::XModuleManager
> xModuleManager(
269 xSMGR
->createInstance(SERVICENAME_MODULEMANAGER
),
270 css::uno::UNO_QUERY_THROW
);
272 css::uno::Reference
< css::container::XNameAccess
> xConfig(
274 css::uno::UNO_QUERY_THROW
);
276 rInfo
.sID
= xModuleManager
->identify(xFrame
);
277 ::comphelper::SequenceAsHashMap lProps
= xConfig
->getByName (rInfo
.sID
);
279 rInfo
.sUIName
= lProps
.getUnpackedValueOrDefault (OFFICEFACTORY_PROPNAME_UINAME
, ::rtl::OUString());
280 rInfo
.nIcon
= lProps
.getUnpackedValueOrDefault (OFFICEFACTORY_PROPNAME_ICON
, INVALID_ICON_ID
);
282 // Note: If we could retrieve a module id ... everything is OK.
283 // UIName and Icon ID are optional values !
284 ::sal_Bool bSuccess
= (rInfo
.sID
.getLength () > 0);
287 catch(const css::uno::Exception
&)
293 //*****************************************************************************************************************
294 void TitleBarUpdate::impl_forceUpdate()
297 ReadGuard
aReadLock(m_aLock
);
298 css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
= m_xSMGR
;
299 css::uno::Reference
< css::frame::XFrame
> xFrame(m_xFrame
.get(), css::uno::UNO_QUERY
);
303 // frame already gone ? We hold it weak only ...
307 // no window -> no chance to set/update title and icon
308 css::uno::Reference
< css::awt::XWindow
> xWindow
= xFrame
->getContainerWindow();
312 impl_updateIcon (xFrame
);
313 impl_updateTitle (xFrame
);
316 //*****************************************************************************************************************
317 void TitleBarUpdate::impl_updateIcon(const css::uno::Reference
< css::frame::XFrame
>& xFrame
)
319 css::uno::Reference
< css::frame::XController
> xController
= xFrame
->getController ();
320 css::uno::Reference
< css::awt::XWindow
> xWindow
= xFrame
->getContainerWindow ();
323 ( ! xController
.is() ) ||
328 // a) set default value to an invalid one. So we can start further searches for right icon id, if
329 // first steps failed!
330 sal_Int32 nIcon
= INVALID_ICON_ID
;
332 // b) try to find information on controller property set directly
333 // Don't forget to catch possible exceptions - because these property is an optional one!
334 css::uno::Reference
< css::beans::XPropertySet
> xSet( xController
, css::uno::UNO_QUERY
);
339 xSet
->getPropertyValue( CONTROLLER_PROPNAME_ICONID
) >>= nIcon
;
341 catch(const css::uno::Exception
&)
345 // c) if b) failed ... identify the used module and retrieve set icon from module config.
346 // Tirck :-) Module was already specified outside and aInfo contains all needed informations.
347 if ( nIcon
== INVALID_ICON_ID
)
350 if (implst_getModuleInfo(xFrame
, aInfo
))
354 // d) if all steps failed - use fallback :-)
355 // ... means using the global staroffice icon
356 if( nIcon
== INVALID_ICON_ID
)
357 nIcon
= DEFAULT_ICON_ID
;
359 // e) set icon on container window now
360 // Don't forget SolarMutex! We use vcl directly :-(
361 // Check window pointer for right WorkWindow class too!!!
363 // VCL SYNCHRONIZED ->
364 ::vos::OClearableGuard
aSolarLock( Application::GetSolarMutex() );
366 Window
* pWindow
= (VCLUnoHelper::GetWindow( xWindow
));
369 ( pWindow
->GetType() == WINDOW_WORKWINDOW
)
372 WorkWindow
* pWorkWindow
= (WorkWindow
*)pWindow
;
373 pWorkWindow
->SetIcon( (sal_uInt16
)nIcon
);
377 // <- VCL SYNCHRONIZED
380 //*****************************************************************************************************************
381 void TitleBarUpdate::impl_updateTitle(const css::uno::Reference
< css::frame::XFrame
>& xFrame
)
383 // no window ... no chance to set any title -> return
384 css::uno::Reference
< css::awt::XWindow
> xWindow
= xFrame
->getContainerWindow ();
385 if ( ! xWindow
.is() )
388 css::uno::Reference
< css::frame::XTitle
> xTitle(xFrame
, css::uno::UNO_QUERY
);
392 const ::rtl::OUString sTitle
= xTitle
->getTitle ();
394 // VCL SYNCHRONIZED ->
395 ::vos::OClearableGuard
aSolarLock( Application::GetSolarMutex() );
397 Window
* pWindow
= (VCLUnoHelper::GetWindow( xWindow
));
400 ( pWindow
->GetType() == WINDOW_WORKWINDOW
)
403 WorkWindow
* pWorkWindow
= (WorkWindow
*)pWindow
;
404 pWorkWindow
->SetText( sTitle
);
408 // <- VCL SYNCHRONIZED
411 } // namespace framework