summaryrefslogtreecommitdiff
path: root/svx/source/tbxctrls/PaletteManager.cxx
blob: 95485ced1a039b341f0cec2b069cc7f84f283d9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <svx/PaletteManager.hxx>
#include <osl/file.hxx>
#include <unotools/pathoptions.hxx>
#include <sfx2/objsh.hxx>
#include "svx/drawitem.hxx"
#include <svx/dialogs.hrc>
#include <svtools/colrdlg.hxx>

#define STR_DEFAULT_PAL         "Default palette"
#define STR_DOC_COLORS          "Document colors"
#define STR_DOC_COLOR_PREFIX    "Document Color "

PaletteManager::PaletteManager() :
    mnNumOfPalettes(2),
    mnCurrentPalette(0),
    mnColorCount(0),
    mLastColor(COL_AUTO)
{
    LoadPalettes();
    mnNumOfPalettes += maPalettes.size();
}

PaletteManager::~PaletteManager()
{
    for( std::vector<Palette*>::iterator it = maPalettes.begin();
         it != maPalettes.end();
         ++it)
    {
        delete *it;
    }
}

void PaletteManager::LoadPalettes()
{
    OUString aPalPath = SvtPathOptions().GetPalettePath();

    osl::Directory aDir(aPalPath);
    maPalettes.clear();
    osl::DirectoryItem aDirItem;
    osl::FileStatus aFileStat( osl_FileStatus_Mask_FileName |
                               osl_FileStatus_Mask_FileURL  |
                               osl_FileStatus_Mask_Type     );
    if( aDir.open() == osl::FileBase::E_None )
    {
        while( aDir.getNextItem(aDirItem) == osl::FileBase::E_None )
        {
            aDirItem.getFileStatus(aFileStat);
            if(aFileStat.isRegular() || aFileStat.isLink())
            {
                OUString aFName = aFileStat.getFileName();
                Palette* pPalette = 0;
                if( aFName.endsWithIgnoreAsciiCase(".gpl") )
                    pPalette = new PaletteGPL( aFileStat.getFileURL(), aFName );
                else if( aFName.endsWithIgnoreAsciiCase(".soc") )
                    pPalette = new PaletteSOC( aFileStat.getFileURL(), aFName );

                if( pPalette && pPalette->IsValid() )
                    maPalettes.push_back( pPalette );
            }
        }
    }
}

void PaletteManager::ReloadColorSet(SvxColorValueSet &rColorSet)
{
    SfxObjectShell* pDocSh = SfxObjectShell::Current();

    if( mnCurrentPalette == 0 )
    {
        const SfxPoolItem* pItem = NULL;
        XColorListRef pColorList;

        if ( pDocSh )
        {
            if ( 0 != ( pItem = pDocSh->GetItem( SID_COLOR_TABLE ) ) )
                pColorList = ( (SvxColorListItem*)pItem )->GetColorList();
        }

        if ( !pColorList.is() )
            pColorList = XColorList::CreateStdColorList();


        if ( pColorList.is() )
        {
            mnColorCount = pColorList->Count();
            rColorSet.Clear();
            rColorSet.addEntriesForXColorList(*pColorList);
        }
    }
    else if( mnCurrentPalette == mnNumOfPalettes - 1 )
    {
        // Add doc colors to palette
        std::vector<Color> aColors = pDocSh->GetDocColors();
        mnColorCount = aColors.size();
        rColorSet.Clear();
        rColorSet.loadColorVector(aColors, STR_DOC_COLOR_PREFIX );
    }
    else
    {
        maPalettes[mnCurrentPalette-1]->LoadColorSet( rColorSet );
        mnColorCount = rColorSet.GetItemCount();
    }
}

std::vector<OUString> PaletteManager::GetPaletteList()
{
    std::vector<OUString> aPaletteNames;

    aPaletteNames.push_back( STR_DEFAULT_PAL );

    for( std::vector<Palette*>::iterator it = maPalettes.begin();
         it != maPalettes.end();
         ++it)
    {
        aPaletteNames.push_back( (*it)->GetName() );
    }

    aPaletteNames.push_back( STR_DOC_COLORS );

    return aPaletteNames;
}

void PaletteManager::SetPalette( sal_Int32 nPos )
{
    mnCurrentPalette = nPos;
}

sal_Int32 PaletteManager::GetPalette()
{
    return mnCurrentPalette;
}

long PaletteManager::GetColorCount()
{
    return mnColorCount;
}

OUString PaletteManager::GetPaletteName()
{
    if( mnCurrentPalette == 0 )
        return OUString( STR_DEFAULT_PAL );
    else if( mnCurrentPalette == mnNumOfPalettes - 1 )
        return OUString( STR_DOC_COLORS );
    else
        return maPalettes[mnCurrentPalette - 1]->GetName();
}

const Color& PaletteManager::GetLastColor()
{
    return mLastColor;
}

void PaletteManager::SetLastColor(const Color& rLastColor)
{
    mLastColor = rLastColor;
}

void PaletteManager::SetBtnUpdater(svx::ToolboxButtonColorUpdater* pBtnUpdater)
{
    mpBtnUpdater = pBtnUpdater;
}

void PaletteManager::PopupColorPicker()
{
    SvColorDialog aColorDlg( 0 );
    aColorDlg.SetColor ( mLastColor );
    aColorDlg.SetMode( svtools::ColorPickerMode_MODIFY );
    if( aColorDlg.Execute() == RET_OK )
    {
        mpBtnUpdater->Update( aColorDlg.GetColor() );
        mLastColor = aColorDlg.GetColor();
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */