Вы находитесь на странице: 1из 6

23/1/2010 AmiBroker Users’ Knowledge Base » Butt…

Users' Knowledge Base


Share your experience, code and everything with other AmiBroker
Users’.

March 9, 2008

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:

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

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.

1 #include <ControlPanelInclude-003.afl>
2
3 global ColNumber;
4 RequestTimedRefresh(1);
5 ButtonHeight = Param("Button Height",20,5,200,1);
6 ButtonWidth = Param("Button Width",120,5,200,1);
7 PanelYoffset = Param("Button Row Offset (px)",10,0,Status("pxheight"),1);
8 PanelXoffset = Param("Button Column Offset (px)",10,0,Status("pxwidth"),1);
9 FontRatio = Param("Font: ButtonHeight ratio",2,1,20,0.1);
10
11 ButtonColumnBegin( "1" );
12 ButtonHeader( "COLUMN HEADER1", colorBlue, colorLightBlue,colorWhite);
13 ButtonText( "AUTO-TRADING ON", colorBlue, colorWhite);
14 Reset = ButtonTrigger( "START SESSION", colorBrightGreen, colorRed, colorBlack);
15 CancelAll = ButtonTrigger( "CANCEL ALL", colorBrightGreen, colorRed, colorBlack);
16 CloseAll = ButtonTrigger( "CLOSE ALL", colorBrightGreen, colorRed, colorBlack);

amibroker.org/…/button-header-collapse… 1/6
23/1/2010 AmiBroker Users’ Knowledge Base » Butt…
17 EndSession = ButtonTrigger( "END SESSION", colorBrightGreen, colorRed, colorBlack);
18 ButtonColumnEnd( );
19
20 ClickCoordinates = Nz(StaticVarGet("ClickCoordinates"));
21 switch( ClickCoordinates )
22 {
23 case 101:
24 Say( "1 1");
25 break;
26 case 102:
27 Say( "1 2");
28 break;
29 case 103:
30 Say( "1 3");
31 break;
32 case 104:
33 Say( "1 4");
34 break;
35 case 105:
36 Say( "1 5");
37 break;
38 case 106:
39 Say( "1 6");
40 break;
41 }
42
43 Plot(C,"",1,128);
44
45 Title = "\n"+
46 " Click Coordinates: "+ClickCoordinates+"\n"+
47 "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:

1 function ButtonHeader( Label, backColor1, BackColor2, TextColor)


2 {
3 global ColNumber, RowNumber, ColExpanded, Colname;
4 RowNumber = Nz(kStaticVarGet("RowNumber"+ColName))+1;
5 kStaticVarSet("RowNumber"+ColName, RowNumber);
6 Trigger = GetMouseClick( ColNumber, RowNumber );
7 if( Trigger )
8 {
9 BackColor = backColor2;
10 ColExpanded = Nz(kStaticVarGet(ColName+"ColExpanded"));
11 if( ColExpanded ) kStaticVarSet(ColName+"ColExpanded", False);
12 else kStaticVarSet(ColName+"ColExpanded", True);
13 }
14 else BackColor = backColor1;
15 ColExpanded = Nz(kStaticVarGet(ColName+"ColExpanded"));
16 kStaticVarSetText("TextButton"+ColName+RowNumber, Label);
17 kStaticVarSet("TextColor"+ColName+RowNumber, TextColor);
18 kStaticVarSet("BackColor"+ColName+RowNumber, backColor);
19 }

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.

1 function ButtonTrigger( Label, backColor1, BackColor2, TextColor)


2 {
3 global ColNumber, RowNumber, ColName;
4 ColExpanded = Nz(kStaticVarGet(ColName+"ColExpanded"));
5 if( ColExpanded )
6 {
7 ColName = VarGetText("ColName");
8 RowNumber = Nz(kStaticVarGet("RowNumber"+ColName))+1;
9 kStaticVarSet("RowNumber"+ColName, RowNumber);
10 Trigger = GetMouseClick( ColNumber, RowNumber );
11 if( Trigger ) BackColor = backColor2; else BackColor = backColor1;
12 kStaticVarSetText("TextButton"+ColName+RowNumber, Label);
13 kStaticVarSet("TextColor"+ColName+RowNumber, TextColor);
14 kStaticVarSet("BackColor"+ColName+RowNumber, backColor);
15 }
16 else Trigger = 0;

amibroker.org/…/button-header-collapse… 2/6
23/1/2010 AmiBroker Users’ Knowledge Base » Butt…
17 return Trigger;
18 }

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.

1 // ControlPanelInclude-003.afl
2 procedure kStaticVarSet( SName, SValue )
3 {
4 ChartID = GetChartID();
5 InIndicator = Status("Action") == 1;
6 if( InIndicator ) StaticVarSet(Sname+ChartID, Svalue);
7 }
8
9 function kStaticVarGet( SName )
10 {
11 ChartID = GetChartID();
12 Var = StaticVarGet(Sname+ChartID);
13 return Var;
14 }
15
16 procedure kStaticVarSetText( SName, SValue )
17 {
18 ChartID = GetChartID();
19 InIndicator = Status("Action") == 1;
20 if( InIndicator ) StaticVarSetText(Sname+ChartID, Svalue);
21 }
22
23 function kStaticVarGetText( SName )
24 {
25 ChartID = GetChartID();
26 return StaticVarGetText(Sname+ChartID);
27 }
28
29 function NewColumn()
30 {
31 VarSet("ColNumber", 0);
32 }
33
34 function GetMouseClick( ColNumber, RowNumber )
35 {
36 global PanelYoffset, PanelXoffset, ButtonHeight, ButtonWidth;
37 LButtonDown = GetCursorMouseButtons() == 9;
38 Click = 0;
39 if( LButtonDown )
40 {
41 ULButtonX = PanelXoffset + (ColNumber-1) * ButtonWidth;
42 LRButtonX = ULButtonX + ButtonWidth;
43 ULButtonY = (RowNumber -1) * ButtonHeight + PanelYoffset;
44 LRButtonY = ULButtonY + ButtonHeight;
45 MouseCoord = Nz(StaticVarGet("ClickCoordinates"));
46 if( MouseCoord == 0 AND LButtonDown )
47 {
48 MousePx = GetCursorXPosition( 1 );
49 MousePy = GetCursorYPosition( 1 );
50 if( MousePx > ULButtonX AND MousePx < LRButtonX AND MousePy > ULButtonY AND MousePy < LRButtonY )
51 {
52 StaticVarSet("ClickCoordinates",ColNumber*100+RowNumber);
53 Click = 1;
54 }
55 }
56 }
57 return Click;
58 }
59
60 function ButtonColumnBegin( ColName )
61 {
62 global FontRatio, ColName, ColNumber, ButtonHeight, ButtonWidth, PanelXoffset, PanelYoffset, Colname;
63 ColNumber = VarGet("ColNumber");
64 if( IsEmpty( ColNumber ) )
65 {
66 VarSet("ColNumber",1);
67 StaticVarSet("ClickCoordinates",0);
68 }
69 else VarSet("ColNumber", ++ColNumber);
70 ColName = ColName+GetChartID();
71 kStaticVarSet("RowNumber"+ColName, 0);
72 VarSetText("ColName",ColName);
73 GfxSetOverlayMode( 0 );
74 GfxSelectFont( "Tahoma", ButtonHeight/FontRatio, 800 );
75 GfxSelectPen( colorBlack );

amibroker.org/…/button-header-collapse… 3/6
23/1/2010 AmiBroker Users’ Knowledge Base » Butt…
76 GfxSetBkMode( 1 );
77 }
78
79 function ButtonText( TextButton, backColor, TextColor)
80 {
81 global ColNumber, RowNumber, Colname;
82 ColExpanded = Nz(kStaticVarGet(ColName+"ColExpanded"));
83 if( ColExpanded )
84 {
85 ColName = VarGetText("ColName");
86 RowNumber = Nz(kStaticVarGet("RowNumber"+ColName))+1;
87 kStaticVarSet("RowNumber"+ColName, RowNumber);
88 kStaticVarSetText("TextButton"+ColName+RowNumber, TextButton);
89 kStaticVarSet("TextColor"+ColName+RowNumber, TextColor);
90 kStaticVarSet("BackColor"+ColName+RowNumber, backColor);
91 }
92 }
93
94 function ButtonTrigger( Label, backColor1, BackColor2, TextColor)
95 {
96 global ColNumber, RowNumber, ColName;
97 ColExpanded = Nz(kStaticVarGet(ColName+"ColExpanded"));
98 if( ColExpanded )
99 {
100 ColName = VarGetText("ColName");
101 RowNumber = Nz(kStaticVarGet("RowNumber"+ColName))+1;
102 kStaticVarSet("RowNumber"+ColName, RowNumber);
103 Trigger = GetMouseClick( ColNumber, RowNumber );
104 if( Trigger ) BackColor = backColor2; else BackColor = backColor1;
105 kStaticVarSetText("TextButton"+ColName+RowNumber, Label);
106 kStaticVarSet("TextColor"+ColName+RowNumber, TextColor);
107 kStaticVarSet("BackColor"+ColName+RowNumber, backColor);
108 }
109 else Trigger = 0;
110 return Trigger;
111 }
112
113 function ButtonHeader( Label, backColor1, BackColor2, TextColor)
114 {
115 global ColNumber, RowNumber, ColExpanded, Colname;
116 RowNumber = Nz(kStaticVarGet("RowNumber"+ColName))+1;
117 kStaticVarSet("RowNumber"+ColName, RowNumber);
118 Trigger = GetMouseClick( ColNumber, RowNumber );
119 if( Trigger )
120 {
121 BackColor = backColor2;
122 ColExpanded = Nz(kStaticVarGet(ColName+"ColExpanded"));
123 if( ColExpanded ) kStaticVarSet(ColName+"ColExpanded", False);
124 else kStaticVarSet(ColName+"ColExpanded", True);
125 }
126 else BackColor = backColor1;
127 ColExpanded = Nz(kStaticVarGet(ColName+"ColExpanded"));
128 kStaticVarSetText("TextButton"+ColName+RowNumber, Label);
129 kStaticVarSet("TextColor"+ColName+RowNumber, TextColor);
130 kStaticVarSet("BackColor"+ColName+RowNumber, backColor);
131 }
132
133 function ButtonColumnEnd()
134 {
135 global ButtonHeight, ButtonWidth, PanelYoffset, PanelXoffset, ColNumber, RowNumber, ColName;
136 ChartIDStr = NumToStr(GetChartID(),1.0,False);
137 ULButtonX = PanelXoffset + (ColNumber-1) * ButtonWidth;
138 LRButtonX = ULButtonX + ButtonWidth;
139 for( Row = 1; Row <= RowNumber; Row++ )
140 {
141 ULButtonY = (Row-1) * ButtonHeight + PanelYoffset;
142 LRButtonY = ULButtonY + ButtonHeight;
143 Label = kStaticVarGetText("TextButton"+ColName+Row);
144 TextColor = Nz(kStaticVarGet("TextColor"+ColName+Row));
145 BackColor = Nz(kStaticVarGet("BackColor"+ColName+Row));
146 GfxSelectSolidBrush( BackColor);
147 GfxRectangle( ULButtonX, ULButtonY, LRButtonX, LRButtonY );
148 GfxSetBkColor( BackColor);
149 GfxSetTextColor( TextColor );
150 GfxDrawText( Label, ULButtonX, ULButtonY, LRButtonX, LRButtonY, 32 | 1 | 4);
151 }
152 }

Filed by Herman at 12:52 pm under Real-Time Control-Panels

(3 votes, average: 5 out of 5)

amibroker.org/…/button-header-collapse… 4/6
23/1/2010 AmiBroker Users’ Knowledge Base » Butt…
Loading ...

No comments yet. Be the first.


Leave a reply
name (required)

email (will not be shown) (required)

website

Submit Comment

Search

Info
Table Of Contents
Contributor Agreement
About
Author Guide
Contacts
FAQ
Terms of Use

Recent Posts
Plotting trades on chart
Date and Time to Number Conversions
Popup Window: Preventing pile-ups
Wordpress upgrade
US-Stocks Database (v2)

Recent Comments
Herman on High-Precision Delay and Interval Timing
Tomasz Janeczko on High-Precision Delay and Interval Timing
Tomasz Janeczko on Static Variables in RT Systems
Yofa on Real-Time Bar Period Timing
Anthony Abry on Introduction to Trading Systems - Management

Categories
Select Category

Archives
February 2009 (2)
August 2008 (1)
April 2008 (1)
March 2008 (20)

amibroker.org/…/button-header-collapse… 5/6
23/1/2010 AmiBroker Users’ Knowledge Base » Butt…
February 2008 (6)
January 2008 (1)
December 2007 (4)
November 2007 (18)
October 2007 (17)
September 2007 (17)
August 2007 (26)
July 2007 (20)
June 2007 (17)
May 2007 (8)
April 2007 (16)
January 2007 (1)

Meta
Log in
Entries RSS
Comments RSS
WordPress.org

Copyright (C)2006 AmiBroker.com.


This site uses WordPress Page generated in 0.520 seconds.

amibroker.org/…/button-header-collapse… 6/6

Вам также может понравиться