-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmainframe.cpp
217 lines (172 loc) · 6.68 KB
/
mainframe.cpp
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "mainframe.hpp"
#include <wx/wx.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <fstream>
#include <filesystem>
#include <wx/string.h>
#include <wx/spinctrl.h>
#include <wx/file.h>
#include "NewWindow.hpp"
void MainFrame::OnSaveAs(wxCommandEvent& event) {
wxString defaultFileName = "test.md";
wxString filePath = wxFileSelector("Save As", "", defaultFileName, ".md", "Markdown files (*.md)|*.md", wxFD_SAVE | wxFD_OVERWRITE_PROMPT, this);
// Check if the user didn't cancel the dialog
if (!filePath.IsEmpty()) {
// Generate the markdown content
wxString content;
content += "## Weapons\n";
content += "| Name | Strikes | Range | Type |\n";
content += "| ---- | ------- | ----- | ---- |\n";
// Write the markdown content to the file
wxFile file(filePath, wxFile::write);
if (file.IsOpened()) {
file.Write(content);
file.Close();
wxLogMessage("File saved successfully!");
} else {
wxLogMessage("Failed to save the file.");
}
}
}
void MainFrame::WriteInFile(const std::string& str) {
std::ofstream myfile;
myfile.open("data.txt", std::ofstream::out | std::ofstream::app);
if(myfile.is_open()) {
myfile << str << std::endl;
myfile.close();
} else {
std::cerr << "Unable to open file" << std::endl;
}
}
void MainFrame::OnSaveButton(wxCommandEvent& event) {
std::string text1 = textInput1->GetValue().ToStdString();
std::string text2 = textInput2->GetValue().ToStdString();
// Clear the file first
std::ofstream clearFile("data.txt", std::ofstream::out | std::ofstream::trunc);
clearFile.close();
WriteInFile(text1);
WriteInFile(text2);
}
void MainFrame::OnLoadButton(wxCommandEvent& event) {
// needs fix, only saves to 1st input field
std::ifstream myfile;
std::string str, ascii, hex;
myfile.open("data.txt");
if(myfile.is_open()){
while(std::getline(myfile, str)) {
if(ascii == "") {
ascii = str;
} else {
hex = str;
}
}
myfile.close();
// Assuming the ASCII string is on the first line and the hex string is on the second line
textInput1->SetValue(ascii);
textInput2->SetValue(hex);
} else {
std::cerr << "Unable to open file" << std::endl;
}
}
void MainFrame::OnClearButton(wxCommandEvent& event) {
textInput1->Clear();
textInput2->Clear();
}
void MainFrame::OnTextInput1KeyDown(wxKeyEvent& key) {
if (key.GetUnicodeKey() == 8) {
wxString hexStr = textInput1->GetValue();
if(!hexStr.IsEmpty()) {
hexStr.RemoveLast();
textInput1->SetValue(hexStr);
}
} else {
std::string keyStr(1, static_cast<char>(key.GetUnicodeKey()));
textInput1->AppendText(keyStr);
}
}
void MainFrame::OnTextInput2KeyDown(wxKeyEvent& key) {
if (key.GetUnicodeKey() == 8) {
wxString hexStr = textInput2->GetValue();
if(!hexStr.IsEmpty()) {
hexStr.RemoveLast();
textInput2->SetValue(hexStr);
}
} else {
std::string keyStr(1, static_cast<char>(key.GetUnicodeKey()));
textInput2->AppendText(keyStr);
}
}
void MainFrame::OnNewWindowButton(wxCommandEvent& event) {
NewWindow *newWindow = new NewWindow("New Window", textInput1->GetValue());
newWindow->Show(true);
}
enum
{
ID_Hello = 1
};
void MainFrame::OnExit(wxCommandEvent& event)
{
Close(true);
}
void MainFrame::OnAbout(wxCommandEvent& event)
{
wxMessageBox("This is a wxWidgets Hello World example",
"About Hello World", wxOK | wxICON_INFORMATION);
}
void MainFrame::OnHello(wxCommandEvent& event)
{
wxLogMessage("Hello world from wxWidgets!");
}
MainFrame::MainFrame(const wxString &title)
: wxFrame(nullptr, wxID_ANY, title, wxDefaultPosition, wxDefaultSize,
wxDEFAULT_FRAME_STYLE & ~wxRESIZE_BORDER) {
panel = new wxPanel(this);
button = new wxButton(panel, wxID_ANY, "4AKIFT", wxPoint(150, 50), wxSize(100, 35));
checkBox = new wxCheckBox(panel, wxID_ANY, "Check", wxPoint(350,30), wxSize(100,70));
label = new wxStaticText(panel, wxID_ANY, "ABCD", wxPoint(150,100), wxSize(100,35));
text = new wxTextCtrl(panel, wxID_ANY, "text", wxPoint(150, 150), wxSize(200,35));
slider = new wxSlider(panel, wxID_ANY, 25,0,100, wxPoint(350,150), wxSize(200,-1));
gauge = new wxGauge(panel, wxID_ANY, 100, wxPoint(150,200),wxSize(200,-1));
gauge->SetValue(75);
choices = new wxArrayString();
choices->Add("Fabian");
choices->Add("Alvin");
choices->Add("Mihael");
choices->Add("Neil");
choice = new wxChoice(panel, wxID_ANY,wxPoint(150,260),wxSize(100,-1), *choices);
choice->Select(0);
spin = new wxSpinCtrl(panel, wxID_ANY, "", wxPoint(400,200));
list = new wxListBox(panel, wxID_ANY, wxPoint(150,350), wxSize(100,-1), *choices);
radioBox = new wxRadioBox(panel, wxID_ANY, "radioBox", wxPoint(285,300), wxDefaultSize, *choices);
textInput1 = new wxTextCtrl(panel, wxID_ANY, "", wxPoint(0, 800), wxSize(100,100));
textInput1->Bind(wxEVT_CHAR, &MainFrame::OnTextInput1KeyDown, this);
textInput2 = new wxTextCtrl(panel, wxID_ANY, "", wxPoint(500, 800), wxSize(100,100));
textInput2->Bind(wxEVT_CHAR, &MainFrame::OnTextInput2KeyDown, this);
clearButton = new wxButton(panel, wxID_ANY, "CLEAR", wxPoint(0, 1000), wxSize(200, 200));
clearButton->Bind(wxEVT_BUTTON, &MainFrame::OnClearButton, this);
saveButton = new wxButton(panel, wxID_ANY, "SAVE", wxPoint(350, 1000), wxSize(200,200));
saveButton->Bind(wxEVT_BUTTON, &MainFrame::OnSaveButton, this);
loadButton = new wxButton(panel, wxID_ANY, "LOAD", wxPoint(700, 1000), wxSize(200,200));
loadButton->Bind(wxEVT_BUTTON, &MainFrame::OnLoadButton, this);
newWindowButton = new wxButton(panel, wxID_ANY, "New Window", wxPoint(700, 700), wxSize(200, 200));
newWindowButton->Bind(wxEVT_BUTTON, &MainFrame::OnNewWindowButton, this);
wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&Hello...\tCtrl-H", "Help string shown in status bar for this menu item");
menuFile->Append(wxID_SAVE, "&Save As\tShift-Ctrl-S", "Help string shown in status bar for this menu item");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append(menuFile, "&File");
menuBar->Append(menuHelp, "&Help");
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText("Welcome to wxWidgets!");
menuBar->Bind(wxEVT_MENU, &MainFrame::OnHello, this, ID_Hello);
menuBar->Bind(wxEVT_MENU, &MainFrame::OnAbout, this, wxID_ABOUT);
menuBar->Bind(wxEVT_MENU, &MainFrame::OnExit, this, wxID_EXIT);
menuBar->Bind(wxEVT_MENU, &MainFrame::OnSaveAs, this, wxID_SAVE);
}