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 
23 
24 #include "precompiled_svx.hxx"
25 
26 #include <svx/sidebar/ColorControl.hxx>
27 #include "svx/svxids.hrc"
28 #include "svx/drawitem.hxx"
29 #include "svx/xtable.hxx"
30 #include "svx/dialmgr.hxx"
31 #include "svx/xflclit.hxx"
32 #include <tools/resid.hxx>
33 #include <sfx2/sidebar/Theme.hxx>
34 #include <sfx2/objsh.hxx>
35 #include <sfx2/bindings.hxx>
36 #include <sfx2/dispatch.hxx>
37 #include <vcl/floatwin.hxx>
38 #include <unotools/pathoptions.hxx>
39 #include <editeng/editrids.hrc>
40 
41 using ::sfx2::sidebar::Theme;
42 
43 namespace svx { namespace sidebar {
44 
45 namespace {
GetItemId_Imp(ValueSet & rValueSet,const Color & rCol)46 	short GetItemId_Imp( ValueSet& rValueSet, const Color& rCol )
47 	{
48 		if(rCol == COL_AUTO)
49 			return 0;
50 
51 		bool	bFound = false;
52 		sal_uInt16	nCount = rValueSet.GetItemCount();
53 		sal_uInt16	n	   = 1;
54 
55 		while ( !bFound && n <= nCount )
56 		{
57 			Color aValCol = rValueSet.GetItemColor(n);
58 
59 			bFound = ( aValCol.GetRed() == rCol.GetRed()
60 				&& aValCol.GetGreen() == rCol.GetGreen()
61 				&& aValCol.GetBlue()  == rCol.GetBlue() );
62 
63 			if ( !bFound )
64 				n++;
65 		}
66 		return bFound ? n : -1;
67 	}
68 
GetColorTable(void)69 	const XColorListSharedPtr GetColorTable (void)
70 	{
71 		SfxObjectShell* pDocSh = SfxObjectShell::Current();
72 		DBG_ASSERT(pDocSh!=NULL, "DocShell not found!");
73 		if (pDocSh != NULL)
74 		{
75 			const SfxPoolItem* pItem = pDocSh->GetItem(SID_COLOR_TABLE);
76 			if (pItem != NULL)
77 			{
78 				return static_cast< const SvxColorTableItem* >(pItem)->GetColorTable();
79 			}
80 		}
81 
82 		return XPropertyListFactory::CreateSharedXColorList(SvtPathOptions().GetPalettePath());
83 	}
84 } // end of anonymous namespace
85 
86 
87 
88 
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)89 ColorControl::ColorControl (
90 	Window* pParent,
91 	SfxBindings* pBindings,
92 	const ResId& rControlResId,
93 	const ResId& rValueSetResId,
94 	const ::boost::function<Color(void)>& rNoColorGetter,
95 	const ::boost::function<void(String&,Color)>& rColorSetter,
96 	FloatingWindow* pFloatingWindow,
97 	const ResId* pNoColorStringResId) // const sal_uInt32 nNoColorStringResId)
98 	: PopupControl(pParent, rControlResId),
99 	  mpBindings(pBindings),
100 	  maVSColor(this, rValueSetResId),
101 	  mpFloatingWindow(pFloatingWindow),
102 	  msNoColorString(
103 		  pNoColorStringResId
104 			  ? String(*pNoColorStringResId)
105 			  : String()),
106 	  maNoColorGetter(rNoColorGetter),
107 	  maColorSetter(rColorSetter)
108 {
109 	FreeResource();
110 	FillColors();
111 }
112 
113 
114 
~ColorControl(void)115 ColorControl::~ColorControl (void)
116 {
117 }
118 
119 
120 
121 
FillColors(void)122 void ColorControl::FillColors (void)
123 {
124 	const XColorListSharedPtr aColorTable(GetColorTable());
125 
126 	const long nColorCount(aColorTable->Count());
127 	if (nColorCount <= 0)
128 		return;
129 
130 	const WinBits aWinBits(maVSColor.GetStyle() | WB_TABSTOP | WB_ITEMBORDER | WB_NAMEFIELD |
131 		WB_NO_DIRECTSELECT | WB_MENUSTYLEVALUESET | WB_NO_DIRECTSELECT);
132 
133 	maVSColor.SetStyle(aWinBits);
134 
135 	// needs to be done *before* layouting
136 	if(msNoColorString.Len() > 0)
137 	{
138 		maVSColor.SetStyle(maVSColor.GetStyle() | WB_NONEFIELD);
139 		maVSColor.SetText(msNoColorString);
140 	}
141 
142 	const Size aNewSize(maVSColor.layoutAllVisible(nColorCount));
143 	maVSColor.SetOutputSizePixel(aNewSize);
144 	static sal_Int32 nAdd = 4;
145 
146 	SetOutputSizePixel(Size(aNewSize.Width() + nAdd, aNewSize.Height() + nAdd));
147 	Link aLink = LINK(this, ColorControl, VSSelectHdl);
148 	maVSColor.SetSelectHdl(aLink);
149 
150 	// Now, after all calls to SetStyle, we can change the
151 	// background color.
152 	maVSColor.SetBackground(Theme::GetWallpaper(Theme::Paint_DropDownBackground));
153 
154 	// add entries
155 	maVSColor.Clear();
156 	maVSColor.addEntriesForXColorList(aColorTable);
157 
158 	maVSColor.Show();
159 }
160 
161 
162 
163 
GetFocus(void)164 void ColorControl::GetFocus (void)
165 {
166 	maVSColor.GrabFocus();
167 }
168 
169 
170 
171 
SetCurColorSelect(Color aCol,bool bAvailable)172 void ColorControl::SetCurColorSelect(Color aCol,bool bAvailable)
173 {
174 	// UUUU When transparent use transparent entry (entry 0)
175 	const bool bIsTransparent(0xff == aCol.GetTransparency());
176 	short nCol = bIsTransparent ? 0 : GetItemId_Imp(maVSColor,aCol);
177 
178 	if(!bAvailable)
179 	{
180 		maVSColor.SetNoSelection();
181 		return;
182 	}
183 
184 	// if not found
185 	if(nCol == -1)
186 	{
187 		maVSColor.SetNoSelection();
188 	}
189 	else
190 	{
191 		// remove selection first to force evtl. scroll when scroll is needed
192 		maVSColor.SetNoSelection();
193 		maVSColor.SelectItem(nCol);
194 	}
195 }
196 
197 
198 
199 
IMPL_LINK(ColorControl,VSSelectHdl,void *,pControl)200 IMPL_LINK(ColorControl, VSSelectHdl, void *, pControl)
201 {
202 	if(pControl == &maVSColor)
203 	{
204 		sal_uInt16 iPos = maVSColor.GetSelectItemId();
205 		Color aColor = maVSColor.GetItemColor( iPos );
206 		String aTmpStr = maVSColor.GetItemText( iPos );
207 
208 		// react when the WB_NONEFIELD created entry is selected
209 		if (aColor.GetColor() == 0 && aTmpStr.Equals(String::CreateFromAscii("")))
210 		{
211 			if (maNoColorGetter)
212 				aColor = maNoColorGetter();
213 		}
214 		if (maColorSetter)
215 			maColorSetter(aTmpStr, aColor);
216 
217 		if (mpFloatingWindow!=NULL && mpFloatingWindow->IsInPopupMode())
218 			mpFloatingWindow->EndPopupMode();
219 	}
220 
221 	return 0;
222 }
223 
224 
225 } } // end of namespace svx::sidebar
226