1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "CommandDispatchContainer.hxx"
21 #include "UndoCommandDispatch.hxx"
22 #include "StatusBarCommandDispatch.hxx"
23 #include "DisposeHelper.hxx"
25 #include "ChartController.hxx"
26 #include "DrawCommandDispatch.hxx"
27 #include "ShapeController.hxx"
29 #include <comphelper/InlineContainer.hxx>
31 #include <com/sun/star/frame/XDispatchProvider.hpp>
33 using namespace ::com::sun::star
;
35 using ::com::sun::star::uno::Reference
;
36 using ::com::sun::star::uno::Sequence
;
41 CommandDispatchContainer::CommandDispatchContainer(
42 const Reference
< uno::XComponentContext
> & xContext
, ChartController
* pController
)
43 :m_xContext( xContext
)
44 ,m_pChartController( pController
)
45 ,m_pDrawCommandDispatch( NULL
)
46 ,m_pShapeController( NULL
)
48 m_aContainerDocumentCommands
=
49 ::comphelper::MakeSet
< OUString
>
50 ("AddDirect") ("NewDoc") ("Open")
51 ("Save") ("SaveAs") ("SendMail")
52 ("EditDoc") ("ExportDirectToPDF") ("PrintDefault")
56 void CommandDispatchContainer::setModel(
57 const Reference
< frame::XModel
> & xModel
)
59 // remove all existing dispatcher that base on the old model
60 m_aCachedDispatches
.clear();
61 DisposeHelper::DisposeAllElements( m_aToBeDisposedDispatches
);
62 m_aToBeDisposedDispatches
.clear();
66 void CommandDispatchContainer::setChartDispatch(
67 const Reference
< frame::XDispatch
>& rChartDispatch
,
68 const ::std::set
< OUString
> & rChartCommands
)
70 OSL_ENSURE(rChartDispatch
.is(),"Invalid fall back dispatcher!");
71 m_xChartDispatcher
.set( rChartDispatch
);
72 m_aChartCommands
= rChartCommands
;
73 m_aToBeDisposedDispatches
.push_back( m_xChartDispatcher
);
76 Reference
< frame::XDispatch
> CommandDispatchContainer::getDispatchForURL(
77 const util::URL
& rURL
)
79 Reference
< frame::XDispatch
> xResult
;
80 tDispatchMap::const_iterator
aIt( m_aCachedDispatches
.find( rURL
.Complete
));
81 if( aIt
!= m_aCachedDispatches
.end())
83 xResult
.set( (*aIt
).second
);
87 uno::Reference
< frame::XModel
> xModel( m_xModel
);
89 if( xModel
.is() && ( rURL
.Path
== "Undo" || rURL
.Path
== "Redo" ) )
91 CommandDispatch
* pDispatch
= new UndoCommandDispatch( m_xContext
, xModel
);
92 xResult
.set( pDispatch
);
93 pDispatch
->initialize();
94 m_aCachedDispatches
[ ".uno:Undo" ].set( xResult
);
95 m_aCachedDispatches
[ ".uno:Redo" ].set( xResult
);
96 m_aToBeDisposedDispatches
.push_back( xResult
);
98 else if( xModel
.is() && ( rURL
.Path
== "Context" || rURL
.Path
== "ModifiedStatus" ) )
100 Reference
< view::XSelectionSupplier
> xSelSupp( xModel
->getCurrentController(), uno::UNO_QUERY
);
101 CommandDispatch
* pDispatch
= new StatusBarCommandDispatch( m_xContext
, xModel
, xSelSupp
);
102 xResult
.set( pDispatch
);
103 pDispatch
->initialize();
104 m_aCachedDispatches
[ ".uno:Context" ].set( xResult
);
105 m_aCachedDispatches
[ ".uno:ModifiedStatus" ].set( xResult
);
106 m_aToBeDisposedDispatches
.push_back( xResult
);
108 else if( xModel
.is() &&
109 (m_aContainerDocumentCommands
.find( rURL
.Path
) != m_aContainerDocumentCommands
.end()) )
111 xResult
.set( getContainerDispatchForURL( xModel
->getCurrentController(), rURL
));
112 // ToDo: can those dispatches be cached?
113 m_aCachedDispatches
[ rURL
.Complete
].set( xResult
);
115 else if( m_xChartDispatcher
.is() &&
116 (m_aChartCommands
.find( rURL
.Path
) != m_aChartCommands
.end()) )
118 xResult
.set( m_xChartDispatcher
);
119 m_aCachedDispatches
[ rURL
.Complete
].set( xResult
);
121 // #i12587# support for shapes in chart
122 // Note, that the chart dispatcher must be queried first, because
123 // the chart dispatcher is the default dispatcher for all context
124 // sensitive commands.
125 else if ( m_pDrawCommandDispatch
&& m_pDrawCommandDispatch
->isFeatureSupported( rURL
.Complete
) )
127 xResult
.set( m_pDrawCommandDispatch
);
128 m_aCachedDispatches
[ rURL
.Complete
].set( xResult
);
130 else if ( m_pShapeController
&& m_pShapeController
->isFeatureSupported( rURL
.Complete
) )
132 xResult
.set( m_pShapeController
);
133 m_aCachedDispatches
[ rURL
.Complete
].set( xResult
);
140 Sequence
< Reference
< frame::XDispatch
> > CommandDispatchContainer::getDispatchesForURLs(
141 const Sequence
< frame::DispatchDescriptor
> & aDescriptors
)
143 sal_Int32 nCount
= aDescriptors
.getLength();
144 uno::Sequence
< uno::Reference
< frame::XDispatch
> > aRet( nCount
);
146 for( sal_Int32 nPos
= 0; nPos
< nCount
; ++nPos
)
148 if ( aDescriptors
[ nPos
].FrameName
== "_self" )
149 aRet
[ nPos
] = getDispatchForURL( aDescriptors
[ nPos
].FeatureURL
);
154 void CommandDispatchContainer::DisposeAndClear()
156 m_aCachedDispatches
.clear();
157 DisposeHelper::DisposeAllElements( m_aToBeDisposedDispatches
);
158 m_aToBeDisposedDispatches
.clear();
159 m_xChartDispatcher
.clear();
160 m_aChartCommands
.clear();
161 m_pChartController
= NULL
;
162 m_pDrawCommandDispatch
= NULL
;
163 m_pShapeController
= NULL
;
166 Reference
< frame::XDispatch
> CommandDispatchContainer::getContainerDispatchForURL(
167 const Reference
< frame::XController
> & xChartController
,
168 const util::URL
& rURL
)
170 Reference
< frame::XDispatch
> xResult
;
171 if( xChartController
.is())
173 Reference
< frame::XFrame
> xFrame( xChartController
->getFrame());
176 Reference
< frame::XDispatchProvider
> xDispProv( xFrame
->getCreator(), uno::UNO_QUERY
);
178 xResult
.set( xDispProv
->queryDispatch( rURL
, "_self", 0 ));
184 void CommandDispatchContainer::setDrawCommandDispatch( DrawCommandDispatch
* pDispatch
)
186 m_pDrawCommandDispatch
= pDispatch
;
187 m_aToBeDisposedDispatches
.push_back( Reference
< frame::XDispatch
>( pDispatch
) );
190 void CommandDispatchContainer::setShapeController( ShapeController
* pController
)
192 m_pShapeController
= pController
;
193 m_aToBeDisposedDispatches
.push_back( Reference
< frame::XDispatch
>( pController
) );
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */