home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / FILEDLG.LIB < prev    next >
Text File  |  1994-03-08  |  6KB  |  132 lines

  1. // FileDlg.lib - Cmm code wrapper for a simple implementation of the
  2. // ver.1         WinFileDlg() function found in PM.You may #include
  3. //               this file in your CMM source to have access to this
  4. //               version of WinFileDlg().
  5.  
  6. // FileDialog([MatchingFileSpec[,Title[,Flags[,OKButtonText]]]])
  7.    // MatchingFileSpec: string file specification, including wildcard in the
  8.    //                   file name.  For example "C:\\DATA\\*.TXT" or
  9.    //                   "*.INI".  If this is not supplied or if it is
  10.    //                   NULL then * will be the default.
  11.    // Title: String to be the title of the open dialog box.
  12.    // Flags: Or of flags describing dialog box type and behavior.  If none
  13.    //        or 0 specified then defaults to: FDS_OPEN_DIALOG | FDS_CENTER
  14.    //        Note that FDS_OPEN_DIALOG or FDS_SAVEAS_DIALOG must be set, but not both.
  15.          #define FDS_CENTER           0x001   // Center within owner wnd
  16.          #define FDS_CUSTOM           0x002   // Use custom user template: NOT USED HERE
  17.          #define FDS_FILTERUNION      0x004   // Use union of filters: NOT USED HERE
  18.          #define FDS_HELPBUTTON       0x008   // Display Help button
  19.          #define FDS_APPLYBUTTON      0x010   // Display Apply button
  20.          #define FDS_PRELOAD_VOLINFO  0x020   // Preload volume info for each drive for current default directory
  21.          #define FDS_MODELESS         0x040   // Make dialog modeless: DO NOT USE
  22.          #define FDS_INCLUDE_EAS      0x080   // Query extended attributes for list box
  23.          #define FDS_OPEN_DIALOG      0x100   // Select Open dialog 
  24.          #define FDS_SAVEAS_DIALOG    0x200   // Select SaveAs dialog
  25.          #define FDS_MULTIPLESEL      0x400   // Enable multiple selection
  26.          #define FDS_ENABLEFILELB     0x800   // Enable File list box if this is FDS_SAVEAS_DIALOG
  27.    // OKButtonText: String to put in the OK button.  If not supplied or NULL
  28.    //               then defaults to OK.
  29.    // Return: Return a string that is the full file specification of the
  30.    //         file selected, or NULL if no file was selected.  If
  31.    //         FDS_MULTIPLESEL was specified, then this function will return
  32.    //         NULL if no file was selected, or an array of names selected.
  33. FileDialog(MatchingFileSpec,Title,Flags,OKButtonText)
  34. {
  35.    // There were a lot of optional inputs, and so set defaults
  36.    _ParmCount = va_arg();
  37.    _MatchingFileSpec = ( _ParmCount < 1  ||  NULL == MatchingFileSpec )
  38.                      ? "*"
  39.                      : MatchingFileSpec ;
  40.    _Title = ( _ParmCount < 2 ) ? NULL : Title;
  41.    _Flags = ( _ParmCount < 3  ||  0 == Flags ) ? FDS_OPEN_DIALOG | FDS_CENTER : Flags;
  42.    _OKButtonText = ( _ParmCount < 4 ) ? NULL : OKButtonText ;
  43.  
  44.    // The file dialog structure is very large, but most of its fields are
  45.    // not used.  Set all to zero except for the few we need.
  46.    #define  FILEDLG_SIZE   328
  47.    BLObSize(FileDlg,FILEDLG_SIZE);
  48.    memset(FileDlg,0,FILEDLG_SIZE);
  49.    BLObPut(FileDlg,0,FILEDLG_SIZE,UWORD32);
  50.    // Set users flags
  51.    #define  FD_FLAG_OFFSET 4
  52.    BLObPut(FileDlg,FD_FLAG_OFFSET,_Flags,UWORD32);
  53.    // set title and button
  54.    #define  FD_TITLE_OFFSET   20
  55.    #define  FD_BUTTON_OFFSET  24
  56.    if ( NULL != _Title ) {
  57.       // must give PM pointer to this title
  58.       PMpoke(_PMtitle = PMalloc(1+strlen(_Title)),_Title,1+strlen(_Title));
  59.       BLObPut(FileDlg,FD_TITLE_OFFSET,_PMtitle,UWORD32);
  60.    }
  61.    if ( NULL != _OKButtonText ) {
  62.       // must give PM pointer to this button text
  63.       PMpoke(_PMbutton = PMalloc(1+strlen(_OKButtonText)),_OKButtonText,1+strlen(_OKButtonText));
  64.       BLObPut(FileDlg,FD_BUTTON_OFFSET,_PMbutton,UWORD32);
  65.    }
  66.    // set initial search spec name
  67.    #define  FD_FULLNAME_OFFSET   52
  68.    #define  FD_FULLNAME_SIZE     260
  69.    BLObPut(FileDlg,FD_FULLNAME_OFFSET,_MatchingFileSpec,1+strlen(_MatchingFileSpec));
  70.  
  71.    // Call the dialog function
  72.    #define ORD_WINFILEDLG  4
  73.    #define HWND_DESKTOP    1
  74.    #define FD_FILECOUNT_OFFSET 316
  75.    #define FD_RETURNCODE_OFFSET 12
  76.    #define DID_OK    1
  77.    _FileCount = ( PMDynamicLink("PMCTLS",ORD_WINFILEDLG,BIT32,CDECL,
  78.                                 HWND_DESKTOP,Info().WinHandle,FileDlg)
  79.                && DID_OK == BLObGet(FileDlg,FD_RETURNCODE_OFFSET,UWORD32) )
  80.               ? BLObGet(FileDlg,FD_FILECOUNT_OFFSET,UWORD32)  :  0 ;
  81.  
  82.    // don't need title and button pointers anymore
  83.    if ( NULL != _Title ) PMfree(_PMtitle);
  84.    if ( NULL != _OKButtonText ) PMfree(_PMbutton);
  85.  
  86.    if ( 0 == _FileCount ) {
  87.       _ret = NULL;  // No files found
  88.    } else if ( _Flags & FDS_MULTIPLESEL ) {
  89.       #define FD_FILELIST_OFFSET 312
  90.       _flist = BLObGet(FileDlg,FD_FILELIST_OFFSET,UWORD32);
  91.       if ( NULL == _flist ) {
  92.          _len = strlen(BLObGet(FileDlg,FD_FULLNAME_OFFSET,FD_FULLNAME_SIZE));
  93.          _ret[0] = BLObGet( FileDlg, FD_FULLNAME_OFFSET, 1 + _len );
  94.       } else {
  95.          for ( _i = 0; _i < _FileCount; _i++ ) {
  96.             _name = PMpeek(_flist + _i*4,UWORD32);
  97.             _len = strlen(PMpeek(_name,FD_FULLNAME_OFFSET));
  98.             _ret[_i] = PMpeek( _name, 1 + _len );
  99.          }
  100.          // finally, must free list of files
  101.          #define ORD_WINFREEFILEDLGLIST   6
  102.          PMDynamicLink("PMCTLS",ORD_WINFREEFILEDLGLIST,BIT32,CDECL,_flist);
  103.       }
  104.    } else {
  105.       // single response in FULLNAME
  106.       _len = strlen(BLObGet(FileDlg,FD_FULLNAME_OFFSET,FD_FULLNAME_SIZE));
  107.       _ret = BLObGet( FileDlg, FD_FULLNAME_OFFSET, 1 + _len );
  108.    }
  109.    return _ret;
  110. }
  111.  
  112.  
  113. /************** Routines used by above code ********************/
  114.  
  115. PMalloc(size)
  116. {
  117.    #define ORD_DOS32ALLOCMEM  299
  118.    #define PAG_COMMIT   0x10
  119.    #define PAG_READ     0x1
  120.    #define PAG_WRITE    0x2
  121.    _FileCount = PMDynamicLink("DOSCALLS",ORD_DOS32ALLOCMEM,BIT32,CDECL,
  122.                               _PMptr,size,PAG_COMMIT | PAG_READ | PAG_WRITE);
  123.    return(_PMptr);
  124. }
  125.  
  126. PMfree(ptr)
  127. {
  128.    #define ORD_DOS32FREEMEM   304
  129.    PMDynamicLink("DOSCALLS",ORD_DOS32FREEMEM,BIT32,CDECL,ptr);
  130. }
  131.  
  132.