bump product version to 5.0.4.1
[LibreOffice.git] / sfx2 / source / dialog / recfloat.cxx
blobce3dabb234b1e812b96da55fc9d33cdcf3093b86
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 <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"
32 #include "dialog.hrc"
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 )
45 OUString aLabel;
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;
57 if ( !xContext.is() )
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
78 try
80 try
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() )
98 try
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;
111 break;
117 catch (uno::Exception& )
122 return aLabel;
125 SFX_IMPL_FLOATINGWINDOW( SfxRecordingFloatWrapper_Impl, SID_RECORDING_FLOATWINDOW );
127 SfxRecordingFloatWrapper_Impl::SfxRecordingFloatWrapper_Impl( vcl::Window* pParentWnd ,
128 sal_uInt16 nId ,
129 SfxBindings* pBind ,
130 SfxChildWinInfo* pInfo )
131 : SfxChildWindow( pParentWnd, nId )
132 , pBindings( pBind )
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!
151 bool bRet = true;
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 );
160 return bRet;
163 SfxRecordingFloat_Impl::SfxRecordingFloat_Impl(
164 SfxBindings* pBind ,
165 SfxChildWindow* pChildWin ,
166 vcl::Window* pParent )
167 : SfxFloatingWindow( pBind,
168 pChildWin,
169 pParent,
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(),
183 xFrame,
184 m_pTbx,
185 nItemId,
186 aCommandStr );
187 xStopRecTbxCtrl = uno::Reference< frame::XToolbarController >(
188 static_cast< cppu::OWeakObject* >( pController ),
189 uno::UNO_QUERY );
190 uno::Reference< util::XUpdatable > xUpdate( xStopRecTbxCtrl, uno::UNO_QUERY );
191 if ( xUpdate.is() )
192 xUpdate->update();
194 m_pTbx->SetSelectHdl( LINK( this, SfxRecordingFloat_Impl, Select ) );
196 // start recording
197 SfxBoolItem aItem( SID_RECORDMACRO, true );
198 GetBindings().GetDispatcher()->Execute( SID_RECORDMACRO, SfxCallMode::SYNCHRON, &aItem, 0L );
201 SfxRecordingFloat_Impl::~SfxRecordingFloat_Impl()
203 disposeOnce();
206 void SfxRecordingFloat_Impl::dispose()
210 if ( xStopRecTbxCtrl.is() )
212 uno::Reference< lang::XComponent > xComp( xStopRecTbxCtrl, uno::UNO_QUERY );
213 xComp->dispose();
216 catch ( uno::Exception& )
219 m_pTbx.clear();
220 SfxFloatingWindow::dispose();
223 bool SfxRecordingFloat_Impl::Close()
225 bool bRet = SfxFloatingWindow::Close();
226 return bRet;
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 );
244 aPoint.X() += 20;
245 aPoint.Y() += 10;
246 SetPosPixel( aPoint );
249 SfxFloatingWindow::StateChanged( nStateChange );
252 IMPL_LINK_TYPED( SfxRecordingFloat_Impl, Select, ToolBox*, pToolBar, void )
254 (void)pToolBar;
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: */