Adjust includes
[LibreOffice.git] / svx / source / unodraw / unoctabl.cxx
blob17e0817c26218174deeb51543cbf2242c2b84301
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>
28 #include <o3tl/make_unique.hxx>
30 using namespace ::com::sun::star;
32 namespace {
34 class SvxUnoColorTable : public cppu::WeakImplHelper< container::XNameContainer, lang::XServiceInfo >
36 private:
37 XColorListRef pList;
39 public:
40 SvxUnoColorTable();
42 // XServiceInfo
43 virtual OUString SAL_CALL getImplementationName() override;
44 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
45 virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
47 // XNameContainer
48 virtual void SAL_CALL insertByName( const OUString& aName, const uno::Any& aElement ) override;
49 virtual void SAL_CALL removeByName( const OUString& Name ) override;
51 // XNameReplace
52 virtual void SAL_CALL replaceByName( const OUString& aName, const uno::Any& aElement ) override;
54 // XNameAccess
55 virtual uno::Any SAL_CALL getByName( const OUString& aName ) override;
57 virtual uno::Sequence< OUString > SAL_CALL getElementNames() override;
59 virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override;
61 // XElementAccess
62 virtual uno::Type SAL_CALL getElementType() override;
63 virtual sal_Bool SAL_CALL hasElements() override;
66 SvxUnoColorTable::SvxUnoColorTable()
68 pList = XPropertyList::AsColorList(
69 XPropertyList::CreatePropertyList(
70 XPropertyListType::Color, SvtPathOptions().GetPalettePath(), ""));
73 sal_Bool SAL_CALL SvxUnoColorTable::supportsService( const OUString& ServiceName )
75 return cppu::supportsService( this, ServiceName );
78 OUString SAL_CALL SvxUnoColorTable::getImplementationName()
80 return OUString("com.sun.star.drawing.SvxUnoColorTable");
83 uno::Sequence< OUString > SAL_CALL SvxUnoColorTable::getSupportedServiceNames()
85 uno::Sequence<OUString> aSNS { "com.sun.star.drawing.ColorTable" };
86 return aSNS;
89 // XNameContainer
90 void SAL_CALL SvxUnoColorTable::insertByName( const OUString& aName, const uno::Any& aElement )
92 if( hasByName( aName ) )
93 throw container::ElementExistException();
95 sal_Int32 nColor = 0;
96 if( !(aElement >>= nColor) )
97 throw lang::IllegalArgumentException();
99 if( pList.is() )
101 pList->Insert(o3tl::make_unique<XColorEntry>(Color((ColorData)nColor), aName));
105 void SAL_CALL SvxUnoColorTable::removeByName( const OUString& Name )
107 long nIndex = pList.is() ? pList->GetIndex( Name ) : -1;
108 if( nIndex == -1 )
109 throw container::NoSuchElementException();
111 pList->Remove( nIndex );
114 // XNameReplace
115 void SAL_CALL SvxUnoColorTable::replaceByName( const OUString& aName, const uno::Any& aElement )
117 sal_Int32 nColor = 0;
118 if( !(aElement >>= nColor) )
119 throw lang::IllegalArgumentException();
121 long nIndex = pList.is() ? pList->GetIndex( aName ) : -1;
122 if( nIndex == -1 )
123 throw container::NoSuchElementException();
125 pList->Replace(nIndex, o3tl::make_unique<XColorEntry>(Color((ColorData)nColor), aName ));
128 // XNameAccess
129 uno::Any SAL_CALL SvxUnoColorTable::getByName( const OUString& aName )
131 long nIndex = pList.is() ? pList->GetIndex( aName ) : -1;
132 if( nIndex == -1 )
133 throw container::NoSuchElementException();
135 const XColorEntry* pEntry = pList->GetColor(nIndex);
136 return uno::Any( (sal_Int32) pEntry->GetColor().GetRGBColor() );
139 uno::Sequence< OUString > SAL_CALL SvxUnoColorTable::getElementNames()
141 const long nCount = pList.is() ? pList->Count() : 0;
143 uno::Sequence< OUString > aSeq( nCount );
144 OUString* pStrings = aSeq.getArray();
146 for( long nIndex = 0; nIndex < nCount; nIndex++ )
148 const XColorEntry* pEntry = pList->GetColor(nIndex);
149 pStrings[nIndex] = pEntry->GetName();
152 return aSeq;
155 sal_Bool SAL_CALL SvxUnoColorTable::hasByName( const OUString& aName )
157 long nIndex = pList.is() ? pList->GetIndex( aName ) : -1;
158 return nIndex != -1;
161 // XElementAccess
162 uno::Type SAL_CALL SvxUnoColorTable::getElementType()
164 return ::cppu::UnoType<sal_Int32>::get();
167 sal_Bool SAL_CALL SvxUnoColorTable::hasElements()
169 return pList.is() && pList->Count() != 0;
174 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
175 com_sun_star_drawing_SvxUnoColorTable_get_implementation(
176 css::uno::XComponentContext *,
177 css::uno::Sequence<css::uno::Any> const &)
179 return cppu::acquire(new SvxUnoColorTable);
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */