xref: /aoo4110/main/sfx2/source/dialog/recfloat.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sfx2.hxx"
26 
27 // includes *******************************************************************
28 #include <com/sun/star/frame/XDispatchRecorderSupplier.hpp>
29 #include <com/sun/star/frame/XModuleManager.hpp>
30 #include <com/sun/star/container/XNameAccess.hpp>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 
34 #include <svl/eitem.hxx>
35 #include <svtools/generictoolboxcontroller.hxx>
36 #include <vcl/msgbox.hxx>
37 #include <comphelper/processfactory.hxx>
38 
39 #include "recfloat.hxx"
40 #include "dialog.hrc"
41 #include "sfx2/sfxresid.hxx"
42 #include <sfx2/app.hxx>
43 #include <sfx2/bindings.hxx>
44 #include <sfx2/dispatch.hxx>
45 #include <sfx2/viewfrm.hxx>
46 #include <sfx2/viewsh.hxx>
47 #include "sfx2/imagemgr.hxx"
48 
49 using namespace ::com::sun::star;
50 
GetLabelFromCommandURL(const rtl::OUString & rCommandURL,const uno::Reference<frame::XFrame> & xFrame)51 static rtl::OUString GetLabelFromCommandURL( const rtl::OUString& rCommandURL, const uno::Reference< frame::XFrame >& xFrame )
52 {
53     rtl::OUString aLabel;
54     rtl::OUString aModuleIdentifier;
55     uno::Reference< container::XNameAccess > xUICommandLabels;
56     uno::Reference< lang::XMultiServiceFactory > xServiceManager;
57     uno::Reference< container::XNameAccess > xUICommandDescription;
58     uno::Reference< ::com::sun::star::frame::XModuleManager > xModuleManager;
59 
60     static uno::WeakReference< lang::XMultiServiceFactory > xTmpServiceManager;
61     static uno::WeakReference< container::XNameAccess >     xTmpNameAccess;
62     static uno::WeakReference< ::com::sun::star::frame::XModuleManager > xTmpModuleMgr;
63 
64     xServiceManager = xTmpServiceManager;
65     if ( !xServiceManager.is() )
66     {
67         xServiceManager = ::comphelper::getProcessServiceFactory();
68         xTmpServiceManager = xServiceManager;
69     }
70 
71     xUICommandDescription = xTmpNameAccess;
72     if ( !xUICommandDescription.is() )
73     {
74         xUICommandDescription = uno::Reference< container::XNameAccess >(
75                                     xServiceManager->createInstance(
76                                         rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
77                                             "com.sun.star.frame.UICommandDescription" ))),
78                                     uno::UNO_QUERY );
79         xTmpNameAccess = xUICommandDescription;
80     }
81 
82     xModuleManager = xTmpModuleMgr;
83     if ( !xModuleManager.is() )
84     {
85         xModuleManager = uno::Reference< ::com::sun::star::frame::XModuleManager >(
86             xServiceManager->createInstance(
87                 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
88                     "com.sun.star.frame.ModuleManager" ))),
89             uno::UNO_QUERY_THROW );
90         xTmpModuleMgr = xModuleManager;
91     }
92 
93     // Retrieve label from UI command description service
94 	try
95     {
96         try
97         {
98             aModuleIdentifier = xModuleManager->identify( xFrame );
99         }
100         catch( uno::Exception& )
101         {
102         }
103 
104         if ( xUICommandDescription.is() )
105         {
106             uno::Any a = xUICommandDescription->getByName( aModuleIdentifier );
107             uno::Reference< container::XNameAccess > xUICommands;
108             a >>= xUICommandLabels;
109         }
110     }
111     catch ( uno::Exception& )
112     {
113     }
114 
115     if ( xUICommandLabels.is() )
116     {
117         try
118         {
119             if ( rCommandURL.getLength() > 0 )
120             {
121                 uno::Sequence< beans::PropertyValue > aPropSeq;
122                 uno::Any a( xUICommandLabels->getByName( rCommandURL ));
123                 if ( a >>= aPropSeq )
124                 {
125                     for ( sal_Int32 i = 0; i < aPropSeq.getLength(); i++ )
126                     {
127                         if ( aPropSeq[i].Name.equalsAscii( "Label" ))
128                         {
129                             aPropSeq[i].Value >>= aLabel;
130                             break;
131                         }
132                     }
133                 }
134             }
135         }
136         catch (uno::Exception& )
137         {
138         }
139     }
140 
141     return aLabel;
142 }
143 
144 SFX_IMPL_FLOATINGWINDOW( SfxRecordingFloatWrapper_Impl, SID_RECORDING_FLOATWINDOW );
145 
SfxRecordingFloatWrapper_Impl(Window * pParentWnd,sal_uInt16 nId,SfxBindings * pBind,SfxChildWinInfo * pInfo)146 SfxRecordingFloatWrapper_Impl::SfxRecordingFloatWrapper_Impl( Window* pParentWnd ,
147 												sal_uInt16 nId ,
148 												SfxBindings* pBind ,
149 												SfxChildWinInfo* pInfo )
150 					: SfxChildWindow( pParentWnd, nId )
151 					, pBindings( pBind )
152 {
153     pWindow = new SfxRecordingFloat_Impl( pBindings, this, pParentWnd );
154 	SetWantsFocus( sal_False );
155 	eChildAlignment = SFX_ALIGN_NOALIGNMENT;
156     ( ( SfxFloatingWindow* ) pWindow )->Initialize( pInfo );
157 }
158 
~SfxRecordingFloatWrapper_Impl()159 SfxRecordingFloatWrapper_Impl::~SfxRecordingFloatWrapper_Impl()
160 {
161 	SfxBoolItem aItem( FN_PARAM_1, sal_True );
162 	com::sun::star::uno::Reference< com::sun::star::frame::XDispatchRecorder > xRecorder = pBindings->GetRecorder();
163 	if ( xRecorder.is() )
164 	    pBindings->GetDispatcher()->Execute( SID_STOP_RECORDING, SFX_CALLMODE_SYNCHRON, &aItem, 0L );
165 }
166 
QueryClose()167 sal_Bool SfxRecordingFloatWrapper_Impl::QueryClose()
168 {
169     // asking for recorded macro should be replaced if index access is available!
170 	sal_Bool bRet = sal_True;
171     com::sun::star::uno::Reference< com::sun::star::frame::XDispatchRecorder > xRecorder = pBindings->GetRecorder();
172     if ( xRecorder.is() && xRecorder->getRecordedMacro().getLength() )
173     {
174         QueryBox aBox( GetWindow(), WB_YES_NO | WB_DEF_NO , String( SfxResId( STR_MACRO_LOSS ) ) );
175         aBox.SetText( String( SfxResId(STR_CANCEL_RECORDING) ) );
176         bRet = ( aBox.Execute() == RET_YES );
177     }
178 
179 	return bRet;
180 }
181 
SfxRecordingFloat_Impl(SfxBindings * pBind,SfxChildWindow * pChildWin,Window * pParent)182 SfxRecordingFloat_Impl::SfxRecordingFloat_Impl(
183     SfxBindings* pBind ,
184 	SfxChildWindow* pChildWin ,
185     Window* pParent )
186     : SfxFloatingWindow( pBind,
187 	                     pChildWin,
188 						 pParent,
189                          SfxResId( SID_RECORDING_FLOATWINDOW ) )
190     , pWrapper( pChildWin )
191     , aTbx( this, SfxResId(SID_RECORDING_FLOATWINDOW) )
192 {
193     // Retrieve label from helper function
194     uno::Reference< frame::XFrame > xFrame = GetBindings().GetActiveFrame();
195     rtl::OUString aCommandStr( RTL_CONSTASCII_USTRINGPARAM( ".uno:StopRecording" ));
196     aTbx.SetItemText( SID_STOP_RECORDING, GetLabelFromCommandURL( aCommandStr, xFrame ));
197 
198     // Determine size of toolbar
199     Size aTbxSize = aTbx.CalcWindowSizePixel();
200     aTbx.SetPosSizePixel( Point(), aTbxSize );
201 	SetOutputSizePixel( aTbxSize );
202 
203     // create a generic toolbox controller for our internal toolbox
204     svt::GenericToolboxController* pController = new svt::GenericToolboxController(
205                                                     ::comphelper::getProcessServiceFactory(),
206                                                     xFrame,
207                                                     &aTbx,
208                                                     SID_STOP_RECORDING,
209                                                     aCommandStr );
210     xStopRecTbxCtrl = uno::Reference< frame::XToolbarController >(
211                             static_cast< cppu::OWeakObject* >( pController ),
212                         uno::UNO_QUERY );
213     uno::Reference< util::XUpdatable > xUpdate( xStopRecTbxCtrl, uno::UNO_QUERY );
214     if ( xUpdate.is() )
215         xUpdate->update();
216 
217     aTbx.SetSelectHdl( LINK( this, SfxRecordingFloat_Impl, Select ) );
218 
219     // start recording
220     SfxBoolItem aItem( SID_RECORDMACRO, sal_True );
221     GetBindings().GetDispatcher()->Execute( SID_RECORDMACRO, SFX_CALLMODE_SYNCHRON, &aItem, 0L );
222 }
223 
~SfxRecordingFloat_Impl()224 SfxRecordingFloat_Impl::~SfxRecordingFloat_Impl()
225 {
226     try
227     {
228         if ( xStopRecTbxCtrl.is() )
229         {
230             uno::Reference< lang::XComponent > xComp( xStopRecTbxCtrl, uno::UNO_QUERY );
231             xComp->dispose();
232         }
233     }
234     catch ( uno::Exception& )
235     {
236     }
237 }
238 
Close()239 sal_Bool SfxRecordingFloat_Impl::Close()
240 {
241     sal_Bool bRet = SfxFloatingWindow::Close();
242 	return bRet;
243 }
244 
FillInfo(SfxChildWinInfo & rInfo) const245 void SfxRecordingFloat_Impl::FillInfo( SfxChildWinInfo& rInfo ) const
246 {
247     SfxFloatingWindow::FillInfo( rInfo );
248 	rInfo.bVisible = sal_False;
249 }
250 
StateChanged(StateChangedType nStateChange)251 void SfxRecordingFloat_Impl::StateChanged( StateChangedType nStateChange )
252 {
253 	if ( nStateChange == STATE_CHANGE_INITSHOW )
254 	{
255 		SfxViewFrame *pFrame = GetBindings().GetDispatcher_Impl()->GetFrame();
256 		Window* pEditWin = pFrame->GetViewShell()->GetWindow();
257 
258 		Point aPoint = pEditWin->OutputToScreenPixel( pEditWin->GetPosPixel() );
259 		aPoint = GetParent()->ScreenToOutputPixel( aPoint );
260 		aPoint.X() += 20;
261 		aPoint.Y() += 10;
262 		SetPosPixel( aPoint );
263 	}
264 
265 	SfxFloatingWindow::StateChanged( nStateChange );
266 }
267 
IMPL_LINK(SfxRecordingFloat_Impl,Select,ToolBox *,pToolBar)268 IMPL_LINK( SfxRecordingFloat_Impl, Select, ToolBox*, pToolBar )
269 {
270     (void)pToolBar;
271     sal_Int16   nKeyModifier( (sal_Int16)aTbx.GetModifier() );
272     if ( xStopRecTbxCtrl.is() )
273         xStopRecTbxCtrl->execute( nKeyModifier );
274 
275     return 1;
276 }
277