1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <com/sun/star/frame/XDispatchRecorderSupplier.hpp>
21 #include <com/sun/star/frame/ModuleManager.hpp>
22 #include <com/sun/star/container/XNameAccess.hpp>
23 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
24 #include <com/sun/star/frame/theUICommandDescription.hpp>
26 #include <svl/eitem.hxx>
27 #include <svtools/generictoolboxcontroller.hxx>
28 #include <vcl/msgbox.hxx>
29 #include <comphelper/processfactory.hxx>
31 #include "recfloat.hxx"
33 #include <sfx2/sfxresid.hxx>
34 #include <sfx2/app.hxx>
35 #include <sfx2/bindings.hxx>
36 #include <sfx2/dispatch.hxx>
37 #include <sfx2/viewfrm.hxx>
38 #include <sfx2/viewsh.hxx>
39 #include <sfx2/imagemgr.hxx>
41 using namespace ::com::sun::star
;
43 static OUString
GetLabelFromCommandURL( const OUString
& rCommandURL
, const uno::Reference
< frame::XFrame
>& xFrame
)
46 OUString aModuleIdentifier
;
47 uno::Reference
< container::XNameAccess
> xUICommandLabels
;
48 uno::Reference
< uno::XComponentContext
> xContext
;
49 uno::Reference
< container::XNameAccess
> xUICommandDescription
;
50 uno::Reference
< ::com::sun::star::frame::XModuleManager2
> xModuleManager
;
52 static uno::WeakReference
< uno::XComponentContext
> xTmpContext
;
53 static uno::WeakReference
< container::XNameAccess
> xTmpNameAccess
;
54 static uno::WeakReference
< ::com::sun::star::frame::XModuleManager2
> xTmpModuleMgr
;
56 xContext
= xTmpContext
;
59 xContext
= ::comphelper::getProcessComponentContext();
60 xTmpContext
= xContext
;
63 xUICommandDescription
= xTmpNameAccess
;
64 if ( !xUICommandDescription
.is() )
66 xUICommandDescription
= frame::theUICommandDescription::get(xContext
);
67 xTmpNameAccess
= xUICommandDescription
;
70 xModuleManager
= xTmpModuleMgr
;
71 if ( !xModuleManager
.is() )
73 xModuleManager
= frame::ModuleManager::create(xContext
);
74 xTmpModuleMgr
= xModuleManager
;
77 // Retrieve label from UI command description service
82 aModuleIdentifier
= xModuleManager
->identify( xFrame
);
84 catch( uno::Exception
& )
88 uno::Any a
= xUICommandDescription
->getByName( aModuleIdentifier
);
89 uno::Reference
< container::XNameAccess
> xUICommands
;
90 a
>>= xUICommandLabels
;
92 catch ( uno::Exception
& )
96 if ( xUICommandLabels
.is() )
100 if ( !rCommandURL
.isEmpty() )
102 uno::Sequence
< beans::PropertyValue
> aPropSeq
;
103 uno::Any
a( xUICommandLabels
->getByName( rCommandURL
));
104 if ( a
>>= aPropSeq
)
106 for ( sal_Int32 i
= 0; i
< aPropSeq
.getLength(); i
++ )
108 if ( aPropSeq
[i
].Name
== "Label" )
110 aPropSeq
[i
].Value
>>= aLabel
;
117 catch (uno::Exception
& )
125 SFX_IMPL_FLOATINGWINDOW( SfxRecordingFloatWrapper_Impl
, SID_RECORDING_FLOATWINDOW
);
127 SfxRecordingFloatWrapper_Impl::SfxRecordingFloatWrapper_Impl( vcl::Window
* pParentWnd
,
130 SfxChildWinInfo
* pInfo
)
131 : SfxChildWindow( pParentWnd
, nId
)
134 pWindow
= VclPtr
<SfxRecordingFloat_Impl
>::Create( pBindings
, this, pParentWnd
);
135 SetWantsFocus( false );
136 eChildAlignment
= SfxChildAlignment::NOALIGNMENT
;
137 static_cast<SfxFloatingWindow
*>(pWindow
.get())->Initialize( pInfo
);
140 SfxRecordingFloatWrapper_Impl::~SfxRecordingFloatWrapper_Impl()
142 SfxBoolItem
aItem( FN_PARAM_1
, true );
143 com::sun::star::uno::Reference
< com::sun::star::frame::XDispatchRecorder
> xRecorder
= pBindings
->GetRecorder();
144 if ( xRecorder
.is() )
145 pBindings
->GetDispatcher()->Execute( SID_STOP_RECORDING
, SfxCallMode::SYNCHRON
, &aItem
, 0L );
148 bool SfxRecordingFloatWrapper_Impl::QueryClose()
150 // asking for recorded macro should be replaced if index access is available!
152 com::sun::star::uno::Reference
< com::sun::star::frame::XDispatchRecorder
> xRecorder
= pBindings
->GetRecorder();
153 if ( xRecorder
.is() && !xRecorder
->getRecordedMacro().isEmpty() )
155 ScopedVclPtrInstance
< QueryBox
> aBox(GetWindow(), WB_YES_NO
| WB_DEF_NO
, SfxResId(STR_MACRO_LOSS
).toString());
156 aBox
->SetText( SfxResId(STR_CANCEL_RECORDING
).toString() );
157 bRet
= ( aBox
->Execute() == RET_YES
);
163 SfxRecordingFloat_Impl::SfxRecordingFloat_Impl(
165 SfxChildWindow
* pChildWin
,
166 vcl::Window
* pParent
)
167 : SfxFloatingWindow( pBind
,
170 "FloatingRecord", "sfx/ui/floatingrecord.ui", pBind
->GetActiveFrame() )
172 get(m_pTbx
, "toolbar");
174 // Retrieve label from helper function
175 uno::Reference
< frame::XFrame
> xFrame
= getFrame();
176 OUString
aCommandStr( ".uno:StopRecording" );
177 sal_uInt16 nItemId
= m_pTbx
->GetItemId(aCommandStr
);
178 m_pTbx
->SetItemText( nItemId
, GetLabelFromCommandURL( aCommandStr
, xFrame
));
180 // create a generic toolbox controller for our internal toolbox
181 svt::GenericToolboxController
* pController
= new svt::GenericToolboxController(
182 ::comphelper::getProcessComponentContext(),
187 xStopRecTbxCtrl
= uno::Reference
< frame::XToolbarController
>(
188 static_cast< cppu::OWeakObject
* >( pController
),
190 uno::Reference
< util::XUpdatable
> xUpdate( xStopRecTbxCtrl
, uno::UNO_QUERY
);
194 m_pTbx
->SetSelectHdl( LINK( this, SfxRecordingFloat_Impl
, Select
) );
197 SfxBoolItem
aItem( SID_RECORDMACRO
, true );
198 GetBindings().GetDispatcher()->Execute( SID_RECORDMACRO
, SfxCallMode::SYNCHRON
, &aItem
, 0L );
201 SfxRecordingFloat_Impl::~SfxRecordingFloat_Impl()
206 void SfxRecordingFloat_Impl::dispose()
210 if ( xStopRecTbxCtrl
.is() )
212 uno::Reference
< lang::XComponent
> xComp( xStopRecTbxCtrl
, uno::UNO_QUERY
);
216 catch ( uno::Exception
& )
220 SfxFloatingWindow::dispose();
223 bool SfxRecordingFloat_Impl::Close()
225 bool bRet
= SfxFloatingWindow::Close();
229 void SfxRecordingFloat_Impl::FillInfo( SfxChildWinInfo
& rInfo
) const
231 SfxFloatingWindow::FillInfo( rInfo
);
232 rInfo
.bVisible
= false;
235 void SfxRecordingFloat_Impl::StateChanged( StateChangedType nStateChange
)
237 if ( nStateChange
== StateChangedType::InitShow
)
239 SfxViewFrame
*pFrame
= GetBindings().GetDispatcher_Impl()->GetFrame();
240 vcl::Window
* pEditWin
= pFrame
->GetViewShell()->GetWindow();
242 Point aPoint
= pEditWin
->OutputToScreenPixel( pEditWin
->GetPosPixel() );
243 aPoint
= GetParent()->ScreenToOutputPixel( aPoint
);
246 SetPosPixel( aPoint
);
249 SfxFloatingWindow::StateChanged( nStateChange
);
252 IMPL_LINK_TYPED( SfxRecordingFloat_Impl
, Select
, ToolBox
*, pToolBar
, void )
255 sal_Int16
nKeyModifier( (sal_Int16
)m_pTbx
->GetModifier() );
256 if ( xStopRecTbxCtrl
.is() )
257 xStopRecTbxCtrl
->execute( nKeyModifier
);
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */