home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk12 / stock / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-09  |  17.8 KB  |  685 lines

  1. /***    file.C - standard file dialogs
  2.  *
  3.  */
  4.  
  5. #define INCL_DOSERRORS
  6. #define INCL_WINCOMMON
  7. #define INCL_WINBUTTONS
  8. #define INCL_WINDIALOGS
  9. #define INCL_WINFRAMEMGR
  10. #define INCL_WININPUT
  11. #define INCL_WINLISTBOXES
  12. #define INCL_WINMENUS
  13. #define INCL_WINMESSAGEMGR
  14.  
  15. #include <os2.h>
  16. #include <stddef.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20.  
  21. #include "fileid.h"
  22. #include "file.h"
  23. #include "mem.h"
  24.  
  25. #define dbg(x)    x
  26.  
  27. typedef struct {    /* fctl */
  28.     char    *pszTitle;            // Dialog box title
  29.     char    *pszPattern;        // wild-card file pattern
  30.     char    achPath[CCHMAXPATH];    // full path of file
  31. } FILECTL;
  32. typedef FILECTL *PFILECTL;
  33.  
  34. MRESULT EXPENTRY FileDlgProc(HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2);
  35.  
  36. USHORT        beginSearch(char *psz, USHORT *pattr);
  37. BOOL        changeDir(HWND hwndDlg, PFILECTL pfctl);
  38. BOOL        changeDrive(HWND hwndDlg, PFILECTL pfctl);
  39. BOOL        changeFile(HWND hwndDlg, PFILECTL pfctl);
  40. void        endSearch(void);
  41. USHORT        nextSearch(char *psz, USHORT *pattr);
  42. USHORT        niceWinDlgBox(HWND hwndParent, HWND hwndOwner, PFNWP pfnDlgProc,
  43.                 HMODULE hmod, USHORT idDlg, PVOID pCreateParams);
  44. BOOL        setCurDir(HWND hwndDlg);
  45. BOOL        setDir(HWND hwndDlg);
  46. BOOL        setDrive(HWND hwndDlg);
  47. BOOL        setFile(HWND hwndDlg);
  48. char *        trimBlanks(char * psz);
  49.  
  50.  
  51. static BOOL    fDidOK;            // true if OK was just pressed
  52.  
  53. static HDIR    hdir;            // directory search handle
  54.  
  55. static USHORT    itDrive;        // last item selected in Drive LB
  56. static USHORT    itDir;            // last item selected in Dir LB
  57. static USHORT    itFile;            // last item selected in File LB
  58.  
  59.  
  60. char *FileOpen(HWND hwndOwner,char *pszTitle,char *pszPattern)
  61. {
  62.     BOOL    fOK;
  63.     FILECTL fctl;
  64.  
  65.     fctl.pszTitle = pszTitle;
  66.     fctl.pszPattern = pszPattern;
  67.  
  68.     fOK = niceWinDlgBox(HWND_DESKTOP, hwndOwner, FileDlgProc,
  69.                 NULL, IDD_FILE, &fctl);
  70.  
  71.     if (fOK)
  72.     return MemStrDup(fctl.achPath);
  73.     else
  74.     return NULL;
  75. }
  76.  
  77.  
  78. USHORT    niceWinDlgBox(HWND hwndParent, HWND hwndOwner, PFNWP pfnDlgProc,
  79.                 HMODULE hmod, USHORT idDlg, PVOID pCreateParams)
  80. {
  81.     HAB        hab;
  82.     HWND    hwndDlg;
  83.     USHORT  us;
  84.     SWP        swp;
  85.     SWP        swpDesk;
  86.  
  87.     hwndDlg = WinLoadDlg(hwndParent, hwndOwner, pfnDlgProc,
  88.                 hmod, idDlg, pCreateParams);
  89.  
  90.     WinQueryWindowPos(hwndDlg,&swp);  // get window position
  91.     WinQueryWindowPos(HWND_DESKTOP,&swpDesk); // get desktop
  92.  
  93.     // center dialog box on screen
  94.  
  95.     swp.x = (swpDesk.cx - swp.cx) >> 1;
  96.     swp.y = (swpDesk.cy - swp.cy) >> 1;
  97.  
  98.     hab = WinQueryAnchorBlock(hwndDlg);
  99.     WinSetMultWindowPos(hab,&swp,1);
  100.  
  101.     us = WinProcessDlg(hwndDlg);
  102.     WinDestroyWindow(hwndDlg);
  103.  
  104.     return us;
  105. }
  106.  
  107. /***    FileDlgProc - "File" Dialog Procedure
  108. *
  109. */
  110. MRESULT EXPENTRY FileDlgProc(HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2)
  111. {
  112.     static char        ach[CCHMAXPATH];
  113.        USHORT   attr;
  114.     static HWND        hwndFocus;
  115.     static HWND        hwndLBDir;
  116.     static HWND        hwndLBDrive;
  117.     static HWND        hwndLBFile;
  118.     static HWND        hwndText;
  119.     static USHORT   idLB;
  120.     static USHORT   idLBPrevious;
  121.        USHORT   it;
  122.     static PFILECTL pfctl;
  123.        USHORT   rc;
  124.  
  125.     switch (msg) {
  126.  
  127.     case WM_INITDLG:
  128.     pfctl = (PFILECTL)(VOID *)SHORT1FROMMP(mp2);    // set file control block
  129.  
  130.     hwndLBDir   = WinWindowFromID(hwnd, IDL_DIR);
  131.     hwndLBDrive = WinWindowFromID(hwnd, IDL_DRIVE);
  132.     hwndLBFile  = WinWindowFromID(hwnd, IDL_FILE);
  133.     hwndText = WinWindowFromID(hwnd, IDC_TEXT);
  134.     idLBPrevious = 0;        // no previous focus
  135.  
  136.     WinSetWindowText(hwnd, pfctl->pszTitle);  // set dialog title
  137.     WinSetWindowText(hwndText, pfctl->pszPattern);    // init file pattern
  138.  
  139.     setDrive(hwnd);            // init drive list box
  140.     setDir(hwnd);            // update dir list
  141.     setFile(hwnd);            // update file list
  142.     setCurDir(hwnd);        // init current drive/dir string
  143.  
  144.     WinDefDlgProc(hwnd, msg, mp1, mp2); // do default stuff
  145.  
  146.     hwndFocus = hwndText;
  147.     WinSetFocus(HWND_DESKTOP,hwndFocus);
  148.     return (MRESULT) TRUE;
  149.  
  150.     case WM_COMMAND:
  151.     switch (LOUSHORT(mp1)) {
  152.  
  153.     case DID_OK:            // store updated list
  154.         dbg( printf("WM_COMMAND: DID_OK\n") );
  155.         WinQueryWindowText(hwndText, CCHMAXPATH, ach);
  156.         trimBlanks(ach);    // Trim leading/trailing blanks
  157.         WinSetWindowText(hwndText, ach);
  158.         dbg( printf("   opening %s\n",ach) );
  159.         rc = DosQFileMode(ach,&attr,0L); // Does file exist?
  160.         dbg( printf("   DosQFileMode rc=%d\n",rc) );
  161.  
  162.         // If directory is specified, pretend file not found
  163.  
  164.         if (attr & FILE_DIRECTORY)
  165.         rc = ERROR_FILE_NOT_FOUND;
  166.  
  167.         fDidOK = TRUE;
  168.         switch (rc) {
  169.  
  170.         case NO_ERROR: {
  171.         USHORT    cch=CCHMAXPATH;
  172.  
  173.         rc = DosQPathInfo(
  174.             ach,        // Path specifiec
  175.             FIL_QUERYFULLNAME,    // Get fully-qualified name
  176.             pfctl->achPath,    // Return buffer
  177.             CCHMAXPATH,        // Return buffer size
  178.             0L            // Reserved
  179.             );
  180.         if (rc == 0)
  181.             WinDismissDlg(hwnd,TRUE); // return SUCCESS
  182.         }
  183.         return FALSE;
  184.  
  185.         case ERROR_FILE_NOT_FOUND:
  186.         case ERROR_PATH_NOT_FOUND:
  187.         // check for and process user wild-card pattern
  188.         if (strcmp(ach,pfctl->pszPattern) != 0)
  189.             setFile(hwnd);
  190.         break;            // continue dialog
  191.  
  192.         case ERROR_ACCESS_DENIED:
  193.         case ERROR_DRIVE_LOCKED:
  194.         case ERROR_NOT_DOS_DISK:
  195.         WinMessageBox(HWND_DESKTOP, hwnd, "Access Denied",
  196.                   "Error", NULL, MB_ICONEXCLAMATION);
  197.         break;
  198.  
  199.         default:
  200.         break;
  201.         }
  202.         WinSetFocus(HWND_DESKTOP,hwndFocus);
  203.         break;
  204.  
  205.     case DID_CANCEL:
  206.         WinDismissDlg(hwnd, NULL);
  207.         break;
  208.     }
  209.     break;
  210.  
  211.     case WM_CONTROL:
  212.     idLBPrevious = idLB;        // remember previous focus
  213.     idLB = SHORT1FROMMP(mp1);
  214.     dbg (printf("WM_CONTROL: id = %04x hwndLB=%08x\n",idLB,mp2) );
  215.     switch (SHORT2FROMMP(mp1)) {
  216.         case LN_ENTER:
  217.         switch (idLB) {
  218.  
  219.         case IDL_DRIVE:
  220.             dbg( printf("LN_ENTER: IDL_DRIVE\n") );
  221.             changeDrive(hwnd,pfctl);
  222.             return FALSE;
  223.  
  224.         case IDL_DIR:
  225.             dbg( printf("LN_ENTER: IDL_DIR\n") );
  226.             changeDir(hwnd,pfctl);
  227.             return FALSE;
  228.  
  229.         case IDL_FILE:
  230.             dbg( printf("LN_ENTER: IDL_FILE\n") );
  231.             changeFile(hwnd,pfctl);
  232.             WinSendMsg(hwnd,WM_COMMAND,MPFROMSHORT(DID_OK),NULL);
  233.             return FALSE;
  234.  
  235.         default:
  236.             dbg( printf("LN_ENTER: unknown list box = %04x\n",idLB) );
  237.             return WinDefDlgProc(hwnd, msg, mp1, mp2);
  238.             break;
  239.         }
  240.         break;
  241.  
  242.         case LN_SELECT:
  243.         switch (idLB) {
  244.  
  245.         case IDL_DRIVE:
  246.             dbg( printf("LN_SELECT: IDL_DRIVE\n") );
  247.             it = SHORT1FROMMR(WinSendMsg(hwndFocus, LM_QUERYSELECTION, 0L, 0L));
  248.             if (it != LIT_NONE) {
  249.             if (fDidOK)
  250.                 fDidOK = FALSE;
  251.             else {
  252.                 itDrive = it;
  253.                 WinSendMsg(hwndFocus, LM_QUERYITEMTEXT,
  254.                 MPFROM2SHORT(it,CCHMAXPATH), MPFROMP(ach));
  255.                 strcat(ach,pfctl->pszPattern);
  256.                 WinSetWindowText(hwndText, ach);
  257.             }
  258.             }
  259.             idLB = 0;        // allow LN_SETFOCUS to work
  260.             return FALSE;
  261.  
  262.         case IDL_DIR:
  263.             dbg( printf("LN_SELECT: IDL_DIR\n") );
  264.             it = SHORT1FROMMR(WinSendMsg(hwndFocus, LM_QUERYSELECTION, 0L, 0L));
  265.             if (it != LIT_NONE) {
  266.             if (fDidOK)
  267.                 fDidOK = FALSE;
  268.             else {
  269.                 itDir = it;
  270.                 WinSendMsg(hwndFocus, LM_QUERYITEMTEXT,
  271.                 MPFROM2SHORT(it,CCHMAXPATH), MPFROMP(ach));
  272.                 strcat(ach,"\\");
  273.                 strcat(ach,pfctl->pszPattern);
  274.                 WinSetWindowText(hwndText, ach);
  275.             }
  276.             }
  277.             idLB = 0;        // allow LN_SETFOCUS to work
  278.             return FALSE;
  279.  
  280.         case IDL_FILE:
  281.             dbg( printf("LN_SELECT: IDL_FILE\n") );
  282.             it = SHORT1FROMMR(WinSendMsg(hwndFocus, LM_QUERYSELECTION, 0L, 0L));
  283.             if (it != LIT_NONE) {
  284.             if (fDidOK)
  285.                 fDidOK = FALSE;
  286.             else {
  287.                 itFile = it;
  288.                 changeFile(hwnd,pfctl);
  289.             }
  290.             }
  291.             idLB = 0;        // allow LN_SETFOCUS to work
  292.             return FALSE;
  293.  
  294.         default:
  295.             dbg( printf("LN_SELECT: unknown list box = %04x\n",idLB) );
  296.             return WinDefDlgProc(hwnd, msg, mp1, mp2);
  297.             break;
  298.         }
  299.         break;
  300.  
  301.         case LN_SETFOCUS:
  302.         //* The following test prevents reselecting the last
  303.         //  selected list box item when the user is scrolling
  304.         //  the list box.
  305.  
  306.         if (idLB == idLBPrevious)
  307.             break;        // no actual focus change
  308.  
  309.         switch (idLB) {
  310.  
  311.         case IDL_DRIVE:
  312.             dbg( printf("LN_SETFOCUS: IDL_DRIVE\n") );
  313.             hwndFocus = hwndLBDrive;
  314.             WinSendMsg(hwndFocus, LM_SELECTITEM,
  315.                 MPFROMSHORT(itDrive), MPFROMSHORT(TRUE));
  316.             return FALSE;
  317.  
  318.         case IDL_DIR:
  319.             dbg( printf("LN_SETFOCUS: IDL_DIR\n") );
  320.             hwndFocus = hwndLBDir;
  321.             WinSendMsg(hwndFocus, LM_SELECTITEM,
  322.                 MPFROMSHORT(itDir), MPFROMSHORT(TRUE));
  323.             return FALSE;
  324.  
  325.         case IDL_FILE:
  326.             dbg( printf("LN_SETFOCUS: IDL_FILE\n") );
  327.             hwndFocus = hwndLBFile;
  328.             WinSendMsg(hwndFocus, LM_SELECTITEM,
  329.                 MPFROMSHORT(itFile), MPFROMSHORT(TRUE));
  330.             return FALSE;
  331.  
  332.         default:
  333.             dbg( printf("LN_SETFOCUS: unknown list box = %04x\n",idLB) );
  334.             return WinDefDlgProc(hwnd, msg, mp1, mp2);
  335.             break;
  336.         }
  337.         break;
  338.  
  339.         case LN_KILLFOCUS:
  340.         switch (idLB) {
  341.  
  342.         case IDL_DRIVE:
  343.             dbg( printf("LN_KILLFOCUS: IDL_DRIVE\n") );
  344.             hwndFocus = hwndLBDrive;
  345.             WinSendMsg(hwndFocus, LM_SELECTITEM,
  346.                 MPFROMSHORT(LIT_NONE), MPFROMSHORT(FALSE));
  347.             return FALSE;
  348.  
  349.         case IDL_DIR:
  350.             dbg( printf("LN_KILLFOCUS: IDL_DIR\n") );
  351.             hwndFocus = hwndLBDir;
  352.             WinSendMsg(hwndFocus, LM_SELECTITEM,
  353.                 MPFROMSHORT(LIT_NONE), MPFROMSHORT(FALSE));
  354.             return FALSE;
  355.  
  356.         case IDL_FILE:
  357.             dbg( printf("LN_KILLFOCUS: IDL_FILE\n") );
  358.             hwndFocus = hwndLBFile;
  359.             WinSendMsg(hwndFocus, LM_SELECTITEM,
  360.                 MPFROMSHORT(LIT_NONE), MPFROMSHORT(FALSE));
  361.             return FALSE;
  362.  
  363.         default:
  364.             dbg( printf("LN_KILLFOCUS: unknown list box = %04x\n",idLB) );
  365.             return WinDefDlgProc(hwnd, msg, mp1, mp2);
  366.             break;
  367.         }
  368.         break;
  369.  
  370.         default:
  371.         dbg( printf("notify code= %04x\n",SHORT2FROMMP(mp1)) );
  372.         return WinDefDlgProc(hwnd, msg, mp1, mp2);
  373.     }
  374.     return FALSE;
  375.  
  376.     default:
  377.     return WinDefDlgProc(hwnd, msg, mp1, mp2);
  378.     }
  379.     return FALSE;
  380. }
  381.  
  382.  
  383. BOOL    changeDrive(HWND hwndDlg, PFILECTL pfctl)
  384. {
  385.     USHORT  it;
  386.     USHORT  drv;
  387.     HWND    hwndLBDrive;
  388.     HWND    hwndText;
  389.  
  390.     hwndLBDrive = WinWindowFromID(hwndDlg, IDL_DRIVE);
  391.     hwndText = WinWindowFromID(hwndDlg, IDC_TEXT);
  392.  
  393.     it = SHORT1FROMMR(WinSendMsg(hwndLBDrive, LM_QUERYSELECTION, 0L, 0L));
  394.     if (it != LIT_NONE) {
  395.     itDrive = it;
  396.     drv = SHORT1FROMMR(WinSendMsg(hwndLBDrive, // get drive number
  397.             LM_QUERYITEMHANDLE, MPFROMSHORT(it), 0L));
  398.     DosSelectDisk(drv);        // change drive
  399.     WinSetWindowText(hwndText, pfctl->pszPattern);    // reset pattern
  400.     setDir(hwndDlg);        // update dir list
  401.     setFile(hwndDlg);        // update file list
  402.     setCurDir(hwndDlg);        // update current drive/dir text
  403.     return TRUE;
  404.     }
  405.     return FALSE;
  406. }
  407.  
  408.  
  409. BOOL    changeDir(HWND hwndDlg, PFILECTL pfctl)
  410. {
  411.     char    ach[CCHMAXPATH];
  412.     HWND    hwndLBDir;
  413.     HWND    hwndText;
  414.     USHORT  it;
  415.  
  416.     hwndLBDir = WinWindowFromID(hwndDlg, IDL_DIR);
  417.     hwndText = WinWindowFromID(hwndDlg, IDC_TEXT);
  418.  
  419.     it = SHORT1FROMMR(WinSendMsg(hwndLBDir, LM_QUERYSELECTION, 0L, 0L));
  420.     if (it != LIT_NONE) {
  421.     itDir = it;
  422.     WinSendMsg(hwndLBDir, LM_QUERYITEMTEXT, // get dir
  423.             MPFROM2SHORT(it,CCHMAXPATH), MPFROMP(ach));
  424.     DosChDir(ach, 0L);
  425.     WinSetWindowText(hwndText, pfctl->pszPattern);    // reset pattern
  426.     setDir(hwndDlg);        // update dir list
  427.     setFile(hwndDlg);        // update file list
  428.     setCurDir(hwndDlg);        // update current drive/dir text
  429.     return TRUE;
  430.     }
  431.     return FALSE;
  432. }
  433.  
  434. BOOL    changeFile(HWND hwndDlg, PFILECTL pfctl)
  435. {
  436.     char    ach[CCHMAXPATH];
  437.     HWND    hwndLBFile;
  438.     HWND    hwndText;
  439.     USHORT  it;
  440.  
  441.     hwndText = WinWindowFromID(hwndDlg, IDC_TEXT);
  442.     hwndLBFile = WinWindowFromID(hwndDlg, IDL_FILE);
  443.  
  444.     it = SHORT1FROMMR(WinSendMsg(hwndLBFile, LM_QUERYSELECTION, 0L, 0L));
  445.     if (it != LIT_NONE) {
  446.     itFile = it;
  447.     WinSendMsg(hwndLBFile, LM_QUERYITEMTEXT, // get dir
  448.             MPFROM2SHORT(it,CCHMAXPATH), MPFROMP(ach));
  449.     WinSetWindowText(hwndText, ach);  // set file name
  450.     return TRUE;
  451.     }
  452.     return FALSE;
  453. }
  454.  
  455. BOOL    setDrive(HWND hwndDlg)
  456. {
  457.     char    ach[3];
  458.     ULONG   bmlDrives;
  459.     USHORT  drvCurrent;
  460.     HWND    hwndLBDrive;
  461.     USHORT  i;
  462.     USHORT  us;
  463.  
  464.  
  465.     hwndLBDrive = WinWindowFromID(hwndDlg, IDL_DRIVE);
  466.  
  467.     if (DosQCurDisk(&drvCurrent,&bmlDrives) != 0) {
  468.     dbg( printf("DosQCurDisk failed!\n") );
  469.     return FALSE;
  470.     }
  471.     drvCurrent--;            // 0-based drive number
  472.  
  473.     ach[1] = ':';            // init drive string
  474.     ach[2] = '\0';
  475.  
  476.     WinEnableWindowUpdate(hwndLBDrive,FALSE); // turn off list box updates
  477.     WinSendMsg(hwndLBDrive, LM_DELETEALL, NULL, NULL); // delete old entries
  478.  
  479.     for (i=0; bmlDrives != NULL; i++) { // get all 1 bits!
  480.     if (bmlDrives & 1) {        // drive exists
  481.         ach[0] = (char)(i + 'a');
  482.         us = SHORT1FROMMR(WinSendMsg(hwndLBDrive, LM_INSERTITEM,
  483.             MPFROMSHORT(LIT_SORTASCENDING), MPFROMP(ach)));
  484.         WinSendMsg(hwndLBDrive, LM_SETITEMHANDLE,     // set drive number
  485.         MPFROMSHORT(us), MPFROMSHORT(i+1));
  486.         if (i == drvCurrent) {
  487.         itDrive = us;        // save index for selection
  488.         }
  489.     }
  490.     bmlDrives >>= 1;        // get next drive bit in bit 0
  491.     }
  492.  
  493.     WinEnableWindowUpdate(hwndLBDrive,TRUE); // repaint list box
  494.     return TRUE;
  495. }
  496.  
  497. BOOL    setCurDir(HWND hwndDlg)
  498. {
  499.     char    ach[CCHMAXPATH];
  500.     ULONG   bmlDrives;
  501.     USHORT  cch;
  502.     USHORT  drvCurrent;
  503.     HWND    hwndCurDir;
  504.     USHORT  rc;
  505.  
  506.     rc = DosQCurDisk(&drvCurrent,&bmlDrives);    // get current drive
  507.  
  508.     ach[0] = (char)((char)drvCurrent + 'a' - (char)1) ; // make drive letter
  509.     ach[1] = ':';            // rest of drive string
  510.     ach[2] = '\\';
  511.  
  512.     cch = CCHMAXPATH-3;                // room for drive string
  513.  
  514.     rc = DosQCurDir(drvCurrent, &ach[3], &cch);
  515.  
  516.     hwndCurDir = WinWindowFromID(hwndDlg, IDC_CURDIR);
  517.     WinSetWindowText(hwndCurDir,ach);    // set current drive/dir text
  518.     return TRUE;
  519. }
  520.  
  521.  
  522. BOOL    setDir(HWND hwndDlg)
  523. {
  524.     char        ach[20];
  525.     static USHORT   attr;
  526.     USHORT        cch;
  527.     HWND        hwndLBDir;
  528.  
  529.     hwndLBDir  = WinWindowFromID(hwndDlg, IDL_DIR);
  530.  
  531.     WinEnableWindowUpdate(hwndLBDir,FALSE); // turn off list box updates
  532.     WinSendMsg(hwndLBDir, LM_DELETEALL, NULL, NULL); // delete old entries
  533.  
  534.     // get all directories
  535.  
  536.     strcpy(ach,"*.*");
  537.     attr = 0x37;
  538.     for (cch = beginSearch(ach,&attr);
  539.      cch != 0;
  540.      cch = nextSearch(ach,&attr)) {
  541.     if (attr & 0x0010) {        // only get directories
  542.     //  if ((cch > 1) || (ach[0] != '.')) { // do all but "."
  543.         WinSendMsg(hwndLBDir, LM_INSERTITEM,
  544.             MPFROMSHORT(LIT_SORTASCENDING), MPFROMP(ach));
  545.     //  }
  546.     }
  547.     }
  548.     endSearch();
  549.  
  550.     WinEnableWindowUpdate(hwndLBDir,TRUE); // repaint list box
  551.     itDir = 1;
  552.     return TRUE;
  553. }
  554.  
  555.  
  556. BOOL    setFile(HWND hwndDlg)
  557. {
  558.     char        ach[20];
  559.     static USHORT   attr;
  560.     USHORT        cch;
  561.     HWND        hwndLBFile;
  562.     HWND        hwndText;
  563.  
  564.     hwndLBFile = WinWindowFromID(hwndDlg, IDL_FILE);
  565.     hwndText   = WinWindowFromID(hwndDlg, IDC_TEXT);
  566.  
  567.     WinEnableWindowUpdate(hwndLBFile,FALSE); // turn off list box updates
  568.     WinSendMsg(hwndLBFile, LM_DELETEALL, NULL, NULL); // delete old entries
  569.  
  570.     // get only files that match user's pattern
  571.  
  572.     WinQueryWindowText(hwndText,CCHMAXPATH,ach);
  573.     dbg( printf("file pattern = %s\n",ach) );
  574.     attr = 0x27;            // do all but directories
  575.     for (cch = beginSearch(ach,&attr);
  576.      cch != 0;
  577.      cch = nextSearch(ach,&attr) ) {
  578.     WinSendMsg(hwndLBFile, LM_INSERTITEM,
  579.         MPFROMSHORT(LIT_SORTASCENDING), MPFROMP(ach));
  580.  
  581.     }
  582.     endSearch();
  583.     WinEnableWindowUpdate(hwndLBFile,TRUE); // repaint list box
  584.     itFile = 1;                // default selection
  585.  
  586.     return TRUE;
  587. }
  588.  
  589.  
  590. USHORT    beginSearch(char *psz, USHORT *pattr)
  591. {
  592.     FILEFINDBUF        findbuf;
  593.     USHORT        cf;
  594.     USHORT        rc;
  595.  
  596.     cf = 1;                // only return 1 file
  597.     hdir = 0xFFFF;            // create a search handle
  598.  
  599.     rc = DosFindFirst(psz,        // file specification
  600.               &hdir,        // pointer to variable for handle
  601.               *pattr,        // search attribute (everything)
  602.               &findbuf,        // pointer to result buffer
  603.               sizeof(FILEFINDBUF), // length of result buffer
  604.               &cf,        // files to find/files found
  605.               0L);        // Must be zero
  606.  
  607.     if (cf != 0) {
  608.     psz[findbuf.cchName] = '\0';    // terminate name
  609.     strncpy(psz,findbuf.achName,findbuf.cchName); // copy name to caller
  610.     *pattr = findbuf.attrFile;
  611.     return findbuf.cchName;
  612.     }
  613.     return 0;
  614. }
  615.  
  616.  
  617. USHORT    nextSearch(char *psz, USHORT *pattr)
  618. {
  619.     FILEFINDBUF        findbuf;
  620.     USHORT        cf;
  621.     USHORT        rc;
  622.  
  623.     cf = 1;        // only return 1 file
  624.  
  625.     rc = DosFindNext(hdir,        // pointer to variable for handle
  626.              &findbuf,        // pointer to result buffer
  627.              sizeof(FILEFINDBUF), // length of result buffer
  628.              &cf);        // files to find/files found
  629.  
  630.     if (cf != 0) {
  631.     psz[findbuf.cchName] = '\0'; // terminate name
  632.     strncpy(psz,findbuf.achName,findbuf.cchName); // copy name to caller
  633.     *pattr = findbuf.attrFile;
  634.     return findbuf.cchName;
  635.     }
  636.     return 0;
  637. }
  638.  
  639.  
  640. void    endSearch(void)
  641. {
  642.     DosFindClose(hdir);
  643. }
  644.  
  645.  
  646. /***    trimBlanks - trim off leading and trailing blanks
  647. *
  648. *    ENTRY    psz - string to be trimmed
  649. */
  650.  
  651. char * trimBlanks(char *psz)
  652. {
  653.     char *pch;
  654.     char *pchDst;
  655.     char *pchRight;
  656.  
  657.     //    Find right-most non-blank character
  658.  
  659.     for (pch = psz+strlen(psz)-1;    // start at last character
  660.      (pch >= psz) && (*pch == ' '); // scan backward to non-blank
  661.     pch--)
  662.         ;
  663.  
  664.     *(pch+1) = '\0';            // trim trailing blanks
  665.  
  666.     pchRight = pch;
  667.  
  668.     // Find left-most non-blank character
  669.  
  670.     for (pch = psz;            // start at first character
  671.      (pch <= pchRight) && (*pch == ' '); // scan forward to non-blank
  672.      pch++)
  673.         ;
  674.  
  675.     // Shift string left to trim leading blanks
  676.  
  677.     if (pch > psz) {            // leading blanks to trim
  678.     pchRight++;            // grab traling null
  679.     pchDst = psz;            // do not destroy psz
  680.     while (pchDst <= pchRight)
  681.         *pchDst++ = *pch++;        // shift string left
  682.     }
  683.     return psz;
  684. }
  685.