home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************
- * Zap *
- * === *
- * *
- * Windows 3 Text Editor *
- * *
- * This programme uses the Windows edit control as an editting engine, *
- * and hence is limited to 32K - 1 characters. The edit control is *
- * sub-classed to provide additional functionality. *
- * *
- * Main module *
- * int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int); *
- * BOOL InitApplication(HANDLE); *
- * BOOL InitInstance(HANDLE, int); *
- * long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG); *
- * BOOL FAR PASCAL AboutProc(HWND, unsigned, WORD, LONG); *
- * void Comment(char *); *
- * *
- * 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"
-
-
- /***********************************************************************
- * Global variables *
- ***********************************************************************/
-
- /*** System variables */
-
- HANDLE hInst;
- HWND hMainWnd;
-
- /*** Edit windows */
-
- EDIT_WND EditWnd[MAX_FILES];
- int NumFiles, CurrentFile;
-
- /*** Pointer to sub-classing function */
-
- FARPROC lpNewEditWndProc;
-
- /*** Buffer for manipulating window contents */
-
- HANDLE hTransBuf;
- LPSTR TransBuf;
-
- /*** Dialog box variables */
-
- OPENFILENAME OpenFileInfo;
- char FilterSpec[128] = "All Files (*.*)\0*.*\0"
- "Text Files (*.TXT)\0*.TXT\0"
- "C programs (*.C)\0*.C\0";
-
- char FileName[MAX_DOS_NAME], FileTitle[MAX_DOS_NAME];
-
- /*** Miscellaneous variables */
-
- int CharWd, CharDp;
-
- char WorkStr[WORK_STR_LEN];
-
-
- /***********************************************************************
- * Entry point from Windows *
- ***********************************************************************/
-
- int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow )
- { WORD i, j;
- HDC hDC;
- MSG msg;
-
- /*** Initialise application */
-
- if (!hPrevInstance)
- { if (!InitApplication(hInstance))
- { Comment("System error: Cannot register window class");
- return(FALSE);
- }
- }
-
- if (!InitInstance(hInstance, nCmdShow)) return(FALSE);
-
- /*** If a file name was given open it */
-
- if (lpszCmdLine[0] != '\0')
- { for (i = 0; lpszCmdLine[i] == ' '; i++);
- for (j = 0; lpszCmdLine[i] != ' ' && lpszCmdLine[i] != '\0'; j++, i++)
- FileName[j] = lpszCmdLine[i];
- FileName[j] = '\0';
-
- if ((j = _lopen(FileName, OF_READ)) == -1)
- { sprintf(WorkStr, "Cannot open file \"%s\"", FileName);
- Comment(WorkStr);
- }
- else
- { if ((i = _lread(j, TransBuf, TRANS_BUF_SIZE - 1)) == TRANS_BUF_SIZE - 1)
- Comment("Warning: file is too big and has been truncated");
- TransBuf[i] = '\0';
-
- _lclose(j);
-
- SetWindowText(EditWnd[0].hwnd, TransBuf);
- strcpy(EditWnd[0].name, FileName);
-
- hDC = GetDC(hMainWnd);
- RedrawStatusBar(hDC, 0);
- ReleaseDC(hMainWnd, hDC);
- }
- }
-
- /*** Enter message loop */
-
- while (GetMessage(&msg, NULL, NULL, NULL))
- { TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
-
- return (msg.wParam);
- }
-
-
- /***********************************************************************
- * Application initialisation routine *
- * Creates window class for application *
- ***********************************************************************/
-
- BOOL InitApplication(HANDLE hInstance)
- { WNDCLASS wc;
-
- wc.style = CS_OWNDC;
- wc.lpfnWndProc = MainWndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon(hInstance, "ZapIcon");
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = GetStockObject(WHITE_BRUSH);
- wc.lpszMenuName = "ZapMenu";
- wc.lpszClassName = "ZapWClass";
-
- return (RegisterClass(&wc));
- }
-
-
- /****************************************************************************
- * Instance initialisation routine
- ****************************************************************************/
-
- BOOL InitInstance(HANDLE hInstance, int nCmdShow)
- { int i;
- HDC hDC;
- RECT wrect;
- TEXTMETRIC tm;
-
- hInst = hInstance;
-
- /*** Allocate the file transfer buffer */
-
- if (!(hTransBuf = GlobalAlloc(GMEM_MOVEABLE, TRANS_BUF_SIZE)))
- { Comment("System error: Insufficient memory for buffers");
- return(FALSE);
- }
-
- if (!(TransBuf = GlobalLock(hTransBuf)))
- { Comment("System error: Insufficient memory to lock buffers");
- return(FALSE);
- }
-
- /*** Create instance for the subclassing procedure */
-
- lpNewEditWndProc = MakeProcInstance((FARPROC)NewEditWndProc, hInst);
-
- /*** Create window and get context */
-
- hMainWnd = CreateWindow("ZapWClass",
- "Zap",
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- NULL,
- NULL,
- hInstance,
- NULL);
- if (!hMainWnd) return (FALSE);
-
- /*** Create the edit control */
-
- NumFiles = 0;
-
- if (!CreateEditWindow())
- { DestroyWindow(hMainWnd);
- return(FALSE);
- }
-
- /*** Resize main window and centre it on screen */
-
- hDC = GetDC(hMainWnd);
-
- SelectObject(hDC, GetStockObject(ANSI_FIXED_FONT));
- GetTextMetrics(hDC, &tm);
- CharWd = tm.tmAveCharWidth;
- CharDp = tm.tmHeight;
-
- GetWindowRect(hMainWnd, &wrect);
- srand(wrect.left);
-
- wrect.right = CharWd*INITIAL_WIDTH
- + 2*GetSystemMetrics(SM_CXFRAME);
-
- wrect.bottom = CharDp*INITIAL_DEPTH
- + 2*GetSystemMetrics(SM_CYFRAME)
- + GetSystemMetrics(SM_CYCAPTION)
- + GetSystemMetrics(SM_CYMENU);
-
- wrect.left = rand() % (GetDeviceCaps(hDC, HORZRES) - wrect.right);
- wrect.top = rand() % (GetDeviceCaps(hDC, VERTRES) - wrect.bottom);
- MoveWindow(hMainWnd, wrect.left, wrect.top, wrect.right, wrect.bottom, FALSE);
-
- ReleaseDC(hMainWnd, hDC);
-
- /*** File dialog box info structure */
-
- OpenFileInfo.lStructSize = sizeof(OPENFILENAME);
- OpenFileInfo.hwndOwner = hMainWnd;
- OpenFileInfo.lpstrFilter = FilterSpec;
- OpenFileInfo.lpstrCustomFilter = NULL;
- OpenFileInfo.nMaxCustFilter = 0;
- OpenFileInfo.nFilterIndex = 1;
- OpenFileInfo.lpstrFile = FileName;
- OpenFileInfo.nMaxFile = MAX_DOS_NAME;
- OpenFileInfo.lpstrInitialDir = NULL;
- OpenFileInfo.lpstrFileTitle = FileTitle;
- OpenFileInfo.nMaxFileTitle = MAX_DOS_NAME;
- OpenFileInfo.lpstrTitle = NULL;
- OpenFileInfo.lpstrDefExt = "TXT";
- OpenFileInfo.Flags = 0;
-
- /*** Display window and return*/
-
- ShowWindow(hMainWnd, nCmdShow);
- UpdateWindow(hMainWnd);
-
- return(TRUE);
- }
-
-
- /***********************************************************************
- * Message handler for main window *
- ***********************************************************************/
-
- long FAR PASCAL MainWndProc(HWND hWnd, unsigned message, WORD wParam, LONG lParam)
- { int i;
- FARPROC lp_proc;
- HDC hDC;
- PAINTSTRUCT ps;
-
- switch (message)
- { case WM_COMMAND:
- switch (wParam)
- {
-
- /*** Create a new file */
-
- case IDM_FILE_NEW:
- if (SendMessage(CurWnd, EM_GETMODIFY, 0, 0L))
- { sprintf(WorkStr, "Save %s", EditWnd[CurrentFile].name);
- i = MessageBox(GetFocus(), WorkStr, "Zap", MB_YESNOCANCEL | MB_ICONQUESTION);
- if (i == IDCANCEL)
- break;
- if (i == IDYES)
- SaveFile(CurrentFile, TRUE);
- }
-
- SetWindowText(CurWnd, "");
- SendMessage(CurWnd, EM_SETMODIFY, 0, 0L);
- strcpy(EditWnd[CurrentFile].name, UNTITLED);
-
- hDC = GetDC(hMainWnd);
- RedrawStatusBar(hDC, CurrentFile);
- ReleaseDC(hMainWnd, hDC);
-
- break;
-
- /*** Open a file */
-
- case IDM_FILE_OPEN:
- if (SendMessage(CurWnd, EM_GETMODIFY, 0, 0L))
- { sprintf(WorkStr, "Save %s", EditWnd[CurrentFile].name);
- i = MessageBox(GetFocus(), WorkStr, "Zap", MB_YESNOCANCEL | MB_ICONQUESTION);
- if (i == IDCANCEL)
- break;
- if (i == IDYES)
- SaveFile(CurrentFile, TRUE);
- }
-
- ReadFile(CurrentFile, TRUE);
- SendMessage(CurWnd, EM_SETMODIFY, 0, 0L);
-
- hDC = GetDC(hMainWnd);
- RedrawStatusBar(hDC, CurrentFile);
- ReleaseDC(hMainWnd, hDC);
-
- break;
-
- /*** Save the file using the current name */
-
- case IDM_FILE_SAVE:
- SaveFile(CurrentFile, TRUE);
- SendMessage(CurWnd, EM_SETMODIFY, 0, 0L);
- break;
-
- /*** Save the file supplying a name */
-
- case IDM_FILE_SAVEAS:
- SaveFile(CurrentFile, FALSE);
- SendMessage(CurWnd, EM_SETMODIFY, 0, 0L);
- break;
-
- /*** Insert the contents of a file */
-
- case IDM_FILE_INSERT:
- ReadFile(CurrentFile, FALSE);
- SendMessage(CurWnd, EM_SETMODIFY, 1, 0L);
- break;
-
- /*** Print a file */
-
- case IDM_FILE_PRINT:
- PrintFile(CurrentFile);
- break;
-
- /*** Exit */
-
- case IDM_FILE_EXIT:
- for (i = 0; i < NumFiles; i++)
- { if (SendMessage(EditWnd[i].hwnd, EM_GETMODIFY, 0, 0L))
- { sprintf(WorkStr, "Save %s", EditWnd[i].name);
- i = MessageBox(GetFocus(), WorkStr, "Zap", MB_YESNOCANCEL | MB_ICONQUESTION);
- if (i == IDCANCEL)
- return(0L);
- if (i == IDYES)
- SaveFile(CurrentFile, TRUE);
- }
- }
-
- DestroyWindow(hMainWnd);
- break;
-
- /*** Traditional about box */
-
- case IDM_FILE_ABOUT:
- lp_proc = MakeProcInstance(AboutProc, hInst);
- DialogBox(hInst, "AboutBox", hWnd, lp_proc);
- FreeProcInstance(lp_proc);
- break;
-
- /*** The edit commands are mostly passed straight on to the controls */
-
- case IDM_EDIT_UNDO:
- CallWindowProc(CurWndProc, CurWnd, EM_UNDO, 0, 0L);
- break;
-
- case IDM_EDIT_CUT:
- CallWindowProc(CurWndProc, CurWnd, WM_CUT, 0, 0L);
- break;
-
- case IDM_EDIT_COPY:
- CallWindowProc(CurWndProc, CurWnd, WM_COPY, 0, 0L);
- break;
-
- case IDM_EDIT_PASTE:
- CallWindowProc(CurWndProc, CurWnd, WM_PASTE, 0, 0L);
- break;
-
- case IDM_EDIT_CLEAR:
- CallWindowProc(CurWndProc, CurWnd, WM_CLEAR, 0, 0L);
- break;
-
- case IDM_EDIT_SELECTALL:
- CallWindowProc(CurWndProc, CurWnd, EM_SETSEL, 0, MAKELONG(0, 0X7FFF));
- break;
-
- /*** Search file for string */
-
- case IDM_EDIT_SEARCH:
- lp_proc = MakeProcInstance(SearchProc, hInst);
- DialogBox(hInst, "SearchBox", hWnd, lp_proc);
- FreeProcInstance(lp_proc);
- break;
-
- /*** Replace string in file */
-
- case IDM_EDIT_REPLACE:
- lp_proc = MakeProcInstance(ReplaceProc, hInst);
- DialogBox(hInst, "ReplaceBox", hWnd, lp_proc);
- FreeProcInstance(lp_proc);
- break;
-
- /*** Open a new window */
-
- case IDM_WINDOW_OPEN:
- if (CreateEditWindow())
- { ReadFile(CurrentFile, TRUE);
- SendMessage(CurWnd, EM_SETMODIFY, 0, 0L);
- RearrangeEditWindows();
- }
- break;
-
- /*** Close a window */
-
- case IDM_WINDOW_CLOSE:
- if (NumFiles == 1)
- { PostMessage(hWnd, WM_COMMAND, IDM_FILE_EXIT, 0L);
- break;
- }
-
- if (SendMessage(CurWnd, EM_GETMODIFY, 0, 0L))
- { sprintf(WorkStr, "Save %s", EditWnd[CurrentFile].name);
- i = MessageBox(GetFocus(), WorkStr, "Zap", MB_YESNOCANCEL | MB_ICONQUESTION);
- if (i == IDCANCEL)
- break;
- if (i == IDYES)
- SaveFile(CurrentFile, TRUE);
- }
-
- DestroyWindow(CurWnd);
- GlobalFree(EditWnd[CurrentFile].ds);
- for (i = CurrentFile + 1; i < NumFiles; i++)
- EditWnd[i-1] = EditWnd[i];
- NumFiles--;
-
- if (CurrentFile == NumFiles) CurrentFile--;
- RearrangeEditWindows();
- SetFocus(CurWnd);
- break;
-
- /*** Finally check for messages from edit controls */
-
- default:
- for (i = 0; i < NumFiles; i++)
- { if (wParam == IDC_EDIT + i)
- { switch (HIWORD(lParam))
- { case EN_ERRSPACE:
- sprintf(WorkStr, "%s is too big for Zap; file has been truncated", EditWnd[i].name);
- Comment(WorkStr);
- break;
-
- case EN_SETFOCUS:
- CurrentFile = i;
- break;
- }
- }
- }
- break;
- }
- break;
-
- /*** The only painting needed is the status bars */
-
- case WM_PAINT:
- hDC = BeginPaint(hWnd, &ps);
- for (i = 0; i < NumFiles; i++) RedrawStatusBar(hDC, i);
- EndPaint(hWnd, &ps);
- break;
-
- /*** Adjust window size down to nearest whole number of characters */
-
- case WM_SIZE:
- if (IsWindowVisible(hWnd))
- RearrangeEditWindows();
- break;
-
- /*** Pass the focus on to the current edit window */
-
- case WM_SETFOCUS:
- SetFocus(CurWnd);
- break;
-
- /*** On closure check all files are saved */
-
- case WM_CLOSE:
- for (i = 0; i < NumFiles; i++)
- { if (SendMessage(EditWnd[i].hwnd, EM_GETMODIFY, 0, 0L))
- { sprintf(WorkStr, "Save %s", EditWnd[i].name);
- i = MessageBox(GetFocus(), WorkStr, "Zap", MB_YESNOCANCEL | MB_ICONQUESTION);
- if (i == IDCANCEL)
- return(0L);;
- if (i == IDYES)
- SaveFile(CurrentFile, TRUE);
- }
- }
- return DefWindowProc( hWnd, message, wParam, lParam );
-
- /*** If window destroyed quit */
-
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
-
- /*** Default action */
-
- default:
- return DefWindowProc( hWnd, message, wParam, lParam );
- }
-
- return(0L);
- }
-
-
- /***********************************************************************
- * Message handler for About dialog box *
- ***********************************************************************/
-
- BOOL FAR PASCAL AboutProc(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
- {
- switch (message)
- { case WM_INITDIALOG:
- return (TRUE);
-
- case WM_COMMAND:
- if (wParam == IDOK)
- { EndDialog(hDlg, TRUE);
- return (TRUE);
- }
- break;
- }
-
- return (FALSE);
- }
-
-
- /***********************************************************************
- * Message routine *
- ***********************************************************************/
-
- void Comment(char *str)
- {
- MessageBox(GetFocus(),
- str,
- "Zap",
- MB_ICONEXCLAMATION | MB_OK);
- }
-
-