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 "viewcontainercreator.h"
6
7 #include "../../lib/ccolor.h"
8 #include "../../lib/cviewcontainer.h"
9 #include "../detail/uiviewcreatorattributes.h"
10 #include "../uiattributes.h"
11 #include "../uiviewcreator.h"
12 #include "../uiviewfactory.h"
13 #include <array>
14
15 //------------------------------------------------------------------------
16 namespace VSTGUI {
17 namespace UIViewCreator {
18
19 //------------------------------------------------------------------------
backgroundColorDrawStyleStrings()20 auto ViewContainerCreator::backgroundColorDrawStyleStrings () -> BackgroundColorDrawStyleStrings&
21 {
22 static BackgroundColorDrawStyleStrings strings = {"stroked", "filled", "filled and stroked"};
23 return strings;
24 }
25
26 //------------------------------------------------------------------------
ViewContainerCreator()27 ViewContainerCreator::ViewContainerCreator ()
28 {
29 UIViewFactory::registerViewCreator (*this);
30 }
31
32 //------------------------------------------------------------------------
getViewName() const33 IdStringPtr ViewContainerCreator::getViewName () const
34 {
35 return kCViewContainer;
36 }
37
38 //------------------------------------------------------------------------
getBaseViewName() const39 IdStringPtr ViewContainerCreator::getBaseViewName () const
40 {
41 return kCView;
42 }
43
44 //------------------------------------------------------------------------
getDisplayName() const45 UTF8StringPtr ViewContainerCreator::getDisplayName () const
46 {
47 return "View Container";
48 }
49
50 //------------------------------------------------------------------------
create(const UIAttributes & attributes,const IUIDescription * description) const51 CView* ViewContainerCreator::create (const UIAttributes& attributes,
52 const IUIDescription* description) const
53 {
54 return new CViewContainer (CRect (0, 0, 100, 100));
55 }
56
57 //------------------------------------------------------------------------
apply(CView * view,const UIAttributes & attributes,const IUIDescription * description) const58 bool ViewContainerCreator::apply (CView* view, const UIAttributes& attributes,
59 const IUIDescription* description) const
60 {
61 CViewContainer* viewContainer = view->asViewContainer ();
62 if (viewContainer == nullptr)
63 return false;
64 CColor backColor;
65 if (stringToColor (attributes.getAttributeValue (kAttrBackgroundColor), backColor, description))
66 viewContainer->setBackgroundColor (backColor);
67 const auto* attr = attributes.getAttributeValue (kAttrBackgroundColorDrawStyle);
68 if (attr)
69 {
70 for (auto index = 0u; index <= kDrawFilledAndStroked; ++index)
71 {
72 if (*attr == backgroundColorDrawStyleStrings ()[index])
73 {
74 viewContainer->setBackgroundColorDrawStyle (static_cast<CDrawStyle> (index));
75 break;
76 }
77 }
78 }
79 return true;
80 }
81
82 //------------------------------------------------------------------------
getAttributeNames(StringList & attributeNames) const83 bool ViewContainerCreator::getAttributeNames (StringList& attributeNames) const
84 {
85 attributeNames.emplace_back (kAttrBackgroundColor);
86 attributeNames.emplace_back (kAttrBackgroundColorDrawStyle);
87 return true;
88 }
89
90 //------------------------------------------------------------------------
getAttributeType(const string & attributeName) const91 auto ViewContainerCreator::getAttributeType (const string& attributeName) const -> AttrType
92 {
93 if (attributeName == kAttrBackgroundColor)
94 return kColorType;
95 if (attributeName == kAttrBackgroundColorDrawStyle)
96 return kListType;
97 return kUnknownType;
98 }
99
100 //------------------------------------------------------------------------
getAttributeValue(CView * view,const string & attributeName,string & stringValue,const IUIDescription * desc) const101 bool ViewContainerCreator::getAttributeValue (CView* view, const string& attributeName,
102 string& stringValue, const IUIDescription* desc) const
103 {
104 CViewContainer* vc = view->asViewContainer ();
105 if (vc == nullptr)
106 return false;
107 if (attributeName == kAttrBackgroundColor)
108 {
109 colorToString (vc->getBackgroundColor (), stringValue, desc);
110 return true;
111 }
112 if (attributeName == kAttrBackgroundColorDrawStyle)
113 {
114 stringValue = backgroundColorDrawStyleStrings ()[vc->getBackgroundColorDrawStyle ()];
115 return true;
116 }
117 return false;
118 }
119
120 //------------------------------------------------------------------------
getPossibleListValues(const string & attributeName,ConstStringPtrList & values) const121 bool ViewContainerCreator::getPossibleListValues (const string& attributeName,
122 ConstStringPtrList& values) const
123 {
124 if (attributeName == kAttrBackgroundColorDrawStyle)
125 {
126 for (auto& str : backgroundColorDrawStyleStrings ())
127 values.emplace_back (&str);
128 return true;
129 }
130 return false;
131 }
132
133 //------------------------------------------------------------------------
134 } // UIViewCreator
135 } // VSTGUI
136