From 6003ea642a1fe3f191ff918c75481f1152504713 Mon Sep 17 00:00:00 2001 From: daleclack Date: Sun, 24 Jul 2022 17:59:57 +0800 Subject: [PATCH] Add config file for gtk137 --- .../gtk137_editor_keyboard/src/TextEditor.cc | 38 +++++++++++++++++-- .../gtk137_editor_keyboard/src/TextEditor.hh | 1 + 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Gtkmm3/gtk137_editor_keyboard/src/TextEditor.cc b/Gtkmm3/gtk137_editor_keyboard/src/TextEditor.cc index 2d57c6c..c8e0c0a 100644 --- a/Gtkmm3/gtk137_editor_keyboard/src/TextEditor.cc +++ b/Gtkmm3/gtk137_editor_keyboard/src/TextEditor.cc @@ -4,6 +4,8 @@ #include #include +using json = nlohmann::json; + // Only for build in this repository #define text_globs supported_globs @@ -14,11 +16,17 @@ TextEditor::TextEditor() file_opened(false) { // Load window config from json file - - + int width = 800, height = 450; + std::ifstream json_file("config.json"); + if(json_file.is_open()){ + json data = json::parse(json_file); + width = data["width"]; + height = data["height"]; + } + json_file.close(); // Initalize Window - set_default_size(800, 450); + vbox.set_size_request(width, height); set_icon_name("my_textedit"); // Initalize HeaderBar @@ -82,6 +90,9 @@ TextEditor::TextEditor() infobox->pack_start(label1); vbox.pack_start(infobar, Gtk::PACK_SHRINK); + // Save config when the window is closed + signal_delete_event().connect(sigc::mem_fun(*this, &TextEditor::window_delete_event)); + // Show everything vbox.pack_start(hbox); add(vbox); @@ -89,6 +100,27 @@ TextEditor::TextEditor() infobar.hide(); } +bool TextEditor::window_delete_event(GdkEventAny *event){ + // Create json raw data + json data = json::parse(R"({ + "width":800, + "height":450 + })"); + + // Override config in json file + data["width"] = vbox.get_width(); + data["height"] = vbox.get_height(); + + // Output json data to file + std::fstream outfile; + outfile.open("config.json", std::ios_base::out); + if(outfile.is_open()){ + outfile << data; + } + outfile.close(); + return false; +} + void TextEditor::btnopen_clicked() { // Create a dialog diff --git a/Gtkmm3/gtk137_editor_keyboard/src/TextEditor.hh b/Gtkmm3/gtk137_editor_keyboard/src/TextEditor.hh index f106654..b3c2136 100644 --- a/Gtkmm3/gtk137_editor_keyboard/src/TextEditor.hh +++ b/Gtkmm3/gtk137_editor_keyboard/src/TextEditor.hh @@ -35,6 +35,7 @@ private: bool file_opened; //Signal Handlers + bool window_delete_event(GdkEventAny *event); // File Operation functions void btnopen_clicked();