home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / df3os2.zip / EDITBOX.CPP < prev    next >
C/C++ Source or Header  |  1993-09-27  |  4KB  |  229 lines

  1. // ------------- editbox.cpp
  2.  
  3. #include <ctype.h>
  4. #include "desktop.h"
  5. #include "editbox.h"
  6.  
  7. // ----------- common constructor code
  8. void EditBox::OpenWindow()
  9. {
  10.     windowtype = EditboxWindow;
  11.     column = 0;
  12.     changed = False;
  13.     text = new String(1);
  14.     BuildTextPointers();
  15. }
  16.  
  17. Bool EditBox::SetFocus()
  18. {
  19.     Bool rtn = TextBox::SetFocus();
  20.     if (rtn)    {
  21.         ResetCursor();
  22.         desktop.cursor().Show();
  23.     }
  24.     return rtn;
  25. }
  26.  
  27. void EditBox::ResetFocus()
  28. {
  29.     desktop.cursor().Hide();
  30.     TextBox::ResetFocus();
  31. }
  32.  
  33. // -------- process keystrokes
  34. void EditBox::Keyboard(int key)
  35. {
  36.     int shift = desktop.keyboard().GetShift();
  37.     if ((shift & ALTKEY) == 0)    {
  38.         switch (key)    {
  39.             case HOME:
  40.                 Home();
  41.                 return;
  42.             case END:
  43.                 End();
  44.                 return;
  45.             case CTRL_FWD:
  46.                 NextWord();
  47.                 return;
  48.             case CTRL_BS:
  49.                 PrevWord();
  50.                 return;
  51.             case FWD:
  52.                 Forward();
  53.                 return;
  54.             case BS:
  55.                 Backward();
  56.                 return;
  57.             case RUBOUT:
  58.                 if (CurrentCharPosition() == 0)
  59.                     break;
  60.                 Backward();
  61.                 // --- fall through
  62.             case DEL:
  63.                 DeleteCharacter();
  64.                 BuildTextPointers();
  65.                 PaintCurrentLine();
  66.                 return;
  67.             default:
  68.                 if (!isprint(key))
  69.                     break;
  70.                 // --- all printable keys get processed by editbox
  71.                 InsertCharacter(key);
  72.                 BuildTextPointers();
  73.                 PaintCurrentLine();
  74.                 return;
  75.         }
  76.     }
  77.     TextBox::Keyboard(key);
  78. }
  79.  
  80. // -------- paint the editbox
  81. void EditBox::Paint()
  82. {
  83.     TextBox::Paint();
  84.     ResetCursor();
  85. }
  86.  
  87. // -------- move the editbox
  88. void EditBox::Move(int x, int y)
  89. {
  90.     TextBox::Move(x, y);
  91.     ResetCursor();
  92. }
  93.  
  94. // --------- clear the text from the editbox
  95. void EditBox::ClearText()
  96. {
  97.     TextBox::ClearText();
  98.     OpenWindow();
  99.     ResetCursor();
  100. }
  101.  
  102. void EditBox::Home()
  103. {
  104.     column = 0;
  105.     if (wleft)    {
  106.         wleft = 0;
  107.         Paint();
  108.     }
  109.     ResetCursor();
  110. }
  111.  
  112. void EditBox::End()
  113. {
  114.     int ch;
  115.     while ((ch = CurrentChar()) != '\0' && ch != '\n')
  116.         column++;
  117.     if (column - wleft >= ClientWidth())    {
  118.         wleft = column - ClientWidth() + 1;
  119.         Paint();
  120.     }
  121.     ResetCursor();
  122. }
  123.  
  124. void EditBox::NextWord()
  125. {
  126.     while (!isWhite(CurrentChar()) && CurrentChar())
  127.         Forward();
  128.     while (isWhite(CurrentChar()))
  129.         Forward();
  130. }
  131.  
  132. void EditBox::PrevWord()
  133. {
  134.     Backward();
  135.     while (isWhite(CurrentChar()) && !AtBufferStart())
  136.         Backward();
  137.     while (!isWhite(CurrentChar()) && !AtBufferStart())
  138.         Backward();
  139.     if (isWhite(CurrentChar()))
  140.         Forward();
  141. }
  142.  
  143. void EditBox::Forward()
  144. {
  145.     if (CurrentChar())    {
  146.         column++;
  147.         if (column-wleft == ClientWidth())
  148.             ScrollLeft();
  149.         ResetCursor();
  150.     }
  151. }
  152.  
  153. void EditBox::Backward()
  154. {
  155.     if (column)    {
  156.         if (column == wleft)
  157.             ScrollRight();
  158.         --column;
  159.         ResetCursor();
  160.     }
  161. }
  162.  
  163. void EditBox::InsertCharacter(int key)
  164. {
  165.     unsigned col = CurrentCharPosition();
  166.     if (insertmode || CurrentChar() == '\0')    {
  167.         // ---- shift the text to make room for new character
  168.         String ls, rs;
  169.         if (col)
  170.             ls = text->left(col);
  171.         int rt = text->Strlen()-col;
  172.         if (rt > 0)
  173.             rs = text->right(rt);
  174.         *text = ls + " " + rs;
  175.     }
  176.     (*text)[col] = (char) key;
  177.     if (key == '\n')
  178.         BuildTextPointers();
  179.     Forward();
  180.     changed = True;
  181. }
  182.  
  183. void EditBox::DeleteCharacter()
  184. {
  185.     if (CurrentChar())    {
  186.         String ls, rs;
  187.         unsigned col = CurrentCharPosition();
  188.         if (col)
  189.             ls = text->left(col);
  190.         int rt = text->Strlen()-col-1;
  191.         if (rt > 0)
  192.             rs = text->right(rt);
  193.         *text = ls + rs;
  194.         changed = True;
  195.     }
  196. }
  197.  
  198. void EditBox::SetCursor(int x, int y)
  199. {
  200.     desktop.cursor().SetPosition(
  201.         x+ClientLeft()-wleft, y+ClientTop()-wtop);
  202. }
  203.  
  204. void EditBox::LeftButton(int mx, int my)
  205. {
  206.     if (ClientRect().Inside(mx, my))    {
  207.         column = max(0, min(text->Strlen()-1, mx-ClientLeft()+wleft));
  208.         ResetCursor();
  209.     }
  210.     else
  211.         TextBox::LeftButton(mx, my);
  212. }
  213.  
  214. void EditBox::SetCursorSize()
  215. {
  216.     if (insertmode)
  217.         desktop.cursor().BoxCursor();
  218.     else 
  219.         desktop.cursor().NormalCursor();
  220. }
  221.  
  222. void EditBox::ResetCursor()
  223. {
  224.     SetCursorSize();
  225.     if (visible)
  226.         SetCursor(column, 0);
  227. }
  228.  
  229.