unit MFMain;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  SRVControl, SRVSkinManager, SRVImagesScroll, ExtCtrls, ImgList,
  SRVTabSet, StdCtrls, jpeg, SclRView, Buttons, SRVButton, SRVImageScrollUtils;

type
  TFMain = class(TForm)
    ilFavorite: TImageList;
    SRVSkinManager1: TSRVSkinManager;
    PanelTop: TPanel;
    imgLogo: TImage;
    CodeUserText: TLabel;
    SRVImagesScroll1: TSRVImagesScroll;
    tsChannels: TSRVTabSet;
    SRichViewEdit1: TSRichViewEdit;
    btnAbout: TSRVButton;
    procedure SRVImagesScroll1DrawBorder(Sender: TSRVImagesScroll;
      Canvas: TCanvas; ARect, PaintRect: TRect; State: TSRVDrawStates;
      var DoDefault: Boolean);
    procedure SRVImagesScroll1DrawBackground(Sender: TSRVImagesScroll;
      Canvas: TCanvas; ARect, PaintRect: TRect; State: TSRVDrawStates;
      var DoDefault: Boolean);
    procedure SRVImagesScroll1DrawImageScroll(Sender: TSRVImagesScroll;
      Canvas: TCanvas; ARect, PaintRect: TRect; ImageScrollIndex: Integer;
      State: TSRVDrawStates; var DoDefault: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure tsChannelsChange(Sender: TObject);
    procedure SRVImagesScroll1Change(Sender: TObject);
    procedure btnAboutClick(Sender: TObject);
  private
    { Private declarations }
    bDrawImage, bgDrawImage, bBorderDBL, bgBorderDBL, bBorderDBR,
    bgBorderDBR : TBitmap;
  public
    { Public declarations }
  end;

var
  FMain: TFMain;

implementation

uses Math;

{$R *.DFM}

procedure TFMain.FormCreate(Sender: TObject);
begin
  bDrawImage := TBitmap.Create;
  bgDrawImage := TBitmap.Create;
  bBorderDBL := TBitmap.Create;
  bgBorderDBL := TBitmap.Create;
  bBorderDBR := TBitmap.Create;
  bgBorderDBR := TBitmap.Create;
  bBorderDBL.PixelFormat := pf24bit;
  bgBorderDBL.PixelFormat := pf24bit;
  bBorderDBR.PixelFormat := pf24bit;
  bgBorderDBR.PixelFormat := pf24bit;
  bDrawImage.PixelFormat := pf24bit;
  bgDrawImage.PixelFormat := pf24bit;
  bDrawImage.Width := SRVImagesScroll1.ItemWidth;
  bDrawImage.Height := SRVImagesScroll1.ItemHeight;
  bgDrawImage.Width := bDrawImage.Width;
  bgDrawImage.Height := (bDrawImage.Height div DEF_IMG_HEIGHT_DIVISOR) + 1;

  ilFavorite.Width := SRVImagesScroll1.ItemWidth;
  ilFavorite.Height := SRVImagesScroll1.ItemHeight;

  tsChannels.TabIndex := 0;
end;

procedure TFMain.FormDestroy(Sender: TObject);
begin
  bBorderDBL.Free;
  bgBorderDBL.Free;
  bBorderDBR.Free;
  bgBorderDBR.Free;
  bgDrawImage.Free;
  bDrawImage.Free;
end;
{------------------------------------------------------------------------------}
// Applies special effects when drawing ImagesScroll1
procedure TFMain.SRVImagesScroll1DrawBorder(Sender: TSRVImagesScroll;
  Canvas: TCanvas; ARect, PaintRect: TRect; State: TSRVDrawStates;
  var DoDefault: Boolean);
const
     W = 33;
begin
  DoDefault := False;
  // shading left side
  if (bBorderDBL.Width <> W) then begin
    bBorderDBL.Width := W;
    bBorderDBL.Height := ARect.Bottom + 1;
    bgBorderDBL.Width := W;
    bgBorderDBL.Height := ARect.Bottom + 1;
    bgBorderDBL.Canvas.Draw(-SRVImagesScroll1.Left, -SRVImagesScroll1.Top,
      imgLogo.Picture.Graphic);
  end;
  bBorderDBL.Canvas.CopyRect(Bounds(0, 0, W, ARect.Bottom + 1),
    Canvas, Bounds(ARect.Left, ARect.Top, W, ARect.Bottom + 1));
  ShadeImageLeft(bBorderDBL, bgBorderDBL);
  Canvas.Draw(ARect.Left, ARect.Top, bBorderDBL);
  // shading right side
  if (bBorderDBR.Width <> W) then begin
    bBorderDBR.Width := W;
    bBorderDBR.Height := ARect.Bottom + 1;
    bgBorderDBR.Width := W;
    bgBorderDBR.Height := ARect.Bottom + 1;
    bgBorderDBR.Canvas.Draw(
      -SRVImagesScroll1.Left - SRVImagesScroll1.Width + W,
      -SRVImagesScroll1.Top,
      imgLogo.Picture.Graphic);
  end;
  bBorderDBR.Canvas.CopyRect(Bounds(0, 0, W, ARect.Bottom + 1),
    Canvas, Bounds(ARect.Right - W, ARect.Top, W, ARect.Bottom + 1));
  ShadeImageRight(bBorderDBR, bgBorderDBR);
  Canvas.Draw(ARect.Right - W, ARect.Top, bBorderDBR);
end;
{------------------------------------------------------------------------------}
// Drawing a background image for SRVImagesScroll1
procedure TFMain.SRVImagesScroll1DrawBackground(Sender: TSRVImagesScroll;
  Canvas: TCanvas; ARect, PaintRect: TRect; State: TSRVDrawStates;
  var DoDefault: Boolean);
begin
  DoDefault := False;
  Canvas.Draw(
    ARect.Left - SRVImagesScroll1.Left,
    ARect.Top - SRVImagesScroll1.Top, imgLogo.Picture.Graphic);
end;
{------------------------------------------------------------------------------}
// Drawing SRVImagesScroll1's item
procedure TFMain.SRVImagesScroll1DrawImageScroll(Sender: TSRVImagesScroll;
  Canvas: TCanvas; ARect, PaintRect: TRect; ImageScrollIndex: Integer;
  State: TSRVDrawStates; var DoDefault: Boolean);
begin
  if ImageScrollIndex < ilFavorite.Count then begin
    DoDefault := False;
    // preparing the image
    ilFavorite.Draw(bDrawImage.Canvas, 0, 0, SRVImagesScroll1.Items[ImageScrollIndex].Tag);
    // highlighting "hot" image
    if srvdsHot in State then
      LightImage(bDrawImage);
    // drawing
    Canvas.Draw(ARect.Left, ARect.Top, bDrawImage);
    // drawing a mirrored "shadow" below the image
    bgDrawImage.Canvas.Draw(
      -ARect.Left - SRVImagesScroll1.Left,
      -ARect.Bottom - SRVImagesScroll1.Top,
      imgLogo.Picture.Graphic);
    MirrorImage(bDrawImage, bgDrawImage);
    Canvas.CopyRect(
      Bounds(ARect.Left, ARect.Bottom, bgDrawImage.Width, bgDrawImage.Height),
      bgDrawImage.Canvas,
      Bounds(0, 0, bgDrawImage.Width, bgDrawImage.Height));
  end else begin
    Canvas.Brush.Style := bsSolid;
    Canvas.Brush.Color := clBlack;
    Canvas.FillRect(ARect);
  end;
end;
{------------------------------------------------------------------------------}
// Changing a channel (News | Moto | Cinema | Business)
procedure TFMain.tsChannelsChange(Sender: TObject);
var
     bmp : TBitmap;
     jpg : TJPEGImage;
     Path : String;
     Index : Integer;
     SI : TSRVImageScrollItem;
begin
  // loading images in ilFavorite;
  // Channel # is stored in tsChannels.Tabs[].Tag
  ilFavorite.Clear;
  SRVImagesScroll1.Items.Clear;
  SRVImagesScroll1.BeginUpdate;
  Path := ExtractFilePath(Application.ExeName) + 'Channels\' +
    IntToStr(tsChannels.Tabs[tsChannels.TabIndex].Tag) + '\';

  bmp := TBitmap.Create;
  bmp.Width := SRVImagesScroll1.ItemWidth;
  bmp.Height := SRVImagesScroll1.ItemHeight;

  jpg := TJPEGImage.Create;

  Index := 1;
  while FileExists(Path + IntToStr(Index) + '.jpg') do begin
    bmp.Canvas.Lock;
    bmp.Canvas.Brush.Color := clBlack;
    bmp.Canvas.Brush.Style := bsSolid;
    bmp.Canvas.Pen.Color := $00CFCCC8;
    bmp.Canvas.Pen.Style := psSolid;
    bmp.Canvas.Rectangle(0, 0, bmp.Width, bmp.Height);

    jpg.LoadFromFile(Path + IntToStr(Index) + '.jpg');
    bmp.Canvas.Draw((bmp.Width - jpg.Width) div 2, 0, jpg);

    bmp.Canvas.Brush.Style := bsClear;
    bmp.Canvas.Pen.Color := $00CFCCC8;
    bmp.Canvas.Rectangle(0, 0, bmp.Width, bmp.Height);

    ilFavorite.Add(bmp, nil);

    SI := TSRVImageScrollItem(SRVImagesScroll1.Items.Add);
    SI.Tag := Index-1;

    Inc(Index);
  end;
  SRVImagesScroll1.ItemIndex := -1;
  SRVImagesScroll1.EndUpdate;
  SRVImagesScroll1Change(SRVImagesScroll1);

  bmp.Free;
  jpg.Free;
end;
// Changing an article; In this demo, there is no correspondence between an image
// and an article. It's just a demo
procedure TFMain.SRVImagesScroll1Change(Sender: TObject);
var
     Path : String;
begin
  Path := ExtractFilePath(Application.ExeName) + 'Channels\' +
    IntToStr(tsChannels.Tabs.Items[tsChannels.TabIndex].Tag) + '\';

  If SRVImagesScroll1.ItemIndex >= 0 then
    Path := Path + IntToStr(SRVImagesScroll1.ItemIndex mod 3) + '.rvf'
  else
    Path := Path + 'blank.rvf';

  SRichViewEdit1.RichViewEdit.Clear;
  SRichViewEdit1.LoadRVF(Path);
  SRichViewEdit1.ViewProperty.ZoomMode := SRichViewEdit1.ViewProperty.ZoomModeEdit;
end;

procedure TFMain.btnAboutClick(Sender: TObject);
begin
  MessageBox(Handle, 'Example for SRVControls', 'About', MB_OK or MB_ICONINFORMATION);
end;

end.
