-
Notifications
You must be signed in to change notification settings - Fork 0
/
twDialog.cpp
322 lines (307 loc) · 9.46 KB
/
twDialog.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <windows.h>
#include <commctrl.h>
#include <iostream>
#include "twString.h"
#include <vector>
#include <sstream>
#include <Shlobj.h>
#include "twDialog.h"
using namespace std;
/*
twDialog::twDialog(twObject *parent, std::twString title, twSize size)
{
twWindow *window = static_cast<twWindow*>(parent);
DLGTEMPLATE dialogTemplate;
dialogTemplate.style = WS_POPUP | WS_BORDER | WS_SYSMENU | DS_MODALFRAME | WS_CAPTION;
dialogTemplate.x = 10;
dialogTemplate.y = 10;
dialogTemplate.cx = 100;
dialogTemplate.cy = 100;
DialogBoxIndirect(NULL, &dialogTemplate, window->getWindow(), dialogProcedure);
}
INT_PTR CALLBACK twDialog::dialogProcedure(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam)
{
switch (umsg)
{
}
return false;
}
*/
void twDialog::information(twObject *parent, twString title, twString message)
{
HWND hwnd = NULL;
if (parent != NULL)
{
twWindow *window = static_cast<twWindow*>(parent);
hwnd = window->getWindow();
}
MessageBox(hwnd, message.c_str(), title.c_str(), MB_OK|MB_ICONINFORMATION);
}
void twDialog::information(twObject *parent, twString title, int message)
{
HWND hwnd = NULL;
if (parent != NULL)
{
twWindow *window = static_cast<twWindow*>(parent);
hwnd = window->getWindow();
}
MessageBox(hwnd, twString::intToString(message).c_str(), title.c_str(), MB_OK|MB_ICONINFORMATION);
}
void twDialog::error(twObject *parent, twString title, twString message)
{
HWND hwnd = NULL;
if (parent != NULL)
{
twWindow *window = static_cast<twWindow*>(parent);
hwnd = window->getWindow();
}
MessageBox(hwnd, message.c_str(), title.c_str(), MB_OK|MB_ICONERROR);
}
void twDialog::warning(twObject *parent, twString title, twString message)
{
HWND hwnd = NULL;
if (parent != NULL)
{
twWindow *window = static_cast<twWindow*>(parent);
hwnd = window->getWindow();
}
MessageBox(hwnd, message.c_str(), title.c_str(), MB_OK|MB_ICONWARNING);
}
bool twDialog::question(twObject *parent, twString title, twString message)
{
HWND hwnd = NULL;
if (parent != NULL)
{
twWindow *window = static_cast<twWindow*>(parent);
hwnd = window->getWindow();
}
if (MessageBox(hwnd, message.c_str(), title.c_str(), MB_YESNO|MB_ICONQUESTION) == IDYES)
return true;
else
return false;
}
twString twDialog::getOpenFileName(twObject *parent, twString filter, twString initialDir)
{
HWND hwnd = NULL;
if (parent != NULL)
{
twWindow *window = static_cast<twWindow*>(parent);
hwnd = window->getWindow();
}
twString path;
OPENFILENAME openFileName;
char filePath[MAX_PATH] = "";
ZeroMemory(&openFileName, sizeof(OPENFILENAME));
openFileName.lStructSize = sizeof(OPENFILENAME);
openFileName.hwndOwner = hwnd;
openFileName.lpstrFile = filePath;
if (!initialDir.empty())
openFileName.lpstrInitialDir = (char*)initialDir.c_str();
openFileName.nMaxFile = MAX_PATH;
if (filter.empty())
openFileName.lpstrFilter = "All\0*\00";
else
{
char strFilter[1000] = {0};
strcpy(strFilter, filter.data());
for (unsigned int i = 0; i < 1000; i++)
if (strFilter[i] == '\n')
strFilter[i] = '\0';
openFileName.lpstrFilter = strFilter;
}
openFileName.nFilterIndex = 1;
openFileName.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if(GetOpenFileName(&openFileName) == TRUE)
{
path = filePath;
}
return path;
}
vector<twString> twDialog::getOpenFileNames(twObject *parent, twString filter, twString initialDir)
{
HWND hwnd = NULL;
if (parent != NULL)
{
twWindow *window = static_cast<twWindow*>(parent);
hwnd = window->getWindow();
}
vector<twString> paths;
OPENFILENAME openFileName;
char filePath[4096] = "";
ZeroMemory(&openFileName, sizeof(OPENFILENAME));
openFileName.lStructSize = sizeof(OPENFILENAME);
openFileName.hwndOwner = hwnd;
openFileName.lpstrFile = filePath;
if (!initialDir.empty())
openFileName.lpstrInitialDir = (char*)initialDir.c_str();
openFileName.nMaxFile = 4096;
if (filter.empty())
openFileName.lpstrFilter = "All\0*\00";
else
{
char strFilter[1000] = {0};
strcpy(strFilter, filter.data());
for (unsigned int i = 0; i < 1000; i++)
if (strFilter[i] == '\n')
strFilter[i] = '\0';
openFileName.lpstrFilter = strFilter;
}
openFileName.nFilterIndex = 1;
openFileName.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_EXPLORER;
if (GetOpenFileName(&openFileName) == TRUE)
{
if (filePath[openFileName.nFileOffset - 1] != '\0')
paths.push_back(filePath);
else
{
twString dir;
twString currentFile;
for (int i = 0;; i++)
{
if (filePath[i] == '\0')
{
if (dir.empty())
dir = currentFile;
else
paths.push_back(dir + "\\" + currentFile);
currentFile.clear();
if (filePath[i + 1] == '\0')
break;
}
else
currentFile += filePath[i];
}
}
}
return paths;
}
twString twDialog::getExistingDirectory(twWidget *parent, twString text, twString initialDir)
{
BROWSEINFO bi = {0};
if (parent != NULL)
bi.hwndOwner = *parent->m_handle;
char buffer[MAX_PATH] = {0};
bi.pszDisplayName = buffer;
if (text != "")
bi.lpszTitle = text.c_str();
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
SHGetPathFromIDList(pidl, buffer);
twString path = buffer;
IMalloc * imalloc = 0;
if (SUCCEEDED(SHGetMalloc(&imalloc)))
{
imalloc->Free(pidl);
imalloc->Release();
}
return path;
}
twString twDialog::getText(twWidget *parent, twString title)
{
twWindow *parent2 = (twWindow*)parent;
twWindow *window = new twWindow(title, twSize(300, 47), tw::NoSizingWindow, parent2);
window->setInteriorMode(true);
window->setPosition(parent2->getPosition() + twPosition((parent2->getSize().width / 2) - (window->getSize().width / 2), (parent2->getSize().height / 2) - (window->getSize().height / 2)));
twTextEdit *textEdit = new twTextEdit(window, false);
textEdit->setRelativePosition(twPosition(0, 0));
textEdit->setRelativeSize(twSize(100, -1));
textEdit->focus();
textEdit->connect(twTextEdit::EnterPressed, window, SLOT(twWindow::closeModal));
twButton *buttonOK = new twButton(window, "OK");
buttonOK->setRelativePosition(twPosition(0, 0));
buttonOK->setRelativeSize(twSize(50, 100));
buttonOK->connect(twButton::Clicked, window, SLOT(twWindow::closeModal));
twButton *buttonCancel = new twButton(window, "Annuler");
buttonCancel->setRelativePosition(twPosition(50, 0));
buttonCancel->setRelativeSize(twSize(50, 100));
buttonCancel->connect(twButton::Clicked, window, SLOT(twWindow::closeModal));
window->modal();
twString text = textEdit->getText();
window->close();
return text;
}
twString twDialog::getSaveFileName(twObject *parent, twString filter)
{
HWND hwnd = NULL;
if (parent != NULL)
{
twWindow *window = static_cast<twWindow*>(parent);
hwnd = window->getWindow();
}
twString path;
OPENFILENAME openFileName;
char filePath[MAX_PATH] = "";
ZeroMemory(&openFileName, sizeof(OPENFILENAME));
openFileName.lStructSize = sizeof(OPENFILENAME);
openFileName.hwndOwner = hwnd;
openFileName.lpstrFile = filePath;
openFileName.nMaxFile = MAX_PATH;
if (filter.empty())
openFileName.lpstrFilter = "All\0*\00";
else
{
char strFilter[1000] = {0};
strcpy(strFilter, filter.data());
for (unsigned int i = 0; i < 1000; i++)
if (strFilter[i] == '\n')
strFilter[i] = '\0';
openFileName.lpstrFilter = strFilter;
}
openFileName.nFilterIndex = 1;
openFileName.Flags = OFN_OVERWRITEPROMPT;
if(GetSaveFileName(&openFileName) == TRUE)
{
path = filePath;
}
return path;
}
twColor twDialog::getColor(twObject *parent)
{
HWND hwnd = NULL;
if (parent != NULL)
{
twWindow *window = static_cast<twWindow*>(parent);
hwnd = window->getWindow();
}
static COLORREF color = 0;
CHOOSECOLOR chooseColor;
static COLORREF acrCustClr[16];
ZeroMemory(&chooseColor, sizeof(CHOOSECOLOR));
chooseColor.lStructSize = sizeof(CHOOSECOLOR);
chooseColor.hwndOwner = hwnd;
chooseColor.lpCustColors = NULL;
chooseColor.lpCustColors = (LPDWORD) acrCustClr;
chooseColor.rgbResult = color;
chooseColor.Flags = CC_FULLOPEN | CC_RGBINIT;
if (ChooseColor(&chooseColor))
{
color = chooseColor.rgbResult;
return twColor(GetRValue(color), GetGValue(color), GetBValue(color));
}
else
return twColor();
}
twFont twDialog::getFont(twObject *parent)
{
HWND hwnd = NULL;
if (parent != NULL)
{
twWindow *window = static_cast<twWindow*>(parent);
hwnd = window->getWindow();
}
static HFONT hfont;
static LOGFONT logFont;
CHOOSEFONT chooseFont;
ZeroMemory(&chooseFont, sizeof(CHOOSEFONT));
chooseFont.lStructSize = sizeof (CHOOSEFONT);
chooseFont.hwndOwner = hwnd;
chooseFont.lpLogFont = &logFont;
chooseFont.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
if (ChooseFont(&chooseFont))
{
DeleteObject(hfont);
hfont = CreateFontIndirect(&logFont);
return twFont(logFont.lfFaceName, logFont.lfHeight);
}
else
return twFont();
}