Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / framework / source / helper / titlebarupdate.cxx
blob9536485f66d506806c2b0583af79f3226a3d4b16
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <services.h>
24 #include <properties.h>
26 #include <com/sun/star/awt/XWindow.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <com/sun/star/lang/IllegalArgumentException.hpp>
29 #include <com/sun/star/frame/ModuleManager.hpp>
30 #include <com/sun/star/container/XNameAccess.hpp>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <com/sun/star/beans/XMaterialHolder.hpp>
33 #include <com/sun/star/frame/XTitle.hpp>
34 #include <com/sun/star/frame/XTitleChangeBroadcaster.hpp>
35 #include <com/sun/star/beans/NamedValue.hpp>
37 #include <comphelper/sequenceashashmap.hxx>
38 #include <unotools/configmgr.hxx>
39 #include <unotools/bootstrap.hxx>
40 #include <vcl/window.hxx>
41 #include <vcl/syswin.hxx>
42 #include <toolkit/helper/vclunohelper.hxx>
43 #include <vcl/svapp.hxx>
44 #include <vcl/wrkwin.hxx>
45 #include <tools/diagnose_ex.h>
47 namespace framework{
49 static const ::sal_Int32 INVALID_ICON_ID = -1;
50 static const ::sal_Int32 DEFAULT_ICON_ID = 0;
52 TitleBarUpdate::TitleBarUpdate(const css::uno::Reference< css::uno::XComponentContext >& xContext)
53 : m_xContext (xContext )
54 , m_xFrame ( )
58 TitleBarUpdate::~TitleBarUpdate()
62 void SAL_CALL TitleBarUpdate::initialize(const css::uno::Sequence< css::uno::Any >& lArguments)
64 // check arguments
65 css::uno::Reference< css::frame::XFrame > xFrame;
66 if (!lArguments.hasElements())
67 throw css::lang::IllegalArgumentException(
68 "Empty argument list!",
69 static_cast< ::cppu::OWeakObject* >(this),
70 1);
72 lArguments[0] >>= xFrame;
73 if (!xFrame.is())
74 throw css::lang::IllegalArgumentException(
75 "No valid frame specified!",
76 static_cast< ::cppu::OWeakObject* >(this),
77 1);
80 SolarMutexGuard g;
81 // hold the frame as weak reference(!) so it can die everytimes :-)
82 m_xFrame = xFrame;
85 // start listening
86 xFrame->addFrameActionListener(this);
88 css::uno::Reference< css::frame::XTitleChangeBroadcaster > xBroadcaster(xFrame, css::uno::UNO_QUERY);
89 if (xBroadcaster.is ())
90 xBroadcaster->addTitleChangeListener (this);
93 void SAL_CALL TitleBarUpdate::frameAction(const css::frame::FrameActionEvent& aEvent)
95 // we are interested on events only, which must trigger a title bar update
96 // because component was changed.
97 if (
98 (aEvent.Action == css::frame::FrameAction_COMPONENT_ATTACHED ) ||
99 (aEvent.Action == css::frame::FrameAction_COMPONENT_REATTACHED) ||
100 (aEvent.Action == css::frame::FrameAction_COMPONENT_DETACHING )
103 impl_forceUpdate ();
107 void SAL_CALL TitleBarUpdate::titleChanged(const css::frame::TitleChangedEvent& /* aEvent */)
109 impl_forceUpdate ();
112 void SAL_CALL TitleBarUpdate::disposing(const css::lang::EventObject&)
114 // nothing todo here - because we hold the frame as weak reference only
117 //http://live.gnome.org/GnomeShell/ApplicationBased
118 //See http://msdn.microsoft.com/en-us/library/dd378459(v=VS.85).aspx for future
119 //Windows 7 equivalent support
120 void TitleBarUpdate::impl_updateApplicationID(const css::uno::Reference< css::frame::XFrame >& xFrame)
122 css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow ();
123 if ( ! xWindow.is() )
124 return;
126 #if !defined(MACOSX)
127 OUString sApplicationID;
130 css::uno::Reference< css::frame::XModuleManager2 > xModuleManager =
131 css::frame::ModuleManager::create( m_xContext );
133 OUString sDesktopName;
134 OUString aModuleId = xModuleManager->identify(xFrame);
135 if ( aModuleId.startsWith("com.sun.star.text.") || aModuleId.startsWith("com.sun.star.xforms.") )
136 sDesktopName = "Writer";
137 else if ( aModuleId.startsWith("com.sun.star.sheet.") )
138 sDesktopName = "Calc";
139 else if ( aModuleId.startsWith("com.sun.star.presentation.") )
140 sDesktopName = "Impress";
141 else if ( aModuleId.startsWith("com.sun.star.drawing." ) )
142 sDesktopName = "Draw";
143 else if ( aModuleId.startsWith("com.sun.star.formula." ) )
144 sDesktopName = "Math";
145 else if ( aModuleId.startsWith("com.sun.star.sdb.") )
146 sDesktopName = "Base";
147 else
148 sDesktopName = "Startcenter";
149 #if defined(_WIN32)
150 // We use a hardcoded product name matching the registry keys so applications can be associated with file types
151 sApplicationID = "TheDocumentFoundation.LibreOffice." + sDesktopName;
152 #else
153 sApplicationID = utl::ConfigManager::getProductName().toAsciiLowerCase() + "-" + sDesktopName.toAsciiLowerCase();
154 #endif
156 catch(const css::uno::Exception&)
159 #else
160 OUString const sApplicationID;
161 #endif
163 // VCL SYNCHRONIZED ->
164 SolarMutexGuard aSolarGuard;
166 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
167 if ( pWindow && pWindow->GetType() == WindowType::WORKWINDOW )
169 WorkWindow* pWorkWindow = static_cast<WorkWindow*>(pWindow.get());
170 pWorkWindow->SetApplicationID( sApplicationID );
172 // <- VCL SYNCHRONIZED
175 bool TitleBarUpdate::implst_getModuleInfo(const css::uno::Reference< css::frame::XFrame >& xFrame,
176 TModuleInfo& rInfo )
178 if ( ! xFrame.is ())
179 return false;
183 css::uno::Reference< css::frame::XModuleManager2 > xModuleManager =
184 css::frame::ModuleManager::create( m_xContext );
186 rInfo.sID = xModuleManager->identify(xFrame);
187 ::comphelper::SequenceAsHashMap lProps = xModuleManager->getByName (rInfo.sID);
189 rInfo.nIcon = lProps.getUnpackedValueOrDefault (OFFICEFACTORY_PROPNAME_ASCII_ICON , INVALID_ICON_ID );
191 // Note: If we could retrieve a module id ... everything is OK.
192 // UIName and Icon ID are optional values !
193 bool bSuccess = !rInfo.sID.isEmpty();
194 return bSuccess;
196 catch(const css::uno::Exception&)
199 return false;
202 void TitleBarUpdate::impl_forceUpdate()
204 css::uno::Reference< css::frame::XFrame > xFrame;
206 SolarMutexGuard g;
207 xFrame.set(m_xFrame.get(), css::uno::UNO_QUERY);
210 // frame already gone ? We hold it weak only ...
211 if ( ! xFrame.is())
212 return;
214 // no window -> no chance to set/update title and icon
215 css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow();
216 if ( ! xWindow.is())
217 return;
219 impl_updateIcon (xFrame);
220 impl_updateTitle (xFrame);
221 #if !defined(MACOSX)
222 impl_updateApplicationID (xFrame);
223 #endif
226 void TitleBarUpdate::impl_updateIcon(const css::uno::Reference< css::frame::XFrame >& xFrame)
228 css::uno::Reference< css::frame::XController > xController = xFrame->getController ();
229 css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow ();
231 if (
232 ( ! xController.is() ) ||
233 ( ! xWindow.is() )
235 return;
237 // a) set default value to an invalid one. So we can start further searches for right icon id, if
238 // first steps failed!
239 sal_Int32 nIcon = INVALID_ICON_ID;
241 // b) try to find information on controller property set directly
242 // Don't forget to catch possible exceptions - because these property is an optional one!
243 css::uno::Reference< css::beans::XPropertySet > xSet( xController, css::uno::UNO_QUERY );
244 if ( xSet.is() )
248 css::uno::Reference< css::beans::XPropertySetInfo > const xPSI( xSet->getPropertySetInfo(), css::uno::UNO_SET_THROW );
249 if ( xPSI->hasPropertyByName( "IconId" ) )
250 xSet->getPropertyValue( "IconId" ) >>= nIcon;
252 catch(const css::uno::Exception&)
254 DBG_UNHANDLED_EXCEPTION("fwk");
258 // c) if b) failed ... identify the used module and retrieve set icon from module config.
259 // Tirck :-) Module was already specified outside and aInfo contains all needed information.
260 if ( nIcon == INVALID_ICON_ID )
262 TModuleInfo aInfo;
263 if (implst_getModuleInfo(xFrame, aInfo))
264 nIcon = aInfo.nIcon;
267 // d) if all steps failed - use fallback :-)
268 // ... means using the global staroffice icon
269 if( nIcon == INVALID_ICON_ID )
270 nIcon = DEFAULT_ICON_ID;
272 // e) set icon on container window now
273 // Don't forget SolarMutex! We use vcl directly :-(
274 // Check window pointer for right WorkWindow class too!!!
276 // VCL SYNCHRONIZED ->
277 SolarMutexGuard aSolarGuard;
279 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
280 if ( pWindow && ( pWindow->GetType() == WindowType::WORKWINDOW ) )
282 WorkWindow* pWorkWindow = static_cast<WorkWindow*>(pWindow.get());
283 pWorkWindow->SetIcon( static_cast<sal_uInt16>(nIcon) );
285 css::uno::Reference< css::frame::XModel > xModel = xController->getModel();
286 OUString aURL;
287 if( xModel.is() )
288 aURL = xModel->getURL();
289 pWorkWindow->SetRepresentedURL( aURL );
291 // <- VCL SYNCHRONIZED
294 void TitleBarUpdate::impl_updateTitle(const css::uno::Reference< css::frame::XFrame >& xFrame)
296 // no window ... no chance to set any title -> return
297 css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow ();
298 if ( ! xWindow.is() )
299 return;
301 css::uno::Reference< css::frame::XTitle > xTitle(xFrame, css::uno::UNO_QUERY);
302 if ( ! xTitle.is() )
303 return;
305 const OUString sTitle = xTitle->getTitle ();
307 // VCL SYNCHRONIZED ->
308 SolarMutexGuard aSolarGuard;
310 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
311 if ( pWindow && ( pWindow->GetType() == WindowType::WORKWINDOW ) )
313 WorkWindow* pWorkWindow = static_cast<WorkWindow*>(pWindow.get());
314 pWorkWindow->SetText( sTitle );
316 // <- VCL SYNCHRONIZED
319 } // namespace framework
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */