|
|
1.1 ! root 1: /*************************************************************************** ! 2: * * ! 3: * MODULE : MpOpen.c * ! 4: * * ! 5: * PURPOSE : Contains the file open dialog function and it's helper * ! 6: * functions. * ! 7: * * ! 8: * FUNCTIONS : IsWild () - Ascertains that the input string * ! 9: * contains a DOS wildcard character. * ! 10: * * ! 11: * SelectFile() - If filename supplied contains a * ! 12: * wildcard, this function fills the * ! 13: * listboxes in File/Open dialog, else* ! 14: * the dialog is closed. * ! 15: * * ! 16: * FileOpenDlgProc() - Dialog funcion for the File/Open * ! 17: * dialog. * ! 18: * * ! 19: * GetFileName () - Gets a file name from the user. * ! 20: * * ! 21: ***************************************************************************/ ! 22: #include "multipad.h" ! 23: #include <fcntl.h> ! 24: #include <io.h> ! 25: #include <string.h> ! 26: CHAR szPropertyName [] = "FILENAME";/* Name of the File name property list item */ ! 27: ! 28: ! 29: ! 30: /**************************************************************************** ! 31: * * ! 32: * FUNCTION : IsWild ( psz ) * ! 33: * * ! 34: * PURPOSE : Checks if the string (referenced by a NEAR pointer) * ! 35: * contains a DOS wildcard character ("*" or "?"). * ! 36: * * ! 37: * RETURNS : TRUE - iff the string contains a wildcard character. * ! 38: * FALSE - otherwise. . * ! 39: * * ! 40: ****************************************************************************/ ! 41: BOOL NEAR PASCAL IsWild(register PSTR psz) ! 42: { ! 43: for(;;) ! 44: switch (*psz++){ ! 45: case '*': ! 46: case '?': ! 47: /* Found wildcard */ ! 48: return TRUE; ! 49: ! 50: case 0: ! 51: /* Reached end of string */ ! 52: return FALSE; ! 53: ! 54: default: ! 55: continue; ! 56: } ! 57: } ! 58: ! 59: /**************************************************************************** ! 60: * * ! 61: * FUNCTION : FileExists(pch) * ! 62: * * ! 63: * PURPOSE : Checks to see if a file exists with the path/filename * ! 64: * described by the string pointed to by 'pch'. * ! 65: * * ! 66: * RETURNS : TRUE - if the described file does exist. * ! 67: * FALSE - otherwise. * ! 68: * * ! 69: ****************************************************************************/ ! 70: ! 71: ! 72: BOOL FileExists(PSTR pch) ! 73: { ! 74: int fh; ! 75: ! 76: if ((fh = open(pch, O_RDONLY)) < 0) ! 77: return(FALSE); ! 78: ! 79: _lclose(fh); ! 80: return(TRUE); ! 81: } ! 82: ! 83: /**************************************************************************** ! 84: * * ! 85: * FUNCTION : SelectFile ( hwnd ) * ! 86: * * ! 87: * PURPOSE : Reads the string in the edit control of the File/Open * ! 88: * dialog. If it contains a wildcard, then it attempts to * ! 89: * fill the listboxes in the File/Open dialog. Othewise it * ! 90: * ends the dialog. Modifies the FILENAME item in the property* ! 91: * list of the window. * ! 92: * * ! 93: ****************************************************************************/ ! 94: ! 95: VOID NEAR PASCAL SelectFile(register HWND hwnd) ! 96: { ! 97: register PSTR pch; ! 98: PSTR pch2; ! 99: ! 100: /* Get handle (near address) to filename data in window's property list */ ! 101: pch = (PSTR)GetProp (hwnd, PROP_FILENAME); ! 102: ! 103: /* Get the text from the dialog's edit control into this address */ ! 104: GetDlgItemText (hwnd, IDD_FILENAME, pch, 64); ! 105: ! 106: if ( IsWild (pch)){ ! 107: /* Select the directory and make a listing of the directories */ ! 108: DlgDirList(hwnd, (LPSTR)pch, (int)IDD_DIRS, (int)IDD_PATH, (WORD)ATTR_DIRS); ! 109: ! 110: /* Obtain the filename-only part of the path in the edit control */ ! 111: for (pch2 = pch; *pch; pch++) ! 112: if (*pch == '\\' || *pch == ':') ! 113: pch2 = pch + 1; ! 114: ! 115: /* List the files in this directory based on the wildcard. */ ! 116: DlgDirList(hwnd, (LPSTR)pch2, IDD_FILES, IDD_PATH, ATTR_FILES); ! 117: ! 118: /* Set the dialog's edit control to the filename part of path ! 119: * string. ! 120: */ ! 121: SetDlgItemText (hwnd, IDD_FILENAME, pch2); ! 122: } ! 123: else ! 124: { ! 125: /* The filename in the property list is not a wildcard */ ! 126: if (FileExists (pch)){ ! 127: ! 128: RemoveProp (hwnd, PROP_FILENAME); ! 129: EndDialog (hwnd, 0); ! 130: } ! 131: else{ ! 132: MPError ( hwnd, MB_OK | MB_SYSTEMMODAL, IDS_CANTOPEN, (LPSTR) pch); ! 133: SetActiveWindow (hwnd); ! 134: } ! 135: } ! 136: } ! 137: ! 138: ! 139: ! 140: /**************************************************************************** ! 141: * * ! 142: * FUNCTION : FileOpenDlgProc() * ! 143: * * ! 144: * PURPOSE : Dialog function for the File/Open dialog. Takes care of * ! 145: * calling the appropriate functions for extracting the * ! 146: * filename and wildcard, filling the listboxes and changing * ! 147: * the FILENAME item in the property list for the window. * ! 148: * * ! 149: ****************************************************************************/ ! 150: ! 151: BOOL APIENTRY FileOpenDlgProc ( ! 152: register HWND hwnd, ! 153: WORD message, ! 154: register UINT wParam, ! 155: LONG lParam) ! 156: { ! 157: PSTR pch; ! 158: ! 159: switch (message){ ! 160: ! 161: case WM_INITDIALOG: ! 162: /* Set the default file extension on edit window, and try to ! 163: * get a listing of the files and directories. ! 164: */ ! 165: SetDlgItemText ( hwnd, IDD_FILENAME, DEFFILESEARCH); ! 166: SetProp (hwnd, PROP_FILENAME, (HANDLE) lParam); ! 167: SendDlgItemMessage (hwnd, IDD_FILENAME, EM_LIMITTEXT, 64, 0L); ! 168: SelectFile (hwnd); ! 169: break; ! 170: ! 171: case WM_COMMAND: ! 172: switch (LOWORD(wParam)) { ! 173: case IDOK: ! 174: SelectFile(hwnd); ! 175: break; ! 176: ! 177: case IDCANCEL: ! 178: /* Set the filename in the prop. list to NULL and quit */ ! 179: pch = (PSTR) GetProp (hwnd, PROP_FILENAME); ! 180: *pch = 0; ! 181: EndDialog (hwnd, 0); ! 182: break; ! 183: ! 184: case IDD_FILENAME: ! 185: /* Enable the OK button if the edit control has text. */ ! 186: EnableWindow ( GetDlgItem (hwnd, IDOK), ! 187: GetWindowTextLength (GET_WM_COMMAND_HWND(wParam, lParam))); ! 188: break; ! 189: ! 190: case IDD_FILES: ! 191: ! 192: /* The files listbox. If file selection has changed, fill ! 193: * the new filename into the property list buffer and set ! 194: * text in edit control. ! 195: */ ! 196: if (GET_WM_COMMAND_CMD(wParam, lParam) == LBN_SELCHANGE){ ! 197: pch = (PSTR) GetProp (hwnd, PROP_FILENAME); ! 198: DlgDirSelectEx(hwnd, (LPSTR)pch, 128, IDD_FILES); ! 199: SetDlgItemText (hwnd, IDD_FILENAME, (LPSTR)pch); ! 200: } ! 201: else if (GET_WM_COMMAND_CMD(wParam, lParam) == LBN_DBLCLK) ! 202: /* if the item was double-clicked, try to open it */ ! 203: SelectFile(hwnd); ! 204: break; ! 205: ! 206: case IDD_DIRS: ! 207: ! 208: /* The directories listbox. Append current filename in edit ! 209: * control (stripped of the path prefix) to the name from ! 210: * the property list and set the new string in the edit ! 211: * control. ! 212: */ ! 213: if (GET_WM_COMMAND_CMD(wParam, lParam) == LBN_SELCHANGE) { ! 214: ! 215: PSTR pch2, pchT, pchS; ! 216: ! 217: pch = (PSTR) GetProp (hwnd, PROP_FILENAME); ! 218: ! 219: /* Get the new drive/dir */ ! 220: DlgDirSelectEx(hwnd, pch, 128, IDD_DIRS); ! 221: pch2 = pch + lstrlen(pch); ! 222: ! 223: /* Fetch current contents of dialog's edit control and append ! 224: * it to name from property list... */ ! 225: GetDlgItemText(hwnd,IDD_FILENAME,(LPSTR)pch2,64); ! 226: if (*pch2 == 0){ ! 227: SetDlgItemText(hwnd, IDD_FILENAME, DEFFILESEARCH); ! 228: GetDlgItemText(hwnd,IDD_FILENAME,(LPSTR)pch2,64); ! 229: } ! 230: else { ! 231: pchS = pch; ! 232: for (pchT = pch = pch2; *pch; pch++) { ! 233: if (*pch == '\\' || *pch == ':') ! 234: pchT = pch2; ! 235: else ! 236: *pchT++ = *pch; ! 237: } ! 238: *pchT = 0; ! 239: pch = pchS; ! 240: } ! 241: ! 242: /* Set the edit control with new string */ ! 243: SetDlgItemText (hwnd, IDD_FILENAME, (LPSTR)pch); ! 244: } ! 245: else if (GET_WM_COMMAND_CMD(wParam, lParam) == LBN_DBLCLK) ! 246: SelectFile (hwnd); ! 247: break; ! 248: ! 249: default: ! 250: return FALSE; ! 251: } ! 252: break; ! 253: ! 254: default: ! 255: return FALSE; ! 256: } ! 257: return TRUE; ! 258: } ! 259: ! 260: /**************************************************************************** ! 261: * * ! 262: * FUNCTION : GetFilename ( pstr ) * ! 263: * * ! 264: * PURPOSE : Gets a filename from the user by calling the File/Open * ! 265: * dialog. * ! 266: * * ! 267: ****************************************************************************/ ! 268: VOID APIENTRY GetFileName(PSTR pstr) ! 269: { ! 270: #ifdef NOTCOMMONDIALOGS ! 271: DialogBoxParam (hInst, IDD_FILEOPEN, hwndFrame, FileOpenDlgProc, (LONG)pstr); ! 272: #else ! 273: OPENFILENAME ofn; ! 274: CHAR szFilterSpec [128] = /* file type filters */ ! 275: "TEXT FILES(*.TXT)\0*.*\0"; ! 276: ! 277: #define MAXFILENAME 256 ! 278: CHAR szFileName[MAXFILENAME]; ! 279: CHAR szFileTitle[MAXFILENAME]; ! 280: ! 281: strcpy(szFileName, ""); /* these need be NULL*/ ! 282: strcpy(szFileTitle, ""); ! 283: ! 284: /* fill in non-variant fields of OPENFILENAME struct. */ ! 285: ofn.lStructSize = sizeof(OPENFILENAME); ! 286: ofn.hwndOwner = NULL; ! 287: ofn.lpstrFilter = szFilterSpec; ! 288: ofn.lpstrCustomFilter = NULL; ! 289: ofn.nMaxCustFilter = 0; ! 290: ofn.nFilterIndex = 0; ! 291: ofn.lpstrFile = szFileName; ! 292: ofn.nMaxFile = MAXFILENAME; ! 293: ofn.lpstrInitialDir = NULL; ! 294: ofn.lpstrFileTitle = szFileTitle; ! 295: ofn.nMaxFileTitle = MAXFILENAME; ! 296: ofn.lpstrTitle = "Open TextFiles"; ! 297: ofn.lpstrDefExt = "TXT"; ! 298: ofn.Flags = OFN_FILEMUSTEXIST; ! 299: /* Use standard open dialog */ ! 300: if (!GetOpenFileName ((LPOPENFILENAME)&ofn)){ ! 301: *pstr = 0; ! 302: MessageBox(hwndFrame, " FileOpen FAILed!", "Multipad", MB_OK | IDOK); ! 303: } ! 304: else{ ! 305: strcpy(pstr, ofn.lpstrFile); ! 306: } ! 307: ! 308: #endif ! 309: return; ! 310: ! 311: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.