TTextMenu builds a standard menu with text editing commands, as described in "Default menus" on page 89, and can be used with either document components or lightweight components. TDocumentTextMenu adds a menu with commands that modify the document's presentation. TDocumentTextMenu uses a TGUIBundle to access the document component; it cannot be used with simple text elements.
You can use these classes as is to add standard menus to an editable text element. For example, the following code adds a menu to a simple text element, based on a TSimpleTextView textView
and launched in a TWindowGroup windowGroup
(see "Creating a simple editable text element" on page 98 for the code to create the component):
TTextMenu textMenu( textView ); TMenu* menu = new TMenu; menu->SetControlLayout( MControl::kTopToBottom ); textMenu.AddTextMenus( *menu ); TMenuWindow* menuWindow = new TMenuWindow( menu, TStandardText( "Text Menus" ) ); windowGroup.AdoptMenuWindow( menuWindow );
To build a subset of the standard menus, override the text menu AddTextMenus function so that it calls only the functions to build the menus you want. TTextMenu::AddTextMenus calls CreateCharacterMenu, CreateParagraphMenu, CreateTypingMenu, and CreateToolMenu. These functions in turn call other functions that build submenus for these menus. TDocumentTextMenu::AddTextMenus additionally calls CreateComponentMenu. See the online class descriptions for TTextMenu and TDocumentTextMenu for a complete list of the functions you can call to build your own text menu.
You can also add functions that build new menu items to your menu class--for example, using a command you create. TTextMenu provides two functions you can use to help you create menu items:
CreateSubMenuItem takes a menu and a label and returns a TSubMenuItem you can add to the main menu:
TSubMenuItem* CreateSubMenuItem( TMenu* subMenuToAdopt, const TText& labelText );
TMomentaryMenuItem* CreateCommandMenuItem( const TText& itemLabel, TCommandOn<MTextSelection> command, TTextLabel* adoptCommandLabel = NIL, MMenuItem::ESeparatorType = MMenuItem::kNone, Boolean updateSelection = FALSE );
MMenuItem* TMyTextMenu::CreateSizeMenu() { TMenu* sizeMenu = new TMenu; sizemenu->SetControlLayout( MControl::kTopToBottom ); sizemenu->SetItemLayout( MControl::kLeftToRight ); sizeMenu->AdoptLast( CreateCommandMenuItem( TStandardText("Fine print"), new TAddStylesCommand(TFontPointSizeStyle(6.0), NIL, MMenuItem::kBefore) ); sizeMenu->AdoptLast( CreateCommandMenuItem( TStandardText("Standard print"), new TAddStylesCommand(TFontPointSizeStyle(12.0)) ); sizeMenu-?AdoptLast( CreateCommandMenuItem( TStandardText("Title print"), new TAddStylesCommand(TFontPointSizeStyle(36.0)) ); TSubMenuItem *item = CreateSubMenuItem( sizemenu, TStandardText( "Size" ) ); return item; }
MMomentaryTextCommandState provides a control state for commands that act on a text selection. You construct an MMomentaryTextCommandState with pointers to the view for the text component and to an instance of TCommandOn<MTextSelection>. When a user selects the corresponding menu item, the control state gets the current text selection from the view and issues the command against it.
MMomentaryTextPresenterCommandState provides a control state for commands that act on a text presentation. You construct an MMomentaryTextPresenterCommandState with a reference to the presenter state for the text component and a pointer to an instance of TCommandOn<TTextPresenterStateSelection>. When the control state is selected, it issues the command against the presenter state. Note that you can use MMomentaryTextPresenterCommandState only with commands that act on editable text document components. For example, TDocumentTextMenu uses this command state, while TTextMenu does not.
NOTE See the manual Desktop Frameworks Concepts for more information on menus, controls, control states, and other related classes.