UI state processing

I have a Windows application written using MFC. The on / off status of menu items depends on many conditions. For example, I should enable a menu item if condition A is fulfilled or if condition B is fulfilled, but should be disabled if both A AND B are TRUE at the same time. How do we simulate this in code? I think I should use some kind of state machine, but my state apparatus seems to contain too many states. What is the general way to solve such problems? Please note that the above was just an example, there will be much more such conditions. In addition, the ability to always maintain the menu and display an error message when the user clicks it does not exist, since I need to disable the menu.

Just to clarify, I'm not looking for how to disable menu items in MFC, what I'm looking for is the best way to decide if a menu item should be turned on / off when there are many interdependent states. participation.

+4
source share
3 answers

Try writing the updateUIStatus() method, which is called after each user interface action. This method will set the on or off state of the menu item (and any other component of the user interface) based on your conditions.

The good thing about having the only way to do all UI state updates is that you centralize all this logic in one place, instead of having multiple calls, like if (condition A && condition B) menu.setEnabled(true);

0
source

MFC has a built-in mechanism for enabling and disabling menu items in the form of a command line and ON_UPDATE_COMMAND_UI macro. For more information, see How to Update User Interface Objects and CCmdUI on MSDN.

You do not need a state machine. For each menu command, select where the command should be executed, for example, in your document, view or main frame class, then execute the OnUpdate handler and add the ON_UPDATE_COMMAND_UI message map ON_UPDATE_COMMAND_UI for the corresponding class.

As an example, consider the answer I gave to this question .

0
source

Look at the reseller template. It is designed to track such things.

from the corrugation:

Use the Mediator Template When

A set of objects communicates in well-defined but complex ways. The resulting interdependencies are unstructured and difficult to understand.

Reusing an object is difficult because it refers to and interacts with many other objects.

The behavior that is distributed between several classes should be customizable without a lot of subclasses.

0
source

All Articles