1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include "precompiled_svx.hxx"
23 
24 #include <svx/sidebar/ColorControl.hxx>
25 #include "svx/svxids.hrc"
26 #include "svx/drawitem.hxx"
27 #include "svx/xtable.hxx"
28 #include "svx/dialmgr.hxx"
29 #include "svx/xflclit.hxx"
30 #include <tools/resid.hxx>
31 #include <sfx2/sidebar/Theme.hxx>
32 #include <sfx2/objsh.hxx>
33 #include <sfx2/bindings.hxx>
34 #include <sfx2/dispatch.hxx>
35 #include <vcl/floatwin.hxx>
36 #include <unotools/pathoptions.hxx>
37 #include <editeng/editrids.hrc>
38 
39 using ::sfx2::sidebar::Theme;
40 
41 namespace svx { namespace sidebar {
42 
43 namespace {
GetItemId_Imp(ValueSet & rValueSet,const Color & rCol)44     short GetItemId_Imp( ValueSet& rValueSet, const Color& rCol )
45     {
46         if(rCol == COL_AUTO)
47             return 0;
48 
49         bool	bFound = false;
50         sal_uInt16	nCount = rValueSet.GetItemCount();
51         sal_uInt16	n	   = 1;
52 
53         while ( !bFound && n <= nCount )
54         {
55             Color aValCol = rValueSet.GetItemColor(n);
56 
57             bFound = (   aValCol.GetRed()   == rCol.GetRed()
58                 && aValCol.GetGreen() == rCol.GetGreen()
59                 && aValCol.GetBlue()  == rCol.GetBlue() );
60 
61             if ( !bFound )
62                 n++;
63         }
64         return bFound ? n : -1;
65     }
66 
GetColorTable(void)67     const XColorListSharedPtr GetColorTable (void)
68     {
69         SfxObjectShell* pDocSh = SfxObjectShell::Current();
70         DBG_ASSERT(pDocSh!=NULL, "DocShell not found!");
71         if (pDocSh != NULL)
72         {
73             const SfxPoolItem* pItem = pDocSh->GetItem(SID_COLOR_TABLE);
74             if (pItem != NULL)
75             {
76                 return static_cast< const SvxColorTableItem* >(pItem)->GetColorTable();
77             }
78         }
79 
80         return XPropertyListFactory::CreateSharedXColorList(SvtPathOptions().GetPalettePath());
81     }
82 } // end of anonymous namespace
83 
84 
85 
86 
ColorControl(Window * pParent,SfxBindings * pBindings,const ResId & rControlResId,const ResId & rValueSetResId,const::boost::function<Color (void)> & rNoColorGetter,const::boost::function<void (String &,Color)> & rColorSetter,FloatingWindow * pFloatingWindow,const ResId * pNoColorStringResId)87 ColorControl::ColorControl (
88     Window* pParent,
89     SfxBindings* pBindings,
90     const ResId& rControlResId,
91     const ResId& rValueSetResId,
92     const ::boost::function<Color(void)>& rNoColorGetter,
93     const ::boost::function<void(String&,Color)>& rColorSetter,
94     FloatingWindow* pFloatingWindow,
95     const ResId* pNoColorStringResId) // const sal_uInt32 nNoColorStringResId)
96     : PopupControl(pParent, rControlResId),
97       mpBindings(pBindings),
98       maVSColor(this, rValueSetResId),
99       mpFloatingWindow(pFloatingWindow),
100       msNoColorString(
101           pNoColorStringResId
102               ? String(*pNoColorStringResId)
103               : String()),
104       maNoColorGetter(rNoColorGetter),
105       maColorSetter(rColorSetter)
106 {
107 	FreeResource();
108 	FillColors();
109 }
110 
111 
112 
~ColorControl(void)113 ColorControl::~ColorControl (void)
114 {
115 }
116 
117 
118 
119 
FillColors(void)120 void ColorControl::FillColors (void)
121 {
122 	const XColorListSharedPtr aColorTable(GetColorTable());
123 
124     const long nColorCount(aColorTable->Count());
125     if (nColorCount <= 0)
126         return;
127 
128     const WinBits aWinBits(maVSColor.GetStyle() | WB_TABSTOP | WB_ITEMBORDER | WB_NAMEFIELD |
129         WB_NO_DIRECTSELECT | WB_MENUSTYLEVALUESET | WB_NO_DIRECTSELECT);
130 
131     maVSColor.SetStyle(aWinBits);
132 
133     // neds to be done *before* layouting
134     if(msNoColorString.Len() > 0)
135     {
136         maVSColor.SetStyle(maVSColor.GetStyle() | WB_NONEFIELD);
137         maVSColor.SetText(msNoColorString);
138     }
139 
140     const Size aNewSize(maVSColor.layoutAllVisible(nColorCount));
141     maVSColor.SetOutputSizePixel(aNewSize);
142     static sal_Int32 nAdd = 4;
143 
144     SetOutputSizePixel(Size(aNewSize.Width() + nAdd, aNewSize.Height() + nAdd));
145     Link aLink = LINK(this, ColorControl, VSSelectHdl);
146     maVSColor.SetSelectHdl(aLink);
147 
148     // Now, after all calls to SetStyle, we can change the
149     // background color.
150     maVSColor.SetBackground(Theme::GetWallpaper(Theme::Paint_DropDownBackground));
151 
152     // add entrties
153     maVSColor.Clear();
154     maVSColor.addEntriesForXColorList(aColorTable);
155 
156 	maVSColor.Show();
157 }
158 
159 
160 
161 
GetFocus(void)162 void ColorControl::GetFocus (void)
163 {
164 	maVSColor.GrabFocus();
165 }
166 
167 
168 
169 
SetCurColorSelect(Color aCol,bool bAvailable)170 void ColorControl::SetCurColorSelect (Color aCol, bool bAvailable)
171 {
172 //	FillColors();
173 	short nCol = GetItemId_Imp( maVSColor, aCol );
174 	if(! bAvailable)
175 	{
176 		maVSColor.SetNoSelection();
177 		return;
178 	}
179 
180 	//if not found
181 	if( nCol == -1)
182 	{
183 		maVSColor.SetNoSelection();
184 	}
185 	else
186 	{
187         // remove selection first to force evtl. scroll when scroll is needed
188 		maVSColor.SetNoSelection();
189 		maVSColor.SelectItem(nCol);
190 	}
191 }
192 
193 
194 
195 
IMPL_LINK(ColorControl,VSSelectHdl,void *,pControl)196 IMPL_LINK(ColorControl, VSSelectHdl, void *, pControl)
197 {
198     if(pControl == &maVSColor)
199     {
200         sal_uInt16 iPos = maVSColor.GetSelectItemId();
201         Color aColor = maVSColor.GetItemColor( iPos );
202         String aTmpStr = maVSColor.GetItemText( iPos );
203 
204         // react when the WB_NONEFIELD created entry is selected
205         if (aColor.GetColor() == 0 && aTmpStr.Equals(String::CreateFromAscii("")))
206         {
207             if (maNoColorGetter)
208                 aColor = maNoColorGetter();
209         }
210         if (maColorSetter)
211             maColorSetter(aTmpStr, aColor);
212 
213         if (mpFloatingWindow!=NULL && mpFloatingWindow->IsInPopupMode())
214             mpFloatingWindow->EndPopupMode();
215     }
216 
217     return 0;
218 }
219 
220 
221 } } // end of namespace svx::sidebar
222