home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
- * FINDDLG.C - Find file dialog box routines. *
- * *
- * Notes - *
- * This following functions will have to be modified in order to *
- * support OS/2 v1.2 long file names : *
- * *
- * Directory() *
- * *
- * Modifications - *
- * 21-Sep-1989 : Moved find file functions into separate file. *
- * Made default button context sensitive. *
- * Prevented buttons from retaining focus after being *
- * clicked. *
- * Fixed bug that caused duplicate error messages to *
- * be displayed when enter key used to select list *
- * box items. *
- * 11-Oct-1989 : Changed to DLL version of ErrMessageBox function. *
- * *
- * (c)Copyright 1989 Rick Yoder *
- ****************************************************************************/
-
- #define INCL_WIN
- #define INCL_DOS
- #define INCL_DOSERRORS
- #include <os2.h>
-
- #include <string.h>
- #include <errmsg.h>
-
- #include "filedlg.h"
- #include "dialog.h"
- #include "tools.h"
- #include "static.h"
- #include "opendata.h" // definition of structure holding dialog box
- // static data (DATA & PDATA types).
-
- /****************************************************************************
- * Procedure declarations *
- ****************************************************************************/
- static MRESULT near FindInit( HWND hwnd,MPARAM mp2 );
- static MRESULT near PatternEditCtrl( HWND hwnd,USHORT msg,
- MPARAM mp1,MPARAM mp2 );
- static MRESULT near FindListbox( HWND hwnd,USHORT msg,
- MPARAM mp1,MPARAM mp2 );
- static MRESULT near SearchButton( HWND hwnd );
- static MRESULT near OpenButton( HWND hwnd );
- static void near ResetDefaultButton( HWND hwnd );
- static BOOL near Directory( HWND hDlg,PSZ pszFileSpec,PDATA pData );
- static USHORT near OpenFile( HWND hDlg,PDATA pData );
- /****************************************************************************/
-
-
- /****************************************************************************
- * _FindDlgProc() *
- ****************************************************************************/
- MRESULT CALLBACK _FindDlgProc( HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2 )
- {
- PDATA pData;
-
- switch ( msg ) {
- case WM_INITDLG:
- return FindInit( hwnd,mp2 );
-
- case WM_CHAR:
- if ( CHARMSG(&msg)->fs & KC_ALT )
- switch ( CHARMSG(&msg)->chr ) {
- case 'p':
- case 'P': WinSetFocus(HWND_DESKTOP,WinWindowFromID(hwnd,FIND_PATTERN));
- return 0;
-
- case 'd':
- case 'D': WinSetFocus(HWND_DESKTOP,WinWindowFromID(hwnd,FIND_DRIVES));
- return 0;
-
- case 'f':
- case 'F': WinSetFocus(HWND_DESKTOP,WinWindowFromID(hwnd,FIND_FLIST));
- return 0;
- }
- break;
-
- case WM_CONTROL:
- switch ( SHORT1FROMMP(mp1) ) {
- case FIND_PATTERN:
- return PatternEditCtrl( hwnd,msg,mp1,mp2 );
-
- case FIND_FLIST:
- return FindListbox( hwnd,msg,mp1,mp2 );
-
- case FIND_DRIVES:
- if ( SHORT2FROMMP(mp1) == LN_SETFOCUS )
- ResetDefaultButton( hwnd );
- break;
- }
- break;
-
- case WM_COMMAND:
- pData = (PDATA)WinQueryWindowULong( hwnd,QWL_USER );
- switch (COMMANDMSG(&msg)->cmd) {
- case FIND_SEARCH:
- return SearchButton( hwnd );
-
- case FIND_OPEN:
- return OpenButton( hwnd );
-
- case DID_CANCEL:
- case FIND_CANCEL:
- WinDismissDlg( hwnd,FDLG_CANCEL );
- return 0;
- }
- break;
- }
-
- return WinDefDlgProc( hwnd,msg,mp1,mp2 );
- }
- /****************************************************************************/
-
-
- /****************************************************************************
- * FindInit() -- Process WM_INITDLG message sent to find file dialog box. *
- ****************************************************************************/
- static MRESULT near FindInit( HWND hwnd,MPARAM mp2 )
- {
- PDATA pData;
- USHORT usResult,usCount,usIndex,usDriveNum;
- ULONG ulMap;
-
- pData = PVOIDFROMMP( mp2 );
- WinSetWindowULong( hwnd,QWL_USER,(ULONG)pData );
- WinSendDlgItemMsg( hwnd,FIND_FLIST,LM_DELETEALL,NULL,NULL );
- WinSendDlgItemMsg( hwnd,FIND_DRIVES,LM_DELETEALL,NULL,NULL );
- WinSetDlgItemText( hwnd,FIND_PATTERN,pData->pszShowSpec );
- WinEnableWindow( WinWindowFromID(hwnd,FIND_OPEN),FALSE );
- WinSetWindowUShort( WinWindowFromID(hwnd,FIND_OPEN),QWS_USER,TRUE );
- pData->usSelectFind = 0;
-
- /* Fill in disk drive list box */
- if ( usResult = DosQCurDisk(&usDriveNum,&ulMap) )
- {
- ErrMessageBox( hwnd,szSearchTitle,usResult,NULL,0 );
- WinDismissDlg( hwnd,FDLG_CANCEL );
- return 0L;
- }
- pData->pszScratch[1] = ':';
- pData->pszScratch[2] = '\0';
- for ( usCount = 0; usCount < 26; usCount++ )
- if ( ulMap & 1L << usCount )
- {
- pData->pszScratch[0] = (CHAR)usCount + 'A';
-
- usIndex = SHORT1FROMMR(
- WinSendDlgItemMsg( hwnd,FIND_DRIVES,
- LM_INSERTITEM,
- MPFROMSHORT(LIT_END),
- MPFROMP(pData->pszScratch) )
- );
-
- if ( usCount == usDriveNum-1 )
- {
- WinSendDlgItemMsg( hwnd,FIND_DRIVES,
- LM_SELECTITEM,
- MPFROMSHORT(usIndex),
- MPFROMSHORT(TRUE) );
- }
- }
-
- return 0L;
- }
- /****************************************************************************/
-
-
- /****************************************************************************
- * PatternEditCtrl() -- Handles messages sent by FIND_PATTERN edit control *
- * to find file dialog box. *
- ****************************************************************************/
- static MRESULT near PatternEditCtrl( HWND hwnd,USHORT msg,
- MPARAM mp1,MPARAM mp2 )
- {
- PDATA pData;
- USHORT usLen;
-
- pData = (PDATA)WinQueryWindowULong( hwnd,QWL_USER );
- switch ( SHORT2FROMMP(mp1) ) {
- case EN_CHANGE:
- WinEnableWindow( WinWindowFromID(hwnd,FIND_PATTERN),
- WinQueryDlgItemTextLength(hwnd,FIND_PATTERN)
- );
- break;
-
- case EN_SETFOCUS:
- usLen = WinQueryDlgItemTextLength( hwnd,FIND_PATTERN );
- WinSendDlgItemMsg( hwnd,FIND_PATTERN,EM_SETSEL,
- MPFROM2SHORT(0,usLen),0L );
- ResetDefaultButton( hwnd );
- break;
- }
-
- return WinDefDlgProc( hwnd,msg,mp1,mp2 );
- }
- /****************************************************************************/
-
-
- /****************************************************************************
- * FindListbox() -- Handle messages sent by FIND_FLIST list box to the *
- * find file dialog box. *
- ****************************************************************************/
- static MRESULT near FindListbox( HWND hwnd,USHORT msg,
- MPARAM mp1,MPARAM mp2 )
- {
- PDATA pData;
- USHORT usResult;
-
- pData = (PDATA)WinQueryWindowULong( hwnd,QWL_USER );
- switch ( SHORT2FROMMP(mp1) ) {
- case LN_SELECT:
- usResult = (USHORT)WinSendDlgItemMsg( hwnd,FIND_FLIST,
- LM_QUERYSELECTION,
- 0L,0L );
- if ( usResult != LIT_NONE )
- pData->usSelectFind = usResult;
- WinSendDlgItemMsg( hwnd,FIND_FLIST,
- LM_QUERYITEMTEXT,
- MPFROM2SHORT(pData->usSelectFind,pData->usMaxPathLen+2),
- MPFROMP(pData->pszScratch) );
- WinEnableWindow( WinWindowFromID(hwnd,FIND_OPEN),
- (*pData->pszScratch == ' ') );
- return 0L;
-
- case LN_ENTER: /* equivalent to open button clicked */
- WinSendDlgItemMsg( hwnd,FIND_OPEN,BM_CLICK,0L,0L );
- return 0L;
-
- case LN_SETFOCUS:
- usResult = (USHORT)WinSendDlgItemMsg( hwnd,FIND_FLIST,
- LM_QUERYTOPINDEX,
- 0L,0L );
- if ( usResult != LIT_NONE )
- {
- if ( pData->usSelectFind < usResult )
- pData->usSelectFind = usResult;
- else if (pData->usSelectFind > usResult+11)
- pData->usSelectFind = usResult+11;
-
- WinSendDlgItemMsg( hwnd,FIND_FLIST,
- LM_SELECTITEM,
- MPFROMSHORT(pData->usSelectFind),
- MPFROMSHORT(TRUE) );
- }
-
- /* Make OPEN button the current default button */
- WinSendDlgItemMsg( hwnd,FIND_SEARCH,BM_SETDEFAULT,
- MPFROMSHORT(FALSE),0L );
- WinSendDlgItemMsg( hwnd,FIND_OPEN,BM_SETDEFAULT,
- MPFROMSHORT(TRUE),0L );
- WinSendDlgItemMsg( hwnd,FIND_CANCEL,BM_SETDEFAULT,
- MPFROMSHORT(FALSE),0L );
- return 0L;
- }
-
- return WinDefDlgProc( hwnd,msg,mp1,mp2 );
- }
- /****************************************************************************/
-
-
- /****************************************************************************
- * SearchButton() -- Executed whenever SEARCH button in find file dialog *
- * box is clicked. *
- ****************************************************************************/
- static MRESULT near SearchButton( HWND hwnd )
- {
- PDATA pData;
- HPOINTER hptrOld;
- SHORT iItem;
- USHORT rc;
- SEL sel;
- PSZ pszFileSpec;
- BOOL fContinue;
-
- pData = (PDATA)WinQueryWindowULong( hwnd,QWL_USER );
-
- /* Allocate memory for file search spec. */
- if ( rc = DosAllocSeg(pData->usMaxPathLen,&sel,SEG_NONSHARED) )
- {
- ErrMessageBox( hwnd,szSearchTitle,rc,NULL,0 );
- return 0L;
- }
- pszFileSpec = MAKEP( sel,0 );
-
- /* Disable open button & change cursor to hourglass */
- WinEnableWindow( WinWindowFromID(hwnd,FIND_OPEN),FALSE );
- hptrOld = WinQueryPointer( HWND_DESKTOP );
- WinSetPointer( HWND_DESKTOP,
- WinQuerySysPointer(HWND_DESKTOP,SPTR_WAIT,FALSE) );
-
- /* Retrieve current search pattern */
- WinQueryDlgItemText( hwnd,FIND_PATTERN,
- pData->usMaxPathLen-3,
- pszFileSpec+3 );
-
- /* Erase current file list */
- WinSendDlgItemMsg( hwnd,FIND_FLIST,LM_DELETEALL,0L,0L );
- pData->usSelectFind = 0;
-
- /* Perform search on each selected drive */
- iItem = (SHORT)WinSendDlgItemMsg( hwnd,FIND_DRIVES,
- LM_QUERYSELECTION,
- MPFROMSHORT(LIT_FIRST),0L );
- fContinue = TRUE;
- while ( (iItem != LIT_NONE) && fContinue )
- {
- WinSendDlgItemMsg( hwnd,FIND_DRIVES,
- LM_QUERYITEMTEXT,
- MPFROM2SHORT(iItem,3),
- MPFROMP(pszFileSpec) );
- *(pszFileSpec+2) = '\\';
- fContinue = Directory( hwnd,pszFileSpec,pData );
- iItem = (SHORT)WinSendDlgItemMsg( hwnd,FIND_DRIVES,
- LM_QUERYSELECTION,
- MPFROMSHORT(iItem),0L );
- }
-
- /* Free file spec. memory block */
- DosFreeSeg( sel );
-
- /* Restore cursor to previous value */
- WinSetPointer( HWND_DESKTOP,hptrOld );
-
- /* Display message if no files were found */
- /* and set focus to appropriate control */
- if ( fContinue )
- {
- if ( !WinSendDlgItemMsg(hwnd,FIND_FLIST,LM_QUERYITEMCOUNT,0L,0L) )
- {
- ErrMessageBox( hwnd,szSearchTitle,NO_FILES_FOUND,
- appMsgList,appMsgCount );
- WinSetFocus( HWND_DESKTOP,WinWindowFromID(hwnd,FIND_PATTERN) );
- }
- else
- WinSetFocus( HWND_DESKTOP,WinWindowFromID(hwnd,FIND_FLIST) );
- }
- else
- WinSetFocus( HWND_DESKTOP,WinWindowFromID(hwnd,FIND_PATTERN) );
-
- return 0L;
- }
- /****************************************************************************/
-
-
- /****************************************************************************
- * OpenButton() -- Executed whenever OPEN button in find file dialog *
- * box is clicked. *
- ****************************************************************************/
- static MRESULT near OpenButton( HWND hwnd )
- {
- PDATA pData;
- SHORT iItem;
- HWND hwndButton;
-
- /* Skip procedure when button does not have the focus. *
- * This prevents the duplicate error message problem that *
- * occurs when the enter key is used to select a list box *
- * item. */
- hwndButton = WinWindowFromID( hwnd,FIND_OPEN );
- if ( hwndButton != WinQueryFocus(HWND_DESKTOP,FALSE) ) return 0L;
-
- pData = (PDATA)WinQueryWindowULong( hwnd,QWL_USER );
- iItem = pData->usSelectFind;
-
- if ( WinSendDlgItemMsg( hwnd,FIND_FLIST,
- LM_QUERYITEMTEXT,
- MPFROM2SHORT(iItem,pData->usMaxPathLen),
- MPFROMP(pData->pszFile) )
- )
- {
- /* Get path name */
- do {
- WinSendDlgItemMsg( hwnd,FIND_FLIST,
- LM_QUERYITEMTEXT,
- MPFROM2SHORT(--iItem,pData->usMaxPathLen),
- MPFROMP(pData->pszScratch) );
- } while ( *pData->pszScratch == ' ' );
- strcat( pData->pszScratch,szSlash );
- strcat( pData->pszScratch,pData->pszFile+3 );
-
- if ( !OpenFile(hwnd,pData) )
- WinDismissDlg( hwnd,FDLG_OK );
- else
- WinSetFocus( HWND_DESKTOP,WinWindowFromID(hwnd,FIND_FLIST) );
- }
-
- return 0L;
- }
- /****************************************************************************/
-
-
- /****************************************************************************
- * ResetDefaultButton() - This function makes the SEARCH button the current *
- * default button. *
- ****************************************************************************/
- static void near ResetDefaultButton( HWND hwnd )
- {
- ULONG ulStyle;
-
- ulStyle = WinQueryWindowULong( WinWindowFromID(hwnd,FIND_SEARCH),
- QWL_STYLE );
- if ( !(ulStyle & BS_DEFAULT) )
- {
- WinSendDlgItemMsg( hwnd,FIND_SEARCH,BM_SETDEFAULT,
- MPFROMSHORT(TRUE),0L );
- ulStyle = WinQueryWindowULong( WinWindowFromID(hwnd,FIND_OPEN),
- QWL_STYLE );
- WinSendDlgItemMsg( hwnd,
- (ulStyle & BS_DEFAULT) ? FIND_OPEN : FIND_CANCEL,
- BM_SETDEFAULT,
- MPFROMSHORT(FALSE),0L );
- }
-
- return;
- }
- /****************************************************************************/
-
-
- /****************************************************************************
- * Directory() - This function is called recursively, successively *
- * searching each subdirectory for files that match *
- * the search criteria until the entire drive has been *
- * traversed. *
- * *
- * This function returns a value of FALSE if an error *
- * occurs, otherwise it returns a value of TRUE. *
- * *
- * This function uses the scratch data area. *
- ****************************************************************************/
- static BOOL near Directory( HWND hwnd,PSZ pszFileSpec,PDATA pData )
- {
- SEL sel;
- PSZ pszSpec;
- PSZ pszSub;
- PFILEFINDBUF pFindbuf;
- HDIR hdir = HDIR_CREATE;
- USHORT usSearchCount = 1;
- USHORT usResult,rc;
-
- /*********************************************************
- * Allocate memory for file find buffer *
- * DosAllocSeg is used to keep from overflowing the *
- * stack when a disk with a large directory tree is *
- * searched. *
- *********************************************************/
- if ( rc = DosAllocSeg(sizeof(FILEFINDBUF),&sel,SEG_NONSHARED) )
- {
- ErrMessageBox( hwnd,szSearchTitle,rc,NULL,0 );
- return FALSE;
- }
- pFindbuf = MAKEP( sel,0 );
-
- /* Split input file spec into directory name and file name */
- strcpy( pData->pszScratch,pszFileSpec );
- pszSpec = strrchr( pszFileSpec,'\\' );
- *pszSpec++ = '\0';
-
- /* Find first file that matches criteria */
- usResult = DosFindFirst( pData->pszScratch,&hdir,pData->usShowAttr,
- pFindbuf,sizeof(FILEFINDBUF),
- &usSearchCount,0L );
-
- /* Add directory name to list box if a file was found */
- if ( !usResult )
- {
- rc = (SHORT)WinSendDlgItemMsg( hwnd,FIND_FLIST,
- LM_INSERTITEM,
- MPFROMSHORT(LIT_END),
- MPFROMP(pszFileSpec) );
- if ( rc == LIT_MEMERROR || rc == LIT_ERROR )
- {
- ErrMessageBox( hwnd,szSearchTitle,LISTBOX_FULL,
- appMsgList,appMsgCount );
- DosFreeSeg( SELECTOROF(pFindbuf) );
- return FALSE;
- }
- }
-
- /* Add names of found files to list box */
- pData->pszScratch[0] = ' ';
- pData->pszScratch[1] = ' ';
- pData->pszScratch[2] = ' ';
- while ( !usResult )
- {
- strcpy( pData->pszScratch+3,pFindbuf->achName );
- rc = (SHORT)WinSendDlgItemMsg( hwnd,FIND_FLIST,
- LM_INSERTITEM,
- MPFROMSHORT(LIT_END),
- MPFROMP(pData->pszScratch) );
- if ( rc == LIT_MEMERROR || rc == LIT_ERROR )
- {
- ErrMessageBox( hwnd,szSearchTitle,LISTBOX_FULL,
- appMsgList,appMsgCount );
- DosFreeSeg( SELECTOROF(pFindbuf) );
- return FALSE;
- }
- usResult = DosFindNext( hdir,pFindbuf,sizeof(FILEFINDBUF),
- &usSearchCount );
- }
- if ( usResult != ERROR_NO_MORE_SEARCH_HANDLES ) DosFindClose(hdir);
- if ( usResult && usResult != ERROR_NO_MORE_FILES )
- {
- ErrMessageBox( hwnd,szSearchTitle,usResult,NULL,0 );
- DosFreeSeg( SELECTOROF(pFindbuf) );
- return FALSE;
- }
-
- /* Allocate memory for subdirectory search spec. */
- if ( rc = DosAllocSeg(pData->usMaxPathLen,&sel,SEG_NONSHARED) )
- {
- ErrMessageBox( hwnd,szSearchTitle,rc,NULL,0 );
- DosFreeSeg( SELECTOROF(pFindbuf) );
- return FALSE;
- }
- pszSub = MAKEP( sel,0 );
-
- /* Search subdirectories for matching files */
- hdir = HDIR_CREATE;
- usSearchCount = 1;
- strcpy( pData->pszScratch,pszFileSpec );
- strcat( pData->pszScratch,szSlash );
- strcat( pData->pszScratch,szStarDotStar );
- usResult = DosFindFirst( pData->pszScratch,&hdir,FILE_DIRECTORY,
- pFindbuf,sizeof(FILEFINDBUF),
- &usSearchCount,0L );
- while ( !usResult )
- {
- if ( (pFindbuf->attrFile & FILE_DIRECTORY)
- && (pFindbuf->achName[0] != '.') )
- {
- strcpy( pszSub,pszFileSpec );
- strcat( pszSub,szSlash );
- strcat( pszSub,pFindbuf->achName );
- strcat( pszSub,szSlash );
- strcat( pszSub,pszSpec );
- if ( !Directory(hwnd,pszSub,pData) )
- {
- DosFreeSeg( SELECTOROF(pFindbuf) );
- DosFreeSeg( SELECTOROF(pszSub) );
- return FALSE;
- }
- }
- usResult = DosFindNext( hdir,pFindbuf,sizeof(FILEFINDBUF),
- &usSearchCount );
- }
- if ( usResult != ERROR_NO_MORE_SEARCH_HANDLES ) DosFindClose(hdir);
- if ( usResult && usResult != ERROR_NO_MORE_FILES )
- {
- ErrMessageBox( hwnd,szSearchTitle,usResult,NULL,0 );
- DosFreeSeg( SELECTOROF(pFindbuf) );
- DosFreeSeg( SELECTOROF(pszSub) );
- return FALSE;
- }
-
- /* Done. Return to caller */
- DosFreeSeg( SELECTOROF(pFindbuf) );
- DosFreeSeg( SELECTOROF(pszSub) );
- return TRUE;
- }
- /****************************************************************************/
-
-
- /****************************************************************************
- * OpenFile() - This function attempts to open the file specified *
- * in the scratch data area. *
- * *
- * This function returns a non-zero value if an error occured *
- * or the input string was a search specification. *
- ****************************************************************************/
- static USHORT near OpenFile( HWND hDlg,PDATA pData )
- {
- USHORT usResult;
-
- usResult = ParseFileName( pData->pszScratch,
- pData->pszFile,
- pData->pszShowSpec );
- if ( usResult )
- {
- ErrMessageBox( hDlg,pData->pszTitle,usResult,NULL,0 );
- return 1;
- }
-
- if ( NULL != strpbrk(pData->pszFile,szWildCardChars) )
- return 1;
- else
- {
- usResult = DosOpen( pData->pszFile,
- pData->phf,
- pData->pusAction,
- pData->ulFileSize,
- pData->usAttribute,
- pData->fsOpenFlags,
- pData->fsOpenMode,
- pData->ulReserved );
- if ( usResult )
- {
- ErrMessageBox( hDlg,pData->pszTitle,usResult,NULL,0 );
- return 1;
- }
- else
- return 0;
- }
- }
- /****************************************************************************/
-