home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************
- * Zap *
- * === *
- * *
- * Windows 3 Text Editor *
- * *
- * Window creation and management module *
- * BOOL ReadFile(int, BOOL); *
- * BOOL SaveFile(int, BOOL); *
- * *
- * BOOL CreateEditWindow(void); *
- * void RearrangeEditWindows(void); *
- * void RedrawStatusBar(HDC, int); *
- * *
- * BOOL SendToClipboard(LPSTR); *
- * *
- * This programme was developed using Microsoft C6.0 and the Microsoft *
- * SDK, however any ANSI C could be used if suitable libraries and the *
- * Windows header are available. *
- ***********************************************************************/
-
- #include <windows.h>
- #include <commdlg.h>
- #include <string.h>
- #include "zap.h"
- #include "zapdlg.h"
-
-
- /***********************************************************************
- * External variables *
- ***********************************************************************/
-
- extern HANDLE hInst;
- extern HWND hMainWnd;
-
- extern EDIT_WND EditWnd[MAX_FILES];
- extern int NumFiles, CurrentFile;
-
- extern FARPROC lpNewEditWndProc;
-
- extern LPSTR TransBuf;
-
- extern int CharWd, CharDp;
-
- extern OPENFILENAME OpenFileInfo;
- extern char FileName[MAX_DOS_NAME], FileTitle[MAX_DOS_NAME];
-
- extern char WorkStr[WORK_STR_LEN];
-
-
- /***********************************************************************
- * Routine to read a file *
- * *
- * Arguments *
- * Id - int, index of buffer to read file into *
- * NewFile - BOOL, if TRUE replace buffer contents else add to buffer *
- ***********************************************************************/
-
- BOOL ReadFile(int Id, BOOL NewFile)
- { int handle;
- long curpos, iL;
-
- /*** Create dialog box to get file name */
-
- if (!GetOpenFileName((LPOPENFILENAME) &OpenFileInfo))
- return FALSE;
- AnsiLower(FileName);
-
- /*** Open and read file */
-
- if ((handle = _lopen(FileName, OF_READ)) == -1)
- { sprintf(WorkStr, "Cannot open file \"%s\"", FileName);
- Comment(WorkStr);
- return(FALSE);
- }
-
- /*** If not inserting clear current data */
-
- if (NewFile) SetWindowText(EditWnd[Id].hwnd, "");
-
- /*** And read data */
-
- if ((iL = _lread(handle, TransBuf, TRANS_BUF_SIZE - 1)) == TRANS_BUF_SIZE - 1)
- Comment("Warning: file is too big and has been truncated");
- TransBuf[iL] = '\0';
-
- /*** Data is inserted by putting it in the clipboard then PASTEing */
-
- curpos = LOWORD(CallWindowProc(EditWnd[Id].proc, EditWnd[Id].hwnd, EM_GETSEL, 0, 0L));
-
- if (SendToClipboard(TransBuf))
- CallWindowProc(EditWnd[Id].proc, EditWnd[Id].hwnd, WM_PASTE, 0, 0L);
-
- CallWindowProc(EditWnd[Id].proc, EditWnd[Id].hwnd, EM_SETSEL, 0, MAKELONG(curpos, curpos));
-
- /*** All done so close file */
-
- _lclose(handle);
-
- /*** And change window title to show file name */
-
- if (NewFile) strcpy(EditWnd[Id].name, FileName);
-
- return(TRUE);
- }
-
-
- /***********************************************************************
- * Routine to save a file *
- ***********************************************************************/
-
- BOOL SaveFile(int Id, BOOL UseName)
- { int handle, i;
- long iL;
- HDC hDC;
-
- /*** Use loop to ensure the file is opened */
-
- handle = -1;
- do
- {
-
- /*** If SAVE AS or no default name we have to get a file name */
-
- if (UseName && strcmp(EditWnd[Id].name, UNTITLED) != 0)
- { strcpy(FileName, EditWnd[Id].name);
- }
- else
- {
-
- /*** Create dialog box to get file name */
-
- if (!GetSaveFileName((LPOPENFILENAME) &OpenFileInfo))
- return(FALSE);
- AnsiLower(FileName);
-
- /*** Check file does not already exist */
-
- if ((i = _lopen(FileName, OF_READ)) != -1)
- { _lclose(i);
- sprintf(WorkStr, "Overwrite existing file %s?", FileName);
- i = MessageBox(GetFocus(), WorkStr, "Save File", MB_YESNO | MB_ICONQUESTION);
- if (i == IDNO) continue;
- }
- }
-
- /*** Open file for output */
-
- if ((handle = _lcreat(FileName, 0)) == -1)
- { sprintf(WorkStr, "Cannot create file %s", FileName);
- Comment(WorkStr);
- UseName = FALSE;
- }
- } while (handle == -1);
-
- /*** Now write data to file */
-
- iL = GetWindowText(EditWnd[Id].hwnd, TransBuf, TRANS_BUF_SIZE);
- _lwrite(handle, TransBuf, (WORD) iL);
-
- /*** All done so close file */
-
- _lclose(handle);
-
- /*** And change window title to show file name */
-
- if (!UseName)
- { strcpy(EditWnd[Id].name, FileName);
- hDC = GetDC(hMainWnd);
- ReDrawStatusBar(hDC, Id);
- ReleaseDC(hDC, hDC);
- }
-
- return(TRUE);
- }
-
-
- /***********************************************************************
- * Routine to create an edit window *
- ***********************************************************************/
-
- BOOL CreateEditWindow(void)
- { int i;
-
- /*** Check we have not reached limit */
-
- if (NumFiles == MAX_FILES)
- { Comment("Maximum no. of windows already open");
- return(FALSE);
- }
-
- /*** Allocate the new segment. This will be expanded later if needed. */
-
- EditWnd[NumFiles].ds = GlobalAlloc(GHND | GMEM_NOT_BANKED | GMEM_DDESHARE, 0x100L);
- if (!EditWnd[NumFiles].ds)
- { Comment("System error: Unable to allocate edit buffer");
- return(FALSE);
- }
-
- /*** If the handle allocated is >= 0x2000 we can't use it since DS handles */
- /*** cannot be larger than this number. */
-
- /*
- if ((unsigned)EditWnd[NumFiles].ds >= 0x2000)
- { GlobalFree(EditWnd[NumFiles].ds);
- EditWnd[NumFiles].ds = hInst;
- }
- */
-
- /*** Now create edit control */
-
- EditWnd[NumFiles].hwnd = CreateWindow("EDIT",
- NULL,
- WS_CHILD | WS_VISIBLE | ES_MULTILINE |
- ES_AUTOHSCROLL | ES_AUTOVSCROLL |
- ES_LEFT | ES_NOHIDESEL,
- 0,
- 0,
- 1,
- 1,
- hMainWnd,
- IDC_EDIT + NumFiles,
- EditWnd[NumFiles].ds,
- NULL);
- if (!EditWnd[NumFiles].hwnd)
- { Comment("System error: Cannot create new window");
- GlobalFree(EditWnd[NumFiles].ds);
- return(FALSE);
- }
-
- SendMessage(EditWnd[NumFiles].hwnd, EM_LIMITTEXT, 0, 0L);
- strcpy(EditWnd[NumFiles].name, UNTITLED);
-
- /*** Subclass the control */
-
- EditWnd[NumFiles].proc = (FARPROC) SetWindowLong(EditWnd[NumFiles].hwnd, GWL_WNDPROC, (LONG) lpNewEditWndProc);
-
- /*** And rearrange the edit windows */
-
- CurrentFile = NumFiles;
- NumFiles++;
- SendMessage(CurWnd, WM_SETFONT, GetStockObject(ANSI_FIXED_FONT), FALSE);
- RearrangeEditWindows();
-
- /*** Finally return indicating success */
-
- return(TRUE);
- }
-
-
- /***********************************************************************
- * Routine to arrange all open windows in the main window *
- ***********************************************************************/
-
- void RearrangeEditWindows(void)
- { int top, depth, i;
- RECT wrect;
-
- /*** Divide space between edit windows */
-
- GetClientRect(hMainWnd, &wrect);
- depth = wrect.bottom/NumFiles - CharDp;
-
- /*** And move windows to allocated space */
-
- top = 0;
-
- for (i = 0; i < NumFiles; i++)
- { MoveWindow(EditWnd[i].hwnd, wrect.left, top, wrect.right, depth, TRUE);
- EditWnd[i].status = top + depth;
-
- top += depth + CharDp;
- }
-
- /*** Trigger repaint for altered window */
-
- InvalidateRect(hMainWnd, &wrect, TRUE);
- }
-
-
- /***********************************************************************
- * Routine to redraw the status bar for an edit window *
- ***********************************************************************/
-
- void RedrawStatusBar(HDC hDC, int Id)
- {
- SetBkColor(hDC, RGB(0, 0, 0));
- SetTextColor(hDC, RGB(255, 255, 255));
-
- SelectObject(hDC, GetStockObject(ANSI_FIXED_FONT));
- sprintf(WorkStr, " %-200s", EditWnd[Id].name);
- TextOut(hDC, 0, EditWnd[Id].status, WorkStr, strlen(WorkStr));
- }
-
-
- /***********************************************************************
- * Routine to copy a string to the clipboard *
- ***********************************************************************/
-
- BOOL SendToClipboard(LPSTR Str)
- { LONG iL;
- HANDLE hClipStr;
- LPSTR ClipStr;
-
- /*** Allocate memory for clipboard transfer */
-
- iL = lstrlen(Str);
-
- if (!(hClipStr = GlobalAlloc(GMEM_MOVEABLE, iL + 1)))
- { Comment("System error: Insufficient memory using clipboard");
- return(FALSE);
- }
-
- if (!(ClipStr = GlobalLock(hClipStr)))
- { Comment("System error: Insufficient memory using clipboard");
- GlobalFree(hClipStr);
- return(FALSE);
- }
-
- /*** Copy string into allocated memory */
-
- lstrcpy(ClipStr, Str);
- GlobalUnlock(hClipStr);
-
- /*** Open clipboard */
-
- if (!OpenClipboard(hMainWnd))
- { Comment("System error: Cannot open clipboard");
- GlobalFree(hClipStr);
- return(FALSE);
- }
-
- /*** And transfer data */
-
- EmptyClipboard();
- SetClipboardData(CF_TEXT, hClipStr);
- CloseClipboard();
-
- /*** Return signalling success */
-
- return(TRUE);
- }
-
-