unit FormatDialog;

interface

uses
	Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
		Dialogs, StdCtrls, ExtCtrls;

type
	TFormatDialog = class(TForm)
		btnOK: TButton;
		btnCancel: TButton;
		btnColor: TButton;
		btnNewLine: TButton;
		btnBold: TButton;
		btnItalic: TButton;
		btnUnderline: TButton;
		btnStrikeOut: TButton;
		btnNoStyle: TButton;
		Memo1: TMemo;
		ColorDialog1: TColorDialog;
		ColorBox1: TColorBox;
		cmbSize: TComboBox;
		cmbFont: TComboBox;
    btnNoColor: TButton;
    btnNoFont: TButton;
    btnNoSize: TButton;
		procedure btnCancelClick(Sender: TObject);
		procedure btnNewLineClick(Sender: TObject);
		procedure btnBoldClick(Sender: TObject);
		procedure btnItalicClick(Sender: TObject);
		procedure btnUnderlineClick(Sender: TObject);
		procedure btnStrikeOutClick(Sender: TObject);
		procedure btnNoStyleClick(Sender: TObject);
		procedure btnColorClick(Sender: TObject);
		procedure ColorBox1Select(Sender: TObject);
		procedure cmbSizeSelect(Sender: TObject);
		procedure FormCreate(Sender: TObject);
		procedure cmbFontSelect(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnNoFontClick(Sender: TObject);
    procedure btnNoSizeClick(Sender: TObject);
    procedure btnNoColorClick(Sender: TObject);
    procedure Memo1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
	private
    Bold, Italic, Underline, StrikeOut, NoStyle, Font, Size, Color: Boolean;
    procedure pBold;    
    procedure pItalic;
    procedure pUnderline;
    procedure pStrikeOut;
    procedure pNoStyle;
	public
		function Execute(var aString: String): Boolean;
	end;

var
	FormatDlg: TFormatDialog;

implementation

{$R *.dfm}

Uses
	PluginTools;

procedure TFormatDialog.FormCreate(Sender: TObject);
begin
  cmbFont.Items := Screen.Fonts;
	
	cmbFont.Hint := 'Font Face' + #13#10 + '[f=aFont] [/f]' + #13#10 + '[font=aFont] [/font]';
	cmbSize.Hint := 'Size' + #13#10 + '[s=aSize] [/s]' + #13#10 + '[size=aSize] [/size]';
	ColorBox1.Hint := 'Color' + #13#10 + '[c=aColor] [/c]' + #13#10 + '[color=aColor] [/color]';
	btnColor.Hint := 'Color' + #13#10 + '[c=aColor] [/c]' + #13#10 + '[color=aColor] [/color]';
	btnNewLine.Hint := 'New Line' + #13#10 + '[nl]';
	btnBold.Hint := 'Bold' + #13#10 + '[b] [/b]' + #13#10 + '[bold] [/bold]';
	btnItalic.Hint := 'Italic' + #13#10 + '[i] [/i]' + #13#10 + '[italic] [/italic]';
	btnUnderline.Hint := 'Underline' + #13#10 + '[u] [/u]' + #13#10 + '[underline] [/underline]';
	btnStrikeOut.Hint := 'StrikeOut' + #13#10 + '[o] [/o]' + #13#10 + '[so] [/so]' + #13#10 + '[strikeout] [/strikeout]';
	btnNoStyle.Hint := 'No Style' + #13#10 + '[n] [/n]' + #13#10 + '[nostyle] [/nostyle]';
end;

procedure TFormatDialog.FormShow(Sender: TObject);
begin
  Memo1.SetFocus;
end;

function TFormatDialog.Execute(var aString: String): Boolean;
var
	i: Integer;
begin
	Memo1.Clear;
	Memo1.Lines[0] := aString;

	Result := ShowModal = mrOK;

	if Result then
	begin
		aString := '';
		for i := 0 to Memo1.Lines.Count-1 do
			aString := aString + Memo1.Lines[i];
	end;
end;

procedure TFormatDialog.btnCancelClick(Sender: TObject);
begin
	self.Close;
end;

procedure TFormatDialog.Memo1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (ssCtrl in Shift) then
    case Key of
      13: Memo1.SelText := '[nl]' + Memo1.SelText;
      66: pBold;
      73: pItalic; 
      78: pNoStyle;   
      83: pStrikeOut;
      85: pUnderline;
    end;
end;

procedure TFormatDialog.pBold;
begin
  if Memo1.SelLength = 0 then
  begin
    if Bold then
    begin
      Memo1.SelText := '[/b]';
      Bold := False;
      btnBold.Caption := 'B';
    end
      else
    begin
      Memo1.SelText := '[b]';
      Bold := True;
      btnBold.Caption := 'B*';
    end;
  end
    else
      Memo1.SelText := '[b]' + Memo1.SelText + '[/b]';
end;

procedure TFormatDialog.pItalic;
begin
  if Memo1.SelLength = 0 then
  begin
    if Italic then
    begin
      Memo1.SelText := '[/i]';
      Italic := False;
      btnItalic.Caption := 'I';
    end
      else
    begin
      Memo1.SelText := '[i]';
      Italic := True;
      btnItalic.Caption := 'I*';
    end;
  end
    else
	    Memo1.SelText := '[i]' + Memo1.SelText + '[/i]';
end;

procedure TFormatDialog.pUnderline;
begin
  if Memo1.SelLength = 0 then
  begin
    if Underline then
    begin
      Memo1.SelText := '[/u]';
      Underline := False;
      btnUnderline.Caption := 'U';
    end
      else
    begin
      Memo1.SelText := '[u]';
      Underline := True;
      btnUnderline.Caption := 'U*';
    end;
  end
    else
	    Memo1.SelText := '[u]' + Memo1.SelText + '[/u]';
end;

procedure TFormatDialog.pStrikeOut;
begin
  if Memo1.SelLength = 0 then
  begin
    if StrikeOut then
    begin
      Memo1.SelText := '[/so]';
      StrikeOut := False;
      btnStrikeOut.Caption := 'SO';
    end
      else
    begin
      Memo1.SelText := '[so]';
      StrikeOut := True;
      btnStrikeOut.Caption := 'SO*';
    end;
  end
    else
	    Memo1.SelText := '[so]' + Memo1.SelText + '[/so]';
end;

procedure TFormatDialog.pNoStyle;
begin
  if Memo1.SelLength = 0 then
  begin
    if NoStyle then
    begin
      Memo1.SelText := '[/n]';
      NoStyle := False;
      btnNoStyle.Caption := 'N';
    end
      else
    begin
      Memo1.SelText := '[n]';
      NoStyle := True;
      btnNoStyle.Caption := 'N*';
    end;
  end
    else
	    Memo1.SelText := '[n]' + Memo1.SelText + '[/n]';
end;            

procedure TFormatDialog.btnNewLineClick(Sender: TObject);
begin
	Memo1.SelText := '[nl]' + #13#10 + Memo1.SelText;
	Memo1.SetFocus;
end;

procedure TFormatDialog.btnBoldClick(Sender: TObject);
begin
  pBold;
	Memo1.SetFocus;
end;

procedure TFormatDialog.btnItalicClick(Sender: TObject);
begin
  pItalic;
	Memo1.SetFocus;
end;

procedure TFormatDialog.btnUnderlineClick(Sender: TObject);
begin
  pUnderline;
	Memo1.SetFocus;
end;

procedure TFormatDialog.btnStrikeOutClick(Sender: TObject);
begin
  pStrikeOut;
	Memo1.SetFocus;
end;

procedure TFormatDialog.btnNoStyleClick(Sender: TObject);
begin
  pNoStyle;
	Memo1.SetFocus;
end;

procedure TFormatDialog.btnColorClick(Sender: TObject);
begin
	if ColorDialog1.Execute then
  begin
    if Memo1.SelLength = 0 then
    begin
      Memo1.SelText := '[c=' + ColorToString(ColorDialog1.Color) + ']';
      Color := True;
      btnNoColor.Caption := '/C*';
    end
      else
		    Memo1.SelText := '[c=' + ColorToString(ColorDialog1.Color) + ']' + Memo1.SelText + '[/c]';
  end;
	Memo1.SetFocus;
end;

procedure TFormatDialog.ColorBox1Select(Sender: TObject);
begin
  if Memo1.SelLength = 0 then
  begin
    Memo1.SelText := '[c=' + ColorToString(ColorBox1.Selected) + ']';
    Color := True;
    btnNoColor.Caption := '/C*';
  end
    else
	    Memo1.SelText := '[c=' + ColorToString(ColorBox1.Selected) + ']' + Memo1.SelText + '[/c]';
	Memo1.SetFocus;
end;

procedure TFormatDialog.btnNoColorClick(Sender: TObject);
begin
  Memo1.SelText := '[/c]' + Memo1.SelText;
  Color := False;
  btnNoColor.Caption := '/C';
end;

procedure TFormatDialog.cmbSizeSelect(Sender: TObject);
begin
  if Memo1.SelLength = 0 then
  begin
    Memo1.SelText := '[s=' + cmbSize.Text + ']';
    Size := True;
    btnNoSize.Caption := '/S*';
  end
    else
	    Memo1.SelText := '[s=' + cmbSize.Text + ']' + Memo1.SelText + '[/s]';
	Memo1.SetFocus;
end;

procedure TFormatDialog.btnNoSizeClick(Sender: TObject);
begin
  Memo1.SelText := '[/s]' + Memo1.SelText;
  Size := False;
  btnNoSize.Caption := '/S';
end;

procedure TFormatDialog.cmbFontSelect(Sender: TObject);
begin
  if Memo1.SelLength = 0 then
  begin
    Memo1.SelText := '[f=' + cmbFont.Text + ']';
    Font := True;
    btnNoFont.Caption := '/F*';
  end
    else
	    Memo1.SelText := '[f=' + cmbFont.Text + ']' + Memo1.SelText + '[/f]';
	Memo1.SetFocus;
end;

procedure TFormatDialog.btnNoFontClick(Sender: TObject);
begin
  Memo1.SelText := '[/f]' + Memo1.SelText;
  Font := False;
  btnNoFont.Caption := '/F';
end;

end.

