View RSS Feed

Development Team Blog

How to Display other Image Formats in the cCJGrid

Rate this Entry
Visual DataFlex supports bitmaps and icons in the standard controls. If you have other graphic format files (like TIFF, GIF, JPEG, PNG etc) you need to use either an ActiveX object, a DLL that does it for you or use the Graphics Library free available for download. For the CodeJock Report Control based Grids in Visual DataFlex 16.0 and higher it is a different case. This blog tells you how to use the popular graphics formats in the grid.

psImage
When you want to display an image in for example the grid column header you set the psImage property of the cCJGridColumn object like:
Code:
Object oYellowCards is a cCJGridColumn
    Set piWidth to 30
    Set psCaption to "Cards"
    Set psImage to "yellowcard.bmp"
End_Object
The cCJGridColumn object does not load the image specified itself, it will tell the cCJGrid to do this by sending AddImage to the cCJGrid object. The method in the cCJGrid class looks at the file extension and when this is "bmp" the file is assumed to be a bitmap file. If it is not "bmp" the file is supposed to be an ICOn file. As with the standard DataFlex support for graphics the product supports icons and bitmaps only.

So how to change
Code:
Set psImage to "yellowcard.bmp"
into:
Code:
Set psImage to "yellowcard.gif"
The trick is to tell the AddImage to do the reverse of what it is doing now. This means that when the extension is ICO it is an ICOn and else it is a bitmap. Yes, right, CodeJock supports "all" other formats via the bitmap loading function. So we augment the AddImage function and reverse the action.

The original is:
Code:
Function AddImage String sImage Integer iId Returns Integer
    Boolean bIsIcon
    Move (Pos(".bmp", Lowercase(sImage)) = 0) to bIsIcon
    Get AddImageType bIsIcon sImage iId to iId
    Function_Return iId
End_Procedure  // AddImage
and it needs to be changed into:
Code:
Function AddImage String sImage Integer iId Returns Integer
    Boolean bIsIcon
    Move (Right (Lowercase(sImage), 4) = '.ico') to bIsIcon
    Get AddImageType bIsIcon sImage iId to iId
    Function_Return iId
End_Procedure  // AddImage
The above code is not in the packages because AddImage is also used in other classes like the cCJCommandBarSystem, the cImageList and cImageList32 classes and we want to have more consistent code in the product.

So This is All?
Yes and No. If you want to load a different graphics format with psImage the answer is yes. But I like to go further and optimized the loading of images a bit by first checking if the image loaded before. The following code uses two array properties to store the already loaded images. If a "new" image is being loaded the code first checks if the image is already in the array and if so the image id of that image is used instead of a new load attempt.

Code:
{ DesignTime = False }
Property String[] psImages
{ DesignTime = False }
Property Integer[] piImageIds

Set peAnchors to anAll

Function AddImage String sImage Integer iId Returns Integer
    Boolean bIsIcon
    String[] sImages
    Integer[] iImageIds
    Integer iElement
    
    Get psImages to sImages
    Move (SearchArray (sImage, sImages)) to iElement
    If (iElement = -1) Begin
        Move (Right (Lowercase(sImage), 4) = '.ico') to bIsIcon
        Move (SizeOfArray (sImages)) to iElement
        Move sImage to sImages[iElement]
        Set psImages to sImages
        
        Get AddImageType bIsIcon sImage iId to iId
        Get piImageIds to iImageIds
        Move iId to iImageIds[iElement]
        Set piImageIds to iImageIds
    End
    Else Begin
        Get piImageIds to iImageIds
        Move iImageIds[iElement] to iId
    End
    
    Function_Return iId
End_Procedure
The above code also bypasses the AddImage function which means you do not need to augment, override this function.

Updated 1-Sep-2011 at 11:55 AM by Vincent Oorsprong

Categories
Uncategorized

Comments