bump product version to 4.1.6.2
[LibreOffice.git] / sfx2 / source / dialog / recfloat.cxx
blobad081894866ad2e5548137b2fa7105c63fbe9f95
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/UICommandDescription.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::UICommandDescription::create(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( Window* pParentWnd ,
128 sal_uInt16 nId ,
129 SfxBindings* pBind ,
130 SfxChildWinInfo* pInfo )
131 : SfxChildWindow( pParentWnd, nId )
132 , pBindings( pBind )
134 pWindow = new SfxRecordingFloat_Impl( pBindings, this, pParentWnd );
135 SetWantsFocus( sal_False );
136 eChildAlignment = SFX_ALIGN_NOALIGNMENT;
137 ( ( SfxFloatingWindow* ) pWindow )->Initialize( pInfo );
140 SfxRecordingFloatWrapper_Impl::~SfxRecordingFloatWrapper_Impl()
142 SfxBoolItem aItem( FN_PARAM_1, sal_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, SFX_CALLMODE_SYNCHRON, &aItem, 0L );
148 sal_Bool SfxRecordingFloatWrapper_Impl::QueryClose()
150 // asking for recorded macro should be replaced if index access is available!
151 sal_Bool bRet = sal_True;
152 com::sun::star::uno::Reference< com::sun::star::frame::XDispatchRecorder > xRecorder = pBindings->GetRecorder();
153 if ( xRecorder.is() && !xRecorder->getRecordedMacro().isEmpty() )
155 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 Window* pParent )
167 : SfxFloatingWindow( pBind,
168 pChildWin,
169 pParent,
170 SfxResId( SID_RECORDING_FLOATWINDOW ) )
171 , aTbx( this, SfxResId(SID_RECORDING_FLOATWINDOW) )
173 // Retrieve label from helper function
174 uno::Reference< frame::XFrame > xFrame = GetBindings().GetActiveFrame();
175 OUString aCommandStr( ".uno:StopRecording" );
176 aTbx.SetItemText( SID_STOP_RECORDING, GetLabelFromCommandURL( aCommandStr, xFrame ));
178 // Determine size of toolbar
179 Size aTbxSize = aTbx.CalcWindowSizePixel();
180 aTbx.SetPosSizePixel( Point(), aTbxSize );
181 SetOutputSizePixel( aTbxSize );
183 // create a generic toolbox controller for our internal toolbox
184 svt::GenericToolboxController* pController = new svt::GenericToolboxController(
185 ::comphelper::getProcessComponentContext(),
186 xFrame,
187 &aTbx,
188 SID_STOP_RECORDING,
189 aCommandStr );
190 xStopRecTbxCtrl = uno::Reference< frame::XToolbarController >(
191 static_cast< cppu::OWeakObject* >( pController ),
192 uno::UNO_QUERY );
193 uno::Reference< util::XUpdatable > xUpdate( xStopRecTbxCtrl, uno::UNO_QUERY );
194 if ( xUpdate.is() )
195 xUpdate->update();
197 aTbx.SetSelectHdl( LINK( this, SfxRecordingFloat_Impl, Select ) );
199 // start recording
200 SfxBoolItem aItem( SID_RECORDMACRO, sal_True );
201 GetBindings().GetDispatcher()->Execute( SID_RECORDMACRO, SFX_CALLMODE_SYNCHRON, &aItem, 0L );
204 SfxRecordingFloat_Impl::~SfxRecordingFloat_Impl()
208 if ( xStopRecTbxCtrl.is() )
210 uno::Reference< lang::XComponent > xComp( xStopRecTbxCtrl, uno::UNO_QUERY );
211 xComp->dispose();
214 catch ( uno::Exception& )
219 sal_Bool SfxRecordingFloat_Impl::Close()
221 sal_Bool bRet = SfxFloatingWindow::Close();
222 return bRet;
225 void SfxRecordingFloat_Impl::FillInfo( SfxChildWinInfo& rInfo ) const
227 SfxFloatingWindow::FillInfo( rInfo );
228 rInfo.bVisible = sal_False;
231 void SfxRecordingFloat_Impl::StateChanged( StateChangedType nStateChange )
233 if ( nStateChange == STATE_CHANGE_INITSHOW )
235 SfxViewFrame *pFrame = GetBindings().GetDispatcher_Impl()->GetFrame();
236 Window* pEditWin = pFrame->GetViewShell()->GetWindow();
238 Point aPoint = pEditWin->OutputToScreenPixel( pEditWin->GetPosPixel() );
239 aPoint = GetParent()->ScreenToOutputPixel( aPoint );
240 aPoint.X() += 20;
241 aPoint.Y() += 10;
242 SetPosPixel( aPoint );
245 SfxFloatingWindow::StateChanged( nStateChange );
248 IMPL_LINK( SfxRecordingFloat_Impl, Select, ToolBox*, pToolBar )
250 (void)pToolBar;
251 sal_Int16 nKeyModifier( (sal_Int16)aTbx.GetModifier() );
252 if ( xStopRecTbxCtrl.is() )
253 xStopRecTbxCtrl->execute( nKeyModifier );
255 return 1;
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */