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