home *** CD-ROM | disk | FTP | other *** search
/ Shareware Supreme Volume 6 #1 / swsii.zip / swsii / 215 / DDJ9210.ZIP / DFLT14.ZIP / FILEOPEN.C < prev    next >
Text File  |  1992-04-22  |  8KB  |  242 lines

  1. /* ----------- fileopen.c ------------- */
  2.  
  3. #include "dflat.h"
  4.  
  5. static BOOL DlgFileOpen(char *, char *, DBOX *);
  6. static int DlgFnOpen(WINDOW, MESSAGE, PARAM, PARAM);
  7. static void InitDlgBox(WINDOW);
  8. static void StripPath(char *);
  9. static BOOL IncompleteFilename(char *);
  10.  
  11. static char *OrigSpec;
  12. static char *FileSpec;
  13. static char *FileName;
  14. static char *NewFileName;
  15.  
  16. static BOOL Saving;
  17. extern DBOX FileOpen;
  18. extern DBOX SaveAs;
  19.  
  20. /*
  21.  * Dialog Box to select a file to open
  22.  */
  23. BOOL OpenFileDialogBox(char *Fpath, char *Fname)
  24. {
  25.     return DlgFileOpen(Fpath, Fname, &FileOpen);
  26. }
  27.  
  28. /*
  29.  * Dialog Box to select a file to save as
  30.  */
  31. BOOL SaveAsDialogBox(char *Fname)
  32. {
  33.     return DlgFileOpen(NULL, Fname, &SaveAs);
  34. }
  35.  
  36. /* --------- generic file open ---------- */
  37. static BOOL DlgFileOpen(char *Fpath, char *Fname, DBOX *db)
  38. {
  39.     BOOL rtn;
  40.     char savedir[80];
  41.     char OSpec[80];
  42.     char FSpec[80];
  43.     char FName[80];
  44.     char NewFName[80];
  45.  
  46.     OrigSpec = OSpec;
  47.     FileSpec = FSpec;
  48.     FileName = FName;
  49.     NewFileName = NewFName;
  50.  
  51.     getcwd(savedir, sizeof savedir);
  52.     if (Fpath != NULL)    {
  53.         strncpy(FileSpec, Fpath, 80);
  54.         Saving = FALSE;
  55.     }
  56.     else    {
  57.         *FileSpec = '\0';
  58.         Saving = TRUE;
  59.     }
  60.     strcpy(FileName, FileSpec);
  61.     strcpy(OrigSpec, FileSpec);
  62.  
  63.     if ((rtn = DialogBox(NULL, db, TRUE, DlgFnOpen)) != FALSE)
  64.         strcpy(Fname, NewFileName);
  65.     else
  66.         *Fname = '\0';
  67.  
  68.     setdisk(toupper(*savedir) - 'A');
  69.     chdir(savedir);
  70.  
  71.     return rtn;
  72. }
  73.  
  74. /*
  75.  *  Process dialog box messages
  76.  */
  77. static int DlgFnOpen(WINDOW wnd,MESSAGE msg,PARAM p1,PARAM p2)
  78. {
  79.     switch (msg)    {
  80.         case CREATE_WINDOW:    {
  81.             int rtn = DefaultWndProc(wnd, msg, p1, p2);
  82.             DBOX *db = wnd->extension;
  83.             WINDOW cwnd = ControlWindow(db, ID_FILENAME);
  84.             SendMessage(cwnd, SETTEXTLENGTH, 64, 0);
  85.             return rtn;
  86.         }
  87.         case INITIATE_DIALOG:
  88.             InitDlgBox(wnd);
  89.             break;
  90.         case COMMAND:
  91.             switch ((int) p1)    {
  92.                 case ID_FILENAME:
  93.                     if (p2 != ENTERFOCUS)    {
  94.                         /* allow user to modify the file spec */
  95.                         GetItemText(wnd, ID_FILENAME,
  96.                                 FileName, 65);
  97.                         if (IncompleteFilename(FileName) ||
  98.                                                     Saving)    {
  99.                             strcpy(OrigSpec, FileName);
  100.                             StripPath(OrigSpec);
  101.                         }
  102.                         if (p2 != LEAVEFOCUS)
  103.                             SendMessage(wnd, COMMAND, ID_OK, 0);
  104.                     }
  105.                     return TRUE;
  106.                 case ID_OK:
  107.                     if (p2 != 0)
  108.                         break;
  109.                     GetItemText(wnd, ID_FILENAME,
  110.                             FileName, 65);
  111.                     strcpy(FileSpec, FileName);
  112.                     if (IncompleteFilename(FileName))    {
  113.                         /* no file name yet */
  114.                         InitDlgBox(wnd);
  115.                         strcpy(OrigSpec, FileSpec);
  116.                         return TRUE;
  117.                     }
  118.                     else    {
  119.                         GetItemText(wnd, ID_PATH, FileName, 65);
  120.                         strcat(FileName, FileSpec);
  121.                         strcpy(NewFileName, FileName);
  122.                     }
  123.                     break;
  124.                 case ID_FILES:
  125.                     switch ((int) p2)    {
  126.                         case ENTERFOCUS:
  127.                         case LB_SELECTION:
  128.                             /* selected a different filename */
  129.                             GetDlgListText(wnd, FileName,
  130.                                         ID_FILES);
  131.                             PutItemText(wnd, ID_FILENAME,
  132.                                             FileName);
  133.                             break;
  134.                         case LB_CHOOSE:
  135.                             /* chose a file name */
  136.                             GetDlgListText(wnd, FileName,
  137.                                     ID_FILES);
  138.                             SendMessage(wnd, COMMAND, ID_OK, 0);
  139.                             break;
  140.                         default:
  141.                             break;
  142.                     }
  143.                     return TRUE;
  144.                 case ID_DRIVE:
  145.                     switch ((int) p2)    {
  146.                         case ENTERFOCUS:
  147.                             if (Saving)
  148.                                 *FileSpec = '\0';
  149.                             break;
  150.                         case LEAVEFOCUS:
  151.                             if (Saving)
  152.                                 strcpy(FileSpec, FileName);
  153.                             break;
  154.                         case LB_SELECTION:    {
  155.                             char dd[25];
  156.                             /* selected different drive/dir */
  157.                             GetDlgListText(wnd, dd,
  158.                                                 ID_DRIVE);
  159.                             if (*(dd+2) == ':')
  160.                                 *(dd+3) = '\0';
  161.                             else
  162.                                 *(dd+strlen(dd)-1) = '\0';
  163.                             strcpy(FileName, dd+1);
  164.                             if (*(dd+2) != ':' && *OrigSpec != '\\')
  165.                                 strcat(FileName, "\\");
  166.                             strcat(FileName, OrigSpec);
  167.                             if (*(FileName+1) != ':' && *FileName != '.')    {
  168.                                 GetItemText(wnd, ID_PATH, FileSpec, 65);
  169.                                 strcat(FileSpec, FileName);
  170.                             }
  171.                             else 
  172.                                 strcpy(FileSpec, FileName);
  173.                             break;
  174.                         }
  175.                         case LB_CHOOSE:
  176.                             /* chose drive/dir */
  177.                             if (Saving)
  178.                                 PutItemText(wnd, ID_FILENAME, "");
  179.                             InitDlgBox(wnd);
  180.                             return TRUE;
  181.                         default:
  182.                             break;
  183.                     }
  184.                     PutItemText(wnd, ID_FILENAME, FileSpec);
  185.                     return TRUE;
  186.  
  187.  
  188.                 default:
  189.                     break;
  190.             }
  191.         default:
  192.             break;
  193.     }
  194.     return DefaultWndProc(wnd, msg, p1, p2);
  195. }
  196.  
  197. /*
  198.  *  Initialize the dialog box
  199.  */
  200. static void InitDlgBox(WINDOW wnd)
  201. {
  202.     if (*FileSpec && !Saving)
  203.         PutItemText(wnd, ID_FILENAME, FileSpec);
  204.     if (DlgDirList(wnd, FileSpec, ID_FILES, ID_PATH, 0))    {
  205.         StripPath(FileSpec);
  206.         DlgDirList(wnd, "*.*", ID_DRIVE, 0, 0xc010);
  207.     }
  208. }
  209.  
  210. /*
  211.  * Strip the drive and path information from a file spec
  212.  */
  213. static void StripPath(char *filespec)
  214. {
  215.     char *cp, *cp1;
  216.  
  217.     cp = strchr(filespec, ':');
  218.     if (cp != NULL)
  219.         cp++;
  220.     else
  221.         cp = filespec;
  222.     while (TRUE)    {
  223.         cp1 = strchr(cp, '\\');
  224.         if (cp1 == NULL)
  225.             break;
  226.         cp = cp1+1;
  227.     }
  228.     strcpy(filespec, cp);
  229. }
  230.  
  231.  
  232. static BOOL IncompleteFilename(char *s)
  233. {
  234.     int lc = strlen(s)-1;
  235.     if (strchr(s, '?') || strchr(s, '*') || !*s)
  236.         return TRUE;
  237.     if (*(s+lc) == ':' || *(s+lc) == '\\')
  238.         return TRUE;
  239.     return FALSE;
  240. }
  241.  
  242.