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 0 : SFX_IMPL_FLOATINGWINDOW( SfxRecordingFloatWrapper_Impl, SID_RECORDING_FLOATWINDOW );
126 :
127 0 : SfxRecordingFloatWrapper_Impl::SfxRecordingFloatWrapper_Impl( 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 : ( ( 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, SFX_CALLMODE_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 : Window* pParent )
167 : : SfxFloatingWindow( pBind,
168 : pChildWin,
169 : pParent,
170 : SfxResId( SID_RECORDING_FLOATWINDOW ) )
171 0 : , aTbx( this, SfxResId(SID_RECORDING_FLOATWINDOW) )
172 : {
173 : // Retrieve label from helper function
174 0 : uno::Reference< frame::XFrame > xFrame = GetBindings().GetActiveFrame();
175 0 : OUString aCommandStr( ".uno:StopRecording" );
176 0 : aTbx.SetItemText( SID_STOP_RECORDING, GetLabelFromCommandURL( aCommandStr, xFrame ));
177 :
178 : // Determine size of toolbar
179 0 : Size aTbxSize = aTbx.CalcWindowSizePixel();
180 0 : aTbx.SetPosSizePixel( Point(), aTbxSize );
181 0 : SetOutputSizePixel( aTbxSize );
182 :
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 0 : aCommandStr );
190 0 : xStopRecTbxCtrl = uno::Reference< frame::XToolbarController >(
191 : static_cast< cppu::OWeakObject* >( pController ),
192 0 : uno::UNO_QUERY );
193 0 : uno::Reference< util::XUpdatable > xUpdate( xStopRecTbxCtrl, uno::UNO_QUERY );
194 0 : if ( xUpdate.is() )
195 0 : xUpdate->update();
196 :
197 0 : aTbx.SetSelectHdl( LINK( this, SfxRecordingFloat_Impl, Select ) );
198 :
199 : // start recording
200 0 : SfxBoolItem aItem( SID_RECORDMACRO, true );
201 0 : GetBindings().GetDispatcher()->Execute( SID_RECORDMACRO, SFX_CALLMODE_SYNCHRON, &aItem, 0L );
202 0 : }
203 :
204 0 : SfxRecordingFloat_Impl::~SfxRecordingFloat_Impl()
205 : {
206 : try
207 : {
208 0 : if ( xStopRecTbxCtrl.is() )
209 : {
210 0 : uno::Reference< lang::XComponent > xComp( xStopRecTbxCtrl, uno::UNO_QUERY );
211 0 : xComp->dispose();
212 : }
213 : }
214 0 : catch ( uno::Exception& )
215 : {
216 : }
217 0 : }
218 :
219 0 : bool SfxRecordingFloat_Impl::Close()
220 : {
221 0 : bool bRet = SfxFloatingWindow::Close();
222 0 : return bRet;
223 : }
224 :
225 0 : void SfxRecordingFloat_Impl::FillInfo( SfxChildWinInfo& rInfo ) const
226 : {
227 0 : SfxFloatingWindow::FillInfo( rInfo );
228 0 : rInfo.bVisible = false;
229 0 : }
230 :
231 0 : void SfxRecordingFloat_Impl::StateChanged( StateChangedType nStateChange )
232 : {
233 0 : if ( nStateChange == STATE_CHANGE_INITSHOW )
234 : {
235 0 : SfxViewFrame *pFrame = GetBindings().GetDispatcher_Impl()->GetFrame();
236 0 : Window* pEditWin = pFrame->GetViewShell()->GetWindow();
237 :
238 0 : Point aPoint = pEditWin->OutputToScreenPixel( pEditWin->GetPosPixel() );
239 0 : aPoint = GetParent()->ScreenToOutputPixel( aPoint );
240 0 : aPoint.X() += 20;
241 0 : aPoint.Y() += 10;
242 0 : SetPosPixel( aPoint );
243 : }
244 :
245 0 : SfxFloatingWindow::StateChanged( nStateChange );
246 0 : }
247 :
248 0 : IMPL_LINK( SfxRecordingFloat_Impl, Select, ToolBox*, pToolBar )
249 : {
250 : (void)pToolBar;
251 0 : sal_Int16 nKeyModifier( (sal_Int16)aTbx.GetModifier() );
252 0 : if ( xStopRecTbxCtrl.is() )
253 0 : xStopRecTbxCtrl->execute( nKeyModifier );
254 :
255 0 : return 1;
256 : }
257 :
258 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|