diff --git a/Applications/TextEditor/main.cpp b/Applications/TextEditor/main.cpp index d665788cc35..55e0651b0ce 100644 --- a/Applications/TextEditor/main.cpp +++ b/Applications/TextEditor/main.cpp @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include #include #include @@ -15,10 +17,23 @@ #include #include +void open_sesame(GWindow& window, GTextEditor& editor, const String& path) +{ + CFile file(path); + + if (!file.open(CIODevice::ReadOnly)) { + GMessageBox::show(String::format("Opening \"%s\" failed: %s", path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, &window); + } + + window.set_title(String::format("TextEditor: %s", path.characters())); + editor.set_text(String::copy(file.read_all())); +} + int main(int argc, char** argv) { GApplication app(argc, argv); + auto* window = new GWindow; auto* widget = new GWidget; widget->set_layout(make(Orientation::Vertical)); widget->layout()->set_spacing(0); @@ -38,21 +53,19 @@ int main(int argc, char** argv) String path = "/tmp/TextEditor.save.txt"; if (argc >= 2) { path = argv[1]; - CFile file(path); - - if (!file.open(CIODevice::ReadOnly)) { - fprintf(stderr, "Opening %s: %s\n", path.characters(), file.error_string()); - return 1; - } - text_editor->set_text(String::copy(file.read_all())); + open_sesame(*window, *text_editor, path); } auto new_action = GAction::create("New document", { Mod_Ctrl, Key_N }, GraphicsBitmap::load_from_file("/res/icons/16x16/new.png"), [] (const GAction&) { dbgprintf("FIXME: Implement File/New\n"); }); - auto open_action = GAction::create("Open document", { Mod_Ctrl, Key_O }, GraphicsBitmap::load_from_file("/res/icons/16x16/open.png"), [] (const GAction&) { - dbgprintf("FIXME: Implement File/Open\n"); + auto open_action = GAction::create("Open document", { Mod_Ctrl, Key_O }, GraphicsBitmap::load_from_file("/res/icons/16x16/open.png"), [window, text_editor, &path](const GAction&) { + GFilePicker picker; + if (picker.exec() == GDialog::ExecOK) { + path = picker.selected_file(); + open_sesame(*window, *text_editor, path); + } }); auto save_action = GAction::create("Save document", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [&] (const GAction&) { @@ -117,8 +130,6 @@ int main(int argc, char** argv) toolbar->add_action(text_editor->undo_action()); toolbar->add_action(text_editor->redo_action()); - auto* window = new GWindow; - window->set_title(String::format("TextEditor: %s", path.characters())); window->set_rect(20, 200, 640, 400); window->set_main_widget(widget); window->set_should_exit_event_loop_on_close(true);