Action, the class and the property

Post your Gambas programming questions here.
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Action, the class and the property

Post by sjsepan »

This thread is a follow-on to my discussion with cogier in the Components forum about the meaning and purpose of Action.
Initially I was baffled by the purpose of the property Action of type String in the controls. I've since seen the Action class defined in the wiki as 'This class acts like a read-only static array.' and discovered that it can control properties of multiple controls, such as their Enabled property.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Action, the class and the property

Post by cogier »

Have a look at the attached.
Action.tar.gz
(14.68 KiB) Downloaded 404 times
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Re: Action, the class and the property

Post by sjsepan »

Thanks! It's a handy way to affect groups of controls.
I've started using it to act on paired controls like a given toolbar-button + menu-item. In this case, the click event handlers of each call a sub containing this code, and the code disables those controls while something is happening, and also turns on/off the progress bar in 'marquee' mode (sorry, .Net term, 'pulse' in Gambas).

...    
    StartProgressBarWithPicture("Printing...", Null, Action["FilePrint"].Picture, True, 33)
    Action["FilePrint"].Enabled = False
...
Finally
    Action["FilePrint"].Enabled = True
    StopProgressBar(sStatusMessage)
...
Updated: per tip below (Thanks cogier)
Last edited by sjsepan on Sunday 27th October 2019 1:51pm, edited 1 time in total.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Action, the class and the property

Post by cogier »

Tip - Use the 'gb' button for adding Gambas code to the forum it looks a lot better.
...    
    StartProgressBarWithPicture("Printing...", Null, Action["FilePrint"].Picture, True, 33)
    Action["FilePrint"].Enabled = False
...
Finally
    Action["FilePrint"].Enabled = True
    StopProgressBar(sStatusMessage)
...
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Re: Action, the class and the property

Post by sjsepan »

Does anyone have any working examples of how Action["action_name"].Raise and Action_Activate are used?

Steve
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Action, the class and the property

Post by cogier »

What are Sunday afternoons if not for but looking at Gambas code. I have learnt a bit here.
Action_Activate
This is a used as a procedure and if a button has an 'Action' value then this procedure is triggered. 'sValue' is the Action text:-
Public Sub Action_Activate(sValue As String) As String

End
I'm not sure why 'As String' is needed but it does not work without it.
Action["action_name"].Raise
I didn't have much luck with this but Action.Raise(Control) may be what you need. This will trigger the procedure above passing it the Action text of the control.

As in all good TV programs, here is an example I prepared earlier!
Test1.tar.gz
(14.62 KiB) Downloaded 344 times
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Re: Action, the class and the property

Post by sjsepan »

I'll have to look that over, Thanks! :)
This is what I came up with too...
Public Function Action_Activate(sActionName As String) As Boolean
    'Note: I am not sure I see the benefit of using this dispatch mechanism over direct click events (I like the other aspects of Action a lot though).
    'Maybe if the actions were *directly executed* by the controls *for me* I'd be more sold on the idea.
    'Having to wire up the dispatching manually seems brittle. But it does work!

    Select sActionName
        Case "FileNew"
            FileNew
        Case "FileOpen"
            FileOpen
'...
       Case "HelpContents"
            HelpContents
        Case Else
            Print Subst("Action_Activate: &1", sActionName)
    End Select
    Return False

End
 
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Re: Action, the class and the property

Post by sjsepan »

Note in my above example, I did not need to call the Raise method myself, as that is done behind the scenes by the control that is clicked.
My initial dislike of having to write a dispatcher in the Activate event is balanced somewhat by realizing that I would still have to write event handlers if the controls firing an Action ultimately caused it to call some code directly. (I was hoping that some method would be called directly by action name, but that might not be workable.)

I was playing with Lazarus Pascal this week for the first time in a long while and saw that objects of type TAction would fire their defined OnExecute event when the referencing control (menu, button) was clicked. But that jsut ended up being a whole bunch of separate event handlers too. Don't know if that's better than a single dispatcher or not.
In both languages, I like the idea of a single Action tied to one or more controls, and managing multiple properties in those controls (Caption, Tooltip, Picture, Enabled, Shortcut, etc.)
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Action, the class and the property

Post by cogier »

Note in my above example, I did not need to call the Raise method myself, as that is done behind the scenes by the control that is clicked.
I included it in my example as it shows how you can trigger the routine for a separate purpose.
I was hoping that some method would be called directly by action name, but that might not be workable.
If you use Action.Raise(Control) it will trigger the routine sending it the control's Action text, which is basically the same thing, or am I missing something?
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Re: Action, the class and the property

Post by sjsepan »

cogier wrote: Sunday 17th November 2019 4:28pm
Note in my above example, I did not need to call the Raise method myself, as that is done behind the scenes by the control that is clicked.
I included it in my example as it shows how you can trigger the routine for a separate purpose.
I was hoping that some method would be called directly by action name, but that might not be workable.
If you use Action.Raise(Control) it will trigger the routine sending it the control's Action text, which is basically the same thing, or am I missing something?

Yes, I appreciate that, so we can see a working example of that in use.

As for the Raise in the context of a button/menu click, I get the impression that Raise fires the action but the button/menu fires that Raise method for us. My point is really about when the action is fired, it does not run a routine specific to the action (like maybe a SomeActionName_Activate), but a general one (Action_Activate). And as for me questioning whether there should be one or the other, its still better that having to code separate click events (button_Click, menu_Click) to call the same code, so I'm not really complaining, just working through the differences out loud between how Gambas and Lazarus do Action objects.
So far I am liking Gambas a lot more than Lazarus, even though the latter is not bad.
Post Reply