#include "dirSelector.h" #include #include extern HINSTANCE app_instance; int CALLBACK initSelect(HWND window, UINT message, LPARAM lParam, LONG lpData) { TCHAR buffer[MAX_PATH]; switch (message) { case BFFM_INITIALIZED: { dirSelector *selector = (dirSelector *) lpData; lstrcpy(buffer, selector->selectedPath()); // GetCurrentDirectory(sizeof(buffer) / sizeof(TCHAR), buffer); SendMessage(window, BFFM_SETSELECTION, TRUE, (LPARAM) buffer); SendMessage(window, BFFM_SETSTATUSTEXT, 0, (LPARAM) buffer); } break; case BFFM_SELCHANGED: if (SHGetPathFromIDList((LPITEMIDLIST) lParam, buffer)) { SendMessage(window, BFFM_SETSTATUSTEXT, 0, (LPARAM) buffer); } break; } return 0; } dirSelector::dirSelector(char *_prompt, char *_title) : configurable(_prompt) { this->path = NULL; this->title = _title; } dirSelector::dirSelector(char *_prompt, char *_optPrompt, char *_title, FilePath *_defPath) : configurable(_prompt, _optPrompt) { this->path = new char[strlen(_defPath->cString())+1]; strcpy(this->path, _defPath->cString()); this->title = _title; } dirSelector::dirSelector(HGLOBAL dlgBlk) { this->path = NULL; this->dlgBlock = dlgBlk; } UINT APIENTRY hookProc(HWND dlg, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: CommDlg_OpenSave_HideControl(GetParent(dlg), edt1); CommDlg_OpenSave_HideControl(GetParent(dlg), stc3); CommDlg_OpenSave_HideControl(GetParent(dlg), cmb1); CommDlg_OpenSave_HideControl(GetParent(dlg), stc2); CommDlg_OpenSave_HideControl(GetParent(dlg), chx1); CommDlg_OpenSave_SetControlText(GetParent(dlg), IDOK, "Select"); break; case WM_NOTIFY: if (((LPNMHDR) lParam)->code == CDN_INITDONE) { CommDlg_OpenSave_HideControl(GetParent(dlg), edt1); } else if (((LPNMHDR) lParam)->code == CDN_FILEOK) { return 0; } break; } return 0; } void dirSelector::setPath(LPSTR newpath) { if (this->path) { delete this->path; } this->path = new char[strlen(newpath) + 1]; strcpy(this->path, newpath); } void dirSelector::setPathFromEdit(HWND window) { char *newpath; newpath = new char[GetWindowTextLength(window) + 1]; if (newpath != NULL) { GetWindowText(window, newpath, GetWindowTextLength(window) + 1); if (this->path) { delete this->path; } this->path = newpath; } } void dirSelector::requestPath(HWND parent, LPSTR message) { OPENFILENAME openStr; char fileName[256] = "dir.dir\0"; DWORD error; BROWSEINFO bInfo; int image; bInfo.hwndOwner = parent; bInfo.pidlRoot = NULL; bInfo.pszDisplayName = fileName; bInfo.lpszTitle = message; bInfo.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT; bInfo.lpfn = initSelect; bInfo.lParam = (LONG) this; bInfo.iImage = image = 0; LPITEMIDLIST itemList = SHBrowseForFolder(&bInfo); if (itemList != NULL) { SHGetPathFromIDList(itemList, fileName); this->setPath(fileName); } return; openStr.lStructSize = sizeof(OPENFILENAME); openStr.hwndOwner = parent; openStr.hInstance = 0; // app_instance; //this->dlgBlock; openStr.lpstrFilter = NULL; openStr.lpstrCustomFilter = NULL; // openStr.nMaxCustFilter = NULL; openStr.nMaxCustFilter = 0; openStr.nFilterIndex = 0; openStr.lpstrFile = fileName; openStr.nMaxFile = 255; openStr.lpstrFileTitle = NULL; openStr.lpstrInitialDir = NULL; openStr.lpstrTitle = message; openStr.Flags = OFN_ENABLEHOOK | OFN_NOTESTFILECREATE | OFN_NOVALIDATE | OFN_EXPLORER; // || OFN_ENABLETEMPLATE | OFN_EXPLORER; openStr.nFileOffset = 0; openStr.nFileExtension = 4; openStr.lpstrDefExt = NULL; // openStr.lCustData = NULL; openStr.lCustData = 0; openStr.lpfnHook = hookProc; openStr.lpTemplateName = 0; // "MySaveAsDlg"; if (GetSaveFileName(&openStr) == 0) { error = CommDlgExtendedError(); } else { if (this->path != NULL) { free(this->path); } this->path = (char *) malloc (strlen(openStr.lpstrFile) + 1); strcpy(this->path, openStr.lpstrFile); } } char *dirSelector::selectedPath() { return this->path; }