Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "ElementSelector.hxx"
21 : #include "macros.hxx"
22 : #include "ObjectNameProvider.hxx"
23 : #include "ObjectHierarchy.hxx"
24 : #include "servicenames.hxx"
25 : #include <chartview/ExplicitValueProvider.hxx>
26 : #include "DrawViewWrapper.hxx"
27 : #include "ResId.hxx"
28 : #include "Strings.hrc"
29 :
30 : #include <toolkit/helper/vclunohelper.hxx>
31 : #include <osl/mutex.hxx>
32 : #include <vcl/svapp.hxx>
33 :
34 : #include <com/sun/star/chart2/XChartDocument.hpp>
35 : #include <com/sun/star/frame/XControlNotificationListener.hpp>
36 : #include <com/sun/star/util/XURLTransformer.hpp>
37 : #include <com/sun/star/view/XSelectionSupplier.hpp>
38 :
39 : namespace chart
40 : {
41 :
42 : using namespace com::sun::star;
43 : using namespace com::sun::star::uno;
44 : using ::com::sun::star::uno::Any;
45 : using ::com::sun::star::uno::Reference;
46 : using ::com::sun::star::uno::Sequence;
47 :
48 : namespace
49 : {
50 34 : static const OUString lcl_aServiceName( "com.sun.star.comp.chart.ElementSelectorToolbarController" );
51 : }
52 :
53 34 : SelectorListBox::SelectorListBox( vcl::Window* pParent, WinBits nStyle )
54 : : ListBox( pParent, nStyle )
55 34 : , m_bReleaseFocus( true )
56 : {
57 34 : }
58 :
59 68 : SelectorListBox::~SelectorListBox()
60 : {
61 68 : }
62 :
63 16107 : void lcl_addObjectsToList( const ObjectHierarchy& rHierarchy, const ObjectHierarchy::tOID & rParent, std::vector< ListBoxEntryData >& rEntries
64 : , const sal_Int32 nHierarchyDepth, const Reference< chart2::XChartDocument >& xChartDoc )
65 : {
66 16107 : ObjectHierarchy::tChildContainer aChildren( rHierarchy.getChildren(rParent) );
67 16107 : ObjectHierarchy::tChildContainer::const_iterator aIt( aChildren.begin());
68 46773 : while( aIt != aChildren.end() )
69 : {
70 14559 : ObjectHierarchy::tOID aOID = *aIt;
71 29118 : OUString aCID = aOID.getObjectCID();
72 29118 : ListBoxEntryData aEntry;
73 14559 : aEntry.OID = aOID;
74 14559 : aEntry.UIName += ObjectNameProvider::getNameForCID( aCID, xChartDoc );
75 14559 : aEntry.nHierarchyDepth = nHierarchyDepth;
76 14559 : rEntries.push_back(aEntry);
77 14559 : lcl_addObjectsToList( rHierarchy, aOID, rEntries, nHierarchyDepth+1, xChartDoc );
78 14559 : ++aIt;
79 30666 : }
80 16107 : }
81 :
82 1550 : void SelectorListBox::SetChartController( const Reference< frame::XController >& xChartController )
83 : {
84 1550 : m_xChartController = xChartController;
85 1550 : }
86 :
87 1550 : void SelectorListBox::UpdateChartElementsListAndSelection()
88 : {
89 1550 : Clear();
90 1550 : m_aEntries.clear();
91 :
92 1550 : Reference< frame::XController > xChartController( m_xChartController );
93 1550 : if( xChartController.is() )
94 : {
95 1548 : Reference< view::XSelectionSupplier > xSelectionSupplier( xChartController, uno::UNO_QUERY);
96 3096 : ObjectHierarchy::tOID aSelectedOID;
97 3096 : OUString aSelectedCID;
98 1548 : if( xSelectionSupplier.is() )
99 : {
100 1548 : aSelectedOID = ObjectIdentifier( xSelectionSupplier->getSelection() );
101 1548 : aSelectedCID = aSelectedOID.getObjectCID();
102 : }
103 :
104 3096 : Reference< chart2::XChartDocument > xChartDoc( xChartController->getModel(), uno::UNO_QUERY );
105 1548 : ObjectType eType( aSelectedOID.getObjectType() );
106 1548 : bool bAddSelectionToList = false;
107 1548 : if ( eType == OBJECTTYPE_DATA_POINT || eType == OBJECTTYPE_DATA_LABEL || eType == OBJECTTYPE_SHAPE )
108 0 : bAddSelectionToList = true;
109 :
110 3096 : Reference< uno::XInterface > xChartView;
111 3096 : Reference< lang::XMultiServiceFactory > xFact( xChartController->getModel(), uno::UNO_QUERY );
112 1548 : if( xFact.is() )
113 1548 : xChartView = xFact->createInstance( CHART_VIEW_SERVICE_NAME );
114 1548 : ExplicitValueProvider* pExplicitValueProvider = 0; //ExplicitValueProvider::getExplicitValueProvider(xChartView); this creates all visible data points, that's too much
115 3096 : ObjectHierarchy aHierarchy( xChartDoc, pExplicitValueProvider, true /*bFlattenDiagram*/, true /*bOrderingForElementSelector*/ );
116 1548 : lcl_addObjectsToList( aHierarchy, ::chart::ObjectHierarchy::getRootNodeOID(), m_aEntries, 0, xChartDoc );
117 :
118 1548 : std::vector< ListBoxEntryData >::iterator aIt( m_aEntries.begin() );
119 1548 : if( bAddSelectionToList )
120 : {
121 0 : if ( aSelectedOID.isAutoGeneratedObject() )
122 : {
123 0 : OUString aSeriesCID = ObjectIdentifier::createClassifiedIdentifierForParticle( ObjectIdentifier::getSeriesParticleFromCID( aSelectedCID ) );
124 0 : for( aIt = m_aEntries.begin(); aIt != m_aEntries.end(); ++aIt )
125 : {
126 0 : if( aIt->OID.getObjectCID().match( aSeriesCID ) )
127 : {
128 0 : ListBoxEntryData aEntry;
129 0 : aEntry.UIName = ObjectNameProvider::getNameForCID( aSelectedCID, xChartDoc );
130 0 : aEntry.OID = aSelectedOID;
131 0 : ++aIt;
132 0 : if( aIt != m_aEntries.end() )
133 0 : m_aEntries.insert(aIt, aEntry);
134 : else
135 0 : m_aEntries.push_back( aEntry );
136 0 : break;
137 : }
138 0 : }
139 : }
140 0 : else if ( aSelectedOID.isAdditionalShape() )
141 : {
142 0 : ListBoxEntryData aEntry;
143 0 : SdrObject* pSelectedObj = DrawViewWrapper::getSdrObject( aSelectedOID.getAdditionalShape() );
144 0 : OUString aName = pSelectedObj ? pSelectedObj->GetName() : OUString();
145 0 : aEntry.UIName = ( aName.isEmpty() ? SCH_RESSTR( STR_OBJECT_SHAPE ) : aName );
146 0 : aEntry.OID = aSelectedOID;
147 0 : m_aEntries.push_back( aEntry );
148 : }
149 : }
150 :
151 1548 : sal_uInt16 nEntryPosToSelect = 0; bool bSelectionFound = false;
152 1548 : aIt = m_aEntries.begin();
153 16107 : for( sal_uInt16 nN=0; aIt != m_aEntries.end(); ++aIt, ++nN )
154 : {
155 14559 : InsertEntry( aIt->UIName );
156 14559 : if ( !bSelectionFound && aSelectedOID == aIt->OID )
157 : {
158 0 : nEntryPosToSelect = nN;
159 0 : bSelectionFound = true;
160 : }
161 : }
162 :
163 1548 : if( bSelectionFound )
164 0 : SelectEntryPos(nEntryPosToSelect);
165 :
166 1548 : sal_uInt16 nEntryCount = GetEntryCount();
167 1548 : if( nEntryCount > 100 )
168 0 : nEntryCount = 100;
169 3096 : SetDropDownLineCount( nEntryCount );
170 : }
171 1550 : SaveValue(); //remind current selection pos
172 1550 : }
173 :
174 0 : void SelectorListBox::ReleaseFocus_Impl()
175 : {
176 0 : if ( !m_bReleaseFocus )
177 : {
178 0 : m_bReleaseFocus = true;
179 0 : return;
180 : }
181 :
182 0 : Reference< frame::XController > xController( m_xChartController );
183 0 : Reference< frame::XFrame > xFrame( xController->getFrame() );
184 0 : if ( xFrame.is() && xFrame->getContainerWindow().is() )
185 0 : xFrame->getContainerWindow()->setFocus();
186 : }
187 :
188 0 : void SelectorListBox::Select()
189 : {
190 0 : ListBox::Select();
191 :
192 0 : if ( !IsTravelSelect() )
193 : {
194 0 : sal_uInt16 nPos = GetSelectEntryPos();
195 0 : if( nPos < m_aEntries.size() )
196 : {
197 0 : ObjectHierarchy::tOID aOID = m_aEntries[nPos].OID;
198 0 : Reference< view::XSelectionSupplier > xSelectionSupplier( m_xChartController.get(), uno::UNO_QUERY );
199 0 : if( xSelectionSupplier.is() )
200 0 : xSelectionSupplier->select( aOID.getAny() );
201 : }
202 0 : ReleaseFocus_Impl();
203 : }
204 0 : }
205 :
206 0 : bool SelectorListBox::Notify( NotifyEvent& rNEvt )
207 : {
208 0 : bool nHandled = false;
209 :
210 0 : if ( rNEvt.GetType() == EVENT_KEYINPUT )
211 : {
212 0 : sal_uInt16 nCode = rNEvt.GetKeyEvent()->GetKeyCode().GetCode();
213 :
214 0 : switch ( nCode )
215 : {
216 : case KEY_RETURN:
217 : case KEY_TAB:
218 : {
219 0 : if ( KEY_TAB == nCode )
220 0 : m_bReleaseFocus = false;
221 : else
222 0 : nHandled = true;
223 0 : Select();
224 0 : break;
225 : }
226 :
227 : case KEY_ESCAPE:
228 0 : SelectEntryPos( GetSavedValue() ); //restore saved selection
229 0 : ReleaseFocus_Impl();
230 0 : break;
231 : }
232 : }
233 0 : else if ( EVENT_LOSEFOCUS == rNEvt.GetType() )
234 : {
235 0 : if ( !HasFocus() )
236 0 : SelectEntryPos( GetSavedValue() );
237 : }
238 :
239 0 : return nHandled || ListBox::Notify( rNEvt );
240 : }
241 :
242 0 : Reference< ::com::sun::star::accessibility::XAccessible > SelectorListBox::CreateAccessible()
243 : {
244 0 : UpdateChartElementsListAndSelection();
245 0 : return ListBox::CreateAccessible();
246 : }
247 :
248 : // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
249 0 : APPHELPER_XSERVICEINFO_IMPL( ElementSelectorToolbarController, lcl_aServiceName );
250 :
251 0 : Sequence< OUString > ElementSelectorToolbarController::getSupportedServiceNames_Static()
252 : {
253 0 : Sequence< OUString > aServices(1);
254 0 : aServices[ 0 ] = "com.sun.star.frame.ToolbarController";
255 0 : return aServices;
256 : }
257 34 : ElementSelectorToolbarController::ElementSelectorToolbarController( const uno::Reference< uno::XComponentContext > & xContext )
258 34 : : m_xCC( xContext )
259 : {
260 34 : }
261 68 : ElementSelectorToolbarController::~ElementSelectorToolbarController()
262 : {
263 68 : }
264 : // XInterface
265 1885 : Any SAL_CALL ElementSelectorToolbarController::queryInterface( const Type& _rType ) throw (RuntimeException, std::exception)
266 : {
267 1885 : Any aReturn = ToolboxController::queryInterface(_rType);
268 1885 : if (!aReturn.hasValue())
269 34 : aReturn = ElementSelectorToolbarController_BASE::queryInterface(_rType);
270 1885 : return aReturn;
271 : }
272 5408 : void SAL_CALL ElementSelectorToolbarController::acquire() throw ()
273 : {
274 5408 : ToolboxController::acquire();
275 5408 : }
276 5408 : void SAL_CALL ElementSelectorToolbarController::release() throw ()
277 : {
278 5408 : ToolboxController::release();
279 5408 : }
280 34 : void SAL_CALL ElementSelectorToolbarController::initialize( const Sequence< Any >& rArguments ) throw (Exception, RuntimeException, std::exception)
281 : {
282 34 : ToolboxController::initialize(rArguments);
283 34 : }
284 1550 : void SAL_CALL ElementSelectorToolbarController::statusChanged( const frame::FeatureStateEvent& rEvent ) throw ( RuntimeException, std::exception )
285 : {
286 1550 : if( m_apSelectorListBox.get() )
287 : {
288 1550 : SolarMutexGuard aSolarMutexGuard;
289 1550 : if ( rEvent.FeatureURL.Path == "ChartElementSelector" )
290 : {
291 1550 : Reference< frame::XController > xChartController;
292 1550 : rEvent.State >>= xChartController;
293 1550 : m_apSelectorListBox->SetChartController( xChartController );
294 1550 : m_apSelectorListBox->UpdateChartElementsListAndSelection();
295 1550 : }
296 : }
297 1550 : }
298 34 : uno::Reference< awt::XWindow > SAL_CALL ElementSelectorToolbarController::createItemWindow( const uno::Reference< awt::XWindow >& xParent )
299 : throw (uno::RuntimeException, std::exception)
300 : {
301 34 : uno::Reference< awt::XWindow > xItemWindow;
302 34 : if( !m_apSelectorListBox.get() )
303 : {
304 34 : vcl::Window* pParent = VCLUnoHelper::GetWindow( xParent );
305 34 : if( pParent )
306 : {
307 34 : m_apSelectorListBox.reset( new SelectorListBox( pParent, WB_DROPDOWN|WB_AUTOHSCROLL|WB_BORDER ) );
308 34 : ::Size aLogicalSize( 95, 160 );
309 34 : ::Size aPixelSize = m_apSelectorListBox->LogicToPixel( aLogicalSize, MAP_APPFONT );
310 34 : m_apSelectorListBox->SetSizePixel( aPixelSize );
311 34 : m_apSelectorListBox->SetDropDownLineCount( 5 );
312 : }
313 : }
314 34 : if( m_apSelectorListBox.get() )
315 34 : xItemWindow = VCLUnoHelper::GetInterface( m_apSelectorListBox.get() );
316 34 : return xItemWindow;
317 : }
318 :
319 : } // chart2
320 :
321 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
322 34 : com_sun_star_comp_chart_ElementSelectorToolbarController_get_implementation(css::uno::XComponentContext *context,
323 : css::uno::Sequence<css::uno::Any> const &)
324 : {
325 34 : return cppu::acquire(new chart::ElementSelectorToolbarController(context));
326 102 : }
327 :
328 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|