bump product version to 5.0.4.1
[LibreOffice.git] / svx / source / tbxctrls / PaletteManager.cxx
blob109f934f1b3ba41039667d1ce2c9e7f21277a9d5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <svx/PaletteManager.hxx>
21 #include <osl/file.hxx>
22 #include <unotools/pathoptions.hxx>
23 #include <sfx2/objsh.hxx>
24 #include <svx/drawitem.hxx>
25 #include <svx/dialogs.hrc>
26 #include <svx/dialmgr.hxx>
27 #include <svtools/colrdlg.hxx>
28 #include <vcl/svapp.hxx>
29 #include <vcl/settings.hxx>
31 PaletteManager::PaletteManager() :
32 mnMaxRecentColors(Application::GetSettings().GetStyleSettings().GetColorValueSetColumnCount()),
33 mnNumOfPalettes(2),
34 mnCurrentPalette(0),
35 mnColorCount(0),
36 mpBtnUpdater(NULL),
37 mLastColor(COL_AUTO)
39 LoadPalettes();
40 mnNumOfPalettes += maPalettes.size();
43 PaletteManager::~PaletteManager()
47 void PaletteManager::LoadPalettes()
49 maPalettes.clear();
50 OUString aPalPath = SvtPathOptions().GetPalettePath();
52 osl::Directory aDir(aPalPath);
53 osl::DirectoryItem aDirItem;
54 osl::FileStatus aFileStat( osl_FileStatus_Mask_FileName |
55 osl_FileStatus_Mask_FileURL |
56 osl_FileStatus_Mask_Type );
57 if( aDir.open() == osl::FileBase::E_None )
59 while( aDir.getNextItem(aDirItem) == osl::FileBase::E_None )
61 aDirItem.getFileStatus(aFileStat);
62 if(aFileStat.isRegular() || aFileStat.isLink())
64 OUString aFName = aFileStat.getFileName();
65 Palette* pPalette = 0;
66 if( aFName.endsWithIgnoreAsciiCase(".gpl") )
67 pPalette = new PaletteGPL( aFileStat.getFileURL(), aFName );
68 else if( aFName.endsWithIgnoreAsciiCase(".soc") )
69 pPalette = new PaletteSOC( aFileStat.getFileURL(), aFName );
70 else if ( aFName.endsWithIgnoreAsciiCase(".ase") )
71 pPalette = new PaletteASE( aFileStat.getFileURL(), aFName );
73 if( pPalette && pPalette->IsValid() )
74 maPalettes.push_back( pPalette );
80 void PaletteManager::ReloadColorSet(SvxColorValueSet &rColorSet)
82 SfxObjectShell* pDocSh = SfxObjectShell::Current();
84 if( mnCurrentPalette == 0 )
86 XColorListRef pColorList;
88 if ( pDocSh )
90 const SfxPoolItem* pItem = NULL;
91 if ( 0 != ( pItem = pDocSh->GetItem( SID_COLOR_TABLE ) ) )
92 pColorList = static_cast<const SvxColorListItem*>(pItem)->GetColorList();
95 if ( !pColorList.is() )
96 pColorList = XColorList::CreateStdColorList();
99 if ( pColorList.is() )
101 mnColorCount = pColorList->Count();
102 rColorSet.Clear();
103 rColorSet.addEntriesForXColorList(*pColorList);
106 else if( mnCurrentPalette == mnNumOfPalettes - 1 )
108 // Add doc colors to palette
109 std::set<Color> aColors = pDocSh->GetDocColors();
110 mnColorCount = aColors.size();
111 rColorSet.Clear();
112 rColorSet.addEntriesForColorSet(aColors, SVX_RESSTR( RID_SVXSTR_DOC_COLOR_PREFIX ) + " " );
114 else
116 maPalettes[mnCurrentPalette-1].LoadColorSet( rColorSet );
117 mnColorCount = rColorSet.GetItemCount();
121 void PaletteManager::ReloadRecentColorSet(SvxColorValueSet& rColorSet)
123 rColorSet.Clear();
124 int nIx = 1;
125 for(std::deque<Color>::const_iterator it = maRecentColors.begin();
126 it != maRecentColors.end(); ++it)
128 rColorSet.InsertItem(nIx, *it, "");
129 ++nIx;
133 std::vector<OUString> PaletteManager::GetPaletteList()
135 std::vector<OUString> aPaletteNames;
137 aPaletteNames.push_back( SVX_RESSTR( RID_SVXSTR_DEFAULT_PAL ) );
139 for( boost::ptr_vector<Palette>::iterator it = maPalettes.begin();
140 it != maPalettes.end();
141 ++it)
143 aPaletteNames.push_back( (*it).GetName() );
146 aPaletteNames.push_back( SVX_RESSTR ( RID_SVXSTR_DOC_COLORS ) );
148 return aPaletteNames;
151 void PaletteManager::SetPalette( sal_Int32 nPos )
153 mnCurrentPalette = nPos;
156 sal_Int32 PaletteManager::GetPalette()
158 return mnCurrentPalette;
161 long PaletteManager::GetColorCount()
163 return mnColorCount;
166 long PaletteManager::GetRecentColorCount()
168 return maRecentColors.size();
171 const Color& PaletteManager::GetLastColor()
173 return mLastColor;
176 void PaletteManager::SetLastColor(const Color& rLastColor)
178 mLastColor = rLastColor;
181 void PaletteManager::AddRecentColor(const Color& rRecentColor)
183 std::deque<Color>::iterator itColor =
184 std::find(maRecentColors.begin(), maRecentColors.end(), rRecentColor);
185 // if recent color to be added is already in list, remove it
186 if( itColor != maRecentColors.end() )
187 maRecentColors.erase( itColor );
189 maRecentColors.push_front( rRecentColor );
190 if( maRecentColors.size() > mnMaxRecentColors )
191 maRecentColors.pop_back();
194 void PaletteManager::SetBtnUpdater(svx::ToolboxButtonColorUpdater* pBtnUpdater)
196 mpBtnUpdater = pBtnUpdater;
199 void PaletteManager::PopupColorPicker(const OUString& aCommand)
201 // The calling object goes away during aColorDlg.Execute(), so we must copy this
202 OUString aCommandCopy = aCommand;
203 SvColorDialog aColorDlg( 0 );
204 aColorDlg.SetColor ( mLastColor );
205 aColorDlg.SetMode( svtools::ColorPickerMode_MODIFY );
206 if( aColorDlg.Execute() == RET_OK )
208 if (mpBtnUpdater)
209 mpBtnUpdater->Update( aColorDlg.GetColor() );
210 mLastColor = aColorDlg.GetColor();
211 AddRecentColor( mLastColor );
212 DispatchColorCommand(aCommandCopy, mLastColor);
216 void PaletteManager::DispatchColorCommand(const OUString& aCommand, const Color& rColor)
218 using namespace css::uno;
219 using namespace css::frame;
220 using namespace css::beans;
221 using namespace css::util;
223 Reference<XComponentContext> xContext(comphelper::getProcessComponentContext());
224 Reference<XDesktop2> xDesktop = Desktop::create(xContext);
225 Reference<XDispatchProvider> xDispatchProvider(xDesktop->getCurrentFrame(), UNO_QUERY );
226 if (xDispatchProvider.is())
228 INetURLObject aObj( aCommand );
230 Sequence<PropertyValue> aArgs(1);
231 aArgs[0].Name = aObj.GetURLPath();
232 aArgs[0].Value = makeAny(sal_Int32(rColor.GetColor()));
234 URL aTargetURL;
235 aTargetURL.Complete = aCommand;
236 Reference<XURLTransformer> xURLTransformer(URLTransformer::create(comphelper::getProcessComponentContext()));
237 xURLTransformer->parseStrict(aTargetURL);
239 Reference<XDispatch> xDispatch = xDispatchProvider->queryDispatch(aTargetURL, OUString(), 0);
240 if (xDispatch.is())
241 xDispatch->dispatch(aTargetURL, aArgs);
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */