Bookstore

Recent

Author Topic: Enable/Disable control of controls/menu items  (Read 283 times)

wittbo

  • Full Member
  • ***
  • Posts: 106
Enable/Disable control of controls/menu items
« on: February 19, 2020, 09:27:54 pm »
I have a basic question about programming technique. What is the best place in the code to control the enabling/disabling of controls or menu items? Up to now I have always looked after a reaction to an event in any event handler to see if there is anything to change. But I think it is better to do this in a central place: in every window, or once per application? Maybe there is something like an OnIdle event in every form? What are your tips and experiences?
-wittbo-
MBAir with MacOS 10.14.6 / Lazarus 2.0.6
iMac with MacOS 10.13.6 / Lazarus 2.0.2

howardpc

  • Hero Member
  • *****
  • Posts: 3356
Re: Enable/Disable control of controls/menu items
« Reply #1 on: February 19, 2020, 09:47:14 pm »
What is the best place in the code to control the enabling/disabling of controls or menu items?
It depends entirely on the enable/disable requirements of each individual control or menu item.
Since Actions were introduced,  judicious use of TCustomAction descendants is often all you need to implement a centralised approach to changes which must be preciptated immediately by user choices, changes in file contents etc. at runtime.
OnIdle handlers are also useful, but if implemented unwisely can make an otherwise nimble GUI become rather sluggish and jerky.

lucamar

  • Hero Member
  • *****
  • Posts: 2572
Re: Enable/Disable control of controls/menu items
« Reply #2 on: February 19, 2020, 10:10:46 pm »
For main menu items I usually enable/disable them when they are going to be used, that is, when the user clicks the root item (say, the "File" item to set/reset "Save", "Save as..", etc.).

For controls, etc. it depends on what you're doing but I usually have a unique procedure/function which traverses all controls and enables/disables them by testing some condition, most of the times depending on their Tag value, which is used as a kind of "mask".

But Howard is completely right: everything depens heavily on what (and how) your applications needs to do.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.6/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

wittbo

  • Full Member
  • ***
  • Posts: 106
Re: Enable/Disable control of controls/menu items
« Reply #3 on: February 20, 2020, 09:39:30 am »
Up to now, I never have used actions, since I could not imagine what to do with it. So I will start reading the concept behind actions.

My opinion is, that I will need one central control routine for each form, which is active, when the form is active. This is especially important for MacOS applications, which have one menu for the whole application only. A typical use case is a master/detail form for any kind of master data to switch between view and edit mode.
-wittbo-
MBAir with MacOS 10.14.6 / Lazarus 2.0.6
iMac with MacOS 10.13.6 / Lazarus 2.0.2

devEric69

  • Sr. Member
  • ****
  • Posts: 254
Re: Enable/Disable control of controls/menu items
« Reply #4 on: February 20, 2020, 10:49:27 am »
The above given advices are excellent: use a TActionList by TForm, and create TActions.
This design pattern has been created to intelligently handle mediation between controls, by pushing the TForm's mediation a step further and smarter (by encoding code into unit actions - in each OnExecute event - which implies the creation of small methods \ features; and TAction allows to handle simply the management of enabled, visible, etc, of the forms's controls, through their OnUpdate event).

TActionList, which contains the created TActions, are two components that are easy to understand, and which save a lot of time when the User Interface becomes complex.
« Last Edit: February 20, 2020, 10:58:14 am by devEric69 »
use: Linux 64 bits (Ubuntu 18.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

wittbo

  • Full Member
  • ***
  • Posts: 106
Re: Enable/Disable control of controls/menu items
« Reply #5 on: February 20, 2020, 05:03:47 pm »
OK, Action Lists I understand now. My idea now is that in each form I define an actionlist that bundles actions on OnClick events that are identical for different controls. Then I only need to control the individual actions. To do this, I imagine one routine per form that checks whether control conditions have changed and sets Enable/Disable for the action accordingly. I could call this routine in the Application.OnIdle event or anywhere in the form code where I think a control condition has changed (something like invalidate only for enable/disable control).
-wittbo-
MBAir with MacOS 10.14.6 / Lazarus 2.0.6
iMac with MacOS 10.13.6 / Lazarus 2.0.2