LibreOffice Module framework (master) 1
taskcreatorsrv.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
24#include <taskcreatordefs.hxx>
25
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>
36
40#include <svtools/colorcfg.hxx>
42#include <utility>
43#include <vcl/svapp.hxx>
44#include <vcl/window.hxx>
45
46using namespace framework;
47
48namespace {
49
51 css::lang::XServiceInfo,
52 css::lang::XSingleServiceFactory> TaskCreatorService_BASE;
53
54class TaskCreatorService : public TaskCreatorService_BASE
55{
56private:
57
61 css::uno::Reference< css::uno::XComponentContext > m_xContext;
62
63public:
64
65 explicit TaskCreatorService(css::uno::Reference< css::uno::XComponentContext > xContext);
66
67 virtual OUString SAL_CALL getImplementationName() override
68 {
69 return "com.sun.star.comp.framework.TaskCreator";
70 }
71
72 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
73 {
74 return cppu::supportsService(this, ServiceName);
75 }
76
77 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
78 {
79 return {"com.sun.star.frame.TaskCreator"};
80 }
81
82 // XSingleServiceFactory
83 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance() override;
84
85 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArguments(const css::uno::Sequence< css::uno::Any >& lArguments) override;
86
87private:
88
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 );
92
93 void implts_applyDocStyleToWindow(const css::uno::Reference< css::awt::XWindow >& xWindow) const;
94
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 );
98
99 void implts_establishWindowStateListener( const css::uno::Reference< css::frame::XFrame2 >& xFrame );
100 void implts_establishTitleBarUpdate( const css::uno::Reference< css::frame::XFrame2 >& xFrame );
101
102 void implts_establishDocModifyListener( const css::uno::Reference< css::frame::XFrame2 >& xFrame );
103
104 OUString impl_filterNames( const OUString& sName );
105};
106
107TaskCreatorService::TaskCreatorService(css::uno::Reference< css::uno::XComponentContext > xContext)
108 : m_xContext (std::move(xContext ))
109{
110}
111
112css::uno::Reference< css::uno::XInterface > SAL_CALL TaskCreatorService::createInstance()
113{
114 return createInstanceWithArguments(css::uno::Sequence< css::uno::Any >());
115}
116
117css::uno::Reference< css::uno::XInterface > SAL_CALL TaskCreatorService::createInstanceWithArguments(const css::uno::Sequence< css::uno::Any >& lArguments)
118{
119 ::comphelper::SequenceAsHashMap lArgs(lArguments);
120
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);
132
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);
136
137 // if no external frame window was given ... create a new one.
138 if ( ! xContainerWindow.is())
139 {
140 css::uno::Reference< css::awt::XWindow > xParentWindow;
141 if (xParentFrame.is())
142 xParentWindow = xParentFrame->getContainerWindow();
143
144 // Parent has no own window ...
145 // So we have to create a top level window always !
146 if ( ! xParentWindow.is())
147 bCreateTopWindow = true;
148
149 xContainerWindow = implts_createContainerWindow(xParentWindow, aPosSize, bCreateTopWindow);
150 }
151
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() &&
160 (
161 (! xParentFrame.is() ) ||
162 ( xDesktop.is() )
163 )
164 );
165 if (bTopLevelDocumentWindow)
166 implts_applyDocStyleToWindow(xContainerWindow);
167 //------------------->
168
169 // create the new frame
170 VclPtr<vcl::Window> pContainerWindow = VCLUnoHelper::GetWindow(xContainerWindow);
171 if (pContainerWindow && bHidden)
172 {
173 WindowExtendedStyle eStyle = pContainerWindow->GetExtendedStyle();
174 eStyle |= WindowExtendedStyle::DocHidden;
175 pContainerWindow->SetExtendedStyle(eStyle);
176 }
177 css::uno::Reference< css::frame::XFrame2 > xFrame = implts_createFrame(xParentFrame, xContainerWindow, sRightName);
178
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);
184
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);
191
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);
197
198 // Make it visible directly here...
199 // if it's required from outside.
200 if (bVisible)
201 xContainerWindow->setVisible(bVisible);
202
203 return css::uno::Reference< css::uno::XInterface >(xFrame, css::uno::UNO_QUERY_THROW);
204}
205
206void TaskCreatorService::implts_applyDocStyleToWindow(const css::uno::Reference< css::awt::XWindow >& xWindow) const
207{
208 // SYNCHRONIZED ->
209 SolarMutexGuard aSolarGuard;
210 VclPtr<vcl::Window> pVCLWindow = VCLUnoHelper::GetWindow(xWindow);
211 if (pVCLWindow)
212 pVCLWindow->SetExtendedStyle(WindowExtendedStyle::Document);
213 // <- SYNCHRONIZED
214}
215
216css::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 )
219{
220 // get toolkit to create task container window
221 css::uno::Reference< css::awt::XToolkit2 > xToolkit = css::awt::Toolkit::create( m_xContext );
222
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)
226 {
227 if ( ! xParentWindow.is())
228 bTopWindow = false;
229 else
230 xParentWindowPeer.set(xParentWindow, css::uno::UNO_QUERY_THROW);
231 }
232
233 // describe window properties.
234 css::awt::WindowDescriptor aDescriptor;
235 if (bTopWindow)
236 {
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;
247 }
248 else
249 {
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;
256 }
257
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 );
261
262 sal_Int32 nBackground = 0xffffffff;
263
264 if (bTopWindow)
265 {
266 try
267 {
268 nBackground = sal_Int32(::svtools::ColorConfig().GetColorValue(::svtools::APPBACKGROUND).nColor);
269 }
270 catch (const css::uno::Exception &)
271 {
272 // Ignore
273 }
274 }
275 xPeer->setBackground(nBackground);
276
277 return xWindow;
278}
279
280css::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 )
283{
284 // create new frame.
285 css::uno::Reference< css::frame::XFrame2 > xNewFrame = css::frame::Frame::create( m_xContext );
286
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 );
291
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())
295 {
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) );
299 }
300
301 // Set it's API name (if there is one from outside)
302 if (!sName.isEmpty())
303 xNewFrame->setName( sName );
304
305 return xNewFrame;
306}
307
308void TaskCreatorService::implts_establishWindowStateListener( const css::uno::Reference< css::frame::XFrame2 >& xFrame )
309{
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.
315
316 css::uno::Sequence< css::uno::Any > lInitData{ css::uno::Any(xFrame) };
317 pPersistentStateHandler->initialize(lInitData);
318}
319
320void TaskCreatorService::implts_establishDocModifyListener( const css::uno::Reference< css::frame::XFrame2 >& xFrame )
321{
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 ...
326
327 css::uno::Sequence< css::uno::Any > lInitData{ css::uno::Any(xFrame) };
328 pTag->initialize(lInitData);
329}
330
331void TaskCreatorService::implts_establishTitleBarUpdate( const css::uno::Reference< css::frame::XFrame2 >& xFrame )
332{
334
335 css::uno::Sequence< css::uno::Any > lInitData{ css::uno::Any(xFrame) };
336 pHelper->initialize(lInitData);
337}
338
339OUString TaskCreatorService::impl_filterNames( const OUString& sName )
340{
341 OUString sFiltered;
342 if (TargetHelper::isValidNameForFrame(sName))
343 sFiltered = sName;
344 return sFiltered;
345}
346
347}
348
349extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
351 css::uno::XComponentContext *context,
352 css::uno::Sequence<css::uno::Any> const &)
353{
354 return cppu::acquire(new TaskCreatorService(context));
355}
356
357/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
HRESULT createInstance(REFIID iid, Ifc **ppIfc)
constexpr OUStringLiteral sFrameName
static vcl::Window * GetWindow(const css::uno::Reference< css::awt::XWindow > &rxWindow)
OUString sName
css::uno::Reference< css::uno::XComponentContext > m_xContext
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Reference< XFrame > xFrame
constexpr OUStringLiteral ARGUMENT_PARENTFRAME
[XFrame] if it's set, it will be used as parent frame for the new created frame.
constexpr OUStringLiteral ARGUMENT_ENABLE_TITLEBARUPDATE
[bool] enable/disable special mode, where the title bar of our the new created frame will be updated ...
constexpr OUStringLiteral ARGUMENT_POSSIZE
[Rectangle] Place the new created frame on this place and resize the container window.
constexpr OUStringLiteral ARGUMENT_CONTAINERWINDOW
[XWindow] an outside created window, used as container window of the new created frame.
constexpr OUStringLiteral ARGUMENT_FRAMENAME
[OUString] if it's not a special name (beginning with "_" ... which are not allowed here!...
constexpr OUStringLiteral ARGUMENT_CREATETOPWINDOW
[bool] If not "ContainerWindow" property is set it force creation of a top level window as new contai...
constexpr OUStringLiteral ARGUMENT_HIDDENFORCONVERSION
[bool] enable/disable if the frame is explicitly requested to be hidden Default = OFF !
constexpr OUStringLiteral ARGUMENT_MAKEVISIBLE
[bool] If it's set to true we will make the new created frame visible.
constexpr OUStringLiteral ARGUMENT_SUPPORTPERSISTENTWINDOWSTATE
[bool] enable/disable special mode, where this frame will be part of the persistent window state feat...
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_framework_TaskCreator_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
bool bVisible
unsigned char sal_Bool
WindowExtendedStyle