1 /* Label.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_LABEL_HPP_
19 #define BWIDGETS_LABEL_HPP_
20 
21 #define BWIDGETS_DEFAULT_LABEL_WIDTH 60
22 #define BWIDGETS_DEFAULT_LABEL_HEIGHT 20
23 #define BWIDGETS_DEFAULT_LABEL_ALIGN BStyles::TEXT_ALIGN_CENTER
24 #define BWIDGETS_DEFAULT_LABEL_VALIGN BStyles::TEXT_VALIGN_MIDDLE
25 
26 #define BWIDGETS_LABEL_TEXT_CHANGED_MESSAGE "TEXT_CHANGED"
27 #define BWIDGETS_LABEL_EDIT_ENTERED_MESSAGE "EDIT_ENTERED"
28 
29 #include "Widget.hpp"
30 
31 namespace BWidgets
32 {
33 
34 enum LabelPosition
35 {
36 	LABEL_CENTER,
37 	LABEL_TOP,
38 	LABEL_BOTTOM,
39 	LABEL_RIGHT,
40 	LABEL_LEFT
41 };
42 
43 /**
44  * Class BWidgets::Label
45  *
46  * Simple text output widget.
47  */
48 class Label : public Widget
49 {
50 public:
51 	Label ();
52 	Label (const double x, const double y, const double width, const double height, const std::string& text);
53 	Label (const double x, const double y, const double width, const double height, const std::string& name, const std::string& text);
54 
55 	/**
56 	 * Assignment. Copies the label properties from a source label and keeps
57 	 * its name and its position within the widget tree. Emits a
58 	 * BEvents::ExposeEvent if the label is visible.
59 	 * @param that Source label
60 	 */
61 	Label& operator= (const Label& that);
62 
63 	/**
64 	 * Pattern cloning. Creates a new instance of the widget and copies all
65 	 * its properties.
66 	 */
67 	virtual Widget* clone () const override;
68 
69 	/**
70 	 * Sets the output text.
71 	 * @param text Output text
72 	 */
73 	void setText (const std::string& text);
74 
75 	/**
76 	 * Gets the output text
77 	 * @return Output text
78 	 */
79 	std::string getText () const;
80 
81 	/**
82 	 * Sets the BColors::ColorSet for this widget
83 	 * @param colors Color set.
84 	 */
85 	void setTextColors (const BColors::ColorSet& colorset);
86 
87 	/**
88 	 * Gets (a pointer to) the BColors::ColorSet of this widget.
89 	 * @return Pointer to the color set.
90 	 */
91 	BColors::ColorSet* getTextColors ();
92 
93 	/**
94 	 * Sets the font for the text output.
95 	 * @param font Font
96 	 */
97 	void setFont (const BStyles::Font& font);
98 
99 	/**
100 	 * Gets (a pointer to) the font for the text output.
101 	 * @return Pointer to font
102 	 */
103 	BStyles::Font* getFont ();
104 
105 	/**
106 	 * Gets the effective width of a text with the given font
107 	 * @param text		Text string
108 	 * @param return	Effective width of the text
109 	 */
110 	double getTextWidth (std::string& text);
111 
112 	/**
113 	 * Resizes the widget, redraw and emits a BEvents::ExposeEvent if the
114 	 * widget is visible. If no parameters are given, the widget will be
115 	 * resized to the size of the containing child widgets or to the text
116 	 * extends (what is higher).
117 	 * @param width		New widgets width
118 	 * @param height	New widgets height
119 	 * @param extends	New widget extends
120 	 */
121 	virtual void resize () override;
122 	virtual void resize (const double width, const double height) override;
123 	virtual void resize (const BUtilities::Point extends) override;
124 
125 	/**
126 	 * Scans theme for widget properties and applies these properties.
127 	 * @param theme Theme to be scanned.
128 	 * 				Styles used are:
129 	 * 				"textcolors" for BColors::ColorSet
130 	 * 				"font" for BStyles::Font
131 	 * @param name Name of the BStyles::StyleSet within the theme to be
132 	 * 		  	   applied.
133 	 */
134 	virtual void applyTheme (BStyles::Theme& theme) override;
135 	virtual void applyTheme (BStyles::Theme& theme, const std::string& name) override;
136 
137 	void setEditable (const bool status);
138 	bool isEditable () const;
139 
140 	void setEditMode (const bool mode);
141 	bool getEditMode () const;
142 
143 	void setCursor (const size_t pos);
144 	void setCursor (const size_t from, const size_t to);
145 
146 	void applyEdit ();
147 	void discardEdit ();
148 
149 	virtual void onButtonClicked (BEvents::PointerEvent* event) override;
150 	virtual void onPointerDragged (BEvents::PointerEvent* event) override;
151 	virtual void onKeyPressed (BEvents::KeyEvent* event) override;
152 	virtual void onKeyReleased (BEvents::KeyEvent* event) override;
153 
154 protected:
155 	size_t getCursorFromCoords (const BUtilities::Point& position);
156 	virtual void draw (const BUtilities::RectArea& area) override;
157 
158 	BColors::ColorSet labelColors;
159 	BStyles::Font labelFont;
160 	std::string labelText;
161 	std::string oldText;
162 	std::u32string u32labelText;
163 	bool editable;
164 	bool editMode;
165 	size_t cursorFrom;
166 	size_t cursorTo;
167 };
168 
169 }
170 
171 #endif /* BWIDGETS_LABEL_HPP_ */
172