OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
Enabling and disabling command items

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,

  1. Add the command-enabling handler function and response table macro to your window class (as shown in Handling command-enablingmessages).
  2. Define the handler function.
  3. 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:

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);
};
EV_COMMAND(CM_FILESAVE, CmFileSave),
EV_COMMAND_ENABLE(CM_FILESAVE, CeFileSave),
void
TMyFrame::CeFileSave(TCommandEnabler& ce)
{
ce.Enable(IsDirty);
}
#define DECLARE_RESPONSE_TABLE(cls)
Definition eventhan.h:436
#define END_RESPONSE_TABLE
Definition eventhan.h:466
#define DEFINE_RESPONSE_TABLE(cls)
Macro to define a response table for a class with no base response tables.
Definition eventhan.h:479
#define EV_COMMAND_ENABLE(id, method)
Response table entry for enabling a command.
Definition windowev.h:193
#define EV_COMMAND(id, method)
Response table entry for a menu/accelerator/push button message.
Definition windowev.h:171

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.

See Also