Jamba  3.2.0
TextButtonView.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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/lib/controls/cbuttons.h>
22 
23 namespace pongasoft {
24 namespace VST {
25 namespace GUI {
26 namespace Views {
27 
28 using namespace VSTGUI;
29 using namespace Params;
30 
37 class TextButtonView : public CustomViewAdapter<CTextButton>
38 {
39 public:
40  using OnClickListener = std::function<void()>;
41 
42  // Constructor
43  explicit TextButtonView(const CRect &iSize) : CustomViewAdapter(iSize) {}
44 
45  // get/set titleTag to (optionally) tie the title of this text button to a (Jmb) parameter
46  virtual void setTitleTag(int32_t iValue);
47  int32_t getTitleTag() const { return fTitleTag; };
48 
49  // get/set disabled text color (color to use when button is disabled)
50  CColor const &getDisabledTextColor() const { return fDisabledTextColor; }
51  virtual void setDisabledTextColor(CColor const &iColor) { fDisabledTextColor = iColor; };
52 
53  // get/set disabled gradient (gradient to use when button is disabled)
54  GradientPtr getDisabledGradient() const { return fDisabledGradient; }
55  virtual void setDisabledGradient(GradientPtr iGradient) { fDisabledGradient = iGradient; };
56 
57  void setMouseEnabled(bool bEnable) override;
58 
59  // draw => overridden to handle disabled state
60  void draw(CDrawContext *context) override;
61 
62  // registerParameters
63  void registerParameters() override;
64 
65  // handles value change
66  void valueChanged() override;
67 
68  // call to programmatically click the button
69  virtual void click();
70 
71  // call to programmatically unClick the button
72  virtual void unClick();
73 
74  // callback when the button is clicked
75  virtual void onClick();
76 
77  // setOnClickListener
78  void setOnClickListener(OnClickListener iListener) { fOnClickListener = std::move(iListener); }
79 
94  BitmapPtr getImage() const { return fImage; }
95  void setImage(BitmapPtr iImage) { fImage = iImage; }
96 
99  bool getImageHasDisabledState() const { return fImageHasDisabledState; }
100  void setImageHasDisabledState(bool iValue) { fImageHasDisabledState = iValue; }
101 
102 protected:
106  virtual void drawButtonText(CDrawContext *context);
107 
111  virtual void drawButtonImage(CDrawContext *context);
112 
113 protected:
114  // (optionally) tie the title of this text button to a (Jmb) parameter
115  int32_t fTitleTag{-1};
116 
117  // Which color and gradient to use when disabled
120 
121  // the underlying jmb parameter of type UTF8String
123 
124  // the image to use (in place of text)
125  BitmapSPtr fImage{nullptr};
126 
127  // whether the image has disabled state (3 frames) or not (2 frames)
128  bool fImageHasDisabledState{false};
129 
130  // the onclick listener
131  OnClickListener fOnClickListener{};
132 
133 public:
134  class Creator : public CustomViewCreator<TextButtonView, CustomViewAdapter<CTextButton>>
135  {
136  public:
137  explicit Creator(char const *iViewName = nullptr, char const *iDisplayName = nullptr) :
138  CustomViewCreator(iViewName, iDisplayName, VSTGUI::UIViewCreator::kCTextButton)
139  {
140  registerTagAttribute("title-tag", &TextButtonView::getTitleTag, &TextButtonView::setTitleTag);
141  registerColorAttribute("disabled-text-color", &TextButtonView::getDisabledTextColor, &TextButtonView::setDisabledTextColor);
142  registerGradientAttribute("disabled-gradient", &TextButtonView::getDisabledGradient, &TextButtonView::setDisabledGradient);
143  registerBitmapAttribute("button-image", &TextButtonView::getImage, &TextButtonView::setImage);
144  registerBooleanAttribute("button-image-has-disabled-state", &TextButtonView::getImageHasDisabledState, &TextButtonView::setImageHasDisabledState);
145  }
146  };
147 };
148 
149 }
150 }
151 }
152 }
153 
GradientSPtr fDisabledGradient
Definition: TextButtonView.h:119
void setOnClickListener(OnClickListener iListener)
Definition: TextButtonView.h:78
bool getImageHasDisabledState() const
Definition: TextButtonView.h:99
Definition: CustomViewCreator.h:1049
SharedPointer< CBitmap > BitmapSPtr
Definition: Types.h:50
CColor const & getDisabledTextColor() const
Definition: TextButtonView.h:50
Definition: Clock.h:22
CBitmap * BitmapPtr
Definition: Types.h:49
virtual void setTitleTag(int32_t iValue)
Definition: TextButtonView.cpp:51
BitmapPtr getImage() const
Definition: TextButtonView.h:94
Definition: TextButtonView.h:37
std::function< void()> OnClickListener
Definition: TextButtonView.h:40
SharedPointer< CGradient > GradientSPtr
Definition: Types.h:58
Creator(char const *iViewName=nullptr, char const *iDisplayName=nullptr)
Definition: TextButtonView.h:137
CColor fDisabledTextColor
Definition: TextButtonView.h:118
void setImage(BitmapPtr iImage)
Definition: TextButtonView.h:95
Definition: Types.h:29
void setImageHasDisabledState(bool iValue)
Definition: TextButtonView.h:100
virtual void setDisabledGradient(GradientPtr iGradient)
Definition: TextButtonView.h:55
virtual void setDisabledTextColor(CColor const &iColor)
Definition: TextButtonView.h:51
TextButtonView(const CRect &iSize)
Definition: TextButtonView.h:43
GradientPtr getDisabledGradient() const
Definition: TextButtonView.h:54
int32_t getTitleTag() const
Definition: TextButtonView.h:47
CGradient * GradientPtr
Definition: Types.h:57