1 /* HSliderValue.hpp
2  * Copyright (C) 2018, 2019  Sven Jähnichen
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef BWIDGETS_HSLIDERVALUE_HPP_
19 #define BWIDGETS_HSLIDERVALUE_HPP_
20 
21 #include "Label.hpp"
22 #include "HSlider.hpp"
23 
24 #define BWIDGETS_DEFAULT_HSLIDERVALUE_WIDTH BWIDGETS_DEFAULT_HSLIDER_WIDTH
25 #define BWIDGETS_DEFAULT_HSLIDERVALUE_HEIGHT (BWIDGETS_DEFAULT_HSLIDER_HEIGHT + BWIDGETS_DEFAULT_LABEL_HEIGHT)
26 
27 namespace BWidgets
28 {
29 
30 /**
31  * Class BWidgets::HSliderValue
32  *
33  * Composite BWidgets::HSlider widget that additionally displays the value.
34  */
35 class HSliderValue : public HSlider
36 {
37 public:
38 	HSliderValue ();
39 	HSliderValue (const double x, const double y, const double width, const double height, const std::string& name,
40 		      const double value, const double min, const double max, const double step,
41 		      const std::string& valueFormat, LabelPosition valuePos = LABEL_TOP);
42 
43 	/**
44 	 * Creates a new (orphan) slider and copies the slider properties from a
45 	 * source slider.
46 	 * @param that Source slider
47 	 */
48 	HSliderValue (const HSliderValue& that);
49 
50 	/**
51 	 * Assignment. Copies the slider properties from a source slider and keeps
52 	 * its name and its position within the widget tree. Emits an expose event
53 	 * if the widget is visible and a value changed event.
54 	 * @param that Source slider
55 	 */
56 	HSliderValue& operator= (const HSliderValue& that);
57 
58 	/**
59 	 * Pattern cloning. Creates a new instance of the widget and copies all
60 	 * its properties.
61 	 */
62 	virtual Widget* clone () const override;
63 
64 	/**
65 	 * Changes the value of the widget and keeps it within the defined range.
66 	 * Passes the value to its predefined child widgets.
67 	 * Emits a value changed event and (if visible) an expose event.
68 	 * @param val Value
69 	 */
70 	virtual void setValue (const double val) override;
71 
72 	/**
73 	 * Sets the position of the value display label.
74 	 * @param pos	Position of the value display label.
75 	 */
76 	virtual void setValuePosition (const LabelPosition pos);
77 
78 	/**
79 	 * Gets the position of the value display label.
80 	 * @return	Position of the value display label.
81 	 */
82 	LabelPosition getValuePosition () const;
83 
84 	/**
85 	 * Sets the value output format.
86 	 * @valueFormat Format of the output in printf standard for type double.
87 	 */
88 	void setValueFormat (const std::string& valueFormat);
89 
90 	/**
91 	 * Gets the value output format.
92 	 * @return Format of the output in printf standard for type double.
93 	 */
94 	std::string getValueFormat () const;
95 
96 	/**
97 	 * Gets (a pointer to) the Label for direct access.
98 	 * @return Pointer to the label
99 	 */
100 	Label* getDisplayLabel ();
101 
102 	/**
103 	 * Calls a redraw of the widget and calls postRedisplay () if the the
104 	 * Widget is visible.
105 	 * This method should be called if the widgets properties are indirectly
106 	 * changed.
107 	 */
108 	virtual void update () override;
109 
110 	/**
111 	 * Scans theme for widget properties and applies these properties.
112 	 * @param theme Theme to be scanned.
113 	 * 				Styles used are:
114 	 * 				BWIDGETS_KEYWORD_BORDER
115 	 * 				BWIDGETS_KEYWORD_BACKGROUND
116 	 * 				BWIDGETS_KEYWORD_FGCOLORS
117 	 * 				BWIDGETS_KEYWORD_BGCOLORS
118 	 * 				BWIDGETS_KEYWORD_TEXTCOLORS
119 	 * 				BWIDGETS_KEYWORD_FONT.
120 	 * @param name Name of the BStyles::StyleSet within the theme to be
121 	 * 		  	   applied.
122 	 */
123 	virtual void applyTheme (BStyles::Theme& theme) override;
124 	virtual void applyTheme (BStyles::Theme& theme, const std::string& name) override;
125 
126 protected:
127 	virtual void updateCoords () override;
128 	static void displayDraggedCallback (BEvents::Event* event);
129 	static void displayMessageCallback (BEvents::Event* event);
130 
131 	Label valueDisplay;
132 
133 	LabelPosition valPosition;
134 	std::string valFormat;
135 	BUtilities::RectArea displayArea;
136 };
137 
138 }
139 
140 #endif /* BWIDGETS_HSLIDERVALUE_HPP_ */
141