home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue145 / Delphi / MacroEd / macrec.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-12-19  |  3.7 KB  |  126 lines

  1. unit Macrec;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, ExtCtrls;
  8.  
  9. type
  10.   TMacroForm = class(TForm)
  11.     Panel1: TPanel;
  12.     OKBtn: TButton;
  13.     CancelBtn: TButton;
  14.     MacroMemo: TMemo;
  15.     MsgLabel: TLabel;
  16.     Panel2: TPanel;
  17.     MacKeyLabel: TLabel;
  18.     KeyCharEdit: TEdit;
  19.     MacKeyMsg: TLabel;
  20.     procedure OKBtnClick(Sender: TObject);
  21.     procedure CancelBtnClick(Sender: TObject);
  22.     procedure MacroMemoKeyUp(Sender: TObject; var Key: Word;
  23.       Shift: TShiftState);
  24.     procedure FormActivate(Sender: TObject);
  25.     procedure KeyCharEditChange(Sender: TObject);
  26.   private
  27.     { Private declarations }
  28.   public
  29.     { Public declarations }
  30.   end;
  31.  
  32. const
  33.   MaxLen = 255; { Delphi 1's default maximum string length }
  34.   MKLABEL = 'Macro Key CTRL-ALT-';
  35.  
  36. var
  37.   MacroForm: TMacroForm;
  38.  
  39. implementation
  40.  
  41. {$R *.DFM}
  42. uses MacEd; { This 'uses' clause must be placed beneath the 'implementation'
  43.               keyword, otherwise you will generate a circular reference (since
  44.               this unit is itself used by the MacEd unit.
  45.  
  46.               The reason for this using MacEd is that it references
  47.               the MainForm.GetMacString method in that unit }
  48.  
  49. procedure TMacroForm.OKBtnClick(Sender: TObject);
  50. var
  51.    OK : TModalResult;
  52. begin
  53.    OK := mrOK;          { by default, return mrOK, which will close MacroForm  }
  54.    if MacroMemo.Text = '' then
  55.      if KeyCharEdit.Text = '' then OK := mrCancel { there's nothing to process }
  56.       else if MessageDlg('Assign no text to this macro key?',
  57.          mtConfirmation, mbOkCancel, 0) = mrCancel then
  58.            OK := mrNone;{ if user selects Cancel, don't close MacroForm        }
  59.    ModalResult := OK;
  60. end;
  61.  
  62. procedure TMacroForm.CancelBtnClick(Sender: TObject);
  63. begin
  64.   ModalResult := mrCancel;
  65. end;
  66.  
  67. procedure TMacroForm.MacroMemoKeyUp(Sender: TObject; var Key: Word;
  68.   Shift: TShiftState);
  69. { When the user has almost entered the maximum allowed macro text
  70.   (MexLen-10), show a warning message. Show another message when
  71.   maximum text is entered }
  72. var
  73.   TextLen : integer;
  74. begin
  75.   TextLen := Length(MacroMemo.Text);
  76.      MsgLabel.Caption := IntToStr(TextLen) + ' chars. ';
  77.   if (TextLen = MaxLen) then
  78.        MsgLabel.Caption := MsgLabel.Caption +
  79.          'WARNING! Cannot add more text!'
  80.   else if (TextLen > (MaxLen-10)) then
  81.      MsgLabel.Caption := MsgLabel.Caption +
  82.          'WARNING! Near to maximum size!';
  83. end;
  84.  
  85. procedure TMacroForm.FormActivate(Sender: TObject);
  86. { Clear any text. Set Memo to ReadOnly.
  87.   Show KeyCharEdit to let user add macro-key char }
  88. begin
  89.    MacroMemo.Clear;
  90.    MacroMemo.ReadOnly := true;
  91.    MacKeyLabel.Caption := MKLABEL;
  92.    KeyCharEdit.Text := '';
  93.    KeyCharEdit.Show;
  94.    ActiveControl := KeyCharEdit; { set focus when showing form }
  95. end;
  96.  
  97. procedure TMacroForm.KeyCharEditChange(Sender: TObject);
  98. { The KeyCharEdit field remains active until the user
  99.   enters a valid macro-activation character ('A' to 'Z').
  100.   Then it is hidden and the Memo is activated for the user
  101.   to enter the macro text. If the activator character already
  102.   has macro text associated with it, this text is placed
  103.   into the Memo for editing }
  104. var
  105.    ch : char;
  106. begin
  107.   If KeyCharEdit.Text <> '' then
  108.   begin
  109.     ch := KeyCharEdit.Text[1];
  110.     if ch = '' then
  111.      { do nothing }
  112.     else if not (UpCase( ch ) in ['A'..'Z']) then
  113.       MacKeyMsg.Caption := 'Key must be a letter.'
  114.     else
  115.     begin
  116.       KeyCharEdit.Hide;
  117.       MacKeyLabel.Caption := MKLABEL + UpCase(ch);
  118.       MacroMemo.ReadOnly := false;
  119.       MacroMemo.Text := MainForm.GetMacString( ch ); { get existing macro text }
  120.       ActiveControl := MacroMemo;
  121.     end;
  122.   end;
  123. end;
  124.  
  125. end.
  126.