;** SET_IMP ********************************************************************
;*  DEFINE IMPRESSORA PADRAO E CAPTURA PORTA DE SAIDA                          *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 02/06/2003 por Anivel Augusto Rodrigues
;Ultima Alteracao: 06/04/2005 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes salva_Impresoras()
;Ultima Alteracao: 20/06/2006 por Anivel Augusto Rodrigues
;Motivo Alteracao: DosPrinter
;Ultima Alteracao: 04/09/2007 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Captura
;Ultima Alteracao: 19/09/2012 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Substitui‡Æo Editor Novo arquivo por FileWrite

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc SD_Dialog(Disparo,Campo,Evento,Elemento)
Temp=True
MSG_CAMPO=""
TECLA=""
If Disparo="OPEN"
   Then AoCriarShowDialog()
        Campo="TG_IMPPORTA"
EndIf
If Disparo="EVENT" And Evento["Type"]="KEY" Then
   Switch
     Case Evento["KeyCode"] = -59: Dialog_F1()
     Case Evento["KeyCode"] = -83 And Campo="TG_IMPCAMINHO":
          VSDGIMPCAMINHO=ControlValue("TG_IMPCAMINHO")
          If NumVal(VSDGIMPCAMINHO)>1
             Then Excluir_Lista()
                  RefreshDialog
          EndIf
     Case Evento["KeyCode"] = -82 And Campo="TG_IMPCAMINHO":
          TC_INS =True
          VSDGIMPNOVOCAMINHO=""
          Campo="TG_IMPNOVOCAMINHO"
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case Campo="TG_IMPPORTA"      : Campo="NO"
						Case Campo="TG_IMPCAMINHO"    : Campo="TG_IMPPORTA"
            Case Campo="TG_IMPNOVOCAMINHO": Campo="TG_IMPCAMINHO"
                 VSDGIMPNOVOCAMINHO=Fill("-",30)
                 TC_INS =False
						Case Campo="TG_IMPTIPO"       : Campo="TG_IMPCAMINHO"
            Case Campo="TG_IMPPADRAO"     : Campo="TG_IMPTIPO"
            Case Campo="YES" Or Campo="NO": Campo="TG_IMPPORTA"
          EndSwitch
     Case Evento["KeyCode"] = 13:
          Temp=False
          If Checa_TAG()
             Then Switch
                    Case Campo="TG_IMPPORTA"       : Campo="TG_IMPCAMINHO"
                    Case Campo="TG_IMPCAMINHO"     : Campo="TG_IMPTIPO"
                         VSDGIMPCAMINHO=ControlValue("TG_IMPCAMINHO")
                         Switch
                           Case RTrim(SubStr(Array_IMPCAMINHO[VSDGIMPCAMINHO],70,10))="Matricial"  : VSDGIMPTIPO=1
                           Case RTrim(SubStr(Array_IMPCAMINHO[VSDGIMPCAMINHO],70,10))="Jato Tinta" : VSDGIMPTIPO=2
                           Case RTrim(SubStr(Array_IMPCAMINHO[VSDGIMPCAMINHO],70,10))="Laser"      : VSDGIMPTIPO=3
                         EndSwitch
                    Case Campo="TG_IMPNOVOCAMINHO" : Campo="TG_IMPTIPO"
                         VSDGIMPNOVOCAMINHO=ControlValue("TG_IMPNOVOCAMINHO")
                         Incluir_Lista()
                         TC_INS =False
                         RefreshDialog
                    Case Campo="TG_IMPTIPO"        : Campo="TG_IMPPADRAO"
                         VSDGIMPTIPO=ControlValue("TG_IMPTIPO")
                    Case Campo="TG_IMPPADRAO"      : Campo="YES"
                  EndSwitch
                  Monta_VSDGMSG()
          EndIf
   EndSwitch
EndIf
RefreshDialog
Switch
  Case Campo="" :
       MSG_CAMPO=""
  Case Campo="TG_IMPPORTA":
       MSG_CAMPO="Selecione a porta p/ captura !"
       TECLA    =""+"=Selecionar  "
	Case Campo="TG_IMPCAMINHO":
       MSG_CAMPO="Selecione o caminho associado !"
       If DynArraySize(Array_IMPCAMINHO)>1
          Then TECLA    =""+"=Selecionar  Ins=Novo  Del=Excluir  "
          Else TECLA    ="Ins=Novo  "
       EndIf
  Case Campo="TG_IMPTIPO":
       MSG_CAMPO="Selecione o tipo de impressora !"
       TECLA    =""+"=Selecionar  "
  Case Campo="TG_IMPPADRAO":
       MSG_CAMPO="Define se impressora ser  padr„o !"
       TECLA    ="Barra de espa‡o=Marca/Desmarca  "
  Case Campo="TG_IMPNOVOCAMINHO" And TC_INS:
       MSG_CAMPO="Digite o novo caminho a associar !"
EndSwitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc Monta_VSDGMSG()
VSDGMSG=Array_IMPPORTA[VSDGIMPPORTA]+" "
If SubStr(Array_IMPCAMINHO[VSDGIMPCAMINHO],1,7)="  Local"
   Then VSDGMSG=VSDGMSG+"Local"
   Else VSDGMSG=VSDGMSG+"Rede:"+RTrim(SubStr(Array_IMPCAMINHO[VSDGIMPCAMINHO],1,40))
EndIf
VSDGMSG=VSDGMSG+" : "+Array_IMPTIPO[VSDGIMPTIPO]
EndProc

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc Excluir_Lista()
Private i,c,v,ii,texto,nc

texto=Array_IMPCAMINHO[VSDGIMPCAMINHO]
Array_IMPCAMINHO[VSDGIMPCAMINHO]=""

DynArray Array_NIMPCAMINHO[]

nc=DynArraySize(Array_IMPCAMINHO)
c=1
For i From 1 To nc
  While True
    If IsBlank(Array_IMPCAMINHO[i]) Then QuitLoop EndIf
    ii=i
    For v From 1 To nc
      If Not IsBlank(Array_IMPCAMINHO[v]) And
         Array_IMPCAMINHO[v]<Array_IMPCAMINHO[ii]
         Then ii=v
      EndIf
    EndFor
    Array_NIMPCAMINHO[c]=Array_IMPCAMINHO[ii]
    Array_IMPCAMINHO[ii]=""
    c=c+1
  EndWhile
EndFor

VSDGIMPCAMINHO=NumVal(VSDGIMPCAMINHO)-1
For c from 1 to DynArraySize(Array_NIMPCAMINHO)
  If Array_NIMPCAMINHO[c]>texto
     Then VSDGIMPCAMINHO=c
          QuitLoop
  EndIf
EndFor

Release Vars Array_IMPCAMINHO
DynArray Array_IMPCAMINHO[]
For c from 1 to DynArraySize(Array_NIMPCAMINHO)
  Array_IMPCAMINHO[c]=Array_NIMPCAMINHO[c]
EndFor

Release Vars i,c,Array_NIMPCAMINHO,texto,ii,v,nc
EndProc

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc Incluir_Lista()
Private nc,i,ExisteLista

If VSDGIMPNOVOCAMINHO<>Fill("-",30)
   Then
        nc=DynArraySize(Array_IMPCAMINHO)
        ExisteLista=False
        For i From 1 To nc
          If VSDGIMPNOVOCAMINHO=RTrim(SubStr(Array_IMPCAMINHO[i],1,40))
             Then ExisteLista=True
                  VSDGIMPCAMINHO=i
                  QuitLoop
          EndIf
        EndFor

        If Not ExisteLista
           Then Array_IMPCAMINHO[nc+1]=Completa(VSDGIMPNOVOCAMINHO,70," ")+Completa(RTrim(ARRAY_IMPTIPO[VSDGIMPTIPO]),10," ")
                VSDGIMPCAMINHO=nc+1
        EndIf
EndIf

VSDGIMPNOVOCAMINHO=Fill("-",30)

Release Vars nc,i,ExisteLista
EndProc

;*******************************************************************************
;Rotina Ler Impressora Configuradas                                            *
;*******************************************************************************
Proc Ler_Impressoras()
Private wcs, caminho
Array_IMPCAMINHO[1]="  Local"
If Not IsFile(PrivDir()+"Net_Imp.Txt")
   Then Return
EndIf
Editor Open PrivDir()+"Net_Imp.Txt"
wcs=2
While True
  Extrair_Linha()
  If IsBlank(ELinha) Then QuitLoop EndIf
  If SubStr(ELinha,2,3)<>"LPT" And
     SubStr(ELinha,2,3)<>"---" And
     SubStr(ELinha,1,3)<>"123" And
     Not IsBlank(RTrim(ELinha))
     Then caminho=RTrim(SubStr(ELinha,12,68))
          caminho=Completa(RTrim(caminho),69," ")
          Array_IMPCAMINHO[WCS]=Caminho+SubStr(ELinha,1,10)
          wcs=wcs+1
  EndIf
EndWhile
CancelEdit

Release Vars wcs,ELinha,ci,cf,Inforray
EndProc

;*******************************************************************************
;Rotina Salva Impressora Compartilhadas                                        *
;*******************************************************************************
Proc Salva_Impressoras()
Private nc,i,linha

SYS_ARRAY_IMPOPCOES[NumVal(VSDGIMPPORTA)]=                                          "D "+ARRAY_IMPPORTA[VSDGIMPPORTA]
SYS_ARRAY_IMPOPCOES[NumVal(VSDGIMPPORTA)]=SYS_ARRAY_IMPOPCOES[NumVal(VSDGIMPPORTA)]+" "+Completa(RTrim(ARRAY_IMPTIPO[VSDGIMPTIPO]),10," ")
SYS_ARRAY_IMPOPCOES[NumVal(VSDGIMPPORTA)]=SYS_ARRAY_IMPOPCOES[NumVal(VSDGIMPPORTA)]+" "+LTrim(RTrim(SubStr(ARRAY_IMPCAMINHO[VSDGIMPCAMINHO],1,69)))

ARRAY_IMPCAMINHO[VSDGIMPCAMINHO]=Completa(RTrim(SubStr(ARRAY_IMPCAMINHO[VSDGIMPCAMINHO],1,69)),69," ")+Completa(RTrim(ARRAY_IMPTIPO[VSDGIMPTIPO]),10," ")

FileWrite        PrivDir()+"Net_Imp.Txt" From ""
For i From 1 to 3
  Linha=Completa(" "+RTrim(SubStr(SYS_ARRAY_IMPOPCOES[i],3,100)),78," ")+"*"
  FileWrite Append PrivDir()+"Net_Imp.Txt" From Linha +"\n"
EndFor
FileWrite Append PrivDir()+"Net_Imp.Txt" From Fill("-",79)+"\n"

nc=DynArraySize(Array_IMPCAMINHO)
For i From 2 To nc
  Linha=Completa(RTrim(SubStr(Array_IMPCAMINHO[i],70,10)),11," ")+SubStr(Array_IMPCAMINHO[i],01,69) Enter
  FileWrite Append PrivDir()+"Net_Imp.Txt" From Linha+"\n"
EndFor

Release Vars nc,i,linha
EndProc

;*******************************************************************************
;Rotina Ler Impressora Capturada                                               *
;*******************************************************************************
Proc Captura()
Private msg, caminho_antigo, caminho_novo

caminho_antigo=RTrim(SubStr(SYS_ARRAY_IMPOPCOES[NumVal(VSDGIMPPORTA)],21,60))
If caminho_antigo<>"Local"
   Then caminho_antigo=SubStr(caminho_antigo,1,Len(caminho_antigo)-2)
EndIf

caminho_novo=LTrim(Rtrim(SubStr(Array_IMPCAMINHO[VSDGIMPCAMINHO],1,69)))

If ( caminho_novo="Local" And ( caminho_antigo="Local" Or caminho_antigo="" ) ) Or
   ( Upper(caminho_antigo)=Upper(caminho_novo) )
   Then msg=""
        Return msg
   Else
        msg=""
        If (caminho_novo ="Local" And caminho_antigo<>"Local") Or
           (caminho_novo<>"Local" And caminho_antigo<>"Local")
           Then Finaliza_Captura(Array_IMPPORTA[VSDGIMPPORTA])
                msg="Finalizada captura para "+Array_IMPPORTA[VSDGIMPPORTA]+" !"
        EndIf
        If (caminho_novo<>"Local" And caminho_antigo ="Local") Or
           (caminho_novo<>"Local" And caminho_antigo<>"Local")
           Then Aguarde("Aguarde, localizando computador e impressora na rede")
                Executa_Captura(Array_IMPPORTA[VSDGIMPPORTA]+" "+caminho_novo)
                msg=Checa_Captura(Array_IMPPORTA[VSDGIMPPORTA],caminho_novo)
                LimpAguarde()
        EndIf
        Return msg
EndIf
Release Vars msg, caminho_antigo, caminho_novo
EndProc


;*******************************************************************************
;Rotina Salva Impressora Capturada                                             *
;*******************************************************************************
Proc Salva_ImpPadrao()

If Array_IMPPADRAO[1]
   Then
        FileWrite        PrivDir()+"Cfg_Imp.sc" From ";** Variaveis de Configura‡„o de Impressora Vise Informatica"+"\n"
        Switch
          Case VSDGIMPTIPO=1:
            FileWrite Append PrivDir()+"Cfg_Imp.sc" From "Sys_Set_Impressora  ="+Chr(34)+"\\027x0\\015"+Chr(34)+"\n"
            FileWrite Append PrivDir()+"Cfg_Imp.sc" From "Sys_Reset_Impressora="+Chr(34)+"\\027x0\\018"+Chr(34)+"\n"
          Case VSDGIMPTIPO=2 Or VSDGIMPTIPO=3:
            FileWrite Append PrivDir()+"Cfg_Imp.sc" From "Sys_Set_Impressora  =Chr(027)+Chr(040)+Chr(115)+"+Chr(34)+"16.67"+Chr(34)+"+Chr(072)"+"\n"
            FileWrite Append PrivDir()+"Cfg_Imp.sc" From "Sys_Reset_Impressora=Chr(027)+Chr(040)+Chr(115)+"+Chr(34)+"10"+Chr(34)+"+Chr(072)"+"\n"
        EndSwitch
        FileWrite Append PrivDir()+"Cfg_Imp.sc" From "Sys_Set_Porta       =\""+SubStr(Array_IMPPORTA[VSDGIMPPORTA],1,4)+"\""+"\n"

        Play PrivDir()+"Cfg_Imp"
EndIf
EndProc


;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
AutoLib="\\Sistemas\\Cadastro\\Bib_Show,\\Sistemas\\Cadastro\\Bibliot"

DynArray SYS_ARRAY_IMPOPCOES[]
DynArray Array_IMPPORTA[]
DynArray Array_IMPCAMINHO[]
Array Array_IMPTIPO[3]
DynArray Array_IMPPADRAO[]

Array_IMPPORTA[1]="LPT1:"
Array_IMPPORTA[2]="LPT2:"
Array_IMPPORTA[3]="LPT3:"

Array_IMPTIPO[1]="Matricial"
Array_IMPTIPO[2]="Jato Tinta"
Array_IMPTIPO[3]="Laser"

While True
  VSDGIMPPORTA      =1
  VSDGIMPCAMINHO    =1
  VSDGIMPTIPO       =1
  Array_IMPPADRAO[1]=False
  VSDGMSG=""

  Ler_Impressoras()
  VSDGIMPNOVOCAMINHO=Fill("-",30)
  Ler_Captura("")
  Ler_Captura(PrivDir()+"Cfg_Imp")

  TC_INS =False
  ShowDialog "Capturar impressora" Proc "SD_Dialog"
   Trigger "OPEN","ARRIVE"
    Key "F1","ENTER","ESC",-82,-83,
       -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140
     @ 04,17   Height 18 Width 63

    Frame Single From 00,00 to 04,60
    Label @ 00,02 "Status "  For "TG_IMPSTATUS"
    PickDynArray @1,01 Height 2 width 59
     Columns 1
      SYS_ARRAY_IMPOPCOES
       Tag "TG_IMPSTATUS"
        To VSDGIMPOPCAO

    Frame Single From 05,00 to 10,08
    Label @ 05,01 "~P~ortas "  For "TG_IMPPORTA"
    PickDynArray @6,01 Height 4 width 7
     Columns 1
      Array_IMPPORTA
       Tag "TG_IMPPORTA"
        To VSDGIMPPORTA

    Frame Single From 05,09 to 10,43
    Label @ 05,10 "C~a~minhos "  For "TG_IMPCAMINHO"
    PickDynArray @6,10 Height 3 width 32
     Columns 1
      Array_IMPCAMINHO
       Tag "TG_IMPCAMINHO"
        To VSDGIMPCAMINHO

    Accept @ 09,10 Width 32 "A40" Picture "\\\\[!*!]" Tag "TG_IMPNOVOCAMINHO" To VSDGIMPNOVOCAMINHO

    Frame Single From 05,44 to 10,60
    Label @ 05,45 "~T~ipos "  For "TG_IMPTIPO"
    PickArray @6,45 Height 4 width 15
     Columns 1
      Array_IMPTIPO
       Tag "TG_IMPTIPO"
        To VSDGIMPTIPO

    @ 11,01 ?? VSDGMSG
    PaintCanvas Attribute 111 11, 00, 11, 60

    CheckBoxes @12,00 Height 1 Width 24 Tag "TG_IMPPADRAO"
    "~D~efinir como padr„o" to Array_IMPPADRAO[1]

    Pushbutton @ 14,18 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 14,32 width 11 "~C~ancela" CANCEL                    Tag "NO"  To BottonValue

  EndDialog

  If RetVal
     Then MSG=Captura()
          If Not IsBlank(MSG) Then Mensagem(MSG) EndIf
          If Checou_Impressora
             Then Salva_ImpPadrao()
                  Salva_Impressoras()
          EndIf
     Else QuitLoop
  EndIf
  Loop

EndWhile
ClearAll
Release Vars Array_IMPPORTA, Array_IMPCAMINHO, Array_IMPTIPO, Array_IMPPADRAO,
             VSDGIMPPORTA, VSDGIMPCAMINHO, VSDGIMPCAMINHO, VSDGIMPNOVOCAMINHO,
             VSDGIMPTIPO

Release Procs Monta_VSDGMSG, Monta_Compartilhamento, Salva_Compartilhamento, Captura,
        Excluir_Lista, Incluir_Lista, Ler_Impressoras, Salva_Impressoras, Salva_ImpPadrao

