Introduction​

Xbase++ is a development tool that offers Xbase Parts for building Windows Desktop applications. One feature that Xbase++ programmers can leverage is the ability to configure these Xbase Parts through subclassing. Not only does this approach allow for personalized coloring and UI behavior for each individual instance, but it also simplifies behavior modifications by centralizing them in one place. In this article, we will explore how Xbase Parts can be configured by subclasses and demonstrate how to implement identical behavior using a subclass of XbpStatic.

Configuring Xbase Parts with Individual Instances​

To illustrate the configuration of Xbase Parts with individual instances, let's consider an example using the XbpStatic class. The following code snippet demonstrates how to configure an XbpStatic instance by customizing its coloring and UI behavior to become a label on an XbpDialog instance:
Xbase++:
   ...
   oXbp := XbpStatic():new( oDrawingArea, , {276,216}, {84,24}, { { XBP_PP_BGCLR, GRA_CLR_GREEN }, { XBP_PP_FGCLR, GRA_CLR_BLUE } } )
   oXbp:caption := "Text"
   oXbp:clipSiblings := .T.
   oXbp:options := XBPSTATIC_TEXT_VCENTER+XBPSTATIC_TEXT_RIGHT
   oXbp:create()
   ...
In this example, we create an XbpStatic instance in the variable oXbp and specify its attributes such as position, size, and color. We also set its caption, enable sibling clipping, and configure the text alignment options. This approach allows you to customize each instance of XbpStatic individually. However, the major drawback of this approach is that changing a single attribute (for example, the background color) entails an immense effort to change every XbpStatic instance in your code.

Behavior Modifications through Subclassing​

Instead of configuring each instance individually, we would instead like to just adjust the position and size in our code. Everything else is to be taken over by a derived class MyLabel:

Xbase++:
   ...
   oXbp := MyLabel():new( oDrawingArea, , {276,216}, {84,24} )
   oXbp:caption := "Text"
   oXbp:create()
   ...
We can achieve this by subclassing from XbpStatic. Let's take a look at the source code that implements the wanted behavior using a subclass:
Xbase++:
CLASS MyLabel FROM XbpStatic
   EXPORTED:
      METHOD init
      METHOD create
ENDCLASS

METHOD MyLabel:init( oP, oO, oPos, aSize, aPP, lVisible )
   aPP := ChangePresParam( aPP, { XBP_PP_BGCLR, GRA_CLR_GREEN } )
   aPP := ChangePresParam( aPP, { XBP_PP_FGCLR, GRA_CLR_BLUE } )
   SUPER:init( oP, oO, oPos, aSize, aPP, lVisible )
RETURN SELF

METHOD MyLabel:create()
   ::clipSiblings := .T.
   ::options := XBPSTATIC_TEXT_VCENTER+XBPSTATIC_TEXT_RIGHT
   SUPER
RETURN SELF
In this code snippet, we define a new class called MyLabel which is a subclass of XbpStatic. Within the :init() method of our subclass, we modify the Presentation Parameters passed by the aPP parameter to ensure a blue font on a green background. We then invoke the parent class's :init() method using SUPER:Init(...) to ensure proper initialization. In the method :create() we configure the clipping and provide the desired alignment of the text before we call the base class implementation with the statement SUPER.

To manipulate the array of presentation parameters we use the auxiliary function ChangePresParam:
Xbase++:
FUNCTION ChangePresParam( aPP, aNew )
   LOCAL nPos

   aPP := Coalesce( aPP, Array(0) )
   nPos := AScan( aPP, {|e| e[1] == aNew[1] } )

   IF nPos > 0
      aPP[nPos,2] := aNew[2]
      RETURN aPP
   ENDIF

   AAdd( aPP, aNew )

RETURN aPP

Conclusion​

Configuring Xbase Parts through subclasses provides a flexible and efficient way to personalize the coloring and UI behavior of individual instances. By centralizing behavior modifications, developers can easily manage and update the code. Additionally, subclassing promotes code reusability, enabling the creation of multiple instances with identical behavior. With Xbase++, you have the power to create dynamic and customized Windows Desktop applications.