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 <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/implbase2.hxx>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <rtl/ref.hxx>
27 #include <svx/xtable.hxx>
29 using namespace ::com::sun::star
;
33 class SvxUnoColorTable
: public cppu::WeakImplHelper2
< container::XNameContainer
, lang::XServiceInfo
>
42 virtual OUString SAL_CALL
getImplementationName() throw( uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
43 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) throw( uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
44 virtual uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() throw( uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
47 virtual void SAL_CALL
insertByName( const OUString
& aName
, const uno::Any
& aElement
) throw( lang::IllegalArgumentException
, container::ElementExistException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
48 virtual void SAL_CALL
removeByName( const OUString
& Name
) throw( container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
51 virtual void SAL_CALL
replaceByName( const OUString
& aName
, const uno::Any
& aElement
) throw( lang::IllegalArgumentException
, container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
54 virtual uno::Any SAL_CALL
getByName( const OUString
& aName
) throw( container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
56 virtual uno::Sequence
< OUString
> SAL_CALL
getElementNames() throw( uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
58 virtual sal_Bool SAL_CALL
hasByName( const OUString
& aName
) throw( uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
61 virtual uno::Type SAL_CALL
getElementType() throw( uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
62 virtual sal_Bool SAL_CALL
hasElements() throw( uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
65 SvxUnoColorTable::SvxUnoColorTable()
67 pList
= XPropertyList::AsColorList(
68 XPropertyList::CreatePropertyList(
69 XCOLOR_LIST
, SvtPathOptions().GetPalettePath(), ""));
72 sal_Bool SAL_CALL
SvxUnoColorTable::supportsService( const OUString
& ServiceName
) throw(uno::RuntimeException
, std::exception
)
74 return cppu::supportsService( this, ServiceName
);
77 OUString SAL_CALL
SvxUnoColorTable::getImplementationName() throw( uno::RuntimeException
, std::exception
)
79 return OUString("com.sun.star.drawing.SvxUnoColorTable");
82 uno::Sequence
< OUString
> SAL_CALL
SvxUnoColorTable::getSupportedServiceNames()
83 throw( uno::RuntimeException
, std::exception
)
85 uno::Sequence
< OUString
> aSNS( 1 );
86 aSNS
.getArray()[0] = "com.sun.star.drawing.ColorTable";
91 void SAL_CALL
SvxUnoColorTable::insertByName( const OUString
& aName
, const uno::Any
& aElement
)
92 throw( lang::IllegalArgumentException
, container::ElementExistException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
)
94 if( hasByName( aName
) )
95 throw container::ElementExistException();
98 if( !(aElement
>>= nColor
) )
99 throw lang::IllegalArgumentException();
103 XColorEntry
* pEntry
= new XColorEntry( Color( (ColorData
)nColor
), aName
);
104 pList
->Insert( pEntry
, pList
->Count() );
108 void SAL_CALL
SvxUnoColorTable::removeByName( const OUString
& Name
)
109 throw( container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
)
111 long nIndex
= pList
.is() ? pList
->GetIndex( Name
) : -1;
113 throw container::NoSuchElementException();
115 pList
->Remove( nIndex
);
119 void SAL_CALL
SvxUnoColorTable::replaceByName( const OUString
& aName
, const uno::Any
& aElement
)
120 throw( lang::IllegalArgumentException
, container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
)
122 sal_Int32 nColor
= 0;
123 if( !(aElement
>>= nColor
) )
124 throw lang::IllegalArgumentException();
126 long nIndex
= pList
.is() ? pList
->GetIndex( aName
) : -1;
128 throw container::NoSuchElementException();
130 XColorEntry
* pEntry
= new XColorEntry( Color( (ColorData
)nColor
), aName
);
131 delete pList
->Replace( nIndex
, pEntry
);
135 uno::Any SAL_CALL
SvxUnoColorTable::getByName( const OUString
& aName
)
136 throw( container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
)
138 long nIndex
= pList
.is() ? pList
->GetIndex( aName
) : -1;
140 throw container::NoSuchElementException();
142 XColorEntry
* pEntry
= pList
->GetColor( nIndex
);
143 return uno::Any( (sal_Int32
) pEntry
->GetColor().GetRGBColor() );
146 uno::Sequence
< OUString
> SAL_CALL
SvxUnoColorTable::getElementNames()
147 throw( uno::RuntimeException
, std::exception
)
149 const long nCount
= pList
.is() ? pList
->Count() : 0;
151 uno::Sequence
< OUString
> aSeq( nCount
);
152 OUString
* pStrings
= aSeq
.getArray();
154 for( long nIndex
= 0; nIndex
< nCount
; nIndex
++ )
156 XColorEntry
* pEntry
= pList
->GetColor( (long)nIndex
);
157 pStrings
[nIndex
] = pEntry
->GetName();
163 sal_Bool SAL_CALL
SvxUnoColorTable::hasByName( const OUString
& aName
)
164 throw( uno::RuntimeException
, std::exception
)
166 long nIndex
= pList
.is() ? pList
->GetIndex( aName
) : -1;
171 uno::Type SAL_CALL
SvxUnoColorTable::getElementType()
172 throw( uno::RuntimeException
, std::exception
)
174 return ::cppu::UnoType
<sal_Int32
>::get();
177 sal_Bool SAL_CALL
SvxUnoColorTable::hasElements()
178 throw( uno::RuntimeException
, std::exception
)
180 return pList
.is() && pList
->Count() != 0;
185 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
186 com_sun_star_drawing_SvxUnoColorTable_get_implementation(
187 css::uno::XComponentContext
*,
188 css::uno::Sequence
<css::uno::Any
> const &)
190 return cppu::acquire(new SvxUnoColorTable
);
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */