Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / svx / source / unodraw / unoctabl.cxx
blob1d50a7c3297f0a301f682fe73cae8316b84a8ab9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <unotools/pathoptions.hxx>
21 #include <com/sun/star/lang/XServiceInfo.hpp>
22 #include <com/sun/star/container/XNameContainer.hpp>
23 #include <com/sun/star/uno/XComponentContext.hpp>
24 #include <cppuhelper/implbase.hxx>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <rtl/ref.hxx>
27 #include <svx/xtable.hxx>
29 using namespace ::com::sun::star;
31 namespace {
33 class SvxUnoColorTable : public cppu::WeakImplHelper< container::XNameContainer, lang::XServiceInfo >
35 private:
36 XColorListRef pList;
38 public:
39 SvxUnoColorTable();
41 // XServiceInfo
42 virtual OUString SAL_CALL getImplementationName() override;
43 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
44 virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
46 // XNameContainer
47 virtual void SAL_CALL insertByName( const OUString& aName, const uno::Any& aElement ) override;
48 virtual void SAL_CALL removeByName( const OUString& Name ) override;
50 // XNameReplace
51 virtual void SAL_CALL replaceByName( const OUString& aName, const uno::Any& aElement ) override;
53 // XNameAccess
54 virtual uno::Any SAL_CALL getByName( const OUString& aName ) override;
56 virtual uno::Sequence< OUString > SAL_CALL getElementNames() override;
58 virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override;
60 // XElementAccess
61 virtual uno::Type SAL_CALL getElementType() override;
62 virtual sal_Bool SAL_CALL hasElements() override;
65 SvxUnoColorTable::SvxUnoColorTable()
66 : pList(XPropertyList::AsColorList(
67 XPropertyList::CreatePropertyList(
68 XPropertyListType::Color, SvtPathOptions().GetPalettePath(), "")))
72 sal_Bool SAL_CALL SvxUnoColorTable::supportsService( const OUString& ServiceName )
74 return cppu::supportsService( this, ServiceName );
77 OUString SAL_CALL SvxUnoColorTable::getImplementationName()
79 return "com.sun.star.drawing.SvxUnoColorTable";
82 uno::Sequence< OUString > SAL_CALL SvxUnoColorTable::getSupportedServiceNames()
84 uno::Sequence<OUString> aSNS { "com.sun.star.drawing.ColorTable" };
85 return aSNS;
88 // XNameContainer
89 void SAL_CALL SvxUnoColorTable::insertByName( const OUString& aName, const uno::Any& aElement )
91 if( hasByName( aName ) )
92 throw container::ElementExistException();
94 Color aColor;
95 if( !(aElement >>= aColor) )
96 throw lang::IllegalArgumentException();
98 if( pList.is() )
100 pList->Insert(std::make_unique<XColorEntry>(aColor, aName));
104 void SAL_CALL SvxUnoColorTable::removeByName( const OUString& Name )
106 tools::Long nIndex = pList.is() ? pList->GetIndex( Name ) : -1;
107 if( nIndex == -1 )
108 throw container::NoSuchElementException();
110 pList->Remove( nIndex );
113 // XNameReplace
114 void SAL_CALL SvxUnoColorTable::replaceByName( const OUString& aName, const uno::Any& aElement )
116 Color nColor;
117 if( !(aElement >>= nColor) )
118 throw lang::IllegalArgumentException();
120 tools::Long nIndex = pList.is() ? pList->GetIndex( aName ) : -1;
121 if( nIndex == -1 )
122 throw container::NoSuchElementException();
124 pList->Replace(nIndex, std::make_unique<XColorEntry>(nColor, aName ));
127 // XNameAccess
128 uno::Any SAL_CALL SvxUnoColorTable::getByName( const OUString& aName )
130 tools::Long nIndex = pList.is() ? pList->GetIndex( aName ) : -1;
131 if( nIndex == -1 )
132 throw container::NoSuchElementException();
134 const XColorEntry* pEntry = pList->GetColor(nIndex);
135 return uno::Any( static_cast<sal_Int32>(pEntry->GetColor().GetRGBColor()) );
138 uno::Sequence< OUString > SAL_CALL SvxUnoColorTable::getElementNames()
140 const tools::Long nCount = pList.is() ? pList->Count() : 0;
142 uno::Sequence< OUString > aSeq( nCount );
143 OUString* pStrings = aSeq.getArray();
145 for( tools::Long nIndex = 0; nIndex < nCount; nIndex++ )
147 const XColorEntry* pEntry = pList->GetColor(nIndex);
148 pStrings[nIndex] = pEntry->GetName();
151 return aSeq;
154 sal_Bool SAL_CALL SvxUnoColorTable::hasByName( const OUString& aName )
156 tools::Long nIndex = pList.is() ? pList->GetIndex( aName ) : -1;
157 return nIndex != -1;
160 // XElementAccess
161 uno::Type SAL_CALL SvxUnoColorTable::getElementType()
163 return ::cppu::UnoType<sal_Int32>::get();
166 sal_Bool SAL_CALL SvxUnoColorTable::hasElements()
168 return pList.is() && pList->Count() != 0;
173 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
174 com_sun_star_drawing_SvxUnoColorTable_get_implementation(
175 css::uno::XComponentContext *,
176 css::uno::Sequence<css::uno::Any> const &)
178 return cppu::acquire(new SvxUnoColorTable);
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */