fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / framework / source / helper / titlebarupdate.cxx
blobb79b5b2bd92136e5c2e8b111b7e4c8e4dea79c79
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 <macros/generic.hxx>
24 #include <services.h>
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>
48 namespace framework{
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 )
55 , m_xFrame ( )
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)
67 // check arguments
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),
73 1);
75 lArguments[0] >>= xFrame;
76 if (!xFrame.is())
77 throw css::lang::IllegalArgumentException(
78 "No valid frame specified!",
79 static_cast< ::cppu::OWeakObject* >(this),
80 1);
83 SolarMutexGuard g;
84 // hold the frame as weak reference(!) so it can die everytimes :-)
85 m_xFrame = xFrame;
88 // start listening
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.
101 if (
102 (aEvent.Action == css::frame::FrameAction_COMPONENT_ATTACHED ) ||
103 (aEvent.Action == css::frame::FrameAction_COMPONENT_REATTACHED) ||
104 (aEvent.Action == css::frame::FrameAction_COMPONENT_DETACHING )
107 impl_forceUpdate ();
111 void SAL_CALL TitleBarUpdate::titleChanged(const css::frame::TitleChangedEvent& /* aEvent */)
112 throw (css::uno::RuntimeException, std::exception)
114 impl_forceUpdate ();
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() )
130 return;
132 OUString sApplicationID;
134 #if !defined(MACOSX)
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";
154 else
155 sDesktopName = "Startcenter";
156 #if defined(WNT)
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;
160 #else
161 sApplicationID = utl::ConfigManager::getProductName().toAsciiLowerCase();
162 sApplicationID += "-";
163 sApplicationID += sDesktopName.toAsciiLowerCase();
164 #endif
166 catch(const css::uno::Exception&)
169 #endif
171 // VCL SYNCHRONIZED ->
172 SolarMutexGuard aSolarGuard;
174 vcl::Window* pWindow = (VCLUnoHelper::GetWindow( xWindow ));
175 if (
176 ( pWindow ) &&
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,
187 TModuleInfo& rInfo )
189 if ( ! xFrame.is ())
190 return false;
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();
206 return bSuccess;
208 catch(const css::uno::Exception&)
211 return false;
214 void TitleBarUpdate::impl_forceUpdate()
216 css::uno::Reference< css::frame::XFrame > xFrame;
218 SolarMutexGuard g;
219 xFrame.set(m_xFrame.get(), css::uno::UNO_QUERY);
222 // frame already gone ? We hold it weak only ...
223 if ( ! xFrame.is())
224 return;
226 // no window -> no chance to set/update title and icon
227 css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow();
228 if ( ! xWindow.is())
229 return;
231 impl_updateIcon (xFrame);
232 impl_updateTitle (xFrame);
233 #if !defined(MACOSX)
234 impl_updateApplicationID (xFrame);
235 #endif
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 ();
243 if (
244 ( ! xController.is() ) ||
245 ( ! xWindow.is() )
247 return;
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 );
256 if ( xSet.is() )
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 )
274 TModuleInfo aInfo;
275 if (implst_getModuleInfo(xFrame, aInfo))
276 nIcon = aInfo.nIcon;
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 ));
292 if (
293 ( pWindow ) &&
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();
301 OUString aURL;
302 if( xModel.is() )
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() )
314 return;
316 css::uno::Reference< css::frame::XTitle > xTitle(xFrame, css::uno::UNO_QUERY);
317 if ( ! xTitle.is() )
318 return;
320 const OUString sTitle = xTitle->getTitle ();
322 // VCL SYNCHRONIZED ->
323 SolarMutexGuard aSolarGuard;
325 vcl::Window* pWindow = (VCLUnoHelper::GetWindow( xWindow ));
326 if (
327 ( pWindow ) &&
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: */