Max OS 0.3
Loading...
Searching...
No Matches
widget.h
Go to the documentation of this file.
1
9#ifndef MaxOS_GUI_WIDGET_H
10#define MaxOS_GUI_WIDGET_H
11
12#include <stdint.h>
13#include <common/rectangle.h>
14#include <common/vector.h>
16#include <common/coordinates.h>
19
20
21namespace MaxOS {
22
23 namespace gui {
24
30 template<int Left, int Top, int Width, int Height> friend class WidgetMoverResizer;
31
32 friend class CompositeWidget;
33
34 protected:
36
37 Widget* m_parent { nullptr };
38 bool m_valid { false };
39
40 uint32_t m_min_width { 5 };
41 uint32_t m_min_height { 5 };
42
43 uint32_t m_max_width { 0x8FFFFFFF };
44 uint32_t m_max_height { 0x8FFFFFFF };
45
46 virtual void set_focus(Widget*);
47 virtual void bring_to_front(Widget*);
48
49 public:
50
51 // Initializing functions
52 Widget();
53 Widget(int32_t left, int32_t top, uint32_t width, uint32_t height);
54 ~Widget();
55
56 // Drawing functions
58 void invalidate();
59 virtual void invalidate(common::Rectangle<int32_t>& area);
60 virtual void add_child(Widget* child);
61
62 // Positioning functions
64 virtual bool contains_coordinate(uint32_t x, uint32_t y);
66 void move(int32_t left, int32_t top);
67 void resize(int32_t width, int32_t height);
68
69 // Focus functions
70 void focus();
71 virtual void on_focus();
72 virtual void on_focus_lost();
73 void bring_to_front();
74
75 // Mouse functions
76 virtual void on_mouse_enter_widget(uint32_t to_x, uint32_t to_y);
77 virtual void on_mouse_leave_widget(uint32_t from_x, uint32_t from_y);
78 virtual void on_mouse_move_widget(uint32_t from_x, uint32_t from_y, uint32_t to_x, uint32_t to_y);
79 virtual drivers::peripherals::MouseEventHandler* on_mouse_button_pressed(uint32_t x, uint32_t y, uint8_t button);
80 virtual void on_mouse_button_released(uint32_t x, uint32_t y, uint8_t button);
81
82 };
83
88 class CompositeWidget : public Widget {
89
90 protected:
91
95
96
97 public:
98
99 // Initializing functions
101 CompositeWidget(int32_t left, int32_t top, uint32_t width, uint32_t height);
103
104 // Drawing functions
106 void add_child(Widget* child) override;
107
108 // Mouse functions
109 void on_mouse_enter_widget(uint32_t to_x, uint32_t to_y) override;
110 void on_mouse_leave_widget(uint32_t from_x, uint32_t from_y) override;
111 void on_mouse_move_widget(uint32_t from_x, uint32_t from_y, uint32_t to_x, uint32_t to_y) override;
112 drivers::peripherals::MouseEventHandler* on_mouse_button_pressed(uint32_t x, uint32_t y, uint8_t button) override;
113 void on_mouse_button_released(uint32_t x, uint32_t y, uint8_t button) override;
114
115 };
116
117
127 template<int Left, int Top, int Width, int Height> class WidgetMoverResizer : public drivers::peripherals::MouseEventHandler {
128 private:
129 Widget* targeted_widget;
130
131 public:
134
135 void on_mouse_move_event(int8_t x, int8_t y) override;
136
137 };
138
140
145
150
152
161 template<int Left, int Top, int Width, int Height> WidgetMoverResizer<Left, Top, Width, Height>::WidgetMoverResizer(Widget* target)
162 : drivers::peripherals::MouseEventHandler() {
163 this->targeted_widget = target;
164 }
165
166 template<int Left, int Top, int Width, int Height> WidgetMoverResizer<Left, Top, Width, Height>::~WidgetMoverResizer() = default;
167
174 template<int Left, int Top, int Width, int Height> void WidgetMoverResizer<Left, Top, Width, Height>::on_mouse_move_event(int8_t x, int8_t y) {
175
176 Widget* targ = this->targeted_widget;
177
178 // If there is actually a size of the widget to change
179 if(Left != 0 || Top != 0)
180 // Move the widget to the left or right, and up or down
181 targ->move(targ->m_position.left + Left * x, targ->m_position.top + Top * y);
182
183 // If there is actually a size of the widget to change
184 if(Width != 0 || Height != 0)
185 // Resize the widget to the left or right, and up or down
186 targ->resize(targ->m_position.width + Width * x, targ->m_position.height + Height * y);
187 }
188 }
189}
190#endif //MaxOS_GUI_WIDGET_H
Draws pixels to the screen, and handles drawing lines, rectangles and circles.
A pair of two objects.
Definition pair.h:22
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Type height
The height of the rectangle.
Definition rectangle.h:27
Type top
The top coordinate of the rectangle.
Definition rectangle.h:25
Type left
The left coordinate of the rectangle.
Definition rectangle.h:24
Type width
The width of the rectangle.
Definition rectangle.h:26
Handles the events that are triggered by the Keyboard Driver.
Definition keyboard.h:274
Handles events that are triggered by the mouse driver.
Definition mouse.h:72
A widget that can contain other widgets.
Definition widget.h:88
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...
Definition widget.cpp:472
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 c...
Definition widget.cpp:544
common::Vector< Widget * > m_children
Widgets contained within this composite widget.
Definition widget.h:92
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 c...
Definition widget.cpp:443
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
void add_child(Widget *child) override
Adds a child to the widget.
Definition widget.cpp:403
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 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
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 c...
Definition widget.cpp:416
A template class that allows you to move and resize a widget.
Definition widget.h:127
WidgetMoverResizer(Widget *widget)
Definition widget.h:161
void on_mouse_move_event(int8_t x, int8_t y) override
Definition widget.h:174
A graphical object that can be drawn on the screen.
Definition widget.h:29
uint32_t m_min_width
The smallest m_width the widget can be resized to.
Definition widget.h:40
virtual void on_focus()
Handles the event when the widget is focussed.
Definition widget.cpp:216
virtual void on_focus_lost()
Handles the event when the widget is unfocused.
Definition widget.cpp:223
uint32_t m_max_width
The largest m_width the widget can be resized to.
Definition widget.h:43
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.
Definition widget.cpp:265
void focus()
Set the current focused widget to be this widget.
Definition widget.cpp:195
void invalidate()
Invalidates the entire widget. This forces the widget to be redrawn on the next screen update.
Definition widget.cpp:55
virtual bool contains_coordinate(uint32_t x, uint32_t y)
Check if the widget contains a specific coordinate.
Definition widget.cpp:116
virtual void draw(common::GraphicsContext *gc, common::Rectangle< int32_t > &area)
Draw the widget on the screen.
Definition widget.cpp:48
uint32_t m_max_height
The largest m_height the widget can be resized to.
Definition widget.h:44
common::Rectangle< int32_t > position()
Get the position of the widget.
Definition widget.cpp:127
bool m_valid
Is the widget drawn to the screen and up to date.
Definition widget.h:38
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.
Definition widget.cpp:159
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.
Definition widget.cpp:255
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.
Definition widget.cpp:277
uint32_t m_min_height
The smallest m_height the widget can be resized to.
Definition widget.h:41
common::Rectangle< int32_t > m_position
The position and size of the widget (relative to its parent)
Definition widget.h:35
void bring_to_front()
Brings this widget to the front of the screen.
Definition widget.cpp:230
virtual drivers::peripherals::MouseEventHandler * on_mouse_button_pressed(uint32_t x, uint32_t y, uint8_t button)
Handles the event when the mouse is pressed on the widget.
Definition widget.cpp:289
Widget()
Construct a new Widget object.
Definition widget.cpp:19
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.
Definition widget.cpp:138
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.
Definition widget.cpp:308
virtual common::Coordinates absolute_coordinates(common::Coordinates coordinates)
Get the absolute coordinates of the widget (relative to the screen)
Definition widget.cpp:98
virtual void add_child(Widget *child)
Set the parent of a widget to this widget, making it into a child.
Definition widget.cpp:85
virtual void set_focus(Widget *)
Sets the widget that is currently focussed.
Definition widget.cpp:206
Widget * m_parent
The widget that owns this widget.
Definition widget.h:37
Defines a simple Coordinates type as a pair of int32_t values.
Defines a GraphicsContext class for drawing pixels, lines, rectangles and circles to the screen.
Defines a Keyboard and related classes for handling keyboard input.
Defines a MouseDriver for handling PS/2 mouse input and generating mouse events.
Defines a Rectangle class for storing rectangle dimensions and performing rectangle operations.
Defines a Vector class for dynamically storing an array of elements.