home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue159 / files / delphi / RTF / rtfunit.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1999-08-23  |  3.2 KB  |  132 lines

  1. unit rtfunit;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, ComCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     RichEdit1: TRichEdit;
  12.     RichEdit2: TRichEdit;
  13.     CopyFrom1To2Btn: TButton;
  14.     RecolourBtn: TButton;
  15.     ColorDialog1: TColorDialog;
  16.     Label1: TLabel;
  17.     Label2: TLabel;
  18.     Label3: TLabel;
  19.     RichEdit3: TRichEdit;
  20.     CopyFrom2To3Btn: TButton;
  21.     SetFontBtn: TButton;
  22.     FontDialog1: TFontDialog;
  23.     procedure FormCreate(Sender: TObject);
  24.     procedure CopyFrom1To2BtnClick(Sender: TObject);
  25.     procedure RecolourBtnClick(Sender: TObject);
  26.     procedure FormKeyDown(Sender: TObject; var Key: Word;
  27.       Shift: TShiftState);
  28.     procedure RichEdit1KeyPress(Sender: TObject; var Key: Char);
  29.     procedure CopyFrom2To3BtnClick(Sender: TObject);
  30.     procedure SetFontBtnClick(Sender: TObject);
  31.   private
  32.     { Private declarations }
  33.   public
  34.     { Public declarations }
  35.     procedure ToggleBold;
  36.     procedure ToggleItalic;
  37.     function SelAtts: TTextAttributes;
  38.   end;
  39.  
  40. var
  41.   Form1: TForm1;
  42.  
  43. implementation
  44.  
  45. {$R *.DFM}
  46. procedure TForm1.CopyFrom1To2BtnClick(Sender: TObject);
  47. var
  48.    ms : TMemoryStream;
  49. begin
  50.     ms := TMemoryStream.Create;
  51.     RichEdit1.Lines.SaveToStream(ms);
  52.     ms.Position := 0;
  53.     RichEdit2.Lines.LoadFromStream(ms);
  54.     ms.Free;
  55.     RichEdit1.SetFocus;
  56. end;
  57.  
  58. procedure TForm1.CopyFrom2To3BtnClick(Sender: TObject);
  59. var
  60.    ms : TMemoryStream;
  61. begin
  62.     ms := TMemoryStream.Create;
  63.     RichEdit2.Lines.SaveToStream(ms);
  64.     ms.Position := 0;
  65.     RichEdit3.Lines.LoadFromStream(ms);
  66.     ms.Free;
  67.     RichEdit2.SetFocus;
  68. end;
  69.  
  70. function TForm1.SelAtts: TTextAttributes;
  71. begin
  72.     Result := RichEdit1.SelAttributes;
  73. end;
  74.  
  75. procedure TForm1.ToggleBold;
  76. begin
  77.    if fsBold in SelAtts.style then
  78.       SelAtts.style := SelAtts.style - [fsBold]
  79.    else SelAtts.style := SelAtts.style + [fsBold];
  80. end;
  81.  
  82. procedure TForm1.ToggleItalic;
  83. begin
  84.    if fsItalic in SelAtts.style then
  85.       SelAtts.style := SelAtts.style - [fsItalic]
  86.    else SelAtts.style := SelAtts.style + [fsItalic];
  87. end;
  88.  
  89. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  90.   Shift: TShiftState);
  91.   // turn on bold or italic
  92. begin
  93.   if (ssCtrl in Shift) then
  94.      if (upcase(chr(Key)) = 'B') then
  95.         ToggleBold
  96.      else if (upcase(chr(Key)) = 'I') then
  97.         ToggleItalic;
  98. end;
  99.  
  100. procedure TForm1.RichEdit1KeyPress(Sender: TObject; var Key: Char);
  101. begin // mop up tab from CTRL-I
  102.    if Key = Chr(9) then Key := Chr(0);
  103. end;
  104.  
  105. procedure TForm1.SetFontBtnClick(Sender: TObject);
  106. begin
  107.   FontDialog1.Font := RichEdit1.Font;
  108.   if FontDialog1.Execute then
  109.      RichEdit1.Font := FontDialog1.Font;
  110.   RichEdit1.SetFocus;
  111. end;
  112.  
  113. procedure TForm1.RecolourBtnClick(Sender: TObject);
  114. begin
  115.   ColorDialog1.Color := SelAtts.Color;
  116.   if ColorDialog1.Execute then
  117.      SelAtts.Color := ColorDialog1.Color;
  118.   RichEdit1.SetFocus;
  119. end;
  120.  
  121. procedure TForm1.FormCreate(Sender: TObject);
  122. // set RichEdit controls 1 and 3 to RTF mode, but 2 to Plain Text 
  123. begin
  124.   RichEdit1.PlainText := False;
  125.   RichEdit2.PlainText := True;
  126.   RichEdit3.PlainText := False;
  127.   KeyPreview := True; { to trap hotkeys }
  128. end;
  129.  
  130. end.
  131.  
  132.