mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 08:02:44 +00:00 
			
		
		
		
	TextEditor: Make use of GFilePicker to select a file to open
This commit is contained in:
		
							parent
							
								
									23c90da26c
								
							
						
					
					
						commit
						cef161c2bc
					
				
					 1 changed files with 22 additions and 11 deletions
				
			
		|  | @ -2,6 +2,8 @@ | |||
| #include <LibGUI/GWidget.h> | ||||
| #include <LibGUI/GBoxLayout.h> | ||||
| #include <LibGUI/GApplication.h> | ||||
| #include <LibGUI/GFilePicker.h> | ||||
| #include <LibGUI/GMessageBox.h> | ||||
| #include <LibGUI/GStatusBar.h> | ||||
| #include <LibGUI/GToolBar.h> | ||||
| #include <LibGUI/GMenuBar.h> | ||||
|  | @ -15,10 +17,23 @@ | |||
| #include <signal.h> | ||||
| #include <fcntl.h> | ||||
| 
 | ||||
| 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<GBoxLayout>(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); | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Robin Burchell
						Robin Burchell