2014-07-03 18:47:10 +02:00
|
|
|
/* -*- 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>
|
2014-07-30 17:15:49 +02:00
|
|
|
#include <svx/drawitem.hxx>
|
2014-07-03 18:47:10 +02:00
|
|
|
#include <svx/dialogs.hrc>
|
2014-11-22 19:49:14 +01:00
|
|
|
#include <svx/dialmgr.hxx>
|
2015-08-31 20:33:37 -04:00
|
|
|
#include <vcl/toolbox.hxx>
|
2014-07-08 19:08:38 +02:00
|
|
|
#include <svtools/colrdlg.hxx>
|
2014-10-28 21:09:16 +02:00
|
|
|
#include <vcl/svapp.hxx>
|
|
|
|
#include <vcl/settings.hxx>
|
2015-06-06 14:24:46 +01:00
|
|
|
#include <stack>
|
|
|
|
#include <set>
|
2016-07-29 18:39:46 +05:30
|
|
|
#include <cppu/unotype.hxx>
|
|
|
|
#include <officecfg/Office/Common.hxx>
|
2014-07-03 18:47:10 +02:00
|
|
|
|
|
|
|
PaletteManager::PaletteManager() :
|
2014-10-28 21:09:16 +02:00
|
|
|
mnMaxRecentColors(Application::GetSettings().GetStyleSettings().GetColorValueSetColumnCount()),
|
2016-08-02 23:53:38 +05:30
|
|
|
mnNumOfPalettes(2),
|
2014-07-03 18:47:10 +02:00
|
|
|
mnCurrentPalette(0),
|
2014-07-08 19:08:38 +02:00
|
|
|
mnColorCount(0),
|
2015-11-10 10:25:28 +01:00
|
|
|
mpBtnUpdater(nullptr),
|
2015-08-19 23:43:39 +02:00
|
|
|
mLastColor(COL_AUTO),
|
2016-07-29 18:39:46 +05:30
|
|
|
maColorSelectFunction(PaletteManager::DispatchColorCommand),
|
|
|
|
m_context(comphelper::getProcessComponentContext())
|
2014-07-03 18:47:10 +02:00
|
|
|
{
|
2016-07-24 17:23:40 +05:30
|
|
|
SfxObjectShell* pDocSh = SfxObjectShell::Current();
|
|
|
|
if(pDocSh)
|
|
|
|
{
|
|
|
|
const SfxPoolItem* pItem = nullptr;
|
|
|
|
if( nullptr != ( pItem = pDocSh->GetItem(SID_COLOR_TABLE) ) )
|
|
|
|
pColorList = static_cast<const SvxColorListItem*>(pItem)->GetColorList();
|
|
|
|
}
|
|
|
|
if(!pColorList.is())
|
|
|
|
pColorList = XColorList::CreateStdColorList();
|
2014-07-03 18:47:10 +02:00
|
|
|
LoadPalettes();
|
2015-07-23 22:21:49 +02:00
|
|
|
mnNumOfPalettes += m_Palettes.size();
|
2014-07-03 18:47:10 +02:00
|
|
|
}
|
|
|
|
|
2014-07-25 16:45:13 +02:00
|
|
|
PaletteManager::~PaletteManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-03 18:47:10 +02:00
|
|
|
void PaletteManager::LoadPalettes()
|
|
|
|
{
|
2015-07-23 22:21:49 +02:00
|
|
|
m_Palettes.clear();
|
2015-06-06 14:24:46 +01:00
|
|
|
OUString aPalPaths = SvtPathOptions().GetPalettePath();
|
2014-07-03 18:47:10 +02:00
|
|
|
|
2015-06-06 14:24:46 +01:00
|
|
|
std::stack<OUString> aDirs;
|
|
|
|
sal_Int32 nIndex = 0;
|
|
|
|
do
|
2014-07-03 18:47:10 +02:00
|
|
|
{
|
2015-06-06 14:24:46 +01:00
|
|
|
aDirs.push(aPalPaths.getToken(0, ';', nIndex));
|
|
|
|
}
|
|
|
|
while (nIndex >= 0);
|
|
|
|
|
|
|
|
std::set<OUString> aNames;
|
|
|
|
//try all entries palette path list user first, then
|
|
|
|
//system, ignoring duplicate file names
|
|
|
|
while (!aDirs.empty())
|
|
|
|
{
|
|
|
|
OUString aPalPath = aDirs.top();
|
|
|
|
aDirs.pop();
|
|
|
|
|
|
|
|
osl::Directory aDir(aPalPath);
|
|
|
|
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 )
|
2014-07-03 18:47:10 +02:00
|
|
|
{
|
2015-06-06 14:24:46 +01:00
|
|
|
while( aDir.getNextItem(aDirItem) == osl::FileBase::E_None )
|
2014-07-03 18:47:10 +02:00
|
|
|
{
|
2015-06-06 14:24:46 +01:00
|
|
|
aDirItem.getFileStatus(aFileStat);
|
|
|
|
if(aFileStat.isRegular() || aFileStat.isLink())
|
|
|
|
{
|
|
|
|
OUString aFName = aFileStat.getFileName();
|
2016-07-24 17:23:40 +05:30
|
|
|
INetURLObject aURLObj( aFileStat.getFileURL() );
|
|
|
|
OUString aFNameWithoutExt = aURLObj.GetBase();
|
2015-06-06 14:24:46 +01:00
|
|
|
if (aNames.find(aFName) == aNames.end())
|
|
|
|
{
|
2015-07-23 22:21:49 +02:00
|
|
|
std::unique_ptr<Palette> pPalette;
|
2015-06-06 14:24:46 +01:00
|
|
|
if( aFName.endsWithIgnoreAsciiCase(".gpl") )
|
2016-07-24 17:23:40 +05:30
|
|
|
pPalette.reset(new PaletteGPL(aFileStat.getFileURL(), aFNameWithoutExt));
|
2015-06-06 14:24:46 +01:00
|
|
|
else if( aFName.endsWithIgnoreAsciiCase(".soc") )
|
2016-07-24 17:23:40 +05:30
|
|
|
pPalette.reset(new PaletteSOC(aFileStat.getFileURL(), aFNameWithoutExt));
|
2015-06-06 14:24:46 +01:00
|
|
|
else if ( aFName.endsWithIgnoreAsciiCase(".ase") )
|
2016-07-24 17:23:40 +05:30
|
|
|
pPalette.reset(new PaletteASE(aFileStat.getFileURL(), aFNameWithoutExt));
|
2014-07-25 16:45:13 +02:00
|
|
|
|
2015-06-06 14:24:46 +01:00
|
|
|
if( pPalette && pPalette->IsValid() )
|
2015-07-23 22:21:49 +02:00
|
|
|
m_Palettes.push_back( std::move(pPalette) );
|
2016-07-24 17:23:40 +05:30
|
|
|
aNames.insert(aFNameWithoutExt);
|
2015-06-06 14:24:46 +01:00
|
|
|
}
|
|
|
|
}
|
2014-07-03 18:47:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PaletteManager::ReloadColorSet(SvxColorValueSet &rColorSet)
|
|
|
|
{
|
|
|
|
SfxObjectShell* pDocSh = SfxObjectShell::Current();
|
|
|
|
|
2016-08-02 23:53:38 +05:30
|
|
|
if( mnCurrentPalette == 0)
|
|
|
|
{
|
|
|
|
rColorSet.Clear();
|
|
|
|
css::uno::Sequence< sal_Int32 > CustomColorList( officecfg::Office::Common::UserColors::CustomColor::get() );
|
|
|
|
css::uno::Sequence< OUString > CustomColorNameList( officecfg::Office::Common::UserColors::CustomColorName::get() );
|
|
|
|
int nIx = 1;
|
|
|
|
for( int i = 0;i < CustomColorList.getLength();i++ )
|
|
|
|
{
|
|
|
|
Color aColor( CustomColorList[i] );
|
|
|
|
OUString aColorName( CustomColorNameList[i] );
|
|
|
|
rColorSet.InsertItem( nIx, aColor, aColorName );
|
|
|
|
++nIx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( mnCurrentPalette == mnNumOfPalettes - 1 )
|
2014-07-03 18:47:10 +02:00
|
|
|
{
|
|
|
|
// Add doc colors to palette
|
2015-02-14 02:46:47 +02:00
|
|
|
std::set<Color> aColors = pDocSh->GetDocColors();
|
2014-07-03 18:47:10 +02:00
|
|
|
mnColorCount = aColors.size();
|
|
|
|
rColorSet.Clear();
|
2015-02-14 02:46:47 +02:00
|
|
|
rColorSet.addEntriesForColorSet(aColors, SVX_RESSTR( RID_SVXSTR_DOC_COLOR_PREFIX ) + " " );
|
2014-07-03 18:47:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-02 23:53:38 +05:30
|
|
|
m_Palettes[mnCurrentPalette - 1]->LoadColorSet( rColorSet );
|
2014-07-25 16:45:13 +02:00
|
|
|
mnColorCount = rColorSet.GetItemCount();
|
2014-07-03 18:47:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-11 17:35:50 +02:00
|
|
|
void PaletteManager::ReloadRecentColorSet(SvxColorValueSet& rColorSet)
|
|
|
|
{
|
2016-07-29 18:39:46 +05:30
|
|
|
maRecentColors.clear();
|
|
|
|
css::uno::Sequence< sal_Int32 > Colorlist(officecfg::Office::Common::UserColors::RecentColor::get());
|
|
|
|
for(int i = 0;i < Colorlist.getLength();i++)
|
|
|
|
{
|
|
|
|
Color aColor( Colorlist[i] );
|
|
|
|
maRecentColors.push_back( aColor );
|
|
|
|
}
|
2014-08-11 17:35:50 +02:00
|
|
|
rColorSet.Clear();
|
|
|
|
int nIx = 1;
|
|
|
|
for(std::deque<Color>::const_iterator it = maRecentColors.begin();
|
|
|
|
it != maRecentColors.end(); ++it)
|
|
|
|
{
|
|
|
|
rColorSet.InsertItem(nIx, *it, "");
|
|
|
|
++nIx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 16:14:29 +02:00
|
|
|
std::vector<OUString> PaletteManager::GetPaletteList()
|
|
|
|
{
|
|
|
|
std::vector<OUString> aPaletteNames;
|
|
|
|
|
2016-08-02 23:53:38 +05:30
|
|
|
aPaletteNames.push_back( SVX_RESSTR( RID_SVXSTR_CUSTOM_PAL ) );
|
2015-07-23 22:21:49 +02:00
|
|
|
for (auto const& it : m_Palettes)
|
2014-07-10 16:14:29 +02:00
|
|
|
{
|
2014-07-30 19:45:37 +02:00
|
|
|
aPaletteNames.push_back( (*it).GetName() );
|
2014-07-10 16:14:29 +02:00
|
|
|
}
|
2014-11-22 19:49:14 +01:00
|
|
|
aPaletteNames.push_back( SVX_RESSTR ( RID_SVXSTR_DOC_COLORS ) );
|
2014-07-10 16:14:29 +02:00
|
|
|
|
|
|
|
return aPaletteNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PaletteManager::SetPalette( sal_Int32 nPos )
|
2014-07-03 18:47:10 +02:00
|
|
|
{
|
2016-08-02 23:53:38 +05:30
|
|
|
mnCurrentPalette = nPos;
|
|
|
|
if( nPos != mnNumOfPalettes - 1 && nPos != 0)
|
2016-07-24 17:23:40 +05:30
|
|
|
{
|
|
|
|
pColorList = XPropertyList::AsColorList(
|
|
|
|
XPropertyList::CreatePropertyListFromURL(
|
2016-08-16 15:51:26 +02:00
|
|
|
XPropertyListType::Color, GetSelectedPalettePath()));
|
2016-08-02 23:53:38 +05:30
|
|
|
pColorList->SetName(GetPaletteName());
|
2016-07-24 17:23:40 +05:30
|
|
|
if(pColorList->Load())
|
|
|
|
{
|
|
|
|
SfxObjectShell* pShell = SfxObjectShell::Current();
|
2016-08-19 16:49:42 +02:00
|
|
|
if (pShell != nullptr)
|
|
|
|
{
|
|
|
|
SvxColorListItem aColorItem(pColorList, SID_COLOR_TABLE);
|
|
|
|
pShell->PutItem( aColorItem );
|
|
|
|
}
|
2016-07-24 17:23:40 +05:30
|
|
|
}
|
|
|
|
}
|
2016-08-02 23:53:38 +05:30
|
|
|
std::shared_ptr<comphelper::ConfigurationChanges> batch(comphelper::ConfigurationChanges::create(m_context));
|
|
|
|
officecfg::Office::Common::UserColors::PaletteName::set(GetPaletteName(), batch);
|
|
|
|
batch->commit();
|
2014-07-03 18:47:10 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 16:14:29 +02:00
|
|
|
sal_Int32 PaletteManager::GetPalette()
|
2014-07-03 18:47:10 +02:00
|
|
|
{
|
2014-07-10 16:14:29 +02:00
|
|
|
return mnCurrentPalette;
|
2014-07-03 18:47:10 +02:00
|
|
|
}
|
|
|
|
|
2016-07-24 17:23:40 +05:30
|
|
|
OUString PaletteManager::GetPaletteName()
|
|
|
|
{
|
2016-08-02 23:53:38 +05:30
|
|
|
std::vector<OUString> aNames(GetPaletteList());
|
|
|
|
if(mnCurrentPalette != mnNumOfPalettes - 1 && mnCurrentPalette != 0)
|
|
|
|
{
|
|
|
|
SfxObjectShell* pDocSh = SfxObjectShell::Current();
|
|
|
|
if(pDocSh)
|
|
|
|
{
|
|
|
|
const SfxPoolItem* pItem = nullptr;
|
|
|
|
if( nullptr != ( pItem = pDocSh->GetItem(SID_COLOR_TABLE) ) )
|
|
|
|
pColorList = static_cast<const SvxColorListItem*>(pItem)->GetColorList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return aNames[mnCurrentPalette];
|
|
|
|
}
|
|
|
|
|
|
|
|
OUString PaletteManager::GetSelectedPalettePath()
|
|
|
|
{
|
|
|
|
if(mnCurrentPalette != mnNumOfPalettes - 1 && mnCurrentPalette != 0)
|
|
|
|
return m_Palettes[mnCurrentPalette - 1]->GetPath();
|
|
|
|
else
|
|
|
|
return OUString();
|
2016-07-24 17:23:40 +05:30
|
|
|
}
|
|
|
|
|
2014-07-03 18:47:10 +02:00
|
|
|
long PaletteManager::GetColorCount()
|
|
|
|
{
|
|
|
|
return mnColorCount;
|
|
|
|
}
|
|
|
|
|
2014-08-11 17:35:50 +02:00
|
|
|
long PaletteManager::GetRecentColorCount()
|
|
|
|
{
|
|
|
|
return maRecentColors.size();
|
|
|
|
}
|
|
|
|
|
2014-07-08 19:08:38 +02:00
|
|
|
const Color& PaletteManager::GetLastColor()
|
|
|
|
{
|
|
|
|
return mLastColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PaletteManager::SetLastColor(const Color& rLastColor)
|
|
|
|
{
|
|
|
|
mLastColor = rLastColor;
|
|
|
|
}
|
|
|
|
|
2014-08-11 17:35:50 +02:00
|
|
|
void PaletteManager::AddRecentColor(const Color& rRecentColor)
|
|
|
|
{
|
|
|
|
std::deque<Color>::iterator itColor =
|
|
|
|
std::find(maRecentColors.begin(), maRecentColors.end(), rRecentColor);
|
|
|
|
// if recent color to be added is already in list, remove it
|
|
|
|
if( itColor != maRecentColors.end() )
|
|
|
|
maRecentColors.erase( itColor );
|
|
|
|
|
|
|
|
maRecentColors.push_front( rRecentColor );
|
|
|
|
if( maRecentColors.size() > mnMaxRecentColors )
|
|
|
|
maRecentColors.pop_back();
|
2016-07-29 18:39:46 +05:30
|
|
|
css::uno::Sequence< sal_Int32 > aColorList(maRecentColors.size());
|
2016-07-30 22:17:50 +05:30
|
|
|
for(std::deque<Color>::size_type i = 0;i < maRecentColors.size();i++)
|
2016-07-29 18:39:46 +05:30
|
|
|
{
|
2016-07-30 09:22:45 +05:30
|
|
|
aColorList[i] = static_cast<sal_Int32>(maRecentColors[i].GetColor());
|
2016-07-29 18:39:46 +05:30
|
|
|
}
|
|
|
|
std::shared_ptr<comphelper::ConfigurationChanges> batch(comphelper::ConfigurationChanges::create(m_context));
|
|
|
|
officecfg::Office::Common::UserColors::RecentColor::set(aColorList, batch);
|
|
|
|
batch->commit();
|
2014-08-11 17:35:50 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 19:08:38 +02:00
|
|
|
void PaletteManager::SetBtnUpdater(svx::ToolboxButtonColorUpdater* pBtnUpdater)
|
|
|
|
{
|
|
|
|
mpBtnUpdater = pBtnUpdater;
|
|
|
|
}
|
|
|
|
|
2016-04-11 13:51:50 +02:00
|
|
|
void PaletteManager::SetColorSelectFunction(const std::function<void(const OUString&, const Color&)>& aColorSelectFunction)
|
2015-08-19 23:43:39 +02:00
|
|
|
{
|
|
|
|
maColorSelectFunction = aColorSelectFunction;
|
|
|
|
}
|
|
|
|
|
2014-09-17 18:58:05 +03:00
|
|
|
void PaletteManager::PopupColorPicker(const OUString& aCommand)
|
2014-07-08 19:08:38 +02:00
|
|
|
{
|
2014-10-19 16:24:55 +08:00
|
|
|
// The calling object goes away during aColorDlg.Execute(), so we must copy this
|
2016-06-04 20:43:44 +01:00
|
|
|
OUString aCommandCopy = aCommand;
|
2015-11-10 10:25:28 +01:00
|
|
|
SvColorDialog aColorDlg( nullptr );
|
2014-07-08 19:08:38 +02:00
|
|
|
aColorDlg.SetColor ( mLastColor );
|
|
|
|
aColorDlg.SetMode( svtools::ColorPickerMode_MODIFY );
|
|
|
|
if( aColorDlg.Execute() == RET_OK )
|
|
|
|
{
|
2014-09-25 11:42:04 +01:00
|
|
|
if (mpBtnUpdater)
|
|
|
|
mpBtnUpdater->Update( aColorDlg.GetColor() );
|
2014-07-08 19:08:38 +02:00
|
|
|
mLastColor = aColorDlg.GetColor();
|
2014-08-11 17:35:50 +02:00
|
|
|
AddRecentColor( mLastColor );
|
2015-08-19 23:43:39 +02:00
|
|
|
maColorSelectFunction(aCommandCopy, mLastColor);
|
2014-07-30 17:15:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 10:11:00 +00:00
|
|
|
void PaletteManager::DispatchColorCommand(const OUString& aCommand, const Color& rColor)
|
2014-07-30 17:15:49 +02:00
|
|
|
{
|
|
|
|
using namespace css::uno;
|
|
|
|
using namespace css::frame;
|
|
|
|
using namespace css::beans;
|
|
|
|
using namespace css::util;
|
|
|
|
|
|
|
|
Reference<XComponentContext> xContext(comphelper::getProcessComponentContext());
|
|
|
|
Reference<XDesktop2> xDesktop = Desktop::create(xContext);
|
|
|
|
Reference<XDispatchProvider> xDispatchProvider(xDesktop->getCurrentFrame(), UNO_QUERY );
|
|
|
|
if (xDispatchProvider.is())
|
|
|
|
{
|
|
|
|
INetURLObject aObj( aCommand );
|
|
|
|
|
|
|
|
Sequence<PropertyValue> aArgs(1);
|
|
|
|
aArgs[0].Name = aObj.GetURLPath();
|
2015-03-09 10:11:00 +00:00
|
|
|
aArgs[0].Value = makeAny(sal_Int32(rColor.GetColor()));
|
2014-07-30 17:15:49 +02:00
|
|
|
|
|
|
|
URL aTargetURL;
|
|
|
|
aTargetURL.Complete = aCommand;
|
|
|
|
Reference<XURLTransformer> xURLTransformer(URLTransformer::create(comphelper::getProcessComponentContext()));
|
|
|
|
xURLTransformer->parseStrict(aTargetURL);
|
|
|
|
|
|
|
|
Reference<XDispatch> xDispatch = xDispatchProvider->queryDispatch(aTargetURL, OUString(), 0);
|
|
|
|
if (xDispatch.is())
|
|
|
|
xDispatch->dispatch(aTargetURL, aArgs);
|
2014-07-08 19:08:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 18:47:10 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|