Help Selecting and Using a Selected Printer

Linux specific forum
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Help Selecting and Using a Selected Printer

Post by yrreti »

I'm working toward moving more away from Windows, but still need to use some programs that I had written.
Using wine under Linux, I have been able to use some complex programs, with everything working except
for Selecting and Using a Selected Printer for printing. It always locks up when I use this code.
I'm still pretty new to Linux yet and know cups is used a lot for printing. But it's all manual setup which
won't work in this case. I use four different printers, and need a better method for choosing which printer
to use for a particular need.
The following code works quite well under windows, but won't work under 'wine' when using Linux.
(Also thanks to ABBKlaus's for his nice Printer_Lib.)

Code: Select all

;Using  ABBKlaus's Printer_Lib V1.10
Enumeration
  ;{  ;#PtrSelWindow
  #PtrSelWindow
  #ctaBtn
  #ctcBtn
  #dpmsg
  #dpmsel
  #adjpm_txt
  #adjpm_spnr
EndEnumeration

Global selprn$,lsa$,dpn$

Procedure.s OpenPtrSelWindow()
  ;First check if there is a printer. Then check if there is more than one printer.
  ;If there is, allow printer selection.  
  
  Options=#PRINTER_ENUM_LOCAL|#PRINTER_ENUM_CONNECTIONS;|#PRINTER_ENUM_REMOTE
  EnumPrinters_(Options, 0, 1, #Null, 0, @dwNeeded, @dwReturned)
  ;Debug GetLastError_()
  If dwNeeded
    *buffer = AllocateMemory(dwNeeded)
    EnumPrinters_(Options, 0, 1, *buffer, dwNeeded, @dwNeeded, @count)
    ;FreeMemory(*buffer)
  EndIf
  If Trim(dpn$)=""
    ;this gets default at startup, but if you change the printer, it stays selected until you restart program
    ;or reselect another printer.
    dpn$=Print_GetDefaultPrinter()
  EndIf
   
  If count=1
    If OpenWindow(#PtrSelWindow, 0, 0, 400, 100, "Selected Printer", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
      ButtonGadget(#ctaBtn, 275, 10, 85, 23, "Click to Accept")
      ButtonGadget(#ctcBtn, 275, 67, 85, 23, "Click to Cancel")
      TextGadget(#dpmsg, 10, 20, 220, 25, "Default Printer.   ")
      ComboBoxGadget(#dpmsel, 10, 40, 380, 20)
      TextGadget(#adjpm_txt, 10, 65, 60, 27, "Adjust Left Print Margin", #PB_Text_Center)
      SpinGadget(#adjpm_spnr, 71, 65, 35, 25, 0, 8, #PB_Spin_Numeric|#PB_Spin_ReadOnly)
      If Trim(lsa$)=""
        lsa$=GetGadgetText(#adjpm_spnr)
        If Trim(lsa$)=""
          lsa$="3";set default at 3
          SetGadgetText(#adjpm_spnr,lsa$)
        EndIf
      Else
        SetGadgetText(#adjpm_spnr,lsa$)
      EndIf
      i=count
      pointer=PeekI(*buffer+(i-1)*SizeOf(PRINTER_INFO_1)+OffsetOf(PRINTER_INFO_1\pName))
      pn$=PeekS(pointer)
      If pn$=dpn$
        dpi=i-1
        If dpi<0:dpi=0:EndIf
      EndIf
      AddGadgetItem(#dpmsel, -1, dpn$)
      SetGadgetState(#dpmsel, dpi);display default printer
      FreeMemory(*buffer)
      ;Debug dpn$
    EndIf
    ;ProcedureReturn dpn$  ;don't exit in case you need o adjust the left print margin
  EndIf
  If count>1
    If OpenWindow(#PtrSelWindow, 0, 0, 400, 100, " Select Printer", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
      ButtonGadget(#ctaBtn, 275, 10, 85, 23, "Click to Accept")
      ButtonGadget(#ctcBtn, 275, 67, 85, 23, "Click to Cancel")
      TextGadget(#dpmsg, 10, 20, 220, 25, "Default Printer.   Click on Bar to Change")
      ComboBoxGadget(#dpmsel, 10, 40, 380, 20)
      TextGadget(#adjpm_txt, 10, 65, 60, 27, "Adjust Left Print Margin", #PB_Text_Center)
      SpinGadget(#adjpm_spnr, 71, 65, 35, 25, 0, 8, #PB_Spin_Numeric|#PB_Spin_ReadOnly)
      If Trim(lsa$)=""
        lsa$=GetGadgetText(#adjpm_spnr)
        If Trim(lsa$)=""
          lsa$="3";set default at 3
          SetGadgetText(#adjpm_spnr,lsa$)
        EndIf
      Else
        SetGadgetText(#adjpm_spnr,lsa$)
      EndIf
      ;Debug dpn$
      For i = 1 To count
        pointer=PeekI(*buffer+(i-1)*SizeOf(PRINTER_INFO_1)+OffsetOf(PRINTER_INFO_1\pName))
        pn$=PeekS(pointer)
        ;Debug pn$+"   "+Str(i)
        If pn$=dpn$
          dpi=i-1
          If dpi<0:dpi=0:EndIf
        EndIf
        AddGadgetItem(#dpmsel, -1, pn$)
      Next
      SetGadgetState(#dpmsel, dpi);display default printer
      FreeMemory(*buffer)
    EndIf
  Else
    If Trim(dpn$)=""
      FreeMemory(*buffer)
      MessageRequester(" Printer Error Message", "Can Not Locate Printer!", #PB_MessageRequester_Ok)
    EndIf
    ProcedureReturn " "
  EndIf
  ps=0
  Repeat
    Select WaitWindowEvent(5)
      ; ///////////////////
    Case #PB_Event_Gadget
      Select EventGadget()
      Case #ctaBtn  ;Click to Accept
        If count>1
          If ps=0;=1 if comboboxgadget was clicked,  then selprn$= GetGadgetText(#dpmsel)
            selprn$=dpn$;selprn$=GetGadgetItemText(#dpmsel, 0 )
          EndIf
        Else
          selprn$=dpn$
        EndIf
        lsa$=GetGadgetText(#adjpm_spnr)
        If Trim(lsa$)="":lsa$="0":EndIf
        CloseWindow(#PtrSelWindow)
        ProcedureReturn selprn$+"|"+lsa$
        
      Case #ctcBtn  ;Click to Cancel
        CloseWindow(#PtrSelWindow)
        ;use to abort print if closed by x-box.
        ;ProcedureReturn  previous_selprn$+"|"+previous_lsa$
        ProcedureReturn " "
        Break
        
      Case #dpmsel
        ps=1
        selprn$= GetGadgetText(#dpmsel)
        dpn$=selprn$
        
        ;Case #adjpm_txt
        ;Case #adjpm_spnr
        
      EndSelect
      ; ////////////////////////
    Case #PB_Event_CloseWindow
      Select EventWindow()
      Case #PtrSelWindow
        CloseWindow(#PtrSelWindow)
        ;use to abort print if closed by x-box.
        ProcedureReturn ""
        Break
      EndSelect
    EndSelect
  ForEver
EndProcedure


selprn$=OpenPtrSelWindow()
Debug selprn$
p=FindString(selprn$,"|",1)
lsa=Val(Mid( selprn$,p+1));Left Side Adjust (Margin)
selprn$=Left(selprn$,p-1)
Debug selprn$
Debug Str(lsa);I add the lsa value to the x position on each printed line.)

If Trim(selprn$)<>"";abort print if =""
  If Print_OpenPrinter(selprn$,"PAPERSIZE="+Str(#DMPAPER_LETTER)+",ORIENTATION="+Str(#DMORIENT_PORTRAIT))
    Print_StartPrinting("MyData")
    Print_NewPage()
    While WindowEvent():Wend;needed or printer locks up
    Print_StopPrinting()
  Else
    err$=Print_GetLastError()
    MessageRequester(" Print_StartPrinting()",err$,0)
  EndIf
  While WindowEvent():Wend;needed or printer locks up
EndIf
Could anybody please give me some ideas of how I could be able to do this using Linux, or of something
that would work using wine. I sure would appreciate any help, because I really need to use these programs yet.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Help Selecting and Using a Selected Printer

Post by IdeasVacuum »

Perhaps one way would be to use PB's PrintRequester() to launch the system's print requester - but how to get which printer was selected I don't know. I see from other forum posts that skilled windows developers are finding printing in Linux to be difficult - of course everyone would like PB to have a cross-platform printer lib that is as good as ABBKlaus's - that is the Utopian, but would require a tremendous effort to achieve it. Probably your best bet is to use whatever printer API is available on Linux.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: Help Selecting and Using a Selected Printer

Post by yrreti »

Thanks for the reply IdeasVacuum
Yes I searched the forum for this answer before posting this question and found the same thing.
It's too bad, because it's one of the main things holding me back from moving to programing in
Linux. I need to be able to select printers and print via program. Not having to do it manually
each time.
Thanks for your help and input.
Post Reply