Recent

Author Topic: Beginner trying to understand TActionList  (Read 706 times)

gustavo

  • Newbie
  • Posts: 2
Beginner trying to understand TActionList
« on: November 11, 2024, 08:19:18 am »
Hello,

I was cheking @zoltanleo 's example project https://forum.lazarus.freepascal.org/index.php/topic,52350.msg385815.html#msg385815

Didn't understand why there was a TActionList with TAction's in the unit1.lfm. I tested removing them completely and it didn't affect the program execution.

Was there a reason to have them there in this particular project?

Thank you,
Gustavo

Thaddy

  • Hero Member
  • *****
  • Posts: 16184
  • Censorship about opinions does not belong here.
Re: Beginner trying to understand TActionList
« Reply #1 on: November 11, 2024, 09:10:10 am »
I suppose he has the same habit as I have: when you start a GUI, you start with TActionlist and TActions.
You do not need to use them, but you will learn that in very complex GUI's it is much easier to work and you in fact decouple from user interface elements. Maybe he thought it was overkill for the simple example.
If I smell bad code it usually is bad code and that includes my own code.

VisualLab

  • Hero Member
  • *****
  • Posts: 573
Re: Beginner trying to understand TActionList
« Reply #2 on: November 11, 2024, 02:21:16 pm »
The Action List helps maintain order and consistency in the program's GUI during the design phase. Let's say you want to have:
  • a main menu,
  • toolbars (one or more),
  • different context menus that are launched in different places.
The main menu (TMainMenu) usually has main sections (e.g. File, Edit, View, etc.). Each of these sections contains a list of menu items of type TMenuItem. Each of these menu items can have (for example):
  • Caption (visible after the menu is displayed),
  • Checked (the command can act as a Checkbox),
  • GroupIndex (commands with the same numeric value create a set of switches acting as RadioButtons),
  • Hint (a hint that can be displayed on the status bar),
  • ImageIndex (the icon number from the image list connected to the TMainMenu object),
  • RadioItem (the command can act as a RadioButton),
  • ShortCut (the keyboard shortcut that starts this command),
  • Visible.
In order for the TMenuItem command to trigger the execution of an action, you need to write an OnClick event handler. Context menus (TPopupMenu) are similar. Each of them has a list of items of type TMenuItem.

It's similar with toolbars (TToolBar). Each bar has a collection of buttons (TToolButton). Buttons also have similar properties to those listed above for TMenuItem (not all of them). To trigger an action after clicking a button, you also need to write an OnClick event handler.

The main menu, context menu, and toolbars can display icons (glyphs) next to their names. An image list (TImageList) is used for this purpose. When an image list is assigned to the main menu or toolbar, an image can be assigned to each menu item or button using its ImageIndex property.

Instead of programming the main menu, shortcut menus, and toolbar items separately, you can do this:
  • place the TActionList on the form,
  • create a list of actions using the action list editor,
  • write OnExecute event handlers for each action,
  • assign the list of images (TImageList) to the action list,
  • set the image number (icon) for each action,
  • finally assign individual actions to positions in the main menu and/or toolbars or context menus.
Menu items (TMenuItem) and toolbar buttons (TToolButton) have an Action property. This is used to assign an action to a menu item or button. Assigning an action to a menu item or toolbar button automatically assigns the appropriate properties (Caption, Hint, ImageIndex, etc.). This allows you to write event handlers once that perform specific operations in the program. I have been using this solution since it appeared in Delphi 4.
« Last Edit: November 11, 2024, 02:26:03 pm by VisualLab »

gustavo

  • Newbie
  • Posts: 2
Re: Beginner trying to understand TActionList
« Reply #3 on: November 11, 2024, 10:32:08 pm »
Thanks a lot for the very useful replies! ;-)

Thaddy

  • Hero Member
  • *****
  • Posts: 16184
  • Censorship about opinions does not belong here.
Re: Beginner trying to understand TActionList
« Reply #4 on: November 12, 2024, 01:04:36 pm »
VisualLab did an amazing job in explaining what I meant in detail. Suggest to add this to the wiki, because it is actually better.
Actionlists makes you almost automatically use good GUI design practices.
« Last Edit: November 12, 2024, 01:35:27 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

VisualLab

  • Hero Member
  • *****
  • Posts: 573
Re: Beginner trying to understand TActionList
« Reply #5 on: November 12, 2024, 05:04:48 pm »
Suggest to add this to the wiki, because it is actually better.
Actionlists makes you almost automatically use good GUI design practices.

This is too general a description. It could be at most a seed for developing a proper article (or tutorial). It would be a bit of a shame to post such limited content. In particular, it would be necessary to supplement this content with screenshots from Lazarus, showing the process of creating menus and toolbars in the application using TActionList. The article would have to be of comparable quality to those prepared by the authors of other articles. If it is posted as it is now, then soon there will be a newbie complaining that the description of using TActionList is too sparse or not very understandable. And he will probably be right. So, if someone is willing to prepare such an article (or tutorial), they can safely use this description.

wp

  • Hero Member
  • *****
  • Posts: 12461
Re: Beginner trying to understand TActionList
« Reply #6 on: November 12, 2024, 05:35:39 pm »
Brian Long, a long time ago, published an excellent article about Actions on his site: http://blong.com/Articles/Actions/Actions.htm. Although being for Delphi it applies to Lazarus as well, except for the chapter about "ActionManager and Action Bands" which do not exist in Lazarus.

 

TinyPortal © 2005-2018