Line data Source code
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 :
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>
25 :
26 : #include <svl/eitem.hxx>
27 : #include <svtools/generictoolboxcontroller.hxx>
28 : #include <vcl/msgbox.hxx>
29 : #include <comphelper/processfactory.hxx>
30 :
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>
40 :
41 : using namespace ::com::sun::star;
42 :
43 0 : static OUString GetLabelFromCommandURL( const OUString& rCommandURL, const uno::Reference< frame::XFrame >& xFrame )
44 : {
45 0 : OUString aLabel;
46 0 : OUString aModuleIdentifier;
47 0 : uno::Reference< container::XNameAccess > xUICommandLabels;
48 0 : uno::Reference< uno::XComponentContext > xContext;
49 0 : uno::Reference< container::XNameAccess > xUICommandDescription;
50 0 : uno::Reference< ::com::sun::star::frame::XModuleManager2 > xModuleManager;
51 :
52 0 : static uno::WeakReference< uno::XComponentContext > xTmpContext;
53 0 : static uno::WeakReference< container::XNameAccess > xTmpNameAccess;
54 0 : static uno::WeakReference< ::com::sun::star::frame::XModuleManager2 > xTmpModuleMgr;
55 :
56 0 : xContext = xTmpContext;
57 0 : if ( !xContext.is() )
58 : {
59 0 : xContext = ::comphelper::getProcessComponentContext();
60 0 : xTmpContext = xContext;
61 : }
62 :
63 0 : xUICommandDescription = xTmpNameAccess;
64 0 : if ( !xUICommandDescription.is() )
65 : {
66 0 : xUICommandDescription = frame::theUICommandDescription::get(xContext);
67 0 : xTmpNameAccess = xUICommandDescription;
68 : }
69 :
70 0 : xModuleManager = xTmpModuleMgr;
71 0 : if ( !xModuleManager.is() )
72 : {
73 0 : xModuleManager = frame::ModuleManager::create(xContext);
74 0 : xTmpModuleMgr = xModuleManager;
75 : }
76 :
77 : // Retrieve label from UI command description service
78 : try
79 : {
80 : try
81 : {
82 0 : aModuleIdentifier = xModuleManager->identify( xFrame );
83 : }
84 0 : catch( uno::Exception& )
85 : {
86 : }
87 :
88 0 : uno::Any a = xUICommandDescription->getByName( aModuleIdentifier );
89 0 : uno::Reference< container::XNameAccess > xUICommands;
90 0 : a >>= xUICommandLabels;
91 : }
92 0 : catch ( uno::Exception& )
93 : {
94 : }
95 :
96 0 : if ( xUICommandLabels.is() )
97 : {
98 : try
99 : {
100 0 : if ( !rCommandURL.isEmpty() )
101 : {
102 0 : uno::Sequence< beans::PropertyValue > aPropSeq;
103 0 : uno::Any a( xUICommandLabels->getByName( rCommandURL ));
104 0 : if ( a >>= aPropSeq )
105 : {
106 0 : for ( sal_Int32 i = 0; i < aPropSeq.getLength(); i++ )
107 : {
108 0 : if ( aPropSeq[i].Name == "Label" )
109 : {
110 0 : aPropSeq[i].Value >>= aLabel;
111 0 : break;
112 : }
113 : }
114 0 : }
115 : }
116 : }
117 0 : catch (uno::Exception& )
118 : {
119 : }
120 : }
121 :
122 0 : return aLabel;
123 : }
124 :
125 303 : SFX_IMPL_FLOATINGWINDOW( SfxRecordingFloatWrapper_Impl, SID_RECORDING_FLOATWINDOW );
126 :
127 0 : SfxRecordingFloatWrapper_Impl::SfxRecordingFloatWrapper_Impl( vcl::Window* pParentWnd ,
128 : sal_uInt16 nId ,
129 : SfxBindings* pBind ,
130 : SfxChildWinInfo* pInfo )
131 : : SfxChildWindow( pParentWnd, nId )
132 0 : , pBindings( pBind )
133 : {
134 0 : pWindow = new SfxRecordingFloat_Impl( pBindings, this, pParentWnd );
135 0 : SetWantsFocus( false );
136 0 : eChildAlignment = SFX_ALIGN_NOALIGNMENT;
137 0 : static_cast<SfxFloatingWindow*>(pWindow)->Initialize( pInfo );
138 0 : }
139 :
140 0 : SfxRecordingFloatWrapper_Impl::~SfxRecordingFloatWrapper_Impl()
141 : {
142 0 : SfxBoolItem aItem( FN_PARAM_1, true );
143 0 : com::sun::star::uno::Reference< com::sun::star::frame::XDispatchRecorder > xRecorder = pBindings->GetRecorder();
144 0 : if ( xRecorder.is() )
145 0 : pBindings->GetDispatcher()->Execute( SID_STOP_RECORDING, SfxCallMode::SYNCHRON, &aItem, 0L );
146 0 : }
147 :
148 0 : bool SfxRecordingFloatWrapper_Impl::QueryClose()
149 : {
150 : // asking for recorded macro should be replaced if index access is available!
151 0 : bool bRet = true;
152 0 : com::sun::star::uno::Reference< com::sun::star::frame::XDispatchRecorder > xRecorder = pBindings->GetRecorder();
153 0 : if ( xRecorder.is() && !xRecorder->getRecordedMacro().isEmpty() )
154 : {
155 0 : QueryBox aBox( GetWindow(), WB_YES_NO | WB_DEF_NO , SfxResId(STR_MACRO_LOSS).toString() );
156 0 : aBox.SetText( SfxResId(STR_CANCEL_RECORDING).toString() );
157 0 : bRet = ( aBox.Execute() == RET_YES );
158 : }
159 :
160 0 : return bRet;
161 : }
162 :
163 0 : SfxRecordingFloat_Impl::SfxRecordingFloat_Impl(
164 : SfxBindings* pBind ,
165 : SfxChildWindow* pChildWin ,
166 : vcl::Window* pParent )
167 : : SfxFloatingWindow( pBind,
168 : pChildWin,
169 : pParent,
170 0 : "FloatingRecord", "sfx/ui/floatingrecord.ui", pBind->GetActiveFrame() )
171 : {
172 0 : get(m_pTbx, "toolbar");
173 :
174 : // Retrieve label from helper function
175 0 : uno::Reference< frame::XFrame > xFrame = getFrame();
176 0 : OUString aCommandStr( ".uno:StopRecording" );
177 0 : sal_uInt16 nItemId = m_pTbx->GetItemId(aCommandStr);
178 0 : m_pTbx->SetItemText( nItemId, GetLabelFromCommandURL( aCommandStr, xFrame ));
179 :
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 0 : aCommandStr );
187 0 : xStopRecTbxCtrl = uno::Reference< frame::XToolbarController >(
188 : static_cast< cppu::OWeakObject* >( pController ),
189 0 : uno::UNO_QUERY );
190 0 : uno::Reference< util::XUpdatable > xUpdate( xStopRecTbxCtrl, uno::UNO_QUERY );
191 0 : if ( xUpdate.is() )
192 0 : xUpdate->update();
193 :
194 0 : m_pTbx->SetSelectHdl( LINK( this, SfxRecordingFloat_Impl, Select ) );
195 :
196 : // start recording
197 0 : SfxBoolItem aItem( SID_RECORDMACRO, true );
198 0 : GetBindings().GetDispatcher()->Execute( SID_RECORDMACRO, SfxCallMode::SYNCHRON, &aItem, 0L );
199 0 : }
200 :
201 0 : SfxRecordingFloat_Impl::~SfxRecordingFloat_Impl()
202 : {
203 : try
204 : {
205 0 : if ( xStopRecTbxCtrl.is() )
206 : {
207 0 : uno::Reference< lang::XComponent > xComp( xStopRecTbxCtrl, uno::UNO_QUERY );
208 0 : xComp->dispose();
209 : }
210 : }
211 0 : catch ( uno::Exception& )
212 : {
213 : }
214 0 : }
215 :
216 0 : bool SfxRecordingFloat_Impl::Close()
217 : {
218 0 : bool bRet = SfxFloatingWindow::Close();
219 0 : return bRet;
220 : }
221 :
222 0 : void SfxRecordingFloat_Impl::FillInfo( SfxChildWinInfo& rInfo ) const
223 : {
224 0 : SfxFloatingWindow::FillInfo( rInfo );
225 0 : rInfo.bVisible = false;
226 0 : }
227 :
228 0 : void SfxRecordingFloat_Impl::StateChanged( StateChangedType nStateChange )
229 : {
230 0 : if ( nStateChange == StateChangedType::INITSHOW )
231 : {
232 0 : SfxViewFrame *pFrame = GetBindings().GetDispatcher_Impl()->GetFrame();
233 0 : vcl::Window* pEditWin = pFrame->GetViewShell()->GetWindow();
234 :
235 0 : Point aPoint = pEditWin->OutputToScreenPixel( pEditWin->GetPosPixel() );
236 0 : aPoint = GetParent()->ScreenToOutputPixel( aPoint );
237 0 : aPoint.X() += 20;
238 0 : aPoint.Y() += 10;
239 0 : SetPosPixel( aPoint );
240 : }
241 :
242 0 : SfxFloatingWindow::StateChanged( nStateChange );
243 0 : }
244 :
245 0 : IMPL_LINK( SfxRecordingFloat_Impl, Select, ToolBox*, pToolBar )
246 : {
247 : (void)pToolBar;
248 0 : sal_Int16 nKeyModifier( (sal_Int16)m_pTbx->GetModifier() );
249 0 : if ( xStopRecTbxCtrl.is() )
250 0 : xStopRecTbxCtrl->execute( nKeyModifier );
251 :
252 0 : return 1;
253 951 : }
254 :
255 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|