unit printcatalog;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,printers,extctrls,loadjpegorbmp,burn, ComCtrls,global;

type
  Tprintcat = class(TForm)
    GroupBox1: TGroupBox;
    printproperties: TButton;
    print: TButton;
    cancel: TButton;
    printprinters: TComboBox;
    GroupBox2: TGroupBox;
    printall: TRadioButton;
    printcurrent: TRadioButton;
    GroupBox3: TGroupBox;
    printbackdrop: TCheckBox;
    printpagenumbers: TCheckBox;
    Label1: TLabel;
    printtitle: TEdit;
    PrinterSetup: TPrinterSetupDialog;
    progressgroup: TGroupBox;
    Label2: TLabel;
    ProgressBar: TProgressBar;
    progressalbum: TLabel;
    abort: TButton;
    printlength: TCheckBox;
    printcollate: TCheckBox;
    procedure FormCreate(Sender: TObject);
    procedure printpropertiesClick(Sender: TObject);
    procedure printClick(Sender: TObject);
    procedure printprintersChange(Sender: TObject);
    procedure abortClick(Sender: TObject);
    procedure printcurrentClick(Sender: TObject);
    procedure printallClick(Sender: TObject);
    procedure cancelClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
procedure showprint;

const
fontratio = 84.43373494;

var
  printcat: Tprintcat;
  fontfactor : real;
  pcount : integer;
  textoffset : integer;
implementation
uses main;

{$R *.DFM}


type ttracklist = record
  count : integer;
  title : array of string;
end;

var
x,albumheight,albumwidth,pagey,pagex,albumspace : integer;
cancelop : boolean;
offsetx : integer;

procedure showprint;
begin
printcat := Tprintcat.Create(Application);
  try
    pcount:=(albumcount div 6)+1;
    printcat.printtitle.text:='Album Catalogue '+datetostr(now);
    printcat.printall.caption:='Print all '+inttostr(albumcount)+ ' albums ('+inttostr(pcount)+' pages)';
    printcat.Showmodal;
    finally
    printcat.free;
  end;
end;

function gettitles (albumindex : integer; var titles : ttracklist) : boolean;
var
content : textfile;
spp : string;
begin
  AssignFile(content,config.path+album[albumindex].artist+'__'+album[albumindex].album+'\title.dat');
  spp:=album[albumindex].artist+'__'+album[albumindex].album;
  titles.count:=0;
  setlength(titles.title,titles.count);
 try
  Reset(content);
 except;
  result:=false;
  exit;
 end;

              While (NOT EOF(content)) do begin
                   inc(titles.count);
                   setlength(titles.title,titles.count);
                   Readln (content,spp);
                   if spp<>'' then titles.title[titles.count-1]:=spp else titles.title[titles.count-1]:='Unknown track title'
             end;
result:=true;
CloseFile(content);
end;

procedure printalbum(index : integer);
var
titles : ttracklist;
n : integer;
o,p : tbitmap;
y : integer;
tr : trect;
trackstart : integer;
tp : string;
tsecs,mins,secs: integer;
slen,title : string;
gottitles : boolean;
allowablechars : integer;
begin
if (pagey+albumheight+ALBUMSPACE)>printer.pageheight then begin
  printer.newpage;
  pagey:=0;
end;

y:=pagey;
x:=pagex;


if printcat.printbackdrop.checked=true then begin
loadjpegbmp(config.path+'\'+album[index].artist +'__'+album[index].album,p);
o:=tbitmap.create;
o.pixelformat:=pf24bit;
o.width:=ALBUMWIDTH;
o.height:=ALBUMHEIGHT;
o.handletype:=bmDIB;
o.assign(p);
p.free;
application.processmessages;
alphablendbitmapwithcolor(o,clWhite,o,0.4);
application.processmessages;
tr.Left:=pagex;
tr.Top:=pagey;
tr.Right:=x+albumwidth;
tr.Bottom:=y+albumheight;
printer.canvas.stretchdraw(tr,o);
o.freeimage;
o.free;
end;

if cancelop=true then exit;

x:=pagex+(albumspace div 4);
printer.canvas.font.name:=JUKEBOXFONT;
printer.canvas.font.height:=MulDiv(GetDeviceCaps(Printer.Canvas.Handle,LOGPIXELSY), round(10*fontfactor), 72);
printer.canvas.font.style:=[fsbold];
title:=album[index].artist +' - '+album[index].album;

printer.canvas.pen.width:=2;
printer.canvas.brush.style:=bsClear;
printer.canvas.rectangle(pagex,pagey,pagex+ALBUMWIDTH,pagey+ALBUMHEIGHT);

allowablechars:=50;
if length(title)>=allowablechars then begin
  title:=copy(title,1,allowablechars-3)+'...';
end;

printer.canvas.font.color:=clwhite;
printer.canvas.textout(x+textoffset,y+textoffset,title);
printer.canvas.font.color:=clblack;

printer.canvas.textout(x,y,title);

trackstart:=pagey+round(printer.canvas.textheight(title)*1.5);
y:=trackstart;
x:=pagex+(albumspace div 2);
gottitles:=gettitles(index,titles);
allowablechars:=42;
printer.canvas.font.height:=MulDiv(GetDeviceCaps(Printer.Canvas.Handle,LOGPIXELSY), round(10*fontfactor), 72);
if titles.count>20 then begin
  printer.canvas.font.height:=MulDiv(GetDeviceCaps(Printer.Canvas.Handle,LOGPIXELSY), round(6*fontfactor), 72);
  allowablechars:=60;
end;
if titles.count>32 then begin
allowablechars:=32;
end;

printer.canvas.font.style:=[];

if gottitles=true then begin;
   for n:=0 to titles.count-1 do begin
       application.processmessages;
       tp:=config.path+'\'+album[index].artist +'__'+album[index].album+'\track'+format('%.2d',[n+1])+'.mp3';
       if cancelop=true then exit;

       if printcat.printlength.checked=true then begin
       if fileexists(tp) then begin
         tsecs:=getmp3length(tp);
         secs:= tsecs mod 60;
         mins:= tsecs div 60;
         slen:=' ['+inttostr(mins)+':'+format('%.2d',[secs])+']';
       end
       else slen:='[?:??]';
       end else slen:='';


       if n+1<10 then titles.title[n]:='0'+inttostr(n+1)+') '+titles.title[n] else titles.title[n]:=inttostr(n+1)+') '+titles.title[n];

       if length(titles.title[n])>=allowablechars then begin
          titles.title[n]:=copy(titles.title[n],1,allowablechars-3)+'...';
       end;

       titles.title[n]:=titles.title[n]+slen;
       printer.canvas.font.color:=clwhite;
       printer.canvas.textout(x+textoffset,y+textoffset,titles.title[n]);
       printer.canvas.font.color:=clblack;
       printer.canvas.textout(x,y,titles.title[n]);



       y:=y+printer.canvas.textheight(titles.title[n]);
       if (y+(printer.canvas.textheight(titles.title[n])*2))>(pagey+albumheight) then begin
           if x=pagex+(albumwidth div 2) then begin
            printer.canvas.font.style:=[fsitalic];
            printer.canvas.font.color:=clwhite;
            printer.canvas.textout(x+textoffset,y+textoffset,'                     (more tracks on disc)');
            printer.canvas.font.color:=clblack;
            printer.canvas.textout(x,y,'                     (more tracks on disc)');

            break;
           end;
           x:=pagex+(albumwidth div 2);
           y:=trackstart;
       end;
   end;
end;
end;


procedure Tprintcat.FormCreate(Sender: TObject);
var
n : integer;
begin
for n:=0 to printer.printers.count-1 do printprinters.items.add(printer.printers[n]);
printprinters.itemindex:=printer.printerindex;
end;

procedure Tprintcat.printpropertiesClick(Sender: TObject);
begin
Printersetup.execute;
end;

procedure Tprintcat.printClick(Sender: TObject);
var
hdcscreen : hdc;
x : integer;
headtext : string;
albumod : integer;

procedure printheader(pagenum : integer);
begin
 pagey:=0;
 if printer.pageheight>printer.pagewidth then offsetx:=(printer.pagewidth-((albumwidth*2)+albumspace)) div 2 else offsetx:=(printer.pagewidth-((albumwidth*3)+albumspace*2)) div 2;
 Printer.Canvas.Font.PixelsPerInch:=GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSY);

 pagex:=offsetx;
 printer.Canvas.font.name:=JUKEBOXFONT;
 printer.Canvas.font.height:=MulDiv(GetDeviceCaps(Printer.Canvas.Handle,LOGPIXELSY), round(12*fontfactor), 72);
 headtext:=printtitle.text;
if printtitle.text<>'' then begin
printer.canvas.textout(((printer.pagewidth div 2)-(printer.canvas.textwidth(headtext) div 2)),0,headtext);
end;
 pagey:=printer.canvas.textheight(printtitle.text)*2;
 if printpagenumbers.checked=true then begin
   printer.canvas.font.style:=[fsitalic];
   printer.canvas.textout((printer.pagewidth-(offsetx div 2))-printer.canvas.textwidth('Page '+inttostr(pagenum)),0,'Page '+inttostr(pagenum));
 end;
end;


procedure printpage(no : integer);
var
x,t: integer;
pagenum : integer;
begin
pagenum:=no;

if no=1 then no:=1 else no:=((6*no)-6)+1;
if (no+6) < albumcount then t:=no+5 else t:=albumcount;



   for x:=no to t do begin
   try
   printalbum(x);
   progressbar.position:=progressbar.position+1;
   except
   messagedlg('There was an unknown error printing the document. ',mterror,[mbOK],0);
   printer.abort;
   DeleteDC(hdcScreen);
   progressgroup.visible:=false;
   screen.cursor:=crDefault;
   exit;
   end;

   if cancelop=true then begin
   exit;
   end;

      if x mod albumod = 0 then begin
      pagex:=offsetx;
      pagey:=pagey+albumheight+albumspace;
      end
      else begin
      pagex:=offsetx+albumwidth+albumspace;
      if (printer.pagewidth>printer.pageheight) and (not (x mod 2=0)) then  pagex:=offsetx+(albumwidth*2)+(albumspace*2);
      end;
    progressalbum.caption:=album[x].artist+' - '+album[x].album+#13#10#13#10+'Page '+inttostr(pagenum)+' of '+inttostr(pcount);
    application.processmessages;
   end;
end;



begin
fontfactor:=(printer.pageheight/83)/fontratio;
textoffset:=round(5*fontfactor);
fontfactor:=1;
cancelop:=false;
progressbar.position:=0;
progressgroup.visible:=true;
screen.cursor:=crHourGlass;
hdcScreen := CreateDC('DISPLAY', nil, nil, nil);
albumspace:=printer.pagewidth div 80;
if printer.pageheight> printer.pagewidth then begin
   albumheight:=(printer.pageheight div 3) - (albumspace*4);
   albumwidth:=albumheight;
   albumod:=2;
end else begin
   albumwidth:=(printer.pagewidth div 3) - (albumspace*4);
   albumheight:=albumwidth;
   albumod:=3;
end;


printer.title:='Empathy - '+printtitle.text;
printer.begindoc;


if printcurrent.checked=true then begin
   printtitle.text:='';
   printpagenumbers.checked:=false;
   printheader(1);
   progressgroup.visible:=true;
   progressbar.min:=0;
   progressbar.max:=1;
   albumwidth:=round(4.6875*getdevicecaps(printer.Handle,LOGPIXELSX));
   albumheight:=round(4.625*getdevicecaps(printer.Handle,LOGPIXELSY));
   if mainform.playlistbut.checked=true then printalbum(mainform.dalbumlist.itemindex+1) else printalbum(mainform.albumlist.itemindex+1);
   progressbar.position:=1;
   application.processmessages;
end;

if printall.checked=true then begin
   progressbar.min:=1;
   progressbar.max:=albumcount+1;

   if printcollate.checked=false then begin
   printheader(1);
    for x:=1 to pcount do begin
     printpage(x);
     if x<pcount then begin
       printer.NewPage;
       printheader(x+1);
     end;
    if cancelop=true then begin
    printer.abort;
    break;
    end;
    end;
   end;

    if printcollate.checked=true then begin
    printheader(pcount);
    for x:=pcount downto 1 do begin
     printpage(x);
     if x>1 then begin
       printer.NewPage;
       printheader(x-1);
     end;
    if cancelop=true then begin
        printer.abort;
        break;
       end;
      end;
   end;
   progressbar.position:=progressbar.max;
   application.processmessages;
end;


if cancelop=false then printer.enddoc;
DeleteDC(hdcScreen);
progressgroup.visible:=false;
screen.cursor:=crDefault;
modalresult:=mrOK;
end;

procedure Tprintcat.printprintersChange(Sender: TObject);
begin
printer.printerindex:=printprinters.itemindex;
end;

procedure Tprintcat.abortClick(Sender: TObject);
begin
cancelop:=true;
end;

procedure Tprintcat.printcurrentClick(Sender: TObject);
begin
printpagenumbers.enabled:=false;
printtitle.enabled:=false;
end;

procedure Tprintcat.printallClick(Sender: TObject);
begin
printpagenumbers.enabled:=true;
printtitle.enabled:=true;
end;

procedure Tprintcat.cancelClick(Sender: TObject);
begin
cancelop:=true;
end;

end.