ASPiK SDK
helpers.h
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 #pragma once
6 
7 #include "../../../../uidescription/uiviewfactory.h"
8 #include "../../../../uidescription/uiviewcreator.h"
9 #include "../../../../uidescription/uiattributes.h"
10 #include "../../../../lib/cbitmap.h"
11 #include "../../../../lib/cgradient.h"
12 #include "../../../../lib/cstring.h"
13 #include "../uidescriptionadapter.h"
14 
15 namespace VSTGUI {
16 
17 constexpr IdStringPtr kColorName = "MyColor";
18 constexpr IdStringPtr kFontName = "MyFont";
19 constexpr IdStringPtr kBitmapName = "MyBitmap";
20 constexpr IdStringPtr kGradientName = "MyGradient";
21 constexpr IdStringPtr kTagName = "tagname";
22 
24 {
25 public:
26  bool getColor (UTF8StringPtr name, CColor& color) const override
27  {
28  if (UTF8StringView(name) == kColorName)
29  {
30  color = this->color;
31  return true;
32  }
33  return false;
34  }
35 
36  UTF8StringPtr lookupColorName (const CColor& color) const override
37  {
38  if (this->color == color)
39  return kColorName;
40  return nullptr;
41  }
42 
43  CFontRef getFont (UTF8StringPtr name) const override
44  {
45  if (UTF8StringView(name) == kFontName)
46  return font;
47  return nullptr;
48  }
49 
50  UTF8StringPtr lookupFontName (const CFontRef font) const override
51  {
52  if (font == this->font)
53  return kFontName;
54  return nullptr;
55  }
56 
57  CBitmap* getBitmap (UTF8StringPtr name) const override
58  {
59  if (UTF8StringView (name) == kBitmapName)
60  return bitmap;
61  return nullptr;
62  }
63 
64  UTF8StringPtr lookupBitmapName (const CBitmap* inBitmap) const override
65  {
66  if (inBitmap == bitmap)
67  return kBitmapName;
68  return nullptr;
69  }
70 
71  CGradient* getGradient (UTF8StringPtr name) const override
72  {
73  if (UTF8StringView(name) == kGradientName)
74  return gradient;
75  return nullptr;
76  }
77 
78  UTF8StringPtr lookupGradientName (const CGradient* gradient) const override
79  {
80  if (gradient == this->gradient)
81  return kGradientName;
82  return nullptr;
83  }
84 
85  int32_t getTagForName (UTF8StringPtr name) const override
86  {
87  if (UTF8StringView (name) == kTagName)
88  return tag;
89  return -1;
90  }
91  UTF8StringPtr lookupControlTagName (const int32_t tag) const override
92  {
93  if (this->tag != -1 && tag == this->tag)
94  return kTagName;
95  return nullptr;
96  }
97  IControlListener* getControlListener (UTF8StringPtr name) const override
98  {
99  if (UTF8StringView (name) == kTagName)
100  return listener;
101  return nullptr;
102  }
103 
104  int32_t tag {-1};
105  CColor color {20, 30, 50, 255};
106  SharedPointer<CFontDesc> font = owned (new CFontDesc ("Arial", 12));
107  SharedPointer<CBitmap> bitmap = owned (new CBitmap (1, 1));
108  SharedPointer<CGradient> gradient = owned (CGradient::create(0, 1, kBlackCColor, kWhiteCColor));
109  IControlListener* listener {nullptr};
110 };
111 
112 inline void testPossibleValues (const IdStringPtr className, const std::string& attrName, IUIDescription* desc, UIViewFactory::StringList expectedValues)
113 {
114  UIViewFactory factory;
115  UIAttributes a;
116  a.setAttribute (UIViewCreator::kAttrClass, className);
117  auto view = owned (factory.createView (a, desc));
118  UIViewFactory::StringPtrList values;
119  EXPECT(factory.getPossibleAttributeListValues (view, attrName, values));
120  for (auto& v : expectedValues)
121  {
122  EXPECT(std::find_if (values.begin(), values.end(), [&] (const UIViewFactory::StringPtrList::value_type& value) {
123  return *value == v;
124  }) != values.end ());
125  }
126  EXPECT(values.size () == expectedValues.size ());
127 }
128 
129 inline void testMinMaxValues (const IdStringPtr className, const std::string& attrName, IUIDescription* desc, double minValue, double maxValue)
130 {
131  UIViewFactory factory;
132  UIAttributes a;
133  a.setAttribute (UIViewCreator::kAttrClass, className);
134  auto view = owned (factory.createView (a, desc));
135  double min, max;
136  EXPECT (factory.getAttributeValueRange(view, attrName, min, max));
137  EXPECT(min == minValue);
138  EXPECT(max == maxValue);
139 }
140 
141 template<typename ViewClass, typename Proc>
142 void testAttribute (const IdStringPtr viewName, const std::string& attrName, const IdStringPtr attrValue, IUIDescription* desc, const Proc& proc, bool disableRememberAttributes = false)
143 {
144  UIViewFactory factory;
145  factory.disableRememberAttributes = disableRememberAttributes;
146  UIAttributes a;
147  a.setAttribute (UIViewCreator::kAttrClass, viewName);
148  a.setAttribute (attrName, attrValue);
149 
150  auto v = owned (factory.createView (a, desc));
151  auto view = v.cast<ViewClass> ();
152  EXPECT(view);
153  EXPECT(proc (view));
154 
155  UIAttributes a2;
156  factory.getAttributesForView (view, desc, a2);
157  auto str = a2.getAttributeValue (attrName);
158  EXPECT(str);
159  EXPECT(*str == attrValue);
160 }
161 
162 template<typename ViewClass, typename Proc>
163 void testAttribute (const IdStringPtr viewName, const std::string& attrName, int32_t attrValue, IUIDescription* desc, const Proc& proc)
164 {
165  UIViewFactory factory;
166  UIAttributes a;
167  a.setAttribute (UIViewCreator::kAttrClass, viewName);
168  a.setIntegerAttribute (attrName, attrValue);
169 
170  auto v = owned (factory.createView (a, desc));
171  auto view = v.cast<ViewClass> ();
172  EXPECT(view);
173  EXPECT(proc (view));
174 
175  UIAttributes a2;
176  factory.getAttributesForView (view, desc, a2);
177  int32_t value;
178  a2.getIntegerAttribute (attrName, value);
179  EXPECT(value == attrValue);
180 }
181 
182 template<typename ViewClass, typename Proc>
183 void testAttribute (const IdStringPtr viewName, const std::string& attrName, bool attrValue, IUIDescription* desc, const Proc& proc)
184 {
185  UIViewFactory factory;
186  UIAttributes a;
187  a.setAttribute (UIViewCreator::kAttrClass, viewName);
188  a.setBooleanAttribute (attrName, attrValue);
189 
190  auto v = owned (factory.createView (a, desc));
191  auto view = v.cast<ViewClass> ();
192  EXPECT(view);
193  EXPECT(proc (view));
194 
195  UIAttributes a2;
196  factory.getAttributesForView (view, desc, a2);
197  bool value;
198  a2.getBooleanAttribute (attrName, value);
199  EXPECT(value == attrValue);
200 }
201 
202 template<typename ViewClass, typename Proc>
203 void testAttribute (const IdStringPtr viewName, const std::string& attrName, double attrValue, IUIDescription* desc, const Proc& proc)
204 {
205  UIViewFactory factory;
206  UIAttributes a;
207  a.setAttribute (UIViewCreator::kAttrClass, viewName);
208  a.setDoubleAttribute (attrName, attrValue);
209 
210  auto v = owned (factory.createView (a, desc));
211  auto view = v.cast<ViewClass> ();
212  EXPECT(view);
213  EXPECT(proc (view));
214 
215  UIAttributes a2;
216  factory.getAttributesForView (view, desc, a2);
217  double value;
218  a2.getDoubleAttribute (attrName, value);
219  EXPECT(value == attrValue);
220 }
221 
222 template<typename ViewClass, typename Proc>
223 void testAttribute (const IdStringPtr viewName, const std::string& attrName, const CRect& attrValue, IUIDescription* desc, const Proc& proc)
224 {
225  UIViewFactory factory;
226  UIAttributes a;
227  a.setAttribute (UIViewCreator::kAttrClass, viewName);
228  a.setRectAttribute (attrName, attrValue);
229 
230  auto v = owned (factory.createView (a, desc));
231  auto view = v.cast<ViewClass> ();
232  EXPECT(view);
233  EXPECT(proc (view));
234 
235  UIAttributes a2;
236  factory.getAttributesForView (view, desc, a2);
237  CRect value;
238  a2.getRectAttribute (attrName, value);
239  EXPECT(value == attrValue);
240 }
241 
242 template<typename ViewClass, typename Proc>
243 void testAttribute (const IdStringPtr viewName, const std::string& attrName, const CPoint& attrValue, IUIDescription* desc, const Proc& proc)
244 {
245  UIViewFactory factory;
246  UIAttributes a;
247  a.setAttribute (UIViewCreator::kAttrClass, viewName);
248  a.setPointAttribute (attrName, attrValue);
249 
250  auto v = owned (factory.createView (a, desc));
251  auto view = v.cast<ViewClass> ();
252  EXPECT(view);
253  EXPECT(proc (view));
254 
255  UIAttributes a2;
256  factory.getAttributesForView (view, desc, a2);
257  CPoint value;
258  a2.getPointAttribute (attrName, value);
259  EXPECT(value == attrValue);
260 }
261 
262 inline bool operator!= (const CGradient& g1, const CGradient& g2)
263 {
264  auto cs1 = g1.getColorStops ();
265  auto cs2 = g2.getColorStops ();
266  return cs1 != cs2;
267 }
268 
269 inline bool operator== (const CGradient& g1, const CGradient& g2)
270 {
271  auto cs1 = g1.getColorStops ();
272  auto cs2 = g2.getColorStops ();
273  return cs1 == cs2;
274 }
275 
276 
277 } // VSTGUI
font class
Definition: cfont.h:31
Definition: iuidescription.h:19
Default view factory.
Definition: uiviewfactory.h:19
Definition: xmlparse.c:181
Definition: vstguibase.h:299
RGBA Color structure.
Definition: ccolor.h:15
Definition: helpers.h:23
Gradient Object [new in 4.0].
Definition: cgradient.h:19
Encapsulates various platform depended kinds of bitmaps.
Definition: cbitmap.h:21
Definition: customcontrols.cpp:8
Definition: uiattributes.h:21
Definition: uidescriptionadapter.h:11
a view on a null terminated UTF-8 String
Definition: cstring.h:172
Definition: icontrollistener.h:14