summaryrefslogtreecommitdiff
path: root/svx/source
diff options
context:
space:
mode:
authorKrisztian Pinter <pin.terminator@gmail.com>2014-07-03 18:47:10 +0200
committerKrisztian Pinter <pin.terminator@gmail.com>2014-07-25 17:14:12 +0200
commit0ac41beac1495e86b103889ffa8d2ba6bf64ddcd (patch)
tree8693979221a9343754b50d58a90666d53942faf3 /svx/source
parent1cff66591f531f2acc40a4400c704f8708d8027d (diff)
Add PaletteManager, refactor palette code
Change-Id: I7e30fc895834318514b51bc648d32aa6d297bfae
Diffstat (limited to 'svx/source')
-rw-r--r--svx/source/tbxctrls/Palette.cxx120
-rw-r--r--svx/source/tbxctrls/PaletteManager.cxx130
-rw-r--r--svx/source/tbxctrls/SvxColorValueSet.cxx83
-rw-r--r--svx/source/tbxctrls/colorwindow.hxx9
-rw-r--r--svx/source/tbxctrls/tbcontrl.cxx78
5 files changed, 280 insertions, 140 deletions
diff --git a/svx/source/tbxctrls/Palette.cxx b/svx/source/tbxctrls/Palette.cxx
new file mode 100644
index 000000000000..aebb7f0fa48d
--- /dev/null
+++ b/svx/source/tbxctrls/Palette.cxx
@@ -0,0 +1,120 @@
+/* -*- 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/Palette.hxx>
+#include <tools/stream.hxx>
+
+// finds first token in rStr from index, separated by whitespace
+// returns position of next token in index
+OString lcl_getToken(const OString& rStr, sal_Int32& index)
+{
+ sal_Int32 substart, toklen = 0;
+
+ while(index < rStr.getLength() &&
+ (rStr[index] == ' ' || rStr[index] == '\n' || rStr[index] == '\t'))
+ ++index;
+ if(index == rStr.getLength())
+ {
+ index = -1;
+ return OString();
+ }
+ substart = index;
+
+ while(index < rStr.getLength() &&
+ !(rStr[index] == ' ' || rStr[index] == '\n' || rStr[index] == '\t'))
+ {
+ ++index;
+ ++toklen;
+ }
+
+ while(index < rStr.getLength() &&
+ (rStr[index] == ' ' || rStr[index] == '\n' || rStr[index] == '\t'))
+ ++index;
+ if(index == rStr.getLength())
+ index = -1;
+
+ return rStr.copy(substart, toklen);
+}
+
+Palette::Palette(const OUString &rFname) :
+ mbLoaded( false ),
+ maFname( rFname ){}
+
+const OString& Palette::GetPaletteName()
+{
+ LoadPalette();
+ return maName;
+}
+
+const Palette::ColorList& Palette::GetPaletteColors()
+{
+ LoadPalette();
+ return maColors;
+}
+
+void Palette::LoadPalette()
+{
+ if( mbLoaded ) return;
+
+ mbLoaded = true;
+
+ // TODO add error handling!!!
+ SvFileStream aFile(maFname, STREAM_READ);
+
+ OString aLine;
+
+ aFile.ReadLine(aLine);
+ if( !aLine.startsWith("GIMP Palette") ) return;
+ aFile.ReadLine(aLine);
+ if( aLine.startsWith("Name: ", &maName) )
+ {
+ aFile.ReadLine(aLine);
+ if( aLine.startsWith("Columns: "))
+ aFile.ReadLine(aLine); // we can ignore this
+ }
+
+ do {
+ if (aLine[0] != '#' && aLine[0] != '\n')
+ {
+ // TODO check if r,g,b are 0<= x <=255, or just clamp?
+ sal_Int32 nIndex = 0;
+ OString token;
+
+ token = lcl_getToken(aLine, nIndex);
+ if(token == "" || nIndex == -1) continue;
+ sal_Int32 r = token.toInt32();
+
+ token = lcl_getToken(aLine, nIndex);
+ if(token == "" || nIndex == -1) continue;
+ sal_Int32 g = token.toInt32();
+
+ token = lcl_getToken(aLine, nIndex);
+ if(token == "") continue;
+ sal_Int32 b = token.toInt32();
+
+ OString name;
+ if(nIndex != -1)
+ name = aLine.copy(nIndex);
+
+ maColors.push_back(std::make_pair(Color(r, g, b), name));
+ }
+ } while (aFile.ReadLine(aLine));
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/svx/source/tbxctrls/PaletteManager.cxx b/svx/source/tbxctrls/PaletteManager.cxx
new file mode 100644
index 000000000000..88916ee85fc7
--- /dev/null
+++ b/svx/source/tbxctrls/PaletteManager.cxx
@@ -0,0 +1,130 @@
+/* -*- 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>
+
+PaletteManager::PaletteManager() :
+ mnNumOfPalettes(2),
+ mnCurrentPalette(0),
+ mnColorCount(0)
+{
+ LoadPalettes();
+ mnNumOfPalettes += maPalettes.size();
+}
+
+void PaletteManager::LoadPalettes()
+{
+ OUString aPalPath = SvtPathOptions().GetPalettePath();
+
+ osl::Directory aDir(aPalPath);
+ maPalettes.clear();
+ osl::DirectoryItem aDirItem;
+ osl::FileStatus aFileStat(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 aPath = aFileStat.getFileURL();
+ if(aPath.getLength() > 4 &&
+ aPath.copy(aPath.getLength()-4).toAsciiLowerCase() == ".gpl")
+ {
+ maPalettes.push_back(Palette(aPath));
+ }
+ }
+ }
+ }
+}
+
+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, "Document Color ");
+ }
+ else
+ {
+ Palette& rPal = maPalettes[mnCurrentPalette-1];
+ mnColorCount = rPal.GetPaletteColors().size();
+ rColorSet.Clear();
+ rColorSet.loadPalette(rPal);
+ }
+}
+
+void PaletteManager::PrevPalette()
+{
+ mnCurrentPalette = mnCurrentPalette == 0 ? mnNumOfPalettes - 1 : mnCurrentPalette - 1;
+}
+
+void PaletteManager::NextPalette()
+{
+ mnCurrentPalette = mnCurrentPalette == mnNumOfPalettes - 1 ? 0 : mnCurrentPalette + 1;
+}
+
+long PaletteManager::GetColorCount()
+{
+ return mnColorCount;
+}
+
+OUString PaletteManager::GetPaletteName()
+{
+ if( mnCurrentPalette == 0 )
+ return OUString("Default palette");
+ else if( mnCurrentPalette == mnNumOfPalettes - 1 )
+ return OUString("Document colors");
+ else
+ return OStringToOUString(maPalettes[mnCurrentPalette - 1].GetPaletteName(), RTL_TEXTENCODING_ASCII_US);
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/svx/source/tbxctrls/SvxColorValueSet.cxx b/svx/source/tbxctrls/SvxColorValueSet.cxx
index dc2f2c23d89e..e33810946d3b 100644
--- a/svx/source/tbxctrls/SvxColorValueSet.cxx
+++ b/svx/source/tbxctrls/SvxColorValueSet.cxx
@@ -23,85 +23,6 @@
#include <vcl/svapp.hxx>
#include <vcl/settings.hxx>
-
-// finds first token in rStr from index, separated by whitespace
-// returns position of next token in index
-OString lcl_getToken(const OString& rStr, sal_Int32& index)
-{
- sal_Int32 substart, toklen = 0;
-
- while(index < rStr.getLength() &&
- (rStr[index] == ' ' || rStr[index] == '\n' || rStr[index] == '\t'))
- ++index;
- if(index == rStr.getLength())
- {
- index = -1;
- return OString();
- }
- substart = index;
-
- while(index < rStr.getLength() &&
- !(rStr[index] == ' ' || rStr[index] == '\n' || rStr[index] == '\t'))
- {
- ++index;
- ++toklen;
- }
-
- while(index < rStr.getLength() &&
- (rStr[index] == ' ' || rStr[index] == '\n' || rStr[index] == '\t'))
- ++index;
- if(index == rStr.getLength())
- index = -1;
-
- return rStr.copy(substart, toklen);
-}
-
-Palette::Palette(const OUString &rFname)
-{
- // TODO add error handling!!!
- SvFileStream aFile(rFname, STREAM_READ);
-
- OString aPaletteName;
- OString aLine;
-
- aFile.ReadLine(aLine);
- if( !aLine.startsWith("GIMP Palette") ) return;
- aFile.ReadLine(aLine);
- if( aLine.startsWith("Name: ", &aPaletteName) )
- {
- aFile.ReadLine(aLine);
- if( aLine.startsWith("Columns: "))
- aFile.ReadLine(aLine); // we can ignore this
- }
-
- do {
- if (aLine[0] != '#' && aLine[0] != '\n')
- {
- // TODO check if r,g,b are 0<= x <=255, or just clamp?
- sal_Int32 nIndex = 0;
- OString token;
-
- token = lcl_getToken(aLine, nIndex);
- if(token == "" || nIndex == -1) continue;
- sal_Int32 r = token.toInt32();
-
- token = lcl_getToken(aLine, nIndex);
- if(token == "" || nIndex == -1) continue;
- sal_Int32 g = token.toInt32();
-
- token = lcl_getToken(aLine, nIndex);
- if(token == "") continue;
- sal_Int32 b = token.toInt32();
-
- OString name;
- if(nIndex != -1)
- name = aLine.copy(nIndex);
-
- maColors.push_back(std::make_pair(Color(r, g, b), name));
- }
- } while (aFile.ReadLine(aLine));
-}
-
SvxColorValueSet::SvxColorValueSet(Window* _pParent, WinBits nWinStyle)
: ValueSet(_pParent, nWinStyle)
{
@@ -186,9 +107,9 @@ void SvxColorValueSet::loadColorVector(const std::vector<Color>& rColorVector, c
}
-void SvxColorValueSet::loadPalette(const Palette& rPalette)
+void SvxColorValueSet::loadPalette(Palette& rPalette)
{
- const Palette::ColorList &rColors = rPalette.maColors;
+ const Palette::ColorList &rColors = rPalette.GetPaletteColors();
Clear();
int nIx = 1;
for(Palette::ColorList::const_iterator it = rColors.begin();
diff --git a/svx/source/tbxctrls/colorwindow.hxx b/svx/source/tbxctrls/colorwindow.hxx
index c6999860d535..5a700c7297a8 100644
--- a/svx/source/tbxctrls/colorwindow.hxx
+++ b/svx/source/tbxctrls/colorwindow.hxx
@@ -29,6 +29,7 @@
#include <rtl/ustring.hxx>
#include <com/sun/star/frame/XFrame.hpp>
#include <svx/SvxColorValueSet.hxx>
+#include <svx/PaletteManager.hxx>
// class SvxColorWindow_Impl --------------------------------------------------
@@ -43,15 +44,15 @@ private:
SvxColorValueSet aColorSet;
PushButton aButtonLeft;
PushButton aButtonRight;
+ FixedText aPaletteName;
OUString maCommand;
Link maSelectedLink;
const sal_uInt16 nNavButtonWidth;
const sal_uInt16 nNavButtonHeight;
- sal_uInt16& rnCurrentPalette;
- sal_uInt16 nNumOfPalettes;
+ PaletteManager& mrPaletteManager;
- void ReloadColorSet();
+ void Update();
DECL_LINK( SelectHdl, void * );
DECL_LINK( StepLeftClickHdl, void * );
@@ -63,7 +64,7 @@ protected:
public:
SvxColorWindow_Impl( const OUString& rCommand,
- sal_uInt16& rnCurrentPalette_,
+ PaletteManager& rPaletteManager,
sal_uInt16 nSlotId,
const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& rFrame,
const OUString& rWndTitle,
diff --git a/svx/source/tbxctrls/tbcontrl.cxx b/svx/source/tbxctrls/tbcontrl.cxx
index da69d77b145c..179355223023 100644
--- a/svx/source/tbxctrls/tbcontrl.cxx
+++ b/svx/source/tbxctrls/tbcontrl.cxx
@@ -31,11 +31,9 @@
#include <svtools/ctrltool.hxx>
#include <svtools/borderhelper.hxx>
#include <svl/stritem.hxx>
-#include <unotools/pathoptions.hxx>
#include <sfx2/tplpitem.hxx>
#include <sfx2/dispatch.hxx>
#include <sfx2/viewsh.hxx>
-#include <sfx2/objsh.hxx>
#include <sfx2/docfac.hxx>
#include <sfx2/templdlg.hxx>
#include <svl/isethint.hxx>
@@ -1019,7 +1017,7 @@ void SvxFontNameBox_Impl::Select()
#endif
SvxColorWindow_Impl::SvxColorWindow_Impl( const OUString& rCommand,
- sal_uInt16& rnCurrentPalette_,
+ PaletteManager& rPaletteManager,
sal_uInt16 nSlotId,
const Reference< XFrame >& rFrame,
const OUString& rWndTitle,
@@ -1029,11 +1027,11 @@ SvxColorWindow_Impl::SvxColorWindow_Impl( const OUString& rCommand,
aColorSet ( this, WinBits( WB_ITEMBORDER | WB_NAMEFIELD | WB_3DLOOK | WB_NO_DIRECTSELECT) ),
aButtonLeft ( this ),
aButtonRight( this ),
+ aPaletteName( this ),
maCommand( rCommand ),
nNavButtonWidth ( 20 ),
nNavButtonHeight( 20 ),
- rnCurrentPalette( rnCurrentPalette_ ),
- nNumOfPalettes( 1 )
+ mrPaletteManager( rPaletteManager )
{
if ( SID_ATTR_CHAR_COLOR_BACKGROUND == theSlotId || SID_BACKGROUND_COLOR == theSlotId )
@@ -1067,9 +1065,6 @@ SvxColorWindow_Impl::SvxColorWindow_Impl( const OUString& rCommand,
aColorSet.SetAccessibleName( SVX_RESSTR( RID_SVXSTR_LINECOLOR ) );
}
- if( SfxObjectShell::Current()->GetDocColors().size() > 0 )
- nNumOfPalettes++;
-
aButtonLeft.SetText("<");
aButtonLeft.SetClickHdl( LINK( this, SvxColorWindow_Impl, StepLeftClickHdl ) );
aButtonLeft.Show();
@@ -1084,49 +1079,20 @@ SvxColorWindow_Impl::SvxColorWindow_Impl( const OUString& rCommand,
SetText( rWndTitle );
aColorSet.Show();
+ aPaletteName.Show();
+
AddStatusListener( OUString( ".uno:ColorTableState" ));
AddStatusListener( maCommand );
- ReloadColorSet();
+ Update();
}
-void SvxColorWindow_Impl::ReloadColorSet()
-{
- SfxObjectShell* pDocSh = SfxObjectShell::Current();
- long nColorCount = 0;
-
- if( rnCurrentPalette == 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() )
- {
- nColorCount = pColorList->Count();
- aColorSet.Clear();
- aColorSet.addEntriesForXColorList(*pColorList);
- }
- }
- else if( rnCurrentPalette == nNumOfPalettes - 1 )
- {
- // Add doc colors to palette
- std::vector<Color> aColors = pDocSh->GetDocColors();
- nColorCount = aColors.size();
- aColorSet.Clear();
- aColorSet.addEntriesForColorVector(aColors);
- }
+void SvxColorWindow_Impl::Update()
+{
+ mrPaletteManager.ReloadColorSet(aColorSet);
- const Size aNewSize(aColorSet.layoutAllVisible(nColorCount));
+ const Size aNewSize(aColorSet.layoutAllVisible(mrPaletteManager.GetColorCount()));
aColorSet.SetOutputSizePixel(aNewSize);
static sal_Int32 nAdd = 4;
@@ -1138,6 +1104,10 @@ void SvxColorWindow_Impl::ReloadColorSet()
aButtonRight.SetSizePixel(Size(nNavButtonWidth, nNavButtonHeight));
aButtonRight.SetPosPixel(Point(aNewSize.Width() + nAdd - nNavButtonWidth, aNewSize.Height() + nAdd + 1));
+
+ aPaletteName.SetSizePixel(Size(150, nNavButtonHeight));
+ aPaletteName.SetPosPixel(Point(nNavButtonWidth, aNewSize.Height() + nAdd + 1));
+ aPaletteName.SetText(mrPaletteManager.GetPaletteName());
}
SvxColorWindow_Impl::~SvxColorWindow_Impl()
@@ -1151,7 +1121,7 @@ void SvxColorWindow_Impl::KeyInput( const KeyEvent& rKEvt )
SfxPopupWindow* SvxColorWindow_Impl::Clone() const
{
- return new SvxColorWindow_Impl( maCommand, rnCurrentPalette, theSlotId, GetFrame(), GetText(), GetParent() );
+ return new SvxColorWindow_Impl( maCommand, mrPaletteManager, theSlotId, GetFrame(), GetText(), GetParent() );
}
IMPL_LINK_NOARG(SvxColorWindow_Impl, SelectHdl)
@@ -1193,15 +1163,15 @@ IMPL_LINK_NOARG(SvxColorWindow_Impl, SelectHdl)
IMPL_LINK_NOARG(SvxColorWindow_Impl, StepLeftClickHdl)
{
- rnCurrentPalette = (rnCurrentPalette - 1) % nNumOfPalettes;
- ReloadColorSet();
+ mrPaletteManager.PrevPalette();
+ Update();
return 0;
}
IMPL_LINK_NOARG(SvxColorWindow_Impl, StepRightClickHdl)
{
- rnCurrentPalette = (rnCurrentPalette + 1) % nNumOfPalettes;
- ReloadColorSet();
+ mrPaletteManager.NextPalette();
+ Update();
return 0;
}
@@ -2238,8 +2208,7 @@ SvxColorToolBoxControl::SvxColorToolBoxControl(
sal_uInt16 nId,
ToolBox& rTbx ) :
SfxToolBoxControl( nSlotId, nId, rTbx ),
- mLastColor( COL_AUTO ),
- nCurrentPalette( 0 )
+ mLastColor( COL_AUTO )
{
rTbx.SetItemBits( nId, TIB_DROPDOWN | rTbx.GetItemBits( nId ) );
@@ -2293,7 +2262,7 @@ SfxPopupWindow* SvxColorToolBoxControl::CreatePopupWindow()
SvxColorWindow_Impl* pColorWin =
new SvxColorWindow_Impl(
m_aCommandURL,
- nCurrentPalette,
+ mrPaletteManager,
GetSlotId(),
m_xFrame,
SVX_RESSTR( RID_SVXITEMS_EXTRAS_CHARCOLOR ),
@@ -2401,8 +2370,7 @@ SvxLineColorToolBoxControl::SvxLineColorToolBoxControl(
ToolBox& rTbx ) :
SfxToolBoxControl( nSlotId, nId, rTbx ),
- mLastColor( COL_BLACK ),
- nCurrentPalette( 0 )
+ mLastColor( COL_BLACK )
{
rTbx.SetItemBits( nId, TIB_DROPDOWN | rTbx.GetItemBits( nId ) );
addStatusListener( OUString( ".uno:XLineColor" ) );
@@ -2423,7 +2391,7 @@ SfxPopupWindow* SvxLineColorToolBoxControl::CreatePopupWindow()
SvxColorWindow_Impl* pColorWin =
new SvxColorWindow_Impl(
m_aCommandURL,
- nCurrentPalette,
+ mrPaletteManager,
GetSlotId(),
m_xFrame,
SVX_RESSTR( RID_SVXSTR_LINECOLOR ),