mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 18:02:05 -05:00
LibGUI: Don't use Core::Object::add() to instantiate dialogs
Now that add() returns a WidgetType&, we can't rely on the parent of a GUI::Dialog to still keep it alive after exec() returns. This happens because exec() will call remove_from_parent() on itself before returning. And so we go back to the old idiom for creating a GUI::Dialog centered above a specific window. Just call GUI::Dialog::construct(), passing the "parent" window as the last parameter.
This commit is contained in:
parent
dfa69b82b4
commit
b29ff7b821
18 changed files with 43 additions and 43 deletions
|
@ -36,8 +36,8 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
PropertiesDialog::PropertiesDialog(GUI::FileSystemModel& model, String path, bool disable_rename, Core::Object* parent)
|
PropertiesDialog::PropertiesDialog(GUI::FileSystemModel& model, String path, bool disable_rename, Window* parent_window)
|
||||||
: Dialog(parent)
|
: Dialog(parent_window)
|
||||||
, m_model(model)
|
, m_model(model)
|
||||||
{
|
{
|
||||||
auto file_path = FileSystemPath(path);
|
auto file_path = FileSystemPath(path);
|
||||||
|
|
|
@ -40,7 +40,7 @@ public:
|
||||||
virtual ~PropertiesDialog() override;
|
virtual ~PropertiesDialog() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PropertiesDialog(GUI::FileSystemModel&, String, bool disable_rename, Core::Object* parent = nullptr);
|
PropertiesDialog(GUI::FileSystemModel&, String, bool disable_rename, Window* parent = nullptr);
|
||||||
|
|
||||||
struct PropertyValuePair {
|
struct PropertyValuePair {
|
||||||
String property;
|
String property;
|
||||||
|
|
|
@ -171,12 +171,12 @@ int main(int argc, char** argv)
|
||||||
});
|
});
|
||||||
|
|
||||||
auto mkdir_action = GUI::Action::create("New directory...", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/mkdir.png"), [&](const GUI::Action&) {
|
auto mkdir_action = GUI::Action::create("New directory...", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/mkdir.png"), [&](const GUI::Action&) {
|
||||||
auto& input_box = window->add<GUI::InputBox>("Enter name:", "New directory");
|
auto input_box = GUI::InputBox::construct("Enter name:", "New directory", window);
|
||||||
if (input_box.exec() == GUI::InputBox::ExecOK && !input_box.text_value().is_empty()) {
|
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
|
||||||
auto new_dir_path = canonicalized_path(
|
auto new_dir_path = canonicalized_path(
|
||||||
String::format("%s/%s",
|
String::format("%s/%s",
|
||||||
directory_view.path().characters(),
|
directory_view.path().characters(),
|
||||||
input_box.text_value().characters()));
|
input_box->text_value().characters()));
|
||||||
int rc = mkdir(new_dir_path.characters(), 0777);
|
int rc = mkdir(new_dir_path.characters(), 0777);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
GUI::MessageBox::show(String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
|
GUI::MessageBox::show(String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
|
||||||
|
|
|
@ -78,7 +78,7 @@ HexEditorWidget::HexEditorWidget()
|
||||||
m_save_action->activate();
|
m_save_action->activate();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto input_box = GUI::InputBox::construct("Enter new file size:", "New file size", this);
|
auto input_box = GUI::InputBox::construct("Enter new file size:", "New file size", window());
|
||||||
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
|
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
|
||||||
auto valid = false;
|
auto valid = false;
|
||||||
auto file_size = input_box->text_value().to_int(valid);
|
auto file_size = input_box->text_value().to_int(valid);
|
||||||
|
@ -154,7 +154,7 @@ HexEditorWidget::HexEditorWidget()
|
||||||
}
|
}
|
||||||
|
|
||||||
m_goto_decimal_offset_action = GUI::Action::create("Go To Offset (Decimal)...", { Mod_Ctrl | Mod_Shift, Key_G }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), [this](const GUI::Action&) {
|
m_goto_decimal_offset_action = GUI::Action::create("Go To Offset (Decimal)...", { Mod_Ctrl | Mod_Shift, Key_G }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), [this](const GUI::Action&) {
|
||||||
auto input_box = GUI::InputBox::construct("Enter Decimal offset:", "Go To", this);
|
auto input_box = GUI::InputBox::construct("Enter Decimal offset:", "Go To", window());
|
||||||
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
|
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
|
||||||
auto valid = false;
|
auto valid = false;
|
||||||
auto new_offset = input_box->text_value().to_int(valid);
|
auto new_offset = input_box->text_value().to_int(valid);
|
||||||
|
@ -165,7 +165,7 @@ HexEditorWidget::HexEditorWidget()
|
||||||
});
|
});
|
||||||
|
|
||||||
m_goto_hex_offset_action = GUI::Action::create("Go To Offset (Hex)...", { Mod_Ctrl, Key_G }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), [this](const GUI::Action&) {
|
m_goto_hex_offset_action = GUI::Action::create("Go To Offset (Hex)...", { Mod_Ctrl, Key_G }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), [this](const GUI::Action&) {
|
||||||
auto input_box = GUI::InputBox::construct("Enter Hex offset:", "Go To", this);
|
auto input_box = GUI::InputBox::construct("Enter Hex offset:", "Go To", window());
|
||||||
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
|
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
|
||||||
auto new_offset = strtol(input_box->text_value().characters(), nullptr, 16);
|
auto new_offset = strtol(input_box->text_value().characters(), nullptr, 16);
|
||||||
m_editor->set_position(new_offset);
|
m_editor->set_position(new_offset);
|
||||||
|
@ -174,7 +174,7 @@ HexEditorWidget::HexEditorWidget()
|
||||||
|
|
||||||
auto edit_menu = GUI::Menu::construct("Edit");
|
auto edit_menu = GUI::Menu::construct("Edit");
|
||||||
edit_menu->add_action(GUI::Action::create("Fill selection...", { Mod_Ctrl, Key_B }, [&](const GUI::Action&) {
|
edit_menu->add_action(GUI::Action::create("Fill selection...", { Mod_Ctrl, Key_B }, [&](const GUI::Action&) {
|
||||||
auto input_box = GUI::InputBox::construct("Fill byte (hex):", "Fill Selection", this);
|
auto input_box = GUI::InputBox::construct("Fill byte (hex):", "Fill Selection", window());
|
||||||
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
|
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
|
||||||
auto fill_byte = strtol(input_box->text_value().characters(), nullptr, 16);
|
auto fill_byte = strtol(input_box->text_value().characters(), nullptr, 16);
|
||||||
m_editor->fill_selection(fill_byte);
|
m_editor->fill_selection(fill_byte);
|
||||||
|
|
|
@ -94,12 +94,12 @@ void IRCAppWindow::setup_client()
|
||||||
};
|
};
|
||||||
|
|
||||||
if (m_client->hostname().is_empty()) {
|
if (m_client->hostname().is_empty()) {
|
||||||
auto& input_box = add<GUI::InputBox>("Enter server:", "Connect to server");
|
auto input_box = GUI::InputBox::construct("Enter server:", "Connect to server", this);
|
||||||
auto result = input_box.exec();
|
auto result = input_box->exec();
|
||||||
if (result == GUI::InputBox::ExecCancel)
|
if (result == GUI::InputBox::ExecCancel)
|
||||||
::exit(0);
|
::exit(0);
|
||||||
|
|
||||||
m_client->set_server(input_box.text_value(), 6667);
|
m_client->set_server(input_box->text_value(), 6667);
|
||||||
}
|
}
|
||||||
update_title();
|
update_title();
|
||||||
bool success = m_client->connect();
|
bool success = m_client->connect();
|
||||||
|
@ -109,9 +109,9 @@ void IRCAppWindow::setup_client()
|
||||||
void IRCAppWindow::setup_actions()
|
void IRCAppWindow::setup_actions()
|
||||||
{
|
{
|
||||||
m_join_action = GUI::Action::create("Join channel", { Mod_Ctrl, Key_J }, Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-join.png"), [&](auto&) {
|
m_join_action = GUI::Action::create("Join channel", { Mod_Ctrl, Key_J }, Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-join.png"), [&](auto&) {
|
||||||
auto& input_box = add<GUI::InputBox>("Enter channel name:", "Join channel");
|
auto input_box = GUI::InputBox::construct("Enter channel name:", "Join channel", this);
|
||||||
if (input_box.exec() == GUI::InputBox::ExecOK && !input_box.text_value().is_empty())
|
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
|
||||||
m_client->handle_join_action(input_box.text_value());
|
m_client->handle_join_action(input_box->text_value());
|
||||||
});
|
});
|
||||||
|
|
||||||
m_part_action = GUI::Action::create("Part from channel", { Mod_Ctrl, Key_P }, Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-part.png"), [this](auto&) {
|
m_part_action = GUI::Action::create("Part from channel", { Mod_Ctrl, Key_P }, Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-part.png"), [this](auto&) {
|
||||||
|
|
|
@ -175,10 +175,10 @@ int main(int argc, char** argv)
|
||||||
};
|
};
|
||||||
|
|
||||||
auto new_action = GUI::Action::create("Add new file to project...", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GUI::Action&) {
|
auto new_action = GUI::Action::create("Add new file to project...", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GUI::Action&) {
|
||||||
auto& input_box = g_window->add<GUI::InputBox>("Enter name of new file:", "Add new file to project");
|
auto input_box = GUI::InputBox::construct("Enter name of new file:", "Add new file to project", g_window);
|
||||||
if (input_box.exec() == GUI::InputBox::ExecCancel)
|
if (input_box->exec() == GUI::InputBox::ExecCancel)
|
||||||
return;
|
return;
|
||||||
auto filename = input_box.text_value();
|
auto filename = input_box->text_value();
|
||||||
auto file = Core::File::construct(filename);
|
auto file = Core::File::construct(filename);
|
||||||
if (!file->open((Core::IODevice::OpenMode)(Core::IODevice::WriteOnly | Core::IODevice::MustBeNew))) {
|
if (!file->open((Core::IODevice::OpenMode)(Core::IODevice::WriteOnly | Core::IODevice::MustBeNew))) {
|
||||||
GUI::MessageBox::show(String::format("Failed to create '%s'", filename.characters()), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, g_window);
|
GUI::MessageBox::show(String::format("Failed to create '%s'", filename.characters()), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, g_window);
|
||||||
|
|
|
@ -33,8 +33,8 @@
|
||||||
|
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
|
||||||
AboutDialog::AboutDialog(const StringView& name, const Gfx::Bitmap* icon, Core::Object* parent)
|
AboutDialog::AboutDialog(const StringView& name, const Gfx::Bitmap* icon, Window* parent_window)
|
||||||
: Dialog(parent)
|
: Dialog(parent_window)
|
||||||
, m_name(name)
|
, m_name(name)
|
||||||
, m_icon(icon)
|
, m_icon(icon)
|
||||||
{
|
{
|
||||||
|
|
|
@ -35,14 +35,14 @@ class AboutDialog final : public Dialog {
|
||||||
public:
|
public:
|
||||||
virtual ~AboutDialog() override;
|
virtual ~AboutDialog() override;
|
||||||
|
|
||||||
static void show(const StringView& name, const Gfx::Bitmap* icon = nullptr, Core::Object* parent = nullptr)
|
static void show(const StringView& name, const Gfx::Bitmap* icon = nullptr, Window* parent_window = nullptr)
|
||||||
{
|
{
|
||||||
auto dialog = AboutDialog::construct(name, icon, parent);
|
auto dialog = AboutDialog::construct(name, icon, parent_window);
|
||||||
dialog->exec();
|
dialog->exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
AboutDialog(const StringView& name, const Gfx::Bitmap* icon = nullptr, Core::Object* parent = nullptr);
|
AboutDialog(const StringView& name, const Gfx::Bitmap* icon = nullptr, Window* parent_window = nullptr);
|
||||||
|
|
||||||
String m_name;
|
String m_name;
|
||||||
RefPtr<Gfx::Bitmap> m_icon;
|
RefPtr<Gfx::Bitmap> m_icon;
|
||||||
|
|
|
@ -34,8 +34,8 @@
|
||||||
|
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
|
||||||
ColorPicker::ColorPicker(Color color, Core::Object* parent)
|
ColorPicker::ColorPicker(Color color, Window* parent_window)
|
||||||
: Dialog(parent)
|
: Dialog(parent_window)
|
||||||
, m_color(color)
|
, m_color(color)
|
||||||
{
|
{
|
||||||
set_title("Edit Color");
|
set_title("Edit Color");
|
||||||
|
|
|
@ -38,7 +38,7 @@ public:
|
||||||
Color color() const { return m_color; }
|
Color color() const { return m_color; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit ColorPicker(Color, Core::Object* parent = nullptr);
|
explicit ColorPicker(Color, Window* parent_window = nullptr);
|
||||||
|
|
||||||
void build();
|
void build();
|
||||||
|
|
||||||
|
|
|
@ -31,8 +31,8 @@
|
||||||
|
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
|
||||||
Dialog::Dialog(Core::Object* parent)
|
Dialog::Dialog(Window* parent_window)
|
||||||
: Window(parent)
|
: Window(parent_window)
|
||||||
{
|
{
|
||||||
set_modal(true);
|
set_modal(true);
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,7 +53,7 @@ public:
|
||||||
virtual void close() override;
|
virtual void close() override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit Dialog(Core::Object* parent);
|
explicit Dialog(Window* parent_window);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
OwnPtr<Core::EventLoop> m_event_loop;
|
OwnPtr<Core::EventLoop> m_event_loop;
|
||||||
|
|
|
@ -74,8 +74,8 @@ Optional<String> FilePicker::get_save_filepath(const String& title, const String
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
FilePicker::FilePicker(Mode mode, const StringView& file_name, const StringView& path, Core::Object* parent)
|
FilePicker::FilePicker(Mode mode, const StringView& file_name, const StringView& path, Window* parent_window)
|
||||||
: Dialog(parent)
|
: Dialog(parent_window)
|
||||||
, m_model(FileSystemModel::create())
|
, m_model(FileSystemModel::create())
|
||||||
, m_mode(mode)
|
, m_mode(mode)
|
||||||
{
|
{
|
||||||
|
|
|
@ -52,7 +52,7 @@ private:
|
||||||
void clear_preview();
|
void clear_preview();
|
||||||
void on_file_return();
|
void on_file_return();
|
||||||
|
|
||||||
FilePicker(Mode type = Mode::Open, const StringView& file_name = "Untitled", const StringView& path = String(get_current_user_home_path()), Core::Object* parent = nullptr);
|
FilePicker(Mode type = Mode::Open, const StringView& file_name = "Untitled", const StringView& path = String(get_current_user_home_path()), Window* parent_window = nullptr);
|
||||||
|
|
||||||
static String ok_button_name(Mode mode)
|
static String ok_button_name(Mode mode)
|
||||||
{
|
{
|
||||||
|
|
|
@ -34,8 +34,8 @@
|
||||||
|
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
|
||||||
InputBox::InputBox(const StringView& prompt, const StringView& title, Core::Object* parent)
|
InputBox::InputBox(const StringView& prompt, const StringView& title, GUI::Window* parent_window)
|
||||||
: Dialog(parent)
|
: Dialog(parent_window)
|
||||||
, m_prompt(prompt)
|
, m_prompt(prompt)
|
||||||
{
|
{
|
||||||
set_title(title);
|
set_title(title);
|
||||||
|
|
|
@ -33,7 +33,7 @@ namespace GUI {
|
||||||
class InputBox : public Dialog {
|
class InputBox : public Dialog {
|
||||||
C_OBJECT(InputBox)
|
C_OBJECT(InputBox)
|
||||||
public:
|
public:
|
||||||
explicit InputBox(const StringView& prompt, const StringView& title, Core::Object* parent = nullptr);
|
explicit InputBox(const StringView& prompt, const StringView& title, Window* parent_window = nullptr);
|
||||||
virtual ~InputBox() override;
|
virtual ~InputBox() override;
|
||||||
|
|
||||||
String text_value() const { return m_text_value; }
|
String text_value() const { return m_text_value; }
|
||||||
|
|
|
@ -33,16 +33,16 @@
|
||||||
|
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
|
||||||
int MessageBox::show(const StringView& text, const StringView& title, Type type, InputType input_type, Core::Object* parent)
|
int MessageBox::show(const StringView& text, const StringView& title, Type type, InputType input_type, Window* parent_window)
|
||||||
{
|
{
|
||||||
auto box = MessageBox::construct(text, title, type, input_type);
|
auto box = MessageBox::construct(text, title, type, input_type);
|
||||||
if (parent)
|
if (parent_window)
|
||||||
parent->add_child(box);
|
parent_window->add_child(box);
|
||||||
return box->exec();
|
return box->exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
MessageBox::MessageBox(const StringView& text, const StringView& title, Type type, InputType input_type, Core::Object* parent)
|
MessageBox::MessageBox(const StringView& text, const StringView& title, Type type, InputType input_type, Window* parent_window)
|
||||||
: Dialog(parent)
|
: Dialog(parent_window)
|
||||||
, m_text(text)
|
, m_text(text)
|
||||||
, m_type(type)
|
, m_type(type)
|
||||||
, m_input_type(input_type)
|
, m_input_type(input_type)
|
||||||
|
|
|
@ -49,10 +49,10 @@ public:
|
||||||
|
|
||||||
virtual ~MessageBox() override;
|
virtual ~MessageBox() override;
|
||||||
|
|
||||||
static int show(const StringView& text, const StringView& title, Type type = Type::None, InputType = InputType::OK, Core::Object* parent = nullptr);
|
static int show(const StringView& text, const StringView& title, Type type = Type::None, InputType = InputType::OK, Window* parent_window = nullptr);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit MessageBox(const StringView& text, const StringView& title, Type type = Type::None, InputType = InputType::OK, Core::Object* parent = nullptr);
|
explicit MessageBox(const StringView& text, const StringView& title, Type type = Type::None, InputType = InputType::OK, Window* parent_window = nullptr);
|
||||||
|
|
||||||
bool should_include_ok_button() const;
|
bool should_include_ok_button() const;
|
||||||
bool should_include_cancel_button() const;
|
bool should_include_cancel_button() const;
|
||||||
|
|
Loading…
Add table
Reference in a new issue