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 | |
parent | e4e92980a17449abb315e2267c897529f700525e (diff) | |
download | serenity-ff6ce422ddecb7dcb7fd9f67d3ea0cfdec7e74ea.zip |
LibGUI: Convert GWidget to ObjectPtr
41 files changed, 115 insertions, 107 deletions
diff --git a/Applications/About/main.cpp b/Applications/About/main.cpp index a559505cfc..e14e07d330 100644 --- a/Applications/About/main.cpp +++ b/Applications/About/main.cpp @@ -17,7 +17,7 @@ int main(int argc, char** argv) window->set_resizable(false); window->set_rect(window_rect); - auto* widget = new GWidget; + auto widget = GWidget::construct(); window->set_main_widget(widget); widget->set_fill_with_background_color(true); widget->set_layout(make<GBoxLayout>(Orientation::Vertical)); diff --git a/Applications/ChanViewer/main.cpp b/Applications/ChanViewer/main.cpp index 625c17d348..095710f270 100644 --- a/Applications/ChanViewer/main.cpp +++ b/Applications/ChanViewer/main.cpp @@ -17,7 +17,7 @@ int main(int argc, char** argv) window->set_rect(100, 100, 800, 500); window->set_icon(load_png("/res/icons/16x16/app-chanviewer.png")); - auto* widget = new GWidget; + auto widget = GWidget::construct(); window->set_main_widget(widget); widget->set_fill_with_background_color(true); widget->set_layout(make<GBoxLayout>(Orientation::Vertical)); diff --git a/Applications/DisplayProperties/DisplayProperties.cpp b/Applications/DisplayProperties/DisplayProperties.cpp index b37e72f3b9..d7b9a933ae 100644 --- a/Applications/DisplayProperties/DisplayProperties.cpp +++ b/Applications/DisplayProperties/DisplayProperties.cpp @@ -75,7 +75,7 @@ void DisplayPropertiesWidget::create_resolution_list() void DisplayPropertiesWidget::create_root_widget() { - m_root_widget = new GWidget; + m_root_widget = GWidget::construct(); m_root_widget->set_layout(make<GBoxLayout>(Orientation::Vertical)); m_root_widget->set_fill_with_background_color(true); m_root_widget->layout()->set_margins({ 4, 4, 4, 16 }); @@ -97,7 +97,7 @@ void DisplayPropertiesWidget::create_frame() auto background_splitter = GSplitter::construct(Orientation::Vertical, nullptr); tab_widget->add_widget("Wallpaper", background_splitter); - auto* background_content = new GWidget(background_splitter); + auto background_content = GWidget::construct(background_splitter); background_content->set_layout(make<GBoxLayout>(Orientation::Vertical)); background_content->layout()->set_margins({ 4, 4, 4, 4 }); @@ -120,7 +120,7 @@ void DisplayPropertiesWidget::create_frame() auto settings_splitter = GSplitter::construct(Orientation::Vertical, nullptr); tab_widget->add_widget("Settings", settings_splitter); - auto* settings_content = new GWidget(settings_splitter); + auto settings_content = GWidget::construct(settings_splitter); settings_content->set_layout(make<GBoxLayout>(Orientation::Vertical)); settings_content->layout()->set_margins({ 4, 4, 4, 4 }); @@ -135,7 +135,7 @@ void DisplayPropertiesWidget::create_frame() settings_content->layout()->add_spacer(); // Add the apply and cancel buttons - auto* bottom_widget = new GWidget(m_root_widget); + auto bottom_widget = GWidget::construct(m_root_widget); bottom_widget->set_layout(make<GBoxLayout>(Orientation::Horizontal)); bottom_widget->layout()->add_spacer(); bottom_widget->set_size_policy(Orientation::Vertical, SizePolicy::Fixed); diff --git a/Applications/DisplayProperties/DisplayProperties.h b/Applications/DisplayProperties/DisplayProperties.h index afba1930cc..ad51f9926a 100644 --- a/Applications/DisplayProperties/DisplayProperties.h +++ b/Applications/DisplayProperties/DisplayProperties.h @@ -29,7 +29,8 @@ public: void send_settings_to_window_server(int tabIndex); void create_frame(); - inline GWidget* get_root_widget() const { return m_root_widget; } + const GWidget* root_widget() const { return m_root_widget; } + GWidget* root_widget() { return m_root_widget; } private: void create_wallpaper_list(); @@ -39,7 +40,7 @@ private: private: String m_wallpaper_path; RefPtr<CConfigFile> m_wm_config; - GWidget* m_root_widget { nullptr }; + ObjectPtr<GWidget> m_root_widget; Vector<Size> m_resolutions; Vector<String> m_wallpapers; ObjectPtr<GLabel> m_wallpaper_preview; diff --git a/Applications/DisplayProperties/main.cpp b/Applications/DisplayProperties/main.cpp index 5e0148c805..3743428057 100644 --- a/Applications/DisplayProperties/main.cpp +++ b/Applications/DisplayProperties/main.cpp @@ -14,7 +14,7 @@ int main(int argc, char** argv) window->set_title("Display Properties"); window->resize(400, 448); window->set_resizable(false); - window->set_main_widget(instance.get_root_widget()); + window->set_main_widget(instance.root_widget()); window->set_icon(load_png("/res/icons/16x16/app-display-properties.png")); window->show(); diff --git a/Applications/FileManager/main.cpp b/Applications/FileManager/main.cpp index 310f3a6ec5..363cd0b878 100644 --- a/Applications/FileManager/main.cpp +++ b/Applications/FileManager/main.cpp @@ -44,7 +44,7 @@ int main(int argc, char** argv) window->set_title("File Manager"); window->set_rect(20, 200, 640, 480); - auto* widget = new GWidget; + auto widget = GWidget::construct(); widget->set_layout(make<GBoxLayout>(Orientation::Vertical)); widget->layout()->set_spacing(0); diff --git a/Applications/IRCClient/IRCAppWindow.cpp b/Applications/IRCClient/IRCAppWindow.cpp index b8696da7a0..d7a81ceda4 100644 --- a/Applications/IRCClient/IRCAppWindow.cpp +++ b/Applications/IRCClient/IRCAppWindow.cpp @@ -153,7 +153,7 @@ void IRCAppWindow::setup_menus() void IRCAppWindow::setup_widgets() { - auto* widget = new GWidget(nullptr); + auto widget = GWidget::construct(); set_main_widget(widget); widget->set_fill_with_background_color(true); widget->set_background_color(Color::WarmGray); @@ -171,7 +171,7 @@ void IRCAppWindow::setup_widgets() toolbar->add_action(*m_open_query_action); toolbar->add_action(*m_close_query_action); - auto* outer_container = new GWidget(widget); + auto outer_container = GWidget::construct(widget); outer_container->set_layout(make<GBoxLayout>(Orientation::Vertical)); outer_container->layout()->set_margins({ 2, 0, 2, 2 }); diff --git a/Applications/Launcher/main.cpp b/Applications/Launcher/main.cpp index 689942c75e..857b4f0356 100644 --- a/Applications/Launcher/main.cpp +++ b/Applications/Launcher/main.cpp @@ -75,7 +75,7 @@ GWindow* make_launcher_window() window->set_show_titlebar(false); window->set_window_type(GWindowType::Launcher); - auto* widget = new GWidget; + auto widget = GWidget::construct(); widget->set_fill_with_background_color(true); widget->set_layout(make<GBoxLayout>(vertical ? Orientation::Vertical : Orientation::Horizontal)); widget->layout()->set_spacing(0); diff --git a/Applications/PaintBrush/ColorDialog.cpp b/Applications/PaintBrush/ColorDialog.cpp index 703c2c7fc3..aef7778650 100644 --- a/Applications/PaintBrush/ColorDialog.cpp +++ b/Applications/PaintBrush/ColorDialog.cpp @@ -19,16 +19,16 @@ ColorDialog::~ColorDialog() void ColorDialog::build() { - auto* horizontal_container = new GWidget; + auto horizontal_container = GWidget::construct(); horizontal_container->set_fill_with_background_color(true); horizontal_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); horizontal_container->layout()->set_margins({ 4, 4, 4, 4 }); set_main_widget(horizontal_container); - auto* left_vertical_container = new GWidget(horizontal_container); + auto left_vertical_container = GWidget::construct(horizontal_container); left_vertical_container->set_layout(make<GBoxLayout>(Orientation::Vertical)); - auto* right_vertical_container = new GWidget(horizontal_container); + auto right_vertical_container = GWidget::construct(horizontal_container); right_vertical_container->set_layout(make<GBoxLayout>(Orientation::Vertical)); enum RGBComponent { diff --git a/Applications/PaintBrush/PaletteWidget.cpp b/Applications/PaintBrush/PaletteWidget.cpp index 19c1ecbc8b..c90933a121 100644 --- a/Applications/PaintBrush/PaletteWidget.cpp +++ b/Applications/PaintBrush/PaletteWidget.cpp @@ -81,16 +81,16 @@ PaletteWidget::PaletteWidget(PaintableWidget& paintable_widget, GWidget* parent) set_secondary_color(color); }; - auto* color_container = new GWidget(this); + auto color_container = GWidget::construct(this); color_container->set_relative_rect(m_secondary_color_widget->relative_rect().right() + 2, 2, 500, 32); color_container->set_layout(make<GBoxLayout>(Orientation::Vertical)); color_container->layout()->set_spacing(1); - auto* top_color_container = new GWidget(color_container); + auto top_color_container = GWidget::construct(color_container); top_color_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); top_color_container->layout()->set_spacing(1); - auto* bottom_color_container = new GWidget(color_container); + auto bottom_color_container = GWidget::construct(color_container); bottom_color_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); bottom_color_container->layout()->set_spacing(1); diff --git a/Applications/PaintBrush/main.cpp b/Applications/PaintBrush/main.cpp index df2bef0308..60bea29517 100644 --- a/Applications/PaintBrush/main.cpp +++ b/Applications/PaintBrush/main.cpp @@ -19,14 +19,14 @@ int main(int argc, char** argv) window->set_title("PaintBrush"); window->set_rect(100, 100, 640, 480); - auto* horizontal_container = new GWidget(nullptr); + auto horizontal_container = GWidget::construct(nullptr); window->set_main_widget(horizontal_container); horizontal_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); horizontal_container->layout()->set_spacing(0); new ToolboxWidget(horizontal_container); - 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(0); diff --git a/Applications/SoundPlayer/main.cpp b/Applications/SoundPlayer/main.cpp index 3df9522139..f3f77ad6f1 100644 --- a/Applications/SoundPlayer/main.cpp +++ b/Applications/SoundPlayer/main.cpp @@ -34,7 +34,7 @@ int main(int argc, char** argv) window->set_title("SoundPlayer"); window->set_rect(300, 300, 300, 200); - auto* widget = new GWidget; + auto widget = GWidget::construct(); window->set_main_widget(widget); widget->set_fill_with_background_color(true); diff --git a/Applications/SystemMonitor/MemoryStatsWidget.cpp b/Applications/SystemMonitor/MemoryStatsWidget.cpp index b0d1331d61..889bea9114 100644 --- a/Applications/SystemMonitor/MemoryStatsWidget.cpp +++ b/Applications/SystemMonitor/MemoryStatsWidget.cpp @@ -23,7 +23,7 @@ MemoryStatsWidget::MemoryStatsWidget(GraphWidget& graph, GWidget* parent) layout()->set_spacing(3); auto build_widgets_for_label = [this](const String& description) -> ObjectPtr<GLabel> { - auto* container = new GWidget(this); + auto container = GWidget::construct(this); container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); container->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed); container->set_preferred_size(275, 12); diff --git a/Applications/SystemMonitor/main.cpp b/Applications/SystemMonitor/main.cpp index fdf1c89d2b..f9df9368cb 100644 --- a/Applications/SystemMonitor/main.cpp +++ b/Applications/SystemMonitor/main.cpp @@ -47,7 +47,7 @@ int main(int argc, char** argv) { GApplication app(argc, argv); - auto* keeper = new GWidget; + auto keeper = GWidget::construct(); keeper->set_layout(make<GBoxLayout>(Orientation::Vertical)); keeper->set_fill_with_background_color(true); keeper->set_background_color(Color::WarmGray); @@ -58,9 +58,9 @@ int main(int argc, char** argv) auto process_container_splitter = GSplitter::construct(Orientation::Vertical, nullptr); tabwidget->add_widget("Processes", process_container_splitter); - auto* process_table_container = new GWidget(process_container_splitter); + auto process_table_container = GWidget::construct(process_container_splitter); - auto* graphs_container = new GWidget; + auto graphs_container = GWidget::construct(); graphs_container->set_fill_with_background_color(true); graphs_container->set_background_color(Color::WarmGray); graphs_container->set_layout(make<GBoxLayout>(Orientation::Vertical)); @@ -236,7 +236,7 @@ public: GWidget* build_file_systems_tab() { - auto* fs_widget = new GWidget(nullptr); + auto fs_widget = GWidget::construct(); fs_widget->set_layout(make<GBoxLayout>(Orientation::Vertical)); fs_widget->layout()->set_margins({ 4, 4, 4, 4 }); auto fs_table_view = GTableView::construct(fs_widget); @@ -301,7 +301,7 @@ GWidget* build_file_systems_tab() GWidget* build_pci_devices_tab() { - auto* pci_widget = new GWidget(nullptr); + auto pci_widget = GWidget::construct(nullptr); pci_widget->set_layout(make<GBoxLayout>(Orientation::Vertical)); pci_widget->layout()->set_margins({ 4, 4, 4, 4 }); auto pci_table_view = GTableView::construct(pci_widget); @@ -355,7 +355,7 @@ GWidget* build_pci_devices_tab() GWidget* build_devices_tab() { - auto* devices_widget = new GWidget(nullptr); + auto devices_widget = GWidget::construct(); devices_widget->set_layout(make<GBoxLayout>(Orientation::Vertical)); devices_widget->layout()->set_margins({ 4, 4, 4, 4 }); diff --git a/Applications/Terminal/main.cpp b/Applications/Terminal/main.cpp index 8304cd0b25..55971ad5e1 100644 --- a/Applications/Terminal/main.cpp +++ b/Applications/Terminal/main.cpp @@ -94,7 +94,7 @@ GWindow* create_settings_window(TerminalWidget& terminal, RefPtr<CConfigFile> co window->set_title("Terminal Settings"); window->set_rect(50, 50, 200, 140); - auto* settings = new GWidget; + auto settings = GWidget::construct(); window->set_main_widget(settings); settings->set_fill_with_background_color(true); settings->set_layout(make<GBoxLayout>(Orientation::Vertical)); diff --git a/Applications/TextEditor/TextEditorWidget.cpp b/Applications/TextEditor/TextEditorWidget.cpp index 723414a3b1..f0d98da6ef 100644 --- a/Applications/TextEditor/TextEditorWidget.cpp +++ b/Applications/TextEditor/TextEditorWidget.cpp @@ -34,7 +34,7 @@ TextEditorWidget::TextEditorWidget() update_title(); }; - m_find_widget = new GWidget(this); + m_find_widget = GWidget::construct(this); m_find_widget->set_fill_with_background_color(true); m_find_widget->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); m_find_widget->set_preferred_size(0, 22); diff --git a/Applications/TextEditor/TextEditorWidget.h b/Applications/TextEditor/TextEditorWidget.h index 3d465ea447..2b59e912d6 100644 --- a/Applications/TextEditor/TextEditorWidget.h +++ b/Applications/TextEditor/TextEditorWidget.h @@ -41,7 +41,7 @@ private: ObjectPtr<GTextBox> m_find_textbox; GButton* m_find_previous_button { nullptr }; GButton* m_find_next_button { nullptr }; - GWidget* m_find_widget { nullptr }; + ObjectPtr<GWidget> m_find_widget; bool m_document_dirty { false }; }; diff --git a/Applications/Welcome/main.cpp b/Applications/Welcome/main.cpp index 6e4bea273d..85608ce286 100644 --- a/Applications/Welcome/main.cpp +++ b/Applications/Welcome/main.cpp @@ -94,13 +94,13 @@ int main(int argc, char** argv) // main section // - auto* main_section = new GWidget(background); + auto main_section = GWidget::construct(background); main_section->set_layout(make<GBoxLayout>(Orientation::Horizontal)); main_section->layout()->set_margins({ 0, 0, 0, 0 }); main_section->layout()->set_spacing(8); main_section->set_size_policy(SizePolicy::Fill, SizePolicy::Fill); - auto* menu = new GWidget(main_section); + auto menu = GWidget::construct(main_section); menu->set_layout(make<GBoxLayout>(Orientation::Vertical)); menu->layout()->set_margins({ 0, 0, 0, 0 }); menu->layout()->set_spacing(8); @@ -111,7 +111,7 @@ int main(int argc, char** argv) stack->set_size_policy(SizePolicy::Fill, SizePolicy::Fill); for (auto& page : pages) { - auto* content = new GWidget(stack); + auto content = GWidget::construct(stack); content->set_layout(make<GBoxLayout>(Orientation::Vertical)); content->layout()->set_margins({ 0, 0, 0, 0 }); content->layout()->set_spacing(8); @@ -139,7 +139,7 @@ int main(int argc, char** argv) menu_option->set_text_alignment(TextAlignment::CenterLeft); menu_option->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); menu_option->set_preferred_size(0, 20); - menu_option->on_click = [stack, content](GButton&) { + menu_option->on_click = [&](auto&) { stack->set_active_widget(content); content->invalidate_layout(); }; diff --git a/Demos/HelloWorld/main.cpp b/Demos/HelloWorld/main.cpp index 6b06b9ed5b..e460ca0cb3 100644 --- a/Demos/HelloWorld/main.cpp +++ b/Demos/HelloWorld/main.cpp @@ -13,7 +13,7 @@ int main(int argc, char** argv) window->set_rect(100, 100, 240, 160); window->set_title("Hello World!"); - auto* main_widget = new GWidget; + auto main_widget = GWidget::construct(); window->set_main_widget(main_widget); main_widget->set_fill_with_background_color(true); main_widget->set_background_color(Color::White); diff --git a/Demos/WidgetGallery/main.cpp b/Demos/WidgetGallery/main.cpp index 8ff79ebb72..fcbbf00ed2 100755 --- a/Demos/WidgetGallery/main.cpp +++ b/Demos/WidgetGallery/main.cpp @@ -22,7 +22,7 @@ int main(int argc, char** argv) window->set_rect(100, 100, 320, 620); window->set_title("Widget Gallery"); - auto* main_widget = new GWidget; + auto main_widget = GWidget::construct(); window->set_main_widget(main_widget); main_widget->set_fill_with_background_color(true); main_widget->set_layout(make<GBoxLayout>(Orientation::Vertical)); @@ -66,7 +66,7 @@ int main(int argc, char** argv) auto spinbox2 = GSpinBox::construct(main_widget); spinbox2->set_enabled(false); - auto* vertical_slider_container = new GWidget(main_widget); + auto vertical_slider_container = GWidget::construct(main_widget); vertical_slider_container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); vertical_slider_container->set_preferred_size(0, 100); vertical_slider_container->set_layout(make<GBoxLayout>(Orientation::Horizontal)); diff --git a/DevTools/Inspector/main.cpp b/DevTools/Inspector/main.cpp index 892f482acb..ed2d98f6f1 100644 --- a/DevTools/Inspector/main.cpp +++ b/DevTools/Inspector/main.cpp @@ -32,7 +32,7 @@ int main(int argc, char** argv) window->set_title("Inspector"); window->set_rect(150, 150, 300, 500); - auto* widget = new GWidget; + auto widget = GWidget::construct(); window->set_main_widget(widget); widget->set_fill_with_background_color(true); widget->set_layout(make<GBoxLayout>(Orientation::Vertical)); diff --git a/DevTools/VisualBuilder/VBPropertiesWindow.cpp b/DevTools/VisualBuilder/VBPropertiesWindow.cpp index ff2759e5b7..78fceb2ced 100644 --- a/DevTools/VisualBuilder/VBPropertiesWindow.cpp +++ b/DevTools/VisualBuilder/VBPropertiesWindow.cpp @@ -55,7 +55,7 @@ VBPropertiesWindow::VBPropertiesWindow() set_title("Properties"); set_rect(780, 200, 240, 280); - auto* widget = new GWidget; + auto widget = GWidget::construct(); widget->set_fill_with_background_color(true); widget->set_layout(make<GBoxLayout>(Orientation::Vertical)); widget->layout()->set_margins({ 2, 2, 2, 2 }); diff --git a/DevTools/VisualBuilder/VBWidget.cpp b/DevTools/VisualBuilder/VBWidget.cpp index f1b3a1879e..f1e341a1d8 100644 --- a/DevTools/VisualBuilder/VBWidget.cpp +++ b/DevTools/VisualBuilder/VBWidget.cpp @@ -30,7 +30,7 @@ VBWidget::~VBWidget() { m_form.m_gwidget_map.remove(m_gwidget); m_form.m_selected_widgets.remove(this); - delete m_gwidget; + m_gwidget->parent()->remove_child(*m_gwidget); } Rect VBWidget::rect() const diff --git a/DevTools/VisualBuilder/VBWidget.h b/DevTools/VisualBuilder/VBWidget.h index 793d15a44f..735e532afa 100644 --- a/DevTools/VisualBuilder/VBWidget.h +++ b/DevTools/VisualBuilder/VBWidget.h @@ -8,10 +8,10 @@ #include <AK/RefCounted.h> #include <AK/Weakable.h> #include <LibDraw/Rect.h> +#include <LibGUI/GWidget.h> class GPainter; class GVariant; -class GWidget; class VBForm; class VBProperty; class VBWidgetPropertyModel; @@ -81,7 +81,7 @@ private: VBWidgetType m_type { VBWidgetType::None }; VBForm& m_form; - GWidget* m_gwidget { nullptr }; + ObjectPtr<GWidget> m_gwidget; NonnullOwnPtrVector<VBProperty> m_properties; NonnullRefPtr<VBWidgetPropertyModel> m_property_model; Rect m_transform_origin_rect; diff --git a/DevTools/VisualBuilder/VBWidgetRegistry.cpp b/DevTools/VisualBuilder/VBWidgetRegistry.cpp index e3b9ca25a5..ef02c0a79a 100644 --- a/DevTools/VisualBuilder/VBWidgetRegistry.cpp +++ b/DevTools/VisualBuilder/VBWidgetRegistry.cpp @@ -68,11 +68,11 @@ VBWidgetType widget_type_from_class_name(const StringView& name) ASSERT_NOT_REACHED(); } -static GWidget* build_gwidget(VBWidgetType type, GWidget* parent) +static ObjectPtr<GWidget> build_gwidget(VBWidgetType type, GWidget* parent) { switch (type) { case VBWidgetType::GWidget: - return new GWidget(parent); + return GWidget::construct(parent); case VBWidgetType::GScrollBar: return GScrollBar::construct(Orientation::Vertical, parent); case VBWidgetType::GGroupBox: @@ -113,21 +113,21 @@ static GWidget* build_gwidget(VBWidgetType type, GWidget* parent) return slider; } case VBWidgetType::GCheckBox: { - auto* box = new GCheckBox(parent); + auto box = GCheckBox::construct(parent); box->set_text("checkbox_1"); return box; } case VBWidgetType::GRadioButton: - return new GRadioButton("radio_1", parent); + return GRadioButton::construct("radio_1", parent); default: ASSERT_NOT_REACHED(); return nullptr; } } -GWidget* VBWidgetRegistry::build_gwidget(VBWidget& widget, VBWidgetType type, GWidget* parent, NonnullOwnPtrVector<VBProperty>& properties) +ObjectPtr<GWidget> VBWidgetRegistry::build_gwidget(VBWidget& widget, VBWidgetType type, GWidget* parent, NonnullOwnPtrVector<VBProperty>& properties) { - auto* gwidget = ::build_gwidget(type, parent); + auto gwidget = ::build_gwidget(type, parent); auto add_readonly_property = [&](const String& name, const GVariant& value) { auto property = make<VBProperty>(widget, name, value); property->set_readonly(true); diff --git a/DevTools/VisualBuilder/VBWidgetRegistry.h b/DevTools/VisualBuilder/VBWidgetRegistry.h index 2d30ed7831..93f1fb091b 100644 --- a/DevTools/VisualBuilder/VBWidgetRegistry.h +++ b/DevTools/VisualBuilder/VBWidgetRegistry.h @@ -1,12 +1,12 @@ #pragma once #include "VBWidgetType.h" -#include <AK/String.h> #include <AK/HashMap.h> #include <AK/NonnullOwnPtrVector.h> #include <AK/OwnPtr.h> +#include <AK/String.h> +#include <LibGUI/GWidget.h> -class GWidget; class VBProperty; class VBWidget; @@ -19,7 +19,7 @@ public: callback((VBWidgetType)i); } - static GWidget* build_gwidget(VBWidget&, VBWidgetType, GWidget* parent, NonnullOwnPtrVector<VBProperty>&); + static ObjectPtr<GWidget> build_gwidget(VBWidget&, VBWidgetType, GWidget* parent, NonnullOwnPtrVector<VBProperty>&); }; String to_class_name(VBWidgetType); diff --git a/DevTools/VisualBuilder/main.cpp b/DevTools/VisualBuilder/main.cpp index 986050abdb..1d314b8467 100644 --- a/DevTools/VisualBuilder/main.cpp +++ b/DevTools/VisualBuilder/main.cpp @@ -80,7 +80,7 @@ GWindow* make_toolbox_window() window->set_title("Widgets"); window->set_rect(20, 200, 80, 300); - auto* widget = new GWidget; + auto widget = GWidget::construct(); widget->set_fill_with_background_color(true); widget->set_layout(make<GBoxLayout>(Orientation::Vertical)); widget->layout()->set_spacing(0); diff --git a/Games/Minesweeper/main.cpp b/Games/Minesweeper/main.cpp index 8d6afb4883..96ad49e1bd 100644 --- a/Games/Minesweeper/main.cpp +++ b/Games/Minesweeper/main.cpp @@ -19,12 +19,12 @@ int main(int argc, char** argv) window->set_title("Minesweeper"); window->set_rect(100, 100, 139, 175); - auto* widget = new GWidget; + auto widget = GWidget::construct(); window->set_main_widget(widget); widget->set_layout(make<GBoxLayout>(Orientation::Vertical)); widget->layout()->set_spacing(0); - auto* container = new GWidget(widget); + auto container = GWidget::construct(widget); container->set_fill_with_background_color(true); container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); container->set_preferred_size(0, 36); 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; |