home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue158 / delphi / RichEditTest / strtest.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-13  |  3.7 KB  |  147 lines

  1. unit strtest;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls,
  8.   StrUtils;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     Edit1: TEdit;
  13.     ParseFirstTokenBtn: TButton;
  14.     Memo1: TMemo;
  15.     ParseAllTokensBtn: TButton;
  16.     HighlightTokensBtn: TButton;
  17.     FindTokenIndexesBtn: TButton;
  18.     HighlightTokensInMemoBtn: TButton;
  19.     procedure ParseFirstTokenBtnClick(Sender: TObject);
  20.     procedure ParseAllTokensBtnClick(Sender: TObject);
  21.     procedure HighlightTokensBtnClick(Sender: TObject);
  22.     procedure FindTokenIndexesBtnClick(Sender: TObject);
  23.     procedure HighlightTokensInMemoBtnClick(Sender: TObject);
  24.   private
  25.     { Private declarations }
  26.   public
  27.     { Public declarations }
  28.     procedure Delay(Num: longint);
  29.     function GetLineSelStartIndex( LineNum: LongInt ) : LongInt;    
  30.   end;
  31.  
  32. var
  33.   Form1: TForm1;
  34.  
  35. implementation
  36.  
  37. {$R *.DFM}
  38. procedure TForm1.Delay(Num: longint);
  39. var
  40.    tc :longint;
  41. begin
  42.      tc :=GetTickCount;
  43.      repeat
  44.            Application.ProcessMessages;
  45.      until ((GetTickCount-tc) >= Num);
  46. end;
  47.  
  48. procedure TForm1.ParseFirstTokenBtnClick(Sender: TObject);
  49. var
  50.    s, first, rest : string;
  51.    tokstart, tokend : integer;
  52. begin
  53.    s := Edit1.Text;
  54.    first := ''; rest := '';
  55.    tokstart := 0; tokend := 0;
  56.    firstTokenAt( s,tokstart,tokend,first,rest);
  57.    Memo1.Clear;
  58.    Memo1.Lines.Add( Format('tokstart=%d, tokend = %d, first=%s, rest=%s',
  59.                     [tokstart,tokend, first, rest]) );
  60. end;
  61.  
  62. procedure TForm1.ParseAllTokensBtnClick(Sender: TObject);
  63. var
  64.    first, rest : string;
  65.    tokstart, tokend : integer;
  66. begin
  67.    rest := Edit1.Text;
  68.    first := '';
  69.    tokstart := 1;
  70.    tokend := 0;
  71.    Memo1.Clear;
  72.    while (tokstart <> 0) do
  73.    begin
  74.      firstTokenAt( rest, tokstart, tokend, first, rest);
  75.      Memo1.Lines.Add( Format('tokstart=%d, tokend = %d, first=%s, rest=%s',
  76.                     [tokstart,tokend,first,rest]) );
  77.    end;
  78.  
  79. end;
  80.  
  81. procedure TForm1.HighlightTokensBtnClick(Sender: TObject);
  82. var
  83.    ti : tokenindexes;
  84.    i, numfound : integer;
  85.    s : string;
  86. begin
  87.   s := Edit1.Text;
  88.   TokensFoundAt( s, numfound, ti );
  89.   Memo1.Clear;
  90.   for i := 1 to numfound do
  91.   begin
  92.        Memo1.Lines.Add( Format('ti[%d].tstart=%d, .tend=%d',
  93.                     [i,ti[i].tstart,ti[i].tend]) );
  94.         Edit1.SetFocus;
  95.         Edit1.SelStart := ti[i].tstart;
  96.         Edit1.SelLength := ti[i].tend;
  97.         Delay(500);
  98.    end;
  99. end;
  100.  
  101. procedure TForm1.FindTokenIndexesBtnClick(Sender: TObject);
  102. var
  103.    ti : tokenindexes;
  104.    i, numfound : integer;
  105.    s : string;
  106. begin
  107.   s := Edit1.Text;
  108.   TokensFoundAt( s, numfound, ti );
  109.   Memo1.Clear;
  110.   Memo1.Lines.Add( Format('s = %s, numfound=%d',
  111.                     [s, numfound]) );
  112.   for i := 1 to numfound do
  113.        Memo1.Lines.Add( Format('ti[%d].tstart=%d, .tend=%d',
  114.                     [i,ti[i].tstart,ti[i].tend]) );
  115.  
  116.  
  117. end;
  118.  
  119. function TForm1.GetLineSelStartIndex( LineNum: LongInt ) : LongInt;
  120. { return Selection (char num from start of RichEdit) index of 1st character in
  121.   the Line specified by LineNum }
  122. begin
  123.    result := SendMessage(Memo1.Handle, EM_LINEINDEX, LineNum,0);
  124. end;
  125.  
  126. procedure TForm1.HighlightTokensInMemoBtnClick(Sender: TObject);
  127. // highlight tokens in 2nd line of Memo
  128. var
  129.    ti : tokenindexes;
  130.    i, numfound : integer;
  131.    StartIndex : LongInt;
  132.    s : string;
  133. begin
  134.   s := Memo1.Lines[1];
  135.   TokensFoundAt( s, numfound, ti );
  136.   Memo1.SetFocus;
  137.   StartIndex := GetLineSelStartIndex( 1 );
  138.   for i := 1 to numfound do
  139.   begin
  140.      Memo1.SelStart := StartIndex + ti[i].tstart;
  141.      Memo1.SelLength := ti[i].tend;
  142.      Delay(500);
  143.    end;
  144. end;
  145.  
  146. end.
  147.