1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4 
5 #include "cstringlist.h"
6 #include "../cdrawcontext.h"
7 #include "../ccolor.h"
8 #include "../cfont.h"
9 
10 //------------------------------------------------------------------------
11 namespace VSTGUI {
12 
13 //------------------------------------------------------------------------
14 struct StringListControlDrawer::Impl
15 {
16 	Func func;
17 
18 	SharedPointer<CFontDesc> font {kNormalFont};
19 	CColor fontColor {kBlackCColor};
20 	CColor fontColorSelected {kWhiteCColor};
21 	CColor backColor {kWhiteCColor};
22 	CColor backColorSelected {kBlueCColor};
23 	CColor hoverColor {MakeCColor (0, 0, 0, 100)};
24 	CColor lineColor {kBlackCColor};
25 	CCoord lineWidth {1.};
26 	CCoord textInset {5.};
27 	CHoriTxtAlign textAlign {kLeftText};
28 
29 };
30 
31 //------------------------------------------------------------------------
StringListControlDrawer()32 StringListControlDrawer::StringListControlDrawer ()
33 {
34 	impl = std::unique_ptr<Impl> (new Impl);
35 }
36 
37 //------------------------------------------------------------------------
38 StringListControlDrawer::~StringListControlDrawer () noexcept = default;
39 
40 //------------------------------------------------------------------------
setStringProvider(Func && getStringFunc)41 void StringListControlDrawer::setStringProvider (Func&& getStringFunc)
42 {
43 	impl->func = std::move (getStringFunc);
44 }
45 
46 //------------------------------------------------------------------------
setStringProvider(const Func & getStringFunc)47 void StringListControlDrawer::setStringProvider (const Func& getStringFunc)
48 {
49 	impl->func = getStringFunc;
50 }
51 
52 //------------------------------------------------------------------------
setFont(CFontRef f)53 void StringListControlDrawer::setFont (CFontRef f)
54 {
55 	impl->font = f;
56 }
57 
58 //------------------------------------------------------------------------
setFontColor(CColor color)59 void StringListControlDrawer::setFontColor (CColor color)
60 {
61 	impl->fontColor = color;
62 }
63 
64 //------------------------------------------------------------------------
setSelectedFontColor(CColor color)65 void StringListControlDrawer::setSelectedFontColor (CColor color)
66 {
67 	impl->fontColorSelected = color;
68 }
69 
70 //------------------------------------------------------------------------
setBackColor(CColor color)71 void StringListControlDrawer::setBackColor (CColor color)
72 {
73 	impl->backColor = color;
74 }
75 
76 //------------------------------------------------------------------------
setSelectedBackColor(CColor color)77 void StringListControlDrawer::setSelectedBackColor (CColor color)
78 {
79 	impl->backColorSelected = color;
80 }
81 
82 //------------------------------------------------------------------------
setHoverColor(CColor color)83 void StringListControlDrawer::setHoverColor (CColor color)
84 {
85 	impl->hoverColor = color;
86 }
87 
88 //------------------------------------------------------------------------
setLineColor(CColor color)89 void StringListControlDrawer::setLineColor (CColor color)
90 {
91 	impl->lineColor = color;
92 }
93 
94 //------------------------------------------------------------------------
setLineWidth(CCoord width)95 void StringListControlDrawer::setLineWidth (CCoord width)
96 {
97 	impl->lineWidth = width;
98 }
99 
100 //------------------------------------------------------------------------
setTextInset(CCoord inset)101 void StringListControlDrawer::setTextInset (CCoord inset)
102 {
103 	impl->textInset = inset;
104 }
105 
106 //------------------------------------------------------------------------
setTextAlign(CHoriTxtAlign align)107 void StringListControlDrawer::setTextAlign (CHoriTxtAlign align)
108 {
109 	impl->textAlign = align;
110 }
111 
112 //------------------------------------------------------------------------
getFont() const113 CFontRef StringListControlDrawer::getFont () const
114 {
115 	return impl->font;
116 }
117 
118 //------------------------------------------------------------------------
getFontColor() const119 CColor StringListControlDrawer::getFontColor () const
120 {
121 	return impl->fontColor;
122 }
123 
124 //------------------------------------------------------------------------
getSelectedFontColor() const125 CColor StringListControlDrawer::getSelectedFontColor () const
126 {
127 	return impl->fontColorSelected;
128 }
129 
130 //------------------------------------------------------------------------
getBackColor() const131 CColor StringListControlDrawer::getBackColor () const
132 {
133 	return impl->backColor;
134 }
135 
136 //------------------------------------------------------------------------
getSelectedBackColor() const137 CColor StringListControlDrawer::getSelectedBackColor () const
138 {
139 	return impl->backColorSelected;
140 }
141 
142 //------------------------------------------------------------------------
getHoverColor() const143 CColor StringListControlDrawer::getHoverColor () const
144 {
145 	return impl->hoverColor;
146 }
147 
148 //------------------------------------------------------------------------
getLineColor() const149 CColor StringListControlDrawer::getLineColor () const
150 {
151 	return impl->lineColor;
152 }
153 
154 //------------------------------------------------------------------------
getLineWidth() const155 CCoord StringListControlDrawer::getLineWidth () const
156 {
157 	return impl->lineWidth;
158 }
159 
160 //------------------------------------------------------------------------
getTextInset() const161 CCoord StringListControlDrawer::getTextInset () const
162 {
163 	return impl->textInset;
164 }
165 
166 //------------------------------------------------------------------------
getTextAlign() const167 CHoriTxtAlign StringListControlDrawer::getTextAlign () const
168 {
169 	return impl->textAlign;
170 }
171 
172 //------------------------------------------------------------------------
drawBackground(CDrawContext * context,CRect size)173 void StringListControlDrawer::drawBackground (CDrawContext* context, CRect size)
174 {
175 	context->setFillColor (impl->backColor);
176 	context->drawRect (size, kDrawFilled);
177 }
178 
179 //------------------------------------------------------------------------
drawRow(CDrawContext * context,CRect size,Row row)180 void StringListControlDrawer::drawRow (CDrawContext* context, CRect size, Row row)
181 {
182 	context->setDrawMode (kAntiAliasing);
183 	if (row.isHovered ())
184 	{
185 		context->setFillColor (impl->hoverColor);
186 		context->drawRect (size, kDrawFilled);
187 	}
188 	if (row.isSelected ())
189 	{
190 		context->setFillColor (impl->backColorSelected);
191 		context->drawRect (size, kDrawFilled);
192 	}
193 
194 	auto lw = impl->lineWidth < 0. ? context->getHairlineSize () : impl->lineWidth;
195 	size.bottom -= lw * 0.5;
196 
197 	if (!(row.isLastRow ()) && lw != 0.)
198 	{
199 		context->setDrawMode (kAntiAliasing | kNonIntegralMode);
200 		context->setFrameColor (impl->lineColor);
201 		context->setLineWidth (lw);
202 		context->drawLine (size.getBottomLeft (), size.getBottomRight ());
203 	}
204 	if (auto string = getString (row))
205 	{
206 		size.inset (impl->textInset, 0);
207 		context->setFontColor (row.isSelected () ? impl->fontColorSelected : impl->fontColor);
208 		context->setFont (impl->font);
209 		context->drawString (string, size, impl->textAlign);
210 	}
211 }
212 
213 //------------------------------------------------------------------------
getString(int32_t row) const214 SharedPointer<IPlatformString> StringListControlDrawer::getString (int32_t row) const
215 {
216 	return impl->func ? impl->func (row) : IPlatformString::createWithUTF8String (toString (row));
217 }
218 
219 //------------------------------------------------------------------------
220 } // VSTGUI
221