1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
{
40 short GetItemId_Imp( ValueSet
& rValueSet
, const Color
& rCol
)
46 sal_uInt16 nCount
= rValueSet
.GetItemCount();
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() );
60 return bFound
? n
: -1;
62 XColorListRef
GetColorTable (void)
64 SfxObjectShell
* pDocSh
= SfxObjectShell::Current();
65 DBG_ASSERT(pDocSh
!=NULL
, "DocShell not found!");
68 const SfxPoolItem
* pItem
= pDocSh
->GetItem(SID_COLOR_TABLE
);
71 XColorListRef xTable
= ((SvxColorListItem
*)pItem
)->GetColorList();
77 return XColorList::GetStdColorList();
79 } // end of anonymous namespace
84 ColorControl::ColorControl (
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
),
98 ? pNoColorStringResId
->toString()
100 maNoColorGetter(rNoColorGetter
),
101 maColorSetter(rColorSetter
)
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)
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
148 maVSColor
.SetBackground(Theme::GetWallpaper(Theme::Paint_DropDownBackground
));
152 maVSColor
.addEntriesForXColorList(*xColorTable
);
161 void ColorControl::GetFocus (void)
163 maVSColor
.GrabFocus();
169 void ColorControl::SetCurColorSelect (Color aCol
, bool bAvailable
)
172 short nCol
= GetItemId_Imp( maVSColor
, aCol
);
175 maVSColor
.SetNoSelection();
182 maVSColor
.SetNoSelection();
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())
207 aColor
= maNoColorGetter();
210 maColorSetter(aTmpStr
, aColor
);
212 if (mpFloatingWindow
!=NULL
&& mpFloatingWindow
->IsInPopupMode())
213 mpFloatingWindow
->EndPopupMode();
220 } } // end of namespace svx::sidebar
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */