Thursday, May 5, 2011

Dynamic menus/actions in Delphi

I have an application with a submenu of places each with unique shortcut. Until now this submenu is static but often there is a need to add a new place to the menu and assign a new shortcut. The application have a unit Clientdata that contains an TActionManager that holds the needed actions for buttons, menus and also the submenu I mentioned.

So I want this submenu to be changable in runtime. All places is persisted in the database. I think the submenu should still have actions in the actionmanager.

Beside that there is a shortcut-editor for all actions, so each user can have its own personal shortcuts. Those shortcuts are saved in a textfile in personal file directory.

There are 3 places that should be changed.

  • System module. Here I can add, edit and delete places in database that will be in the submenu.
  • Actionmanager. It holds all actions for the submenu. It also holds shortcuts and can load/save shortcuts from a textfile.
  • Plan module. It uses the submenu for places. It should build the submenu from places in the database and assign the right shortcut to them.

I have started the implementation but I'm not sure if I am on the right track. It is important that the data in ActionManager is in sync when the list of places is edited in the datamodule. I'm not sure how to add, edit delete actions in the ActionManager in runtime. I have only done this in designtime.

Other comments and possible improvements are welcome!

Regards

From stackoverflow
  • Adding or removing action at runtime is a bit backwards. You create a new TContainedAction, then set its ActionList property to your list. To remove it from the list, either set ActionList := nil, or .Free the action.

    Everything's done from the action, not the list. You can get the actions from the list with the list's Actions property, or enumerate the list if you're using a Delphi version that supports for..in loops.

    You can get more information by examining the source in actnlist.pas. It's not that big and compilcated; only 1162 lines for the D2009 version.

0 comments:

Post a Comment