[ Previous | Next | Contents | Glossary | Home | Search ]
Motif 2.1 Widget Writer's Guide

Why Use Traits?

The trait mechanism provides an easy way for two widgets to communicate with each other. Frequently, the two communicating widgets will be a parent and its child; however, unrelated widgets can also communicate through traits. This communication takes two forms:

  1. Widgets can ask each other about specific capabilities.

  2. Widgets can access each other's trait methods. Trait methods are defined in a trait structure variable rather than in a class record. (Trait structure variables are described later in this chapter.)

    For example, consider a Motif application that needs to create a menu. To create the menu, the Motif application will need at least one XmRowColumn manager and some appropriate menu children. The XmRowColumn widget needs some way to ask each of its children if they are suitable menu children. The XmQTmenuSavvy trait provides a way. A child widget holding the XmQTmenuSavvy trait is announcing to a menu parent (XmRowColumn) that it is capable of becoming a menu child. Therefore, the menu parent (XmRowColumn) can ask each child if it holds this trait. If a child does not hold this trait, XmRowColumn can issue a warning that the child is not an acceptable menu child.

    The XmQTmenuSavvy trait provides several trait methods. One of these trait methods, disableCallback, enables or disables the activate callback method associated with a menu child. Without a trait mechanism, there would be no way for the XmRowColumn widget to call disableCallback. After all, XmRowColumn shares no common ancestry with any primitive widgets. However, since disableCallback is a trait method, XmRowColumn can call disableCallback.

  3. [ Previous | Next | Contents | Glossary | Home | Search ]