Enabling and disabling command items is as simple as calling the TCommandEnabler::Enable() function in your handler function.
You decide the criteria for enabling and disabling a particular item. For example, if a particular library is not available, you might want to disable any commands that access that library. If your application handles files in a number of different formats, you might want to disable commands that are not appropriate to the current format.
To enable or disable a command,
- Add the command-enabling handler function and response table macro to your window class (as shown in Handling command-enablingmessages)."
-# Define the handler function.
-# Inside the handler function, call the Enable member function of the
command-enabling object passed into the handler function. The Enable function
takes a single bool parameter. Call Enable with the value of the parameter set
either to true to enable the command or to false to disable the command.
The following sample class has a bool flag, IsDirty, to tell if the file has
been modified since it was opened or last saved, and a CeFileSave function to
enable and disable the File|Save command:
@code
class TMyFrame : public
TFrameWindow
{
public:
TMyFrame(TWindow *parent = 0, char *title = 0)
:
TFrameWindow(parent, title), IsDirty(false) {}
protected:
bool IsDirty;
void CmFileSave();
// This is the command-enabling handler function.
void
CeFileSave(TCommandEnabler& commandEnabler);
DECLARE_RESPONSE_TABLE(TMyFrame);
};
DEFINE_RESPONSE_TABLE(TMyFrame)
EV_COMMAND(CM_FILESAVE, CmFileSave),
EV_COMMAND_ENABLE(CM_FILESAVE, CeFileSave),
END_RESPONSE_TABLE;
void
TMyFrame::CeFileSave(TCommandEnabler& ce)
{
ce.Enable(IsDirty);
}
\endcode
CeFileSave checks the IsDirty flag. If IsDirty is false (the file has not been
modified), it disables the CmFileSave command by calling Enable, passing false
as the parameter. If IsDirty is true (the file has been modified), CeFileSave
enables the CmFileSave command, passing true as the parameter. Because you want
to call Enable with the true parameter when IsDirty is true and vice versa, you
can just pass IsDirty as the parameter to Enable.
This method of enabling and disabling a command works for both menu items and
button gadgets. In the preceding example, if you have both a control bar button
and a menu item that send the CM_FILESAVE command, both commands are implemented
in the CmFileSave function. Similarly, command enabling for the control bar
button and the menu item is implemented in the CeFileSave function.
@section seealso See Also
- \ref handlingcommandenablingmessages "Handling command-enabling messages"
- \ref changingmenuitemtext "Changing menu item text"
- \ref togglingcommanditems "Toggling command items"