bump product version to 4.1.6.2
[LibreOffice.git] / framework / source / services / taskcreatorsrv.cxx
blob1231066268168074b883432ee4ce6c12b836479a
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 "services/taskcreatorsrv.hxx"
22 #include <helper/persistentwindowstate.hxx>
23 #include <helper/tagwindowasmodified.hxx>
24 #include <helper/titlebarupdate.hxx>
25 #include <threadhelp/readguard.hxx>
26 #include <threadhelp/writeguard.hxx>
27 #include <loadenv/targethelper.hxx>
28 #include <services.h>
30 #include <com/sun/star/frame/Frame.hpp>
31 #include <com/sun/star/frame/XController.hpp>
32 #include <com/sun/star/frame/XModel.hpp>
33 #include <com/sun/star/frame/XDesktop.hpp>
34 #include <com/sun/star/awt/Toolkit.hpp>
35 #include <com/sun/star/awt/XTopWindow.hpp>
36 #include <com/sun/star/awt/WindowDescriptor.hpp>
37 #include <com/sun/star/awt/WindowAttribute.hpp>
38 #include <com/sun/star/awt/VclWindowPeerAttribute.hpp>
40 #include <svtools/colorcfg.hxx>
41 #include <vcl/svapp.hxx>
43 #include <toolkit/unohlp.hxx>
44 #include <vcl/window.hxx>
46 //_______________________________________________
47 // namespaces
49 namespace framework
51 //-----------------------------------------------
52 DEFINE_XINTERFACE_3(TaskCreatorService ,
53 OWeakObject ,
54 DIRECT_INTERFACE(css::lang::XTypeProvider ),
55 DIRECT_INTERFACE(css::lang::XServiceInfo ),
56 DIRECT_INTERFACE(css::lang::XSingleServiceFactory))
58 //-----------------------------------------------
59 DEFINE_XTYPEPROVIDER_3(TaskCreatorService ,
60 css::lang::XTypeProvider ,
61 css::lang::XServiceInfo ,
62 css::lang::XSingleServiceFactory)
64 //-----------------------------------------------
65 DEFINE_XSERVICEINFO_ONEINSTANCESERVICE(TaskCreatorService ,
66 ::cppu::OWeakObject ,
67 "com.sun.star.frame.TaskCreator",
68 IMPLEMENTATIONNAME_FWK_TASKCREATOR)
70 //-----------------------------------------------
71 DEFINE_INIT_SERVICE(
72 TaskCreatorService,
74 /*Attention
75 I think we don't need any mutex or lock here ... because we are called by our own static method impl_createInstance()
76 to create a new instance of this class by our own supported service factory.
77 see macro DEFINE_XSERVICEINFO_MULTISERVICE and "impl_initService()" for further information!
82 //-----------------------------------------------
83 TaskCreatorService::TaskCreatorService(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
84 : ThreadHelpBase (&Application::GetSolarMutex())
85 , ::cppu::OWeakObject( )
86 , m_xSMGR (xSMGR )
90 //-----------------------------------------------
91 TaskCreatorService::~TaskCreatorService()
95 //-----------------------------------------------
96 css::uno::Reference< css::uno::XInterface > SAL_CALL TaskCreatorService::createInstance()
97 throw(css::uno::Exception ,
98 css::uno::RuntimeException)
100 return createInstanceWithArguments(css::uno::Sequence< css::uno::Any >());
103 //-----------------------------------------------
104 css::uno::Reference< css::uno::XInterface > SAL_CALL TaskCreatorService::createInstanceWithArguments(const css::uno::Sequence< css::uno::Any >& lArguments)
105 throw(css::uno::Exception ,
106 css::uno::RuntimeException)
108 ::comphelper::SequenceAsHashMap lArgs(lArguments);
110 css::uno::Reference< css::frame::XFrame > xParentFrame = lArgs.getUnpackedValueOrDefault(OUString(ARGUMENT_PARENTFRAME) , css::uno::Reference< css::frame::XFrame >());
111 OUString sFrameName = lArgs.getUnpackedValueOrDefault(OUString(ARGUMENT_FRAMENAME) , OUString() );
112 sal_Bool bVisible = lArgs.getUnpackedValueOrDefault(OUString(ARGUMENT_MAKEVISIBLE) , sal_False );
113 sal_Bool bCreateTopWindow = lArgs.getUnpackedValueOrDefault(OUString(ARGUMENT_CREATETOPWINDOW) , sal_True );
114 // only possize=[0,0,0,0] triggers default handling of vcl !
115 css::awt::Rectangle aPosSize = lArgs.getUnpackedValueOrDefault(OUString(ARGUMENT_POSSIZE) , css::awt::Rectangle(0, 0, 0, 0) );
116 css::uno::Reference< css::awt::XWindow > xContainerWindow = lArgs.getUnpackedValueOrDefault(OUString(ARGUMENT_CONTAINERWINDOW) , css::uno::Reference< css::awt::XWindow >() );
117 sal_Bool bSupportPersistentWindowState = lArgs.getUnpackedValueOrDefault(OUString(ARGUMENT_SUPPORTPERSISTENTWINDOWSTATE) , sal_False );
118 sal_Bool bEnableTitleBarUpdate = lArgs.getUnpackedValueOrDefault(OUString(ARGUMENT_ENABLE_TITLEBARUPDATE) , sal_True );
120 /* SAFE { */
121 ReadGuard aReadLock( m_aLock );
122 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
123 aReadLock.unlock();
124 /* } SAFE */
126 // We use FrameName property to set it as API name of the new created frame later.
127 // But those frame names must be different from the set of special target names as e.g. _blank, _self etcpp !
128 OUString sRightName = impl_filterNames(sFrameName);
130 // if no external frame window was given ... create a new one.
131 if ( ! xContainerWindow.is())
133 css::uno::Reference< css::awt::XWindow > xParentWindow;
134 if (xParentFrame.is())
135 xParentWindow = xParentFrame->getContainerWindow();
137 // Parent has no own window ...
138 // So we have to create a top level window always !
139 if ( ! xParentWindow.is())
140 bCreateTopWindow = sal_True;
142 xContainerWindow = implts_createContainerWindow(xParentWindow, aPosSize, bCreateTopWindow);
145 // #i53630#
146 // Mark all document windows as "special ones", so VCL can bind
147 // special features to it. Because VCL doesnt know anything about documents ...
148 // Note: Doing so it's no longer supported, that e.g. our wizards can use findFrame(_blank)
149 // to create it's previes frames. They must do it manually by using WindowDescriptor+Toolkit!
150 css::uno::Reference< css::frame::XDesktop > xDesktop(xParentFrame, css::uno::UNO_QUERY);
151 ::sal_Bool bTopLevelDocumentWindow = (
152 sRightName.isEmpty() &&
154 (! xParentFrame.is() ) ||
155 ( xDesktop.is() )
158 if (bTopLevelDocumentWindow)
159 implts_applyDocStyleToWindow(xContainerWindow);
160 //------------------->
162 // create the new frame
163 css::uno::Reference< css::frame::XFrame2 > xFrame = implts_createFrame(xParentFrame, xContainerWindow, sRightName);
165 // special freature:
166 // A special listener will restore pos/size states in case
167 // a component was loaded into the frame first time.
168 if (bSupportPersistentWindowState)
169 implts_establishWindowStateListener(xFrame);
171 // special feature: On Mac we need tagging the window in case
172 // the underlying model was modified.
173 // VCL will ignore our calls in case different platform then Mac
174 // is used ...
175 if (bTopLevelDocumentWindow)
176 implts_establishDocModifyListener (xFrame);
178 // special freature:
179 // A special listener will update title bar (text and icon)
180 // if component of frame will be changed.
181 if (bEnableTitleBarUpdate)
182 implts_establishTitleBarUpdate(xFrame);
184 // Make it visible directly here ...
185 // if its required from outside.
186 if (bVisible)
187 xContainerWindow->setVisible(bVisible);
189 return css::uno::Reference< css::uno::XInterface >(xFrame, css::uno::UNO_QUERY_THROW);
192 //-----------------------------------------------
193 void TaskCreatorService::implts_applyDocStyleToWindow(const css::uno::Reference< css::awt::XWindow >& xWindow) const
195 // SYNCHRONIZED ->
196 SolarMutexGuard aSolarGuard;
197 Window* pVCLWindow = VCLUnoHelper::GetWindow(xWindow);
198 if (pVCLWindow)
199 pVCLWindow->SetExtendedStyle(WB_EXT_DOCUMENT);
200 // <- SYNCHRONIZED
203 //-----------------------------------------------
204 css::uno::Reference< css::awt::XWindow > TaskCreatorService::implts_createContainerWindow( const css::uno::Reference< css::awt::XWindow >& xParentWindow ,
205 const css::awt::Rectangle& aPosSize ,
206 sal_Bool bTopWindow )
208 // SAFE ->
209 ReadGuard aReadLock( m_aLock );
210 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
211 aReadLock.unlock();
212 // <- SAFE
214 // get toolkit to create task container window
215 css::uno::Reference< css::awt::XToolkit2 > xToolkit = css::awt::Toolkit::create( comphelper::getComponentContext(xSMGR) );
217 // Check if child frames can be created realy. We need at least a valid window at the parent frame ...
218 css::uno::Reference< css::awt::XWindowPeer > xParentWindowPeer;
219 if ( ! bTopWindow)
221 if ( ! xParentWindow.is())
222 bTopWindow = sal_False;
223 else
224 xParentWindowPeer = css::uno::Reference< css::awt::XWindowPeer >(xParentWindow, css::uno::UNO_QUERY_THROW);
227 // describe window properties.
228 css::awt::WindowDescriptor aDescriptor;
229 if (bTopWindow)
231 aDescriptor.Type = css::awt::WindowClass_TOP ;
232 aDescriptor.WindowServiceName = OUString("window");
233 aDescriptor.ParentIndex = -1 ;
234 aDescriptor.Parent = css::uno::Reference< css::awt::XWindowPeer >() ;
235 aDescriptor.Bounds = aPosSize ;
236 aDescriptor.WindowAttributes = css::awt::WindowAttribute::BORDER |
237 css::awt::WindowAttribute::MOVEABLE |
238 css::awt::WindowAttribute::SIZEABLE |
239 css::awt::WindowAttribute::CLOSEABLE |
240 css::awt::VclWindowPeerAttribute::CLIPCHILDREN ;
242 else
244 aDescriptor.Type = css::awt::WindowClass_TOP ;
245 aDescriptor.WindowServiceName = OUString("dockingwindow");
246 aDescriptor.ParentIndex = 1 ;
247 aDescriptor.Parent = xParentWindowPeer ;
248 aDescriptor.Bounds = aPosSize ;
249 aDescriptor.WindowAttributes = css::awt::VclWindowPeerAttribute::CLIPCHILDREN ;
252 // create a new blank container window and get access to parent container to append new created task.
253 css::uno::Reference< css::awt::XWindowPeer > xPeer = xToolkit->createWindow( aDescriptor );
254 css::uno::Reference< css::awt::XWindow > xWindow ( xPeer, css::uno::UNO_QUERY );
255 if ( ! xWindow.is())
256 throw css::uno::Exception(OUString("TaskCreator service was not able to create suitable frame window."),
257 static_cast< ::cppu::OWeakObject* >(this));
258 if (bTopWindow)
259 xPeer->setBackground(::svtools::ColorConfig().GetColorValue(::svtools::APPBACKGROUND).nColor);
260 else
261 xPeer->setBackground(0xffffffff);
263 return xWindow;
266 //-----------------------------------------------
267 css::uno::Reference< css::frame::XFrame2 > TaskCreatorService::implts_createFrame( const css::uno::Reference< css::frame::XFrame >& xParentFrame ,
268 const css::uno::Reference< css::awt::XWindow >& xContainerWindow,
269 const OUString& sName )
271 // SAFE ->
272 ReadGuard aReadLock( m_aLock );
273 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
274 aReadLock.unlock();
275 // <- SAFE
277 // create new frame.
278 css::uno::Reference< css::frame::XFrame2 > xNewFrame = css::frame::Frame::create( comphelper::getComponentContext(xSMGR) );
280 // Set window on frame.
281 // Do it before calling any other interface methods ...
282 // The new created frame must be initialized before you can do anything else there.
283 xNewFrame->initialize( xContainerWindow );
285 // Put frame to the frame tree.
286 // Note: The property creator/parent will be set on the new putted frame automaticly ... by the parent container.
287 if (xParentFrame.is())
289 css::uno::Reference< css::frame::XFramesSupplier > xSupplier (xParentFrame, css::uno::UNO_QUERY_THROW);
290 css::uno::Reference< css::frame::XFrames > xContainer = xSupplier->getFrames();
291 xContainer->append( css::uno::Reference<css::frame::XFrame>(xNewFrame, css::uno::UNO_QUERY_THROW) );
294 // Set it's API name (if there is one from outside)
295 if (!sName.isEmpty())
296 xNewFrame->setName( sName );
298 return xNewFrame;
301 //-----------------------------------------------
302 void TaskCreatorService::implts_establishWindowStateListener( const css::uno::Reference< css::frame::XFrame2 >& xFrame )
304 // SAFE ->
305 ReadGuard aReadLock( m_aLock );
306 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
307 aReadLock.unlock();
308 // <- SAFE
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 PersistentWindowState* pPersistentStateHandler = new PersistentWindowState(xSMGR);
315 css::uno::Reference< css::lang::XInitialization > xInit(static_cast< ::cppu::OWeakObject* >(pPersistentStateHandler), css::uno::UNO_QUERY_THROW);
317 css::uno::Sequence< css::uno::Any > lInitData(1);
318 lInitData[0] <<= xFrame;
319 xInit->initialize(lInitData);
322 //-----------------------------------------------
323 void TaskCreatorService::implts_establishDocModifyListener( const css::uno::Reference< css::frame::XFrame2 >& xFrame )
325 // SAFE ->
326 ReadGuard aReadLock( m_aLock );
327 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
328 aReadLock.unlock();
329 // <- SAFE
331 // Special feature: It's allowed for frames using a top level window only!
332 // We must create a special listener service and couple it with the new created task frame.
333 // It will tag the window as modified if the underlying model was modified ...
334 TagWindowAsModified* pTag = new TagWindowAsModified(xSMGR);
335 css::uno::Reference< css::lang::XInitialization > xInit(static_cast< ::cppu::OWeakObject* >(pTag), css::uno::UNO_QUERY_THROW);
337 css::uno::Sequence< css::uno::Any > lInitData(1);
338 lInitData[0] <<= xFrame;
339 xInit->initialize(lInitData);
342 //-----------------------------------------------
343 void TaskCreatorService::implts_establishTitleBarUpdate( const css::uno::Reference< css::frame::XFrame2 >& xFrame )
345 // SAFE ->
346 ReadGuard aReadLock( m_aLock );
347 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
348 aReadLock.unlock();
349 // <- SAFE
351 TitleBarUpdate* pHelper = new TitleBarUpdate (xSMGR);
352 css::uno::Reference< css::lang::XInitialization > xInit(static_cast< ::cppu::OWeakObject* >(pHelper), css::uno::UNO_QUERY_THROW);
354 css::uno::Sequence< css::uno::Any > lInitData(1);
355 lInitData[0] <<= xFrame;
356 xInit->initialize(lInitData);
359 //-----------------------------------------------
360 OUString TaskCreatorService::impl_filterNames( const OUString& sName )
362 OUString sFiltered;
363 if (TargetHelper::isValidNameForFrame(sName))
364 sFiltered = sName;
365 return sFiltered;
368 } // namespace framework
370 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */