1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir #include "ListenerHelper.h"
30*cdf0e10cSrcweir #include "MyProtocolHandler.h"
31*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/frame/XFrame.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/frame/XController.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/frame/DispatchDescriptor.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/text/XTextViewCursor.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/text/XTextCursor.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/text/XTextDocument.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/sheet/XSpreadsheetView.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/frame/ControlCommand.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/frame/ControlEvent.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/frame/XControlNotificationListener.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
45*cdf0e10cSrcweir #include <com/sun/star/awt/XWindowPeer.hpp>
46*cdf0e10cSrcweir #include <com/sun/star/awt/WindowAttribute.hpp>
47*cdf0e10cSrcweir #include <com/sun/star/awt/XMessageBox.hpp>
48*cdf0e10cSrcweir #include <com/sun/star/frame/XComponentLoader.hpp>
49*cdf0e10cSrcweir #include <com/sun/star/view/XSelectionSupplier.hpp>
50*cdf0e10cSrcweir #include <com/sun/star/system/XSystemShellExecute.hpp>
51*cdf0e10cSrcweir #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
52*cdf0e10cSrcweir #include <com/sun/star/frame/XStorable.hpp>
53*cdf0e10cSrcweir #include <com/sun/star/container/XContainerQuery.hpp>
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir #include <osl/file.hxx>
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir using namespace com::sun::star::uno;
58*cdf0e10cSrcweir using namespace com::sun::star::frame;
59*cdf0e10cSrcweir using com::sun::star::lang::XMultiServiceFactory;
60*cdf0e10cSrcweir using com::sun::star::beans::PropertyValue;
61*cdf0e10cSrcweir using com::sun::star::util::URL;
62*cdf0e10cSrcweir using com::sun::star::text::XTextViewCursorSupplier;
63*cdf0e10cSrcweir using com::sun::star::text::XTextViewCursor;
64*cdf0e10cSrcweir using com::sun::star::text::XTextCursor;
65*cdf0e10cSrcweir using com::sun::star::sheet::XSpreadsheetView;
66*cdf0e10cSrcweir using com::sun::star::frame::XModel;
67*cdf0e10cSrcweir using com::sun::star::text::XTextRange;
68*cdf0e10cSrcweir using com::sun::star::text::XTextDocument;
69*cdf0e10cSrcweir using com::sun::star::beans::NamedValue;
70*cdf0e10cSrcweir using namespace com::sun::star::awt;
71*cdf0e10cSrcweir using com::sun::star::view::XSelectionSupplier;
72*cdf0e10cSrcweir using namespace com::sun::star::system;
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir ListenerHelper aListenerHelper;
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir void BaseDispatch::ShowMessageBox( const Reference< XFrame >& rFrame, const ::rtl::OUString& aTitle, const ::rtl::OUString& aMsgText )
77*cdf0e10cSrcweir {
78*cdf0e10cSrcweir 	if ( !mxToolkit.is() )
79*cdf0e10cSrcweir 		mxToolkit = Reference< XToolkit > ( mxMSF->createInstance(
80*cdf0e10cSrcweir 			::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
81*cdf0e10cSrcweir                                             "com.sun.star.awt.Toolkit" ))), UNO_QUERY );
82*cdf0e10cSrcweir     if ( rFrame.is() && mxToolkit.is() )
83*cdf0e10cSrcweir     {
84*cdf0e10cSrcweir         // describe window properties.
85*cdf0e10cSrcweir         WindowDescriptor                aDescriptor;
86*cdf0e10cSrcweir         aDescriptor.Type              = WindowClass_MODALTOP;
87*cdf0e10cSrcweir         aDescriptor.WindowServiceName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "infobox" ));
88*cdf0e10cSrcweir         aDescriptor.ParentIndex       = -1;
89*cdf0e10cSrcweir         aDescriptor.Parent            = Reference< XWindowPeer >( rFrame->getContainerWindow(), UNO_QUERY );
90*cdf0e10cSrcweir         aDescriptor.Bounds            = Rectangle(0,0,300,200);
91*cdf0e10cSrcweir         aDescriptor.WindowAttributes  = WindowAttribute::BORDER | WindowAttribute::MOVEABLE | WindowAttribute::CLOSEABLE;
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir         Reference< XWindowPeer > xPeer = mxToolkit->createWindow( aDescriptor );
94*cdf0e10cSrcweir         if ( xPeer.is() )
95*cdf0e10cSrcweir         {
96*cdf0e10cSrcweir             Reference< XMessageBox > xMsgBox( xPeer, UNO_QUERY );
97*cdf0e10cSrcweir             if ( xMsgBox.is() )
98*cdf0e10cSrcweir             {
99*cdf0e10cSrcweir                 xMsgBox->setCaptionText( aTitle );
100*cdf0e10cSrcweir                 xMsgBox->setMessageText( aMsgText );
101*cdf0e10cSrcweir                 xMsgBox->execute();
102*cdf0e10cSrcweir             }
103*cdf0e10cSrcweir         }
104*cdf0e10cSrcweir     }
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir void BaseDispatch::SendCommand( const com::sun::star::util::URL& aURL, const ::rtl::OUString& rCommand, const Sequence< NamedValue >& rArgs, sal_Bool bEnabled )
108*cdf0e10cSrcweir {
109*cdf0e10cSrcweir     Reference < XDispatch > xDispatch =
110*cdf0e10cSrcweir             aListenerHelper.GetDispatch( mxFrame, aURL.Path );
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir     FeatureStateEvent aEvent;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     aEvent.FeatureURL = aURL;
115*cdf0e10cSrcweir     aEvent.Source     = xDispatch;
116*cdf0e10cSrcweir     aEvent.IsEnabled  = bEnabled;
117*cdf0e10cSrcweir     aEvent.Requery    = sal_False;
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     ControlCommand aCtrlCmd;
120*cdf0e10cSrcweir     aCtrlCmd.Command   = rCommand;
121*cdf0e10cSrcweir     aCtrlCmd.Arguments = rArgs;
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir     aEvent.State <<= aCtrlCmd;
124*cdf0e10cSrcweir     aListenerHelper.Notify( mxFrame, aEvent.FeatureURL.Path, aEvent );
125*cdf0e10cSrcweir }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir void BaseDispatch::SendCommandTo( const Reference< XStatusListener >& xControl, const URL& aURL, const ::rtl::OUString& rCommand, const Sequence< NamedValue >& rArgs, sal_Bool bEnabled )
128*cdf0e10cSrcweir {
129*cdf0e10cSrcweir     FeatureStateEvent aEvent;
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir     aEvent.FeatureURL = aURL;
132*cdf0e10cSrcweir     aEvent.Source     = (::com::sun::star::frame::XDispatch*) this;
133*cdf0e10cSrcweir     aEvent.IsEnabled  = bEnabled;
134*cdf0e10cSrcweir     aEvent.Requery    = sal_False;
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir     ControlCommand aCtrlCmd;
137*cdf0e10cSrcweir     aCtrlCmd.Command   = rCommand;
138*cdf0e10cSrcweir     aCtrlCmd.Arguments = rArgs;
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir     aEvent.State <<= aCtrlCmd;
141*cdf0e10cSrcweir     xControl->statusChanged( aEvent );
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir void SAL_CALL MyProtocolHandler::initialize( const Sequence< Any >& aArguments ) throw ( Exception, RuntimeException)
145*cdf0e10cSrcweir {
146*cdf0e10cSrcweir 	Reference < XFrame > xFrame;
147*cdf0e10cSrcweir 	if ( aArguments.getLength() )
148*cdf0e10cSrcweir 	{
149*cdf0e10cSrcweir 		// das erste Argument ist immer der Frame, da ein ProtocolHandler den braucht um Zugriff
150*cdf0e10cSrcweir 		// auf den Context zu haben, in dem er aufgerufen wird
151*cdf0e10cSrcweir 		aArguments[0] >>= xFrame;
152*cdf0e10cSrcweir 		mxFrame = xFrame;
153*cdf0e10cSrcweir 	}
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir Reference< XDispatch > SAL_CALL MyProtocolHandler::queryDispatch(	const URL& aURL, const ::rtl::OUString& sTargetFrameName, sal_Int32 nSearchFlags )
157*cdf0e10cSrcweir 				throw( RuntimeException )
158*cdf0e10cSrcweir {
159*cdf0e10cSrcweir 	Reference < XDispatch > xRet;
160*cdf0e10cSrcweir 	if ( !mxFrame.is() )
161*cdf0e10cSrcweir 		return 0;
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 	Reference < XController > xCtrl = mxFrame->getController();
164*cdf0e10cSrcweir 	if ( xCtrl.is() && !aURL.Protocol.compareToAscii("vnd.demo.complextoolbarcontrols.demoaddon:") )
165*cdf0e10cSrcweir 	{
166*cdf0e10cSrcweir 		Reference < XTextViewCursorSupplier > xCursor( xCtrl, UNO_QUERY );
167*cdf0e10cSrcweir 		Reference < XSpreadsheetView > xView( xCtrl, UNO_QUERY );
168*cdf0e10cSrcweir 		if ( !xCursor.is() && !xView.is() )
169*cdf0e10cSrcweir 			// ohne ein entsprechendes Dokument funktioniert der Handler nicht
170*cdf0e10cSrcweir 			return xRet;
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 		if ( aURL.Path.equalsAscii("Command1" ) ||
173*cdf0e10cSrcweir 			 aURL.Path.equalsAscii("Command2" ) ||
174*cdf0e10cSrcweir 			 aURL.Path.equalsAscii("Command3" ) ||
175*cdf0e10cSrcweir 			 aURL.Path.equalsAscii("Command4" ) ||
176*cdf0e10cSrcweir 			 aURL.Path.equalsAscii("Command5" ) ||
177*cdf0e10cSrcweir 			 aURL.Path.equalsAscii("Command6" ) ||
178*cdf0e10cSrcweir              aURL.Path.equalsAscii("Command7" ) )
179*cdf0e10cSrcweir 		{
180*cdf0e10cSrcweir 			xRet = aListenerHelper.GetDispatch( mxFrame, aURL.Path );
181*cdf0e10cSrcweir 			if ( !xRet.is() )
182*cdf0e10cSrcweir 			{
183*cdf0e10cSrcweir 				xRet = xCursor.is() ? (BaseDispatch*) new WriterDispatch( mxMSF, mxFrame ) :
184*cdf0e10cSrcweir 					(BaseDispatch*) new CalcDispatch( mxMSF, mxFrame );
185*cdf0e10cSrcweir 				aListenerHelper.AddDispatch( xRet, mxFrame, aURL.Path );
186*cdf0e10cSrcweir 			}
187*cdf0e10cSrcweir 		}
188*cdf0e10cSrcweir 	}
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 	return xRet;
191*cdf0e10cSrcweir }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir Sequence < Reference< XDispatch > > SAL_CALL MyProtocolHandler::queryDispatches( const Sequence < DispatchDescriptor >& seqDescripts )
194*cdf0e10cSrcweir 			throw( RuntimeException )
195*cdf0e10cSrcweir {
196*cdf0e10cSrcweir     sal_Int32 nCount = seqDescripts.getLength();
197*cdf0e10cSrcweir     Sequence < Reference < XDispatch > > lDispatcher( nCount );
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir     for( sal_Int32 i=0; i<nCount; ++i )
200*cdf0e10cSrcweir         lDispatcher[i] = queryDispatch( seqDescripts[i].FeatureURL, seqDescripts[i].FrameName, seqDescripts[i].SearchFlags );
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir     return lDispatcher;
203*cdf0e10cSrcweir }
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir ::rtl::OUString MyProtocolHandler_getImplementationName ()
206*cdf0e10cSrcweir 	throw (RuntimeException)
207*cdf0e10cSrcweir {
208*cdf0e10cSrcweir     return ::rtl::OUString::createFromAscii(MYPROTOCOLHANDLER_IMPLEMENTATIONNAME);
209*cdf0e10cSrcweir }
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir sal_Bool SAL_CALL MyProtocolHandler_supportsService( const ::rtl::OUString& ServiceName )
212*cdf0e10cSrcweir 	throw (RuntimeException)
213*cdf0e10cSrcweir {
214*cdf0e10cSrcweir     return (
215*cdf0e10cSrcweir             ServiceName.equalsAscii(MYPROTOCOLHANDLER_SERVICENAME  ) ||
216*cdf0e10cSrcweir             ServiceName.equalsAscii("com.sun.star.frame.ProtocolHandler")
217*cdf0e10cSrcweir            );
218*cdf0e10cSrcweir }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL MyProtocolHandler_getSupportedServiceNames(  )
221*cdf0e10cSrcweir 	throw (RuntimeException)
222*cdf0e10cSrcweir {
223*cdf0e10cSrcweir 	Sequence < ::rtl::OUString > aRet(1);
224*cdf0e10cSrcweir     aRet[0] = ::rtl::OUString::createFromAscii(MYPROTOCOLHANDLER_SERVICENAME);
225*cdf0e10cSrcweir     return aRet;
226*cdf0e10cSrcweir }
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir #undef SERVICE_NAME
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir Reference< XInterface > SAL_CALL MyProtocolHandler_createInstance( const Reference< XMultiServiceFactory > & rSMgr)
231*cdf0e10cSrcweir 	throw( Exception )
232*cdf0e10cSrcweir {
233*cdf0e10cSrcweir 	return (cppu::OWeakObject*) new MyProtocolHandler( rSMgr );
234*cdf0e10cSrcweir }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir // XServiceInfo
237*cdf0e10cSrcweir ::rtl::OUString SAL_CALL MyProtocolHandler::getImplementationName(  )
238*cdf0e10cSrcweir 	throw (RuntimeException)
239*cdf0e10cSrcweir {
240*cdf0e10cSrcweir 	return MyProtocolHandler_getImplementationName();
241*cdf0e10cSrcweir }
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir sal_Bool SAL_CALL MyProtocolHandler::supportsService( const ::rtl::OUString& rServiceName )
244*cdf0e10cSrcweir 	throw (RuntimeException)
245*cdf0e10cSrcweir {
246*cdf0e10cSrcweir     return MyProtocolHandler_supportsService( rServiceName );
247*cdf0e10cSrcweir }
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL MyProtocolHandler::getSupportedServiceNames(  )
250*cdf0e10cSrcweir 	throw (RuntimeException)
251*cdf0e10cSrcweir {
252*cdf0e10cSrcweir     return MyProtocolHandler_getSupportedServiceNames();
253*cdf0e10cSrcweir }
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir void SAL_CALL BaseDispatch::dispatch( const URL& aURL, const Sequence < PropertyValue >& lArgs ) throw (RuntimeException)
256*cdf0e10cSrcweir {
257*cdf0e10cSrcweir 	/* Its neccessary to hold this object alive, till this method finish.
258*cdf0e10cSrcweir 	   May the outside dispatch cache (implemented by the menu/toolbar!)
259*cdf0e10cSrcweir 	   forget this instance during de-/activation of frames (focus!).
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir 		E.g. An open db beamer in combination with the My-Dialog
262*cdf0e10cSrcweir 		can force such strange situation :-(
263*cdf0e10cSrcweir 	 */
264*cdf0e10cSrcweir 	Reference< XInterface > xSelfHold(static_cast< XDispatch* >(this), UNO_QUERY);
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir 	if ( !aURL.Protocol.compareToAscii("vnd.demo.complextoolbarcontrols.demoaddon:") )
267*cdf0e10cSrcweir 	{
268*cdf0e10cSrcweir 		if ( !aURL.Path.compareToAscii("Command1" ) )
269*cdf0e10cSrcweir 		{
270*cdf0e10cSrcweir             // open the OpenOffice.org web page
271*cdf0e10cSrcweir             ::rtl::OUString sURL(::rtl::OUString::createFromAscii("http://www.openoffice.org"));
272*cdf0e10cSrcweir             Reference< XSystemShellExecute > xSystemShellExecute( mxMSF->createInstance(
273*cdf0e10cSrcweir                 ::rtl::OUString::createFromAscii( "com.sun.star.system.SystemShellExecute" )), UNO_QUERY );
274*cdf0e10cSrcweir             if ( xSystemShellExecute.is() )
275*cdf0e10cSrcweir             {
276*cdf0e10cSrcweir                 try
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir                 {
279*cdf0e10cSrcweir                     xSystemShellExecute->execute( sURL, ::rtl::OUString(), SystemShellExecuteFlags::DEFAULTS );
280*cdf0e10cSrcweir                 }
281*cdf0e10cSrcweir                 catch( Exception& rEx )
282*cdf0e10cSrcweir                 {
283*cdf0e10cSrcweir                     (void)rEx;
284*cdf0e10cSrcweir                 }
285*cdf0e10cSrcweir             }
286*cdf0e10cSrcweir         }
287*cdf0e10cSrcweir 		else if ( !aURL.Path.compareToAscii("Command2" ) )
288*cdf0e10cSrcweir 		{
289*cdf0e10cSrcweir             // remove the text if it's in our list
290*cdf0e10cSrcweir             Sequence< NamedValue > aRemoveArgs( 1 );
291*cdf0e10cSrcweir             aRemoveArgs[0].Name  = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" ));
292*cdf0e10cSrcweir             aRemoveArgs[0].Value <<= maComboBoxText;
293*cdf0e10cSrcweir             SendCommand( aURL, ::rtl::OUString::createFromAscii( "RemoveEntryText" ), aRemoveArgs, sal_True );
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir             // add the new text to the start of the list
296*cdf0e10cSrcweir             Sequence< NamedValue > aInsertArgs( 2 );
297*cdf0e10cSrcweir             aInsertArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Pos" ));
298*cdf0e10cSrcweir             aInsertArgs[0].Value <<= sal_Int32( 0 );
299*cdf0e10cSrcweir             aInsertArgs[1].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" ));
300*cdf0e10cSrcweir             aInsertArgs[1].Value <<= maComboBoxText;
301*cdf0e10cSrcweir             SendCommand( aURL, ::rtl::OUString::createFromAscii( "InsertEntry" ), aInsertArgs, sal_True );
302*cdf0e10cSrcweir         }
303*cdf0e10cSrcweir 		else if ( !aURL.Path.compareToAscii("Command3" ) )
304*cdf0e10cSrcweir 		{
305*cdf0e10cSrcweir             // Retrieve the text argument from the sequence property value
306*cdf0e10cSrcweir             rtl::OUString aText;
307*cdf0e10cSrcweir             for ( sal_Int32 i = 0; i < lArgs.getLength(); i++ )
308*cdf0e10cSrcweir             {
309*cdf0e10cSrcweir                 if ( lArgs[i].Name.equalsAsciiL( "Text", 4 ))
310*cdf0e10cSrcweir                 {
311*cdf0e10cSrcweir                     lArgs[i].Value >>= aText;
312*cdf0e10cSrcweir                     break;
313*cdf0e10cSrcweir                 }
314*cdf0e10cSrcweir             }
315*cdf0e10cSrcweir 
316*cdf0e10cSrcweir             // create new URL to address the combox box
317*cdf0e10cSrcweir             URL aCmdURL;
318*cdf0e10cSrcweir             aCmdURL.Path = rtl::OUString::createFromAscii( "Command2" );
319*cdf0e10cSrcweir             aCmdURL.Protocol = rtl::OUString::createFromAscii( "vnd.demo.complextoolbarcontrols.demoaddon:" );
320*cdf0e10cSrcweir             aCmdURL.Complete = aCmdURL.Path + aCmdURL.Protocol;
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir             // set the selected item as text into the combobox
323*cdf0e10cSrcweir             Sequence< NamedValue > aArgs( 1 );
324*cdf0e10cSrcweir             aArgs[0].Name = rtl::OUString::createFromAscii( "Text" );
325*cdf0e10cSrcweir             aArgs[0].Value <<= aText;
326*cdf0e10cSrcweir             SendCommand( aCmdURL, ::rtl::OUString::createFromAscii( "SetText" ), aArgs, sal_True );
327*cdf0e10cSrcweir 		}
328*cdf0e10cSrcweir 		else if ( !aURL.Path.compareToAscii("Command4" ) )
329*cdf0e10cSrcweir 		{
330*cdf0e10cSrcweir             // Retrieve the text argument from the sequence property value
331*cdf0e10cSrcweir             rtl::OUString aText;
332*cdf0e10cSrcweir             for ( sal_Int32 i = 0; i < lArgs.getLength(); i++ )
333*cdf0e10cSrcweir             {
334*cdf0e10cSrcweir                 if ( lArgs[i].Name.equalsAsciiL( "Text", 4 ))
335*cdf0e10cSrcweir                 {
336*cdf0e10cSrcweir                     lArgs[i].Value >>= aText;
337*cdf0e10cSrcweir                     break;
338*cdf0e10cSrcweir                 }
339*cdf0e10cSrcweir             }
340*cdf0e10cSrcweir 
341*cdf0e10cSrcweir             // just enable this command
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir             // set enable flag according to selection
344*cdf0e10cSrcweir             if ( aText.equalsAscii( "Button Disabled" ))
345*cdf0e10cSrcweir                 mbButtonEnabled = sal_False;
346*cdf0e10cSrcweir             else
347*cdf0e10cSrcweir                 mbButtonEnabled = sal_True;
348*cdf0e10cSrcweir 
349*cdf0e10cSrcweir             // create new URL to address the image button
350*cdf0e10cSrcweir             URL aCmdURL;
351*cdf0e10cSrcweir             aCmdURL.Path = rtl::OUString::createFromAscii( "Command1" );
352*cdf0e10cSrcweir             aCmdURL.Protocol = rtl::OUString::createFromAscii( "vnd.demo.complextoolbarcontrols.demoaddon:" );
353*cdf0e10cSrcweir             aCmdURL.Complete = aCmdURL.Path + aCmdURL.Protocol;
354*cdf0e10cSrcweir 
355*cdf0e10cSrcweir             // create and initialize FeatureStateEvent with IsEnabled
356*cdf0e10cSrcweir             ::com::sun::star::frame::FeatureStateEvent aEvent;
357*cdf0e10cSrcweir 			aEvent.FeatureURL = aCmdURL;
358*cdf0e10cSrcweir 		    aEvent.Source = (::com::sun::star::frame::XDispatch*) this;
359*cdf0e10cSrcweir 			aEvent.IsEnabled = mbButtonEnabled;
360*cdf0e10cSrcweir 			aEvent.Requery = sal_False;
361*cdf0e10cSrcweir 			aEvent.State <<= Any();
362*cdf0e10cSrcweir 
363*cdf0e10cSrcweir             // Notify listener about new state
364*cdf0e10cSrcweir             Reference < XDispatch > xDispatch = aListenerHelper.GetDispatch( mxFrame, aURL.Path );
365*cdf0e10cSrcweir             aListenerHelper.Notify( mxFrame, aEvent.FeatureURL.Path, aEvent );
366*cdf0e10cSrcweir         }
367*cdf0e10cSrcweir         else if ( !aURL.Path.compareToAscii("Command5" ) )
368*cdf0e10cSrcweir         {
369*cdf0e10cSrcweir         }
370*cdf0e10cSrcweir 	}
371*cdf0e10cSrcweir }
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir void SAL_CALL BaseDispatch::addStatusListener( const Reference< XStatusListener >& xControl, const URL& aURL ) throw (RuntimeException)
374*cdf0e10cSrcweir {
375*cdf0e10cSrcweir 	if ( aURL.Protocol.equalsAscii("vnd.demo.complextoolbarcontrols.demoaddon:") )
376*cdf0e10cSrcweir 	{
377*cdf0e10cSrcweir 		if ( aURL.Path.equalsAscii("Command1" ) )
378*cdf0e10cSrcweir         {
379*cdf0e10cSrcweir 			// just enable this command
380*cdf0e10cSrcweir 		    ::com::sun::star::frame::FeatureStateEvent aEvent;
381*cdf0e10cSrcweir 			aEvent.FeatureURL = aURL;
382*cdf0e10cSrcweir 		    aEvent.Source = (::com::sun::star::frame::XDispatch*) this;
383*cdf0e10cSrcweir 			aEvent.IsEnabled = mbButtonEnabled;
384*cdf0e10cSrcweir 			aEvent.Requery = sal_False;
385*cdf0e10cSrcweir 			aEvent.State <<= Any();
386*cdf0e10cSrcweir 			xControl->statusChanged( aEvent );
387*cdf0e10cSrcweir         }
388*cdf0e10cSrcweir         else if ( aURL.Path.equalsAscii("Command2" ) )
389*cdf0e10cSrcweir 		{
390*cdf0e10cSrcweir 			// just enable this command
391*cdf0e10cSrcweir 		    ::com::sun::star::frame::FeatureStateEvent aEvent;
392*cdf0e10cSrcweir 			aEvent.FeatureURL = aURL;
393*cdf0e10cSrcweir 		    aEvent.Source = (::com::sun::star::frame::XDispatch*) this;
394*cdf0e10cSrcweir 			aEvent.IsEnabled = sal_True;
395*cdf0e10cSrcweir 			aEvent.Requery = sal_False;
396*cdf0e10cSrcweir 			aEvent.State <<= Any();
397*cdf0e10cSrcweir 			xControl->statusChanged( aEvent );
398*cdf0e10cSrcweir 		}
399*cdf0e10cSrcweir 		else if ( aURL.Path.equalsAscii("Command3" ) )
400*cdf0e10cSrcweir 		{
401*cdf0e10cSrcweir             // A toggle dropdown box is normally used for a group of commands
402*cdf0e10cSrcweir             // where the user can select the last issued command easily.
403*cdf0e10cSrcweir             // E.g. a typical command group would be "Insert shape"
404*cdf0e10cSrcweir             Sequence< NamedValue > aArgs( 1 );
405*cdf0e10cSrcweir 
406*cdf0e10cSrcweir             // send command to set context menu content
407*cdf0e10cSrcweir             Sequence< rtl::OUString > aContextMenu( 3 );
408*cdf0e10cSrcweir             aContextMenu[0] = rtl::OUString::createFromAscii( "Command 1" );
409*cdf0e10cSrcweir             aContextMenu[1] = rtl::OUString::createFromAscii( "Command 2" );
410*cdf0e10cSrcweir             aContextMenu[2] = rtl::OUString::createFromAscii( "Command 3" );
411*cdf0e10cSrcweir 
412*cdf0e10cSrcweir             aArgs[0].Name = rtl::OUString::createFromAscii( "List" );
413*cdf0e10cSrcweir             aArgs[0].Value <<= aContextMenu;
414*cdf0e10cSrcweir             SendCommandTo( xControl, aURL, rtl::OUString::createFromAscii( "SetList" ), aArgs, sal_True );
415*cdf0e10cSrcweir 
416*cdf0e10cSrcweir             // send command to check item on pos=0
417*cdf0e10cSrcweir             aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Pos" ));
418*cdf0e10cSrcweir             aArgs[0].Value <<= sal_Int32( 0 );
419*cdf0e10cSrcweir             SendCommandTo( xControl, aURL, ::rtl::OUString::createFromAscii( "CheckItemPos" ), aArgs, sal_True );
420*cdf0e10cSrcweir         }
421*cdf0e10cSrcweir         else if ( aURL.Path.equalsAscii("Command4" ) )
422*cdf0e10cSrcweir         {
423*cdf0e10cSrcweir             // A dropdown box is normally used for a group of dependent modes, where
424*cdf0e10cSrcweir             // the user can only select one. The modes cannot be combined.
425*cdf0e10cSrcweir             // E.g. a typical group would be left,right,center,block.
426*cdf0e10cSrcweir             Sequence< NamedValue > aArgs( 1 );
427*cdf0e10cSrcweir 
428*cdf0e10cSrcweir             // send command to set context menu content
429*cdf0e10cSrcweir             Sequence< rtl::OUString > aContextMenu( 2 );
430*cdf0e10cSrcweir             aContextMenu[0] = rtl::OUString::createFromAscii( "Button Enabled" );
431*cdf0e10cSrcweir             aContextMenu[1] = rtl::OUString::createFromAscii( "Button Disabled" );
432*cdf0e10cSrcweir 
433*cdf0e10cSrcweir             aArgs[0].Name = rtl::OUString::createFromAscii( "List" );
434*cdf0e10cSrcweir             aArgs[0].Value <<= aContextMenu;
435*cdf0e10cSrcweir             SendCommandTo( xControl, aURL, rtl::OUString::createFromAscii( "SetList" ), aArgs, sal_True );
436*cdf0e10cSrcweir 
437*cdf0e10cSrcweir             // set position according to enable/disable state of button
438*cdf0e10cSrcweir             sal_Int32 nPos( mbButtonEnabled ? 0 : 1 );
439*cdf0e10cSrcweir 
440*cdf0e10cSrcweir             // send command to check item on pos=0
441*cdf0e10cSrcweir             aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Pos" ));
442*cdf0e10cSrcweir             aArgs[0].Value <<= nPos;
443*cdf0e10cSrcweir             SendCommandTo( xControl, aURL, ::rtl::OUString::createFromAscii( "CheckItemPos" ), aArgs, sal_True );
444*cdf0e10cSrcweir         }
445*cdf0e10cSrcweir         else if ( aURL.Path.equalsAscii("Command5" ) )
446*cdf0e10cSrcweir         {
447*cdf0e10cSrcweir             // A spin button
448*cdf0e10cSrcweir             Sequence< NamedValue > aArgs( 5 );
449*cdf0e10cSrcweir 
450*cdf0e10cSrcweir             // send command to initialize spin button
451*cdf0e10cSrcweir             aArgs[0].Name = rtl::OUString::createFromAscii( "Value" );
452*cdf0e10cSrcweir             aArgs[0].Value <<= double( 0.0 );
453*cdf0e10cSrcweir             aArgs[1].Name = rtl::OUString::createFromAscii( "UpperLimit" );
454*cdf0e10cSrcweir             aArgs[1].Value <<= double( 10.0 );
455*cdf0e10cSrcweir             aArgs[2].Name = rtl::OUString::createFromAscii( "LowerLimit" );
456*cdf0e10cSrcweir             aArgs[2].Value <<= double( 0.0 );
457*cdf0e10cSrcweir             aArgs[3].Name = rtl::OUString::createFromAscii( "Step" );
458*cdf0e10cSrcweir             aArgs[3].Value <<= double( 0.1 );
459*cdf0e10cSrcweir             aArgs[4].Name = rtl::OUString::createFromAscii( "OutputFormat" );
460*cdf0e10cSrcweir             aArgs[4].Value <<= rtl::OUString::createFromAscii( "%.2f cm" );
461*cdf0e10cSrcweir 
462*cdf0e10cSrcweir             SendCommandTo( xControl, aURL, rtl::OUString::createFromAscii( "SetValues" ), aArgs, sal_True );
463*cdf0e10cSrcweir         }
464*cdf0e10cSrcweir         else if ( aURL.Path.equalsAscii("Command7" ) )
465*cdf0e10cSrcweir         {
466*cdf0e10cSrcweir             // A dropdown box is normally used for a group of commands
467*cdf0e10cSrcweir             // where the user can select one of a defined set.
468*cdf0e10cSrcweir             Sequence< NamedValue > aArgs( 1 );
469*cdf0e10cSrcweir 
470*cdf0e10cSrcweir             // send command to set context menu content
471*cdf0e10cSrcweir             Sequence< rtl::OUString > aList( 10 );
472*cdf0e10cSrcweir             aList[0] = rtl::OUString::createFromAscii( "White" );
473*cdf0e10cSrcweir             aList[1] = rtl::OUString::createFromAscii( "Black" );
474*cdf0e10cSrcweir             aList[2] = rtl::OUString::createFromAscii( "Red" );
475*cdf0e10cSrcweir             aList[3] = rtl::OUString::createFromAscii( "Blue" );
476*cdf0e10cSrcweir             aList[4] = rtl::OUString::createFromAscii( "Green" );
477*cdf0e10cSrcweir             aList[5] = rtl::OUString::createFromAscii( "Grey" );
478*cdf0e10cSrcweir             aList[6] = rtl::OUString::createFromAscii( "Yellow" );
479*cdf0e10cSrcweir             aList[7] = rtl::OUString::createFromAscii( "Orange" );
480*cdf0e10cSrcweir             aList[8] = rtl::OUString::createFromAscii( "Brown" );
481*cdf0e10cSrcweir             aList[9] = rtl::OUString::createFromAscii( "Pink" );
482*cdf0e10cSrcweir 
483*cdf0e10cSrcweir             aArgs[0].Name = rtl::OUString::createFromAscii( "List" );
484*cdf0e10cSrcweir             aArgs[0].Value <<= aList;
485*cdf0e10cSrcweir             SendCommandTo( xControl, aURL, rtl::OUString::createFromAscii( "SetList" ), aArgs, sal_True );
486*cdf0e10cSrcweir         }
487*cdf0e10cSrcweir 
488*cdf0e10cSrcweir 		aListenerHelper.AddListener( mxFrame, xControl, aURL.Path );
489*cdf0e10cSrcweir 	}
490*cdf0e10cSrcweir }
491*cdf0e10cSrcweir 
492*cdf0e10cSrcweir void SAL_CALL BaseDispatch::removeStatusListener( const Reference< XStatusListener >& xControl, const URL& aURL ) throw (RuntimeException)
493*cdf0e10cSrcweir {
494*cdf0e10cSrcweir 	aListenerHelper.RemoveListener( mxFrame, xControl, aURL.Path );
495*cdf0e10cSrcweir }
496*cdf0e10cSrcweir 
497*cdf0e10cSrcweir void SAL_CALL BaseDispatch::controlEvent( const ControlEvent& Event ) throw (RuntimeException)
498*cdf0e10cSrcweir {
499*cdf0e10cSrcweir     if ( Event.aURL.Protocol.equalsAscii("vnd.demo.complextoolbarcontrols.demoaddon:" ))
500*cdf0e10cSrcweir 	{
501*cdf0e10cSrcweir         if ( Event.aURL.Path.equalsAscii( "Command2" ))
502*cdf0e10cSrcweir         {
503*cdf0e10cSrcweir             // We get notifications whenever the text inside the combobox has been changed.
504*cdf0e10cSrcweir             // We store the new text into a member.
505*cdf0e10cSrcweir             if ( Event.Event.equalsAscii( "TextChanged" ))
506*cdf0e10cSrcweir             {
507*cdf0e10cSrcweir                 rtl::OUString aNewText;
508*cdf0e10cSrcweir                 sal_Bool      bHasText( sal_False );
509*cdf0e10cSrcweir                 for ( sal_Int32 i = 0; i < Event.aInformation.getLength(); i++ )
510*cdf0e10cSrcweir                 {
511*cdf0e10cSrcweir                     if ( Event.aInformation[i].Name.equalsAsciiL( "Text", 4 ))
512*cdf0e10cSrcweir                     {
513*cdf0e10cSrcweir                         bHasText = Event.aInformation[i].Value >>= aNewText;
514*cdf0e10cSrcweir                         break;
515*cdf0e10cSrcweir                     }
516*cdf0e10cSrcweir                 }
517*cdf0e10cSrcweir 
518*cdf0e10cSrcweir                 if ( bHasText )
519*cdf0e10cSrcweir                     maComboBoxText = aNewText;
520*cdf0e10cSrcweir             }
521*cdf0e10cSrcweir         }
522*cdf0e10cSrcweir     }
523*cdf0e10cSrcweir }
524*cdf0e10cSrcweir 
525*cdf0e10cSrcweir BaseDispatch::BaseDispatch( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > &rxMSF,
526*cdf0e10cSrcweir         const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame, const ::rtl::OUString& rServiceName )
527*cdf0e10cSrcweir         : mxMSF( rxMSF )
528*cdf0e10cSrcweir 		, mxFrame( xFrame )
529*cdf0e10cSrcweir         , msDocService( rServiceName )
530*cdf0e10cSrcweir         , mbButtonEnabled( sal_True )
531*cdf0e10cSrcweir {
532*cdf0e10cSrcweir }
533*cdf0e10cSrcweir 
534*cdf0e10cSrcweir 
535*cdf0e10cSrcweir BaseDispatch::~BaseDispatch()
536*cdf0e10cSrcweir {
537*cdf0e10cSrcweir 	mxFrame.clear();
538*cdf0e10cSrcweir 	mxMSF.clear();
539*cdf0e10cSrcweir }
540