1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <helper/titlebarupdate.hxx>
22 #include <pattern/window.hxx>
23 #include <macros/generic.hxx>
25 #include <properties.h>
27 #include <com/sun/star/awt/XWindow.hpp>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <com/sun/star/lang/IllegalArgumentException.hpp>
30 #include <com/sun/star/frame/ModuleManager.hpp>
31 #include <com/sun/star/container/XNameAccess.hpp>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/beans/XMaterialHolder.hpp>
34 #include <com/sun/star/frame/XTitleChangeBroadcaster.hpp>
35 #include <com/sun/star/beans/NamedValue.hpp>
37 #include <comphelper/processfactory.hxx>
38 #include <comphelper/sequenceashashmap.hxx>
39 #include <unotools/configmgr.hxx>
40 #include <unotools/bootstrap.hxx>
41 #include <vcl/window.hxx>
42 #include <vcl/syswin.hxx>
43 #include <toolkit/helper/vclunohelper.hxx>
44 #include <vcl/svapp.hxx>
45 #include <vcl/wrkwin.hxx>
46 #include <tools/diagnose_ex.h>
50 static const ::sal_Int32 INVALID_ICON_ID
= -1;
51 static const ::sal_Int32 DEFAULT_ICON_ID
= 0;
53 TitleBarUpdate::TitleBarUpdate(const css::uno::Reference
< css::uno::XComponentContext
>& xContext
)
54 : m_xContext (xContext
)
59 TitleBarUpdate::~TitleBarUpdate()
63 void SAL_CALL
TitleBarUpdate::initialize(const css::uno::Sequence
< css::uno::Any
>& lArguments
)
64 throw(css::uno::Exception
,
65 css::uno::RuntimeException
, std::exception
)
68 css::uno::Reference
< css::frame::XFrame
> xFrame
;
69 if (lArguments
.getLength() < 1)
70 throw css::lang::IllegalArgumentException(
71 "Empty argument list!",
72 static_cast< ::cppu::OWeakObject
* >(this),
75 lArguments
[0] >>= xFrame
;
77 throw css::lang::IllegalArgumentException(
78 "No valid frame specified!",
79 static_cast< ::cppu::OWeakObject
* >(this),
84 // hold the frame as weak reference(!) so it can die everytimes :-)
89 xFrame
->addFrameActionListener(this);
91 css::uno::Reference
< css::frame::XTitleChangeBroadcaster
> xBroadcaster(xFrame
, css::uno::UNO_QUERY
);
92 if (xBroadcaster
.is ())
93 xBroadcaster
->addTitleChangeListener (this);
96 void SAL_CALL
TitleBarUpdate::frameAction(const css::frame::FrameActionEvent
& aEvent
)
97 throw(css::uno::RuntimeException
, std::exception
)
99 // we are interested on events only, which must trigger a title bar update
100 // because component was changed.
102 (aEvent
.Action
== css::frame::FrameAction_COMPONENT_ATTACHED
) ||
103 (aEvent
.Action
== css::frame::FrameAction_COMPONENT_REATTACHED
) ||
104 (aEvent
.Action
== css::frame::FrameAction_COMPONENT_DETACHING
)
111 void SAL_CALL
TitleBarUpdate::titleChanged(const css::frame::TitleChangedEvent
& /* aEvent */)
112 throw (css::uno::RuntimeException
, std::exception
)
117 void SAL_CALL
TitleBarUpdate::disposing(const css::lang::EventObject
&)
118 throw(css::uno::RuntimeException
, std::exception
)
120 // nothing todo here - because we hold the frame as weak reference only
123 //http://live.gnome.org/GnomeShell/ApplicationBased
124 //See http://msdn.microsoft.com/en-us/library/dd378459(v=VS.85).aspx for future
125 //Windows 7 equivalent support
126 void TitleBarUpdate::impl_updateApplicationID(const css::uno::Reference
< css::frame::XFrame
>& xFrame
)
128 css::uno::Reference
< css::awt::XWindow
> xWindow
= xFrame
->getContainerWindow ();
129 if ( ! xWindow
.is() )
132 OUString sApplicationID
;
137 css::uno::Reference
< css::frame::XModuleManager2
> xModuleManager
=
138 css::frame::ModuleManager::create( m_xContext
);
140 OUString sDesktopName
;
141 OUString aModuleId
= xModuleManager
->identify(xFrame
);
142 if ( aModuleId
.startsWith("com.sun.star.text.") || aModuleId
.startsWith("com.sun.star.xforms.") )
143 sDesktopName
= "Writer";
144 else if ( aModuleId
.startsWith("com.sun.star.sheet.") )
145 sDesktopName
= "Calc";
146 else if ( aModuleId
.startsWith("com.sun.star.presentation.") )
147 sDesktopName
= "Impress";
148 else if ( aModuleId
.startsWith("com.sun.star.drawing." ) )
149 sDesktopName
= "Draw";
150 else if ( aModuleId
.startsWith("com.sun.star.formula." ) )
151 sDesktopName
= "Math";
152 else if ( aModuleId
.startsWith("com.sun.star.sdb.") )
153 sDesktopName
= "Base";
155 sDesktopName
= "Startcenter";
157 // We use a hardcoded product name matching the registry keys so applications can be associated with file types
158 sApplicationID
= "TheDocumentFoundation.LibreOffice.";
159 sApplicationID
+= sDesktopName
;
161 sApplicationID
= utl::ConfigManager::getProductName().toAsciiLowerCase();
162 sApplicationID
+= "-";
163 sApplicationID
+= sDesktopName
.toAsciiLowerCase();
166 catch(const css::uno::Exception
&)
171 // VCL SYNCHRONIZED ->
172 SolarMutexGuard aSolarGuard
;
174 vcl::Window
* pWindow
= (VCLUnoHelper::GetWindow( xWindow
));
177 ( pWindow
->GetType() == WINDOW_WORKWINDOW
)
180 WorkWindow
* pWorkWindow
= static_cast<WorkWindow
*>(pWindow
);
181 pWorkWindow
->SetApplicationID( sApplicationID
);
183 // <- VCL SYNCHRONIZED
186 bool TitleBarUpdate::implst_getModuleInfo(const css::uno::Reference
< css::frame::XFrame
>& xFrame
,
194 css::uno::Reference
< css::frame::XModuleManager2
> xModuleManager
=
195 css::frame::ModuleManager::create( m_xContext
);
197 rInfo
.sID
= xModuleManager
->identify(xFrame
);
198 ::comphelper::SequenceAsHashMap lProps
= xModuleManager
->getByName (rInfo
.sID
);
200 rInfo
.sUIName
= lProps
.getUnpackedValueOrDefault (OFFICEFACTORY_PROPNAME_UINAME
, OUString());
201 rInfo
.nIcon
= lProps
.getUnpackedValueOrDefault (OFFICEFACTORY_PROPNAME_ICON
, INVALID_ICON_ID
);
203 // Note: If we could retrieve a module id ... everything is OK.
204 // UIName and Icon ID are optional values !
205 bool bSuccess
= !rInfo
.sID
.isEmpty();
208 catch(const css::uno::Exception
&)
214 void TitleBarUpdate::impl_forceUpdate()
216 css::uno::Reference
< css::frame::XFrame
> xFrame
;
219 xFrame
.set(m_xFrame
.get(), css::uno::UNO_QUERY
);
222 // frame already gone ? We hold it weak only ...
226 // no window -> no chance to set/update title and icon
227 css::uno::Reference
< css::awt::XWindow
> xWindow
= xFrame
->getContainerWindow();
231 impl_updateIcon (xFrame
);
232 impl_updateTitle (xFrame
);
234 impl_updateApplicationID (xFrame
);
238 void TitleBarUpdate::impl_updateIcon(const css::uno::Reference
< css::frame::XFrame
>& xFrame
)
240 css::uno::Reference
< css::frame::XController
> xController
= xFrame
->getController ();
241 css::uno::Reference
< css::awt::XWindow
> xWindow
= xFrame
->getContainerWindow ();
244 ( ! xController
.is() ) ||
249 // a) set default value to an invalid one. So we can start further searches for right icon id, if
250 // first steps failed!
251 sal_Int32 nIcon
= INVALID_ICON_ID
;
253 // b) try to find information on controller property set directly
254 // Don't forget to catch possible exceptions - because these property is an optional one!
255 css::uno::Reference
< css::beans::XPropertySet
> xSet( xController
, css::uno::UNO_QUERY
);
260 css::uno::Reference
< css::beans::XPropertySetInfo
> const xPSI( xSet
->getPropertySetInfo(), css::uno::UNO_SET_THROW
);
261 if ( xPSI
->hasPropertyByName( "IconId" ) )
262 xSet
->getPropertyValue( "IconId" ) >>= nIcon
;
264 catch(const css::uno::Exception
&)
266 DBG_UNHANDLED_EXCEPTION();
270 // c) if b) failed ... identify the used module and retrieve set icon from module config.
271 // Tirck :-) Module was already specified outside and aInfo contains all needed information.
272 if ( nIcon
== INVALID_ICON_ID
)
275 if (implst_getModuleInfo(xFrame
, aInfo
))
279 // d) if all steps failed - use fallback :-)
280 // ... means using the global staroffice icon
281 if( nIcon
== INVALID_ICON_ID
)
282 nIcon
= DEFAULT_ICON_ID
;
284 // e) set icon on container window now
285 // Don't forget SolarMutex! We use vcl directly :-(
286 // Check window pointer for right WorkWindow class too!!!
288 // VCL SYNCHRONIZED ->
289 SolarMutexGuard aSolarGuard
;
291 vcl::Window
* pWindow
= (VCLUnoHelper::GetWindow( xWindow
));
294 ( pWindow
->GetType() == WINDOW_WORKWINDOW
)
297 WorkWindow
* pWorkWindow
= static_cast<WorkWindow
*>(pWindow
);
298 pWorkWindow
->SetIcon( (sal_uInt16
)nIcon
);
300 css::uno::Reference
< css::frame::XModel
> xModel
= xController
->getModel();
303 aURL
= xModel
->getURL();
304 pWorkWindow
->SetRepresentedURL( aURL
);
306 // <- VCL SYNCHRONIZED
309 void TitleBarUpdate::impl_updateTitle(const css::uno::Reference
< css::frame::XFrame
>& xFrame
)
311 // no window ... no chance to set any title -> return
312 css::uno::Reference
< css::awt::XWindow
> xWindow
= xFrame
->getContainerWindow ();
313 if ( ! xWindow
.is() )
316 css::uno::Reference
< css::frame::XTitle
> xTitle(xFrame
, css::uno::UNO_QUERY
);
320 const OUString sTitle
= xTitle
->getTitle ();
322 // VCL SYNCHRONIZED ->
323 SolarMutexGuard aSolarGuard
;
325 vcl::Window
* pWindow
= (VCLUnoHelper::GetWindow( xWindow
));
328 ( pWindow
->GetType() == WINDOW_WORKWINDOW
)
331 WorkWindow
* pWorkWindow
= static_cast<WorkWindow
*>(pWindow
);
332 pWorkWindow
->SetText( sTitle
);
334 // <- VCL SYNCHRONIZED
337 } // namespace framework
339 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */