Notifyflag param of GuiButton function

The Syntax is:GuiButton( “Text”, id, x, y, width , height , notifyflags )`

So the Notifyflags param has below possilble values:

notifyflags - decides which events fire execution of your formula, it can be any combination of values below
1 - clicked (button)
2 - setfocus (all)
4 - killfocus (all)
8 - hit enter/return key (edit)
16 - edit change (edit)

Refer to this page: http://www.amibroker.com/devlog/wp-content/uploads/2017/03/readme6210.html

It has an example usage of `GuiButton( “Custom button”, 1, 10, 40, 100, 30, 7 );

But question is, why the Notifyflags param value has been passed as “7” in this example usage and not one from the pre-defined list of 1,2,4,8 or 16?

7 is the sum of 1+2+4. In this case any of the events from 1 to 4 will trigger execution of the formula.

notifyflags - decides which events fire execution of your formula, it can be any combination of values below

1 - clicked (button)
2 - setfocus (all)
4 - killfocus (all)
8 - hit enter/return key (edit)
16 - edit change (edit)

If you want to trigger execution of the formula when a button is clicked, you can (for example) do it this way:

GuiButton( "Custom button", 1, 10, 40, 100, 30, 1 );
GuiSetColors( 1, 1, 0 ); // default (system) look

id = GuiGetEvent( 0, 0 );
event = GuiGetEvent( 0, 1 );

if  ( id == 1 && event == 1 )
{
//.... your code
}
2 Likes

Very well explained. Thanks @Milosz

Can I ask one more question. In Notifyflags :

2 - setfocus (all)
4 - killfocus (all)

What these two are and what are the potential practical applications of these two way of triggering a button?

Hello

@Milosz I show your floating window with so many GuiButton on it. Very nice work.

I just wondering, did you made any myGuiFunction(with,parameters,here) to call all these buttons, and to handle theme with better way?
Thank you

@PanoS if you are asking about this implementation: AmiBroker - much more than just ordinary technical analysis software , it was’t necessary for me to create any “myGuiFunctions”. It may be beneficial in some cases, but not in this one. In the code which displays the buttons (floating window) I do almost everything n times - where n is the number of displayed rows. I use only one loop for that and don’t need any other loops or functions. Take a look at the example code:

id = GuiGetEvent( 0, 0 );
event = GuiGetEvent( 0, 1 );
nRows = 30;  // number of rows and buttons to be displayed

for( i = 1; i < nRows + 1; i++ )
{
    GuiButton( StaticVarGetText( "Symbol" + i ), i, x, y, width, height, 1 );

    if( id == i && event == 1 ) ShellExecute( StaticVarGetText( "Link" + i ), "", "" );
}

In this way - (in only two lines of code) I can easily import all the necessary string variables from another code (an Exploration which extracts information from a web page) to display a selected button, check its state and if this button is clicked, open a new tab in my default web browser with the coressponding content. This repeats n-times to display the rest of the buttons. Notice, that only these two lines of “Gui” code are inside a loop. The whole code is very light and effective.

@Neil81 Because I want some parts of my code to be executed each time a button is clicked, I use only flag number 1. Flag number 2 and 4 allow to execute a code conditionally - for example only when you click a button and after that transfer focus to some other button or edit window (click some other button or edit window) or the other way - first click some other control and then click a button. When you use flag 2 or flag 4, the code will not be (or might not be) executed each time you click a button.

Hope it helps.

4 Likes

@Milosz

I am using your code, but getting error which i am not able to figure out why do they show up?

Neil, GuiButton functions were introduced in AmiBroker 6.21
http://www.amibroker.com/devlog/wp-content/uploads/2017/03/readme6210.html

You must be using some older version.

1 Like

Mine is 6.20.1 - Thanks for pointing it out. Updating now.

In my post with replies to @PanoS and @Neil81 I noticed a small mistake in the code. There should be:

for( i = 1; i < nRows + 1; i++ ) Sorry for that.

Moderator comment: it is now corrected in the original code.

Thank you @milosz that loop was nice idea for your case
Now I just wondering, if someone he like to share any GuiButton Snippet in here?
I try to make one ParamToggle, but I think with the way i am try to code is conflict the ID or something else that I cannot see .
Do I have to change the notifyflags or all this function is better to bin it
Thank you

// Attention for beginers this is experimental AFL code and is NOT Correct
event = GuiGetEvent( 0, 1 );

function GuiParamToggle( Title, id, x, y, width, height, notifyflag )
{
    global  Onoff;
    GuiButton( Title, ID, x, y, width, height, notifyflag );

    Onoff = Nz( StaticVarGet( "GuiOnOff" + id ), 0 );

    if( id == ID && event == 1 )
    {
        if( Onoff == 1 )  { StaticVarSet( "GuiOnOff" + id, 0 );  Say( "zero" ); }

        if( Onoff == 0 )  { StaticVarSet( "GuiOnOff" + id, 1 );  Say( "one" ); }

        //RequestMouseMoveRefresh();
        RequestTimedRefresh( 0.1 );
    }
}


GuiParamToggle( "TEST 1", 10, Status( "pxwidth" ) - 80 , 80, 80, 24, 1 );
GuiParamToggle( "TEST 2", 12, Status( "pxwidth" ) - 80 , 100, 80, 24, 1 );

if( Onoff == 1 ) GfxTextOut( "ParamToggle is ON", 50, 100 );

1 Like

Just a word of caution: for GUI work use RequestMouseMoveRefresh() instead of timed refresh. RequestMouseMoveRefresh() is way more efficient and resource-friendly than constant timed refresh.

1 Like

@PanoS,

Here is one solution to your problem.

function GuiParamToggle( text, id, x, y, width, height, notifyflag )
{
    /// @link http://forum.amibroker.com/t/notifyflag-param-of-guibutton-function/721/13
    GuiButton( text, ID, x, y, width, height, notifyflag );
	event = GuiGetEvent( 0, 1 );   
    
    gcx  = Nz( GetCursorXPosition(1) );
	gcy  = Nz( GetCursorYPosition(1) );
    
    mouseonbutton = gcx > x && gcy > y && gcx < x + width && gcy < y + height;  	
    
    VarSet( "ONOFF" + id, Nz( StaticVarGet( "GuiOnOff" + id ), 0 ) );
    
    if( id == ID && event == 1 && mouseonbutton )
    {
        ONOFF = VarGet( "ONOFF" + id );
        
        if( ONOFF == 1 )  { StaticVarSet( "GuiOnOff" + id, 0 );  Say( "OFF" ); }
        if( ONOFF == 0 )  { StaticVarSet( "GuiOnOff" + id, 1 );  Say( "ON" ); }

        RequestMouseMoveRefresh();
    }
}


GuiParamToggle( "TEST 1", 10, Status( "pxwidth" ) - 80 , 80, 80, 24, 1 );
GuiParamToggle( "TEST 2", 12, Status( "pxwidth" ) - 80 , 100, 80, 24, 1 );

if( ONOFF10 == 1 ) GfxTextOut( "ParamToggle of Test 1 is ON", 50, 100 );

if( ONOFF12 == 1 ) GfxTextOut( "ParamToggle of Test 2 is ON", 50, 120 );
1 Like

Thank you @ fxshrat

As you said this is one solution and works perfect.
I really like to see the other solution;
I mean something new for me.
Something that don’t remind me GFX version… LOL

Fxshrat, was really fast reply thank you, and thank you again. Do not listen what I am saying.
Sometimes is to Greek for you. :slight_smile:
xxx

i am thinking again and again.
thanks to @fxshrat he gave me food for thought

the other solutiion maybe is here, i will try it in a minute.
lets say if (text_cliked) do something

below code is from the manual

// read all pending events
for( i = 0; GuiGetEvent( i, 0 ); i++ )
{
 id   = GuiGetEvent( i, 0 );
 code = GuiGetEvent( i, 1 );
 text = GuiGetEvent( i, 2 );
 
 printf("\n Id\t" +id + "\nCode \t" +code + "\nText \t" +text);
} 

@PanoS

I didn’t have much time to play with your and @fxshrat’s code, but I think we don’t need to check the mouse position and can get rid of those three lines, which (as you wrote) remind you of GFX version :wink: In my version of fxshrat’s code idButton variable, identifies which button was clicked. It’s a simpler solution:

function GuiParamToggle( text, id, x, y, width, height, notifyflag )
{
    /// @link http://forum.amibroker.com/t/notifyflag-param-of-guibutton-function/721/13
    GuiButton( text, ID, x, y, width, height, notifyflag );
	event = GuiGetEvent( 0, 1 );  
	idButton = GuiGetEvent( 0, 0 );  	
    VarSet( "ONOFF" + id, Nz( StaticVarGet( "GuiOnOff" + id ), 0 ) );
     
    if( idButton == ID && event == 1)
    {
        ONOFF = VarGet( "ONOFF" + id );
        
        if( ONOFF == 1 )  { StaticVarSet( "GuiOnOff" + id, 0 );  Say( "OFF" ); }
        if( ONOFF == 0 )  { StaticVarSet( "GuiOnOff" + id, 1 );  Say( "ON" ); }
        
		RequestMouseMoveRefresh();
    }
}

GuiParamToggle( "TEST 1", 10, Status( "pxwidth" ) - 80 , 80, 80, 24, 1 );
GuiParamToggle( "TEST 2", 12, Status( "pxwidth" ) - 80 , 100, 80, 24, 1 );

if( ONOFF10 == 1 ) GfxTextOut( "ParamToggle of Test 1 is ON", 50, 100 );
if( ONOFF12 == 1 ) GfxTextOut( "ParamToggle of Test 2 is ON", 50, 120 );
2 Likes

Yeah, id == ID in my upper code modification is bullshit of course (it’s always returning true). It’s a remainder of @PanoS code. I didn’t check @PanoS original code carefully (that’s why it is free after all). I just did a quick test and since it was not working properly I added mouseonbutton variable which would not be required with proper replacement which is to be seen below (In my own codes I actually do use GuiGetEvent( 0, 0 ) == someID but that mindf*** of upper/lower case equality check of id == ID in @PanoS’s code made my brain going to sleep like Greek wine apparently. LOL) :

procedure GuiParamToggle( text, id, x, y, width, height, notifyflag ) {
    /// @link http://forum.amibroker.com/t/notifyflag-param-of-guibutton-function/721/17
    // Example:
    // GuiParamToggle( "TEST 1", 10, 0, 0, 80, 24, 1 );
    // if( ONOFF10 == 1 ) GfxTextOut( "ParamToggle of Test 1 is ON", 50, 100 );

    local event, staticname, getstaticvar, ONOFF;

    GuiButton( text, idset, x, y, width, height, notifyflag );
    id = GuiGetEvent( 0, 0 );
    event = GuiGetEvent( 0, 1 );

    staticname = StrFormat( "GuiParamToggle_%s_%g_%g", GetDatabaseName(), GetChartID(), idset );
    getstaticvar = StaticVarGet( staticname );

    VarSet( "ONOFF" + idset, Nz( getstaticvar, 0 ) );
   
    if( id == idset && event == 1 ) {
        ONOFF = VarGet( "ONOFF" + idset );
        
        if( ONOFF == 1 )  { StaticVarSet( staticname, 0 );  Say( "OFF" ); }
        if( ONOFF == 0 )  { StaticVarSet( staticname, 1 );  Say( "ON" ); }

        RequestMouseMoveRefresh();
    }
}
1 Like

VarSet/VarGet is not required in updated code also. It’s a further remainder of mouseonbutton example.
Function should be OK now…
So I’m out of this thread now…

function GuiParamToggle( text, idset, x, y, width, height, notifyflag ) {
    /// @link http://forum.amibroker.com/t/notifyflag-param-of-guibutton-function/721/18
    // Example:
    // ONOFF1 = GuiParamToggle( "TEST 1", 10, 0, 0, 80, 24, 1 );
    // if( ONOFF1 == 1 ) GfxTextOut( "ParamToggle of Test 1 is ON", 50, 100 );

    local event, id, staticname, ONOFF;

    GuiButton( text, idset, x, y, width, height, notifyflag );
    id = GuiGetEvent( 0, 0 );
    event = GuiGetEvent( 0, 1 );

    staticname = StrFormat( "GuiParamToggle_%s_%g_%g", GetDatabaseName(), GetChartID(), idset );
    ONOFF = Nz( StaticVarGet( staticname ), 0 );
   
    if( id == idset && event == 1 ) {  
        if( ONOFF == 1 )  { StaticVarSet( staticname, 0 );  Say( "OFF" ); }
        if( ONOFF == 0 )  { StaticVarSet( staticname, 1 );  Say( "ON" ); }

        RequestMouseMoveRefresh();
    }
    return ONOFF;
}

ONOFF1 = GuiParamToggle( "TEST 1", 10, Status( "pxchartwidth" ) - 80, 80, 80, 24, 1 );
ONOFF2 = GuiParamToggle( "TEST 2", 12, Status( "pxchartwidth" ) - 80, 100, 80, 24, 1 );

if( ONOFF1 == 1 ) GfxTextOut( "ParamToggle of Test 1 is ON", 50, 100 );

if( ONOFF2 == 1 ) GfxTextOut( "ParamToggle of Test 2 is ON", 50, 120 );
2 Likes

NOOOOO… Stay here i will open another bottle now

thanks

You guys left…. i have to drink on my own now :frowning:

We forgot the Toggle second (Yes/No) parameter
i just add it here

function GuiParamToggle( Text1, Text2, idset, x, y, width, height, notifyflag )
{	
    ///by Milosz, Panos fxshrat @link http://forum.amibroker.com/t/notifyflag-param-of-guibutton-function/721/20
    GuiButton( Text1, idset, x, y, width, height, notifyflag );
	ID = GuiGetEvent( 0, 0 );  	event = GuiGetEvent( 0, 1 ); 
 
	StaticName	="GuiOnOff"+GetChartID()+ idset;
    VarSet( "ONOFF" + idset, Nz( StaticVarGet( StaticName ), 0 ) );
    ONOFF = VarGet( "ONOFF" + idset );
    if( ID == idset && event == 1)
    {
        if( ONOFF == 1 )  { StaticVarSet( StaticName, 0 );  Say( "OFF" ); }
        if( ONOFF == 0 )  { StaticVarSet( StaticName, 1 );  Say( "ON" ); }
		RequestMouseMoveRefresh();
    }
    	if( ONOFF == 0 ) GuiSetText( Text2, idset );
}


GuiParamToggle( "Test1 ON","Test1 OFF", 10, Status( "pxwidth" ) - 80 , 80, 80, 24, 1 );
GuiParamToggle( "Test2 ON","Test2 OFF", 12, Status( "pxwidth" ) - 80 , 100, 80, 24, 1 );
if( ONOFF10 == 1 ) GfxTextOut( "ParamToggle of Test 1 is ON", 50, 100 );
if( ONOFF12 == 1 ) GfxTextOut( "ParamToggle of Test 2 is ON", 50, 120 );
1 Like