diff options
author | Krisztian Pinter <pin.terminator@gmail.com> | 2014-07-03 18:47:10 +0200 |
---|---|---|
committer | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2014-09-17 10:19:34 +0200 |
commit | e3fcf5591eb2ac28a96315fb089f5c88e0c6aa1f (patch) | |
tree | a222b9cf0839f156959f08d58c8468f5a7649726 /svx | |
parent | 24933a3c920af29add1a19f19dbb8a8f129659cf (diff) |
Add PaletteManager, refactor palette code
Change-Id: I7e30fc895834318514b51bc648d32aa6d297bfae
Diffstat (limited to 'svx')
-rw-r--r-- | svx/Library_svxcore.mk | 2 | ||||
-rw-r--r-- | svx/source/tbxctrls/Palette.cxx | 120 | ||||
-rw-r--r-- | svx/source/tbxctrls/PaletteManager.cxx | 130 | ||||
-rw-r--r-- | svx/source/tbxctrls/SvxColorValueSet.cxx | 83 | ||||
-rw-r--r-- | svx/source/tbxctrls/colorwindow.hxx | 9 | ||||
-rw-r--r-- | svx/source/tbxctrls/tbcontrl.cxx | 78 |
6 files changed, 282 insertions, 140 deletions
diff --git a/svx/Library_svxcore.mk b/svx/Library_svxcore.mk index 65c69fbbc4cb..fadb007bccf0 100644 --- a/svx/Library_svxcore.mk +++ b/svx/Library_svxcore.mk @@ -344,6 +344,8 @@ $(eval $(call gb_Library_add_exception_objects,svxcore,\ svx/source/table/viewcontactoftableobj \ svx/source/tbxctrls/extrusioncontrols \ svx/source/tbxctrls/fontworkgallery \ + svx/source/tbxctrls/Palette \ + svx/source/tbxctrls/PaletteManager \ svx/source/tbxctrls/tbcontrl \ svx/source/tbxctrls/tbxcolorupdate \ svx/source/tbxctrls/SvxColorValueSet \ 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 b0e3f075cd93..a05556e5d831 100644 --- a/svx/source/tbxctrls/colorwindow.hxx +++ b/svx/source/tbxctrls/colorwindow.hxx @@ -26,6 +26,7 @@ #include <rtl/ustring.hxx> #include <com/sun/star/frame/XFrame.hpp> #include <svx/SvxColorValueSet.hxx> +#include <svx/PaletteManager.hxx> class SvxColorWindow_Impl : public SfxPopupWindow { @@ -36,15 +37,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 * ); @@ -56,7 +57,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 08da983a5e4e..e002db8a02aa 100644 --- a/svx/source/tbxctrls/tbcontrl.cxx +++ b/svx/source/tbxctrls/tbcontrl.cxx @@ -30,11 +30,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> @@ -1003,7 +1001,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, @@ -1013,11 +1011,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 ) @@ -1051,9 +1049,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(); @@ -1068,49 +1063,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; @@ -1122,6 +1088,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() @@ -1135,7 +1105,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) @@ -1177,15 +1147,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; } @@ -2222,8 +2192,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 ) ); @@ -2277,7 +2246,7 @@ SfxPopupWindow* SvxColorToolBoxControl::CreatePopupWindow() SvxColorWindow_Impl* pColorWin = new SvxColorWindow_Impl( m_aCommandURL, - nCurrentPalette, + mrPaletteManager, GetSlotId(), m_xFrame, SVX_RESSTR( RID_SVXITEMS_EXTRAS_CHARCOLOR ), @@ -2385,8 +2354,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" ) ); @@ -2407,7 +2375,7 @@ SfxPopupWindow* SvxLineColorToolBoxControl::CreatePopupWindow() SvxColorWindow_Impl* pColorWin = new SvxColorWindow_Impl( m_aCommandURL, - nCurrentPalette, + mrPaletteManager, GetSlotId(), m_xFrame, SVX_RESSTR( RID_SVXSTR_LINECOLOR ), |