mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-10 13:00:29 +03:00
GTextEditor: Add write_to_file(String path) :^)
This commit is contained in:
parent
a5bc20c733
commit
187d7cb400
Notes:
sideshowbarker
2024-07-19 15:08:13 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/187d7cb4001
@ -66,8 +66,9 @@ int main(int argc, char** argv)
|
||||
dbgprintf("FIXME: Implement File/Open");
|
||||
});
|
||||
|
||||
auto save_action = GAction::create("Save document", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/save16.rgb", { 16, 16 }), [] (const GAction&) {
|
||||
dbgprintf("FIXME: Implement File/Save");
|
||||
auto save_action = GAction::create("Save document", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/save16.rgb", { 16, 16 }), [&] (const GAction&) {
|
||||
dbgprintf("Writing document to '%s'\n", path.characters());
|
||||
text_editor->write_to_file(path);
|
||||
});
|
||||
|
||||
auto menubar = make<GMenuBar>();
|
||||
|
@ -3,6 +3,9 @@
|
||||
#include <LibGUI/GFontDatabase.h>
|
||||
#include <SharedGraphics/Painter.h>
|
||||
#include <Kernel/KeyCode.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
|
||||
GTextEditor::GTextEditor(GWidget* parent)
|
||||
: GWidget(parent)
|
||||
@ -429,3 +432,35 @@ void GTextEditor::Line::truncate(int length)
|
||||
m_text.resize(length + 1);
|
||||
m_text.last() = 0;
|
||||
}
|
||||
|
||||
bool GTextEditor::write_to_file(const String& path)
|
||||
{
|
||||
int fd = open(path.characters(), O_WRONLY | O_CREAT, 0666);
|
||||
if (fd < 0) {
|
||||
perror("open");
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < m_lines.size(); ++i) {
|
||||
auto& line = *m_lines[i];
|
||||
if (line.length()) {
|
||||
ssize_t nwritten = write(fd, line.characters(), line.length());
|
||||
if (nwritten < 0) {
|
||||
perror("write");
|
||||
close(fd);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (i != m_lines.size() - 1) {
|
||||
char ch = '\n';
|
||||
ssize_t nwritten = write(fd, &ch, 1);
|
||||
if (nwritten != 1) {
|
||||
perror("write");
|
||||
close(fd);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return true;
|
||||
}
|
||||
|
@ -49,6 +49,8 @@ public:
|
||||
GTextPosition cursor() const { return m_cursor; }
|
||||
int glyph_width() const { return font().glyph_width('x'); }
|
||||
|
||||
bool write_to_file(const String& path);
|
||||
|
||||
private:
|
||||
virtual void paint_event(GPaintEvent&) override;
|
||||
virtual void resize_event(GResizeEvent&) override;
|
||||
|
Loading…
Reference in New Issue
Block a user