-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.cpp
170 lines (148 loc) · 3.93 KB
/
Menu.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
#include "Menu.h"
#include "Freehand.h"
#include "App.h"
#include <shobjidl.h>
#include <windows.h>
void Menu::addItem(const wchar_t* caption, int pos, int cmdId, Menu* subMenu) {
MENUITEMINFO s;
s.cbSize = sizeof(s);
s.fMask = MIIM_TYPE | MIIM_SUBMENU | MIIM_ID;
s.wID = cmdId;
s.hSubMenu = subMenu ? subMenu->hMenu : nullptr;
s.fType = MFT_STRING;
s.dwTypeData = const_cast<wchar_t*>(caption);
s.cch = wcslen(caption);
if (InsertMenuItem(hMenu, pos, true, &s)) {
_cmds[caption] = cmdId;
}
else {
assert(false);
return;
}
}
void Menu::setItemState(int cmdId, int fState, bool checked) {
MENUITEMINFO s;
s.cbSize = sizeof(s);
s.fMask = MIIM_STATE;
if (checked) {
s.fState |= fState;
}
else {
s.fState &= ~fState;
}
SetMenuItemInfo(hMenu, cmdId, false, &s);
}
void Menu::removeItemByCmd(int cmdId) {
RemoveMenu(hMenu, cmdId, MF_BYCOMMAND);
}
bool Menu::_onWin32Command(HINSTANCE hInst, HWND hWnd, int cmdId)
{
switch (cmdId)
{
case MCMD_FILE_OPEN: {
#if 0
wchar_t filenameBuff[MAX_PATH] = { 0 };
const wchar_t* defaultFilename = L"saveApp.txt";
swprintf(filenameBuff, MAX_PATH, defaultFilename);
auto currentDir = my_getCurrentDirectory();
OPENFILENAME s;
my_bzero(s);
s.lStructSize = sizeof(s);
s.hwndOwner = hWnd;
s.hInstance = hInst;
s.lpstrFilter = L"Text File (*.txt)\0*.txt\0"
L"All Files (*.*)\0*.*\0"
L"\0";
s.lpstrFile = filenameBuff;
s.nMaxFile = MAX_PATH;
s.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
s.lpstrInitialDir = currentDir.c_str();
bool isOpen = GetOpenFileName(&s);
if (isOpen) {
App::Instance()->load(s.lpstrFile);
printf("Loaded from fp: %ws\n", s.lpstrFile);
}
#else
//#include <shobjidl.h>
//#include <windows.h>
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |
COINIT_DISABLE_OLE1DDE);
if (SUCCEEDED(hr))
{
IFileOpenDialog* pFileOpen;
// Create the FileOpenDialog object.
hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
if (SUCCEEDED(hr))
{
// Show the Open dialog box.
hr = pFileOpen->Show(NULL);
// Get the file name from the dialog box.
if (SUCCEEDED(hr))
{
IShellItem* pItem;
hr = pFileOpen->GetResult(&pItem);
if (SUCCEEDED(hr))
{
PWSTR pszFilePath;
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
// Display the file name to the user.
if (SUCCEEDED(hr))
{
MessageBoxW(NULL, pszFilePath, L"File Path", MB_OK);
CoTaskMemFree(pszFilePath);
}
pItem->Release();
}
}
pFileOpen->Release();
}
CoUninitialize();
}
#endif
} return true;
case MCMD_FILE_SAVE: {
wchar_t filenameBuff[MAX_PATH] = { 0 };
const wchar_t* defaultFilename = L"saveApp.txt";
swprintf(filenameBuff, MAX_PATH, defaultFilename);
auto currentDir = my_getCurrentDirectory();
OPENFILENAME s;
my_bzero(s);
s.lStructSize = sizeof(s);
s.hwndOwner = hWnd;
s.hInstance = hInst;
s.lpstrFilter = L"Text File (*.txt)\0*.txt\0"
L"All Files (*.*)\0*.*\0"
L"\0";
s.lpstrFile = filenameBuff;
s.nMaxFile = MAX_PATH;
s.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_OVERWRITEPROMPT;
s.lpstrInitialDir = currentDir.c_str();
bool isOpen = GetSaveFileName(&s);
if (isOpen) {
App::Instance()->save(s.lpstrFile);
printf("Saved at fp: %ws\n", s.lpstrFile);
}
} return true;
case MCMD_FILE_EXIT: { SendMessage(hWnd, WM_CLOSE, 0, 0); } return true;
case MCMD_FILE_ABOUT: { DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, &About); } return true;
}
return false;
}
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}