Code: Select all
#include "FiveMac.ch"
request DbfCdx
static oWnd
//----------------------------------------------------------------------------//
function Main()
local oBar
BuildMenu()
DEFINE WINDOW oWnd TITLE "FiveDBU for FiveMac"
oWnd:SetSize( ScreenWidth(), 0 )
oWnd:SetPos( ScreenHeight() - oWnd:nHeight, 0 )
DEFINE TOOLBAR oBar OF oWnd
DEFINE BUTTON OF oBar PROMPT "New" IMAGE ImgPath() + "new.png" ;
ACTION New()
DEFINE BUTTON OF oBar PROMPT "Open" IMAGE ImgPath() + "open.png" ;
ACTION Open()
DEFINE BUTTON OF oBar PROMPT "Exit" IMAGE ImgPath() + "exit2.png" ;
ACTION oWnd:End()
ACTIVATE WINDOW oWnd ;
VALID MsgYesNo( "Want to end ?" )
return nil
//----------------------------------------------------------------------------//
function BuildMenu()
local oMenu
MENU oMenu
MENUITEM "Files"
MENU
MENUITEM "New..." ACTION New()
MENUITEM "Open..." ACTION Open()
SEPARATOR
MENUITEM "Exit..." ACTION oWnd:End()
ENDMENU
MENUITEM "Help"
MENU
MENUITEM "Wiki..."
MENUITEM "About..." ACTION MsgAbout( "(C) FiveTech Software 2012", "FiveMac" )
ENDMENU
ENDMENU
return oMenu
//----------------------------------------------------------------------------//
function New()
local oDlg, oBrw, cFieldName := "", cType, nLength := 10, nDec := 0
local cFileName := "", cRDD
local aFields := { }
local oGet,oGetDeci ,oGetName,oGetLen,oCbx
local oBtnCreate,oBtnEdit
local aStruct
DEFINE DIALOG oDlg TITLE "DBF Builder" ;
FROM 207, 274 TO 590, 790
@ 351, 20 SAY "FieldName:" OF oDlg SIZE 78, 17
@ 326, 20 GET oGet VAR cFieldName OF oDlg SIZE 125, 22
@ 351, 153 SAY "Type:" OF oDlg SIZE 51, 17
@ 326, 153 COMBOBOX oCbx VAR cType OF oDlg ;
SIZE 124, 25 ITEMS { "Character", "Numeric", "Logical", "Date", "Memo" } ;
ON CHANGE ( Iif(cType== "Numeric",oGetDeci:enabled() ,oGetDeci:disabled() ) )
@ 351, 285 SAY "Length:" OF oDlg SIZE 50, 17
@ 326, 285 GET oGetLen VAR nLength OF oDlg SIZE 43, 22
oGetLen:setnumeric()
@ 351, 336 SAY "Dec:" OF oDlg SIZE 31, 17
@ 326, 336 GET oGetDeci VAR nDec OF oDlg SIZE 43, 22
oGetDeci:disabled()
@ 73, 20 LISTBOX oBrw FIELDS "", "", "", "" ;
HEADERS "FieldName", "Type", "Length", "Decimals" ;
OF oDlg SIZE 379, 245 ;
ON CHANGE ( cFieldName:= aFields[oBrw:nrowPos,1],oGet:refresh(),;
cType:= aFields[oBrw:nrowPos,2], oCbx:refresh(),;
nLength:= aFields[oBrw:nrowPos,3],oGetLen:refresh() ,;
nDec := aFields[oBrw:nrowPos,4],oGetDeci:refresh() )
oBrw:SetArray( aFields )
oBrw:bLine = { | nRow | { aFields[ nRow ][ 1 ], aFields[ nRow ][ 2 ], alltrim(str(aFields[ nRow ][ 3 ])),;
alltrim(str(aFields[ nRow ][ 4 ] ))} }
oBrw:SetSelectorStyle( 1 )
oBrw:SetAlternateColor( .t. )
@ 324, 407 BUTTON "Add" OF oDlg ;
ACTION ( btnAddField(@aFields,{ cFieldName,cType,nLength,nDec },oBrw:nRowPos),;
cFieldName :="" ,oBrw:refresh(), oGet:refresh() )
@ 274, 407 BUTTON oBtnedit PROMPT "Edit" OF oDlg ;
ACTION ( aFields[oBrw:nRowPos]:= { cFieldName,cType,nLength,nDec } , ;
cFieldName :="" ,oBrw:refresh(), oGet:refresh() )
oBtnEdit:disable()
@ 242, 407 BUTTON "Up" OF oDlg ACTION SetFieldUp(@aFields,obrw:nRowPos )
@ 210, 407 BUTTON "Down" OF oDlg ACTION SetFieldDown(@aFields,obrw:nRowPos )
@ 178, 407 BUTTON "Del" OF oDlg ACTION ( aDel( aFields, obrw:nRowPos ),ASize(aFields,Len(aFields)-1),oBrw:Refresh() )
@ 48, 20 SAY "DBF filename:" OF oDlg SIZE 92, 17
@ 46, 110 GET oGetName VAR cFileName OF oDlg SIZE 275, 22 ;
VALID ( IF( !Empty(cFileName) .and. len(afields)>0 , oBtnCreate:Enable(),oBtnCreate:disable() ) , .t. )
@ 20, 73 SAY "RDD:" OF oDlg SIZE 108, 17
@ 16, 110 COMBOBOX cRDD OF oDlg ;
SIZE 94, 25 ITEMS { "DBFNTX", "DBFCDX" }
@ 101, 407 BUTTON "Code Gen." OF oDlg ACTION (msginfo(DbfGen( aFields )))
@ 71, 407 BUTTON "Import" OF oDlg ACTION ( aStruct:= importDbf(), if ( !Empty(aStruct), ( afields:= aStruct,oBrw:SetArray( aFields ) ) ,),oBrw:refresh() )
@ 41, 407 BUTTON oBtnCreate PROMPT "Create" OF oDlg ;
ACTION ( msginfo("crea dbf") , DBCREATE( cFileName, aFields ,cRdd ) )
oBtnCreate:disable()
ACTIVATE DIALOG oDlg
return nil
//----------------------------------------------------------------------------//
function SetFieldUp( aFields, nIndex )
local BakaField
if nIndex > 1
BakaField = aFields[ nIndex - 1 ]
aFields[ nIndex - 1 ] = aFields[ nIndex ]
aFields[ nIndex ] = BakaField
endif
return nil
//----------------------------------------------------------------------------//
function SetFieldDown( aFields, nIndex )
local BakaField
if nIndex < Len( aFields )
BakaField = aFields[ nIndex + 1 ]
aFields[ nIndex + 1 ] = aFields[ nIndex ]
aFields[ nIndex ] = BakaField
endif
return nil
//----------------------------------------------------------------------------//
function importDbf()
local cFile := ChooseFile( "Select dbf file:", "Dbf" )
local calias, aStruct
if Upper( SubStr( cFile, Len( cFile ) - 3, 4 ) ) == ".DBF"
USE ( cFile ) NEW
cAlias = Alias()
aStruct = ( cAlias )->( DbStruct() )
close( cAlias )
MsgInfo( Len( aStruct ) )
else
MsgInfo( Upper( SubStr( cFile, Len( cFile ) - 3, 4 ) ) )
MsgInfo( "Invalid file type" )
endif
return aStruct
//----------------------------------------------------------------------------//
function btnAddField( aFields, aField, nAt )
DEFAULT nAt := Len( aFields )
if Valtype( aField[ 3 ] ) == "C"
aField[ 3 ] = Val( aField[ 3 ] )
endif
if Valtype( aField[ 4 ] ) == "C"
aField[ 4 ] = Val( aField[ 4 ] )
endif
if Len( aFields ) == 0
nAt = Len( aFields )
endif
if nAt == Len( aFields)
AAdd( aFields, aField )
else
ASize( aFields, Len( aFields ) + 1 )
AIns( aFields, nAt + 1 )
aFields[ nAt + 1 ] = aField
endif
return nil
//----------------------------------------------------------------------------//
static function DbfGen( aFields )
local cPrg := "local aFields := "
local aInfo := aFields
local n, cTempName
for n = 1 to Len( aInfo )
cPrg += If( n == 1, "{ ", space(18) ) + '{ "' + ;
aInfo[ n ][ 1 ] + '", "' + ;
aInfo[ n ][ 2 ] + '", ' + ;
AllTrim( Str( aInfo[ n ][ 3 ] ) ) + ", " + ;
AllTrim( Str( aInfo[ n ][ 4 ] ) ) + "}" + ;
If( n < Len( aInfo ), ",;", " } " ) + CRLF
next
return cPrg
//----------------------------------------------------------------------------//
function Open()
local cDbfName := ChooseFile( "Please select a DBF file", "dbf" )
local oWnd, oBar, oBrw, oSay
if ! File( cDbfName )
return nil
endif
if File( cFileNoExt( cDbfName ) + ".cdx" )
USE ( cDbfName ) VIA "DBFCDX" NEW
else
USE ( cDbfName ) NEW
endif
DEFINE WINDOW oWnd TITLE cDbfName ;
FROM 213, 109 TO 650, 820
DEFINE TOOLBAR oBar OF oWnd
DEFINE BUTTON OF oBar PROMPT "Add" IMAGE ImgPath() + "add.png" ;
ACTION ( oBrw:GoBottom(), ( oBrw:cAlias )->( DbAppend() ) )
DEFINE BUTTON OF oBar PROMPT "Edit" IMAGE ImgPath() + "edit.png" ;
ACTION RecEdit( oBrw )
DEFINE BUTTON OF oBar PROMPT "Del" IMAGE ImgPath() + "minus.png" ;
ACTION If( MsgYesNo( "Delete this record ?" ),;
( ( oBrw:cAlias )->( DbGoTo( oBrw:nRowPos ) ),;
( oBrw:cAlias )->( DbDelete() ), ( oBrw:cAlias )->( DbPack() ),;
oBrw:Refresh() ),)
DEFINE BUTTON OF oBar PROMPT "First" IMAGE ImgPath() + "first.png" ;
ACTION oBrw:GoTop()
DEFINE BUTTON OF oBar PROMPT "Last" IMAGE ImgPath() + "last.png" ;
ACTION oBrw:GoBottom()
DEFINE BUTTON OF oBar PROMPT "Structure" IMAGE ImgPath() + "preferences.png" ;
ACTION ShowStruct( oBrw:cAlias )
DEFINE BUTTON OF oBar PROMPT "Exit" IMAGE ImgPath() + "exit2.png" ;
ACTION oWnd:End()
@ 35, 20 BROWSE oBrw OF oWnd SIZE 672, 393 ALIAS Alias()
// oBrw:bHeadClick:= { | obj , nindex| if(nindex== 1, msginfo("clickada cabecera"+str(nindex)),) }
oBrw:SetColor( CLR_BLACK, CLR_GRAY )
oBrw:GoTop()
oBrw:bChanged = { || oSay:SetText( "Alias: " + oBrw:cAlias + ;
" Record: " + AllTrim( Str( oBrw:nRowPos ) ) + " / " + ;
AllTrim( Str( ( oBrw:cAlias )->( RecCount() ) ) ) ) }
DEFINE MSGBAR OF oWnd
@ 0, 10 SAY oSay PROMPT "Alias: " + oBrw:cAlias + ;
" Record: 1 / " + AllTrim( Str( ( oBrw:cAlias )->( RecCount() ) ) ) ;
OF oWnd SIZE 300, 20 RAISED
ACTIVATE WINDOW oWnd ;
VALID ( ( oBrw:cAlias )->( DbCloseArea() ), .T. )
return nil
//----------------------------------------------------------------------------//
function ShowStruct( cAlias )
local oDlg, oBrw, aInfo := ( cAlias )->( DbStruct() )
DEFINE DIALOG oDlg TITLE "Dbf " + cAlias + " fields"
oDlg:SetSize( 430, 395 )
@ 33, 10 BROWSE oBrw ;
FIELDS "", "", "", "" ;
HEADERS "Name", "Type", "Len", "Dec" ;
OF oDlg SIZE 410, 327
oBrw:SetArray( aInfo )
oBrw:bLine = { | nRow | { aInfo[ nRow ][ 1 ], aInfo[ nRow ][ 2 ],;
Str( aInfo[ nRow ][ 3 ] ), Str( aInfo[ nRow ][ 4 ] ) } }
oBrw:SetColor( CLR_BLACK, CLR_GRAY )
DEFINE MSGBAR OF oDlg
@ 0, 10 SAY "Total fields: " + AllTrim( Str( ( cAlias )->( FCount() ) ) ) ;
OF oDlg SIZE 300, 20 RAISED
ACTIVATE DIALOG oDlg CENTERED
return nil
//----------------------------------------------------------------------------//
function RecEdit( oBrw )
local oWnd, oBar, oSayRec, nRecNo
local aRecord, cAlias := oBrw:cAlias
( cAlias )->( DbGoto( oBrw:nRowPos ) )
nRecNo = ( cAlias )->( RecNo() )
aRecord = ( cAlias )->( GetRecord() )
DEFINE WINDOW oWnd TITLE "Edit: " + oBrw:cAlias SIZE 600, 500
DEFINE TOOLBAR oBar OF oWnd
DEFINE BUTTON OF oBar PROMPT "Prev" IMAGE ImgPath() + "first.png" ;
ACTION ( If( nRecNo > 1, ( cAlias )->( DbGoTo( --nRecNo ) ),),;
aRecord := ( cAlias )->( GetRecord() ), oBrw:Refresh(),;
oSayRec:SetText( ( cAlias )->( GetRecNo() ) ) )
DEFINE BUTTON OF oBar PROMPT "Next" IMAGE ImgPath() + "last.png" ;
ACTION ( If( nRecNo < ( cAlias )->( RecCount() ), ( cAlias )->( DbGoTo( ++nRecNo ) ),),;
aRecord := ( cAlias )->( GetRecord() ), oBrw:Refresh(),;
oSayRec:SetText( ( cAlias )->( GetRecNo() ) ) )
DEFINE BUTTON OF oBar PROMPT "Exit" IMAGE ImgPath() + "exit2.png" ;
ACTION oWnd:End()
@ 33, 10 BROWSE oBrw ;
FIELDS "", "" ;
HEADERS "Field", "Value" ;
OF oWnd SIZE 579, 436
oBrw:SetColEditable( 1, .F. )
oBrw:SetColWidth( 2, 454 )
oBrw:SetArray( aRecord )
oBrw:bLine = { | nRow | { aRecord[ nRow ][ 1 ], aRecord[ nRow ][ 2 ] } }
oBrw:SetColor( CLR_BLACK, CLR_GRAY )
oBrw:GoTop()
DEFINE MSGBAR OF oWnd
@ 0, 10 SAY oSayRec PROMPT ( cAlias )->( GetRecNo() ) ;
OF oWnd SIZE 300, 20 RAISED
ACTIVATE WINDOW oWnd CENTERED
return nil
//----------------------------------------------------------------------------//
function GetRecNo()
return "Record: " + AllTrim( Str( RecNo() ) ) + " / " + ;
AllTrim( Str( RecCount() ) )
//----------------------------------------------------------------------------//
function GetRecord()
local aRecord := {}, n
for n = 1 to ( Alias() )->( FCount() )
AAdd( aRecord, { ( Alias() )->( FieldName( n ) ),;
cValToChar( ( Alias() )->( FieldGet( n ) ) ) } )
next
return aRecord
//----------------------------------------------------------------------------//