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

A widget that can contain other widgets. More...

#include <widget.h>

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

Public Member Functions

 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 add_child (Widget *child) override
 Adds a child to the widget.
 
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.
 
drivers::peripherals::MouseEventHandleron_mouse_button_pressed (uint32_t x, uint32_t y, uint8_t button) override
 Passes the event to the child that the mouse is over.
 
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.
 

Protected Member Functions

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.
 
virtual void draw_self (common::GraphicsContext *gc, common::Rectangle< int32_t > &area)
 Draws the widget itself (should be overridden by the derived class)
 
- 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

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 widget that can contain other widgets.

Definition at line 88 of file widget.h.

Constructor & Destructor Documentation

◆ CompositeWidget()

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.

Parameters
leftThe left position of the widget relative to its parent
topThe top position of the widget relative to its parent
widthHow many pixels wide the widget is
heightHow many pixels tall the widget is

Definition at line 322 of file widget.cpp.

323: Widget(left, top, width, height) {
324
325}
Widget()
Construct a new Widget object.
Definition widget.cpp:19

Member Function Documentation

◆ add_child()

void CompositeWidget::add_child ( Widget child)
overridevirtual

Adds a child to the widget.

Parameters
childThe child to add

Reimplemented from MaxOS::gui::Widget.

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

Definition at line 403 of file widget.cpp.

403 {
404
405 // Store the child and parent the child to this widget
406 m_children.push_back(child);
408}
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
common::Vector< Widget * > m_children
Widgets contained within this composite widget.
Definition widget.h:92
virtual void add_child(Widget *child)
Set the parent of a widget to this widget, making it into a child.
Definition widget.cpp:85

References MaxOS::gui::Widget::add_child(), and m_children.

Referenced by MaxOS::gui::Desktop::add_child(), and MaxOS::gui::Window::add_child().

◆ draw() [1/2]

void CompositeWidget::draw ( common::GraphicsContext gc,
common::Rectangle< int32_t > &  area 
)
overridevirtual

Draws a section of the widget and its m_children.

Parameters
gcThe graphics context to draw to
areaThe area to draw

Reimplemented from MaxOS::gui::Widget.

Definition at line 335 of file widget.cpp.

335 {
336
337 // Draw the widget with its m_children
338 draw(gc, area, m_children.begin());
339}
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.
Definition widget.cpp:348

References draw(), and m_children.

◆ draw() [2/2]

void CompositeWidget::draw ( common::GraphicsContext gc,
common::Rectangle< int32_t > &  area,
common::Vector< Widget * >::iterator  start 
)
protected

Draws a section of the widget and the m_children after a specific child.

Parameters
gcThe graphics context to draw to
areaThe area to draw
startThe child to start drawing from

Definition at line 348 of file widget.cpp.

348 {
349
350 // Draw the widget
352
353 // Get the area of the widget
355
356 //Note: has to use iterator as the start is not necessarily the m_first_memory_chunk child
358
359 Rectangle<int32_t> child_area = (*child_widget)->position();
360
361 // Check if the child is in the area that needs to be redrawn
363
364 // Get the area that needs to be redrawn
366
367 // Translate the area so that it is relative to the child
370
371 // Draw the child
372 (*child_widget)->draw(gc, rectangle);
373
374 // Draw what is left of the area that needs to be redrawn
376 for (auto& rest_area_part: rest_draw_area)
378
379 // Return as the entire area has now been drawn
380 return;
381 }
382 }
383
384 // Now draw the widget itself
385 draw_self(gc, area);
386}
Type top
The top coordinate of the rectangle.
Definition rectangle.h:25
bool intersects(const Rectangle< Type > &)
Definition rectangle.h:88
Rectangle< Type > intersection(const Rectangle< Type > &)
Definition rectangle.h:116
Type left
The left coordinate of the rectangle.
Definition rectangle.h:24
Vector< Rectangle< Type > > subtract(const Rectangle< Type > &)
Definition rectangle.h:142
virtual void draw_self(common::GraphicsContext *gc, common::Rectangle< int32_t > &area)
Draws the widget itself (should be overridden by the derived class)
Definition widget.cpp:394
virtual void draw(common::GraphicsContext *gc, common::Rectangle< int32_t > &area)
Draw the widget on the screen.
Definition widget.cpp:48
common::Rectangle< int32_t > position()
Get the position of the widget.
Definition widget.cpp:127

References MaxOS::gui::Widget::draw(), draw(), draw_self(), MaxOS::common::Rectangle< Type >::intersection(), MaxOS::common::Rectangle< Type >::intersects(), MaxOS::common::Rectangle< Type >::left, m_children, MaxOS::gui::Widget::position(), MaxOS::common::Rectangle< Type >::subtract(), and MaxOS::common::Rectangle< Type >::top.

Referenced by draw(), draw(), and MaxOS::gui::Desktop::on_time().

◆ draw_self()

void CompositeWidget::draw_self ( common::GraphicsContext gc,
common::Rectangle< int32_t > &  area 
)
protectedvirtual

Draws the widget itself (should be overridden by the derived class)

Parameters
gcThe graphics context to draw to
areaThe area to draw

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

Definition at line 394 of file widget.cpp.

394 {
395
396}

Referenced by draw().

◆ on_mouse_button_pressed()

peripherals::MouseEventHandler * CompositeWidget::on_mouse_button_pressed ( uint32_t  x,
uint32_t  y,
uint8_t  button 
)
overridevirtual

Passes the event to the child that the mouse is over.

Parameters
xThe x coordinate of the mouse
yThe y coordinate of the mouse
buttonThe button that was pressed
Returns
The object that has the mouseEventHandler which handled the event

Reimplemented from MaxOS::gui::Widget.

Reimplemented in MaxOS::gui::Window.

Definition at line 520 of file widget.cpp.

520 {
521
523
524 for (auto& child_widget: m_children) {
525
526 // Pass the event to the child
527 if (child_widget->contains_coordinate(x, y)) {
528 mouse_event_handler = child_widget->on_mouse_button_pressed(x - child_widget->m_position.left,
529 y - child_widget->m_position.top, button);
530 break;
531 }
532 }
533
534 return mouse_event_handler;
535}
Handles events that are triggered by the mouse driver.
Definition mouse.h:72

References MaxOS::common::Rectangle< Type >::left, m_children, and MaxOS::common::Rectangle< Type >::top.

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

◆ on_mouse_button_released()

void CompositeWidget::on_mouse_button_released ( uint32_t  x,
uint32_t  y,
uint8_t  button 
)
overridevirtual

Passes the event to the child that the mouse is over. (Event handling should be done by the derived class)

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

Reimplemented from MaxOS::gui::Widget.

Definition at line 544 of file widget.cpp.

544 {
545
546 // Loop through the m_children
547 for (auto& child_widget: m_children) {
548
549 // Pass the event to the child
550 if (child_widget->contains_coordinate(x, y)) {
551 child_widget->on_mouse_button_released(x - child_widget->m_position.left, y - child_widget->m_position.top,
552 button);
553 break;
554 }
555 }
556}

References MaxOS::common::Rectangle< Type >::left, m_children, and MaxOS::common::Rectangle< Type >::top.

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

◆ on_mouse_enter_widget()

void CompositeWidget::on_mouse_enter_widget ( uint32_t  to_x,
uint32_t  to_y 
)
overridevirtual

Passes the event to the child that the mouse is over. (Event handling should be done by the derived class)

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

Reimplemented from MaxOS::gui::Widget.

Definition at line 416 of file widget.cpp.

416 {
417
418 for (auto& child_widget: m_children) {
419
420 // Check if the mouse is in the child
423
424 // Get the position of the mouse relative to the child
427
428 // Call the child's on_mouse_enter_widget function
429 child_widget->on_mouse_enter_widget(child_x, child_y);
430
431 // Break as the event has been handled
432 break;
433 }
434 }
435}
bool contains(const Rectangle< Type > &)
Definition rectangle.h:192

References MaxOS::common::Rectangle< Type >::contains(), MaxOS::common::Rectangle< Type >::left, m_children, and MaxOS::common::Rectangle< Type >::top.

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

◆ on_mouse_leave_widget()

void CompositeWidget::on_mouse_leave_widget ( uint32_t  from_x,
uint32_t  from_y 
)
overridevirtual

Passes the event to the child that the mouse is over. (Event handling should be done by the derived class)

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

Reimplemented from MaxOS::gui::Widget.

Definition at line 443 of file widget.cpp.

443 {
444
445 for (auto& child_widget: m_children) {
446
447 // Check if the mouse is in the child
450
451 // Get the position of the mouse relative to the child
454
455 // Call the child's on_mouse_leave_widget function
456 child_widget->on_mouse_leave_widget(child_x, child_y);
457
458 // Event has been handled
459 break;
460 }
461 }
462}

References MaxOS::common::Rectangle< Type >::contains(), MaxOS::common::Rectangle< Type >::left, m_children, and MaxOS::common::Rectangle< Type >::top.

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

◆ on_mouse_move_widget()

void CompositeWidget::on_mouse_move_widget ( uint32_t  from_x,
uint32_t  from_y,
uint32_t  to_x,
uint32_t  to_y 
)
overridevirtual

Passes the event to the child that the mouse is over, also generates a leave/enter event for children.

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

Reimplemented from MaxOS::gui::Widget.

Definition at line 472 of file widget.cpp.

472 {
473
474 Widget* left_child = nullptr;
475 Widget* entered_child = nullptr;
476
477 for (auto& child_widget: m_children) {
478
479 // Check if the mouse is in the child
483
484 // If the mouse started in the child
485 if (mouse_in_from) {
486
487 // The mouse moved out of the child
488 if (!mouse_in_to) {
490 continue;
491 }
492
493 // Mouse still in the child
494 child_widget->on_mouse_move_widget(from_x, from_y, to_x, to_y);
495
496 } else {
497
498 // Mouse moved into the child
499 if (mouse_in_to)
501 }
502
503 // Pass the events to the child
504 if (left_child != nullptr)
505 left_child->on_mouse_leave_widget(from_x, from_y);
506
507 if (entered_child != nullptr)
508 entered_child->on_mouse_enter_widget(to_x, to_y);
509 }
510}
A graphical object that can be drawn on the screen.
Definition widget.h:29

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

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

Member Data Documentation

◆ m_children

common::Vector<Widget*> MaxOS::gui::CompositeWidget::m_children
protected

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