home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / TEMPLATE / EDITFILE.C < prev    next >
Text File  |  1993-12-01  |  16KB  |  492 lines

  1.  
  2.                 case IDM_OPEN:
  3.                     if (!QuerySaveFile(hWnd))
  4.                         return (NULL);
  5.  
  6.                     lpOpenDlg = MakeProcInstance((FARPROC) OpenDlg, hInst);
  7.  
  8.                     /* Open the file and get its handle */
  9.  
  10.                     hFile = DialogBox(hInst, "Open", hWnd, lpOpenDlg);
  11.                     FreeProcInstance(lpOpenDlg);
  12.                     if (!hFile)
  13.                         return (NULL);
  14.  
  15.                     /* Allocate edit buffer to the size of the file + 1 */
  16.  
  17.                     hEditBuffer =
  18.                         LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT,
  19.                 (WORD)(FileStatus.st_size+1));
  20.  
  21.                     if (!hEditBuffer) {
  22.                         MessageBox(hWnd, "Not enough memory.",
  23.                             NULL, MB_OK | MB_ICONHAND);
  24.                         return (NULL);
  25.                     }
  26.                     hSaveCursor = SetCursor(hHourGlass);
  27.                     pEditBuffer = LocalLock(hEditBuffer);
  28.  
  29.                     IOStatus = read(hFile, pEditBuffer, FileStatus.st_size);
  30.                     close(hFile);
  31.  
  32.                     /* # bytes read must equal file size */
  33.  
  34.                     if (IOStatus != FileStatus.st_size) {
  35.  
  36.                         sprintf(str, "Error reading %s.", FileName);
  37.                         SetCursor(hSaveCursor);      /* Remove the hourglass */
  38.                         MessageBox(hWnd, str, NULL, MB_OK | MB_ICONEXCLAMATION);
  39.                     }
  40.  
  41.                     LocalUnlock(hEditBuffer);
  42.  
  43.                     /* Set up a new buffer and window title */
  44.  
  45.                     sprintf(str, "EditFile - %s", FileName);
  46.                     SetNewBuffer(hWnd, hEditBuffer, str);
  47.                     SetCursor(hSaveCursor);            /* restore the cursor */
  48.                     break;
  49.  
  50.  
  51. /****************************************************************************
  52.  
  53.     FUNCTION: TraceOutput(HWND, unsigned, WORD, LONG)
  54.  
  55.     PURPOSE: Let user select a file, and open it.
  56.  
  57. ****************************************************************************/
  58.  
  59. HANDLE FAR PASCAL TraceOutput(hDlg, message, wParam, lParam)
  60. HWND hDlg;
  61. unsigned message;
  62. WORD wParam;
  63. LONG lParam;
  64. {
  65.     WORD index;
  66.     PSTR pTptr;
  67.     HANDLE hFile;
  68.  
  69.     switch (message) {
  70.         case WM_COMMAND:
  71.             switch (wParam) {
  72.  
  73.                 case IDC_LISTBOX:
  74.                     switch (HIWORD(lParam)) {
  75.  
  76.                         case LBN_SELCHANGE:
  77.                             /* If item is a directory name, append "*.*" */
  78.                             if (DlgDirSelect(hDlg, str, IDC_LISTBOX)) 
  79.                                 strcat(str, DefSpec);
  80.  
  81.                             SetDlgItemText(hDlg, IDC_EDIT, str);
  82.                             SendDlgItemMessage(hDlg,
  83.                                 IDC_EDIT,
  84.                                 EM_SETSEL,
  85.                                 NULL,
  86.                                 MAKELONG(0, 0x7fff));
  87.                             break;
  88.  
  89.                         case LBN_DBLCLK:
  90.                             goto openfile;
  91.                     }
  92.                     return (TRUE);
  93.  
  94.                 case IDD_OK:
  95. openfile:
  96.                     GetDlgItemText(hDlg, IDC_EDIT, OpenName, 128);
  97.                     if (strchr(OpenName, '*') || strchr(OpenName, '?')) {
  98.                         SeparateFile(hDlg, (LPSTR) str, (LPSTR) DefSpec,
  99.                             (LPSTR) OpenName);
  100.                         if (str[0])
  101.                             strcpy(DefPath, str);
  102.                         ChangeDefExt(DefExt, DefSpec);
  103.                         UpdateListBox(hDlg);
  104.                         return (TRUE);
  105.                     }
  106.  
  107.                     if (!OpenName[0]) {
  108.                         MessageBox(hDlg, "No filename specified.",
  109.                             NULL, MB_OK | MB_ICONHAND);
  110.                         return (TRUE);
  111.                     }
  112.  
  113.                     AddExt(OpenName, DefExt);
  114.  
  115.                     /* Open the file */
  116.  
  117.                     if ((hFile = OpenFile(OpenName, (LPOFSTRUCT) &OfStruct,
  118.                             OF_READ)) == -1) {
  119.                         sprintf(str, "Error %d opening %s.",
  120.                             OfStruct.nErrCode, OpenName);
  121.                         MessageBox(hDlg, str, NULL,
  122.                             MB_OK | MB_ICONHAND);
  123.                     }
  124.                     else {
  125.  
  126.                         /* Make sure there's enough room for the file */
  127.  
  128.                         fstat(hFile, &FileStatus);
  129.                         if (FileStatus.st_size > MAXFILESIZE) {
  130.                             sprintf(str,
  131.                     "Not enough memory to load %s.\n%s exceeds %ld bytes.",
  132.                                 OpenName, OpenName, MAXFILESIZE);
  133.                             MessageBox(hDlg, str, NULL,
  134.                                 MB_OK | MB_ICONHAND);
  135.                             return (TRUE);
  136.                         }
  137.  
  138.                         /* File is opened and there is enough room so return
  139.                          * the handle to the caller.
  140.                          */
  141.  
  142.                         strcpy(FileName, OpenName);
  143.                         EndDialog(hDlg, hFile);
  144.                         return (TRUE);
  145.                     }
  146.                     return (TRUE);
  147.  
  148.                 case IDD_CANCEL:
  149.                     EndDialog(hDlg, NULL);
  150.                     return (TRUE);
  151.             }
  152.             break;
  153.  
  154.         case WM_INITDIALOG:                        /* message: initialize    */
  155.             UpdateListBox(hDlg);
  156.             SetDlgItemText(hDlg, IDC_EDIT, DefSpec);
  157.             SendDlgItemMessage(hDlg,               /* dialog handle      */
  158.                 IDC_EDIT,                          /* where to send message  */
  159.                 EM_SETSEL,                         /* select characters      */
  160.                 NULL,                              /* additional information */
  161.                 MAKELONG(0, 0x7fff));              /* entire contents      */
  162.             SetFocus(GetDlgItem(hDlg, IDC_EDIT));
  163.             return (FALSE); /* Indicates the focus is set to a control */
  164.     }
  165.     return FALSE;
  166. }
  167.  
  168. /****************************************************************************
  169.  
  170.     FUNCTION: UpdateListBox(HWND);
  171.  
  172.     PURPOSE: Update the list box of TraceOutput
  173.  
  174. ****************************************************************************/
  175.  
  176. void UpdateListBox(hDlg)
  177. HWND hDlg;
  178. {
  179.     strcpy(str, DefPath);
  180.     strcat(str, DefSpec);
  181.     DlgDirList(hDlg, str, IDC_LISTBOX, IDC_PATH, 0x4010);
  182.  
  183.     /* To ensure that the listing is made for a subdir. of
  184.      * current drive dir...
  185.      */
  186.     if (!strchr (DefPath, ':'))
  187.     DlgDirList(hDlg, DefSpec, IDC_LISTBOX, IDC_PATH, 0x4010);
  188.  
  189.     /* Remove the '..' character from path if it exists, since this
  190.      * will make DlgDirList move us up an additional level in the tree
  191.      * when UpdateListBox() is called again.
  192.      */
  193.     if (strstr (DefPath, ".."))
  194.     DefPath[0] = '\0';
  195.  
  196.     SetDlgItemText(hDlg, IDC_EDIT, DefSpec);
  197. }
  198.  
  199. /****************************************************************************
  200.  
  201.     FUNCTION: ChangeDefExt(PSTR, PSTR);
  202.  
  203.     PURPOSE: Change the default extension
  204.  
  205. ****************************************************************************/
  206.  
  207. void ChangeDefExt(Ext, Name)
  208. PSTR Ext, Name;
  209. {
  210.     PSTR pTptr;
  211.  
  212.     pTptr = Name;
  213.     while (*pTptr && *pTptr != '.')
  214.         pTptr++;
  215.     if (*pTptr)
  216.         if (!strchr(pTptr, '*') && !strchr(pTptr, '?'))
  217.             strcpy(Ext, pTptr);
  218. }
  219.  
  220. /****************************************************************************
  221.  
  222.     FUNCTION: SeparateFile(HWND, LPSTR, LPSTR, LPSTR)
  223.  
  224.     PURPOSE: Separate filename and pathname
  225.  
  226. ****************************************************************************/
  227.  
  228. void SeparateFile(hDlg, lpDestPath, lpDestFileName, lpSrcFileName)
  229. HWND hDlg;
  230. LPSTR lpDestPath, lpDestFileName, lpSrcFileName;
  231. {
  232.     LPSTR lpTmp;
  233.     char  cTmp;
  234.  
  235.     lpTmp = lpSrcFileName + (long) lstrlen(lpSrcFileName);
  236.     while (*lpTmp != ':' && *lpTmp != '\\' && lpTmp > lpSrcFileName)
  237.         lpTmp = AnsiPrev(lpSrcFileName, lpTmp);
  238.     if (*lpTmp != ':' && *lpTmp != '\\') {
  239.         lstrcpy(lpDestFileName, lpSrcFileName);
  240.         lpDestPath[0] = 0;
  241.         return;
  242.     }
  243.     lstrcpy(lpDestFileName, lpTmp + 1);
  244.     cTmp = *(lpTmp + 1);
  245.     lstrcpy(lpDestPath, lpSrcFileName);
  246.      *(lpTmp + 1) = cTmp;
  247.     lpDestPath[(lpTmp - lpSrcFileName) + 1] = 0;
  248. }
  249.  
  250. /****************************************************************************
  251.  
  252.     FUNCTION: AddExt(PSTR, PSTR);
  253.  
  254.     PURPOSE: Add default extension
  255.  
  256. /***************************************************************************/
  257.  
  258. void AddExt(Name, Ext)
  259. PSTR Name, Ext;
  260. {
  261.     PSTR pTptr;
  262.  
  263.     pTptr = Name;
  264.     while (*pTptr && *pTptr != '.')
  265.         pTptr++;
  266.     if (*pTptr != '.')
  267.         strcat(Name, Ext);
  268. }
  269.  
  270. /****************************************************************************
  271.  
  272.     FUNCTION: CheckFileName(HWND, PSTR, PSTR)
  273.  
  274.     PURPOSE: Check for wildcards, add extension if needed
  275.  
  276.     COMMENTS:
  277.  
  278.         Make sure you have a filename and that it does not contain any
  279.         wildcards.  If needed, add the default extension.  This function is
  280.         called whenever your application wants to save a file.
  281.  
  282. ****************************************************************************/
  283.  
  284. BOOL CheckFileName(hWnd, pDest, pSrc)
  285. HWND hWnd;
  286. PSTR pDest, pSrc;
  287. {
  288.     PSTR pTmp;
  289.  
  290.     if (!pSrc[0])
  291.         return (FALSE);               /* Indicates no filename was specified */
  292.  
  293.     pTmp = pSrc;
  294.     while (*pTmp) {                     /* Searches the string for wildcards */
  295.         switch (*pTmp++) {
  296.             case '*':
  297.             case '?':
  298.                 MessageBox(hWnd, "Wildcards not allowed.",
  299.                     NULL, MB_OK | MB_ICONEXCLAMATION);
  300.                 return (FALSE);
  301.         }
  302.     }
  303.  
  304.     AddExt(pSrc, DefExt);            /* Adds the default extension if needed */
  305.  
  306.     if (OpenFile(pSrc, (LPOFSTRUCT) &OfStruct, OF_EXIST) >= 0) {
  307.         sprintf(str, "Replace existing %s?", pSrc);
  308.         if (MessageBox(hWnd, str, "EditFile",
  309.                 MB_OKCANCEL | MB_ICONHAND) == IDCANCEL)
  310.             return (FALSE);
  311.     }
  312.     strcpy(pDest, pSrc);
  313.     return (TRUE);
  314. }
  315.  
  316. /****************************************************************************
  317.  
  318.     FUNCTION: SaveFile(HWND)
  319.  
  320.     PURPOSE: Save current file
  321.  
  322.     COMMENTS:
  323.  
  324.         This saves the current contents of the Edit buffer, and changes
  325.         bChanges to indicate that the buffer has not been changed since the
  326.         last save.
  327.  
  328.         Before the edit buffer is sent, you must get its handle and lock it
  329.         to get its address.  Once the file is written, you must unlock the
  330.         buffer.  This allows Windows to move the buffer when not in immediate
  331.         use.
  332.  
  333. ****************************************************************************/
  334.  
  335. BOOL SaveFile(hWnd)
  336. HWND hWnd;
  337. {
  338.     BOOL bSuccess;
  339.     int IOStatus;                                  /* result of a file write */
  340.  
  341.     if ((hFile = OpenFile(FileName, &OfStruct,
  342.         OF_PROMPT | OF_CANCEL | OF_CREATE)) < 0) {
  343.  
  344.         /* If the file can't be saved */
  345.  
  346.         sprintf(str, "Cannot write to %s.", FileName);
  347.         MessageBox(hWnd, str, NULL, MB_OK | MB_ICONEXCLAMATION);
  348.         return (FALSE);
  349.     }
  350.  
  351.  
  352.     hEditBuffer = SendMessage(hEditWnd, EM_GETHANDLE, 0, 0L);
  353.     pEditBuffer = LocalLock(hEditBuffer);
  354.  
  355.     /* Set the cursor to an hourglass during the file transfer */
  356.  
  357.     hSaveCursor = SetCursor(hHourGlass);
  358.     IOStatus = write(hFile, pEditBuffer, strlen(pEditBuffer));
  359.     close(hFile);
  360.     SetCursor(hSaveCursor);
  361.     if (IOStatus != strlen(pEditBuffer)) {
  362.         sprintf(str, "Error writing to %s.", FileName);
  363.         MessageBox(hWnd, str,
  364.             NULL, MB_OK | MB_ICONHAND);
  365.         bSuccess = FALSE;
  366.     }
  367.     else {
  368.         bSuccess = TRUE;                /* Indicates the file was saved      */
  369.         bChanges = FALSE;               /* Indicates changes have been saved */
  370.     }
  371.  
  372.     LocalUnlock(hEditBuffer);
  373.     return (bSuccess);
  374. }
  375.  
  376. /****************************************************************************
  377.  
  378.     FUNCTION: QuerySaveFile(HWND);
  379.  
  380.     PURPOSE: Called when some action might lose current contents
  381.  
  382.     COMMENTS:
  383.  
  384.         This function is called whenever we are about to take an action that
  385.         would lose the current contents of the edit buffer.
  386.  
  387. ****************************************************************************/
  388.  
  389. BOOL QuerySaveFile(hWnd)
  390. HWND hWnd;
  391. {
  392.     int Response;
  393.     FARPROC lpSaveAsDlg;
  394.  
  395.     if (bChanges) {
  396.         sprintf(str, "Save current changes: %s", FileName);
  397.         Response = MessageBox(hWnd, str,
  398.             "EditFile",  MB_YESNOCANCEL | MB_ICONEXCLAMATION);
  399.         if (Response == IDYES) {
  400. check_name:
  401.  
  402.             /* Make sure there is a filename to save to */
  403.  
  404.             if (!FileName[0]) {
  405.                 lpSaveAsDlg = MakeProcInstance(SaveAsDlg, hInst);
  406.                 Response = DialogBox(hInst, "SaveAs",
  407.                     hWnd, lpSaveAsDlg);
  408.                 FreeProcInstance(lpSaveAsDlg);
  409.                 if (Response == IDOK)
  410.                     goto check_name;
  411.                 else
  412.                     return (FALSE);
  413.             }
  414.             SaveFile(hWnd);
  415.         }
  416.         else if (Response == IDCANCEL)
  417.             return (FALSE);
  418.     }
  419.     else
  420.         return (TRUE);
  421. }
  422.  
  423. /****************************************************************************
  424.  
  425.     FUNCTION: SetNewBuffer(HWND, HANDLE, PSTR)
  426.  
  427.     PURPOSE: Set new buffer for edit window
  428.  
  429.     COMMENTS:
  430.  
  431.         Point the edit window to the new buffer, update the window title, and
  432.         redraw the edit window.  If hNewBuffer is NULL, then create an empty
  433.         1K buffer, and return its handle.
  434.  
  435. ****************************************************************************/
  436.  
  437. void SetNewBuffer(hWnd, hNewBuffer, Title)
  438. HWND hWnd;
  439. HANDLE hNewBuffer;
  440. PSTR Title;
  441. {
  442.     HANDLE hOldBuffer;
  443.  
  444.     hOldBuffer = SendMessage(hEditWnd, EM_GETHANDLE, 0, 0L);
  445.     LocalFree(hOldBuffer);
  446.     if (!hNewBuffer)                    /* Allocates a buffer if none exists */
  447.         hNewBuffer = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, 1);
  448.  
  449.     /* Updates the buffer and displays new buffer */
  450.     SendMessage(hEditWnd, EM_SETHANDLE, hNewBuffer, 0L);
  451.  
  452.     SetWindowText(hWnd, Title);
  453.     SetFocus(hEditWnd);
  454.     bChanges = FALSE;
  455. }
  456.  
  457.  
  458. /****************************************************************************
  459.  
  460.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  461.  
  462.     PURPOSE:  Processes messages for "About" dialog box
  463.  
  464.     MESSAGES:
  465.  
  466.         WM_INITDIALOG - initialize dialog box
  467.         WM_COMMAND    - Input received
  468.  
  469. ****************************************************************************/
  470.  
  471. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  472. HWND hDlg;
  473. unsigned message;
  474. WORD wParam;
  475. LONG lParam;
  476. {
  477.     switch (message) {
  478.         case WM_INITDIALOG:
  479.             return (TRUE);
  480.  
  481.         case WM_COMMAND:
  482.         if (wParam == IDOK
  483.                 || wParam == IDCANCEL) {
  484.                 EndDialog(hDlg, TRUE);
  485.                 return (TRUE);
  486.             }
  487.             break;
  488.     }
  489.     return (FALSE);
  490. }
  491. 
  492.