Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / services / taskcreatorsrv.cxx
bloba4db7856d30a07b1b09ae06f856e9dfd10f7a823
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/persistentwindowstate.hxx>
21 #include <helper/tagwindowasmodified.hxx>
22 #include <helper/titlebarupdate.hxx>
23 #include <loadenv/targethelper.hxx>
24 #include <taskcreatordefs.hxx>
26 #include <com/sun/star/frame/Frame.hpp>
27 #include <com/sun/star/frame/XFrame2.hpp>
28 #include <com/sun/star/frame/XDesktop.hpp>
29 #include <com/sun/star/awt/Rectangle.hpp>
30 #include <com/sun/star/awt/Toolkit.hpp>
31 #include <com/sun/star/awt/WindowDescriptor.hpp>
32 #include <com/sun/star/awt/WindowAttribute.hpp>
33 #include <com/sun/star/awt/VclWindowPeerAttribute.hpp>
34 #include <com/sun/star/lang/XServiceInfo.hpp>
35 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
37 #include <comphelper/sequenceashashmap.hxx>
38 #include <comphelper/compbase.hxx>
39 #include <cppuhelper/supportsservice.hxx>
40 #include <svtools/colorcfg.hxx>
41 #include <toolkit/helper/vclunohelper.hxx>
42 #include <utility>
43 #include <vcl/svapp.hxx>
44 #include <vcl/window.hxx>
46 using namespace framework;
48 namespace {
50 typedef comphelper::WeakComponentImplHelper<
51 css::lang::XServiceInfo,
52 css::lang::XSingleServiceFactory> TaskCreatorService_BASE;
54 class TaskCreatorService : public TaskCreatorService_BASE
56 private:
58 /** @short the global uno service manager.
59 @descr Must be used to create own needed services.
61 css::uno::Reference< css::uno::XComponentContext > m_xContext;
63 public:
65 explicit TaskCreatorService(css::uno::Reference< css::uno::XComponentContext > xContext);
67 virtual OUString SAL_CALL getImplementationName() override
69 return "com.sun.star.comp.framework.TaskCreator";
72 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
74 return cppu::supportsService(this, ServiceName);
77 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
79 return {"com.sun.star.frame.TaskCreator"};
82 // XSingleServiceFactory
83 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance() override;
85 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArguments(const css::uno::Sequence< css::uno::Any >& lArguments) override;
87 private:
89 css::uno::Reference< css::awt::XWindow > implts_createContainerWindow( const css::uno::Reference< css::awt::XWindow >& xParentWindow ,
90 const css::awt::Rectangle& aPosSize ,
91 bool bTopWindow );
93 void implts_applyDocStyleToWindow(const css::uno::Reference< css::awt::XWindow >& xWindow) const;
95 css::uno::Reference< css::frame::XFrame2 > implts_createFrame( const css::uno::Reference< css::frame::XFrame >& xParentFrame ,
96 const css::uno::Reference< css::awt::XWindow >& xContainerWindow ,
97 const OUString& sName );
99 void implts_establishWindowStateListener( const css::uno::Reference< css::frame::XFrame2 >& xFrame );
100 void implts_establishTitleBarUpdate( const css::uno::Reference< css::frame::XFrame2 >& xFrame );
102 void implts_establishDocModifyListener( const css::uno::Reference< css::frame::XFrame2 >& xFrame );
104 OUString impl_filterNames( const OUString& sName );
107 TaskCreatorService::TaskCreatorService(css::uno::Reference< css::uno::XComponentContext > xContext)
108 : m_xContext (std::move(xContext ))
112 css::uno::Reference< css::uno::XInterface > SAL_CALL TaskCreatorService::createInstance()
114 return createInstanceWithArguments(css::uno::Sequence< css::uno::Any >());
117 css::uno::Reference< css::uno::XInterface > SAL_CALL TaskCreatorService::createInstanceWithArguments(const css::uno::Sequence< css::uno::Any >& lArguments)
119 ::comphelper::SequenceAsHashMap lArgs(lArguments);
121 css::uno::Reference< css::frame::XFrame > xParentFrame = lArgs.getUnpackedValueOrDefault(ARGUMENT_PARENTFRAME , css::uno::Reference< css::frame::XFrame >());
122 OUString sFrameName = lArgs.getUnpackedValueOrDefault(ARGUMENT_FRAMENAME , OUString() );
123 bool bVisible = lArgs.getUnpackedValueOrDefault(ARGUMENT_MAKEVISIBLE , false );
124 bool bCreateTopWindow = lArgs.getUnpackedValueOrDefault(ARGUMENT_CREATETOPWINDOW , true );
125 // only possize=[0,0,0,0] triggers default handling of vcl !
126 css::awt::Rectangle aPosSize = lArgs.getUnpackedValueOrDefault(ARGUMENT_POSSIZE , css::awt::Rectangle(0, 0, 0, 0) );
127 css::uno::Reference< css::awt::XWindow > xContainerWindow = lArgs.getUnpackedValueOrDefault(ARGUMENT_CONTAINERWINDOW , css::uno::Reference< css::awt::XWindow >() );
128 bool bSupportPersistentWindowState = lArgs.getUnpackedValueOrDefault(ARGUMENT_SUPPORTPERSISTENTWINDOWSTATE , false );
129 bool bEnableTitleBarUpdate = lArgs.getUnpackedValueOrDefault(ARGUMENT_ENABLE_TITLEBARUPDATE , true );
130 // If the frame is explicitly requested to be hidden.
131 bool bHidden = lArgs.getUnpackedValueOrDefault(ARGUMENT_HIDDENFORCONVERSION, false);
133 // We use FrameName property to set it as API name of the new created frame later.
134 // But those frame names must be different from the set of special target names as e.g. _blank, _self etcpp !
135 OUString sRightName = impl_filterNames(sFrameName);
137 // if no external frame window was given ... create a new one.
138 if ( ! xContainerWindow.is())
140 css::uno::Reference< css::awt::XWindow > xParentWindow;
141 if (xParentFrame.is())
142 xParentWindow = xParentFrame->getContainerWindow();
144 // Parent has no own window ...
145 // So we have to create a top level window always !
146 if ( ! xParentWindow.is())
147 bCreateTopWindow = true;
149 xContainerWindow = implts_createContainerWindow(xParentWindow, aPosSize, bCreateTopWindow);
152 // #i53630#
153 // Mark all document windows as "special ones", so VCL can bind
154 // special features to it. Because VCL doesn't know anything about documents ...
155 // Note: Doing so it's no longer supported, that e.g. our wizards can use findFrame(_blank)
156 // to create it's previous frames. They must do it manually by using WindowDescriptor+Toolkit!
157 css::uno::Reference< css::frame::XDesktop > xDesktop(xParentFrame, css::uno::UNO_QUERY);
158 bool bTopLevelDocumentWindow = (
159 sRightName.isEmpty() &&
161 (! xParentFrame.is() ) ||
162 ( xDesktop.is() )
165 if (bTopLevelDocumentWindow)
166 implts_applyDocStyleToWindow(xContainerWindow);
167 //------------------->
169 // create the new frame
170 VclPtr<vcl::Window> pContainerWindow = VCLUnoHelper::GetWindow(xContainerWindow);
171 if (pContainerWindow && bHidden)
173 WindowExtendedStyle eStyle = pContainerWindow->GetExtendedStyle();
174 eStyle |= WindowExtendedStyle::DocHidden;
175 pContainerWindow->SetExtendedStyle(eStyle);
177 css::uno::Reference< css::frame::XFrame2 > xFrame = implts_createFrame(xParentFrame, xContainerWindow, sRightName);
179 // special feature:
180 // A special listener will restore pos/size states in case
181 // a component was loaded into the frame first time.
182 if (bSupportPersistentWindowState)
183 implts_establishWindowStateListener(xFrame);
185 // special feature: On Mac we need tagging the window in case
186 // the underlying model was modified.
187 // VCL will ignore our calls in case different platform then Mac
188 // is used ...
189 if (bTopLevelDocumentWindow)
190 implts_establishDocModifyListener (xFrame);
192 // special feature:
193 // A special listener will update title bar (text and icon)
194 // if component of frame will be changed.
195 if (bEnableTitleBarUpdate)
196 implts_establishTitleBarUpdate(xFrame);
198 // Make it visible directly here...
199 // if it's required from outside.
200 if (bVisible)
201 xContainerWindow->setVisible(bVisible);
203 return css::uno::Reference< css::uno::XInterface >(xFrame, css::uno::UNO_QUERY_THROW);
206 void TaskCreatorService::implts_applyDocStyleToWindow(const css::uno::Reference< css::awt::XWindow >& xWindow) const
208 // SYNCHRONIZED ->
209 SolarMutexGuard aSolarGuard;
210 VclPtr<vcl::Window> pVCLWindow = VCLUnoHelper::GetWindow(xWindow);
211 if (pVCLWindow)
212 pVCLWindow->SetExtendedStyle(WindowExtendedStyle::Document);
213 // <- SYNCHRONIZED
216 css::uno::Reference< css::awt::XWindow > TaskCreatorService::implts_createContainerWindow( const css::uno::Reference< css::awt::XWindow >& xParentWindow ,
217 const css::awt::Rectangle& aPosSize ,
218 bool bTopWindow )
220 // get toolkit to create task container window
221 css::uno::Reference< css::awt::XToolkit2 > xToolkit = css::awt::Toolkit::create( m_xContext );
223 // Check if child frames can be created really. We need at least a valid window at the parent frame ...
224 css::uno::Reference< css::awt::XWindowPeer > xParentWindowPeer;
225 if ( ! bTopWindow)
227 if ( ! xParentWindow.is())
228 bTopWindow = false;
229 else
230 xParentWindowPeer.set(xParentWindow, css::uno::UNO_QUERY_THROW);
233 // describe window properties.
234 css::awt::WindowDescriptor aDescriptor;
235 if (bTopWindow)
237 aDescriptor.Type = css::awt::WindowClass_TOP;
238 aDescriptor.WindowServiceName = "window";
239 aDescriptor.ParentIndex = -1;
240 aDescriptor.Parent.clear();
241 aDescriptor.Bounds = aPosSize;
242 aDescriptor.WindowAttributes = css::awt::WindowAttribute::BORDER |
243 css::awt::WindowAttribute::MOVEABLE |
244 css::awt::WindowAttribute::SIZEABLE |
245 css::awt::WindowAttribute::CLOSEABLE |
246 css::awt::VclWindowPeerAttribute::CLIPCHILDREN;
248 else
250 aDescriptor.Type = css::awt::WindowClass_TOP;
251 aDescriptor.WindowServiceName = "dockingwindow";
252 aDescriptor.ParentIndex = 1;
253 aDescriptor.Parent = xParentWindowPeer;
254 aDescriptor.Bounds = aPosSize;
255 aDescriptor.WindowAttributes = css::awt::VclWindowPeerAttribute::CLIPCHILDREN;
258 // create a new blank container window and get access to parent container to append new created task.
259 css::uno::Reference< css::awt::XWindowPeer > xPeer = xToolkit->createWindow( aDescriptor );
260 css::uno::Reference< css::awt::XWindow > xWindow ( xPeer, css::uno::UNO_QUERY_THROW );
262 sal_Int32 nBackground = 0xffffffff;
264 if (bTopWindow)
268 nBackground = sal_Int32(::svtools::ColorConfig().GetColorValue(::svtools::APPBACKGROUND).nColor);
270 catch (const css::uno::Exception &)
272 // Ignore
275 xPeer->setBackground(nBackground);
277 return xWindow;
280 css::uno::Reference< css::frame::XFrame2 > TaskCreatorService::implts_createFrame( const css::uno::Reference< css::frame::XFrame >& xParentFrame ,
281 const css::uno::Reference< css::awt::XWindow >& xContainerWindow,
282 const OUString& sName )
284 // create new frame.
285 css::uno::Reference< css::frame::XFrame2 > xNewFrame = css::frame::Frame::create( m_xContext );
287 // Set window on frame.
288 // Do it before calling any other interface methods ...
289 // The new created frame must be initialized before you can do anything else there.
290 xNewFrame->initialize( xContainerWindow );
292 // Put frame to the frame tree.
293 // Note: The property creator/parent will be set on the new putted frame automatically ... by the parent container.
294 if (xParentFrame.is())
296 css::uno::Reference< css::frame::XFramesSupplier > xSupplier (xParentFrame, css::uno::UNO_QUERY_THROW);
297 css::uno::Reference< css::frame::XFrames > xContainer = xSupplier->getFrames();
298 xContainer->append( css::uno::Reference<css::frame::XFrame>(xNewFrame, css::uno::UNO_QUERY_THROW) );
301 // Set it's API name (if there is one from outside)
302 if (!sName.isEmpty())
303 xNewFrame->setName( sName );
305 return xNewFrame;
308 void TaskCreatorService::implts_establishWindowStateListener( const css::uno::Reference< css::frame::XFrame2 >& xFrame )
310 // Special feature: It's allowed for frames using a top level window only!
311 // We must create a special listener service and couple it with the new created task frame.
312 // He will restore or save the window state of it ...
313 // See used classes for further information too.
314 rtl::Reference<PersistentWindowState> pPersistentStateHandler = new PersistentWindowState( m_xContext );
316 css::uno::Sequence< css::uno::Any > lInitData{ css::uno::Any(xFrame) };
317 pPersistentStateHandler->initialize(lInitData);
320 void TaskCreatorService::implts_establishDocModifyListener( const css::uno::Reference< css::frame::XFrame2 >& xFrame )
322 // Special feature: It's allowed for frames using a top level window only!
323 // We must create a special listener service and couple it with the new created task frame.
324 // It will tag the window as modified if the underlying model was modified ...
325 rtl::Reference<TagWindowAsModified> pTag = new TagWindowAsModified();
327 css::uno::Sequence< css::uno::Any > lInitData{ css::uno::Any(xFrame) };
328 pTag->initialize(lInitData);
331 void TaskCreatorService::implts_establishTitleBarUpdate( const css::uno::Reference< css::frame::XFrame2 >& xFrame )
333 rtl::Reference<TitleBarUpdate> pHelper = new TitleBarUpdate (m_xContext);
335 css::uno::Sequence< css::uno::Any > lInitData{ css::uno::Any(xFrame) };
336 pHelper->initialize(lInitData);
339 OUString TaskCreatorService::impl_filterNames( const OUString& sName )
341 OUString sFiltered;
342 if (TargetHelper::isValidNameForFrame(sName))
343 sFiltered = sName;
344 return sFiltered;
349 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
350 com_sun_star_comp_framework_TaskCreator_get_implementation(
351 css::uno::XComponentContext *context,
352 css::uno::Sequence<css::uno::Any> const &)
354 return cppu::acquire(new TaskCreatorService(context));
357 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */