XBrowse has too many settings and all of them can be handled through parameters to a function. If we try to, the function may be too clumsy. Instead use most commonly varying variables as parameters and rest of them through a call back codeblock.
In any case, the most important parameters are the column specifications. They are (1) Column name or an expression or codeblock, (2) Header, (3) Picture (4) Width, (5) Sort order and (6) Alignment. There are quite a few other specifications for the columns, which we can handle through call back codeblock.
In most cases, we do not need to specify all the specifications for the columns. When we speicify column name, XBrowse itself decides all other specifications which in most cases are suitable or even better than what we may specify. But there are cases like when we speicify expressions or codeblocks we need to provide other specifications too.
I suggest making a multi-dimensional array of all the specifications that are necessary. Such an array makes the specifications clear to understand and easier to maintain. Also we may notice later that the same specification would apply without any change for RDD, RecordSets and TDatabase classes. Example you can see in the program given below.
I have chosen the CUSTOMER.DBF in the \fwh\samples folder for this purpose.
- Code: Select all Expand view
- #include "FiveWin.Ch"
#include "ord.ch"
#include "xbrowse.ch"
REQUEST DBFCDX
//----------------------------------------------------------------------------//
function Main()
local aCols
USE CUSTOMER NEW ALIAS CUST SHARED
/*
* Set up all info for the all the columns in this multi-dimentional array
* Each row contains parameters for each column
* Each row or array can contain 1 to 6 columns ( elements )
* Columns 2 to 6 are optional
* Col 1: Column name or Expression or CodeBlock
* Col 2: Header
* Col 3: Picture
* Col 4: Column width in pixels
* col 5: Sort Order
* Col 6: Alignment
*
* Specify what are essential. Better we leave as much as possible to defults of xbrowse
*/
aCols := { ;
{ "PADR( FIRST - ( '-' + LAST ), 60 )", "Customer Name", , 200 }, ;
{ "City" }, ;
{ "(DATE()-HIREDATE)/365", "Service", "999" }, ;
{ "Salary" } ;
}
MyXBrowse( nil, nil, "My Title", "CUST", aCols, nil ) // see below for syntax
return (0)
//----------------------------------------------------------------------------//
init procedure PrgInit
SET DATE ITALIAN
SET CENTURY ON
SET TIME FORMAT TO "HH:MM:SS"
SET EPOCH TO YEAR(DATE())-50
SET DELETED ON
SET EXCLUSIVE OFF
RDDSETDEFAULT( "DBFCDX" )
XbrNumFormat( 'I', .t. )
SetGetColorFocus()
return
//----------------------------------------------------------------------------//
function MyXBrowse( oWnd, aRect, cTitle, uData, aCols, bSetUp )
local oBrw
local lOwnWnd := .f.
DEFAULT aRect := { 0, 0, 0, 0 }, ;
cTitle := "XBROWSE", ;
uData := Alias()
aCols := ASize( ArrTranspose( aCols ), 6 )
if oWnd == nil
DEFINE WINDOW oWnd TITLE cTitle
lOwnWnd := .t.
endif
@ aRect[ 1 ], aRect[ 2 ] XBROWSE oBrw SIZE aRect[ 3 ], aRect[ 4 ] PIXEL OF oWnd ;
COLUMNS aCols[ 1 ] HEADERS aCols[ 2 ] PICTURES aCols[ 3 ] ;
COLSIZES aCols[ 4 ] SORT aCols[ 5 ] JUSTIFY aCols[ 6 ] ;
AUTOSORT DATASOURCE uData ;
CELL LINES
if bSetUp != nil
Eval( bSetUp, oBrw )
endif
if lOwnWnd
oBrw:CreateFromCode()
if aRect[ 1 ] == 0 .and. aRect[ 2 ] == 0 .and. aRect[ 3 ] == 0 .and. aRect[ 4 ] == 0
oWnd:oClient := oBrw
endif
ACTIVATE WINDOW oWnd MAXIMIZED
endif
return oBrw
Please compile and test this program in the \fwh\samples folder.
I see that there is a prevalent opinion that it is not possible to make a paramtertrized xbrowse function without using the syntax like oBrw:AddCol() and some programmers have even argued with me. The above sample proves that this opinion is not correct. And I still maintain that approach is not the way to get the best out of xbrowse.
This function could be the starting point for development of your own custom parametrized function. After you test we can discuss how to proceed further to implement it for your taste and needs.