Jamba C++ API  4.1.0
CustomViewCreator.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2019 pongasoft
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  *
16  * @author Yan Pujante
17  */
18 #pragma once
19 
20 #include <vstgui4/vstgui/uidescription/iviewcreator.h>
21 #include <vstgui4/vstgui/uidescription/uiviewcreator.h>
22 #include <vstgui4/vstgui/uidescription/uiviewfactory.h>
23 #include <vstgui4/vstgui/uidescription/uiattributes.h>
24 #include <vstgui4/vstgui/uidescription/detail/uiviewcreatorattributes.h>
25 #include <vstgui4/vstgui/lib/crect.h>
26 #include <vstgui4/vstgui/lib/ccolor.h>
27 #include <vstgui4/vstgui/lib/cbitmap.h>
28 #include <map>
29 #include <memory>
30 #include <pongasoft/logging/logging.h>
34 #include <pongasoft/Utils/Cpp17.h>
35 #include <pongasoft/VST/Types.h>
36 
38 
39 using namespace VSTGUI;
40 
41 //------------------------------------------------------------------------
42 // Implementation Note: Half of the API (the "getter"/"read" part of it) is not used in Release mode because the
43 // editor code is built only during Debug mode (EDITOR_MODE is defined) and only the editor code is using it.
44 // As a result, it is conditionnally compiled so that it doesn't use unecessary space in Release mode.
45 //------------------------------------------------------------------------
46 
51 {
52 public:
53  // Constructor
54  explicit ViewAttribute(std::string iName) :
55  fName(std::move(iName))
56  {}
57 
61  virtual IViewCreator::AttrType getType() = 0;
62 
67  std::string getName() const
68  {
69  return fName;
70  }
71 
78  virtual bool apply(CView *iView, const UIAttributes &iAttributes, const IUIDescription *iDescription) = 0;
79 
80 #ifdef EDITOR_MODE
81 
87  virtual bool getAttributeValue(CView *iView, const IUIDescription *iDescription, std::string &oStringValue) const = 0;
88 
92  virtual bool getPossibleListValues(std::list<const std::string *> &iValues) const
93  {
94  return false;
95  }
96 #endif
97 
98 private:
99  std::string fName;
100 };
101 
106 template<typename TView>
107 inline TView *createCustomView(CRect const &iSize,
108  const UIAttributes &iAttributes,
109  const IUIDescription * /* iDescription */) { return new TView(iSize); }
110 
113 template<typename T>
114 using AttrValMap = std::map<std::string, T>;
115 
119 template<typename T>
120 using AttrValInitList = std::initializer_list<typename AttrValMap<T>::value_type>;
121 
154 template<typename TView>
155 class TCustomViewCreator : public ViewCreatorAdapter
156 {
157 private:
163  template<typename T, typename TGetter, typename TSetter>
164  class TAttribute : public ViewAttribute
165  {
166  public:
167  using Getter = TGetter;
168  using Setter = TSetter;
169 
170  // Constructor
171  TAttribute(std::string const &iName, Getter iGetter, Setter iSetter) :
172  ViewAttribute(iName),
173 #ifdef EDITOR_MODE
174  fGetter{iGetter},
175 #endif
176  fSetter{iSetter}
177  { }
178 
179  // getType
180  IViewCreator::AttrType getType() override
181  {
182  return IViewCreator::kUnknownType;
183  }
184 
189  virtual bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, T &oValue) const
190  {
191  return false;
192  }
193 
194  // apply => set a color to the view
195  bool apply(CView *iView, const UIAttributes &iAttributes, const IUIDescription *iDescription) override
196  {
197  auto *tv = dynamic_cast<TView *>(iView);
198  if(tv != nullptr)
199  {
200  auto attributeValue = iAttributes.getAttributeValue(getName());
201  if(attributeValue)
202  {
203  T value;
204  if(fromString(iDescription, *attributeValue, value))
205  {
206  (tv->*fSetter)(value);
207  return true;
208  }
209  }
210  }
211  return false;
212  }
213 
214 #ifdef EDITOR_MODE
215 
219  virtual bool toString(IUIDescription const *iDescription, T const &iValue, std::string &oStringValue) const
220  {
221  return false;
222  }
223 
224 
225  // getAttributeValue => get a color from the view
226  bool getAttributeValue(CView *iView, const IUIDescription *iDescription, std::string &oStringValue) const override
227  {
228  auto *tv = dynamic_cast<TView *>(iView);
229  if(tv != nullptr)
230  {
231  auto value = (tv->*fGetter)();
232  return toString(iDescription, value, oStringValue);
233  }
234  return false;
235  }
236 
237  private:
238  Getter fGetter;
239 #endif
240 
241  private:
243  };
244 
248  template<typename T>
249  using ByValAttribute = TAttribute<T, T (TView::*)() const, void (TView::*)(T)>;
250 
254  template<typename T>
255  using ByRefAttribute = TAttribute<T, T const &(TView::*)() const, void (TView::*)(T const &)>;
256 
262  {
263  public:
264  TagAttribute(std::string const &iName,
265  typename ByValAttribute<TagID>::Getter iGetter,
266  typename ByValAttribute<TagID>::Setter iSetter) :
267  ByValAttribute<TagID>(iName, iGetter, iSetter) {}
268 
269  // getType
270  IViewCreator::AttrType getType() override
271  {
272  return IViewCreator::kTagType;
273  }
274 
275  // fromString
276  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TagID &oValue) const override
277  {
278  if(iAttributeValue.length() != 0)
279  {
280  auto tag = static_cast<TagID>(iDescription->getTagForName(iAttributeValue.c_str()));
281  if(tag == UNDEFINED_TAG_ID)
282  {
283  char *endPtr = nullptr;
284  tag = static_cast<TagID>(strtol(iAttributeValue.c_str(), &endPtr, 10));
285  if(endPtr == iAttributeValue.c_str())
286  {
287  return false;
288  }
289  }
290  oValue = tag;
291  }
292  else
293  // when selecting "NONE" iAttributeValue is an empty string
294  oValue = UNDEFINED_PARAM_ID;
295 
296  return true;
297  }
298 
299 #ifdef EDITOR_MODE
300  // toString
301  bool toString(IUIDescription const *iDescription, const TagID &iValue, std::string &oStringValue) const override
302  {
303  if(iValue != UNDEFINED_TAG_ID)
304  {
305  UTF8StringPtr controlTag = iDescription->lookupControlTagName(iValue);
306  if(controlTag)
307  {
308  oStringValue = controlTag;
309  return true;
310  }
311  }
312 
313  return false;
314  }
315 #endif
316  };
317 
322  template<typename TInt>
323  class IntegerAttribute : public ByValAttribute<TInt>
324  {
325  public:
328 
329  // Constructor
330  IntegerAttribute(std::string const &iName, Getter iGetter, Setter iSetter) :
331  ByValAttribute<TInt>(iName, iGetter, iSetter) {}
332 
333  // getType
334  IViewCreator::AttrType getType() override
335  {
336  return IViewCreator::kIntegerType;
337  }
338 
339  // fromString
340  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TInt &oValue) const override
341  {
342  char *endPtr = nullptr;
343  auto value = static_cast<TInt>(strtol(iAttributeValue.c_str(), &endPtr, 10));
344  if(endPtr == iAttributeValue.c_str())
345  {
346  DLOG_F(WARNING, "could not convert <%s> to an integer", iAttributeValue.c_str());
347  return false;
348  }
349  oValue = value;
350  return true;
351  }
352 
353 #ifdef EDITOR_MODE
354  // toString
355  bool toString(IUIDescription const *iDescription, const TInt &iValue, std::string &oStringValue) const override
356  {
357  std::stringstream str;
358  str << iValue;
359  oStringValue = str.str();
360  return true;
361  }
362 #endif
363  };
364 
369  template<typename TFloat>
370  class FloatAttribute : public ByValAttribute<TFloat>
371  {
372  public:
375 
376  // Constructor
377  FloatAttribute(std::string const &iName, Getter iGetter, Setter iSetter) :
378  ByValAttribute<TFloat>(iName, iGetter, iSetter) {}
379 
380  // getType
381  IViewCreator::AttrType getType() override
382  {
383  return IViewCreator::kFloatType;
384  }
385 
386  // fromString
387  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TFloat &oValue) const override
388  {
389  TFloat value;
390  if(Utils::stringToFloat<TFloat>(iAttributeValue, value))
391  {
392  oValue = value;
393  return true;
394  }
395  DLOG_F(WARNING, "could not convert <%s> to a float", iAttributeValue.c_str());
396  return false;
397  }
398 
399 #ifdef EDITOR_MODE
400  // toString
401  bool toString(IUIDescription const *iDescription, const TFloat &iValue, std::string &oStringValue) const override
402  {
403  std::stringstream str;
404  str << iValue;
405  oStringValue = str.str();
406  return true;
407  }
408 #endif
409  };
410 
415  class ColorAttribute : public ByRefAttribute<CColor>
416  {
417  public:
418 
419  // Constructor
420  ColorAttribute(std::string const &iName,
421  typename ByRefAttribute<CColor>::Getter iGetter,
422  typename ByRefAttribute<CColor>::Setter iSetter) :
423  ByRefAttribute<CColor>(iName, iGetter, iSetter) {}
424 
425  // getType
426  IViewCreator::AttrType getType() override
427  {
428  return IViewCreator::kColorType;
429  }
430 
431  // fromString
432  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, CColor &oValue) const override
433  {
434  CColor color;
435  if(UIViewCreator::stringToColor(&iAttributeValue, color, iDescription))
436  {
437  oValue = color;
438  return true;
439  }
440  return false;
441  }
442 
443 #ifdef EDITOR_MODE
444  // toString
445  bool toString(IUIDescription const *iDescription, const CColor &iValue, std::string &oStringValue) const override
446  {
447  return UIViewCreator::colorToString(iValue, oStringValue, iDescription);
448  }
449 #endif
450  };
451 
456  class GradientAttribute : public ByValAttribute<GradientPtr>
457  {
458  public:
459 
460  // Constructor
461  GradientAttribute(std::string const &iName,
462  typename ByValAttribute<GradientPtr>::Getter iGetter,
463  typename ByValAttribute<GradientPtr>::Setter iSetter) :
464  ByValAttribute<GradientPtr>(iName, iGetter, iSetter) {}
465 
466  // getType
467  IViewCreator::AttrType getType() override
468  {
469  return IViewCreator::kGradientType;
470  }
471 
472  // fromString
473  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, GradientPtr &oValue) const override
474  {
475  auto gradient = iDescription->getGradient(iAttributeValue.c_str());
476  if(gradient)
477  {
478  oValue = gradient;
479  return true;
480  }
481  return false;
482  }
483 
484 #ifdef EDITOR_MODE
485  // toString
486  bool toString(IUIDescription const *iDescription, GradientPtr const &iValue, std::string &oStringValue) const override
487  {
488  if(iValue)
489  {
490  auto name = iDescription->lookupGradientName(iValue);
491  if(name)
492  {
493  oStringValue = name;
494  return true;
495  }
496 
497  }
498  return false;
499  }
500 #endif
501  };
502 
507  class BooleanAttribute : public ByValAttribute<bool>
508  {
509  public:
510  // Constructor
511  BooleanAttribute(std::string const &iName,
512  typename ByValAttribute<bool>::Getter iGetter,
513  typename ByValAttribute<bool>::Setter iSetter) :
514  ByValAttribute<bool>(iName, iGetter, iSetter) {}
515 
516  // getType
517  IViewCreator::AttrType getType() override
518  {
519  return IViewCreator::kBooleanType;
520  }
521 
522  // fromString
523  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, bool &oValue) const override
524  {
525  if(iAttributeValue == "true")
526  {
527  oValue = true;
528  return true;
529  }
530 
531  if(iAttributeValue == "false")
532  {
533  oValue = false;
534  return true;
535  }
536 
537  return false;
538  }
539 
540 #ifdef EDITOR_MODE
541  // toString
542  bool toString(IUIDescription const *iDescription, const bool &iValue, std::string &oStringValue) const override
543  {
544  oStringValue = iValue ? "true" : "false";
545  return true;
546  }
547 #endif
548  };
549 
554  class BitmapAttribute : public ByValAttribute<BitmapPtr>
555  {
556  public:
557  // Constructor
558  BitmapAttribute(std::string const &iName,
559  typename ByValAttribute<BitmapPtr>::Getter iGetter,
560  typename ByValAttribute<BitmapPtr>::Setter iSetter) :
561  ByValAttribute<BitmapPtr>(iName, iGetter, iSetter) {}
562 
563  // getType
564  IViewCreator::AttrType getType() override
565  {
566  return IViewCreator::kBitmapType;
567  }
568 
569  // fromString
570  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, BitmapPtr &oValue) const override
571  {
572  BitmapPtr bitmap;
573  if(UIViewCreator::stringToBitmap(&iAttributeValue, bitmap, iDescription))
574  {
575  oValue = bitmap;
576  return true;
577  }
578  return false;
579  }
580 
581 #ifdef EDITOR_MODE
582  // toString
583  bool toString(IUIDescription const *iDescription, BitmapPtr const &iValue, std::string &oStringValue) const override
584  {
585  if(iValue)
586  return UIViewCreator::bitmapToString(iValue, oStringValue, iDescription);
587  else
588  return false;
589  }
590 #endif
591  };
592 
597  class FontAttribute : public ByValAttribute<FontPtr>
598  {
599  public:
600  // Constructor
601  FontAttribute(std::string const &iName,
602  typename ByValAttribute<FontPtr>::Getter iGetter,
603  typename ByValAttribute<FontPtr>::Setter iSetter) :
604  ByValAttribute<FontPtr>(iName, iGetter, iSetter) {}
605 
606  // getType
607  IViewCreator::AttrType getType() override
608  {
609  return IViewCreator::kFontType;
610  }
611 
612  // fromString
613  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, FontPtr &oValue) const override
614  {
615  auto font = iDescription->getFont(iAttributeValue.c_str());
616  if(font)
617  {
618  oValue = font;
619  return true;
620  }
621  return false;
622  }
623 
624 #ifdef EDITOR_MODE
625  // toString
626  bool toString(IUIDescription const *iDescription, FontPtr const &iValue, std::string &oStringValue) const override
627  {
628  if(iValue)
629  {
630  auto fontName = iDescription->lookupFontName(iValue);
631  if(fontName)
632  {
633  oStringValue = fontName;
634  return true;
635  }
636  }
637  return false;
638  }
639 #endif
640  };
641 
646  class MarginAttribute : public ByRefAttribute<Margin>
647  {
648  public:
649  MarginAttribute(std::string const &iName,
650  typename ByRefAttribute<Margin>::Getter iGetter,
651  typename ByRefAttribute<Margin>::Setter iSetter) :
652  ByRefAttribute<Margin>(iName, iGetter, iSetter) {}
653 
654  // fromString
655  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, Margin &oValue) const override
656  {
657  auto parts = Utils::splitFloats<CCoord>(iAttributeValue, ',');
658 
659  if(parts.empty())
660  return false;
661 
662  // look for nan in the array
663  if(std::find_if(parts.cbegin(), parts.cend(), [] (auto f) {return std::isnan(f);}) != parts.cend())
664  return false;
665 
666  if(parts.size() == 1)
667  {
668  oValue = Margin{parts[0]};
669  }
670  else
671  {
672  if(parts.size() < 4)
673  return false;
674 
675  oValue = Margin{parts[0], parts[1], parts[2], parts[3]};
676  }
677 
678  return true;
679  }
680 
681 #ifdef EDITOR_MODE
682  // toString
683  bool toString(IUIDescription const *iDescription, const Margin &iValue, std::string &oStringValue) const override
684  {
685  std::stringstream str;
686  if(iValue.fTop == iValue.fRight && iValue.fTop == iValue.fBottom && iValue.fTop == iValue.fLeft)
687  str << iValue.fTop;
688  else
689  {
690  str << iValue.fTop;
691  str << ",";
692  str << iValue.fRight;
693  str << ",";
694  str << iValue.fBottom;
695  str << ",";
696  str << iValue.fLeft;
697  }
698  oStringValue = str.str();
699  return true;
700  }
701 #endif
702  };
703 
708  class RangeAttribute : public ByRefAttribute<Range>
709  {
710  public:
711  RangeAttribute(std::string const &iName,
712  typename ByRefAttribute<Range>::Getter iGetter,
713  typename ByRefAttribute<Range>::Setter iSetter) :
714  ByRefAttribute<Range>(iName, iGetter, iSetter) {}
715 
716  // fromString
717  bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, Range &oValue) const override
718  {
719  auto parts = Utils::splitFloats<CCoord>(iAttributeValue, ',');
720 
721  if(parts.empty())
722  return false;
723 
724  // look for nan in the array
725  if(std::find_if(parts.cbegin(), parts.cend(), [] (auto f) {return std::isnan(f);}) != parts.cend())
726  return false;
727 
728  if(parts.size() == 1)
729  oValue = Range{parts[0]};
730  else
731  oValue = Range{parts[0], parts[1]};
732 
733  return true;
734  }
735 
736 #ifdef EDITOR_MODE
737  // toString
738  bool toString(IUIDescription const *iDescription, const Range &iValue, std::string &oStringValue) const override
739  {
740  std::stringstream str;
741  if(iValue.fFrom == iValue.fTo)
742  str << iValue.fFrom;
743  else
744  {
745  str << iValue.fFrom;
746  str << ",";
747  str << iValue.fTo;
748  }
749  oStringValue = str.str();
750  return true;
751  }
752 #endif
753  };
754 
758  class VectorStringAttribute : public ByRefAttribute<std::vector<std::string>>
759  {
761 
762  public:
763  // Constructor
764  VectorStringAttribute(std::string const &iName,
765  typename super_type::Getter iGetter,
766  typename super_type::Setter iSetter,
767  char iDelimiter = ',',
768  bool iSkipEmptyEntries = false) :
769  super_type(iName, iGetter, iSetter),
770  fDelimiter{iDelimiter},
771  fSkipEmptyEntries{iSkipEmptyEntries}
772  {}
773 
774  // fromString
775  bool fromString(IUIDescription const *iDescription,
776  std::string const &iAttributeValue,
777  std::vector<std::string> &oValue) const override
778  {
779  oValue = Utils::splitString(iAttributeValue, fDelimiter, fSkipEmptyEntries);
780  return true;
781  }
782 
783 #ifdef EDITOR_MODE
784  // toString
785  bool toString(IUIDescription const *iDescription,
786  const std::vector<std::string> &iValue,
787  std::string &oStringValue) const override
788  {
789  oStringValue.clear();
790  int i = 0;
791  for(auto &entry : iValue)
792  {
793  if(i > 0)
794  oStringValue += fDelimiter;
795  if(!entry.empty() || !fSkipEmptyEntries)
796  {
797  oStringValue += entry;
798  i++;
799  }
800  }
801  return true;
802  }
803 #endif
804 
805  protected:
808  };
809 
813  template<typename T>
814  class ListAttribute : public ByValAttribute<T>
815  {
817 
818  public:
819  // Constructor
820  ListAttribute(std::string const &iName,
821  typename super_type::Getter iGetter,
822  typename super_type::Setter iSetter,
823  AttrValInitList<T> const &iAttributeValues) :
824  super_type(iName, iGetter, iSetter),
825  fAttributeValuesMap(iAttributeValues)
826 #ifdef EDITOR_MODE
827  ,fAttributeValuesList{iAttributeValues}
828 #endif
829  {
830  }
831 
832  // getType
833  IViewCreator::AttrType getType() override
834  {
835  return IViewCreator::kListType;
836  }
837 
838  // fromString
839  bool fromString(IUIDescription const *iDescription,
840  std::string const &iAttributeValue,
841  T &oValue) const override
842  {
843  if(fAttributeValuesMap.find(iAttributeValue) != fAttributeValuesMap.cend())
844  {
845  oValue = fAttributeValuesMap.at(iAttributeValue);
846  return true;
847  }
848 
849  DLOG_F(WARNING, "Attribute value '%s' is not valid for '%s'", iAttributeValue.c_str(), ViewAttribute::getName().c_str());
850  return false;
851  }
852 
853 #ifdef EDITOR_MODE
854  // toString
855  bool toString(IUIDescription const *iDescription,
856  T const &iValue,
857  std::string &oStringValue) const override
858  {
859  auto pos = std::find_if(std::begin(fAttributeValuesList),
860  std::end(fAttributeValuesList),
861  [&iValue](auto entry) -> bool {
862  return entry.second == iValue;
863  });
864  if(pos != std::end(fAttributeValuesList))
865  {
866  oStringValue = pos->first;
867  return true;
868  }
869 
870  return false;
871  }
872 
873  // getPossibleListValues
874  bool getPossibleListValues(std::list<const std::string *> &iValues) const override
875  {
876  for(auto const &p : fAttributeValuesList)
877  {
878  iValues.emplace_back(&p.first);
879  }
880  return true;
881  }
882 #endif
883 
884  protected:
886 #ifdef EDITOR_MODE
887  std::vector<typename AttrValMap<T>::value_type> const fAttributeValuesList;
888 #endif
889  };
890 
891 public:
892  // Constructor
893  explicit TCustomViewCreator(char const *iViewName = nullptr,
894  char const *iDisplayName = nullptr,
895  char const *iBaseViewName = VSTGUI::UIViewCreator::kCView) :
896  fViewName{iViewName},
897  fDisplayName{iDisplayName},
898  fBaseViewName{iBaseViewName},
899  fAttributes{}
900  {
901  // this allows for inheritance!
902  if(iViewName != nullptr && iDisplayName != nullptr)
903  VSTGUI::UIViewFactory::registerViewCreator(*this);
904  }
905 
906  // Destructor
908  {
909  // we simply clear the map since it holds shared pointers which will be discarded when they are no longer
910  // held by another object
911  fAttributes.clear();
912  }
913 
914  // getViewName
915  IdStringPtr getViewName() const override
916  {
917  return fViewName;
918  }
919 
920  // getDisplayName
921  UTF8StringPtr getDisplayName() const override
922  {
923  return fDisplayName;
924  }
925 
926  // getBaseViewName
927  IdStringPtr getBaseViewName() const override
928  {
929  return fBaseViewName;
930  }
931 
935  template<typename XView>
937  {
938  for(auto attribute : iOther.fAttributes)
939  {
940  registerAttribute(attribute.second);
941  }
942 
943  if(std::string(fBaseViewName) == std::string(VSTGUI::UIViewCreator::kCView))
944  fBaseViewName = iOther.fBaseViewName;
945  }
946 
950  void registerColorAttribute(std::string const &iName,
951  typename ColorAttribute::Getter iGetter,
952  typename ColorAttribute::Setter iSetter)
953  {
954  registerAttribute<ColorAttribute>(iName, iGetter, iSetter);
955  }
956 
960  void registerGradientAttribute(std::string const &iName,
961  typename GradientAttribute::Getter iGetter,
962  typename GradientAttribute::Setter iSetter)
963  {
964  registerAttribute<GradientAttribute>(iName, iGetter, iSetter);
965  }
966 
970  void registerBitmapAttribute(std::string const &iName,
971  typename BitmapAttribute::Getter iGetter,
972  typename BitmapAttribute::Setter iSetter)
973  {
974  registerAttribute<BitmapAttribute>(iName, iGetter, iSetter);
975  }
976 
980  void registerFontAttribute(std::string const &iName,
981  typename FontAttribute::Getter iGetter,
982  typename FontAttribute::Setter iSetter)
983  {
984  registerAttribute<FontAttribute>(iName, iGetter, iSetter);
985  }
986 
990  void registerMarginAttribute(std::string const &iName,
991  typename MarginAttribute::Getter iGetter,
992  typename MarginAttribute::Setter iSetter)
993  {
994  registerAttribute<MarginAttribute>(iName, iGetter, iSetter);
995  }
996 
1000  void registerRangeAttribute(std::string const &iName,
1001  typename RangeAttribute::Getter iGetter,
1002  typename RangeAttribute::Setter iSetter)
1003  {
1004  registerAttribute<RangeAttribute>(iName, iGetter, iSetter);
1005  }
1006 
1010  void registerVectorStringAttribute(std::string const &iName,
1011  typename VectorStringAttribute::Getter iGetter,
1012  typename VectorStringAttribute::Setter iSetter,
1013  char iDelimiter = ',',
1014  bool iSkipEmptyEntries = false)
1015  {
1016  registerAttribute<VectorStringAttribute>(iName, iGetter, iSetter, iDelimiter, iSkipEmptyEntries);
1017  }
1018 
1035  template<typename T>
1036  void registerListAttribute(std::string const &iName,
1037  typename ListAttribute<T>::Getter iGetter,
1038  typename ListAttribute<T>::Setter iSetter,
1039  AttrValInitList<T> const &iAttributeValues)
1040  {
1041  registerAttribute<ListAttribute<T>>(iName, iGetter, iSetter, iAttributeValues);
1042  }
1043 
1047  void registerTagAttribute(std::string const &iName,
1048  typename TagAttribute::Getter iGetter,
1049  typename TagAttribute::Setter iSetter)
1050  {
1051  registerAttribute<TagAttribute>(iName, iGetter, iSetter);
1052  }
1053 
1057  template<typename TInt>
1058  void registerIntegerAttribute(std::string const &iName,
1059  typename IntegerAttribute<TInt>::Getter iGetter,
1060  typename IntegerAttribute<TInt>::Setter iSetter)
1061  {
1062  registerAttribute<IntegerAttribute<TInt>>(iName, iGetter, iSetter);
1063  }
1064 
1068  void registerIntAttribute(std::string const &iName,
1069  typename IntegerAttribute<int32_t>::Getter iGetter,
1070  typename IntegerAttribute<int32_t>::Setter iSetter)
1071  {
1072  registerIntegerAttribute<int32_t>(iName, iGetter, iSetter);
1073  }
1074 
1078  void registerFloatAttribute(std::string const &iName,
1079  typename FloatAttribute<float>::Getter iGetter,
1080  typename FloatAttribute<float>::Setter iSetter)
1081  {
1082  registerAttribute<FloatAttribute<float>>(iName, iGetter, iSetter);
1083  }
1084 
1088  void registerDoubleAttribute(std::string const &iName,
1089  typename FloatAttribute<double>::Getter iGetter,
1090  typename FloatAttribute<double>::Setter iSetter)
1091  {
1092  registerAttribute<FloatAttribute<double>>(iName, iGetter, iSetter);
1093  }
1094 
1098  void registerBooleanAttribute(std::string const &iName,
1099  typename BooleanAttribute::Getter iGetter,
1100  typename BooleanAttribute::Setter iSetter)
1101  {
1102  registerAttribute<BooleanAttribute>(iName, iGetter, iSetter);
1103  }
1104 
1108  CView *create(const UIAttributes &attributes, const IUIDescription *description) const override
1109  {
1110 #ifdef JAMBA_DEBUG_LOGGING
1111  DLOG_F(INFO, "CustomViewCreator<%s>::create()", getViewName());
1112 #endif
1113 
1114  return createCustomView<TView>(CRect(0, 0, 0, 0), attributes, description);
1115  }
1116 
1121  bool apply(CView *view, const UIAttributes &attributes, const IUIDescription *description) const override
1122  {
1123  auto *tv = dynamic_cast<TView *>(view);
1124 
1125  if(tv == nullptr)
1126  return false;
1127 
1128  for(auto attribute : fAttributes)
1129  {
1130  attribute.second->apply(tv, attributes, description);
1131  }
1132 
1133  return true;
1134  }
1135 
1136  // getAttributeNames
1137  bool getAttributeNames(std::list<std::string> &attributeNames) const override
1138  {
1139  for(auto attribute : fAttributes)
1140  {
1141  attributeNames.emplace_back(attribute.first);
1142  }
1143  return true;
1144  }
1145 
1146  // getAttributeType
1147  AttrType getAttributeType(const std::string &attributeName) const override
1148  {
1149  auto iter = fAttributes.find(attributeName);
1150  if(iter != fAttributes.cend())
1151  {
1152  return iter->second->getType();
1153  }
1154  return kUnknownType;
1155  }
1156 
1157 #ifdef EDITOR_MODE
1158 
1161  bool getAttributeValue(CView *iView,
1162  const std::string &iAttributeName,
1163  std::string &oStringValue,
1164  const IUIDescription *iDescription) const override
1165  {
1166  auto *cdv = dynamic_cast<TView *>(iView);
1167 
1168  if(cdv == nullptr)
1169  return false;
1170 
1171  auto iter = fAttributes.find(iAttributeName);
1172  if(iter != fAttributes.cend())
1173  {
1174  return iter->second->getAttributeValue(cdv, iDescription, oStringValue);
1175  }
1176 
1177  return false;
1178  }
1179 
1183  bool getPossibleListValues(const std::string &iAttributeName, std::list<const std::string *> &iValues) const override
1184  {
1185  auto iter = fAttributes.find(iAttributeName);
1186  if(iter != fAttributes.cend())
1187  {
1188  return iter->second->getPossibleListValues(iValues);
1189  }
1190  return false;
1191  }
1192 #endif
1193 
1194 private:
1195 
1196  // somehow this is required...
1197  template<typename XView>
1198  friend class TCustomViewCreator;
1199 
1203  void registerAttribute(std::shared_ptr<ViewAttribute> iAttribute)
1204  {
1205  // making sure there are no duplicates (cannot use loguru here!)
1206  assert(fAttributes.find(iAttribute->getName()) == fAttributes.cend());
1207  fAttributes[iAttribute->getName()] = std::move(iAttribute);
1208  }
1209 
1213  template<typename TViewAttribute, typename... Args>
1214  void registerAttribute(std::string const &iName,
1215  typename TViewAttribute::Getter iGetter,
1216  typename TViewAttribute::Setter iSetter,
1217  Args&& ...iArgs)
1218  {
1219  std::shared_ptr<ViewAttribute> cva;
1220  cva.reset(new TViewAttribute(iName, iGetter, iSetter, std::forward<Args>(iArgs)...));
1221  registerAttribute(cva);
1222  }
1223 
1224 
1225  char const *fViewName;
1226  char const *fDisplayName;
1227  char const *fBaseViewName;
1228 
1229  // use a map of shared pointers so that they can easily be copied (see registerAttributes)
1230  std::map<std::string, std::shared_ptr<ViewAttribute>> fAttributes;
1231 };
1232 
1233 namespace impl {
1234  template<typename T>
1235  using creator_ctor_t = decltype(typename T::Creator());
1236 
1237  template<typename T>
1238  constexpr auto is_creator_ctor_detected = Utils::cpp17::experimental::is_detected_v<creator_ctor_t, T>;
1239 }
1240 
1310 template<typename TView, typename TBaseView = void>
1312 {
1313 public:
1314  explicit CustomViewCreator(char const *iViewName = nullptr,
1315  char const *iDisplayName = nullptr,
1316  char const *iBaseViewName = VSTGUI::UIViewCreator::kCView) :
1317  TCustomViewCreator<TView>(iViewName, iDisplayName, iBaseViewName)
1318  {
1319  if constexpr(impl::is_creator_ctor_detected<TBaseView>)
1320  TCustomViewCreator<TView>::registerAttributes(typename TBaseView::Creator());
1321  }
1322 };
1323 
1324 }
1325 
char const * fBaseViewName
Definition: CustomViewCreator.h:1227
BitmapAttribute(std::string const &iName, typename ByValAttribute< BitmapPtr >::Getter iGetter, typename ByValAttribute< BitmapPtr >::Setter iSetter)
Definition: CustomViewCreator.h:558
IViewCreator::AttrType getType() override
Definition: CustomViewCreator.h:517
CView * create(const UIAttributes &attributes, const IUIDescription *description) const override
This is the factory method which will instantiate the view.
Definition: CustomViewCreator.h:1108
void registerFloatAttribute(std::string const &iName, typename FloatAttribute< float >::Getter iGetter, typename FloatAttribute< float >::Setter iSetter)
Registers a float attribute with the given name and getter/setter.
Definition: CustomViewCreator.h:1078
Inherit from this class to provide the factory for a custom view.
Definition: CustomViewCreator.h:1311
void registerColorAttribute(std::string const &iName, typename ColorAttribute::Getter iGetter, typename ColorAttribute::Setter iSetter)
Registers a color attribute with the given name and getter/setter.
Definition: CustomViewCreator.h:950
void registerGradientAttribute(std::string const &iName, typename GradientAttribute::Getter iGetter, typename GradientAttribute::Setter iSetter)
Registers a gradient attribute with the given name and getter/setter.
Definition: CustomViewCreator.h:960
IViewCreator::AttrType getType() override
Definition: CustomViewCreator.h:607
IntegerAttribute(std::string const &iName, Getter iGetter, Setter iSetter)
Definition: CustomViewCreator.h:330
IViewCreator::AttrType getType() override
Definition: CustomViewCreator.h:467
Generic base class that implements the logic for a ViewAttribute that uses a getter and setter in TVi...
Definition: CustomViewCreator.h:164
constexpr TagID UNDEFINED_TAG_ID
Constant used to test whether the TagID represents a valid id or an undefined one.
Definition: Types.h:61
Specialization for an float attribute (which can be a double or a float, etc..).
Definition: CustomViewCreator.h:370
TCustomViewCreator(char const *iViewName=nullptr, char const *iDisplayName=nullptr, char const *iBaseViewName=VSTGUI::UIViewCreator::kCView)
Definition: CustomViewCreator.h:893
Specialization for the margin attribute.
Definition: CustomViewCreator.h:646
ListAttribute(std::string const &iName, typename super_type::Getter iGetter, typename super_type::Setter iSetter, AttrValInitList< T > const &iAttributeValues)
Definition: CustomViewCreator.h:820
RangeAttribute(std::string const &iName, typename ByRefAttribute< Range >::Getter iGetter, typename ByRefAttribute< Range >::Setter iSetter)
Definition: CustomViewCreator.h:711
VectorStringAttribute(std::string const &iName, typename super_type::Getter iGetter, typename super_type::Setter iSetter, char iDelimiter=',', bool iSkipEmptyEntries=false)
Definition: CustomViewCreator.h:764
void registerMarginAttribute(std::string const &iName, typename MarginAttribute::Getter iGetter, typename MarginAttribute::Setter iSetter)
Registers a Margin attribute with the given name and getter/setter.
Definition: CustomViewCreator.h:990
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TFloat &oValue) const override
Definition: CustomViewCreator.h:387
FloatAttribute(std::string const &iName, Getter iGetter, Setter iSetter)
Definition: CustomViewCreator.h:377
void registerAttributes(TCustomViewCreator< XView > const &iOther)
This method is called to register all the attributes from another CustomViewCreator (used in case of ...
Definition: CustomViewCreator.h:936
IViewCreator::AttrType getType() override
Definition: CustomViewCreator.h:180
Specialization for a vector of strings.
Definition: CustomViewCreator.h:758
FontAttribute(std::string const &iName, typename ByValAttribute< FontPtr >::Getter iGetter, typename ByValAttribute< FontPtr >::Setter iSetter)
Definition: CustomViewCreator.h:601
TSetter Setter
Definition: CustomViewCreator.h:168
AttrType getAttributeType(const std::string &attributeName) const override
Definition: CustomViewCreator.h:1147
T fTo
Definition: Lerp.h:341
void registerListAttribute(std::string const &iName, typename ListAttribute< T >::Getter iGetter, typename ListAttribute< T >::Setter iSetter, AttrValInitList< T > const &iAttributeValues)
Registers a list attribute with the given name and getter/setter.
Definition: CustomViewCreator.h:1036
Specialization for the range attribute.
Definition: CustomViewCreator.h:708
Specialization for a bitmap attribute.
Definition: CustomViewCreator.h:554
Specialization for an Integer attribute (which can be any kind of integer, like short,...
Definition: CustomViewCreator.h:323
CustomViewCreator(char const *iViewName=nullptr, char const *iDisplayName=nullptr, char const *iBaseViewName=VSTGUI::UIViewCreator::kCView)
Definition: CustomViewCreator.h:1314
std::initializer_list< typename AttrValMap< T >::value_type > AttrValInitList
Defines the type to initialize an [AttrValMap], for an example check [TCustomViewCreator::registerLis...
Definition: CustomViewCreator.h:120
TView * createCustomView(CRect const &iSize, const UIAttributes &iAttributes, const IUIDescription *)
Factory method which creates the actual view.
Definition: CustomViewCreator.h:107
void registerTagAttribute(std::string const &iName, typename TagAttribute::Getter iGetter, typename TagAttribute::Setter iSetter)
Registers a tag attribute with the given name and getter/setter.
Definition: CustomViewCreator.h:1047
Specialization for the color attribute.
Definition: CustomViewCreator.h:415
TAttribute< T, T const &(StepButtonView ::*)() const, void(StepButtonView ::*)(T const &)> ByRefAttribute
ByRefAttribute defines getter/setter by const reference.
Definition: CustomViewCreator.h:255
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TagID &oValue) const override
Definition: CustomViewCreator.h:276
ParamID TagID
Defining a type for tags.
Definition: Types.h:57
TAttribute(std::string const &iName, Getter iGetter, Setter iSetter)
Definition: CustomViewCreator.h:171
std::vector< std::string > splitString(const std::string &iString, char iDelimiter, bool iSkipEmptyEntries)
Split a string according to a delimiter and returns a vector.
Definition: StringUtils.cpp:30
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, BitmapPtr &oValue) const override
Definition: CustomViewCreator.h:570
MarginAttribute(std::string const &iName, typename ByRefAttribute< Margin >::Getter iGetter, typename ByRefAttribute< Margin >::Setter iSetter)
Definition: CustomViewCreator.h:649
char const * fDisplayName
Definition: CustomViewCreator.h:1226
bool apply(CView *view, const UIAttributes &attributes, const IUIDescription *description) const override
Extract all the attribute values and apply them to the view.
Definition: CustomViewCreator.h:1121
std::string getName() const
Name of the attribute (which ends up being an attribute in the xml file) Ex: <view back-color="~ Blac...
Definition: CustomViewCreator.h:67
bool getAttributeNames(std::list< std::string > &attributeNames) const override
Definition: CustomViewCreator.h:1137
CFontDesc * FontPtr
Definition: Types.h:53
AttrValMap< T > const fAttributeValuesMap
Definition: CustomViewCreator.h:885
Specialization for a tag attribute (vst type TagID).
Definition: CustomViewCreator.h:261
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, GradientPtr &oValue) const override
Definition: CustomViewCreator.h:473
TAttribute< T, T(StepButtonView ::*)() const, void(StepButtonView ::*)(T)> ByValAttribute
ByValAttribute defines getter/setter by value (copy)
Definition: CustomViewCreator.h:249
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, FontPtr &oValue) const override
Definition: CustomViewCreator.h:613
CCoord fTop
Definition: LookAndFeel.h:58
char const * fViewName
Definition: CustomViewCreator.h:1225
IViewCreator::AttrType getType() override
Definition: CustomViewCreator.h:564
ViewAttribute(std::string iName)
Definition: CustomViewCreator.h:54
IViewCreator::AttrType getType() override
Definition: CustomViewCreator.h:334
TagAttribute(std::string const &iName, typename ByValAttribute< TagID >::Getter iGetter, typename ByValAttribute< TagID >::Setter iSetter)
Definition: CustomViewCreator.h:264
bool apply(CView *iView, const UIAttributes &iAttributes, const IUIDescription *iDescription) override
Extracts the value from iAttributes for getName() attribute and "apply" it on the view provided.
Definition: CustomViewCreator.h:195
TGetter Getter
Definition: CustomViewCreator.h:167
Margin is a similar concept to css: used to create space around elements, outside of any defined bord...
Definition: LookAndFeel.h:32
CCoord fRight
Definition: LookAndFeel.h:59
std::string fName
Definition: CustomViewCreator.h:99
Definition: Types.h:29
IViewCreator::AttrType getType() override
Definition: CustomViewCreator.h:381
Specialization for the color attribute.
Definition: CustomViewCreator.h:456
constexpr auto is_creator_ctor_detected
Definition: CustomViewCreator.h:1238
Specialization for a bitmap attribute.
Definition: CustomViewCreator.h:597
void registerFontAttribute(std::string const &iName, typename FontAttribute::Getter iGetter, typename FontAttribute::Setter iSetter)
Registers a font attribute with the given name and getter/setter.
Definition: CustomViewCreator.h:980
std::map< std::string, T > AttrValMap
Defines a map of string to attribute value.
Definition: CustomViewCreator.h:114
void registerAttribute(std::shared_ptr< ViewAttribute > iAttribute)
Internal method to register an attribute...
Definition: CustomViewCreator.h:1203
T fFrom
Definition: Lerp.h:340
void registerBooleanAttribute(std::string const &iName, typename BooleanAttribute::Getter iGetter, typename BooleanAttribute::Setter iSetter)
Registers a boolean attribute with the given name and getter/setter.
Definition: CustomViewCreator.h:1098
~TCustomViewCreator() override
Definition: CustomViewCreator.h:907
decltype(typename T::Creator()) creator_ctor_t
Definition: CustomViewCreator.h:1235
Definition: CustomController.h:24
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, bool &oValue) const override
Definition: CustomViewCreator.h:523
IdStringPtr getViewName() const override
Definition: CustomViewCreator.h:915
IViewCreator::AttrType getType() override
Definition: CustomViewCreator.h:426
std::map< std::string, std::shared_ptr< ViewAttribute > > fAttributes
Definition: CustomViewCreator.h:1230
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, Margin &oValue) const override
Definition: CustomViewCreator.h:655
void registerRangeAttribute(std::string const &iName, typename RangeAttribute::Getter iGetter, typename RangeAttribute::Setter iSetter)
Registers a Range attribute with the given name and getter/setter.
Definition: CustomViewCreator.h:1000
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, std::vector< std::string > &oValue) const override
Definition: CustomViewCreator.h:775
CCoord fLeft
Definition: LookAndFeel.h:61
Specialization for a list of possible values defined by the AttributeMap
Definition: CustomViewCreator.h:814
IdStringPtr getBaseViewName() const override
Definition: CustomViewCreator.h:927
void registerIntegerAttribute(std::string const &iName, typename IntegerAttribute< TInt >::Getter iGetter, typename IntegerAttribute< TInt >::Setter iSetter)
Registers an Integer (any kind) attribute with the given name and getter/setter.
Definition: CustomViewCreator.h:1058
void registerVectorStringAttribute(std::string const &iName, typename VectorStringAttribute::Getter iGetter, typename VectorStringAttribute::Setter iSetter, char iDelimiter=',', bool iSkipEmptyEntries=false)
Registers a Range attribute with the given name and getter/setter.
Definition: CustomViewCreator.h:1010
GradientAttribute(std::string const &iName, typename ByValAttribute< GradientPtr >::Getter iGetter, typename ByValAttribute< GradientPtr >::Setter iSetter)
Definition: CustomViewCreator.h:461
ColorAttribute(std::string const &iName, typename ByRefAttribute< CColor >::Getter iGetter, typename ByRefAttribute< CColor >::Setter iSetter)
Definition: CustomViewCreator.h:420
void registerIntAttribute(std::string const &iName, typename IntegerAttribute< int32_t >::Getter iGetter, typename IntegerAttribute< int32_t >::Setter iSetter)
Registers an int attribute with the given name and getter/setter.
Definition: CustomViewCreator.h:1068
UTF8StringPtr getDisplayName() const override
Definition: CustomViewCreator.h:921
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, CColor &oValue) const override
Definition: CustomViewCreator.h:432
virtual bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, T &oValue) const
Subclasses need to implement this method to convert a string (iAttributeValue) to a T.
Definition: CustomViewCreator.h:189
Generic custom view creator base class.
Definition: CustomViewCreator.h:155
IViewCreator::AttrType getType() override
Definition: CustomViewCreator.h:833
void registerDoubleAttribute(std::string const &iName, typename FloatAttribute< double >::Getter iGetter, typename FloatAttribute< double >::Setter iSetter)
Registers a double attribute with the given name and getter/setter.
Definition: CustomViewCreator.h:1088
void registerAttribute(std::string const &iName, typename TViewAttribute::Getter iGetter, typename TViewAttribute::Setter iSetter, Args &&...iArgs)
Generic register attribute.
Definition: CustomViewCreator.h:1214
CBitmap * BitmapPtr
Definition: Types.h:49
Specialization for the boolean attribute.
Definition: CustomViewCreator.h:507
CCoord fBottom
Definition: LookAndFeel.h:60
void registerBitmapAttribute(std::string const &iName, typename BitmapAttribute::Getter iGetter, typename BitmapAttribute::Setter iSetter)
Registers a bitmap attribute with the given name and getter/setter.
Definition: CustomViewCreator.h:970
constexpr ParamID UNDEFINED_PARAM_ID
Constant used throughout the code to test whether the ParamID represents a valid id or an undefined o...
Definition: Types.h:47
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, T &oValue) const override
Subclasses need to implement this method to convert a string (iAttributeValue) to a T.
Definition: CustomViewCreator.h:839
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, Range &oValue) const override
Definition: CustomViewCreator.h:717
BooleanAttribute(std::string const &iName, typename ByValAttribute< bool >::Getter iGetter, typename ByValAttribute< bool >::Setter iSetter)
Definition: CustomViewCreator.h:511
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TInt &oValue) const override
Definition: CustomViewCreator.h:340
Base abstract class for an attribute of a view.
Definition: CustomViewCreator.h:50
IViewCreator::AttrType getType() override
Definition: CustomViewCreator.h:270
Setter fSetter
Definition: CustomViewCreator.h:242
CGradient * GradientPtr
Definition: Types.h:57