DBF Viewer 2020 – Código Fuente II

*------------------------------------------------------*
Procedure SeleArea()
*------------------------------------------------------*
   If !Empty( Alias() )

      DBSelectArea( ( Alias() )->( Select( oWndBase.Tab_1.caption( oWndBase.Tab_1.value ) ) ) )
      cBase := Alias()

      If IsControlDefine(&(Browse_n()),oWndBase)
         oWndBase.&(Browse_n()).SetFocus
      Endif

   Endif

Return

*------------------------------------------------------------------------------*
Procedure CierraBase()
*------------------------------------------------------------------------------*
   If !Empty( Alias() )

      oTab      := GetControlObject("Tab_1","oWndBase")
      nPos      := ( ( Alias() )->( Select( oWndBase.Tab_1.caption( oWndBase.Tab_1.value ) ) ) )

      Set Index To

      If IsControlDefine( &(Browse_n()), oWndBase )
         oWndBase.&(Browse_n()).release
         Close ( oWndBase.Tab_1.caption( oWndBase.Tab_1.value ) )
         oTab:DeletePage ( oTab:value, oTab:caption( oTab:value ) )
         If oWndBase.Tab_1.ItemCount > 0
            DBSelectArea( ( Alias() )->( Select( oWndBase.Tab_1.caption( oWndBase.Tab_1.value ) ) ))
            Titulo()
         Else
            Close data
            oWndBase.Tab_1.release
            aFiles   := {}
            aNewFile := {}
            nRecCopy := {}
            OpenBase("")
         Endif
      Else
         Close data
      Endif

   Endif

return

*------------------------------------------------------------------------------*
Procedure CierraAll()
*------------------------------------------------------------------------------*
   Local nc
   If !Empty( Alias() )
       For nc := 1 to oWndBase.Tab_1.ItemCount
           Close ( oWndBase.Tab_1.caption( nc ) )
       Next
       oWndBase.Tab_1.release
       aFiles   := {}
       aNewFile := {}
       nRecCopy := {}
       OpenBase("")
   Endif

Return

*------------------------------------------------------*
Function Iniciando()     // Cortesía: Fernando Yurisich
*------------------------------------------------------*
   ooWndBase:AcceptFiles := .T.
   ooWndBase:OnDropFiles := { |f| AEval( f, { |c| OpenBase( c ) } ) }
Return Nil

*------------------------------------------------------*
Function AssiCtrlBrw( cTypeField, nlongTot, nLongDec, cFieldName )
*------------------------------------------------------*

   cCtrlBrw := ""
   cComa    := iif( (FieldNum(cFieldName)<=FCount()-1),',','')

   If cTypeField=='L'
      //cCtrlBrw := "{'CHECKBOX','Yes','No'}"+cComa
      cCtrlBrw := "TGridControlLComboBox():New( 'Yes', 'No' )"+cComa
   Elseif cTypeField=='D'
      //cCtrlBrw := "{'DATEPICKER','UPDOWN'}"+cComa
      cCtrlBrw := "TGridControlDatePicker():New( .t., .t. )"+cComa
   Elseif cTypeField=='@' .OR. cTypeField=='T'
      cCtrlBrw := "{'TEXTBOX','DATE',''}"+cComa
      //cCtrlBrw := "{'TIMEPICKER','UPDOWN'}"+cComa
   Elseif cTypeField=='N' .OR. cTypeField=='I' .OR. cTypeField=='W' .OR. cTypeField=='Y' .OR. cTypeField=='B'
      cInpMsk  := iif( nLongDec > 0, REPLICATE( '9', nLongTot - nLongDec -1 ) + '.' + REPLICATE( '9', nLongDec ), REPLICATE( '9', nLongTot - nLongDec ) )
      cCtrlBrw := "{'TEXTBOX','NUMERIC','"+cInpMsk+"'}"+cComa
   Elseif cTypeField=='C'
//      cCtrlBrw := "{'TEXTBOX','CHARACTER','@A'}"+cComa
      cCtrlBrw := "{'TEXTBOX','CHARACTER',}"+cComa
   Elseif cTypeField=='M' .OR. cTypeField=='G' .OR. cTypeField=='P'
      cCtrlBrw := "{}"+cComa
      //cCtrlBrw := "TGridControlMemo():New('Edit Memo Field')"+cComa
   Else
      cCtrlBrw := "{}"+cComa
   Endif

Return( cCtrlBrw )

*------------------------------------------------------------------------------*
Function VerHeadIcon( aTip )
*------------------------------------------------------------------------------*
   Local nv
   aFtype    := aClone( aTip )
   aHeadIcon := {"hdel"}

   For nv := 1 to FCount()
       Do Case
          Case aFType[nv]=='L'
               aadd(aHeadIcon,"hlogic")
          Case aFType[nv]=='D'
               aadd(aHeadIcon,"hfech")
          Case aFType[nv]=='@'
               aadd(aHeadIcon,"hfech")
          Case aFType[nv]=='N'
               aadd(aHeadIcon,"hnum")
          Case aFType[nv]=='B'
               aadd(aHeadIcon,"hnum")
          Case aFType[nv]=='I'
               aadd(aHeadIcon,"hnum")
          Case aFType[nv]=='C'
               aadd(aHeadIcon,"hchar")
          Case aFType[nv]=='M'
               aadd(aHeadIcon,"hmemo")
          Case aFType[nv]=='G'
               aadd(aHeadIcon,"hmemo")
          Case aFType[nv]=='T'
               aadd(aHeadIcon,"hfech")
          Case aFType[nv]=='P'
               aadd(aHeadIcon,"hmemo")
          Case aFType[nv]=='W'
               aadd(aHeadIcon,"hnum")
          Case aFType[nv]=='Y'
               aadd(aHeadIcon,"hnum")
          Otherwise
               aadd(aHeadIcon,"hchar")
       Endcase
   Next

Return( aHeadIcon )

*------------------------------------------------------------------------------*
Function SetHeaderImages()
*------------------------------------------------------------------------------*
   Local nc

   If !Empty( Alias() )

      For nc := 1 to oWndBase.Tab_1.ItemCount
          cAreaPos  := AllTrim( Str(nc) )
          cBrowse_n := "Browse_"+cAreaPos

          If IsControlDefined(&cBrowse_n,oWndBase)
             oWndBase.&(cBrowse_n).Fontname  := cFont
             oWndBase.&(cBrowse_n).Fontsize  := nSize
             oWndBase.&(cBrowse_n).fontcolor := aFntClr
             oWndBase.&(cBrowse_n).Backcolor := aBackClr
          Endif
      Next
   Endif

Return Nil

*------------------------------------------------------------------------------*
Function GetIndexInfo()
*------------------------------------------------------------------------------*
   LOCAL aInd:= {}, ig, cKey

   If !Empty( Alias() )
      FOR ig := 1 to 50
         IF ( cKey := ( Alias() )->( IndexKey( ig ) ) ) == ''
            EXIT
         ENDIF
         Aadd( aInd, "TAG "+( Alias() )->( OrdName( ig ) ) + ' : ' + "KEY " + cKey )
      NEXT
   Endif

RETURN aInd

*------------------------------------------------------------------------------*
Procedure SeleOrder()
*------------------------------------------------------------------------------*
   If !Empty( Alias() )
      ( Alias() )->(OrdSetFocus(0))
   Endif
Return

*------------------------------------------------------------------------------*
Procedure IndexItems()
*------------------------------------------------------------------------------*
   Local ni
   If !Empty( Alias() )
      nTags := ( Alias() )->( OrdCount() )
      for ni = 1 to nTags
         if ! Empty( OrdName( ni ) )
            if ! Empty( OrdName( 1 ) )
               DbSetOrder( OrdName( 1 ) )
               DbGoTop()
            endif
            ITEM OrdName( ni ) ACTION  ( Alias() )->( DbSetOrder( OrdName(ni) ) )
         endif
      next
   Endif
Return

*------------------------------------------------------------------------------*
Procedure CopyRec(nOp)  // Copiar
*------------------------------------------------------------------------------*
   If !Empty( Alias() )

       If nOp = 1   // Selecciona registro
          RecReply()
          MuestraRec()
       Else         // Limpia Selección de registro
          nRecCopy := {}
          nRecSel := 0
          oWndBase.StatusBar.Item(2) := 'Selected Record: '
          Actualizar()
       Endif
   Endif
return

*------------------------------------------------------------------------------*
Function RecToMost()
*------------------------------------------------------------------------------*
   Local nr
   If Empty(nRecCopy)
      nRet := 0
   Else
      For nr := 1 to Len(nRecCopy)
          nRet := nRecCopy[ iif(AScan( nRecCopy[nr], nArea )>0, AScan( nRecCopy[nr], nArea ),1), 2 ]
      Next
   Endif
Return(nRet)

*------------------------------------------------------------------------------*
Function RecReply()
*------------------------------------------------------------------------------*

   aRec := oWndBase.&( Browse_n() ).Value

   If Empty(nRecCopy)
      Aadd( nRecCopy, { ( Alias() )->( Select( oWndBase.Tab_1.caption( oWndBase.Tab_1.value ) ) ), aRec[1] } )
   Endif

Return Nil

*------------------------------------------------------------------------------*
procedure PasteRec() // Pegar
*------------------------------------------------------------------------------*
   local nPos   := 0, i := 1
   local aDatos := {}

   If !Empty( Alias() )
      If Empty(nRecCopy)
         Msginfo('No Selected Record',PROGRAM)
      Else
        If ( Alias() )->( Select() ) = nRecCopy[ 1, 1 ]
           If MsgYesNo("This action will replace the current record"+Hb_OsNewLine()+"with the data of the selected record"+Hb_OsNewLine()+"Are you sure?",PROGRAM)
              nPos := oWndBase.&( Browse_n() ).Value
              ( Alias() )->( DBGoTo( nRecCopy[ 1, 2 ] ) )
              For i = 1 to ( Alias() )->( FCount() )
                  Aadd( aDatos, ( Alias() )->( Fieldget(i) ) )
              Next
              ( Alias() )->( DbGoTo( nPos[1]) )
              ( Alias() )->( flock() )
              For i = 1 to ( Alias() )->( Fcount() )
                  ( Alias() )->( Fieldput( i, adatos[i] ) )
              Next
              ( Alias() )->( DbUnLock() )
              nRecSel  := 0
              nRecCopy := {}
              oWndBase.StatusBar.Item(2) := 'Selected Record: '
           Endif
        Endif
      EndIf
      Actualizar()
   Endif

Return

*------------------------------------------------------------------------------*
Function DBF_Idx()   // Indices de la base de datos
*------------------------------------------------------------------------------*
   Local ix
   lSalida := .t. ; k := 1 ; nVeces := 1
   aIndice       := {}
   aIndiceCampo  := {}

   Do while lSalida
      If ( (Alias() )->( OrdName( k ) ) == "" )
         lSalida := .f.
      Else
         cIndice := ( Alias() )->( OrdName( k ) )
         aAdd( aIndice, cIndice )
         cClave := Upper( (Alias() )->( OrdKey( k ) ) )
         For ix := 1 to FCount()
             If nVeces <= 1
                nInicio := At( FieldName(ix), cClave )
                If  nInicio != 0
                    aAdd( aIndiceCampo, ix )
                    nVeces++
                Endif
             Endif
         Next
      Endif
      k++
      nVeces := 1
   Enddo

   // Numero de indice
   If ( (Alias())->( ordSetFocus() ) == "" )
      nIndiceActivo := 1
   Else
      nIndiceActivo := AScan( aIndice, (Alias())->( OrdSetFocus() ) )
   Endif

   AutoMsgInfo( aIndice, "aIndice" )

Return(aIndiceCampo)

 

DBF Viewer 2020 – Código Fuente I

2015-03-25_160535

/*
 *
 * Program to view DBF files using standard Browse control
 * Miguel Angel Juárez A. - 2009-2015 MigSoft <mig2soft/at/yahoo.com>
 * Includes the code of Grigory Filatov <[email protected]>
 * and Rathinagiri <[email protected]>
 *
 */

#include "oohg.ch"
#include "dbstruct.ch"
#include "fileio.ch"
#include "dbuvar.ch"

*------------------------------------------------------------------------------*
Function Main( cDBF )
*------------------------------------------------------------------------------*

   REQUEST DBFNTX
   REQUEST DBFCDX, DBFFPT
   RDDSETDEFAULT( "DBFCDX" )
   SET AUTOPEN OFF

   Publicar()

   Load window oWndBase

   ON KEY F3 OF oWndBase ACTION AutoMsgInfo( aFiles, "aFiles" )
   ON KEY F4 OF oWndBase ACTION AutoMsgInfo( nRecCopy, "nRecCopy" )
   ON KEY F5 OF oWndBase ACTION AutoMsgInfo( ( Alias() )->( Select() ), "Select()" )
   ON KEY F6 OF oWndBase ACTION AutoMsgInfo( ( Alias() )->( Dbf() ), "Dbf()" )
   ON KEY F7 OF oWndBase ACTION AutoMsgInfo( ( Alias() )->( Used() ), "Used()" )

   If PCOUNT() > 0
      OpenBase( cDBF )
   Else
      OpenBase( "" )
   Endif

   oWndBase.Center
   oWndBase.Activate

Return Nil

*------------------------------------------------------------------------------*
Procedure Publicar()
*------------------------------------------------------------------------------*

    Public nAltoPantalla  := GetDesktopHeight() + GetTitleHeight() + GetBorderHeight()
    Public nAnchoPantalla := GetDesktopWidth()
    Public nRow           := nAltoPantalla  * 0.10
    Public nCol           := nAnchoPantalla * 0.10
    Public nWidth         := nAnchoPantalla * 0.95
    Public nHeight        := nAltoPantalla  * 0.85
    Public _OOHG_PRINTLIBRARY
    Public cBaseFolder, aTypes, aNewFile := {}, aFtype := {}, aCtrl := {}
    Public nCamp, aEst    := {}, aNomb := {}, aJust := {}, aLong := {}, i, cBase
    Public cFont          := 'MS Sans Serif'
    Public nSize          := 8 , Nuevo := .F.
    Public nRecCopy       := {}
    Public aArea          := {}
    Public aFiles         := {}
    Public nArea          := 0
    Public nBrow          := 0
    Public nBase          := 0
    Public nRecSel        := 0
    Public nPage          := 1
    Public aFntClr        := {0,0,0}
    Public aBackClr       := {255,255,255}
    Public aSearch        := {}, aReplace := {}
    Public nSearch        := 1, nReplace := 1, nColumns := 1
    Public lMatchCase     := .F., lMatchWhole := .F.
    Public nDirect        := 3, cDateFormat := "DD.MM.YYYY"
    Public _DBULastPath   := ''
    Public VERSION        := "v."+substr(__DATE__,3,2)+"."+right(__DATE__,4)
    HB_LANGSELECT( "EN" )
    DECLARE WINDOW Form_Query
    DECLARE WINDOW Form_Find
    DECLARE WINDOW _DBUcreadbf
    DECLARE WINDOW Form_Prop
    DECLARE WINDOW oWndBase

Return

*------------------------------------------------------------------------------*
Procedure OpenBase( cDBF )
*------------------------------------------------------------------------------*
   local nn, aTemp := {}

   cBaseFolder := GetStartupFolder()
   LoadArchIni(cBaseFolder+'\')

   If Empty(cDBF) .OR. ValType ( cDBF ) == 'U'
      If !IsControlDefined(Tab_1,oWndBase)
         oWndBase.Image_1.Show
      Endif
      aTypes   := { {'Database files (*.dbf)', '*.dbf'} }
      aTemp    := iif( !Empty(aNewFile),aNewFile[1],"")
      aNewFile := GetFile( aTypes, 'Select database files', CurDir(), .T. )
      If Empty(aNewFile)
         Aadd( aNewFile, aTemp )
      Endif
   Else
      AAdd( aNewFile, cDBF )
   Endif

   IF !Empty(aNewFile)
       For nn := 1 to Len(aNewFile)
           If !Empty(aNewFile[nn]) .AND. Upper(Right(aNewFile[nn],3))="DBF"
                  If DB_Open( aNewFile[nn] )
                     _DBULastPath := hb_Curdrive()+':\'+CurDir()+'\'

                     If Used(aNewFile[nn])

                        Aadd( aFiles, aNewFile[nn] )
                        oWndBase.Title := PROGRAM+VERSION+COPYRIGHT+aNewfile[nn]

                        ArmMatrix()

                        cAreaPos  := AllTrim( Str( ( Alias() )->( Select( oWndBase.Tab_1.caption( oWndBase.Tab_1.value ) ) ) ) )
                        cBrowse_n := "Browse_"+cAreaPos

                        oWndBase.&(cBrowse_n).ColumnsAutoFitH
                        oWndBase.&(cBrowse_n).SetFocus             // Ilumina barra en primer registro
                        oWndBase.&(cBrowse_n).GoTop
                     Endif

                  Endif

           Endif
       Next nn

   Else
      cBase := ''
      MuestraRec()
      oWndBase.Title := PROGRAM+VERSION+COPYRIGHT
      If !IsControlDefined(Tab_1,oWndBase)
         oWndBase.Image_1.Show
      Endif
   Endif

Return

*------------------------------------------------------------------------------*
Procedure ArmMatrix()
*------------------------------------------------------------------------------*
   Local i
   cBase := Alias() ; nCamp := Fcount() ; aEst  := DBstruct() ; aCtrl := {}
   aNomb := {'iif(deleted(),0,1)'} ; aJust := {0} ; aLong := {0} ; aFtype:={}
   cCtrl := "{},"

   For i := 1 to nCamp
       Aadd(aNomb,aEst[i,1])                             // Carga el nombre de campo
       Aadd(aJust,iif(aEst[i,2]=='N',1,0))               // Justifica a la izquierda o derecha de acuerdo al tipo de dato
       cCtrl += AssiCtrlBrw( aEst[i,2], aEst[i,3],aEst[i,4],FieldName(i) )   // control por tipo de campo
       Aadd(aLong,Max(100,Min(160,aEst[i,3]*14)))        // Asigna la longitud del dato en el browse
       If     aEst[i,2]=="I" .OR. aEst[i,2]=="W" .OR. aEst[i,2]=="Y" .OR. aEst[i,2]=="B"
              aEst[i,2]:= 'N'
       ElseIf aEst[i,2]=="G" .OR. aEst[i,2]=="P"
              aEst[i,2]:= 'M'
       ElseIf aEst[i,2]=="@" .OR. aEst[i,2]=="T"
              aEst[i,2]:= 'D'
       Endif
       Aadd(aFtype, aEst[i,2])                           // Carga el tipo de campo
   Next

   aCtrl :=  &("{"+cCtrl+"}")                            // Asigna controles por tipo de campo
   CreaBrowse( cBase, aNomb, aLong, aJust, aFtype, aCtrl )

Return

*------------------------------------------------------------------------------*
Function DB_Open( cFileDBF )
*------------------------------------------------------------------------------*
   lSuc := .F.
      TRY
          If ! ( DelExt(GetName(cFileDBF)) )->( Used() )
             Use ( cFileDBF ) New
             lSuc := .T.
             Aadd( aArea, ( Alias() )->( Select() ) )
             nArea++
             nBase++
          Endif
      CATCH loError
          MsgInfo("Unable open file: "+cFileDBF, PROGRAM+" TRY")
      END
Return (lSuc)

*---------------------------------------------------------------------*
FUNCTION GetName(cFileName)
*---------------------------------------------------------------------*
  LOCAL cTrim  := ALLTRIM(cFileName)
  LOCAL nSlash := MAX(RAT('\', cTrim), AT(':', cTrim))
  LOCAL cName  := IF(EMPTY(nSlash), cTrim, SUBSTR(cTrim, nSlash + 1))
RETURN( cName )

*---------------------------------------------------------------------*
FUNCTION DelExt(cFileName)
*---------------------------------------------------------------------*
  LOCAL cTrim  := ALLTRIM(cFileName)
  LOCAL nDot   := RAT('.', cTrim)
  LOCAL nSlash := MAX(RAT('\', cTrim), AT(':', cTrim))
  LOCAL cNamew := IF(nDot <= nSlash .OR. nDot == nSlash + 1, ;
                  cTrim, LEFT(cTrim, nDot - 1))
RETURN( cNamew )

*------------------------------------------------------------------------------*
Function CreaBrowse( cBase, aNomb, aLong, aJust, aFtype, aCtrl )
*------------------------------------------------------------------------------*
    aHdr       := aClone(aNomb)
    aJst       := aClone(aJust)
    aHdr[1]    := ""
    aLong[1]   := 20
    aCabImg    := aClone(VerHeadIcon(aFtype))

    oWndBase.Image_1.Hide

    If IsControlDefined(Tab_1,oWndBase)
       NuevoTab()
    Else
       DEFINE TAB Tab_1 OF oWndBase AT 40,15 WIDTH ooWndBase:Clientwidth  - 30 HEIGHT ooWndBase:Clientheight - 70 ;
       VALUE 1 FONT "Arial" SIZE 9 FLAT ON CHANGE SeleArea()
           PAGE cBase IMAGE "Main1"
                MakeBrowse()
           END PAGE
       END TAB

       oTab := GetControlObject("Tab_1","oWndBase")
       oTab:Anchor := "TOPLEFTBOTTOMRIGHT"

    Endif

    SetHeaderImages()

Return Nil

*------------------------------------------------------*
Procedure MakeBrowse()
*------------------------------------------------------*
   cAreaPos  := AllTrim( Str( ( Alias() )->( Select() ) ) )
   cBrowse_n := "Browse_"+cAreaPos

           If !IsControlDefined(&cBrowse_n,oWndBase)

                  @ 26,0 BROWSE &cBrowse_n              ;
                     OF oWndBase                        ;
                     WIDTH  ooWndBase:Clientwidth  - 32  ;
                     HEIGHT ooWndBase:Clientheight - 100 ;
                     HEADERS aHdr                       ;
                     WIDTHS aLong                       ;
                     WORKAREA &( Alias() )              ;
                     FIELDS aNomb                       ;
                     VALUE 0                            ;
                     FONT "MS Sans Serif" SIZE 8        ;
                     TOOLTIP ""                         ;
                     ON CHANGE { || MuestraRec() }      ;
                     IMAGE { "br_no", "br_ok" }         ;
                     JUSTIFY aJst                       ;
                     COLUMNCONTROLS aCtrl               ;
                     LOCK                               ;
                     EDIT                               ;
                     INPLACE                            ;
                     DELETE                             ;
                     ON HEADCLICK Nil                   ;
                     HEADERIMAGES aCabImg               ;
                     DOUBLEBUFFER                       ;
                     NAVIGATEBYCELL                     ;
                     SELECTEDCOLORS { WHITE, {65,105,225},           ;           // Cursor Fuente/Fondo
                                      WHITE, {128,128,128},          ;           // Cursor ventana sin foco Fuente/Fondo
                                      {106,90,205}, {135,206,250},   ;           // Fila resaltada  Fuente/Fondo
                                      {105,105,105},{220,220,220} }              // Fila resaltada click en columna  Fuente/Fondo

           Endif

   oBrowse := GetControlObject(cBrowse_n,"oWndBase")
   oBrowse:Anchor := "TOPLEFTBOTTOMRIGHT"

   nBrow++

Return


*------------------------------------------------------*
Procedure NuevoTab() // Cortesía: Ciro Vargas Clemow
*------------------------------------------------------*
   cAreaPos  := AllTrim( Str( aArea[nBase] ) )
   cBrowse_n := "Browse_"+cAreaPos

   oTab := GetControlObject("Tab_1","oWndBase")

      oTab:AddPage ( ( Alias() )->( Select() ), Alias() )
      nPage++
      oTab:Value := ( Alias() )->( Select() )

      MakeBrowse()

      oTab:AddControl( cBrowse_n, ( Alias() )->( Select() ), 26, 0 )

Return