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

A window that can be moved and resized and contains a widget. More...

#include <window.h>

Inheritance diagram for MaxOS::gui::Window:
MaxOS::gui::CompositeWidget MaxOS::gui::Widget MaxOS::drivers::peripherals::KeyboardEventHandler MaxOS::common::EventHandler< KeyboardEvents >

Public Member Functions

 Window (int32_t left, int32_t top, uint32_t width, uint32_t height, const string &title_text)
 Construct a new Window object at a specific position and size (relative to the screen) with a title.
 
 Window (Widget *contained_widget, const string &title_text)
 Construct a new Window object that contains a specific widget with a title. The window sizes itself to fit the widget.
 
void draw_self (common::GraphicsContext *gc, common::Rectangle< int32_t > &area) override
 Draws the window and its children.
 
void add_child (Widget *child) override
 Adds a child to the window.
 
drivers::peripherals::MouseEventHandleron_mouse_button_pressed (uint32_t x, uint32_t y, uint8_t button) override
 Handles the mouse button being pressed.
 
- Public Member Functions inherited from MaxOS::gui::CompositeWidget
 CompositeWidget (int32_t left, int32_t top, uint32_t width, uint32_t height)
 Construct a new Composite Widget object at a specific position and size.
 
void draw (common::GraphicsContext *gc, common::Rectangle< int32_t > &area) override
 Draws a section of the widget and its m_children.
 
void on_mouse_enter_widget (uint32_t to_x, uint32_t to_y) override
 Passes the event to the child that the mouse is over. (Event handling should be done by the derived class)
 
void on_mouse_leave_widget (uint32_t from_x, uint32_t from_y) override
 Passes the event to the child that the mouse is over. (Event handling should be done by the derived class)
 
void on_mouse_move_widget (uint32_t from_x, uint32_t from_y, uint32_t to_x, uint32_t to_y) override
 Passes the event to the child that the mouse is over, also generates a leave/enter event for children.
 
void on_mouse_button_released (uint32_t x, uint32_t y, uint8_t button) override
 Passes the event to the child that the mouse is over. (Event handling should be done by the derived class)
 
- Public Member Functions inherited from MaxOS::gui::Widget
 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.
 
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 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.
 
- 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.
 

Public Attributes

uint8_t frame_thickness { 5 }
 How thick the bar around the window is.
 
uint8_t title_bar_height { 10 }
 How large the bar at the top of the window is.
 
common::Colour area_colour
 The background colour of the window inner area.
 
common::Colour frame_colour
 The colour of the bar around the window.
 
common::Colour frame_border_colour
 The colour of the border around the bar around the window.
 

Additional Inherited Members

- Protected Member Functions inherited from MaxOS::gui::CompositeWidget
void draw (common::GraphicsContext *gc, common::Rectangle< int32_t > &area, common::Vector< Widget * >::iterator start)
 Draws a section of the widget and the m_children after a specific child.
 
- Protected Member Functions inherited from MaxOS::gui::Widget
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 inherited from MaxOS::gui::CompositeWidget
common::Vector< Widget * > m_children
 Widgets contained within this composite widget.
 
- Protected Attributes inherited from MaxOS::gui::Widget
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.
 

Detailed Description

A window that can be moved and resized and contains a widget.

Definition at line 24 of file window.h.

Constructor & Destructor Documentation

◆ Window() [1/2]

Window::Window ( int32_t  left,
int32_t  top,
uint32_t  width,
uint32_t  height,
const string title_text 
)

Construct a new Window object at a specific position and size (relative to the screen) with a title.

Parameters
leftHow many pixels from the left of the screen the window is
topHow many pixels from the top of the screen the window is
widthHow many pixels wide the window is
heightHow many pixels tall the window is
title_textThe text to display in the title bar of the window

Definition at line 27 of file window.cpp.

28: CompositeWidget(left, top, width, height),
29 m_title(0, -(10 + 5) + 2, width - 2 * 5, 10 + 5 - 3, title_text),
30 m_mover(this),
31 m_resizer_top(this),
32 m_resizer_bottom(this),
33 m_resizer_left(this),
34 m_resizer_right(this),
35 m_resizer_top_left(this),
36 m_resizer_top_right(this),
37 m_resizer_bottom_left(this),
38 m_resizer_bottom_right(this)
39{
40
41 // Set the sizing
44
45 // Set the colours
46 area_colour = Colour(0xff, 0xff, 0xff); // White
47 frame_border_colour = Colour(0x00, 0x00, 0x00); // Black
48 frame_colour = Colour(0x57, 0x57, 0x57); // Davy's Grey
49 m_title.foreground_colour = Colour(0xff, 0xff, 0xff); // White
51
52 // Add the title to the window
53 Window::add_child(&m_title);
54
55}
Stores the red, green, blue and alpha values of a colour. Can be parsed from a hex string,...
Definition colour.h:91
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
A widget that can contain other widgets.
Definition widget.h:88
uint32_t m_min_width
The smallest m_width the widget can be resized to.
Definition widget.h:40
uint32_t m_min_height
The smallest m_height the widget can be resized to.
Definition widget.h:41
uint8_t title_bar_height
How large the bar at the top of the window is.
Definition window.h:44
uint8_t frame_thickness
How thick the bar around the window is.
Definition window.h:43
common::Colour area_colour
The background colour of the window inner area.
Definition window.h:46
common::Colour frame_colour
The colour of the bar around the window.
Definition window.h:47
void add_child(Widget *child) override
Adds a child to the window.
Definition window.cpp:220
common::Colour frame_border_colour
The colour of the border around the bar around the window.
Definition window.h:48
common::Colour foreground_colour
The colour of the text characters.
Definition text.h:33
common::Colour background_colour
The colour to draw behind the text characters.
Definition text.h:34

References add_child(), area_colour, MaxOS::gui::widgets::Text::background_colour, MaxOS::gui::widgets::Text::foreground_colour, frame_border_colour, frame_colour, frame_thickness, MaxOS::gui::Widget::m_min_height, MaxOS::gui::Widget::m_min_width, and title_bar_height.

◆ Window() [2/2]

Window::Window ( Widget contained_widget,
const string title_text 
)

Construct a new Window object that contains a specific widget with a title. The window sizes itself to fit the widget.

Parameters
contained_widgetThe widget to contain within the window
title_textThe text to display in the title bar of the window

Definition at line 63 of file window.cpp.

64: CompositeWidget(0, 0, contained_widget->position().width + 2 * 5 + 2, contained_widget->position().height + 2 * 5 + 10 + 2),
65 m_title(0, -(10 + 5) + 2, contained_widget->position().width, 10 + 5 - 3, title_text),
66 m_mover(this),
67 m_resizer_top(this),
68 m_resizer_bottom(this),
69 m_resizer_left(this),
70 m_resizer_right(this),
71 m_resizer_top_left(this),
72 m_resizer_top_right(this),
73 m_resizer_bottom_left(this),
74 m_resizer_bottom_right(this)
75{
76
77 // Set the sizing
80
81 // Set the colours
82 area_colour = Colour(0xff, 0xff, 0xff); // White
83 frame_border_colour = Colour(0x00, 0x00, 0x00); // Black
84 frame_colour = Colour(0x57, 0x57, 0x57); // Davy's Grey
85 m_title.foreground_colour = Colour(0xff, 0xff, 0xff); // White
87
88 // Add the title to the window
89 Window::add_child(&m_title);
91}
Type height
The height of the rectangle.
Definition rectangle.h:27
Type width
The width of the rectangle.
Definition rectangle.h:26

References add_child(), area_colour, MaxOS::gui::widgets::Text::background_colour, MaxOS::gui::widgets::Text::foreground_colour, frame_border_colour, frame_colour, frame_thickness, MaxOS::gui::Widget::m_min_height, MaxOS::gui::Widget::m_min_width, and title_bar_height.

Member Function Documentation

◆ add_child()

void Window::add_child ( Widget child)
overridevirtual

Adds a child to the window.

Parameters
childThe child to add.

Reimplemented from MaxOS::gui::CompositeWidget.

Definition at line 220 of file window.cpp.

220 {
221
222 // If there is a child to add
223 if (child != nullptr) {
224
225 // Change the position of the child to be inside the window contents
229
230 }
231
232 // Add the child to the window
234}
Type top
The top coordinate of the rectangle.
Definition rectangle.h:25
Type left
The left coordinate of the rectangle.
Definition rectangle.h:24
void add_child(Widget *child) override
Adds a child to the widget.
Definition widget.cpp:403

References MaxOS::gui::CompositeWidget::add_child(), frame_thickness, MaxOS::common::Rectangle< Type >::left, title_bar_height, and MaxOS::common::Rectangle< Type >::top.

Referenced by Window(), and Window().

◆ draw_self()

void Window::draw_self ( common::GraphicsContext gc,
common::Rectangle< int32_t > &  area 
)
overridevirtual

Draws the window and its children.

Parameters
gcThe graphics context to draw on.
areaThe area to draw

Reimplemented from MaxOS::gui::CompositeWidget.

Definition at line 154 of file window.cpp.

154 {
155
156 // Get the positioning of the window
161
162 // Create an area for the window contents
166
167 // Draw the window contents if they are in the area to draw
173 }
174
175 // Draw the frame if it is in the area to draw
183 }
184
185 // Draw the bottom of the window frame
193 }
194
195 // Draw the left of the window frame
202 }
203
204 // Draw the right of the window frame
212 }
213}
A pair of two objects.
Definition pair.h:22
bool intersects(const Rectangle< Type > &)
Definition rectangle.h:88
Rectangle< Type > intersection(const Rectangle< Type > &)
Definition rectangle.h:116
common::Rectangle< int32_t > position()
Get the position of the widget.
Definition widget.cpp:127
virtual common::Coordinates absolute_coordinates(common::Coordinates coordinates)
Get the absolute coordinates of the widget (relative to the screen)
Definition widget.cpp:98

References MaxOS::gui::Widget::absolute_coordinates(), area_colour, frame_colour, frame_thickness, MaxOS::common::Rectangle< Type >::height, MaxOS::common::Rectangle< Type >::intersection(), MaxOS::common::Rectangle< Type >::intersects(), MaxOS::common::Rectangle< Type >::left, MaxOS::gui::Widget::position(), title_bar_height, MaxOS::common::Rectangle< Type >::top, and MaxOS::common::Rectangle< Type >::width.

◆ on_mouse_button_pressed()

MouseEventHandler * Window::on_mouse_button_pressed ( uint32_t  mouse_x,
uint32_t  mouse_y,
uint8_t  button 
)
overridevirtual

Handles the mouse button being pressed.

Parameters
mouse_xThe x coordinate of the mouse.
mouse_yThe y coordinate of the mouse.
buttonThe button that is pressed.
Returns
The MouseEventHandler of the widget that will handle the event, nullptr otherwise.

Reimplemented from MaxOS::gui::CompositeWidget.

Definition at line 103 of file window.cpp.

103 {
104
105 // Pass the mouse event to the children
108
109 // Bring the window to the front
111
112 // Convert the mouse coordinates to an int32_t
113 auto x = (int32_t) mouse_x;
114 auto y = (int32_t) mouse_y;
115
116 if (x <= frame_thickness) {
117 if (y <= frame_thickness)
118 return &m_resizer_top_left;
119
121 return &m_resizer_left;
122
123 else
124 return &m_resizer_bottom_left;
125 } else if (x < window_position.width - frame_thickness) {
126 if (y <= frame_thickness)
127 return &m_resizer_top;
128
129 else if (y < frame_thickness + title_bar_height)
130 return &m_mover;
131
132 else if (y >= window_position.height - frame_thickness)
133 return &m_resizer_bottom;
134 } else {
135 if (y <= frame_thickness)
136 return &m_resizer_top_right;
137
139 return &m_resizer_right;
140
141 else
142 return &m_resizer_bottom_right;
143 }
144
145 return child_result;
146}
Handles events that are triggered by the mouse driver.
Definition mouse.h:72
drivers::peripherals::MouseEventHandler * on_mouse_button_pressed(uint32_t x, uint32_t y, uint8_t button) override
Passes the event to the child that the mouse is over.
Definition widget.cpp:520
void bring_to_front()
Brings this widget to the front of the screen.
Definition widget.cpp:230

References MaxOS::gui::Widget::bring_to_front(), frame_thickness, MaxOS::common::Rectangle< Type >::height, MaxOS::gui::CompositeWidget::on_mouse_button_pressed(), MaxOS::gui::Widget::position(), title_bar_height, and MaxOS::common::Rectangle< Type >::width.

Member Data Documentation

◆ area_colour

common::Colour MaxOS::gui::Window::area_colour

The background colour of the window inner area.

Definition at line 46 of file window.h.

Referenced by draw_self(), Window(), and Window().

◆ frame_border_colour

common::Colour MaxOS::gui::Window::frame_border_colour

The colour of the border around the bar around the window.

Definition at line 48 of file window.h.

Referenced by Window(), and Window().

◆ frame_colour

common::Colour MaxOS::gui::Window::frame_colour

The colour of the bar around the window.

Definition at line 47 of file window.h.

Referenced by draw_self(), Window(), and Window().

◆ frame_thickness

uint8_t MaxOS::gui::Window::frame_thickness { 5 }

How thick the bar around the window is.

Definition at line 43 of file window.h.

43{ 5 };

Referenced by add_child(), draw_self(), on_mouse_button_pressed(), Window(), and Window().

◆ title_bar_height

uint8_t MaxOS::gui::Window::title_bar_height { 10 }

How large the bar at the top of the window is.

Definition at line 44 of file window.h.

44{ 10 };

Referenced by add_child(), draw_self(), on_mouse_button_pressed(), Window(), and Window().


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