Friday, March 24, 2006

Advanced Xailer Techniques: X classes and T classes

[Original post by José F. Gímenez]

Many of you know that Xailer's class hierarchy is extensive and it also has the most of the classes duplicated in "X" classes and "T" classes, basically the class hierarchy is something like this:

XComponent -> TComponent -> XWinObject -> TWinObject ->
XControl -> TControl -> etc.

In the class hierarchy published in the help file this duality is not shown, you will only see the "T" classes, but the "X" classes also exist and they are the ones that hold all the code, so the "T" classes remain absoluty empty. For example, the TControl class is declared like this:

CLASS TControl FROM XControl
ENDCLASS

Now what ?, why do we want to have duplicated classes ?, what's the goal with this ?

If you don't know how to take advantage of this feature, this can be useless. But, as you will see as follows, it has enormous benefits.

In the past, I used to develop my windows applications with a well-known xBase library that you may already known and use. You also know that under this well-known-library if you want to correct a bug, modify certain behave of a control or perform any other modification, you must to work modifiying the original class source code, and then include the modified source in your list of .PRG files, or compile the code into an OBJ, and place it inside the original LIB o inside any other library, all a mess. At the end I had a library of patches as big as the original lib, and whenever a new release of the original library came, I was scared.

Using X classes and T classes you don't have to worry. Let's suppose that the SetSel() method of the TEdit class has a bug, to correct it, we just have to add the correction in any module of our application (or in a library of our own):

CLASS TEdit FROM XEdit
METHOD SetSel( nStart, nEnd ) INLINE ;
::SendMsg( EM_SETSEL, nStart, nEnd )
ENDCLASS

That's all. If you notice, we just simply had declared a new class TEdit inherited from XEdit, and in this new class we had overwritten SetSel() method with a new corrected version. We don't need the original source of the class, either with any new release, nor we have a lot of stand alone modules with corrections.

This feature can be taken far away, not only for bug correction. Now let's suppose we want that all the Edit controls of our app were shown in yellow background when catching the focus, of course you can modify the nClrPaneFocus property of every single Edit control (a damn ammount of job). But it's easier to apply this techinique this way:

CLASS TEdit FROM XEdit
PROPERTY nClrPaneFocus INIT RGB( 255, 255, 192 )
ENDCLASS

Also, as TMaskEdit, TEditBtn, TDateEdit, TDBEdit, TDBMaskEdit, etc. are inherited directly or indirectly from TEdit, all these controls will have the very same behave.

Now let's think we want to create a demo of our application, limited by dates, that cannot be greater than 31-12-05, this code snipet can do the job:
CLASS TMaskEdit FROM XMaskEdit
METHOD Valid( oNextCtl )
ENDCLASS

METHOD Valid( oNextCtl ) CLASS TMaskEdit
LOCAL uRet := Super:Valid( oNextCtl )
IF ::cType == "D" .AND. Day( ::Value ) > 0
IF DTOS( ::Value ) > "20052131"
MsgStop( "Date not allowed in demo version." )
RETURN .F.
ENDIF
ENDIF
RETURN uRet

Well these are just some samples of this technique, but posibilities are enourmous and they are only limited by your imagination.

0 Comments:

Post a Comment

<< Home