Using a control state to issue commands

When you create a menu item, you need to define both the appearance (for example, a label or icon) and the operation represented by the item. In many cases, the programming operation represented by a menu item is to issue a command against the currently selected data or to add new data to the model.

In Step 3, you saw how to issue a command against a target selection using a command binding. When a user selected a tile, the TTilesView MouseDown function created the TTilesSelection and immediately issued a TChangeColorCommand against it. In Step 4, you saw how to encapsulate the user's selection and maintain it as the current selection, so that the user could first make a selection and then decide how to operate on the selected data.

In this step you create a control state which encapsulates the operation represented by a menu item. When the user selects a menu item, the control state's Select function causes the corresponding command to be issued against the current selection.

The Presentation framework provides a template class, TCommandControlStateOn<ATarget>, that you can use as is for a momentary control state that, when selected, initiates a command on the current selection. This Tiles menu uses this control state to issue commands--TChangeColorCommand and TCreateTileCommand--against the current TTilesSelection.

At construction, TCommandControlStateOn<TTilesSelection> requires a prototype of the command to initiate and a reference to the GUI bundle for the document component so it can access the current selection. When a user activates a control based on this control state, the Select function:

  1. Gets the current selection from the GUI bundle
  2. Clones the command prototype and binds it to the selection with a TGUIDocumentComponentCommandBindingTo<TTilesSelection>
  3. Requests the document to adopt and execute the command binding
In addition, the control state receives notification whenever the current selection changes and uses it to determine whether the menu item should be enabled to act on the new selection.

For example, this diagram shows how TCommandControlStateOn<TTilesSelection> binds a TChangeColorCommand to a TTilesSelection:


NOTE The TCreateTileCommand, described in "Implementing TCreateTileCommand" on page 90, also gets bound to a TTilesSelection using this control state.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker