Dynamically assigning button images

Date:18 January 2005
Product/Release:Visual LANSA
Abstract:Method for dynamically assigning images to buttons
Submitted By:LANSA Technical Support
Last Review:November 2010

Method for dynamically assigning images to buttons.

To do this, create a non-visual reusable part, which will be referred to as the image librarian. In this reusable part, define all of the available images in the repository in a collection of bitmap objects keyed by their names:

FUNCTION OPTIONS(*DIRECT)
BEGIN_COM ROLE(*EXTENDS #PRIM_OBJT)

* Collection of Bitmaps - the 'library'
DEFINE_COM CLASS(#prim_kcol<#Prim_bmp #Std_Obj>) NAME(#Bitmaps)

* Property to retrieve a bitmap
Define_Pty Bitmap Get(Get_Bitmap)
PtyRoutine Get_Bitmap
Define_Map *output #Prim_bmp #Pty_002 pass(*By_reference)
Define_Map *input #Std_Obj #Pty_001

* Set the Reference
Set_Ref #Pty_002 #Bitmaps<#Pty_001.Value>
Endroutine

EVTROUTINE HANDLING(#COM_OWNER.CreateInstance) OPTIONS(*NOCLEARMESSAGES *NOCLEARERRORS)
* Hardcode the images in here
Set_ref #Bitmaps<'VB_OPEN'> #VB_OPEN
Set_ref #Bitmaps<'VB_CLOSE'> #VB_CLOSE
Set_ref #Bitmaps<'VB_CD'> #VB_CD
* Etc...
ENDROUTINE
END_COM

This creates a library with 3 bitmaps. The property routine uses the standard input/output parameters to pass back a reference to the bitmap when given a string input.

Then in the button reusable part, include this librarian with a define_com; allowing access to the images. Note that the scope parameter has been used to define this object as a shared component. This means that many different forms and reusable parts can reference this object but only one instance of the object is loaded into memory:

FUNCTION OPTIONS(*DIRECT)
BEGIN_COM ROLE(*EXTENDS #PRIM_SPBN) DISPLAYPOSITION(1) HEIGHT(18) LEFT(0) TABPOSITION(1) TOP(0) WIDTH(58)

* Define an Image Library in this Component - note the use of SCOPE
DEFINE_COM CLASS(#ImgLib) NAME(#ImgLib) SCOPE(*SHARED)
define_pty name(uImage) set(SetuImage)
ptyroutine name(SetuImage)
define_map for(*input) class(#std_obj) name(#InputName)

* Now retrieve the image from the image library and assign it to the button
set #com_owner image(#ImgLib.Bitmap<#InputName>)
endroutine
END_COM

Note: This method is not as obvious and technically not completely dynamic. This same technique is used in the Visual LANSA Framework when assigning images to buttons.