home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************
- * Zap *
- * === *
- * *
- * Windows 3 Text Editor *
- * *
- * Sub-classing function *
- * long FAR PASCAL NewEditWndProc(HWND, unsigned, WORD, LONG); *
- * BOOL IsSeparator(char); *
- * BOOL IsNotSeparator(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 <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 char WorkStr[WORK_STR_LEN];
-
-
- /***********************************************************************
- * Global variables *
- ***********************************************************************/
-
- char LastDelete[WORK_STR_LEN];
-
-
- /***********************************************************************
- * Subclass procedure for the edit control. *
- ***********************************************************************/
-
- long FAR PASCAL NewEditWndProc(HWND hWnd, unsigned message, WORD wParam, LONG lParam )
- { int w_id, i;
- long curpos, endpos, maxpos, iL;
- char c;
- BOOL b;
- BYTE key_state[256];
- FARPROC lp_proc;
-
- /*** First identify the edit window */
-
- for (w_id = 0; w_id < NumFiles; w_id++)
- if (EditWnd[w_id].hwnd == hWnd) break;
-
- if (w_id == NumFiles)
- { MessageBeep(0);
- return(0L);
- }
-
- /*** Now process the message */
-
- switch (message)
- { case WM_KEYDOWN:
- GetKeyboardState((BYTE FAR *) key_state);
-
- switch (wParam)
- {
-
- /*** The word left action is modified */
-
- case VK_LEFT:
- if (CONTROL_DOWN)
- { GetWindowText(EditWnd[w_id].hwnd, TransBuf, TRANS_BUF_SIZE);
-
- iL = CallWindowProc(EditWnd[w_id].proc, hWnd, EM_GETSEL, 0, 0L);
- if ((curpos = LOWORD(iL)) == 0) return(0L);
- endpos = HIWORD(iL);
- curpos--;
-
- if (TransBuf[curpos] == '\n' && TransBuf[curpos-1] == '\r')
- { curpos--;
- }
- else
- { while (curpos > 0 && !IsNotSeparator(TransBuf[curpos]))
- curpos--;
- while (curpos > 0 && IsNotSeparator(TransBuf[curpos]))
- curpos--;
- curpos++;
- }
-
- if (!(SHIFT_DOWN)) endpos = curpos;
- CallWindowProc(EditWnd[w_id].proc, hWnd, EM_SETSEL, 0, MAKELONG(curpos, endpos));
- return(0L);
- }
- break;
-
- /*** The word right action is modified */
-
- case VK_RIGHT:
- if (CONTROL_DOWN)
- { GetWindowText(EditWnd[w_id].hwnd, TransBuf, TRANS_BUF_SIZE);
- maxpos = lstrlen(TransBuf);
-
- iL = CallWindowProc(EditWnd[w_id].proc, hWnd, EM_GETSEL, 0, 0L);
- if ((curpos = HIWORD(iL)) == maxpos - 1) return(0L);
- endpos = LOWORD(iL);
- curpos++;
-
- if (TransBuf[curpos] == '\r' && TransBuf[curpos+1] == '\n')
- { curpos++;
- }
- else
- { while (curpos > 0 && IsNotSeparator(TransBuf[curpos]))
- curpos++;
- while (curpos > 0 && !IsNotSeparator(TransBuf[curpos]))
- curpos++;
- }
-
- if (!(SHIFT_DOWN)) endpos = curpos;
- CallWindowProc(EditWnd[w_id].proc, hWnd, EM_SETSEL, 0, MAKELONG(endpos, curpos));
- return(0L);
- }
- break;
-
- /*** Control Up or Down means goto line number */
-
- case VK_DOWN:
- case VK_UP:
- if (CONTROL_DOWN)
- { lp_proc = MakeProcInstance(GotoLineProc, hInst);
- iL = DialogBoxParam(hInst, "GotoBox", hMainWnd, lp_proc, (LONG)w_id);
- FreeProcInstance(lp_proc);
-
- GotoLine(w_id, iL);
-
- return(0L);
- }
- break;
-
- /*** Control Page Up or Down means change window */
-
- case VK_NEXT:
- if (CONTROL_DOWN)
- { if (NumFiles > 1)
- { CurrentFile++;
- if (CurrentFile == NumFiles) CurrentFile = 0;
- SetFocus(CurWnd);
- }
-
- return(0L);
- }
- break;
-
- case VK_PRIOR:
- if (CONTROL_DOWN)
- { if (NumFiles > 1)
- { CurrentFile--;
- if (CurrentFile < 0) CurrentFile = NumFiles - 1;
- SetFocus(CurWnd);
- }
-
- return(0L);
- }
- break;
-
- /*** Control-Shift-Insert means insert the last deletion */
-
- case VK_INSERT:
- if (CONTROL_DOWN && SHIFT_DOWN)
- { if (LastDelete[0] != '\0')
- { if (SendToClipboard(LastDelete))
- { CallWindowProc(EditWnd[w_id].proc, hWnd, WM_PASTE, 0, 0L);
- }
- }
-
- return(0L);
- }
- break;
-
- /*** The delete key has a variety of meanings depending on control and shift */
-
- case VK_DELETE:
- if (CONTROL_DOWN)
- {
-
- /*** Control-Shift-Delete deletes the current line */
-
- if (SHIFT_DOWN)
- { GetWindowText(EditWnd[w_id].hwnd, TransBuf, TRANS_BUF_SIZE);
- maxpos = lstrlen(TransBuf);
- curpos = LOWORD(CallWindowProc(EditWnd[w_id].proc, hWnd, EM_GETSEL, 0, 0L));
-
- while (curpos > 0)
- { if (TransBuf[curpos-1] == '\n')
- if (TransBuf[curpos-2] == '\r')
- break;
- curpos--;
- }
-
- endpos = curpos;
- while (endpos < maxpos)
- { if (TransBuf[endpos] == '\r')
- { if (TransBuf[endpos+1] == '\n')
- { endpos += 2;
- break;
- }
- }
-
- endpos++;
- }
-
- CallWindowProc(EditWnd[w_id].proc, hWnd, EM_SETSEL, 0, MAKELONG(curpos, endpos));
- CallWindowProc(EditWnd[w_id].proc, hWnd, WM_CLEAR, 0, 0L);
-
- for (i = 0; i < endpos - curpos; i++)
- LastDelete[i] = TransBuf[curpos + i];
- LastDelete[i] = '\0';
-
- return(0L);
- }
-
- /*** Control-Delete deletes word right */
-
- else
- { GetWindowText(EditWnd[w_id].hwnd, TransBuf, TRANS_BUF_SIZE);
- maxpos = lstrlen(TransBuf);
- curpos = LOWORD(CallWindowProc(EditWnd[w_id].proc, hWnd, EM_GETSEL, 0, 0L));
- if (curpos == maxpos) return(0L);
-
- if (TransBuf[curpos] == '\r' && TransBuf[curpos + 1] == '\n')
- { endpos = curpos + 2;
- }
- else if (!IsSeparator(TransBuf[curpos]) && !IsNotSeparator(TransBuf[curpos]))
- { endpos = curpos + 1;
- }
- else
- { endpos = curpos;
- while (endpos < maxpos && IsNotSeparator(TransBuf[endpos]))
- endpos++;
- while (endpos < maxpos && IsSeparator(TransBuf[endpos]))
- endpos++;
- }
-
- CallWindowProc(EditWnd[w_id].proc, hWnd, EM_SETSEL, 0, MAKELONG(curpos, endpos));
- CallWindowProc(EditWnd[w_id].proc, hWnd, WM_CLEAR, 0, 0L);
-
- for (i = 0; i < endpos - curpos; i++)
- LastDelete[i] = TransBuf[curpos + i];
- LastDelete[i] = '\0';
-
- return(0L);
- }
- }
- }
- break;
-
- /*** Some keypresses can be trapped via WM_CHAR messages */
-
- case WM_CHAR:
- GetKeyboardState((BYTE FAR *) key_state);
-
- switch (wParam)
- {
-
- /*** Tab character is expanded to spaces */
-
- case VK_TAB:
- GetWindowText(EditWnd[w_id].hwnd, TransBuf, TRANS_BUF_SIZE);
- curpos = LOWORD(CallWindowProc(EditWnd[w_id].proc, hWnd, EM_GETSEL, 0, 0L));
-
- i = 0;
- while (curpos - i > 0 && TransBuf[curpos - (i + 1)] != '\n') i++;
- i = 8 - (i % 8);
-
- for (; i > 0; i--)
- CallWindowProc(EditWnd[w_id].proc, hWnd, WM_CHAR, (WORD) ' ', 0L);
- return(0L);
-
- /*** Return inserts spaces to match previous line */
-
- case VK_RETURN:
- GetWindowText(EditWnd[w_id].hwnd, TransBuf, TRANS_BUF_SIZE);
- curpos = LOWORD(CallWindowProc(EditWnd[w_id].proc, hWnd, EM_GETSEL, 0, 0L));
-
- while (curpos > 0 && TransBuf[curpos - 1] != '\n') curpos--;
- i = 0;
- while (TransBuf[curpos + i] == ' ') i++;
-
- CallWindowProc(EditWnd[w_id].proc, hWnd, WM_CHAR, VK_RETURN, lParam);
- for (; i > 0; i--)
- CallWindowProc(EditWnd[w_id].proc, hWnd, WM_CHAR, (WORD) ' ', 0L);
- return(0L);
-
- /*** 0x1A results from ^Z */
-
- case 0x1A:
- GetWindowText(EditWnd[w_id].hwnd, TransBuf, TRANS_BUF_SIZE);
- curpos = LOWORD(CallWindowProc(EditWnd[w_id].proc, hWnd, EM_GETSEL, 0, 0L));
-
- c = TransBuf[curpos];
- if (IsCharUpper(c))
- { AnsiLowerBuff(&c, 1);
- }
- else if (IsCharLower(c))
- { AnsiUpperBuff(&c, 1);;
- }
- else
- { CallWindowProc(EditWnd[w_id].proc, hWnd, EM_SETSEL, 0, MAKELONG(curpos + 1, curpos + 1));
- return(0L);
- }
-
- WorkStr[0] = c;
- WorkStr[1] = '\0';
- CallWindowProc(EditWnd[w_id].proc, hWnd, EM_SETSEL, 0, MAKELONG(curpos, curpos + 1));
- CallWindowProc(EditWnd[w_id].proc, hWnd, EM_REPLACESEL, 0, (LONG)WorkStr);
- CallWindowProc(EditWnd[w_id].proc, hWnd, EM_SETSEL, 0, MAKELONG(curpos + 1, curpos + 1));
-
- return(0L);
-
- /*** 0x7F results from ^BackSpace */
-
- case 0x7F:
- if (CONTROL_DOWN && !(SHIFT_DOWN))
- { endpos = LOWORD(CallWindowProc(EditWnd[w_id].proc, hWnd, EM_GETSEL, 0, 0L));
- if (endpos == 0) return(0L);
-
- GetWindowText(EditWnd[w_id].hwnd, TransBuf, TRANS_BUF_SIZE);
-
- curpos = endpos - 1;
- if (TransBuf[curpos] == '\n' && TransBuf[curpos-1] == '\r')
- { curpos--;
- }
- else if (IsSeparator(TransBuf[curpos]) || IsNotSeparator(TransBuf[curpos]))
- { while (curpos > 0 && IsSeparator(TransBuf[curpos]))
- curpos--;
- while (curpos > 0 && IsNotSeparator(TransBuf[curpos]))
- curpos--;
- curpos++;
- }
-
- CallWindowProc(EditWnd[w_id].proc, hWnd, EM_SETSEL, 0, MAKELONG(curpos, endpos));
- CallWindowProc(EditWnd[w_id].proc, hWnd, WM_CLEAR, 0, 0L);
-
- for (i = 0; i < endpos - curpos; i++)
- LastDelete[i] = TransBuf[curpos + i];
- LastDelete[i] = '\0';
-
- return(0L);
- }
- break;
-
- }
- break;
- }
-
- return(CallWindowProc(EditWnd[w_id].proc, hWnd, message, wParam, lParam));
- }
-
-
- /***********************************************************************
- * Routines for changing key states *
- ***********************************************************************/
-
- BOOL IsSeparator(char c)
- {
- if (c == ' ')
- return(TRUE);
- else
- return(FALSE);
- }
-
-
- BOOL IsNotSeparator(char c)
- {
- if (c >= 'A' && c <= 'Z')
- return(TRUE);
- else if (c >= 'a' && c <= 'z')
- return(TRUE);
- else
- return(FALSE);
- }
-
-