aboutsummaryrefslogtreecommitdiffstats
path: root/gui/wxmain_mfile.h
blob: 7cc9177d46b5569f09fc89a9cd4296f6f071a03b (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
void MainFrame::onFileNew(wxCommandEvent&)
{
    m_open_file_path = "";
    m_text_editor->SetText(file_content);
    m_text_editor->DiscardEdits();
    m_status_bar->SetStatusText("Ready.");
}

void MainFrame::onFileOpen(wxCommandEvent&)
{
    wxFileDialog openDialog(this, "Open filter file", "", "",
                            "C++ source file (*.cpp)|*.cpp",
                            wxFD_OPEN | wxFD_FILE_MUST_EXIST);

    if (openDialog.ShowModal() != wxID_CANCEL) {
        if (wxFileInputStream file_stream (openDialog.GetPath()); file_stream.IsOk()) {
            auto size = file_stream.GetSize();
            auto buffer = new char[size + 1];
            buffer[size] = '\0';
            if (file_stream.ReadAll(buffer, size)) {
                m_open_file_path = openDialog.GetPath();
                m_text_editor->SetText(buffer);
                m_text_editor->DiscardEdits();
                m_compile_output->ChangeValue("");
                m_status_bar->SetStatusText("Ready.");
            } else {
                m_status_bar->SetStatusText("Failed to read file contents.");
            }
            delete[] buffer;
        } else {
            m_status_bar->SetStatusText("Failed to open file.");
        }
    } else {
        m_status_bar->SetStatusText("Ready.");
    }
}

void MainFrame::onFileOpenTemplate(wxCommandEvent& event)
{
    auto file_path = wxGetCwd() + "/templates/" + m_menu_bar->GetLabel(event.GetId());

    if (wxFileInputStream file_stream (file_path); file_stream.IsOk()) {
        auto size = file_stream.GetSize();
        auto buffer = new char[size + 1];
        buffer[size] = '\0';
        if (file_stream.ReadAll(buffer, size)) {
            m_open_file_path = "";
            m_text_editor->SetText(buffer);
            //m_text_editor->DiscardEdits();
            m_status_bar->SetStatusText("Ready.");
        } else {
            m_status_bar->SetStatusText("Failed to read file contents.");
        }
        delete[] buffer;
    } else {
        m_status_bar->SetStatusText("Ready.");
    }
}


void MainFrame::onFileSave(wxCommandEvent& ce)
{
    if (m_text_editor->IsModified()) {
        if (m_open_file_path.IsEmpty()) {
            onFileSaveAs(ce);
        } else {
            if (wxFile file (m_open_file_path, wxFile::write); file.IsOpened()) {
                file.Write(m_text_editor->GetText());
                file.Close();
                m_text_editor->DiscardEdits();
                m_status_bar->SetStatusText("Saved.");
            } else {
                m_status_bar->SetStatusText("Save failed: couldn't open file.");
            }
        }
    } else {
        m_status_bar->SetStatusText("No modifications to save.");
    }
}

void MainFrame::onFileSaveAs(wxCommandEvent&)
{
    if (m_text_editor->IsModified()) {
        wxFileDialog saveDialog(this, "Save filter file", "", "",
                                "C++ source file (*.cpp)|*.cpp",
                                wxFD_SAVE | wxFD_OVERWRITE_PROMPT);

        if (saveDialog.ShowModal() != wxID_CANCEL) {
            if (wxFile file (saveDialog.GetPath(), wxFile::write); file.IsOpened()) {
                file.Write(m_text_editor->GetText());
                file.Close();
                m_text_editor->DiscardEdits();
                m_open_file_path = saveDialog.GetPath();
                m_status_bar->SetStatusText("Saved.");
            } else {
                m_status_bar->SetStatusText("Save failed: couldn't open file.");
            }
        }
    } else {
        m_status_bar->SetStatusText("No modifications to save.");
    }
}

void MainFrame::onFileQuit(wxCommandEvent&)
{
    Close(true);
}