home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / w3_prog / zap.arj / Z2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-08  |  10.3 KB  |  344 lines

  1. /***********************************************************************
  2. * Zap                                                                  *
  3. * ===                                                                  *
  4. *                                                                      *
  5. * Windows 3 Text Editor                                                *
  6. *                                                                      *
  7. * Window creation and management module                                *
  8. *   BOOL ReadFile(int, BOOL);                                          *
  9. *   BOOL SaveFile(int, BOOL);                                          *
  10. *                                                                      *
  11. *   BOOL CreateEditWindow(void);                                       *
  12. *   void RearrangeEditWindows(void);                                   *
  13. *   void RedrawStatusBar(HDC, int);                                    *
  14. *                                                                      *
  15. *   BOOL SendToClipboard(LPSTR);                                       *
  16. *                                                                      *
  17. * This programme was developed using Microsoft C6.0 and the Microsoft  *
  18. * SDK, however any ANSI C could be used if suitable libraries and the  *
  19. * Windows header are available.                                        *
  20. ***********************************************************************/
  21.  
  22. #include <windows.h>
  23. #include <commdlg.h>
  24. #include <string.h>
  25. #include "zap.h"
  26. #include "zapdlg.h"
  27.  
  28.  
  29. /***********************************************************************
  30. * External variables                                                   *
  31. ***********************************************************************/
  32.  
  33. extern HANDLE hInst;
  34. extern HWND   hMainWnd;
  35.  
  36. extern EDIT_WND EditWnd[MAX_FILES];
  37. extern int NumFiles, CurrentFile;
  38.  
  39. extern FARPROC lpNewEditWndProc;
  40.  
  41. extern LPSTR TransBuf;
  42.  
  43. extern int  CharWd, CharDp;
  44.  
  45. extern OPENFILENAME OpenFileInfo;
  46. extern char FileName[MAX_DOS_NAME], FileTitle[MAX_DOS_NAME];
  47.  
  48. extern char WorkStr[WORK_STR_LEN];
  49.  
  50.  
  51. /***********************************************************************
  52. * Routine to read a file                                               *
  53. *                                                                      *
  54. * Arguments                                                            *
  55. *   Id      - int,  index of buffer to read file into                  *
  56. *   NewFile - BOOL, if TRUE replace buffer contents else add to buffer *
  57. ***********************************************************************/
  58.  
  59. BOOL ReadFile(int Id, BOOL NewFile)
  60. { int handle;
  61.   long curpos, iL;
  62.  
  63. /*** Create dialog box to get file name */
  64.  
  65.   if (!GetOpenFileName((LPOPENFILENAME) &OpenFileInfo))
  66.     return FALSE;
  67.   AnsiLower(FileName);
  68.  
  69. /*** Open and read file */
  70.  
  71.   if ((handle = _lopen(FileName, OF_READ)) == -1)
  72.   { sprintf(WorkStr, "Cannot open file \"%s\"", FileName);
  73.     Comment(WorkStr);
  74.     return(FALSE);
  75.   }
  76.  
  77. /*** If not inserting clear current data */
  78.  
  79.   if (NewFile) SetWindowText(EditWnd[Id].hwnd, "");
  80.  
  81. /*** And read data */
  82.  
  83.   if ((iL = _lread(handle, TransBuf, TRANS_BUF_SIZE - 1)) == TRANS_BUF_SIZE - 1)
  84.     Comment("Warning: file is too big and has been truncated");
  85.   TransBuf[iL] = '\0';
  86.  
  87. /*** Data is inserted by putting it in the clipboard then PASTEing */
  88.  
  89.   curpos = LOWORD(CallWindowProc(EditWnd[Id].proc, EditWnd[Id].hwnd, EM_GETSEL, 0, 0L));
  90.  
  91.   if (SendToClipboard(TransBuf))
  92.     CallWindowProc(EditWnd[Id].proc, EditWnd[Id].hwnd, WM_PASTE, 0, 0L);
  93.  
  94.   CallWindowProc(EditWnd[Id].proc, EditWnd[Id].hwnd, EM_SETSEL, 0, MAKELONG(curpos, curpos));
  95.  
  96. /*** All done so close file */
  97.  
  98.   _lclose(handle);
  99.  
  100. /*** And change window title to show file name */
  101.  
  102.   if (NewFile) strcpy(EditWnd[Id].name, FileName);
  103.  
  104.   return(TRUE);
  105. }
  106.  
  107.  
  108. /***********************************************************************
  109. * Routine to save a file                                               *
  110. ***********************************************************************/
  111.  
  112. BOOL SaveFile(int Id, BOOL UseName)
  113. { int handle, i;
  114.   long iL;
  115.   HDC hDC;
  116.  
  117. /*** Use loop to ensure the file is opened */
  118.  
  119.   handle = -1;
  120.   do
  121.   {
  122.  
  123. /*** If SAVE AS or no default name we have to get a file name */
  124.  
  125.     if (UseName && strcmp(EditWnd[Id].name, UNTITLED) != 0)
  126.     { strcpy(FileName, EditWnd[Id].name);
  127.     }
  128.     else
  129.     {
  130.  
  131. /*** Create dialog box to get file name */
  132.  
  133.       if (!GetSaveFileName((LPOPENFILENAME) &OpenFileInfo))
  134.         return(FALSE);
  135.       AnsiLower(FileName);
  136.  
  137. /*** Check file does not already exist */
  138.  
  139.       if ((i = _lopen(FileName, OF_READ)) != -1)
  140.       { _lclose(i);
  141.         sprintf(WorkStr, "Overwrite existing file %s?", FileName);
  142.         i = MessageBox(GetFocus(), WorkStr, "Save File", MB_YESNO | MB_ICONQUESTION);
  143.         if (i == IDNO) continue;
  144.       }
  145.     }
  146.  
  147. /*** Open file for output */
  148.  
  149.     if ((handle = _lcreat(FileName, 0)) == -1)
  150.     { sprintf(WorkStr, "Cannot create file %s", FileName);
  151.       Comment(WorkStr);
  152.       UseName = FALSE;
  153.     }
  154.   } while (handle == -1);
  155.  
  156. /*** Now write data to file */
  157.  
  158.   iL = GetWindowText(EditWnd[Id].hwnd, TransBuf, TRANS_BUF_SIZE);
  159.   _lwrite(handle, TransBuf, (WORD) iL);
  160.  
  161. /*** All done so close file */
  162.  
  163.   _lclose(handle);
  164.  
  165. /*** And change window title to show file name */
  166.  
  167.   if (!UseName)
  168.   { strcpy(EditWnd[Id].name, FileName);
  169.     hDC = GetDC(hMainWnd);
  170.     ReDrawStatusBar(hDC, Id);
  171.     ReleaseDC(hDC, hDC);
  172.   }
  173.  
  174.   return(TRUE);
  175. }
  176.  
  177.  
  178. /***********************************************************************
  179. * Routine to create an edit window                                     *
  180. ***********************************************************************/
  181.  
  182. BOOL CreateEditWindow(void)
  183. { int i;
  184.  
  185. /*** Check we have not reached limit */
  186.  
  187.   if (NumFiles == MAX_FILES)
  188.   { Comment("Maximum no. of windows already open");
  189.     return(FALSE);
  190.   }
  191.  
  192. /*** Allocate the new segment. This will be expanded later if needed. */
  193.  
  194.   EditWnd[NumFiles].ds = GlobalAlloc(GHND | GMEM_NOT_BANKED | GMEM_DDESHARE, 0x100L);
  195.   if (!EditWnd[NumFiles].ds)
  196.   { Comment("System error: Unable to allocate edit buffer");
  197.     return(FALSE);
  198.   }
  199.  
  200. /*** If the handle allocated is >= 0x2000 we can't use it since DS handles */
  201. /*** cannot be larger than this number.                                    */
  202.  
  203. /*
  204.   if ((unsigned)EditWnd[NumFiles].ds >= 0x2000)
  205.   { GlobalFree(EditWnd[NumFiles].ds);
  206.     EditWnd[NumFiles].ds = hInst;
  207.   }
  208. */
  209.  
  210. /*** Now create edit control */
  211.  
  212.   EditWnd[NumFiles].hwnd = CreateWindow("EDIT",
  213.                                         NULL,
  214.                                         WS_CHILD | WS_VISIBLE | ES_MULTILINE |
  215.                                         ES_AUTOHSCROLL | ES_AUTOVSCROLL |
  216.                                         ES_LEFT | ES_NOHIDESEL,
  217.                                         0,
  218.                                         0,
  219.                                         1,
  220.                                         1,
  221.                                         hMainWnd,
  222.                                         IDC_EDIT + NumFiles,
  223.                                         EditWnd[NumFiles].ds,
  224.                                         NULL);
  225.   if (!EditWnd[NumFiles].hwnd)
  226.   { Comment("System error: Cannot create new window");
  227.     GlobalFree(EditWnd[NumFiles].ds);
  228.     return(FALSE);
  229.   }
  230.  
  231.   SendMessage(EditWnd[NumFiles].hwnd, EM_LIMITTEXT, 0, 0L);
  232.   strcpy(EditWnd[NumFiles].name, UNTITLED);
  233.  
  234. /*** Subclass the control */
  235.  
  236.   EditWnd[NumFiles].proc = (FARPROC) SetWindowLong(EditWnd[NumFiles].hwnd, GWL_WNDPROC, (LONG) lpNewEditWndProc);
  237.  
  238. /*** And rearrange the edit windows */
  239.  
  240.   CurrentFile = NumFiles;
  241.   NumFiles++;
  242.   SendMessage(CurWnd, WM_SETFONT, GetStockObject(ANSI_FIXED_FONT), FALSE);
  243.   RearrangeEditWindows();
  244.  
  245. /*** Finally return indicating success */
  246.  
  247.   return(TRUE);
  248. }
  249.  
  250.  
  251. /***********************************************************************
  252. * Routine to arrange all open windows in the main window               *
  253. ***********************************************************************/
  254.  
  255. void RearrangeEditWindows(void)
  256. { int top, depth, i;
  257.   RECT wrect;
  258.  
  259. /*** Divide space between edit windows */
  260.  
  261.   GetClientRect(hMainWnd, &wrect);
  262.   depth = wrect.bottom/NumFiles - CharDp;
  263.  
  264. /*** And move windows to allocated space */
  265.  
  266.   top = 0;
  267.  
  268.   for (i = 0; i < NumFiles; i++)
  269.   { MoveWindow(EditWnd[i].hwnd, wrect.left, top, wrect.right, depth, TRUE);
  270.     EditWnd[i].status = top + depth;
  271.  
  272.     top += depth + CharDp;
  273.   }
  274.  
  275. /*** Trigger repaint for altered window */
  276.  
  277.   InvalidateRect(hMainWnd, &wrect, TRUE);
  278. }
  279.  
  280.  
  281. /***********************************************************************
  282. * Routine to redraw the status bar for an edit window                  *
  283. ***********************************************************************/
  284.  
  285. void RedrawStatusBar(HDC hDC, int Id)
  286. {
  287.   SetBkColor(hDC, RGB(0, 0, 0));
  288.   SetTextColor(hDC, RGB(255, 255, 255));
  289.  
  290.   SelectObject(hDC, GetStockObject(ANSI_FIXED_FONT));
  291.   sprintf(WorkStr, "  %-200s", EditWnd[Id].name);
  292.   TextOut(hDC, 0, EditWnd[Id].status, WorkStr, strlen(WorkStr));
  293. }
  294.  
  295.  
  296. /***********************************************************************
  297. * Routine to copy a string to the clipboard                            *
  298. ***********************************************************************/
  299.  
  300. BOOL SendToClipboard(LPSTR Str)
  301. { LONG   iL;
  302.   HANDLE hClipStr;
  303.   LPSTR  ClipStr;
  304.  
  305. /*** Allocate memory for clipboard transfer */
  306.  
  307.   iL = lstrlen(Str);
  308.  
  309.   if (!(hClipStr = GlobalAlloc(GMEM_MOVEABLE, iL + 1)))
  310.   { Comment("System error: Insufficient memory using clipboard");
  311.     return(FALSE);
  312.   }
  313.  
  314.   if (!(ClipStr = GlobalLock(hClipStr)))
  315.   { Comment("System error: Insufficient memory using clipboard");
  316.     GlobalFree(hClipStr);
  317.     return(FALSE);
  318.   }
  319.  
  320. /*** Copy string into allocated memory */
  321.  
  322.   lstrcpy(ClipStr, Str);
  323.   GlobalUnlock(hClipStr);
  324.  
  325. /*** Open clipboard */
  326.  
  327.   if (!OpenClipboard(hMainWnd))
  328.   { Comment("System error: Cannot open clipboard");
  329.     GlobalFree(hClipStr);
  330.     return(FALSE);
  331.   }
  332.  
  333. /*** And transfer data */
  334.  
  335.   EmptyClipboard();
  336.   SetClipboardData(CF_TEXT, hClipStr);
  337.   CloseClipboard();
  338.  
  339. /*** Return signalling success */
  340.  
  341.   return(TRUE);
  342. }
  343.  
  344.