unit pmclasses;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, controls, inifiles,
  comctrls;

type
  THelper = class(TControl)
  public
    property Color;
    property Font;
  end;

  TPanelAppearance = class
  public
    Font : TFont;
    BackGround : TColor;
    constructor Create;overload;
    constructor Create(acomponent : TWinControl);overload;
    procedure LoadFromIni(Section : string; IniFile : TIniFile);overload;
    procedure SaveToIni(Section : string; IniFile : TIniFile);overload;
    procedure ApplyColours(acomponent : TWinControl);
    procedure UseDefault;
    destructor destroy;override;
  end;

  TMessageHistoryItem = class
  public
    Subject, From, FileName, Folder : string;
  end;

procedure LoadHelperFromIni(AControl : TWinControl; IniFile : TIniFile);
procedure SaveHelperToIni(AControl : TWinCOntrol; IniFile : TIniFIle);
procedure AddObjectToHistoryStrings(AnObject : TMessageHistoryItem;
  StL : TStrings; maxitems : integer; AddToTop : Boolean);

implementation

procedure LoadHelperFromIni(AControl : TWinControl; IniFile : TIniFile);
begin
  THelper(AControl).Color :=
    TColor(IniFile.ReadInteger(AControl.Name, 'BackGround', clInfoBk));
  THelper(AControl).Font.Name := IniFile.ReadString(AControl.Name,
           'FontName', 'MS Sans Serif');
  THelper(AControl).Font.Size := IniFile.ReadInteger(AControl.Name,
            'FontSize', 8);
  THelper(AControl).Font.Color := TColor(IniFile.ReadInteger(AControl.Name,
            'FontColor', clBlack));
  THelper(AControl).Font.Style :=  TFontStyles(Byte(IniFile.ReadInteger(
           AControl.Name, 'FontStyle', Byte(0))));
end;

procedure SaveHelperToIni(AControl : TWinCOntrol; IniFile : TIniFIle);
begin
  IniFile.WriteInteger(AControl.Name, 'BackGround', THelper(AControl).Color);
  IniFile.WriteString(AControl.Name, 'FontName', THelper(AControl).Font.Name);
  Inifile.WriteInteger(AControl.Name, 'FontSize', THelper(AControl).Font.Size);
  Inifile.WriteInteger(AControl.Name, 'FontColor',
    THelper(AControl).Font.Color);
  IniFile.WriteInteger(AControl.Name, 'FontStyle',
    Byte(THelper(AControl).Font.Style));
end;


constructor TPanelAppearance.Create(Acomponent : TWinControl);
begin
  inherited Create;
  Font := TFont.Create;
  BackGround := THelper(AComponent).Color;
  Font.Assign(THelper(AComponent).Font);
end;

constructor TPanelAppearance.Create;
begin
  inherited Create;
  Font := TFont.Create;
  BackGround := clWindow;
end;

procedure TPanelAppearance.LoadFromIni(Section : string; IniFile : TIniFile);
begin
  BackGround :=
    TColor(IniFile.ReadInteger(Section, 'BackGround', clInfoBk));
  Font.Name := IniFile.ReadString(Section, 'FontName', 'MS Sans Serif');
  Font.Size := IniFile.ReadInteger(Section, 'FontSize', 8);
  Font.Color := TColor(IniFile.ReadInteger(Section, 'FontColor', clBlack));
  Font.Style :=  TFontStyles(Byte(IniFile.ReadInteger(Section,
    'FontStyle', Byte(0))));
end;

procedure TPanelAppearance.SaveToIni(Section : string; IniFile : TIniFile);
begin
  IniFile.WriteInteger(Section, 'BackGround', BackGround);
  IniFile.WriteString(Section, 'FontName', Font.Name);
  Inifile.WriteInteger(Section, 'FontSize', Font.Size);
  Inifile.WriteInteger(Section, 'FontColor', Font.Color);
  IniFile.WriteInteger(Section, 'FontStyle', Byte(Font.Style));
end;

procedure TPanelAppearance.UseDefault;
begin
  BackGround := clWhite;
  Font.Name := 'MS Sans Serif';
  Font.Size := 8;
  Font.Style := [];
  Font.Color := clBlack;
end;

procedure TPanelAppearance.ApplyColours(AComponent : TWinControl);
begin
  THelper(AComponent).Font.Assign(Font);
  THelper(AComponent).Color := Background;
end;

destructor TPanelAppearance.destroy;
begin
  Font.Free;
  inherited Destroy;
end;

procedure AddObjectToHistoryStrings(AnObject : TMessageHistoryItem;
  StL : TStrings; maxitems : integer; AddToTop : Boolean);
var x : integer;
begin
  if AnObject = nil then exit;

  if AddToTop then
    Stl.InsertObject(0, AnObject.Subject + ', From: ' + AnObject.From,
      AnObject)
  else
    StL.AddObject(AnObject.Subject + ', From: ' + AnObject.From, AnObject);

  while StL.Count > maxitems do
  begin
    // For the people who don't believe in Boolean magic:
    // If AddToTop then remove the *last* item else
    //   remove the *first* item.
    x := Ord(AddToTop) * (Stl.Count -1);
    if (Stl.Objects[x] <> nil)
      and (StL.Objects[x] is TMessageHistoryItem) then
    begin
      TMessageHistoryItem(Stl.Objects[x]).Free;
    end;
    StL.Delete(x);
  end;
end;


end.
