# Adding PRINT functions.

OK., I admit it,
(1) I am new to Euphoria,
(2) new to Windoze,
(3) indeed, new to programming,
(4) and wasted a lot of time trying to print the addressbook database through the Windoze print spooler in two columns, so I hope it works for you, as it finally did for me.
Before you get too involved in this lesson you might want to check out the lesson Extra_02, which this is based on. Also please note my caution regarding the pTextOut() procedure in this lesson.

As the Extra_02 lesson tries to illustrate, all this Windows printing is actually done in a 'dot' mode, with the page having extents defined by a grid of these 'dots'. This makes printing simple lines of text a little more complicated, especially if one tries to 'place' the text in a certain way, as I have done here, with two columns. The difficulty is compounded by the fact that you may be using a proportional font in which each character might be a different width. Just trying to 'format' the page with spaces just won't always work!
..and, as you can probably guess, using the spoolers 'default' printer lets you print to LPT2 or COM1, or even a networked printer, for example, without having to program specifically for these 'ports'. Windows will do it for you!

This additional program segment adds a print item to our menu, a new print procedure, and procedures to set up the printer, print our database, and close the printer. This was my first attempt to print two columns using the 'spooler', and it worked, so I hope I don't get too much !@#$ from 'real' pros about this. Honestly though, any helpful suggestions would be appreciated ;-)

When Mr. Cuny adds printing functions to win32lib, printing might be a little easier.
( -UPDATE- printing functions added Oct./99, check out the UPDATE01 lesson for a simple example. )

-- address4.exw

    -- a simple addressbook example, part four.

    -- ** adding the following red line to our menu.
      MenuSave  = create(MenuItem, "&Save", MenuTop, 0, 0, 0, 0, 0),
      MenuPrint = create(MenuItem,"&Print", MenuTop, 0, 0, 0, 0, 0),
      MenuExit  = create(MenuItem, "Eξt", MenuTop, 0, 0, 0, 0, 0),

    -- ** the end of our previous onClick_btnDel procedure.
      setFocus(List1)            -- and show it.
      show_parsed()              -- in any case, set the focus to our listbox.
      end if
    end procedure

    -- ** start our new code here.

    -- from pr123.exw in lesson Extra_02--

    include printws.ew

    atom myphdc, j, k, charheight, width, hor, ver, column2
    sequence mypr, textinfo

    procedure print_setup(sequence filename)
      mypr=DefaultPrinterName()         -- get default printer name
      myphdc=GetPrinterDC(mypr)         -- create a print 'handle'
      textinfo=GetTextMetrics(myphdc)   -- get printer 'font' info
      charheight=textinfo[1]+1          -- character height+1
      width=textinfo[2]                 -- character width
      hor=GetDeviceCaps(myphdc,8)       -- page width
      column2=floor(hor/2)              -- location for our second column
      ver=GetDeviceCaps(myphdc,10)      -- page length
      j=StartDoc(myphdc, filename)      -- start document with 'dummy' filename
      j=StartPage(myphdc)               -- start a new page
    end procedure

    procedure end_print()
      j=EndPage(myphdc)                 -- eject page,
      j=EndDoc(myphdc)                  -- close 'file',
      DeleteDC(myphdc)                  -- and clean up.
    end procedure

    procedure print_data()
      k=charheight*4                      -- some empty space at the top
      listlen = getCount(List1)           -- how many lines are in it ?
      if listlen > 0 then                 -- can't print an empty list.
        for l=1 to listlen by 2 do
          line=getItem(List1,l)
          show_parsed()                   -- fill our sle's for each item.
    -- print the first column of the first line.
          line=getText(Sle2) & " " & getText(Sle1)
          pTextOut(myphdc,0,k,line )      -- send to print spooler

          if listlen > l then
    -- print the second column of the first line.
            line=getItem(List1,l+1)       -- go to next data item
            show_parsed()
            line=getText(Sle2) & " " & getText(Sle1)
            pTextOut(myphdc,column2,k,line )
          end if

    -- return to previous data item
    -- and print the second line of the first column.
          line=getItem(List1,l)
          show_parsed()
          k=k+charheight               -- increment line count
          line=getText(Sle3)
          if length(line) > 0 then     -- lets NOT print a comma for nothing.
            line=getText(Sle3) & ", " & getText(Sle4)
          else line=getText(Sle4)      -- skip empty sle
          end if
          pTextOut(myphdc,0,k,line)

          if listlen > l then
    -- print the second column of the second line.
            line=getItem(List1,l+1)
            show_parsed()
            line=getText(Sle3)
            if length(line) > 0 then
              line=getText(Sle3) & ", " & getText(Sle4)
            else line=getText(Sle4)    -- skip empty sle
            end if
            pTextOut(myphdc,column2,k,line)
          end if

    -- return to previous data item
    -- and print the third line of the first column.
          line=getItem(List1,l)
          show_parsed()
          k=k+charheight
          line=getText(Sle5)
          if length(line) > 0 then
            line=getText(Sle5) & ", " & getText(Sle6) & "   " & getText(Sle7)
          else line=getText(Sle6) & "   " & getText(Sle7)
          end if
          pTextOut(myphdc,0,k,line)

          if listlen > l then
    -- print the second column of the third line.
            line=getItem(List1,l+1)
            show_parsed()
            line=getText(Sle5)
            if length(line) > 0 then
              line=getText(Sle5) & ", " & getText(Sle6) & "   " & getText(Sle7)
            else line=getText(Sle6) & "   " & getText(Sle7)  -- skip empty sle
            end if
            pTextOut(myphdc,column2,k,line)
          end if

          k=k+charheight*3              -- increment and skip 2 lines.
          if k > ver-charheight*6 then  -- close to bottom?
            j=EndPage(myphdc)           -- if so, start a new page.
            j=StartPage(myphdc)         -- next page
            k=charheight*4              -- reset line count
          end if
        end for
        line=getItem(List1,1)
        show_parsed()                   -- correct our display
      end if
    end procedure

    procedure onMenu_MenuPrint( integer self, integer event, sequence params )
      print_setup("mydata3.txt")
      print_data()
      end_print()
    end procedure

    procedure onMenu_Exit()
        closeWindow(MyWin)
    end procedure

    -- ** adding the following red line to our event handlers.
    setHandler( MenuPrint, w32HClick, routine_id("onMenu_MenuPrint") )
    -- ** etcetera.

.. and yes, this program is a little strange, in trying to 'capture' the data to be printed from the sle's as the program cycles through the listbox. If your computer is too fast, try adding a short delay routine in the show_parsed() procedure so you can SEE what it's really doing in the Label1 box. Neat-o!
A normal program might first update our database file to disk, and then read the lines from this to print them, or work from memory, but thats not as entertaining.

Adding a much simpler version of the print_data() routine, to print individual data records on envelopes, for example, would be a great addition to this program. Just keep track of 'where' you are, and you're almost done!

..end of lesson.

CONTENTS