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 <properties.h>
24 #include <com/sun/star/awt/XWindow.hpp>
25 #include <com/sun/star/lang/IllegalArgumentException.hpp>
26 #include <com/sun/star/frame/ModuleManager.hpp>
27 #include <com/sun/star/beans/XPropertySet.hpp>
28 #include <com/sun/star/frame/XTitle.hpp>
29 #include <com/sun/star/frame/XTitleChangeBroadcaster.hpp>
31 #include <comphelper/sequenceashashmap.hxx>
32 #include <unotools/configmgr.hxx>
34 #include <vcl/window.hxx>
35 #include <toolkit/helper/vclunohelper.hxx>
36 #include <vcl/svapp.hxx>
37 #include <vcl/wrkwin.hxx>
38 #include <comphelper/diagnose_ex.hxx>
42 const ::sal_Int32 INVALID_ICON_ID
= -1;
43 const ::sal_Int32 DEFAULT_ICON_ID
= 0;
45 TitleBarUpdate::TitleBarUpdate(css::uno::Reference
< css::uno::XComponentContext
> xContext
)
46 : m_xContext (std::move(xContext
))
50 TitleBarUpdate::~TitleBarUpdate()
54 void SAL_CALL
TitleBarUpdate::initialize(const css::uno::Sequence
< css::uno::Any
>& lArguments
)
57 css::uno::Reference
< css::frame::XFrame
> xFrame
;
58 if (!lArguments
.hasElements())
59 throw css::lang::IllegalArgumentException(
60 "Empty argument list!",
61 static_cast< ::cppu::OWeakObject
* >(this),
64 lArguments
[0] >>= xFrame
;
66 throw css::lang::IllegalArgumentException(
67 "No valid frame specified!",
68 static_cast< ::cppu::OWeakObject
* >(this),
73 // hold the frame as weak reference(!) so it can die everytimes :-)
78 xFrame
->addFrameActionListener(this);
80 css::uno::Reference
< css::frame::XTitleChangeBroadcaster
> xBroadcaster(xFrame
, css::uno::UNO_QUERY
);
81 if (xBroadcaster
.is ())
82 xBroadcaster
->addTitleChangeListener (this);
85 void SAL_CALL
TitleBarUpdate::frameAction(const css::frame::FrameActionEvent
& aEvent
)
87 // we are interested on events only, which must trigger a title bar update
88 // because component was changed.
90 (aEvent
.Action
== css::frame::FrameAction_COMPONENT_ATTACHED
) ||
91 (aEvent
.Action
== css::frame::FrameAction_COMPONENT_REATTACHED
) ||
92 (aEvent
.Action
== css::frame::FrameAction_COMPONENT_DETACHING
)
99 void SAL_CALL
TitleBarUpdate::titleChanged(const css::frame::TitleChangedEvent
& /* aEvent */)
104 void SAL_CALL
TitleBarUpdate::disposing(const css::lang::EventObject
&)
106 css::uno::Reference
< css::frame::XFrame
> xFrame(m_xFrame
.get(), css::uno::UNO_QUERY
);
108 xFrame
->removeFrameActionListener(this);
110 // nothing todo here - because we hold the frame as weak reference only
113 //http://live.gnome.org/GnomeShell/ApplicationBased
114 //http://msdn.microsoft.com/en-us/library/dd378459(v=VS.85).aspx
115 void TitleBarUpdate::impl_updateApplicationID(const css::uno::Reference
< css::frame::XFrame
>& xFrame
)
117 css::uno::Reference
< css::awt::XWindow
> xWindow
= xFrame
->getContainerWindow ();
118 if ( ! xWindow
.is() )
122 OUString sApplicationID
;
125 css::uno::Reference
< css::frame::XModuleManager2
> xModuleManager
=
126 css::frame::ModuleManager::create( m_xContext
);
128 OUString sDesktopName
;
129 OUString aModuleId
= xModuleManager
->identify(xFrame
);
130 if ( aModuleId
.startsWith("com.sun.star.text.") || aModuleId
.startsWith("com.sun.star.xforms.") )
131 sDesktopName
= "Writer";
132 else if ( aModuleId
.startsWith("com.sun.star.sheet.") )
133 sDesktopName
= "Calc";
134 else if ( aModuleId
.startsWith("com.sun.star.presentation.") )
135 sDesktopName
= "Impress";
136 else if ( aModuleId
.startsWith("com.sun.star.drawing." ) )
137 sDesktopName
= "Draw";
138 else if ( aModuleId
.startsWith("com.sun.star.formula." ) )
139 sDesktopName
= "Math";
140 else if ( aModuleId
.startsWith("com.sun.star.sdb.") )
141 sDesktopName
= "Base";
143 sDesktopName
= "Startcenter";
145 // We use a hardcoded product name matching the registry keys so applications can be associated with file types
146 sApplicationID
= "TheDocumentFoundation.LibreOffice." + sDesktopName
;
148 sApplicationID
= utl::ConfigManager::getProductName().toAsciiLowerCase() + "-" + sDesktopName
.toAsciiLowerCase();
151 catch(const css::uno::Exception
&)
155 OUString
const sApplicationID
;
158 // VCL SYNCHRONIZED ->
159 SolarMutexGuard aSolarGuard
;
161 VclPtr
<vcl::Window
> pWindow
= VCLUnoHelper::GetWindow( xWindow
);
162 if ( pWindow
&& pWindow
->GetType() == WindowType::WORKWINDOW
)
164 WorkWindow
* pWorkWindow
= static_cast<WorkWindow
*>(pWindow
.get());
165 pWorkWindow
->SetApplicationID( sApplicationID
);
167 // <- VCL SYNCHRONIZED
170 bool TitleBarUpdate::implst_getModuleInfo(const css::uno::Reference
< css::frame::XFrame
>& xFrame
,
178 css::uno::Reference
< css::frame::XModuleManager2
> xModuleManager
=
179 css::frame::ModuleManager::create( m_xContext
);
181 rInfo
.sID
= xModuleManager
->identify(xFrame
);
182 ::comphelper::SequenceAsHashMap lProps
= xModuleManager
->getByName (rInfo
.sID
);
184 rInfo
.nIcon
= lProps
.getUnpackedValueOrDefault (OFFICEFACTORY_PROPNAME_ASCII_ICON
, INVALID_ICON_ID
);
186 // Note: If we could retrieve a module id ... everything is OK.
187 // UIName and Icon ID are optional values !
188 bool bSuccess
= !rInfo
.sID
.isEmpty();
191 catch(const css::uno::Exception
&)
197 void TitleBarUpdate::impl_forceUpdate()
199 css::uno::Reference
< css::frame::XFrame
> xFrame
;
202 xFrame
.set(m_xFrame
.get(), css::uno::UNO_QUERY
);
205 // frame already gone ? We hold it weak only ...
209 // no window -> no chance to set/update title and icon
210 css::uno::Reference
< css::awt::XWindow
> xWindow
= xFrame
->getContainerWindow();
214 impl_updateIcon (xFrame
);
215 impl_updateTitle (xFrame
);
217 impl_updateApplicationID (xFrame
);
221 void TitleBarUpdate::impl_updateIcon(const css::uno::Reference
< css::frame::XFrame
>& xFrame
)
223 css::uno::Reference
< css::frame::XController
> xController
= xFrame
->getController ();
224 css::uno::Reference
< css::awt::XWindow
> xWindow
= xFrame
->getContainerWindow ();
227 ( ! xController
.is() ) ||
232 // a) set default value to an invalid one. So we can start further searches for right icon id, if
233 // first steps failed!
234 sal_Int32 nIcon
= INVALID_ICON_ID
;
236 // b) try to find information on controller property set directly
237 // Don't forget to catch possible exceptions - because these property is an optional one!
238 css::uno::Reference
< css::beans::XPropertySet
> xSet( xController
, css::uno::UNO_QUERY
);
243 css::uno::Reference
< css::beans::XPropertySetInfo
> const xPSI( xSet
->getPropertySetInfo(), css::uno::UNO_SET_THROW
);
244 if ( xPSI
->hasPropertyByName( "IconId" ) )
245 xSet
->getPropertyValue( "IconId" ) >>= nIcon
;
247 catch(const css::uno::Exception
&)
249 DBG_UNHANDLED_EXCEPTION("fwk");
253 // c) if b) failed ... identify the used module and retrieve set icon from module config.
254 // Tirck :-) Module was already specified outside and aInfo contains all needed information.
255 if ( nIcon
== INVALID_ICON_ID
)
258 if (implst_getModuleInfo(xFrame
, aInfo
))
262 // d) if all steps failed - use fallback :-)
263 // ... means using the global staroffice icon
264 if( nIcon
== INVALID_ICON_ID
)
265 nIcon
= DEFAULT_ICON_ID
;
267 // e) set icon on container window now
268 // Don't forget SolarMutex! We use vcl directly :-(
269 // Check window pointer for right WorkWindow class too!!!
271 // VCL SYNCHRONIZED ->
272 SolarMutexGuard aSolarGuard
;
274 VclPtr
<vcl::Window
> pWindow
= VCLUnoHelper::GetWindow( xWindow
);
275 if ( pWindow
&& ( pWindow
->GetType() == WindowType::WORKWINDOW
) )
277 WorkWindow
* pWorkWindow
= static_cast<WorkWindow
*>(pWindow
.get());
278 pWorkWindow
->SetIcon( static_cast<sal_uInt16
>(nIcon
) );
280 css::uno::Reference
< css::frame::XModel
> xModel
= xController
->getModel();
283 aURL
= xModel
->getURL();
284 pWorkWindow
->SetRepresentedURL( aURL
);
286 // <- VCL SYNCHRONIZED
289 void TitleBarUpdate::impl_updateTitle(const css::uno::Reference
< css::frame::XFrame
>& xFrame
)
291 // no window ... no chance to set any title -> return
292 css::uno::Reference
< css::awt::XWindow
> xWindow
= xFrame
->getContainerWindow ();
293 if ( ! xWindow
.is() )
296 css::uno::Reference
< css::frame::XTitle
> xTitle(xFrame
, css::uno::UNO_QUERY
);
300 const OUString sTitle
= xTitle
->getTitle ();
302 // VCL SYNCHRONIZED ->
303 SolarMutexGuard aSolarGuard
;
305 VclPtr
<vcl::Window
> pWindow
= VCLUnoHelper::GetWindow( xWindow
);
306 if ( pWindow
&& ( pWindow
->GetType() == WindowType::WORKWINDOW
) )
308 WorkWindow
* pWorkWindow
= static_cast<WorkWindow
*>(pWindow
.get());
309 pWorkWindow
->SetText( sTitle
);
311 // <- VCL SYNCHRONIZED
314 } // namespace framework
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */