{Program Listing Express - Program Copyright (C) Doug Overmyer 9/2/91}
{Begun 8/2/91}
{1.1 9/15/91 change BN5, add sculpted static text fields }
{1.2 9/23/91 revise printer method to eliminate double spacing of over-wide
	lines;force enumeration and reselection of font after running control panel;
	explicitly 	dispose of Faces collection; better redraw of PSText objects}

program PLXpress;

{$S-}
{$R plx.RES}
{$R-}
uses WinTypes,WinProcs,Strings,WObjects,WOPlus,WFPlus,StdDlgs;

const
  cm_FOpen   = 101;     {menuitem FileOpen    }
  cm_FPrint  = 102;     {menuitem FilePrint   }
  cm_FSetUp  = 103;     {menuitem FilePageSetup}
  cm_FExit   = 104;     {menuitem FileExit    }
  cm_TFont   = 111;     {menuitem TextFont    }
  id_But1    = 201;     {User defined button 1}
  id_But2    = 202;     {      "             2}
  id_But3    = 203;     {      "             3}
  id_But4    = 204;     {      "             3}
  id_But5    = 205;     {      "             5}
  id_D1Lb1   = 301;     {List box element in Dlg1}
  id_St1     = 401;     {Static text 1        }
  id_St2     = 402;     {Static text 2        }
  id_St3     = 403;     {Static text 3        }
  id_St4     = 404;     {Static text 4        }
  id_D3Setup = 501;     {Setup button in  Dlg3}
  id_D3OKPrt = 521;     {OK button in Dlg3    }
  id_D2EC1   = 603;     {Edit Control 1 in Dlg2}
  id_D2EC2   = 605;     {             2        }
  id_D2EC3   = 607;     {             3        }
  id_D2EC4   = 609;     {             4        }
  id_D2EC5   = 617;     {             5        }
  id_D2CB1   = 612;     {Check box 1 in Dlg2   }
  id_D2CB2   = 613;     {Check box 2 in Dlg2   }
  id_D2CB3   = 614;     {Check box 3 in Dlg2   }
  id_D2CB4   = 615;     {Check box 4 in Dlg2   }
  id_D2CB5   = 619;     {Check box 5 in Dlg2   }
  id_D2CB6   = 620;     {Check box 6 in dlg2   }
  id_D2OK    = 601;     {OK button in Dlg2     }
  id_D4LB1	 = 701;     {List box       in Dlg4}
  id_D4LB2   = 702;     {List box in Dlg4      }
  idm_About  = 801;     {menu id for PLX_About menu}
  idm_RunCP  = 802;     {menu id for run control panel}
  um_FilePrint = 803;   {User defined message }
{******************************************************************}
{ Types                                                            }
{******************************************************************}
type
	TPLXApplication = object(TApplication)
   	procedure InitMainWindow;virtual;
	end;

type
	pFormatRec = ^FormatRec;
	FormatRec = record
  ShowRuler,ShowFName,ShowDTStamp,ShowPageNum,ShowLineNum,UseCCB:Integer;
end;

PPLXDlg2 = ^TPLXDlg2;
TPLXDlg2 = object(TDialog)              {Page setup dialog}
	Margins:TRect;
  Format:FormatRec;
  TabSize:Integer;
	procedure WMInitDialog(var Msg:TMessage);virtual wm_First+wm_InitDialog;
  procedure IDD2OK(var Msg:TMessage); virtual id_First+id_D2OK;
  procedure IDD2EC1(var Msg:TMessage);virtual id_First+id_D2EC1;
  procedure IDD2EC2(var Msg:TMessage);virtual id_First+id_D2EC2;
  procedure IDD2EC3(var Msg:TMessage);virtual id_First+id_D2EC3;
  procedure IDD2EC4(var Msg:TMessage);virtual id_First+id_D2EC4;
  procedure IDD2EC5(var Msg:TMessage);virtual id_First+id_D2EC5;
  procedure IDD2CB1(var Msg:TMessage);virtual id_First+id_D2CB1;
  procedure IDD2CB2(var Msg:TMessage);virtual id_First+id_D2CB2;
  procedure IDD2CB3(var Msg:TMessage);virtual id_First+id_D2CB3;
  procedure IDD2CB4(var Msg:TMessage);virtual id_First+id_D2CB4;
  procedure IDD2CB5(var Msg:TMessage);virtual id_First+id_D2CB5;
  procedure IDD2CB6(var Msg:TMessage);virtual id_First+id_D2CB6;
	end;


PPLXDlg3 = ^TPLXDlg3;
TPLXDlg3 = object(TDialog)              {Print setup dialog}
	procedure WMInitDialog(var Msg:TMessage);virtual wm_First+wm_InitDialog;
  procedure IDSetup(var Msg:TMessage);virtual id_First+id_D3Setup;
  procedure IDOKPrt(var Msg:TMessage);virtual id_First+id_D3OKPrt;
	end;

PPLXDlg4 = ^TPLXDlg4;                     {Type Faces & Sizes Dialog}
TPLXDlg4 = object(TDialog)
	FontSize: Integer;
	procedure WMInitDialog(var Msg:TMessage);virtual wm_First+wm_InitDialog;
  procedure IDD4Lb1(var Msg:TMessage);virtual id_First+id_D4Lb1;
  procedure IDD4Lb2(var Msg:TMessage);virtual id_First+id_D4Lb2;
  procedure LoadSizes;virtual;
	end;


type                          {convert TLogFont records to objects}
PFontItem = ^TFontItem;
TFontItem = object(TObject)
	LogFont:TLogFont;
  FontType:Integer;
  constructor Init(NewItem:TLogFont;NewType:Integer);
  destructor Done;virtual;
end;

PFontCollection = ^TFontCollection;   {Collection of printer TLOGFont recs}
TFontCollection = object(TSortedCollection)
	function KeyOf(Item:Pointer):Pointer;virtual;
  function Compare(Key1,Key2:Pointer):Integer;virtual;
  function GetCount:Integer;virtual;
end;

type
pPLXPrinter = ^tPLXPrinter;
tPlxPrinter = object(tWOPrinter)
	HeadLine1:Array[0..210] of Char;
	function DoHeader:Boolean;virtual;
  function SetHeader1(NewHeadLine1:PChar):Boolean;virtual;
end;


type                           {MainWindow of Application}
PPLXWindow = ^TPLXWindow;
TPLXWindow = object(TWindow)
	BWin:PEdit;     {child window displaying sample lines from infile}
  TheIcon:HIcon;
  Bn1,Bn2,Bn3,Bn4,Bn5 :PODButton;
  FileName:Array[0..79] of Char; {infile name}
  CharsInFile:LongInt; {chars in infile}
  St1,St2:PSText;
  FontSelection:Integer;              {Index into Faces collection}
  PFontSize:Integer;        {Current font size for printed text}
  LogPixX,LogPixY:Integer; {LogPixelsX & Y for current Printer}
  Records:PCollection;     {Collection of Infile recordds}
  Margins:TRect;  {in inches * 100}
  Format:FormatRec;
  Tabsize:Integer;
	constructor Init(AParent:PWindowsObject;ATitle:PChar);
  destructor Done;virtual;
  procedure SetupWindow;virtual;
  procedure Paint(PaintDC:HDC;var PaintInfo:TPaintStruct);virtual;
  procedure SetStaticText;
  procedure LoadBWin;
  procedure	WMDrawItem(var Msg:TMessage);virtual wm_First + wm_DrawItem;
  procedure WMSize(var Msg:TMessage);virtual wm_First+wm_Size;
  procedure WMSetFocus(var Msg:TMessage);virtual wm_First+wm_SetFocus;
  procedure IDBut1(var Msg:TMessage);virtual id_First+id_But1; {Print}
	procedure IDBut2(var Msg:TMessage);virtual id_First+id_But2; {FileOpen}
  procedure IDBut3(var Msg:TMessage);virtual id_First+id_But3; {PageSetup}
  procedure	IDBut4(var Msg:TMessage);virtual id_First+id_But4; {SelectFont}
  procedure IDBut5(var Msg:TMessage);virtual id_First+id_But5; {Exit}
  procedure EnumerateFaces;virtual;
  procedure	EnumerateSizes;virtual;
  function GetFontSelection:Integer;virtual;
  procedure SetFontSelection(NewSelection:Integer);
  function GetLogPixX:Integer;virtual;
  function GetLogPixY:Integer;virtual;
  procedure	SetFontSize(NewfontSize:Integer);virtual;
  procedure	SetPFontSize(NewfontSize:Integer);virtual;
  procedure	UMFilePrint(var Msg:TMessage);virtual wm_User+um_FilePrint;
  procedure	WMSysCommand(var Msg:TMessage);virtual wm_First+wm_SysCommand;
  procedure CMFOpen(var Msg:TMessage);virtual cm_First+cm_FOpen;
  procedure CMFPrint(var Msg:TMessage);virtual cm_First+cm_FPrint;
	procedure CMFSetUp(var Msg:TMessage);virtual cm_First+cm_FSetUp;
	procedure CMFExit(var Msg:TMessage);virtual cm_First+cm_FExit;
	procedure CMTFont(var Msg:TMessage);virtual cm_First+cm_TFont;
  procedure GetFormatFlags(var pFormat:PFormatRec);virtual;
  procedure SetFormatFlags(NewFormat:FormatRec);virtual;
  procedure GetMargins(var pMargins:PRect);virtual;
  procedure SetMargins(NewMargins:TRect);virtual;
  function GetTabSize:Integer;virtual;
  procedure SetTabSize(NewTabSize:Integer);virtual;
  procedure GetProfileValues;virtual;
end;


{********************************************************************}
{G L O B A L  V A R I A B L E S                                      }
{********************************************************************}
var
  Faces:PFontCollection; {collection of PFontItem for call-back func}
  Sizes:PCollection;    {collection of stacks for call-back func}

{********************************************************************}
{M E T H O D S                                                       }
{********************************************************************}

procedure TPLXApplication.InitMainWindow;
begin
	MainWindow := New(PPLXWindow,Init(nil,'PLX'));
end;

{********************************************************************}
{Init}
constructor TPLXWindow.Init(AParent:PWindowsObject;ATitle:PChar);
begin
	TWindow.Init(AParent,ATitle);
  Attr.Menu := LoadMenu(HInstance,'PLX_Menu');
  Attr.X := 20; Attr.Y := 25; Attr.W := 595; Attr.H := 260;
  Bn1 := New(PODButton,Init(@Self,id_But1,'Print',200,0,50,50,False,'PLX_Bn1'));
  Bn2 := New(PODButton,Init(@Self,id_But2,'File Open',0,0,50,50,False,'PLX_Bn2'));
  Bn3 := New(PODButton,Init(@Self,id_But3,'Page Setup',50,0,100,50,False,'PLX_Bn3'));
  Bn4 := New(PODButton,Init(@Self,id_But4,'Font',150,0,50,50,False,'PLX_Bn4'));
  Bn5 := New(PODButton,Init(@Self,id_But5,'Exit',250,0,50,50,False,'PLX_Bn5'));
  St1 := New(PSText,Init(@Self,id_St1,'',315,3,240,20,1,dt_Center or dt_VCenter));
  St2 := New(PSText,Init(@Self,id_St2,'',315,26,240,20,1,dt_Center or dt_VCenter));
  LogPixY := 1;
  FontSelection := 9999;
  PFontsize := 10;
  Faces := New(PFontCollection,Init(100,100));
  Faces^.Duplicates := False;
	Sizes := New(PCollection,Init(10,10));
  EnumerateFaces;
  EnumerateSizes;
  BWin := New(PEdit,Init(@Self,200,nil,0,0,0,0,0,True));
  with BWin^.Attr do
   	Style := Style or es_NoHideSel ;
  Records := New(PCollection,Init(1000,500));
  CharsInFile := 0;
  Margins.left := 0;Margins.right := 0;Margins.top := 0;Margins.bottom := 0;
  Format.ShowRuler := 1;Format.ShowFName := 1;
	Format.ShowDTStamp := 1;Format.ShowPageNum := 1;
  Format.ShowLineNum := 1;Format.UseCCB := 0;
  Tabsize := 2;
  GetProfileValues;
end;

{SetupWindow}
procedure TPLXWindow.SetupWindow;
var
	SysMenu:hMenu;
  OEMFixFont:hFont;
begin
	TWindow.SetupWindow;
	SetClassWord(HWindow,GCW_HIcon,LoadIcon(HInstance,'PLX_Icon'));
  Sysmenu := GetSystemMenu(hWindow,false);
  AppendMenu(SysMenu,MF_Separator,0,nil);
  AppendMenu(SysMenu,0,idm_RunCP,'Run Control Panel');
  AppendMenu(Sysmenu,0,idm_About,'About...');
  OEMFixFont := GetStockObject(OEM_Fixed_Font);
  SendMessage(BWin^.hWindow,wm_SetFont,OEMFixFont,LongInt(1));
end;

{Paint}
procedure TPLXWindow.Paint(PaintDC:HDC;var PaintInfo:TPaintStruct);
var
	ThePen:HPen;
  TheBrush :HBrush;
  OldBrush :HBrush;
  OldPen:HPen;
begin
	TheBrush := GetStockObject(LtGray_Brush);
	ThePen := CreatePen(ps_Solid,1,$00000000);
  OldPen := SelectObject(PaintDC,ThePen);
  OldBrush := SelectObject(PaintDC,TheBrush);
  Rectangle(PaintDC,0,0,1024,50);
  SelectObject(PaintDC,OldBrush);
  SelectObject(PaintDC,OldPen);
  DeleteObject(ThePen);
end;

{Route the Ownerdraw msgs to correct object}
procedure	TPLXWindow.WMDrawItem(var Msg:TMessage);
var
	PDIS : ^TDrawItemStruct;
begin
	PDIS := Pointer(Msg.lParam);
  case PDIS^.CtlType of
  	odt_Button:
    	case PDIS^.CtlID of
   			id_But1 :Bn1^.DrawItem(Msg);
   			id_But2 :Bn2^.DrawItem(Msg);
   			id_But3 :Bn3^.DrawItem(Msg);
				id_But4 :Bn4^.DrawItem(Msg);
      	id_But5 :Bn5^.DrawItem(Msg);
      end;
  end;
end;


{Done}
destructor TPLXWindow.Done;
var
	Buf:Array[0..5] of Char;
  Buf2:Array[0..30] of Char;
  FI:pFontItem;
begin
	StrCopy(Buf,'');
  if FontSelection <> 9999 then
  begin
	FI := Faces^.At(FontSelection);
  WritePrivateProfileString('PLX','FaceName',FI^.LogFont.lfFaceName,'PLX.INI');
  Str(PFontSize:2,Buf);
  WritePrivateProfileString('PLX','FontSize',Buf,'PLX.INI');
  Str(Format.ShowRuler:1,Buf);
  WritePrivateProfileString('PLX','ShowRuler',Buf,'PLX.INI');
  Str(Format.ShowFName:1,Buf);
  WritePrivateProfileString('PLX','ShowFName',Buf,'PLX.INI');
  Str(Format.ShowDTStamp:1,Buf);
  WritePrivateProfileString('PLX','ShowDTStamp',Buf,'PLX.INI');
  Str(Format.ShowPageNum:1,Buf);
  WritePrivateProfileString('PLX','ShowPageNum',Buf,'PLX.INI');
  Str(Format.ShowLineNum:1,Buf);
  WritePrivateProfileString('PLX','ShowLineNum',Buf,'PLX.INI');
  Str(Format.UseCCB:1,Buf);
  WritePrivateProfileString('PLX','UseCCB',Buf,'PLX.INI');

  Str(Margins.Left,Buf);
  WritePrivateProfileString('PLX','MarginL',Buf,'PLX.INI');
  Str(Margins.Right,Buf);
  WritePrivateProfileString('PLX','MarginR',Buf,'PLX.INI');
  Str(Margins.Top,Buf);
  WritePrivateProfileString('PLX','MarginT',Buf,'PLX.INI');
  Str(Margins.Bottom,Buf);
  WritePrivateProfileString('PLX','MarginB',Buf,'PLX.INI');
  Str(TabSize,Buf);
  WritePrivateProfileString('PLX','TabSize',Buf,'PLX.INI');
  WritePrivateProfileString('PLX','End','','PLX.INI');
  end;
  Dispose(Faces,Done);
	Dispose(Sizes,Done);
  Dispose(Records,Done);
	TWindow.Done;
end;

{WMSize}
procedure TPLXWindow.WMSize(var Msg:TMessage);
begin
	SetWindowPos(BWin^.HWindow,0,-1,50,
   	(Msg.LParamLo )+1,(Msg.LParamHi-50),swp_NoZOrder);
end;

{WMSetFocus}
procedure TPLXWindow.WMSetFocus(var Msg:TMessage);
begin

end;

{IDBut1} {Print file dialog }
procedure TPLXWindow.IDBut1(var Msg:TMessage);
var
	Dlg : PDialog;
begin
	Dlg :=New(PPLXDlg3,Init(@Self,'PLX_Dlg3'));
	Application^.ExecDialog(Dlg);
end;

{IDBut2} {run file open dialog box, load file}
procedure TPLXWindow.IDBut2(var Msg:TMessage);
var
	Dlg1 :PFileDialog;
  InFile :PTextStream;
  InRecord:PChar;
  ExpRecord:PChar;
  PctMeter:PMeterWindow;
  Division,Pctdone:Integer;
  indx1 : Integer;
  Indx2 : Integer;
  hTab :Integer;
begin
	Division := 10;
	StrCopy(FileName,'*.*');
  InRecord :=MemAlloc(9999);
  ExpRecord := MemAlloc(9999);
  if Records^.Count > 0 then
  	begin
  	Dispose(Records,Done);
    Records := New(PCollection,Init(1000,500));
  	end;
  BWin^.Clear;
  Dlg1 := new(PfileDialog,Init(@Self,PChar(sd_FileOpen),@FileName));
  If Application^.ExecDialog(Dlg1) <> id_OK then
  	begin
 		StrCopy(FileName,'');
    exit;
    end;
  if StrIComp(FileName,'*.*') <> 0 then
  	begin
    PctMeter := New(PMeterWindow,Init(@Self,'PLX - Reading File'));
    Application^.MakeWindow(PctMeter);
    PctMeter^.Draw(0);
    InFile := New(PTextStream,Init(FileName,stOpen,1024));
    CharsInFile := InFile^.CharsToRead;
    While NOT InFile^.IsEOF do
    	begin
    	StrCopy(InRecord,InFile^.GetNext);
    	if InFile^.IsEOF = FALSE then
    		begin
				CheckCC(Inrecord,ExpRecord);     {check for control characters}
        if ExpRecord = nil then          {avoid storing null pointers }
        	StrCopy(ExpRecord,' ');
      	Records^.Insert(New(PTextObj,Init(ExpRecord)));
      	end;
      if InFile^.GetPctDone > Division then
      	begin
      	PctMeter^.Draw(Division);
      	Inc(Division,10);
      	end;
      end;
  	Dispose(PctMeter,Done);
    end;
  FreeMem(InRecord,9999);
  FreeMem(ExpRecord,9999);
  UpdateWindow(hWindow);        {get a redraw before loading preview window}
  LoadBWin;
end;


{IDBut3}   {page setup}
procedure TPLXWindow.IDBut3(var Msg:TMessage);
var
	TotChars:Integer;
begin
  Application^.ExecDialog(New(PPLXDlg2,Init(@Self,'PLX_Dlg2')));
  InvalidateRect(BWin^.HWindow,nil,True);
end;

{IDBut4} {run font selection dialogs}
procedure TPLXWindow.IDBut4(var Msg:TMessage);
var
	Dlg2:PPLXDlg4;
begin
	if Faces^.Count = 0 then      {if necessary, enumerate fonts for current printer}
  	begin
  	EnumerateFaces;
    EnumerateSizes;
    end;
	Dlg2 := new(PPLXDlg4,Init(@Self,'PLX_Dlg4'));
  Application^.ExecDialog(Dlg2);
	if FontSelection = 9999  then
  	begin
  	MessageBox(hWindow,'Please select a font size','Alert',mb_OK or mb_IconExclamation);
    exit;
    end;
end;


{IdBut5}  {exit}
procedure TPLXWindow.IDBut5(var Msg:TMessage);
begin
   SendMessage(HWindow,wm_Close,0,0);
end;


function EnumerateFace(var LogFont: TLogFont; TextMetric: PTextMetric;
  	FontType: Integer; Data: PChar): Integer; export;
 function DupF(Item:PFontItem):Boolean;far;
 	begin
  DupF := (StrIComp(Item^.LogFont.lfFaceName, LogFont.lfFacename)= 0);
   end;
var
  OldFont: HFont;
  Result:PFontItem;
begin
   Result := Faces^.FirstThat(@DupF);
   if Result = nil then Faces^.Insert(New(PFontItem,Init(LogFont,FontType)));
  	EnumerateFace := 1;
end;


function EnumerateSize(var LogFont: TLogFont; TextMetric: PTextMetric;
  		FontType: Integer; Data: PChar): Integer; export;
 function DupS(Item:PIntObj):Boolean;far;
  	begin
   	DupS := (Item^.Int = LogFont.lfHeight);
   end;
var
	FHeight:Array[0..6] of Char;
   PStk :PStack;
   Result :PIntObj;
begin
	PStk :=Sizes^.At(Sizes^.Count-1);
   Result := PStk^.FirstThat(@DupS);
   if Result = nil then PStk^.Push(New(PIntObj,Init(LogFont.lfHeight))) ;
	EnumerateSize := 1;
end;


{ Collect all of faces of current system printer }
procedure TPLXWindow.EnumerateFaces;
var
  EnumProc: TFarProc;
  ThePrinter:pWOPrinter;
begin
    ThePrinter := New(pWOPrinter,Init(hInstance,@Self));
    ThePrinter^.GetPrinterParms;
    ThePrinter^.DCCreated;
    EnumProc := MakeProcInstance(@EnumerateFace, HInstance);
    EnumFonts(ThePrinter^.hPrintDC, nil, EnumProc,nil);
    LogPixY := GetDeviceCaps(ThePrinter^.hPrintDC,LogPixelsY);
    LogPixX := GetDeviceCaps(ThePrinter^.hPrintDC,LogPixelsX);
    ThePrinter^.DeleteContext;
    Dispose(ThePrinter,Done);
end;

{ Collect all of sizes for each face of current system printer }
{EnumerateSizes}
procedure TPLXWindow.EnumerateSizes;
var
  EnumProc: TFarProc;
  ThePrinter:pWOPrinter;
  FontItem :PFontItem;
  Indx : Integer;
begin
    ThePrinter := New(pWOPrinter,Init(hInstance,@Self));
    ThePrinter^.GetPrinterParms;
    ThePrinter^.DCCreated;
    EnumProc := MakeProcInstance(@EnumerateSize, HInstance);
    for Indx := 0 to Faces^.Count -1 do
    	begin
    	FontItem := Faces^.At(Indx);
    	Sizes^.Insert(New(PStack,Init(10,10)));
    	EnumFonts(ThePrinter^.hPrintDC, FontItem^.LogFont.lfFaceName,
      	 EnumProc,nil);
    	end;
    ThePrinter^.DeleteContext;
    Dispose(ThePrinter,Done);
end;

{GetFontSelection}
function TPLXWindow.GetFontSelection:Integer;
begin
	GetFontSelection := FontSelection;
end;

{SetFontSelection}
procedure TPLXWindow.SetFontSelection(NewSelection:Integer);
begin
	FontSelection := NewSelection;
end;

{SetFontSize}
procedure TPLXWindow.SetFontSize(NewFontSize:Integer);
begin
	PFontSize := NewFontSize;
end;

{SetPFontSize}
procedure TPLXWindow.SetPFontSize(NewFontSize:Integer);
begin
	PFontSize := NewFontSize;
end;

{GetLogPixX}
function TPLXWindow.GetLogPixX:Integer;
begin
	GetLogPixX := LogPixX;
end;

{GetLogPixY}
function TPLXWindow.GetLogPixY:Integer;
begin
	GetLogPixY := LogPixY;
end;

{UMFilePrint}
procedure TPLXWindow.UMFilePrint(var Msg:TMessage);
var
	aPtr : pPLXPrinter;
  indx : Integer;
  FI : PFontItem;
  OldFont,NewFont:hFont;
  szSize:Array[0..7] of Char;
  LogFont:TLogFont;
  TM:TTextMetric;
  Buf1,Buf2:PChar;
  szIndx:Array[0..5] of Char;
  OutRecord:pTextObj;
  ExpRec:PChar;
  CCB:Char;
begin
	if Records^.Count = 0  then
  	begin
  	MessageBox(hWindow,'You need to open a file - click the disk icon',
			'Alert',mb_OK or mb_IconExclamation);
    exit;
    end;
	if FontSelection = 9999  then
  	begin
  	MessageBox(hWindow,'You need to select a font - click the font button',
			'Alert',mb_OK or mb_IconExclamation);
    exit;
    end;
	aPtr := New(pPLXPrinter,Init(hInstance,@Self));
	indx := 0;
  GetMem(Buf1,16000);
  GetMem(ExpRec,16000);
  if aPtr^.Start('PLX',hWindow) then
   	begin
      aPtr^.SetMarginL(round(Margins.left * LogPixX div 100)) ;{margin in device pixels}
      FI := Faces^.At(FontSelection);
      FI^.LogFont.lfHeight := Round(PFontsize * LogPixY / 72);
      FI^.LogFont.lfWidth := 0;
      FI^.LogFont.lfWeight := fw_Normal;
      FI^.LogFont.lfQuality := Draft_Quality;
      NewFont := CreateFontIndirect(FI^.LogFont);
      OldFont := aPtr^.SetFont(NewFont);
      aPtr^.SetHeader1(FileName);
      aPtr^.DoHeader;
      StrCopy(szIndx,'');
      for indx := 0 to  (Records^.Count-1) do
      	begin
        OutRecord := Records^.AT(indx);
        if OutRecord^.Text <> nil then
        	StrCopy(Buf1,OutRecord^.Text)
        else
        	StrCopy(Buf1,'');
        Buf2 := Buf1;
        if Format.ShowLineNum = 1 then
        	Str((indx+1):5,szIndx)
        else
        	StrCopy(szIndx,'');
        if (Format.UseCCB = 1) and (buf1 <> nil) then
        	begin
          CCB := Buf1[0];
          Buf2 := Buf1+1;
          end
        else
        	CCB := ' ';
        if Buf1 <> nil  then
          begin
          ExpandTabs(Buf2,ExpRec,Tabsize);
        	StrCat(StrCat(StrCopy(Buf1,szIndx),' '),ExpRec);
          end
        else
					StrCopy(Buf1,szIndx);

        case CCB of
        	'1':	aPtr^.NewPage;
          '0':	aPtr^.PrintLine(' ');
          '-':	begin
          			aPtr^.PrintLine(' ');
                aPtr^.PrintLine(' ');
                end;
        	end;
        aPtr^.printLine(Buf1);

        end;
      OldFont := aPtr^.SetFont(OldFont);
      DeleteObject(NewFont);
      aPtr^.Finish;
      Dispose(aPtr,Done);
    end; {end if}
  FreeMem(Buf1,16000);
  FreeMem(ExpRec,16000);
end;

{WMSysCommand}
procedure	TPLXWindow.WMSysCommand(var Msg:TMessage);
begin
	case Msg.Wparam of
		idm_About:
			Application^.ExecDialog(New(PDialog,Init(@Self,'PLX_About')));
    idm_RunCP:
			begin
    	WinExec('Control',1);
      FontSelection := 9999;           {Force a reselection of font}
      PFontSize := 10;
      Dispose(Faces,Done);
      Dispose(Sizes,Done);
      Faces := New(PFontCollection,Init(100,100));
      Faces^.Duplicates := False;
      Sizes := New(PCollection,Init(10,10));     {since this occurs asynchronously, }
    	end;                                       {we'll force a reload of Faces & Sizes later}
   	else
   		DefWndProc(Msg);
   	end;
end;


{SetStaticText}
procedure TPLXWindow.SetStaticText;
var
  I: Integer;
  Buf:Array[0..80] of Char;
  szLines:Array[0..5] of Char;
  LPY:Integer;
  FontMetrics:TTextMetric;
  szBytes:Array[0..7] of Char;
  nBytes:Integer;
begin                                             {build text display}
  StrCopy(Buf,'File: ');
	St1^.SetText(StrCat(Buf,FileName));
  Str(CharsInFile:5,szBytes);
  Str(Records^.Count:5,szLines);
  StrECopy(StrECopy(StrECopy(StrECopy(Buf,'# of Lines:'),szLines),'  Bytes:'),szBytes);
  St2^.SetText(Buf);
end;

{LoadBWin}
procedure TPLXWindow.LoadBWin;
var
	InRecord:PTextObj;
  Indx:Integer;
  Buf1:pChar;
  Cursor:hCursor;
  CRLF :Array[0..2] of Char;
  RCount:Integer;
begin
	StrCopy(CRLF,#13#10#0);
  RCount := Records^.Count;
	if RCount > 0 then
   	begin
      SetCursor(LoadCursor(0,Idc_Wait));
      indx := 0;
      BWin^.Clear;
      GetMem(Buf1,27000);
      StrCopy(Buf1,'');
      while (indx < 100) AND (indx < RCount) AND (StrLen(Buf1) < 15000) do
      	begin
        InRecord := Records^.At(indx);
        If InRecord^.Text <> NIL then
        	StrCat(StrCat(Buf1,InRecord^.Text),CRLF)
        else
         	StrCat(Buf1,CRLF) ;
      	Inc(indx,1);
      	end;
      if (StrLen(Buf1) > 14999) or (Indx > 99)  then
      	begin
      	StrCat(Buf1,CRLF);
      	StrCat(StrCat(Buf1,'... Rest of text not displayed!!!'),CRLF);
        end;
      BWin^.Insert(Buf1);
      FreeMem(Buf1,27000);
      BWin^.Scroll(0,-9999);
      SetCursor(LoadCursor(0,Idc_Arrow));
      SetStaticText;
   	end;
end;

{CMFOpen}
procedure TPLXWindow.CMFOpen(var Msg:TMessage);
begin
	IDBut2(Msg);
end;

{CMFPrint}
procedure TPLXWindow.CMFPrint(var Msg:TMessage);
begin
	IDBut1(Msg);
  end;

{CMFSetup}
procedure TPLXWindow.CMFSetUp(var Msg:TMessage);
begin
	IDBut3(Msg);
end;

{CMFExit}
procedure TPLXWindow.CMFExit(var Msg:TMessage);
begin
	IDBut5(Msg);
end;

{CMTFont}
procedure TPLXWindow.CMTFont(var Msg:TMessage);
begin
	IDBut4(Msg);
end;

procedure TPLXWindow.GetFormatFlags(var pFormat:PFormatRec);
begin
	pFormat^.ShowRuler := Format.ShowRuler;
	pFormat^.ShowFName := Format.ShowFName;
	pFormat^.ShowDTStamp := Format.ShowDTStamp;
	pFormat^.ShowPageNum := Format.ShowPageNum;
	pFormat^.ShowLineNum := Format.ShowLineNum;
  pFormat^.UseCCB      := Format.UseCCB;
end;

procedure TPLXWindow.SetFormatFlags(NewFormat:FormatRec);
begin
	Format.ShowRuler := NewFormat.ShowRuler;
  Format.ShowFName := NewFormat.ShowFName;
  Format.ShowDTStamp := NewFormat.ShowDTStamp;
  Format.ShowPageNum := NewFormat.ShowPageNum;
  Format.ShowLineNum := NewFormat.ShowLineNum;
  Format.UseCCB := NewFormat.UseCCB;
end;


procedure TPLXWindow.GetMargins(var pMargins:PRect);
begin
	pMargins^.Left := Margins.left;
  pMargins^.Right := Margins.right;
  pMargins^.Top := Margins.Top;
  pMargins^.Bottom := Margins.Bottom;
end;

procedure TPLXWindow.SetMargins(NewMargins:TRect);
begin
	Margins.left := NewMargins.left;
  Margins.right := NewMargins.right;
  Margins.top := NewMargins.top;
  Margins.Bottom := NewMargins.bottom;
end;

function TPLXWindow.GetTabSize:Integer;
begin
	GetTabSize := TabSize;
end;

procedure TPLXWindow.SetTabSize(NewTabSize:Integer);
begin
	TabSize := NewTabSize;
end;

procedure TPLXWindow.GetProfileValues;
var
	Buf1:Array[0..30] of Char;
  Indx:Integer;
  Item:PFontItem;
  Found:Boolean;
begin
	Format.ShowRuler := GetPrivateProfileInt('PLX','ShowRuler',1,'PLX.INI');
	Format.ShowFName := GetPrivateProfileInt('PLX','ShowFName',1,'PLX.INI');
	Format.ShowDTStamp := GetPrivateProfileInt('PLX','ShowDTStamp',1,'PLX.INI');
	Format.ShowPageNum := GetPrivateProfileInt('PLX','ShowPageNum',1,'PLX.INI');
	Format.ShowLineNum := GetPrivateProfileInt('PLX','ShowLineNum',1,'PLX.INI');
	Format.UseCCB := GetPrivateProfileInt('PLX','UseCCB',0,'PLX.INI');

	Margins.Left := GetPrivateProfileInt('PLX','MarginL',0,'PLX.INI');
	Margins.Right := GetPrivateProfileInt('PLX','MarginR',0,'PLX.INI');
	Margins.Top := GetPrivateProfileInt('PLX','MarginT',0,'PLX.INI');
	Margins.Bottom := GetPrivateProfileInt('PLX','MarginB',0,'PLX.INI');
	TabSize := GetPrivateProfileInt('PLX','TabSize',2,'PLX.INI');

	GetPrivateProfileString('PLX','FaceName','9999',Buf1,SizeOf(Buf1),'PLX.INI');
	PFontSize:= GetPrivateProfileInt('PLX','FontSize',8,'PLX.INI');
	Found := False;
	for Indx := 0 to Faces^.GetCount -1 do
  	begin
    Item := Faces^.At(Indx);
    If (StrIComp(Item^.LogFont.lfFaceName,Buf1) = 0) then
    	begin
      FontSelection := Indx;
      Found := True;
      end
    end;
end;
{***********************************************************************}
constructor TFontItem.Init(NewItem:TLogFont;NewType:Integer);
begin
	LogFont := NewItem;
  FontType := NewType;
end;

destructor TFontItem.Done;
begin
end;

{***********************************************************************}
function TFontCollection.KeyOf(Item:Pointer):Pointer;
var
	Ptr :PChar;
begin
	Ptr := PFontItem(Item)^.LogFont.lfFaceName;
	KeyOf := Ptr;
end;


function TFontCollection.Compare(Key1,Key2:Pointer):Integer;
begin
	Compare := StrIComp(PChar(Key1),PChar(Key2));
end;

function TFontCollection.GetCount:Integer;
begin
	GetCount := Count;
end;

{*********************************************************************}
procedure TPLXDlg2.WMInitDialog(var Msg:TMessage);
var
	Buf1:Array[0..5] of Char;
  pBuf:PChar;
  pMargins : PRect;
  pFormat:pFormatRec;
begin
	pMargins := @Margins;
	PPLXWindow(Parent)^.GetMargins(pMargins);
  pFormat := @Format;
  PPLXWindow(Parent)^.GetFormatFlags(pFormat);
  TabSize := PPLXWindow(Parent)^.GetTabSize;
	SendDlgItemMsg(id_D2CB1,bm_SetCheck,Format.ShowRuler,0);
	SendDlgItemMsg(id_D2CB2,bm_SetCheck,Format.ShowFName,0);
	SendDlgItemMsg(id_D2CB3,bm_SetCheck,Format.ShowDTStamp,0);
	SendDlgItemMsg(id_D2CB4,bm_SetCheck,Format.ShowPageNum,0);
	SendDlgItemMsg(id_D2CB5,bm_SetCheck,Format.ShowLineNum,0);
	SendDlgItemMsg(id_D2CB6,bm_SetCheck,Format.UseCCB,0);

  pBuf := Buf1;
  Str(Margins.Left/100:3:1,Buf1);
  SendDlgItemMsg(id_D2EC1,wm_SetText,0,LongInt(pBuf));
  Str(Margins.Right/100:3:1,Buf1);
  SendDlgItemMsg(id_D2EC2,wm_SetText,0,LongInt(pBuf));
  Str(Margins.Top/100:3:1,Buf1);
  SendDlgItemMsg(id_D2EC3,wm_SetText,0,LongInt(pBuf));
  Str(Margins.Bottom/100:3:1,Buf1);
  SendDlgItemMsg(id_D2EC4,wm_SetText,0,LongInt(pBuf));
  Str(TabSize,Buf1);
  SendDlgItemMsg(id_D2EC5,wm_SetText,0,LongInt(pBuf));
end;

procedure TPLXDlg2.IDD2OK(var Msg:TMessage);
begin
  PPLXWindow(Parent)^.SetMargins(Margins);
  PPLXWindow(Parent)^.SetFormatFlags(Format);
  PPLXWindow(Parent)^.SetTabSize(TabSize);
  EndDlg(1);
end;

procedure TPLXDlg2.IDD2CB1(var Msg:TMessage);
begin
	case Msg.lParamHi of
    bn_Clicked:
   		begin
      Format.ShowRuler := SendDlgItemMsg(id_D2CB1,bm_GetCheck,0,0);
      Exit;
      end;
  end;
end;

procedure TPLXDlg2.IDD2CB2(var Msg:TMessage);
begin
	case Msg.lParamHi of
    bn_Clicked:
   		begin
      Format.ShowFName := SendDlgItemMsg(id_D2CB2,bm_GetCheck,0,0);
      Exit;
      end;
  end;
end;

procedure TPLXDlg2.IDD2CB3(var Msg:TMessage);
begin
	case Msg.lParamHi of
    bn_Clicked:
   		begin
      Format.ShowDTStamp := SendDlgItemMsg(id_D2CB3,bm_GetCheck,0,0);
      Exit;
      end;
  end;
end;

procedure TPLXDlg2.IDD2CB4(var Msg:TMessage);
begin
	case Msg.lParamHi of
    bn_Clicked:
   		begin
      Format.ShowPageNum := SendDlgItemMsg(id_D2CB4,bm_GetCheck,0,0);
      Exit;
      end;
  end;
end;

procedure TPLXDlg2.IDD2CB5(var Msg:TMessage);
begin
	case Msg.lParamHi of
    bn_Clicked:
   		begin
      Format.ShowLineNum := SendDlgItemMsg(id_D2CB5,bm_GetCheck,0,0);
      Exit;
      end;
  end;
end;

procedure TPLXDlg2.IDD2CB6(var Msg:TMessage);
begin
	case Msg.lParamHi of
    bn_Clicked:
   		begin
      Format.UseCCB := SendDlgItemMsg(id_D2CB6,bm_GetCheck,0,0);
      Exit;
      end;
  end;
end;

procedure TPLXDlg2.IDD2EC1(var Msg:TMessage);
var
	Idx : Integer;
  Buf:Array[0..5] of Char;
  Ptr : PChar;
  ErrCode:Integer;
  Margin:Real;
  return:Integer;
begin
	case Msg.lParamHi of
    en_KillFocus:
   		begin
      Ptr := Buf;
      Idx := 5;
      Return := SendDlgItemMsg(id_D2EC1,wm_GetText,word(Idx),LongInt(Ptr));
      val(Ptr,Margin,ErrCode);
      Margins.Left := round(Margin * 100);
      Exit;
      end;
  end;
end;

procedure TPLXDlg2.IDD2EC2(var Msg:TMessage);
var
	Idx : Integer;
  Buf:Array[0..5] of Char;
  Ptr : PChar;
  ErrCode:Integer;
  Margin:Real;
  return:Integer;
begin
	case Msg.lParamHi of
    en_KillFocus:
   	begin
      Ptr := Buf;
      Idx := 5;
      Return := SendDlgItemMsg(id_D2EC2,wm_GetText,word(Idx),LongInt(Ptr));
      val(Ptr,Margin,ErrCode);
      Margins.Right := round(Margin * 100);
      Exit;
      end;
   end;
end ;

procedure TPLXDlg2.IDD2EC3(var Msg:TMessage);
var
	Idx : Integer;
  Buf:Array[0..5] of Char;
  Ptr : PChar;
  ErrCode:Integer;
  Margin:Real;
  return:Integer;
begin
	case Msg.lParamHi of
    en_KillFocus:
   	begin
      Ptr := Buf;
      Idx := 5;
      Return := SendDlgItemMsg(id_D2EC3,wm_GetText,word(Idx),LongInt(Ptr));
      val(Ptr,Margin,ErrCode);
      Margins.Top := round(Margin * 100);
      Exit;
      end;
   end;
end;

procedure TPLXDlg2.IDD2EC4(var Msg:TMessage);
var
	Idx : Integer;
  Buf:Array[0..5] of Char;
  Ptr : PChar;
  ErrCode:Integer;
  Margin:Real;
  return:Integer;
begin
	case Msg.lParamHi of
    en_KillFocus:
   	begin
      Ptr := Buf;
      Idx := 5;
      Return := SendDlgItemMsg(id_D2EC4,wm_GetText,word(Idx),LongInt(Ptr));
      val(Ptr,Margin,ErrCode);
      Margins.Bottom := round(Margin * 100);
      Exit;
      end;
   end;
end;

procedure TPLXDlg2.IDD2EC5(var Msg:TMessage);
var
	Idx : Integer;
  Buf:Array[0..5] of Char;
  Ptr : PChar;
  ErrCode:Integer;
  TSize:Integer;
  return:Integer;
begin
	case Msg.lParamHi of
    en_KillFocus:
		begin
      Ptr := Buf;
      Idx := 5;
      Return := SendDlgItemMsg(id_D2EC5,wm_GetText,word(Idx),LongInt(Ptr));
      val(Ptr,TSize,ErrCode);
      TabSize := TSize;
      Exit;
      end;
   end;
end;

{*********************************************************************}
procedure TPLXDlg3.WMInitDialog(var Msg:TMessage);
var
  ThePrinter:pWOPrinter;
  DeviceName:Array[0..40] of Char;
begin
	 TDialog.WMInitDialog(Msg);
   ThePrinter := New(pWOPrinter,Init(hInstance,@Self));
   ThePrinter^.GetPrinterParms;
   ThePrinter^.DCCreated;
   StrCopy(DeviceName,ThePrinter^.deviceName);
   ThePrinter^.DeleteContext;
   Dispose(ThePrinter,Done);
   SetDlgItemText(HWindow,503,DeviceName);
end;

procedure TPLXDlg3.IDSetup(var Msg:TMessage);
var
	ThePrinter:pWOPrinter;
begin
	ThePrinter := New(pWOPrinter,Init(hInstance,@Self));
  ThePrinter^.prnDeviceMode(hWindow);
  dispose(ThePrinter,Done);
  PPLXWindow(Parent)^.EnumerateFaces;
  PPLXWindow(Parent)^.EnumerateSizes;
end;

 procedure TPLXDlg3.IDOKPrt(var Msg:TMessage);
begin
   EndDlg(1);
	SendMessage(PPLXWindow(Parent)^.HWindow,wm_User+um_FilePrint,Msg.wParam,Msg.LParam);
end;

{***********************************************************************}

procedure TPLXDlg4.WMInitDialog(var Msg:TMessage);
var
	Indx : Integer;
  Font : PFontItem;
	pTextItem:PChar;
begin
	TDialog.WMInitDialog(Msg);
	for indx := 0 to (Faces^.GetCount -1) do
   	begin
		Font := Faces^.At(indx);
    pTextItem :=  Font^.LogFont.lfFaceName;
    SendDlgItemMsg(id_D4LB1,lb_AddString,word(0),LongInt(pTextItem));
    end;
  IF pPLXWindow(Parent)^.FontSelection <> 9999 then
  	SendDlgItemMsg(id_D4Lb1,lb_SetCurSel,
			pPLXWindow(Parent)^.FontSelection,0);
end;

procedure TPLXDlg4.IDD4Lb1(var Msg:TMessage);
var
	Idx : Integer;
   Buf:Array[0..5] of Char;
   Ptr : PChar;
   ErrCode:Integer;
begin
	case Msg.lParamHi of
    lbn_SelChange,lbn_DblClk:
   	begin
      Ptr := Buf;

      Idx := SendDlgItemMsg(id_D4Lb1,lb_GetCurSel,0,0);
      PPLXWindow(Parent)^.SetFontSelection(Idx);
      loadsizes;
      Exit;
      end;
   end;
end;

procedure TPLXDlg4.IDD4Lb2(var Msg:TMessage);
var
	Idx : Integer;
   Buf:Array[0..5] of Char;
   Ptr : PChar;
   ErrCode:Integer;
begin
	case Msg.lParamHi of
    lbn_SelChange,lbn_DblClk:
   	begin
      Ptr := @Buf;
      Idx := SendDlgItemMsg(id_D4Lb2,lb_GetCurSel,0,0);
      SendDlgItemMsg(id_D4Lb2,lb_GetText,Idx,LongInt(Ptr));
      val(Ptr,FontSize,ErrCode);
      PPLXWindow(Parent)^.SetFontSize(FontSize);
      EndDlg(Idx);
      Exit;
      end;
   end;
end;

procedure TPLXDlg4.LoadSizes;
var
	pTextItem:PChar;
  Buf:Array[0..5] of Char;
	Indx:Integer;
  FontItem:PFontItem;
  Item:PIntObj;
  PStk :PStack;
  Indx2:Integer;
  Res,Res2:Integer;
  Height:Integer;
  LPY:Integer;
begin
  LPY := PPLXWindow(Parent)^.GetLogPixY;
  FontItem := Faces^.At(PPLXWindow(Parent)^.GetFontSelection);
	PStk := Sizes^.At(PPLXWindow(Parent)^.GetFontSelection);
  Indx2 := 0;
  Indx := 6;
  pTextItem := Buf;
  Res := FontItem^.FontType and Raster_FontType; {0 = vector font}
  Res2 := FontItem^.FontType and Device_FontType; {0 = GDI font}
SendDlgItemMsg(id_D4Lb2,lb_ResetContent,word(0),LongInt(pTextItem));
  if Res = 0 then
   	begin
   	Str(Indx:3,Buf);
   	while Indx < 20 do
   		begin
   		SendDlgItemMsg(id_D4Lb2,lb_AddString,word(0),LongInt(pTextItem));
			Indx := Indx + 2;
    	Str(Indx:3,Buf);
   		end;
   	end
   else
   	for Indx2 := 0 to PStk^.Count-1  do
    	begin
      Item := PStk^.At(Indx2);
      Height := Item^.Int;
      Str(Round(Height * 72 / LPY):3,Buf);
   		SendDlgItemMsg(id_D4Lb2,lb_AddString,word(0),LongInt(pTextItem));
      end;
end;

{***********************************************************************}
function  tPLXPrinter.DoHeader:Boolean;
var
	indx : Integer;
	FI : PFontItem;
	szSize:Array[0..7] of Char;
	LogFont:TLogFont;
	TM:TTextMetric;
	Buf1:Array[0..100] of Char;
	szDateTime:Array[0..79] of Char;
  szPageNumber:Array[0..5] of Char;
  Ruler : Array[0..210] of Char;
  Format:FormatRec;
  pFormat:pFormatRec;
begin
  pFormat :=@Format;
  PPLXWindow(Application^.MainWindow)^.GetFormatFlags(pFormat);
  if Format.ShowLineNum = 1 then
  	StrCopy(Ruler,'     ')
  else
  	StrCopy(Ruler,'');
	StrCat(Ruler,' |...+....1....+....2....+....3....+....4....+....5');
  StrCat(Ruler,'....+....6....+....7....+....8....+....9....+....0');
	StrCat(Ruler,'....+....1....+....2....+....3....+....4....+....5');
  StrCat(Ruler,'....+....6....+....7....+....8....+....9....+....0');
	GetTextMetrics(hPrintDC,TM);
  ResetPos;
  StrCopy(Buf1,'');
  GetDateTime(szDateTime);
  if Format.ShowFName <> 0 then
  	StrCopy(Buf1,HeadLine1);
  if Format.ShowDTStamp <> 0 then
  	StrCat(StrCat(Buf1,'  '),szDateTime);
  Str(PageNumber:3,szPageNumber);
  if Format.ShowPageNum <> 0 then
  	StrCat(StrCat(Buf1,'       page:'),szPageNumber);
  if StrLen(Buf1) <> 0 then
  	print(Buf1);
  SetMarginL(Margin.left); {Set margin = 0 inch}
  NewLine;
  if Format.ShowRuler <> 0 then
  	PrintLine(Ruler);
end;

function tPLXPrinter.SetHeader1(NewHeadLine1:PChar):Boolean;
begin
	StrCopy(HeadLine1,NewHeadLine1);
  SetHeader1 := True;
end;

{*********************************************************************}
{*** M A I N L I N E                                                  }
{*********************************************************************}
var
	PLXApp : TPLXApplication;
begin
    PLXApp.Init('Font Preview');
    PLXApp.Run;
    PLXApp.Done;
end.
