[Libreoffice-commits] core.git: Branch 'feature/gsoc14-colors' - 2 commits - include/svx svx/Library_svxcore.mk svx/source

Krisztian Pinter pin.terminator at gmail.com
Thu Jul 3 09:49:08 PDT 2014


 include/svx/Palette.hxx                  |   46 ++++++++++
 include/svx/PaletteManager.hxx           |   45 ++++++++++
 include/svx/SvxColorValueSet.hxx         |    4 
 include/svx/tbcontrl.hxx                 |    5 -
 svx/Library_svxcore.mk                   |    2 
 svx/source/tbxctrls/Palette.cxx          |  120 ++++++++++++++++++++++++++++
 svx/source/tbxctrls/PaletteManager.cxx   |  130 +++++++++++++++++++++++++++++++
 svx/source/tbxctrls/SvxColorValueSet.cxx |   35 ++++++--
 svx/source/tbxctrls/colorwindow.hxx      |    9 +-
 svx/source/tbxctrls/tbcontrl.cxx         |   78 +++++-------------
 10 files changed, 406 insertions(+), 68 deletions(-)

New commits:
commit cf8ef7746474c24b93b8083909439f588eb813f6
Author: Krisztian Pinter <pin.terminator at gmail.com>
Date:   Thu Jul 3 18:47:10 2014 +0200

    Add PaletteManager, refactor palette code
    
    Change-Id: I7e30fc895834318514b51bc648d32aa6d297bfae

diff --git a/include/svx/Palette.hxx b/include/svx/Palette.hxx
new file mode 100644
index 0000000..f7ced03
--- /dev/null
+++ b/include/svx/Palette.hxx
@@ -0,0 +1,46 @@
+/* -*- 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 .
+ */
+#ifndef INCLUDED_SVX_PALETTE_HXX
+#define INCLUDED_SVX_PALETTE_HXX
+
+#include <rtl/ustring.hxx>
+#include <tools/color.hxx>
+
+class Palette
+{
+public:
+    typedef std::pair<Color, OString> NamedColor;
+    typedef std::vector< NamedColor > ColorList;
+private:
+    bool        mbLoaded;
+    OUString    maFname;
+    OString     maName;
+    ColorList   maColors;
+
+    void LoadPalette();
+public:
+    Palette(const OUString &rFname);
+
+    const OString&      GetPaletteName();
+    const ColorList&    GetPaletteColors();
+};
+
+#endif // INCLUDED_SVX_PALETTE_HXX
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/include/svx/PaletteManager.hxx b/include/svx/PaletteManager.hxx
new file mode 100644
index 0000000..a6b2dfa
--- /dev/null
+++ b/include/svx/PaletteManager.hxx
@@ -0,0 +1,45 @@
+/* -*- 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 .
+ */
+#ifndef INCLUDED_SVX_PALETTEMANAGER_HXX
+#define INCLUDED_SVX_PALETTEMANAGER_HXX
+
+#include <svx/SvxColorValueSet.hxx>
+#include <rtl/ustring.hxx>
+
+class PaletteManager
+{
+    sal_uInt16  mnNumOfPalettes;
+    sal_uInt16  mnCurrentPalette;
+
+    long        mnColorCount;
+
+    std::vector<Palette> maPalettes;
+public:
+    PaletteManager();
+    void        LoadPalettes();
+    void        ReloadColorSet(SvxColorValueSet& rColorSet);
+    void        PrevPalette();
+    void        NextPalette();
+    long        GetColorCount();
+    OUString    GetPaletteName();
+};
+
+#endif // INCLUDED_SVX_PALETTEMANAGER_HXX
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/include/svx/SvxColorValueSet.hxx b/include/svx/SvxColorValueSet.hxx
index 77ad9b2..7db9f4c 100644
--- a/include/svx/SvxColorValueSet.hxx
+++ b/include/svx/SvxColorValueSet.hxx
@@ -19,21 +19,12 @@
 #ifndef INCLUDED_SVX_SVXCOLORVALUESET_HXX
 #define INCLUDED_SVX_SVXCOLORVALUESET_HXX
 
+#include <svx/Palette.hxx>
 #include <svtools/valueset.hxx>
 #include <svx/svxdllapi.h>
 
 class XColorList;
 
-struct Palette
-{
-    typedef std::pair<Color, OString> NamedColor;
-    typedef std::vector< NamedColor > ColorList;
-    Palette(){};
-    Palette(const OUString &rFname);
-    OString maName;
-    ColorList maColors;
-};
-
 class SVX_DLLPUBLIC SvxColorValueSet : public ValueSet
 {
 private:
@@ -50,7 +41,7 @@ public:
 
     void addEntriesForXColorList(const XColorList& rXColorList, sal_uInt32 nStartIndex = 1);
     void loadColorVector(const std::vector<Color>& rColorVector, const OUString& rNamePrefix, sal_uInt32 nStartIndex = 1);
-    void loadPalette(const Palette& rPalette);
+    void loadPalette(Palette& rPalette);
     Size layoutAllVisible(sal_uInt32 nEntryCount);
     Size layoutToGivenHeight(sal_uInt32 nHeight, sal_uInt32 nEntryCount);
 };
diff --git a/include/svx/tbcontrl.hxx b/include/svx/tbcontrl.hxx
index b1b578f..6d9975f 100644
--- a/include/svx/tbcontrl.hxx
+++ b/include/svx/tbcontrl.hxx
@@ -133,6 +133,7 @@
 #include <svx/svxdllapi.h>
 #include <boost/scoped_ptr.hpp>
 #include <com/sun/star/awt/FontDescriptor.hpp>
+#include <svx/PaletteManager.hxx>
 
 // important im tbxctrls.hxx created HeDaBu !!!
 class SvxLineItem;
@@ -221,7 +222,7 @@ class SVX_DLLPUBLIC SvxColorToolBoxControl : public SfxToolBoxControl
 
     ::boost::scoped_ptr< ::svx::ToolboxButtonColorUpdater > pBtnUpdater;
     Color                               mLastColor;
-    sal_uInt16                          nCurrentPalette;
+    PaletteManager                      mrPaletteManager;
     DECL_LINK( SelectedHdl, Color* );
 public:
     SFX_DECL_TOOLBOX_CONTROL();
@@ -242,7 +243,7 @@ class SVX_DLLPUBLIC SvxLineColorToolBoxControl : public SfxToolBoxControl
 {
     ::boost::scoped_ptr< ::svx::ToolboxButtonColorUpdater > pBtnUpdater;
     Color                               mLastColor;
-    sal_uInt16                          nCurrentPalette;
+    PaletteManager                      mrPaletteManager;
     DECL_LINK( SelectedHdl, Color* );
 public:
     SFX_DECL_TOOLBOX_CONTROL();
diff --git a/svx/Library_svxcore.mk b/svx/Library_svxcore.mk
index 7ed776f..48a8217 100644
--- a/svx/Library_svxcore.mk
+++ b/svx/Library_svxcore.mk
@@ -342,6 +342,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 0000000..aebb7f0
--- /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 0000000..88916ee
--- /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 dc2f2c2..e338109 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 c699986..5a700c7 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 f2608bd..c905d42 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>
@@ -1076,7 +1074,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,
@@ -1088,11 +1086,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 )
@@ -1126,9 +1124,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();
@@ -1143,49 +1138,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;
 
@@ -1197,6 +1163,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()
@@ -1210,7 +1180,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)
@@ -1252,15 +1222,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;
 }
 
@@ -2379,8 +2349,7 @@ SvxColorToolBoxControl::SvxColorToolBoxControl(
     ToolBox& rTbx ) :
 
     SfxToolBoxControl( nSlotId, nId, rTbx ),
-    mLastColor( COL_AUTO ),
-    nCurrentPalette( 0 )
+    mLastColor( COL_AUTO )
 {
     rTbx.SetItemBits( nId, TIB_DROPDOWN | rTbx.GetItemBits( nId ) );
 
@@ -2434,7 +2403,7 @@ SfxPopupWindow* SvxColorToolBoxControl::CreatePopupWindow()
     SvxColorWindow_Impl* pColorWin =
         new SvxColorWindow_Impl(
                             m_aCommandURL,
-                            nCurrentPalette,
+                            mrPaletteManager,
                             GetSlotId(),
                             m_xFrame,
                             SVX_RESSTR( RID_SVXITEMS_EXTRAS_CHARCOLOR ),
@@ -2547,8 +2516,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" ) );
@@ -2569,7 +2537,7 @@ SfxPopupWindow* SvxLineColorToolBoxControl::CreatePopupWindow()
     SvxColorWindow_Impl* pColorWin =
         new SvxColorWindow_Impl(
                             m_aCommandURL,
-                            nCurrentPalette,
+                            mrPaletteManager,
                             GetSlotId(),
                             m_xFrame,
                             SVX_RESSTR( RID_SVXSTR_LINECOLOR ),
commit f316663215e2db129885997f367c356e258a2155
Author: Krisztian Pinter <pin.terminator at gmail.com>
Date:   Tue Jul 1 23:56:25 2014 +0200

    Add GIMP palette loading code
    
    Change-Id: Ie0d0787342bc806a1848cb904114f0ca16c9df69

diff --git a/include/svx/SvxColorValueSet.hxx b/include/svx/SvxColorValueSet.hxx
index 7502d93..77ad9b2 100644
--- a/include/svx/SvxColorValueSet.hxx
+++ b/include/svx/SvxColorValueSet.hxx
@@ -24,6 +24,16 @@
 
 class XColorList;
 
+struct Palette
+{
+    typedef std::pair<Color, OString> NamedColor;
+    typedef std::vector< NamedColor > ColorList;
+    Palette(){};
+    Palette(const OUString &rFname);
+    OString maName;
+    ColorList maColors;
+};
+
 class SVX_DLLPUBLIC SvxColorValueSet : public ValueSet
 {
 private:
@@ -39,7 +49,8 @@ public:
     sal_uInt32 getColumnCount() const;
 
     void addEntriesForXColorList(const XColorList& rXColorList, sal_uInt32 nStartIndex = 1);
-    void addEntriesForColorVector(const std::vector<Color>& rColorVector, sal_uInt32 nStartIndex = 1);
+    void loadColorVector(const std::vector<Color>& rColorVector, const OUString& rNamePrefix, sal_uInt32 nStartIndex = 1);
+    void loadPalette(const Palette& rPalette);
     Size layoutAllVisible(sal_uInt32 nEntryCount);
     Size layoutToGivenHeight(sal_uInt32 nHeight, sal_uInt32 nEntryCount);
 };
diff --git a/svx/source/tbxctrls/SvxColorValueSet.cxx b/svx/source/tbxctrls/SvxColorValueSet.cxx
index b2ada21..dc2f2c2 100644
--- a/svx/source/tbxctrls/SvxColorValueSet.cxx
+++ b/svx/source/tbxctrls/SvxColorValueSet.cxx
@@ -24,6 +24,83 @@
 #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)
@@ -88,12 +165,37 @@ void SvxColorValueSet::addEntriesForXColorList(const XColorList& rXColorList, sa
     }
 }
 
-void SvxColorValueSet::addEntriesForColorVector(const std::vector<Color>& rColorVector, sal_uInt32 nStartIndex)
+void SvxColorValueSet::loadColorVector(const std::vector<Color>& rColorVector, const OUString& rNamePrefix, sal_uInt32 nStartIndex)
+{
+    if(rNamePrefix.getLength() != 0)
+    {
+        for(std::vector<Color>::const_iterator it = rColorVector.begin();
+            it != rColorVector.end(); it++, nStartIndex++)
+        {
+            InsertItem(nStartIndex, *it, rNamePrefix + OUString::number(nStartIndex));
+        }
+    }
+    else
+    {
+        for(std::vector<Color>::const_iterator it = rColorVector.begin();
+            it != rColorVector.end(); it++, nStartIndex++)
+        {
+            InsertItem(nStartIndex, *it, "");
+        }
+    }
+}
+
+
+void SvxColorValueSet::loadPalette(const Palette& rPalette)
 {
-    for(std::vector<Color>::const_iterator it = rColorVector.begin();
-        it != rColorVector.end(); it++, nStartIndex++)
+    const Palette::ColorList &rColors = rPalette.maColors;
+    Clear();
+    int nIx = 1;
+    for(Palette::ColorList::const_iterator it = rColors.begin();
+        it != rColors.end(); ++it)
     {
-        InsertItem(nStartIndex, *it, "");
+        InsertItem(nIx, it->first, OStringToOUString(it->second, RTL_TEXTENCODING_ASCII_US));
+        ++nIx;
     }
 }
 


More information about the Libreoffice-commits mailing list