Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::gui::Widget Class Reference

A graphical object that can be drawn on the screen. More...

#include <widget.h>

Inheritance diagram for MaxOS::gui::Widget:
MaxOS::drivers::peripherals::KeyboardEventHandler MaxOS::common::EventHandler< KeyboardEvents > MaxOS::gui::CompositeWidget MaxOS::gui::widgets::Button MaxOS::gui::widgets::InputBox MaxOS::gui::widgets::Text MaxOS::gui::Desktop MaxOS::gui::Window

Public Member Functions

 Widget ()
 Construct a new Widget object.
 
 Widget (int32_t left, int32_t top, uint32_t width, uint32_t height)
 Construct a new Widget object at a specific position and size.
 
virtual void draw (common::GraphicsContext *gc, common::Rectangle< int32_t > &area)
 Draw the widget on the screen.
 
void invalidate ()
 Invalidates the entire widget. This forces the widget to be redrawn on the next screen update.
 
virtual void invalidate (common::Rectangle< int32_t > &area)
 Invalidates a specific area of the widget. This forces that part of the screen to be redrawn on the next screen update.
 
virtual void add_child (Widget *child)
 Set the parent of a widget to this widget, making it into a child.
 
virtual common::Coordinates absolute_coordinates (common::Coordinates coordinates)
 Get the absolute coordinates of the widget (relative to the screen)
 
virtual bool contains_coordinate (uint32_t x, uint32_t y)
 Check if the widget contains a specific coordinate.
 
common::Rectangle< int32_tposition ()
 Get the position of the widget.
 
void move (int32_t left, int32_t top)
 Set the position of the widget, and invalidate the old and new positions so they are redrawn.
 
void resize (int32_t width, int32_t height)
 Set the size of the widget, and invalidate the old and new positions so they are redrawn.
 
void focus ()
 Set the current focused widget to be this widget.
 
virtual void on_focus ()
 Handles the event when the widget is focussed.
 
virtual void on_focus_lost ()
 Handles the event when the widget is unfocused.
 
void bring_to_front ()
 Brings this widget to the front of the screen.
 
virtual void on_mouse_enter_widget (uint32_t to_x, uint32_t to_y)
 Handles the event when the mouse is moved on to the widget.
 
virtual void on_mouse_leave_widget (uint32_t from_x, uint32_t from_y)
 Handles the event when the mouse is moved out of the widget.
 
virtual void on_mouse_move_widget (uint32_t from_x, uint32_t from_y, uint32_t to_x, uint32_t to_y)
 Handles the event when the mouse is moved over the widget.
 
virtual drivers::peripherals::MouseEventHandleron_mouse_button_pressed (uint32_t x, uint32_t y, uint8_t button)
 Handles the event when the mouse is pressed on the widget.
 
virtual void on_mouse_button_released (uint32_t x, uint32_t y, uint8_t button)
 Handles the event when the mouse is released on the widget.
 
- Public Member Functions inherited from MaxOS::drivers::peripherals::KeyboardEventHandler
common::Event< KeyboardEvents > * on_event (common::Event< KeyboardEvents > *) override
 Handle the trigger of an event.
 
virtual void on_key_down (KeyCode, const KeyboardState &)
 Handle the key down event.
 
virtual void on_key_up (KeyCode, const KeyboardState &)
 Handle the key up event.
 
- Public Member Functions inherited from MaxOS::common::EventHandler< KeyboardEvents >
virtual Event< KeyboardEvents > * on_event (Event< KeyboardEvents > *event)
 This function is called when an event is raised.
 

Protected Member Functions

virtual void set_focus (Widget *)
 Sets the widget that is currently focussed.
 
virtual void bring_to_front (Widget *)
 Brings a specific widget to the front of the screen.
 

Protected Attributes

common::Rectangle< int32_t > m_position
 The position and size of the widget (relative to its parent)
 
Widgetm_parent { nullptr }
 The widget that owns this widget.
 
bool m_valid { false }
 Is the widget drawn to the screen and up to date.
 
uint32_t m_min_width { 5 }
 The smallest m_width the widget can be resized to.
 
uint32_t m_min_height { 5 }
 The smallest m_height the widget can be resized to.
 
uint32_t m_max_width { 0x8FFFFFFF }
 The largest m_width the widget can be resized to.
 
uint32_t m_max_height { 0x8FFFFFFF }
 The largest m_height the widget can be resized to.
 

Friends

template<int Left, int Top, int Width, int Height>
class WidgetMoverResizer
 
class CompositeWidget
 

Detailed Description

A graphical object that can be drawn on the screen.

Definition at line 29 of file widget.h.

Constructor & Destructor Documentation

◆ Widget() [1/2]

Widget::Widget ( )

Construct a new Widget object.

Definition at line 19 of file widget.cpp.

21{
22
23}
Handles the events that are triggered by the Keyboard Driver.
Definition keyboard.h:274

◆ Widget() [2/2]

Widget::Widget ( int32_t  left,
int32_t  top,
uint32_t  width,
uint32_t  height 
)

Construct a new Widget object at a specific position and size.

Parameters
leftThe left position of the widget relative to its parent
topThe top position of the widget relative to its parent
widthThe width of the widget
heightThe height of the widget

Definition at line 33 of file widget.cpp.

35 m_position(left, top, width, height)
36{
37
38}
common::Rectangle< int32_t > m_position
The position and size of the widget (relative to its parent)
Definition widget.h:35

Member Function Documentation

◆ absolute_coordinates()

Coordinates Widget::absolute_coordinates ( common::Coordinates  coordinates)
virtual

Get the absolute coordinates of the widget (relative to the screen)

Parameters
coordinatesThe coordinates within the widget to convert
Returns
The absolute coordinates of the widget

Definition at line 98 of file widget.cpp.

98 {
99
100 // Return the parents absolute coordinates
101 if (m_parent != nullptr)
103
104 // If the widget has no m_parent, return the coordinates of the widget
105 return {coordinates.first + m_position.left, coordinates.second + m_position.top};
106
107}
A pair of two objects.
Definition pair.h:22
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Type top
The top coordinate of the rectangle.
Definition rectangle.h:25
Type left
The left coordinate of the rectangle.
Definition rectangle.h:24
virtual common::Coordinates absolute_coordinates(common::Coordinates coordinates)
Get the absolute coordinates of the widget (relative to the screen)
Definition widget.cpp:98
Widget * m_parent
The widget that owns this widget.
Definition widget.h:37

References absolute_coordinates(), MaxOS::common::Rectangle< Type >::left, m_parent, m_position, and MaxOS::common::Rectangle< Type >::top.

Referenced by absolute_coordinates(), MaxOS::gui::widgets::Button::draw(), MaxOS::gui::widgets::InputBox::draw(), MaxOS::gui::widgets::Text::draw(), MaxOS::gui::Window::draw_self(), and invalidate().

◆ add_child()

void Widget::add_child ( Widget child)
virtual

Set the parent of a widget to this widget, making it into a child.

Parameters
childThe child to add

Reimplemented in MaxOS::gui::Desktop, MaxOS::gui::CompositeWidget, and MaxOS::gui::Window.

Definition at line 85 of file widget.cpp.

85 {
86
87 // Parent the child to this widget
88 child->m_parent = this;
89
90}

Referenced by MaxOS::gui::CompositeWidget::add_child().

◆ bring_to_front() [1/2]

void Widget::bring_to_front ( )

Brings this widget to the front of the screen.

Definition at line 230 of file widget.cpp.

230 {
231
232 // Bring this widget to the front of the screen
233 bring_to_front(this);
234}
void bring_to_front()
Brings this widget to the front of the screen.
Definition widget.cpp:230

References bring_to_front().

Referenced by bring_to_front(), on_mouse_button_pressed(), and MaxOS::gui::Window::on_mouse_button_pressed().

◆ bring_to_front() [2/2]

void Widget::bring_to_front ( Widget widget)
protectedvirtual

Brings a specific widget to the front of the screen.

Parameters
widgetThe widget to bring to the front

Reimplemented in MaxOS::gui::Desktop.

Definition at line 241 of file widget.cpp.

241 {
242
243 // Bring the parent to the front of the screen
244 if (m_parent != nullptr)
246
247}
virtual void bring_to_front(Widget *)
Brings a specific widget to the front of the screen.
Definition widget.cpp:241

References bring_to_front(), and m_parent.

Referenced by bring_to_front().

◆ contains_coordinate()

bool Widget::contains_coordinate ( uint32_t  x,
uint32_t  y 
)
virtual

Check if the widget contains a specific coordinate.

Parameters
xThe x coordinate
yThe y coordinate
Returns
True if the widget contains the coordinate, false if it does not

Definition at line 116 of file widget.cpp.

116 {
117
118 // Check if the coordinates are within the bounds of the widget
119 return m_position.contains(x, y);
120}
bool contains(const Rectangle< Type > &)
Definition rectangle.h:192

References MaxOS::common::Rectangle< Type >::contains(), and m_position.

◆ draw()

void Widget::draw ( common::GraphicsContext gc,
common::Rectangle< int32_t > &  area 
)
virtual

Draw the widget on the screen.

Parameters
gcThe graphics context to draw the widgets pixels on
areaThe area of the widget to draw

Reimplemented in MaxOS::gui::CompositeWidget, MaxOS::gui::widgets::Button, MaxOS::gui::widgets::InputBox, and MaxOS::gui::widgets::Text.

Definition at line 48 of file widget.cpp.

48 {
49
50}

Referenced by MaxOS::gui::widgets::Button::draw(), MaxOS::gui::widgets::InputBox::draw(), MaxOS::gui::widgets::Text::draw(), and MaxOS::gui::CompositeWidget::draw().

◆ focus()

void Widget::focus ( )

Set the current focused widget to be this widget.

Definition at line 195 of file widget.cpp.

195 {
196
197 // Set the focus the widget to this widget
198 set_focus(this);
199}
virtual void set_focus(Widget *)
Sets the widget that is currently focussed.
Definition widget.cpp:206

References set_focus().

Referenced by on_mouse_button_pressed().

◆ invalidate() [1/2]

void Widget::invalidate ( )

Invalidates the entire widget. This forces the widget to be redrawn on the next screen update.

Definition at line 55 of file widget.cpp.

55 {
56
57 // Convert the relative coordinates to absolute coordinates
59
60 // Create a rectangle with the absolute coordinates and the size of the widget
62
63 // Invalidate the area
65
66}
Type height
The height of the rectangle.
Definition rectangle.h:27
Type width
The width of the rectangle.
Definition rectangle.h:26
void invalidate()
Invalidates the entire widget. This forces the widget to be redrawn on the next screen update.
Definition widget.cpp:55

References absolute_coordinates(), MaxOS::common::Rectangle< Type >::height, invalidate(), m_position, and MaxOS::common::Rectangle< Type >::width.

Referenced by MaxOS::gui::Desktop::Desktop(), MaxOS::gui::Desktop::internal_invalidate(), invalidate(), invalidate(), move(), MaxOS::gui::widgets::InputBox::on_focus(), MaxOS::gui::widgets::InputBox::on_focus_lost(), MaxOS::gui::widgets::InputBox::on_key_down(), MaxOS::gui::widgets::Button::on_mouse_button_pressed(), MaxOS::gui::widgets::Button::on_mouse_button_released(), resize(), MaxOS::gui::widgets::InputBox::update_text(), and MaxOS::gui::widgets::Text::update_text().

◆ invalidate() [2/2]

void Widget::invalidate ( common::Rectangle< int32_t > &  area)
virtual

Invalidates a specific area of the widget. This forces that part of the screen to be redrawn on the next screen update.

Parameters
areaThe area of the widget to invalidate

Reimplemented in MaxOS::gui::Desktop.

Definition at line 73 of file widget.cpp.

73 {
74
75 // If the widget has a parent, invalidate the area of the parent
76 if (m_parent != nullptr)
78}

References invalidate(), and m_parent.

◆ move()

void Widget::move ( int32_t  left,
int32_t  top 
)

Set the position of the widget, and invalidate the old and new positions so they are redrawn.

Parameters
leftThe new left position of the rectangle
topThe new top position of the rectangle

Definition at line 138 of file widget.cpp.

138 {
139
140 // Invalidate the old position
141 invalidate();
142
143 // Set the new position
144 m_position.left = left;
145 m_position.top = top;
146
147 // Re draw the widget in the new position
148 invalidate();
149}

References invalidate(), MaxOS::common::Rectangle< Type >::left, m_position, and MaxOS::common::Rectangle< Type >::top.

Referenced by MaxOS::gui::WidgetMoverResizer< Left, Top, Width, Height >::on_mouse_move_event().

◆ on_focus()

void Widget::on_focus ( )
virtual

Handles the event when the widget is focussed.

Reimplemented in MaxOS::gui::widgets::InputBox.

Definition at line 216 of file widget.cpp.

216 {
217
218}

Referenced by MaxOS::gui::Desktop::set_focus().

◆ on_focus_lost()

void Widget::on_focus_lost ( )
virtual

Handles the event when the widget is unfocused.

Reimplemented in MaxOS::gui::widgets::InputBox.

Definition at line 223 of file widget.cpp.

223 {
224
225}

Referenced by MaxOS::gui::Desktop::set_focus().

◆ on_mouse_button_pressed()

peripherals::MouseEventHandler * Widget::on_mouse_button_pressed ( uint32_t  x,
uint32_t  y,
uint8_t  button 
)
virtual

Handles the event when the mouse is pressed on the widget.

Parameters
xThe x coordinate of the mouse when it was pressed
yThe y coordinate of the mouse when it was pressed
buttonThe button that was pressed
Returns
nullptr

Reimplemented in MaxOS::gui::CompositeWidget, MaxOS::gui::widgets::Button, and MaxOS::gui::Window.

Definition at line 289 of file widget.cpp.

289 {
290
291 // Bring the widget to the front of the screen
293
294 // Focus the widget
295 focus();
296
297 // Return 0 as the event has been handled
298 return nullptr;
299}
void focus()
Set the current focused widget to be this widget.
Definition widget.cpp:195

References bring_to_front(), and focus().

Referenced by MaxOS::gui::widgets::Button::on_mouse_button_pressed().

◆ on_mouse_button_released()

void Widget::on_mouse_button_released ( uint32_t  x,
uint32_t  y,
uint8_t  button 
)
virtual

Handles the event when the mouse is released on the widget.

Parameters
xThe x coordinate of the mouse when it was released
yThe y coordinate of the mouse when it was released
buttonThe button that was released

Reimplemented in MaxOS::gui::CompositeWidget, and MaxOS::gui::widgets::Button.

Definition at line 308 of file widget.cpp.

308 {
309
310}

Referenced by MaxOS::gui::widgets::Button::on_mouse_button_released().

◆ on_mouse_enter_widget()

void Widget::on_mouse_enter_widget ( uint32_t  to_x,
uint32_t  to_y 
)
virtual

Handles the event when the mouse is moved on to the widget.

Parameters
to_xThe x coordinate of the mouse
to_yThe y coordinate of the mouse

Reimplemented in MaxOS::gui::CompositeWidget.

Definition at line 255 of file widget.cpp.

255 {
256
257}

◆ on_mouse_leave_widget()

void Widget::on_mouse_leave_widget ( uint32_t  from_x,
uint32_t  from_y 
)
virtual

Handles the event when the mouse is moved out of the widget.

Parameters
from_xThe x coordinate of the mouse
from_yThe y coordinate of the mouse

Reimplemented in MaxOS::gui::CompositeWidget.

Definition at line 265 of file widget.cpp.

265 {
266
267}

◆ on_mouse_move_widget()

void Widget::on_mouse_move_widget ( uint32_t  from_x,
uint32_t  from_y,
uint32_t  to_x,
uint32_t  to_y 
)
virtual

Handles the event when the mouse is moved over the widget.

Parameters
from_xThe x original coordinate of the mouse
from_yThe y original coordinate of the mouse
to_xThe x new coordinate of the mouse
to_yThe y new coordinate of the mouse

Reimplemented in MaxOS::gui::CompositeWidget.

Definition at line 277 of file widget.cpp.

277 {
278
279}

◆ position()

◆ resize()

void Widget::resize ( int32_t  width,
int32_t  height 
)

Set the size of the widget, and invalidate the old and new positions so they are redrawn.

Parameters
widthThe new m_width of the rectangle
heightThe new m_height of the rectangle
Todo:
Fix the invalidation so it only redraws the areas that need to be redrawn (right and bottom are always fully redrawn currently)

Definition at line 159 of file widget.cpp.

159 {
160
161 // Restrict the width and height to the minimum and maximum values
162 if (width < (int) m_min_width) width = m_min_width;
163 if (height < (int) m_min_height) height = m_min_height;
164 if (width > (int) m_max_width) width = m_max_width;
165 if (height > (int) m_max_height) height = m_max_height;
166
167 // Store the old position, set the new position
169 m_position.width = width;
170 m_position.height = height;
171
172 // Find the areas that need to be redrawn by subtracting the old position from the new position, and vice versa
175
176 // Right and Bottom require to be fully invalidated
179 invalidate();
180 return;
181 }
182
183 //Loop through the areas that need to be redrawn and invalidate them
184 for (auto& area: invalid_areas_old)
186
187 for (auto& area: invalid_areas_new)
189
190}
Vector< Rectangle< Type > > subtract(const Rectangle< Type > &)
Definition rectangle.h:142
uint32_t m_min_width
The smallest m_width the widget can be resized to.
Definition widget.h:40
uint32_t m_max_width
The largest m_width the widget can be resized to.
Definition widget.h:43
uint32_t m_max_height
The largest m_height the widget can be resized to.
Definition widget.h:44
uint32_t m_min_height
The smallest m_height the widget can be resized to.
Definition widget.h:41

References MaxOS::common::Rectangle< Type >::height, invalidate(), m_max_height, m_max_width, m_min_height, m_min_width, m_position, MaxOS::common::Rectangle< Type >::subtract(), and MaxOS::common::Rectangle< Type >::width.

Referenced by MaxOS::gui::WidgetMoverResizer< Left, Top, Width, Height >::on_mouse_move_event().

◆ set_focus()

void Widget::set_focus ( Widget widget)
protectedvirtual

Sets the widget that is currently focussed.

Parameters
widgetThe widget to set as focussed

Reimplemented in MaxOS::gui::Desktop.

Definition at line 206 of file widget.cpp.

206 {
207
208 // Focus the parent to this widget
209 if (m_parent != nullptr)
211}

References m_parent, and set_focus().

Referenced by focus(), and set_focus().

Friends And Related Symbol Documentation

◆ CompositeWidget

friend class CompositeWidget
friend

Definition at line 32 of file widget.h.

◆ WidgetMoverResizer

template<int Left, int Top, int Width, int Height>
friend class WidgetMoverResizer
friend

Definition at line 30 of file widget.h.

Member Data Documentation

◆ m_max_height

uint32_t MaxOS::gui::Widget::m_max_height { 0x8FFFFFFF }
protected

The largest m_height the widget can be resized to.

Definition at line 44 of file widget.h.

44{ 0x8FFFFFFF };

Referenced by resize().

◆ m_max_width

uint32_t MaxOS::gui::Widget::m_max_width { 0x8FFFFFFF }
protected

The largest m_width the widget can be resized to.

Definition at line 43 of file widget.h.

43{ 0x8FFFFFFF };

Referenced by resize().

◆ m_min_height

uint32_t MaxOS::gui::Widget::m_min_height { 5 }
protected

The smallest m_height the widget can be resized to.

Definition at line 41 of file widget.h.

41{ 5 };

Referenced by resize(), MaxOS::gui::Window::Window(), and MaxOS::gui::Window::Window().

◆ m_min_width

uint32_t MaxOS::gui::Widget::m_min_width { 5 }
protected

The smallest m_width the widget can be resized to.

Definition at line 40 of file widget.h.

40{ 5 };

Referenced by resize(), MaxOS::gui::Window::Window(), and MaxOS::gui::Window::Window().

◆ m_parent

Widget* MaxOS::gui::Widget::m_parent { nullptr }
protected

The widget that owns this widget.

Definition at line 37 of file widget.h.

37{ nullptr };

Referenced by absolute_coordinates(), bring_to_front(), invalidate(), and set_focus().

◆ m_position

common::Rectangle<int32_t> MaxOS::gui::Widget::m_position
protected

The position and size of the widget (relative to its parent)

Definition at line 35 of file widget.h.

Referenced by absolute_coordinates(), contains_coordinate(), invalidate(), move(), MaxOS::gui::WidgetMoverResizer< Left, Top, Width, Height >::on_mouse_move_event(), position(), and resize().

◆ m_valid

bool MaxOS::gui::Widget::m_valid { false }
protected

Is the widget drawn to the screen and up to date.

Definition at line 38 of file widget.h.

38{ false };

The documentation for this class was generated from the following files: