summaryrefslogtreecommitdiff
path: root/chart2/source/controller/main/ElementSelector.cxx
blob: 31073b103fe4546487f222b2f61470d966d87f8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "ElementSelector.hxx"
#include "macros.hxx"
#include "ObjectNameProvider.hxx"
#include "ObjectHierarchy.hxx"
#include "servicenames.hxx"
#include <chartview/ExplicitValueProvider.hxx>
#include "DrawViewWrapper.hxx"
#include "ResId.hxx"
#include "Strings.hrc"

#include <toolkit/helper/vclunohelper.hxx>
#include <osl/mutex.hxx>
#include <vcl/svapp.hxx>

#include <com/sun/star/chart2/XChartDocument.hpp>
#include <com/sun/star/frame/XControlNotificationListener.hpp>
#include <com/sun/star/util/XURLTransformer.hpp>
#include <com/sun/star/view/XSelectionSupplier.hpp>

namespace chart
{

using namespace com::sun::star;
using namespace com::sun::star::uno;
using ::com::sun::star::uno::Any;
using ::com::sun::star::uno::Reference;
using ::com::sun::star::uno::Sequence;

namespace
{
static const char lcl_aServiceName[] = "com.sun.star.comp.chart.ElementSelectorToolbarController";
}

SelectorListBox::SelectorListBox( vcl::Window* pParent, WinBits nStyle )
    : ListBox( pParent, nStyle )
    , m_bReleaseFocus( true )
{
}

SelectorListBox::~SelectorListBox()
{
}

void lcl_addObjectsToList( const ObjectHierarchy& rHierarchy, const ObjectHierarchy::tOID & rParent, std::vector< ListBoxEntryData >& rEntries
                          , const sal_Int32 nHierarchyDepth, const Reference< chart2::XChartDocument >& xChartDoc )
{
    ObjectHierarchy::tChildContainer aChildren( rHierarchy.getChildren(rParent) );
    ObjectHierarchy::tChildContainer::const_iterator aIt( aChildren.begin());
    while( aIt != aChildren.end() )
    {
        ObjectHierarchy::tOID aOID = *aIt;
        OUString aCID = aOID.getObjectCID();
        ListBoxEntryData aEntry;
        aEntry.OID = aOID;
        aEntry.UIName += ObjectNameProvider::getNameForCID( aCID, xChartDoc );
        aEntry.nHierarchyDepth = nHierarchyDepth;
        rEntries.push_back(aEntry);
        lcl_addObjectsToList( rHierarchy, aOID, rEntries, nHierarchyDepth+1, xChartDoc );
        ++aIt;
    }
}

void SelectorListBox::SetChartController( const Reference< frame::XController >& xChartController )
{
    m_xChartController = xChartController;
}

void SelectorListBox::UpdateChartElementsListAndSelection()
{
    Clear();
    m_aEntries.clear();

    Reference< frame::XController > xChartController( m_xChartController );
    if( xChartController.is() )
    {
        Reference< view::XSelectionSupplier > xSelectionSupplier( xChartController, uno::UNO_QUERY);
        ObjectHierarchy::tOID aSelectedOID;
        OUString aSelectedCID;
        if( xSelectionSupplier.is() )
        {
            aSelectedOID = ObjectIdentifier( xSelectionSupplier->getSelection() );
            aSelectedCID = aSelectedOID.getObjectCID();
        }

        Reference< chart2::XChartDocument > xChartDoc( xChartController->getModel(), uno::UNO_QUERY );
        ObjectType eType( aSelectedOID.getObjectType() );
        bool bAddSelectionToList = false;
        if ( eType == OBJECTTYPE_DATA_POINT || eType == OBJECTTYPE_DATA_LABEL || eType == OBJECTTYPE_SHAPE )
            bAddSelectionToList = true;

        Reference< uno::XInterface > xChartView;
        Reference< lang::XMultiServiceFactory > xFact( xChartController->getModel(), uno::UNO_QUERY );
        if( xFact.is() )
            xChartView = xFact->createInstance( CHART_VIEW_SERVICE_NAME );
        ExplicitValueProvider* pExplicitValueProvider = 0; //ExplicitValueProvider::getExplicitValueProvider(xChartView); this creates all visible data points, that's too much
        ObjectHierarchy aHierarchy( xChartDoc, pExplicitValueProvider, true /*bFlattenDiagram*/, true /*bOrderingForElementSelector*/ );
        lcl_addObjectsToList( aHierarchy, ::chart::ObjectHierarchy::getRootNodeOID(), m_aEntries, 0, xChartDoc );

        std::vector< ListBoxEntryData >::iterator aIt( m_aEntries.begin() );
        if( bAddSelectionToList )
        {
            if ( aSelectedOID.isAutoGeneratedObject() )
            {
                OUString aSeriesCID = ObjectIdentifier::createClassifiedIdentifierForParticle( ObjectIdentifier::getSeriesParticleFromCID( aSelectedCID ) );
                for( aIt = m_aEntries.begin(); aIt != m_aEntries.end(); ++aIt )
                {
                    if( aIt->OID.getObjectCID().match( aSeriesCID ) )
                    {
                        ListBoxEntryData aEntry;
                        aEntry.UIName = ObjectNameProvider::getNameForCID( aSelectedCID, xChartDoc );
                        aEntry.OID = aSelectedOID;
                        ++aIt;
                        if( aIt != m_aEntries.end() )
                            m_aEntries.insert(aIt, aEntry);
                        else
                            m_aEntries.push_back( aEntry );
                        break;
                    }
                }
            }
            else if ( aSelectedOID.isAdditionalShape() )
            {
                ListBoxEntryData aEntry;
                SdrObject* pSelectedObj = DrawViewWrapper::getSdrObject( aSelectedOID.getAdditionalShape() );
                OUString aName = pSelectedObj ? pSelectedObj->GetName() : OUString();
                aEntry.UIName = ( aName.isEmpty() ?  SCH_RESSTR( STR_OBJECT_SHAPE ) : aName );
                aEntry.OID = aSelectedOID;
                m_aEntries.push_back( aEntry );
            }
        }

        sal_uInt16 nEntryPosToSelect = 0; bool bSelectionFound = false;
        aIt = m_aEntries.begin();
        for( sal_uInt16 nN=0; aIt != m_aEntries.end(); ++aIt, ++nN )
        {
            InsertEntry( aIt->UIName );
            if ( !bSelectionFound && aSelectedOID == aIt->OID )
            {
                nEntryPosToSelect = nN;
                bSelectionFound = true;
            }
        }

        if( bSelectionFound )
            SelectEntryPos(nEntryPosToSelect);

        sal_uInt16 nEntryCount = GetEntryCount();
        if( nEntryCount > 100 )
            nEntryCount = 100;
        SetDropDownLineCount( nEntryCount );
    }
    SaveValue(); //remind current selection pos
}

void SelectorListBox::ReleaseFocus_Impl()
{
    if ( !m_bReleaseFocus )
    {
        m_bReleaseFocus = true;
        return;
    }

    Reference< frame::XController > xController( m_xChartController );
    Reference< frame::XFrame > xFrame( xController->getFrame() );
    if ( xFrame.is() && xFrame->getContainerWindow().is() )
        xFrame->getContainerWindow()->setFocus();
}

void SelectorListBox::Select()
{
    ListBox::Select();

    if ( !IsTravelSelect() )
    {
        sal_uInt16 nPos = GetSelectEntryPos();
        if( nPos < m_aEntries.size() )
        {
            ObjectHierarchy::tOID aOID = m_aEntries[nPos].OID;
            Reference< view::XSelectionSupplier > xSelectionSupplier( m_xChartController.get(), uno::UNO_QUERY );
            if( xSelectionSupplier.is() )
                xSelectionSupplier->select( aOID.getAny() );
        }
        ReleaseFocus_Impl();
    }
}

bool SelectorListBox::Notify( NotifyEvent& rNEvt )
{
    bool nHandled = false;

    if ( rNEvt.GetType() == MouseNotifyEvent::KEYINPUT )
    {
        sal_uInt16 nCode = rNEvt.GetKeyEvent()->GetKeyCode().GetCode();

        switch ( nCode )
        {
            case KEY_RETURN:
            case KEY_TAB:
            {
                if ( KEY_TAB == nCode )
                    m_bReleaseFocus = false;
                else
                    nHandled = true;
                Select();
                break;
            }

            case KEY_ESCAPE:
                SelectEntryPos( GetSavedValue() ); //restore saved selection
                ReleaseFocus_Impl();
                break;
        }
    }
    else if ( MouseNotifyEvent::LOSEFOCUS == rNEvt.GetType() )
    {
        if ( !HasFocus() )
            SelectEntryPos( GetSavedValue() );
    }

    return nHandled || ListBox::Notify( rNEvt );
}

Reference< ::com::sun::star::accessibility::XAccessible > SelectorListBox::CreateAccessible()
{
    UpdateChartElementsListAndSelection();
    return ListBox::CreateAccessible();
}

// implement XServiceInfo methods basing upon getSupportedServiceNames_Static
APPHELPER_XSERVICEINFO_IMPL( ElementSelectorToolbarController, OUString(lcl_aServiceName) );

Sequence< OUString > ElementSelectorToolbarController::getSupportedServiceNames_Static()
{
    Sequence< OUString > aServices(1);
    aServices[ 0 ] = "com.sun.star.frame.ToolbarController";
    return aServices;
}
ElementSelectorToolbarController::ElementSelectorToolbarController( const uno::Reference< uno::XComponentContext > & xContext )
    : m_xCC( xContext )
{
}
ElementSelectorToolbarController::~ElementSelectorToolbarController()
{
}
// XInterface
Any SAL_CALL ElementSelectorToolbarController::queryInterface( const Type& _rType ) throw (RuntimeException, std::exception)
{
    Any aReturn = ToolboxController::queryInterface(_rType);
    if (!aReturn.hasValue())
        aReturn = ElementSelectorToolbarController_BASE::queryInterface(_rType);
    return aReturn;
}
void SAL_CALL ElementSelectorToolbarController::acquire() throw ()
{
    ToolboxController::acquire();
}
void SAL_CALL ElementSelectorToolbarController::release() throw ()
{
    ToolboxController::release();
}
void SAL_CALL ElementSelectorToolbarController::initialize( const Sequence< Any >& rArguments ) throw (Exception, RuntimeException, std::exception)
{
    ToolboxController::initialize(rArguments);
}
void SAL_CALL ElementSelectorToolbarController::statusChanged( const frame::FeatureStateEvent& rEvent ) throw ( RuntimeException, std::exception )
{
    if( m_apSelectorListBox.get() )
    {
        SolarMutexGuard aSolarMutexGuard;
        if ( rEvent.FeatureURL.Path == "ChartElementSelector" )
        {
            Reference< frame::XController > xChartController;
            rEvent.State >>= xChartController;
            m_apSelectorListBox->SetChartController( xChartController );
            m_apSelectorListBox->UpdateChartElementsListAndSelection();
        }
    }
}
uno::Reference< awt::XWindow > SAL_CALL ElementSelectorToolbarController::createItemWindow( const uno::Reference< awt::XWindow >& xParent )
        throw (uno::RuntimeException, std::exception)
{
    uno::Reference< awt::XWindow > xItemWindow;
    if( !m_apSelectorListBox.get() )
    {
        vcl::Window* pParent = VCLUnoHelper::GetWindow( xParent );
        if( pParent )
        {
            m_apSelectorListBox.reset( new SelectorListBox( pParent, WB_DROPDOWN|WB_AUTOHSCROLL|WB_BORDER ) );
            ::Size aLogicalSize( 95, 160 );
            ::Size aPixelSize = m_apSelectorListBox->LogicToPixel( aLogicalSize, MAP_APPFONT );
            m_apSelectorListBox->SetSizePixel( aPixelSize );
            m_apSelectorListBox->SetDropDownLineCount( 5 );
        }
    }
    if( m_apSelectorListBox.get() )
        xItemWindow = VCLUnoHelper::GetInterface( m_apSelectorListBox.get() );
    return xItemWindow;
}

} // chart2

extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
com_sun_star_comp_chart_ElementSelectorToolbarController_get_implementation(css::uno::XComponentContext *context,
                                                                            css::uno::Sequence<css::uno::Any> const &)
{
    return cppu::acquire(new chart::ElementSelectorToolbarController(context));
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */