merge the formfield patch from ooo-build
[ooovba.git] / framework / source / helper / titlebarupdate.cxx
blob1db613237dd19caada32b817bfb9ea93691b0426
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: titlebarupdate.cxx,v $
11 * $Revision: 1.3 $
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>
37 #endif
39 //_________________________________________________________________________________________________________________
40 // my own includes
41 //_________________________________________________________________________________________________________________
43 #ifndef __FRAMEWORK_PATTERN_WINDOW_HXX_
44 #include <pattern/window.hxx>
45 #endif
47 #ifndef __FRAMEWORK_THREADHELP_WRITEGUARD_HXX_
48 #include <threadhelp/writeguard.hxx>
49 #endif
51 #ifndef __FRAMEWORK_THREADHELP_READGUARD_HXX_
52 #include <threadhelp/readguard.hxx>
53 #endif
55 #ifndef __FRAMEWORK_MACROS_GENERIC_HXX_
56 #include <macros/generic.hxx>
57 #endif
59 #ifndef __FRAMEWORK_SERVICES_H_
60 #include <services.h>
61 #endif
63 #ifndef __FRAMEWORK_PROPETIES_H_
64 #include <properties.h>
65 #endif
67 //_________________________________________________________________________________________________________________
68 // interface includes
69 //_________________________________________________________________________________________________________________
71 #ifndef _COM_SUN_STAR_AWT_XWINDOW_HPP_
72 #include <com/sun/star/awt/XWindow.hpp>
73 #endif
75 #ifndef _COM_SUN_STAR_LANG_XSERVICXEINFO_HPP_
76 #include <com/sun/star/lang/XServiceInfo.hpp>
77 #endif
79 #ifndef _COM_SUN_STAR_LANG_ILLEGALARGUMENTEXCEPTION_HPP_
80 #include <com/sun/star/lang/IllegalArgumentException.hpp>
81 #endif
83 #ifndef _COM_SUN_STAR_FRAME_XMODULEMANAGER_HPP_
84 #include <com/sun/star/frame/XModuleManager.hpp>
85 #endif
87 #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_
88 #include <com/sun/star/container/XNameAccess.hpp>
89 #endif
91 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
92 #include <com/sun/star/beans/XPropertySet.hpp>
93 #endif
95 #ifndef _COM_SUN_STAR_BEANS_XMATERIALHOLDER_HPP_
96 #include <com/sun/star/beans/XMaterialHolder.hpp>
97 #endif
99 #ifndef _COM_SUN_STAR_FRAME_XTITLECHANGEBROADCASTER_HPP_
100 #include <com/sun/star/frame/XTitleChangeBroadcaster.hpp>
101 #endif
103 #ifndef _COM_SUN_STAR_BEANS_NAMEDVALUE_HPP_
104 #include <com/sun/star/beans/NamedValue.hpp>
105 #endif
107 //_________________________________________________________________________________________________________________
108 // other includes
109 //_________________________________________________________________________________________________________________
111 #ifndef _COMPHELPER_SEQUENCEASHASHMAP_HXX
112 #include <comphelper/sequenceashashmap.hxx>
113 #endif
115 #ifndef _UTL_CONFIGMGR_HXX
116 #include <unotools/configmgr.hxx>
117 #endif
119 #ifndef _UTL_BOOTSTRAP_HXX
120 #include <unotools/bootstrap.hxx>
121 #endif
123 #ifndef _SV_WINDOW_HXX
124 #include <vcl/window.hxx>
125 #endif
127 #ifndef _SV_SYSWIN_HXX
128 #include <vcl/syswin.hxx>
129 #endif
131 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
132 #include <toolkit/unohlp.hxx>
133 #endif
135 #ifndef _SV_SVAPP_HXX
136 #include <vcl/svapp.hxx>
137 #endif
139 #ifndef _SV_WRKWIN_HXX
140 #include <vcl/wrkwin.hxx>
141 #endif
143 //_________________________________________________________________________________________________________________
144 // namespace
146 namespace framework{
148 //_________________________________________________________________________________________________________________
149 // const
151 static const ::sal_Int32 INVALID_ICON_ID = -1;
152 static const ::sal_Int32 DEFAULT_ICON_ID = 0;
154 //_________________________________________________________________________________________________________________
155 // definitions
157 //*****************************************************************************************************************
158 // XInterface, XTypeProvider
160 DEFINE_XINTERFACE_5(TitleBarUpdate ,
161 OWeakObject ,
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())
178 , m_xSMGR (xSMGR )
179 , m_xFrame ( )
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)
193 // check arguments
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;
202 if (!xFrame.is())
203 throw css::lang::IllegalArgumentException(
204 DECLARE_ASCII("No valid frame specified!"),
205 static_cast< ::cppu::OWeakObject* >(this),
208 // SYNCHRONIZED ->
209 WriteGuard aWriteLock(m_aLock);
210 // hold the frame as weak reference(!) so it can die everytimes :-)
211 m_xFrame = xFrame;
212 aWriteLock.unlock();
213 // <- SYNCHRONIZED
215 // start listening
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.
229 if (
230 (aEvent.Action == css::frame::FrameAction_COMPONENT_ATTACHED ) ||
231 (aEvent.Action == css::frame::FrameAction_COMPONENT_REATTACHED) ||
232 (aEvent.Action == css::frame::FrameAction_COMPONENT_DETACHING )
235 impl_forceUpdate ();
239 //*****************************************************************************************************************
240 void SAL_CALL TitleBarUpdate::titleChanged(const css::frame::TitleChangedEvent& /* aEvent */)
241 throw (css::uno::RuntimeException)
243 impl_forceUpdate ();
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,
255 TModuleInfo& rInfo )
257 if ( ! xFrame.is ())
258 return sal_False;
260 // SYNCHRONIZED ->
261 ReadGuard aReadLock(m_aLock);
262 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
263 aReadLock.unlock();
264 // <- SYNCHRONIZED
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(
273 xModuleManager,
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);
285 return bSuccess;
287 catch(const css::uno::Exception&)
290 return sal_False;
293 //*****************************************************************************************************************
294 void TitleBarUpdate::impl_forceUpdate()
296 // SYNCHRONIZED ->
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);
300 aReadLock.unlock();
301 // <- SYNCHRONIZED
303 // frame already gone ? We hold it weak only ...
304 if ( ! xFrame.is())
305 return;
307 // no window -> no chance to set/update title and icon
308 css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow();
309 if ( ! xWindow.is())
310 return;
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 ();
322 if (
323 ( ! xController.is() ) ||
324 ( ! xWindow.is() )
326 return;
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 );
335 if ( xSet.is() )
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 )
349 TModuleInfo aInfo;
350 if (implst_getModuleInfo(xFrame, aInfo))
351 nIcon = aInfo.nIcon;
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 ));
367 if (
368 ( pWindow ) &&
369 ( pWindow->GetType() == WINDOW_WORKWINDOW )
372 WorkWindow* pWorkWindow = (WorkWindow*)pWindow;
373 pWorkWindow->SetIcon( (sal_uInt16)nIcon );
376 aSolarLock.clear();
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() )
386 return;
388 css::uno::Reference< css::frame::XTitle > xTitle(xFrame, css::uno::UNO_QUERY);
389 if ( ! xTitle.is() )
390 return;
392 const ::rtl::OUString sTitle = xTitle->getTitle ();
394 // VCL SYNCHRONIZED ->
395 ::vos::OClearableGuard aSolarLock( Application::GetSolarMutex() );
397 Window* pWindow = (VCLUnoHelper::GetWindow( xWindow ));
398 if (
399 ( pWindow ) &&
400 ( pWindow->GetType() == WINDOW_WORKWINDOW )
403 WorkWindow* pWorkWindow = (WorkWindow*)pWindow;
404 pWorkWindow->SetText( sTitle );
407 aSolarLock.clear();
408 // <- VCL SYNCHRONIZED
411 } // namespace framework