home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OWL1.PAK / FILEDIAL.CPP < prev    next >
Text File  |  1995-08-29  |  7KB  |  236 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. /* --------------------------------------------------------
  4.   FILEDIAL.CPP
  5.   Defines type TFileDialog.  This defines the basic
  6.   behavior of all file dialogs.
  7.   -------------------------------------------------------- */
  8.  
  9. #include "filedial.h"
  10. #include <string.h>
  11. #include <dir.h>
  12.  
  13. LPSTR GetFileName(LPSTR FilePath)
  14. {
  15.   LPSTR P;
  16.  
  17.   P = _fstrrchr(FilePath, '\\');
  18.   if ( !P )
  19.     P = _fstrrchr(FilePath, ':');
  20.   if ( !P )
  21.     return FilePath;
  22.   else
  23.     return P + 1;
  24. }
  25.  
  26. LPSTR GetExtension(LPSTR FilePath)
  27. {
  28.   LPSTR P;
  29.  
  30.   P = _fstrchr( GetFileName(FilePath), '.');
  31.   if ( !P )
  32.     return _fstrchr(FilePath, '\0');
  33.   else
  34.     return P;
  35. }
  36.  
  37. BOOL HasWildCards(LPSTR FilePath)
  38. {
  39.   return _fstrchr(FilePath, '\*') || _fstrchr(FilePath, '\?');
  40. }
  41.  
  42. /* Constructor for a TFileDialog. Uses the contents of AFilePath as
  43.    the initial mask (for example, "*.*") for the files to be listed
  44.    in the file list box. AFilePath is also used as a buffer in which
  45.    the name of the file retrieved from the user is returned.  The
  46.    resource identifier is set to SD_FILESAVE or SD_FILEOPEN, which
  47.    correspond to dialog resources in the OWL-supplied filedial.dlg
  48.    file.  If the resource identifier is reset a matching resource
  49.    with similar controls must be supplied. */
  50. TFileDialog::TFileDialog(PTWindowsObject AParent,
  51.                          int ResourceId, LPSTR AFilePath, PTModule AModule)
  52.                : TDialog(AParent, ResourceId, AModule)
  53. {
  54.   FilePath = AFilePath;
  55.   Extension[0] = '\0';
  56. }
  57.  
  58. /* Returns TRUE if a valid file name has been retrieved from the user to
  59.    indicate that the file dialog can then be closed.  Retrieves the text
  60.    of the edit control, and updates PathName.  Calls UpdateListBoxes;
  61.    returns FALSE if UpdateListBoxes returns TRUE.  If the edit control
  62.    contains an invalid file name, also returns FALSE. */
  63. BOOL TFileDialog::CanClose()
  64. {
  65.   WORD PathLen;
  66.   OFSTRUCT AnOfstruct;
  67.  
  68.   GetDlgItemText(HWindow, ID_FNAME, PathName, MAXPATH);
  69.   if ( lstrcmpi(&PathName[_fstrlen(PathName) - 2], "..") == 0 )
  70.     _fstrcat(PathName, "\\");  // otherwise OpenFile messes up
  71.   if ( OpenFile(PathName, &AnOfstruct, OF_PARSE) == -1 )
  72.   {
  73.     MessageBeep(0);
  74.     SelectFileName();
  75.     return FALSE;
  76.   }
  77.   // Note that szPathName initially uses OEM char set.
  78.   OemToAnsi((LPSTR)(AnOfstruct.szPathName), (LPSTR)(AnOfstruct.szPathName));
  79.   _fstrncpy(PathName, (LPSTR)(AnOfstruct.szPathName), MAXPATH);
  80.   PathName[MAXPATH-1] = '\0';
  81.   PathLen = _fstrlen(PathName);
  82.   if ( PathName[PathLen - 1] == '\\' ||
  83.        HasWildCards(PathName) ||
  84.        GetFocus() == GetDlgItem(HWindow, ID_DLIST) )
  85.   {
  86.     if ( PathName[PathLen - 1] == '\\' )
  87.       _fstrncat(PathName, FileSpec, (MAXPATH-1) - _fstrlen(PathName));
  88.     if ( !UpdateListBoxes() )
  89.     {
  90.       MessageBeep(0);
  91.       SelectFileName();
  92.     }
  93.     return FALSE;
  94.   }
  95.   _fstrncat(PathName, "\\", (MAXPATH-1) - _fstrlen(PathName));
  96.   _fstrncat(PathName, FileSpec, (MAXPATH-1) - _fstrlen(PathName));
  97.  
  98.   if ( UpdateListBoxes() )
  99.     return FALSE;
  100.   PathName[PathLen] = '\0';
  101.   if ( GetExtension(PathName)[0] == '\0' )
  102.   {
  103.     _fstrncat(PathName, Extension, (MAXPATH-1) - _fstrlen(PathName));
  104.     PathName[MAXPATH-1] = '\0';
  105.   }
  106.   AnsiLower(_fstrcpy(FilePath, PathName));
  107.   return TRUE;
  108. }
  109.  
  110. /* Sets up the file dialog.  Limits the number of characters which
  111.    can be entered into the edit control to MAXPATH - 1.  Then calls
  112.    UpdateListBoxes and SelectFileName. */
  113. void TFileDialog::SetupWindow()
  114. {
  115.   TDialog::SetupWindow();
  116.   SendDlgItemMessage(HWindow, ID_FNAME, EM_LIMITTEXT, MAXPATH-1, 0);
  117.   _fstrncpy(PathName, FilePath, MAXPATH);
  118.   PathName[MAXPATH-1] = '\0';
  119.  
  120.   if ( !UpdateListBoxes() )
  121.   {
  122.     _fstrcpy(PathName, "*.*");
  123.     UpdateListBoxes();
  124.   }
  125.   SelectFileName();
  126. }
  127.  
  128. /* Responds to messages from the edit control.  Enables the OK button
  129.    if the edit control contains text. */
  130. void TFileDialog::HandleFName(TMessage& Msg)
  131. {
  132.   if ( HIWORD(Msg.LParam) == EN_CHANGE )
  133.     EnableWindow(GetDlgItem(HWindow, IDOK),
  134.       SendMessage((HWND)LOWORD(Msg.LParam), WM_GETTEXTLENGTH, 0, 0) != 0 );
  135. }
  136.  
  137. /* Responds to messages from the file list box.  Updates PathName with
  138.    the name of the selected file and calls UpdateFileName, when the
  139.    selection in the list box changes.  Attempts to close the dialog if
  140.    the selected entry was double-clicked.  Clears the selection in the
  141.    list box when it loses the focus. */
  142. void TFileDialog::HandleFList(TMessage& Msg)
  143. {
  144.   switch (HIWORD(Msg.LParam)) {
  145.     case LBN_SELCHANGE:
  146.     case LBN_DBLCLK:
  147.          DlgDirSelect(HWindow, PathName, ID_FLIST);
  148.          UpdateFileName();
  149.          if ( HIWORD(Msg.LParam) == LBN_DBLCLK )
  150.            CloseWindow(IDOK);
  151.          break;
  152.     case LBN_KILLFOCUS:
  153.      SendMessage((HWND)LOWORD(Msg.LParam), LB_SETCURSEL, (WORD)-1, 0);
  154.          break;
  155.   }
  156. }
  157.  
  158. /* Responds to messages from the directory list box.  Updates PathName
  159.    when the selection in the list box changes.  Calls UpdateListBoxes if
  160.    the selected entry was double-clicked, else calls UpdateFileName.
  161.    Clears the selection in the list box when it loses the focus.  */
  162. void TFileDialog::HandleDList(TMessage& Msg)
  163. {
  164.   switch (HIWORD(Msg.LParam)) {
  165.     case LBN_SELCHANGE:
  166.     case LBN_DBLCLK:
  167.          DlgDirSelect(HWindow, PathName, ID_DLIST);
  168.          _fstrcat(PathName, FileSpec);
  169.          if ( HIWORD(Msg.LParam) == LBN_DBLCLK )
  170.            UpdateListBoxes();
  171.          else
  172.            UpdateFileName();
  173.          break;
  174.     case LBN_KILLFOCUS:
  175.      SendMessage((HWND)LOWORD(Msg.LParam), LB_SETCURSEL, (WORD)-1, 0);
  176.          break;
  177.   }
  178. }
  179.  
  180. /* Selects the text in the edit control, and sets the focus to the
  181.    edit control. */
  182. void TFileDialog::SelectFileName()
  183. {
  184.   SendDlgItemMessage(HWindow, ID_FNAME, EM_SETSEL, 0, 0x7FFF000L);
  185.   SetFocus(GetDlgItem(HWindow, ID_FNAME));
  186. }
  187.  
  188. /* Sets the text of the edit control to PathName, and selects the
  189.    text. */
  190. void TFileDialog::UpdateFileName()
  191. {
  192.   SetDlgItemText(HWindow, ID_FNAME, AnsiLower(PathName));
  193.   SendDlgItemMessage(HWindow, ID_FNAME, EM_SETSEL, 0, 0x7FFF0000L);
  194. }
  195.  
  196. /* Attempts to update the file and/or directory list boxes.  If updated,
  197.    calls UpdateFileName and returns TRUE.  Else returns FALSE. */
  198. BOOL TFileDialog::UpdateListBoxes()
  199. {
  200.   int Result;
  201.   char Path[MAXPATH];
  202.  
  203.   Path[0] = '\0';
  204.   if ( GetDlgItem(HWindow, ID_FLIST) )
  205.   {
  206.     _fstrcpy(Path, PathName);
  207.     Result = DlgDirList(HWindow, Path, ID_FLIST, ID_FPATH, 0);
  208.     if ( Result )
  209.       DlgDirList(HWindow, "*.*", ID_DLIST, 0, 0xC010);
  210.   }
  211.   else
  212.   {
  213.       _fstrncpy(Path, PathName, MAXPATH);
  214.       Path[MAXPATH-1] = '\0';
  215.       _fstrncat(Path, "*.*", (MAXPATH-1) - strlen(Path));
  216.       Result = DlgDirList(HWindow, Path, ID_DLIST, ID_FPATH, 0xC010);
  217.   }
  218.   if ( Result )
  219.   {
  220.     _fstrncpy(FileSpec, GetFileName(PathName), FILESPEC);
  221.     FileSpec[FILESPEC-1] = '\0';
  222.     _fstrcpy(PathName, FileSpec);
  223.     UpdateFileName();
  224.     return TRUE;
  225.   }
  226.   return FALSE;
  227. }
  228.  
  229. TStreamable *TFileDialog::build()
  230. {
  231.   return new TFileDialog(streamableInit);
  232. }
  233.  
  234. TStreamableClass RegFileDialog("TFileDialog", TFileDialog::build,
  235.                         __DELTA(TFileDialog));
  236.