home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / vrac / dflt20.zip / EDITOR.C < prev    next >
C/C++ Source or Header  |  1995-03-07  |  5KB  |  205 lines

  1. /* ------------- editor.c ------------ */
  2. #include "dflat.h"
  3.  
  4. #define pTab ('\t' + 0x80)
  5. #define sTab ('\f' + 0x80)
  6.  
  7. /* ---------- SETTEXT Message ------------ */
  8. static int SetTextMsg(WINDOW wnd, char *Buf)
  9. {
  10.        unsigned char *tp, *ep, *ttp;
  11.        int x = 0;
  12.        int sz = 0;
  13.     int rtn;
  14.  
  15.     tp = Buf;
  16.     /* --- compute the buffer size based on tabs in the text --- */
  17.     while (*tp)    {
  18.         if (*tp == '\t')    {
  19.             /* --- tab, adjust the buffer length --- */
  20.             int sps = cfg.Tabs - (x % cfg.Tabs);
  21.             sz += sps;
  22.             x += sps;
  23.         }
  24.         else    {
  25.             /* --- not a tab, count the character --- */
  26.             sz++;
  27.             x++;
  28.         }
  29.         if (*tp == '\n')
  30.             x = 0;    /* newline, reset x --- */
  31.         tp++;
  32.     }
  33.     /* --- allocate a buffer --- */
  34.     ep = DFcalloc(1, sz+1);
  35.     /* --- detab the input file --- */
  36.     tp = Buf;
  37.     ttp = ep;
  38.     x = 0;
  39.     while (*tp)    {
  40.         /* --- put the character (\t, too) into the buffer --- */
  41.         x++;
  42.         /* --- expand tab into subst tab (\f + 0x80)
  43.                         and expansions (\t + 0x80) --- */
  44.         if (*tp == '\t')    {
  45.             *ttp++ = sTab;    /* --- substitute tab character --- */
  46.             while ((x % cfg.Tabs) != 0)
  47.                 *ttp++ = pTab, x++;
  48.         }
  49.         else    {
  50.             *ttp++ = *tp;
  51.             if (*tp == '\n')
  52.                 x = 0;
  53.         }
  54.         tp++;
  55.     }
  56.     *ttp = '\0';
  57.     rtn = BaseWndProc(EDITOR, wnd, SETTEXT, (PARAM) ep, 0);
  58.     free(ep);
  59.     return rtn;
  60. }
  61. void CollapseTabs(WINDOW wnd)
  62. {
  63.     unsigned char *cp = wnd->text, *cp2;
  64.     while (*cp)    {
  65.         if (*cp == sTab)    {
  66.             *cp = '\t';
  67.             cp2 = cp;
  68.             while (*++cp2 == pTab)
  69.                 ;
  70.             memmove(cp+1, cp2, strlen(cp2)+1);
  71.         }
  72.         cp++;
  73.     }
  74. }
  75.  
  76. void ExpandTabs(WINDOW wnd)
  77. {
  78.     int Holdwtop = wnd->wtop;
  79.     int Holdwleft = wnd->wleft;
  80.     int HoldRow = wnd->CurrLine;
  81.     int HoldCol = wnd->CurrCol;
  82.     int HoldwRow = wnd->WndRow;
  83.     SendMessage(wnd, SETTEXT, (PARAM) wnd->text, 0);
  84.     wnd->wtop = Holdwtop;
  85.     wnd->wleft = Holdwleft;
  86.     wnd->CurrLine = HoldRow;
  87.     wnd->CurrCol = HoldCol;
  88.     wnd->WndRow = HoldwRow;
  89.     SendMessage(wnd, PAINT, 0, 0);
  90.     SendMessage(wnd, KEYBOARD_CURSOR, 0, wnd->WndRow);
  91. }
  92.  
  93. /* --- When inserting or deleting, adjust next following tab, same line --- */
  94. static void AdjustTab(WINDOW wnd)
  95. {
  96.     /* ---- test if there is a tab beyond this character ---- */
  97.     int col = wnd->CurrCol;
  98.     while (*CurrChar && *CurrChar != '\n')    {
  99.         if (*CurrChar == sTab)    {
  100.             int exp = (cfg.Tabs - 1) - (wnd->CurrCol % cfg.Tabs);
  101.             wnd->CurrCol++;
  102.             while (*CurrChar == pTab)
  103.                 BaseWndProc(EDITOR, wnd, KEYBOARD, DEL, 0);
  104.             while (exp--)
  105.                 BaseWndProc(EDITOR, wnd, KEYBOARD, pTab, 0);
  106.             break;
  107.         }
  108.         wnd->CurrCol++;
  109.     }
  110.     wnd->CurrCol = col;
  111. }
  112.  
  113. static void TurnOffDisplay(WINDOW wnd)
  114. {
  115.     SendMessage(NULL, HIDE_CURSOR, 0, 0);
  116.     ClearVisible(wnd);
  117. }
  118.  
  119. static void TurnOnDisplay(WINDOW wnd)
  120. {
  121.     SetVisible(wnd);
  122.     SendMessage(NULL, SHOW_CURSOR, 0, 0);
  123. }
  124.  
  125. static void RepaintLine(WINDOW wnd)
  126. {
  127.     SendMessage(wnd, KEYBOARD_CURSOR, WndCol, wnd->WndRow);
  128.     WriteTextLine(wnd, NULL, wnd->CurrLine, FALSE);
  129. }
  130.  
  131. /* --------- KEYBOARD Message ---------- */
  132. static int KeyboardMsg(WINDOW wnd, PARAM p1, PARAM p2)
  133. {
  134.     int c = (int) p1;
  135.     BOOL delnl;
  136.     PARAM pn = p1;
  137.     if (WindowMoving || WindowSizing || ((int)p2 & ALTKEY))
  138.         return FALSE;
  139.     switch (c)    {
  140.         case PGUP:
  141.         case PGDN:
  142.         case UP:
  143.         case DN:
  144.             pn = (PARAM) BS;
  145.         case FWD:
  146.         case BS:
  147.             BaseWndProc(EDITOR, wnd, KEYBOARD, p1, p2);
  148.             TurnOffDisplay(wnd);
  149.             while (*CurrChar == pTab)
  150.                 BaseWndProc(EDITOR, wnd, KEYBOARD, pn, p2);
  151.             TurnOnDisplay(wnd);
  152.             return TRUE;
  153.         case DEL:
  154.             TurnOffDisplay(wnd);
  155.             delnl = *CurrChar == '\n' || TextBlockMarked(wnd);
  156.             BaseWndProc(EDITOR, wnd, KEYBOARD, p1, p2);
  157.             while (*CurrChar == pTab)
  158.                 BaseWndProc(EDITOR, wnd, KEYBOARD, p1, p2);
  159.             AdjustTab(wnd);
  160.             TurnOnDisplay(wnd);
  161.             RepaintLine(wnd);
  162.             if (delnl)
  163.                 SendMessage(wnd, PAINT, 0, 0);
  164.             return TRUE;
  165.         case '\t':
  166.             TurnOffDisplay(wnd);
  167.             BaseWndProc(EDITOR, wnd, KEYBOARD, (PARAM) sTab, p2);
  168.             while ((wnd->CurrCol % cfg.Tabs) != 0)
  169.                 BaseWndProc(EDITOR, wnd, KEYBOARD, pTab, p2);
  170.             TurnOnDisplay(wnd);
  171.             RepaintLine(wnd);
  172.             return TRUE;
  173.         default:
  174.             if (((c & OFFSET) == 0) && (isprint(c) || c == '\r'))    {
  175.                 TurnOffDisplay(wnd);
  176.                 BaseWndProc(EDITOR, wnd, KEYBOARD, p1, p2);
  177.                 AdjustTab(wnd);
  178.                 TurnOnDisplay(wnd);
  179.                 RepaintLine(wnd);
  180.                 if (c == '\r')
  181.                     SendMessage(wnd, PAINT, 0, 0);
  182.                 return TRUE;
  183.             }
  184.             break;
  185.     }
  186.     return FALSE;
  187. }
  188.  
  189. /* ------- Window processing module for EDITBOX class ------ */
  190. int EditorProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  191. {
  192.     switch (msg)    {
  193.         case KEYBOARD:
  194.             if (KeyboardMsg(wnd, p1, p2))
  195.                 return TRUE;
  196.             break;
  197.         case SETTEXT:
  198.             return SetTextMsg(wnd, (char *) p1);
  199.         default:
  200.             break;
  201.     }
  202.     return BaseWndProc(EDITOR, wnd, msg, p1, p2);
  203. }
  204.  
  205.