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 "ElementSelector.hxx"
22 #include "ObjectNameProvider.hxx"
23 #include "ObjectHierarchy.hxx"
24 #include "servicenames.hxx"
25 #include <chartview/ExplicitValueProvider.hxx>
26 #include "DrawViewWrapper.hxx"
28 #include "Strings.hrc"
30 #include <cppuhelper/supportsservice.hxx>
31 #include <toolkit/helper/vclunohelper.hxx>
32 #include <osl/mutex.hxx>
33 #include <vcl/svapp.hxx>
35 #include <com/sun/star/chart2/XChartDocument.hpp>
36 #include <com/sun/star/frame/XControlNotificationListener.hpp>
37 #include <com/sun/star/util/XURLTransformer.hpp>
38 #include <com/sun/star/view/XSelectionSupplier.hpp>
43 using namespace com::sun::star
;
44 using namespace com::sun::star::uno
;
45 using ::com::sun::star::uno::Any
;
46 using ::com::sun::star::uno::Reference
;
47 using ::com::sun::star::uno::Sequence
;
51 static const char lcl_aServiceName
[] = "com.sun.star.comp.chart.ElementSelectorToolbarController";
54 SelectorListBox::SelectorListBox( vcl::Window
* pParent
, WinBits nStyle
)
55 : ListBox( pParent
, nStyle
)
56 , m_bReleaseFocus( true )
60 void lcl_addObjectsToList( const ObjectHierarchy
& rHierarchy
, const ObjectHierarchy::tOID
& rParent
, std::vector
< ListBoxEntryData
>& rEntries
61 , const sal_Int32 nHierarchyDepth
, const Reference
< chart2::XChartDocument
>& xChartDoc
)
63 ObjectHierarchy::tChildContainer
aChildren( rHierarchy
.getChildren(rParent
) );
64 ObjectHierarchy::tChildContainer::const_iterator
aIt( aChildren
.begin());
65 while( aIt
!= aChildren
.end() )
67 ObjectHierarchy::tOID aOID
= *aIt
;
68 OUString aCID
= aOID
.getObjectCID();
69 ListBoxEntryData aEntry
;
71 aEntry
.UIName
+= ObjectNameProvider::getNameForCID( aCID
, xChartDoc
);
72 aEntry
.nHierarchyDepth
= nHierarchyDepth
;
73 rEntries
.push_back(aEntry
);
74 lcl_addObjectsToList( rHierarchy
, aOID
, rEntries
, nHierarchyDepth
+1, xChartDoc
);
79 void SelectorListBox::SetChartController( const Reference
< frame::XController
>& xChartController
)
81 m_xChartController
= xChartController
;
84 void SelectorListBox::UpdateChartElementsListAndSelection()
89 Reference
< frame::XController
> xChartController( m_xChartController
);
90 if( xChartController
.is() )
92 Reference
< view::XSelectionSupplier
> xSelectionSupplier( xChartController
, uno::UNO_QUERY
);
93 ObjectHierarchy::tOID aSelectedOID
;
94 OUString aSelectedCID
;
95 if( xSelectionSupplier
.is() )
97 aSelectedOID
= ObjectIdentifier( xSelectionSupplier
->getSelection() );
98 aSelectedCID
= aSelectedOID
.getObjectCID();
101 Reference
< chart2::XChartDocument
> xChartDoc( xChartController
->getModel(), uno::UNO_QUERY
);
102 ObjectType
eType( aSelectedOID
.getObjectType() );
103 bool bAddSelectionToList
= false;
104 if ( eType
== OBJECTTYPE_DATA_POINT
|| eType
== OBJECTTYPE_DATA_LABEL
|| eType
== OBJECTTYPE_SHAPE
)
105 bAddSelectionToList
= true;
107 Reference
< uno::XInterface
> xChartView
;
108 Reference
< lang::XMultiServiceFactory
> xFact( xChartController
->getModel(), uno::UNO_QUERY
);
110 xChartView
= xFact
->createInstance( CHART_VIEW_SERVICE_NAME
);
111 ExplicitValueProvider
* pExplicitValueProvider
= 0; //ExplicitValueProvider::getExplicitValueProvider(xChartView); this creates all visible data points, that's too much
112 ObjectHierarchy
aHierarchy( xChartDoc
, pExplicitValueProvider
, true /*bFlattenDiagram*/, true /*bOrderingForElementSelector*/ );
113 lcl_addObjectsToList( aHierarchy
, ::chart::ObjectHierarchy::getRootNodeOID(), m_aEntries
, 0, xChartDoc
);
115 std::vector
< ListBoxEntryData
>::iterator
aIt( m_aEntries
.begin() );
116 if( bAddSelectionToList
)
118 if ( aSelectedOID
.isAutoGeneratedObject() )
120 OUString aSeriesCID
= ObjectIdentifier::createClassifiedIdentifierForParticle( ObjectIdentifier::getSeriesParticleFromCID( aSelectedCID
) );
121 for( aIt
= m_aEntries
.begin(); aIt
!= m_aEntries
.end(); ++aIt
)
123 if( aIt
->OID
.getObjectCID().match( aSeriesCID
) )
125 ListBoxEntryData aEntry
;
126 aEntry
.UIName
= ObjectNameProvider::getNameForCID( aSelectedCID
, xChartDoc
);
127 aEntry
.OID
= aSelectedOID
;
129 if( aIt
!= m_aEntries
.end() )
130 m_aEntries
.insert(aIt
, aEntry
);
132 m_aEntries
.push_back( aEntry
);
137 else if ( aSelectedOID
.isAdditionalShape() )
139 ListBoxEntryData aEntry
;
140 SdrObject
* pSelectedObj
= DrawViewWrapper::getSdrObject( aSelectedOID
.getAdditionalShape() );
141 OUString aName
= pSelectedObj
? pSelectedObj
->GetName() : OUString();
142 aEntry
.UIName
= ( aName
.isEmpty() ? SCH_RESSTR( STR_OBJECT_SHAPE
) : aName
);
143 aEntry
.OID
= aSelectedOID
;
144 m_aEntries
.push_back( aEntry
);
148 sal_uInt16 nEntryPosToSelect
= 0; bool bSelectionFound
= false;
149 aIt
= m_aEntries
.begin();
150 for( sal_uInt16 nN
=0; aIt
!= m_aEntries
.end(); ++aIt
, ++nN
)
152 InsertEntry( aIt
->UIName
);
153 if ( !bSelectionFound
&& aSelectedOID
== aIt
->OID
)
155 nEntryPosToSelect
= nN
;
156 bSelectionFound
= true;
160 if( bSelectionFound
)
161 SelectEntryPos(nEntryPosToSelect
);
163 sal_uInt16 nEntryCount
= GetEntryCount();
164 if( nEntryCount
> 100 )
166 SetDropDownLineCount( nEntryCount
);
168 SaveValue(); //remind current selection pos
171 void SelectorListBox::ReleaseFocus_Impl()
173 if ( !m_bReleaseFocus
)
175 m_bReleaseFocus
= true;
179 Reference
< frame::XController
> xController( m_xChartController
);
180 Reference
< frame::XFrame
> xFrame( xController
->getFrame() );
181 if ( xFrame
.is() && xFrame
->getContainerWindow().is() )
182 xFrame
->getContainerWindow()->setFocus();
185 void SelectorListBox::Select()
189 if ( !IsTravelSelect() )
191 sal_uInt16 nPos
= GetSelectEntryPos();
192 if( nPos
< m_aEntries
.size() )
194 ObjectHierarchy::tOID aOID
= m_aEntries
[nPos
].OID
;
195 Reference
< view::XSelectionSupplier
> xSelectionSupplier( m_xChartController
.get(), uno::UNO_QUERY
);
196 if( xSelectionSupplier
.is() )
197 xSelectionSupplier
->select( aOID
.getAny() );
203 bool SelectorListBox::Notify( NotifyEvent
& rNEvt
)
205 bool nHandled
= false;
207 if ( rNEvt
.GetType() == MouseNotifyEvent::KEYINPUT
)
209 sal_uInt16 nCode
= rNEvt
.GetKeyEvent()->GetKeyCode().GetCode();
216 if ( KEY_TAB
== nCode
)
217 m_bReleaseFocus
= false;
225 SelectEntryPos( GetSavedValue() ); //restore saved selection
230 else if ( MouseNotifyEvent::LOSEFOCUS
== rNEvt
.GetType() )
233 SelectEntryPos( GetSavedValue() );
236 return nHandled
|| ListBox::Notify( rNEvt
);
239 Reference
< ::com::sun::star::accessibility::XAccessible
> SelectorListBox::CreateAccessible()
241 UpdateChartElementsListAndSelection();
242 return ListBox::CreateAccessible();
245 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
246 OUString SAL_CALL
ElementSelectorToolbarController::getImplementationName()
247 throw( css::uno::RuntimeException
, std::exception
)
249 return getImplementationName_Static();
252 OUString
ElementSelectorToolbarController::getImplementationName_Static()
254 return OUString(lcl_aServiceName
);
257 sal_Bool SAL_CALL
ElementSelectorToolbarController::supportsService( const OUString
& rServiceName
)
258 throw( css::uno::RuntimeException
, std::exception
)
260 return cppu::supportsService(this, rServiceName
);
263 css::uno::Sequence
< OUString
> SAL_CALL
ElementSelectorToolbarController::getSupportedServiceNames()
264 throw( css::uno::RuntimeException
, std::exception
)
266 return getSupportedServiceNames_Static();
269 Sequence
< OUString
> ElementSelectorToolbarController::getSupportedServiceNames_Static()
271 Sequence
< OUString
> aServices(1);
272 aServices
[ 0 ] = "com.sun.star.frame.ToolbarController";
275 ElementSelectorToolbarController::ElementSelectorToolbarController( const uno::Reference
< uno::XComponentContext
> & xContext
)
279 ElementSelectorToolbarController::~ElementSelectorToolbarController()
283 Any SAL_CALL
ElementSelectorToolbarController::queryInterface( const Type
& _rType
) throw (RuntimeException
, std::exception
)
285 Any aReturn
= ToolboxController::queryInterface(_rType
);
286 if (!aReturn
.hasValue())
287 aReturn
= ElementSelectorToolbarController_BASE::queryInterface(_rType
);
290 void SAL_CALL
ElementSelectorToolbarController::acquire() throw ()
292 ToolboxController::acquire();
294 void SAL_CALL
ElementSelectorToolbarController::release() throw ()
296 ToolboxController::release();
298 void SAL_CALL
ElementSelectorToolbarController::initialize( const Sequence
< Any
>& rArguments
) throw (Exception
, RuntimeException
, std::exception
)
300 ToolboxController::initialize(rArguments
);
302 void SAL_CALL
ElementSelectorToolbarController::statusChanged( const frame::FeatureStateEvent
& rEvent
) throw ( RuntimeException
, std::exception
)
304 if( m_apSelectorListBox
.get() )
306 SolarMutexGuard aSolarMutexGuard
;
307 if ( rEvent
.FeatureURL
.Path
== "ChartElementSelector" )
309 Reference
< frame::XController
> xChartController
;
310 rEvent
.State
>>= xChartController
;
311 m_apSelectorListBox
->SetChartController( xChartController
);
312 m_apSelectorListBox
->UpdateChartElementsListAndSelection();
316 uno::Reference
< awt::XWindow
> SAL_CALL
ElementSelectorToolbarController::createItemWindow( const uno::Reference
< awt::XWindow
>& xParent
)
317 throw (uno::RuntimeException
, std::exception
)
319 uno::Reference
< awt::XWindow
> xItemWindow
;
320 if( !m_apSelectorListBox
.get() )
322 vcl::Window
* pParent
= VCLUnoHelper::GetWindow( xParent
);
325 m_apSelectorListBox
.reset( VclPtr
<SelectorListBox
>::Create( pParent
, WB_DROPDOWN
|WB_AUTOHSCROLL
|WB_BORDER
) );
326 ::Size
aLogicalSize( 95, 160 );
327 ::Size aPixelSize
= m_apSelectorListBox
->LogicToPixel( aLogicalSize
, MAP_APPFONT
);
328 m_apSelectorListBox
->SetSizePixel( aPixelSize
);
329 m_apSelectorListBox
->SetDropDownLineCount( 5 );
332 if( m_apSelectorListBox
.get() )
333 xItemWindow
= VCLUnoHelper::GetInterface( m_apSelectorListBox
.get() );
339 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
340 com_sun_star_comp_chart_ElementSelectorToolbarController_get_implementation(css::uno::XComponentContext
*context
,
341 css::uno::Sequence
<css::uno::Any
> const &)
343 return cppu::acquire(new chart::ElementSelectorToolbarController(context
));
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */