Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / sidebar / tools / ColorControl.cxx
blobd94db5d29cdd32fffac2a8c028e49bae099082de
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/sidebar/ColorControl.hxx>
21 #include "svx/svxids.hrc"
22 #include "svx/drawitem.hxx"
23 #include "svx/xtable.hxx"
24 #include "svx/dialmgr.hxx"
25 #include "svx/xflclit.hxx"
26 #include <tools/resid.hxx>
27 #include <sfx2/sidebar/Theme.hxx>
28 #include <sfx2/objsh.hxx>
29 #include <sfx2/bindings.hxx>
30 #include <sfx2/dispatch.hxx>
31 #include <vcl/floatwin.hxx>
32 #include <unotools/pathoptions.hxx>
33 #include <editeng/editrids.hrc>
35 using ::sfx2::sidebar::Theme;
37 namespace svx { namespace sidebar {
39 namespace {
40 short GetItemId_Imp( ValueSet& rValueSet, const Color& rCol )
42 if(rCol == COL_AUTO)
43 return 0;
45 bool bFound = false;
46 sal_uInt16 nCount = rValueSet.GetItemCount();
47 sal_uInt16 n = 1;
49 while ( !bFound && n <= nCount )
51 Color aValCol = rValueSet.GetItemColor(n);
53 bFound = ( aValCol.GetRed() == rCol.GetRed()
54 && aValCol.GetGreen() == rCol.GetGreen()
55 && aValCol.GetBlue() == rCol.GetBlue() );
57 if ( !bFound )
58 n++;
60 return bFound ? n : -1;
62 XColorListRef GetColorTable (void)
64 SfxObjectShell* pDocSh = SfxObjectShell::Current();
65 DBG_ASSERT(pDocSh!=NULL, "DocShell not found!");
66 if (pDocSh != NULL)
68 const SfxPoolItem* pItem = pDocSh->GetItem(SID_COLOR_TABLE);
69 if (pItem != NULL)
71 XColorListRef xTable = ((SvxColorListItem*)pItem)->GetColorList();
72 if (xTable.is())
73 return xTable;
77 return XColorList::GetStdColorList();
79 } // end of anonymous namespace
84 ColorControl::ColorControl (
85 Window* pParent,
86 SfxBindings* /* pBindings */,
87 const ResId& rControlResId,
88 const ResId& rValueSetResId,
89 const ::boost::function<Color(void)>& rNoColorGetter,
90 const ::boost::function<void(OUString&,Color)>& rColorSetter,
91 FloatingWindow* pFloatingWindow,
92 const ResId* pNoColorStringResId) // const sal_uInt32 nNoColorStringResId)
93 : PopupControl(pParent, rControlResId),
94 maVSColor(this, rValueSetResId),
95 mpFloatingWindow(pFloatingWindow),
96 msNoColorString(
97 pNoColorStringResId
98 ? pNoColorStringResId->toString()
99 : OUString()),
100 maNoColorGetter(rNoColorGetter),
101 maColorSetter(rColorSetter)
103 FreeResource();
104 FillColors();
109 ColorControl::~ColorControl (void)
116 void ColorControl::FillColors (void)
118 XColorListRef xColorTable (GetColorTable());
120 if (xColorTable.is())
122 const long nColorCount(xColorTable->Count());
123 if (nColorCount <= 0)
124 return;
126 const WinBits aWinBits(maVSColor.GetStyle() | WB_TABSTOP | WB_ITEMBORDER | WB_NAMEFIELD |
127 WB_NO_DIRECTSELECT | WB_MENUSTYLEVALUESET);
129 maVSColor.SetStyle(aWinBits);
131 // neds to be done *before* layouting
132 if(!msNoColorString.isEmpty())
134 maVSColor.SetStyle(maVSColor.GetStyle() | WB_NONEFIELD);
135 maVSColor.SetText(msNoColorString);
138 const Size aNewSize(maVSColor.layoutAllVisible(nColorCount));
139 maVSColor.SetOutputSizePixel(aNewSize);
140 const sal_Int32 nAdd = 4;
142 SetOutputSizePixel(Size(aNewSize.Width() + nAdd, aNewSize.Height() + nAdd));
143 Link aLink = LINK(this, ColorControl, VSSelectHdl);
144 maVSColor.SetSelectHdl(aLink);
146 // Now, after all calls to SetStyle, we can change the
147 // background color.
148 maVSColor.SetBackground(Theme::GetWallpaper(Theme::Paint_DropDownBackground));
150 // add entrties
151 maVSColor.Clear();
152 maVSColor.addEntriesForXColorList(*xColorTable);
155 maVSColor.Show();
161 void ColorControl::GetFocus (void)
163 maVSColor.GrabFocus();
169 void ColorControl::SetCurColorSelect (Color aCol, bool bAvailable)
171 // FillColors();
172 short nCol = GetItemId_Imp( maVSColor, aCol );
173 if(! bAvailable)
175 maVSColor.SetNoSelection();
176 return;
179 //if not found
180 if( nCol == -1)
182 maVSColor.SetNoSelection();
184 else
186 // remove selection first to force evtl. scroll when scroll is needed
187 maVSColor.SetNoSelection();
188 maVSColor.SelectItem(nCol);
195 IMPL_LINK(ColorControl, VSSelectHdl, void *, pControl)
197 if(pControl == &maVSColor)
199 sal_uInt16 iPos = maVSColor.GetSelectItemId();
200 Color aColor = maVSColor.GetItemColor( iPos );
201 OUString aTmpStr = maVSColor.GetItemText( iPos );
203 // react when the WB_NONEFIELD created entry is selected
204 if (aColor.GetColor() == 0 && aTmpStr.isEmpty())
206 if (maNoColorGetter)
207 aColor = maNoColorGetter();
209 if (maColorSetter)
210 maColorSetter(aTmpStr, aColor);
212 if (mpFloatingWindow!=NULL && mpFloatingWindow->IsInPopupMode())
213 mpFloatingWindow->EndPopupMode();
216 return 0;
220 } } // end of namespace svx::sidebar
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */