Button Header (Collapse/Expand)

Note: A number of variables and function names were changed to provide more consistent naming; please upgrade your code and Include file. I regret that such changes may happen rather often, however, the only alternative would be to complete all posts for this topic and publish them all at once. Since there is no guarantee that all code will ever be fully completed (most of my development work never stops), I think it is better to publish whatever is functional, as if this were a development blog.

The purpose of the Button Header is to name button columns (this is needed to key static variables) and provide a Collapse/Expand function for Button Columns. Clicking on a Button Header will alternatively Collapse and Expand button columns. This allows you to quickly expose chart sections that were hidden by the Control Panel. A collapsed Button Column will look like this:

clip_image002

Clicking on the Header Button will expand the column to look as show below:

clip_image002[5]

A few variables are displayed in the Title to facilitate debugging. The following listing shows the test code used to display the above Button Column. Note again that there are two ways to process button clicks: using the values returned by the button functions, or using the Switch() statement.

#include <ControlPanelInclude-003.afl>

global ColNumber;
RequestTimedRefresh(1);
ButtonHeight Param("Button Height",20,5,200,1); 
ButtonWidth Param("Button Width",120,5,200,1); 
PanelYoffset Param("Button Row Offset (px)",10,0,Status("pxheight"),1); 
PanelXoffset Param("Button Column Offset (px)",10,0,Status("pxwidth"),1); 
FontRatio Param("Font: ButtonHeight ratio",2,1,20,0.1);

ButtonColumnBegin"1" );
ButtonHeader"COLUMN HEADER1"colorBluecolorLightBlue,colorWhite);
ButtonText"AUTO-TRADING ON"colorBluecolorWhite);
Reset ButtonTrigger"START SESSION"colorBrightGreencolorRedcolorBlack);
CancelAll ButtonTrigger"CANCEL ALL"colorBrightGreencolorRedcolorBlack);
CloseAll ButtonTrigger"CLOSE ALL"colorBrightGreencolorRedcolorBlack);
EndSession ButtonTrigger"END SESSION"colorBrightGreencolorRedcolorBlack);
ButtonColumnEnd( );

ClickCoordinates Nz(StaticVarGet("ClickCoordinates"));
switch( ClickCoordinates )
    {
    case 101:
    Say"1 1");
    break;
    case 102:
    Say"1 2");
    break;
    case 103:
    Say"1 3");
    break;
    case 104:
    Say"1 4");
    break;
    case 105:
    Say"1 5");
    break;
    case 106:
    Say"1 6");
    break;
    }

Plot(C,"",1,128);

Title "\n"+
"  Click Coordinates: "+ClickCoordinates+"\n"+
"Column Expanded Var: "+Nz(kStaticVarGet(ColName+"ColExpanded"));

The ButtonHeader() is similar to the ButtonText() function but has a Collapse/Expand variable added. Here is the code for the new ButtonHeader() function:

function ButtonHeaderLabelbackColor1BackColor2TextColor)
    {
    global ColNumberRowNumberColExpandedColname;
    RowNumber Nz(kStaticVarGet("RowNumber"+ColName))+1;
    kStaticVarSet("RowNumber"+ColNameRowNumber);
    Trigger GetMouseClickColNumberRowNumber );
    if( Trigger ) 
        {
        BackColor backColor2; 
        ColExpanded Nz(kStaticVarGet(ColName+"ColExpanded"));
        if( ColExpanded kStaticVarSet(ColName+"ColExpanded"False);
        else kStaticVarSet(ColName+"ColExpanded"True);
        }
    else BackColor backColor1;
    ColExpanded Nz(kStaticVarGet(ColName+"ColExpanded"));
    kStaticVarSetText("TextButton"+ColName+RowNumberLabel);
    kStaticVarSet("TextColor"+ColName+RowNumberTextColor);
    kStaticVarSet("BackColor"+ColName+RowNumberbackColor);
    }

In the above code, when the left Mouse button is clicked, a static variable named ColExpanded is toggled between True and False. All earlier button functions have been modified to only execute their internal code only if this variable is True. This way Buttons will only display if the variable ColExpanded it True. The listing below shows how the TriggerButton() was modified. This and the HeaderButton functions are located in the Include file at the end of this post; there is no need to copy them separately.

function ButtonTriggerLabelbackColor1BackColor2TextColor)
    {
    global ColNumberRowNumberColName;
    ColExpanded Nz(kStaticVarGet(ColName+"ColExpanded"));
    if( ColExpanded )
        {
        ColName VarGetText("ColName");
        RowNumber Nz(kStaticVarGet("RowNumber"+ColName))+1;
        kStaticVarSet("RowNumber"+ColNameRowNumber);
        Trigger GetMouseClickColNumberRowNumber );
        if( Trigger BackColor backColor2; else BackColor backColor1;
        kStaticVarSetText("TextButton"+ColName+RowNumberLabel);
        kStaticVarSet("TextColor"+ColName+RowNumberTextColor);
        kStaticVarSet("BackColor"+ColName+RowNumberbackColor);
        }
    else Trigger 0;
    return Trigger;
    }  

The new ControlPanelInclude-003.afl Include file is listed below, it must be copied to your default Include folder for the above code to work.

// ControlPanelInclude-003.afl
procedure kStaticVarSetSNameSValue )         
    {
    ChartID GetChartID();
    InIndicator Status("Action") == 1;
    if( InIndicator StaticVarSet(Sname+ChartIDSvalue); 
    }

function kStaticVarGetSName )                     
    { 
    ChartID     GetChartID();
    Var = StaticVarGet(Sname+ChartID);
    return Var;
    }

procedure kStaticVarSetTextSNameSValue )     
    { 
    ChartID     GetChartID();
    InIndicator Status("Action") == 1;
    if( InIndicator StaticVarSetText(Sname+ChartIDSvalue); 
    }

function kStaticVarGetTextSName )                 
    { 
    ChartID GetChartID();
    return StaticVarGetText(Sname+ChartID); 
    }

function NewColumn()
    {
    VarSet("ColNumber"0);
    }

function GetMouseClickColNumberRowNumber )
    {
    global PanelYoffsetPanelXoffsetButtonHeightButtonWidth;
    LButtonDown GetCursorMouseButtons() == 9;
    Click 0;
    if( LButtonDown )
        {
        ULButtonX         PanelXoffset + (ColNumber-1) * ButtonWidth;
        LRButtonX        ULButtonX ButtonWidth;
        ULButtonY         = (RowNumber -1) * ButtonHeight PanelYoffset;
        LRButtonY        ULButtonY ButtonHeight;
        MouseCoord Nz(StaticVarGet("ClickCoordinates"));
        if( MouseCoord == AND LButtonDown )
            {
            MousePx GetCursorXPosition);
            MousePy GetCursorYPosition);
            if( MousePx ULButtonX AND MousePx LRButtonX AND MousePy ULButtonY AND MousePy LRButtonY )
                {
                StaticVarSet("ClickCoordinates",ColNumber*100+RowNumber);
                Click 1;
                }
            }
        }
    return Click;
    }

function ButtonColumnBeginColName ) 
    {
    global FontRatioColNameColNumberButtonHeightButtonWidthPanelXoffsetPanelYoffsetColname;
    ColNumber VarGet("ColNumber");
    if( IsEmptyColNumber ) ) 
        {
        VarSet("ColNumber",1);
        StaticVarSet("ClickCoordinates",0);
        }
    else VarSet("ColNumber", ++ColNumber);
    ColName ColName+GetChartID();
    kStaticVarSet("RowNumber"+ColName0);
    VarSetText("ColName",ColName);
    GfxSetOverlayMode);
    GfxSelectFont"Tahoma"ButtonHeight/FontRatio800 ); 
    GfxSelectPencolorBlack ); 
    GfxSetBkMode);
    }

function ButtonTextTextButtonbackColorTextColor)
    {
    global ColNumberRowNumberColname;
    ColExpanded Nz(kStaticVarGet(ColName+"ColExpanded"));
    if( ColExpanded )
        {
        ColName VarGetText("ColName");
        RowNumber Nz(kStaticVarGet("RowNumber"+ColName))+1;
        kStaticVarSet("RowNumber"+ColNameRowNumber);
        kStaticVarSetText("TextButton"+ColName+RowNumberTextButton);
        kStaticVarSet("TextColor"+ColName+RowNumberTextColor);
        kStaticVarSet("BackColor"+ColName+RowNumberbackColor);
        }
    }

function ButtonTriggerLabelbackColor1BackColor2TextColor)
    {
    global ColNumberRowNumberColName;
    ColExpanded Nz(kStaticVarGet(ColName+"ColExpanded"));
    if( ColExpanded )
        {
        ColName VarGetText("ColName");
        RowNumber Nz(kStaticVarGet("RowNumber"+ColName))+1;
        kStaticVarSet("RowNumber"+ColNameRowNumber);
        Trigger GetMouseClickColNumberRowNumber );
        if( Trigger BackColor backColor2; else BackColor backColor1;
        kStaticVarSetText("TextButton"+ColName+RowNumberLabel);
        kStaticVarSet("TextColor"+ColName+RowNumberTextColor);
        kStaticVarSet("BackColor"+ColName+RowNumberbackColor);
        }
    else Trigger 0;
    return Trigger;
    }

function ButtonHeaderLabelbackColor1BackColor2TextColor)
    {
    global ColNumberRowNumberColExpandedColname;
    RowNumber Nz(kStaticVarGet("RowNumber"+ColName))+1;
    kStaticVarSet("RowNumber"+ColNameRowNumber);
    Trigger GetMouseClickColNumberRowNumber );
    if( Trigger ) 
        {
        BackColor backColor2; 
        ColExpanded Nz(kStaticVarGet(ColName+"ColExpanded"));
        if( ColExpanded kStaticVarSet(ColName+"ColExpanded"False);
        else kStaticVarSet(ColName+"ColExpanded"True);
        }
    else BackColor backColor1;
    ColExpanded Nz(kStaticVarGet(ColName+"ColExpanded"));
    kStaticVarSetText("TextButton"+ColName+RowNumberLabel);
    kStaticVarSet("TextColor"+ColName+RowNumberTextColor);
    kStaticVarSet("BackColor"+ColName+RowNumberbackColor);
    }

function ButtonColumnEnd()
    {
    global ButtonHeightButtonWidthPanelYoffsetPanelXoffsetColNumberRowNumberColName;
    ChartIDStr     NumToStr(GetChartID(),1.0,False);
    ULButtonX         PanelXoffset + (ColNumber-1) * ButtonWidth;
    LRButtonX        ULButtonX ButtonWidth;
    for( Row 1Row <= RowNumberRow++ ) 
        {
        ULButtonY         = (Row-1) * ButtonHeight PanelYoffset;
        LRButtonY        ULButtonY ButtonHeight;
        Label     kStaticVarGetText("TextButton"+ColName+Row);
        TextColor     Nz(kStaticVarGet("TextColor"+ColName+Row));
        BackColor     Nz(kStaticVarGet("BackColor"+ColName+Row));
        GfxSelectSolidBrushBackColor);
        GfxRectangleULButtonXULButtonYLRButtonXLRButtonY ); 
        GfxSetBkColorBackColor);
        GfxSetTextColorTextColor );
        GfxDrawTextLabelULButtonXULButtonYLRButtonXLRButtonY32 4);
        }
    }

Comments are closed.