aboutsummaryrefslogtreecommitdiffstats
path: root/source/file.cpp
blob: b4332f4b26ac9d1fa10318ce7fb0f661c1cfb85d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
 * @file file.cpp
 * @brief Contains code for file-management-related UI elements and logic.
 *
 * Copyright (C) 2021 Clyne Sullivan
 *
 * Distributed under the GNU GPL v3 or later. You should have received a copy of
 * the GNU General Public License along with this program.
 * If not, see <https://www.gnu.org/licenses/>.
 */

#include "imgui.h"
#include "backends/imgui_impl_sdl.h"
#include "backends/imgui_impl_opengl2.h"
#include "ImGuiFileDialog.h"
#include "TextEditor.h"

#include "stmdsp_code.hpp"

#include <algorithm>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

#include <SDL2/SDL.h>

extern TextEditor editor;
extern void log(const std::string& str);

enum class FileAction {
    None,
    Open,
    Save,
    SaveAs
};

static FileAction fileAction = FileAction::None;
static std::string fileCurrentPath;
static std::vector<std::filesystem::path> fileExampleList;

static void saveCurrentFile()
{
    if (std::ofstream ofs (fileCurrentPath, std::ios::binary); ofs.good()) {
        const auto& text = editor.GetText();
        ofs.write(text.data(), text.size());
        log("Saved.");
    }
}

static void openCurrentFile()
{
    if (std::ifstream ifs (fileCurrentPath); ifs.good()) {
        std::ostringstream sstr;
        sstr << ifs.rdbuf();
        editor.SetText(sstr.str());
    }
}

static void openNewFile()
{
    fileCurrentPath.clear();
    editor.SetText(stmdsp::file_content);
}

static std::vector<std::filesystem::path> fileScanExamples()
{
    const auto path = std::filesystem::current_path() / "examples";
    const std::filesystem::recursive_directory_iterator rdi (path);

    std::vector<std::filesystem::path> list;
    std::transform(
        std::filesystem::begin(rdi),
        std::filesystem::end(rdi),
        std::back_inserter(list),
        [](const auto& file) { return file.path(); });
    std::sort(list.begin(), list.end());
    return list;
}

void fileInit()
{
    fileExampleList = fileScanExamples();
    openNewFile();
}

void fileRenderMenu()
{
    if (ImGui::BeginMenu("File")) {
        if (ImGui::MenuItem("New")) {
            // TODO modified?
            openNewFile();
            log("Ready.");
        }

        if (ImGui::MenuItem("Open")) {
            fileAction = FileAction::Open;
            ImGuiFileDialog::Instance()->OpenModal(
                "ChooseFileOpenSave", "Choose File", ".cpp", ".");
        }

        if (ImGui::BeginMenu("Open Example")) {
            for (const auto& file : fileExampleList) {
                if (ImGui::MenuItem(file.filename().string().c_str())) {
                    fileCurrentPath = file.string();
                    openCurrentFile();

                    // Treat like new file.
                    fileCurrentPath.clear();
                    log("Ready.");
                }
            }

            ImGui::EndMenu();
        }

        if (ImGui::MenuItem("Save")) {
            if (fileCurrentPath.size() > 0) {
                saveCurrentFile();
            } else {
                fileAction = FileAction::SaveAs;
                ImGuiFileDialog::Instance()->OpenModal(
                    "ChooseFileOpenSave", "Choose File", ".cpp", ".");
            }
        }

        if (ImGui::MenuItem("Save As")) {
            fileAction = FileAction::SaveAs;
            ImGuiFileDialog::Instance()->OpenModal(
                "ChooseFileOpenSave", "Choose File", ".cpp", ".");
        }

        ImGui::Separator();
        if (ImGui::MenuItem("Quit")) {
            SDL_Event quitEvent (SDL_QUIT);
            SDL_PushEvent(&quitEvent);
        }

        ImGui::EndMenu();
    }
}

void fileRenderDialog()
{
    if (ImGuiFileDialog::Instance()->Display("ChooseFileOpenSave",
                                             ImGuiWindowFlags_NoCollapse,
                                             ImVec2(460, 540)))
    {
        if (ImGuiFileDialog::Instance()->IsOk()) {
            std::string filePathName = ImGuiFileDialog::Instance()->GetFilePathName();

	    if (fileAction == FileAction::Open) {
                fileCurrentPath = filePathName;
                openCurrentFile();
                log("Ready.");
	    } else if (fileAction == FileAction::SaveAs) {
                fileCurrentPath = filePathName;
                saveCurrentFile();
            }
        }
        
        ImGuiFileDialog::Instance()->Close();
    }
}