{ *************************************************************************** }
{                                                                             }
{ EControl Syntax Editor SDK                                                  }
{                                                                             }
{ Copyright (c) 2004 - 2008 EControl Ltd., Zaharov Michael                    }
{     www.econtrol.ru                                                         }
{     support@econtrol.ru                                                     }
{                                                                             }
{ *************************************************************************** }
{$I ecSyntEdit.INC}

unit ecsnStyleEdit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  {$IFNDEF EC_VCL6_UP} ecColCombo,{$ENDIF}
  StdCtrls, ExtCtrls, ecSyntAnal, ecStrUtils;

type
  TSyntStyleFrame = class(TFrame)
    Label2: TLabel;
    Label3: TLabel;
    ColorBox1: TColorBox;
    Button3: TButton;
    ColorBox2: TColorBox;
    GroupBox1: TGroupBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    Label1: TLabel;
    ComboBox1: TComboBox;
    Label4: TLabel;
    ComboBox2: TComboBox;
    FontDialog1: TFontDialog;
    CheckBox1: TCheckBox;
    GroupBox2: TGroupBox;
    ColorBox3: TColorBox;
    ComboBox3: TComboBox;
    Label5: TLabel;
    ColorBox4: TColorBox;
    ComboBox4: TComboBox;
    Label6: TLabel;
    ColorBox5: TColorBox;
    ComboBox5: TComboBox;
    Label7: TLabel;
    ColorBox6: TColorBox;
    ComboBox6: TComboBox;
    Label8: TLabel;
    CheckBox6: TCheckBox;
    CheckBox7: TCheckBox;
    Label9: TLabel;
    ComboBox7: TComboBox;
    procedure ComboBox1Change(Sender: TObject);
    procedure ComboBox2Change(Sender: TObject);
    procedure ColorBox1Change(Sender: TObject);
    procedure ColorBox2Change(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure ComboBox3DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure ComboBox3Change(Sender: TObject);
    procedure ColorBox3Change(Sender: TObject);
    procedure CheckBox6Click(Sender: TObject);
    procedure CheckBox7Click(Sender: TObject);
    procedure ComboBox7Change(Sender: TObject);
  private
    FSelfChange: Boolean;
    FStyle: TSyntaxFormat;
    procedure SetStyle(const Value: TSyntaxFormat);
    procedure SetControls;
    procedure SetEnabling;
    procedure UpdateFlags;
  public
    FullEdit: Boolean;
    DisableStyle: Boolean;
    constructor Create(AOwner: TComponent); override;
    property Style: TSyntaxFormat read FStyle write SetStyle;
  end;

procedure SetColorBox(ColorBox: TColorBox; Color: TColor);

implementation

{$R *.dfm}
{$IFDEF EC_DOTNET}
uses Types, Variants;
{$ENDIF}

procedure SetColorBox(ColorBox: TColorBox; Color: TColor);
var idx: integer;
begin
  ColorBox.Items.Objects[0] := TObject($00010101);
  idx := ColorBox.ItemIndex;
  ColorBox.Selected := Color;
  if idx = ColorBox.ItemIndex then
    ColorBox.Invalidate;
end;

{ TSyntStyleFrame }

constructor TSyntStyleFrame.Create(AOwner: TComponent);
begin
  inherited;
  FullEdit := False;
  SetControls;
end;

procedure TSyntStyleFrame.SetStyle(const Value: TSyntaxFormat);
begin
  FStyle := Value;
  SetControls;
end;

procedure TSyntStyleFrame.SetControls;
  procedure SetCheck(C: TCheckBox; en, s: Boolean);
  begin
    if not en then
      C.State := cbGrayed else
    if s then
      C.State := cbChecked else
      C.State := cbUnchecked;
  end;
begin
  ComboBox1.Visible := not FullEdit;
  Label1.Visible := not FullEdit;
  if FStyle <> nil then
   with FStyle do
   begin
     FSelfChange := True;

     SetColorBox(ColorBox1, BgColor);
     SetColorBox(ColorBox2, Font.Color);

     SetCheck(CheckBox2, ffBold in FormatFlags, fsBold in Font.Style);
     SetCheck(CheckBox3, ffItalic in FormatFlags, fsItalic in Font.Style);
     SetCheck(CheckBox4, ffUnderline in FormatFlags, fsUnderline in Font.Style);
     SetCheck(CheckBox5, ffStrikeout in FormatFlags, fsStrikeout in Font.Style);
     SetCheck(CheckBox1, ffHidden in FormatFlags, Hidden);
     SetCheck(CheckBox7, ffReadOnly in FormatFlags, ReadOnly);

     CheckBox6.Checked := MultiLineBorder;
     ComboBox1.ItemIndex := integer(FormatType);
     ComboBox2.ItemIndex := integer(VertAlignment);

     ComboBox3.ItemIndex := integer(BorderTypes[0]);
     ComboBox4.ItemIndex := integer(BorderTypes[1]);
     ComboBox5.ItemIndex := integer(BorderTypes[2]);
     ComboBox6.ItemIndex := integer(BorderTypes[3]);

     SetColorBox(ColorBox3, BorderColors[0]);
     SetColorBox(ColorBox4, BorderColors[1]);
     SetColorBox(ColorBox5, BorderColors[2]);
     SetColorBox(ColorBox6, BorderColors[3]);

     ComboBox7.ItemIndex := integer(ChangeCase);

     FSelfChange := False;
   end;
  SetEnabling;
end;

procedure TSyntStyleFrame.SetEnabling;
var b: Boolean;
  function GetColor: TColor;
  begin
    if b then Result := clWindow
     else Result := Color;
  end;
begin
  b := FStyle <> nil;
  Label1.Enabled := b;
  ComboBox1.Enabled := b and not DisableStyle;
  ComboBox1.Color := GetColor;
  Label2.Enabled := b;
  ColorBox1.Enabled := b;
  ColorBox1.Color := GetColor;

  b := b and ((FStyle.FormatType <> ftBackGround) or FullEdit);
  Label3.Enabled := b;
  ColorBox2.Enabled := b;
  ColorBox2.Color := GetColor;

  b := b and ((FStyle.FormatType <> ftColor) or FullEdit);
  Label4.Enabled := b;
  ComboBox2.Enabled := b;
  ComboBox2.Color := GetColor;
  CheckBox2.Enabled := b;
  CheckBox3.Enabled := b;
  CheckBox4.Enabled := b;
  CheckBox5.Enabled := b;

  b := b and ((FStyle.FormatType = ftCustomFont) or FullEdit);
  Button3.Enabled := b;
end;

procedure TSyntStyleFrame.ComboBox1Change(Sender: TObject);
begin
  if FSelfChange or (FStyle = nil) then Exit;
  FStyle.FormatType := TFormatType(ComboBox1.ItemIndex);
  SetEnabling;
end;

procedure TSyntStyleFrame.ComboBox2Change(Sender: TObject);
begin
  if FSelfChange or (FStyle = nil) then Exit;
  FStyle.VertAlignment := TVertAlignment(ComboBox2.ItemIndex);
end;

procedure TSyntStyleFrame.ColorBox1Change(Sender: TObject);
begin
  if FSelfChange or (FStyle = nil) then Exit;
  FStyle.BgColor := ColorBox1.Selected;
end;

procedure TSyntStyleFrame.ColorBox2Change(Sender: TObject);
begin
  if FSelfChange or (FStyle = nil) then Exit;
  FStyle.Font.Color := ColorBox2.Selected;
end;

procedure TSyntStyleFrame.UpdateFlags;
var fs: TFormatFlags;
begin
  fs := [];
  if CheckBox2.State <> cbGrayed then Include(fs, ffBold);
  if CheckBox3.State <> cbGrayed then Include(fs, ffItalic);
  if CheckBox4.State <> cbGrayed then Include(fs, ffUnderline);
  if CheckBox5.State <> cbGrayed then Include(fs, ffStrikeOut);
  if CheckBox1.State <> cbGrayed then Include(fs, ffHidden);
  if CheckBox7.State <> cbGrayed then Include(fs, ffReadOnly);
  FStyle.FormatFlags := fs;
end;

procedure TSyntStyleFrame.CheckBox2Click(Sender: TObject);
var fs: TFontStyles;
begin
  if FSelfChange or (FStyle = nil) then Exit;
  fs := [];
  if CheckBox2.Checked then Include(fs, fsBold);
  if CheckBox3.Checked then Include(fs, fsItalic);
  if CheckBox4.Checked then Include(fs, fsUnderline);
  if CheckBox5.Checked then Include(fs, fsStrikeout);
  FStyle.Font.Style := fs;
  UpdateFlags;
end;

procedure TSyntStyleFrame.CheckBox1Click(Sender: TObject);
begin
  FStyle.Hidden := CheckBox1.Checked;
  UpdateFlags;
end;

procedure TSyntStyleFrame.CheckBox7Click(Sender: TObject);
begin
  FStyle.ReadOnly := CheckBox7.Checked;
  UpdateFlags;
end;

procedure TSyntStyleFrame.Button3Click(Sender: TObject);
begin
  if FStyle = nil then Exit;
  FontDialog1.Font := FStyle.Font;
  if FontDialog1.Execute then
   begin
    FStyle.Font := FontDialog1.Font;
    SetControls;
   end;
end;

procedure TSyntStyleFrame.ComboBox3DrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
var y: integer;
begin
  y := (Rect.Top + Rect.Bottom) div 2;
  with TComboBox(Control) do
   begin
     Canvas.FillRect(Rect);
     Canvas.Pen.Color := Canvas.Font.Color;
     DrawBorder(Canvas, TBorderLineType(Index), Point(Rect.Left + 2, y), Point(Rect.Right - 2, y));
   end;
end;

procedure TSyntStyleFrame.ComboBox3Change(Sender: TObject);
begin
  if FSelfChange or (FStyle = nil) then Exit;
  with Sender as TComboBox do
   FStyle.BorderTypes[Tag] := TBorderLineType(ItemIndex);
end;

procedure TSyntStyleFrame.ColorBox3Change(Sender: TObject);
begin
  if FSelfChange or (FStyle = nil) then Exit;
  with Sender as TColorBox do
   FStyle.BorderColors[Tag] := Selected;
end;

procedure TSyntStyleFrame.CheckBox6Click(Sender: TObject);
begin
  FStyle.MultiLineBorder := CheckBox6.Checked;
end;

procedure TSyntStyleFrame.ComboBox7Change(Sender: TObject);
begin
  if FSelfChange or (FStyle = nil) then Exit;
  FStyle.ChangeCase := TChangeCase(ComboBox7.ItemIndex);
end;

end.
