| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | // FilePageAddDlg.cpp : implementation file |
| 20 | // |
| 21 | |
| 22 | #include "stdafx.h" |
| 23 | #include "editor.h" |
| 24 | #include "FilePageAddDlg.h" |
| 25 | #include "ddio.h" |
| 26 | #include "mem.h" |
| 27 | #include <direct.h> |
| 28 | |
| 29 | #ifdef _DEBUG |
| 30 | #define new DEBUG_NEW |
| 31 | #undef THIS_FILE |
| 32 | static char THIS_FILE[] = __FILE__; |
| 33 | #endif |
| 34 | |
| 35 | ///////////////////////////////////////////////////////////////////////////// |
| 36 | // CFilePageAddDlg dialog |
| 37 | |
| 38 | CFilePageAddDlg::CFilePageAddDlg(CWnd *pParent /*=NULL*/) : CDialog(CFilePageAddDlg::IDD, pParent) { |
| 39 | //{{AFX_DATA_INIT(CFilePageAddDlg) |
| 40 | // NOTE: the ClassWizard will add member initialization here |
| 41 | //}}AFX_DATA_INIT |
| 42 | m_NumberOfSelectedFiles = 0; |
| 43 | m_SelectedFiles = NULL; |
| 44 | m_SelectedDestDir = NULL; |
| 45 | initial_path = ""; |
| 46 | } |
| 47 | |
| 48 | CFilePageAddDlg::~CFilePageAddDlg(void) { |
| 49 | if (m_SelectedFiles) { |
| 50 | for (int i = 0; i < m_NumberOfSelectedFiles; i++) { |
| 51 | if (m_SelectedFiles[i]) |
| 52 | mem_free(m_SelectedFiles[i]); |
| 53 | } |
| 54 | mem_free(m_SelectedFiles); |
| 55 | } |
| 56 | |
| 57 | if (m_SelectedDestDir) { |
| 58 | mem_free(m_SelectedDestDir); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void CFilePageAddDlg::SetInitialPath(char *path) { |
| 63 | char olddir[_MAX_PATH]; |
| 64 | ddio_GetWorkingDir(olddir, _MAX_PATH); |
| 65 | if (ddio_SetWorkingDir(path)) { |
| 66 | if (path[1] == ':') |
| 67 | initial_path = path; |
| 68 | } |
| 69 | ddio_SetWorkingDir(olddir); |
| 70 | } |
| 71 | |
| 72 | void CFilePageAddDlg::DoDataExchange(CDataExchange *pDX) { |
| 73 | CDialog::DoDataExchange(pDX); |
| 74 | //{{AFX_DATA_MAP(CFilePageAddDlg) |
| 75 | // NOTE: the ClassWizard will add DDX and DDV calls here |
| 76 | //}}AFX_DATA_MAP |
| 77 | } |
| 78 | |
| 79 | BEGIN_MESSAGE_MAP(CFilePageAddDlg, CDialog) |
| 80 | //{{AFX_MSG_MAP(CFilePageAddDlg) |
| 81 | ON_CBN_SELCHANGE(IDC_LIST_DRIVES, OnSelchangeListDrives) |
| 82 | ON_LBN_SELCHANGE(IDC_LIST_FOLDERS, OnSelchangeListFolders) |
| 83 | //}}AFX_MSG_MAP |
| 84 | END_MESSAGE_MAP() |
| 85 | |
| 86 | ///////////////////////////////////////////////////////////////////////////// |
| 87 | // CFilePageAddDlg message handlers |
| 88 | |
| 89 | // CFileList |
| 90 | |
| 91 | CFileList::CFileList() {} |
| 92 | |
| 93 | CFileList::~CFileList() {} |
| 94 | |
| 95 | BEGIN_MESSAGE_MAP(CFileList, CListBox) |
| 96 | //{{AFX_MSG_MAP(CFileList) |
| 97 | ON_CONTROL_REFLECT(LBN_DBLCLK, OnDblclk) |
| 98 | //}}AFX_MSG_MAP |
| 99 | END_MESSAGE_MAP() |
| 100 | |
| 101 | ///////////////////////////////////////////////////////////////////////////// |
| 102 | // CFileList message handlers |
| 103 | |
| 104 | // fills the list-box with the appropriate information |
| 105 | // either files, directories or drives |
| 106 | // in this case system and/or hidden files are not shown |
| 107 | BOOL CFileList::FillBox() { |
| 108 | WIN32_FIND_DATA FileData; |
| 109 | HANDLE hSearch; |
| 110 | CString csString; |
| 111 | BOOL check_dir; |
| 112 | |
| 113 | SetRedraw(FALSE); |
| 114 | |
| 115 | if (m_nFlag == DRIVES) { |
| 116 | Dir(0x4000, m_csPath); |
| 117 | } else { |
| 118 | // resets the list-box; parent-function |
| 119 | ResetContent(); |
| 120 | |
| 121 | // construct the path and mask for displaying |
| 122 | csString = m_csPath; |
| 123 | csString += m_csFileMask; |
| 124 | |
| 125 | hSearch = FindFirstFile(csString, &FileData); |
| 126 | if (hSearch == INVALID_HANDLE_VALUE) |
| 127 | return FALSE; |
| 128 | |
| 129 | check_dir = (m_nFlag == DIRECTORIES ? TRUE : FALSE); |
| 130 | |
| 131 | CheckEntry(FileData.cFileName, FileData.dwFileAttributes, check_dir); |
| 132 | |
| 133 | while (1) { |
| 134 | if (!FindNextFile(hSearch, &FileData)) { |
| 135 | |
| 136 | SetRedraw(); |
| 137 | InvalidateRect(NULL); |
| 138 | // Close the search handle. |
| 139 | if (!FindClose(hSearch)) |
| 140 | return FALSE; |
| 141 | |
| 142 | return TRUE; |
| 143 | } else |
| 144 | CheckEntry(FileData.cFileName, FileData.dwFileAttributes, check_dir); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | SetRedraw(); |
| 149 | InvalidateRect(NULL); |
| 150 | |
| 151 | return TRUE; |
| 152 | } |
| 153 | |
| 154 | BOOL CFileList::InitBox(UINT res, CWnd *parent, LPSTR lpszPath, LPSTR lpszFileMask, UINT flags, UINT msg, |
| 155 | void (*cb)(char *)) { |
| 156 | m_nFlag = flags; |
| 157 | m_csPath = lpszPath; |
| 158 | m_csFileMask = lpszFileMask; |
| 159 | m_nMsg = msg; |
| 160 | m_cwndParent = parent; |
| 161 | callback = cb; |
| 162 | |
| 163 | // let's check if a \ is the last letter in the path |
| 164 | if (m_nFlag == DRIVES || m_nFlag == DIRECTORIES) { |
| 165 | if (m_csPath.Right(1) != "\\") |
| 166 | m_csPath += "\\"; |
| 167 | } |
| 168 | |
| 169 | // subclassing the dialog-template listbox |
| 170 | if (SubclassDlgItem(res, parent) == FALSE) |
| 171 | return FALSE; |
| 172 | |
| 173 | return FillBox(); |
| 174 | } |
| 175 | |
| 176 | // by double-clicking .. in a drive-box the |
| 177 | // path will get longer and longer such as |
| 178 | // c:\inn\pics\..\html\..\..\windows\system\..\..\det.log |
| 179 | // the proper path in this example should be |
| 180 | // c:\det.log |
| 181 | void CFileList::OnDblclk() { |
| 182 | // TODO: Add your control notification handler code here |
| 183 | CString temp_string, temp_string2; |
| 184 | GetText(GetCurSel(), temp_string); |
| 185 | |
| 186 | switch (m_nFlag) { |
| 187 | case DRIVES: |
| 188 | // is the drive available |
| 189 | // temp_string2=temp_string; |
| 190 | temp_string.MakeUpper(); |
| 191 | |
| 192 | if (!_chdrive(temp_string[2] - 'A' + 1)) { |
| 193 | m_csPath.Format("%c:\\", temp_string[2]); |
| 194 | m_cwndParent->PostMessage(m_nMsg, DRIVES, 0); |
| 195 | } else { |
| 196 | SetCurSel(-1); |
| 197 | temp_string2.Empty(); |
| 198 | temp_string2.Format("Drive %c is not available", temp_string[2]); |
| 199 | MessageBox(temp_string2); |
| 200 | } |
| 201 | break; |
| 202 | case DIRECTORIES: |
| 203 | // if we dblclk a .. then we move up one level |
| 204 | if (temp_string == "..") { |
| 205 | m_csPath = m_csPath.Left(m_csPath.ReverseFind('\\')); |
| 206 | m_csPath = m_csPath.Left(m_csPath.ReverseFind('\\')); |
| 207 | m_csPath += '\\'; |
| 208 | } else |
| 209 | // otherwise we just add another sub-dir to the path |
| 210 | m_csPath = m_csPath + temp_string + '\\'; |
| 211 | |
| 212 | FillBox(); |
| 213 | // notify mama about an update |
| 214 | m_cwndParent->PostMessage(m_nMsg, DIRECTORIES, 0); |
| 215 | break; |
| 216 | case FILES: |
| 217 | m_cwndParent->PostMessage(m_nMsg, FILES, 0); |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // get and set functions |
| 223 | inline CString CFileList::getPath() { return m_csPath; } |
| 224 | |
| 225 | inline CString CFileList::getFileMask() { return m_csFileMask; } |
| 226 | |
| 227 | inline void CFileList::setFileMask(CString mask) { m_csFileMask = (LPCTSTR)mask; } |
| 228 | |
| 229 | inline BOOL CFileList::RefreshMask(CString mask) { |
| 230 | m_csFileMask = mask; |
| 231 | return FillBox(); |
| 232 | } |
| 233 | |
| 234 | // let's check if we display the right entries |
| 235 | // we don't want any hidden, system, temporary and/or directories (for a file-box) |
| 236 | inline void CFileList::CheckEntry(LPSTR name, DWORD flag, BOOL dir) { |
| 237 | // we don't want to display a .; that is the current directory, or |
| 238 | // a ..(previous directory) in a filebox |
| 239 | if (name[0] == '.') |
| 240 | if (!dir) |
| 241 | return; |
| 242 | if (name[1] == '\0') |
| 243 | return; |
| 244 | |
| 245 | // we don't want to show any system and/or hidden files/directories |
| 246 | if (flag & FILE_ATTRIBUTE_HIDDEN) |
| 247 | return; |
| 248 | |
| 249 | if (flag & FILE_ATTRIBUTE_SYSTEM) |
| 250 | return; |
| 251 | |
| 252 | // we don't want any directories in a file-box |
| 253 | if (dir) { |
| 254 | if (!(flag & FILE_ATTRIBUTE_DIRECTORY)) |
| 255 | return; |
| 256 | } |
| 257 | //... or files in a dir-box |
| 258 | else { |
| 259 | if (flag & FILE_ATTRIBUTE_DIRECTORY) |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | AddString(name); |
| 264 | } |
| 265 | |
| 266 | inline BOOL CFileList::RefreshBox(CString path, CString mask) { |
| 267 | setPath(path); |
| 268 | setFileMask(mask); |
| 269 | return FillBox(); |
| 270 | } |
| 271 | |
| 272 | inline BOOL CFileList::RefreshBox(CString path) { |
| 273 | setPath(path); |
| 274 | return FillBox(); |
| 275 | } |
| 276 | |
| 277 | inline void CFileList::setPath(CString path) { |
| 278 | CString temp = path; |
| 279 | char *buffer; |
| 280 | |
| 281 | // check for valid entry |
| 282 | if (temp.Right(1) != "\\") |
| 283 | temp += "\\"; |
| 284 | |
| 285 | buffer = (char *)mem_malloc(temp.GetLength() + 1); |
| 286 | |
| 287 | if (!buffer) |
| 288 | return; |
| 289 | |
| 290 | // ddio_CleanPath(buffer,temp.GetBuffer(0)); |
| 291 | strcpy(buffer, temp.GetBuffer(0)); |
| 292 | |
| 293 | m_csPath = buffer; |
| 294 | |
| 295 | if (callback) |
| 296 | (*callback)(buffer); |
| 297 | |
| 298 | mem_free(buffer); |
| 299 | } |
| 300 | |
| 301 | BOOL CFilePageAddDlg::OnInitDialog() { |
| 302 | CDialog::OnInitDialog(); |
| 303 | |
| 304 | // TODO: Add extra initialization here |
| 305 | |
| 306 | // register a personal, user-defined msg so that our boxes can post msg's for |
| 307 | // interaction |
| 308 | m_nBoxMsg = RegisterWindowMessage("FilePageDlgAdd"); |
| 309 | // init the boxes |
| 310 | // the definition of: |
| 311 | // BOOL InitBox(UINT res_from_template, |
| 312 | // CWnd *parent, |
| 313 | // LPSTR lpszPath, |
| 314 | // LPSTR lpszFileMask, |
| 315 | // UINT flags, |
| 316 | // UINT msg); |
| 317 | |
| 318 | if (initial_path.IsEmpty()) |
| 319 | initial_path = "c:\\"; |
| 320 | |
| 321 | m_cflDrive.InitBox(IDC_LIST_DRIVES, this, initial_path.GetBuffer(0), "*.*", DRIVES, m_nBoxMsg); |
| 322 | m_cflDir.InitBox(IDC_LIST_FOLDERS, this, initial_path.GetBuffer(0), "*.*", DIRECTORIES, m_nBoxMsg); |
| 323 | m_cflFile.InitBox(IDC_LIST_FILES, this, initial_path.GetBuffer(0), "*.*", FILES, m_nBoxMsg); |
| 324 | |
| 325 | // Add the drives to the drive list |
| 326 | CComboBox *combo = (CComboBox *)GetDlgItem(IDC_LIST_DRIVES); |
| 327 | char *root_drives[256]; |
| 328 | int root_count = ddio_GetFileSysRoots(root_drives, 256); |
| 329 | int i, c_drive = 0; |
| 330 | |
| 331 | char initial_d[3]; |
| 332 | strncpy(initial_d, initial_path.GetBuffer(0), 2); |
| 333 | initial_d[2] = '\0'; |
| 334 | |
| 335 | for (i = 0; i < root_count; i++) { |
| 336 | if (!strnicmp(root_drives[i], initial_d, 2)) |
| 337 | c_drive = i; |
| 338 | combo->AddString(root_drives[i]); |
| 339 | mem_free(root_drives[i]); |
| 340 | } |
| 341 | combo->SetCurSel(c_drive); |
| 342 | |
| 343 | combo = (CComboBox *)GetDlgItem(IDC_DIRS); |
| 344 | for (i = 0; i < NumDirectoryNames; i++) { |
| 345 | combo->AddString(DirectoryNames[i]); |
| 346 | } |
| 347 | combo->SetCurSel(0); |
| 348 | |
| 349 | return TRUE; |
| 350 | } |
| 351 | |
| 352 | LRESULT CFilePageAddDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) { |
| 353 | // TODO: Add your specialized code here and/or call the base class ... |
| 354 | // process the user-defined msg |
| 355 | if (message == m_nBoxMsg) { |
| 356 | // update the dir-box and file-box if we change drive |
| 357 | if (wParam == DRIVES) { |
| 358 | m_cflDir.RefreshBox(m_cflDrive.getPath()); |
| 359 | m_cflFile.RefreshBox(m_cflDrive.getPath()); |
| 360 | } |
| 361 | |
| 362 | // update the file-box if we change directory |
| 363 | else if (wParam == DIRECTORIES) { |
| 364 | m_cflFile.RefreshBox(m_cflDir.getPath()); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | return CDialog::WindowProc(message, wParam, lParam); |
| 369 | } |
| 370 | |
| 371 | void CFilePageAddDlg::OnSelchangeListDrives() { |
| 372 | CComboBox *combo = (CComboBox *)GetDlgItem(IDC_LIST_DRIVES); |
| 373 | int sel = combo->GetCurSel(); |
| 374 | CString drive; |
| 375 | |
| 376 | combo->GetLBText(sel, drive); |
| 377 | |
| 378 | m_cflDrive.setPath(drive); |
| 379 | m_cflDir.RefreshBox(m_cflDrive.getPath()); |
| 380 | m_cflFile.RefreshBox(m_cflDrive.getPath()); |
| 381 | } |
| 382 | |
| 383 | void CFilePageAddDlg::OnSelchangeListFolders() {} |
| 384 | |
| 385 | bool CFilePageAddDlg::Quit(void) { |
| 386 | char buffer[_MAX_PATH]; |
| 387 | int cur_sel; |
| 388 | CComboBox *combo = (CComboBox *)GetDlgItem(IDC_DIRS); |
| 389 | cur_sel = combo->GetCurSel(); |
| 390 | combo->GetLBText(cur_sel, buffer); |
| 391 | |
| 392 | char temp[1024]; |
| 393 | sprintf(temp, "Are you sure you want to add these files to the data\\%s directory?", buffer); |
| 394 | if (MessageBox(temp, NULL, MB_YESNO) != IDYES) { |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | m_SelectedDestDir = mem_strdup(buffer); |
| 399 | |
| 400 | CListBox *list = (CListBox *)GetDlgItem(IDC_LIST_FILES); |
| 401 | m_NumberOfSelectedFiles = list->GetSelCount(); |
| 402 | |
| 403 | if (m_NumberOfSelectedFiles == 0) { |
| 404 | m_SelectedFiles = NULL; |
| 405 | } else { |
| 406 | |
| 407 | m_SelectedFiles = (char **)mem_malloc(m_NumberOfSelectedFiles * sizeof(char *)); |
| 408 | if (!m_SelectedFiles) { |
| 409 | m_SelectedFiles = NULL; |
| 410 | m_NumberOfSelectedFiles = 0; |
| 411 | return true; |
| 412 | } |
| 413 | |
| 414 | int *sel_items = (int *)mem_malloc(sizeof(int) * m_NumberOfSelectedFiles); |
| 415 | if (!sel_items) { |
| 416 | mem_free(m_SelectedFiles); |
| 417 | m_SelectedFiles = NULL; |
| 418 | m_NumberOfSelectedFiles = 0; |
| 419 | return true; |
| 420 | } |
| 421 | |
| 422 | list->GetSelItems(m_NumberOfSelectedFiles, sel_items); |
| 423 | char temp[_MAX_PATH]; |
| 424 | |
| 425 | char drive[_MAX_PATH]; |
| 426 | strcpy(drive, m_cflFile.getPath()); |
| 427 | |
| 428 | for (int i = 0; i < m_NumberOfSelectedFiles; i++) { |
| 429 | cur_sel = sel_items[i]; |
| 430 | |
| 431 | list->GetText(cur_sel, temp); |
| 432 | |
| 433 | // ummm, figure out real path here |
| 434 | ddio_MakePath(buffer, drive, temp, NULL); |
| 435 | |
| 436 | m_SelectedFiles[i] = mem_strdup(buffer); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | return true; |
| 441 | } |
| 442 | |
| 443 | void CFilePageAddDlg::OnOK() { |
| 444 | if (!Quit()) |
| 445 | return; |
| 446 | |
| 447 | CDialog::OnOK(); |
| 448 | } |