-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditorUtil.cpp
More file actions
95 lines (80 loc) · 3.48 KB
/
EditorUtil.cpp
File metadata and controls
95 lines (80 loc) · 3.48 KB
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
#include "EditorUtil.h"
#include <Windows.h>
#include <mmsystem.h>
#include <commdlg.h>
#include <direct.h>
namespace RTEGUI {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int EditorUtil::DisplayDialogBox(const std::string &message, const HWND &windowHandle) {
PlaySound("SystemExclamation", nullptr, SND_ASYNC);
int result = MessageBox(windowHandle, message.c_str(), "Cortex Command GUI Editor", MB_YESNOCANCEL);
if (result == IDNO) { return -1; }
if (result == IDYES) { return 1; }
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool EditorUtil::DisplayLoadFileDialogBox(std::string &filename, const HWND &windowHandle) {
OPENFILENAMEA dialogBox;
std::string filenameToLoad(MAX_PATH, '\0'); // Make sure the string is initialized with the correct size and filled with null characters.
std::string currentDir(MAX_PATH, '\0');
currentDir = std::filesystem::current_path().string();
ZeroMemory(&dialogBox, sizeof(OPENFILENAME)); // Initialize OPENFILENAME
dialogBox.lStructSize = sizeof(OPENFILENAME);
dialogBox.hInstance = nullptr;
dialogBox.hwndOwner = windowHandle;
dialogBox.lpstrFile = filenameToLoad.data();
dialogBox.nMaxFile = filenameToLoad.size();
dialogBox.lpstrFilter = "GUI Files (*.ini)\0*.ini\0All Files\0*.*";
dialogBox.nFilterIndex = 1;
dialogBox.lpstrFileTitle = nullptr;
dialogBox.nMaxFileTitle = 0;
dialogBox.lpstrTitle = "Open";
dialogBox.lpstrInitialDir = currentDir.data();
dialogBox.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
dialogBox.lpstrDefExt = "ini";
if (GetOpenFileName(&dialogBox)) {
filename = std::string(filenameToLoad.data());
std::filesystem::current_path(currentDir);
return true;
}
std::filesystem::current_path(currentDir);
return false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool EditorUtil::DisplaySaveFileDialogBox(std::string &filename, const HWND &windowHandle) {
OPENFILENAMEA dialogBox;
std::string filenameToSave(MAX_PATH, '\0'); // Make sure the string is initialized with the correct size and filled with null characters.
std::string currentDir(MAX_PATH, '\0');
currentDir = std::filesystem::current_path().string();
ZeroMemory(&dialogBox, sizeof(OPENFILENAME)); // Initialize OPENFILENAME
dialogBox.lStructSize = sizeof(OPENFILENAME);
dialogBox.hInstance = nullptr;
dialogBox.hwndOwner = windowHandle;
dialogBox.lpstrFile = filenameToSave.data();
dialogBox.nMaxFile = filenameToSave.size();
dialogBox.lpstrFilter = "GUI Files (*.ini)\0*.ini\0All Files\0*.*";
dialogBox.nFilterIndex = 1;
dialogBox.lpstrFileTitle = nullptr;
dialogBox.nMaxFileTitle = 0;
dialogBox.lpstrTitle = "Save As";
dialogBox.lpstrInitialDir = currentDir.data();
dialogBox.Flags = OFN_PATHMUSTEXIST;
dialogBox.lpstrDefExt = "ini";
if (GetSaveFileName(&dialogBox)) {
// Check if the file exists
if (FILE *file = fopen(filenameToSave.c_str(), "rt")) {
fclose(file);
if (MessageBox(windowHandle, "File Exists\nOverwrite it?", "Confirmation", MB_YESNO) == IDNO) {
std::filesystem::current_path(currentDir);
return false;
}
}
filename = filenameToSave;
std::filesystem::current_path(currentDir);
return true;
}
// Restore the current working directory
std::filesystem::current_path(currentDir);
return false;
}
}