1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "ElementSelector.hxx"
32 #include "ObjectNameProvider.hxx"
33 #include "ObjectHierarchy.hxx"
34 #include "servicenames.hxx"
35 #include <chartview/ExplicitValueProvider.hxx>
36 #include "DrawViewWrapper.hxx"
38 #include "Strings.hrc"
40 #include <toolkit/helper/vclunohelper.hxx>
41 #include <osl/mutex.hxx>
42 #include <vcl/svapp.hxx>
44 #include <com/sun/star/chart2/XChartDocument.hpp>
45 #include <com/sun/star/frame/XControlNotificationListener.hpp>
46 #include <com/sun/star/util/XURLTransformer.hpp>
47 #include <com/sun/star/view/XSelectionSupplier.hpp>
52 using namespace com::sun::star
;
53 using namespace com::sun::star::uno
;
54 using ::com::sun::star::uno::Any
;
55 using ::com::sun::star::uno::Reference
;
56 using ::com::sun::star::uno::Sequence
;
60 static const ::rtl::OUString
lcl_aServiceName(
61 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart.ElementSelectorToolbarController" ));
64 //------------------------------------------------------------------------------
66 SelectorListBox::SelectorListBox( Window
* pParent
, WinBits nStyle
)
67 : ListBox( pParent
, nStyle
)
68 , m_bReleaseFocus( true )
72 SelectorListBox::~SelectorListBox()
76 void lcl_addObjectsToList( const ObjectHierarchy
& rHierarchy
, const ObjectHierarchy::tOID
& rParent
, std::vector
< ListBoxEntryData
>& rEntries
77 , const sal_Int32 nHierarchyDepth
, const Reference
< chart2::XChartDocument
>& xChartDoc
)
79 ObjectHierarchy::tChildContainer
aChildren( rHierarchy
.getChildren(rParent
) );
80 ObjectHierarchy::tChildContainer::const_iterator
aIt( aChildren
.begin());
81 while( aIt
!= aChildren
.end() )
83 ObjectHierarchy::tOID aOID
= *aIt
;
84 ::rtl::OUString aCID
= aOID
.getObjectCID();
85 ListBoxEntryData aEntry
;
87 aEntry
.UIName
+= ObjectNameProvider::getNameForCID( aCID
, xChartDoc
);
88 aEntry
.nHierarchyDepth
= nHierarchyDepth
;
89 rEntries
.push_back(aEntry
);
90 lcl_addObjectsToList( rHierarchy
, aOID
, rEntries
, nHierarchyDepth
+1, xChartDoc
);
95 void SelectorListBox::SetChartController( const Reference
< frame::XController
>& xChartController
)
97 m_xChartController
= xChartController
;
100 void SelectorListBox::UpdateChartElementsListAndSelection()
105 Reference
< frame::XController
> xChartController( m_xChartController
);
106 if( xChartController
.is() )
108 Reference
< view::XSelectionSupplier
> xSelectionSupplier( xChartController
, uno::UNO_QUERY
);
109 ObjectHierarchy::tOID aSelectedOID
;
110 rtl::OUString aSelectedCID
;
111 if( xSelectionSupplier
.is() )
113 aSelectedOID
= ObjectIdentifier( xSelectionSupplier
->getSelection() );
114 aSelectedCID
= aSelectedOID
.getObjectCID();
117 Reference
< chart2::XChartDocument
> xChartDoc( xChartController
->getModel(), uno::UNO_QUERY
);
118 ObjectType
eType( aSelectedOID
.getObjectType() );
119 bool bAddSelectionToList
= false;
120 if ( eType
== OBJECTTYPE_DATA_POINT
|| eType
== OBJECTTYPE_DATA_LABEL
|| eType
== OBJECTTYPE_SHAPE
)
121 bAddSelectionToList
= true;
123 Reference
< uno::XInterface
> xChartView
;
124 Reference
< lang::XMultiServiceFactory
> xFact( xChartController
->getModel(), uno::UNO_QUERY
);
126 xChartView
= xFact
->createInstance( CHART_VIEW_SERVICE_NAME
);
127 ExplicitValueProvider
* pExplicitValueProvider
= 0;//ExplicitValueProvider::getExplicitValueProvider(xChartView); dies erzeugt alle sichtbaren datenpinkte, das ist zu viel
128 ObjectHierarchy
aHierarchy( xChartDoc
, pExplicitValueProvider
, true /*bFlattenDiagram*/, true /*bOrderingForElementSelector*/ );
129 lcl_addObjectsToList( aHierarchy
, aHierarchy
.getRootNodeOID(), m_aEntries
, 0, xChartDoc
);
131 std::vector
< ListBoxEntryData
>::iterator
aIt( m_aEntries
.begin() );
132 if( bAddSelectionToList
)
134 if ( aSelectedOID
.isAutoGeneratedObject() )
136 rtl::OUString aSeriesCID
= ObjectIdentifier::createClassifiedIdentifierForParticle( ObjectIdentifier::getSeriesParticleFromCID( aSelectedCID
) );
137 for( aIt
= m_aEntries
.begin(); aIt
!= m_aEntries
.end(); ++aIt
)
139 if( aIt
->OID
.getObjectCID().match( aSeriesCID
) )
141 ListBoxEntryData aEntry
;
142 aEntry
.UIName
= ObjectNameProvider::getNameForCID( aSelectedCID
, xChartDoc
);
143 aEntry
.OID
= aSelectedOID
;
145 if( aIt
!= m_aEntries
.end() )
146 m_aEntries
.insert(aIt
, aEntry
);
148 m_aEntries
.push_back( aEntry
);
153 else if ( aSelectedOID
.isAdditionalShape() )
155 ListBoxEntryData aEntry
;
156 SdrObject
* pSelectedObj
= DrawViewWrapper::getSdrObject( aSelectedOID
.getAdditionalShape() );
157 rtl::OUString aName
= pSelectedObj
? pSelectedObj
->GetName() : rtl::OUString();
158 aEntry
.UIName
= ( aName
.isEmpty() ? ::rtl::OUString( String( SchResId( STR_OBJECT_SHAPE
) ) ) : aName
);
159 aEntry
.OID
= aSelectedOID
;
160 m_aEntries
.push_back( aEntry
);
164 sal_uInt16 nEntryPosToSelect
= 0; bool bSelectionFound
= false;
165 aIt
= m_aEntries
.begin();
166 for( sal_uInt16 nN
=0; aIt
!= m_aEntries
.end(); ++aIt
, ++nN
)
168 InsertEntry( aIt
->UIName
);
169 if ( !bSelectionFound
&& aSelectedOID
== aIt
->OID
)
171 nEntryPosToSelect
= nN
;
172 bSelectionFound
= true;
176 if( bSelectionFound
)
177 SelectEntryPos(nEntryPosToSelect
);
179 sal_uInt16 nEntryCount
= GetEntryCount();
180 if( nEntryCount
> 100 )
182 SetDropDownLineCount( nEntryCount
);
184 SaveValue();//remind current selection pos
187 void SelectorListBox::ReleaseFocus_Impl()
189 if ( !m_bReleaseFocus
)
191 m_bReleaseFocus
= true;
195 Reference
< frame::XController
> xController( m_xChartController
);
196 Reference
< frame::XFrame
> xFrame( xController
->getFrame() );
197 if ( xFrame
.is() && xFrame
->getContainerWindow().is() )
198 xFrame
->getContainerWindow()->setFocus();
201 void SelectorListBox::Select()
205 if ( !IsTravelSelect() )
207 sal_uInt16 nPos
= GetSelectEntryPos();
208 if( nPos
< m_aEntries
.size() )
210 ObjectHierarchy::tOID aOID
= m_aEntries
[nPos
].OID
;
211 Reference
< view::XSelectionSupplier
> xSelectionSupplier( m_xChartController
.get(), uno::UNO_QUERY
);
212 if( xSelectionSupplier
.is() )
213 xSelectionSupplier
->select( aOID
.getAny() );
219 long SelectorListBox::Notify( NotifyEvent
& rNEvt
)
223 if ( rNEvt
.GetType() == EVENT_KEYINPUT
)
225 sal_uInt16 nCode
= rNEvt
.GetKeyEvent()->GetKeyCode().GetCode();
232 if ( KEY_TAB
== nCode
)
233 m_bReleaseFocus
= false;
241 SelectEntryPos( GetSavedValue() ); //restore saved selection
246 else if ( EVENT_LOSEFOCUS
== rNEvt
.GetType() )
249 SelectEntryPos( GetSavedValue() );
252 return nHandled
? nHandled
: ListBox::Notify( rNEvt
);
255 Reference
< ::com::sun::star::accessibility::XAccessible
> SelectorListBox::CreateAccessible()
257 UpdateChartElementsListAndSelection();
258 return ListBox::CreateAccessible();
261 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
262 APPHELPER_XSERVICEINFO_IMPL( ElementSelectorToolbarController
, lcl_aServiceName
);
264 //------------------------------------------------------------------------------
265 Sequence
< ::rtl::OUString
> ElementSelectorToolbarController::getSupportedServiceNames_Static()
267 Sequence
< ::rtl::OUString
> aServices(1);
268 aServices
[ 0 ] = C2U( "com.sun.star.frame.ToolbarController" );
271 // -----------------------------------------------------------------------------
272 ElementSelectorToolbarController::ElementSelectorToolbarController( const uno::Reference
< uno::XComponentContext
> & xContext
)
276 // -----------------------------------------------------------------------------
277 ElementSelectorToolbarController::~ElementSelectorToolbarController()
280 // -----------------------------------------------------------------------------
282 Any SAL_CALL
ElementSelectorToolbarController::queryInterface( const Type
& _rType
) throw (RuntimeException
)
284 Any aReturn
= ToolboxController::queryInterface(_rType
);
285 if (!aReturn
.hasValue())
286 aReturn
= ElementSelectorToolbarController_BASE::queryInterface(_rType
);
289 // -----------------------------------------------------------------------------
290 void SAL_CALL
ElementSelectorToolbarController::acquire() throw ()
292 ToolboxController::acquire();
294 // -----------------------------------------------------------------------------
295 void SAL_CALL
ElementSelectorToolbarController::release() throw ()
297 ToolboxController::release();
299 // -----------------------------------------------------------------------------
300 void SAL_CALL
ElementSelectorToolbarController::initialize( const Sequence
< Any
>& rArguments
) throw (Exception
, RuntimeException
)
302 ToolboxController::initialize(rArguments
);
304 // -----------------------------------------------------------------------------
305 void SAL_CALL
ElementSelectorToolbarController::statusChanged( const frame::FeatureStateEvent
& rEvent
) throw ( RuntimeException
)
307 if( m_apSelectorListBox
.get() )
309 SolarMutexGuard aSolarMutexGuard
;
310 if ( rEvent
.FeatureURL
.Path
== "ChartElementSelector" )
312 Reference
< frame::XController
> xChartController
;
313 rEvent
.State
>>= xChartController
;
314 m_apSelectorListBox
->SetChartController( xChartController
);
315 m_apSelectorListBox
->UpdateChartElementsListAndSelection();
319 // -----------------------------------------------------------------------------
320 uno::Reference
< awt::XWindow
> SAL_CALL
ElementSelectorToolbarController::createItemWindow( const uno::Reference
< awt::XWindow
>& xParent
)
321 throw (uno::RuntimeException
)
323 uno::Reference
< awt::XWindow
> xItemWindow
;
324 if( !m_apSelectorListBox
.get() )
326 Window
* pParent
= VCLUnoHelper::GetWindow( xParent
);
329 m_apSelectorListBox
= ::std::auto_ptr
< SelectorListBox
>( new SelectorListBox( pParent
, WB_DROPDOWN
|WB_AUTOHSCROLL
|WB_BORDER
) );
330 ::Size
aLogicalSize( 95, 160 );
331 ::Size aPixelSize
= m_apSelectorListBox
->LogicToPixel( aLogicalSize
, MAP_APPFONT
);
332 m_apSelectorListBox
->SetSizePixel( aPixelSize
);
333 m_apSelectorListBox
->SetDropDownLineCount( 5 );
336 if( m_apSelectorListBox
.get() )
337 xItemWindow
= VCLUnoHelper::GetInterface( m_apSelectorListBox
.get() );
341 //..........................................................................
343 //..........................................................................
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */