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 <sal/config.h>
22 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
23 #include <o3tl/safeint.hxx>
24 #include <unotools/accessiblerelationsethelper.hxx>
25 #include <comphelper/sequence.hxx>
27 using namespace ::utl
;
28 using namespace ::com::sun::star
;
29 using namespace ::com::sun::star::accessibility
;
33 AccessibleRelation
lcl_getRelationByType( std::vector
<AccessibleRelation
>& raRelations
, AccessibleRelationType eRelationType
)
35 for (const auto& aRelation
: raRelations
)
37 if (aRelation
.RelationType
== eRelationType
)
40 return AccessibleRelation();
43 //===== internal ============================================================
45 AccessibleRelationSetHelper::AccessibleRelationSetHelper ()
49 AccessibleRelationSetHelper::AccessibleRelationSetHelper (const AccessibleRelationSetHelper
& rHelper
)
50 : cppu::WeakImplHelper
<XAccessibleRelationSet
>(rHelper
),
51 maRelations(rHelper
.maRelations
)
55 css::uno::Reference
<css::accessibility::XAccessibleRelationSet
> AccessibleRelationSetHelper::Clone() const
57 std::scoped_lock
aGuard (maMutex
);
58 return new AccessibleRelationSetHelper(*this);
61 AccessibleRelationSetHelper::~AccessibleRelationSetHelper()
65 //===== XAccessibleRelationSet ==============================================
67 /** Returns the number of relations in this relation set.
70 Returns the number of relations or zero if there are none.
73 AccessibleRelationSetHelper::getRelationCount( )
75 std::scoped_lock
aGuard (maMutex
);
77 return maRelations
.size();
80 /** Returns the relation of this relation set that is specified by
84 This index specifies the relatio to return.
87 For a valid index, i.e. inside the range 0 to the number of
88 relations minus one, the returned value is the requested
89 relation. If the index is invalid then the returned relation
93 AccessibleRelation SAL_CALL
94 AccessibleRelationSetHelper::getRelation( sal_Int32 nIndex
)
96 std::scoped_lock
aGuard (maMutex
);
98 if ((nIndex
< 0) || (o3tl::make_unsigned(nIndex
) >= maRelations
.size()))
99 throw lang::IndexOutOfBoundsException();
101 return maRelations
[nIndex
];
104 /** Tests whether the relation set contains a relation matching the
108 The type of relation to look for in this set of relations. This
109 has to be one of the constants of
110 <type>AccessibleRelationType</type>.
113 Returns <TRUE/> if there is a (at least one) relation of the
114 given type and <FALSE/> if there is no such relation in the set.
117 AccessibleRelationSetHelper::containsRelation(AccessibleRelationType eRelationType
)
119 std::scoped_lock
aGuard (maMutex
);
121 AccessibleRelation defaultRelation
; // default is INVALID
122 AccessibleRelation relationByType
= lcl_getRelationByType(maRelations
, eRelationType
);
123 return relationByType
.RelationType
!= defaultRelation
.RelationType
;
126 /** Retrieve and return the relation with the given relation type.
129 The type of the relation to return.
132 If a relation with the given type could be found, then (a copy
133 of) this relation is returned. Otherwise a relation with the
134 type INVALID is returned.
136 AccessibleRelation SAL_CALL
137 AccessibleRelationSetHelper::getRelationByType(AccessibleRelationType eRelationType
)
139 std::scoped_lock
aGuard (maMutex
);
141 return lcl_getRelationByType(maRelations
, eRelationType
);
144 void AccessibleRelationSetHelper::AddRelation(const AccessibleRelation
& rRelation
)
146 std::scoped_lock
aGuard (maMutex
);
148 for (auto& aRelation
: maRelations
)
150 if (aRelation
.RelationType
== rRelation
.RelationType
)
152 aRelation
.TargetSet
= comphelper::concatSequences(aRelation
.TargetSet
, rRelation
.TargetSet
);
156 maRelations
.push_back(rRelation
);
159 //===== XTypeProvider =======================================================
161 uno::Sequence
< css::uno::Type
> AccessibleRelationSetHelper::getTypes()
163 static const uno::Sequence
< css::uno::Type
> aTypes
{
164 cppu::UnoType
<XAccessibleRelationSet
>::get(),
165 cppu::UnoType
<lang::XTypeProvider
>::get()
170 uno::Sequence
<sal_Int8
> SAL_CALL
AccessibleRelationSetHelper::getImplementationId()
172 return css::uno::Sequence
<sal_Int8
>();
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */