|
|
1.1.1.4 ! root 1: ! 2: /******************************************************************************\ ! 3: * This is a part of the Microsoft Source Code Samples. ! 4: * Copyright (C) 1993 Microsoft Corporation. ! 5: * All rights reserved. ! 6: * This source code is only intended as a supplement to ! 7: * Microsoft Development Tools and/or WinHelp documentation. ! 8: * See these sources for detailed information regarding the ! 9: * Microsoft samples programs. ! 10: \******************************************************************************/ ! 11: 1.1 root 12: /******************************************************************************* 1.1.1.4 ! root 13: * * ! 14: * MODULE : DLGOPEN.C * ! 15: * * 1.1 root 16: * DESCRIPTION : Routines to display a standard File/Open and File/Save * 1.1.1.4 ! root 17: * dialog boxes. * ! 18: * * ! 19: * FUNCTIONS : DlgOpenFile() - Displays a dialog box for opening or saving a* ! 20: * file. * ! 21: * * ! 22: * DlgfnOpen() - Dialog function for the above dialog. * ! 23: * * ! 24: * AddExt() - Adds an extension to a filename if not * ! 25: * already present. * ! 26: * * ! 27: * FSearchSpec() - Checks if given string contains a wildcard * ! 28: * character. * ! 29: * * ! 30: * FillListBox() - Fills listbox with files that match specs. * ! 31: * * ! 32: * DlgCheckOkEnable() - Enables <OK> button iff there's text in * ! 33: * the edit control. * ! 34: * * ! 35: * NOTE : These routines require that the app. be running * ! 36: * SS = DS since they use near pointers into the stack. * ! 37: * * 1.1 root 38: *******************************************************************************/ 39: #include <windows.h> 40: #include <string.h> 41: #include "showdib.h" 42: 43: static PSTR szExt; 44: static PSTR szFileName; 45: static PSTR szTitle; 46: static DWORD flags; 47: static WORD fOpt; 48: 49: /* Forward declarations of helper functions */ 50: 51: static VOID NEAR DlgCheckOkEnable(HWND hwnd, INT idEdit, UINT message); 52: static CHAR *NEAR FillListBox (HWND,CHAR*, UINT); 53: static BOOL NEAR FSearchSpec (CHAR*); 54: static VOID NEAR AddExt (CHAR*,CHAR*); 55: 1.1.1.4 ! root 56: #define DLGOPEN_UNUSED 0 1.1 root 57: 58: /* Mask to eliminate bogus style and bitcount combinations ... 59: * RLE formats, if chosen should be matched with the bitcounts: 60: * RLE4 scheme should be used only for 4 bitcount DIBs. 1.1.1.4 ! root 61: * RLE8 " " " " " " 8 " " 1.1 root 62: * 63: * BITCOUNTMASK is indexed by DLGOPEN_RLE4 >> 4, DLGOPEN_RLE8 >> 4 64: * and DLGOPEN_RLE8 >> 4 65: */ 66: 67: static WORD BITCOUNTMASK[] = { DLGOPEN_UNUSED, 1.1.1.4 ! root 68: DLGOPEN_1BPP | DLGOPEN_8BPP | DLGOPEN_24BPP, ! 69: DLGOPEN_1BPP | DLGOPEN_4BPP | DLGOPEN_24BPP, ! 70: DLGOPEN_UNUSED, ! 71: 0 }; 1.1 root 72: 73: 74: /******************************************************************************* 1.1.1.4 ! root 75: * * ! 76: * FUNCTION :DlgOpen(LPSTR szFile) * ! 77: * * 1.1 root 78: * PURPOSE :Display dialog box for opening files. Allow user to interact * 1.1.1.4 ! root 79: * with dialogbox, change directories as necessary, and try to * ! 80: * open file if user selects one. Automatically append * ! 81: * extension to filename if necessary. * ! 82: * This routine correctly parses filenames containing KANJI * ! 83: * characters. * ! 84: * * ! 85: * RETURNS : - Handle to the opened file if legal filename. * ! 86: * - 0 if user presses <cancel> * ! 87: * - 1 if filename entered is illegal * ! 88: * * 1.1 root 89: *******************************************************************************/ 90: HFILE APIENTRY DlgOpenFile ( 1.1.1.4 ! root 91: HWND hwndParent, ! 92: CHAR *szTitleIn, ! 93: DWORD flagsIn, ! 94: CHAR *szExtIn, ! 95: CHAR *szFileNameIn, ! 96: WORD *pfOpt) 1.1 root 97: { 98: INT fh; 99: FARPROC lpProc; 100: CHAR achFile[128]; 101: CHAR achExt[128]; 102: HANDLE hInstance; 103: WORD w; 104: 105: if (pfOpt == NULL) 106: pfOpt = &w; 107: 108: flags = flagsIn; 109: fOpt = *pfOpt; 110: 111: lstrcpy (szFileName = achFile, szFileNameIn); 112: lstrcpy (szExt = achExt, szExtIn); 113: szTitle = szTitleIn; 114: 115: hInstance = (HANDLE)GetWindowLong (hwndParent, GWL_HINSTANCE); 116: 117: /* Show the dialog box */ 118: lpProc = MakeProcInstance ((FARPROC)DlgfnOpen, hInstance); 1.1.1.2 root 119: fh = DialogBox (hInstance, "DlgOpenBox", hwndParent, lpProc); 1.1 root 120: FreeProcInstance (lpProc); 121: 122: if (fh != 0){ 1.1.1.4 ! root 123: lstrcpy (szFileNameIn, szFileName); 1.1 root 124: *pfOpt = fOpt; 125: } 126: return fh; 127: } 128: 129: /**************************************************************************** 1.1.1.4 ! root 130: * * ! 131: * FUNCTION :DlgfnOpen (hwnd, msg, wParam, lParam) * ! 132: * * ! 133: * PURPOSE :Dialog function for File/Open dialog. * ! 134: * * 1.1 root 135: ****************************************************************************/ 136: LONG APIENTRY DlgfnOpen ( 137: HWND hwnd, 138: UINT msg, 139: UINT wParam, 140: LONG lParam) 141: { 142: INT result = -1; /* Assume illegal filename initially */ 143: int w; 144: CHAR c; 145: WORD f; 146: OFSTRUCT of; 147: RECT rc, rcCtl; 148: HWND hwndT; 149: BOOL fEnable; 150: 151: switch (msg) { 1.1.1.4 ! root 152: case WM_INITDIALOG: ! 153: if (szTitle && *szTitle) ! 154: SetWindowText (hwnd, szTitle); ! 155: ! 156: /* Set text on <OK> button according to mode (File/Open or File/Save) */ ! 157: if (flags & OF_SAVE) ! 158: SetDlgItemText (hwnd, IDOK, "&Save"); ! 159: if (flags & OF_OPEN) ! 160: SetDlgItemText (hwnd, IDOK, "&Open"); ! 161: ! 162: if ((flags & OF_NOOPTIONS) && ! 163: (hwndT = GetDlgItem(hwnd,DLGOPEN_FOLDOUT))) ! 164: EnableWindow (hwndT,FALSE); ! 165: ! 166: if (hwndT = GetDlgItem (hwnd, DLGOPEN_SMALL)) { ! 167: GetWindowRect (hwnd,&rc); ! 168: GetWindowRect (GetDlgItem(hwnd,DLGOPEN_SMALL),&rcCtl); ! 169: ! 170: SetWindowPos (hwnd, ! 171: NULL, ! 172: 0, ! 173: 0, ! 174: rcCtl.left - rc.left, ! 175: rc.bottom - rc.top, ! 176: SWP_NOZORDER | SWP_NOMOVE); ! 177: } ! 178: /* fill list box with filenames that match specifications, and ! 179: * fill static field with path name. ! 180: */ ! 181: FillListBox(hwnd,szExt, WM_INITDIALOG); ! 182: ! 183: /* If in Save mode, set the edit control with default (current) ! 184: * file name,and select the corresponding entry in the listbox. ! 185: */ ! 186: if ((flags & OF_SAVE) && *szFileName) { ! 187: SetDlgItemText (hwnd, DLGOPEN_EDIT, szFileName); ! 188: SendDlgItemMessage (hwnd, ! 189: DLGOPEN_FILE_LISTBOX, ! 190: LB_SELECTSTRING, ! 191: 0, ! 192: (LONG)(LPSTR)szFileName); ! 193: } ! 194: else { ! 195: /* Set the edit field with the default extensions... */ ! 196: if (flags & OF_NOSHOWSPEC) ! 197: SetDlgItemText (hwnd, DLGOPEN_EDIT, ""); ! 198: else ! 199: SetDlgItemText (hwnd, DLGOPEN_EDIT, szExt); ! 200: } ! 201: /* ...and select all text in the edit field */ ! 202: /* JAP added HWND cast*/ ! 203: SendMessage((HWND)GetDlgItem(hwnd, DLGOPEN_EDIT), EM_SETSEL, GET_EM_SETSEL_MPS(0, 0x7fff)); ! 204: ! 205: /* check all options that are set */ ! 206: for ( f = DLGOPEN_1BPP; f; f<<=1) ! 207: CheckDlgButton(hwnd, (INT)FID(f), (WORD) (fOpt & f)); ! 208: ! 209: break; ! 210: ! 211: case WM_COMMAND: ! 212: w = LOWORD(wParam); ! 213: switch (w) { ! 214: case IDOK: ! 215: if (IsWindowEnabled (GetDlgItem(hwnd, IDOK))) { ! 216: /* Get contents of edit field and add search spec. if it ! 217: * does not contain one. ! 218: */ ! 219: GetDlgItemText (hwnd, DLGOPEN_EDIT, (LPSTR)szFileName, 128); ! 220: ! 221: w = lstrlen(szFileName)-1; ! 222: c = szFileName[w]; ! 223: switch (c) { ! 224: case '\\': ! 225: case '/': ! 226: szFileName[w] = 0; ! 227: break; ! 228: } ! 229: if (SetCurrentDirectory ((LPSTR)szFileName)) ! 230: lstrcpy (szFileName,szExt); ! 231: ! 232: /* Try to open path. If successful, fill listbox with ! 233: * contents of new directory. Otherwise, open datafile. ! 234: */ ! 235: if (FSearchSpec(szFileName)) { ! 236: lstrcpy (szExt, FillListBox (hwnd, szFileName, WM_COMMAND)); ! 237: if (flags & OF_NOSHOWSPEC) { ! 238: SetDlgItemText (hwnd, DLGOPEN_EDIT, ""); ! 239: } else { ! 240: SetDlgItemText (hwnd, DLGOPEN_EDIT, szExt); 1.1 root 241: } 1.1.1.4 ! root 242: break; ! 243: } 1.1 root 244: 1.1.1.4 ! root 245: /* Make filename upper case and if it's a legal DOS ! 246: * name, try to open the file. ! 247: */ ! 248: AnsiUpper(szFileName); ! 249: AddExt(szFileName,szExt); ! 250: result = (INT)OpenFile(szFileName, &of, (WORD)flags); ! 251: ! 252: if (result != -1) { ! 253: lstrcpy(szFileName,of.szPathName); ! 254: } ! 255: else if (flags & OF_MUSTEXIST) { ! 256: MessageBeep(0); ! 257: return 0L; ! 258: } ! 259: ! 260: /* Get the state of all checked options */ ! 261: for (f = DLGOPEN_1BPP; f; f <<= 1){ ! 262: if (IsDlgButtonChecked (hwnd, FID (f))) ! 263: fOpt |= f; ! 264: else ! 265: fOpt &= ~f; ! 266: } ! 267: ! 268: EndDialog (hwnd, result); ! 269: } ! 270: break; ! 271: ! 272: case DLGOPEN_OPTION + DLGOPEN_RLE4: ! 273: case DLGOPEN_OPTION + DLGOPEN_RLE8: ! 274: case DLGOPEN_OPTION + DLGOPEN_RGB: ! 275: /* Mask out incompatible bitcount options and gray the ! 276: * appropriate radiobuttons. ! 277: */ ! 278: for (f = DLGOPEN_1BPP; f <= DLGOPEN_24BPP; f <<= 1){ ! 279: fEnable = !(f & BITCOUNTMASK [IDF(w) >> 4 ]); ! 280: EnableWindow (GetDlgItem (hwnd, FID(f)), fEnable); ! 281: ! 282: /* If the radiobutton is being grayed, uncheck it and ! 283: * and check an "allowed" option so the bitcount group ! 284: * is still accessible via the keyboard ! 285: */ ! 286: if (!fEnable && IsDlgButtonChecked (hwnd, FID (f))){ ! 287: CheckDlgButton(hwnd, FID(f), FALSE); ! 288: CheckDlgButton(hwnd, FID(IDF(w) >> 3), TRUE); ! 289: } ! 290: } ! 291: break; ! 292: ! 293: case IDCANCEL: ! 294: /* User pressed cancel. Just take down dialog box. */ ! 295: EndDialog (hwnd, 0); ! 296: break; ! 297: ! 298: /* User single clicked or doubled clicked in listbox - ! 299: * Single click means fill edit box with selection. ! 300: * Double click means go ahead and open the selection. ! 301: */ ! 302: case DLGOPEN_FILE_LISTBOX: ! 303: case DLGOPEN_DIR_LISTBOX: ! 304: switch (GET_WM_COMMAND_CMD(wParam, lParam)) { ! 305: /* Single click case */ ! 306: case LBN_SELCHANGE: ! 307: /* Get selection, which may be either a prefix to a ! 308: * new search path or a filename. DlgDirSelectEx parses ! 309: * selection, and appends a backslash if selection ! 310: * is a prefix ! 311: */ ! 312: DlgDirSelectEx(hwnd, szFileName, 128, LOWORD(wParam)); ! 313: w = lstrlen(szFileName)-1; ! 314: c = szFileName[w]; ! 315: switch (c) { ! 316: case ':': ! 317: lstrcat (szFileName,"."); ! 318: break; ! 319: case '\\': ! 320: case '/': ! 321: szFileName[w] = 0; ! 322: break; ! 323: } ! 324: SetDlgItemText(hwnd, DLGOPEN_EDIT, szFileName); ! 325: break; ! 326: /* Double click case - first click has already been ! 327: * processed as single click ! 328: */ ! 329: case LBN_DBLCLK: ! 330: PostMessage (hwnd,WM_COMMAND,IDOK,0L); ! 331: break; ! 332: } ! 333: break; ! 334: ! 335: case DLGOPEN_EDIT: ! 336: DlgCheckOkEnable(hwnd, DLGOPEN_EDIT, HIWORD(lParam)); ! 337: break; ! 338: ! 339: case DLGOPEN_FOLDOUT: ! 340: GetWindowRect(hwnd,&rc); ! 341: GetWindowRect(GetDlgItem(hwnd,DLGOPEN_BIG),&rcCtl); ! 342: ! 343: if ((rcCtl.left <= rc.right) && (rcCtl.top <= rc.bottom)) ! 344: GetWindowRect (GetDlgItem (hwnd, DLGOPEN_SMALL), &rcCtl); ! 345: ! 346: SetWindowPos (hwnd, ! 347: NULL, ! 348: 0, ! 349: 0, ! 350: rcCtl.left - rc.left, ! 351: rc.bottom - rc.top, ! 352: SWP_NOZORDER | SWP_NOMOVE); ! 353: break; ! 354: } ! 355: default: ! 356: return FALSE; 1.1 root 357: } 358: return TRUE; 359: } 360: 361: /**************************************************************************** 1.1.1.4 ! root 362: * * 1.1 root 363: * FUNCTION : static void NEAR DlgCheckOkEnable(hwnd, idEdit, message) * 1.1.1.4 ! root 364: * * 1.1 root 365: * PURPOSE : Enables the <OK> button in a dialog box iff the edit item * 1.1.1.4 ! root 366: * contains text. * ! 367: * * 1.1 root 368: ****************************************************************************/ 369: static VOID NEAR DlgCheckOkEnable( 1.1.1.4 ! root 370: HWND hwnd, ! 371: INT idEdit, 1.1 root 372: UINT message) 373: { 374: if (message == EN_CHANGE) { 1.1.1.4 ! root 375: EnableWindow ( GetDlgItem (hwnd, IDOK), ! 376: (BOOL)SendMessage (GetDlgItem (hwnd, idEdit), ! 377: WM_GETTEXTLENGTH, ! 378: 0, 0L)); 1.1 root 379: } 380: } 381: 382: /**************************************************************************** 1.1.1.4 ! root 383: * * ! 384: * FUNCTION : AddExt (pch, ext) * ! 385: * * 1.1 root 386: * PURPOSE : Add an extension to a filename if none is already specified* 1.1.1.4 ! root 387: * * 1.1 root 388: ****************************************************************************/ 389: static VOID NEAR AddExt ( 1.1.1.4 ! root 390: CHAR *pch, /* File name */ 1.1 root 391: CHAR *ext) /* Extension to add */ 392: { 393: CHAR acExt[20]; 394: CHAR *pext = acExt; 395: 396: while (*ext && *ext != '.') 1.1.1.4 ! root 397: ext++; 1.1 root 398: while (*ext && *ext != ';') 1.1.1.4 ! root 399: *pext++ = *ext++; 1.1 root 400: *pext = 0; 401: pext = acExt; 402: 403: while (*pch == '.') { 1.1.1.4 ! root 404: pch++; ! 405: if ((*pch == '.') && pch[1] == '\\') ! 406: pch += 2; /* ..\ */ ! 407: if (*pch == '\\') ! 408: pch++; /* .\ */ 1.1 root 409: } 410: while (*pch != '\0') 1.1.1.4 ! root 411: if (*pch++ == '.') ! 412: return; 1.1 root 413: 414: // *pch++ = '.'; 415: do 1.1.1.4 ! root 416: *pch++ = *pext; 1.1 root 417: while (*pext++ != '\0'); 418: } 419: /**************************************************************************** 1.1.1.4 ! root 420: * * ! 421: * FUNCTION : FSearchSpec (sz) * ! 422: * * 1.1 root 423: * PURPOSE : Checks to see if NULL-terminated strings contains a "*" or * 1.1.1.4 ! root 424: * a "?". * ! 425: * * 1.1 root 426: * RETURNS : TRUE - if the above characters are found in the string * 1.1.1.4 ! root 427: * FALSE - otherwise. * ! 428: * * 1.1 root 429: ****************************************************************************/ 1.1.1.4 ! root 430: static BOOL NEAR FSearchSpec(CHAR *sz) 1.1 root 431: { 432: for (; *sz ;sz++) { 1.1.1.4 ! root 433: if (*sz == '*' || *sz == '?') ! 434: return TRUE; 1.1 root 435: } 436: return FALSE; 437: } 438: 439: /**************************************************************************** 1.1.1.4 ! root 440: * * ! 441: * FUNCTION : static char * NEAR FillListBox (hDlg,pFile, cmd) * ! 442: * * 1.1 root 443: * PURPOSE : Fill list box with filenames that match specifications, and* 1.1.1.4 ! root 444: * fills the static field with the path name. * ! 445: * * ! 446: * RETURNS : A pointer to the pathname. * ! 447: * * 1.1 root 448: ****************************************************************************/ 449: static CHAR * NEAR FillListBox ( 450: HWND hDlg, 451: CHAR *pFile, /* [path]{list of file wild cards, separated by ';'} */ 1.1.1.4 ! root 452: UINT cmd) /* if initdialog, or WM_COMMAND*/ 1.1 root 453: { 454: CHAR ach[128]; 455: CHAR *pch; 456: CHAR *pDir; /* Directory name or path */ 457: CHAR pCurDir[256]; 458: 459: pch = pFile; 460: pDir = ach; 461: if(cmd == WM_INITDIALOG){ 462: while (*pch && *pch != ';') 1.1.1.4 ! root 463: pch++; 1.1 root 464: while ((pch > pFile) && (*pch != '/') && (*pch != '\\')) 1.1.1.4 ! root 465: pch--; 1.1 root 466: if (pch > pFile) { 467: *pch = 0; 468: lstrcpy (pDir, pFile); 469: pFile = pch + 1; 470: } 471: else { 472: lstrcpy (pDir,"."); 473: } 474: } 475: else{ 476: /* since SetCurrentDirectory was called already, I'll use GetCurrentDirectory*/ 477: /* to get pDir*/ 478: GetCurrentDirectory(256, pCurDir); 479: strcpy(pDir, pCurDir); 480: } 481: DlgDirList (hDlg, pDir, (INT)DLGOPEN_DIR_LISTBOX, (INT)DLGOPEN_PATH,(WORD)ATTRDIRLIST); 482: SendDlgItemMessage (hDlg, DLGOPEN_FILE_LISTBOX, LB_RESETCONTENT, 0, 0L); 483: SendDlgItemMessage (hDlg, DLGOPEN_FILE_LISTBOX, WM_SETREDRAW, FALSE, 0L); 1.1.1.4 ! root 484: pDir = pFile; /* save pFile to return */ 1.1 root 485: while (*pFile) { 1.1.1.4 ! root 486: pch = ach; ! 487: while (*pFile==' ') ! 488: pFile++; ! 489: while (*pFile && *pFile != ';') ! 490: *pch++ = *pFile++; ! 491: *pch = 0; ! 492: if (*pFile) ! 493: pFile++; ! 494: SendDlgItemMessage (hDlg, ! 495: DLGOPEN_FILE_LISTBOX, ! 496: LB_DIR,ATTRFILELIST, ! 497: (LONG)(LPSTR)ach); 1.1 root 498: } 499: SendDlgItemMessage (hDlg, DLGOPEN_FILE_LISTBOX, WM_SETREDRAW, TRUE, 0L); 500: InvalidateRect (GetDlgItem (hDlg, DLGOPEN_FILE_LISTBOX), NULL, TRUE); 501: 502: return pDir; 503: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.