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