1 /* Text.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_TEXT_HPP_
19 #define BWIDGETS_TEXT_HPP_
20 
21 #include "Widget.hpp"
22 
23 namespace BWidgets
24 {
25 
26 /**
27  * Class BWidgets::Text
28  *
29  * Text output widget with line breaks. Line breaks occur
30  * (i) on "\n" or
31  * (ii) on spaces when text length exceed the widget width or
32  * (iii) on any position when text length exceed the widget width.
33  */
34 class Text : public Widget
35 {
36 public:
37 	Text ();
38 	Text (const std::string& text);
39 	Text (const double x, const double y, const double width, const double height, const std::string& text);
40 	Text (const double x, const double y, const double width, const double height, const std::string& name, const std::string& text, bool resizable = false);
41 
42 	/**
43 	 * Creates a new (orphan) text widget and copies the text widget properties
44 	 * from a source text widget. This method doesn't copy any parent or child
45 	 * widgets.
46 	 * @param that Source text widget
47 	 */
48 	Text (const Text& that);
49 
50 	~Text ();
51 
52 	/**
53 	 * Assignment. Copies the text widget properties from a source text widget
54 	 *  and keeps its name and its position within the widget tree. Emits a
55 	 * BEvents::ExposeEvent if the text widget is visible.
56 	 * @param that Source text widget
57 	 */
58 	Text& operator= (const Text& that);
59 
60 	/**
61 	 * Pattern cloning. Creates a new instance of the widget and copies all
62 	 * its properties.
63 	 */
64 	virtual Widget* clone () const override;
65 
66 	/**
67 	 * Sets the output text.
68 	 * @param text Output text
69 	 */
70 	void setText (const std::string& text);
71 
72 	/**
73 	 * Gets the output text
74 	 * @return Output text
75 	 */
76 	std::string getText () const;
77 
78 	/**
79 	 * Sets the BColors::ColorSet for this widget
80 	 * @param colors Color set.
81 	 */
82 	void setTextColors (const BColors::ColorSet& colorset);
83 
84 	/**
85 	 * Gets (a pointer to) the BColors::ColorSet of this widget.
86 	 * @return Pointer to the color set.
87 	 */
88 	BColors::ColorSet* getTextColors ();
89 
90 	/**
91 	 * Sets the font for the text output.
92 	 * @param font Font
93 	 */
94 	void setFont (const BStyles::Font& font);
95 
96 	/**
97 	 * Gets (a pointer to) the font for the text output.
98 	 * @return Pointer to font
99 	 */
100 	BStyles::Font* getFont ();
101 
102 	/**
103 	 * Sets whether the widget resizes to the text block size or not.
104 	 * @param resizable True, if the widget resizes.
105 	 */
106 	void setYResizable (const bool resizable);
107 
108 	/**
109 	 * Gets whether the widget resizes to the text block size or not.
110 	 * @param return True, if the widget resizes.
111 	 */
112 	bool isYResizable () const;
113 
114 	/**
115 	 * Resizes the widget, redraw and emits a BEvents::ExposeEvent if the
116 	 * widget is visible.
117 	 * @param width New widgets width
118 	 */
119 	virtual void setWidth (const double width);
120 
121 	/**
122 	 * Resizes the widget, redraw and emits a BEvents::ExposeEvent if the
123 	 * widget is visible. If no parameters are given, the widget will be
124 	 * resized to the size of the containing child widgets.
125 	 * @param width		New widgets width
126 	 * @param height	New widgets height
127 	 * @param extends	New widget extends
128 	 */
129 	virtual void resize () override;
130 	virtual void resize (const double width, const double height) override;
131 	virtual void resize (const BUtilities::Point extends) override;
132 
133 	/**
134 	 * Scans theme for widget properties and applies these properties.
135 	 * @param theme Theme to be scanned.
136 	 * 				Styles used are:
137 	 * 				"textcolors" for BColors::ColorSet
138 	 * 				"font" for BStyles::Font
139 	 * @param name Name of the BStyles::StyleSet within the theme to be
140 	 * 		  	   applied.
141 	 */
142 	virtual void applyTheme (BStyles::Theme& theme) override;
143 	virtual void applyTheme (BStyles::Theme& theme, const std::string& name) override;
144 
145 	/**
146 	 * Gets a block (a vector) of text lines that fit into the widget output.
147 	 * If the widget is not resizable: the text is clipped, when lines exceed
148 	 * the widget height. If the widget is resizable, the whole text will be
149 	 * returned as a block of lines.
150 	 * @return	Vector of text lines
151 	 */
152 	std::vector<std::string> getTextBlock ();
153 
154 	/**
155 	 * Gets the height of a given text block as calculated using Cairo.
156 	 * @param textBlock Vector of text lines
157 	 * @return 			Text block height.
158 	 */
159 	double getTextBlockHeight (std::vector<std::string> textBlock);
160 
161 
162 protected:
163 	virtual void draw (const BUtilities::RectArea& area) override;
164 
165 	BColors::ColorSet textColors;
166 	BStyles::Font textFont;
167 	std::string textString;
168 	bool yResizable;
169 };
170 
171 }
172 
173 #endif /* BWIDGETS_TEXT_HPP_ */
174