text_block.h
1 /*
2 ** UICore
3 ** Copyright (c) 1997-2015 The UICore Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries UICore may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
29 #pragma once
30 
31 #include <memory>
32 #include "../../Core/Math/rect.h"
33 #include "../../Core/Math/size.h"
34 #include "../../Core/Math/color.h"
35 
36 namespace uicore
37 {
38  class Font;
39  typedef std::shared_ptr<Font> FontPtr;
40  class GraphicContext;
41  class Pointf;
42  class Sizef;
43  class Image;
44  typedef std::shared_ptr<Image> ImagePtr;
45  class Canvas;
46  typedef std::shared_ptr<Canvas> CanvasPtr;
47 
49  enum class SpanAlign
50  {
51  left,
52  right,
53  center,
54  justify
55  };
56 
59  {
60  public:
62  virtual Sizef size() const = 0;
63 
65  virtual void set_geometry(const Rectf &geometry) = 0;
66  };
67 
69  class TextBlock
70  {
71  public:
72  static std::shared_ptr<TextBlock> create();
73 
75  {
76  enum Type
77  {
84  };
85 
87  int object_id = -1;
88  int offset = 0;
89  };
90 
92  virtual void clear() = 0;
93 
95  virtual void add_text(const std::string &text, const FontPtr &font, const Colorf &color = Colorf::white, int id = -1) = 0;
96 
98  virtual void add_image(const ImagePtr &image, float baseline_offset = 0.0f, int id = -1) = 0;
99 
101  virtual void add_component(std::shared_ptr<SpanComponent> component, float baseline_offset = 0.0f, int id = -1) = 0;
102 
104  virtual void layout(const CanvasPtr &canvas, float max_width) = 0;
105 
107  virtual void set_position(const Pointf &pos) = 0;
108 
110  virtual Sizef size() const = 0;
111 
113  virtual Rectf rect() const = 0;
114 
116  virtual std::vector<Rectf> rect_by_id(int id) const = 0;
117 
119  virtual HitTestResult hit_test(const CanvasPtr &canvas, const Pointf &pos) = 0;
120 
122  virtual void draw_layout(const CanvasPtr &canvas) = 0;
123 
125  virtual void draw_layout_ellipsis(const CanvasPtr &canvas, const Rectf &content_rect) = 0;
126 
128  virtual void set_component_geometry() = 0;
129 
131  virtual Sizef find_preferred_size(const CanvasPtr &canvas) = 0;
132 
134  virtual void set_selection_range(std::string::size_type start, std::string::size_type end) = 0;
135 
137  virtual void set_selection_colors(const Colorf &foreground, const Colorf &background) = 0;
138 
140  virtual void show_cursor() = 0;
141 
143  virtual void hide_cursor() = 0;
144 
148  virtual void set_cursor_pos(std::string::size_type pos) = 0;
149 
153  virtual void set_cursor_overwrite_mode(bool enable) = 0;
154 
158  virtual void set_cursor_color(const Colorf &color) = 0;
159 
161  virtual std::string combined_text() const = 0;
162 
166  virtual void set_align(SpanAlign align) = 0;
167 
169  virtual float first_baseline_offset() = 0;
170 
172  virtual float last_baseline_offset() = 0;
173  };
174 
175  typedef std::shared_ptr<TextBlock> TextBlockPtr;
176 }
virtual Sizef find_preferred_size(const CanvasPtr &canvas)=0
Find the preferred size for the given layout.
virtual Sizef size() const =0
Returns the size of the layout.
virtual void set_selection_range(std::string::size_type start, std::string::size_type end)=0
Set selection range.
virtual void set_align(SpanAlign align)=0
Sets the text alignment.
virtual void add_image(const ImagePtr &image, float baseline_offset=0.0f, int id=-1)=0
Add image object.
static Colorf white
Definition: color.h:1207
std::shared_ptr< TextBlock > TextBlockPtr
Definition: text_block.h:175
2D (x,y) point structure - Float
Definition: point.h:68
virtual void set_component_geometry()=0
Set component geometry.
virtual void set_cursor_color(const Colorf &color)=0
Sets the cursor color.
Definition: text_block.h:83
virtual void layout(const CanvasPtr &canvas, float max_width)=0
Layout objects.
Floating point color description class (for float).
Definition: color.h:630
virtual Sizef size() const =0
Size of component.
static std::shared_ptr< TextBlock > create()
virtual void set_cursor_pos(std::string::size_type pos)=0
Sets the cursor position.
int object_id
Definition: text_block.h:87
virtual void show_cursor()=0
Shows the cursor caret.
Type type
Definition: text_block.h:86
virtual void set_cursor_overwrite_mode(bool enable)=0
Toggles whether the cursor caret is shown as a solid box or a line.
virtual std::vector< Rectf > rect_by_id(int id) const =0
Returns the geometry of the object with the given id.
SpanAlign
Span Align.
Definition: text_block.h:49
2D (width,height) size structure - Float
Definition: size.h:180
virtual void add_text(const std::string &text, const FontPtr &font, const Colorf &color=Colorf::white, int id=-1)=0
Add text object.
2D (left,top,right,bottom) rectangle structure - Float
Definition: rect.h:505
virtual Rectf rect() const =0
Returns the geometry of the layout.
Component placed in the layout.
Definition: text_block.h:58
std::shared_ptr< Font > FontPtr
Definition: path.h:39
std::shared_ptr< Image > ImagePtr
Definition: image.h:116
virtual std::string combined_text() const =0
Get all text.
Type
Definition: text_block.h:76
virtual void draw_layout(const CanvasPtr &canvas)=0
Draw layout.
Definition: text_block.h:74
virtual float last_baseline_offset()=0
Returns the baseline offset for the last baseline.
virtual void draw_layout_ellipsis(const CanvasPtr &canvas, const Rectf &content_rect)=0
Draw layout generating ellipsis for clipped text.
Span layout class.
Definition: text_block.h:69
virtual float first_baseline_offset()=0
Returns the baseline offset for the first baseline.
virtual void set_selection_colors(const Colorf &foreground, const Colorf &background)=0
Set selection colors.
virtual void add_component(std::shared_ptr< SpanComponent > component, float baseline_offset=0.0f, int id=-1)=0
Add component object.
std::shared_ptr< Canvas > CanvasPtr
Definition: canvas.h:126
virtual void set_geometry(const Rectf &geometry)=0
Sets the component geometry after layout.
virtual void clear()=0
Removes all objects.
virtual void hide_cursor()=0
Hides the cursor caret.
Definition: Application/application.h:35
virtual HitTestResult hit_test(const CanvasPtr &canvas, const Pointf &pos)=0
Hit test which object is located at the specified position.
virtual void set_position(const Pointf &pos)=0
Set position of layout.
int offset
Definition: text_block.h:88