bump product version to 4.1.6.2
[LibreOffice.git] / svx / source / sidebar / tools / ColorControl.cxx
blobba43c0fc367d2ecdfa947115106ec60cf6edab5c
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include <svx/sidebar/ColorControl.hxx>
20 #include "svx/svxids.hrc"
21 #include "svx/drawitem.hxx"
22 #include "svx/xtable.hxx"
23 #include "svx/dialmgr.hxx"
24 #include "svx/xflclit.hxx"
25 #include <tools/resid.hxx>
26 #include <sfx2/sidebar/Theme.hxx>
27 #include <sfx2/objsh.hxx>
28 #include <sfx2/bindings.hxx>
29 #include <sfx2/dispatch.hxx>
30 #include <vcl/floatwin.hxx>
31 #include <unotools/pathoptions.hxx>
32 #include <editeng/editrids.hrc>
34 using ::sfx2::sidebar::Theme;
36 namespace svx { namespace sidebar {
38 namespace {
39 short GetItemId_Imp( ValueSet& rValueSet, const Color& rCol )
41 if(rCol == COL_AUTO)
42 return 0;
44 bool bFound = false;
45 sal_uInt16 nCount = rValueSet.GetItemCount();
46 sal_uInt16 n = 1;
48 while ( !bFound && n <= nCount )
50 Color aValCol = rValueSet.GetItemColor(n);
52 bFound = ( aValCol.GetRed() == rCol.GetRed()
53 && aValCol.GetGreen() == rCol.GetGreen()
54 && aValCol.GetBlue() == rCol.GetBlue() );
56 if ( !bFound )
57 n++;
59 return bFound ? n : -1;
61 class JustReleaseDeleter {public:
62 void operator() (XColorList*) const {/* release but don't delete pointer */}
64 XColorListRef GetColorTable (void)
66 SfxObjectShell* pDocSh = SfxObjectShell::Current();
67 DBG_ASSERT(pDocSh!=NULL, "DocShell not found!");
68 if (pDocSh != NULL)
70 const SfxPoolItem* pItem = pDocSh->GetItem(SID_COLOR_TABLE);
71 if (pItem != NULL)
73 XColorListRef xTable = ((SvxColorListItem*)pItem)->GetColorList();
74 if (xTable.is())
75 return xTable;
79 return XColorList::GetStdColorList();
81 } // end of anonymous namespace
86 ColorControl::ColorControl (
87 Window* pParent,
88 SfxBindings* /* pBindings */,
89 const ResId& rControlResId,
90 const ResId& rValueSetResId,
91 const ::boost::function<Color(void)>& rNoColorGetter,
92 const ::boost::function<void(String&,Color)>& rColorSetter,
93 FloatingWindow* pFloatingWindow,
94 const ResId* pNoColorStringResId) // const sal_uInt32 nNoColorStringResId)
95 : PopupControl(pParent, rControlResId),
96 maVSColor(this, rValueSetResId),
97 mpFloatingWindow(pFloatingWindow),
98 msNoColorString(
99 pNoColorStringResId
100 ? String(*pNoColorStringResId)
101 : String()),
102 maNoColorGetter(rNoColorGetter),
103 maColorSetter(rColorSetter)
105 FreeResource();
106 FillColors();
111 ColorControl::~ColorControl (void)
118 void ColorControl::FillColors (void)
120 XColorListRef xColorTable (GetColorTable());
122 if (xColorTable.is())
124 const long nColorCount(xColorTable->Count());
125 if (nColorCount <= 0)
126 return;
128 const WinBits aWinBits(maVSColor.GetStyle() | WB_TABSTOP | WB_ITEMBORDER | WB_NAMEFIELD |
129 WB_NO_DIRECTSELECT | WB_MENUSTYLEVALUESET | WB_NO_DIRECTSELECT);
131 maVSColor.SetStyle(aWinBits);
133 // neds to be done *before* layouting
134 if(msNoColorString.Len() > 0)
136 maVSColor.SetStyle(maVSColor.GetStyle() | WB_NONEFIELD);
137 maVSColor.SetText(msNoColorString);
140 const Size aNewSize(maVSColor.layoutAllVisible(nColorCount));
141 maVSColor.SetOutputSizePixel(aNewSize);
142 static sal_Int32 nAdd = 4;
144 SetOutputSizePixel(Size(aNewSize.Width() + nAdd, aNewSize.Height() + nAdd));
145 Link aLink = LINK(this, ColorControl, VSSelectHdl);
146 maVSColor.SetSelectHdl(aLink);
148 // Now, after all calls to SetStyle, we can change the
149 // background color.
150 maVSColor.SetBackground(Theme::GetWallpaper(Theme::Paint_DropDownBackground));
152 // add entrties
153 maVSColor.Clear();
154 maVSColor.addEntriesForXColorList(*xColorTable);
157 maVSColor.Show();
163 void ColorControl::GetFocus (void)
165 maVSColor.GrabFocus();
171 void ColorControl::SetCurColorSelect (Color aCol, bool bAvailable)
173 // FillColors();
174 short nCol = GetItemId_Imp( maVSColor, aCol );
175 if(! bAvailable)
177 maVSColor.SetNoSelection();
178 return;
181 //if not found
182 if( nCol == -1)
184 maVSColor.SetNoSelection();
186 else
188 // remove selection first to force evtl. scroll when scroll is needed
189 maVSColor.SetNoSelection();
190 maVSColor.SelectItem(nCol);
197 IMPL_LINK(ColorControl, VSSelectHdl, void *, pControl)
199 if(pControl == &maVSColor)
201 sal_uInt16 iPos = maVSColor.GetSelectItemId();
202 Color aColor = maVSColor.GetItemColor( iPos );
203 String aTmpStr = maVSColor.GetItemText( iPos );
205 // react when the WB_NONEFIELD created entry is selected
206 if (aColor.GetColor() == 0 && aTmpStr.Len() == 0)
208 if (maNoColorGetter)
209 aColor = maNoColorGetter();
211 if (maColorSetter)
212 maColorSetter(aTmpStr, aColor);
214 if (mpFloatingWindow!=NULL && mpFloatingWindow->IsInPopupMode())
215 mpFloatingWindow->EndPopupMode();
218 return 0;
222 } } // end of namespace svx::sidebar