1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
37 #include <com/sun/star/beans/PropertyValue.hpp>
38 #include <com/sun/star/frame/XFrame.hpp>
39 #include <com/sun/star/frame/XController.hpp>
40 #include <com/sun/star/awt/Toolkit.hpp>
41 #include <com/sun/star/awt/XWindowPeer.hpp>
42 #include <com/sun/star/awt/WindowAttribute.hpp>
43 #include <com/sun/star/awt/XMessageBox.hpp>
44 #include <com/sun/star/uno/XComponentContext.hpp>
45 #include <cppuhelper/supportsservice.hxx>
46 #include <rtl/ustring.hxx>
49 using namespace com::sun::star::uno
;
50 using namespace com::sun::star::frame
;
51 using namespace com::sun::star::awt
;
52 using com::sun::star::beans::PropertyValue
;
53 using com::sun::star::util::URL
;
55 // This is the service name an Add-On has to implement
56 #define SERVICE_NAME "com.sun.star.frame.ProtocolHandler"
60 * Show a message box with the UNO based toolkit
62 static void ShowMessageBox( const Reference
< XToolkit
>& rToolkit
, const Reference
< XFrame
>& rFrame
, const OUString
& aTitle
, const OUString
& aMsgText
)
64 if ( rFrame
.is() && rToolkit
.is() )
66 // describe window properties.
67 WindowDescriptor aDescriptor
;
68 aDescriptor
.Type
= WindowClass_MODALTOP
;
69 aDescriptor
.WindowServiceName
= "infobox";
70 aDescriptor
.ParentIndex
= -1;
71 aDescriptor
.Parent
= Reference
< XWindowPeer
>( rFrame
->getContainerWindow(), UNO_QUERY
);
72 aDescriptor
.Bounds
= Rectangle(0,0,300,200);
73 aDescriptor
.WindowAttributes
= WindowAttribute::BORDER
|
74 WindowAttribute::MOVEABLE
|
75 WindowAttribute::CLOSEABLE
;
77 Reference
< XWindowPeer
> xPeer
= rToolkit
->createWindow( aDescriptor
);
80 Reference
< XMessageBox
> xMsgBox( xPeer
, UNO_QUERY
);
83 xMsgBox
->setCaptionText( aTitle
);
84 xMsgBox
->setMessageText( aMsgText
);
92 * Called by the Office framework.
93 * One-time initialization. We have to store the context information
94 * given, like the frame we are bound to, into our members.
96 void SAL_CALL
Addon::initialize( const Sequence
< Any
>& aArguments
)
98 Reference
< XFrame
> xFrame
;
99 if ( aArguments
.getLength() )
101 aArguments
[0] >>= xFrame
;
105 // Create the toolkit to have access to it later
106 mxToolkit
= Reference
< XToolkit
>( Toolkit::create(mxContext
), UNO_QUERY_THROW
);
110 * Called by the Office framework.
111 * We are asked to query the given URL and return a dispatch object if the URL
112 * contains an Add-On command.
114 Reference
< XDispatch
> SAL_CALL
Addon::queryDispatch( const URL
& aURL
, const ::rtl::OUString
& sTargetFrameName
, sal_Int32 nSearchFlags
)
116 Reference
< XDispatch
> xRet
;
117 if ( aURL
.Protocol
.equalsAscii("org.openoffice.Office.addon.example:") )
119 if ( aURL
.Path
.equalsAscii( "Function1" ) )
121 else if ( aURL
.Path
.equalsAscii( "Function2" ) )
123 else if ( aURL
.Path
.equalsAscii( "Help" ) )
131 * Called by the Office framework.
132 * We are asked to execute the given Add-On command URL.
134 void SAL_CALL
Addon::dispatch( const URL
& aURL
, const Sequence
< PropertyValue
>& lArgs
)
136 if ( aURL
.Protocol
.equalsAscii("org.openoffice.Office.addon.example:") )
138 if ( aURL
.Path
.equalsAscii( "Function1" ) )
140 ShowMessageBox( mxToolkit
, mxFrame
,
141 OUString( "SDK Add-On example" ),
142 OUString( "Function 1 activated" ) );
144 else if ( aURL
.Path
.equalsAscii( "Function2" ) )
146 ShowMessageBox( mxToolkit
, mxFrame
,
147 OUString( "SDK Add-On example" ),
148 OUString( "Function 2 activated" ) );
150 else if ( aURL
.Path
.equalsAscii( "Help" ) )
153 ShowMessageBox( mxToolkit
, mxFrame
,
154 OUString( "About SDK Add-On example" ),
155 OUString( "This is the SDK Add-On example" ) );
161 * Called by the Office framework.
162 * We are asked to query the given sequence of URLs and return dispatch objects if the URLs
163 * contain Add-On commands.
165 Sequence
< Reference
< XDispatch
> > SAL_CALL
Addon::queryDispatches( const Sequence
< DispatchDescriptor
>& seqDescripts
)
167 sal_Int32 nCount
= seqDescripts
.getLength();
168 Sequence
< Reference
< XDispatch
> > lDispatcher( nCount
);
170 for( sal_Int32 i
=0; i
<nCount
; ++i
)
171 lDispatcher
[i
] = queryDispatch( seqDescripts
[i
].FeatureURL
, seqDescripts
[i
].FrameName
, seqDescripts
[i
].SearchFlags
);
177 * Called by the Office framework.
178 * We are asked to query the given sequence of URLs and return dispatch objects if the URLs
179 * contain Add-On commands.
181 void SAL_CALL
Addon::addStatusListener( const Reference
< XStatusListener
>& xControl
, const URL
& aURL
)
186 * Called by the Office framework.
187 * We are asked to query the given sequence of URLs and return dispatch objects if the URLs
188 * contain Add-On commands.
190 void SAL_CALL
Addon::removeStatusListener( const Reference
< XStatusListener
>& xControl
, const URL
& aURL
)
194 // Helper functions for the implementation of UNO component interfaces.
195 OUString
Addon_getImplementationName()
197 return OUString ( IMPLEMENTATION_NAME
);
200 Sequence
< ::rtl::OUString
> SAL_CALL
Addon_getSupportedServiceNames()
202 Sequence
< ::rtl::OUString
> aRet(1);
203 ::rtl::OUString
* pArray
= aRet
.getArray();
204 pArray
[0] = OUString ( SERVICE_NAME
);
208 Reference
< XInterface
> SAL_CALL
Addon_createInstance( const Reference
< XComponentContext
> & rContext
)
210 return (cppu::OWeakObject
*) new Addon( rContext
);
213 // Implementation of the recommended/mandatory interfaces of a UNO component.
215 ::rtl::OUString SAL_CALL
Addon::getImplementationName( )
217 return Addon_getImplementationName();
220 sal_Bool SAL_CALL
Addon::supportsService( const ::rtl::OUString
& rServiceName
)
222 return cppu::supportsService(this, rServiceName
);
225 Sequence
< ::rtl::OUString
> SAL_CALL
Addon::getSupportedServiceNames( )
227 return Addon_getSupportedServiceNames();
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */