1 /* B.Oops
2  * Glitch effect sequencer LV2 plugin
3  *
4  * Copyright (C) 2020 by Sven Jähnichen
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ICONPADBUTTON_HPP_
21 #define ICONPADBUTTON_HPP_
22 
23 #include "PadButton.hpp"
24 #include "BWidgets/ImageIcon.hpp"
25 
26 class IconPadButton : public PadButton
27 {
28 public:
IconPadButton()29 	IconPadButton () : IconPadButton (0.0, 0.0, BWIDGETS_DEFAULT_BUTTON_WIDTH, BWIDGETS_DEFAULT_BUTTON_HEIGHT, "padbutton", "", "", 0.0) {}
IconPadButton(const double x,const double y,const double width,const double height,const std::string & name,const std::string buttonfilename,const std::string iconfilename,double defaultValue=0.0)30 	IconPadButton (const double x, const double y, const double width, const double height, const std::string& name,
31 		const std::string buttonfilename, const std::string iconfilename, double defaultValue = 0.0) :
32 		PadButton (x, y, width, height, name, NOSYMBOL, defaultValue),
33 		button (0, 0, height, height, name + "/icon", buttonfilename),
34 		icon (height, 0, width - height, height, name + "/icon", iconfilename)
35 	{
36 		button.setFocusable (false);
37 		add (button);
38 		icon.setFocusable (false);
39 		icon.setClickable (false);
40 		add (icon);
41 	}
42 
IconPadButton(const IconPadButton & that)43 	IconPadButton (const IconPadButton& that) : PadButton (that), button (that.button), icon (that.icon)
44 	{
45 		add (button);
46 		add (icon);
47 	}
48 
operator =(const IconPadButton & that)49 	IconPadButton& operator= (const IconPadButton& that)
50 	{
51 		release (&icon);
52 		release (&button);
53 
54 		PadButton::operator= (that);
55 		button = that.button;
56 		icon = that.icon;
57 
58 		add (button);
59 		add (icon);
60 
61 		return *this;
62 	}
63 
clone() const64 	virtual Widget* clone () const override {return new IconPadButton (*this);}
65 
setWidth(const double width)66 	virtual void setWidth (const double width) override
67 	{
68 		PadButton::setWidth (width);
69 		button.moveTo (0, 0);
70 		button.setWidth (getHeight());
71 		icon.moveTo (getHeight(), 0);
72 		icon.setWidth (getWidth() - getHeight());
73 	}
74 
setHeight(const double height)75 	virtual void setHeight (const double height) override
76 	{
77 		PadButton::setHeight (height);
78 		button.moveTo (0, 0);
79 		button.setWidth (getHeight());
80 		icon.moveTo (getHeight(), 0);
81 		icon.setWidth (getWidth() - getHeight());
82 	}
83 
resize()84 	virtual void resize () override
85 	{
86 		icon.resize ();
87 		icon.moveTo (icon.getHeight(), 0);
88 		button.moveTo (0, 0);
89 		button.resize (icon.getHeight(), icon.getHeight());
90 		PadButton::resize ();
91 	}
92 
resize(const double width,const double height)93 	virtual void resize (const double width, const double height) override {resize (BUtilities::Point (width, height));}
94 
resize(const BUtilities::Point extends)95 	virtual void resize (const BUtilities::Point extends) override
96 	{
97 		PadButton::resize (BUtilities::Point (extends.x, extends.y));
98 		button.moveTo (0, 0);
99 		button.resize (getHeight(), getHeight());
100 		icon.moveTo (getHeight(), 0);
101 		icon.resize (getWidth() - getHeight(), getHeight());
102 	}
103 
setSymbol(const SymbolIndex sym)104 	virtual void setSymbol (const SymbolIndex sym) override {}
105 
loadImage(BColors::State state,const std::string & filename)106 	void loadImage (BColors::State state, const std::string& filename)
107 	{
108 		icon.loadImage (state, filename);
109 		icon.update();
110 	}
111 
onFocusIn(BEvents::FocusEvent * event)112 	virtual void onFocusIn (BEvents::FocusEvent* event) override
113 	{
114 		if (event && event->getWidget())
115 		{
116 			BUtilities::Point pos = event->getPosition();
117 
118 			if
119 			(
120 				(pos.x >= button.getPosition().x) &&
121 				(pos.x <= button.getPosition().x + button.getWidth()) &&
122 				(pos.y >= button.getPosition().y) &&
123 				(pos.y <= button.getPosition().y + button.getHeight())
124 			)
125 			{
126 				raiseToTop();
127 				focusLabel_.raiseToTop();
128 				focusLabel_.setText (BOOPS_LABEL_MENU);
129 				focusLabel_.resize();
130 				focusLabel_.moveTo (pos.x - 0.5 * focusLabel_.getWidth(), pos.y - focusLabel_.getHeight());
131 				focusLabel_.show();
132 			}
133 
134 			else focusLabel_.hide();
135 		}
136 		Widget::onFocusIn (event);
137 	}
138 
139 	BWidgets::ImageIcon button;
140 	BWidgets::ImageIcon icon;
141 };
142 
143 #endif /* ICONPADBUTTON_HPP_ */
144