diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-09-21 17:05:35 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-09-21 17:05:35 +0200 |
commit | ff6ce422ddecb7dcb7fd9f67d3ea0cfdec7e74ea (patch) | |
tree | 8a66cc12d14da634984978aa387a92537d161915 /Libraries | |
parent | e4e92980a17449abb315e2267c897529f700525e (diff) | |
download | serenity-ff6ce422ddecb7dcb7fd9f67d3ea0cfdec7e74ea.zip |
LibGUI: Convert GWidget to ObjectPtr
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibGUI/GAboutDialog.cpp | 8 | ||||
-rw-r--r-- | Libraries/LibGUI/GAbstractView.cpp | 13 | ||||
-rw-r--r-- | Libraries/LibGUI/GAbstractView.h | 2 | ||||
-rw-r--r-- | Libraries/LibGUI/GFilePicker.cpp | 12 | ||||
-rw-r--r-- | Libraries/LibGUI/GInputBox.cpp | 6 | ||||
-rw-r--r-- | Libraries/LibGUI/GMessageBox.cpp | 8 | ||||
-rw-r--r-- | Libraries/LibGUI/GScrollableWidget.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibGUI/GScrollableWidget.h | 2 | ||||
-rw-r--r-- | Libraries/LibGUI/GStackWidget.h | 5 | ||||
-rw-r--r-- | Libraries/LibGUI/GTabWidget.h | 5 | ||||
-rw-r--r-- | Libraries/LibGUI/GWidget.h | 42 | ||||
-rw-r--r-- | Libraries/LibGUI/GWindow.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibGUI/GWindow.h | 2 |
13 files changed, 58 insertions, 51 deletions
diff --git a/Libraries/LibGUI/GAboutDialog.cpp b/Libraries/LibGUI/GAboutDialog.cpp index e89f9447c3..09e688708c 100644 --- a/Libraries/LibGUI/GAboutDialog.cpp +++ b/Libraries/LibGUI/GAboutDialog.cpp @@ -13,12 +13,12 @@ GAboutDialog::GAboutDialog(const StringView& name, const GraphicsBitmap* icon, C set_title(String::format("About %s", m_name.characters())); set_resizable(false); - auto* widget = new GWidget; + auto widget = GWidget::construct(); set_main_widget(widget); widget->set_fill_with_background_color(true); widget->set_layout(make<GBoxLayout>(Orientation::Horizontal)); - auto* left_container = new GWidget(widget); + auto left_container = GWidget::construct(widget); left_container->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); left_container->set_preferred_size(48, 0); left_container->set_layout(make<GBoxLayout>(Orientation::Vertical)); @@ -28,7 +28,7 @@ GAboutDialog::GAboutDialog(const StringView& name, const GraphicsBitmap* icon, C icon_label->set_preferred_size(40, 40); left_container->layout()->add_spacer(); - auto* right_container = new GWidget(widget); + auto right_container = GWidget::construct(widget); right_container->set_layout(make<GBoxLayout>(Orientation::Vertical)); right_container->layout()->set_margins({ 0, 4, 4, 4 }); @@ -46,7 +46,7 @@ GAboutDialog::GAboutDialog(const StringView& name, const GraphicsBitmap* icon, C right_container->layout()->add_spacer(); - auto* button_container = new GWidget(right_container); + auto button_container = GWidget::construct(right_container); button_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); button_container->set_preferred_size(0, 20); button_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); diff --git a/Libraries/LibGUI/GAbstractView.cpp b/Libraries/LibGUI/GAbstractView.cpp index bfeaf5a47b..aea8ada294 100644 --- a/Libraries/LibGUI/GAbstractView.cpp +++ b/Libraries/LibGUI/GAbstractView.cpp @@ -14,7 +14,6 @@ GAbstractView::GAbstractView(GWidget* parent) GAbstractView::~GAbstractView() { - delete m_edit_widget; } void GAbstractView::set_model(RefPtr<GModel>&& model) @@ -63,8 +62,10 @@ void GAbstractView::begin_editing(const GModelIndex& index) return; if (!model()->is_editable(index)) return; - if (m_edit_widget) - delete m_edit_widget; + if (m_edit_widget) { + remove_child(*m_edit_widget); + m_edit_widget = nullptr; + } m_edit_index = index; ASSERT(aid_create_editing_delegate); @@ -88,8 +89,10 @@ void GAbstractView::begin_editing(const GModelIndex& index) void GAbstractView::stop_editing() { m_edit_index = {}; - delete m_edit_widget; - m_edit_widget = nullptr; + if (m_edit_widget) { + remove_child(*m_edit_widget); + m_edit_widget = nullptr; + } } void GAbstractView::activate(const GModelIndex& index) diff --git a/Libraries/LibGUI/GAbstractView.h b/Libraries/LibGUI/GAbstractView.h index 04496027e5..444d7acc4e 100644 --- a/Libraries/LibGUI/GAbstractView.h +++ b/Libraries/LibGUI/GAbstractView.h @@ -52,7 +52,7 @@ protected: bool m_editable { false }; GModelIndex m_edit_index; - GWidget* m_edit_widget { nullptr }; + ObjectPtr<GWidget> m_edit_widget; Rect m_edit_widget_content_rect; private: diff --git a/Libraries/LibGUI/GFilePicker.cpp b/Libraries/LibGUI/GFilePicker.cpp index babccdcbaa..d4ebc80320 100644 --- a/Libraries/LibGUI/GFilePicker.cpp +++ b/Libraries/LibGUI/GFilePicker.cpp @@ -50,18 +50,18 @@ GFilePicker::GFilePicker(Mode mode, const StringView& file_name, const StringVie { set_title(m_mode == Mode::Open ? "Open File" : "Save File"); set_rect(200, 200, 700, 400); - auto* horizontal_container = new GWidget; + auto horizontal_container = GWidget::construct(); set_main_widget(horizontal_container); horizontal_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); horizontal_container->layout()->set_margins({ 4, 4, 4, 4 }); horizontal_container->set_fill_with_background_color(true); horizontal_container->set_background_color(Color::WarmGray); - auto* vertical_container = new GWidget(horizontal_container); + auto vertical_container = GWidget::construct(horizontal_container); vertical_container->set_layout(make<GBoxLayout>(Orientation::Vertical)); vertical_container->layout()->set_spacing(4); - auto* upper_container = new GWidget(vertical_container); + auto upper_container = GWidget::construct(vertical_container); upper_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); upper_container->layout()->set_spacing(4); upper_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); @@ -118,13 +118,13 @@ GFilePicker::GFilePicker(Mode mode, const StringView& file_name, const StringVie }); toolbar->add_action(*mkdir_action); - auto* lower_container = new GWidget(vertical_container); + auto lower_container = GWidget::construct(vertical_container); lower_container->set_layout(make<GBoxLayout>(Orientation::Vertical)); lower_container->layout()->set_spacing(4); lower_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); lower_container->set_preferred_size(0, 60); - auto* filename_container = new GWidget(lower_container); + auto filename_container = GWidget::construct(lower_container); filename_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); filename_container->set_preferred_size(0, 20); filename_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); @@ -155,7 +155,7 @@ GFilePicker::GFilePicker(Mode mode, const StringView& file_name, const StringVie set_preview(path); }; - auto* button_container = new GWidget(lower_container); + auto button_container = GWidget::construct(lower_container); button_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); button_container->set_preferred_size(0, 20); button_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); diff --git a/Libraries/LibGUI/GInputBox.cpp b/Libraries/LibGUI/GInputBox.cpp index a14cf1970c..8989ba19f9 100644 --- a/Libraries/LibGUI/GInputBox.cpp +++ b/Libraries/LibGUI/GInputBox.cpp @@ -19,7 +19,7 @@ GInputBox::~GInputBox() void GInputBox::build() { - auto* widget = new GWidget; + auto widget = GWidget::construct(); set_main_widget(widget); int text_width = widget->font().width(m_prompt); @@ -42,12 +42,12 @@ void GInputBox::build() m_text_editor->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); m_text_editor->set_preferred_size(0, 19); - auto* button_container_outer = new GWidget(widget); + auto button_container_outer = GWidget::construct(widget); button_container_outer->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); button_container_outer->set_preferred_size(0, 20); button_container_outer->set_layout(make<GBoxLayout>(Orientation::Vertical)); - auto* button_container_inner = new GWidget(button_container_outer); + auto button_container_inner = GWidget::construct(button_container_outer); button_container_inner->set_layout(make<GBoxLayout>(Orientation::Horizontal)); button_container_inner->layout()->set_spacing(8); diff --git a/Libraries/LibGUI/GMessageBox.cpp b/Libraries/LibGUI/GMessageBox.cpp index be95447c2e..f3c2c940c9 100644 --- a/Libraries/LibGUI/GMessageBox.cpp +++ b/Libraries/LibGUI/GMessageBox.cpp @@ -50,7 +50,7 @@ bool GMessageBox::should_include_cancel_button() const void GMessageBox::build() { - auto* widget = new GWidget; + auto widget = GWidget::construct(); set_main_widget(widget); int text_width = widget->font().width(m_text); @@ -62,9 +62,9 @@ void GMessageBox::build() widget->layout()->set_margins({ 0, 15, 0, 15 }); widget->layout()->set_spacing(15); - GWidget* message_container = widget; + ObjectPtr<GWidget> message_container = widget; if (m_type != Type::None) { - message_container = new GWidget(widget); + message_container = GWidget::construct(widget); message_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); message_container->layout()->set_margins({ 8, 0, 8, 0 }); message_container->layout()->set_spacing(8); @@ -80,7 +80,7 @@ void GMessageBox::build() label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); label->set_preferred_size(text_width, 16); - auto* button_container = new GWidget(widget); + auto button_container = GWidget::construct(widget); button_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); button_container->layout()->set_spacing(5); button_container->layout()->set_margins({ 15, 0, 15, 0 }); diff --git a/Libraries/LibGUI/GScrollableWidget.cpp b/Libraries/LibGUI/GScrollableWidget.cpp index 9379a6ada8..246158d9f4 100644 --- a/Libraries/LibGUI/GScrollableWidget.cpp +++ b/Libraries/LibGUI/GScrollableWidget.cpp @@ -19,7 +19,7 @@ GScrollableWidget::GScrollableWidget(GWidget* parent) update(); }; - m_corner_widget = new GWidget(this); + m_corner_widget = GWidget::construct(this); m_corner_widget->set_fill_with_background_color(true); } diff --git a/Libraries/LibGUI/GScrollableWidget.h b/Libraries/LibGUI/GScrollableWidget.h index 73394c1890..c42dcfc2e0 100644 --- a/Libraries/LibGUI/GScrollableWidget.h +++ b/Libraries/LibGUI/GScrollableWidget.h @@ -55,7 +55,7 @@ private: ObjectPtr<GScrollBar> m_vertical_scrollbar; ObjectPtr<GScrollBar> m_horizontal_scrollbar; - GWidget* m_corner_widget { nullptr }; + ObjectPtr<GWidget> m_corner_widget; Size m_content_size; Size m_size_occupied_by_fixed_elements; bool m_scrollbars_enabled { true }; diff --git a/Libraries/LibGUI/GStackWidget.h b/Libraries/LibGUI/GStackWidget.h index 980d4ea382..7474dc012c 100644 --- a/Libraries/LibGUI/GStackWidget.h +++ b/Libraries/LibGUI/GStackWidget.h @@ -8,7 +8,8 @@ public: explicit GStackWidget(GWidget* parent); virtual ~GStackWidget() override; - GWidget* active_widget() const { return m_active_widget; } + GWidget* active_widget() { return m_active_widget.ptr(); } + const GWidget* active_widget() const { return m_active_widget.ptr(); } void set_active_widget(GWidget*); Function<void(GWidget*)> on_active_widget_change; @@ -18,5 +19,5 @@ protected: virtual void resize_event(GResizeEvent&) override; private: - GWidget* m_active_widget { nullptr }; + ObjectPtr<GWidget> m_active_widget; }; diff --git a/Libraries/LibGUI/GTabWidget.h b/Libraries/LibGUI/GTabWidget.h index 22bf13b985..46a042c06b 100644 --- a/Libraries/LibGUI/GTabWidget.h +++ b/Libraries/LibGUI/GTabWidget.h @@ -18,7 +18,8 @@ public: int active_tab_index() const; - GWidget* active_widget() const { return m_active_widget; } + GWidget* active_widget() { return m_active_widget.ptr(); } + const GWidget* active_widget() const { return m_active_widget.ptr(); } void set_active_widget(GWidget*); int bar_height() const { return 21; } @@ -41,7 +42,7 @@ private: Rect container_rect() const; void update_bar(); - GWidget* m_active_widget { nullptr }; + ObjectPtr<GWidget> m_active_widget; struct TabData { Rect rect(const Font&) const; diff --git a/Libraries/LibGUI/GWidget.h b/Libraries/LibGUI/GWidget.h index ef489af01b..c5f459e690 100644 --- a/Libraries/LibGUI/GWidget.h +++ b/Libraries/LibGUI/GWidget.h @@ -45,10 +45,10 @@ enum class VerticalDirection { class GWidget : public CObject { C_OBJECT(GWidget) public: - explicit GWidget(GWidget* parent = nullptr); virtual ~GWidget() override; GLayout* layout() { return m_layout.ptr(); } + const GLayout* layout() const { return m_layout.ptr(); } void set_layout(OwnPtr<GLayout>&&); SizePolicy horizontal_size_policy() const { return m_horizontal_size_policy; } @@ -72,25 +72,6 @@ public: void set_updates_enabled(bool); virtual void event(CEvent&) override; - virtual void paint_event(GPaintEvent&); - virtual void resize_event(GResizeEvent&); - 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&); - virtual void mousewheel_event(GMouseEvent&); - virtual void click_event(GMouseEvent&); - virtual void doubleclick_event(GMouseEvent&); - virtual void context_menu_event(GContextMenuEvent&); - 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; - virtual void change_event(GEvent&); // This is called after children have been painted. virtual void second_paint_event(GPaintEvent&); @@ -221,8 +202,29 @@ public: virtual void save_to(AK::JsonObject&) override; protected: + explicit GWidget(GWidget* parent = nullptr); + virtual void custom_layout() {} virtual void did_change_font() {} + virtual void paint_event(GPaintEvent&); + virtual void resize_event(GResizeEvent&); + 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&); + virtual void mousewheel_event(GMouseEvent&); + virtual void click_event(GMouseEvent&); + virtual void doubleclick_event(GMouseEvent&); + virtual void context_menu_event(GContextMenuEvent&); + 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; + virtual void change_event(GEvent&); private: void handle_paint_event(GPaintEvent&); diff --git a/Libraries/LibGUI/GWindow.cpp b/Libraries/LibGUI/GWindow.cpp index ce29cd52e4..1c340ae42c 100644 --- a/Libraries/LibGUI/GWindow.cpp +++ b/Libraries/LibGUI/GWindow.cpp @@ -698,7 +698,7 @@ Vector<GWidget*> GWindow::focusable_widgets() const }); }; - collect_focusable_widgets(*m_main_widget); + collect_focusable_widgets(const_cast<GWidget&>(*m_main_widget)); return collected_widgets; } diff --git a/Libraries/LibGUI/GWindow.h b/Libraries/LibGUI/GWindow.h index 7591a66ad8..4f4803a85b 100644 --- a/Libraries/LibGUI/GWindow.h +++ b/Libraries/LibGUI/GWindow.h @@ -154,7 +154,7 @@ private: RefPtr<GraphicsBitmap> m_icon; int m_window_id { 0 }; float m_opacity_when_windowless { 1.0f }; - GWidget* m_main_widget { nullptr }; + ObjectPtr<GWidget> m_main_widget; WeakPtr<GWidget> m_focused_widget; WeakPtr<GWidget> m_global_cursor_tracking_widget; WeakPtr<GWidget> m_automatic_cursor_tracking_widget; |