1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #include "vbapalette.hxx"
21
22 #include <sal/macros.h>
23 #include <cppuhelper/implbase.hxx>
24 #include <sfx2/objsh.hxx>
25 #include <docsh.hxx>
26 #include <com/sun/star/beans/XPropertySet.hpp>
27 #include <com/sun/star/container/XIndexAccess.hpp>
28 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
29 #include "excelvbahelper.hxx"
30
31 using namespace ::com::sun::star;
32 using namespace ::ooo::vba;
33
34 /** Standard EGA colors, bright. */
35 #define EXC_PALETTE_EGA_COLORS_LIGHT \
36 Color(0x000000), Color(0xFFFFFF), Color(0xFF0000), Color(0x00FF00), Color(0x0000FF), Color(0xFFFF00), Color(0xFF00FF), Color(0x00FFFF)
37 /** Standard EGA colors), dark. */
38 #define EXC_PALETTE_EGA_COLORS_DARK \
39 Color(0x800000), Color(0x008000), Color(0x000080), Color(0x808000), Color(0x800080), Color(0x008080), Color(0xC0C0C0), Color(0x808080)
40
41 static const Color spnDefColorTable8[] =
42 {
43 /* 8 */ EXC_PALETTE_EGA_COLORS_LIGHT,
44 /* 16 */ EXC_PALETTE_EGA_COLORS_DARK,
45 /* 24 */ Color(0x9999FF), Color(0x993366), Color(0xFFFFCC), Color(0xCCFFFF), Color(0x660066), Color(0xFF8080), Color(0x0066CC), Color(0xCCCCFF),
46 /* 32 */ Color(0x000080), Color(0xFF00FF), Color(0xFFFF00), Color(0x00FFFF), Color(0x800080), Color(0x800000), Color(0x008080), Color(0x0000FF),
47 /* 40 */ Color(0x00CCFF), Color(0xCCFFFF), Color(0xCCFFCC), Color(0xFFFF99), Color(0x99CCFF), Color(0xFF99CC), Color(0xCC99FF), Color(0xFFCC99),
48 /* 48 */ Color(0x3366FF), Color(0x33CCCC), Color(0x99CC00), Color(0xFFCC00), Color(0xFF9900), Color(0xFF6600), Color(0x666699), Color(0x969696),
49 /* 56 */ Color(0x003366), Color(0x339966), Color(0x003300), Color(0x333300), Color(0x993300), Color(0x993366), Color(0x333399), Color(0x333333)
50 };
51
52 typedef ::cppu::WeakImplHelper< container::XIndexAccess > XIndexAccess_BASE;
53
54 class DefaultPalette : public XIndexAccess_BASE
55 {
56 public:
DefaultPalette()57 DefaultPalette(){}
58
59 // Methods XIndexAccess
getCount()60 virtual ::sal_Int32 SAL_CALL getCount() override
61 {
62 return SAL_N_ELEMENTS(spnDefColorTable8);
63 }
64
getByIndex(::sal_Int32 Index)65 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
66 {
67 if ( Index < 0 || Index >= getCount() )
68 throw lang::IndexOutOfBoundsException();
69 return uno::makeAny( sal_Int32( spnDefColorTable8[ Index ] ) );
70 }
71
72 // Methods XElementAccess
getElementType()73 virtual uno::Type SAL_CALL getElementType() override
74 {
75 return ::cppu::UnoType<sal_Int32>::get();
76 }
hasElements()77 virtual sal_Bool SAL_CALL hasElements() override
78 {
79 return true;
80 }
81
82 };
83
ScVbaPalette(const uno::Reference<frame::XModel> & rxModel)84 ScVbaPalette::ScVbaPalette( const uno::Reference< frame::XModel >& rxModel ) :
85 m_pShell( excel::getDocShell( rxModel ) )
86 {
87 }
88
89 uno::Reference< container::XIndexAccess >
getDefaultPalette()90 ScVbaPalette::getDefaultPalette()
91 {
92 return new DefaultPalette();
93 }
94
95 uno::Reference< container::XIndexAccess >
getPalette() const96 ScVbaPalette::getPalette() const
97 {
98 uno::Reference< container::XIndexAccess > xIndex;
99 uno::Reference< beans::XPropertySet > xProps;
100 if ( !m_pShell )
101 throw uno::RuntimeException("Can't extract palette, no doc shell" );
102
103 xProps.set( m_pShell->GetModel(), uno::UNO_QUERY_THROW );
104
105 xIndex.set( xProps->getPropertyValue("ColorPalette"), uno::UNO_QUERY );
106 if ( !xIndex.is() )
107 return new DefaultPalette();
108 return xIndex;
109 }
110
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
112