unit TAlphabetBar;

interface
uses classes,controls,stdctrls,windows,graphics,extctrls,jpeg,sysutils,resourceskins,global,Transparentpanel,messages,forms;


type TVirtualKeyEvent = procedure(Sender: TObject;key : Word) of object;

type tbuttonmatrix = record
  tr : Trect;
  down : boolean;
end;

type tbutmatrix = array [0..25] of tbuttonmatrix;

type TAlphaBar=Class(TCustomControl)
 private
 buttonheight : real;
 FVirtualKeyPress: TVirtualKeyEvent;
 ffont : tfont;
 vcanv : tbitmap;
 buttonmatrix : tbutmatrix;

 public

 Procedure Resize; override;
 Procedure Paint;override;
 Procedure MouseDown (Button : TMouseButton;Shift : TShiftState;x: integer;y : integer);override;
 Procedure MouseUp(Button : TMouseButton;Shift : TShiftState;x: integer;y : integer);override;
 Constructor Create(AOwner : TComponent); override;
 Destructor Destroy; override;
 property OnVirtualKey : TVirtualKeyEvent read fvirtualkeypress write fvirtualkeypress;
 property Font : TFont read ffont write ffont;
 property btnmatrix : tbutmatrix read buttonmatrix;
 // procedure CreateParams(var Params : TCreateParams); override;
// procedure StopFlicker(var Msg: TWMEraseBkgnd); message WM_ERASEBKGND;
End;


implementation
uses jukeboxform;

{procedure Talphabar.StopFlicker(var Msg: TWMEraseBkgnd);
begin
SetBkMode( msg.DC, TRANSPARENT );
Msg.Result := 0;
end;

 }



Constructor TAlphaBar.Create(AOwner : TComponent);
begin

    inherited;
  //Create Components here
  Parent := (AOwner As TWincontrol);
  ffont:=tfont.Create;
  vcanv:=tbitmap.create;

end;

{procedure TAlphabar.CreateParams(var Params : TCreateParams);
begin
  inherited CreateParams(Params);
 Params.ExStyle:=Params.ExStyle or WS_EX_TRANSPARENT;
end;
 }

Procedure TAlphaBar.Resize;
var
x,y,yy : integer;
bInTransparent : boolean;
yStartTransparent : integer;
hRgnTemp :  HRGN;
hRgnOut: HRGN;
begin
if width=0  then exit;

inherited;
paint;
yStartTransparent:=0;
bInTransparent:=false;
yy:=0;

hRgnOut:= CreateRectRgn(0,0,width,height);
with vcanv.Canvas do begin

for x:= -1 to Width do begin
  For y := 0 to Height do begin
    If Pixels[x,y] = Tcolor(rgb(255,0,0)) Then begin
      If Not bInTransparent Then begin
        bInTransparent:= True;
        yStartTransparent:= y;
      end;
    end else begin
      If bInTransparent Then begin
        hRgnTemp:= CreateRectRgn(x,yStartTransparent,x+1,y);
        CombineRgn(hRgnOut, hRgnOut,hRgnTemp, RGN_XOR);
        deleteobject(hRgnTemp);
        bInTransparent := False;
      End;
    end;
     yy:=y;
  End;
  If bInTransparent Then begin
   hRgnTemp := CreateRectRgn(x,yStartTransparent,x+1,yy);
    CombineRgn (hRgnOut, hRgnOut,hRgnTemp, RGN_XOR);
    DeleteObject(hRgnTemp);
    bInTransparent:= False
  End;
End;

end;


//hRgnOut:=createrectrgn(200,20,250,40);



if setwindowrgn(self.Handle,hRgnOut,false)=0 then begin
beep;
end;

end;


Procedure TAlphaBar.Paint;
var
u,d : TBitmap;
n : integer;
tr : Trect;
c : string;
bmp : TBitmap;

begin
vcanv.Free;
vcanv:=tbitmap.create;
vcanv.Width:=width;
vcanv.Height:=height;
vcanv.Canvas.Brush.Color:=rgb(255,0,0);
vcanv.Canvas.Brush.Style:=bsSolid;
vcanv.canvas.pen.color:=rgb(255,0,0);
vcanv.canvas.Pen.Style:=psSolid;

vcanv.Canvas.Rectangle(0,0,width,height);

if height>width then buttonheight := height / 26 else buttonheight :=width /26;
vcanv.canvas.font.assign(ffont);

if height>width  then begin
 tr.Left:=0;
 tr.Right:=width;
 tr.Top:=0;
 tr.Bottom:=round(buttonheight);
end else begin
 tr.Left:=0;
 tr.Right:=round(buttonheight);
 tr.Top:=0;
 tr.Bottom:=height;
end;

bmp:=Tbitmap.Create;

u:=Tbitmap.Create;
u.Width:=tr.right;
u.Height:=tr.Bottom;
bmp.assign(LoadSkinResource('AlphaButtonUp',config.theme.salphabuttons.Skin,config.theme.salphabuttons.Color,false));
u.Canvas.StretchDraw(tr,bmp);


d:=Tbitmap.Create;
d.Width:=tr.right;
d.Height:=tr.Bottom;
bmp.assign(LoadSkinResource('AlphaButtonDown',config.theme.salphabuttons.Skin,config.theme.salphabuttons.Color,false));
d.Canvas.StretchDraw(tr,bmp);


bmp.free;

vcanv.canvas.Brush.Style:=bsClear;
with vcanv.canvas do begin
 for n := 0 to 25 do begin
 if height>width  then begin
  tr.Top:=round(n*buttonheight);
  tr.Bottom:=round((n*buttonheight)+buttonheight);
 end else begin
  tr.Left:=round(n*buttonheight);
  tr.Right:=round((n*buttonheight)+buttonheight);
 end;

   if buttonmatrix[n].down=false then BrushCopy(tr,u,u.Canvas.ClipRect,u.Canvas.Pixels[0,0]) else BrushCopy(tr,d,d.Canvas.ClipRect,d.Canvas.Pixels[0,0]);
  //if buttonmatrix[n].down=false then stretchdraw(tr,u) else stretchdraw(tr,d);
  c:=chr(97+n);
  TextOut(tr.left+(((tr.Right-tr.left) div 2))-canvas.TextWidth(c) div 2,tr.top+(((tr.bottom-tr.top) div 2))-canvas.TextHeight(c) div 2,c);
  buttonmatrix[n].tr:=tr;
 end;
end;
u.Free;
d.Free;


tr.Top:=0;
tr.left:=0;
tr.Right:=Width;
tr.Bottom:=Height;
canvas.Draw(0,0,vcanv);
//canvas.BrushCopy(tr,vcanv,tr,vcanv.canvas.pixels[0,0]);

end;


Procedure TAlphabar.MouseDown(Button: TMouseButton; Shift: TShiftState; x: Integer; y: Integer);
var
n : integer;
begin
for n := 0 to 25 do begin
if height>width then begin
 if (y>buttonmatrix[n].tr.top) and (y<buttonmatrix[n].tr.top+buttonheight) then begin
    buttonmatrix[n].down:=true;
    break;
   end;
end else begin
 if (x>buttonmatrix[n].tr.left) and (x<buttonmatrix[n].tr.left+buttonheight) then begin
    buttonmatrix[n].down:=true;
    break;
   end;
end;
end;
paint;
end;

Procedure TAlphabar.MouseUp(Button: TMouseButton; Shift: TShiftState; x: Integer; y: Integer);
var
  n: Integer;
  c : word;
begin
  for n := 0 to 25 do begin
    c:=VkKeyScan(chr(n+97));
    if buttonmatrix[n].down=true then if assigned(Onvirtualkey) then fvirtualkeypress(self,c);
    buttonmatrix[n].down:=false;
  end;
  paint;

end;

Destructor TAlphabar.Destroy;
begin
  inherited;
  ffont.Free;
end;


end.