-- pr123.exw by Wolf.

include win32lib.ew
include printws.ew
without warning

-- create the window and menu.
constant
  MenuWin = create(Window,"Print Demo",0,Default,Default,340,130,0),
  PopupFile = create(Menu,"&File",MenuWin,0,0,0,0,0),
  MenuPrint = create(MenuItem,"&Print",PopupFile,0,0,0,0,0),
  MenuSep1  = create(MenuItem,"-",PopupFile,0,0,0,0,0),
  MenuExit  = create(MenuItem,"E&xit",PopupFile,0,0,0,0,0),
  PopupHelp = create(Menu,"&Help",MenuWin,0,0,0,0,0),
  MenuAbout = create(MenuItem,"&About",PopupHelp,0,0,0,0,0),
  Dummy = create(Menu," -prints 3 pages thru FILE,PRINT-",MenuWin,0,0,0,0,0)

setFont( MenuWin,"Courier New",10,1)

procedure onMenu_MenuAbout( integer self, integer event, sequence params )
    integer result
    result = message_box("Win32Lib Print Demo, prints itself !",
                         "HELLO !",#2000)
end procedure
setHandler( MenuAbout, w32HClick, routine_id("onMenu_MenuAbout") )

integer Handle
object line

procedure print_stuff(sequence a) --a is filename
  atom myphdc, i, j, charheight, width, hor, ver
  sequence mypr, textinfo, string1
  mypr = DefaultPrinterName()
  myphdc = GetPrinterDC(mypr)
  textinfo = GetTextMetrics(myphdc) 
  charheight = textinfo[1]+1       --character height+1, <this is optional
  --using textinfo[1] only, allows characters to "touch"
  --might be useful for DOS'like graphics
  width = textinfo[2]              --character width
  hor = GetDeviceCaps(myphdc,8)    --page width
  ver = GetDeviceCaps(myphdc,10)   --page length

  setPenPos(MenuWin,0,1)
  string1= " character height  = " & sprintf("%d",textinfo[1]) & " + 1 extra"
  wPuts(MenuWin,string1)
  setPenPos(MenuWin,0,18)
  string1= " character width   = " & sprintf("%d",width)
  wPuts(MenuWin,string1)
  setPenPos(MenuWin,0,36)
  string1= " total page height = " & sprintf("%d",ver)
  wPuts(MenuWin,string1)
  setPenPos(MenuWin,0,54)
  string1= " total page width  = " & sprintf("%d",hor)
  wPuts(MenuWin,string1)

  i=StartDoc(myphdc, a )
  --a bit of fooling around...
  i=StartPage(myphdc)
  pTextOut(myphdc,150,70,"An Example")
  pTextOut(myphdc,400,140,"of placing stuff")
  pTextOut(myphdc,20*width,400,"wherever")
  pTextOut(myphdc,28*width,408," you want..")
  pTextOut(myphdc,10*width,400+4*charheight,"within the page extents.")
  pTextOut(myphdc,floor(hor/2-(8*width)),floor(ver/2-(charheight/2)),
           "near the middle.")
  pTextOut(myphdc,50,floor(ver-(8*charheight)),"end of page one.")
  i=EndPage(myphdc)
  --end of fooling...

  i=StartPage(myphdc)
  Handle=open( a ,"r")
  j=charheight*4 --some empty space
  while 1 do
     line=gets(Handle)
     if atom(line) then exit end if
     line=line[1..(length(line)-1)] --remove line feed.
     --max. char. per line ??
     --so do parsing here, using "hor/width=maxx"
     --or the end of your line will "disappear"
     if j > ver-charheight*8 then --close to bottom?
       i=EndPage(myphdc)
       i=StartPage(myphdc) --next page
       j=charheight*4 --reset line count
     end if
     --careful here, I think this only likes integers.
     pTextOut(myphdc,0,j,line )
     j=j+charheight --increment line count
   end while
   close(Handle)
   i=EndPage(myphdc)  -- eject page
   i=EndDoc(myphdc)   -- and close
   DeleteDC(myphdc)
end procedure

procedure onMenu_MenuExit( integer self, integer event, sequence params )
    closeWindow(MenuWin)
end procedure
setHandler( MenuExit, w32HClick, routine_id("onMenu_MenuExit") )

procedure onMenu_MenuPrint( integer self, integer event, sequence params )
    print_stuff("pr123.exw") --if it ain't there,CRASH.
end procedure
setHandler( MenuPrint, w32HClick, routine_id("onMenu_MenuPrint") )

WinMain(MenuWin,Normal)
----
