GCC Middle and Back End API Reference
widget.h
Go to the documentation of this file.
1/* Hierarchical diagram elements.
2 Copyright (C) 2023-2025 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#ifndef GCC_TEXT_ART_WIDGET_H
22#define GCC_TEXT_ART_WIDGET_H
23
24#include "text-art/canvas.h"
25#include "text-art/table.h"
26
27namespace text_art {
28
29/* Abstract base class: something that knows how to size itself and
30 how to paint itself to a canvas, potentially with children, with
31 support for hierarchical sizing and positioning.
32
33 Widgets have a two-phase sizing/positioning algorithm.
34
35 Step 1: size requests: the root widget is asked for its size request i.e
36 how big it wants to be. This is handled by recursively asking child
37 widgets for their requested sizes. Each widget subclass can implement
38 their own logic for this in the "calc_req_size" vfunc, and the result
39 is cached in m_req_size.
40
41 Step 2: rect allocation: the root widget is set a canvas::rect_t as
42 its "allocated" rectangle. Each widget subclass can then place its
43 children recursively using the "update_child_alloc_rects" vfunc.
44 For simplicity, all coordinates in the hierarchy are within the same
45 coordinate system (rather than attempting to store per-child offsets).
46
47 Widget subclasses are responsible for managing their own children. */
48
49/* Subclasses in this header, with indentation indicating inheritance. */
50
51class widget; /* Abstract base class. */
52 class wrapper_widget; /* Concrete subclass: a widget with a single child. */
53 class container_widget; /* Abstract subclass: widgets with an arbitrary
54 number of children. */
55 class vbox_widget; /* Concrete widget subclass: lay out children
56 vertically. */
57 class leaf_widget; /* Abstract subclass: a widget with no children. */
58 class text_widget; /* Concrete subclass: a text string. */
59 class canvas_widget; /* Concrete subclass: a pre-rendered canvas. */
60
61class widget
62{
63 public:
64 /* This can be very useful for debugging when implementing new
65 widget subclasses. */
66 static const bool DEBUG_GEOMETRY = false;
67
68 virtual ~widget () {}
69
70 canvas to_canvas (const style_manager &style_mgr);
71
73 {
76 fprintf (stderr, "calc_req_size (%s) -> (w:%i, h:%i)\n",
77 get_desc (),
79 return m_req_size;
80 }
81
83 {
85 fprintf (stderr, "set_alloc_rect (%s): ((x:%i, y:%i), (w:%i, h:%i))\n",
86 get_desc (),
88 rect.m_size.w, rect.m_size.h);
91 }
92
93 virtual const char *get_desc () const = 0;
95 virtual void update_child_alloc_rects () = 0;
96 virtual void paint_to_canvas (canvas &canvas) = 0;
97
98 /* Access to the cached size request of this widget. */
99 const canvas::size_t get_req_size () const { return m_req_size; }
100 int get_req_w () const { return m_req_size.w; }
101 int get_req_h () const { return m_req_size.h; }
102
103 /* Access to the allocated canvas coordinates of this widget. */
104 const canvas::rect_t &get_alloc_rect () const { return m_alloc_rect; }
105 int get_alloc_w () const { return m_alloc_rect.get_width (); }
106 int get_alloc_h () const { return m_alloc_rect.get_height (); }
107 int get_min_x () const { return m_alloc_rect.get_min_x (); }
108 int get_max_x () const { return m_alloc_rect.get_max_x (); }
109 int get_next_x () const { return m_alloc_rect.get_next_x (); }
110 int get_min_y () const { return m_alloc_rect.get_min_y (); }
111 int get_max_y () const { return m_alloc_rect.get_max_y (); }
112 int get_next_y () const { return m_alloc_rect.get_max_y (); }
113 canvas::range_t get_x_range () const { return m_alloc_rect.get_x_range (); }
114 canvas::range_t get_y_range () const { return m_alloc_rect.get_y_range (); }
116 {
117 return m_alloc_rect.m_top_left;
118 }
119
120 protected:
122 : m_req_size (0, 0),
123 m_alloc_rect (canvas::coord_t (0, 0),
124 canvas::size_t (0, 0))
125 {}
126
127private:
128 /* How much size this widget requested. */
130 /* Where (and how big) this widget was allocated. */
132};
133
134/* Concrete subclass for a widget with a single child. */
135
136class wrapper_widget : public widget
137{
138 public:
139 wrapper_widget (std::unique_ptr<widget> child)
140 : m_child (std::move (child))
141 {}
142
143 const char *get_desc () const override
144 {
145 return "wrapper_widget";
146 }
148 {
149 if (m_child)
150 return m_child->get_req_size ();
151 else
152 return canvas::size_t (0,0);
153 }
155 {
156 if (m_child)
157 m_child->set_alloc_rect (get_alloc_rect ());
158 }
160 {
161 if (m_child)
162 m_child->paint_to_canvas (canvas);
163 }
164 private:
165 std::unique_ptr<widget> m_child;
166};
167
168/* Abstract subclass for widgets with an arbitrary number of children. */
169
171{
172 public:
173 void add_child (std::unique_ptr<widget> child)
174 {
175 m_children.push_back (std::move (child));
176 }
177
178 void paint_to_canvas (canvas &canvas) final override
179 {
180 for (auto &child : m_children)
181 child->paint_to_canvas (canvas);
182 }
183
184 protected:
185 std::vector<std::unique_ptr<widget>> m_children;
186};
187
188/* Concrete widget subclass: lay out children vertically. */
189
191{
192 public:
193 const char *get_desc () const override;
194 canvas::size_t calc_req_size () override;
195 void update_child_alloc_rects () final override;
196};
197
198/* Abstract subclass for widgets with no children. */
199
200class leaf_widget : public widget
201{
202 public:
204 {
205 /* no-op. */
206 }
207
208 protected:
210};
211
212/* Concrete widget subclass for a text string. */
213
215{
216 public:
218 : leaf_widget (), m_str (std::move (str))
219 {
220 }
221
222 const char *get_desc () const override;
224 void paint_to_canvas (canvas &canvas) final override;
225
226private:
228};
229
230/* Concrete widget subclass for a pre-rendered canvas. */
231
233{
234 public:
236 : leaf_widget (), m_canvas (std::move (c))
237 {
238 }
239
240 const char *get_desc () const override;
242 void paint_to_canvas (canvas &canvas) final override;
243
244private:
246};
247
248} // namespace text_art
249
250#endif /* GCC_TEXT_ART_WIDGET_H */
Definition widget.h:233
canvas_widget(canvas &&c)
Definition widget.h:235
canvas m_canvas
Definition widget.h:245
Definition canvas.h:38
range< class canvas > range_t
Definition canvas.h:43
rect< class canvas > rect_t
Definition canvas.h:44
size< class canvas > size_t
Definition canvas.h:41
coord< class canvas > coord_t
Definition canvas.h:42
Definition widget.h:171
void add_child(std::unique_ptr< widget > child)
Definition widget.h:173
std::vector< std::unique_ptr< widget > > m_children
Definition widget.h:185
void paint_to_canvas(canvas &canvas) final override
Definition widget.h:178
Definition widget.h:201
leaf_widget()
Definition widget.h:209
void update_child_alloc_rects() final override
Definition widget.h:203
Definition types.h:342
Definition types.h:424
Definition widget.h:215
canvas::size_t calc_req_size() final override
Definition widget.cc:95
styled_string m_str
Definition widget.h:227
const char * get_desc() const override
Definition widget.cc:89
text_widget(styled_string str)
Definition widget.h:217
void paint_to_canvas(canvas &canvas) final override
Definition widget.cc:101
Definition widget.h:191
canvas::size_t calc_req_size() override
Definition widget.cc:59
const char * get_desc() const override
Definition widget.cc:53
void update_child_alloc_rects() final override
Definition widget.cc:72
Definition widget.h:62
int get_max_y() const
Definition widget.h:111
int get_alloc_w() const
Definition widget.h:105
canvas::range_t get_y_range() const
Definition widget.h:114
canvas to_canvas(const style_manager &style_mgr)
Definition widget.cc:35
canvas::rect_t m_alloc_rect
Definition widget.h:131
static const bool DEBUG_GEOMETRY
Definition widget.h:66
canvas::range_t get_x_range() const
Definition widget.h:113
virtual void update_child_alloc_rects()=0
int get_next_y() const
Definition widget.h:112
int get_req_h() const
Definition widget.h:101
canvas::size_t m_req_size
Definition widget.h:129
int get_next_x() const
Definition widget.h:109
int get_max_x() const
Definition widget.h:108
virtual const char * get_desc() const =0
virtual void paint_to_canvas(canvas &canvas)=0
int get_min_x() const
Definition widget.h:107
const canvas::rect_t & get_alloc_rect() const
Definition widget.h:104
virtual canvas::size_t calc_req_size()=0
const canvas::size_t get_req_size() const
Definition widget.h:99
int get_req_w() const
Definition widget.h:100
virtual ~widget()
Definition widget.h:68
canvas::size_t get_req_size()
Definition widget.h:72
int get_alloc_h() const
Definition widget.h:106
void set_alloc_rect(const canvas::rect_t &rect)
Definition widget.h:82
int get_min_y() const
Definition widget.h:110
widget()
Definition widget.h:121
const canvas::coord_t & get_top_left() const
Definition widget.h:115
Definition widget.h:137
wrapper_widget(std::unique_ptr< widget > child)
Definition widget.h:139
void update_child_alloc_rects() override
Definition widget.h:154
canvas::size_t calc_req_size() override
Definition widget.h:147
std::unique_ptr< widget > m_child
Definition widget.h:165
void paint_to_canvas(canvas &canvas) override
Definition widget.h:159
const char * get_desc() const override
Definition widget.h:143
void final(rtx_insn *first, FILE *file, int optimize_p)
Definition final.cc:2009
Definition diagnostics/context.h:56
Definition ira-emit.cc:158
Definition types.h:94
size< CoordinateSystem > m_size
Definition types.h:129
coord< CoordinateSystem > m_top_left
Definition types.h:128