LibreOffice Module framework (master) 1
titlebarupdate.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21
22#include <properties.h>
23
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>
30
33#include <utility>
34#include <vcl/window.hxx>
36#include <vcl/svapp.hxx>
37#include <vcl/wrkwin.hxx>
39
40namespace framework{
41
42const ::sal_Int32 INVALID_ICON_ID = -1;
43const ::sal_Int32 DEFAULT_ICON_ID = 0;
44
45TitleBarUpdate::TitleBarUpdate(css::uno::Reference< css::uno::XComponentContext > xContext)
46 : m_xContext (std::move(xContext ))
47{
48}
49
51{
52}
53
54void SAL_CALL TitleBarUpdate::initialize(const css::uno::Sequence< css::uno::Any >& lArguments)
55{
56 // check arguments
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),
62 1);
63
64 lArguments[0] >>= xFrame;
65 if (!xFrame.is())
66 throw css::lang::IllegalArgumentException(
67 "No valid frame specified!",
68 static_cast< ::cppu::OWeakObject* >(this),
69 1);
70
71 {
73 // hold the frame as weak reference(!) so it can die everytimes :-)
75 }
76
77 // start listening
78 xFrame->addFrameActionListener(this);
79
80 css::uno::Reference< css::frame::XTitleChangeBroadcaster > xBroadcaster(xFrame, css::uno::UNO_QUERY);
81 if (xBroadcaster.is ())
82 xBroadcaster->addTitleChangeListener (this);
83}
84
85void SAL_CALL TitleBarUpdate::frameAction(const css::frame::FrameActionEvent& aEvent)
86{
87 // we are interested on events only, which must trigger a title bar update
88 // because component was changed.
89 if (
90 (aEvent.Action == css::frame::FrameAction_COMPONENT_ATTACHED ) ||
91 (aEvent.Action == css::frame::FrameAction_COMPONENT_REATTACHED) ||
92 (aEvent.Action == css::frame::FrameAction_COMPONENT_DETACHING )
93 )
94 {
96 }
97}
98
99void SAL_CALL TitleBarUpdate::titleChanged(const css::frame::TitleChangedEvent& /* aEvent */)
100{
102}
103
104void SAL_CALL TitleBarUpdate::disposing(const css::lang::EventObject&)
105{
106 css::uno::Reference< css::frame::XFrame > xFrame(m_xFrame.get(), css::uno::UNO_QUERY);
107 if (xFrame.is())
108 xFrame->removeFrameActionListener(this);
109
110 // nothing todo here - because we hold the frame as weak reference only
111}
112
113//http://live.gnome.org/GnomeShell/ApplicationBased
114//http://msdn.microsoft.com/en-us/library/dd378459(v=VS.85).aspx
115void TitleBarUpdate::impl_updateApplicationID(const css::uno::Reference< css::frame::XFrame >& xFrame)
116{
117 css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow ();
118 if ( ! xWindow.is() )
119 return;
120
121#if !defined(MACOSX)
122 OUString sApplicationID;
123 try
124 {
125 css::uno::Reference< css::frame::XModuleManager2 > xModuleManager =
126 css::frame::ModuleManager::create( m_xContext );
127
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";
142 else
143 sDesktopName = "Startcenter";
144#if defined(_WIN32)
145 // We use a hardcoded product name matching the registry keys so applications can be associated with file types
146 sApplicationID = "TheDocumentFoundation.LibreOffice." + sDesktopName;
147#else
148 sApplicationID = utl::ConfigManager::getProductName().toAsciiLowerCase() + "-" + sDesktopName.toAsciiLowerCase();
149#endif
150 }
151 catch(const css::uno::Exception&)
152 {
153 }
154#else
155 OUString const sApplicationID;
156#endif
157
158 // VCL SYNCHRONIZED ->
159 SolarMutexGuard aSolarGuard;
160
161 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
162 if ( pWindow && pWindow->GetType() == WindowType::WORKWINDOW )
163 {
164 WorkWindow* pWorkWindow = static_cast<WorkWindow*>(pWindow.get());
165 pWorkWindow->SetApplicationID( sApplicationID );
166 }
167 // <- VCL SYNCHRONIZED
168}
169
170bool TitleBarUpdate::implst_getModuleInfo(const css::uno::Reference< css::frame::XFrame >& xFrame,
171 TModuleInfo& rInfo )
172{
173 if ( ! xFrame.is ())
174 return false;
175
176 try
177 {
178 css::uno::Reference< css::frame::XModuleManager2 > xModuleManager =
179 css::frame::ModuleManager::create( m_xContext );
180
181 rInfo.sID = xModuleManager->identify(xFrame);
182 ::comphelper::SequenceAsHashMap lProps = xModuleManager->getByName (rInfo.sID);
183
185
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();
189 return bSuccess;
190 }
191 catch(const css::uno::Exception&)
192 {}
193
194 return false;
195}
196
198{
199 css::uno::Reference< css::frame::XFrame > xFrame;
200 {
202 xFrame.set(m_xFrame.get(), css::uno::UNO_QUERY);
203 }
204
205 // frame already gone ? We hold it weak only ...
206 if ( ! xFrame.is())
207 return;
208
209 // no window -> no chance to set/update title and icon
210 css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow();
211 if ( ! xWindow.is())
212 return;
213
216#if !defined(MACOSX)
218#endif
219}
220
221void TitleBarUpdate::impl_updateIcon(const css::uno::Reference< css::frame::XFrame >& xFrame)
222{
223 css::uno::Reference< css::frame::XController > xController = xFrame->getController ();
224 css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow ();
225
226 if (
227 ( ! xController.is() ) ||
228 ( ! xWindow.is() )
229 )
230 return;
231
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;
235
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 );
239 if ( xSet.is() )
240 {
241 try
242 {
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;
246 }
247 catch(const css::uno::Exception&)
248 {
250 }
251 }
252
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 )
256 {
257 TModuleInfo aInfo;
258 if (implst_getModuleInfo(xFrame, aInfo))
259 nIcon = aInfo.nIcon;
260 }
261
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;
266
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!!!
270
271 // VCL SYNCHRONIZED ->
272 SolarMutexGuard aSolarGuard;
273
274 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
275 if ( pWindow && ( pWindow->GetType() == WindowType::WORKWINDOW ) )
276 {
277 WorkWindow* pWorkWindow = static_cast<WorkWindow*>(pWindow.get());
278 pWorkWindow->SetIcon( static_cast<sal_uInt16>(nIcon) );
279
280 css::uno::Reference< css::frame::XModel > xModel = xController->getModel();
281 OUString aURL;
282 if( xModel.is() )
283 aURL = xModel->getURL();
284 pWorkWindow->SetRepresentedURL( aURL );
285 }
286 // <- VCL SYNCHRONIZED
287}
288
289void TitleBarUpdate::impl_updateTitle(const css::uno::Reference< css::frame::XFrame >& xFrame)
290{
291 // no window ... no chance to set any title -> return
292 css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow ();
293 if ( ! xWindow.is() )
294 return;
295
296 css::uno::Reference< css::frame::XTitle > xTitle(xFrame, css::uno::UNO_QUERY);
297 if ( ! xTitle.is() )
298 return;
299
300 const OUString sTitle = xTitle->getTitle ();
301
302 // VCL SYNCHRONIZED ->
303 SolarMutexGuard aSolarGuard;
304
305 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
306 if ( pWindow && ( pWindow->GetType() == WindowType::WORKWINDOW ) )
307 {
308 WorkWindow* pWorkWindow = static_cast<WorkWindow*>(pWindow.get());
309 pWorkWindow->SetText( sTitle );
310 }
311 // <- VCL SYNCHRONIZED
312}
313
314} // namespace framework
315
316/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
virtual void SetText(const OUString &rStr) override
void SetIcon(sal_uInt16 nIcon)
void SetRepresentedURL(const OUString &)
void SetApplicationID(const OUString &rApplicationID)
static vcl::Window * GetWindow(const css::uno::Reference< css::awt::XWindow > &rxWindow)
reference_type * get() const
TValueType getUnpackedValueOrDefault(const OUString &sKey, const TValueType &aDefault) const
css::uno::WeakReference< css::frame::XFrame > m_xFrame
reference to the frame which was created by the office himself
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &lArguments) override
css::uno::Reference< css::uno::XComponentContext > m_xContext
may we need a uno service manager to create own services
void impl_updateTitle(const css::uno::Reference< css::frame::XFrame > &xFrame)
gets the current title from the frame and set it on the window.
virtual void SAL_CALL titleChanged(const css::frame::TitleChangedEvent &aEvent) override
virtual ~TitleBarUpdate() override
virtual void SAL_CALL frameAction(const css::frame::FrameActionEvent &aEvent) override
virtual void SAL_CALL disposing(const css::lang::EventObject &aEvent) override
void impl_updateApplicationID(const css::uno::Reference< css::frame::XFrame > &xFrame)
void impl_forceUpdate()
set a new icon and title on the title bar of our connected frame window.
bool implst_getModuleInfo(const css::uno::Reference< css::frame::XFrame > &xFrame, TModuleInfo &rInfo)
identify the application module, which is used behind the component of our frame.
void impl_updateIcon(const css::uno::Reference< css::frame::XFrame > &xFrame)
identify the current component (inside the connected frame) and set the right module icon on the titl...
TitleBarUpdate(css::uno::Reference< css::uno::XComponentContext > xContext)
static OUString getProductName()
#define DBG_UNHANDLED_EXCEPTION(...)
URL aURL
css::uno::Reference< css::uno::XComponentContext > m_xContext
const ::sal_Int32 INVALID_ICON_ID
constexpr OUStringLiteral OFFICEFACTORY_PROPNAME_ASCII_ICON
Definition: properties.h:93
const ::sal_Int32 DEFAULT_ICON_ID
::sal_Int32 nIcon
configured icon for this module
OUString sID
internal id of this module
Reference< XController > xController
Reference< XFrame > xFrame
Reference< XModel > xModel