home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GP_MSWTX.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  21KB  |  780 lines

  1. /* Copyright (C) 1993  Russell Lang
  2.  
  3. Permission to use, copy, modify, distribute, and sell this
  4. software and its documentation for any purpose is hereby granted
  5. without fee, provided that the above copyright notice and this
  6. permission notice appear in all copies of the software and related
  7. documentation.
  8. */
  9.  
  10. /* gp_mswtx.c */
  11. /*
  12.  * Microsoft Windows 3.n text window for Ghostscript.
  13.  * Original version by Russell Lang
  14.  */
  15.  
  16. #include <stdlib.h>
  17. #include "string_.h"    /* use only far items */
  18. #include "memory_.h"
  19. #include "dos_.h"
  20. #include "ctype_.h"
  21.  
  22. #define STRICT
  23. #include "windows_.h"
  24. #include <windowsx.h>
  25. #if WINVER >= 0x030a
  26. #include <commdlg.h>
  27. #include <shellapi.h>
  28. #endif
  29.  
  30. #include "gp_mswin.h"
  31. #include "gp_mswtx.h"
  32.  
  33. /* sysmenu */
  34. #define M_COPY_CLIP 1
  35. /* font stuff */
  36. #define TEXTFONTSIZE 9
  37. #define TEXTFONTNAME "Terminal"
  38.  
  39. /* limits */
  40. POINT ScreenMinSize = {16,4};
  41. LRESULT CALLBACK _export WndTextProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  42.  
  43. char szNoMemory[] = "out of memory";
  44. LPSTR szTextClass = "gstext_class";
  45.  
  46. void
  47. TextMessage(void)
  48. {
  49.     MSG msg;
  50.     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  51.     {
  52.     TranslateMessage(&msg);
  53.     DispatchMessage(&msg);
  54.     }
  55.     return;
  56. }
  57.  
  58.  
  59. void
  60. CreateTextClass(LPTW lptw)
  61. {
  62. WNDCLASS wndclass;
  63. static BOOL init = FALSE;
  64.     if (init)
  65.         return;
  66.     wndclass.style = CS_HREDRAW | CS_VREDRAW;
  67.     wndclass.lpfnWndProc = WndTextProc;
  68.     wndclass.cbClsExtra = 0;
  69.     wndclass.cbWndExtra = sizeof(void FAR *);
  70.     wndclass.hInstance = lptw->hInstance;
  71.     if (lptw->hIcon)
  72.         wndclass.hIcon = lptw->hIcon;
  73.     else
  74.         wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  75.     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  76.     wndclass.hbrBackground = GetStockBrush(WHITE_BRUSH);
  77.     wndclass.lpszMenuName = NULL;
  78.     wndclass.lpszClassName = szTextClass;
  79.     RegisterClass(&wndclass);
  80.     init = TRUE;
  81. }
  82.  
  83.  
  84. /* make text window */
  85. int
  86. TextInit(LPTW lptw)
  87. {
  88.     HMENU sysmenu;
  89.     HGLOBAL hglobal;
  90.     
  91.     if (!lptw->hPrevInstance)
  92.         CreateTextClass(lptw);
  93.  
  94.     if (lptw->KeyBufSize == 0)
  95.         lptw->KeyBufSize = 256;
  96.  
  97.     if (lptw->ScreenSize.x < ScreenMinSize.x)
  98.         lptw->ScreenSize.x = ScreenMinSize.x;
  99.     if (lptw->ScreenSize.y < ScreenMinSize.y)
  100.         lptw->ScreenSize.y = ScreenMinSize.y;
  101.  
  102.     lptw->CursorPos.x = lptw->CursorPos.y = 0;
  103.     lptw->bFocus = FALSE;
  104.     lptw->bGetCh = FALSE;
  105.     lptw->CaretHeight = 0;
  106.     if (!lptw->nCmdShow)
  107.         lptw->nCmdShow = SW_SHOWNORMAL;
  108.  
  109.     hglobal = GlobalAlloc(GHND, lptw->ScreenSize.x * lptw->ScreenSize.y);
  110.     lptw->ScreenBuffer = (BYTE FAR *)GlobalLock(hglobal);
  111.     if (lptw->ScreenBuffer == (BYTE FAR *)NULL) {
  112.         MessageBox((HWND)NULL,szNoMemory,(LPSTR)NULL, MB_ICONHAND | MB_SYSTEMMODAL);
  113.         return(1);
  114.     }
  115.     _fmemset(lptw->ScreenBuffer, ' ', lptw->ScreenSize.x * lptw->ScreenSize.y);
  116.     hglobal = GlobalAlloc(LHND, lptw->KeyBufSize);
  117.     lptw->KeyBuf = (BYTE FAR *)GlobalLock(hglobal);
  118.     if (lptw->KeyBuf == (BYTE FAR *)NULL) {
  119.         MessageBox((HWND)NULL,szNoMemory,(LPSTR)NULL, MB_ICONHAND | MB_SYSTEMMODAL);
  120.         return(1);
  121.     }
  122.     lptw->KeyBufIn = lptw->KeyBufOut = lptw->KeyBuf;
  123.  
  124.     lptw->hWndText = CreateWindow(szTextClass, lptw->Title,
  125.           WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
  126.           CW_USEDEFAULT, CW_USEDEFAULT,
  127.           CW_USEDEFAULT, CW_USEDEFAULT,
  128.           NULL, NULL, lptw->hInstance, lptw);
  129.     if (lptw->hWndText == (HWND)NULL) {
  130.         MessageBox((HWND)NULL,"Couldn't open text window",(LPSTR)NULL, MB_ICONHAND | MB_SYSTEMMODAL);
  131.         return(1);
  132.     }
  133.     ShowWindow(lptw->hWndText, lptw->nCmdShow);
  134.     sysmenu = GetSystemMenu(lptw->hWndText,0);    /* get the sysmenu */
  135.     AppendMenu(sysmenu, MF_SEPARATOR, 0, NULL);
  136.     AppendMenu(sysmenu, MF_STRING, M_COPY_CLIP, "Copy to Clip&board");
  137.  
  138.     return(0);
  139. }
  140.  
  141. /* close a text window */
  142. void
  143. TextClose(LPTW lptw)
  144. {
  145.     HGLOBAL hglobal;
  146.  
  147.     /* close window */
  148.     if (lptw->hWndText)
  149.         DestroyWindow(lptw->hWndText);
  150.     TextMessage();
  151.  
  152.     hglobal = (HGLOBAL)GlobalHandle( SELECTOROF(lptw->ScreenBuffer) );
  153.     if (hglobal) {
  154.         GlobalUnlock(hglobal);
  155.         GlobalFree(hglobal);
  156.     }
  157.     hglobal = (HGLOBAL)GlobalHandle( SELECTOROF(lptw->KeyBuf) );
  158.     if (hglobal) {
  159.         GlobalUnlock(hglobal);
  160.         GlobalFree(hglobal);
  161.     }
  162.     lptw->hWndText = (HWND)NULL;
  163. }
  164.     
  165.  
  166. /* Bring Cursor into text window */
  167. void
  168. TextToCursor(LPTW lptw)
  169. {
  170. int nXinc=0;
  171. int nYinc=0;
  172. int cxCursor;
  173. int cyCursor;
  174.     cyCursor = lptw->CursorPos.y * lptw->CharSize.y;
  175.     if ( (cyCursor + lptw->CharSize.y > lptw->ScrollPos.y + lptw->ClientSize.y) 
  176.       || (cyCursor < lptw->ScrollPos.y) ) {
  177.         nYinc = max(0, cyCursor + lptw->CharSize.y - lptw->ClientSize.y) - lptw->ScrollPos.y;
  178.         nYinc = min(nYinc, lptw->ScrollMax.y - lptw->ScrollPos.y);
  179.     }
  180.     cxCursor = lptw->CursorPos.x * lptw->CharSize.x;
  181.     if ( (cxCursor + lptw->CharSize.x > lptw->ScrollPos.x + lptw->ClientSize.x)
  182.       || (cxCursor < lptw->ScrollPos.x) ) {
  183.         nXinc = max(0, cxCursor + lptw->CharSize.x - lptw->ClientSize.x/2) - lptw->ScrollPos.x;
  184.         nXinc = min(nXinc, lptw->ScrollMax.x - lptw->ScrollPos.x);
  185.     }
  186.     if (nYinc || nXinc) {
  187.         lptw->ScrollPos.y += nYinc;
  188.         lptw->ScrollPos.x += nXinc;
  189.         ScrollWindow(lptw->hWndText,-nXinc,-nYinc,NULL,NULL);
  190.         SetScrollPos(lptw->hWndText,SB_VERT,lptw->ScrollPos.y,TRUE);
  191.         SetScrollPos(lptw->hWndText,SB_HORZ,lptw->ScrollPos.x,TRUE);
  192.         UpdateWindow(lptw->hWndText);
  193.     }
  194. }
  195.  
  196. void
  197. NewLine(LPTW lptw)
  198. {
  199.     lptw->CursorPos.x = 0;
  200.     lptw->CursorPos.y++;
  201.     if (lptw->CursorPos.y >= lptw->ScreenSize.y) {
  202.         int i =  lptw->ScreenSize.x * (lptw->ScreenSize.y - 1);
  203.         _fmemmove(lptw->ScreenBuffer, lptw->ScreenBuffer+lptw->ScreenSize.x, i);
  204.         _fmemset(lptw->ScreenBuffer + i, ' ', lptw->ScreenSize.x);
  205.         lptw->CursorPos.y--;
  206.         ScrollWindow(lptw->hWndText,0,-lptw->CharSize.y,NULL,NULL);
  207.         UpdateWindow(lptw->hWndText);
  208.     }
  209.     if (lptw->CursorFlag)
  210.         TextToCursor(lptw);
  211.     TextMessage();
  212. }
  213.  
  214. /* Update count characters in window at cursor position */
  215. /* Updates cursor position */
  216. void
  217. UpdateText(LPTW lptw, int count)
  218. {
  219. HDC hdc;
  220. int xpos, ypos;
  221.     xpos = lptw->CursorPos.x*lptw->CharSize.x - lptw->ScrollPos.x;
  222.     ypos = lptw->CursorPos.y*lptw->CharSize.y - lptw->ScrollPos.y;
  223.     hdc = GetDC(lptw->hWndText);
  224.     SelectFont(hdc, lptw->hfont);
  225.     TextOut(hdc,xpos,ypos,
  226.         (LPSTR)(lptw->ScreenBuffer + lptw->CursorPos.y*lptw->ScreenSize.x + 
  227.         lptw->CursorPos.x), count);
  228.     (void)ReleaseDC(lptw->hWndText,hdc);
  229.     lptw->CursorPos.x += count;
  230.     if (lptw->CursorPos.x >= lptw->ScreenSize.x)
  231.         NewLine(lptw);
  232. }
  233.  
  234. int
  235. TextPutCh(LPTW lptw, BYTE ch)
  236. {
  237. int pos;
  238.     switch(ch) {
  239.         case '\r':
  240.             lptw->CursorPos.x = 0;
  241.             if (lptw->CursorFlag)
  242.                 TextToCursor(lptw);
  243.             break;
  244.         case '\n':
  245.             NewLine(lptw);
  246.             break;
  247.         case 7:
  248.             MessageBeep(-1);
  249.             if (lptw->CursorFlag)
  250.                 TextToCursor(lptw);
  251.             break;
  252.         case '\t':
  253.             {
  254.             int n;
  255.                 for ( n = 8 - (lptw->CursorPos.x % 8); n>0; n-- )
  256.                     TextPutCh(lptw, ' ');
  257.             }
  258.             break;
  259.         case 0x08:
  260.         case 0x7f:
  261.             lptw->CursorPos.x--;
  262.             if (lptw->CursorPos.x < 0) {
  263.                 lptw->CursorPos.x = lptw->ScreenSize.x - 1;
  264.                 lptw->CursorPos.y--;
  265.             }
  266.             if (lptw->CursorPos.y < 0)
  267.                 lptw->CursorPos.y = 0;
  268.             break;
  269.         default:
  270.             pos = lptw->CursorPos.y*lptw->ScreenSize.x + lptw->CursorPos.x;
  271.             lptw->ScreenBuffer[pos] = ch;
  272.             UpdateText(lptw, 1);
  273.     }
  274.     return ch;
  275. }
  276.  
  277. void 
  278. TextWriteBuf(LPTW lptw, LPSTR str, int cnt)
  279. {
  280. BYTE FAR *p;
  281. int count, limit;
  282.     while (cnt>0) {
  283.     p = lptw->ScreenBuffer + lptw->CursorPos.y*lptw->ScreenSize.x + lptw->CursorPos.x;
  284.     limit = lptw->ScreenSize.x - lptw->CursorPos.x;
  285.     for (count=0; (count < limit) && (cnt>0) && (isprint(*str) || *str=='\t'); count++) {
  286.         if (*str=='\t') {
  287.         int n;
  288.         for ( n = 8 - ((lptw->CursorPos.x+count) % 8); (count < limit) & (n>0); n--, count++ ) {
  289.             *p++ = ' ';
  290.         }
  291.         str++;
  292.         count--;
  293.            }
  294.         else {
  295.         *p++ = *str++;
  296.         }
  297.         cnt--;
  298.     }
  299.     if (count>0) {
  300.         UpdateText(lptw, count);
  301.     }
  302.     if (cnt > 0) {
  303.         if (*str=='\n') {
  304.         NewLine(lptw);
  305.         str++;
  306.         cnt--;
  307.         }
  308.         else if (!isprint(*str) && *str!='\t') {
  309.         TextPutCh(lptw, *str++);
  310.         cnt--;
  311.         }
  312.     }
  313.     }
  314. }
  315.  
  316.  
  317. /* TRUE if key hit, FALSE if no key */
  318. int
  319. TextKBHit(LPTW lptw)
  320. {
  321.     return (lptw->KeyBufIn != lptw->KeyBufOut);
  322. }
  323.  
  324. /* get character from keyboard, no echo */
  325. /* need to add extended codes */
  326. int
  327. TextGetCh(LPTW lptw)
  328. {
  329.     int ch;
  330.     TextToCursor(lptw);
  331.     lptw->bGetCh = TRUE;
  332.     if (lptw->bFocus) {
  333.         SetCaretPos(lptw->CursorPos.x*lptw->CharSize.x - lptw->ScrollPos.x,
  334.             lptw->CursorPos.y*lptw->CharSize.y + lptw->CharAscent 
  335.             - lptw->CaretHeight - lptw->ScrollPos.y);
  336.         ShowCaret(lptw->hWndText);
  337.     }
  338.     do {
  339.         TextMessage();
  340.     } while (!TextKBHit(lptw));
  341.     ch = *lptw->KeyBufOut++;
  342.     if (ch=='\r')
  343.         ch = '\n';
  344.     if (lptw->KeyBufOut - lptw->KeyBuf >= lptw->KeyBufSize)
  345.         lptw->KeyBufOut = lptw->KeyBuf;    /* wrap around */
  346.     if (lptw->bFocus)
  347.         HideCaret(lptw->hWndText);
  348.     lptw->bGetCh = FALSE;
  349.     return ch;
  350. }
  351.  
  352. #if WINVER >= 0x030a
  353. /* Windows 3.1 drag-drop feature */
  354. char szFile[80];
  355. void
  356. DragFunc(LPTW lptw, HDROP hdrop)
  357. {
  358.     int i, cFiles;
  359.     LPSTR p;
  360.     if ( (lptw->DragPre==(LPSTR)NULL) || (lptw->DragPost==(LPSTR)NULL) )
  361.         return;
  362.     cFiles = DragQueryFile(hdrop, 0xffff, (LPSTR)NULL, 0);
  363.     for (i=0; i<cFiles; i++) {
  364.         DragQueryFile(hdrop, i, szFile, 80);
  365.         for (p=lptw->DragPre; *p; p++)
  366.             SendMessage(lptw->hWndText,WM_CHAR,*p,1L);
  367.         for (p=szFile; *p; p++) {
  368.             if (*p == '\\')
  369.             SendMessage(lptw->hWndText,WM_CHAR,'/',1L);
  370.             else 
  371.             SendMessage(lptw->hWndText,WM_CHAR,*p,1L);
  372.         }
  373.         for (p=lptw->DragPost; *p; p++)
  374.             SendMessage(lptw->hWndText,WM_CHAR,*p,1L);
  375.     }
  376.     DragFinish(hdrop);
  377. }
  378. #endif
  379.  
  380.  
  381. void
  382. TextMakeFont(LPTW lptw)
  383. {
  384.     LOGFONT lf;
  385.     TEXTMETRIC tm;
  386.     LPSTR p;
  387.     HDC hdc;
  388.     
  389.     if ((lptw->fontname[0]=='\0') || (lptw->fontsize==0)) {
  390.         _fstrcpy(lptw->fontname,TEXTFONTNAME);
  391.         lptw->fontsize = TEXTFONTSIZE;
  392.     }
  393.  
  394.     hdc = GetDC(lptw->hWndText);
  395.     _fmemset(&lf, 0, sizeof(LOGFONT));
  396.     _fstrncpy(lf.lfFaceName,lptw->fontname,LF_FACESIZE);
  397.     lf.lfHeight = -MulDiv(lptw->fontsize, GetDeviceCaps(hdc, LOGPIXELSY), 72);
  398.     lf.lfPitchAndFamily = FIXED_PITCH;
  399.     lf.lfCharSet = DEFAULT_CHARSET;
  400.     if ( (p = _fstrstr(lptw->fontname," Italic")) != (LPSTR)NULL ) {
  401.         lf.lfFaceName[ (unsigned int)(p-lptw->fontname) ] = '\0';
  402.         lf.lfItalic = TRUE;
  403.     }
  404.     if ( (p = _fstrstr(lptw->fontname," Bold")) != (LPSTR)NULL ) {
  405.         lf.lfFaceName[ (unsigned int)(p-lptw->fontname) ] = '\0';
  406.         lf.lfWeight = FW_BOLD;
  407.     }
  408.     if (lptw->hfont != 0)
  409.         DeleteFont(lptw->hfont);
  410.     lptw->hfont = CreateFontIndirect((LOGFONT FAR *)&lf);
  411.     /* get text size */
  412.     SelectFont(hdc, lptw->hfont);
  413.     GetTextMetrics(hdc,(TEXTMETRIC FAR *)&tm);
  414.     lptw->CharSize.y = tm.tmHeight;
  415.     lptw->CharSize.x = tm.tmAveCharWidth;
  416.     lptw->CharAscent = tm.tmAscent;
  417.     if (lptw->bFocus)
  418.         CreateCaret(lptw->hWndText, 0, lptw->CharSize.x, 2+lptw->CaretHeight);
  419.     ReleaseDC(lptw->hWndText, hdc);
  420.     return;
  421. }
  422.  
  423. void
  424. TextCopyClip(LPTW lptw)
  425. {
  426.     int size, count;
  427.     HGLOBAL hGMem;
  428.     LPSTR cbuf, cp;
  429.     TEXTMETRIC tm;
  430.     UINT type;
  431.     HDC hdc;
  432.     int i;
  433.  
  434.     size = lptw->ScreenSize.y * (lptw->ScreenSize.x + 2) + 1;
  435.     hGMem = GlobalAlloc(GHND | GMEM_SHARE, (DWORD)size);
  436.     cbuf = cp = (LPSTR)GlobalLock(hGMem);
  437.     if (cp == (LPSTR)NULL)
  438.         return;
  439.     
  440.     for (i=0; i<lptw->ScreenSize.y; i++) {
  441.         count = lptw->ScreenSize.x;
  442.         _fmemcpy(cp, lptw->ScreenBuffer + lptw->ScreenSize.x*i, count);
  443.         /* remove trailing spaces */
  444.         for (count=count-1; count>=0; count--) {
  445.             if (cp[count]!=' ')
  446.                 break;
  447.             cp[count] = '\0';
  448.         }
  449.         cp[++count] = '\r';
  450.         cp[++count] = '\n';
  451.         cp[++count] = '\0';
  452.         cp += count;
  453.     }
  454.     size = _fstrlen(cbuf) + 1;
  455.     GlobalUnlock(hGMem);
  456.     hGMem = GlobalReAlloc(hGMem, (DWORD)size, GHND | GMEM_SHARE);
  457.     /* find out what type to put into clipboard */
  458.     hdc = GetDC(lptw->hWndText);
  459.     SelectFont(hdc, lptw->hfont);
  460.     GetTextMetrics(hdc,(TEXTMETRIC FAR *)&tm);
  461.     if (tm.tmCharSet == OEM_CHARSET)
  462.         type = CF_OEMTEXT;
  463.     else
  464.         type = CF_TEXT;
  465.     ReleaseDC(lptw->hWndText, hdc);
  466.     /* give buffer to clipboard */
  467.     OpenClipboard(lptw->hWndText);
  468.     EmptyClipboard();
  469.     SetClipboardData(type, hGMem);
  470.     CloseClipboard();
  471. }
  472.  
  473.  
  474. /* text window */
  475. LRESULT CALLBACK _export
  476. WndTextProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  477. {
  478.     HDC hdc;
  479.     PAINTSTRUCT ps;
  480.     RECT rect;
  481.     int nYinc, nXinc;
  482.     LPTW lptw;
  483.  
  484.     lptw = (LPTW)GetWindowLong(hwnd, 0);
  485.  
  486.     switch(message) {
  487.         case WM_GSVIEW:
  488.             if (wParam != PIPE_DATA)
  489.                 return 0;
  490.             if (pipe_count) {
  491.                 MessageBox(hwnd, "pipe overflow", szAppName, MB_OK);
  492.                 pipe_count = 0;
  493.                 if (pipe_lpbyte != (LPBYTE)NULL)
  494.                     GlobalUnlock(pipe_hglobal);
  495.                 if (pipe_hglobal != (HGLOBAL)NULL)
  496.                     GlobalFree(pipe_hglobal);
  497.             }
  498.             pipe_hglobal = (HGLOBAL)lParam;
  499.             return 0;
  500.         case WM_SYSCOMMAND:
  501.             switch(LOWORD(wParam)) {
  502.                 case M_COPY_CLIP:
  503.                     TextCopyClip(lptw);
  504.                     return 0;
  505.             }
  506.             break;
  507.         case WM_SETFOCUS: 
  508.             lptw->bFocus = TRUE;
  509.             CreateCaret(hwnd, 0, lptw->CharSize.x, 2+lptw->CaretHeight);
  510.             SetCaretPos(lptw->CursorPos.x*lptw->CharSize.x - lptw->ScrollPos.x,
  511.                 lptw->CursorPos.y*lptw->CharSize.y + lptw->CharAscent
  512.                  - lptw->CaretHeight - lptw->ScrollPos.y);
  513.             if (lptw->bGetCh)
  514.                 ShowCaret(hwnd);
  515.             break;
  516.         case WM_KILLFOCUS: 
  517.             DestroyCaret();
  518.             lptw->bFocus = FALSE;
  519.             break;
  520.         case WM_SIZE:
  521.             lptw->ClientSize.y = HIWORD(lParam);
  522.             lptw->ClientSize.x = LOWORD(lParam);
  523.  
  524.             lptw->ScrollMax.y = max(0, lptw->CharSize.y*lptw->ScreenSize.y - lptw->ClientSize.y);
  525.             lptw->ScrollPos.y = min(lptw->ScrollPos.y, lptw->ScrollMax.y);
  526.  
  527.             SetScrollRange(hwnd, SB_VERT, 0, lptw->ScrollMax.y, FALSE);
  528.             SetScrollPos(hwnd, SB_VERT, lptw->ScrollPos.y, TRUE);
  529.  
  530.             lptw->ScrollMax.x = max(0, lptw->CharSize.x*lptw->ScreenSize.x - lptw->ClientSize.x);
  531.             lptw->ScrollPos.x = min(lptw->ScrollPos.x, lptw->ScrollMax.x);
  532.  
  533.             SetScrollRange(hwnd, SB_HORZ, 0, lptw->ScrollMax.x, FALSE);
  534.             SetScrollPos(hwnd, SB_HORZ, lptw->ScrollPos.x, TRUE);
  535.  
  536.             if (lptw->bFocus && lptw->bGetCh) {
  537.                 SetCaretPos(lptw->CursorPos.x*lptw->CharSize.x - lptw->ScrollPos.x,
  538.                     lptw->CursorPos.y*lptw->CharSize.y + lptw->CharAscent 
  539.                     - lptw->CaretHeight - lptw->ScrollPos.y);
  540.                 ShowCaret(hwnd);
  541.             }
  542.             return(0);
  543.         case WM_VSCROLL:
  544.             switch(LOWORD(wParam)) {
  545.                 case SB_TOP:
  546.                     nYinc = -lptw->ScrollPos.y;
  547.                     break;
  548.                 case SB_BOTTOM:
  549.                     nYinc = lptw->ScrollMax.y - lptw->ScrollPos.y;
  550.                     break;
  551.                 case SB_LINEUP:
  552.                     nYinc = -lptw->CharSize.y;
  553.                     break;
  554.                 case SB_LINEDOWN:
  555.                     nYinc = lptw->CharSize.y;
  556.                     break;
  557.                 case SB_PAGEUP:
  558.                     nYinc = min(-1,-lptw->ClientSize.y);
  559.                     break;
  560.                 case SB_PAGEDOWN:
  561.                     nYinc = max(1,lptw->ClientSize.y);
  562.                     break;
  563.                 case SB_THUMBPOSITION:
  564. #ifdef __WIN32__
  565.                     nYinc = HIWORD(wParam) - lptw->ScrollPos.y;
  566. #else
  567.                     nYinc = LOWORD(lParam) - lptw->ScrollPos.y;
  568. #endif
  569.                     break;
  570.                 default:
  571.                     nYinc = 0;
  572.                 }
  573.             if ( (nYinc = max(-lptw->ScrollPos.y, 
  574.                 min(nYinc, lptw->ScrollMax.y - lptw->ScrollPos.y)))
  575.                 != 0 ) {
  576.                 lptw->ScrollPos.y += nYinc;
  577.                 ScrollWindow(hwnd,0,-nYinc,NULL,NULL);
  578.                 SetScrollPos(hwnd,SB_VERT,lptw->ScrollPos.y,TRUE);
  579.                 UpdateWindow(hwnd);
  580.             }
  581.             return(0);
  582.         case WM_HSCROLL:
  583.             switch(LOWORD(wParam)) {
  584.                 case SB_LINEUP:
  585.                     nXinc = -lptw->CharSize.x;
  586.                     break;
  587.                 case SB_LINEDOWN:
  588.                     nXinc = lptw->CharSize.x;
  589.                     break;
  590.                 case SB_PAGEUP:
  591.                     nXinc = min(-1,-lptw->ClientSize.x);
  592.                     break;
  593.                 case SB_PAGEDOWN:
  594.                     nXinc = max(1,lptw->ClientSize.x);
  595.                     break;
  596.                 case SB_THUMBPOSITION:
  597. #ifdef __WIN32__
  598.                     nXinc = HIWORD(wParam) - lptw->ScrollPos.x;
  599. #else
  600.                     nXinc = LOWORD(lParam) - lptw->ScrollPos.x;
  601. #endif
  602.                     break;
  603.                 default:
  604.                     nXinc = 0;
  605.                 }
  606.             if ( (nXinc = max(-lptw->ScrollPos.x, 
  607.                 min(nXinc, lptw->ScrollMax.x - lptw->ScrollPos.x)))
  608.                 != 0 ) {
  609.                 lptw->ScrollPos.x += nXinc;
  610.                 ScrollWindow(hwnd,-nXinc,0,NULL,NULL);
  611.                 SetScrollPos(hwnd,SB_HORZ,lptw->ScrollPos.x,TRUE);
  612.                 UpdateWindow(hwnd);
  613.             }
  614.             return(0);
  615.         case WM_KEYDOWN:
  616.             if (GetKeyState(VK_SHIFT) < 0) {
  617.               switch(wParam) {
  618.                 case VK_HOME:
  619.                     SendMessage(hwnd, WM_VSCROLL, SB_TOP, (LPARAM)0);
  620.                     break;
  621.                 case VK_END:
  622.                     SendMessage(hwnd, WM_VSCROLL, SB_BOTTOM, (LPARAM)0);
  623.                     break;
  624.                 case VK_PRIOR:
  625.                     SendMessage(hwnd, WM_VSCROLL, SB_PAGEUP, (LPARAM)0);
  626.                     break;
  627.                 case VK_NEXT:
  628.                     SendMessage(hwnd, WM_VSCROLL, SB_PAGEDOWN, (LPARAM)0);
  629.                     break;
  630.                 case VK_UP:
  631.                     SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, (LPARAM)0);
  632.                     break;
  633.                 case VK_DOWN:
  634.                     SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, (LPARAM)0);
  635.                     break;
  636.                 case VK_LEFT:
  637.                     SendMessage(hwnd, WM_HSCROLL, SB_LINEUP, (LPARAM)0);
  638.                     break;
  639.                 case VK_RIGHT:
  640.                     SendMessage(hwnd, WM_HSCROLL, SB_LINEDOWN, (LPARAM)0);
  641.                     break;
  642.               }
  643.             }
  644.             else {
  645.               switch(wParam) {
  646.                 case VK_HOME:
  647.                 case VK_END:
  648.                 case VK_PRIOR:
  649.                 case VK_NEXT:
  650.                 case VK_UP:
  651.                 case VK_DOWN:
  652.                 case VK_LEFT:
  653.                 case VK_RIGHT:
  654.                 case VK_DELETE:
  655.                 { /* store key in circular buffer */
  656.                 long count;
  657.                     count = lptw->KeyBufIn - lptw->KeyBufOut;
  658.                     if (count < 0) count += lptw->KeyBufSize;
  659.                     if (count < lptw->KeyBufSize-2) {
  660.                         *lptw->KeyBufIn++ = 0;
  661.                         if (lptw->KeyBufIn - lptw->KeyBuf >= lptw->KeyBufSize)
  662.                             lptw->KeyBufIn = lptw->KeyBuf;    /* wrap around */
  663.                         *lptw->KeyBufIn++ = HIWORD(lParam) & 0xff;
  664.                         if (lptw->KeyBufIn - lptw->KeyBuf >= lptw->KeyBufSize)
  665.                             lptw->KeyBufIn = lptw->KeyBuf;    /* wrap around */
  666.                     }
  667.                 }
  668.               }
  669.             }
  670.             break;
  671.         case WM_CHAR:
  672.             { /* store key in circular buffer */
  673.             long count;
  674.                 count = lptw->KeyBufIn - lptw->KeyBufOut;
  675.                 if (count < 0) count += lptw->KeyBufSize;
  676.                 if (count < lptw->KeyBufSize-1) {
  677.                     *lptw->KeyBufIn++ = wParam;
  678.                     if (lptw->KeyBufIn - lptw->KeyBuf >= lptw->KeyBufSize)
  679.                         lptw->KeyBufIn = lptw->KeyBuf;    /* wrap around */
  680.                 }
  681.             }
  682.             return(0);
  683.         case WM_PAINT:
  684.             {
  685.             POINT source, width, dest;
  686.             hdc = BeginPaint(hwnd, &ps);
  687.             SelectFont(hdc, lptw->hfont);
  688.             SetMapMode(hdc, MM_TEXT);
  689.             SetBkMode(hdc,OPAQUE);
  690.             GetClientRect(hwnd, &rect);
  691.             source.x = (rect.left + lptw->ScrollPos.x) / lptw->CharSize.x;        /* source */
  692.             source.y = (rect.top + lptw->ScrollPos.y) / lptw->CharSize.y;
  693.             dest.x = source.x * lptw->CharSize.x - lptw->ScrollPos.x;                 /* destination */
  694.             dest.y = source.y * lptw->CharSize.y - lptw->ScrollPos.y;
  695.             width.x = ((rect.right  + lptw->ScrollPos.x + lptw->CharSize.x - 1) / lptw->CharSize.x) - source.x; /* width */
  696.             width.y = ((rect.bottom + lptw->ScrollPos.y + lptw->CharSize.y - 1) / lptw->CharSize.y) - source.y;
  697.             if (source.x < 0)
  698.                 source.x = 0;
  699.             if (source.y < 0)
  700.                 source.y = 0;
  701.             if (source.x+width.x > lptw->ScreenSize.x)
  702.                 width.x = lptw->ScreenSize.x - source.x;
  703.             if (source.y+width.y > lptw->ScreenSize.y)
  704.                 width.y = lptw->ScreenSize.y - source.y;
  705.             /* for each line */
  706.             while (width.y>0) {
  707.                 TextOut(hdc,dest.x,dest.y,
  708.                     (LPSTR)(lptw->ScreenBuffer + source.y*lptw->ScreenSize.x + source.x),
  709.                     width.x);
  710.                 dest.y += lptw->CharSize.y;
  711.                 source.y++;
  712.                 width.y--;
  713.             }
  714.             EndPaint(hwnd, &ps);
  715.             return 0;
  716.             }
  717. #if WINVER >= 0x030a
  718.         case WM_DROPFILES:
  719.             {
  720.             WORD version = LOWORD(GetVersion());
  721.             if ((LOBYTE(version)*100 + HIBYTE(version)) >= 310)
  722.                 DragFunc(lptw, (HDROP)wParam);
  723.             }
  724.             break;
  725. #endif
  726.         case WM_CREATE:
  727.             {
  728.             RECT crect, wrect;
  729.             TEXTMETRIC tm;
  730.             lptw = ((CREATESTRUCT FAR *)lParam)->lpCreateParams;
  731.             SetWindowLong(hwnd, 0, (LONG)lptw);
  732.             lptw->hWndText = hwnd;
  733.             /* get character size */
  734.             TextMakeFont(lptw);
  735.             hdc = GetDC(hwnd);
  736.             SelectFont(hdc, lptw->hfont);
  737.             GetTextMetrics(hdc,(LPTEXTMETRIC)&tm);
  738.             lptw->CharSize.y = tm.tmHeight;
  739.             lptw->CharSize.x = tm.tmAveCharWidth;
  740.             lptw->CharAscent = tm.tmAscent;
  741.             ReleaseDC(hwnd,hdc);
  742.             GetClientRect(hwnd, &crect);
  743.             if ( (lptw->CharSize.y*lptw->ScreenSize.y < crect.bottom)
  744.               || (lptw->CharSize.x*lptw->ScreenSize.x < crect.right) ) {
  745.                 /* shrink size */
  746.                 GetWindowRect(lptw->hWndText,&wrect);
  747.                 MoveWindow(lptw->hWndText, wrect.left, wrect.top,
  748.                  wrect.right-wrect.left + (lptw->CharSize.x*lptw->ScreenSize.x - crect.right),
  749.                  wrect.bottom-wrect.top + (lptw->CharSize.y*lptw->ScreenSize.y - crect.bottom),
  750.                  TRUE);
  751.             }
  752. #if WINVER >= 0x030a
  753.             {
  754.             WORD version = LOWORD(GetVersion());
  755.             if ((LOBYTE(version)*100 + HIBYTE(version)) >= 310)
  756.                 if ( (lptw->DragPre!=(LPSTR)NULL) && (lptw->DragPost!=(LPSTR)NULL) )
  757.                     DragAcceptFiles(hwnd, TRUE);
  758.             }
  759. #endif
  760.             }
  761.             break;
  762.         case WM_CLOSE:
  763.             if (lptw->shutdown)
  764.                 (*(lptw->shutdown))();
  765.             break;
  766.         case WM_DESTROY:
  767. #if WINVER >= 0x030a
  768.             {
  769.             WORD version = LOWORD(GetVersion());
  770.             if ((LOBYTE(version)*100 + HIBYTE(version)) >= 310)
  771.                 DragAcceptFiles(hwnd, FALSE);
  772.             }
  773. #endif
  774.             if (lptw->hfont != 0)
  775.                 DeleteFont(lptw->hfont);
  776.             break;
  777.     }
  778.     return DefWindowProc(hwnd, message, wParam, lParam);
  779. }
  780.