2018-10-10 16:12:38 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-10 17:14:44 +03:00
|
|
|
#include <LibCore/CElapsedTimer.h>
|
2019-03-25 03:42:15 +03:00
|
|
|
#include <LibGUI/GEvent.h>
|
|
|
|
#include <LibGUI/GObject.h>
|
2019-01-20 01:49:56 +03:00
|
|
|
#include <SharedGraphics/Rect.h>
|
|
|
|
#include <SharedGraphics/Color.h>
|
|
|
|
#include <SharedGraphics/Font.h>
|
2019-02-10 13:07:13 +03:00
|
|
|
#include <AK/Badge.h>
|
2018-12-21 04:18:16 +03:00
|
|
|
#include <AK/AKString.h>
|
2018-10-10 16:12:38 +03:00
|
|
|
|
2019-01-09 04:06:04 +03:00
|
|
|
class GraphicsBitmap;
|
2019-02-10 13:07:13 +03:00
|
|
|
class GLayout;
|
2019-01-20 06:49:48 +03:00
|
|
|
class GWindow;
|
2018-10-12 02:03:22 +03:00
|
|
|
|
2019-02-10 13:07:13 +03:00
|
|
|
enum class SizePolicy { Fixed, Fill };
|
|
|
|
enum class Orientation { Horizontal, Vertical };
|
2019-03-06 16:06:40 +03:00
|
|
|
enum class HorizontalDirection { Left, Right };
|
|
|
|
enum class VerticalDirection { Up, Down };
|
2019-02-10 13:07:13 +03:00
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
class GWidget : public GObject {
|
2018-10-10 16:12:38 +03:00
|
|
|
public:
|
2019-01-20 06:49:48 +03:00
|
|
|
explicit GWidget(GWidget* parent = nullptr);
|
2019-01-21 02:46:08 +03:00
|
|
|
virtual ~GWidget() override;
|
2019-01-20 06:49:48 +03:00
|
|
|
|
2019-02-10 13:07:13 +03:00
|
|
|
GLayout* layout() { return m_layout.ptr(); }
|
|
|
|
void set_layout(OwnPtr<GLayout>&&);
|
|
|
|
|
|
|
|
SizePolicy horizontal_size_policy() const { return m_horizontal_size_policy; }
|
|
|
|
SizePolicy vertical_size_policy() const { return m_vertical_size_policy; }
|
|
|
|
SizePolicy size_policy(Orientation orientation) { return orientation == Orientation::Horizontal ? m_horizontal_size_policy : m_vertical_size_policy; }
|
|
|
|
void set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy);
|
|
|
|
|
|
|
|
Size preferred_size() const { return m_preferred_size; }
|
|
|
|
void set_preferred_size(const Size&);
|
|
|
|
|
2019-04-08 19:58:44 +03:00
|
|
|
bool has_tooltip() const { return !m_tooltip.is_empty(); }
|
|
|
|
String tooltip() const { return m_tooltip; }
|
|
|
|
void set_tooltip(const String& tooltip) { m_tooltip = tooltip; }
|
|
|
|
|
2019-04-10 17:56:55 +03:00
|
|
|
virtual void event(CEvent&) override;
|
2019-01-21 02:46:08 +03:00
|
|
|
virtual void paint_event(GPaintEvent&);
|
2019-02-09 13:19:38 +03:00
|
|
|
virtual void resize_event(GResizeEvent&);
|
2019-01-21 02:46:08 +03:00
|
|
|
virtual void show_event(GShowEvent&);
|
|
|
|
virtual void hide_event(GHideEvent&);
|
|
|
|
virtual void keydown_event(GKeyEvent&);
|
|
|
|
virtual void keyup_event(GKeyEvent&);
|
|
|
|
virtual void mousemove_event(GMouseEvent&);
|
|
|
|
virtual void mousedown_event(GMouseEvent&);
|
|
|
|
virtual void mouseup_event(GMouseEvent&);
|
2019-03-25 03:42:15 +03:00
|
|
|
virtual void click_event(GMouseEvent&);
|
|
|
|
virtual void doubleclick_event(GMouseEvent&);
|
2019-04-10 17:56:55 +03:00
|
|
|
virtual void focusin_event(CEvent&);
|
|
|
|
virtual void focusout_event(CEvent&);
|
|
|
|
virtual void enter_event(CEvent&);
|
|
|
|
virtual void leave_event(CEvent&);
|
|
|
|
virtual void child_event(CChildEvent&) override;
|
2019-01-21 02:46:08 +03:00
|
|
|
|
|
|
|
Rect relative_rect() const { return m_relative_rect; }
|
|
|
|
Point relative_position() const { return m_relative_rect.location(); }
|
|
|
|
|
2019-02-09 16:30:05 +03:00
|
|
|
Rect window_relative_rect() const;
|
2019-04-08 19:58:44 +03:00
|
|
|
Rect screen_relative_rect() const;
|
2019-02-09 16:30:05 +03:00
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
int x() const { return m_relative_rect.x(); }
|
|
|
|
int y() const { return m_relative_rect.y(); }
|
|
|
|
int width() const { return m_relative_rect.width(); }
|
|
|
|
int height() const { return m_relative_rect.height(); }
|
2018-10-12 15:15:14 +03:00
|
|
|
|
|
|
|
Rect rect() const { return { 0, 0, width(), height() }; }
|
2019-01-21 02:46:08 +03:00
|
|
|
Size size() const { return m_relative_rect.size(); }
|
2018-10-10 17:49:36 +03:00
|
|
|
|
2018-10-10 16:12:38 +03:00
|
|
|
void update();
|
2019-02-10 16:28:39 +03:00
|
|
|
void update(const Rect&);
|
2018-10-10 16:12:38 +03:00
|
|
|
|
2019-01-26 13:24:16 +03:00
|
|
|
virtual bool accepts_focus() const { return false; }
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
bool is_focused() const;
|
|
|
|
void set_focus(bool);
|
2018-10-13 18:52:47 +03:00
|
|
|
|
2018-10-10 17:49:36 +03:00
|
|
|
struct HitTestResult {
|
2019-01-20 06:49:48 +03:00
|
|
|
GWidget* widget { nullptr };
|
2018-10-10 17:49:36 +03:00
|
|
|
int localX { 0 };
|
|
|
|
int localY { 0 };
|
|
|
|
};
|
2019-01-21 02:46:08 +03:00
|
|
|
HitTestResult hit_test(int x, int y);
|
2018-10-10 17:49:36 +03:00
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
virtual const char* class_name() const override { return "GWidget"; }
|
2018-10-10 17:49:36 +03:00
|
|
|
|
2019-01-25 01:40:12 +03:00
|
|
|
void set_relative_rect(const Rect&);
|
2019-02-09 13:19:38 +03:00
|
|
|
void set_relative_rect(int x, int y, int width, int height) { set_relative_rect({ x, y, width, height }); }
|
2019-02-08 02:14:37 +03:00
|
|
|
|
2019-02-02 10:05:14 +03:00
|
|
|
void move_to(const Point& point) { set_relative_rect({ point, relative_rect().size() }); }
|
2019-02-08 02:14:37 +03:00
|
|
|
void move_to(int x, int y) { move_to({ x, y }); }
|
|
|
|
void resize(const Size& size) { set_relative_rect({ relative_rect().location(), size }); }
|
|
|
|
void resize(int width, int height) { resize({ width, height }); }
|
2018-10-10 17:49:36 +03:00
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
Color background_color() const { return m_background_color; }
|
|
|
|
Color foreground_color() const { return m_foreground_color; }
|
2018-10-11 02:48:09 +03:00
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void set_background_color(Color color) { m_background_color = color; }
|
|
|
|
void set_foreground_color(Color color) { m_foreground_color = color; }
|
2018-10-11 02:48:09 +03:00
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
GWindow* window()
|
2018-10-12 02:03:22 +03:00
|
|
|
{
|
2019-01-21 02:46:08 +03:00
|
|
|
if (auto* pw = parent_widget())
|
2018-10-12 02:03:22 +03:00
|
|
|
return pw->window();
|
|
|
|
return m_window;
|
|
|
|
}
|
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
const GWindow* window() const
|
2018-10-12 02:03:22 +03:00
|
|
|
{
|
2019-01-21 02:46:08 +03:00
|
|
|
if (auto* pw = parent_widget())
|
2018-10-12 02:03:22 +03:00
|
|
|
return pw->window();
|
|
|
|
return m_window;
|
|
|
|
}
|
2018-10-11 17:52:40 +03:00
|
|
|
|
2019-01-20 09:03:38 +03:00
|
|
|
void set_window(GWindow*);
|
2018-10-12 03:41:27 +03:00
|
|
|
|
2019-03-19 04:20:00 +03:00
|
|
|
GWidget* parent_widget()
|
|
|
|
{
|
|
|
|
if (parent() && parent()->is_widget())
|
|
|
|
return static_cast<GWidget*>(parent());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const GWidget* parent_widget() const
|
|
|
|
{
|
|
|
|
if (parent() && parent()->is_widget())
|
|
|
|
return static_cast<const GWidget*>(parent());
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-10-11 17:52:40 +03:00
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void set_fill_with_background_color(bool b) { m_fill_with_background_color = b; }
|
|
|
|
bool fill_with_background_color() const { return m_fill_with_background_color; }
|
2018-10-14 01:21:42 +03:00
|
|
|
|
2018-10-14 14:06:05 +03:00
|
|
|
const Font& font() const { return *m_font; }
|
2019-01-21 02:46:08 +03:00
|
|
|
void set_font(RetainPtr<Font>&&);
|
2018-10-14 14:06:05 +03:00
|
|
|
|
2019-01-27 10:48:34 +03:00
|
|
|
void set_global_cursor_tracking(bool);
|
|
|
|
bool global_cursor_tracking() const;
|
|
|
|
|
2019-02-10 13:07:13 +03:00
|
|
|
void notify_layout_changed(Badge<GLayout>);
|
2019-03-30 15:53:30 +03:00
|
|
|
void invalidate_layout();
|
2019-02-10 13:07:13 +03:00
|
|
|
|
2019-03-15 18:12:06 +03:00
|
|
|
bool is_visible() const { return m_visible; }
|
|
|
|
void set_visible(bool);
|
|
|
|
|
2019-03-29 04:20:22 +03:00
|
|
|
bool spans_entire_window_horizontally() const;
|
|
|
|
|
2018-10-10 16:12:38 +03:00
|
|
|
private:
|
2019-03-15 18:12:06 +03:00
|
|
|
virtual bool is_widget() const final { return true; }
|
|
|
|
|
2019-02-09 13:19:38 +03:00
|
|
|
void handle_paint_event(GPaintEvent&);
|
2019-02-10 13:07:13 +03:00
|
|
|
void handle_resize_event(GResizeEvent&);
|
2019-04-01 23:03:32 +03:00
|
|
|
void handle_mousedown_event(GMouseEvent&);
|
2019-03-25 03:42:15 +03:00
|
|
|
void handle_mouseup_event(GMouseEvent&);
|
2019-04-10 17:56:55 +03:00
|
|
|
void handle_enter_event(CEvent&);
|
|
|
|
void handle_leave_event(CEvent&);
|
2019-02-10 13:07:13 +03:00
|
|
|
void do_layout();
|
2019-02-09 13:19:38 +03:00
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
GWindow* m_window { nullptr };
|
2019-02-10 13:07:13 +03:00
|
|
|
OwnPtr<GLayout> m_layout;
|
2018-10-12 02:03:22 +03:00
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
Rect m_relative_rect;
|
2019-02-19 03:42:53 +03:00
|
|
|
Color m_background_color;
|
|
|
|
Color m_foreground_color;
|
2018-10-14 14:06:05 +03:00
|
|
|
RetainPtr<Font> m_font;
|
2019-04-08 19:58:44 +03:00
|
|
|
String m_tooltip;
|
2018-10-11 13:33:03 +03:00
|
|
|
|
2019-02-10 13:07:13 +03:00
|
|
|
SizePolicy m_horizontal_size_policy { SizePolicy::Fill };
|
|
|
|
SizePolicy m_vertical_size_policy { SizePolicy::Fill };
|
|
|
|
Size m_preferred_size;
|
|
|
|
|
2019-03-10 15:16:36 +03:00
|
|
|
bool m_fill_with_background_color { false };
|
2019-03-15 18:12:06 +03:00
|
|
|
bool m_visible { true };
|
2019-03-25 03:42:15 +03:00
|
|
|
|
2019-04-10 17:14:44 +03:00
|
|
|
CElapsedTimer m_click_clock;
|
2018-10-10 16:12:38 +03:00
|
|
|
};
|