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