/* The distribution image CDFileOpen class only permits single file selection.
We need a slight variation which alters the flags and understands the return
of multiple file names. */!!

inherit(CDFileOpen, #CDMultiFileOpen, #(fileColl	/* OrderedCollection */), 
2, nil)!!

setClassVars(CDMultiFileOpen, #($BufSize	/* return class var */))!!

now(class(CDMultiFileOpen))!!

now(CDMultiFileOpen)!!

/* Set up the OPENFILENAME structure as before, but add an additional flag. */
Def init(self)
{ init(self:ancestor);
  cdFlags := cdFlags bitOr 0x200; /* OFN_ALLOWMULTISELECT */
}!!

/* Execute this dialog with additions for multiple file returns. */
Def execute(self | initDir, fnBuff, fnTitle, path, fileTitle, filterStr, 
dlgTitle, rtn, cb, pathEnd, aStrm, fn)
{ ofnStruct[#hwndOwner] := handle(parent);
  if (filterStr := getFilterString(self))
  then ofnStruct[#lpstrFilter] := lP(filterStr);
  endif;
  ofnStruct[#nFilterIndex] := selection cor 1;
  if initialFileName
  then fnBuff := initialFileName + new(String, ($BufSize + 1) - 
    size(initialFileName));
  else fnBuff := new(String, ($BufSize + 1));
  endif;
  ofnStruct[#lpstrFile] := lP(fnBuff);
  ofnStruct[#nMaxFile] := $BufSize;
  fnTitle := new(String, 257);
  ofnStruct[#lpstrFileTitle] := lP(fnTitle);
  ofnStruct[#nMaxFileTitle] := 256;
  if initialDir
  then initDir := asciiz(initialDir);
   ofnStruct[#lpstrInitialDir] := lP(initDir);
  endif;
  if size(title) > 0
  then dlgTitle := asciiz(title);
    ofnStruct[#lpstrTitle] := lP(dlgTitle);
  endif;
  if (cb := cbproc(self))
  then ofnStruct[#Flags] := cdFlags bitOr 0x20; /* OFN_ENABLEHOOK */
    ofnStruct[#lpfnHook] := lock(cb);
  else ofnStruct[#Flags] := cdFlags;
  endif;
  loadLib(class(self));
  rtn := callLibFunction(self);
  fileTitle := getText(fnTitle);
  path := getText(fnBuff);
  initDir cand freeHandle(initDir);
  cb cand free(cb);
  freeHandle(fnBuff);
  freeHandle(fnTitle);
  dlgTitle cand freeHandle(dlgTitle);
  filterStr cand freeHandle(filterStr);
  if rtn cand size(path) > 0
  then pathEnd := ofnStruct[#nFileOffset];
    initialDir := subString(path, 0,
      ((pathEnd >= 3) cand pathEnd - 1) cor pathEnd);
    fileColl := new(OrderedCollection, 12);
    aStrm := streamOver(subString(path, pathEnd, size(path)));
    loop
    while not(atEnd(aStrm))
    begin fn := asUpperCase(word(aStrm, ' '));
      add(fileColl, initialDir + "\" + fn);
    endLoop;
    ^fileColl;
  else
    if callLibErrFunction(self) == 0x3003 /* FNERR_BUFFERTOOSMALL */
    then ^askAboutBufSize(self);
    endif;
  endif;
  ^nil;
}!!

/* Nothing in the distribution image checks on error functions. */
Def callLibErrFunction(self)
{ ^pcall($CDLibrary[#CommDlgExtendedError]);
}!!

/* The buffer size was found to be too small on the last execute.  Ask
  the user if a larger buffer is desired. */
Def askAboutBufSize(self | ans)
{ if new(ErrorBox, ThePort,
    "The files you selected were too numerous to fit in the space " +
    "alloted.  Do you want to increase the space and try again?",
    "File Open Common Dialog Problem", MB_OKCANCEL) == IDOK
  then
    $BufSize := $BufSize + 1024;
    ^execute(self);
  endif;
}!!

/* CDMultiFileOpen Class Initialization Code */
$BufSize := 2048;



