1*6b3ad84fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*6b3ad84fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*6b3ad84fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*6b3ad84fSAndrew Rist  * distributed with this work for additional information
6*6b3ad84fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*6b3ad84fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*6b3ad84fSAndrew Rist  * "License"); you may not use this file except in compliance
9*6b3ad84fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*6b3ad84fSAndrew Rist  *
11*6b3ad84fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*6b3ad84fSAndrew Rist  *
13*6b3ad84fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*6b3ad84fSAndrew Rist  * software distributed under the License is distributed on an
15*6b3ad84fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*6b3ad84fSAndrew Rist  * KIND, either express or implied.  See the License for the
17*6b3ad84fSAndrew Rist  * specific language governing permissions and limitations
18*6b3ad84fSAndrew Rist  * under the License.
19*6b3ad84fSAndrew Rist  *
20*6b3ad84fSAndrew Rist  *************************************************************/
21*6b3ad84fSAndrew Rist 
22*6b3ad84fSAndrew 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>
33cb2654afSPedro Giffuni #include <com/sun/star/system/XSystemShellExecute.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;
43cb2654afSPedro Giffuni using com::sun::star::lang::XMultiServiceFactory;
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 
50cdf0e10cSrcweir void BaseDispatch::ShowMessageBox( const Reference< XFrame >& rFrame, const ::rtl::OUString& aTitle, const ::rtl::OUString& aMsgText )
51cdf0e10cSrcweir {
52cdf0e10cSrcweir 	if ( !mxToolkit.is() )
53cdf0e10cSrcweir 		mxToolkit = Reference< XToolkit > ( mxMSF->createInstance(
54cb2654afSPedro Giffuni 			::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.Toolkit" ))), UNO_QUERY );
55cb2654afSPedro Giffuni 
56cb2654afSPedro Giffuni     Reference< XMessageBoxFactory > xMsgBoxFactory( mxToolkit, UNO_QUERY );
57cb2654afSPedro Giffuni     if ( rFrame.is() && xMsgBoxFactory.is() )
58cdf0e10cSrcweir     {
59cb2654afSPedro Giffuni         Reference< XMessageBox > xMsgBox = xMsgBoxFactory->createMessageBox(
60cb2654afSPedro Giffuni             Reference< XWindowPeer >( rFrame->getContainerWindow(), UNO_QUERY ),
61cb2654afSPedro Giffuni             Rectangle(0,0,300,200),
62cb2654afSPedro Giffuni             rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "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 
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 
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 
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 
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 			{
149cdf0e10cSrcweir 				xRet = xCursor.is() ? (BaseDispatch*) new WriterDispatch( mxMSF, mxFrame ) :
150cdf0e10cSrcweir 					(BaseDispatch*) new CalcDispatch( mxMSF, mxFrame );
151cdf0e10cSrcweir 				aListenerHelper.AddDispatch( xRet, mxFrame, aURL.Path );
152cdf0e10cSrcweir 			}
153cdf0e10cSrcweir 		}
154cdf0e10cSrcweir 	}
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 	return xRet;
157cdf0e10cSrcweir }
158cdf0e10cSrcweir 
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 
171cdf0e10cSrcweir ::rtl::OUString MyProtocolHandler_getImplementationName ()
172cdf0e10cSrcweir 	throw (RuntimeException)
173cdf0e10cSrcweir {
174cb2654afSPedro Giffuni     return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( MYPROTOCOLHANDLER_IMPLEMENTATIONNAME ) );
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
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 
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 
196cdf0e10cSrcweir Reference< XInterface > SAL_CALL MyProtocolHandler_createInstance( const Reference< XMultiServiceFactory > & rSMgr)
197cdf0e10cSrcweir 	throw( Exception )
198cdf0e10cSrcweir {
199cdf0e10cSrcweir 	return (cppu::OWeakObject*) new MyProtocolHandler( rSMgr );
200cdf0e10cSrcweir }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir // XServiceInfo
203cdf0e10cSrcweir ::rtl::OUString SAL_CALL MyProtocolHandler::getImplementationName(  )
204cdf0e10cSrcweir 	throw (RuntimeException)
205cdf0e10cSrcweir {
206cdf0e10cSrcweir 	return MyProtocolHandler_getImplementationName();
207cdf0e10cSrcweir }
208cdf0e10cSrcweir 
209cdf0e10cSrcweir sal_Bool SAL_CALL MyProtocolHandler::supportsService( const ::rtl::OUString& rServiceName )
210cdf0e10cSrcweir 	throw (RuntimeException)
211cdf0e10cSrcweir {
212cdf0e10cSrcweir     return MyProtocolHandler_supportsService( rServiceName );
213cdf0e10cSrcweir }
214cdf0e10cSrcweir 
215cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL MyProtocolHandler::getSupportedServiceNames(  )
216cdf0e10cSrcweir 	throw (RuntimeException)
217cdf0e10cSrcweir {
218cdf0e10cSrcweir     return MyProtocolHandler_getSupportedServiceNames();
219cdf0e10cSrcweir }
220cdf0e10cSrcweir 
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 		{
236cdf0e10cSrcweir             // open the OpenOffice.org web page
237cb2654afSPedro Giffuni             ::rtl::OUString sURL( RTL_CONSTASCII_USTRINGPARAM( "http://www.openoffice.org" ) );
238cdf0e10cSrcweir             Reference< XSystemShellExecute > xSystemShellExecute( mxMSF->createInstance(
239cb2654afSPedro Giffuni                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.system.SystemShellExecute" ) ) ), UNO_QUERY );
240cdf0e10cSrcweir             if ( xSystemShellExecute.is() )
241cb2654afSPedro Giffuni             {
242cdf0e10cSrcweir                 try
243cdf0e10cSrcweir                 {
244cdf0e10cSrcweir                     xSystemShellExecute->execute( sURL, ::rtl::OUString(), SystemShellExecuteFlags::DEFAULTS );
245cb2654afSPedro Giffuni                 }
246cdf0e10cSrcweir                 catch( Exception& rEx )
247cdf0e10cSrcweir                 {
248cdf0e10cSrcweir                     (void)rEx;
249cdf0e10cSrcweir                 }
250cdf0e10cSrcweir             }
251cdf0e10cSrcweir         }
252cb2654afSPedro Giffuni 		else if ( !aURL.Path.compareToAscii( RTL_CONSTASCII_STRINGPARAM( "ComboboxCmd" ) ) )
253cdf0e10cSrcweir 		{
254cdf0e10cSrcweir             // remove the text if it's in our list
255cdf0e10cSrcweir             Sequence< NamedValue > aRemoveArgs( 1 );
256cdf0e10cSrcweir             aRemoveArgs[0].Name  = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" ));
257cdf0e10cSrcweir             aRemoveArgs[0].Value <<= maComboBoxText;
258cb2654afSPedro Giffuni             SendCommand( aURL, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "RemoveEntryText" ) ), aRemoveArgs, sal_True );
259cdf0e10cSrcweir 
260cdf0e10cSrcweir             // add the new text to the start of the list
261cdf0e10cSrcweir             Sequence< NamedValue > aInsertArgs( 2 );
262cdf0e10cSrcweir             aInsertArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Pos" ));
263cdf0e10cSrcweir             aInsertArgs[0].Value <<= sal_Int32( 0 );
264cdf0e10cSrcweir             aInsertArgs[1].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" ));
265cdf0e10cSrcweir             aInsertArgs[1].Value <<= maComboBoxText;
266cb2654afSPedro Giffuni             SendCommand( aURL, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "InsertEntry" ) ), aInsertArgs, sal_True );
267cdf0e10cSrcweir         }
268cb2654afSPedro Giffuni 		else if ( !aURL.Path.compareToAscii( RTL_CONSTASCII_STRINGPARAM( "ToggleDropdownButtonCmd" ) ) )
269cdf0e10cSrcweir 		{
270cdf0e10cSrcweir             // Retrieve the text argument from the sequence property value
271cdf0e10cSrcweir             rtl::OUString aText;
272cdf0e10cSrcweir             for ( sal_Int32 i = 0; i < lArgs.getLength(); i++ )
273cdf0e10cSrcweir             {
274cb2654afSPedro Giffuni                 if ( lArgs[i].Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Text" ) ) )
275cdf0e10cSrcweir                 {
276cdf0e10cSrcweir                     lArgs[i].Value >>= aText;
277cdf0e10cSrcweir                     break;
278cdf0e10cSrcweir                 }
279cdf0e10cSrcweir             }
280cdf0e10cSrcweir 
281cdf0e10cSrcweir             // create new URL to address the combox box
282cdf0e10cSrcweir             URL aCmdURL;
283cb2654afSPedro Giffuni             aCmdURL.Path = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ComboboxCmd" ) );
284cb2654afSPedro Giffuni             aCmdURL.Protocol = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "vnd.demo.complextoolbarcontrols.demoaddon:" ) );
285cdf0e10cSrcweir             aCmdURL.Complete = aCmdURL.Path + aCmdURL.Protocol;
286cb2654afSPedro Giffuni 
287cdf0e10cSrcweir             // set the selected item as text into the combobox
288cdf0e10cSrcweir             Sequence< NamedValue > aArgs( 1 );
289cb2654afSPedro Giffuni             aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" ) );
290cdf0e10cSrcweir             aArgs[0].Value <<= aText;
291cb2654afSPedro Giffuni             SendCommand( aCmdURL, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SetText" ) ), aArgs, sal_True );
292cdf0e10cSrcweir 		}
293cb2654afSPedro Giffuni 		else if ( !aURL.Path.compareToAscii( RTL_CONSTASCII_STRINGPARAM( "DropdownButtonCmd" ) ) )
294cdf0e10cSrcweir 		{
295cdf0e10cSrcweir             // Retrieve the text argument from the sequence property value
296cdf0e10cSrcweir             rtl::OUString aText;
297cdf0e10cSrcweir             for ( sal_Int32 i = 0; i < lArgs.getLength(); i++ )
298cdf0e10cSrcweir             {
299cb2654afSPedro Giffuni                 if ( lArgs[i].Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Text" ) ) )
300cdf0e10cSrcweir                 {
301cdf0e10cSrcweir                     lArgs[i].Value >>= aText;
302cdf0e10cSrcweir                     break;
303cdf0e10cSrcweir                 }
304cdf0e10cSrcweir             }
305cdf0e10cSrcweir 
306cdf0e10cSrcweir             // just enable this command
307cdf0e10cSrcweir 
308cdf0e10cSrcweir             // set enable flag according to selection
309cb2654afSPedro Giffuni             if ( aText.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Button Disabled" ) ))
310cdf0e10cSrcweir                 mbButtonEnabled = sal_False;
311cdf0e10cSrcweir             else
312cdf0e10cSrcweir                 mbButtonEnabled = sal_True;
313cdf0e10cSrcweir 
314cdf0e10cSrcweir             // create new URL to address the image button
315cdf0e10cSrcweir             URL aCmdURL;
316cb2654afSPedro Giffuni             aCmdURL.Path = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ImageButtonCmd" ) );
317cb2654afSPedro Giffuni             aCmdURL.Protocol = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "vnd.demo.complextoolbarcontrols.demoaddon:" ) );
318cdf0e10cSrcweir             aCmdURL.Complete = aCmdURL.Path + aCmdURL.Protocol;
319cb2654afSPedro Giffuni 
320cdf0e10cSrcweir             // create and initialize FeatureStateEvent with IsEnabled
321cdf0e10cSrcweir             ::com::sun::star::frame::FeatureStateEvent aEvent;
322cdf0e10cSrcweir 			aEvent.FeatureURL = aCmdURL;
323cdf0e10cSrcweir 		    aEvent.Source = (::com::sun::star::frame::XDispatch*) this;
324cdf0e10cSrcweir 			aEvent.IsEnabled = mbButtonEnabled;
325cdf0e10cSrcweir 			aEvent.Requery = sal_False;
326cdf0e10cSrcweir 			aEvent.State <<= Any();
327cb2654afSPedro Giffuni 
328cdf0e10cSrcweir             // Notify listener about new state
329cdf0e10cSrcweir             Reference < XDispatch > xDispatch = aListenerHelper.GetDispatch( mxFrame, aURL.Path );
330cdf0e10cSrcweir             aListenerHelper.Notify( mxFrame, aEvent.FeatureURL.Path, aEvent );
331cdf0e10cSrcweir         }
332cb2654afSPedro Giffuni         else if ( !aURL.Path.compareToAscii( RTL_CONSTASCII_STRINGPARAM( "SpinfieldCmd" ) ) )
333cdf0e10cSrcweir         {
334cdf0e10cSrcweir         }
335cb2654afSPedro Giffuni         else if ( !aURL.Path.compareToAscii( RTL_CONSTASCII_STRINGPARAM( "DropdownboxCmd" ) ) )
336cb2654afSPedro Giffuni         {
337cb2654afSPedro Giffuni             // Retrieve the text argument from the sequence property value
338cb2654afSPedro Giffuni             rtl::OUString aText;
339cb2654afSPedro Giffuni             for ( sal_Int32 i = 0; i < lArgs.getLength(); i++ )
340cb2654afSPedro Giffuni             {
341cb2654afSPedro Giffuni                 if ( lArgs[i].Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Text" ) ) )
342cb2654afSPedro Giffuni                 {
343cb2654afSPedro Giffuni                     lArgs[i].Value >>= aText;
344cb2654afSPedro Giffuni                     break;
345cb2654afSPedro Giffuni                 }
346cb2654afSPedro Giffuni             }
347cb2654afSPedro Giffuni             OSL_TRACE( "Dropdownbox control - selected entry text : %s",
348cb2654afSPedro Giffuni                        rtl::OUStringToOString( aText, RTL_TEXTENCODING_UTF8 ).getStr() );
349cb2654afSPedro Giffuni         }
350cdf0e10cSrcweir 	}
351cdf0e10cSrcweir }
352cdf0e10cSrcweir 
353cdf0e10cSrcweir void SAL_CALL BaseDispatch::addStatusListener( const Reference< XStatusListener >& xControl, const URL& aURL ) throw (RuntimeException)
354cdf0e10cSrcweir {
355cb2654afSPedro Giffuni 	if ( aURL.Protocol.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "vnd.demo.complextoolbarcontrols.demoaddon:" ) ) )
356cdf0e10cSrcweir 	{
357cb2654afSPedro Giffuni 		if ( aURL.Path.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "ImageButtonCmd" ) ) )
358cdf0e10cSrcweir         {
359cdf0e10cSrcweir 			// just enable this command
360cdf0e10cSrcweir 		    ::com::sun::star::frame::FeatureStateEvent aEvent;
361cdf0e10cSrcweir 			aEvent.FeatureURL = aURL;
362cdf0e10cSrcweir 		    aEvent.Source = (::com::sun::star::frame::XDispatch*) this;
363cdf0e10cSrcweir 			aEvent.IsEnabled = mbButtonEnabled;
364cdf0e10cSrcweir 			aEvent.Requery = sal_False;
365cdf0e10cSrcweir 			aEvent.State <<= Any();
366cdf0e10cSrcweir 			xControl->statusChanged( aEvent );
367cdf0e10cSrcweir         }
368cb2654afSPedro Giffuni         else if ( aURL.Path.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "ComboboxCmd" ) ) )
369cdf0e10cSrcweir 		{
370cdf0e10cSrcweir 			// just enable this command
371cdf0e10cSrcweir 		    ::com::sun::star::frame::FeatureStateEvent aEvent;
372cdf0e10cSrcweir 			aEvent.FeatureURL = aURL;
373cdf0e10cSrcweir 		    aEvent.Source = (::com::sun::star::frame::XDispatch*) this;
374cdf0e10cSrcweir 			aEvent.IsEnabled = sal_True;
375cdf0e10cSrcweir 			aEvent.Requery = sal_False;
376cdf0e10cSrcweir 			aEvent.State <<= Any();
377cdf0e10cSrcweir 			xControl->statusChanged( aEvent );
378cdf0e10cSrcweir 		}
379cb2654afSPedro Giffuni 		else if ( aURL.Path.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "ToggleDropdownButtonCmd" ) ) )
380cdf0e10cSrcweir 		{
381cdf0e10cSrcweir             // A toggle dropdown box is normally used for a group of commands
382cdf0e10cSrcweir             // where the user can select the last issued command easily.
383cdf0e10cSrcweir             // E.g. a typical command group would be "Insert shape"
384cdf0e10cSrcweir             Sequence< NamedValue > aArgs( 1 );
385cb2654afSPedro Giffuni 
386cdf0e10cSrcweir             // send command to set context menu content
387cdf0e10cSrcweir             Sequence< rtl::OUString > aContextMenu( 3 );
388cb2654afSPedro Giffuni             aContextMenu[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Command 1" ) );
389cb2654afSPedro Giffuni             aContextMenu[1] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Command 2" ) );
390cb2654afSPedro Giffuni             aContextMenu[2] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Command 3" ) );
391cb2654afSPedro Giffuni 
392cb2654afSPedro Giffuni             aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "List" ) );
393cdf0e10cSrcweir             aArgs[0].Value <<= aContextMenu;
394cb2654afSPedro Giffuni             SendCommandTo( xControl, aURL, rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SetList" ) ), aArgs, sal_True );
395cb2654afSPedro Giffuni 
396cdf0e10cSrcweir             // send command to check item on pos=0
397cdf0e10cSrcweir             aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Pos" ));
398cb2654afSPedro Giffuni             aArgs[0].Value <<= sal_Int32( 0 );
399cb2654afSPedro Giffuni             SendCommandTo( xControl, aURL, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CheckItemPos" ) ), aArgs, sal_True );
400cdf0e10cSrcweir         }
401cb2654afSPedro Giffuni         else if ( aURL.Path.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "DropdownButtonCmd" ) ) )
402cdf0e10cSrcweir         {
403cdf0e10cSrcweir             // A dropdown box is normally used for a group of dependent modes, where
404cdf0e10cSrcweir             // the user can only select one. The modes cannot be combined.
405cdf0e10cSrcweir             // E.g. a typical group would be left,right,center,block.
406cdf0e10cSrcweir             Sequence< NamedValue > aArgs( 1 );
407cb2654afSPedro Giffuni 
408cdf0e10cSrcweir             // send command to set context menu content
409cdf0e10cSrcweir             Sequence< rtl::OUString > aContextMenu( 2 );
410cb2654afSPedro Giffuni             aContextMenu[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Button Enabled" ) );
411cb2654afSPedro Giffuni             aContextMenu[1] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Button Disabled" ) );
412cb2654afSPedro Giffuni 
413cb2654afSPedro Giffuni             aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "List" ) );
414cdf0e10cSrcweir             aArgs[0].Value <<= aContextMenu;
415cb2654afSPedro Giffuni             SendCommandTo( xControl, aURL, rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SetList" ) ), aArgs, sal_True );
416cdf0e10cSrcweir 
417cdf0e10cSrcweir             // set position according to enable/disable state of button
418cdf0e10cSrcweir             sal_Int32 nPos( mbButtonEnabled ? 0 : 1 );
419cb2654afSPedro Giffuni 
420cdf0e10cSrcweir             // send command to check item on pos=0
421cb2654afSPedro Giffuni             aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Pos" ) );
422cdf0e10cSrcweir             aArgs[0].Value <<= nPos;
423cb2654afSPedro Giffuni             SendCommandTo( xControl, aURL, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CheckItemPos" ) ), aArgs, sal_True );
424cdf0e10cSrcweir         }
425cb2654afSPedro Giffuni         else if ( aURL.Path.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "SpinfieldCmd" ) ) )
426cdf0e10cSrcweir         {
427cdf0e10cSrcweir             // A spin button
428cdf0e10cSrcweir             Sequence< NamedValue > aArgs( 5 );
429cb2654afSPedro Giffuni 
430cdf0e10cSrcweir             // send command to initialize spin button
431cb2654afSPedro Giffuni             aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Value" ) );
432cdf0e10cSrcweir             aArgs[0].Value <<= double( 0.0 );
433cb2654afSPedro Giffuni             aArgs[1].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "UpperLimit" ) );
434cdf0e10cSrcweir             aArgs[1].Value <<= double( 10.0 );
435cb2654afSPedro Giffuni             aArgs[2].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "LowerLimit" ) );
436cdf0e10cSrcweir             aArgs[2].Value <<= double( 0.0 );
437cb2654afSPedro Giffuni             aArgs[3].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Step" ) );
438cdf0e10cSrcweir             aArgs[3].Value <<= double( 0.1 );
439cb2654afSPedro Giffuni             aArgs[4].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "OutputFormat" ) );
440cb2654afSPedro Giffuni             aArgs[4].Value <<= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "%.2f cm" ) );
441cdf0e10cSrcweir 
442cb2654afSPedro Giffuni             SendCommandTo( xControl, aURL, rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SetValues" ) ), aArgs, sal_True );
443cdf0e10cSrcweir         }
444cb2654afSPedro Giffuni         else if ( aURL.Path.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "DropdownboxCmd" ) ) )
445cdf0e10cSrcweir         {
446cdf0e10cSrcweir             // A dropdown box is normally used for a group of commands
447cdf0e10cSrcweir             // where the user can select one of a defined set.
448cdf0e10cSrcweir             Sequence< NamedValue > aArgs( 1 );
449cb2654afSPedro Giffuni 
450cdf0e10cSrcweir             // send command to set context menu content
451cdf0e10cSrcweir             Sequence< rtl::OUString > aList( 10 );
452cb2654afSPedro Giffuni             aList[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "White" ) );
453cb2654afSPedro Giffuni             aList[1] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Black" ) );
454cb2654afSPedro Giffuni             aList[2] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Red" ) );
455cb2654afSPedro Giffuni             aList[3] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Blue" ) );
456cb2654afSPedro Giffuni             aList[4] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Green" ) );
457cb2654afSPedro Giffuni             aList[5] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Grey" ) );
458cb2654afSPedro Giffuni             aList[6] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Yellow" ) );
459cb2654afSPedro Giffuni             aList[7] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Orange" ) );
460cb2654afSPedro Giffuni             aList[8] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Brown" ) );
461cb2654afSPedro Giffuni             aList[9] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Pink" ) );
462cb2654afSPedro Giffuni 
463cb2654afSPedro Giffuni             aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "List" ) );
464cdf0e10cSrcweir             aArgs[0].Value <<= aList;
465cb2654afSPedro Giffuni             SendCommandTo( xControl, aURL, rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SetList" ) ), aArgs, sal_True );
466cdf0e10cSrcweir         }
467cdf0e10cSrcweir 
468cdf0e10cSrcweir 		aListenerHelper.AddListener( mxFrame, xControl, aURL.Path );
469cdf0e10cSrcweir 	}
470cdf0e10cSrcweir }
471cdf0e10cSrcweir 
472cdf0e10cSrcweir void SAL_CALL BaseDispatch::removeStatusListener( const Reference< XStatusListener >& xControl, const URL& aURL ) throw (RuntimeException)
473cdf0e10cSrcweir {
474cdf0e10cSrcweir 	aListenerHelper.RemoveListener( mxFrame, xControl, aURL.Path );
475cdf0e10cSrcweir }
476cdf0e10cSrcweir 
477cdf0e10cSrcweir void SAL_CALL BaseDispatch::controlEvent( const ControlEvent& Event ) throw (RuntimeException)
478cdf0e10cSrcweir {
479cb2654afSPedro Giffuni     if ( Event.aURL.Protocol.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "vnd.demo.complextoolbarcontrols.demoaddon:" ) ) )
480cdf0e10cSrcweir 	{
481cb2654afSPedro Giffuni         if ( Event.aURL.Path.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "ComboboxCmd" ) ) )
482cdf0e10cSrcweir         {
483cdf0e10cSrcweir             // We get notifications whenever the text inside the combobox has been changed.
484cdf0e10cSrcweir             // We store the new text into a member.
485cb2654afSPedro Giffuni             if ( Event.Event.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "TextChanged" ) ) )
486cdf0e10cSrcweir             {
487cdf0e10cSrcweir                 rtl::OUString aNewText;
488cdf0e10cSrcweir                 sal_Bool      bHasText( sal_False );
489cdf0e10cSrcweir                 for ( sal_Int32 i = 0; i < Event.aInformation.getLength(); i++ )
490cdf0e10cSrcweir                 {
491cb2654afSPedro Giffuni                     if ( Event.aInformation[i].Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Text" ) ) )
492cdf0e10cSrcweir                     {
493cdf0e10cSrcweir                         bHasText = Event.aInformation[i].Value >>= aNewText;
494cdf0e10cSrcweir                         break;
495cdf0e10cSrcweir                     }
496cdf0e10cSrcweir                 }
497cb2654afSPedro Giffuni 
498cdf0e10cSrcweir                 if ( bHasText )
499cdf0e10cSrcweir                     maComboBoxText = aNewText;
500cdf0e10cSrcweir             }
501cdf0e10cSrcweir         }
502cdf0e10cSrcweir     }
503cdf0e10cSrcweir }
504cb2654afSPedro Giffuni 
505cb2654afSPedro Giffuni BaseDispatch::BaseDispatch( const Reference< XMultiServiceFactory > &rxMSF,
506cb2654afSPedro Giffuni         const Reference< XFrame >& xFrame, const rtl::OUString& rServiceName )
507cdf0e10cSrcweir         : mxMSF( rxMSF )
508cdf0e10cSrcweir 		, mxFrame( xFrame )
509cdf0e10cSrcweir         , msDocService( rServiceName )
510cdf0e10cSrcweir         , mbButtonEnabled( sal_True )
511cdf0e10cSrcweir {
512cdf0e10cSrcweir }
513cdf0e10cSrcweir 
514cdf0e10cSrcweir 
515cdf0e10cSrcweir BaseDispatch::~BaseDispatch()
516cdf0e10cSrcweir {
517cdf0e10cSrcweir 	mxFrame.clear();
518cdf0e10cSrcweir 	mxMSF.clear();
519cdf0e10cSrcweir }
520